nest.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468
  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 2009 *
  23. ******************************************************************************/
  24. #include "tintin.h"
  25. struct listroot *search_nest_root(struct listroot *root, char *arg)
  26. {
  27. struct listnode *node = search_node_list(root, arg);
  28. return node ? node->root : NULL;
  29. }
  30. struct listroot *search_nest_base_ses(struct session *ses, char *arg)
  31. {
  32. struct listnode *node;
  33. struct listroot *root;
  34. int index;
  35. if (HAS_BIT(gtd->flags, TINTIN_FLAG_LOCAL))
  36. {
  37. for (index = gtd->script_index ; index >= 0 ; index--)
  38. {
  39. root = gtd->script_stack[index]->local;
  40. if (root->used)
  41. {
  42. node = search_node_list(root, arg);
  43. if (node)
  44. {
  45. return root;
  46. }
  47. }
  48. }
  49. }
  50. node = search_node_list(ses->list[LIST_VARIABLE], arg);
  51. if (node == NULL)
  52. {
  53. return NULL;
  54. }
  55. return ses->list[LIST_VARIABLE];
  56. }
  57. struct listnode *search_base_node(struct listroot *root, char *variable)
  58. {
  59. char name[BUFFER_SIZE];
  60. get_arg_to_brackets(root->ses, variable, name);
  61. return search_node_list(root, name);
  62. }
  63. struct listnode *search_nest_node_ses(struct session *ses, char *variable)
  64. {
  65. char name[BUFFER_SIZE], *arg;
  66. struct listroot *root;
  67. struct listnode *node;
  68. int index;
  69. if (HAS_BIT(gtd->flags, TINTIN_FLAG_LOCAL))
  70. {
  71. for (index = gtd->script_index ; index >= 0 ; index--)
  72. {
  73. root = gtd->script_stack[index]->local;
  74. if (root->used)
  75. {
  76. arg = get_arg_to_brackets(root->ses, variable, name);
  77. while (root && *arg)
  78. {
  79. root = search_nest_root(root, name);
  80. if (root)
  81. {
  82. arg = get_arg_in_brackets(root->ses, arg, name);
  83. }
  84. }
  85. if (root)
  86. {
  87. node = search_node_list(root, name);
  88. if (node)
  89. {
  90. return node;
  91. }
  92. }
  93. }
  94. }
  95. }
  96. root = ses->list[LIST_VARIABLE];
  97. arg = get_arg_to_brackets(ses, variable, name);
  98. while (root && *arg)
  99. {
  100. root = search_nest_root(root, name);
  101. if (root)
  102. {
  103. arg = get_arg_in_brackets(root->ses, arg, name);
  104. }
  105. }
  106. if (root)
  107. {
  108. return search_node_list(root, name);
  109. }
  110. return NULL;
  111. }
  112. struct listnode *search_nest_node(struct listroot *root, char *variable)
  113. {
  114. char name[BUFFER_SIZE], *arg;
  115. arg = get_arg_to_brackets(root->ses, variable, name);
  116. if (*name == 0)
  117. {
  118. arg = get_arg_in_brackets(root->ses, arg, name);
  119. }
  120. while (root && *arg)
  121. {
  122. root = search_nest_root(root, name);
  123. if (root)
  124. {
  125. arg = get_arg_in_brackets(root->ses, arg, name);
  126. }
  127. }
  128. if (root)
  129. {
  130. return search_node_list(root, name);
  131. }
  132. return NULL;
  133. }
  134. // same as search_nest_node, but store the path in *path
  135. struct listnode *search_nest_node_path(struct listroot *root, char *variable, char *path)
  136. {
  137. struct listnode *node;
  138. char name[BUFFER_SIZE], *arg;
  139. arg = get_arg_to_brackets(root->ses, variable, name);
  140. node = search_node_list(root, name);
  141. if (node)
  142. {
  143. path += sprintf(path, "%s", node->arg1);
  144. if (*arg == 0)
  145. {
  146. return node;
  147. }
  148. }
  149. root = node ? node->root : NULL;
  150. if (root)
  151. {
  152. arg = get_arg_in_brackets(root->ses, arg, name);
  153. }
  154. while (root && *arg)
  155. {
  156. node = search_node_list(root, name);
  157. if (node)
  158. {
  159. path += sprintf(path, "[%s]", node->arg1);
  160. }
  161. root = node ? node->root : NULL;
  162. if (root)
  163. {
  164. arg = get_arg_in_brackets(root->ses, arg, name);
  165. }
  166. }
  167. if (root)
  168. {
  169. node = search_node_list(root, name);
  170. if (node)
  171. {
  172. path += sprintf(path, "[%s]", node->arg1);
  173. }
  174. return node;
  175. }
  176. return NULL;
  177. }
  178. int search_nest_index(struct listroot *root, char *variable)
  179. {
  180. char name[BUFFER_SIZE], *arg;
  181. arg = get_arg_to_brackets(root->ses, variable, name);
  182. while (root && *arg)
  183. {
  184. root = search_nest_root(root, name);
  185. if (root)
  186. {
  187. arg = get_arg_in_brackets(root->ses, arg, name);
  188. }
  189. }
  190. if (root)
  191. {
  192. return search_index_list(root, name, NULL);
  193. }
  194. return -1;
  195. }
  196. struct listroot *update_nest_root(struct listroot *root, char *arg)
  197. {
  198. struct listnode *node;
  199. node = search_node_list(root, arg);
  200. if (node == NULL)
  201. {
  202. node = update_node_list(root, arg, "", "", "");
  203. }
  204. if (node->root == NULL)
  205. {
  206. node->root = init_list(root->ses, root->type, LIST_SIZE);
  207. }
  208. return node->root;
  209. }
  210. void update_nest_node(struct listroot *root, char *arg)
  211. {
  212. char *arg1, *arg2;
  213. arg1 = str_mim(arg);
  214. arg2 = str_mim(arg);
  215. while (*arg)
  216. {
  217. arg = get_arg_in_braces(root->ses, arg, arg1, GET_ONE);
  218. arg = get_arg_in_braces(root->ses, arg, arg2, GET_ONE);
  219. if (*arg2 == DEFAULT_OPEN)
  220. {
  221. update_nest_node(update_nest_root(root, arg1), arg2);
  222. }
  223. else //if (*arg1)
  224. {
  225. update_node_list(root, arg1, arg2, "", "");
  226. }
  227. if (*arg == COMMAND_SEPARATOR)
  228. {
  229. arg++;
  230. }
  231. }
  232. str_free(arg1);
  233. str_free(arg2);
  234. return;
  235. }
  236. int delete_nest_node(struct listroot *root, char *variable)
  237. {
  238. char name[BUFFER_SIZE], *arg;
  239. int index;
  240. arg = get_arg_to_brackets(root->ses, variable, name);
  241. while (root && *arg)
  242. {
  243. root = search_nest_root(root, name);
  244. if (root)
  245. {
  246. arg = get_arg_in_brackets(root->ses, arg, name);
  247. }
  248. }
  249. if (root)
  250. {
  251. index = search_index_list(root, name, NULL);
  252. if (index != -1)
  253. {
  254. delete_index_list(root, index);
  255. return TRUE;
  256. }
  257. }
  258. return FALSE;
  259. }
  260. int delete_nest_node_with_wild(struct listroot *root, char *variable)
  261. {
  262. char name[BUFFER_SIZE], *arg, *ptv;
  263. int index, found;
  264. arg = get_arg_to_brackets(root->ses, variable, name);
  265. ptv = arg;
  266. while (root && *arg)
  267. {
  268. root = search_nest_root(root, name);
  269. if (root)
  270. {
  271. arg = get_arg_in_brackets(root->ses, arg, name);
  272. if (*arg != 0)
  273. {
  274. ptv = arg;
  275. }
  276. }
  277. }
  278. found = FALSE;
  279. if (root)
  280. {
  281. for (index = root->used - 1 ; index >= 0 ; index--)
  282. {
  283. if (match(root->ses, root->list[index]->arg1, name, SUB_VAR|SUB_FUN))
  284. {
  285. if (*ptv)
  286. {
  287. show_message(root->ses, LIST_VARIABLE, "#OK. {%.*s[%s]} IS NO LONGER A VARIABLE.", ptv - variable, variable, root->list[index]->arg1);
  288. }
  289. else
  290. {
  291. show_message(root->ses, LIST_VARIABLE, "#OK. {%s} IS NO LONGER A VARIABLE.", root->list[index]->arg1);
  292. }
  293. delete_index_list(root, index);
  294. found = TRUE;
  295. }
  296. }
  297. }
  298. return found;
  299. }
  300. // Return the number of indices of a node.
  301. int get_nest_size(struct listroot *root, char *variable)
  302. {
  303. char name[BUFFER_SIZE], *arg;
  304. int index, count;
  305. arg = get_arg_to_brackets(root->ses, variable, name);
  306. if (!strcmp(arg, "[]"))
  307. {
  308. if (*name == 0)
  309. {
  310. return root->used + 1;
  311. }
  312. if (search_nest_root(root, name) == NULL)
  313. {
  314. if (search_node_list(root, name))
  315. {
  316. return 1;
  317. }
  318. }
  319. }
  320. if (*name == 0)
  321. {
  322. arg = get_arg_in_brackets(root->ses, arg, name);
  323. }
  324. while (root && *name)
  325. {
  326. // Handle regex queries
  327. if (search_nest_root(root, name) == NULL)
  328. {
  329. if (search_node_list(root, name) == NULL)
  330. {
  331. if (tintin_regexp_check(root->ses, name))
  332. {
  333. for (index = count = 0 ; index < root->used ; index++)
  334. {
  335. if (match(root->ses, root->list[index]->arg1, name, SUB_NONE))
  336. {
  337. count++;
  338. }
  339. }
  340. return count + 1;
  341. }
  342. else if (strstr(name, "..") && is_math(root->ses, name))
  343. {
  344. int min, max, range;
  345. if (root->used)
  346. {
  347. range = get_ellipsis(root->ses, root->used, name, &min, &max);
  348. return range + 1;
  349. }
  350. else
  351. {
  352. return 1;
  353. }
  354. }
  355. else
  356. {
  357. return 0;
  358. }
  359. }
  360. }
  361. root = search_nest_root(root, name);
  362. if (root)
  363. {
  364. if (!strcmp(arg, "[]"))
  365. {
  366. return root->used + 1;
  367. }
  368. arg = get_arg_in_brackets(root->ses, arg, name);
  369. }
  370. }
  371. return 0;
  372. }
  373. int get_nest_size_index(struct listroot *root, char *variable, char **result)
  374. {
  375. char name[BUFFER_SIZE], *arg;
  376. int index, count;
  377. arg = get_arg_to_brackets(root->ses, variable, name);
  378. str_cpy(result, "");
  379. if (!strcmp(arg, "[]"))
  380. {
  381. if (*name == 0)
  382. {
  383. return root->used + 1;
  384. }
  385. if (search_nest_root(root, name) == NULL)
  386. {
  387. if (search_node_list(root, name))
  388. {
  389. return 1;
  390. }
  391. }
  392. }
  393. if (*name == 0)
  394. {
  395. arg = get_arg_in_brackets(root->ses, arg, name);
  396. }
  397. while (root && *name)
  398. {
  399. // Handle regex queries
  400. if (search_nest_root(root, name) == NULL)
  401. {
  402. if (search_node_list(root, name) == NULL)
  403. {
  404. if (tintin_regexp_check(root->ses, name))
  405. {
  406. for (index = count = 0 ; index < root->used ; index++)
  407. {
  408. if (match(root->ses, root->list[index]->arg1, name, SUB_NONE))
  409. {
  410. count++;
  411. }
  412. }
  413. return count + 1;
  414. }
  415. else if (strstr(name, "..") && is_math(root->ses, name))
  416. {
  417. int min, max, range;
  418. if (root->used)
  419. {
  420. range = get_ellipsis(root->ses, root->used, name, &min, &max);
  421. return range + 1;
  422. }
  423. else
  424. {
  425. return 1;
  426. }
  427. }
  428. else
  429. {
  430. return 0;
  431. }
  432. }
  433. }
  434. root = search_nest_root(root, name);
  435. if (root)
  436. {
  437. if (!strcmp(arg, "[]"))
  438. {
  439. return root->used + 1;
  440. }
  441. arg = get_arg_in_brackets(root->ses, arg, name);
  442. }
  443. }
  444. return 0;
  445. }
  446. int get_nest_size_key(struct listroot *root, char *variable, char **result)
  447. {
  448. char name[BUFFER_SIZE], *arg;
  449. int index, count;
  450. arg = get_arg_to_brackets(root->ses, variable, name);
  451. str_cpy(result, "");
  452. if (!strcmp(arg, "[]"))
  453. {
  454. if (*name == 0)
  455. {
  456. for (index = 0 ; index < root->used ; index++)
  457. {
  458. str_cat_printf(result, "{%s}", root->list[index]->arg1);
  459. }
  460. return root->used + 1;
  461. }
  462. if (search_nest_root(root, name) == NULL)
  463. {
  464. if (search_node_list(root, name))
  465. {
  466. return 1;
  467. }
  468. }
  469. }
  470. if (*name == 0)
  471. {
  472. arg = get_arg_in_brackets(root->ses, arg, name);
  473. }
  474. while (root && *name)
  475. {
  476. // Handle regex queries
  477. if (search_nest_root(root, name) == NULL)
  478. {
  479. if (search_node_list(root, name) == NULL)
  480. {
  481. if (tintin_regexp_check(root->ses, name))
  482. {
  483. for (index = count = 0 ; index < root->used ; index++)
  484. {
  485. if (match(root->ses, root->list[index]->arg1, name, SUB_NONE))
  486. {
  487. str_cat_printf(result, "{%s}", root->list[index]->arg1);
  488. count++;
  489. }
  490. }
  491. return count + 1;
  492. }
  493. else if (strstr(name, "..") && is_math(root->ses, name))
  494. {
  495. int min, max, range;
  496. if (root->used)
  497. {
  498. range = get_ellipsis(root->ses, root->used, name, &min, &max);
  499. // tintin_printf2(root->ses, "debug: %d %d %d\n", min, max, range);
  500. if (min < max)
  501. {
  502. while (min <= max)
  503. {
  504. str_cat_printf(result, "{%s}", root->list[min++]->arg1);
  505. }
  506. }
  507. else
  508. {
  509. while (min >= max)
  510. {
  511. str_cat_printf(result, "{%s}", root->list[min--]->arg1);
  512. }
  513. }
  514. return range + 1;
  515. }
  516. else
  517. {
  518. return 1;
  519. }
  520. }
  521. else
  522. {
  523. return 0;
  524. }
  525. }
  526. }
  527. root = search_nest_root(root, name);
  528. if (root)
  529. {
  530. if (!strcmp(arg, "[]"))
  531. {
  532. for (index = 0 ; index < root->used ; index++)
  533. {
  534. str_cat_printf(result, "{%s}", root->list[index]->arg1);
  535. }
  536. return root->used + 1;
  537. }
  538. arg = get_arg_in_brackets(root->ses, arg, name);
  539. }
  540. }
  541. return 0;
  542. }
  543. int get_nest_size_val(struct listroot *root, char *variable, char **result)
  544. {
  545. char name[BUFFER_SIZE], *arg;
  546. int index, count;
  547. arg = get_arg_to_brackets(root->ses, variable, name);
  548. str_cpy(result, "");
  549. if (!strcmp(arg, "[]"))
  550. {
  551. if (*name == 0)
  552. {
  553. for (index = 0 ; index < root->used ; index++)
  554. {
  555. show_nest_node(root->list[index], result, FALSE); // behaves like strcat
  556. // str_cat_printf(result, "{%s}", root->list[index]->arg2);
  557. }
  558. return root->used + 1;
  559. }
  560. if (search_nest_root(root, name) == NULL)
  561. {
  562. if (search_node_list(root, name))
  563. {
  564. return 1;
  565. }
  566. }
  567. }
  568. if (*name == 0)
  569. {
  570. arg = get_arg_in_brackets(root->ses, arg, name);
  571. }
  572. while (root && *name)
  573. {
  574. // Handle regex queries
  575. if (search_nest_root(root, name) == NULL)
  576. {
  577. if (search_node_list(root, name) == NULL)
  578. {
  579. if (tintin_regexp_check(root->ses, name))
  580. {
  581. for (index = count = 0 ; index < root->used ; index++)
  582. {
  583. if (match(root->ses, root->list[index]->arg1, name, SUB_NONE))
  584. {
  585. show_nest_node(root->list[index], result, FALSE); // behaves like strcat
  586. count++;
  587. }
  588. }
  589. return count + 1;
  590. }
  591. else if (strstr(name, "..") && is_math(root->ses, name))
  592. {
  593. int min, max, range;
  594. if (root->used)
  595. {
  596. range = get_ellipsis(root->ses, root->used, name, &min, &max);
  597. if (min < max)
  598. {
  599. while (min <= max)
  600. {
  601. show_nest_node(root->list[min++], result, FALSE);
  602. }
  603. }
  604. else
  605. {
  606. while (min >= max)
  607. {
  608. show_nest_node(root->list[min--], result, FALSE);
  609. }
  610. }
  611. return range + 1;
  612. }
  613. else
  614. {
  615. return 1;
  616. }
  617. }
  618. else
  619. {
  620. return 0;
  621. }
  622. }
  623. }
  624. root = search_nest_root(root, name);
  625. if (root)
  626. {
  627. if (!strcmp(arg, "[]"))
  628. {
  629. for (index = 0 ; index < root->used ; index++)
  630. {
  631. show_nest_node(root->list[index], result, FALSE); // behaves like strcat
  632. // str_cat_printf(result, "{%s}", root->list[index]->arg2);
  633. }
  634. return root->used + 1;
  635. }
  636. arg = get_arg_in_brackets(root->ses, arg, name);
  637. }
  638. }
  639. return 0;
  640. }
  641. struct listnode *get_nest_node_key(struct listroot *root, char *variable, char **result, int def)
  642. {
  643. struct listnode *node;
  644. int size;
  645. size = get_nest_size_key(root, variable, result); // will copy keys to result
  646. if (size)
  647. {
  648. return NULL;
  649. }
  650. node = search_nest_node(root, variable);
  651. if (node)
  652. {
  653. str_cpy_printf(result, "%s", node->arg1);
  654. if (node->shots && --node->shots == 0)
  655. {
  656. delete_nest_node(root, variable);
  657. }
  658. return node;
  659. }
  660. node = search_base_node(root, variable);
  661. if (node || def)
  662. {
  663. str_cpy(result, "");
  664. }
  665. else
  666. {
  667. str_cpy_printf(result, "*%s", variable);
  668. }
  669. return NULL;
  670. }
  671. struct listnode *get_nest_node_val(struct listroot *root, char *variable, char **result, int def)
  672. {
  673. struct listnode *node;
  674. int size;
  675. size = get_nest_size_val(root, variable, result);
  676. if (size)
  677. {
  678. return NULL;
  679. }
  680. node = search_nest_node(root, variable);
  681. if (node)
  682. {
  683. show_nest_node(node, result, TRUE);
  684. if (node->shots && --node->shots == 0)
  685. {
  686. delete_nest_node(root, variable);
  687. }
  688. return node;
  689. }
  690. node = search_base_node(root, variable);
  691. if (node || def)
  692. {
  693. str_cpy(result, "");
  694. }
  695. else
  696. {
  697. str_cpy_printf(result, "$%s", variable);
  698. }
  699. return NULL;
  700. }
  701. int get_nest_index(struct listroot *root, char *variable, char **result, int def)
  702. {
  703. struct listnode *node;
  704. int index, size;
  705. size = get_nest_size_index(root, variable, result);
  706. if (size)
  707. {
  708. str_cpy_printf(result, "%d", size - 1);
  709. return -1;
  710. }
  711. node = search_nest_node(root, variable);
  712. index = search_nest_index(root, variable);
  713. if (node && index >= 0)
  714. {
  715. str_cpy_printf(result, "%d", index + 1);
  716. if (node->shots && --node->shots == 0)
  717. {
  718. delete_index_list(root, index);
  719. }
  720. return index;
  721. }
  722. node = search_base_node(root, variable);
  723. if (node || def)
  724. {
  725. str_cpy(result, "0");
  726. }
  727. else
  728. {
  729. str_cpy_printf(result, "&%s", variable);
  730. }
  731. return -1;
  732. }
  733. // cats to result when initialize is 0
  734. void show_nest_node(struct listnode *node, char **str_result, int initialize)
  735. {
  736. if (initialize)
  737. {
  738. str_cpy(str_result, "");
  739. }
  740. if (node->root == NULL)
  741. {
  742. if (initialize)
  743. {
  744. str_cat(str_result, node->arg2);
  745. }
  746. else
  747. {
  748. str_cat_printf(str_result, "{%s}", node->arg2);
  749. }
  750. }
  751. else
  752. {
  753. struct listroot *root = node->root;
  754. int i;
  755. if (!initialize)
  756. {
  757. str_cat(str_result, "{");
  758. }
  759. for (i = 0 ; i < root->used ; i++)
  760. {
  761. str_cat_printf(str_result, "{%s}", root->list[i]->arg1);
  762. show_nest_node(root->list[i], str_result, FALSE);
  763. }
  764. if (!initialize)
  765. {
  766. str_cat(str_result, "}");
  767. }
  768. }
  769. }
  770. void view_nest_node(struct listnode *node, char **str_result, int nest, int initialize, int color)
  771. {
  772. if (initialize == TRUE)
  773. {
  774. str_cpy(str_result, "");
  775. }
  776. if (node->root == NULL)
  777. {
  778. if (initialize)
  779. {
  780. str_cat(str_result, node->arg2);
  781. }
  782. else
  783. {
  784. if (color)
  785. {
  786. str_cat_printf(str_result, COLOR_BRACE "{" COLOR_STRING "%s" COLOR_BRACE "}\n", node->arg2);
  787. }
  788. else
  789. {
  790. str_cat_printf(str_result, "{%s}\n", node->arg2);
  791. }
  792. }
  793. }
  794. else
  795. {
  796. struct listroot *root = node->root;
  797. int i;
  798. if (initialize == FALSE)
  799. {
  800. if (color)
  801. {
  802. str_cat_printf(str_result, "\n" COLOR_BRACE "%s{\n", indent(nest));
  803. }
  804. else
  805. {
  806. str_cat_printf(str_result, "\n%s{\n", indent(nest));
  807. }
  808. }
  809. nest++;
  810. for (i = 0 ; i < root->used ; i++)
  811. {
  812. if (color)
  813. {
  814. str_cat_printf(str_result, COLOR_BRACE "%s{" COLOR_STRING "%s" COLOR_BRACE "} ", indent(nest), root->list[i]->arg1);
  815. }
  816. else
  817. {
  818. str_cat_printf(str_result, "%s{%s} ", indent(nest), root->list[i]->arg1);
  819. }
  820. view_nest_node(root->list[i], str_result, nest, FALSE, color);
  821. }
  822. nest--;
  823. if (initialize == FALSE)
  824. {
  825. if (color)
  826. {
  827. str_cat_printf(str_result, COLOR_BRACE "%s}\n", indent(nest), "");
  828. }
  829. else
  830. {
  831. str_cat_printf(str_result, "%s}\n", indent(nest), "");
  832. }
  833. }
  834. }
  835. }
  836. void view_nest_node_json(struct listnode *node, char **str_result, int nest, int initialize)
  837. {
  838. if (initialize == TRUE)
  839. {
  840. str_cpy(str_result, "");
  841. }
  842. if (node->root == NULL)
  843. {
  844. str_cat_printf(str_result, "\"%s\"", node->arg2);
  845. }
  846. else
  847. {
  848. struct listroot *root = node->root;
  849. int i;
  850. if (initialize)
  851. {
  852. str_cat_printf(str_result, "%s{\n", indent(nest));
  853. }
  854. else
  855. {
  856. str_cat_printf(str_result, "\n%s{\n", indent(nest));
  857. }
  858. nest++;
  859. for (i = 0 ; i < root->used ; i++)
  860. {
  861. if (i)
  862. {
  863. str_cat_printf(str_result, ",\n");
  864. }
  865. str_cat_printf(str_result, "%s\"%s\" : ", indent(nest), root->list[i]->arg1);
  866. view_nest_node_json(root->list[i], str_result, nest, FALSE);
  867. }
  868. nest--;
  869. if (initialize)
  870. {
  871. str_cat_printf(str_result, "\n%s}\n", indent(nest), "");
  872. }
  873. else
  874. {
  875. str_cat_printf(str_result, "\n%s}", indent(nest), "");
  876. }
  877. }
  878. }
  879. // Adds gtd->level->local support
  880. struct listnode *set_nest_node_ses(struct session *ses, char *arg1, char *format, ...)
  881. {
  882. struct listnode *node;
  883. struct listroot *root;
  884. char *arg, *arg2, name[BUFFER_SIZE];
  885. va_list args;
  886. push_call("set_nest_node_ses(%p,%s,%p,...)",ses,arg1,format);
  887. va_start(args, format);
  888. if (vasprintf(&arg2, format, args) == -1)
  889. {
  890. syserr_printf(ses, "set_nest_node_ses: vasprintf");
  891. }
  892. va_end(args);
  893. arg = get_arg_to_brackets(ses, arg1, name);
  894. if (HAS_BIT(ses->event_flags, EVENT_FLAG_VARIABLE))
  895. {
  896. check_all_events(ses, EVENT_FLAG_VARIABLE, 1, 3, "VARIABLE UPDATE %s", name, name, arg2, arg1);
  897. }
  898. root = search_nest_base_ses(ses, name);
  899. if (root == NULL)
  900. {
  901. if (gtd->level->local)
  902. {
  903. root = local_list(ses);
  904. }
  905. else
  906. {
  907. root = ses->list[LIST_VARIABLE];
  908. }
  909. }
  910. while (*arg)
  911. {
  912. root = update_nest_root(root, name);
  913. if (root)
  914. {
  915. arg = get_arg_in_brackets(root->ses, arg, name);
  916. }
  917. }
  918. node = search_node_list(root, name);
  919. if (node && node->root)
  920. {
  921. free_list(node->root);
  922. node->root = NULL;
  923. }
  924. if (*space_out(arg2) == DEFAULT_OPEN)
  925. {
  926. update_nest_node(update_nest_root(root, name), arg2);
  927. node = search_node_list(root, name);
  928. }
  929. else if (node)
  930. {
  931. str_cpy(&node->arg2, arg2);
  932. }
  933. else
  934. {
  935. node = update_node_list(root, name, arg2, "", "");
  936. }
  937. if (gtd->level->shots)
  938. {
  939. node->shots = gtd->level->mshot;
  940. }
  941. if (HAS_BIT(root->ses->event_flags, EVENT_FLAG_VARIABLE))
  942. {
  943. arg = get_arg_to_brackets(root->ses, arg1, name);
  944. check_all_events(root->ses, EVENT_FLAG_VARIABLE, 1, 3, "VARIABLE UPDATED %s", name, name, arg2, arg1);
  945. if (strcmp(arg1, name))
  946. {
  947. check_all_events(root->ses, EVENT_FLAG_VARIABLE, 1, 3, "VARIABLE UPDATED %s", arg1, name, arg2, arg1);
  948. }
  949. }
  950. free(arg2);
  951. pop_call();
  952. return node;
  953. }
  954. // like set, but we're adding here.
  955. struct listnode *add_nest_node_ses(struct session *ses, char *arg1, char *format, ...)
  956. {
  957. struct listnode *node;
  958. struct listroot *root;
  959. char *arg, *arg2, *name;
  960. va_list args;
  961. push_call("add_nest_node_ses(%p,%s,%p,...)",ses,arg1,format);
  962. name = str_alloc_stack(0);
  963. va_start(args, format);
  964. if (vasprintf(&arg2, format, args) == -1)
  965. {
  966. syserr_printf(ses, "add_nest_node_ses: vasprintf");
  967. }
  968. va_end(args);
  969. arg = get_arg_to_brackets(ses, arg1, name);
  970. if (HAS_BIT(ses->event_flags, EVENT_FLAG_VARIABLE))
  971. {
  972. check_all_events(ses, EVENT_FLAG_VARIABLE, 1, 3, "VARIABLE UPDATE %s", name, name, arg2, arg1);
  973. }
  974. root = search_nest_base_ses(ses, name);
  975. if (root == NULL)
  976. {
  977. root = ses->list[LIST_VARIABLE];
  978. }
  979. while (*arg)
  980. {
  981. root = update_nest_root(root, name);
  982. if (root)
  983. {
  984. arg = get_arg_in_brackets(root->ses, arg, name);
  985. }
  986. }
  987. node = search_node_list(root, name);
  988. /*
  989. if (node && node->root)
  990. {
  991. free_list(node->root);
  992. node->root = NULL;
  993. }
  994. */
  995. if (*space_out(arg2) == DEFAULT_OPEN)
  996. {
  997. update_nest_node(update_nest_root(root, name), arg2);
  998. node = search_node_list(root, name);
  999. }
  1000. else if (node)
  1001. {
  1002. str_cat(&node->arg2, arg2);
  1003. }
  1004. else
  1005. {
  1006. node = update_node_list(root, name, arg2, "", "");
  1007. }
  1008. if (gtd->level->shots)
  1009. {
  1010. node->shots = gtd->level->mshot;
  1011. }
  1012. if (HAS_BIT(root->ses->event_flags, EVENT_FLAG_VARIABLE))
  1013. {
  1014. arg = get_arg_to_brackets(root->ses, arg1, name);
  1015. check_all_events(root->ses, EVENT_FLAG_VARIABLE, 1, 3, "VARIABLE UPDATED %s", name, name, arg2, arg1);
  1016. if (strcmp(arg1, name))
  1017. {
  1018. check_all_events(root->ses, EVENT_FLAG_VARIABLE, 1, 3, "VARIABLE UPDATED %s", arg1, name, arg2, arg1);
  1019. }
  1020. }
  1021. free(arg2);
  1022. pop_call();
  1023. return node;
  1024. }
  1025. struct listnode *set_nest_node(struct listroot *root, char *arg1, char *format, ...)
  1026. {
  1027. struct listroot *base;
  1028. struct listnode *node;
  1029. char *arg, *arg2, name[BUFFER_SIZE];
  1030. va_list args;
  1031. push_call("set_nest_node(%p,%s,%p,...)",root,arg1,format);
  1032. va_start(args, format);
  1033. if (vasprintf(&arg2, format, args) == -1)
  1034. {
  1035. syserr_printf(root->ses, "set_nest_node: vasprintf");
  1036. }
  1037. va_end(args);
  1038. arg = get_arg_to_brackets(root->ses, arg1, name);
  1039. if (HAS_BIT(root->ses->event_flags, EVENT_FLAG_VARIABLE))
  1040. {
  1041. check_all_events(root->ses, EVENT_FLAG_VARIABLE, 1, 3, "VARIABLE UPDATE %s", name, name, arg2, arg1);
  1042. }
  1043. if (HAS_BIT(gtd->flags, TINTIN_FLAG_LOCAL))
  1044. {
  1045. base = search_nest_base_ses(root->ses, name);
  1046. if (base)
  1047. {
  1048. root = base;
  1049. }
  1050. }
  1051. while (*arg)
  1052. {
  1053. root = update_nest_root(root, name);
  1054. if (root)
  1055. {
  1056. arg = get_arg_in_brackets(root->ses, arg, name);
  1057. }
  1058. }
  1059. node = search_node_list(root, name);
  1060. if (node && node->root)
  1061. {
  1062. free_list(node->root);
  1063. node->root = NULL;
  1064. }
  1065. if (*space_out(arg2) == DEFAULT_OPEN)
  1066. {
  1067. update_nest_node(update_nest_root(root, name), arg2);
  1068. node = search_node_list(root, name);
  1069. }
  1070. else if (node)
  1071. {
  1072. str_cpy(&node->arg2, arg2);
  1073. }
  1074. else
  1075. {
  1076. if (*name == '-' || *name == '+')
  1077. {
  1078. get_number_string(root->ses, name, name);
  1079. // printf("debug: set_nest_node - or +\n");
  1080. }
  1081. node = update_node_list(root, name, arg2, "", "");
  1082. }
  1083. if (gtd->level->shots)
  1084. {
  1085. node->shots = gtd->level->mshot;
  1086. }
  1087. if (HAS_BIT(root->ses->event_flags, EVENT_FLAG_VARIABLE))
  1088. {
  1089. arg = get_arg_to_brackets(root->ses, arg1, name);
  1090. check_all_events(root->ses, EVENT_FLAG_VARIABLE, 1, 3, "VARIABLE UPDATED %s", name, name, arg2, arg1);
  1091. if (strcmp(arg1, name))
  1092. {
  1093. check_all_events(root->ses, EVENT_FLAG_VARIABLE, 1, 3, "VARIABLE UPDATED %s", arg1, name, arg2, arg1);
  1094. }
  1095. }
  1096. free(arg2);
  1097. pop_call();
  1098. return node;
  1099. }
  1100. // Like set, but don't erase old data.
  1101. struct listnode *add_nest_node(struct listroot *root, char *arg1, char *format, ...)
  1102. {
  1103. struct listroot *base;
  1104. struct listnode *node;
  1105. char *arg, *arg2, name[BUFFER_SIZE];
  1106. va_list args;
  1107. push_call("add_nest_node(%p,%s,%p,...)",root,arg1,format);
  1108. va_start(args, format);
  1109. if (vasprintf(&arg2, format, args) == -1)
  1110. {
  1111. syserr_printf(root->ses, "add_nest_node: vasprintf");
  1112. }
  1113. va_end(args);
  1114. arg = get_arg_to_brackets(root->ses, arg1, name);
  1115. if (HAS_BIT(root->ses->event_flags, EVENT_FLAG_VARIABLE))
  1116. {
  1117. check_all_events(root->ses, EVENT_FLAG_VARIABLE, 1, 3, "VARIABLE UPDATE %s", name, name, arg2, arg1);
  1118. }
  1119. if (HAS_BIT(gtd->flags, TINTIN_FLAG_LOCAL))
  1120. {
  1121. base = search_nest_base_ses(root->ses, name);
  1122. if (base)
  1123. {
  1124. root = base;
  1125. }
  1126. }
  1127. while (*arg)
  1128. {
  1129. root = update_nest_root(root, name);
  1130. if (root)
  1131. {
  1132. arg = get_arg_in_brackets(root->ses, arg, name);
  1133. }
  1134. }
  1135. node = search_node_list(root, name);
  1136. /*
  1137. if (node && node->root)
  1138. {
  1139. free_list(node->root);
  1140. node->root = NULL;
  1141. }
  1142. */
  1143. if (*space_out(arg2) == DEFAULT_OPEN)
  1144. {
  1145. root = update_nest_root(root, name);
  1146. update_nest_node(root, arg2);
  1147. node = search_node_list(root, name);
  1148. }
  1149. else if (node)
  1150. {
  1151. str_cat(&node->arg2, arg2);
  1152. }
  1153. else
  1154. {
  1155. node = update_node_list(root, name, arg2, "", "");
  1156. }
  1157. if (gtd->level->shots)
  1158. {
  1159. node->shots = gtd->level->mshot;
  1160. }
  1161. if (HAS_BIT(root->ses->event_flags, EVENT_FLAG_VARIABLE))
  1162. {
  1163. arg = get_arg_to_brackets(root->ses, arg1, name);
  1164. check_all_events(root->ses, EVENT_FLAG_VARIABLE, 1, 3, "VARIABLE UPDATED %s", name, name, arg2, arg1);
  1165. if (strcmp(arg1, name))
  1166. {
  1167. check_all_events(root->ses, EVENT_FLAG_VARIABLE, 1, 3, "VARIABLE UPDATED %s", arg1, name, arg2, arg1);
  1168. }
  1169. }
  1170. free(arg2);
  1171. pop_call();
  1172. return node;
  1173. }
  1174. void copy_nest_node(struct listroot *dst_root, struct listnode *dst, struct listnode *src)
  1175. {
  1176. int index;
  1177. if (src->root == NULL)
  1178. {
  1179. return;
  1180. }
  1181. dst_root = dst->root = init_list(dst_root->ses, dst_root->type, src->root->size);
  1182. for (index = 0 ; index < src->root->used ; index++)
  1183. {
  1184. dst = create_node_list(dst_root, src->root->list[index]->arg1, src->root->list[index]->arg2, src->root->list[index]->arg3, src->root->list[index]->arg4);
  1185. if (src->root->list[index]->root)
  1186. {
  1187. copy_nest_node(dst_root, dst, src->root->list[index]);
  1188. }
  1189. }
  1190. }