vt100.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. #undef __STRICT_ANSI__ /* we need to use fileno() */
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <unistd.h>
  5. #include <fcntl.h>
  6. #include <string.h>
  7. #include <sys/ioctl.h>
  8. #include <sys/time.h>
  9. #include <sys/types.h>
  10. #include <sys/select.h>
  11. #include <errno.h>
  12. #include "private.h"
  13. typedef struct {
  14. const char* sequence;
  15. tinyrl_vt100_escape_t code;
  16. } vt100_decode_t;
  17. /* This table maps the vt100 escape codes to an enumeration */
  18. static vt100_decode_t cmds[] = {
  19. {"[A", tinyrl_vt100_CURSOR_UP},
  20. {"[B", tinyrl_vt100_CURSOR_DOWN},
  21. {"[C", tinyrl_vt100_CURSOR_RIGHT},
  22. {"[D", tinyrl_vt100_CURSOR_LEFT},
  23. {"[H", tinyrl_vt100_HOME},
  24. {"[1~", tinyrl_vt100_HOME},
  25. {"[F", tinyrl_vt100_END},
  26. {"[4~", tinyrl_vt100_END},
  27. {"[2~", tinyrl_vt100_INSERT},
  28. {"[3~", tinyrl_vt100_DELETE},
  29. {"[5~", tinyrl_vt100_PGUP},
  30. {"[6~", tinyrl_vt100_PGDOWN},
  31. };
  32. /*--------------------------------------------------------- */
  33. static void _tinyrl_vt100_setInputNonBlocking(const tinyrl_vt100_t * this)
  34. {
  35. #if defined(STDIN_FILENO)
  36. int flags = (fcntl(STDIN_FILENO, F_GETFL, 0) | O_NONBLOCK);
  37. fcntl(STDIN_FILENO, F_SETFL, flags);
  38. #endif /* STDIN_FILENO */
  39. }
  40. /*--------------------------------------------------------- */
  41. static void _tinyrl_vt100_setInputBlocking(const tinyrl_vt100_t * this)
  42. {
  43. #if defined(STDIN_FILENO)
  44. int flags = (fcntl(STDIN_FILENO, F_GETFL, 0) & ~O_NONBLOCK);
  45. fcntl(STDIN_FILENO, F_SETFL, flags);
  46. #endif /* STDIN_FILENO */
  47. }
  48. /*--------------------------------------------------------- */
  49. tinyrl_vt100_escape_t tinyrl_vt100_escape_decode(const tinyrl_vt100_t * this)
  50. {
  51. tinyrl_vt100_escape_t result = tinyrl_vt100_UNKNOWN;
  52. char sequence[10], *p = sequence;
  53. int c;
  54. unsigned i;
  55. /* before the while loop, set the input as non-blocking */
  56. _tinyrl_vt100_setInputNonBlocking(this);
  57. /* dump the control sequence into our sequence buffer
  58. * ANSI standard control sequences will end
  59. * with a character between 64 - 126
  60. */
  61. while (1) {
  62. c = getc(this->istream);
  63. /* ignore no-character condition */
  64. if (-1 != c) {
  65. *p++ = (c & 0xFF);
  66. if ((c != '[') && (c > 63)) {
  67. /* this is an ANSI control sequence terminator code */
  68. result = tinyrl_vt100_CURSOR_UP; /* just a non-UNKNOWN value */
  69. break;
  70. }
  71. } else {
  72. result = tinyrl_vt100_UNKNOWN;
  73. break;
  74. }
  75. }
  76. /* terminate the string */
  77. *p = '\0';
  78. /* restore the blocking status */
  79. _tinyrl_vt100_setInputBlocking(this);
  80. if (tinyrl_vt100_UNKNOWN != result) {
  81. p = sequence;
  82. result = tinyrl_vt100_UNKNOWN;
  83. /* now decode the sequence */
  84. for (i = 0; i < sizeof(cmds) / sizeof(vt100_decode_t); i++) {
  85. if (strcmp(cmds[i].sequence, p) == 0) {
  86. /* found the code in the lookup table */
  87. result = cmds[i].code;
  88. break;
  89. }
  90. }
  91. }
  92. return result;
  93. }
  94. /*-------------------------------------------------------- */
  95. int tinyrl_vt100_printf(const tinyrl_vt100_t * this, const char *fmt, ...)
  96. {
  97. va_list args;
  98. int len;
  99. va_start(args, fmt);
  100. len = tinyrl_vt100_vprintf(this, fmt, args);
  101. va_end(args);
  102. return len;
  103. }
  104. /*-------------------------------------------------------- */
  105. int
  106. tinyrl_vt100_vprintf(const tinyrl_vt100_t * this, const char *fmt, va_list args)
  107. {
  108. return vfprintf(this->ostream, fmt, args);
  109. }
  110. /*-------------------------------------------------------- */
  111. int tinyrl_vt100_getchar(const tinyrl_vt100_t *this)
  112. {
  113. unsigned char c;
  114. int istream_fd;
  115. fd_set rfds;
  116. struct timeval tv;
  117. int retval;
  118. ssize_t res;
  119. istream_fd = fileno(this->istream);
  120. /* Just wait for the input if no timeout */
  121. if (this->timeout <= 0) {
  122. while (((res = read(istream_fd, &c, 1)) < 0) &&
  123. (EAGAIN == errno));
  124. /* EOF or error */
  125. if (res < 0)
  126. return VT100_ERR;
  127. if (!res)
  128. return VT100_EOF;
  129. return c;
  130. }
  131. /* Set timeout for the select() */
  132. FD_ZERO(&rfds);
  133. FD_SET(istream_fd, &rfds);
  134. tv.tv_sec = this->timeout;
  135. tv.tv_usec = 0;
  136. while (((retval = select(istream_fd + 1, &rfds, NULL, NULL, &tv)) < 0) &&
  137. (EAGAIN == errno));
  138. /* Error or timeout */
  139. if (retval < 0)
  140. return VT100_ERR;
  141. if (!retval)
  142. return VT100_TIMEOUT;
  143. res = read(istream_fd, &c, 1);
  144. /* EOF or error */
  145. if (res < 0)
  146. return VT100_ERR;
  147. if (!res)
  148. return VT100_EOF;
  149. return c;
  150. }
  151. /*-------------------------------------------------------- */
  152. int tinyrl_vt100_oflush(const tinyrl_vt100_t * this)
  153. {
  154. return fflush(this->ostream);
  155. }
  156. /*-------------------------------------------------------- */
  157. int tinyrl_vt100_ierror(const tinyrl_vt100_t * this)
  158. {
  159. return ferror(this->istream);
  160. }
  161. /*-------------------------------------------------------- */
  162. int tinyrl_vt100_oerror(const tinyrl_vt100_t * this)
  163. {
  164. return ferror(this->ostream);
  165. }
  166. /*-------------------------------------------------------- */
  167. int tinyrl_vt100_ieof(const tinyrl_vt100_t * this)
  168. {
  169. return feof(this->istream);
  170. }
  171. /*-------------------------------------------------------- */
  172. int tinyrl_vt100_eof(const tinyrl_vt100_t * this)
  173. {
  174. return feof(this->istream);
  175. }
  176. /*-------------------------------------------------------- */
  177. unsigned int tinyrl_vt100__get_width(const tinyrl_vt100_t *this)
  178. {
  179. #ifdef TIOCGWINSZ
  180. struct winsize ws;
  181. int res;
  182. ws.ws_col = 0;
  183. res = ioctl(fileno(this->ostream), TIOCGWINSZ, &ws);
  184. if (res || !ws.ws_col)
  185. return 80;
  186. return ws.ws_col;
  187. #else
  188. return 80;
  189. #endif
  190. }
  191. /*-------------------------------------------------------- */
  192. unsigned int tinyrl_vt100__get_height(const tinyrl_vt100_t *this)
  193. {
  194. #ifdef TIOCGWINSZ
  195. struct winsize ws;
  196. int res;
  197. ws.ws_row = 0;
  198. res = ioctl(fileno(this->ostream), TIOCGWINSZ, &ws);
  199. if (res || !ws.ws_row)
  200. return 25;
  201. return ws.ws_row;
  202. #else
  203. return 25;
  204. #endif
  205. }
  206. /*-------------------------------------------------------- */
  207. static void
  208. tinyrl_vt100_init(tinyrl_vt100_t * this, FILE * istream, FILE * ostream)
  209. {
  210. this->istream = istream;
  211. this->ostream = ostream;
  212. this->timeout = -1; /* No timeout by default */
  213. }
  214. /*-------------------------------------------------------- */
  215. static void tinyrl_vt100_fini(tinyrl_vt100_t * this)
  216. {
  217. /* nothing to do yet... */
  218. this = this;
  219. }
  220. /*-------------------------------------------------------- */
  221. tinyrl_vt100_t *tinyrl_vt100_new(FILE * istream, FILE * ostream)
  222. {
  223. tinyrl_vt100_t *this = NULL;
  224. this = malloc(sizeof(tinyrl_vt100_t));
  225. if (NULL != this) {
  226. tinyrl_vt100_init(this, istream, ostream);
  227. }
  228. return this;
  229. }
  230. /*-------------------------------------------------------- */
  231. void tinyrl_vt100_delete(tinyrl_vt100_t * this)
  232. {
  233. tinyrl_vt100_fini(this);
  234. /* release the memory */
  235. free(this);
  236. }
  237. /*-------------------------------------------------------- */
  238. void tinyrl_vt100_ding(const tinyrl_vt100_t * this)
  239. {
  240. tinyrl_vt100_printf(this, "%c", KEY_BEL);
  241. (void)tinyrl_vt100_oflush(this);
  242. }
  243. /*-------------------------------------------------------- */
  244. void tinyrl_vt100_attribute_reset(const tinyrl_vt100_t * this)
  245. {
  246. tinyrl_vt100_printf(this, "%c[0m", KEY_ESC);
  247. }
  248. /*-------------------------------------------------------- */
  249. void tinyrl_vt100_attribute_bright(const tinyrl_vt100_t * this)
  250. {
  251. tinyrl_vt100_printf(this, "%c[1m", KEY_ESC);
  252. }
  253. /*-------------------------------------------------------- */
  254. void tinyrl_vt100_attribute_dim(const tinyrl_vt100_t * this)
  255. {
  256. tinyrl_vt100_printf(this, "%c[2m", KEY_ESC);
  257. }
  258. /*-------------------------------------------------------- */
  259. void tinyrl_vt100_attribute_underscore(const tinyrl_vt100_t * this)
  260. {
  261. tinyrl_vt100_printf(this, "%c[4m", KEY_ESC);
  262. }
  263. /*-------------------------------------------------------- */
  264. void tinyrl_vt100_attribute_blink(const tinyrl_vt100_t * this)
  265. {
  266. tinyrl_vt100_printf(this, "%c[5m", KEY_ESC);
  267. }
  268. /*-------------------------------------------------------- */
  269. void tinyrl_vt100_attribute_reverse(const tinyrl_vt100_t * this)
  270. {
  271. tinyrl_vt100_printf(this, "%c[7m", KEY_ESC);
  272. }
  273. /*-------------------------------------------------------- */
  274. void tinyrl_vt100_attribute_hidden(const tinyrl_vt100_t * this)
  275. {
  276. tinyrl_vt100_printf(this, "%c[8m", KEY_ESC);
  277. }
  278. /*-------------------------------------------------------- */
  279. void tinyrl_vt100_erase_line(const tinyrl_vt100_t * this)
  280. {
  281. tinyrl_vt100_printf(this, "%c[2K", KEY_ESC);
  282. }
  283. /*-------------------------------------------------------- */
  284. void tinyrl_vt100_clear_screen(const tinyrl_vt100_t * this)
  285. {
  286. tinyrl_vt100_printf(this, "%c[2J", KEY_ESC);
  287. }
  288. /*-------------------------------------------------------- */
  289. void tinyrl_vt100_cursor_save(const tinyrl_vt100_t * this)
  290. {
  291. tinyrl_vt100_printf(this, "%c7", KEY_ESC); /* VT100 */
  292. /* tinyrl_vt100_printf(this, "%c[s", KEY_ESC); */ /* ANSI */
  293. }
  294. /*-------------------------------------------------------- */
  295. void tinyrl_vt100_cursor_restore(const tinyrl_vt100_t * this)
  296. {
  297. tinyrl_vt100_printf(this, "%c8", KEY_ESC); /* VT100 */
  298. /* tinyrl_vt100_printf(this, "%c[u", KEY_ESC); */ /* ANSI */
  299. }
  300. /*-------------------------------------------------------- */
  301. void tinyrl_vt100_cursor_forward(const tinyrl_vt100_t * this, unsigned count)
  302. {
  303. tinyrl_vt100_printf(this, "%c[%dC", KEY_ESC, count);
  304. }
  305. /*-------------------------------------------------------- */
  306. void tinyrl_vt100_cursor_back(const tinyrl_vt100_t * this, unsigned count)
  307. {
  308. tinyrl_vt100_printf(this, "%c[%dD", KEY_ESC, count);
  309. }
  310. /*-------------------------------------------------------- */
  311. void tinyrl_vt100_cursor_up(const tinyrl_vt100_t * this, unsigned count)
  312. {
  313. tinyrl_vt100_printf(this, "%c[%dA", KEY_ESC, count);
  314. }
  315. /*-------------------------------------------------------- */
  316. void tinyrl_vt100_cursor_down(const tinyrl_vt100_t * this, unsigned count)
  317. {
  318. tinyrl_vt100_printf(this, "%c[%dB", KEY_ESC, count);
  319. }
  320. /*-------------------------------------------------------- */
  321. void tinyrl_vt100_scroll_up(const tinyrl_vt100_t *this)
  322. {
  323. tinyrl_vt100_printf(this, "%cD", KEY_ESC);
  324. }
  325. /*-------------------------------------------------------- */
  326. void tinyrl_vt100_scroll_down(const tinyrl_vt100_t *this)
  327. {
  328. tinyrl_vt100_printf(this, "%cM", KEY_ESC);
  329. }
  330. /*-------------------------------------------------------- */
  331. void tinyrl_vt100_next_line(const tinyrl_vt100_t *this)
  332. {
  333. tinyrl_vt100_printf(this, "%cE", KEY_ESC);
  334. }
  335. /*-------------------------------------------------------- */
  336. void tinyrl_vt100_cursor_home(const tinyrl_vt100_t * this)
  337. {
  338. tinyrl_vt100_printf(this, "%c[H", KEY_ESC);
  339. }
  340. /*-------------------------------------------------------- */
  341. void tinyrl_vt100_erase(const tinyrl_vt100_t * this, unsigned count)
  342. {
  343. tinyrl_vt100_printf(this, "%c[%dP", KEY_ESC, count);
  344. }
  345. /*-------------------------------------------------------- */
  346. void tinyrl_vt100__set_timeout(tinyrl_vt100_t *this, int timeout)
  347. {
  348. this->timeout = timeout;
  349. }
  350. /*-------------------------------------------------------- */
  351. void tinyrl_vt100_erase_down(const tinyrl_vt100_t * this)
  352. {
  353. tinyrl_vt100_printf(this, "%c[J", KEY_ESC);
  354. }
  355. /*-------------------------------------------------------- */
  356. void tinyrl_vt100__set_istream(tinyrl_vt100_t * this, FILE * istream)
  357. {
  358. this->istream = istream;
  359. }
  360. /*-------------------------------------------------------- */
  361. FILE *tinyrl_vt100__get_istream(const tinyrl_vt100_t * this)
  362. {
  363. return this->istream;
  364. }
  365. /*-------------------------------------------------------- */
  366. FILE *tinyrl_vt100__get_ostream(const tinyrl_vt100_t * this)
  367. {
  368. return this->ostream;
  369. }
  370. /*-------------------------------------------------------- */