1
0

vt100.c 8.6 KB

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