vt100.c 11 KB

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