| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229 |
- /******************************************************************************
- * 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 2008 *
- ******************************************************************************/
- #include "tintin.h"
- struct scriptdata
- {
- long long min;
- long long max;
- long long cnt;
- int inc;
- char * cpy;
- char * hlt;
- char * str;
- char * arg;
- };
- struct scriptnode
- {
- struct scriptnode * next;
- struct scriptnode * prev;
- union
- {
- struct scriptdata * data;
- struct script_regex * regex;
- };
- char * str;
- short lvl;
- short type;
- short cmd;
- };
- struct script_regex
- {
- char * str;
- char * bod;
- char * buf;
- int val;
- };
- void debugtoken(struct session *ses, struct scriptroot *root, struct scriptnode *token)
- {
- push_call("debugtoken(%p,%d,%p,%d)",ses,root->list,token,token->type);
- if (gtd->level->debug || HAS_BIT(root->ses->list[root->list]->flags, LIST_FLAG_DEBUG) || HAS_BIT(root->ses->list[root->list]->flags, LIST_FLAG_LOG))
- {
- switch (token->type)
- {
- case TOKEN_TYPE_STRING:
- case TOKEN_TYPE_SESSION:
- show_debug(ses, root->list, "[%02d] %s%s", token->type, indent(token->lvl), token->str);
- break;
- case TOKEN_TYPE_ELSE:
- case TOKEN_TYPE_END:
- show_debug(ses, root->list, "[%02d] %s" COLOR_STATEMENT "%s\e[0m", token->type, indent(token->lvl), token->str);
- break;
- case TOKEN_TYPE_DEFAULT:
- show_debug(ses, root->list, "[%02d] %s" COLOR_STATEMENT "%s\e[0m", token->type, indent(token->lvl), command_table[token->cmd].name);
- break;
- case TOKEN_TYPE_BREAK:
- case TOKEN_TYPE_CONTINUE:
- show_debug(ses, root->list, "[%02d] %s" COLOR_STATEMENT "%s\e[0m", token->type, indent(token->lvl), command_table[token->cmd].name);
- break;
- case TOKEN_TYPE_COMMAND:
- show_debug(ses, root->list, "[%02d] %s" COLOR_COMMAND "%s " COLOR_STRING "%s\e[0m", token->type, indent(token->lvl), command_table[token->cmd].name, token->str);
- break;
- case TOKEN_TYPE_RETURN:
- show_debug(ses, root->list, "[%02d] %s" COLOR_STATEMENT "%s " COLOR_STRING "%s\e[0m", token->type, indent(token->lvl), command_table[token->cmd].name, token->str);
- break;
- case TOKEN_TYPE_CASE:
- case TOKEN_TYPE_ELSEIF:
- case TOKEN_TYPE_IF:
- case TOKEN_TYPE_WHILE:
- show_debug(ses, root->list, "[%02d] %s" COLOR_STATEMENT "%s " COLOR_BRACE "{" COLOR_STRING "%s" COLOR_BRACE "}\e[0m", token->type, indent(token->lvl), command_table[token->cmd].name, token->str);
- break;
- case TOKEN_TYPE_FOREACH:
- case TOKEN_TYPE_LOOP:
- case TOKEN_TYPE_PARSE:
- case TOKEN_TYPE_SWITCH:
- show_debug(ses, root->list, "[%02d] %s" COLOR_STATEMENT "%s " COLOR_STRING "%s\e[0m", token->type, indent(token->lvl), command_table[token->cmd].name, token->data->hlt);
- break;
- case TOKEN_TYPE_REGEX:
- show_debug(ses, root->list, "[%02d] %s" COLOR_STATEMENT "%s " COLOR_BRACE "{" COLOR_STRING "%s" COLOR_BRACE "} {" COLOR_STRING "%s" COLOR_BRACE "}\e[0m", token->type, indent(token->lvl), command_table[token->cmd].name, token->str, token->regex->str);
- break;
- default:
- if (token == (struct scriptnode *) ses)
- {
- show_debug(ses, root->list, "[--] (error) token == ses");
- }
- else
- {
- show_debug(ses, root->list, "[%02d] %s\e[1;33m%d {\e[0m%s\e[1;32m}\e[0m", token->type, indent(token->lvl), token->cmd, token->str);
- }
- break;
- }
- }
- pop_call();
- return;
- }
- void addtoken(struct scriptroot *root, int lvl, int opr, int cmd, char *str)
- {
- struct scriptnode *token;
- token = (struct scriptnode *) calloc(1, sizeof(struct scriptnode));
- token->lvl = lvl;
- token->type = opr;
- token->cmd = cmd;
- token->str = strdup(str);
- LINK(token, root->next, root->prev);
- }
- char *addlooptoken(struct scriptroot *root, int lvl, int opr, int cmd, char *str)
- {
- struct scriptdata *data;
- char min[BUFFER_SIZE], max[BUFFER_SIZE], var[BUFFER_SIZE];
- data = (struct scriptdata *) calloc(1, sizeof(struct scriptdata));
- str = get_arg_in_braces(root->ses, str, min, GET_ONE);
- str = get_arg_in_braces(root->ses, str, max, GET_ONE);
- str = get_arg_in_braces(root->ses, str, var, GET_ONE);
- data->cpy = restringf(NULL, "{%s} {%s} {%s}", min, max, var);
- data->hlt = restringf(NULL, COLOR_BRACE "{" COLOR_STRING "%s" COLOR_BRACE "} {" COLOR_STRING "%s" COLOR_BRACE "} {" COLOR_STRING "%s" COLOR_BRACE "}", min, max, var);
- addtoken(root, lvl, opr, cmd, var);
- data->str = strdup("");
- root->prev->data = data;
- return str;
- }
- char *addswitchtoken(struct scriptroot *root, int lvl, int opr, int cmd, char *str)
- {
- struct scriptdata *data;
- char arg[BUFFER_SIZE];
- data = (struct scriptdata *) calloc(1, sizeof(struct scriptdata));
- str = get_arg_in_braces(root->ses, str, arg, GET_ONE);
- data->cpy = restringf(NULL, "{%s}", arg);
- data->hlt = restringf(NULL, COLOR_BRACE "{" COLOR_STRING "%s" COLOR_BRACE "}", arg);
- addtoken(root, lvl, opr, cmd, arg);
- data->str = strdup("");
- root->prev->data = data;
- return str;
- }
- void resetlooptoken(struct session *ses, struct scriptnode *token)
- {
- char *str, min[BUFFER_SIZE], max[BUFFER_SIZE];
- str = token->data->cpy;
- str = get_arg_in_braces(ses, str, min, GET_ONE);
- str = get_arg_in_braces(ses, str, max, GET_ONE);
- token->data->min = (int) get_number(ses, min);
- token->data->max = (int) get_number(ses, max);
- token->data->inc = token->data->min <= token->data->max ? 1 : -1;
- token->data->cnt = token->data->min;
- }
- void breaklooptoken(struct scriptnode *token)
- {
- token->data->min = token->data->max = token->data->cnt = token->data->inc = 0;
- }
- char *addforeachtoken(struct scriptroot *root, int lvl, int opr, int cmd, char *str)
- {
- struct scriptdata *data;
- char arg[BUFFER_SIZE], var[BUFFER_SIZE];
- str = get_arg_in_braces(root->ses, str, arg, GET_ONE);
- str = get_arg_in_braces(root->ses, str, var, GET_ONE);
- addtoken(root, lvl, opr, cmd, var);
- data = (struct scriptdata *) calloc(1, sizeof(struct scriptdata));
- data->cpy = restringf(NULL, "{%s} {%s}", arg, var);
- data->hlt = restringf(NULL, COLOR_BRACE "{" COLOR_STRING "%s" COLOR_BRACE "} {" COLOR_STRING "%s" COLOR_BRACE "}", arg, var);
- data->str = strdup("");
- data->arg = data->str;
- root->prev->data = data;
- return str;
- }
- void resetforeachtoken(struct session *ses, struct scriptnode *token)
- {
- char *str, arg[BUFFER_SIZE];
- str = token->data->cpy;
- str = sub_arg_in_braces(ses, str, arg, GET_ONE, SUB_VAR|SUB_FUN);
- RESTRING(token->data->str, arg);
- token->data->arg = token->data->str;
- }
- void breakforeachtoken(struct scriptnode *token)
- {
- RESTRING(token->data->str, "");
- token->data->arg = token->data->str;
- }
- void handlereturntoken(struct session *ses, struct scriptnode *token)
- {
- char arg[BUFFER_SIZE];
- substitute(ses, token->str, arg, SUB_VAR|SUB_FUN);
- set_nest_node_ses(ses, "result", "%s", arg);
- }
- void handleswitchtoken(struct session *ses, struct scriptnode *token)
- {
- char arg[BUFFER_SIZE];
- mathexp(ses, token->str, arg, 0);
- RESTRING(token->data->str, arg);
- }
- char *get_arg_foreach(struct scriptroot *root, struct scriptnode *token)
- {
- static char buf[BUFFER_SIZE];
- token->data->arg = get_arg_in_braces(root->ses, token->data->arg, buf, GET_ALL);
- if (*token->data->arg == COMMAND_SEPARATOR)
- {
- token->data->arg++;
- }
- return buf;
- }
- char *addparsetoken(struct scriptroot *root, int lvl, int opr, int cmd, char *str)
- {
- struct scriptdata *data;
- char arg[BUFFER_SIZE], var[BUFFER_SIZE];
- str = get_arg_in_braces(root->ses, str, arg, GET_ONE);
- str = get_arg_in_braces(root->ses, str, var, GET_ONE);
- addtoken(root, lvl, opr, cmd, var);
- data = (struct scriptdata *) calloc(1, sizeof(struct scriptdata));
- data->cpy = restringf(NULL, "{%s} {%s}", arg, var);
- data->hlt = restringf(NULL, COLOR_BRACE "{" COLOR_STRING "%s" COLOR_BRACE "} {" COLOR_STRING "%s" COLOR_BRACE "}", arg, var);
- data->str = strdup("");
- data->arg = data->str;
- root->prev->data = data;
- return str;
- }
- void resetparsetoken(struct session *ses, struct scriptnode *token)
- {
- char *str, arg[BUFFER_SIZE];
- str = token->data->cpy;
- str = sub_arg_in_braces(ses, str, arg, GET_ONE, SUB_VAR|SUB_FUN);
- RESTRING(token->data->str, arg);
- token->data->arg = token->data->str;
- }
- void breakparsetoken(struct scriptnode *token)
- {
- RESTRING(token->data->str, "");
- token->data->arg = token->data->str;
- }
- char *get_arg_parse(struct session *ses, struct scriptnode *token)
- {
- static char buf[5];
- if (HAS_BIT(ses->charset, CHARSET_FLAG_EUC) && is_euc_head(ses, token->data->arg))
- {
- token->data->arg += sprintf(buf, "%.*s", get_euc_size(ses, token->data->arg), token->data->arg);
- }
- else if (HAS_BIT(ses->charset, CHARSET_FLAG_UTF8) && is_utf8_head(token->data->arg))
- {
- token->data->arg += sprintf(buf, "%.*s", get_utf8_size(token->data->arg), token->data->arg);
- }
- else
- {
- token->data->arg += sprintf(buf, "%c", token->data->arg[0]);
- }
- return buf;
- }
- char *addregextoken(struct scriptroot *root, int lvl, int type, int cmd, char *str)
- {
- struct script_regex *regex;
- char arg1[BUFFER_SIZE], arg2[BUFFER_SIZE], arg3[BUFFER_SIZE];
- str = get_arg_in_braces(root->ses, str, arg1, GET_ONE);
- str = get_arg_in_braces(root->ses, str, arg2, GET_ONE);
- str = get_arg_in_braces(root->ses, str, arg3, GET_ALL);
- addtoken(root, lvl, type, cmd, arg1);
- regex = (struct script_regex *) calloc(1, sizeof(struct script_regex));
- regex->str = strdup(arg2);
- regex->bod = strdup(arg3);
- regex->buf = calloc(1, BUFFER_SIZE);
- root->prev->regex = regex;
- return str;
- }
- void deltoken(struct scriptroot *root, struct scriptnode *token)
- {
- push_call("deltoken(%p,%p)",root,token);
- UNLINK(token, root->next, root->prev);
- free(token->str);
- switch (token->type)
- {
- case TOKEN_TYPE_REGEX:
- free(token->regex->str);
- free(token->regex->bod);
- free(token->regex->buf);
- free(token->regex);
- break;
- case TOKEN_TYPE_LOOP:
- case TOKEN_TYPE_FOREACH:
- case TOKEN_TYPE_PARSE:
- case TOKEN_TYPE_SWITCH:
- free(token->data->cpy);
- free(token->data->hlt);
- free(token->data->str);
- free(token->data);
- break;
- }
- free(token);
- pop_call();
- return;
- }
- int find_command(char *command)
- {
- int cmd;
- if (find_session(command))
- {
- return -1;
- }
- if (isalpha((int) *command))
- {
- for (cmd = gtd->command_ref[tolower((int) *command) - 'a'] ; *command_table[cmd].name ; cmd++)
- {
- if (is_abbrev(command, command_table[cmd].name))
- {
- return cmd;
- }
- }
- }
- return -1;
- }
- void init_local(struct session *ses)
- {
- struct scriptroot *root;
- root = (struct scriptroot *) calloc(1, sizeof(struct scriptroot));
- root->ses = ses;
- root->list = LIST_VARIABLE;
- root->local = init_list(ses, LIST_VARIABLE, LIST_SIZE);
- gtd->script_stack[0] = root;
- return;
- }
- struct listroot *local_list(struct session *ses)
- {
- struct listroot *root;
- push_call("local_list(%p)",ses);
- root = gtd->script_stack[gtd->script_index]->local;
- pop_call();
- return root;
- }
- void tokenize_script(struct scriptroot *root, int lvl, char *str)
- {
- char *arg, *line;
- int cmd;
- if (*str == 0)
- {
- addtoken(root, lvl, TOKEN_TYPE_STRING, -1, "");
- return;
- }
- line = (char *) calloc(1, UMAX(BUFFER_SIZE, strlen(str)));
- while (*str)
- {
- if (!VERBATIM(root->ses))
- {
- str = space_out(str);
- }
- if (*str != gtd->tintin_char)
- {
- str = get_arg_all(root->ses, str, line, VERBATIM(root->ses));
- addtoken(root, lvl, TOKEN_TYPE_STRING, -1, line);
- }
- else
- {
- arg = get_arg_stop_spaces(root->ses, str, line, 0);
- cmd = find_command(line+1);
- if (cmd == -1)
- {
- str = get_arg_all(root->ses, str, line, 0);
- addtoken(root, lvl, TOKEN_TYPE_SESSION, -1, line+1);
- }
- else
- {
- switch (command_table[cmd].type)
- {
- case TOKEN_TYPE_BREAK:
- str = get_arg_with_spaces(root->ses, arg, line, 1);
- addtoken(root, lvl, TOKEN_TYPE_BREAK, cmd, line);
- break;
- case TOKEN_TYPE_CASE:
- str = get_arg_in_braces(root->ses, arg, line, GET_ONE);
- addtoken(root, lvl++, TOKEN_TYPE_CASE, cmd, line);
- str = get_arg_in_braces(root->ses, str, line, GET_ALL);
- tokenize_script(root, lvl--, line);
- addtoken(root, lvl, TOKEN_TYPE_END, -1, "endcase");
- break;
- case TOKEN_TYPE_CONTINUE:
- str = get_arg_with_spaces(root->ses, arg, line, 1);
- addtoken(root, lvl, TOKEN_TYPE_CONTINUE, cmd, line);
- break;
- case TOKEN_TYPE_DEFAULT:
- addtoken(root, lvl++, TOKEN_TYPE_DEFAULT, cmd, "");
- str = get_arg_in_braces(root->ses, arg, line, GET_ALL);
- tokenize_script(root, lvl--, line);
- addtoken(root, lvl, TOKEN_TYPE_END, -1, "enddefault");
- break;
- case TOKEN_TYPE_ELSE:
- addtoken(root, lvl++, TOKEN_TYPE_ELSE, cmd, "else");
- str = get_arg_in_braces(root->ses, arg, line, GET_ALL);
- tokenize_script(root, lvl--, line);
- addtoken(root, lvl, TOKEN_TYPE_END, -1, "endelse");
- break;
- case TOKEN_TYPE_ELSEIF:
- str = get_arg_in_braces(root->ses, arg, line, GET_ONE);
- addtoken(root, lvl++, TOKEN_TYPE_ELSEIF, cmd, line);
- str = get_arg_in_braces(root->ses, str, line, GET_ALL);
- tokenize_script(root, lvl--, line);
- addtoken(root, lvl, TOKEN_TYPE_END, -1, "endif");
- break;
- case TOKEN_TYPE_FOREACH:
- str = addforeachtoken(root, lvl++, TOKEN_TYPE_FOREACH, cmd, arg);
- str = get_arg_in_braces(root->ses, str, line, GET_ALL);
- tokenize_script(root, lvl--, line);
- addtoken(root, lvl, TOKEN_TYPE_END, -1, "endforeach");
- break;
- case TOKEN_TYPE_IF:
- str = get_arg_in_braces(root->ses, arg, line, GET_ONE);
- addtoken(root, lvl++, TOKEN_TYPE_IF, cmd, line);
- str = get_arg_in_braces(root->ses, str, line, GET_ALL);
- tokenize_script(root, lvl--, line);
- addtoken(root, lvl, TOKEN_TYPE_END, -1, "endif");
- if (*str && *str != COMMAND_SEPARATOR)
- {
- addtoken(root, lvl++, TOKEN_TYPE_ELSE, -1, "else");
- str = get_arg_in_braces(root->ses, str, line, GET_ALL);
- tokenize_script(root, lvl--, line);
- addtoken(root, lvl, TOKEN_TYPE_END, -1, "endif");
- }
- break;
- case TOKEN_TYPE_LOOP:
- str = addlooptoken(root, lvl++, TOKEN_TYPE_LOOP, cmd, arg);
- str = get_arg_in_braces(root->ses, str, line, GET_ALL);
- tokenize_script(root, lvl--, line);
- addtoken(root, lvl, TOKEN_TYPE_END, -1, "endloop");
- break;
- case TOKEN_TYPE_PARSE:
- str = addparsetoken(root, lvl++, TOKEN_TYPE_PARSE, cmd, arg);
- str = get_arg_in_braces(root->ses, str, line, GET_ALL);
- tokenize_script(root, lvl--, line);
- addtoken(root, lvl, TOKEN_TYPE_END, -1, "endparse");
- break;
- case TOKEN_TYPE_REGEX:
- str = addregextoken(root, lvl, TOKEN_TYPE_REGEX, cmd, arg);
- // addtoken(root, --lvl, TOKEN_TYPE_END, -1, "endregex");
- if (*str && *str != COMMAND_SEPARATOR)
- {
- addtoken(root, lvl++, TOKEN_TYPE_ELSE, -1, "else");
- str = get_arg_in_braces(root->ses, str, line, GET_ALL);
- tokenize_script(root, lvl--, line);
- addtoken(root, lvl, TOKEN_TYPE_END, -1, "endregex");
- }
- break;
- case TOKEN_TYPE_RETURN:
- str = get_arg_in_braces(root->ses, arg, line, GET_ALL);
- addtoken(root, lvl, TOKEN_TYPE_RETURN, cmd, line);
- break;
- case TOKEN_TYPE_SWITCH:
- str = addswitchtoken(root, lvl++, TOKEN_TYPE_SWITCH, cmd, arg);
- str = get_arg_in_braces(root->ses, str, line, GET_ALL);
- tokenize_script(root, lvl--, line);
- addtoken(root, lvl, TOKEN_TYPE_END, -1, "end");
- break;
- case TOKEN_TYPE_WHILE:
- str = get_arg_in_braces(root->ses, arg, line, GET_ONE);
- addtoken(root, lvl++, TOKEN_TYPE_WHILE, cmd, line);
- str = get_arg_in_braces(root->ses, str, line, GET_ALL);
- tokenize_script(root, lvl--, line);
- addtoken(root, lvl, TOKEN_TYPE_END, -1, "endwhile");
- break;
- default:
- str = get_arg_with_spaces(root->ses, arg, line, GET_ALL);
- addtoken(root, lvl, TOKEN_TYPE_COMMAND, cmd, line);
- break;
- }
- }
- }
- str = space_out(str);
- if (*str == COMMAND_SEPARATOR)
- {
- str++;
- }
- }
- free(line);
- }
- struct scriptnode *parse_script(struct scriptroot *root, int lvl, struct scriptnode *token, struct scriptnode *shift)
- {
- struct scriptnode *split = NULL;
- while (token)
- {
- if (token->lvl < lvl)
- {
- if (shift->lvl + 1 == lvl)
- {
- switch (shift->type)
- {
- case TOKEN_TYPE_FOREACH:
- case TOKEN_TYPE_LOOP:
- case TOKEN_TYPE_PARSE:
- case TOKEN_TYPE_WHILE:
- debugtoken(root->ses, root, token);
- return shift;
- case TOKEN_TYPE_BROKEN_FOREACH:
- case TOKEN_TYPE_BROKEN_LOOP:
- case TOKEN_TYPE_BROKEN_PARSE:
- case TOKEN_TYPE_BROKEN_WHILE:
- shift->type--;
- return token;
- }
- }
- return token;
- }
- debugtoken(root->ses, root, token);
- switch (token->type)
- {
- case TOKEN_TYPE_BREAK:
- switch (shift->type)
- {
- case TOKEN_TYPE_FOREACH:
- breakforeachtoken(shift);
- shift->type++;
- break;
- case TOKEN_TYPE_LOOP:
- breaklooptoken(shift);
- shift->type++;
- break;
- case TOKEN_TYPE_PARSE:
- breakparsetoken(shift);
- shift->type++;
- break;
- case TOKEN_TYPE_WHILE:
- shift->type++;
- break;
- }
- do
- {
- token = token->next;
- }
- while (token && token->lvl > shift->lvl);
- continue;
- case TOKEN_TYPE_CASE:
- if (shift->data && mathswitch(root->ses, shift->data->str, token->str))
- {
- token = token->next;
- token = parse_script(root, lvl + 1, token, shift);
- while (token && token->lvl >= lvl)
- {
- token = token->next;
- }
- }
- else
- {
- do
- {
- token = token->next;
- }
- while (token && token->lvl > lvl);
- }
- continue;
- case TOKEN_TYPE_COMMAND:
- push_call("do_%s(%p,%p)", command_table[token->cmd].name, root->ses, token->str);
- root->ses = (*command_table[token->cmd].command) (root->ses, token->str);
- pop_call();
- /*
- return;
- }
- */
- break;
- case TOKEN_TYPE_CONTINUE:
- do
- {
- token = token->next;
- }
- while (token && token->lvl > shift->lvl);
- continue;
- case TOKEN_TYPE_DEFAULT:
- token = token->next;
- token = parse_script(root, lvl + 1, token, shift);
- while (token && token->lvl >= lvl)
- {
- token = token->next;
- }
- continue;
- case TOKEN_TYPE_ELSE:
- if (split)
- {
- token = parse_script(root, lvl + 1, token->next, shift);
- split = NULL;
- }
- else
- {
- do
- {
- token = token->next;
- }
- while (token && token->lvl > lvl);
- }
- continue;
- case TOKEN_TYPE_ELSEIF:
- if (split && get_number(root->ses, token->str))
- {
- token = parse_script(root, lvl + 1, token->next, shift);
- split = NULL;
- }
- else
- {
- do
- {
- token = token->next;
- }
- while (token && token->lvl > lvl);
- }
- continue;
- case TOKEN_TYPE_END:
- break;
- case TOKEN_TYPE_FOREACH:
- if (*token->data->arg == 0)
- {
- resetforeachtoken(root->ses, token);
- }
- if (*token->data->arg == 0)
- {
- token->type++;
- do
- {
- token = token->next;
- }
- while (token && token->lvl > lvl);
- }
- else
- {
- set_nest_node_ses(root->ses, token->str, "%s", get_arg_foreach(root, token));
- if (*token->data->arg == 0)
- {
- token->type++;
- }
- token = parse_script(root, lvl + 1, token->next, token);
- }
- continue;
- case TOKEN_TYPE_IF:
- split = NULL;
- if (get_number(root->ses, token->str))
- {
- token = parse_script(root, lvl + 1, token->next, shift);
- }
- else
- {
- split = token;
- do
- {
- token = token->next;
- }
- while (token && token->lvl > lvl);
- }
- continue;
- case TOKEN_TYPE_LOOP:
- if (token->data->cnt == token->data->max + token->data->inc)
- {
- resetlooptoken(root->ses, token);
- }
- set_nest_node_ses(root->ses, token->str, "%lld", token->data->cnt);
- token->data->cnt += token->data->inc;
- if (token->data->cnt == token->data->max + token->data->inc)
- {
- token->type++;
- }
- token = parse_script(root, lvl + 1, token->next, token);
- continue;
- case TOKEN_TYPE_PARSE:
- if (*token->data->arg == 0)
- {
- resetparsetoken(root->ses, token);
- if (*token->data->arg == 0)
- {
- token->type++;
- do
- {
- token = token->next;
- }
- while (token && token->lvl > lvl);
- continue;
- }
- }
- set_nest_node_ses(root->ses, token->str, "%s", get_arg_parse(root->ses, token));
- if (*token->data->arg == 0)
- {
- token->type++;
- }
- token = parse_script(root, lvl + 1, token->next, token);
- continue;
- case TOKEN_TYPE_REGEX:
- split = NULL;
- token->regex->val = find(root->ses, token->str, token->regex->str, SUB_VAR|SUB_FUN, REGEX_FLAG_CMD);
- if (token->regex->val)
- {
- substitute(root->ses, token->regex->bod, token->regex->buf, SUB_CMD);
- root->ses = script_driver(root->ses, LIST_COMMAND, token->regex->buf);
- }
- else
- {
- split = token;
- }
- break;
- case TOKEN_TYPE_RETURN:
- handlereturntoken(root->ses, token);
- if (lvl)
- {
- return NULL;
- }
- else
- {
- return (struct scriptnode *) root->ses;
- }
- break;
- case TOKEN_TYPE_SESSION:
- root->ses = parse_tintin_command(root->ses, token->str);
- break;
- case TOKEN_TYPE_STRING:
- root->ses = parse_input(root->ses, token->str);
- break;
- case TOKEN_TYPE_SWITCH:
- handleswitchtoken(root->ses, token);
- token = parse_script(root, lvl + 1, token->next, token);
- continue;
- case TOKEN_TYPE_WHILE:
- if (get_number(root->ses, token->str))
- {
- token = parse_script(root, lvl + 1, token->next, token);
- }
- else
- {
- // token->type++;
- do
- {
- token = token->next;
- }
- while (token && token->lvl > lvl);
- }
- continue;
- }
- if (token)
- {
- token = token->next;
- }
- }
- if (lvl)
- {
- return NULL;
- }
- return (struct scriptnode *) root->ses;
- }
- char *write_script(struct session *ses, struct scriptroot *root)
- {
- struct scriptnode *token;
- static char buf[STRING_SIZE];
- token = root->next;
- buf[0] = 0;
- while (token)
- {
- switch (token->type)
- {
- case TOKEN_TYPE_STRING:
- cat_sprintf(buf, "%s%s", indent(token->lvl), token->str);
- break;
- case TOKEN_TYPE_BREAK:
- case TOKEN_TYPE_CONTINUE:
- cat_sprintf(buf, "%s%c%s", indent(token->lvl), gtd->tintin_char, command_table[token->cmd].name);
- break;
- case TOKEN_TYPE_COMMAND:
- case TOKEN_TYPE_RETURN:
- cat_sprintf(buf, "%s%c%s%s%s", indent(token->lvl), gtd->tintin_char, command_table[token->cmd].name, *token->str ? " " : "", token->str);
- break;
- case TOKEN_TYPE_ELSE:
- cat_sprintf(buf, "%s%c%s\n%s{\n", indent(token->lvl), gtd->tintin_char, token->str, indent(token->lvl));
- break;
- case TOKEN_TYPE_DEFAULT:
- cat_sprintf(buf, "%s%c%s\n%s{\n", indent(token->lvl), gtd->tintin_char, command_table[token->cmd].name, indent(token->lvl));
- break;
- case TOKEN_TYPE_FOREACH:
- case TOKEN_TYPE_LOOP:
- case TOKEN_TYPE_PARSE:
- case TOKEN_TYPE_SWITCH:
- cat_sprintf(buf, "%s%c%s %s\n%s{\n", indent(token->lvl), gtd->tintin_char, command_table[token->cmd].name, token->data->cpy, indent(token->lvl));
- break;
- case TOKEN_TYPE_CASE:
- case TOKEN_TYPE_ELSEIF:
- case TOKEN_TYPE_IF:
- case TOKEN_TYPE_WHILE:
- cat_sprintf(buf, "%s%c%s {%s}\n%s{\n", indent(token->lvl), gtd->tintin_char, command_table[token->cmd].name, token->str, indent(token->lvl));
- break;
- case TOKEN_TYPE_REGEX:
- cat_sprintf(buf, "%s%c%s {%s} {%s}\n%s{\n%s%s\n%s}", indent(token->lvl), gtd->tintin_char, command_table[token->cmd].name, token->str, token->regex->str, indent(token->lvl), indent(token->lvl + 1), token->regex->bod, indent(token->lvl));
- break;
- case TOKEN_TYPE_END:
- cat_sprintf(buf, "\n%s}", indent(token->lvl));
- break;
- case TOKEN_TYPE_SESSION:
- cat_sprintf(buf, "%s%c%s", indent(token->lvl), gtd->tintin_char, token->str);
- break;
- default:
- tintin_printf2(ses, "#WRITE: UNKNOWN TOKEN TYPE: %d", token->type);
- break;
- }
- if (token->next && token->lvl == token->next->lvl)
- {
- strcat(buf, ";\n");
- }
- token = token->next;
- }
- while (root->next)
- {
- deltoken(root, root->next);
- }
- free(root);
- return buf;
- }
- char *view_script(struct session *ses, struct scriptroot *root)
- {
- struct scriptnode *token;
- static char buf[STRING_SIZE];
- token = root->next;
- buf[0] = 0;
- while (token)
- {
- switch (token->type)
- {
- case TOKEN_TYPE_STRING:
- cat_sprintf(buf, "%s" COLOR_STRING "%s", indent(token->lvl), token->str);
- break;
- case TOKEN_TYPE_BREAK:
- case TOKEN_TYPE_CONTINUE:
- cat_sprintf(buf, "%s" COLOR_TINTIN "%c" COLOR_STATEMENT "%s", indent(token->lvl), gtd->tintin_char, command_table[token->cmd].name);
- break;
- case TOKEN_TYPE_RETURN:
- cat_sprintf(buf, "%s" COLOR_TINTIN "%c" COLOR_STATEMENT "%s" COLOR_STRING "%s%s", indent(token->lvl), gtd->tintin_char, command_table[token->cmd].name, *token->str ? " " : "", token->str);
- break;
- case TOKEN_TYPE_COMMAND:
- cat_sprintf(buf, "%s" COLOR_TINTIN "%c" COLOR_COMMAND "%s" COLOR_STRING "%s%s", indent(token->lvl), gtd->tintin_char, command_table[token->cmd].name, *token->str ? " " : "", token->str);
- break;
- case TOKEN_TYPE_ELSE:
- cat_sprintf(buf, "%s" COLOR_TINTIN "%c" COLOR_STATEMENT "%s\n%s" COLOR_BRACE "{\n", indent(token->lvl), gtd->tintin_char, token->str, indent(token->lvl));
- break;
- case TOKEN_TYPE_DEFAULT:
- cat_sprintf(buf, "%s" COLOR_TINTIN "%c" COLOR_STATEMENT "%s\n%s" COLOR_BRACE "{\n", indent(token->lvl), gtd->tintin_char, command_table[token->cmd].name, indent(token->lvl));
- break;
- case TOKEN_TYPE_FOREACH:
- case TOKEN_TYPE_LOOP:
- case TOKEN_TYPE_PARSE:
- case TOKEN_TYPE_SWITCH:
- cat_sprintf(buf, "%s" COLOR_TINTIN "%c" COLOR_STATEMENT "%s " COLOR_STRING "%s\n%s" COLOR_BRACE "{\n", indent(token->lvl), gtd->tintin_char, command_table[token->cmd].name, token->data->hlt, indent(token->lvl));
- break;
- case TOKEN_TYPE_CASE:
- case TOKEN_TYPE_ELSEIF:
- case TOKEN_TYPE_IF:
- case TOKEN_TYPE_WHILE:
- cat_sprintf(buf, "%s" COLOR_TINTIN "%c" COLOR_STATEMENT "%s " COLOR_BRACE "{" COLOR_STRING "%s" COLOR_BRACE "}" COLOR_STRING "\n%s" COLOR_BRACE "{\n", indent(token->lvl), gtd->tintin_char, command_table[token->cmd].name, token->str, indent(token->lvl));
- break;
- case TOKEN_TYPE_REGEX:
- cat_sprintf(buf, "%s" COLOR_TINTIN "%c" COLOR_STATEMENT "%s " COLOR_BRACE "{" COLOR_STRING "%s" COLOR_BRACE "} {" COLOR_STRING "%s" COLOR_BRACE "}\n%s{\n%s" COLOR_STRING "%s\n" COLOR_BRACE "%s}", indent(token->lvl), gtd->tintin_char, command_table[token->cmd].name, token->str, token->regex->str, indent(token->lvl), indent(token->lvl + 1), token->regex->bod, indent(token->lvl));
- break;
- case TOKEN_TYPE_END:
- cat_sprintf(buf, "\n%s" COLOR_BRACE "}" COLOR_STRING, indent(token->lvl));
- break;
- case TOKEN_TYPE_SESSION:
- cat_sprintf(buf, "%s" COLOR_TINTIN "%c" COLOR_STRING "%s", indent(token->lvl), gtd->tintin_char, token->str);
- break;
- default:
- tintin_printf2(ses, "#ERROR: UNKNOWN TOKEN TYPE: %d", token->type);
- break;
- }
- if (token->next && token->lvl == token->next->lvl)
- {
- strcat(buf, COLOR_SEPARATOR ";\n");
- }
- token = token->next;
- }
- while (root->next)
- {
- deltoken(root, root->next);
- }
- free(root);
- return buf;
- }
- struct session *script_driver(struct session *ses, int list, char *str)
- {
- struct scriptroot *root;
- push_call("script_driver(%p,%d,%p)",ses,list,str);
- root = (struct scriptroot *) calloc(1, sizeof(struct scriptroot));
- root->ses = ses;
- root->list = list;
- root->local = init_list(ses, LIST_VARIABLE, LIST_SIZE);
- gtd->level->input += list != LIST_COMMAND;
- gtd->script_stack[++gtd->script_index] = root;
- tokenize_script(root, 0, str);
- ses = (struct session *) parse_script(root, 0, root->next, root->prev);
- if (--gtd->script_index == 0)
- {
- DEL_BIT(gtd->flags, TINTIN_FLAG_LOCAL);
- }
- gtd->level->input -= list != LIST_COMMAND;
- while (root->prev)
- {
- deltoken(root, root->prev);
- }
- free_list(root->local);
- free(root);
-
- if (HAS_BIT(ses->flags, SES_FLAG_CLOSED))
- {
- pop_call();
- return gtd->ses;
- }
- pop_call();
- return ses;
- }
- char *script_writer(struct session *ses, char *str)
- {
- struct scriptroot *root;
- root = (struct scriptroot *) calloc(1, sizeof(struct scriptroot));
- root->ses = ses;
- tokenize_script(root, 1, str);
- return write_script(ses, root);
- }
- char *script_viewer(struct session *ses, char *str)
- {
- struct scriptroot *root;
- root = (struct scriptroot *) calloc(1, sizeof(struct scriptroot));
- root->ses = ses;
- tokenize_script(root, 1, str);
- return view_script(ses, root);
- }
|