vt100.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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. // Hotkeys
  36. const char *hotkey_map[] = {
  37. "^@", /* 0 Null character */
  38. "^A", /* 1 Start of heading, = console interrupt */
  39. "^B", /* 2 Start of text, maintenance mode on HP console */
  40. "^C", /* 3 End of text */
  41. "^D", /* 4 End of transmission, not the same as ETB */
  42. "^E", /* 5 Enquiry, goes with ACK; old HP flow control */
  43. "^F", /* 6 Acknowledge, clears ENQ logon hand */
  44. "^G", /* 7 Bell, rings the bell... */
  45. "^H", /* 8 Backspace, works on HP terminals/computers */
  46. "^I", /* 9 Horizontal tab, move to next tab stop */
  47. "^J", /* 10 Line Feed */
  48. "^K", /* 11 Vertical tab */
  49. "^L", /* 12 Form Feed, page eject */
  50. "^M", /* 13 Carriage Return*/
  51. "^N", /* 14 Shift Out, alternate character set */
  52. "^O", /* 15 Shift In, resume defaultn character set */
  53. "^P", /* 16 Data link escape */
  54. "^Q", /* 17 XON, with XOFF to pause listings; "okay to send". */
  55. "^R", /* 18 Device control 2, block-mode flow control */
  56. "^S", /* 19 XOFF, with XON is TERM=18 flow control */
  57. "^T", /* 20 Device control 4 */
  58. "^U", /* 21 Negative acknowledge */
  59. "^V", /* 22 Synchronous idle */
  60. "^W", /* 23 End transmission block, not the same as EOT */
  61. "^X", /* 24 Cancel line, MPE echoes !!! */
  62. "^Y", /* 25 End of medium, Control-Y interrupt */
  63. "^Z", /* 26 Substitute */
  64. "^[", /* 27 Escape, next character is not echoed */
  65. "^\\", /* 28 File separator */
  66. "^]", /* 29 Group separator */
  67. "^^", /* 30 Record separator, block-mode terminator */
  68. "^_", /* 31 Unit separator */
  69. NULL
  70. };
  71. vt100_t *vt100_new(FILE *istream, FILE *ostream)
  72. {
  73. vt100_t *vt100 = NULL;
  74. vt100 = malloc(sizeof(vt100_t));
  75. if (!vt100)
  76. return NULL;
  77. // Initialize
  78. vt100->istream = istream;
  79. vt100->ostream = ostream;
  80. return vt100;
  81. }
  82. void vt100_free(vt100_t *vt100)
  83. {
  84. free(vt100);
  85. }
  86. FILE *vt100_istream(const vt100_t *vt100)
  87. {
  88. if (!vt100)
  89. return NULL;
  90. return vt100->istream;
  91. }
  92. void vt100_set_istream(vt100_t *vt100, FILE *istream)
  93. {
  94. if (!vt100)
  95. return;
  96. vt100->istream = istream;
  97. }
  98. FILE *vt100_ostream(const vt100_t *vt100)
  99. {
  100. if (!vt100)
  101. return NULL;
  102. return vt100->ostream;
  103. }
  104. void vt100_set_ostream(vt100_t *vt100, FILE *ostream)
  105. {
  106. if (!vt100)
  107. return;
  108. vt100->ostream = ostream;
  109. }
  110. vt100_esc_e vt100_esc_decode(const char *esc_seq)
  111. {
  112. vt100_esc_e result = VT100_UNKNOWN;
  113. unsigned int i = 0;
  114. for (i = 0; i < (sizeof(esc_map) / sizeof(vt100_decode_t)); i++) {
  115. if (strcmp(esc_map[i].sequence, esc_seq))
  116. continue;
  117. result = esc_map[i].code;
  118. break;
  119. }
  120. return result;
  121. }
  122. ssize_t vt100_hotkey_decode(const char *hotkey)
  123. {
  124. unsigned int i = 0;
  125. if (!hotkey)
  126. return -1;
  127. while (hotkey_map[i]) {
  128. if (strcmp(hotkey_map[i], hotkey) == 0)
  129. return i;
  130. i++;
  131. }
  132. return -1;
  133. }
  134. int vt100_printf(const vt100_t *vt100, const char *fmt, ...)
  135. {
  136. va_list args;
  137. int len = 0;
  138. // If ostream is not set don't consider it as a error. Consider it
  139. // as a printf to NULL.
  140. if (!vt100 || !vt100->ostream)
  141. return 0;
  142. va_start(args, fmt);
  143. len = vt100_vprintf(vt100, fmt, args);
  144. va_end(args);
  145. return len;
  146. }
  147. int vt100_vprintf(const vt100_t *vt100, const char *fmt, va_list args)
  148. {
  149. if (!vt100 || !vt100->ostream)
  150. return 0;
  151. return vfprintf(vt100->ostream, fmt, args);
  152. }
  153. int vt100_getchar(const vt100_t *vt100, unsigned char *c)
  154. {
  155. if (!vt100 || !vt100->istream || !c) {
  156. errno = ENOENT;
  157. return -1;
  158. }
  159. return read(fileno(vt100->istream), c, 1);
  160. }
  161. int vt100_oflush(const vt100_t *vt100)
  162. {
  163. if (!vt100 || !vt100->ostream)
  164. return 0;
  165. return fflush(vt100->ostream);
  166. }
  167. int vt100_ierror(const vt100_t *vt100)
  168. {
  169. if (!vt100 || !vt100->istream)
  170. return 0;
  171. return ferror(vt100->istream);
  172. }
  173. int vt100_oerror(const vt100_t *vt100)
  174. {
  175. if (!vt100 || !vt100->ostream)
  176. return 0;
  177. return ferror(vt100->ostream);
  178. }
  179. int vt100_ieof(const vt100_t *vt100)
  180. {
  181. if (!vt100 || !vt100->istream)
  182. return 0;
  183. return feof(vt100->istream);
  184. }
  185. int vt100_eof(const vt100_t *vt100)
  186. {
  187. if (!vt100 || !vt100->istream)
  188. return 0;
  189. return feof(vt100->istream);
  190. }
  191. void vt100_winsize(const vt100_t *vt100, size_t *width, size_t *height)
  192. {
  193. #ifdef TIOCGWINSZ
  194. struct winsize ws = {};
  195. int res = 0;
  196. #endif
  197. size_t w = 80;
  198. size_t h = 25;
  199. #ifdef TIOCGWINSZ
  200. if(vt100 && vt100->ostream && isatty(fileno(vt100->ostream))) {
  201. res = ioctl(fileno(vt100->ostream), TIOCGWINSZ, &ws);
  202. if (!res) {
  203. if (0 != ws.ws_col)
  204. w = (size_t)ws.ws_col;
  205. if (0 != ws.ws_row)
  206. h = (size_t)ws.ws_row;
  207. }
  208. }
  209. #endif
  210. if (width)
  211. *width = w;
  212. if (height)
  213. *height = h;
  214. }
  215. size_t vt100_width(const vt100_t *vt100)
  216. {
  217. size_t w = 0;
  218. vt100_winsize(vt100, &w, NULL);
  219. return w;
  220. }
  221. size_t vt100_height(const vt100_t *vt100)
  222. {
  223. size_t h = 0;
  224. vt100_winsize(vt100, NULL, &h);
  225. return h;
  226. }
  227. void vt100_ding(const vt100_t *vt100)
  228. {
  229. vt100_printf(vt100, "%c", KEY_BEL);
  230. vt100_oflush(vt100);
  231. }
  232. void vt100_attr_reset(const vt100_t *vt100)
  233. {
  234. vt100_printf(vt100, "%c[0m", KEY_ESC);
  235. }
  236. void vt100_attr_bright(const vt100_t *vt100)
  237. {
  238. vt100_printf(vt100, "%c[1m", KEY_ESC);
  239. }
  240. void vt100_attr_dim(const vt100_t *vt100)
  241. {
  242. vt100_printf(vt100, "%c[2m", KEY_ESC);
  243. }
  244. void vt100_attr_underscore(const vt100_t *vt100)
  245. {
  246. vt100_printf(vt100, "%c[4m", KEY_ESC);
  247. }
  248. void vt100_attr_blink(const vt100_t *vt100)
  249. {
  250. vt100_printf(vt100, "%c[5m", KEY_ESC);
  251. }
  252. void vt100_attr_reverse(const vt100_t *vt100)
  253. {
  254. vt100_printf(vt100, "%c[7m", KEY_ESC);
  255. }
  256. void vt100_attr_hidden(const vt100_t *vt100)
  257. {
  258. vt100_printf(vt100, "%c[8m", KEY_ESC);
  259. }
  260. void vt100_erase_line(const vt100_t *vt100)
  261. {
  262. vt100_printf(vt100, "%c[2K", KEY_ESC);
  263. }
  264. void vt100_clear_screen(const vt100_t *vt100)
  265. {
  266. vt100_printf(vt100, "%c[2J", KEY_ESC);
  267. }
  268. void vt100_cursor_save(const vt100_t *vt100)
  269. {
  270. vt100_printf(vt100, "%c7", KEY_ESC); // VT100
  271. // vt100_printf(vt100, "%c[s", KEY_ESC); // ANSI
  272. }
  273. void vt100_cursor_restore(const vt100_t *vt100)
  274. {
  275. vt100_printf(vt100, "%c8", KEY_ESC); // VT100
  276. // vt100_printf(vt100, "%c[u", KEY_ESC); // ANSI
  277. }
  278. void vt100_cursor_forward(const vt100_t *vt100, size_t count)
  279. {
  280. vt100_printf(vt100, "%c[%dC", KEY_ESC, count);
  281. }
  282. void vt100_cursor_back(const vt100_t *vt100, size_t count)
  283. {
  284. vt100_printf(vt100, "%c[%dD", KEY_ESC, count);
  285. }
  286. void vt100_cursor_up(const vt100_t *vt100, size_t count)
  287. {
  288. vt100_printf(vt100, "%c[%dA", KEY_ESC, count);
  289. }
  290. void vt100_cursor_down(const vt100_t *vt100, size_t count)
  291. {
  292. vt100_printf(vt100, "%c[%dB", KEY_ESC, count);
  293. }
  294. void vt100_scroll_up(const vt100_t *vt100)
  295. {
  296. vt100_printf(vt100, "%cD", KEY_ESC);
  297. }
  298. void vt100_scroll_down(const vt100_t *vt100)
  299. {
  300. vt100_printf(vt100, "%cM", KEY_ESC);
  301. }
  302. void vt100_next_line(const vt100_t *vt100)
  303. {
  304. vt100_printf(vt100, "%cE", KEY_ESC);
  305. }
  306. void vt100_cursor_home(const vt100_t *vt100)
  307. {
  308. vt100_printf(vt100, "%c[H", KEY_ESC);
  309. }
  310. void vt100_erase(const vt100_t *vt100, size_t count)
  311. {
  312. vt100_printf(vt100, "%c[%dP", KEY_ESC, count);
  313. }
  314. void vt100_erase_down(const vt100_t *vt100)
  315. {
  316. vt100_printf(vt100, "%c[J", KEY_ESC);
  317. }