vt100.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. #undef __STRICT_ANSI__ /* we need to use fileno() */
  2. #include <stdlib.h>
  3. # include <unistd.h>
  4. # include <fcntl.h>
  5. #include <string.h>
  6. #include "private.h"
  7. typedef struct {
  8. const char* sequence;
  9. tinyrl_vt100_escape_t code;
  10. } vt100_decode_t;
  11. /* This table maps the vt100 escape codes to an enumeration */
  12. static vt100_decode_t cmds[] = {
  13. {"[A", tinyrl_vt100_CURSOR_UP},
  14. {"[B", tinyrl_vt100_CURSOR_DOWN},
  15. {"[C", tinyrl_vt100_CURSOR_RIGHT},
  16. {"[D", tinyrl_vt100_CURSOR_LEFT},
  17. {"[H", tinyrl_vt100_HOME},
  18. {"[F", tinyrl_vt100_END},
  19. {"[2~", tinyrl_vt100_INSERT},
  20. {"[3~", tinyrl_vt100_DELETE},
  21. {"[5~", tinyrl_vt100_PGUP},
  22. {"[6~", tinyrl_vt100_PGDOWN},
  23. };
  24. /*--------------------------------------------------------- */
  25. static void _tinyrl_vt100_setInputNonBlocking(const tinyrl_vt100_t * this)
  26. {
  27. #if defined(STDIN_FILENO)
  28. int flags = (fcntl(STDIN_FILENO, F_GETFL, 0) | O_NONBLOCK);
  29. fcntl(STDIN_FILENO, F_SETFL, flags);
  30. #endif /* STDIN_FILENO */
  31. }
  32. /*--------------------------------------------------------- */
  33. static void _tinyrl_vt100_setInputBlocking(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. tinyrl_vt100_escape_t tinyrl_vt100_escape_decode(const tinyrl_vt100_t * this)
  42. {
  43. tinyrl_vt100_escape_t result = tinyrl_vt100_UNKNOWN;
  44. char sequence[10], *p = sequence;
  45. int c;
  46. unsigned i;
  47. /* before the while loop, set the input as non-blocking */
  48. _tinyrl_vt100_setInputNonBlocking(this);
  49. /* dump the control sequence into our sequence buffer
  50. * ANSI standard control sequences will end
  51. * with a character between 64 - 126
  52. */
  53. while (1) {
  54. c = getc(this->istream);
  55. /* ignore no-character condition */
  56. if (-1 != c) {
  57. *p++ = (c & 0xFF);
  58. if ((c != '[') && (c > 63)) {
  59. /* this is an ANSI control sequence terminator code */
  60. result = tinyrl_vt100_CURSOR_UP; /* just a non-UNKNOWN value */
  61. break;
  62. }
  63. } else {
  64. result = tinyrl_vt100_UNKNOWN;
  65. break;
  66. }
  67. }
  68. /* terminate the string */
  69. *p = '\0';
  70. /* restore the blocking status */
  71. _tinyrl_vt100_setInputBlocking(this);
  72. if (tinyrl_vt100_UNKNOWN != result) {
  73. p = sequence;
  74. result = tinyrl_vt100_UNKNOWN;
  75. /* now decode the sequence */
  76. for (i = 0; i < sizeof(cmds) / sizeof(vt100_decode_t); i++) {
  77. if (strcmp(cmds[i].sequence, p) == 0) {
  78. /* found the code in the lookup table */
  79. result = cmds[i].code;
  80. break;
  81. }
  82. }
  83. }
  84. return result;
  85. }
  86. /*-------------------------------------------------------- */
  87. int tinyrl_vt100_printf(const tinyrl_vt100_t * this, const char *fmt, ...)
  88. {
  89. va_list args;
  90. int len;
  91. va_start(args, fmt);
  92. len = tinyrl_vt100_vprintf(this, fmt, args);
  93. va_end(args);
  94. return len;
  95. }
  96. /*-------------------------------------------------------- */
  97. int
  98. tinyrl_vt100_vprintf(const tinyrl_vt100_t * this, const char *fmt, va_list args)
  99. {
  100. return vfprintf(this->ostream, fmt, args);
  101. }
  102. /*-------------------------------------------------------- */
  103. int tinyrl_vt100_getchar(const tinyrl_vt100_t * this)
  104. {
  105. return getc(this->istream);
  106. }
  107. /*-------------------------------------------------------- */
  108. int tinyrl_vt100_oflush(const tinyrl_vt100_t * this)
  109. {
  110. return fflush(this->ostream);
  111. }
  112. /*-------------------------------------------------------- */
  113. int tinyrl_vt100_ierror(const tinyrl_vt100_t * this)
  114. {
  115. return ferror(this->istream);
  116. }
  117. /*-------------------------------------------------------- */
  118. int tinyrl_vt100_oerror(const tinyrl_vt100_t * this)
  119. {
  120. return ferror(this->ostream);
  121. }
  122. /*-------------------------------------------------------- */
  123. int tinyrl_vt100_ieof(const tinyrl_vt100_t * this)
  124. {
  125. return feof(this->istream);
  126. }
  127. /*-------------------------------------------------------- */
  128. int tinyrl_vt100_eof(const tinyrl_vt100_t * this)
  129. {
  130. return feof(this->istream);
  131. }
  132. /*-------------------------------------------------------- */
  133. unsigned tinyrl_vt100__get_width(const tinyrl_vt100_t * this)
  134. {
  135. this = this;
  136. /* hard code until we suss out how to do it properly */
  137. return 80;
  138. }
  139. /*-------------------------------------------------------- */
  140. static void
  141. tinyrl_vt100_init(tinyrl_vt100_t * this, FILE * istream, FILE * ostream)
  142. {
  143. this->istream = istream;
  144. this->ostream = ostream;
  145. }
  146. /*-------------------------------------------------------- */
  147. static void tinyrl_vt100_fini(tinyrl_vt100_t * this)
  148. {
  149. /* nothing to do yet... */
  150. this = this;
  151. }
  152. /*-------------------------------------------------------- */
  153. tinyrl_vt100_t *tinyrl_vt100_new(FILE * istream, FILE * ostream)
  154. {
  155. tinyrl_vt100_t *this = NULL;
  156. this = malloc(sizeof(tinyrl_vt100_t));
  157. if (NULL != this) {
  158. tinyrl_vt100_init(this, istream, ostream);
  159. }
  160. return this;
  161. }
  162. /*-------------------------------------------------------- */
  163. void tinyrl_vt100_delete(tinyrl_vt100_t * this)
  164. {
  165. tinyrl_vt100_fini(this);
  166. /* release the memory */
  167. free(this);
  168. }
  169. /*-------------------------------------------------------- */
  170. void tinyrl_vt100_ding(const tinyrl_vt100_t * this)
  171. {
  172. tinyrl_vt100_printf(this, "%c", KEY_BEL);
  173. (void)tinyrl_vt100_oflush(this);
  174. }
  175. /*-------------------------------------------------------- */
  176. void tinyrl_vt100_attribute_reset(const tinyrl_vt100_t * this)
  177. {
  178. tinyrl_vt100_printf(this, "%c[0m", KEY_ESC);
  179. }
  180. /*-------------------------------------------------------- */
  181. void tinyrl_vt100_attribute_bright(const tinyrl_vt100_t * this)
  182. {
  183. tinyrl_vt100_printf(this, "%c[1m", KEY_ESC);
  184. }
  185. /*-------------------------------------------------------- */
  186. void tinyrl_vt100_attribute_dim(const tinyrl_vt100_t * this)
  187. {
  188. tinyrl_vt100_printf(this, "%c[2m", KEY_ESC);
  189. }
  190. /*-------------------------------------------------------- */
  191. void tinyrl_vt100_attribute_underscore(const tinyrl_vt100_t * this)
  192. {
  193. tinyrl_vt100_printf(this, "%c[4m", KEY_ESC);
  194. }
  195. /*-------------------------------------------------------- */
  196. void tinyrl_vt100_attribute_blink(const tinyrl_vt100_t * this)
  197. {
  198. tinyrl_vt100_printf(this, "%c[5m", KEY_ESC);
  199. }
  200. /*-------------------------------------------------------- */
  201. void tinyrl_vt100_attribute_reverse(const tinyrl_vt100_t * this)
  202. {
  203. tinyrl_vt100_printf(this, "%c[7m", KEY_ESC);
  204. }
  205. /*-------------------------------------------------------- */
  206. void tinyrl_vt100_attribute_hidden(const tinyrl_vt100_t * this)
  207. {
  208. tinyrl_vt100_printf(this, "%c[8m", KEY_ESC);
  209. }
  210. /*-------------------------------------------------------- */
  211. void tinyrl_vt100_erase_line(const tinyrl_vt100_t * this)
  212. {
  213. tinyrl_vt100_printf(this, "%c[2K", KEY_ESC);
  214. }
  215. /*-------------------------------------------------------- */
  216. void tinyrl_vt100_clear_screen(const tinyrl_vt100_t * this)
  217. {
  218. tinyrl_vt100_printf(this, "%c[2J", KEY_ESC);
  219. }
  220. /*-------------------------------------------------------- */
  221. void tinyrl_vt100_cursor_save(const tinyrl_vt100_t * this)
  222. {
  223. tinyrl_vt100_printf(this, "%c7", KEY_ESC);
  224. }
  225. /*-------------------------------------------------------- */
  226. void tinyrl_vt100_cursor_restore(const tinyrl_vt100_t * this)
  227. {
  228. tinyrl_vt100_printf(this, "%c8", KEY_ESC);
  229. }
  230. /*-------------------------------------------------------- */
  231. void tinyrl_vt100_cursor_forward(const tinyrl_vt100_t * this, unsigned count)
  232. {
  233. tinyrl_vt100_printf(this, "%c[%dC", KEY_ESC, count);
  234. }
  235. /*-------------------------------------------------------- */
  236. void tinyrl_vt100_cursor_back(const tinyrl_vt100_t * this, unsigned count)
  237. {
  238. tinyrl_vt100_printf(this, "%c[%dD", KEY_ESC, count);
  239. }
  240. /*-------------------------------------------------------- */
  241. void tinyrl_vt100_cursor_up(const tinyrl_vt100_t * this, unsigned count)
  242. {
  243. tinyrl_vt100_printf(this, "%c[%dA", KEY_ESC, count);
  244. }
  245. /*-------------------------------------------------------- */
  246. void tinyrl_vt100_cursor_down(const tinyrl_vt100_t * this, unsigned count)
  247. {
  248. tinyrl_vt100_printf(this, "%c[%dB", KEY_ESC, count);
  249. }
  250. /*-------------------------------------------------------- */
  251. void tinyrl_vt100_cursor_home(const tinyrl_vt100_t * this)
  252. {
  253. tinyrl_vt100_printf(this, "%c[H", KEY_ESC);
  254. }
  255. /*-------------------------------------------------------- */
  256. void tinyrl_vt100_erase(const tinyrl_vt100_t * this, unsigned count)
  257. {
  258. tinyrl_vt100_printf(this, "%c[%dP", KEY_ESC, count);
  259. }
  260. /*-------------------------------------------------------- */
  261. void tinyrl_vt100__set_istream(tinyrl_vt100_t * this, FILE * istream)
  262. {
  263. this->istream = istream;
  264. }
  265. /*-------------------------------------------------------- */
  266. FILE *tinyrl_vt100__get_istream(const tinyrl_vt100_t * this)
  267. {
  268. return this->istream;
  269. }
  270. /*-------------------------------------------------------- */
  271. FILE *tinyrl_vt100__get_ostream(const tinyrl_vt100_t * this)
  272. {
  273. return this->ostream;
  274. }
  275. /*-------------------------------------------------------- */