main.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  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. command(gts, do_screen, "LOAD BOTH");
  291. command(gts, do_screen, "SAVE BOTH");
  292. command(gts, do_screen, "SET BOTH %s", optarg);
  293. // print_stdout(0, 0, "\e]0;%s\007", optarg);
  294. break;
  295. case 'T':
  296. SET_BIT(greeting, STARTUP_FLAG_NOTITLE);
  297. break;
  298. case 'v':
  299. // do_configure(gtd->ses, "{VERBOSE} {ON}");
  300. break;
  301. default:
  302. // tintin_printf2(NULL, "Unknown option '%c'.", c);
  303. break;;
  304. }
  305. }
  306. }
  307. if (!HAS_BIT(greeting, STARTUP_FLAG_NOTITLE))
  308. {
  309. command(gts, do_screen, "LOAD BOTH");
  310. command(gts, do_screen, "SAVE BOTH");
  311. command(gts, do_screen, "SET BOTH TinTin++");
  312. }
  313. gtd->system->exec = strdup(argv[0]);
  314. if (argc > 2)
  315. {
  316. RESTRING(gtd->vars[3], argv[2]);
  317. for (i = 3 ; i <= optind ; i++)
  318. {
  319. RESTRING(gtd->vars[i+1], argv[i] ? argv[i] : "");
  320. }
  321. arg[0] = 0;
  322. for (i = optind + 1 ; i < argc ; i++)
  323. {
  324. if (*arg)
  325. {
  326. strcat(arg, " ");
  327. }
  328. strcat(arg, argv[i]);
  329. if (i < 100)
  330. {
  331. RESTRING(gtd->vars[i+1], argv[i]);
  332. }
  333. }
  334. if (!HAS_BIT(greeting, STARTUP_FLAG_ARGUMENT))
  335. {
  336. RESTRING(gtd->vars[0], arg);
  337. }
  338. }
  339. gtd->varc = UMIN(argc + 1, 100);
  340. if (argv[optind] != NULL)
  341. {
  342. if (!strncasecmp(argv[optind], "telnet://", 9))
  343. {
  344. gtd->ses = command(gtd->ses, do_session, "%s", argv[optind]);
  345. }
  346. else
  347. {
  348. gtd->level->input++;
  349. gtd->ses = command(gtd->ses, do_read, "%s", argv[optind]);
  350. gtd->level->input--;
  351. }
  352. }
  353. check_all_events(gts, EVENT_FLAG_SYSTEM, 0, 0, "PROGRAM START");
  354. mainloop();
  355. return 0;
  356. }
  357. void init_tintin(int greeting)
  358. {
  359. char filename[PATH_SIZE];
  360. int index;
  361. gtd = (struct tintin_data *) calloc(1, sizeof(struct tintin_data));
  362. init_memory();
  363. push_call("init_tintin(%d)",greeting);
  364. gtd->level = (struct level_data *) calloc(1, sizeof(struct level_data));
  365. gtd->buf = str_alloc(BUFFER_SIZE);
  366. gtd->out = str_alloc(BUFFER_SIZE);
  367. gtd->flags = TINTIN_FLAG_INHERITANCE;
  368. gtd->mccp_len = 16384;
  369. gtd->mccp_buf = (unsigned char *) calloc(1, gtd->mccp_len);
  370. gtd->mud_output_max = 16384;
  371. gtd->mud_output_buf = (char *) calloc(1, gtd->mud_output_max);
  372. gtd->mud_output_strip_buf = (char *) calloc(1, gtd->mud_output_max);
  373. gtd->mud_output_line = gtd->mud_output_buf;
  374. // WSL: /proc/sys/kernel/osrelease
  375. gtd->system = (struct system_data *) calloc(1, sizeof(struct system_data));
  376. gtd->system->os = strdup(getenv("OS") ? getenv("OS") : "UNKNOWN");
  377. gtd->system->home = strdup(getenv("HOME") ? getenv("HOME") : "~/");
  378. gtd->system->lang = strdup(getenv("LANG") ? getenv("LANG") : "UNKNOWN");
  379. gtd->system->term = strdup(getenv("TERM") ? getenv("TERM") : "UNKNOWN");
  380. gtd->system->tt_dir = restringf(gtd->system->tt_dir, "%s/%s", gtd->system->home, TINTIN_DIR);
  381. if (mkdir(gtd->system->tt_dir, 0755) && errno != EEXIST)
  382. {
  383. gtd->system->tt_dir = restringf(gtd->system->tt_dir, "%s", TINTIN_DIR);
  384. mkdir(gtd->system->tt_dir, 0755);
  385. }
  386. gtd->detach_file = strdup("");
  387. gtd->attach_file = strdup("");
  388. gtd->tintin_char = '#';
  389. gtd->time = time(NULL);
  390. gtd->utime = utime();
  391. for (index = 0 ; index < 100 ; index++)
  392. {
  393. gtd->vars[index] = strdup("");
  394. gtd->cmds[index] = strdup("");
  395. }
  396. for (index = 1 ; index ; index++)
  397. {
  398. if (*event_table[index].name == 0)
  399. {
  400. break;
  401. }
  402. if (strcmp(event_table[index - 1].name, event_table[index].name) > 0)
  403. {
  404. print_stdout(0, 0, "\e[1;31minit_tintin() unsorted event table %s vs %s.", event_table[index - 1].name, event_table[index].name);
  405. break;
  406. }
  407. }
  408. init_commands();
  409. gtd->screen = calloc(1, sizeof(struct screen_data));
  410. gtd->screen->rows = SCREEN_HEIGHT;
  411. gtd->screen->cols = SCREEN_WIDTH;
  412. gtd->screen->height = SCREEN_HEIGHT * 16;
  413. gtd->screen->width = SCREEN_WIDTH * 10;
  414. gtd->screen->focus = 1;
  415. gtd->dispose_list = init_list(NULL, 0, 32);
  416. init_msdp_table();
  417. // global tintin session
  418. gts = (struct session *) calloc(1, sizeof(struct session));
  419. gts->created = gtd->time;
  420. gts->name = strdup("gts");
  421. gts->group = strdup("");
  422. gts->session_host = strdup("");
  423. gts->session_ip = strdup("");
  424. gts->session_port = strdup("");
  425. gts->cmd_color = strdup("");
  426. gts->telopts = TELOPT_FLAG_ECHO;
  427. gts->config_flags = CONFIG_FLAG_MCCP;
  428. gts->socket = 1;
  429. gts->read_max = 16384;
  430. gts->more_output = str_dup("");
  431. gtd->ses = gts;
  432. for (index = 0 ; index < LIST_MAX ; index++)
  433. {
  434. gts->list[index] = init_list(gts, index, 32);
  435. }
  436. gtd->banner_list = init_list(gts, LIST_CONFIG, 32);
  437. gts->log = calloc(1, sizeof(struct log_data));
  438. gts->input = calloc(1, sizeof(struct input_data));
  439. gts->scroll = calloc(1, sizeof(struct scroll_data));
  440. gts->split = calloc(1, sizeof(struct split_data));
  441. init_local(gts);
  442. init_terminal_size(gts);
  443. init_log(gts);
  444. init_input(gts, 0, 0, 0, 0);
  445. init_buffer(gts, 10000);
  446. if (HAS_BIT(greeting, STARTUP_FLAG_VERBOSE))
  447. {
  448. gtd->level->verbose++;
  449. }
  450. gtd->level->input++;
  451. /* command(gts, do_configure, "{AUTO TAB} {5000}"); */ gts->scrollback_tab = 5000;
  452. command(gts, do_configure, "{BUFFER SIZE} {10000}");
  453. command(gts, do_configure, "{COLOR MODE} {ON}");
  454. command(gts, do_configure, "{COLOR PATCH} {OFF}");
  455. command(gts, do_configure, "{COMMAND COLOR} {<078>}");
  456. command(gts, do_configure, "{COMMAND ECHO} {ON}");
  457. command(gts, do_configure, "{COMPACT} {OFF}");
  458. /* command(gts, do_configure, "{CONNECT RETRY} {0}"); */
  459. command(gts, do_configure, "{CHARSET} {AUTO}");
  460. command(gts, do_configure, "{HISTORY SIZE} {1000}");
  461. command(gts, do_configure, "{LOG MODE} {RAW}");
  462. command(gts, do_configure, "{MOUSE} {OFF}");
  463. command(gts, do_configure, "{PACKET PATCH} {AUTO}");
  464. /* command(gts, do_configure, "{RANDOM SEED} {AUTO}"); */ seed_rand(gts, ++gtd->utime);
  465. command(gts, do_configure, "{REPEAT CHAR} {!}");
  466. command(gts, do_configure, "{REPEAT ENTER} {OFF}");
  467. command(gts, do_configure, "{SCREEN READER} {%s}", HAS_BIT(greeting, STARTUP_FLAG_SCREENREADER) ? "ON" : "OFF");
  468. command(gts, do_configure, "{SCROLL LOCK} {ON}");
  469. command(gts, do_configure, "{SPEEDWALK} {OFF}");
  470. /* command(gts, do_configure, "{TAB WIDTH} {AUTO}"); */ gts->tab_width = 8;
  471. command(gts, do_configure, "{TELNET} {ON}");
  472. /* command(gts, do_configure, "{TINTIN CHAR} {#}"); */
  473. command(gts, do_configure, "{VERBATIM} {OFF}");
  474. command(gts, do_configure, "{VERBATIM CHAR} {\\}");
  475. command(gts, do_configure, "{VERBOSE} {%s}", HAS_BIT(greeting, STARTUP_FLAG_VERBOSE) ? "ON" : "OFF");
  476. command(gts, do_configure, "{WORDWRAP} {ON}");
  477. command(gts, do_pathdir, " n s 1");
  478. command(gts, do_pathdir, " e w 2");
  479. command(gts, do_pathdir, " s n 4");
  480. command(gts, do_pathdir, " w e 8");
  481. command(gts, do_pathdir, " u d 16");
  482. command(gts, do_pathdir, " d u 32");
  483. command(gts, do_pathdir, "ne sw 3");
  484. command(gts, do_pathdir, "nw se 9");
  485. command(gts, do_pathdir, "se nw 6");
  486. command(gts, do_pathdir, "sw ne 12");
  487. sprintf(filename, "%s/%s", gtd->system->tt_dir, HISTORY_FILE);
  488. if (access(filename, F_OK ) != -1)
  489. {
  490. command(gts, do_history, "read %s", filename);
  491. }
  492. gtd->level->input--;
  493. if (HAS_BIT(greeting, STARTUP_FLAG_VERBOSE))
  494. {
  495. gtd->level->verbose--;
  496. }
  497. if (HAS_BIT(greeting, STARTUP_FLAG_NOHUP))
  498. {
  499. SET_BIT(gtd->flags, TINTIN_FLAG_NOHUP);
  500. }
  501. init_terminal(gts);
  502. reset_screen(gts);
  503. command(gts, do_banner, "INIT");
  504. if (!HAS_BIT(greeting, STARTUP_FLAG_NOGREETING))
  505. {
  506. if (HAS_BIT(greeting, STARTUP_FLAG_SCREENREADER))
  507. {
  508. tintin_printf2(gts, "Welcome to TinTin Plus Plus. Don't know which MUD to play? How about the following MUD.");
  509. command(gts, do_banner, "RANDOM");
  510. tintin_printf2(gts, "You're using TinTin Plus Plus version %s written by Peter Unold, Bill Reis, and Igor van den Hoven.", CLIENT_VERSION);
  511. tintin_printf2(gts, "");
  512. tintin_printf2(gts, "For help and requests visit the Discord channel at https://discord.gg/gv7a37n");
  513. }
  514. else
  515. {
  516. command(gts, do_banner, "RANDOM");
  517. if (gtd->screen->cols >= 80)
  518. {
  519. command(gts, do_help, "GREETING");
  520. }
  521. else
  522. {
  523. tintin_printf2(gts, "\e[0;37mT I N T I N + + %s", CLIENT_VERSION);
  524. tintin_printf2(gts, "");
  525. // 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");
  526. tintin_printf2(gts, "Code by Peter Unold, Bill Reis, and Igor van den Hoven");
  527. }
  528. }
  529. }
  530. pop_call();
  531. return;
  532. }
  533. void quitmsg(char *message)
  534. {
  535. struct session *ses;
  536. static char crashed = FALSE;
  537. if (crashed++)
  538. {
  539. print_stdout(0, 0, "quitmsg(crashed)\n");
  540. fflush(NULL);
  541. exit(-1);
  542. }
  543. SET_BIT(gtd->flags, TINTIN_FLAG_TERMINATE);
  544. for (ses = gts->next ; ses ; ses = gts->next)
  545. {
  546. cleanup_session(ses);
  547. }
  548. if (gtd->chat)
  549. {
  550. chat_uninitialize("", "");
  551. }
  552. check_all_events(gts, EVENT_FLAG_SYSTEM, 0, 1, "PROGRAM TERMINATION", message ? message : "");
  553. if (HAS_BIT(gtd->flags, TINTIN_FLAG_HISTORYUPDATE) && gtd->history_size)
  554. {
  555. command(gts, do_history, "write %s/%s", gtd->system->tt_dir, HISTORY_FILE);
  556. }
  557. reset_daemon();
  558. reset_terminal(gts);
  559. reset_screen(gts);
  560. if (message == NULL || *message)
  561. {
  562. if (message)
  563. {
  564. print_stdout(0, 0, "\n\e[0m%s", message);
  565. }
  566. print_stdout(0, 0, "\nGoodbye from TinTin++\n\n");
  567. }
  568. fflush(stdout);
  569. exit(0);
  570. }
  571. void syserr_printf(struct session *ses, char *fmt, ...)
  572. {
  573. char buf[BUFFER_SIZE], name[BUFFER_SIZE], *errstr;
  574. errstr = strerror(errno);
  575. va_list args;
  576. va_start(args, fmt);
  577. vsprintf(buf, fmt, args);
  578. va_end(args);
  579. if (ses)
  580. {
  581. sprintf(name, "(%s)", ses->name);
  582. }
  583. else
  584. {
  585. sprintf(name, "(null)");
  586. }
  587. check_all_events(ses, SUB_SEC|EVENT_FLAG_SYSTEM, 0, 4, "SYSTEM ERROR", name, buf, ntos(errno), errstr);
  588. if (!check_all_events(ses, SUB_SEC|EVENT_FLAG_SYSTEM, 0, 4, "CATCH SYSTEM ERROR", name, buf, ntos(errno), errstr))
  589. {
  590. if (gts)
  591. {
  592. tintin_printf2(gts, "#SYSTEM ERROR %s %s (%d: %s)\e[0m", name, buf, errno, errstr);
  593. }
  594. if (ses && ses != gts)
  595. {
  596. tintin_printf2(ses, "#SYSTEM ERROR: %s %s (%d: %s)\e[0m", name, buf, errno, errstr);
  597. }
  598. if (ses && gtd->ses != ses && gtd->ses != gts)
  599. {
  600. tintin_printf2(gtd->ses, "#SYSTEM ERROR: %s %s (%d: %s)\e[0m", name, buf, errno, errstr);
  601. }
  602. }
  603. }
  604. void syserr_signal(int signal, char *msg)
  605. {
  606. char buf[256];
  607. static char crashed = FALSE;
  608. if (crashed++)
  609. {
  610. print_stdout(0, 0, "syserr_signal(crashed)\n");
  611. fflush(NULL);
  612. exit(-1);
  613. }
  614. reset_terminal(gts);
  615. reset_screen(gts);
  616. fflush(NULL);
  617. dump_stack();
  618. sprintf(buf, "\e[1;31mFATAL SIGNAL FROM (%s): %s\e[0m\n", msg, strsignal(signal));
  619. print_stdout(0, 0, "%s", buf);
  620. fflush(NULL);
  621. abort();
  622. }
  623. void syserr_fatal(int signal, char *msg)
  624. {
  625. char errstr[128];
  626. static char crashed = FALSE;
  627. if (crashed++)
  628. {
  629. print_stdout(0, 0, "\e[1;36msyserr_fatal(crashed) \e[0m\n");
  630. fflush(NULL);
  631. exit(-1);
  632. }
  633. if (signal == SIGHUP)
  634. {
  635. crashed--;
  636. check_all_events(gts, SUB_SEC|EVENT_FLAG_SYSTEM, 0, 0, "SIGHUB");
  637. crashed++;
  638. }
  639. if (signal <= 0)
  640. {
  641. sprintf(errstr, "(error %d: %s", errno, strerror(errno));
  642. }
  643. else
  644. {
  645. sprintf(errstr, "(signal %d: %s)", signal, strsignal(signal));
  646. }
  647. check_all_events(NULL, SUB_SEC|EVENT_FLAG_SYSTEM, 0, 1, "SYSTEM CRASH", errstr);
  648. if (gtd->level->quiet)
  649. {
  650. gtd->level->quiet = 0;
  651. }
  652. reset_terminal(gts);
  653. reset_screen(gts);
  654. print_stdout(0, 0, "\e[?1049l\e[r");
  655. dump_stack();
  656. print_stdout(0, 0, "%*s", (unsigned char) gtd->screen->cols, "");
  657. print_stdout(0, 0, "\n\e[1;31mFATAL ERROR \e[1;32m%s %s\e[0m\n", (unsigned char) gtd->screen->cols, "", msg, errstr);
  658. reset_daemon();
  659. fflush(NULL);
  660. abort();
  661. }