1
0

vt100.c 11 KB

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