| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468 |
- /******************************************************************************
- * This file is part of TinTin++ *
- * *
- * Copyright 2004-2020 Igor van den Hoven *
- * *
- * TinTin++ is free software; you can redistribute it and/or modify *
- * it under the terms of the GNU General Public License as published by *
- * the Free Software Foundation; either version 3 of the License, or *
- * (at your option) any later version. *
- * *
- * This program is distributed in the hope that it will be useful, *
- * but WITHOUT ANY WARRANTY; without even the implied warranty of *
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
- * GNU General Public License for more details. *
- * *
- * You should have received a copy of the GNU General Public License *
- * along with TinTin++. If not, see https://www.gnu.org/licenses. *
- ******************************************************************************/
- /******************************************************************************
- * T I N T I N + + *
- * *
- * coded by Igor van den Hoven 2009 *
- ******************************************************************************/
- #include "tintin.h"
- struct listroot *search_nest_root(struct listroot *root, char *arg)
- {
- struct listnode *node = search_node_list(root, arg);
- return node ? node->root : NULL;
- }
- struct listroot *search_nest_base_ses(struct session *ses, char *arg)
- {
- struct listnode *node;
- struct listroot *root;
- int index;
- if (HAS_BIT(gtd->flags, TINTIN_FLAG_LOCAL))
- {
- for (index = gtd->script_index ; index >= 0 ; index--)
- {
- root = gtd->script_stack[index]->local;
- if (root->used)
- {
- node = search_node_list(root, arg);
- if (node)
- {
- return root;
- }
- }
- }
- }
- node = search_node_list(ses->list[LIST_VARIABLE], arg);
- if (node == NULL)
- {
- return NULL;
- }
- return ses->list[LIST_VARIABLE];
- }
- struct listnode *search_base_node(struct listroot *root, char *variable)
- {
- char name[BUFFER_SIZE];
- get_arg_to_brackets(root->ses, variable, name);
- return search_node_list(root, name);
- }
- struct listnode *search_nest_node_ses(struct session *ses, char *variable)
- {
- char name[BUFFER_SIZE], *arg;
- struct listroot *root;
- struct listnode *node;
- int index;
- if (HAS_BIT(gtd->flags, TINTIN_FLAG_LOCAL))
- {
- for (index = gtd->script_index ; index >= 0 ; index--)
- {
- root = gtd->script_stack[index]->local;
- if (root->used)
- {
- arg = get_arg_to_brackets(root->ses, variable, name);
- while (root && *arg)
- {
- root = search_nest_root(root, name);
- if (root)
- {
- arg = get_arg_in_brackets(root->ses, arg, name);
- }
- }
- if (root)
- {
- node = search_node_list(root, name);
- if (node)
- {
- return node;
- }
- }
- }
- }
- }
- root = ses->list[LIST_VARIABLE];
- arg = get_arg_to_brackets(ses, variable, name);
- while (root && *arg)
- {
- root = search_nest_root(root, name);
- if (root)
- {
- arg = get_arg_in_brackets(root->ses, arg, name);
- }
- }
- if (root)
- {
- return search_node_list(root, name);
- }
- return NULL;
- }
- struct listnode *search_nest_node(struct listroot *root, char *variable)
- {
- char name[BUFFER_SIZE], *arg;
- arg = get_arg_to_brackets(root->ses, variable, name);
- if (*name == 0)
- {
- arg = get_arg_in_brackets(root->ses, arg, name);
- }
- while (root && *arg)
- {
- root = search_nest_root(root, name);
- if (root)
- {
- arg = get_arg_in_brackets(root->ses, arg, name);
- }
- }
- if (root)
- {
- return search_node_list(root, name);
- }
- return NULL;
- }
- // same as search_nest_node, but store the path in *path
- struct listnode *search_nest_node_path(struct listroot *root, char *variable, char *path)
- {
- struct listnode *node;
- char name[BUFFER_SIZE], *arg;
- arg = get_arg_to_brackets(root->ses, variable, name);
- node = search_node_list(root, name);
- if (node)
- {
- path += sprintf(path, "%s", node->arg1);
- if (*arg == 0)
- {
- return node;
- }
- }
- root = node ? node->root : NULL;
- if (root)
- {
- arg = get_arg_in_brackets(root->ses, arg, name);
- }
- while (root && *arg)
- {
- node = search_node_list(root, name);
- if (node)
- {
- path += sprintf(path, "[%s]", node->arg1);
- }
- root = node ? node->root : NULL;
- if (root)
- {
- arg = get_arg_in_brackets(root->ses, arg, name);
- }
- }
- if (root)
- {
- node = search_node_list(root, name);
- if (node)
- {
- path += sprintf(path, "[%s]", node->arg1);
- }
- return node;
- }
- return NULL;
- }
- int search_nest_index(struct listroot *root, char *variable)
- {
- char name[BUFFER_SIZE], *arg;
- arg = get_arg_to_brackets(root->ses, variable, name);
- while (root && *arg)
- {
- root = search_nest_root(root, name);
- if (root)
- {
- arg = get_arg_in_brackets(root->ses, arg, name);
- }
- }
- if (root)
- {
- return search_index_list(root, name, NULL);
- }
- return -1;
- }
- struct listroot *update_nest_root(struct listroot *root, char *arg)
- {
- struct listnode *node;
- node = search_node_list(root, arg);
- if (node == NULL)
- {
- node = update_node_list(root, arg, "", "", "");
- }
- if (node->root == NULL)
- {
- node->root = init_list(root->ses, root->type, LIST_SIZE);
- }
- return node->root;
- }
- void update_nest_node(struct listroot *root, char *arg)
- {
- char *arg1, *arg2;
- arg1 = str_mim(arg);
- arg2 = str_mim(arg);
- while (*arg)
- {
- arg = get_arg_in_braces(root->ses, arg, arg1, GET_ONE);
- arg = get_arg_in_braces(root->ses, arg, arg2, GET_ONE);
- if (*arg2 == DEFAULT_OPEN)
- {
- update_nest_node(update_nest_root(root, arg1), arg2);
- }
- else //if (*arg1)
- {
- update_node_list(root, arg1, arg2, "", "");
- }
- if (*arg == COMMAND_SEPARATOR)
- {
- arg++;
- }
- }
- str_free(arg1);
- str_free(arg2);
- return;
- }
- int delete_nest_node(struct listroot *root, char *variable)
- {
- char name[BUFFER_SIZE], *arg;
- int index;
- arg = get_arg_to_brackets(root->ses, variable, name);
- while (root && *arg)
- {
- root = search_nest_root(root, name);
- if (root)
- {
- arg = get_arg_in_brackets(root->ses, arg, name);
- }
- }
- if (root)
- {
- index = search_index_list(root, name, NULL);
- if (index != -1)
- {
- delete_index_list(root, index);
- return TRUE;
- }
- }
- return FALSE;
- }
- int delete_nest_node_with_wild(struct listroot *root, char *variable)
- {
- char name[BUFFER_SIZE], *arg, *ptv;
- int index, found;
- arg = get_arg_to_brackets(root->ses, variable, name);
- ptv = arg;
- while (root && *arg)
- {
- root = search_nest_root(root, name);
- if (root)
- {
- arg = get_arg_in_brackets(root->ses, arg, name);
- if (*arg != 0)
- {
- ptv = arg;
- }
- }
- }
- found = FALSE;
- if (root)
- {
- for (index = root->used - 1 ; index >= 0 ; index--)
- {
- if (match(root->ses, root->list[index]->arg1, name, SUB_VAR|SUB_FUN))
- {
- if (*ptv)
- {
- show_message(root->ses, LIST_VARIABLE, "#OK. {%.*s[%s]} IS NO LONGER A VARIABLE.", ptv - variable, variable, root->list[index]->arg1);
- }
- else
- {
- show_message(root->ses, LIST_VARIABLE, "#OK. {%s} IS NO LONGER A VARIABLE.", root->list[index]->arg1);
- }
- delete_index_list(root, index);
- found = TRUE;
- }
- }
- }
- return found;
- }
- // Return the number of indices of a node.
- int get_nest_size(struct listroot *root, char *variable)
- {
- char name[BUFFER_SIZE], *arg;
- int index, count;
- arg = get_arg_to_brackets(root->ses, variable, name);
- if (!strcmp(arg, "[]"))
- {
- if (*name == 0)
- {
- return root->used + 1;
- }
- if (search_nest_root(root, name) == NULL)
- {
- if (search_node_list(root, name))
- {
- return 1;
- }
- }
- }
- if (*name == 0)
- {
- arg = get_arg_in_brackets(root->ses, arg, name);
- }
- while (root && *name)
- {
- // Handle regex queries
- if (search_nest_root(root, name) == NULL)
- {
- if (search_node_list(root, name) == NULL)
- {
- if (tintin_regexp_check(root->ses, name))
- {
- for (index = count = 0 ; index < root->used ; index++)
- {
- if (match(root->ses, root->list[index]->arg1, name, SUB_NONE))
- {
- count++;
- }
- }
- return count + 1;
- }
- else if (strstr(name, "..") && is_math(root->ses, name))
- {
- int min, max, range;
- if (root->used)
- {
- range = get_ellipsis(root->ses, root->used, name, &min, &max);
- return range + 1;
- }
- else
- {
- return 1;
- }
- }
- else
- {
- return 0;
- }
- }
- }
- root = search_nest_root(root, name);
- if (root)
- {
- if (!strcmp(arg, "[]"))
- {
- return root->used + 1;
- }
- arg = get_arg_in_brackets(root->ses, arg, name);
- }
- }
- return 0;
- }
- int get_nest_size_index(struct listroot *root, char *variable, char **result)
- {
- char name[BUFFER_SIZE], *arg;
- int index, count;
- arg = get_arg_to_brackets(root->ses, variable, name);
- str_cpy(result, "");
- if (!strcmp(arg, "[]"))
- {
- if (*name == 0)
- {
- return root->used + 1;
- }
- if (search_nest_root(root, name) == NULL)
- {
- if (search_node_list(root, name))
- {
- return 1;
- }
- }
- }
- if (*name == 0)
- {
- arg = get_arg_in_brackets(root->ses, arg, name);
- }
- while (root && *name)
- {
- // Handle regex queries
- if (search_nest_root(root, name) == NULL)
- {
- if (search_node_list(root, name) == NULL)
- {
- if (tintin_regexp_check(root->ses, name))
- {
- for (index = count = 0 ; index < root->used ; index++)
- {
- if (match(root->ses, root->list[index]->arg1, name, SUB_NONE))
- {
- count++;
- }
- }
- return count + 1;
- }
- else if (strstr(name, "..") && is_math(root->ses, name))
- {
- int min, max, range;
- if (root->used)
- {
- range = get_ellipsis(root->ses, root->used, name, &min, &max);
- return range + 1;
- }
- else
- {
- return 1;
- }
- }
- else
- {
- return 0;
- }
- }
- }
- root = search_nest_root(root, name);
- if (root)
- {
- if (!strcmp(arg, "[]"))
- {
- return root->used + 1;
- }
- arg = get_arg_in_brackets(root->ses, arg, name);
- }
- }
- return 0;
- }
- int get_nest_size_key(struct listroot *root, char *variable, char **result)
- {
- char name[BUFFER_SIZE], *arg;
- int index, count;
- arg = get_arg_to_brackets(root->ses, variable, name);
- str_cpy(result, "");
- if (!strcmp(arg, "[]"))
- {
- if (*name == 0)
- {
- for (index = 0 ; index < root->used ; index++)
- {
- str_cat_printf(result, "{%s}", root->list[index]->arg1);
- }
- return root->used + 1;
- }
- if (search_nest_root(root, name) == NULL)
- {
- if (search_node_list(root, name))
- {
- return 1;
- }
- }
- }
- if (*name == 0)
- {
- arg = get_arg_in_brackets(root->ses, arg, name);
- }
- while (root && *name)
- {
- // Handle regex queries
- if (search_nest_root(root, name) == NULL)
- {
- if (search_node_list(root, name) == NULL)
- {
- if (tintin_regexp_check(root->ses, name))
- {
- for (index = count = 0 ; index < root->used ; index++)
- {
- if (match(root->ses, root->list[index]->arg1, name, SUB_NONE))
- {
- str_cat_printf(result, "{%s}", root->list[index]->arg1);
- count++;
- }
- }
- return count + 1;
- }
- else if (strstr(name, "..") && is_math(root->ses, name))
- {
- int min, max, range;
- if (root->used)
- {
- range = get_ellipsis(root->ses, root->used, name, &min, &max);
- // tintin_printf2(root->ses, "debug: %d %d %d\n", min, max, range);
- if (min < max)
- {
- while (min <= max)
- {
- str_cat_printf(result, "{%s}", root->list[min++]->arg1);
- }
- }
- else
- {
- while (min >= max)
- {
- str_cat_printf(result, "{%s}", root->list[min--]->arg1);
- }
- }
- return range + 1;
- }
- else
- {
- return 1;
- }
- }
- else
- {
- return 0;
- }
- }
- }
- root = search_nest_root(root, name);
- if (root)
- {
- if (!strcmp(arg, "[]"))
- {
- for (index = 0 ; index < root->used ; index++)
- {
- str_cat_printf(result, "{%s}", root->list[index]->arg1);
- }
- return root->used + 1;
- }
- arg = get_arg_in_brackets(root->ses, arg, name);
- }
- }
- return 0;
- }
- int get_nest_size_val(struct listroot *root, char *variable, char **result)
- {
- char name[BUFFER_SIZE], *arg;
- int index, count;
- arg = get_arg_to_brackets(root->ses, variable, name);
- str_cpy(result, "");
- if (!strcmp(arg, "[]"))
- {
- if (*name == 0)
- {
- for (index = 0 ; index < root->used ; index++)
- {
- show_nest_node(root->list[index], result, FALSE); // behaves like strcat
- // str_cat_printf(result, "{%s}", root->list[index]->arg2);
- }
- return root->used + 1;
- }
- if (search_nest_root(root, name) == NULL)
- {
- if (search_node_list(root, name))
- {
- return 1;
- }
- }
- }
- if (*name == 0)
- {
- arg = get_arg_in_brackets(root->ses, arg, name);
- }
- while (root && *name)
- {
- // Handle regex queries
- if (search_nest_root(root, name) == NULL)
- {
- if (search_node_list(root, name) == NULL)
- {
- if (tintin_regexp_check(root->ses, name))
- {
- for (index = count = 0 ; index < root->used ; index++)
- {
- if (match(root->ses, root->list[index]->arg1, name, SUB_NONE))
- {
- show_nest_node(root->list[index], result, FALSE); // behaves like strcat
- count++;
- }
- }
- return count + 1;
- }
- else if (strstr(name, "..") && is_math(root->ses, name))
- {
- int min, max, range;
- if (root->used)
- {
- range = get_ellipsis(root->ses, root->used, name, &min, &max);
- if (min < max)
- {
- while (min <= max)
- {
- show_nest_node(root->list[min++], result, FALSE);
- }
- }
- else
- {
- while (min >= max)
- {
- show_nest_node(root->list[min--], result, FALSE);
- }
- }
- return range + 1;
- }
- else
- {
- return 1;
- }
- }
- else
- {
- return 0;
- }
- }
- }
- root = search_nest_root(root, name);
- if (root)
- {
- if (!strcmp(arg, "[]"))
- {
- for (index = 0 ; index < root->used ; index++)
- {
- show_nest_node(root->list[index], result, FALSE); // behaves like strcat
- // str_cat_printf(result, "{%s}", root->list[index]->arg2);
- }
- return root->used + 1;
- }
- arg = get_arg_in_brackets(root->ses, arg, name);
- }
- }
- return 0;
- }
- struct listnode *get_nest_node_key(struct listroot *root, char *variable, char **result, int def)
- {
- struct listnode *node;
- int size;
- size = get_nest_size_key(root, variable, result); // will copy keys to result
- if (size)
- {
- return NULL;
- }
- node = search_nest_node(root, variable);
- if (node)
- {
- str_cpy_printf(result, "%s", node->arg1);
- if (node->shots && --node->shots == 0)
- {
- delete_nest_node(root, variable);
- }
- return node;
- }
- node = search_base_node(root, variable);
- if (node || def)
- {
- str_cpy(result, "");
- }
- else
- {
- str_cpy_printf(result, "*%s", variable);
- }
- return NULL;
- }
- struct listnode *get_nest_node_val(struct listroot *root, char *variable, char **result, int def)
- {
- struct listnode *node;
- int size;
- size = get_nest_size_val(root, variable, result);
- if (size)
- {
- return NULL;
- }
- node = search_nest_node(root, variable);
- if (node)
- {
- show_nest_node(node, result, TRUE);
- if (node->shots && --node->shots == 0)
- {
- delete_nest_node(root, variable);
- }
- return node;
- }
- node = search_base_node(root, variable);
- if (node || def)
- {
- str_cpy(result, "");
- }
- else
- {
- str_cpy_printf(result, "$%s", variable);
- }
- return NULL;
- }
- int get_nest_index(struct listroot *root, char *variable, char **result, int def)
- {
- struct listnode *node;
- int index, size;
- size = get_nest_size_index(root, variable, result);
- if (size)
- {
- str_cpy_printf(result, "%d", size - 1);
- return -1;
- }
- node = search_nest_node(root, variable);
- index = search_nest_index(root, variable);
- if (node && index >= 0)
- {
- str_cpy_printf(result, "%d", index + 1);
- if (node->shots && --node->shots == 0)
- {
- delete_index_list(root, index);
- }
- return index;
- }
- node = search_base_node(root, variable);
- if (node || def)
- {
- str_cpy(result, "0");
- }
- else
- {
- str_cpy_printf(result, "&%s", variable);
- }
- return -1;
- }
- // cats to result when initialize is 0
- void show_nest_node(struct listnode *node, char **str_result, int initialize)
- {
- if (initialize)
- {
- str_cpy(str_result, "");
- }
- if (node->root == NULL)
- {
- if (initialize)
- {
- str_cat(str_result, node->arg2);
- }
- else
- {
- str_cat_printf(str_result, "{%s}", node->arg2);
- }
- }
- else
- {
- struct listroot *root = node->root;
- int i;
- if (!initialize)
- {
- str_cat(str_result, "{");
- }
- for (i = 0 ; i < root->used ; i++)
- {
- str_cat_printf(str_result, "{%s}", root->list[i]->arg1);
- show_nest_node(root->list[i], str_result, FALSE);
- }
- if (!initialize)
- {
- str_cat(str_result, "}");
- }
- }
- }
- void view_nest_node(struct listnode *node, char **str_result, int nest, int initialize, int color)
- {
- if (initialize == TRUE)
- {
- str_cpy(str_result, "");
- }
- if (node->root == NULL)
- {
- if (initialize)
- {
- str_cat(str_result, node->arg2);
- }
- else
- {
- if (color)
- {
- str_cat_printf(str_result, COLOR_BRACE "{" COLOR_STRING "%s" COLOR_BRACE "}\n", node->arg2);
- }
- else
- {
- str_cat_printf(str_result, "{%s}\n", node->arg2);
- }
- }
- }
- else
- {
- struct listroot *root = node->root;
- int i;
- if (initialize == FALSE)
- {
- if (color)
- {
- str_cat_printf(str_result, "\n" COLOR_BRACE "%s{\n", indent(nest));
- }
- else
- {
- str_cat_printf(str_result, "\n%s{\n", indent(nest));
- }
- }
- nest++;
- for (i = 0 ; i < root->used ; i++)
- {
- if (color)
- {
- str_cat_printf(str_result, COLOR_BRACE "%s{" COLOR_STRING "%s" COLOR_BRACE "} ", indent(nest), root->list[i]->arg1);
- }
- else
- {
- str_cat_printf(str_result, "%s{%s} ", indent(nest), root->list[i]->arg1);
- }
- view_nest_node(root->list[i], str_result, nest, FALSE, color);
- }
- nest--;
- if (initialize == FALSE)
- {
- if (color)
- {
- str_cat_printf(str_result, COLOR_BRACE "%s}\n", indent(nest), "");
- }
- else
- {
- str_cat_printf(str_result, "%s}\n", indent(nest), "");
- }
- }
- }
- }
- void view_nest_node_json(struct listnode *node, char **str_result, int nest, int initialize)
- {
- if (initialize == TRUE)
- {
- str_cpy(str_result, "");
- }
- if (node->root == NULL)
- {
- str_cat_printf(str_result, "\"%s\"", node->arg2);
- }
- else
- {
- struct listroot *root = node->root;
- int i;
- if (initialize)
- {
- str_cat_printf(str_result, "%s{\n", indent(nest));
- }
- else
- {
- str_cat_printf(str_result, "\n%s{\n", indent(nest));
- }
- nest++;
- for (i = 0 ; i < root->used ; i++)
- {
- if (i)
- {
- str_cat_printf(str_result, ",\n");
- }
- str_cat_printf(str_result, "%s\"%s\" : ", indent(nest), root->list[i]->arg1);
- view_nest_node_json(root->list[i], str_result, nest, FALSE);
- }
- nest--;
- if (initialize)
- {
- str_cat_printf(str_result, "\n%s}\n", indent(nest), "");
- }
- else
- {
- str_cat_printf(str_result, "\n%s}", indent(nest), "");
- }
- }
- }
- // Adds gtd->level->local support
- struct listnode *set_nest_node_ses(struct session *ses, char *arg1, char *format, ...)
- {
- struct listnode *node;
- struct listroot *root;
- char *arg, *arg2, name[BUFFER_SIZE];
- va_list args;
- push_call("set_nest_node_ses(%p,%s,%p,...)",ses,arg1,format);
- va_start(args, format);
- if (vasprintf(&arg2, format, args) == -1)
- {
- syserr_printf(ses, "set_nest_node_ses: vasprintf");
- }
- va_end(args);
- arg = get_arg_to_brackets(ses, arg1, name);
- if (HAS_BIT(ses->event_flags, EVENT_FLAG_VARIABLE))
- {
- check_all_events(ses, EVENT_FLAG_VARIABLE, 1, 3, "VARIABLE UPDATE %s", name, name, arg2, arg1);
- }
- root = search_nest_base_ses(ses, name);
- if (root == NULL)
- {
- if (gtd->level->local)
- {
- root = local_list(ses);
- }
- else
- {
- root = ses->list[LIST_VARIABLE];
- }
- }
- while (*arg)
- {
- root = update_nest_root(root, name);
- if (root)
- {
- arg = get_arg_in_brackets(root->ses, arg, name);
- }
- }
- node = search_node_list(root, name);
- if (node && node->root)
- {
- free_list(node->root);
- node->root = NULL;
- }
- if (*space_out(arg2) == DEFAULT_OPEN)
- {
- update_nest_node(update_nest_root(root, name), arg2);
- node = search_node_list(root, name);
- }
- else if (node)
- {
- str_cpy(&node->arg2, arg2);
- }
- else
- {
- node = update_node_list(root, name, arg2, "", "");
- }
- if (gtd->level->shots)
- {
- node->shots = gtd->level->mshot;
- }
- if (HAS_BIT(root->ses->event_flags, EVENT_FLAG_VARIABLE))
- {
- arg = get_arg_to_brackets(root->ses, arg1, name);
- check_all_events(root->ses, EVENT_FLAG_VARIABLE, 1, 3, "VARIABLE UPDATED %s", name, name, arg2, arg1);
- if (strcmp(arg1, name))
- {
- check_all_events(root->ses, EVENT_FLAG_VARIABLE, 1, 3, "VARIABLE UPDATED %s", arg1, name, arg2, arg1);
- }
- }
- free(arg2);
- pop_call();
- return node;
- }
- // like set, but we're adding here.
- struct listnode *add_nest_node_ses(struct session *ses, char *arg1, char *format, ...)
- {
- struct listnode *node;
- struct listroot *root;
- char *arg, *arg2, *name;
- va_list args;
- push_call("add_nest_node_ses(%p,%s,%p,...)",ses,arg1,format);
- name = str_alloc_stack(0);
- va_start(args, format);
- if (vasprintf(&arg2, format, args) == -1)
- {
- syserr_printf(ses, "add_nest_node_ses: vasprintf");
- }
- va_end(args);
- arg = get_arg_to_brackets(ses, arg1, name);
- if (HAS_BIT(ses->event_flags, EVENT_FLAG_VARIABLE))
- {
- check_all_events(ses, EVENT_FLAG_VARIABLE, 1, 3, "VARIABLE UPDATE %s", name, name, arg2, arg1);
- }
- root = search_nest_base_ses(ses, name);
- if (root == NULL)
- {
- root = ses->list[LIST_VARIABLE];
- }
- while (*arg)
- {
- root = update_nest_root(root, name);
- if (root)
- {
- arg = get_arg_in_brackets(root->ses, arg, name);
- }
- }
- node = search_node_list(root, name);
- /*
- if (node && node->root)
- {
- free_list(node->root);
- node->root = NULL;
- }
- */
- if (*space_out(arg2) == DEFAULT_OPEN)
- {
- update_nest_node(update_nest_root(root, name), arg2);
- node = search_node_list(root, name);
- }
- else if (node)
- {
- str_cat(&node->arg2, arg2);
- }
- else
- {
- node = update_node_list(root, name, arg2, "", "");
- }
- if (gtd->level->shots)
- {
- node->shots = gtd->level->mshot;
- }
- if (HAS_BIT(root->ses->event_flags, EVENT_FLAG_VARIABLE))
- {
- arg = get_arg_to_brackets(root->ses, arg1, name);
- check_all_events(root->ses, EVENT_FLAG_VARIABLE, 1, 3, "VARIABLE UPDATED %s", name, name, arg2, arg1);
- if (strcmp(arg1, name))
- {
- check_all_events(root->ses, EVENT_FLAG_VARIABLE, 1, 3, "VARIABLE UPDATED %s", arg1, name, arg2, arg1);
- }
- }
- free(arg2);
- pop_call();
- return node;
- }
- struct listnode *set_nest_node(struct listroot *root, char *arg1, char *format, ...)
- {
- struct listroot *base;
- struct listnode *node;
- char *arg, *arg2, name[BUFFER_SIZE];
- va_list args;
- push_call("set_nest_node(%p,%s,%p,...)",root,arg1,format);
- va_start(args, format);
- if (vasprintf(&arg2, format, args) == -1)
- {
- syserr_printf(root->ses, "set_nest_node: vasprintf");
- }
- va_end(args);
- arg = get_arg_to_brackets(root->ses, arg1, name);
- if (HAS_BIT(root->ses->event_flags, EVENT_FLAG_VARIABLE))
- {
- check_all_events(root->ses, EVENT_FLAG_VARIABLE, 1, 3, "VARIABLE UPDATE %s", name, name, arg2, arg1);
- }
- if (HAS_BIT(gtd->flags, TINTIN_FLAG_LOCAL))
- {
- base = search_nest_base_ses(root->ses, name);
- if (base)
- {
- root = base;
- }
- }
- while (*arg)
- {
- root = update_nest_root(root, name);
- if (root)
- {
- arg = get_arg_in_brackets(root->ses, arg, name);
- }
- }
- node = search_node_list(root, name);
- if (node && node->root)
- {
- free_list(node->root);
- node->root = NULL;
- }
- if (*space_out(arg2) == DEFAULT_OPEN)
- {
- update_nest_node(update_nest_root(root, name), arg2);
- node = search_node_list(root, name);
- }
- else if (node)
- {
- str_cpy(&node->arg2, arg2);
- }
- else
- {
- if (*name == '-' || *name == '+')
- {
- get_number_string(root->ses, name, name);
- // printf("debug: set_nest_node - or +\n");
- }
- node = update_node_list(root, name, arg2, "", "");
- }
- if (gtd->level->shots)
- {
- node->shots = gtd->level->mshot;
- }
- if (HAS_BIT(root->ses->event_flags, EVENT_FLAG_VARIABLE))
- {
- arg = get_arg_to_brackets(root->ses, arg1, name);
- check_all_events(root->ses, EVENT_FLAG_VARIABLE, 1, 3, "VARIABLE UPDATED %s", name, name, arg2, arg1);
- if (strcmp(arg1, name))
- {
- check_all_events(root->ses, EVENT_FLAG_VARIABLE, 1, 3, "VARIABLE UPDATED %s", arg1, name, arg2, arg1);
- }
- }
- free(arg2);
- pop_call();
- return node;
- }
- // Like set, but don't erase old data.
- struct listnode *add_nest_node(struct listroot *root, char *arg1, char *format, ...)
- {
- struct listroot *base;
- struct listnode *node;
- char *arg, *arg2, name[BUFFER_SIZE];
- va_list args;
- push_call("add_nest_node(%p,%s,%p,...)",root,arg1,format);
- va_start(args, format);
- if (vasprintf(&arg2, format, args) == -1)
- {
- syserr_printf(root->ses, "add_nest_node: vasprintf");
- }
- va_end(args);
- arg = get_arg_to_brackets(root->ses, arg1, name);
- if (HAS_BIT(root->ses->event_flags, EVENT_FLAG_VARIABLE))
- {
- check_all_events(root->ses, EVENT_FLAG_VARIABLE, 1, 3, "VARIABLE UPDATE %s", name, name, arg2, arg1);
- }
- if (HAS_BIT(gtd->flags, TINTIN_FLAG_LOCAL))
- {
- base = search_nest_base_ses(root->ses, name);
- if (base)
- {
- root = base;
- }
- }
- while (*arg)
- {
- root = update_nest_root(root, name);
- if (root)
- {
- arg = get_arg_in_brackets(root->ses, arg, name);
- }
- }
- node = search_node_list(root, name);
- /*
- if (node && node->root)
- {
- free_list(node->root);
- node->root = NULL;
- }
- */
- if (*space_out(arg2) == DEFAULT_OPEN)
- {
- root = update_nest_root(root, name);
- update_nest_node(root, arg2);
- node = search_node_list(root, name);
- }
- else if (node)
- {
- str_cat(&node->arg2, arg2);
- }
- else
- {
- node = update_node_list(root, name, arg2, "", "");
- }
- if (gtd->level->shots)
- {
- node->shots = gtd->level->mshot;
- }
- if (HAS_BIT(root->ses->event_flags, EVENT_FLAG_VARIABLE))
- {
- arg = get_arg_to_brackets(root->ses, arg1, name);
- check_all_events(root->ses, EVENT_FLAG_VARIABLE, 1, 3, "VARIABLE UPDATED %s", name, name, arg2, arg1);
- if (strcmp(arg1, name))
- {
- check_all_events(root->ses, EVENT_FLAG_VARIABLE, 1, 3, "VARIABLE UPDATED %s", arg1, name, arg2, arg1);
- }
- }
- free(arg2);
- pop_call();
- return node;
- }
- void copy_nest_node(struct listroot *dst_root, struct listnode *dst, struct listnode *src)
- {
- int index;
- if (src->root == NULL)
- {
- return;
- }
- dst_root = dst->root = init_list(dst_root->ses, dst_root->type, src->root->size);
- for (index = 0 ; index < src->root->used ; index++)
- {
- 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);
- if (src->root->list[index]->root)
- {
- copy_nest_node(dst_root, dst, src->root->list[index]);
- }
- }
- }
|