nest.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197
  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;
  28. node = search_node_list(root, arg);
  29. if (node == NULL || node->root == NULL)
  30. {
  31. return NULL;
  32. }
  33. return node->root;
  34. }
  35. struct listroot *search_nest_base_ses(struct session *ses, char *arg)
  36. {
  37. struct listnode *node;
  38. struct listroot *root;
  39. int index;
  40. if (HAS_BIT(gtd->flags, TINTIN_FLAG_LOCAL))
  41. {
  42. for (index = gtd->script_index ; index ; index--)
  43. {
  44. root = gtd->script_stack[index]->local;
  45. if (root->used)
  46. {
  47. node = search_node_list(root, arg);
  48. if (node)
  49. {
  50. return root;
  51. }
  52. }
  53. }
  54. }
  55. node = search_node_list(ses->list[LIST_VARIABLE], arg);
  56. if (node == NULL)
  57. {
  58. return NULL;
  59. }
  60. return ses->list[LIST_VARIABLE];
  61. }
  62. struct listnode *search_base_node(struct listroot *root, char *variable)
  63. {
  64. char name[BUFFER_SIZE];
  65. get_arg_to_brackets(root->ses, variable, name);
  66. return search_node_list(root, name);
  67. }
  68. struct listnode *search_nest_node_ses(struct session *ses, char *variable)
  69. {
  70. char name[BUFFER_SIZE], *arg;
  71. struct listroot *root;
  72. struct listnode *node;
  73. int index;
  74. if (HAS_BIT(gtd->flags, TINTIN_FLAG_LOCAL))
  75. {
  76. for (index = gtd->script_index ; index ; index--)
  77. {
  78. root = gtd->script_stack[index]->local;
  79. if (root->used)
  80. {
  81. arg = get_arg_to_brackets(root->ses, variable, name);
  82. while (root && *arg)
  83. {
  84. root = search_nest_root(root, name);
  85. if (root)
  86. {
  87. arg = get_arg_in_brackets(root->ses, arg, name);
  88. }
  89. }
  90. if (root)
  91. {
  92. node = search_node_list(root, name);
  93. if (node)
  94. {
  95. return node;
  96. }
  97. }
  98. }
  99. }
  100. }
  101. root = ses->list[LIST_VARIABLE];
  102. arg = get_arg_to_brackets(ses, variable, name);
  103. while (root && *arg)
  104. {
  105. root = search_nest_root(root, name);
  106. if (root)
  107. {
  108. arg = get_arg_in_brackets(root->ses, arg, name);
  109. }
  110. }
  111. if (root)
  112. {
  113. return search_node_list(root, name);
  114. }
  115. return NULL;
  116. }
  117. struct listnode *search_nest_node(struct listroot *root, char *variable)
  118. {
  119. char name[BUFFER_SIZE], *arg;
  120. arg = get_arg_to_brackets(root->ses, variable, name);
  121. while (root && *arg)
  122. {
  123. root = search_nest_root(root, name);
  124. if (root)
  125. {
  126. arg = get_arg_in_brackets(root->ses, arg, name);
  127. }
  128. }
  129. if (root)
  130. {
  131. return search_node_list(root, name);
  132. }
  133. return NULL;
  134. }
  135. int search_nest_index(struct listroot *root, char *variable)
  136. {
  137. char name[BUFFER_SIZE], *arg;
  138. arg = get_arg_to_brackets(root->ses, variable, name);
  139. while (root && *arg)
  140. {
  141. root = search_nest_root(root, name);
  142. if (root)
  143. {
  144. arg = get_arg_in_brackets(root->ses, arg, name);
  145. }
  146. }
  147. if (root)
  148. {
  149. return search_index_list(root, name, NULL);
  150. }
  151. return -1;
  152. }
  153. struct listroot *update_nest_root(struct listroot *root, char *arg)
  154. {
  155. struct listnode *node;
  156. node = search_node_list(root, arg);
  157. if (node == NULL)
  158. {
  159. node = update_node_list(root, arg, "", "", "");
  160. }
  161. if (node->root == NULL)
  162. {
  163. node->root = init_list(root->ses, root->type, LIST_SIZE);
  164. }
  165. return node->root;
  166. }
  167. void update_nest_node(struct listroot *root, char *arg)
  168. {
  169. // char arg1[BUFFER_SIZE], arg2[BUFFER_SIZE];
  170. char *arg1, *arg2;
  171. arg1 = str_mim(arg);
  172. arg2 = str_mim(arg);
  173. while (*arg)
  174. {
  175. arg = get_arg_in_braces(root->ses, arg, arg1, GET_ONE);
  176. arg = get_arg_in_braces(root->ses, arg, arg2, GET_ONE);
  177. if (*arg2 == DEFAULT_OPEN)
  178. {
  179. update_nest_node(update_nest_root(root, arg1), arg2);
  180. }
  181. else if (*arg1)
  182. {
  183. update_node_list(root, arg1, arg2, "", "");
  184. }
  185. if (*arg == COMMAND_SEPARATOR)
  186. {
  187. arg++;
  188. }
  189. }
  190. str_free(arg1);
  191. str_free(arg2);
  192. return;
  193. }
  194. int delete_nest_node(struct listroot *root, char *variable)
  195. {
  196. char name[BUFFER_SIZE], *arg;
  197. int index;
  198. arg = get_arg_to_brackets(root->ses, variable, name);
  199. while (root && *arg)
  200. {
  201. root = search_nest_root(root, name);
  202. if (root)
  203. {
  204. arg = get_arg_in_brackets(root->ses, arg, name);
  205. }
  206. }
  207. if (root)
  208. {
  209. index = search_index_list(root, name, NULL);
  210. if (index != -1)
  211. {
  212. delete_index_list(root, index);
  213. return TRUE;
  214. }
  215. }
  216. return FALSE;
  217. }
  218. // Return the number of indices of a node.
  219. int get_nest_size(struct listroot *root, char *variable)
  220. {
  221. char name[BUFFER_SIZE], *arg;
  222. int index, count;
  223. arg = get_arg_to_brackets(root->ses, variable, name);
  224. if (!strcmp(arg, "[]"))
  225. {
  226. if (*name == 0)
  227. {
  228. return root->used + 1;
  229. }
  230. if (search_nest_root(root, name) == NULL)
  231. {
  232. if (search_node_list(root, name))
  233. {
  234. return 1;
  235. }
  236. }
  237. }
  238. while (root && *name)
  239. {
  240. // Handle regex queries
  241. if (search_nest_root(root, name) == NULL)
  242. {
  243. if (search_node_list(root, name) == NULL)
  244. {
  245. if (tintin_regexp_check(root->ses, name))
  246. {
  247. for (index = count = 0 ; index < root->used ; index++)
  248. {
  249. if (match(root->ses, root->list[index]->arg1, name, SUB_NONE))
  250. {
  251. count++;
  252. }
  253. }
  254. return count + 1;
  255. }
  256. else if (strstr(name, "..") && is_math(root->ses, name))
  257. {
  258. int min, max, range;
  259. if (root->used)
  260. {
  261. range = get_ellipsis(root, name, &min, &max);
  262. return range + 1;
  263. }
  264. else
  265. {
  266. return 1;
  267. }
  268. }
  269. else
  270. {
  271. return 0;
  272. }
  273. }
  274. }
  275. root = search_nest_root(root, name);
  276. if (root)
  277. {
  278. if (!strcmp(arg, "[]"))
  279. {
  280. return root->used + 1;
  281. }
  282. arg = get_arg_in_brackets(root->ses, arg, name);
  283. }
  284. }
  285. return 0;
  286. }
  287. int get_nest_size_index(struct listroot *root, char *variable, char **result)
  288. {
  289. char name[BUFFER_SIZE], *arg;
  290. int index, count;
  291. arg = get_arg_to_brackets(root->ses, variable, name);
  292. str_cpy(result, "");
  293. if (!strcmp(arg, "[]"))
  294. {
  295. if (*name == 0)
  296. {
  297. return root->used + 1;
  298. }
  299. if (search_nest_root(root, name) == NULL)
  300. {
  301. if (search_node_list(root, name))
  302. {
  303. return 1;
  304. }
  305. }
  306. }
  307. while (root && *name)
  308. {
  309. // Handle regex queries
  310. if (search_nest_root(root, name) == NULL)
  311. {
  312. if (search_node_list(root, name) == NULL)
  313. {
  314. if (tintin_regexp_check(root->ses, name))
  315. {
  316. for (index = count = 0 ; index < root->used ; index++)
  317. {
  318. if (match(root->ses, root->list[index]->arg1, name, SUB_NONE))
  319. {
  320. count++;
  321. }
  322. }
  323. return count + 1;
  324. }
  325. else if (strstr(name, "..") && is_math(root->ses, name))
  326. {
  327. int min, max, range;
  328. if (root->used)
  329. {
  330. range = get_ellipsis(root, name, &min, &max);
  331. return range + 1;
  332. }
  333. else
  334. {
  335. return 1;
  336. }
  337. }
  338. else
  339. {
  340. return 0;
  341. }
  342. }
  343. }
  344. root = search_nest_root(root, name);
  345. if (root)
  346. {
  347. if (!strcmp(arg, "[]"))
  348. {
  349. return root->used + 1;
  350. }
  351. arg = get_arg_in_brackets(root->ses, arg, name);
  352. }
  353. }
  354. return 0;
  355. }
  356. int get_nest_size_key(struct listroot *root, char *variable, char **result)
  357. {
  358. char name[BUFFER_SIZE], *arg;
  359. int index, count;
  360. arg = get_arg_to_brackets(root->ses, variable, name);
  361. str_cpy(result, "");
  362. if (!strcmp(arg, "[]"))
  363. {
  364. if (*name == 0)
  365. {
  366. for (index = 0 ; index < root->used ; index++)
  367. {
  368. str_cat_printf(result, "{%s}", root->list[index]->arg1);
  369. }
  370. return root->used + 1;
  371. }
  372. if (search_nest_root(root, name) == NULL)
  373. {
  374. if (search_node_list(root, name))
  375. {
  376. return 1;
  377. }
  378. }
  379. }
  380. while (root && *name)
  381. {
  382. // Handle regex queries
  383. if (search_nest_root(root, name) == NULL)
  384. {
  385. if (search_node_list(root, name) == NULL)
  386. {
  387. if (tintin_regexp_check(root->ses, name))
  388. {
  389. for (index = count = 0 ; index < root->used ; index++)
  390. {
  391. if (match(root->ses, root->list[index]->arg1, name, SUB_NONE))
  392. {
  393. str_cat_printf(result, "{%s}", root->list[index]->arg1);
  394. count++;
  395. }
  396. }
  397. return count + 1;
  398. }
  399. else if (strstr(name, "..") && is_math(root->ses, name))
  400. {
  401. int min, max, range;
  402. if (root->used)
  403. {
  404. range = get_ellipsis(root, name, &min, &max);
  405. if (min < max)
  406. {
  407. while (min <= max)
  408. {
  409. str_cat_printf(result, "{%s}", root->list[min++]->arg1);
  410. }
  411. }
  412. else
  413. {
  414. while (min >= max)
  415. {
  416. str_cat_printf(result, "{%s}", root->list[min--]->arg1);
  417. }
  418. }
  419. return range + 1;
  420. }
  421. else
  422. {
  423. return 1;
  424. }
  425. }
  426. else
  427. {
  428. return 0;
  429. }
  430. }
  431. }
  432. root = search_nest_root(root, name);
  433. if (root)
  434. {
  435. if (!strcmp(arg, "[]"))
  436. {
  437. for (index = 0 ; index < root->used ; index++)
  438. {
  439. str_cat_printf(result, "{%s}", root->list[index]->arg1);
  440. }
  441. return root->used + 1;
  442. }
  443. arg = get_arg_in_brackets(root->ses, arg, name);
  444. }
  445. }
  446. return 0;
  447. }
  448. int get_nest_size_val(struct listroot *root, char *variable, char **result)
  449. {
  450. char name[BUFFER_SIZE], *arg;
  451. int index, count;
  452. static int warning;
  453. arg = get_arg_to_brackets(root->ses, variable, name);
  454. str_cpy(result, "");
  455. if (!strcmp(arg, "[]"))
  456. {
  457. if (*name == 0)
  458. {
  459. for (index = 0 ; index < root->used ; index++)
  460. {
  461. str_cat_printf(result, "{%s}", root->list[index]->arg1);
  462. }
  463. return root->used + 1;
  464. }
  465. if (search_nest_root(root, name) == NULL)
  466. {
  467. if (search_node_list(root, name))
  468. {
  469. return 1;
  470. }
  471. }
  472. }
  473. while (root && *name)
  474. {
  475. // Handle regex queries
  476. if (search_nest_root(root, name) == NULL)
  477. {
  478. if (search_node_list(root, name) == NULL)
  479. {
  480. if (tintin_regexp_check(root->ses, name))
  481. {
  482. for (index = count = 0 ; index < root->used ; index++)
  483. {
  484. if (match(root->ses, root->list[index]->arg1, name, SUB_NONE))
  485. {
  486. show_nest_node(root->list[index], result, FALSE); // behaves like strcat
  487. count++;
  488. }
  489. }
  490. return count + 1;
  491. }
  492. else if (strstr(name, "..") && is_math(root->ses, name))
  493. {
  494. int min, max, range;
  495. if (root->used)
  496. {
  497. range = get_ellipsis(root, name, &min, &max);
  498. if (min < max)
  499. {
  500. while (min <= max)
  501. {
  502. show_nest_node(root->list[min++], result, FALSE);
  503. }
  504. }
  505. else
  506. {
  507. while (min >= max)
  508. {
  509. show_nest_node(root->list[min--], result, FALSE);
  510. }
  511. }
  512. return range + 1;
  513. }
  514. else
  515. {
  516. return 1;
  517. }
  518. }
  519. else
  520. {
  521. return 0;
  522. }
  523. }
  524. }
  525. root = search_nest_root(root, name);
  526. if (root)
  527. {
  528. if (!strcmp(arg, "[]"))
  529. {
  530. if (++warning < 100)
  531. {
  532. tintin_printf2(root->ses, "\n\e[1;5;31mdebug: please use *%s instead of $%s.\n", variable, variable);
  533. }
  534. for (index = 0 ; index < root->used ; index++)
  535. {
  536. str_cat_printf(result, "{%s}", root->list[index]->arg1);
  537. }
  538. return root->used + 1;
  539. }
  540. arg = get_arg_in_brackets(root->ses, arg, name);
  541. }
  542. }
  543. return 0;
  544. }
  545. struct listnode *get_nest_node_key(struct listroot *root, char *variable, char **result, int def)
  546. {
  547. struct listnode *node;
  548. int size;
  549. size = get_nest_size_key(root, variable, result); // will copy keys to result
  550. if (size)
  551. {
  552. return NULL;
  553. }
  554. node = search_nest_node(root, variable);
  555. if (node)
  556. {
  557. str_cpy_printf(result, "%s", node->arg1);
  558. if (HAS_BIT(node->flags, NODE_FLAG_ONESHOT))
  559. {
  560. delete_nest_node(root, variable);
  561. }
  562. return node;
  563. }
  564. node = search_base_node(root, variable);
  565. if (node || def)
  566. {
  567. str_cpy(result, "");
  568. }
  569. else
  570. {
  571. str_cpy_printf(result, "*%s", variable);
  572. }
  573. return NULL;
  574. }
  575. // Has ONESHOT check
  576. struct listnode *get_nest_node_val(struct listroot *root, char *variable, char **result, int def)
  577. {
  578. struct listnode *node;
  579. int size;
  580. size = get_nest_size_val(root, variable, result);
  581. if (size)
  582. {
  583. return NULL;
  584. }
  585. node = search_nest_node(root, variable);
  586. if (node)
  587. {
  588. show_nest_node(node, result, TRUE);
  589. if (HAS_BIT(node->flags, NODE_FLAG_ONESHOT))
  590. {
  591. delete_nest_node(root, variable);
  592. }
  593. return node;
  594. }
  595. node = search_base_node(root, variable);
  596. if (node || def)
  597. {
  598. str_cpy(result, "");
  599. }
  600. else
  601. {
  602. str_cpy_printf(result, "$%s", variable);
  603. }
  604. return NULL;
  605. }
  606. int get_nest_index(struct listroot *root, char *variable, char **result, int def)
  607. {
  608. struct listnode *node;
  609. int index, size;
  610. size = get_nest_size_index(root, variable, result);
  611. if (size)
  612. {
  613. str_cpy_printf(result, "%d", size - 1);
  614. return -1;
  615. }
  616. node = search_nest_node(root, variable);
  617. index = search_nest_index(root, variable);
  618. if (node && index >= 0)
  619. {
  620. str_cpy_printf(result, "%d", index + 1);
  621. if (HAS_BIT(node->flags, NODE_FLAG_ONESHOT))
  622. {
  623. delete_index_list(root, index);
  624. }
  625. return index;
  626. }
  627. node = search_base_node(root, variable);
  628. if (node || def)
  629. {
  630. str_cpy(result, "0");
  631. }
  632. else
  633. {
  634. str_cpy_printf(result, "&%s", variable);
  635. }
  636. return -1;
  637. }
  638. // cats to result when initialize is 0
  639. void show_nest_node(struct listnode *node, char **str_result, int initialize)
  640. {
  641. if (initialize)
  642. {
  643. str_cpy(str_result, "");
  644. }
  645. if (node->root == NULL)
  646. {
  647. if (initialize)
  648. {
  649. str_cat(str_result, node->arg2);
  650. }
  651. else
  652. {
  653. str_cat_printf(str_result, "{%s}", node->arg2);
  654. }
  655. }
  656. else
  657. {
  658. struct listroot *root = node->root;
  659. int i;
  660. if (!initialize)
  661. {
  662. str_cat(str_result, "{");
  663. }
  664. for (i = 0 ; i < root->used ; i++)
  665. {
  666. str_cat_printf(str_result, "{%s}", root->list[i]->arg1);
  667. show_nest_node(root->list[i], str_result, FALSE);
  668. }
  669. if (!initialize)
  670. {
  671. str_cat(str_result, "}");
  672. }
  673. }
  674. }
  675. void view_nest_node(struct listnode *node, char **str_result, int nest, int initialize)
  676. {
  677. if (initialize == TRUE)
  678. {
  679. str_cpy(str_result, "");
  680. }
  681. if (node->root == NULL)
  682. {
  683. if (initialize)
  684. {
  685. str_cat(str_result, node->arg2);
  686. }
  687. else
  688. {
  689. str_cat_printf(str_result, COLOR_BRACE "{" COLOR_STRING "%s" COLOR_BRACE "}\n", node->arg2);
  690. }
  691. }
  692. else
  693. {
  694. struct listroot *root = node->root;
  695. int i;
  696. if (initialize == FALSE)
  697. {
  698. str_cat_printf(str_result, "\n" COLOR_BRACE "%s{\n", indent(nest));
  699. }
  700. nest++;
  701. for (i = 0 ; i < root->used ; i++)
  702. {
  703. str_cat_printf(str_result, COLOR_BRACE "%s{" COLOR_STRING "%s" COLOR_BRACE "} ", indent(nest), root->list[i]->arg1);
  704. view_nest_node(root->list[i], str_result, nest, FALSE);
  705. }
  706. nest--;
  707. if (initialize == FALSE)
  708. {
  709. str_cat_printf(str_result, COLOR_BRACE "%s}\n", indent(nest), "");
  710. }
  711. }
  712. }
  713. struct listnode *set_nest_node_ses(struct session *ses, char *arg1, char *format, ...)
  714. {
  715. struct listnode *node;
  716. struct listroot *root;
  717. char *arg, *arg2, name[BUFFER_SIZE];
  718. va_list args;
  719. push_call("set_nest_node_ses(%p,%s,%p,...)",ses,arg1,format);
  720. va_start(args, format);
  721. vasprintf(&arg2, format, args);
  722. va_end(args);
  723. arg = get_arg_to_brackets(ses, arg1, name);
  724. check_all_events(ses, SUB_ARG, 1, 2, "VARIABLE UPDATE %s", name, name, arg2);
  725. root = search_nest_base_ses(ses, name);
  726. if (root == NULL)
  727. {
  728. root = ses->list[LIST_VARIABLE];
  729. node = NULL;
  730. }
  731. else
  732. {
  733. node = search_nest_node(root, arg1);
  734. }
  735. while (*arg)
  736. {
  737. root = update_nest_root(root, name);
  738. if (root)
  739. {
  740. arg = get_arg_in_brackets(root->ses, arg, name);
  741. }
  742. }
  743. node = search_node_list(root, name);
  744. if (node && node->root)
  745. {
  746. free_list(node->root);
  747. node->root = NULL;
  748. }
  749. if (*space_out(arg2) == DEFAULT_OPEN)
  750. {
  751. update_nest_node(update_nest_root(root, name), arg2);
  752. node = search_node_list(root, name);
  753. }
  754. else if (node)
  755. {
  756. str_cpy(&node->arg2, arg2);
  757. }
  758. else
  759. {
  760. node = update_node_list(root, name, arg2, "", "");
  761. }
  762. if (gtd->level->oneshot)
  763. {
  764. SET_BIT(node->flags, NODE_FLAG_ONESHOT);
  765. }
  766. check_all_events(root->ses, SUB_ARG, 1, 1, "VARIABLE UPDATED %s", name, name, arg2);
  767. free(arg2);
  768. pop_call();
  769. return node;
  770. }
  771. // like set, but we're adding here.
  772. struct listnode *add_nest_node_ses(struct session *ses, char *arg1, char *format, ...)
  773. {
  774. struct listnode *node;
  775. struct listroot *root;
  776. char *arg, *arg2, name[BUFFER_SIZE];
  777. va_list args;
  778. push_call("add_nest_node_ses(%p,%s,%p,...)",ses,arg1,format);
  779. va_start(args, format);
  780. vasprintf(&arg2, format, args);
  781. va_end(args);
  782. arg = get_arg_to_brackets(ses, arg1, name);
  783. check_all_events(ses, SUB_ARG, 1, 2, "VARIABLE UPDATE %s", name, name, arg2);
  784. root = search_nest_base_ses(ses, name);
  785. if (root == NULL)
  786. {
  787. root = ses->list[LIST_VARIABLE];
  788. node = NULL;
  789. }
  790. else
  791. {
  792. node = search_nest_node(root, arg1);
  793. }
  794. while (*arg)
  795. {
  796. root = update_nest_root(root, name);
  797. if (root)
  798. {
  799. arg = get_arg_in_brackets(root->ses, arg, name);
  800. }
  801. }
  802. node = search_node_list(root, name);
  803. /*
  804. if (node && node->root)
  805. {
  806. free_list(node->root);
  807. node->root = NULL;
  808. }
  809. */
  810. if (*space_out(arg2) == DEFAULT_OPEN)
  811. {
  812. update_nest_node(update_nest_root(root, name), arg2);
  813. node = search_node_list(root, name);
  814. }
  815. else if (node)
  816. {
  817. str_cpy(&node->arg2, arg2);
  818. }
  819. else
  820. {
  821. node = update_node_list(root, name, arg2, "", "");
  822. }
  823. if (gtd->level->oneshot)
  824. {
  825. SET_BIT(node->flags, NODE_FLAG_ONESHOT);
  826. }
  827. check_all_events(root->ses, SUB_ARG, 1, 1, "VARIABLE UPDATED %s", name, name, arg2);
  828. free(arg2);
  829. pop_call();
  830. return node;
  831. }
  832. struct listnode *set_nest_node(struct listroot *root, char *arg1, char *format, ...)
  833. {
  834. struct listroot *base;
  835. struct listnode *node;
  836. char *arg, *arg2, name[BUFFER_SIZE];
  837. va_list args;
  838. push_call("set_nest_node(%p,%s,%p,...)",root,arg1,format);
  839. va_start(args, format);
  840. vasprintf(&arg2, format, args);
  841. va_end(args);
  842. arg = get_arg_to_brackets(root->ses, arg1, name);
  843. check_all_events(root->ses, SUB_ARG, 1, 2, "VARIABLE UPDATE %s", name, name, arg2);
  844. if (HAS_BIT(gtd->flags, TINTIN_FLAG_LOCAL))
  845. {
  846. base = search_nest_base_ses(root->ses, name);
  847. if (base)
  848. {
  849. root = base;
  850. }
  851. }
  852. while (*arg)
  853. {
  854. root = update_nest_root(root, name);
  855. if (root)
  856. {
  857. arg = get_arg_in_brackets(root->ses, arg, name);
  858. }
  859. }
  860. node = search_node_list(root, name);
  861. if (node && node->root)
  862. {
  863. free_list(node->root);
  864. node->root = NULL;
  865. }
  866. if (*space_out(arg2) == DEFAULT_OPEN)
  867. {
  868. update_nest_node(update_nest_root(root, name), arg2);
  869. node = search_node_list(root, name);
  870. }
  871. else if (node)
  872. {
  873. str_cpy(&node->arg2, arg2);
  874. }
  875. else
  876. {
  877. node = update_node_list(root, name, arg2, "", "");
  878. }
  879. if (gtd->level->oneshot)
  880. {
  881. SET_BIT(node->flags, NODE_FLAG_ONESHOT);
  882. }
  883. check_all_events(root->ses, SUB_ARG, 1, 1, "VARIABLE UPDATED %s", name, name, arg2);
  884. free(arg2);
  885. pop_call();
  886. return node;
  887. }
  888. // Like set, but don't erase old data.
  889. struct listnode *add_nest_node(struct listroot *root, char *arg1, char *format, ...)
  890. {
  891. struct listroot *base;
  892. struct listnode *node;
  893. char *arg, *arg2, name[BUFFER_SIZE];
  894. va_list args;
  895. push_call("add_nest_node(%p,%s,%p,...)",root,arg1,format);
  896. va_start(args, format);
  897. vasprintf(&arg2, format, args);
  898. va_end(args);
  899. arg = get_arg_to_brackets(root->ses, arg1, name);
  900. check_all_events(root->ses, SUB_ARG, 1, 2, "VARIABLE UPDATE %s", name, name, arg2);
  901. if (HAS_BIT(gtd->flags, TINTIN_FLAG_LOCAL))
  902. {
  903. base = search_nest_base_ses(root->ses, name);
  904. if (base)
  905. {
  906. root = base;
  907. }
  908. }
  909. while (*arg)
  910. {
  911. root = update_nest_root(root, name);
  912. if (root)
  913. {
  914. arg = get_arg_in_brackets(root->ses, arg, name);
  915. }
  916. }
  917. node = search_node_list(root, name);
  918. /*
  919. if (node && node->root)
  920. {
  921. free_list(node->root);
  922. node->root = NULL;
  923. }
  924. */
  925. if (*space_out(arg2) == DEFAULT_OPEN)
  926. {
  927. root = update_nest_root(root, name);
  928. update_nest_node(root, arg2);
  929. node = search_node_list(root, name);
  930. }
  931. else if (node)
  932. {
  933. str_cat(&node->arg2, arg2);
  934. }
  935. else
  936. {
  937. node = update_node_list(root, name, arg2, "", "");
  938. }
  939. if (gtd->level->oneshot)
  940. {
  941. SET_BIT(node->flags, NODE_FLAG_ONESHOT);
  942. }
  943. check_all_events(root->ses, SUB_ARG, 1, 1, "VARIABLE UPDATED %s", name, name, arg2);
  944. free(arg2);
  945. pop_call();
  946. return node;
  947. }
  948. void copy_nest_node(struct listroot *dst_root, struct listnode *dst, struct listnode *src)
  949. {
  950. int index;
  951. if (src->root == NULL)
  952. {
  953. return;
  954. }
  955. dst_root = dst->root = init_list(dst_root->ses, dst_root->type, src->root->size);
  956. for (index = 0 ; index < src->root->used ; index++)
  957. {
  958. dst = insert_node_list(dst_root, src->root->list[index]->arg1, src->root->list[index]->arg2, src->root->list[index]->arg3, src->root->list[index]->arg4);
  959. if (src->root->list[index]->root)
  960. {
  961. copy_nest_node(dst_root, dst, src->root->list[index]);
  962. }
  963. }
  964. }