vt100.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include <fcntl.h>
  5. #include <string.h>
  6. #include <sys/ioctl.h>
  7. #include <sys/time.h>
  8. #include <sys/types.h>
  9. #include <sys/select.h>
  10. #include <errno.h>
  11. #include <tinyrl/vt100.h>
  12. struct vt100_s {
  13. FILE *istream;
  14. FILE *ostream;
  15. };
  16. typedef struct {
  17. const char *sequence;
  18. vt100_esc_e code;
  19. } vt100_decode_t;
  20. // This table maps the vt100 escape codes to an enumeration
  21. static vt100_decode_t esc_map[] = {
  22. {"[A", VT100_CURSOR_UP},
  23. {"[B", VT100_CURSOR_DOWN},
  24. {"[C", VT100_CURSOR_RIGHT},
  25. {"[D", VT100_CURSOR_LEFT},
  26. {"[H", VT100_HOME},
  27. {"[1~", VT100_HOME},
  28. {"[F", VT100_END},
  29. {"[4~", VT100_END},
  30. {"[2~", VT100_INSERT},
  31. {"[3~", VT100_DELETE},
  32. {"[5~", VT100_PGUP},
  33. {"[6~", VT100_PGDOWN},
  34. };
  35. vt100_t *vt100_new(FILE *istream, FILE *ostream)
  36. {
  37. vt100_t *vt100 = NULL;
  38. vt100 = malloc(sizeof(vt100_t));
  39. if (!vt100)
  40. return NULL;
  41. // Initialize
  42. vt100->istream = istream;
  43. vt100->ostream = ostream;
  44. return vt100;
  45. }
  46. void vt100_free(vt100_t *vt100)
  47. {
  48. free(vt100);
  49. }
  50. FILE *vt100_istream(const vt100_t *vt100)
  51. {
  52. if (!vt100)
  53. return NULL;
  54. return vt100->istream;
  55. }
  56. void vt100_set_istream(vt100_t *vt100, FILE *istream)
  57. {
  58. if (!vt100)
  59. return;
  60. vt100->istream = istream;
  61. }
  62. FILE *vt100_ostream(const vt100_t *vt100)
  63. {
  64. if (!vt100)
  65. return NULL;
  66. return vt100->ostream;
  67. }
  68. void vt100_set_ostream(vt100_t *vt100, FILE *ostream)
  69. {
  70. if (!vt100)
  71. return;
  72. vt100->ostream = ostream;
  73. }
  74. vt100_esc_e vt100_esc_decode(const vt100_t *vt100, const char *esc_seq)
  75. {
  76. vt100_esc_e result = VT100_UNKNOWN;
  77. unsigned int i = 0;
  78. for (i = 0; i < (sizeof(esc_map) / sizeof(vt100_decode_t)); i++) {
  79. if (strcmp(esc_map[i].sequence, esc_seq))
  80. continue;
  81. result = esc_map[i].code;
  82. break;
  83. }
  84. vt100 = vt100; // Happy compiler
  85. return result;
  86. }
  87. int vt100_printf(const vt100_t *vt100, const char *fmt, ...)
  88. {
  89. va_list args;
  90. int len = 0;
  91. // If ostream is not set don't consider it as a error. Consider it
  92. // as a printf to NULL.
  93. if (!vt100 || !vt100->ostream)
  94. return 0;
  95. va_start(args, fmt);
  96. len = vt100_vprintf(vt100, fmt, args);
  97. va_end(args);
  98. return len;
  99. }
  100. int vt100_vprintf(const vt100_t *vt100, const char *fmt, va_list args)
  101. {
  102. if (!vt100 || !vt100->ostream)
  103. return 0;
  104. return vfprintf(vt100->ostream, fmt, args);
  105. }
  106. int vt100_getchar(const vt100_t *vt100)
  107. {
  108. unsigned char c = 0;
  109. ssize_t res = VT100_RET_ERR;
  110. if (!vt100 || !vt100->istream)
  111. return VT100_RET_ERR;
  112. res = read(fileno(vt100->istream), &c, 1);
  113. if (res < 0) {
  114. if (EAGAIN == errno)
  115. return VT100_RET_EMPTY; // Non-blocking read
  116. return VT100_RET_ERR;
  117. }
  118. if (0 == res)
  119. return VT100_RET_EOF;
  120. return c;
  121. }
  122. int vt100_oflush(const vt100_t *vt100)
  123. {
  124. if (!vt100 || !vt100->ostream)
  125. return 0;
  126. return fflush(vt100->ostream);
  127. }
  128. int vt100_ierror(const vt100_t *vt100)
  129. {
  130. if (!vt100 || !vt100->istream)
  131. return 0;
  132. return ferror(vt100->istream);
  133. }
  134. int vt100_oerror(const vt100_t *vt100)
  135. {
  136. if (!vt100 || !vt100->ostream)
  137. return 0;
  138. return ferror(vt100->ostream);
  139. }
  140. int vt100_ieof(const vt100_t *vt100)
  141. {
  142. if (!vt100 || !vt100->istream)
  143. return 0;
  144. return feof(vt100->istream);
  145. }
  146. int vt100_eof(const vt100_t *vt100)
  147. {
  148. if (!vt100 || !vt100->istream)
  149. return 0;
  150. return feof(vt100->istream);
  151. }
  152. size_t vt100_width(const vt100_t *vt100)
  153. {
  154. #ifdef TIOCGWINSZ
  155. struct winsize ws = {};
  156. int res = 0;
  157. #endif
  158. const size_t default_width = 80;
  159. if(!vt100 || !vt100->ostream)
  160. return default_width;
  161. #ifdef TIOCGWINSZ
  162. ws.ws_col = 0;
  163. res = ioctl(fileno(vt100->ostream), TIOCGWINSZ, &ws);
  164. if (res || (0 == ws.ws_col))
  165. return default_width;
  166. return (size_t)ws.ws_col;
  167. #else
  168. return default_width;
  169. #endif
  170. }
  171. size_t vt100_height(const vt100_t *vt100)
  172. {
  173. #ifdef TIOCGWINSZ
  174. struct winsize ws = {};
  175. int res = 0;
  176. #endif
  177. const size_t default_height = 25;
  178. if(!vt100 || !vt100->ostream)
  179. return default_height;
  180. #ifdef TIOCGWINSZ
  181. ws.ws_row = 0;
  182. res = ioctl(fileno(vt100->ostream), TIOCGWINSZ, &ws);
  183. if (res || (0 == ws.ws_row))
  184. return default_height;
  185. return (size_t)ws.ws_row;
  186. #else
  187. return default_height;
  188. #endif
  189. }
  190. void vt100_ding(const vt100_t *vt100)
  191. {
  192. vt100_printf(vt100, "%c", KEY_BEL);
  193. vt100_oflush(vt100);
  194. }
  195. void vt100_attr_reset(const vt100_t *vt100)
  196. {
  197. vt100_printf(vt100, "%c[0m", KEY_ESC);
  198. }
  199. void vt100_attr_bright(const vt100_t *vt100)
  200. {
  201. vt100_printf(vt100, "%c[1m", KEY_ESC);
  202. }
  203. void vt100_attr_dim(const vt100_t *vt100)
  204. {
  205. vt100_printf(vt100, "%c[2m", KEY_ESC);
  206. }
  207. void vt100_attr_underscore(const vt100_t *vt100)
  208. {
  209. vt100_printf(vt100, "%c[4m", KEY_ESC);
  210. }
  211. void vt100_attr_blink(const vt100_t *vt100)
  212. {
  213. vt100_printf(vt100, "%c[5m", KEY_ESC);
  214. }
  215. void vt100_attr_reverse(const vt100_t *vt100)
  216. {
  217. vt100_printf(vt100, "%c[7m", KEY_ESC);
  218. }
  219. void vt100_attr_hidden(const vt100_t *vt100)
  220. {
  221. vt100_printf(vt100, "%c[8m", KEY_ESC);
  222. }
  223. void vt100_erase_line(const vt100_t *vt100)
  224. {
  225. vt100_printf(vt100, "%c[2K", KEY_ESC);
  226. }
  227. void vt100_clear_screen(const vt100_t *vt100)
  228. {
  229. vt100_printf(vt100, "%c[2J", KEY_ESC);
  230. }
  231. void vt100_cursor_save(const vt100_t *vt100)
  232. {
  233. vt100_printf(vt100, "%c7", KEY_ESC); // VT100
  234. // vt100_printf(vt100, "%c[s", KEY_ESC); // ANSI
  235. }
  236. void vt100_cursor_restore(const vt100_t *vt100)
  237. {
  238. vt100_printf(vt100, "%c8", KEY_ESC); // VT100
  239. // vt100_printf(vt100, "%c[u", KEY_ESC); // ANSI
  240. }
  241. void vt100_cursor_forward(const vt100_t *vt100, size_t count)
  242. {
  243. vt100_printf(vt100, "%c[%dC", KEY_ESC, count);
  244. }
  245. void vt100_cursor_back(const vt100_t *vt100, size_t count)
  246. {
  247. vt100_printf(vt100, "%c[%dD", KEY_ESC, count);
  248. }
  249. void vt100_cursor_up(const vt100_t *vt100, size_t count)
  250. {
  251. vt100_printf(vt100, "%c[%dA", KEY_ESC, count);
  252. }
  253. void vt100_cursor_down(const vt100_t *vt100, size_t count)
  254. {
  255. vt100_printf(vt100, "%c[%dB", KEY_ESC, count);
  256. }
  257. void vt100_scroll_up(const vt100_t *vt100)
  258. {
  259. vt100_printf(vt100, "%cD", KEY_ESC);
  260. }
  261. void vt100_scroll_down(const vt100_t *vt100)
  262. {
  263. vt100_printf(vt100, "%cM", KEY_ESC);
  264. }
  265. void vt100_next_line(const vt100_t *vt100)
  266. {
  267. vt100_printf(vt100, "%cE", KEY_ESC);
  268. }
  269. void vt100_cursor_home(const vt100_t *vt100)
  270. {
  271. vt100_printf(vt100, "%c[H", KEY_ESC);
  272. }
  273. void vt100_erase(const vt100_t *vt100, size_t count)
  274. {
  275. vt100_printf(vt100, "%c[%dP", KEY_ESC, count);
  276. }
  277. void vt100_erase_down(const vt100_t *vt100)
  278. {
  279. vt100_printf(vt100, "%c[J", KEY_ESC);
  280. }