mystic.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <unistd.h> // usleep()
  4. #include <fcntl.h>
  5. #include <pty.h>
  6. #include <termios.h>
  7. #include <sys/select.h>
  8. #include <sys/wait.h>
  9. #include <signal.h> // handle Ctrl-C/SIGINT
  10. #include <strings.h> // strcasecmp
  11. #include <time.h> // usleep(), nanonsleep() ?
  12. #include <ctype.h>
  13. #include <stdlib.h> // random()
  14. #include <regex.h>
  15. // LOGGING with file output
  16. #include "zf_log.h"
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. FILE *g_log_file;
  20. static void file_output_callback(const zf_log_message *msg, void *arg) {
  21. (void)arg;
  22. *msg->p = '\n';
  23. fwrite(msg->buf, msg->p - msg->buf + 1, 1, g_log_file);
  24. fflush(g_log_file);
  25. }
  26. static void file_output_close(void) { fclose(g_log_file); }
  27. static void file_output_open(const char *const log_path) {
  28. g_log_file = fopen(log_path, "a");
  29. if (!g_log_file) {
  30. ZF_LOGW("Failed to open log file %s", log_path);
  31. return;
  32. }
  33. atexit(file_output_close);
  34. zf_log_set_output_v(ZF_LOG_PUT_STD, 0, file_output_callback);
  35. }
  36. // END LOGGING
  37. /*
  38. What is the name of the actual, real Mystic executable
  39. that we'll be executing and mangling?
  40. */
  41. #define TARGET "./mySTIC"
  42. // Size of our input and output buffers.
  43. #define BSIZE 128
  44. /*
  45. // Don't need this, zf_log does date/time stamps on output.
  46. const char * it_is_now(void) {
  47. static char buffer[100];
  48. time_t timer;
  49. struct tm* tm_info;
  50. timer = time(NULL);
  51. tm_info = localtime(&timer);
  52. strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", tm_info);
  53. return buffer;
  54. }
  55. void slow_write(int fd, int speed, char * buffer, int len) {
  56. int x;
  57. for( x = 0; x < len; x++) {
  58. usleep(speed);
  59. write( fd, &buffer[x], 1);
  60. }
  61. }
  62. */
  63. /**
  64. * Display a repr of the given string.
  65. *
  66. * This converts most \n\r\v\f\t codes,
  67. * defaults to \xHH (hex value).
  68. */
  69. const char *repr(const char *data) {
  70. static char buffer[4096];
  71. char *cp;
  72. strcpy(buffer, data);
  73. cp = buffer;
  74. while (*cp != 0) {
  75. char c = *cp;
  76. if (isspace(c)) {
  77. if (c == ' ') {
  78. cp++;
  79. continue;
  80. };
  81. /* Ok, it's form-feed ('\f'), newline ('\n'), carriage return ('\r'),
  82. * horizontal tab ('\t'), and vertical tab ('\v') */
  83. memmove(cp + 1, cp, strlen(cp) + 1);
  84. *cp = '\\';
  85. cp++;
  86. switch (c) {
  87. case '\f':
  88. *cp = 'f';
  89. cp++;
  90. break;
  91. case '\n':
  92. *cp = 'n';
  93. cp++;
  94. break;
  95. case '\r':
  96. *cp = 'r';
  97. cp++;
  98. break;
  99. case '\t':
  100. *cp = 't';
  101. cp++;
  102. break;
  103. case '\v':
  104. *cp = 'v';
  105. cp++;
  106. break;
  107. default:
  108. *cp = '?';
  109. cp++;
  110. break;
  111. }
  112. continue;
  113. }
  114. if (isprint(c)) {
  115. cp++;
  116. continue;
  117. };
  118. // Ok, default to \xHH output.
  119. memmove(cp + 3, cp, strlen(cp) + 1);
  120. *cp = '\\';
  121. cp++;
  122. *cp = 'x';
  123. cp++;
  124. char buffer[3];
  125. sprintf(buffer, "%02x", (int)c & 0xff);
  126. *cp = buffer[0];
  127. cp++;
  128. *cp = buffer[1];
  129. cp++;
  130. continue;
  131. }
  132. return buffer;
  133. }
  134. struct render {
  135. int speed;
  136. int effect;
  137. } current_render;
  138. int render_overlimit = 0;
  139. void reset_render(void) {
  140. current_render.speed = 0;
  141. current_render.effect = 0;
  142. render_overlimit = 0;
  143. }
  144. #define TRIGGER "^"
  145. // Max limit we'll sleep before ignoring effects/speed.
  146. #define SLEEP_LIMIT 30
  147. int ms_sleep(unsigned int ms) {
  148. int result = 0;
  149. struct timespec ts = {ms / 1000, (ms % 1000) * 1000000L};
  150. do {
  151. struct timespec ts_sleep = ts;
  152. result = nanosleep(&ts_sleep, &ts);
  153. } while ((-1 == result));
  154. return result;
  155. }
  156. void render_sleep(void) {
  157. if (render_overlimit)
  158. return;
  159. if (current_render.speed) { // * 100 still too slow.
  160. ms_sleep(current_render.speed * 10);
  161. }
  162. }
  163. /*
  164. Terminal tracking
  165. */
  166. struct console_details {
  167. int posx, posy;
  168. int savedx, savedy;
  169. char ansi[20]; // current ANSI command being processed.
  170. int in_ansi;
  171. int fgcolor; // 0-15
  172. int bgcolor; // 0-7
  173. int status; // 0 or 5 (Blink)
  174. } console;
  175. void console_init(struct console_details *cdp) {
  176. cdp->posx = 0;
  177. cdp->posy = 0;
  178. cdp->savedx = 0;
  179. cdp->savedy = 0;
  180. cdp->in_ansi = 0;
  181. cdp->fgcolor = 0;
  182. cdp->bgcolor = 0;
  183. cdp->status = 0;
  184. }
  185. void console_ansi(const char *ansi) {
  186. int understood = 0;
  187. const char *cp = ansi;
  188. const char *last = ansi + strlen(ansi) - 1;
  189. int number, number2;
  190. if (*cp == '\x1b') {
  191. cp++;
  192. // Ok, that's expected.
  193. if (*cp == '[') {
  194. cp++;
  195. // https://en.wikipedia.org/wiki/ANSI_escape_code#CSI_sequences
  196. switch (*last) {
  197. case 'A':
  198. // cursor up
  199. if (cp == last) {
  200. number = 1;
  201. } else {
  202. number = atoi(cp);
  203. if (number < 1) {
  204. ZF_LOGD("console_ansi( %s ): number error (%d)", repr(ansi),
  205. number);
  206. number = 1;
  207. }
  208. };
  209. console.posy -= number;
  210. if (console.posy < 0) {
  211. console.posy = 0;
  212. ZF_LOGD(
  213. "console_ansi( %s ): attempt to move above top of screen (%d)",
  214. repr(ansi), number);
  215. }
  216. understood = 1;
  217. return;
  218. case 'B':
  219. // cursor down
  220. if (cp == last) {
  221. number = 1;
  222. } else {
  223. number = atoi(cp);
  224. if (number < 1) {
  225. ZF_LOGD("console_ansi( %s ): number error (%d)", repr(ansi),
  226. number);
  227. number = 1;
  228. }
  229. };
  230. console.posy += number;
  231. // check range/"scroll"
  232. understood = 1;
  233. return;
  234. case 'C':
  235. // cursor forward
  236. if (cp == last) {
  237. number = 1;
  238. } else {
  239. number = atoi(cp);
  240. if (number < 1) {
  241. ZF_LOGD("console_ansi( %s ): number error (%d)", repr(ansi),
  242. number);
  243. number = 1;
  244. }
  245. };
  246. console.posx += number;
  247. // Well. According to the "spec", the screen limits are hard
  248. // If the cursor is already at the edge of the screen, this has no
  249. // effect. ^ This is *NOT* how any ANSI BBS terminal program acts!
  250. while (console.posx > 79) {
  251. console.posy++;
  252. // check range/"scroll"
  253. console.posx -= 79;
  254. }
  255. understood = 1;
  256. return;
  257. case 'D':
  258. // cursor backwards
  259. if (cp == last) {
  260. number = 1;
  261. } else {
  262. number = atoi(cp);
  263. if (number < 1) {
  264. ZF_LOGD("console_ansi( %s ): number error (%d)", repr(ansi),
  265. number);
  266. number = 1;
  267. }
  268. };
  269. console.posx -= number;
  270. // Well. According to the "spec", the screen limits are hard
  271. // If the cursor is already at the edge of the screen, this has no
  272. // effect. ^ This is *NOT* how any ANSI BBS terminal program acts!
  273. while (console.posx < 0) {
  274. console.posy--;
  275. if (console.posy < 0) {
  276. console.posy = 0;
  277. }
  278. console.posx += 79;
  279. }
  280. understood = 1;
  281. return;
  282. case 'H':
  283. // cursor position
  284. if (*cp == ';') {
  285. // Missing first number
  286. number = 1;
  287. cp++;
  288. if (cp == last) {
  289. // missing 2nd number as well?
  290. number2 = 1;
  291. } else {
  292. number2 = atoi(cp);
  293. }
  294. } else {
  295. // Ok, find the first number
  296. number = atoi(cp);
  297. cp = strchr(cp, ';');
  298. if (cp == NULL) {
  299. // Missing 2nd number
  300. number2 = 1;
  301. } else {
  302. // 2nd number found, maybe.
  303. cp++;
  304. if (cp == last) {
  305. number2 = 1;
  306. } else {
  307. number2 = atoi(cp);
  308. }
  309. }
  310. }
  311. // Our positions start at zero, not one.
  312. console.posx = number - 1;
  313. console.posy = number2 - 1;
  314. understood = 1;
  315. break;
  316. case 'J':
  317. // clear
  318. if (cp == last) {
  319. number = 0;
  320. } else {
  321. number = atoi(cp);
  322. };
  323. // clears ... part of the screen.
  324. if (number == 2) {
  325. console.posx = 0;
  326. console.posy = 0;
  327. };
  328. understood = 1;
  329. break;
  330. default:
  331. // unsure -- possibly not important
  332. ZF_LOGD("console_ansi( %s ): ???", repr(ansi));
  333. understood = 0;
  334. }
  335. }
  336. };
  337. if (!understood) {
  338. ZF_LOGD("console_ansi( %s ): was not understood.", repr(ansi));
  339. }
  340. }
  341. void console_char(char ch) {
  342. char *cp;
  343. if (console.in_ansi) {
  344. // Ok, append this char
  345. cp = console.ansi + strlen(console.ansi);
  346. *cp = ch;
  347. cp++;
  348. *cp = 0;
  349. if (isalpha(ch)) {
  350. // Ok!
  351. console_ansi(console.ansi);
  352. console.in_ansi = 0;
  353. console.ansi[0] = 0;
  354. return;
  355. }
  356. } else {
  357. if (ch == '\x1b') {
  358. cp = console.ansi;
  359. *cp = ch;
  360. cp++;
  361. *cp = 0;
  362. console.in_ansi = 1;
  363. return;
  364. }
  365. if (ch == '\r') {
  366. // Carriage return
  367. console.posx = 0;
  368. return;
  369. }
  370. if (ch == '\n') {
  371. console.posy++;
  372. // check range/"scroll"
  373. return;
  374. }
  375. if (ch == '\b') {
  376. // Backspace.
  377. if (console.posx > 0) {
  378. console.posx--;
  379. }
  380. return;
  381. }
  382. if (ch == '\f') {
  383. // form feed
  384. // treat as clear screen
  385. console.posx = 0;
  386. console.posy = 0;
  387. return;
  388. }
  389. /*
  390. I don't believe that anything else can possibly be here, other then an
  391. actual printable character. So!
  392. */
  393. console.posx++;
  394. if (console.posx > 79) {
  395. console.posx = 0;
  396. console.posy++;
  397. // check range/"scroll"
  398. }
  399. }
  400. }
  401. void console_string(const char *chars) {
  402. int x;
  403. for (x = 0; x < strlen(chars); x++) {
  404. console_char(chars[x]);
  405. }
  406. }
  407. void console_receive(const char *chars, int len) {
  408. int x;
  409. for (x = 0; x < len; x++) {
  410. console_char(chars[x]);
  411. }
  412. }
  413. /*
  414. Well SNAP! Mystic numbers don't remotely match ANSI color codes.
  415. 00 : Sets the current foreground to Black 0;30
  416. 01 : Sets the current foreground to Dark Blue 0;34
  417. 02 : Sets the current foreground to Dark Green 0;32
  418. 03 : Sets the current foreground to Dark Cyan 0;36
  419. 04 : Sets the current foreground to Dark Red 0;31
  420. 05 : Sets the current foreground to Dark Magenta 0;35
  421. 06 : Sets the current foreground to Brown 0;33
  422. 07 : Sets the current foreground to Grey 0;37
  423. 08 : Sets the current foreground to Dark Grey 1;30
  424. 09 : Sets the current foreground to Light Blue 1;34
  425. 10 : Sets the current foreground to Light Green 1;32
  426. 11 : Sets the current foreground to Light Cyan 1;36
  427. 12 : Sets the current foreground to Light Red 1;31
  428. 13 : Sets the current foreground to Light Magenta 1;35
  429. 14 : Sets the current foreground to Yellow 1;33
  430. 15 : Sets the current foreground to White 1;37
  431. 16 : Sets the current background to Black 40
  432. 17 : Sets the current background to Blue 44
  433. 18 : Sets the current background to Green 42
  434. 19 : Sets the current background to Cyan 46
  435. 20 : Sets the current background to Red 41
  436. 21 : Sets the current background to Magenta 45
  437. 22 : Sets the current background to Brown 43
  438. 23 : Sets the current background to Grey 47
  439. 24 : Sets the current background to black with blinking foreground 5;40
  440. 25 : Sets the current background to blue with blinking foreground 5;44
  441. 26 : Sets the current background to green with blinking foreground 5;42
  442. 27 : Sets the current background to cyan with blinking foreground 5;46
  443. 28 : Sets the current background to red with blinking foreground 5;41
  444. 29 : Sets the current background to magenta with blinking foreground 5;45
  445. 30 : Sets the current background to brown with blinking foreground 5;43
  446. 31 : Sets the current background to grey with blinking foreground 5;47
  447. Other things that Mystic does ...
  448. [A## - Move the cursor up ## lines
  449. [B## - Move the cursor down ## lines
  450. [C## - Move the cursor forward (to the right) ## columns
  451. [D## - Move the cursor backwards (to the left) ## columns
  452. [K - Clear from the current cursor position to the end of the line
  453. [L - Move cursor and erase data backwards from current column to column ##
  454. [X## - Move cursor to X coordinate ##
  455. [Y## - Move cursor to Y coordinate ##
  456. BS - Sends 1 destructive backspace sequence (ASCII 8-32-8)
  457. CL - Clears the screen (ANSI 1,1 locate and [2J or ASCII 12)
  458. CR - Send a carrage return and line feed (move to next line)
  459. RA - Restore the saved text attribute color
  460. RS - Restore the saved user's terminal screen
  461. SA - Save the current text attribute color
  462. SS - Save the entire user's terminal screen
  463. */
  464. // Covert MYSTIC color to (Proper) ANSI COLOR.
  465. const int MYSTIC[] = {0, 4, 2, 6, 1, 5, 3, 7};
  466. // ANSI_color = MYSTIC[ odd_mystic_color % 8 ]
  467. void write_color(int fd, int color) {
  468. char buffer[10];
  469. switch (color) {
  470. case 0:
  471. case 1:
  472. case 2:
  473. case 3:
  474. case 4:
  475. case 5:
  476. case 6:
  477. case 7:
  478. sprintf(buffer, "\x1b[0;3%dm", MYSTIC[color]);
  479. break;
  480. case 8:
  481. case 9:
  482. case 10:
  483. case 11:
  484. case 12:
  485. case 13:
  486. case 14:
  487. case 15:
  488. sprintf(buffer, "\x1b[1;3%dm", MYSTIC[color - 8]);
  489. break;
  490. case 16:
  491. case 17:
  492. case 18:
  493. case 19:
  494. case 20:
  495. case 21:
  496. case 22:
  497. case 23:
  498. sprintf(buffer, "\x1b[4%dm", MYSTIC[color - 16]);
  499. break;
  500. case 24:
  501. case 25:
  502. case 26:
  503. case 27:
  504. case 28:
  505. case 29:
  506. case 30:
  507. case 31:
  508. sprintf(buffer, "\x1b[5;4%dm", MYSTIC[color - 24]);
  509. break;
  510. default:
  511. buffer[0] = 0;
  512. break;
  513. }
  514. ZF_LOGD("write_color( %d ): %s", color, repr(buffer));
  515. write(fd, buffer, strlen(buffer));
  516. }
  517. /**
  518. * process_trigger( fd, *cp )
  519. *
  520. * This process a command trigger.
  521. * It has seen TRIGGER, and now it is
  522. * processing whatever comes after it.
  523. * It will perform the process, and
  524. * return the char * of whatever is next.
  525. */
  526. const char *process_trigger(int fd, const char *cp) {
  527. char ch;
  528. int i, x, y;
  529. ch = toupper(*cp);
  530. cp++;
  531. switch (ch) {
  532. case 'D':
  533. i = 0;
  534. if (isdigit(*cp)) {
  535. i = (*cp) - '0';
  536. cp++;
  537. };
  538. if (isdigit(*cp)) {
  539. i *= 10;
  540. i += (*cp) - '0';
  541. cp++;
  542. };
  543. if ((i > 0) && (i < 80)) {
  544. ZF_LOGI("DEL %02d", i);
  545. for (x = 0; x < i; x++) {
  546. write(fd, "\b \b", 3);
  547. }
  548. };
  549. break;
  550. case 'C':
  551. i = 0;
  552. if (isdigit(*cp)) {
  553. i = (*cp) - '0';
  554. cp++;
  555. };
  556. if (isdigit(*cp)) {
  557. i *= 10;
  558. i += (*cp) - '0';
  559. cp++;
  560. };
  561. write_color(fd, i);
  562. break;
  563. case 'G':
  564. x = 0;
  565. if (isdigit(*cp)) {
  566. x = (*cp) - '0';
  567. cp++;
  568. };
  569. if (isdigit(*cp)) {
  570. x *= 10;
  571. x += (*cp) - '0';
  572. cp++;
  573. };
  574. y = 0;
  575. if (isdigit(*cp)) {
  576. y = (*cp) - '0';
  577. cp++;
  578. };
  579. if (isdigit(*cp)) {
  580. y *= 10;
  581. y += (*cp) - '0';
  582. cp++;
  583. };
  584. break;
  585. case 'R':
  586. i = 0;
  587. if (isdigit(*cp)) {
  588. i = (*cp) - '0';
  589. cp++;
  590. };
  591. if ((i > 0) && (i < 10)) {
  592. ZF_LOGI("RENDER %d", i);
  593. current_render.effect = i;
  594. } else {
  595. current_render.effect = 0;
  596. }
  597. break;
  598. case 'S':
  599. i = 0;
  600. if (isdigit(*cp)) {
  601. i = (*cp) - '0';
  602. cp++;
  603. };
  604. if ((i > 0) && (i < 10)) {
  605. ZF_LOGI("SPEED %d", i);
  606. current_render.speed = i;
  607. } else {
  608. current_render.speed = 0;
  609. }
  610. break;
  611. case 'P':
  612. i = 0;
  613. if (isdigit(*cp)) {
  614. i = (*cp) - '0';
  615. cp++;
  616. };
  617. if ((i > 0) && (i < 10)) {
  618. ZF_LOGI("PAWS %d", i);
  619. // sleep(i);
  620. if (!render_overlimit) {
  621. sleep(i);
  622. };
  623. }
  624. break;
  625. }
  626. return cp;
  627. }
  628. /**
  629. * render_effect( fd, ch )
  630. *
  631. * Displays the given character with whatever
  632. * rendering effect is currently active.
  633. * (If any).
  634. */
  635. void render_effect(int fd, char ch) {
  636. int effect = current_render.effect;
  637. int l;
  638. char space = ' ';
  639. char bs = '\b';
  640. switch (effect) {
  641. case 1:
  642. // CHAR + SPC + BS
  643. render_sleep();
  644. write(fd, &ch, 1);
  645. render_sleep();
  646. write(fd, &space, 1);
  647. render_sleep();
  648. render_sleep();
  649. write(fd, &bs, 1);
  650. break;
  651. case 2:
  652. // CHAR + 8 spaces + 8 BS
  653. render_sleep();
  654. write(fd, &ch, 1);
  655. for (l = 0; l < 8; l++) {
  656. render_sleep();
  657. write(fd, &space, 1);
  658. }
  659. for (l = 0; l < 8; l++) {
  660. render_sleep();
  661. write(fd, &bs, 1);
  662. }
  663. break;
  664. case 0:
  665. default:
  666. // NORMAL
  667. render_sleep();
  668. write(fd, &ch, 1);
  669. break;
  670. }
  671. }
  672. /**
  673. * render( fd, string_out )
  674. *
  675. * Render an entire string.
  676. * Handles TRIGGER.
  677. * Renders with effects.
  678. */
  679. void render(int fd, const char *string_out) {
  680. const char *cp = string_out;
  681. const char *trigger = cp;
  682. time_t start = time(NULL);
  683. int elapsed;
  684. int over = 0;
  685. reset_render();
  686. ZF_LOGD("render(%d, %s)", fd, repr(string_out));
  687. // Check our time from time to time.
  688. // If we start running long, disable sleeps.
  689. while ((trigger = strstr(cp, TRIGGER)) != NULL) {
  690. // There is special things to handle in here.
  691. while (cp != trigger) {
  692. elapsed = time(NULL) - start;
  693. if (elapsed > SLEEP_LIMIT) {
  694. render_overlimit = 1;
  695. current_render.speed = 0;
  696. };
  697. // write(fd, cp, 1 );
  698. render_effect(fd, *cp);
  699. cp++;
  700. };
  701. // ZF_LOGI( "at trigger: (%s)", cp);
  702. cp += strlen(TRIGGER);
  703. // Ok, we're pointing at the trigger -- do something.
  704. cp = process_trigger(fd, cp);
  705. // ZF_LOGI( "after trigger: (%s)", cp);
  706. };
  707. // We still might be under a rendering effect.
  708. while (*cp != 0) {
  709. elapsed = time(NULL) - start;
  710. if (elapsed > SLEEP_LIMIT) {
  711. render_overlimit = 1;
  712. current_render.speed = 0;
  713. };
  714. // write(fd, cp, 1);
  715. render_effect(fd, *cp);
  716. cp++;
  717. }
  718. }
  719. // Beanzilla's no repeats
  720. /**
  721. * have_seen( list, len, item )
  722. *
  723. * Returns 1 (true) if item is in the list.
  724. * Rotates the list [x] = [x+1], and
  725. * list[len-1] = item, return 0 (false)
  726. */
  727. int have_seen(int *list, int len, int item) {
  728. int x;
  729. for (x = 0; x < len; x++) {
  730. if (list[x] == item) {
  731. return 1;
  732. }
  733. };
  734. // Ok, it is something different
  735. for (x = 0; x < len - 1; x++) {
  736. list[x] = list[x + 1];
  737. };
  738. list[x] = item;
  739. return 0;
  740. }
  741. /**
  742. * init_have_seen( list, len )
  743. *
  744. * Initialize the have_seen list with -1.
  745. * (-1 isn't a valid index, so we start
  746. * out with all invalid.)
  747. */
  748. void init_have_seen(int *list, int len) {
  749. int x;
  750. for (x = 0; x < len; x++) {
  751. list[x] = -1;
  752. }
  753. }
  754. /*
  755. These are harry "timeout" events.
  756. These happen when we've been sitting around awhile.
  757. */
  758. #define MAX_HARRY_EVENT_DUPS 2
  759. int last_seen_harry_event[MAX_HARRY_EVENT_DUPS];
  760. #ifdef CPP_MADMAN_STL_CODE
  761. const char *random_phrase(const char *words, int len, int last_seen) {
  762. // ooh. a map of char *s to last_seen_events. :P
  763. static map<const char *, array<int>> tracker;
  764. map<const char *, array<int>>::iterator it;
  765. array<int, last_seen> it = tracker.find(words);
  766. if (it == tracker.end()) {
  767. // key does not exist.
  768. array<int, last_seen> last;
  769. for (int i = 0; i < last_seen; i++) {
  770. last[i] = -1;
  771. };
  772. tracker.insert(words, last);
  773. it = tracker.find(words);
  774. };
  775. }
  776. #endif
  777. void harry_event(int fd) {
  778. // Make something happen
  779. char buffer[100];
  780. int r;
  781. // This is no where near finished, BUT!
  782. const char *phrases[] = {"Hahaha", "Snicker, snicker", "Boo!",
  783. "MeOW", "I see U", "Arrooo!",
  784. "Ahh-wooo!", "Aaaooo!"};
  785. const char *cp;
  786. // Remember the last phrase used,
  787. // and don't repeat (the last two)!
  788. do {
  789. r = random() % ((sizeof(phrases) / sizeof(char *)) - 1);
  790. } while (have_seen(last_seen_harry_event, MAX_HARRY_EVENT_DUPS, r));
  791. ZF_LOGD("%d => %d %d", r, last_seen_harry_event[0], last_seen_harry_event[1]);
  792. cp = phrases[r];
  793. int color = random() % 16;
  794. if (color == 0) {
  795. color++;
  796. } // If it's 0 let's make it 1.
  797. sprintf(buffer, "^S2^C%02d%s^P2^D%02d", color, cp, (int)strlen(cp));
  798. ZF_LOGD("harry_event: render(%d, \"%s\")", fd, buffer);
  799. render(fd, buffer);
  800. }
  801. void init_harry() {
  802. init_have_seen(last_seen_harry_event, MAX_HARRY_EVENT_DUPS);
  803. ZF_LOGD("init => %d %d", last_seen_harry_event[0], last_seen_harry_event[1]);
  804. }
  805. /*
  806. The code to get the username and fullname is useless on telnet
  807. connections.
  808. */
  809. char *username = NULL;
  810. char *fullname = NULL;
  811. /*
  812. Pascal String Copy. Copy from pascal string, to C String.
  813. First char is pascal string length. (Max 255).
  814. */
  815. void pcopy(char *pstring, char *str) {
  816. int len = (int)*pstring;
  817. strncpy(str, pstring + 1, len);
  818. str[len] = 0;
  819. }
  820. /*
  821. This only works for those few idiots that use the
  822. horribly broken SSH crap that Mystic uses.
  823. */
  824. int locate_user(const char *alias) {
  825. FILE *user;
  826. char buffer[0x600];
  827. char temp[100];
  828. user = fopen("data/users.dat", "rb");
  829. if (user == NULL)
  830. return 0;
  831. // Carry on!
  832. while (fread(buffer, 0x600, 1, user) == 1) {
  833. pcopy(buffer + 0x6d, temp);
  834. if (strcasecmp(temp, username) == 0) {
  835. pcopy(buffer + 0x8c, temp);
  836. fullname = strdup(temp);
  837. break;
  838. }
  839. /*
  840. printf("Alias: %s\n", temp);
  841. pcopy(buffer + 0x8c, temp );
  842. printf("Full Name: %s\n", temp );
  843. */
  844. }
  845. fclose(user);
  846. return 1;
  847. }
  848. // Buffers are BSIZE + 1, so a buffer that size can strcpy safely.
  849. regex_t ANSI;
  850. regex_t WORDS;
  851. regex_t WORD;
  852. int init_regex(void) {
  853. int ret;
  854. char ansi[] = "\x1b\[[0-9]+(;[0-9]+)*?[a-zA-Z]";
  855. char words[] = "[a-zA-Z]+( [a-zA-Z]+)+";
  856. char word[] = "[a-zA-Z]+";
  857. char errorbuf[100];
  858. if (ret = regcomp(&ANSI, ansi, REG_EXTENDED | REG_NEWLINE)) {
  859. regerror(ret, &ANSI, errorbuf, sizeof(errorbuf));
  860. ZF_LOGW("Regex %s failed to compile: %s", ansi, errorbuf);
  861. return 0;
  862. };
  863. if (ret = regcomp(&WORDS, words, REG_EXTENDED | REG_NEWLINE)) {
  864. regerror(ret, &WORDS, errorbuf, sizeof(errorbuf));
  865. ZF_LOGW("Regex %s failed to compile: %s", words, errorbuf);
  866. return 0;
  867. };
  868. if (ret = regcomp(&WORD, word, REG_EXTENDED | REG_NEWLINE)) {
  869. regerror(ret, &WORD, errorbuf, sizeof(errorbuf));
  870. ZF_LOGW("Regex %s failed to compile: %s", word, errorbuf);
  871. return 0;
  872. };
  873. return 1;
  874. }
  875. int regmatch(regex_t *preg, const char *string, size_t nmatch,
  876. regmatch_t pmatch[], int eflags) {
  877. // returns number of matches found. (Max nmatch)
  878. int matches = 0;
  879. int offset = 0;
  880. int ret;
  881. while (matches < nmatch) {
  882. ret = regexec(preg, string + offset, nmatch - matches, pmatch + matches,
  883. eflags);
  884. if (!ret) {
  885. int current = offset;
  886. offset += pmatch[matches].rm_eo;
  887. pmatch[matches].rm_so += current;
  888. pmatch[matches].rm_eo += current;
  889. matches++;
  890. } else if (ret == REG_NOMATCH) {
  891. break;
  892. } else {
  893. break;
  894. }
  895. }
  896. return matches;
  897. }
  898. #define MAX_MATCH 32
  899. regmatch_t rxmatch[MAX_MATCH];
  900. int rx_match(regex_t *regex, const char *buffer) {
  901. int ret;
  902. ret = regmatch(regex, buffer, MAX_MATCH, rxmatch, 0);
  903. if (0) {
  904. for (int i = 0; i < ret; i++) {
  905. ZF_LOGI("%d : (%d-%d)", i, rxmatch[i].rm_so, rxmatch[i].rm_eo);
  906. }
  907. }
  908. return ret;
  909. }
  910. /*
  911. Terminal processing section.
  912. Monitor lines, position, color.
  913. What screen size do I want to emulate?
  914. ANSI codes.
  915. Do I need this??
  916. */
  917. /**
  918. * random_activate()
  919. *
  920. * Is a weight (1-10),
  921. * tests if random number is < weight * 10.
  922. *
  923. * So random_activate(9) happens more frequently
  924. * then random_activate(8) or lower.
  925. *
  926. * This probably needs to be fixed.
  927. * We need a better randint(RANGE) code.
  928. */
  929. int random_activate(int w) {
  930. int r = random() % 100;
  931. if (r <= (w * 10)) {
  932. return 1;
  933. };
  934. return 0;
  935. }
  936. /*
  937. word_state():
  938. -1 only lower
  939. +1 only upper
  940. 0 mixed
  941. */
  942. int word_state(const char *buffer, int len) {
  943. int p;
  944. int upper = 0;
  945. int lower = 0;
  946. int ret;
  947. float pct;
  948. for (p = 0; p < len; p++) {
  949. char c = buffer[p];
  950. if (isalpha(c)) {
  951. if (isupper(c)) {
  952. upper++;
  953. };
  954. if (islower(c)) {
  955. lower++;
  956. };
  957. }
  958. }
  959. if (upper == lower) {
  960. return 0;
  961. }
  962. if (upper > lower) {
  963. ret = 1;
  964. pct = ((float)lower / (float)upper) * 100.0;
  965. } else {
  966. ret = -1;
  967. pct = ((float)upper / (float)lower) * 100.0;
  968. }
  969. // ZF_LOGD("So far %d with %f %%", ret, pct);
  970. // They must be > 75%, otherwise return "mixed".
  971. if (pct < 40.0) {
  972. return ret;
  973. }
  974. return 0;
  975. }
  976. /*
  977. Given a buffer and length, mangle away.
  978. We *REALLY* want this to do something, so better run some tests.
  979. Or, maybe we don't. This treats words consistently the same way.
  980. HMM.
  981. toupper, tolower, flipper
  982. */
  983. int word_mangler(char *buffer, int len) {
  984. int p;
  985. int count = 0;
  986. int state;
  987. char c;
  988. state = word_state(buffer, len);
  989. // ZF_LOGD("word_state(%.*s) %d", len, buffer, state);
  990. // TODO: Transposer
  991. for (p = 0; p < len; p++) {
  992. c = buffer[p];
  993. switch (state) {
  994. case -1:
  995. // upper
  996. if (islower(c)) {
  997. count++;
  998. buffer[p] = toupper(c);
  999. }
  1000. break;
  1001. case 1:
  1002. // lower
  1003. if (isupper(c)) {
  1004. count++;
  1005. buffer[p] = tolower(c);
  1006. }
  1007. break;
  1008. case 0:
  1009. // flipper
  1010. if (islower(c)) {
  1011. count++;
  1012. buffer[p] = toupper(c);
  1013. } else {
  1014. if (isupper(c)) {
  1015. count++;
  1016. buffer[p] = tolower(c);
  1017. }
  1018. }
  1019. break;
  1020. }
  1021. }
  1022. return count;
  1023. }
  1024. /*
  1025. * The buffer that we've been given is much larger now.
  1026. *
  1027. */
  1028. int mangle(int fd, char *buffer) {
  1029. int x, i;
  1030. int need_render = 0; // changing word case around doesn't need the render
  1031. int mangled = 0;
  1032. int mangled_chars = 0;
  1033. char *cp;
  1034. // Ok, we want to look for words to:
  1035. // MaNGlE , or transpose (both?!)
  1036. // or possibly transpose words
  1037. char work[(BSIZE * 4) + 1];
  1038. ZF_LOGI("mangle(%s)", repr(buffer));
  1039. strcpy(work, buffer);
  1040. /*
  1041. NOTE: We copy the buffer, so we can clear out ANSI codes, etc.
  1042. Otherwise we might mess some ANSI up in the manglying
  1043. process.
  1044. */
  1045. /*
  1046. (random) Look for ANSI CLS and:
  1047. display random spooky texts around, with delays ... then CLS.
  1048. display ANSI graphic file, with delays ... then CLS
  1049. */
  1050. const char *ANSI_CLS = "\x1b[2J";
  1051. cp = strstr(buffer, ANSI_CLS);
  1052. if (cp != NULL) {
  1053. ZF_LOGI("seen: ANSI_CLS");
  1054. if (random_activate(9)) {
  1055. int r;
  1056. int color;
  1057. char display[100] = "";
  1058. const char *phrasing[] = {
  1059. "HeHeHe",
  1060. "Poof!",
  1061. "Got U",
  1062. "Anyone there?",
  1063. };
  1064. ZF_LOGI("mangle(ANSI_CLS)");
  1065. // sprintf( display, "^P2...");
  1066. // This string actually screws up ANSI detection (takes too long)
  1067. // strcpy(display, "^P2^S501234567890^P1abcdef^P2g^P3h^P4i^S0^P2");
  1068. // strcpy(display, "^P2^S301234^P15^S0^P2");
  1069. r = random() % 10;
  1070. if (r == 8) {
  1071. // Add in random text, plus color!
  1072. r = random() % ((sizeof(phrasing) / sizeof(char *)) - 1);
  1073. color = random() % 16;
  1074. if (color == 0) {
  1075. color++;
  1076. }
  1077. sprintf(display, "^P1^S3^C%02d%s^S0^P1", color, phrasing[r]);
  1078. ZF_LOGI("mangle(ANSI_CLS): Inserted (%02d) %s", color, phrasing[r]);
  1079. } else {
  1080. ZF_LOGI("mangle(ANSI_CLS): Inserted Nothing");
  1081. }
  1082. // Move the buffer so there's room for the display string.
  1083. memmove(cp + strlen(display), cp, strlen(cp) + 1);
  1084. strncpy(cp, display, strlen(display));
  1085. ZF_LOGI("mangle(ANSI_CLS): [%s]", repr(buffer));
  1086. need_render = 1;
  1087. /*
  1088. Copy the new buffer over, but hide our "render" code
  1089. from the remaining mangler steps.
  1090. */
  1091. strcpy(work, buffer);
  1092. i = cp - buffer;
  1093. // find offset into "buffer"
  1094. // apply to work.
  1095. memset(work + i, ' ', strlen(display));
  1096. };
  1097. }
  1098. /* work -- clear out ANSI so we don't mangle ANSI codes. */
  1099. x = rx_match(&ANSI, work);
  1100. char replace_with = ' ';
  1101. if (x > 0) {
  1102. ZF_LOGD("found %d ANSI", x);
  1103. for (i = 0; i < x; i++) {
  1104. memset(work + rxmatch[i].rm_so, replace_with,
  1105. rxmatch[i].rm_eo - rxmatch[i].rm_so);
  1106. };
  1107. ZF_LOGD("Work Now: [%s]", repr(work));
  1108. }
  1109. // ZF_LOGI("mangle: %s", repr(work));
  1110. /*
  1111. (random) Locate words (in work), and possibly flip them around.
  1112. Transpose words. Transpose case. Transpose letters.
  1113. */
  1114. x = rx_match(&WORDS, work);
  1115. ZF_LOGD("found %d word groups", x);
  1116. if (x > 0) {
  1117. for (i = 0; i < x; i++) {
  1118. // Yes! Be random!
  1119. if (random_activate(8)) {
  1120. int c = word_mangler(buffer + rxmatch[i].rm_so,
  1121. rxmatch[i].rm_eo - rxmatch[i].rm_so);
  1122. if (c) {
  1123. mangled++;
  1124. mangled_chars += c;
  1125. }
  1126. }
  1127. // Do things here.
  1128. /*
  1129. // NO! This is predictable
  1130. if (i % 3 == 0) {
  1131. mangled++;
  1132. for (int p = rxmatch[i].rm_so; p < rxmatch[i].rm_eo; p++) {
  1133. buffer[p] = tolower(buffer[p]);
  1134. mangled_chars++;
  1135. }
  1136. } else {
  1137. if (i % 3 == 1) {
  1138. mangled++;
  1139. for (int p = rxmatch[i].rm_so; p < rxmatch[i].rm_eo; p++) {
  1140. buffer[p] = toupper(buffer[p]);
  1141. mangled_chars++;
  1142. }
  1143. }
  1144. }
  1145. */
  1146. }
  1147. }
  1148. /*
  1149. (random) Locate single words, and transpose words.
  1150. Transpose letters.
  1151. */
  1152. /*
  1153. (random) Display up to certain point. Delay.
  1154. Print some characters slowly. Delay.
  1155. */
  1156. if (need_render) {
  1157. ZF_LOGD("HH %d : (%d) %s", need_render, (int)strlen(buffer), repr(buffer));
  1158. } else {
  1159. if (mangled) {
  1160. ZF_LOGD("Mangled %d words, %d chars : %s", mangled, mangled_chars,
  1161. repr(buffer));
  1162. }
  1163. }
  1164. if (need_render) {
  1165. render(fd, buffer);
  1166. } else {
  1167. write(fd, buffer, strlen(buffer));
  1168. };
  1169. return need_render && mangled;
  1170. }
  1171. int harry_happens(time_t *last_event, int wakeup) {
  1172. time_t now = time(NULL);
  1173. int elapsed = now - *last_event;
  1174. if (elapsed > wakeup) {
  1175. // Ok! It's been too long since we've done something.
  1176. *last_event = now;
  1177. return 1;
  1178. }
  1179. return 0;
  1180. }
  1181. int main(int argc, char *argv[]) {
  1182. int master;
  1183. pid_t pid;
  1184. int node = -1;
  1185. file_output_open("horrible_harry.log");
  1186. init_harry();
  1187. srandom(time(NULL));
  1188. // ./mystic -TID7 -IP192.168.0.1 -HOSTUnknown -ML1 -SL0 -ST2 -CUnknown -Ubugz
  1189. // -PUWISHPASSWORD
  1190. // ./mystic -TID7 -IP192.168.0.1 -HOSTUnknown -ML0 -SL0 -ST0 -CUnknown
  1191. // ./mystic -TID7 -IP192.168.0.1 -HOSTUnknown -ML1 -SL0 -ST2 -CUnknown -Ubugz
  1192. // -PUP2LAT3
  1193. // ./mystic -TID7 -IP192.168.0.1 -HOSTUnknown -ML0 -SL0 -ST0 -CUnknown
  1194. // ./mystic -TID7 -IP192.168.0.1 -HOSTUnknown -ML0 -SL0 -ST0 -CUnknown
  1195. // ./mystic -TID9 -IP192.168.0.1 -HOSTUnknown -ML0 -SL1 -ST0 -CUnknown
  1196. // ./mystic -TID7 -IP192.168.0.1 -HOSTUnknown -ML1 -SL0 -ST2 -CUnknown -Ubugz
  1197. // -PUP2LAT3
  1198. // ./mystic -TID9 -IP192.168.0.1 -HOSTUnknown -ML1 -SL1 -ST2 -CUnknown -Ubugz
  1199. // -PUP2LAT3
  1200. // SSH: -ML1 -ST2
  1201. // Telnet: -ML0 -ST0
  1202. // Locate username (if given) in the command line
  1203. // -U<username>
  1204. for (int x = 0; x < argc; x++) {
  1205. if (strncmp("-U", argv[x], 2) == 0) {
  1206. username = argv[x] + 2;
  1207. ZF_LOGI("Username: [%s]", username);
  1208. };
  1209. if (strncmp("-SL", argv[x], 3) == 0) {
  1210. node = argv[x][3] - '0' + 1;
  1211. ZF_LOGI("Node: %d", node);
  1212. }
  1213. }
  1214. if (username != NULL) {
  1215. locate_user(username);
  1216. ZF_LOGD("Username: [%s] A.K.A. [%s]", username, fullname);
  1217. }
  1218. if (!init_regex())
  1219. return 2;
  1220. // With IGNBRK I don't think I need this anymore. (Nope!)
  1221. // signal(SIGINT, SIG_IGN);
  1222. pid = forkpty(&master, NULL, NULL, NULL);
  1223. // impossible to fork
  1224. if (pid < 0) {
  1225. return 1;
  1226. }
  1227. // child
  1228. else if (pid == 0) {
  1229. char *args[20]; // max 20 args
  1230. int x;
  1231. args[0] = TARGET;
  1232. for (x = 1; x < argc; x++) {
  1233. args[x] = argv[x];
  1234. };
  1235. args[x] = NULL;
  1236. // run Mystic, run!
  1237. execvp(TARGET, args);
  1238. }
  1239. // parent
  1240. else {
  1241. // remove the echo
  1242. // ICANON - raw mode. No more line buffering!
  1243. struct termios tios, orig1;
  1244. struct timeval timeout;
  1245. time_t last_event = 0; // time(NULL);
  1246. ZF_LOGD("starting");
  1247. tcgetattr(master, &tios);
  1248. tios.c_lflag &= ~(ECHO | ECHONL | ICANON);
  1249. tcsetattr(master, TCSAFLUSH, &tios);
  1250. tcgetattr(1, &orig1);
  1251. tios = orig1;
  1252. tios.c_iflag &= ~(ICRNL | IXON); // Disable software flow control
  1253. tios.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
  1254. // https://viewsourcecode.org/snaptoken/kilo/02.enteringRawMode.html
  1255. // ISIG should be Ctrl-C and Ctrl-Z IGNBRK +
  1256. tcsetattr(1, TCSAFLUSH, &tios);
  1257. for (;;) {
  1258. // define estruturas para o select, que serve para verificar qual
  1259. // se tornou "pronto pra uso"
  1260. fd_set read_fd;
  1261. fd_set write_fd;
  1262. fd_set except_fd;
  1263. // inicializa as estruturas
  1264. FD_ZERO(&read_fd);
  1265. FD_ZERO(&write_fd);
  1266. FD_ZERO(&except_fd);
  1267. // atribui o descritor master, obtido pelo forkpty, ao read_fd
  1268. FD_SET(master, &read_fd);
  1269. // atribui o stdin ao read_fd
  1270. FD_SET(STDIN_FILENO, &read_fd);
  1271. // o descritor tem que ser unico para o programa, a documentacao
  1272. // recomenda um calculo entre os descritores sendo usados + 1
  1273. /*
  1274. TODO: Figure out how this would work.
  1275. I'm thinking something like timeouts 30-50 seconds?
  1276. And as we get closer, 15-25 seconds.
  1277. */
  1278. // we're in luck! The last parameter is time interval/timeout. :D
  1279. timeout.tv_sec = 10;
  1280. timeout.tv_usec = 0;
  1281. // select(master+1, &read_fd, &write_fd, &except_fd, NULL);
  1282. if (select(master + 1, &read_fd, &write_fd, &except_fd, &timeout) == 0) {
  1283. // This means timeout!
  1284. ZF_LOGI("TIMEOUT");
  1285. harry_event(STDOUT_FILENO);
  1286. }
  1287. char input[BSIZE + 1];
  1288. static char output[(BSIZE * 4) + 1];
  1289. int total;
  1290. // read_fd esta atribuido com read_fd?
  1291. if (FD_ISSET(master, &read_fd)) {
  1292. // leia o que bc esta mandando
  1293. if ((total = read(master, &output, BSIZE)) != -1) {
  1294. // e escreva isso na saida padrao
  1295. output[total] = 0;
  1296. // if ( harry_happens( &last_event, 5)) {
  1297. if (1) {
  1298. ZF_LOGI("harry_happens");
  1299. if (mangle(STDOUT_FILENO, output) == 0) {
  1300. // failed, so. Try again.
  1301. last_event = 0;
  1302. }
  1303. } else {
  1304. write(STDOUT_FILENO, &output, total);
  1305. // This is OUTPUT from the BBS
  1306. // ZF_LOGI( ">> %s", repr(output));
  1307. ZF_LOGI(">> %d chars", (int)strlen(output));
  1308. // I think repr is flipping out here. :(
  1309. // ZF_LOGI( ">> %d", (int)strlen(repr(output)));
  1310. }
  1311. } else
  1312. break;
  1313. }
  1314. // read_fd esta atribuido com a entrada padrao?
  1315. if (FD_ISSET(STDIN_FILENO, &read_fd)) {
  1316. // leia a entrada padrao
  1317. total = read(STDIN_FILENO, &input, BSIZE);
  1318. input[total] = 0;
  1319. // e escreva no bc
  1320. ZF_LOGI("<< %s", repr(input));
  1321. write(master, &input, total);
  1322. // This is INPUT from the USER
  1323. // ZF_LOGI_MEM( input, strlen(input), "<< ");
  1324. }
  1325. }
  1326. // Restore terminal
  1327. tcsetattr(1, TCSAFLUSH, &orig1);
  1328. ZF_LOGD("exit");
  1329. }
  1330. return 0;
  1331. }