terminal.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. Terminal tracking
  3. Actually, I believe I only really need to track the color information.
  4. Everything else, I'm not sure I really care about.
  5. */
  6. #include <string.h>
  7. #include <ctype.h>
  8. #include <stdlib.h>
  9. #include <stdio.h> // sprintf
  10. #include "terminal.h"
  11. #include "zf_log.h"
  12. extern const char *repr(const char *data);
  13. void console_init(struct console_details *cdp) {
  14. cdp->posx = 0;
  15. cdp->posy = 0;
  16. cdp->savedx = 0;
  17. cdp->savedy = 0;
  18. cdp->in_ansi = 0;
  19. cdp->fgcolor = 7;
  20. cdp->bgcolor = 0;
  21. cdp->status = 0;
  22. }
  23. void ansi_color(struct console_details *cdp, int color) {
  24. // ZF_LOGV("ansi_color(%d)", color);
  25. if (color == 0) {
  26. cdp->status = 0;
  27. cdp->fgcolor = 7;
  28. cdp->bgcolor = 0;
  29. return;
  30. }
  31. if (color == 1) {
  32. cdp->status = 1;
  33. return;
  34. }
  35. if (color == 5) {
  36. cdp->status = 5;
  37. return;
  38. }
  39. if ((color >= 30) && (color <= 37)) {
  40. cdp->fgcolor = color - 30;
  41. return;
  42. }
  43. if ((color >= 40) && (color <= 47)) {
  44. cdp->bgcolor = color - 40;
  45. return;
  46. }
  47. ZF_LOGD("ansi_color( %d ) is unknown to me.", color);
  48. }
  49. const char *color_restore(struct console_details *cdp) {
  50. static char buffer[30];
  51. if (cdp->status == 0) {
  52. sprintf(buffer, "\x1b[0;3%d;4%dm", cdp->fgcolor, cdp->bgcolor);
  53. } else {
  54. sprintf(buffer, "\x1b[0;%d;3%d;4%dm", cdp->status, cdp->fgcolor,
  55. cdp->bgcolor);
  56. };
  57. return buffer;
  58. }
  59. void console_ansi(struct console_details *cdp, const char *ansi) {
  60. int understood = 0;
  61. const char *cp = ansi;
  62. const char *last = ansi + strlen(ansi) - 1;
  63. int number, number2;
  64. if (*cp == '\x1b') {
  65. cp++;
  66. // Ok, that's expected.
  67. if (*cp == '[') {
  68. cp++;
  69. // https://en.wikipedia.org/wiki/ANSI_escape_code#CSI_sequences
  70. switch (*last) {
  71. case 'A':
  72. // cursor up
  73. if (cp == last) {
  74. number = 1;
  75. } else {
  76. number = atoi(cp);
  77. if (number < 1) {
  78. ZF_LOGD("console_ansi( %s ): number error (%d)", repr(ansi),
  79. number);
  80. number = 1;
  81. }
  82. };
  83. cdp->posy -= number;
  84. if (cdp->posy < 0) {
  85. cdp->posy = 0;
  86. ZF_LOGD(
  87. "console_ansi( %s ): attempt to move above top of screen (%d)",
  88. repr(ansi), number);
  89. }
  90. understood = 1;
  91. return;
  92. case 'B':
  93. // cursor down
  94. if (cp == last) {
  95. number = 1;
  96. } else {
  97. number = atoi(cp);
  98. if (number < 1) {
  99. ZF_LOGD("console_ansi( %s ): number error (%d)", repr(ansi),
  100. number);
  101. number = 1;
  102. }
  103. };
  104. cdp->posy += number;
  105. // check range/"scroll"
  106. understood = 1;
  107. return;
  108. case 'C':
  109. // cursor forward
  110. if (cp == last) {
  111. number = 1;
  112. } else {
  113. number = atoi(cp);
  114. if (number < 1) {
  115. ZF_LOGD("console_ansi( %s ): number error (%d)", repr(ansi),
  116. number);
  117. number = 1;
  118. }
  119. };
  120. cdp->posx += number;
  121. // Well. According to the "spec", the screen limits are hard
  122. // If the cursor is already at the edge of the screen, this has no
  123. // effect. ^ This is *NOT* how any ANSI BBS terminal program acts!
  124. while (cdp->posx > 79) {
  125. cdp->posy++;
  126. // check range/"scroll"
  127. cdp->posx -= 79;
  128. }
  129. understood = 1;
  130. return;
  131. case 'D':
  132. // cursor backwards
  133. if (cp == last) {
  134. number = 1;
  135. } else {
  136. number = atoi(cp);
  137. if (number < 1) {
  138. ZF_LOGD("console_ansi( %s ): number error (%d)", repr(ansi),
  139. number);
  140. number = 0;
  141. }
  142. };
  143. cdp->posx -= number;
  144. // Well. According to the "spec", the screen limits are hard
  145. // If the cursor is already at the edge of the screen, this has no
  146. // effect. ^ This is *NOT* how any ANSI BBS terminal program acts!
  147. while (cdp->posx < 0) {
  148. cdp->posy--;
  149. if (cdp->posy < 0) {
  150. cdp->posy = 0;
  151. }
  152. cdp->posx += 79;
  153. }
  154. understood = 1;
  155. return;
  156. case 'H':
  157. // cursor position
  158. if (*cp == ';') {
  159. // Missing first number
  160. number = 1;
  161. cp++;
  162. if (cp == last) {
  163. // missing 2nd number as well?
  164. number2 = 1;
  165. } else {
  166. number2 = atoi(cp);
  167. }
  168. } else {
  169. // Ok, find the first number
  170. number = atoi(cp);
  171. cp = strchr(cp, ';');
  172. if (cp == NULL) {
  173. // Missing 2nd number
  174. number2 = 1;
  175. } else {
  176. // 2nd number found, maybe.
  177. cp++;
  178. if (cp == last) {
  179. number2 = 1;
  180. } else {
  181. number2 = atoi(cp);
  182. }
  183. }
  184. }
  185. // Our positions start at zero, not one.
  186. cdp->posx = number - 1;
  187. cdp->posy = number2 - 1;
  188. understood = 1;
  189. break;
  190. case 'J':
  191. // clear
  192. if (cp == last) {
  193. number = 0;
  194. } else {
  195. number = atoi(cp);
  196. };
  197. // clears ... part of the screen.
  198. if (number == 2) {
  199. cdp->posx = 0;
  200. cdp->posy = 0;
  201. };
  202. understood = 1;
  203. break;
  204. case 'm':
  205. // color
  206. if (cp == last) {
  207. // nothing given, default to 0.
  208. number = 0;
  209. ansi_color(cdp, number);
  210. } else {
  211. while (cp != last) {
  212. number = atoi(cp);
  213. ansi_color(cdp, number);
  214. cp++;
  215. while ((cp != last) && (isdigit(*cp))) {
  216. cp++;
  217. };
  218. if (cp != last) {
  219. if (*cp == ';') {
  220. cp++;
  221. }
  222. }
  223. }
  224. }
  225. understood = 1;
  226. break;
  227. case 't':
  228. case 'r':
  229. case 'h':
  230. case '!':
  231. // These are ones that I don't care about.
  232. case 'n': // This is terminal detect -- give me cursor position
  233. understood = 1;
  234. break;
  235. default:
  236. // unsure -- possibly not important
  237. ZF_LOGD("console_ansi( %s ): ???", repr(ansi));
  238. understood = 0;
  239. }
  240. }
  241. };
  242. if (!understood) {
  243. ZF_LOGD("console_ansi( %s ): was not understood.", repr(ansi));
  244. }
  245. }
  246. /*
  247. * console_char()
  248. * return whether or not we are still in_ansi
  249. */
  250. int console_char(struct console_details *cdp, char ch) {
  251. char *cp;
  252. if (cdp->in_ansi) {
  253. // Ok, append this char
  254. cp = cdp->ansi + strlen(cdp->ansi);
  255. *cp = ch;
  256. cp++;
  257. *cp = 0;
  258. if (isalpha(ch)) {
  259. // Ok!
  260. console_ansi(cdp, cdp->ansi);
  261. cdp->in_ansi = 0;
  262. cdp->ansi[0] = 0;
  263. return 1;
  264. }
  265. return 1;
  266. } else {
  267. if (ch == '\x1b') {
  268. cp = cdp->ansi;
  269. *cp = ch;
  270. cp++;
  271. *cp = 0;
  272. cdp->in_ansi = 1;
  273. return 1;
  274. }
  275. if (ch == '\r') {
  276. // Carriage return
  277. cdp->posx = 0;
  278. return 0;
  279. }
  280. if (ch == '\n') {
  281. cdp->posy++;
  282. // check range/"scroll"
  283. return 0;
  284. }
  285. if (ch == '\b') {
  286. // Backspace.
  287. if (cdp->posx > 0) {
  288. cdp->posx--;
  289. }
  290. return 0;
  291. }
  292. if (ch == '\f') {
  293. // form feed
  294. // treat as clear screen
  295. cdp->posx = 0;
  296. cdp->posy = 0;
  297. return 0;
  298. }
  299. /*
  300. I don't believe that anything else can possibly be here, other then an
  301. actual printable character. So!
  302. */
  303. cdp->posx++;
  304. if (cdp->posx > 79) {
  305. cdp->posx = 0;
  306. cdp->posy++;
  307. // check range/"scroll"
  308. }
  309. return 0;
  310. }
  311. }
  312. void console_string(struct console_details *cdp, const char *chars) {
  313. int x;
  314. for (x = 0; x < strlen(chars); x++) {
  315. console_char(cdp, chars[x]);
  316. }
  317. }
  318. void console_receive(struct console_details *cdp, const char *chars, int len) {
  319. int x;
  320. for (x = 0; x < len; x++) {
  321. console_char(cdp, chars[x]);
  322. }
  323. }