1
0

vt100.c 11 KB

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