nest.c 21 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202
  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 (node->shots && --node->shots == 0)
  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. struct listnode *get_nest_node_val(struct listroot *root, char *variable, char **result, int def)
  576. {
  577. struct listnode *node;
  578. int size;
  579. size = get_nest_size_val(root, variable, result);
  580. if (size)
  581. {
  582. return NULL;
  583. }
  584. node = search_nest_node(root, variable);
  585. if (node)
  586. {
  587. show_nest_node(node, result, TRUE);
  588. if (node->shots && --node->shots == 0)
  589. {
  590. delete_nest_node(root, variable);
  591. }
  592. return node;
  593. }
  594. node = search_base_node(root, variable);
  595. if (node || def)
  596. {
  597. str_cpy(result, "");
  598. }
  599. else
  600. {
  601. str_cpy_printf(result, "$%s", variable);
  602. }
  603. return NULL;
  604. }
  605. int get_nest_index(struct listroot *root, char *variable, char **result, int def)
  606. {
  607. struct listnode *node;
  608. int index, size;
  609. size = get_nest_size_index(root, variable, result);
  610. if (size)
  611. {
  612. str_cpy_printf(result, "%d", size - 1);
  613. return -1;
  614. }
  615. node = search_nest_node(root, variable);
  616. index = search_nest_index(root, variable);
  617. if (node && index >= 0)
  618. {
  619. str_cpy_printf(result, "%d", index + 1);
  620. if (node->shots && --node->shots == 0)
  621. {
  622. delete_index_list(root, index);
  623. }
  624. return index;
  625. }
  626. node = search_base_node(root, variable);
  627. if (node || def)
  628. {
  629. str_cpy(result, "0");
  630. }
  631. else
  632. {
  633. str_cpy_printf(result, "&%s", variable);
  634. }
  635. return -1;
  636. }
  637. // cats to result when initialize is 0
  638. void show_nest_node(struct listnode *node, char **str_result, int initialize)
  639. {
  640. if (initialize)
  641. {
  642. str_cpy(str_result, "");
  643. }
  644. if (node->root == NULL)
  645. {
  646. if (initialize)
  647. {
  648. str_cat(str_result, node->arg2);
  649. }
  650. else
  651. {
  652. str_cat_printf(str_result, "{%s}", node->arg2);
  653. }
  654. }
  655. else
  656. {
  657. struct listroot *root = node->root;
  658. int i;
  659. if (!initialize)
  660. {
  661. str_cat(str_result, "{");
  662. }
  663. for (i = 0 ; i < root->used ; i++)
  664. {
  665. str_cat_printf(str_result, "{%s}", root->list[i]->arg1);
  666. show_nest_node(root->list[i], str_result, FALSE);
  667. }
  668. if (!initialize)
  669. {
  670. str_cat(str_result, "}");
  671. }
  672. }
  673. }
  674. void view_nest_node(struct listnode *node, char **str_result, int nest, int initialize)
  675. {
  676. if (initialize == TRUE)
  677. {
  678. str_cpy(str_result, "");
  679. }
  680. if (node->root == NULL)
  681. {
  682. if (initialize)
  683. {
  684. str_cat(str_result, node->arg2);
  685. }
  686. else
  687. {
  688. str_cat_printf(str_result, COLOR_BRACE "{" COLOR_STRING "%s" COLOR_BRACE "}\n", node->arg2);
  689. }
  690. }
  691. else
  692. {
  693. struct listroot *root = node->root;
  694. int i;
  695. if (initialize == FALSE)
  696. {
  697. str_cat_printf(str_result, "\n" COLOR_BRACE "%s{\n", indent(nest));
  698. }
  699. nest++;
  700. for (i = 0 ; i < root->used ; i++)
  701. {
  702. str_cat_printf(str_result, COLOR_BRACE "%s{" COLOR_STRING "%s" COLOR_BRACE "} ", indent(nest), root->list[i]->arg1);
  703. view_nest_node(root->list[i], str_result, nest, FALSE);
  704. }
  705. nest--;
  706. if (initialize == FALSE)
  707. {
  708. str_cat_printf(str_result, COLOR_BRACE "%s}\n", indent(nest), "");
  709. }
  710. }
  711. }
  712. struct listnode *set_nest_node_ses(struct session *ses, char *arg1, char *format, ...)
  713. {
  714. struct listnode *node;
  715. struct listroot *root;
  716. char *arg, *arg2, name[BUFFER_SIZE];
  717. va_list args;
  718. push_call("set_nest_node_ses(%p,%s,%p,...)",ses,arg1,format);
  719. va_start(args, format);
  720. vasprintf(&arg2, format, args);
  721. va_end(args);
  722. arg = get_arg_to_brackets(ses, arg1, name);
  723. check_all_events(ses, SUB_ARG, 1, 2, "VARIABLE UPDATE %s", name, name, arg2);
  724. root = search_nest_base_ses(ses, name);
  725. if (root == NULL)
  726. {
  727. if (gtd->level->local)
  728. {
  729. root = local_list(ses);
  730. }
  731. else
  732. {
  733. root = ses->list[LIST_VARIABLE];
  734. }
  735. node = NULL;
  736. }
  737. else
  738. {
  739. node = search_nest_node(root, arg1);
  740. }
  741. while (*arg)
  742. {
  743. root = update_nest_root(root, name);
  744. if (root)
  745. {
  746. arg = get_arg_in_brackets(root->ses, arg, name);
  747. }
  748. }
  749. node = search_node_list(root, name);
  750. if (node && node->root)
  751. {
  752. free_list(node->root);
  753. node->root = NULL;
  754. }
  755. if (*space_out(arg2) == DEFAULT_OPEN)
  756. {
  757. update_nest_node(update_nest_root(root, name), arg2);
  758. node = search_node_list(root, name);
  759. }
  760. else if (node)
  761. {
  762. str_cpy(&node->arg2, arg2);
  763. }
  764. else
  765. {
  766. node = update_node_list(root, name, arg2, "", "");
  767. }
  768. if (gtd->level->shots)
  769. {
  770. node->shots = gtd->level->mshot;
  771. }
  772. check_all_events(root->ses, SUB_ARG, 1, 1, "VARIABLE UPDATED %s", name, name, arg2);
  773. free(arg2);
  774. pop_call();
  775. return node;
  776. }
  777. // like set, but we're adding here.
  778. struct listnode *add_nest_node_ses(struct session *ses, char *arg1, char *format, ...)
  779. {
  780. struct listnode *node;
  781. struct listroot *root;
  782. char *arg, *arg2, name[BUFFER_SIZE];
  783. va_list args;
  784. push_call("add_nest_node_ses(%p,%s,%p,...)",ses,arg1,format);
  785. va_start(args, format);
  786. vasprintf(&arg2, format, args);
  787. va_end(args);
  788. arg = get_arg_to_brackets(ses, arg1, name);
  789. check_all_events(ses, SUB_ARG, 1, 2, "VARIABLE UPDATE %s", name, name, arg2);
  790. root = search_nest_base_ses(ses, name);
  791. if (root == NULL)
  792. {
  793. root = ses->list[LIST_VARIABLE];
  794. node = NULL;
  795. }
  796. else
  797. {
  798. node = search_nest_node(root, arg1);
  799. }
  800. while (*arg)
  801. {
  802. root = update_nest_root(root, name);
  803. if (root)
  804. {
  805. arg = get_arg_in_brackets(root->ses, arg, name);
  806. }
  807. }
  808. node = search_node_list(root, name);
  809. /*
  810. if (node && node->root)
  811. {
  812. free_list(node->root);
  813. node->root = NULL;
  814. }
  815. */
  816. if (*space_out(arg2) == DEFAULT_OPEN)
  817. {
  818. update_nest_node(update_nest_root(root, name), arg2);
  819. node = search_node_list(root, name);
  820. }
  821. else if (node)
  822. {
  823. str_cpy(&node->arg2, arg2);
  824. }
  825. else
  826. {
  827. node = update_node_list(root, name, arg2, "", "");
  828. }
  829. if (gtd->level->shots)
  830. {
  831. node->shots = gtd->level->mshot;
  832. }
  833. check_all_events(root->ses, SUB_ARG, 1, 1, "VARIABLE UPDATED %s", name, name, arg2);
  834. free(arg2);
  835. pop_call();
  836. return node;
  837. }
  838. struct listnode *set_nest_node(struct listroot *root, char *arg1, char *format, ...)
  839. {
  840. struct listroot *base;
  841. struct listnode *node;
  842. char *arg, *arg2, name[BUFFER_SIZE];
  843. va_list args;
  844. push_call("set_nest_node(%p,%s,%p,...)",root,arg1,format);
  845. va_start(args, format);
  846. vasprintf(&arg2, format, args);
  847. va_end(args);
  848. arg = get_arg_to_brackets(root->ses, arg1, name);
  849. check_all_events(root->ses, SUB_ARG, 1, 2, "VARIABLE UPDATE %s", name, name, arg2);
  850. if (HAS_BIT(gtd->flags, TINTIN_FLAG_LOCAL))
  851. {
  852. base = search_nest_base_ses(root->ses, name);
  853. if (base)
  854. {
  855. root = base;
  856. }
  857. }
  858. while (*arg)
  859. {
  860. root = update_nest_root(root, name);
  861. if (root)
  862. {
  863. arg = get_arg_in_brackets(root->ses, arg, name);
  864. }
  865. }
  866. node = search_node_list(root, name);
  867. if (node && node->root)
  868. {
  869. free_list(node->root);
  870. node->root = NULL;
  871. }
  872. if (*space_out(arg2) == DEFAULT_OPEN)
  873. {
  874. update_nest_node(update_nest_root(root, name), arg2);
  875. node = search_node_list(root, name);
  876. }
  877. else if (node)
  878. {
  879. str_cpy(&node->arg2, arg2);
  880. }
  881. else
  882. {
  883. node = update_node_list(root, name, arg2, "", "");
  884. }
  885. if (gtd->level->shots)
  886. {
  887. node->shots = gtd->level->mshot;
  888. }
  889. check_all_events(root->ses, SUB_ARG, 1, 1, "VARIABLE UPDATED %s", name, name, arg2);
  890. free(arg2);
  891. pop_call();
  892. return node;
  893. }
  894. // Like set, but don't erase old data.
  895. struct listnode *add_nest_node(struct listroot *root, char *arg1, char *format, ...)
  896. {
  897. struct listroot *base;
  898. struct listnode *node;
  899. char *arg, *arg2, name[BUFFER_SIZE];
  900. va_list args;
  901. push_call("add_nest_node(%p,%s,%p,...)",root,arg1,format);
  902. va_start(args, format);
  903. vasprintf(&arg2, format, args);
  904. va_end(args);
  905. arg = get_arg_to_brackets(root->ses, arg1, name);
  906. check_all_events(root->ses, SUB_ARG, 1, 2, "VARIABLE UPDATE %s", name, name, arg2);
  907. if (HAS_BIT(gtd->flags, TINTIN_FLAG_LOCAL))
  908. {
  909. base = search_nest_base_ses(root->ses, name);
  910. if (base)
  911. {
  912. root = base;
  913. }
  914. }
  915. while (*arg)
  916. {
  917. root = update_nest_root(root, name);
  918. if (root)
  919. {
  920. arg = get_arg_in_brackets(root->ses, arg, name);
  921. }
  922. }
  923. node = search_node_list(root, name);
  924. /*
  925. if (node && node->root)
  926. {
  927. free_list(node->root);
  928. node->root = NULL;
  929. }
  930. */
  931. if (*space_out(arg2) == DEFAULT_OPEN)
  932. {
  933. root = update_nest_root(root, name);
  934. update_nest_node(root, arg2);
  935. node = search_node_list(root, name);
  936. }
  937. else if (node)
  938. {
  939. str_cat(&node->arg2, arg2);
  940. }
  941. else
  942. {
  943. node = update_node_list(root, name, arg2, "", "");
  944. }
  945. if (gtd->level->shots)
  946. {
  947. node->shots = gtd->level->mshot;
  948. }
  949. check_all_events(root->ses, SUB_ARG, 1, 1, "VARIABLE UPDATED %s", name, name, arg2);
  950. free(arg2);
  951. pop_call();
  952. return node;
  953. }
  954. void copy_nest_node(struct listroot *dst_root, struct listnode *dst, struct listnode *src)
  955. {
  956. int index;
  957. if (src->root == NULL)
  958. {
  959. return;
  960. }
  961. dst_root = dst->root = init_list(dst_root->ses, dst_root->type, src->root->size);
  962. for (index = 0 ; index < src->root->used ; index++)
  963. {
  964. 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);
  965. if (src->root->list[index]->root)
  966. {
  967. copy_nest_node(dst_root, dst, src->root->list[index]);
  968. }
  969. }
  970. }