vt100.c 9.3 KB

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