terminal.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  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. (NNY!)
  5. */
  6. #include "terminal.h"
  7. #include "utils.h"
  8. #include "zf_log.h"
  9. #include <ctype.h>
  10. #include <sstream>
  11. #include <stdio.h> // snprintf
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <vector>
  15. Terminal::Terminal() { this->init(); }
  16. Terminal::Terminal(const Terminal &old)
  17. : saved_cursor_position(old.saved_cursor_position) {
  18. posx = old.posx;
  19. posy = old.posy;
  20. in_ansi = old.in_ansi;
  21. fgcolor = old.fgcolor;
  22. bgcolor = old.bgcolor;
  23. status = old.status;
  24. ansi = old.ansi;
  25. }
  26. Terminal &Terminal::operator=(Terminal &rhs) {
  27. posx = rhs.posx;
  28. posy = rhs.posy;
  29. in_ansi = rhs.in_ansi;
  30. fgcolor = rhs.fgcolor;
  31. bgcolor = rhs.bgcolor;
  32. status = rhs.status;
  33. ansi = rhs.ansi;
  34. saved_cursor_position = rhs.saved_cursor_position;
  35. return *this;
  36. }
  37. void Terminal::init(void) {
  38. this->posx = this->posy = 0;
  39. this->in_ansi = 0;
  40. this->fgcolor = 7;
  41. this->bgcolor = 0;
  42. this->status = 0;
  43. this->saved_cursor_position.clear();
  44. this->dcs_map = 0x0b;
  45. }
  46. int Terminal::getx(void) { return posx; }
  47. int Terminal::gety(void) { return posy; }
  48. int Terminal::getstatus(void) { return status; }
  49. int Terminal::inANSI(void) { return in_ansi; }
  50. int Terminal::fg(void) { return fgcolor; }
  51. int Terminal::bg(void) { return bgcolor; }
  52. bool Terminal::ansiempty(void) { return ansi.empty(); }
  53. int Terminal::dcs(void) { return dcs_map; }
  54. void Terminal::ansi_color(int color) {
  55. // ZF_LOGV("ansi_color(%d)", color);
  56. if (color == 0) {
  57. this->status = 0;
  58. this->fgcolor = 7;
  59. this->bgcolor = 0;
  60. return;
  61. }
  62. if ((color == 1) || (color == 2) || (color == 3) || (color == 4) ||
  63. (color == 5)) {
  64. this->status = color;
  65. return;
  66. }
  67. if ((color >= 30) && (color <= 37)) {
  68. this->fgcolor = color - 30;
  69. return;
  70. }
  71. if ((color >= 40) && (color <= 47)) {
  72. this->bgcolor = color - 40;
  73. return;
  74. }
  75. if (color == 39) {
  76. // default fg color
  77. this->fgcolor = 7;
  78. return;
  79. }
  80. if (color == 49) {
  81. // default bg color
  82. this->bgcolor = 0;
  83. return;
  84. }
  85. ZF_LOGD("ansi_color( %d ) is unknown to me.", color);
  86. }
  87. std::string Terminal::color_restore(void) {
  88. std::ostringstream oss;
  89. // possible optimize: If fg is 7, don't set (0 reset does that), if bg is 0,
  90. // don't set (0 does that)
  91. if (this->status == 0) {
  92. oss << "\x1b[0;" << this->fgcolor + 30 << ";" << this->bgcolor + 40 << "m";
  93. } else {
  94. oss << "\x1b[0;" << this->status << ";" << this->fgcolor + 30 << ";"
  95. << this->bgcolor + 40 << "m";
  96. }
  97. std::string buffer = oss.str();
  98. return buffer;
  99. }
  100. Terminal::ANSI_TYPE Terminal::ansi_code(std::string ansi) {
  101. std::string::iterator cp = ansi.begin();
  102. std::string::iterator last = ansi.end() - 1;
  103. int number, number2;
  104. if (*cp == '\x1b') {
  105. ++cp;
  106. // Ok, that's expected.
  107. if (*cp == '[') {
  108. ++cp;
  109. // https://en.wikipedia.org/wiki/ANSI_escape_code#CSI_sequences
  110. switch (*last) {
  111. case 'A':
  112. // cursor up
  113. if (cp == last) {
  114. number = 1;
  115. } else {
  116. number = std::stoi(std::string(cp, last));
  117. if (number < 1) {
  118. ZF_LOGD("console_ansi( %s ): number error (%d)", repr(ansi.c_str()),
  119. number);
  120. number = 1;
  121. }
  122. };
  123. this->posy -= number;
  124. if (this->posy < 0) {
  125. this->posy = 0;
  126. ZF_LOGD(
  127. "console_ansi( %s ): attempt to move above top of screen (%d)",
  128. repr(ansi.c_str()), number);
  129. }
  130. return CURSOR;
  131. case 'B':
  132. // cursor down
  133. if (cp == last) {
  134. number = 1;
  135. } else {
  136. number = std::stoi(std::string(cp, last));
  137. if (number < 1) {
  138. ZF_LOGD("console_ansi( %s ): number error (%d)", repr(ansi.c_str()),
  139. number);
  140. number = 1;
  141. }
  142. };
  143. this->posy += number;
  144. // check range/"scroll"
  145. return CURSOR;
  146. case 'C':
  147. // cursor forward
  148. if (cp == last) {
  149. number = 1;
  150. } else {
  151. number = std::stoi(std::string(cp, last));
  152. if (number < 1) {
  153. ZF_LOGD("console_ansi( %s ): number error (%d)", repr(ansi.c_str()),
  154. number);
  155. number = 1;
  156. }
  157. };
  158. this->posx += number;
  159. // Well. According to the "spec", the screen limits are hard
  160. // If the cursor is already at the edge of the screen, this has no
  161. // effect. ^ This is *NOT* how any ANSI BBS terminal program acts!
  162. while (this->posx > 79) {
  163. this->posy++;
  164. // check range/"scroll"
  165. this->posx -= 79;
  166. }
  167. return CURSOR;
  168. case 'D':
  169. // cursor backwards
  170. if (cp == last) {
  171. number = 1;
  172. } else {
  173. number = std::stoi(std::string(cp, last));
  174. if (number < 1) {
  175. ZF_LOGD("console_ansi( %s ): number error (%d)", repr(ansi.c_str()),
  176. number);
  177. number = 0;
  178. }
  179. };
  180. this->posx -= number;
  181. // Well. According to the "spec", the screen limits are hard
  182. // If the cursor is already at the edge of the screen, this has no
  183. // effect. ^ This is *NOT* how any ANSI BBS terminal program acts!
  184. while (this->posx < 0) {
  185. this->posy--;
  186. if (this->posy < 0) {
  187. this->posy = 0;
  188. }
  189. this->posx += 79;
  190. }
  191. return CURSOR;
  192. case 'H':
  193. // cursor position
  194. if (cp == last) {
  195. // Missing first and last
  196. number = 1;
  197. number2 = 1;
  198. } else {
  199. if (*cp == ';') {
  200. // Missing first number
  201. number = 1;
  202. ++cp;
  203. if (cp == last) {
  204. // missing 2nd number as well?
  205. number2 = 1;
  206. } else {
  207. number2 = std::stoi(std::string(cp, last));
  208. }
  209. } else {
  210. // Ok, find the first number
  211. number = std::stoi(std::string(cp, last));
  212. // covert iterator to position
  213. std::size_t pos = ansi.find(';', std::distance(ansi.begin(), cp));
  214. if (pos == std::string::npos) {
  215. // Missing 2nd number
  216. number2 = 1;
  217. } else {
  218. // 2nd number found, maybe.
  219. cp = ansi.begin() + pos;
  220. ++cp;
  221. if (cp == last) {
  222. number2 = 1;
  223. } else {
  224. number2 = std::stoi(std::string(cp, last));
  225. }
  226. }
  227. }
  228. }
  229. // Our positions start at zero, not one.
  230. this->posy = number - 1;
  231. this->posx = number2 - 1;
  232. return CURSOR;
  233. case 'J':
  234. // clear
  235. if (cp == last) {
  236. number = 0;
  237. } else {
  238. number = std::stoi(std::string(cp, last));
  239. };
  240. // clears ... part of the screen.
  241. if (number == 2) {
  242. this->posx = 0;
  243. this->posy = 0;
  244. };
  245. return CLEAR;
  246. case 'K':
  247. // clear line
  248. if (cp == last) {
  249. number = 0;
  250. } else {
  251. number = std::stoi(std::string(cp, last));
  252. };
  253. return CLEAR;
  254. case 'm':
  255. // color
  256. if (cp == last) {
  257. // nothing given, default to 0.
  258. number = 0;
  259. ansi_color(number);
  260. } else {
  261. while (cp != last) {
  262. number = std::stoi(std::string(cp, last));
  263. ansi_color(number);
  264. ++cp;
  265. while ((cp != last) && (isdigit(*cp))) {
  266. ++cp;
  267. };
  268. if (cp != last) {
  269. if (*cp == ';') {
  270. cp++;
  271. }
  272. }
  273. }
  274. }
  275. return COLOR;
  276. case 's':
  277. // save position
  278. saved_cursor_position.push_back(std::make_pair(this->posx, this->posy));
  279. return CURSOR;
  280. case 'u':
  281. // restore position
  282. if (saved_cursor_position.empty()) {
  283. ZF_LOGE("Restore cursor position from empty history.");
  284. } else {
  285. std::pair<int, int> pos = saved_cursor_position.back();
  286. this->posx = pos.first;
  287. this->posy = pos.second;
  288. saved_cursor_position.pop_back();
  289. }
  290. return CURSOR;
  291. case 't':
  292. case 'r':
  293. case 'h':
  294. case '!':
  295. // These are ones that I don't care about.
  296. case 'n': // This is terminal detect -- give me cursor position
  297. return OTHER;
  298. default:
  299. // unsure -- possibly not important
  300. ZF_LOGD("console_ansi( %s ): ???", repr(ansi.c_str()));
  301. return OTHER;
  302. }
  303. }
  304. }
  305. ZF_LOGD("console_ansi( %s ) : ???", repr(ansi.c_str()));
  306. return OTHER;
  307. }
  308. Terminal::termchar Terminal::putchar(char ch) {
  309. Terminal::termchar tc;
  310. if (this->in_ansi) {
  311. // Ok, append this char
  312. this->ansi.append(1, ch);
  313. // DCS (Designate Character Set)
  314. if (ansi[1] == '(') {
  315. if (ansi.size() == 3) {
  316. // End DCS
  317. switch (ansi[2]) {
  318. case '0':
  319. dcs_map = 0;
  320. break;
  321. case 'B':
  322. dcs_map = 0x0b;
  323. break;
  324. default:
  325. ZF_LOGE("Unknown DCS Mode ^ESC(%c Selected!", ansi[2]);
  326. // Use our default, but we know it's wrong!
  327. dcs_map = 0x0b;
  328. break;
  329. }
  330. this->in_ansi = 0;
  331. this->ansi.clear();
  332. tc.in_ansi = 1;
  333. tc.ansi = DCS;
  334. return tc;
  335. }
  336. }
  337. if (isalpha(ch)) {
  338. // Ok! end of ANSI code, process it.
  339. tc.ansi = ansi_code(this->ansi);
  340. this->in_ansi = 0;
  341. this->ansi.clear();
  342. tc.in_ansi = 1;
  343. return tc;
  344. }
  345. tc.ansi = START;
  346. tc.in_ansi = 1;
  347. return tc;
  348. } else {
  349. tc.ansi = START;
  350. tc.in_ansi = 0;
  351. if (ch == '\x1b') {
  352. this->ansi.append(1, ch);
  353. this->in_ansi = 1;
  354. tc.in_ansi = 1;
  355. return tc;
  356. }
  357. // should I try reporting MOTION non-ANSI ?
  358. if (ch == '\r') {
  359. // Carriage return
  360. this->posx = 0;
  361. return tc;
  362. }
  363. if (ch == '\n') {
  364. this->posy++;
  365. // check range/"scroll"
  366. return tc;
  367. }
  368. if (ch == '\b') {
  369. // Backspace.
  370. if (this->posx > 0) {
  371. this->posx--;
  372. }
  373. return tc;
  374. }
  375. if (ch == '\f') {
  376. // form feed
  377. // treat as clear screen
  378. this->posx = 0;
  379. this->posy = 0;
  380. return tc;
  381. }
  382. /*
  383. I don't believe that anything else can possibly be here, other then an
  384. actual printable character. So!
  385. FUTURE: Store the screen text + colors
  386. */
  387. this->posx++;
  388. if (this->posx > 79) {
  389. this->posx = 0;
  390. this->posy++;
  391. // check range/"scroll"
  392. }
  393. return tc;
  394. }
  395. }
  396. void Terminal::putstring(std::string text) {
  397. // This gets noisy when called from render effect ^D
  398. // ZF_LOGI("console_char %lu chars", chars.size());
  399. for (auto strit = text.begin(); strit != text.end(); strit++)
  400. putchar(*strit);
  401. }
  402. // Old non-C++ ways
  403. void console_init(struct console_details *cdp) {
  404. cdp->posx = 0;
  405. cdp->posy = 0;
  406. cdp->savedx = 0;
  407. cdp->savedy = 0;
  408. cdp->in_ansi = 0;
  409. cdp->fgcolor = 7;
  410. cdp->bgcolor = 0;
  411. cdp->status = 0;
  412. }
  413. /*
  414. * Given the ansi number codes
  415. * Figure out fg, bg and status.
  416. */
  417. void ansi_color(struct console_details *cdp, int color) {
  418. // There are many calls to this.
  419. // ZF_LOGV("ansi_color(%d)", color);
  420. if (color == 0) {
  421. cdp->status = 0;
  422. cdp->fgcolor = 7;
  423. cdp->bgcolor = 0;
  424. return;
  425. }
  426. if ((color == 1) || (color == 2) || (color == 3) || (color == 4) ||
  427. (color == 5)) {
  428. cdp->status = color;
  429. return;
  430. }
  431. if ((color >= 30) && (color <= 37)) {
  432. cdp->fgcolor = color - 30;
  433. return;
  434. }
  435. if ((color >= 40) && (color <= 47)) {
  436. cdp->bgcolor = color - 40;
  437. return;
  438. }
  439. if (color == 39) {
  440. // default fg color
  441. cdp->fgcolor = 7;
  442. return;
  443. }
  444. if (color == 49) {
  445. // default bg color
  446. cdp->bgcolor = 0;
  447. return;
  448. }
  449. ZF_LOGD("ansi_color( %d ) is unknown to me.", color);
  450. }
  451. const char *color_restore(struct console_details *cdp) {
  452. static char buffer[30];
  453. int slen;
  454. // possible optimize: If fg is 7, don't set (0 reset does that), if bg is 0,
  455. // don't set (0 does that)
  456. if (cdp->status == 0) {
  457. slen = snprintf(buffer, sizeof(buffer), "\x1b[0;3%d;4%dm", cdp->fgcolor,
  458. cdp->bgcolor);
  459. if (slen >= (int)sizeof(buffer)) {
  460. ZF_LOGE("snprintf %d > size %d", slen, (int)sizeof(buffer));
  461. buffer[0] = 0;
  462. }
  463. } else {
  464. slen = snprintf(buffer, sizeof(buffer), "\x1b[0;%d;3%d;4%dm", cdp->status,
  465. cdp->fgcolor, cdp->bgcolor);
  466. if (slen >= (int)sizeof(buffer)) {
  467. ZF_LOGE("snprintf %d > size %d", slen, (int)sizeof(buffer));
  468. buffer[0] = 0;
  469. }
  470. };
  471. return buffer;
  472. }
  473. /* store cursor position X,Y */
  474. std::vector<std::pair<int, int>> cursor_position_history;
  475. ANSI_TYPE console_ansi(struct console_details *cdp, const char *ansi) {
  476. const char *cp = ansi;
  477. const char *last = ansi + strlen(ansi) - 1;
  478. int number, number2;
  479. if (*cp == '\x1b') {
  480. cp++;
  481. // Ok, that's expected.
  482. if (*cp == '[') {
  483. cp++;
  484. // https://en.wikipedia.org/wiki/ANSI_escape_code#CSI_sequences
  485. switch (*last) {
  486. case 'A':
  487. // cursor up
  488. if (cp == last) {
  489. number = 1;
  490. } else {
  491. number = atoi(cp);
  492. if (number < 1) {
  493. ZF_LOGD("console_ansi( %s ): number error (%d)", repr(ansi),
  494. number);
  495. number = 1;
  496. }
  497. };
  498. cdp->posy -= number;
  499. if (cdp->posy < 0) {
  500. cdp->posy = 0;
  501. ZF_LOGD(
  502. "console_ansi( %s ): attempt to move above top of screen (%d)",
  503. repr(ansi), number);
  504. }
  505. return CURSOR;
  506. case 'B':
  507. // cursor down
  508. if (cp == last) {
  509. number = 1;
  510. } else {
  511. number = atoi(cp);
  512. if (number < 1) {
  513. ZF_LOGD("console_ansi( %s ): number error (%d)", repr(ansi),
  514. number);
  515. number = 1;
  516. }
  517. };
  518. cdp->posy += number;
  519. // check range/"scroll"
  520. return CURSOR;
  521. case 'C':
  522. // cursor forward
  523. if (cp == last) {
  524. number = 1;
  525. } else {
  526. number = atoi(cp);
  527. if (number < 1) {
  528. ZF_LOGD("console_ansi( %s ): number error (%d)", repr(ansi),
  529. number);
  530. number = 1;
  531. }
  532. };
  533. cdp->posx += number;
  534. // Well. According to the "spec", the screen limits are hard
  535. // If the cursor is already at the edge of the screen, this has no
  536. // effect. ^ This is *NOT* how any ANSI BBS terminal program acts!
  537. while (cdp->posx > 79) {
  538. cdp->posy++;
  539. // check range/"scroll"
  540. cdp->posx -= 79;
  541. }
  542. return CURSOR;
  543. case 'D':
  544. // cursor backwards
  545. if (cp == last) {
  546. number = 1;
  547. } else {
  548. number = atoi(cp);
  549. if (number < 1) {
  550. ZF_LOGD("console_ansi( %s ): number error (%d)", repr(ansi),
  551. number);
  552. number = 0;
  553. }
  554. };
  555. cdp->posx -= number;
  556. // Well. According to the "spec", the screen limits are hard
  557. // If the cursor is already at the edge of the screen, this has no
  558. // effect. ^ This is *NOT* how any ANSI BBS terminal program acts!
  559. while (cdp->posx < 0) {
  560. cdp->posy--;
  561. if (cdp->posy < 0) {
  562. cdp->posy = 0;
  563. }
  564. cdp->posx += 79;
  565. }
  566. return CURSOR;
  567. case 'H':
  568. // cursor position
  569. if (*cp == ';') {
  570. // Missing first number
  571. number = 1;
  572. cp++;
  573. if (cp == last) {
  574. // missing 2nd number as well?
  575. number2 = 1;
  576. } else {
  577. number2 = atoi(cp);
  578. }
  579. } else {
  580. // Ok, find the first number
  581. number = atoi(cp);
  582. cp = strchr(cp, ';');
  583. if (cp == NULL) {
  584. // Missing 2nd number
  585. number2 = 1;
  586. } else {
  587. // 2nd number found, maybe.
  588. cp++;
  589. if (cp == last) {
  590. number2 = 1;
  591. } else {
  592. number2 = atoi(cp);
  593. }
  594. }
  595. }
  596. // Our positions start at zero, not one.
  597. cdp->posy = number - 1;
  598. cdp->posx = number2 - 1;
  599. return CURSOR;
  600. case 'J':
  601. // clear
  602. if (cp == last) {
  603. number = 0;
  604. } else {
  605. number = atoi(cp);
  606. };
  607. // clears ... part of the screen.
  608. if (number == 2) {
  609. cdp->posx = 0;
  610. cdp->posy = 0;
  611. };
  612. return CLEAR;
  613. case 'K':
  614. // clear line
  615. if (cp == last) {
  616. number = 0;
  617. } else {
  618. number = atoi(cp);
  619. };
  620. return CLEAR;
  621. case 'm':
  622. // color
  623. if (cp == last) {
  624. // nothing given, default to 0.
  625. number = 0;
  626. ansi_color(cdp, number);
  627. } else {
  628. while (cp != last) {
  629. number = atoi(cp);
  630. ansi_color(cdp, number);
  631. cp++;
  632. while ((cp != last) && (isdigit(*cp))) {
  633. cp++;
  634. };
  635. if (cp != last) {
  636. if (*cp == ';') {
  637. cp++;
  638. }
  639. }
  640. }
  641. }
  642. return COLOR;
  643. case 's':
  644. // save position
  645. cursor_position_history.push_back(std::make_pair(cdp->posx, cdp->posy));
  646. return CURSOR;
  647. case 'u':
  648. // restore position
  649. if (cursor_position_history.empty()) {
  650. ZF_LOGE("Restore cursor position from empty history.");
  651. } else {
  652. std::pair<int, int> pos = cursor_position_history.back();
  653. cdp->posx = pos.first;
  654. cdp->posy = pos.second;
  655. cursor_position_history.pop_back();
  656. }
  657. return CURSOR;
  658. case 't':
  659. case 'r':
  660. case 'h':
  661. case '!':
  662. // These are ones that I don't care about.
  663. case 'n': // This is terminal detect -- give me cursor position
  664. return OTHER;
  665. default:
  666. // unsure -- possibly not important
  667. ZF_LOGD("console_ansi( %s ): ???", repr(ansi));
  668. return OTHER;
  669. }
  670. }
  671. }
  672. ZF_LOGD("console_ansi( %s ) : ???", repr(ansi));
  673. return OTHER;
  674. }
  675. /*
  676. * console_char()
  677. * return whether or not we are still in_ansi
  678. *
  679. * in_ansi TRUE: START (receiving start of/fragment of ANSI)
  680. * CURSOR, COLOR, CLEAR, OTHER
  681. * Results of the last ANSI parse.
  682. * in_ansi FALSE: START Normal character.
  683. *
  684. */
  685. termchar console_char(struct console_details *cdp, char ch) {
  686. char *cp;
  687. termchar tc;
  688. if (cdp->in_ansi) {
  689. // Ok, append this char
  690. cp = cdp->ansi + strlen(cdp->ansi);
  691. *cp = ch;
  692. cp++;
  693. *cp = 0;
  694. if (isalpha(ch)) {
  695. // Ok! end of ANSI code, process it.
  696. tc.ansi = console_ansi(cdp, cdp->ansi);
  697. cdp->in_ansi = 0;
  698. cdp->ansi[0] = 0;
  699. tc.in_ansi = 1;
  700. return tc;
  701. }
  702. tc.ansi = START;
  703. tc.in_ansi = 1;
  704. return tc;
  705. } else {
  706. tc.ansi = START;
  707. tc.in_ansi = 0;
  708. if (ch == '\x1b') {
  709. cp = cdp->ansi;
  710. *cp = ch;
  711. cp++;
  712. *cp = 0;
  713. cdp->in_ansi = 1;
  714. tc.in_ansi = 1;
  715. return tc;
  716. }
  717. // should I try reporting MOTION non-ANSI ?
  718. if (ch == '\r') {
  719. // Carriage return
  720. cdp->posx = 0;
  721. return tc;
  722. }
  723. if (ch == '\n') {
  724. cdp->posy++;
  725. // check range/"scroll"
  726. return tc;
  727. }
  728. if (ch == '\b') {
  729. // Backspace.
  730. if (cdp->posx > 0) {
  731. cdp->posx--;
  732. }
  733. return tc;
  734. }
  735. if (ch == '\f') {
  736. // form feed
  737. // treat as clear screen
  738. cdp->posx = 0;
  739. cdp->posy = 0;
  740. return tc;
  741. }
  742. /*
  743. I don't believe that anything else can possibly be here, other then an
  744. actual printable character. So!
  745. FUTURE: Store the screen text + colors
  746. */
  747. cdp->posx++;
  748. if (cdp->posx > 79) {
  749. cdp->posx = 0;
  750. cdp->posy++;
  751. // check range/"scroll"
  752. }
  753. return tc;
  754. }
  755. }
  756. void console_receive(struct console_details *cdp, std::string chars) {
  757. // This gets noisy when called from render effect ^D
  758. // ZF_LOGI("console_char %lu chars", chars.size());
  759. for (auto strit = chars.begin(); strit != chars.end(); strit++)
  760. console_char(cdp, *strit);
  761. }