vt100.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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 {
  13. FILE *istream;
  14. FILE *ostream;
  15. int timeout; /* Input timeout in seconds */
  16. };
  17. #include "private.h"
  18. typedef struct {
  19. const char* sequence;
  20. vt100_escape_e code;
  21. } vt100_decode_t;
  22. /* This table maps the vt100 escape codes to an enumeration */
  23. static vt100_decode_t cmds[] = {
  24. {"[A", vt100_CURSOR_UP},
  25. {"[B", vt100_CURSOR_DOWN},
  26. {"[C", vt100_CURSOR_RIGHT},
  27. {"[D", vt100_CURSOR_LEFT},
  28. {"[H", vt100_HOME},
  29. {"[1~", vt100_HOME},
  30. {"[F", vt100_END},
  31. {"[4~", vt100_END},
  32. {"[2~", vt100_INSERT},
  33. {"[3~", vt100_DELETE},
  34. {"[5~", vt100_PGUP},
  35. {"[6~", vt100_PGDOWN},
  36. };
  37. /*--------------------------------------------------------- */
  38. vt100_escape_e vt100_escape_decode(const vt100_t *this,
  39. const char *esc_seq)
  40. {
  41. vt100_escape_e result = vt100_UNKNOWN;
  42. unsigned int i;
  43. /* Decode the sequence to macros */
  44. for (i = 0; i < (sizeof(cmds) / sizeof(vt100_decode_t)); i++) {
  45. if (strcmp(cmds[i].sequence, esc_seq))
  46. continue;
  47. result = cmds[i].code;
  48. break;
  49. }
  50. this = this; /* Happy compiler */
  51. return result;
  52. }
  53. /*-------------------------------------------------------- */
  54. int vt100_printf(const vt100_t * this, const char *fmt, ...)
  55. {
  56. va_list args;
  57. int len;
  58. if (!this->ostream)
  59. return 0;
  60. va_start(args, fmt);
  61. len = vt100_vprintf(this, fmt, args);
  62. va_end(args);
  63. return len;
  64. }
  65. /*-------------------------------------------------------- */
  66. int
  67. vt100_vprintf(const vt100_t * this, const char *fmt, va_list args)
  68. {
  69. if (!this->ostream)
  70. return 0;
  71. return vfprintf(this->ostream, fmt, args);
  72. }
  73. /*-------------------------------------------------------- */
  74. int vt100_getchar(const vt100_t *this)
  75. {
  76. unsigned char c;
  77. int istream_fd;
  78. fd_set rfds;
  79. struct timeval tv;
  80. int retval;
  81. ssize_t res;
  82. if (!this->istream)
  83. return VT100_ERR;
  84. istream_fd = fileno(this->istream);
  85. /* Just wait for the input if no timeout */
  86. if (this->timeout <= 0) {
  87. while (((res = read(istream_fd, &c, 1)) < 0) &&
  88. (EAGAIN == errno));
  89. /* EOF or error */
  90. if (res < 0)
  91. return VT100_ERR;
  92. if (!res)
  93. return VT100_EOF;
  94. return c;
  95. }
  96. /* Set timeout for the select() */
  97. FD_ZERO(&rfds);
  98. FD_SET(istream_fd, &rfds);
  99. tv.tv_sec = this->timeout;
  100. tv.tv_usec = 0;
  101. while (((retval = select(istream_fd + 1, &rfds, NULL, NULL, &tv)) < 0) &&
  102. (EAGAIN == errno));
  103. /* Error or timeout */
  104. if (retval < 0)
  105. return VT100_ERR;
  106. if (!retval)
  107. return VT100_TIMEOUT;
  108. res = read(istream_fd, &c, 1);
  109. /* EOF or error */
  110. if (res < 0)
  111. return VT100_ERR;
  112. if (!res)
  113. return VT100_EOF;
  114. return c;
  115. }
  116. /*-------------------------------------------------------- */
  117. int vt100_oflush(const vt100_t * this)
  118. {
  119. if (!this->ostream)
  120. return 0;
  121. return fflush(this->ostream);
  122. }
  123. /*-------------------------------------------------------- */
  124. int vt100_ierror(const vt100_t * this)
  125. {
  126. if (!this->istream)
  127. return 0;
  128. return ferror(this->istream);
  129. }
  130. /*-------------------------------------------------------- */
  131. int vt100_oerror(const vt100_t * this)
  132. {
  133. if (!this->ostream)
  134. return 0;
  135. return ferror(this->ostream);
  136. }
  137. /*-------------------------------------------------------- */
  138. int vt100_ieof(const vt100_t * this)
  139. {
  140. if (!this->istream)
  141. return 0;
  142. return feof(this->istream);
  143. }
  144. /*-------------------------------------------------------- */
  145. int vt100_eof(const vt100_t * this)
  146. {
  147. if (!this->istream)
  148. return 0;
  149. return feof(this->istream);
  150. }
  151. /*-------------------------------------------------------- */
  152. unsigned int vt100__get_width(const vt100_t *this)
  153. {
  154. #ifdef TIOCGWINSZ
  155. struct winsize ws;
  156. int res;
  157. #endif
  158. if(!this->ostream)
  159. return 80;
  160. #ifdef TIOCGWINSZ
  161. ws.ws_col = 0;
  162. res = ioctl(fileno(this->ostream), TIOCGWINSZ, &ws);
  163. if (res || !ws.ws_col)
  164. return 80;
  165. return ws.ws_col;
  166. #else
  167. return 80;
  168. #endif
  169. }
  170. /*-------------------------------------------------------- */
  171. unsigned int vt100__get_height(const vt100_t *this)
  172. {
  173. #ifdef TIOCGWINSZ
  174. struct winsize ws;
  175. int res;
  176. #endif
  177. if(!this->ostream)
  178. return 25;
  179. #ifdef TIOCGWINSZ
  180. ws.ws_row = 0;
  181. res = ioctl(fileno(this->ostream), TIOCGWINSZ, &ws);
  182. if (res || !ws.ws_row)
  183. return 25;
  184. return ws.ws_row;
  185. #else
  186. return 25;
  187. #endif
  188. }
  189. /*-------------------------------------------------------- */
  190. static void
  191. vt100_init(vt100_t * this, FILE * istream, FILE * ostream)
  192. {
  193. this->istream = istream;
  194. this->ostream = ostream;
  195. this->timeout = -1; /* No timeout by default */
  196. }
  197. /*-------------------------------------------------------- */
  198. static void vt100_fini(vt100_t * this)
  199. {
  200. /* nothing to do yet... */
  201. this = this;
  202. }
  203. /*-------------------------------------------------------- */
  204. vt100_t *vt100_new(FILE * istream, FILE * ostream)
  205. {
  206. vt100_t *this = NULL;
  207. this = malloc(sizeof(vt100_t));
  208. if (this) {
  209. vt100_init(this, istream, ostream);
  210. }
  211. return this;
  212. }
  213. /*-------------------------------------------------------- */
  214. void vt100_delete(vt100_t * this)
  215. {
  216. vt100_fini(this);
  217. /* release the memory */
  218. free(this);
  219. }
  220. /*-------------------------------------------------------- */
  221. void vt100_ding(const vt100_t * this)
  222. {
  223. vt100_printf(this, "%c", KEY_BEL);
  224. (void)vt100_oflush(this);
  225. }
  226. /*-------------------------------------------------------- */
  227. void vt100_attribute_reset(const vt100_t * this)
  228. {
  229. vt100_printf(this, "%c[0m", KEY_ESC);
  230. }
  231. /*-------------------------------------------------------- */
  232. void vt100_attribute_bright(const vt100_t * this)
  233. {
  234. vt100_printf(this, "%c[1m", KEY_ESC);
  235. }
  236. /*-------------------------------------------------------- */
  237. void vt100_attribute_dim(const vt100_t * this)
  238. {
  239. vt100_printf(this, "%c[2m", KEY_ESC);
  240. }
  241. /*-------------------------------------------------------- */
  242. void vt100_attribute_underscore(const vt100_t * this)
  243. {
  244. vt100_printf(this, "%c[4m", KEY_ESC);
  245. }
  246. /*-------------------------------------------------------- */
  247. void vt100_attribute_blink(const vt100_t * this)
  248. {
  249. vt100_printf(this, "%c[5m", KEY_ESC);
  250. }
  251. /*-------------------------------------------------------- */
  252. void vt100_attribute_reverse(const vt100_t * this)
  253. {
  254. vt100_printf(this, "%c[7m", KEY_ESC);
  255. }
  256. /*-------------------------------------------------------- */
  257. void vt100_attribute_hidden(const vt100_t * this)
  258. {
  259. vt100_printf(this, "%c[8m", KEY_ESC);
  260. }
  261. /*-------------------------------------------------------- */
  262. void vt100_erase_line(const vt100_t * this)
  263. {
  264. vt100_printf(this, "%c[2K", KEY_ESC);
  265. }
  266. /*-------------------------------------------------------- */
  267. void vt100_clear_screen(const vt100_t * this)
  268. {
  269. vt100_printf(this, "%c[2J", KEY_ESC);
  270. }
  271. /*-------------------------------------------------------- */
  272. void vt100_cursor_save(const vt100_t * this)
  273. {
  274. vt100_printf(this, "%c7", KEY_ESC); /* VT100 */
  275. /* vt100_printf(this, "%c[s", KEY_ESC); */ /* ANSI */
  276. }
  277. /*-------------------------------------------------------- */
  278. void vt100_cursor_restore(const vt100_t * this)
  279. {
  280. vt100_printf(this, "%c8", KEY_ESC); /* VT100 */
  281. /* vt100_printf(this, "%c[u", KEY_ESC); */ /* ANSI */
  282. }
  283. /*-------------------------------------------------------- */
  284. void vt100_cursor_forward(const vt100_t * this, unsigned count)
  285. {
  286. vt100_printf(this, "%c[%dC", KEY_ESC, count);
  287. }
  288. /*-------------------------------------------------------- */
  289. void vt100_cursor_back(const vt100_t * this, unsigned count)
  290. {
  291. vt100_printf(this, "%c[%dD", KEY_ESC, count);
  292. }
  293. /*-------------------------------------------------------- */
  294. void vt100_cursor_up(const vt100_t * this, unsigned count)
  295. {
  296. vt100_printf(this, "%c[%dA", KEY_ESC, count);
  297. }
  298. /*-------------------------------------------------------- */
  299. void vt100_cursor_down(const vt100_t * this, unsigned count)
  300. {
  301. vt100_printf(this, "%c[%dB", KEY_ESC, count);
  302. }
  303. /*-------------------------------------------------------- */
  304. void vt100_scroll_up(const vt100_t *this)
  305. {
  306. vt100_printf(this, "%cD", KEY_ESC);
  307. }
  308. /*-------------------------------------------------------- */
  309. void vt100_scroll_down(const vt100_t *this)
  310. {
  311. vt100_printf(this, "%cM", KEY_ESC);
  312. }
  313. /*-------------------------------------------------------- */
  314. void vt100_next_line(const vt100_t *this)
  315. {
  316. vt100_printf(this, "%cE", KEY_ESC);
  317. }
  318. /*-------------------------------------------------------- */
  319. void vt100_cursor_home(const vt100_t * this)
  320. {
  321. vt100_printf(this, "%c[H", KEY_ESC);
  322. }
  323. /*-------------------------------------------------------- */
  324. void vt100_erase(const vt100_t * this, unsigned count)
  325. {
  326. vt100_printf(this, "%c[%dP", KEY_ESC, count);
  327. }
  328. /*-------------------------------------------------------- */
  329. void vt100__set_timeout(vt100_t *this, int timeout)
  330. {
  331. this->timeout = timeout;
  332. }
  333. /*-------------------------------------------------------- */
  334. void vt100_erase_down(const vt100_t * this)
  335. {
  336. vt100_printf(this, "%c[J", KEY_ESC);
  337. }
  338. /*-------------------------------------------------------- */
  339. void vt100__set_istream(vt100_t * this, FILE * istream)
  340. {
  341. this->istream = istream;
  342. }
  343. /*-------------------------------------------------------- */
  344. FILE *vt100__get_istream(const vt100_t * this)
  345. {
  346. return this->istream;
  347. }
  348. /*-------------------------------------------------------- */
  349. FILE *vt100__get_ostream(const vt100_t * this)
  350. {
  351. return this->ostream;
  352. }
  353. /*-------------------------------------------------------- */