main.c 19 KB

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