main.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  1. /******************************************************************************
  2. * This file is part of TinTin++ *
  3. * *
  4. * Copyright 2004-2019 Igor van den Hoven *
  5. * *
  6. * TinTin++ is free software; you can redistribute it and/or modify *
  7. * it under the terms of the GNU General Public License as published by *
  8. * the Free Software Foundation; either version 3 of the License, or *
  9. * (at your option) any later version. *
  10. * *
  11. * This program is distributed in the hope that it will be useful, *
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  14. * GNU General Public License for more details. *
  15. * *
  16. * *
  17. * You should have received a copy of the GNU General Public License *
  18. * along with TinTin++. If not, see https://www.gnu.org/licenses. *
  19. ******************************************************************************/
  20. /******************************************************************************
  21. * (T)he K(I)cki(N) (T)ickin D(I)kumud Clie(N)t *
  22. * *
  23. * coded by Peter Unold 1992 *
  24. * recoded by Igor van den Hoven 2004 *
  25. ******************************************************************************/
  26. #include "tintin.h"
  27. #include <signal.h>
  28. /*************** globals ******************/
  29. struct session *gts;
  30. struct tintin_data *gtd;
  31. void pipe_handler(int signal)
  32. {
  33. syserr_printf(gtd->ses, "pipe_handler");
  34. }
  35. void xfsz_handler(int signal)
  36. {
  37. syserr_printf(gtd->ses, "xfsz_handler");
  38. }
  39. void hub_handler(int signal)
  40. {
  41. syserr_printf(gtd->ses, "hub_handler");
  42. }
  43. void ttin_handler(int signal)
  44. {
  45. syserr_printf(gtd->ses, "ttin_handler");
  46. }
  47. void ttou_handler(int signal)
  48. {
  49. syserr_printf(gtd->ses, "ttou_handler");
  50. }
  51. /*
  52. when the screen size changes, take note of it
  53. */
  54. void winch_handler(int signal)
  55. {
  56. struct session *ses;
  57. init_terminal_size(gts);
  58. for (ses = gts->next ; ses ; ses = ses->next)
  59. {
  60. init_terminal_size(ses);
  61. if (HAS_BIT(ses->telopts, TELOPT_FLAG_NAWS))
  62. {
  63. client_send_sb_naws(ses, 0, NULL);
  64. }
  65. }
  66. winch_daemon();
  67. }
  68. void abort_handler(int signal)
  69. {
  70. syserr_fatal(signal, "abort_handler");
  71. }
  72. void child_handler(int signal)
  73. {
  74. return;
  75. syserr_printf(gtd->ses, "child_handler");
  76. // syserr_fatal(signal, "child_handler");
  77. }
  78. void interrupt_handler(int signal)
  79. {
  80. if (gtd->ses->connect_retry > utime())
  81. {
  82. gtd->ses->connect_retry = 0;
  83. }
  84. else if (HAS_BIT(gtd->ses->telopts, TELOPT_FLAG_SGA) && !HAS_BIT(gtd->ses->telopts, TELOPT_FLAG_ECHO))
  85. {
  86. socket_printf(gtd->ses, 1, "%c", 4);
  87. }
  88. else if (gtd->attach_sock)
  89. {
  90. gtd->attach_sock = close(gtd->attach_sock);
  91. show_message(gtd->ses, LIST_COMMAND, "#REDETACHING PROCESS TO {%s}", gtd->attach_file);
  92. }
  93. else
  94. {
  95. cursor_delete_or_exit(gtd->ses, "");
  96. }
  97. }
  98. void suspend_handler(int signal)
  99. {
  100. show_message(gtd->ses, LIST_COMMAND, "#SIGNAL: SIGTSTP");
  101. if (gtd->attach_sock)
  102. {
  103. show_message(gtd->ses, LIST_COMMAND, "#DAEMON {%s} WANTS TO DETACH.", gtd->attach_file);
  104. gtd->attach_sock = close(gtd->attach_sock);
  105. return;
  106. }
  107. }
  108. void trap_handler(int signal)
  109. {
  110. syserr_fatal(signal, "trap_handler");
  111. }
  112. /****************************************************************************/
  113. /* main() - show title - setup signals - init lists - readcoms - mainloop() */
  114. /****************************************************************************/
  115. int main(int argc, char **argv)
  116. {
  117. int c, i = 0, greeting = 0;
  118. char filename[256];
  119. char arg[BUFFER_SIZE];
  120. #ifdef SOCKS
  121. SOCKSinit(argv[0]);
  122. #endif
  123. if (signal(SIGTERM, trap_handler) == BADSIG)
  124. {
  125. syserr_fatal(-1, "signal SIGTERM");
  126. }
  127. if (signal(SIGSEGV, trap_handler) == BADSIG)
  128. {
  129. syserr_fatal(-1, "signal SIGSEGV");
  130. }
  131. if (signal(SIGHUP, trap_handler) == BADSIG)
  132. {
  133. syserr_fatal(-1, "signal SIGHUP");
  134. }
  135. if (signal(SIGABRT, abort_handler) == BADSIG)
  136. {
  137. syserr_fatal(-1, "signal SIGTERM");
  138. }
  139. /* if (signal(SIGCHLD, child_handler) == BADSIG)
  140. {
  141. syserr_fatal(-1, "signal SIGCHLD");
  142. }
  143. */
  144. /*
  145. if (signal(SIGINT, interrupt_handler) == BADSIG)
  146. {
  147. syserr_fatal(-1, "signal SIGINT");
  148. }
  149. */
  150. if (signal(SIGTSTP, suspend_handler) == BADSIG)
  151. {
  152. syserr_fatal(-1, "signal SIGTSTP");
  153. }
  154. /*
  155. if (signal(SIGPIPE, pipe_handler) == BADSIG)
  156. {
  157. syserr_fatal(-1, "signal SIGPIPE");
  158. }
  159. if (signal(SIGXFSZ, xfsz_handler) == BADSIG)
  160. {
  161. syserr_fatal(-1, "signal SIGXFSZ");
  162. }
  163. if (signal(SIGHUP, hub_handler) == BADSIG)
  164. {
  165. syserr_fatal(-1, "signal SIGHUP");
  166. }
  167. if (signal(SIGTTIN, hub_handler) == BADSIG)
  168. {
  169. syserr_fatal(-1, "signal SIGTTIN");
  170. }
  171. if (signal(SIGTTOU, hub_handler) == BADSIG)
  172. {
  173. syserr_fatal(-1, "signal SIGTTOU");
  174. }
  175. */
  176. if (signal(SIGWINCH, winch_handler) == BADSIG)
  177. {
  178. syserr_fatal(-1, "signal SIGWINCH");
  179. }
  180. signal(SIGPIPE, SIG_IGN);
  181. for (c = 0 ; c < argc ; c++)
  182. {
  183. if (c)
  184. {
  185. cat_sprintf(arg, " %s", argv[c]);
  186. }
  187. {
  188. strcpy(arg, argv[0]);
  189. }
  190. }
  191. if (argc > 1)
  192. {
  193. while ((c = getopt(argc, argv, "a: e: G h M:: r: R:: s t: T v V")) != EOF)
  194. {
  195. switch (c)
  196. {
  197. case 'h':
  198. printf("Usage: %s [OPTION]... [FILE]...\n", argv[0]);
  199. printf("\n");
  200. printf(" -a Set argument for PROGRAM START event.\n");
  201. printf(" -e Execute given command.\n");
  202. printf(" -G Don't show the greeting screen.\n");
  203. printf(" -h This help section.\n");
  204. printf(" -M Matrix Digital Rain.\n");
  205. printf(" -r Read given file.\n");
  206. printf(" -s Enable screen reader mode.\n");
  207. printf(" -t Set given title.\n");
  208. printf(" -T Don't set the default title.\n");
  209. printf(" -v Enable verbose mode.\n");
  210. printf(" -V Show version information.\n");
  211. exit(1);
  212. case 'M':
  213. case 'G':
  214. SET_BIT(greeting, STARTUP_FLAG_NOGREETING);
  215. break;
  216. case 's':
  217. SET_BIT(greeting, STARTUP_FLAG_SCREENREADER);
  218. break;
  219. case 'V':
  220. printf("\nTinTin++ " CLIENT_VERSION "\n");
  221. printf("\n(C) 2004-2019 Igor van den Hoven\n");
  222. printf("\nLicense GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n\n");
  223. exit(1);
  224. }
  225. }
  226. }
  227. init_tintin(greeting);
  228. sprintf(filename, "%s/%s", gtd->home, TINTIN_DIR);
  229. if (mkdir(filename, 0777) || errno == EEXIST)
  230. {
  231. sprintf(filename, "%s/%s/%s", gtd->home, TINTIN_DIR, HISTORY_FILE);
  232. if (access(filename, F_OK ) != -1)
  233. {
  234. history_read(gts, filename);
  235. }
  236. }
  237. RESTRING(gtd->vars[1], argv[0]);
  238. if (argc > 1)
  239. {
  240. optind = 1;
  241. RESTRING(gtd->vars[2], argv[1]);
  242. while ((c = getopt(argc, argv, "a: e: G h M:: r: R:: s t: T v")) != EOF)
  243. {
  244. switch (c)
  245. {
  246. case 'a':
  247. RESTRING(gtd->vars[0], argv[2]);
  248. SET_BIT(greeting, STARTUP_FLAG_ARGUMENT);
  249. break;
  250. case 'e':
  251. gtd->level->input++;
  252. gtd->ses = script_driver(gtd->ses, LIST_COMMAND, optarg);
  253. gtd->level->input--;
  254. break;
  255. case 'G':
  256. break;
  257. case 'M':
  258. do_test(gts, optarg ? optarg : "");
  259. break;
  260. case 'r':
  261. gtd->level->input++;
  262. gtd->ses = do_read(gtd->ses, optarg);
  263. gtd->level->input--;
  264. break;
  265. case 'R':
  266. SET_BIT(gtd->flags, TINTIN_FLAG_DAEMONIZE);
  267. daemon_attach(gtd->ses, optarg ? optarg : "");
  268. break;
  269. case 's':
  270. break;
  271. case 't':
  272. SET_BIT(greeting, STARTUP_FLAG_NOTITLE);
  273. print_stdout("\e]0;%s\007", optarg);
  274. break;
  275. case 'T':
  276. SET_BIT(greeting, STARTUP_FLAG_NOTITLE);
  277. break;
  278. case 'v':
  279. do_configure(gtd->ses, "{VERBOSE} {ON}");
  280. break;
  281. default:
  282. // tintin_printf2(NULL, "Unknown option '%c'.", c);
  283. break;;
  284. }
  285. }
  286. }
  287. if (!HAS_BIT(greeting, STARTUP_FLAG_NOTITLE))
  288. {
  289. do_screen(gts, "LOAD BOTH");
  290. do_screen(gts, "SAVE BOTH");
  291. do_screen(gts, "SET BOTH TinTin++");
  292. }
  293. gtd->exec = strdup(argv[0]);
  294. if (argc > 2)
  295. {
  296. RESTRING(gtd->vars[3], argv[2]);
  297. for (i = 3 ; i <= optind ; i++)
  298. {
  299. RESTRING(gtd->vars[i+1], argv[i] ? argv[i] : "");
  300. }
  301. arg[0] = 0;
  302. for (i = optind + 1 ; i < argc ; i++)
  303. {
  304. if (*arg)
  305. {
  306. strcat(arg, " ");
  307. }
  308. strcat(arg, argv[i]);
  309. if (i < 100)
  310. {
  311. RESTRING(gtd->vars[i+1], argv[i]);
  312. }
  313. }
  314. if (!HAS_BIT(greeting, STARTUP_FLAG_ARGUMENT))
  315. {
  316. RESTRING(gtd->vars[0], arg);
  317. }
  318. }
  319. if (argv[optind] != NULL)
  320. {
  321. if (!strncasecmp(argv[optind], "telnet://", 9))
  322. {
  323. do_session(gts, argv[optind]);
  324. }
  325. else
  326. {
  327. gtd->level->input++;
  328. gtd->ses = do_read(gtd->ses, argv[optind]);
  329. gtd->level->input--;
  330. }
  331. }
  332. check_all_events(gts, SUB_ARG, 0, 0, "PROGRAM START");
  333. mainloop();
  334. return 0;
  335. }
  336. void init_tintin(int greeting)
  337. {
  338. int ref, index;
  339. push_call("init_tintin(%d)",greeting);
  340. gtd = (struct tintin_data *) calloc(1, sizeof(struct tintin_data));
  341. gtd->level = (struct level_data *) calloc(1, sizeof(struct level_data));
  342. gtd->memory = calloc(1, sizeof(struct str_data));
  343. gtd->buf = str_alloc(STRING_SIZE);
  344. gtd->out = str_alloc(STRING_SIZE);
  345. gtd->flags = TINTIN_FLAG_INHERITANCE;
  346. gtd->mccp_len = 10000;
  347. gtd->mccp_buf = (unsigned char *) calloc(1, gtd->mccp_len);
  348. gtd->mud_output_max = 16384;
  349. gtd->mud_output_buf = (char *) calloc(1, gtd->mud_output_max);
  350. gtd->input_off = 1;
  351. gtd->os = strdup(getenv("OS") ? getenv("OS") : "UNKNOWN");
  352. gtd->home = strdup(getenv("HOME") ? getenv("HOME") : "~/");
  353. gtd->lang = strdup(getenv("LANG") ? getenv("LANG") : "UNKNOWN");
  354. gtd->term = strdup(getenv("TERM") ? getenv("TERM") : "UNKNOWN");
  355. gtd->detach_file = strdup("");
  356. gtd->attach_file = strdup("");
  357. gtd->time = time(NULL);
  358. gtd->calendar = *localtime(&gtd->time);
  359. for (index = 0 ; index < 100 ; index++)
  360. {
  361. gtd->vars[index] = strdup("");
  362. gtd->cmds[index] = strdup("");
  363. }
  364. for (ref = 0 ; ref < 26 ; ref++)
  365. {
  366. for (index = 0 ; *command_table[index].name != 0 ; index++)
  367. {
  368. if (index && strcmp(command_table[index - 1].name, command_table[index].name) > 0)
  369. {
  370. print_stdout("\e[1;31minit_tintin() unsorted command table %s vs %s.", command_table[index - 1].name, command_table[index].name);
  371. }
  372. if (*command_table[index].name == 'a' + ref)
  373. {
  374. gtd->command_ref[ref] = index;
  375. break;
  376. }
  377. }
  378. }
  379. for (index = 1 ; index ; index++)
  380. {
  381. if (*event_table[index].name == 0)
  382. {
  383. break;
  384. }
  385. if (strcmp(event_table[index - 1].name, event_table[index].name) > 0)
  386. {
  387. print_stdout("\e[1;31minit_tintin() unsorted event table %s vs %s.", event_table[index - 1].name, event_table[index].name);
  388. break;
  389. }
  390. }
  391. gtd->screen = calloc(1, sizeof(struct screen_data));
  392. gtd->screen->rows = SCREEN_HEIGHT;
  393. gtd->screen->cols = SCREEN_WIDTH;
  394. gtd->screen->height = SCREEN_HEIGHT * 16;
  395. gtd->screen->width = SCREEN_WIDTH * 10;
  396. gtd->screen->focus = 1;
  397. init_msdp_table();
  398. // global tintin session
  399. gts = (struct session *) calloc(1, sizeof(struct session));
  400. gts->name = strdup("gts");
  401. gts->group = strdup("");
  402. gts->session_host = strdup("");
  403. gts->session_ip = strdup("");
  404. gts->session_port = strdup("");
  405. gts->cmd_color = strdup("");
  406. gts->telopts = TELOPT_FLAG_ECHO;
  407. gts->flags = SES_FLAG_MCCP;
  408. gts->socket = 1;
  409. gts->read_max = 16384;
  410. gts->lognext_name = strdup("");
  411. gts->logline_name = strdup("");
  412. gtd->ses = gts;
  413. for (index = 0 ; index < LIST_MAX ; index++)
  414. {
  415. gts->list[index] = init_list(gts, index, 32);
  416. }
  417. gts->split = calloc(1, sizeof(struct split_data));
  418. gts->scroll = calloc(1, sizeof(struct scroll_data));
  419. init_local(gts);
  420. init_terminal_size(gts);
  421. gtd->level->input++;
  422. do_class(gts, "{CONFIG} {OPEN}");
  423. do_configure(gts, "{AUTO TAB} {5000}");
  424. do_configure(gts, "{BUFFER SIZE} {10000}");
  425. do_configure(gts, "{COLOR MODE} {ON}");
  426. do_configure(gts, "{COLOR PATCH} {OFF}");
  427. do_configure(gts, "{COMMAND COLOR} {<078>}");
  428. do_configure(gts, "{COMMAND ECHO} {ON}");
  429. do_configure(gts, "{CONNECT RETRY} {0}");
  430. do_configure(gts, "{CHARSET} {AUTO}");
  431. do_configure(gts, "{HISTORY SIZE} {1000}");
  432. do_configure(gts, "{LOG MODE} {RAW}");
  433. do_configure(gts, "{MOUSE TRACKING} {OFF}");
  434. do_configure(gts, "{PACKET PATCH} {AUTO}");
  435. do_configure(gts, "{RANDOM SEED} {AUTO}");
  436. do_configure(gts, "{REPEAT CHAR} {!}");
  437. do_configure(gts, "{REPEAT ENTER} {OFF}");
  438. do_configure(gts, HAS_BIT(greeting, STARTUP_FLAG_SCREENREADER) ? "{SCREEN READER} {ON}" : "{SCREEN READER} {OFF}");
  439. do_configure(gts, "{SCROLL LOCK} {ON}");
  440. do_configure(gts, "{SPEEDWALK} {OFF}");
  441. do_configure(gts, "{TAB WIDTH} {AUTO}");
  442. do_configure(gts, "{TELNET} {ON}");
  443. do_configure(gts, "{TINTIN CHAR} {#}");
  444. do_configure(gts, "{VERBATIM} {OFF}");
  445. do_configure(gts, "{VERBATIM CHAR} {\\}");
  446. do_configure(gts, "{VERBOSE} {OFF}");
  447. do_configure(gts, "{WORDWRAP} {ON}");
  448. do_class(gts, "{CONFIG} {CLOSE}");
  449. do_class(gts, "{PATHDIR} {OPEN}");
  450. insert_node_list(gts->list[LIST_PATHDIR], "n", "s", "1", "");
  451. insert_node_list(gts->list[LIST_PATHDIR], "e", "w", "2", "");
  452. insert_node_list(gts->list[LIST_PATHDIR], "s", "n", "4", "");
  453. insert_node_list(gts->list[LIST_PATHDIR], "w", "e", "8", "");
  454. insert_node_list(gts->list[LIST_PATHDIR], "u", "d", "16", "");
  455. insert_node_list(gts->list[LIST_PATHDIR], "d", "u", "32", "");
  456. insert_node_list(gts->list[LIST_PATHDIR], "ne", "sw", "3", "");
  457. insert_node_list(gts->list[LIST_PATHDIR], "nw", "se", "9", "");
  458. insert_node_list(gts->list[LIST_PATHDIR], "se", "nw", "6", "");
  459. insert_node_list(gts->list[LIST_PATHDIR], "sw", "ne", "12", "");
  460. do_class(gts, "{PATHDIR} {CLOSE}");
  461. gtd->level->input--;
  462. init_terminal(gts);
  463. reset_screen(gts);
  464. if (!HAS_BIT(greeting, STARTUP_FLAG_NOGREETING))
  465. {
  466. if (HAS_BIT(greeting, STARTUP_FLAG_SCREENREADER))
  467. {
  468. tintin_printf2(gts, "Welcome to TinTin Plus Plus. Don't know which MUD to play? How about the following MUD.");
  469. do_advertise(gts, "");
  470. tintin_printf2(gts, "You're using TinTin Plus Plus written by Peter Unold, Bill Reis, and Igor van den Hoven.", CLIENT_VERSION);
  471. tintin_printf2(gts, "For help and requests visit tintin.sourceforge.io/forum the captcha answer is 3671.");
  472. }
  473. else
  474. {
  475. do_advertise(gts, "");
  476. if (gtd->screen->cols >= 80)
  477. {
  478. do_help(gts, "GREETING");
  479. }
  480. else
  481. {
  482. tintin_printf2(gts,
  483. "\e[0;37mT I N T I N + + %s"
  484. "\n\n\e[0;36mT\e[0;37mhe K\e[0;36mi\e[0;37mcki\e[0;36mn\e[0;37m \e[0;36mT\e[0;37mickin D\e[0;36mi\e[0;37mkuMUD Clie\e[0;36mn\e[0;37mt\n\n"
  485. "Code by Peter Unold, Bill Reis, and Igor van den Hoven\n",
  486. CLIENT_VERSION);
  487. }
  488. }
  489. }
  490. pop_call();
  491. return;
  492. }
  493. void quitmsg(char *message)
  494. {
  495. struct session *ses;
  496. static char crashed = FALSE;
  497. if (crashed++)
  498. {
  499. print_stdout("quitmsg(crashed)\n");
  500. fflush(NULL);
  501. exit(-1);
  502. }
  503. SET_BIT(gtd->flags, TINTIN_FLAG_TERMINATE);
  504. while ((ses = gts->next) != NULL)
  505. {
  506. cleanup_session(ses);
  507. }
  508. if (gtd->chat)
  509. {
  510. chat_uninitialize("", "");
  511. }
  512. check_all_events(gts, SUB_ARG, 0, 1, "PROGRAM TERMINATION", message ? message : "");
  513. if (gtd->history_size)
  514. {
  515. char filename[BUFFER_SIZE];
  516. sprintf(filename, "%s/%s/%s", gtd->home, TINTIN_DIR, HISTORY_FILE);
  517. history_write(gts, filename);
  518. }
  519. reset_daemon();
  520. reset_terminal(gts);
  521. reset_screen(gts);
  522. if (message == NULL || *message)
  523. {
  524. if (message)
  525. {
  526. print_stdout("\n\e[0m%s", message);
  527. }
  528. print_stdout("\nGoodbye from TinTin++\n\n");
  529. }
  530. fflush(NULL);
  531. exit(0);
  532. }
  533. void syserr_printf(struct session *ses, char *fmt, ...)
  534. {
  535. char buf[BUFFER_SIZE], name[BUFFER_SIZE], *errstr;
  536. errstr = strerror(errno);
  537. va_list args;
  538. va_start(args, fmt);
  539. vsprintf(buf, fmt, args);
  540. va_end(args);
  541. if (ses)
  542. {
  543. sprintf(name, "(%s)", ses->name);
  544. }
  545. else
  546. {
  547. sprintf(name, "(null)");
  548. }
  549. check_all_events(ses, SUB_ARG|SUB_SEC, 0, 0, "SYSTEM ERROR", name, buf, ntos(errno), errstr);
  550. if (!check_all_events(ses, SUB_ARG|SUB_SEC, 0, 0, "CATCH SYSTEM ERROR", name, buf, ntos(errno), errstr))
  551. {
  552. if (gts)
  553. {
  554. tintin_printf2(gts, "#SYSTEM ERROR %s %s (%d: %s)\e[0m", name, buf, errno, errstr);
  555. }
  556. if (ses && ses != gts)
  557. {
  558. tintin_printf2(ses, "#SYSTEM ERROR: %s %s (%d: %s)\e[0m", name, buf, errno, errstr);
  559. }
  560. if (ses && gtd->ses != ses && gtd->ses != gts)
  561. {
  562. tintin_printf2(gtd->ses, "#SYSTEM ERROR: %s %s (%d: %s)\e[0m", name, buf, errno, errstr);
  563. }
  564. }
  565. }
  566. void syserr_signal(int signal, char *msg)
  567. {
  568. char buf[256];
  569. static char crashed = FALSE;
  570. if (crashed++)
  571. {
  572. print_stdout("\ecsyserr_signal(crashed)\n");
  573. fflush(NULL);
  574. exit(-1);
  575. }
  576. reset_terminal(gts);
  577. reset_screen(gts);
  578. fflush(NULL);
  579. dump_stack_fatal();
  580. sprintf(buf, "\e[1;31mFATAL SIGNAL FROM (%s): %s\e[0m\n", msg, strsignal(signal));
  581. print_stdout("%s", buf);
  582. fflush(NULL);
  583. exit(0);
  584. }
  585. void syserr_fatal(int signal, char *msg)
  586. {
  587. char buf[256], errstr[128];
  588. static char crashed = FALSE;
  589. if (crashed++)
  590. {
  591. print_stdout("\ecsyserr_fatal(crashed)");
  592. fflush(NULL);
  593. exit(-1);
  594. }
  595. if (signal <= 0)
  596. {
  597. sprintf(errstr, "(error %d: %s", errno, strerror(errno));
  598. }
  599. else
  600. {
  601. sprintf(errstr, "(signal %d: %s)", signal, strsignal(signal));
  602. }
  603. if (gtd->level->quiet)
  604. {
  605. gtd->level->quiet = 0;
  606. }
  607. reset_terminal(gts);
  608. reset_screen(gts);
  609. print_stdout("\e[r");
  610. dump_stack_fatal();
  611. sprintf(buf, "\n\e[1;31mFATAL ERROR \e[1;32m%s %s\e[0m\n", msg, errstr);
  612. print_stdout("%s", buf);
  613. reset_daemon();
  614. fflush(NULL);
  615. exit(0);
  616. }