vt100.c 8.3 KB

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