data.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159
  1. /******************************************************************************
  2. * This file is part of TinTin++ *
  3. * *
  4. * Copyright 2004-2020 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. * You should have received a copy of the GNU General Public License *
  17. * along with TinTin++. If not, see https://www.gnu.org/licenses. *
  18. ******************************************************************************/
  19. /******************************************************************************
  20. * T I N T I N + + *
  21. * *
  22. * coded by Igor van den Hoven 2004 *
  23. ******************************************************************************/
  24. #include "tintin.h"
  25. struct listroot *init_list(struct session *ses, int type, int size)
  26. {
  27. struct listroot *listhead;
  28. if ((listhead = (struct listroot *) calloc(1, sizeof(struct listroot))) == NULL)
  29. {
  30. syserr_fatal(-1, "init_list: calloc");
  31. }
  32. listhead->ses = ses;
  33. listhead->list = (struct listnode **) calloc(size, sizeof(struct listnode *));
  34. listhead->size = size;
  35. listhead->type = type;
  36. listhead->flags = list_table[type].flags;
  37. return listhead;
  38. }
  39. void kill_list(struct listroot *root)
  40. {
  41. while (root->used)
  42. {
  43. delete_index_list(root, root->used - 1);
  44. }
  45. // root->update = 0;
  46. }
  47. void free_list(struct listroot *root)
  48. {
  49. kill_list(root);
  50. free(root->list);
  51. free(root);
  52. }
  53. struct listroot *copy_list(struct session *ses, struct listroot *sourcelist, int type)
  54. {
  55. int i;
  56. struct listnode *node;
  57. push_call("copy_list(%p,%p,%p)",ses,sourcelist,type);
  58. ses->list[type] = init_list(ses, type, sourcelist->size);
  59. if (HAS_BIT(sourcelist->flags, LIST_FLAG_INHERIT))
  60. {
  61. for (i = 0 ; i < sourcelist->used ; i++)
  62. {
  63. node = (struct listnode *) calloc(1, sizeof(struct listnode));
  64. node->arg1 = str_dup_clone(sourcelist->list[i]->arg1);
  65. node->arg2 = str_dup_clone(sourcelist->list[i]->arg2);
  66. node->arg3 = str_dup_clone(sourcelist->list[i]->arg3);
  67. node->arg4 = str_dup_clone(sourcelist->list[i]->arg4);
  68. node->flags = sourcelist->list[i]->flags;
  69. node->group = strdup(sourcelist->list[i]->group);
  70. switch (type)
  71. {
  72. case LIST_ALIAS:
  73. node->regex = tintin_regexp_compile(ses, node, node->arg1, PCRE_ANCHORED);
  74. break;
  75. case LIST_ACTION:
  76. case LIST_GAG:
  77. case LIST_HIGHLIGHT:
  78. case LIST_PROMPT:
  79. case LIST_SUBSTITUTE:
  80. node->regex = tintin_regexp_compile(ses, node, node->arg1, 0);
  81. break;
  82. case LIST_BUTTON:
  83. node->val16[0] = sourcelist->list[i]->val16[0];
  84. node->val16[1] = sourcelist->list[i]->val16[1];
  85. node->val16[2] = sourcelist->list[i]->val16[2];
  86. node->val16[3] = sourcelist->list[i]->val16[3];
  87. break;
  88. case LIST_VARIABLE:
  89. copy_nest_node(ses->list[type], node, sourcelist->list[i]);
  90. break;
  91. }
  92. ses->list[type]->list[i] = node;
  93. }
  94. ses->list[type]->used = sourcelist->used;
  95. }
  96. ses->list[type]->flags = sourcelist->flags;
  97. pop_call();
  98. return ses->list[type];
  99. }
  100. struct listnode *insert_node_list(struct listroot *root, char *arg1, char *arg2, char *arg3, char *arg4)
  101. {
  102. int index;
  103. struct listnode *node;
  104. node = (struct listnode *) calloc(1, sizeof(struct listnode));
  105. if (list_table[root->type].priority_arg == 3 && *arg3 == 0)
  106. {
  107. strcpy(arg3, "5");
  108. }
  109. node->arg1 = str_dup(arg1);
  110. node->arg2 = str_dup(arg2);
  111. node->arg3 = str_dup(arg3);
  112. node->arg4 = str_dup(arg4);
  113. if (gtd->level->oneshot)
  114. {
  115. SET_BIT(node->flags, NODE_FLAG_ONESHOT);
  116. }
  117. node->group = HAS_BIT(root->flags, LIST_FLAG_CLASS) ? strdup(root->ses->group) : strdup("");
  118. switch (root->type)
  119. {
  120. case LIST_ALIAS:
  121. node->regex = tintin_regexp_compile(root->ses, node, node->arg1, PCRE_ANCHORED);
  122. break;
  123. case LIST_ACTION:
  124. case LIST_GAG:
  125. case LIST_HIGHLIGHT:
  126. case LIST_PROMPT:
  127. case LIST_SUBSTITUTE:
  128. node->regex = tintin_regexp_compile(root->ses, node, node->arg1, 0);
  129. break;
  130. }
  131. index = locate_index_list(root, arg1, arg3);
  132. return insert_index_list(root, node, index);
  133. }
  134. struct listnode *update_node_list(struct listroot *root, char *arg1, char *arg2, char *arg3, char *arg4)
  135. {
  136. int index;
  137. struct listnode *node;
  138. index = search_index_list(root, arg1, NULL);
  139. if (index != -1)
  140. {
  141. if (list_table[root->type].mode == SORT_DELAY && is_number(arg1))
  142. {
  143. return insert_node_list(root, arg1, arg2, arg3, arg4);
  144. }
  145. node = root->list[index];
  146. if (gtd->level->oneshot)
  147. {
  148. SET_BIT(node->flags, NODE_FLAG_ONESHOT);
  149. }
  150. if (strcmp(node->arg2, arg2) != 0)
  151. {
  152. node->arg2 = str_cpy(&node->arg2, arg2);
  153. }
  154. switch (root->type)
  155. {
  156. case LIST_DELAY:
  157. case LIST_TICKER:
  158. node->val64 = 0;
  159. break;
  160. }
  161. if (list_table[root->type].priority_arg == 3 && *arg3 == 0)
  162. {
  163. strcpy(arg3, "5");
  164. }
  165. switch (list_table[root->type].mode)
  166. {
  167. case SORT_PRIORITY:
  168. if (atof(node->arg3) != atof(arg3))
  169. {
  170. delete_index_list(root, index);
  171. return insert_node_list(root, arg1, arg2, arg3, arg4);
  172. }
  173. break;
  174. case SORT_APPEND:
  175. delete_index_list(root, index);
  176. return insert_node_list(root, arg1, arg2, arg3, arg4);
  177. break;
  178. case SORT_ALPHA:
  179. case SORT_DELAY:
  180. if (strcmp(node->arg3, arg3) != 0)
  181. {
  182. str_cpy(&node->arg3, arg3);
  183. }
  184. break;
  185. default:
  186. tintin_printf2(root->ses, "#BUG: update_node_list: unknown mode: %d", list_table[root->type].mode);
  187. break;
  188. }
  189. return node;
  190. }
  191. else
  192. {
  193. return insert_node_list(root, arg1, arg2, arg3, arg4);
  194. }
  195. }
  196. struct listnode *insert_index_list(struct listroot *root, struct listnode *node, int index)
  197. {
  198. root->used++;
  199. if (root->used == root->size)
  200. {
  201. root->size *= 2;
  202. root->list = (struct listnode **) realloc(root->list, (root->size) * sizeof(struct listnode *));
  203. }
  204. memmove(&root->list[index + 1], &root->list[index], (root->used - index) * sizeof(struct listnode *));
  205. root->list[index] = node;
  206. return node;
  207. }
  208. void delete_node_list(struct session *ses, int type, struct listnode *node)
  209. {
  210. int index = search_index_list(ses->list[type], node->arg1, node->arg3);
  211. delete_index_list(ses->list[type], index);
  212. }
  213. void delete_index_list(struct listroot *root, int index)
  214. {
  215. struct listnode *node = root->list[index];
  216. if (node->root)
  217. {
  218. free_list(node->root);
  219. }
  220. if (index <= root->update)
  221. {
  222. root->update--;
  223. }
  224. str_free(node->arg1);
  225. str_free(node->arg2);
  226. str_free(node->arg3);
  227. str_free(node->arg4);
  228. free(node->group);
  229. if (HAS_BIT(list_table[root->type].flags, LIST_FLAG_REGEX))
  230. {
  231. if (node->regex)
  232. {
  233. free(node->regex);
  234. }
  235. }
  236. switch (root->type)
  237. {
  238. case LIST_TERRAIN:
  239. free(node->room);
  240. break;
  241. case LIST_CLASS:
  242. if (node->data)
  243. {
  244. free(node->data);
  245. }
  246. break;
  247. }
  248. free(node);
  249. memmove(&root->list[index], &root->list[index + 1], (root->used - index) * sizeof(struct listnode *));
  250. root->used--;
  251. return;
  252. }
  253. struct listnode *search_node_list(struct listroot *root, char *text)
  254. {
  255. int index;
  256. push_call("search_node_list(%p,%p)",root,text);
  257. switch (list_table[root->type].mode)
  258. {
  259. case SORT_ALPHA:
  260. case SORT_DELAY:
  261. index = bsearch_alpha_list(root, text, 0);
  262. break;
  263. default:
  264. index = nsearch_list(root, text);
  265. break;
  266. }
  267. if (index != -1)
  268. {
  269. pop_call();
  270. return root->list[index];
  271. }
  272. pop_call();
  273. return NULL;
  274. }
  275. int search_index_list(struct listroot *root, char *text, char *priority)
  276. {
  277. if (list_table[root->type].mode == SORT_ALPHA || list_table[root->type].mode == SORT_DELAY)
  278. {
  279. return bsearch_alpha_list(root, text, 0);
  280. }
  281. if (list_table[root->type].mode == SORT_PRIORITY && priority)
  282. {
  283. return bsearch_priority_list(root, text, priority, 0);
  284. }
  285. return nsearch_list(root, text);
  286. }
  287. /*
  288. Return insertion index.
  289. */
  290. int locate_index_list(struct listroot *root, char *text, char *priority)
  291. {
  292. switch (list_table[root->type].mode)
  293. {
  294. case SORT_ALPHA:
  295. case SORT_DELAY:
  296. return bsearch_alpha_list(root, text, 1);
  297. case SORT_PRIORITY:
  298. return bsearch_priority_list(root, text, priority, 1);
  299. default:
  300. return root->used;
  301. }
  302. }
  303. /*
  304. binary search on alphabetically sorted list
  305. */
  306. int bsearch_alpha_list(struct listroot *root, char *text, int seek)
  307. {
  308. long long bot, top, val;
  309. long double toi, toj, srt;
  310. push_call("bsearch_alpha_list(%p,%p,%d)",root,text,seek);
  311. bot = 0;
  312. top = root->used - 1;
  313. val = top;
  314. // toi = get_number(root->ses, text);
  315. if (seek == 0 && (*text == '+' || *text == '-') && is_math(root->ses, text) && HAS_BIT(root->flags, LIST_FLAG_NEST))
  316. {
  317. toi = get_number(root->ses, text);
  318. if (toi > 0 && toi <= root->used)
  319. {
  320. pop_call();
  321. return toi - 1;
  322. }
  323. if (toi < 0 && toi + root->used >= 0)
  324. {
  325. pop_call();
  326. return root->used + toi;
  327. }
  328. else
  329. {
  330. pop_call();
  331. return -1;
  332. }
  333. }
  334. toi = is_number(text) ? tintoi(text) : 0;
  335. while (bot <= top)
  336. {
  337. toj = is_number(root->list[val]->arg1) ? tintoi(root->list[val]->arg1) : 0;
  338. if (toi)
  339. {
  340. srt = toi - toj;
  341. }
  342. else if (toj)
  343. {
  344. srt = -1;
  345. }
  346. else
  347. {
  348. srt = strcmp(text, root->list[val]->arg1);
  349. }
  350. if (srt == 0)
  351. {
  352. pop_call();
  353. return val;
  354. }
  355. if (srt < 0)
  356. {
  357. top = val - 1;
  358. }
  359. else
  360. {
  361. bot = val + 1;
  362. }
  363. val = bot + (top - bot) / 2;
  364. }
  365. if (seek)
  366. {
  367. pop_call();
  368. return UMAX(0, val);
  369. }
  370. pop_call();
  371. return -1;
  372. }
  373. /*
  374. Binary search on priorially and alphabetically sorted list
  375. */
  376. int bsearch_priority_list(struct listroot *root, char *text, char *priority, int seek)
  377. {
  378. int bot, top, val;
  379. double srt;
  380. bot = 0;
  381. top = root->used - 1;
  382. val = top;
  383. while (bot <= top)
  384. {
  385. srt = atof(priority) - atof(root->list[val]->arg3);
  386. if (!srt)
  387. {
  388. srt = strcmp(text, root->list[val]->arg1);
  389. }
  390. if (srt == 0)
  391. {
  392. return val;
  393. }
  394. if (srt < 0)
  395. {
  396. top = val - 1;
  397. }
  398. else
  399. {
  400. bot = val + 1;
  401. }
  402. val = bot + (top - bot) / 2;
  403. }
  404. if (seek)
  405. {
  406. return UMAX(0, val);
  407. }
  408. else
  409. {
  410. return -1;
  411. }
  412. }
  413. /*
  414. Linear search
  415. */
  416. int nsearch_list(struct listroot *root, char *text)
  417. {
  418. int i;
  419. for (i = 0 ; i < root->used ; i++)
  420. {
  421. if (!strcmp(text, root->list[i]->arg1))
  422. {
  423. return i;
  424. }
  425. }
  426. return -1;
  427. }
  428. /*
  429. show content of a node on screen
  430. */
  431. void show_node(struct listroot *root, struct listnode *node, int level)
  432. {
  433. char *str_arg2 = str_dup("");
  434. show_nest_node(node, &str_arg2, TRUE);
  435. switch (list_table[root->type].args)
  436. {
  437. case 4:
  438. tintin_printf2(root->ses, "%s" COLOR_TINTIN "#" COLOR_COMMAND "%s " COLOR_BRACE "{" COLOR_STRING "%s" COLOR_BRACE "} {" COLOR_STRING "%s" COLOR_BRACE "} {" COLOR_STRING "%s" COLOR_BRACE "} {" COLOR_STRING "%s" COLOR_BRACE "}", indent(level), list_table[root->type].name, node->arg1, str_arg2, node->arg3, node->arg4);
  439. break;
  440. case 3:
  441. if (list_table[root->type].priority_arg == 3 && !strcmp(node->arg3, "5"))
  442. {
  443. tintin_printf2(root->ses, "%s" COLOR_TINTIN "#" COLOR_COMMAND "%s " COLOR_BRACE "{" COLOR_STRING "%s" COLOR_BRACE "} {" COLOR_STRING "%s" COLOR_BRACE "}", indent(level), list_table[root->type].name, node->arg1, str_arg2);
  444. }
  445. else
  446. {
  447. tintin_printf2(root->ses, "%s" COLOR_TINTIN "#" COLOR_COMMAND "%s " COLOR_BRACE "{" COLOR_STRING "%s" COLOR_BRACE "} {" COLOR_STRING "%s" COLOR_BRACE "} {" COLOR_STRING "%s" COLOR_BRACE "}", indent(level), list_table[root->type].name, node->arg1, str_arg2, node->arg3);
  448. }
  449. break;
  450. case 2:
  451. tintin_printf2(root->ses, "%s" COLOR_TINTIN "#" COLOR_COMMAND "%s " COLOR_BRACE "{" COLOR_STRING "%s" COLOR_BRACE "} {" COLOR_STRING "%s" COLOR_BRACE "}", indent(level), list_table[root->type].name, node->arg1, str_arg2);
  452. break;
  453. case 1:
  454. tintin_printf2(root->ses, "%s" COLOR_TINTIN "#" COLOR_COMMAND "%s " COLOR_BRACE "{" COLOR_STRING "%s" COLOR_BRACE "}", indent(level), list_table[root->type].name, node->arg1);
  455. break;
  456. }
  457. str_free(str_arg2);
  458. }
  459. /*
  460. list content of a list on screen
  461. */
  462. void show_list(struct listroot *root, int level)
  463. {
  464. int i;
  465. if (root == root->ses->list[root->type])
  466. {
  467. tintin_header(root->ses, " %s ", list_table[root->type].name_multi);
  468. }
  469. for (i = 0 ; i < root->used ; i++)
  470. {
  471. show_node(root, root->list[i], level);
  472. }
  473. }
  474. int show_node_with_wild(struct session *ses, char *text, struct listroot *root)
  475. {
  476. struct listnode *node;
  477. int i, found = FALSE;
  478. push_call("show_node_with_wild(%p,%p,%p)",ses,text,root);
  479. node = search_node_list(root, text);
  480. if (node)
  481. {
  482. if (list_table[root->type].script_arg == 2)
  483. {
  484. if (list_table[root->type].args == 2)
  485. {
  486. tintin_printf2(ses, COLOR_TINTIN "%c" COLOR_COMMAND "%s " COLOR_BRACE "{" COLOR_STRING "%s" COLOR_BRACE "}\n{\n" COLOR_STRING "%s\n" COLOR_BRACE "}\n", gtd->tintin_char, list_table[root->type].name, node->arg1, script_viewer(ses, node->arg2));
  487. }
  488. else if (list_table[root->type].args == 3)
  489. {
  490. if (list_table[root->type].priority_arg == 3)
  491. {
  492. if (!strcmp(node->arg3, "5"))
  493. {
  494. tintin_printf2(ses, COLOR_TINTIN "%c" COLOR_COMMAND "%s " COLOR_BRACE "{" COLOR_STRING "%s" COLOR_BRACE "}\n{\n" COLOR_STRING "%s\n" COLOR_BRACE "}\n", gtd->tintin_char, list_table[root->type].name, node->arg1, script_viewer(ses, node->arg2));
  495. }
  496. else
  497. {
  498. tintin_printf2(ses, COLOR_TINTIN "%c" COLOR_COMMAND "%s " COLOR_BRACE "{" COLOR_STRING "%s" COLOR_BRACE "}\n{\n" COLOR_STRING "%s\n" COLOR_BRACE "}\n{" COLOR_STRING "%s" COLOR_BRACE "}\n", gtd->tintin_char, list_table[root->type].name, node->arg1, script_viewer(ses, node->arg2), node->arg3);
  499. }
  500. }
  501. else
  502. {
  503. tintin_printf2(ses, COLOR_TINTIN "%c" COLOR_COMMAND "%s " COLOR_BRACE "{" COLOR_STRING "%s" COLOR_BRACE "}\n{\n" COLOR_STRING "%s\n" COLOR_BRACE "}\n{" COLOR_STRING "%s" COLOR_BRACE "}\n", gtd->tintin_char, list_table[root->type].name, node->arg1, script_viewer(ses, node->arg2), node->arg3);
  504. }
  505. }
  506. }
  507. else
  508. {
  509. show_node(root, node, 0);
  510. }
  511. switch(root->type)
  512. {
  513. // case LIST_EVENT:
  514. // case LIST_FUNCTION:
  515. // case LIST_MACRO:
  516. // tintin_printf2(ses, COLOR_TINTIN "%c" COLOR_COMMAND "%s " COLOR_BRACE "{" COLOR_STRING "%s" COLOR_BRACE "}\n{\n" COLOR_STRING "%s\n" COLOR_BRACE "}\n", gtd->tintin_char, list_table[root->type].name, node->arg1, script_viewer(ses, node->arg2));
  517. // break;
  518. // case LIST_ACTION:
  519. // case LIST_ALIAS:
  520. // case LIST_BUTTON:
  521. // if (!strcmp(node->arg3, "5"))
  522. // {
  523. // tintin_printf2(ses, COLOR_TINTIN "%c" COLOR_COMMAND "%s " COLOR_BRACE "{" COLOR_STRING "%s" COLOR_BRACE "}\n{\n" COLOR_STRING "%s\n" COLOR_BRACE "}\n", gtd->tintin_char, list_table[root->type].name, node->arg1, script_viewer(ses, node->arg2));
  524. // }
  525. // else
  526. // {
  527. // tintin_printf2(ses, COLOR_TINTIN "%c" COLOR_COMMAND "%s " COLOR_BRACE "{" COLOR_STRING "%s" COLOR_BRACE "}\n{\n" COLOR_STRING "%s\n" COLOR_BRACE "}\n{" COLOR_STRING "%s" COLOR_BRACE "}\n", gtd->tintin_char, list_table[root->type].name, node->arg1, script_viewer(ses, node->arg2), node->arg3);
  528. // }
  529. // break;
  530. // case LIST_DELAY:
  531. // case LIST_TICKER:
  532. // tintin_printf2(ses, COLOR_TINTIN "%c" COLOR_COMMAND "%s " COLOR_BRACE "{" COLOR_STRING "%s" COLOR_BRACE "}\n{\n" COLOR_STRING "%s\n" COLOR_BRACE "}\n{" COLOR_STRING "%s" COLOR_BRACE "}\n\n", gtd->tintin_char, list_table[root->type].name, node->arg1, script_viewer(ses, node->arg2), node->arg3);
  533. // break;
  534. // default:
  535. // show_node(root, node, 0);
  536. // break;
  537. }
  538. pop_call();
  539. return TRUE;
  540. }
  541. for (i = 0 ; i < root->used ; i++)
  542. {
  543. if (match(ses, root->list[i]->arg1, text, SUB_VAR|SUB_FUN))
  544. {
  545. show_node(root, root->list[i], 0);
  546. found = TRUE;
  547. }
  548. }
  549. pop_call();
  550. return found;
  551. }
  552. int delete_node_with_wild(struct session *ses, int type, char *text)
  553. {
  554. struct listroot *root = ses->list[type];
  555. struct listnode *node;
  556. char arg1[BUFFER_SIZE];
  557. int i, found = FALSE;
  558. sub_arg_in_braces(ses, text, arg1, GET_ALL, SUB_VAR|SUB_FUN);
  559. node = search_node_list(root, arg1);
  560. if (node)
  561. {
  562. show_message(ses, type, "#OK. {%s} IS NO LONGER %s %s.", node->arg1, (*list_table[type].name == 'A' || *list_table[type].name == 'E') ? "AN" : "A", list_table[type].name);
  563. delete_node_list(ses, type, node);
  564. return TRUE;
  565. }
  566. for (i = root->used - 1 ; i >= 0 ; i--)
  567. {
  568. if (match(ses, root->list[i]->arg1, arg1, SUB_VAR|SUB_FUN))
  569. {
  570. show_message(ses, type, "#OK. {%s} IS NO LONGER %s %s.", root->list[i]->arg1, (*list_table[type].name == 'A' || *list_table[type].name == 'E') ? "AN" : "A", list_table[type].name);
  571. delete_index_list(root, i);
  572. found = TRUE;
  573. }
  574. }
  575. if (found == 0)
  576. {
  577. show_message(ses, type, "#KILL: NO MATCHES FOUND FOR %s {%s}.", list_table[type].name, arg1);
  578. return FALSE;
  579. }
  580. return TRUE;
  581. }
  582. DO_COMMAND(do_killall)
  583. {
  584. tintin_printf2(ses, "\e[1;31m#NOTICE: PLEASE CHANGE #KILLALL TO #KILL ALL.");
  585. do_kill(ses, arg);
  586. return ses;
  587. }
  588. DO_COMMAND(do_kill)
  589. {
  590. char arg1[BUFFER_SIZE], arg2[BUFFER_SIZE];
  591. int index;
  592. arg = get_arg_in_braces(ses, arg, arg1, GET_ONE);
  593. get_arg_in_braces(ses, arg, arg2, GET_ALL);
  594. if (*arg1 == 0 || !strcasecmp(arg1, "ALL"))
  595. {
  596. for (index = 0 ; index < LIST_MAX ; index++)
  597. {
  598. if (!HAS_BIT(ses->list[index]->flags, LIST_FLAG_HIDE))
  599. {
  600. kill_list(ses->list[index]);
  601. }
  602. }
  603. show_message(ses, LIST_COMMAND, "#KILL - ALL LISTS CLEARED.");
  604. return ses;
  605. }
  606. for (index = 0 ; index < LIST_MAX ; index++)
  607. {
  608. if (!is_abbrev(arg1, list_table[index].name) && !is_abbrev(arg1, list_table[index].name_multi))
  609. {
  610. continue;
  611. }
  612. if (*arg2 == 0 || !strcasecmp(arg2, "ALL"))
  613. {
  614. kill_list(ses->list[index]);
  615. show_message(ses, LIST_COMMAND, "#OK: #%s LIST CLEARED.", list_table[index].name);
  616. }
  617. else
  618. {
  619. delete_node_with_wild(ses, index, arg);
  620. }
  621. break;
  622. }
  623. if (index == LIST_MAX)
  624. {
  625. show_error(ses, LIST_COMMAND, "#ERROR: #KILL {%s} {%s} - NO MATCH FOUND.", arg1, arg2);
  626. }
  627. return ses;
  628. }
  629. DO_COMMAND(do_message)
  630. {
  631. char arg1[BUFFER_SIZE], arg2[BUFFER_SIZE];
  632. int index, found = FALSE;
  633. arg = get_arg_in_braces(ses, arg, arg1, GET_ONE);
  634. arg = get_arg_in_braces(ses, arg, arg2, GET_ONE);
  635. if (*arg1 == 0)
  636. {
  637. tintin_header(ses, " MESSAGES ");
  638. for (index = 0 ; index < LIST_MAX ; index++)
  639. {
  640. if (!HAS_BIT(list_table[index].flags, LIST_FLAG_HIDE))
  641. {
  642. tintin_printf2(ses, " %-20s %3s", list_table[index].name_multi, HAS_BIT(ses->list[index]->flags, LIST_FLAG_MESSAGE) ? "ON" : "OFF");
  643. }
  644. }
  645. tintin_header(ses, "");
  646. }
  647. else
  648. {
  649. for (index = found = 0 ; index < LIST_MAX ; index++)
  650. {
  651. if (HAS_BIT(list_table[index].flags, LIST_FLAG_HIDE))
  652. {
  653. continue;
  654. }
  655. if (!is_abbrev(arg1, list_table[index].name) && !is_abbrev(arg1, list_table[index].name_multi) && strcasecmp(arg1, "ALL"))
  656. {
  657. continue;
  658. }
  659. if (*arg2 == 0)
  660. {
  661. TOG_BIT(ses->list[index]->flags, LIST_FLAG_MESSAGE);
  662. }
  663. else if (is_abbrev(arg2, "ON"))
  664. {
  665. SET_BIT(ses->list[index]->flags, LIST_FLAG_MESSAGE);
  666. }
  667. else if (is_abbrev(arg2, "OFF"))
  668. {
  669. DEL_BIT(ses->list[index]->flags, LIST_FLAG_MESSAGE);
  670. }
  671. else
  672. {
  673. show_error(ses, LIST_COMMAND, "#SYNTAX: #MESSAGE {%s} [ON|OFF]", arg1);
  674. return ses;
  675. }
  676. show_message(ses, LIST_COMMAND, "#OK: #%s MESSAGES HAVE BEEN SET TO: %s.", list_table[index].name, HAS_BIT(ses->list[index]->flags, LIST_FLAG_MESSAGE) ? "ON" : "OFF");
  677. found = TRUE;
  678. }
  679. if (found == FALSE)
  680. {
  681. show_error(ses, LIST_COMMAND, "#ERROR: #MESSAGE {%s} - NO MATCH FOUND.", arg1);
  682. }
  683. }
  684. return ses;
  685. }
  686. DO_COMMAND(do_ignore)
  687. {
  688. char arg1[BUFFER_SIZE], arg2[BUFFER_SIZE];
  689. int index, found = FALSE;
  690. arg = get_arg_in_braces(ses, arg, arg1, GET_ONE);
  691. arg = get_arg_in_braces(ses, arg, arg2, GET_ONE);
  692. if (*arg1 == 0)
  693. {
  694. tintin_header(ses, " IGNORES ");
  695. for (index = 0 ; index < LIST_MAX ; index++)
  696. {
  697. if (!HAS_BIT(list_table[index].flags, LIST_FLAG_HIDE))
  698. {
  699. tintin_printf2(ses, " %-20s %3s", list_table[index].name_multi, HAS_BIT(ses->list[index]->flags, LIST_FLAG_IGNORE) ? "ON" : "OFF");
  700. }
  701. }
  702. tintin_header(ses, "");
  703. }
  704. else
  705. {
  706. for (index = found = 0 ; index < LIST_MAX ; index++)
  707. {
  708. if (HAS_BIT(list_table[index].flags, LIST_FLAG_HIDE))
  709. {
  710. continue;
  711. }
  712. if (!is_abbrev(arg1, list_table[index].name) && !is_abbrev(arg1, list_table[index].name_multi) && strcasecmp(arg1, "ALL"))
  713. {
  714. continue;
  715. }
  716. if (*arg2 == 0)
  717. {
  718. TOG_BIT(ses->list[index]->flags, LIST_FLAG_IGNORE);
  719. }
  720. else if (is_abbrev(arg2, "ON"))
  721. {
  722. SET_BIT(ses->list[index]->flags, LIST_FLAG_IGNORE);
  723. }
  724. else if (is_abbrev(arg2, "OFF"))
  725. {
  726. DEL_BIT(ses->list[index]->flags, LIST_FLAG_IGNORE);
  727. }
  728. else
  729. {
  730. show_error(ses, LIST_COMMAND, "#SYNTAX: #IGNORE {%s} [ON|OFF]", arg1);
  731. return ses;
  732. }
  733. show_message(ses, LIST_COMMAND, "#OK: #%s IGNORE STATUS HAS BEEN SET TO: %s.", list_table[index].name, HAS_BIT(ses->list[index]->flags, LIST_FLAG_IGNORE) ? "ON" : "OFF");
  734. found = TRUE;
  735. }
  736. if (found == FALSE)
  737. {
  738. show_error(ses, LIST_COMMAND, "#ERROR: #IGNORE {%s} - NO MATCH FOUND.", arg1);
  739. }
  740. }
  741. return ses;
  742. }
  743. DO_COMMAND(do_debug)
  744. {
  745. char arg1[BUFFER_SIZE], arg2[BUFFER_SIZE];
  746. int index, found = FALSE;
  747. arg = get_arg_in_braces(ses, arg, arg1, GET_ONE);
  748. arg = get_arg_in_braces(ses, arg, arg2, GET_ONE);
  749. if (*arg1 == 0)
  750. {
  751. tintin_header(ses, " DEBUGS ");
  752. for (index = 0 ; index < LIST_MAX ; index++)
  753. {
  754. if (!HAS_BIT(list_table[index].flags, LIST_FLAG_HIDE))
  755. {
  756. tintin_printf2(ses, " %-20s %3s", list_table[index].name_multi, HAS_BIT(ses->list[index]->flags, LIST_FLAG_DEBUG) ? "ON" : "OFF");
  757. }
  758. }
  759. tintin_header(ses, "");
  760. }
  761. else
  762. {
  763. for (index = found = 0 ; index < LIST_MAX ; index++)
  764. {
  765. if (HAS_BIT(list_table[index].flags, LIST_FLAG_HIDE))
  766. {
  767. continue;
  768. }
  769. if (!is_abbrev(arg1, list_table[index].name) && !is_abbrev(arg1, list_table[index].name_multi) && strcasecmp(arg1, "ALL"))
  770. {
  771. continue;
  772. }
  773. if (*arg2 == 0)
  774. {
  775. TOG_BIT(ses->list[index]->flags, LIST_FLAG_DEBUG);
  776. }
  777. else if (is_abbrev(arg2, "ON"))
  778. {
  779. SET_BIT(ses->list[index]->flags, LIST_FLAG_DEBUG);
  780. }
  781. else if (is_abbrev(arg2, "OFF"))
  782. {
  783. DEL_BIT(ses->list[index]->flags, LIST_FLAG_DEBUG);
  784. DEL_BIT(ses->list[index]->flags, LIST_FLAG_LOG);
  785. }
  786. else if (is_abbrev(arg2, "LOG"))
  787. {
  788. SET_BIT(ses->list[index]->flags, LIST_FLAG_LOG);
  789. }
  790. else
  791. {
  792. show_error(ses, LIST_COMMAND, "#SYNTAX: #DEBUG {%s} [ON|OFF|LOG]", arg1);
  793. return ses;
  794. }
  795. show_message(ses, LIST_COMMAND, "#OK: #%s DEBUG STATUS HAS BEEN SET TO: %s.", list_table[index].name, is_abbrev(arg2, "LOG") ? "LOG" : HAS_BIT(ses->list[index]->flags, LIST_FLAG_DEBUG) ? "ON" : "OFF");
  796. found = TRUE;
  797. }
  798. if (found == FALSE)
  799. {
  800. show_error(ses, LIST_COMMAND, "#DEBUG {%s} - NO MATCH FOUND.", arg1);
  801. }
  802. }
  803. return ses;
  804. }
  805. DO_COMMAND(do_info)
  806. {
  807. char arg1[BUFFER_SIZE], arg2[BUFFER_SIZE], name[BUFFER_SIZE];
  808. int cnt, index, found = FALSE;
  809. struct listroot *root;
  810. arg = get_arg_in_braces(ses, arg, arg1, GET_ONE);
  811. arg = get_arg_in_braces(ses, arg, arg2, GET_ONE);
  812. if (*arg1 == 0)
  813. {
  814. tintin_header(ses, " INFORMATION ");
  815. for (index = 0 ; index < LIST_MAX ; index++)
  816. {
  817. if (!HAS_BIT(ses->list[index]->flags, LIST_FLAG_HIDE))
  818. {
  819. tintin_printf2(ses, "%-15s %5d IGNORE %3s MESSAGE %3s INFO %3s DEBUG %3s %3s",
  820. list_table[index].name_multi,
  821. ses->list[index]->used,
  822. HAS_BIT(ses->list[index]->flags, LIST_FLAG_IGNORE) ? "ON" : "OFF",
  823. HAS_BIT(ses->list[index]->flags, LIST_FLAG_MESSAGE) ? "ON" : "OFF",
  824. HAS_BIT(ses->list[index]->flags, LIST_FLAG_INFO) ? "ON" : "OFF",
  825. HAS_BIT(ses->list[index]->flags, LIST_FLAG_DEBUG) ? "ON" : "OFF",
  826. HAS_BIT(ses->list[index]->flags, LIST_FLAG_LOG) ? "LOG" : " ");
  827. }
  828. }
  829. tintin_header(ses, "");
  830. }
  831. else
  832. {
  833. for (index = found = 0 ; index < LIST_MAX ; index++)
  834. {
  835. if (HAS_BIT(list_table[index].flags, LIST_FLAG_HIDE))
  836. {
  837. continue;
  838. }
  839. if (!is_abbrev(arg1, list_table[index].name) && !is_abbrev(arg1, list_table[index].name_multi) && strcasecmp(arg1, "ALL"))
  840. {
  841. continue;
  842. }
  843. if (*arg2 == 0)
  844. {
  845. TOG_BIT(ses->list[index]->flags, LIST_FLAG_INFO);
  846. }
  847. else if (is_abbrev(arg2, "ON"))
  848. {
  849. SET_BIT(ses->list[index]->flags, LIST_FLAG_INFO);
  850. }
  851. else if (is_abbrev(arg2, "OFF"))
  852. {
  853. DEL_BIT(ses->list[index]->flags, LIST_FLAG_INFO);
  854. }
  855. else
  856. {
  857. root = ses->list[index];
  858. if (is_abbrev(arg2, "LIST"))
  859. {
  860. for (cnt = 0 ; cnt < root->used ; cnt++)
  861. {
  862. tintin_printf2(ses, "#INFO %s %4d {arg1}{%s} {arg2}{%s} {arg3}{%s} {arg4}{%s} {class}{%s} {flags}{%d}", list_table[index].name, cnt, root->list[cnt]->arg1, root->list[cnt]->arg2, root->list[cnt]->arg3, root->list[cnt]->arg4, root->list[cnt]->group, root->list[cnt]->flags);
  863. }
  864. }
  865. else if (is_abbrev(arg2, "SAVE"))
  866. {
  867. sprintf(name, "info[%s]", list_table[index].name);
  868. // delete_nest_node(ses->list[LIST_VARIABLE], name);
  869. for (cnt = 0 ; cnt < root->used ; cnt++)
  870. {
  871. sprintf(name, "info[%s][%d]", list_table[index].name, cnt);
  872. set_nest_node_ses(ses, name, "{arg1}{%s}{arg2}{%s}{arg3}{%s}{arg4}{%s}{class}{%s}{flags}{%d}", root->list[cnt]->arg1, root->list[cnt]->arg2, root->list[cnt]->arg3, root->list[cnt]->arg4, root->list[cnt]->group, root->list[cnt]->flags);
  873. }
  874. show_message(ses, LIST_COMMAND, "#INFO: DATA WRITTEN TO {info[%s]}", list_table[index].name);
  875. }
  876. else
  877. {
  878. show_error(ses, LIST_COMMAND, "#SYNTAX: #INFO {%s} [ON|OFF|LIST|SAVE|SYSTEM]", arg1);
  879. }
  880. return ses;
  881. }
  882. show_message(ses, LIST_COMMAND, "#OK: #%s INFO STATUS HAS BEEN SET TO: %s.", list_table[index].name, HAS_BIT(ses->list[index]->flags, LIST_FLAG_INFO) ? "ON" : "OFF");
  883. found = TRUE;
  884. }
  885. if (found == FALSE)
  886. {
  887. if (is_abbrev(arg1, "CPU"))
  888. {
  889. show_cpu(ses);
  890. }
  891. else if (is_abbrev(arg1, "MCCP"))
  892. {
  893. if (ses->mccp2)
  894. {
  895. tintin_printf2(ses, "#INFO MCCP2: TOTAL IN: %9ull TOTAL OUT: %9ull RATIO: %3d", ses->mccp2->total_in, ses->mccp2->total_out, 100 * ses->mccp2->total_out / ses->mccp2->total_in);
  896. }
  897. if (ses->mccp3)
  898. {
  899. tintin_printf2(ses, "#INFO MCCP3: TOTAL IN: %9ull TOTAL OUT: %9ull RATIO: %3d", ses->mccp3->total_in, ses->mccp3->total_out, 100 * ses->mccp3->total_out / ses->mccp3->total_in);
  900. }
  901. }
  902. else if (is_abbrev(arg1, "SESSION"))
  903. {
  904. if (is_abbrev(arg2, "SAVE"))
  905. {
  906. sprintf(name, "info[SESSION]");
  907. set_nest_node_ses(ses, name, "{SESSION_NAME}{%s}", ses->name);
  908. add_nest_node_ses(ses, name, "{SESSION_CLASS}{%s}", ses->group);
  909. add_nest_node_ses(ses, name, "{SESSION_CREATED}{%d}", ses->created);
  910. add_nest_node_ses(ses, name, "{SESSION_HOST} {%s}", ses->session_host);
  911. add_nest_node_ses(ses, name, "{SESSION_IP} {%s}", ses->session_ip);
  912. add_nest_node_ses(ses, name, "{SESSION_PORT} {%s}", ses->session_port);
  913. show_message(ses, LIST_COMMAND, "#INFO: DATA WRITTEN TO {info[SESSION]}");
  914. }
  915. else
  916. {
  917. tintin_printf2(ses, "{SESSION_NAME}{%s}", ses->name);
  918. tintin_printf2(ses, "{SESSION_CLASS}{%s}", ses->group);
  919. tintin_printf2(ses, "{SESSION_CREATED}{%d}", ses->created);
  920. tintin_printf2(ses, "{SESSION_HOST} {%s}", ses->session_host);
  921. tintin_printf2(ses, "{SESSION_IP} {%s}", ses->session_ip);
  922. tintin_printf2(ses, "{SESSION_PORT} {%s}", ses->session_port);
  923. }
  924. }
  925. else if (is_abbrev(arg1, "STACK"))
  926. {
  927. dump_stack();
  928. }
  929. else if (is_abbrev(arg1, "SYSTEM"))
  930. {
  931. if (is_abbrev(arg2, "SAVE"))
  932. {
  933. sprintf(name, "info[SYSTEM]");
  934. set_nest_node_ses(ses, name, "{CLIENT_NAME}{%s}{CLIENT_VERSION}{%s}", CLIENT_NAME, CLIENT_VERSION);
  935. add_nest_node_ses(ses, name, "{CLIENT}{{NAME}{%s}{VERSION}{%s}}", CLIENT_NAME, CLIENT_VERSION);
  936. add_nest_node_ses(ses, name, "{EXEC}{%s}{HOME}{%s}{LANG}{%s}{OS}{%s}{TERM}{%s}", gtd->exec, gtd->home, gtd->lang, gtd->os, gtd->term);
  937. add_nest_node_ses(ses, name, "{DETACH_FILE}{%s}{ATTACH_FILE}{%s}", gtd->detach_port > 0 ? gtd->detach_file : "", gtd->attach_sock > 0 ? gtd->attach_file : "");
  938. show_message(ses, LIST_COMMAND, "#INFO: DATA WRITTEN TO {info[SYSTEM]}");
  939. }
  940. else
  941. {
  942. tintin_printf2(ses, "#INFO SYSTEM: CLIENT_NAME = %s", CLIENT_NAME);
  943. tintin_printf2(ses, "#INFO SYSTEM: CLIENT_VERSION = %s", CLIENT_VERSION);
  944. tintin_printf2(ses, "#INFO SYSTEM: EXEC = %s", gtd->exec);
  945. tintin_printf2(ses, "#INFO SYSTEM: HOME = %s", gtd->home);
  946. tintin_printf2(ses, "#INFO SYSTEM: LANG = %s", gtd->lang);
  947. tintin_printf2(ses, "#INFO SYSTEM: OS = %s", gtd->os);
  948. tintin_printf2(ses, "#INFO SYSTEM: TERM = %s", gtd->term);
  949. tintin_printf2(ses, "#INFO SYSTEM: DETACH_PORT = %d", gtd->detach_port);
  950. tintin_printf2(ses, "#INFO SYSTEM: DETACH_FILE = %s", gtd->detach_port ? gtd->detach_file : "");
  951. tintin_printf2(ses, "#INFO SYSTEM: ATTACH_SOCK = %d", gtd->attach_sock);
  952. tintin_printf2(ses, "#INFO SYSTEM: ATTACH_FILE = %s", gtd->attach_sock ? gtd->attach_file : "");
  953. }
  954. }
  955. else
  956. {
  957. show_error(ses, LIST_COMMAND, "#INFO {%s} - NO MATCH FOUND.", arg1);
  958. }
  959. }
  960. }
  961. return ses;
  962. }