vt100.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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, char *c)
  107. {
  108. if (!vt100 || !vt100->istream || !c) {
  109. errno = ENOENT;
  110. return -1;
  111. }
  112. return read(fileno(vt100->istream), &c, 1);
  113. }
  114. int vt100_oflush(const vt100_t *vt100)
  115. {
  116. if (!vt100 || !vt100->ostream)
  117. return 0;
  118. return fflush(vt100->ostream);
  119. }
  120. int vt100_ierror(const vt100_t *vt100)
  121. {
  122. if (!vt100 || !vt100->istream)
  123. return 0;
  124. return ferror(vt100->istream);
  125. }
  126. int vt100_oerror(const vt100_t *vt100)
  127. {
  128. if (!vt100 || !vt100->ostream)
  129. return 0;
  130. return ferror(vt100->ostream);
  131. }
  132. int vt100_ieof(const vt100_t *vt100)
  133. {
  134. if (!vt100 || !vt100->istream)
  135. return 0;
  136. return feof(vt100->istream);
  137. }
  138. int vt100_eof(const vt100_t *vt100)
  139. {
  140. if (!vt100 || !vt100->istream)
  141. return 0;
  142. return feof(vt100->istream);
  143. }
  144. size_t vt100_width(const vt100_t *vt100)
  145. {
  146. #ifdef TIOCGWINSZ
  147. struct winsize ws = {};
  148. int res = 0;
  149. #endif
  150. const size_t default_width = 80;
  151. if(!vt100 || !vt100->ostream)
  152. return default_width;
  153. #ifdef TIOCGWINSZ
  154. ws.ws_col = 0;
  155. res = ioctl(fileno(vt100->ostream), TIOCGWINSZ, &ws);
  156. if (res || (0 == ws.ws_col))
  157. return default_width;
  158. return (size_t)ws.ws_col;
  159. #else
  160. return default_width;
  161. #endif
  162. }
  163. size_t vt100_height(const vt100_t *vt100)
  164. {
  165. #ifdef TIOCGWINSZ
  166. struct winsize ws = {};
  167. int res = 0;
  168. #endif
  169. const size_t default_height = 25;
  170. if(!vt100 || !vt100->ostream)
  171. return default_height;
  172. #ifdef TIOCGWINSZ
  173. ws.ws_row = 0;
  174. res = ioctl(fileno(vt100->ostream), TIOCGWINSZ, &ws);
  175. if (res || (0 == ws.ws_row))
  176. return default_height;
  177. return (size_t)ws.ws_row;
  178. #else
  179. return default_height;
  180. #endif
  181. }
  182. void vt100_ding(const vt100_t *vt100)
  183. {
  184. vt100_printf(vt100, "%c", KEY_BEL);
  185. vt100_oflush(vt100);
  186. }
  187. void vt100_attr_reset(const vt100_t *vt100)
  188. {
  189. vt100_printf(vt100, "%c[0m", KEY_ESC);
  190. }
  191. void vt100_attr_bright(const vt100_t *vt100)
  192. {
  193. vt100_printf(vt100, "%c[1m", KEY_ESC);
  194. }
  195. void vt100_attr_dim(const vt100_t *vt100)
  196. {
  197. vt100_printf(vt100, "%c[2m", KEY_ESC);
  198. }
  199. void vt100_attr_underscore(const vt100_t *vt100)
  200. {
  201. vt100_printf(vt100, "%c[4m", KEY_ESC);
  202. }
  203. void vt100_attr_blink(const vt100_t *vt100)
  204. {
  205. vt100_printf(vt100, "%c[5m", KEY_ESC);
  206. }
  207. void vt100_attr_reverse(const vt100_t *vt100)
  208. {
  209. vt100_printf(vt100, "%c[7m", KEY_ESC);
  210. }
  211. void vt100_attr_hidden(const vt100_t *vt100)
  212. {
  213. vt100_printf(vt100, "%c[8m", KEY_ESC);
  214. }
  215. void vt100_erase_line(const vt100_t *vt100)
  216. {
  217. vt100_printf(vt100, "%c[2K", KEY_ESC);
  218. }
  219. void vt100_clear_screen(const vt100_t *vt100)
  220. {
  221. vt100_printf(vt100, "%c[2J", KEY_ESC);
  222. }
  223. void vt100_cursor_save(const vt100_t *vt100)
  224. {
  225. vt100_printf(vt100, "%c7", KEY_ESC); // VT100
  226. // vt100_printf(vt100, "%c[s", KEY_ESC); // ANSI
  227. }
  228. void vt100_cursor_restore(const vt100_t *vt100)
  229. {
  230. vt100_printf(vt100, "%c8", KEY_ESC); // VT100
  231. // vt100_printf(vt100, "%c[u", KEY_ESC); // ANSI
  232. }
  233. void vt100_cursor_forward(const vt100_t *vt100, size_t count)
  234. {
  235. vt100_printf(vt100, "%c[%dC", KEY_ESC, count);
  236. }
  237. void vt100_cursor_back(const vt100_t *vt100, size_t count)
  238. {
  239. vt100_printf(vt100, "%c[%dD", KEY_ESC, count);
  240. }
  241. void vt100_cursor_up(const vt100_t *vt100, size_t count)
  242. {
  243. vt100_printf(vt100, "%c[%dA", KEY_ESC, count);
  244. }
  245. void vt100_cursor_down(const vt100_t *vt100, size_t count)
  246. {
  247. vt100_printf(vt100, "%c[%dB", KEY_ESC, count);
  248. }
  249. void vt100_scroll_up(const vt100_t *vt100)
  250. {
  251. vt100_printf(vt100, "%cD", KEY_ESC);
  252. }
  253. void vt100_scroll_down(const vt100_t *vt100)
  254. {
  255. vt100_printf(vt100, "%cM", KEY_ESC);
  256. }
  257. void vt100_next_line(const vt100_t *vt100)
  258. {
  259. vt100_printf(vt100, "%cE", KEY_ESC);
  260. }
  261. void vt100_cursor_home(const vt100_t *vt100)
  262. {
  263. vt100_printf(vt100, "%c[H", KEY_ESC);
  264. }
  265. void vt100_erase(const vt100_t *vt100, size_t count)
  266. {
  267. vt100_printf(vt100, "%c[%dP", KEY_ESC, count);
  268. }
  269. void vt100_erase_down(const vt100_t *vt100)
  270. {
  271. vt100_printf(vt100, "%c[J", KEY_ESC);
  272. }