tokenize.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287
  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 2008 *
  23. ******************************************************************************/
  24. #include "tintin.h"
  25. extern struct command_type command_table[];
  26. struct scriptnode
  27. {
  28. struct scriptnode * next;
  29. struct scriptnode * prev;
  30. union
  31. {
  32. struct scriptdata * data;
  33. struct script_regex * regex;
  34. };
  35. char * str;
  36. short lvl;
  37. short type;
  38. short cmd;
  39. };
  40. struct scriptdata
  41. {
  42. long long min;
  43. long long max;
  44. long long cnt;
  45. int inc;
  46. char * cpy;
  47. char * hlt;
  48. char * str;
  49. char * arg;
  50. };
  51. struct script_regex
  52. {
  53. char * str;
  54. char * bod;
  55. char * buf;
  56. int val;
  57. };
  58. void debugtoken(struct session *ses, struct scriptroot *root, struct scriptnode *token)
  59. {
  60. push_call("debugtoken(%p,%d,%p,%d)",ses,root->list,token,token->type);
  61. 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))
  62. {
  63. switch (token->type)
  64. {
  65. case TOKEN_TYPE_REPEAT:
  66. show_debug(ses, root->list, "%s" COLOR_REPEAT "!\e[0m%s", indent(token->lvl + 1), token->str);
  67. break;
  68. case TOKEN_TYPE_STRING:
  69. case TOKEN_TYPE_SESSION:
  70. show_debug(ses, root->list, "%s%s", indent(token->lvl + 1), token->str);
  71. break;
  72. case TOKEN_TYPE_ELSE:
  73. case TOKEN_TYPE_END:
  74. show_debug(ses, root->list, "%s" COLOR_TINTIN "%c" COLOR_STATEMENT "%s\e[0m", indent(token->lvl + 1), gtd->tintin_char, token->str);
  75. break;
  76. case TOKEN_TYPE_DEFAULT:
  77. show_debug(ses, root->list, "%s" COLOR_TINTIN "%c" COLOR_STATEMENT "%s\e[0m", indent(token->lvl + 1), gtd->tintin_char, command_table[token->cmd].name);
  78. break;
  79. case TOKEN_TYPE_BREAK:
  80. case TOKEN_TYPE_CONTINUE:
  81. show_debug(ses, root->list, "%s" COLOR_TINTIN "%c" COLOR_STATEMENT "%s\e[0m", indent(token->lvl + 1), gtd->tintin_char, command_table[token->cmd].name);
  82. break;
  83. case TOKEN_TYPE_COMMAND:
  84. show_debug(ses, root->list, "%s" COLOR_TINTIN "%c" COLOR_COMMAND "%s " COLOR_STRING "%s\e[0m", indent(token->lvl + 1), gtd->tintin_char, command_table[token->cmd].name, token->str);
  85. break;
  86. case TOKEN_TYPE_RETURN:
  87. show_debug(ses, root->list, "%s" COLOR_TINTIN "%c" COLOR_STATEMENT "%s " COLOR_STRING "%s\e[0m", indent(token->lvl + 1), gtd->tintin_char, command_table[token->cmd].name, token->str);
  88. break;
  89. case TOKEN_TYPE_CASE:
  90. case TOKEN_TYPE_ELSEIF:
  91. case TOKEN_TYPE_IF:
  92. case TOKEN_TYPE_WHILE:
  93. show_debug(ses, root->list, "%s" COLOR_TINTIN "%c" COLOR_STATEMENT "%s " COLOR_BRACE "{" COLOR_STRING "%s" COLOR_BRACE "}\e[0m", indent(token->lvl + 1), gtd->tintin_char, command_table[token->cmd].name, token->str);
  94. break;
  95. case TOKEN_TYPE_FOREACH:
  96. case TOKEN_TYPE_LOOP:
  97. case TOKEN_TYPE_PARSE:
  98. case TOKEN_TYPE_SWITCH:
  99. show_debug(ses, root->list, "%s" COLOR_TINTIN "%c" COLOR_STATEMENT "%s " COLOR_STRING "%s\e[0m", indent(token->lvl + 1), gtd->tintin_char, command_table[token->cmd].name, token->data->hlt);
  100. break;
  101. case TOKEN_TYPE_REGEX:
  102. show_debug(ses, root->list, "%s" COLOR_TINTIN "%c" COLOR_STATEMENT "%s " COLOR_BRACE "{" COLOR_STRING "%s" COLOR_BRACE "} {" COLOR_STRING "%s" COLOR_BRACE "}\e[0m", indent(token->lvl + 1), gtd->tintin_char, command_table[token->cmd].name, token->str, token->regex->str);
  103. break;
  104. default:
  105. if (token == (struct scriptnode *) ses)
  106. {
  107. show_debug(ses, root->list, "[--] (error) token == ses");
  108. }
  109. else
  110. {
  111. show_debug(ses, root->list, "[%02d] %s\e[1;33m%d {\e[0m%s\e[1;32m}\e[0m", token->type, indent(token->lvl + 1), token->cmd, token->str);
  112. }
  113. break;
  114. }
  115. }
  116. pop_call();
  117. return;
  118. }
  119. void addtoken(struct scriptroot *root, int lvl, int opr, int cmd, char *str)
  120. {
  121. struct scriptnode *token;
  122. token = (struct scriptnode *) calloc(1, sizeof(struct scriptnode));
  123. token->lvl = lvl;
  124. token->type = opr;
  125. token->cmd = cmd;
  126. token->str = strdup(str);
  127. LINK(token, root->next, root->prev);
  128. }
  129. char *addlooptoken(struct scriptroot *root, int lvl, int opr, int cmd, char *str)
  130. {
  131. struct scriptdata *data;
  132. char min[BUFFER_SIZE], max[BUFFER_SIZE], var[BUFFER_SIZE];
  133. data = (struct scriptdata *) calloc(1, sizeof(struct scriptdata));
  134. str = get_arg_in_braces(root->ses, str, min, GET_ONE);
  135. str = get_arg_in_braces(root->ses, str, max, GET_ONE);
  136. str = get_arg_in_braces(root->ses, str, var, GET_ONE);
  137. data->cpy = restringf(NULL, "{%s} {%s} {%s}", min, max, var);
  138. data->hlt = restringf(NULL, COLOR_BRACE "{" COLOR_STRING "%s" COLOR_BRACE "} {" COLOR_STRING "%s" COLOR_BRACE "} {" COLOR_STRING "%s" COLOR_BRACE "}", min, max, var);
  139. addtoken(root, lvl, opr, cmd, var);
  140. data->str = strdup("");
  141. root->prev->data = data;
  142. return str;
  143. }
  144. char *addswitchtoken(struct scriptroot *root, int lvl, int opr, int cmd, char *str)
  145. {
  146. struct scriptdata *data;
  147. char arg[BUFFER_SIZE];
  148. data = (struct scriptdata *) calloc(1, sizeof(struct scriptdata));
  149. str = get_arg_in_braces(root->ses, str, arg, GET_ONE);
  150. data->cpy = restringf(NULL, "{%s}", arg);
  151. data->hlt = restringf(NULL, COLOR_BRACE "{" COLOR_STRING "%s" COLOR_BRACE "}", arg);
  152. addtoken(root, lvl, opr, cmd, arg);
  153. data->str = strdup("");
  154. root->prev->data = data;
  155. return str;
  156. }
  157. void resetlooptoken(struct session *ses, struct scriptnode *token)
  158. {
  159. char *str, min[BUFFER_SIZE], max[BUFFER_SIZE];
  160. str = token->data->cpy;
  161. str = get_arg_in_braces(ses, str, min, GET_ONE);
  162. str = get_arg_in_braces(ses, str, max, GET_ONE);
  163. token->data->min = (int) get_number(ses, min);
  164. token->data->max = (int) get_number(ses, max);
  165. token->data->inc = token->data->min <= token->data->max ? 1 : -1;
  166. token->data->cnt = token->data->min;
  167. }
  168. void breaklooptoken(struct scriptnode *token)
  169. {
  170. token->data->min = token->data->max = token->data->cnt = token->data->inc = 0;
  171. }
  172. char *addforeachtoken(struct scriptroot *root, int lvl, int opr, int cmd, char *str)
  173. {
  174. struct scriptdata *data;
  175. char *arg, var[BUFFER_SIZE];
  176. arg = str_dup(str);
  177. str = get_arg_in_braces(root->ses, str, arg, GET_ONE);
  178. str = get_arg_in_braces(root->ses, str, var, GET_ONE);
  179. addtoken(root, lvl, opr, cmd, var);
  180. data = (struct scriptdata *) calloc(1, sizeof(struct scriptdata));
  181. data->cpy = restringf(NULL, "{%s} {%s}", arg, var);
  182. data->hlt = restringf(NULL, COLOR_BRACE "{" COLOR_STRING "%s" COLOR_BRACE "} {" COLOR_STRING "%s" COLOR_BRACE "}", arg, var);
  183. data->str = strdup("");
  184. data->arg = data->str;
  185. root->prev->data = data;
  186. str_free(arg);
  187. return str;
  188. }
  189. void resetforeachtoken(struct session *ses, struct scriptnode *token)
  190. {
  191. char *str, arg[BUFFER_SIZE];
  192. str = token->data->cpy;
  193. str = sub_arg_in_braces(ses, str, arg, GET_ONE, SUB_VAR|SUB_FUN);
  194. RESTRING(token->data->str, arg);
  195. token->data->arg = token->data->str;
  196. }
  197. void breakforeachtoken(struct scriptnode *token)
  198. {
  199. RESTRING(token->data->str, "");
  200. token->data->arg = token->data->str;
  201. }
  202. void handlereturntoken(struct session *ses, struct scriptnode *token)
  203. {
  204. char arg[BUFFER_SIZE];
  205. substitute(ses, token->str, arg, SUB_VAR|SUB_FUN);
  206. if (gtd->script_index == 0)
  207. {
  208. set_nest_node_ses(ses, "result", "%s", arg);
  209. }
  210. else
  211. {
  212. DEL_BIT(gtd->flags, TINTIN_FLAG_LOCAL);
  213. set_nest_node(gtd->script_stack[gtd->script_index - 1]->local, "result", "%s", arg);
  214. SET_BIT(gtd->flags, TINTIN_FLAG_LOCAL);
  215. }
  216. }
  217. void handleswitchtoken(struct session *ses, struct scriptnode *token)
  218. {
  219. char arg[BUFFER_SIZE];
  220. mathexp(ses, token->str, arg, 0);
  221. RESTRING(token->data->str, arg);
  222. }
  223. char *get_arg_foreach(struct scriptroot *root, struct scriptnode *token)
  224. {
  225. static char buf[BUFFER_SIZE];
  226. token->data->arg = get_arg_in_braces(root->ses, token->data->arg, buf, GET_ALL);
  227. if (*token->data->arg == COMMAND_SEPARATOR)
  228. {
  229. token->data->arg++;
  230. }
  231. return buf;
  232. }
  233. char *addparsetoken(struct scriptroot *root, int lvl, int opr, int cmd, char *str)
  234. {
  235. struct scriptdata *data;
  236. char arg[BUFFER_SIZE], var[BUFFER_SIZE];
  237. str = get_arg_in_braces(root->ses, str, arg, GET_ONE);
  238. str = get_arg_in_braces(root->ses, str, var, GET_ONE);
  239. addtoken(root, lvl, opr, cmd, var);
  240. data = (struct scriptdata *) calloc(1, sizeof(struct scriptdata));
  241. data->cpy = restringf(NULL, "{%s} {%s}", arg, var);
  242. data->hlt = restringf(NULL, COLOR_BRACE "{" COLOR_STRING "%s" COLOR_BRACE "} {" COLOR_STRING "%s" COLOR_BRACE "}", arg, var);
  243. data->str = strdup("");
  244. data->arg = data->str;
  245. root->prev->data = data;
  246. return str;
  247. }
  248. void resetparsetoken(struct session *ses, struct scriptnode *token)
  249. {
  250. char *str, arg[BUFFER_SIZE];
  251. str = token->data->cpy;
  252. str = sub_arg_in_braces(ses, str, arg, GET_ONE, SUB_VAR|SUB_FUN);
  253. RESTRING(token->data->str, arg);
  254. token->data->arg = token->data->str;
  255. }
  256. void breakparsetoken(struct scriptnode *token)
  257. {
  258. RESTRING(token->data->str, "");
  259. token->data->arg = token->data->str;
  260. }
  261. char *get_arg_parse(struct session *ses, struct scriptnode *token)
  262. {
  263. static char buf[5];
  264. if (HAS_BIT(ses->charset, CHARSET_FLAG_EUC) && is_euc_head(ses, token->data->arg))
  265. {
  266. token->data->arg += sprintf(buf, "%.*s", get_euc_size(ses, token->data->arg), token->data->arg);
  267. }
  268. else if (HAS_BIT(ses->charset, CHARSET_FLAG_UTF8) && is_utf8_head(token->data->arg))
  269. {
  270. token->data->arg += sprintf(buf, "%.*s", get_utf8_size(token->data->arg), token->data->arg);
  271. }
  272. else
  273. {
  274. token->data->arg += sprintf(buf, "%c", token->data->arg[0]);
  275. }
  276. return buf;
  277. }
  278. char *addregextoken(struct scriptroot *root, int lvl, int type, int cmd, char *str)
  279. {
  280. struct script_regex *regex;
  281. char arg1[BUFFER_SIZE], arg2[BUFFER_SIZE], arg3[BUFFER_SIZE];
  282. str = get_arg_in_braces(root->ses, str, arg1, GET_ONE);
  283. str = get_arg_in_braces(root->ses, str, arg2, GET_ONE);
  284. str = get_arg_in_braces(root->ses, str, arg3, GET_ALL);
  285. addtoken(root, lvl, type, cmd, arg1);
  286. regex = (struct script_regex *) calloc(1, sizeof(struct script_regex));
  287. regex->str = strdup(arg2);
  288. regex->bod = strdup(arg3);
  289. regex->buf = calloc(1, BUFFER_SIZE);
  290. root->prev->regex = regex;
  291. return str;
  292. }
  293. void deltoken(struct scriptroot *root, struct scriptnode *token)
  294. {
  295. UNLINK(token, root->next, root->prev);
  296. free(token->str);
  297. switch (token->type)
  298. {
  299. case TOKEN_TYPE_REGEX:
  300. free(token->regex->str);
  301. free(token->regex->bod);
  302. free(token->regex->buf);
  303. free(token->regex);
  304. break;
  305. case TOKEN_TYPE_LOOP:
  306. case TOKEN_TYPE_FOREACH:
  307. case TOKEN_TYPE_PARSE:
  308. case TOKEN_TYPE_SWITCH:
  309. free(token->data->cpy);
  310. free(token->data->hlt);
  311. free(token->data->str);
  312. free(token->data);
  313. break;
  314. }
  315. free(token);
  316. }
  317. int find_command(char *command)
  318. {
  319. int cmd;
  320. if (find_session(command))
  321. {
  322. return -1;
  323. }
  324. if (is_alpha(*command) && command[1] != 0)
  325. {
  326. for (cmd = gtd->command_ref[*command % 32] ; *command_table[cmd].name ; cmd++)
  327. {
  328. if (is_abbrev(command, command_table[cmd].name))
  329. {
  330. return cmd;
  331. }
  332. }
  333. }
  334. return -1;
  335. }
  336. void init_local(struct session *ses)
  337. {
  338. struct scriptroot *root;
  339. root = (struct scriptroot *) calloc(1, sizeof(struct scriptroot));
  340. root->ses = ses;
  341. root->list = LIST_VARIABLE;
  342. root->local = init_list(ses, LIST_VARIABLE, LIST_SIZE);
  343. gtd->script_stack[0] = root;
  344. return;
  345. }
  346. struct listroot *local_list(struct session *ses)
  347. {
  348. struct listroot *root;
  349. root = gtd->script_stack[gtd->script_index]->local;
  350. return root;
  351. }
  352. struct scriptroot *push_script_stack(struct session *ses, int list)
  353. {
  354. struct scriptroot *root;
  355. root = (struct scriptroot *) calloc(1, sizeof(struct scriptroot));
  356. root->ses = ses;
  357. root->list = list;
  358. root->local = init_list(ses, LIST_VARIABLE, LIST_SIZE);
  359. gtd->script_stack[++gtd->script_index] = root;
  360. return root;
  361. }
  362. void pop_script_stack(void)
  363. {
  364. free_list(gtd->script_stack[gtd->script_index]->local);
  365. free(gtd->script_stack[gtd->script_index]);
  366. if (--gtd->script_index == 0)
  367. {
  368. if (HAS_BIT(gtd->flags, TINTIN_FLAG_LOCAL))
  369. {
  370. kill_list(gtd->script_stack[0]->local);
  371. DEL_BIT(gtd->flags, TINTIN_FLAG_LOCAL);
  372. }
  373. }
  374. }
  375. void tokenize_script(struct scriptroot *root, int lvl, char *str)
  376. {
  377. char *arg, *line;
  378. int cmd;
  379. push_call("tokenize_script(%p,%d,%p)",root,lvl,str);
  380. if (*str == 0)
  381. {
  382. addtoken(root, lvl, TOKEN_TYPE_STRING, -1, "");
  383. pop_call();
  384. return;
  385. }
  386. line = str_alloc_stack(strlen(str));
  387. while (*str)
  388. {
  389. if (!VERBATIM(root->ses))
  390. {
  391. str = space_out(str);
  392. }
  393. if (*str != gtd->tintin_char)
  394. {
  395. if (*str == gtd->repeat_char && gtd->level->repeat == 0)
  396. {
  397. str = get_arg_all(root->ses, str+1, line, VERBATIM(root->ses));
  398. addtoken(root, lvl, TOKEN_TYPE_REPEAT, -1, line);
  399. }
  400. else
  401. {
  402. str = get_arg_all(root->ses, str, line, VERBATIM(root->ses));
  403. addtoken(root, lvl, TOKEN_TYPE_STRING, -1, line);
  404. }
  405. }
  406. else
  407. {
  408. // arg = get_arg_in_braces(root->ses, str+1, line, GET_ONE);
  409. arg = sub_arg_in_braces(root->ses, str+1, line, GET_ONE, SUB_VAR|SUB_FUN);
  410. cmd = find_command(line);
  411. if (cmd == -1)
  412. {
  413. str = get_arg_all(root->ses, str, line, 0);
  414. addtoken(root, lvl, TOKEN_TYPE_SESSION, -1, line+1);
  415. }
  416. else
  417. {
  418. switch (command_table[cmd].type)
  419. {
  420. case TOKEN_TYPE_BREAK:
  421. str = get_arg_with_spaces(root->ses, arg, line, 1);
  422. addtoken(root, lvl, TOKEN_TYPE_BREAK, cmd, line);
  423. break;
  424. case TOKEN_TYPE_CASE:
  425. str = get_arg_in_braces(root->ses, arg, line, GET_ONE);
  426. addtoken(root, lvl++, TOKEN_TYPE_CASE, cmd, line);
  427. str = get_arg_in_braces(root->ses, str, line, GET_ALL);
  428. tokenize_script(root, lvl--, line);
  429. addtoken(root, lvl, TOKEN_TYPE_END, -1, "endcase");
  430. break;
  431. case TOKEN_TYPE_CONTINUE:
  432. str = get_arg_with_spaces(root->ses, arg, line, 1);
  433. addtoken(root, lvl, TOKEN_TYPE_CONTINUE, cmd, line);
  434. break;
  435. case TOKEN_TYPE_DEFAULT:
  436. addtoken(root, lvl++, TOKEN_TYPE_DEFAULT, cmd, "");
  437. str = get_arg_in_braces(root->ses, arg, line, GET_ALL);
  438. tokenize_script(root, lvl--, line);
  439. addtoken(root, lvl, TOKEN_TYPE_END, -1, "enddefault");
  440. break;
  441. case TOKEN_TYPE_ELSE:
  442. addtoken(root, lvl++, TOKEN_TYPE_ELSE, cmd, "else");
  443. str = get_arg_in_braces(root->ses, arg, line, GET_ALL);
  444. tokenize_script(root, lvl--, line);
  445. addtoken(root, lvl, TOKEN_TYPE_END, -1, "endelse");
  446. break;
  447. case TOKEN_TYPE_ELSEIF:
  448. str = get_arg_in_braces(root->ses, arg, line, GET_ONE);
  449. addtoken(root, lvl++, TOKEN_TYPE_ELSEIF, cmd, line);
  450. str = get_arg_in_braces(root->ses, str, line, GET_ALL);
  451. tokenize_script(root, lvl--, line);
  452. addtoken(root, lvl, TOKEN_TYPE_END, -1, "endif");
  453. break;
  454. case TOKEN_TYPE_FOREACH:
  455. str = addforeachtoken(root, lvl++, TOKEN_TYPE_FOREACH, cmd, arg);
  456. str = get_arg_in_braces(root->ses, str, line, GET_ALL);
  457. tokenize_script(root, lvl--, line);
  458. addtoken(root, lvl, TOKEN_TYPE_END, -1, "endforeach");
  459. break;
  460. case TOKEN_TYPE_IF:
  461. str = get_arg_in_braces(root->ses, arg, line, GET_ONE);
  462. addtoken(root, lvl++, TOKEN_TYPE_IF, cmd, line);
  463. str = get_arg_in_braces(root->ses, str, line, GET_ALL);
  464. tokenize_script(root, lvl--, line);
  465. addtoken(root, lvl, TOKEN_TYPE_END, -1, "endif");
  466. if (*str && *str != COMMAND_SEPARATOR)
  467. {
  468. addtoken(root, lvl++, TOKEN_TYPE_ELSE, -1, "else");
  469. str = get_arg_in_braces(root->ses, str, line, GET_ALL);
  470. tokenize_script(root, lvl--, line);
  471. addtoken(root, lvl, TOKEN_TYPE_END, -1, "endif");
  472. }
  473. break;
  474. case TOKEN_TYPE_LOOP:
  475. str = addlooptoken(root, lvl++, TOKEN_TYPE_LOOP, cmd, arg);
  476. str = get_arg_in_braces(root->ses, str, line, GET_ALL);
  477. tokenize_script(root, lvl--, line);
  478. addtoken(root, lvl, TOKEN_TYPE_END, -1, "endloop");
  479. break;
  480. case TOKEN_TYPE_PARSE:
  481. str = addparsetoken(root, lvl++, TOKEN_TYPE_PARSE, cmd, arg);
  482. str = get_arg_in_braces(root->ses, str, line, GET_ALL);
  483. tokenize_script(root, lvl--, line);
  484. addtoken(root, lvl, TOKEN_TYPE_END, -1, "endparse");
  485. break;
  486. case TOKEN_TYPE_REGEX:
  487. str = addregextoken(root, lvl, TOKEN_TYPE_REGEX, cmd, arg);
  488. // addtoken(root, --lvl, TOKEN_TYPE_END, -1, "endregex");
  489. if (*str && *str != COMMAND_SEPARATOR)
  490. {
  491. addtoken(root, lvl++, TOKEN_TYPE_ELSE, -1, "else");
  492. str = get_arg_in_braces(root->ses, str, line, GET_ALL);
  493. tokenize_script(root, lvl--, line);
  494. addtoken(root, lvl, TOKEN_TYPE_END, -1, "endregex");
  495. }
  496. break;
  497. case TOKEN_TYPE_RETURN:
  498. str = get_arg_in_braces(root->ses, arg, line, GET_ALL);
  499. addtoken(root, lvl, TOKEN_TYPE_RETURN, cmd, line);
  500. break;
  501. case TOKEN_TYPE_SWITCH:
  502. str = addswitchtoken(root, lvl++, TOKEN_TYPE_SWITCH, cmd, arg);
  503. str = get_arg_in_braces(root->ses, str, line, GET_ALL);
  504. tokenize_script(root, lvl--, line);
  505. addtoken(root, lvl, TOKEN_TYPE_END, -1, "endswitch");
  506. break;
  507. case TOKEN_TYPE_WHILE:
  508. str = get_arg_in_braces(root->ses, arg, line, GET_ONE);
  509. addtoken(root, lvl++, TOKEN_TYPE_WHILE, cmd, line);
  510. str = get_arg_in_braces(root->ses, str, line, GET_ALL);
  511. tokenize_script(root, lvl--, line);
  512. addtoken(root, lvl, TOKEN_TYPE_END, -1, "endwhile");
  513. break;
  514. default:
  515. str = get_arg_with_spaces(root->ses, arg, line, GET_ALL);
  516. addtoken(root, lvl, TOKEN_TYPE_COMMAND, cmd, line);
  517. break;
  518. }
  519. }
  520. }
  521. str = space_out(str);
  522. if (*str == COMMAND_SEPARATOR)
  523. {
  524. str++;
  525. }
  526. }
  527. pop_call();
  528. return;
  529. }
  530. struct scriptnode *parse_script(struct scriptroot *root, int lvl, struct scriptnode *token, struct scriptnode *shift)
  531. {
  532. struct scriptnode *split = NULL;
  533. while (token)
  534. {
  535. if (token->lvl < lvl)
  536. {
  537. if (shift->lvl + 1 == lvl)
  538. {
  539. switch (shift->type)
  540. {
  541. case TOKEN_TYPE_FOREACH:
  542. case TOKEN_TYPE_LOOP:
  543. case TOKEN_TYPE_PARSE:
  544. case TOKEN_TYPE_WHILE:
  545. debugtoken(root->ses, root, token);
  546. return shift;
  547. case TOKEN_TYPE_BROKEN_FOREACH:
  548. case TOKEN_TYPE_BROKEN_LOOP:
  549. case TOKEN_TYPE_BROKEN_PARSE:
  550. case TOKEN_TYPE_BROKEN_WHILE:
  551. shift->type--;
  552. return token;
  553. }
  554. }
  555. return token;
  556. }
  557. debugtoken(root->ses, root, token);
  558. switch (token->type)
  559. {
  560. case TOKEN_TYPE_BREAK:
  561. switch (shift->type)
  562. {
  563. case TOKEN_TYPE_FOREACH:
  564. breakforeachtoken(shift);
  565. shift->type++;
  566. break;
  567. case TOKEN_TYPE_LOOP:
  568. breaklooptoken(shift);
  569. shift->type++;
  570. break;
  571. case TOKEN_TYPE_PARSE:
  572. breakparsetoken(shift);
  573. shift->type++;
  574. break;
  575. case TOKEN_TYPE_WHILE:
  576. shift->type++;
  577. break;
  578. }
  579. do
  580. {
  581. token = token->next;
  582. }
  583. while (token && token->lvl > shift->lvl);
  584. continue;
  585. case TOKEN_TYPE_CASE:
  586. if (shift->data && mathswitch(root->ses, shift->data->str, token->str))
  587. {
  588. token = token->next;
  589. token = parse_script(root, lvl + 1, token, shift);
  590. while (token && token->lvl >= lvl)
  591. {
  592. token = token->next;
  593. }
  594. }
  595. else
  596. {
  597. do
  598. {
  599. token = token->next;
  600. }
  601. while (token && token->lvl > lvl);
  602. }
  603. continue;
  604. case TOKEN_TYPE_COMMAND:
  605. if (push_call_printf("do_%s(%s,%p)", command_table[token->cmd].name, root->ses->name, token->str))
  606. {
  607. switch (command_table[token->cmd].args)
  608. {
  609. case 0:
  610. root->ses = (*command_table[token->cmd].command) (root->ses, token->str, NULL, NULL, NULL, NULL);
  611. break;
  612. case 1:
  613. root->ses = (*command_table[token->cmd].command) (root->ses, token->str, str_alloc_stack(0), NULL, NULL, NULL);
  614. break;
  615. case 2:
  616. root->ses = (*command_table[token->cmd].command) (root->ses, token->str, str_alloc_stack(0), str_alloc_stack(0), NULL, NULL);
  617. break;
  618. case 3:
  619. root->ses = (*command_table[token->cmd].command) (root->ses, token->str, str_alloc_stack(0), str_alloc_stack(0), str_alloc_stack(0), NULL);
  620. break;
  621. case 4:
  622. tintin_printf2(gtd->ses, "error: parse_script: command_table[%d].command == 4", token->cmd);
  623. root->ses = (*command_table[token->cmd].command) (root->ses, token->str, str_alloc_stack(0), str_alloc_stack(0), str_alloc_stack(0), str_alloc_stack(0));
  624. break;
  625. }
  626. }
  627. pop_call();
  628. /*
  629. return;
  630. }
  631. */
  632. break;
  633. case TOKEN_TYPE_CONTINUE:
  634. do
  635. {
  636. token = token->next;
  637. }
  638. while (token && token->lvl > shift->lvl);
  639. continue;
  640. case TOKEN_TYPE_DEFAULT:
  641. token = token->next;
  642. token = parse_script(root, lvl + 1, token, shift);
  643. while (token && token->lvl >= lvl)
  644. {
  645. token = token->next;
  646. }
  647. continue;
  648. case TOKEN_TYPE_ELSE:
  649. if (split)
  650. {
  651. token = parse_script(root, lvl + 1, token->next, shift);
  652. split = NULL;
  653. }
  654. else
  655. {
  656. do
  657. {
  658. token = token->next;
  659. }
  660. while (token && token->lvl > lvl);
  661. }
  662. continue;
  663. case TOKEN_TYPE_ELSEIF:
  664. if (split && get_number(root->ses, token->str))
  665. {
  666. token = parse_script(root, lvl + 1, token->next, shift);
  667. split = NULL;
  668. }
  669. else
  670. {
  671. do
  672. {
  673. token = token->next;
  674. }
  675. while (token && token->lvl > lvl);
  676. }
  677. continue;
  678. case TOKEN_TYPE_END:
  679. break;
  680. case TOKEN_TYPE_FOREACH:
  681. if (*token->data->arg == 0)
  682. {
  683. resetforeachtoken(root->ses, token);
  684. }
  685. if (*token->data->arg == 0)
  686. {
  687. // token->type++;
  688. do
  689. {
  690. token = token->next;
  691. }
  692. while (token && token->lvl > lvl);
  693. }
  694. else
  695. {
  696. set_nest_node_ses(root->ses, token->str, "%s", get_arg_foreach(root, token));
  697. if (*token->data->arg == 0)
  698. {
  699. token->type++;
  700. }
  701. token = parse_script(root, lvl + 1, token->next, token);
  702. }
  703. continue;
  704. case TOKEN_TYPE_IF:
  705. split = NULL;
  706. if (get_number(root->ses, token->str))
  707. {
  708. token = parse_script(root, lvl + 1, token->next, shift);
  709. }
  710. else
  711. {
  712. split = token;
  713. do
  714. {
  715. token = token->next;
  716. }
  717. while (token && token->lvl > lvl);
  718. }
  719. continue;
  720. case TOKEN_TYPE_LOOP:
  721. if (token->data->cnt == token->data->max + token->data->inc)
  722. {
  723. resetlooptoken(root->ses, token);
  724. }
  725. set_nest_node_ses(root->ses, token->str, "%lld", token->data->cnt);
  726. token->data->cnt += token->data->inc;
  727. if (token->data->cnt == token->data->max + token->data->inc)
  728. {
  729. token->type++;
  730. }
  731. token = parse_script(root, lvl + 1, token->next, token);
  732. continue;
  733. case TOKEN_TYPE_PARSE:
  734. if (*token->data->arg == 0)
  735. {
  736. resetparsetoken(root->ses, token);
  737. if (*token->data->arg == 0)
  738. {
  739. // token->type++;
  740. do
  741. {
  742. token = token->next;
  743. }
  744. while (token && token->lvl > lvl);
  745. continue;
  746. }
  747. }
  748. set_nest_node_ses(root->ses, token->str, "%s", get_arg_parse(root->ses, token));
  749. if (*token->data->arg == 0)
  750. {
  751. token->type++;
  752. }
  753. token = parse_script(root, lvl + 1, token->next, token);
  754. continue;
  755. case TOKEN_TYPE_REGEX:
  756. split = NULL;
  757. token->regex->val = find(root->ses, token->str, token->regex->str, SUB_VAR|SUB_FUN, REGEX_FLAG_CMD);
  758. if (token->regex->val)
  759. {
  760. substitute(root->ses, token->regex->bod, token->regex->buf, SUB_CMD);
  761. root->ses = script_driver(root->ses, LIST_COMMAND, token->regex->buf);
  762. }
  763. else
  764. {
  765. split = token;
  766. }
  767. break;
  768. case TOKEN_TYPE_RETURN:
  769. handlereturntoken(root->ses, token);
  770. goto end;
  771. case TOKEN_TYPE_SESSION:
  772. root->ses = parse_tintin_command(root->ses, token->str);
  773. break;
  774. case TOKEN_TYPE_STRING:
  775. root->ses = parse_input(root->ses, token->str);
  776. break;
  777. case TOKEN_TYPE_REPEAT:
  778. root->ses = repeat_history(root->ses, token->str);
  779. break;
  780. case TOKEN_TYPE_SWITCH:
  781. handleswitchtoken(root->ses, token);
  782. token = parse_script(root, lvl + 1, token->next, token);
  783. continue;
  784. case TOKEN_TYPE_WHILE:
  785. if (get_number(root->ses, token->str))
  786. {
  787. token = parse_script(root, lvl + 1, token->next, token);
  788. }
  789. else
  790. {
  791. // token->type++;
  792. do
  793. {
  794. token = token->next;
  795. }
  796. while (token && token->lvl > lvl);
  797. }
  798. continue;
  799. }
  800. if (token)
  801. {
  802. token = token->next;
  803. }
  804. }
  805. end:
  806. if (lvl)
  807. {
  808. return NULL;
  809. }
  810. return (struct scriptnode *) root->ses;
  811. }
  812. char *write_script(struct session *ses, struct scriptroot *root)
  813. {
  814. struct scriptnode *token;
  815. static char buf[STRING_SIZE];
  816. token = root->next;
  817. buf[0] = 0;
  818. while (token)
  819. {
  820. switch (token->type)
  821. {
  822. case TOKEN_TYPE_STRING:
  823. cat_sprintf(buf, "%s%s", indent(token->lvl), token->str);
  824. break;
  825. case TOKEN_TYPE_BREAK:
  826. case TOKEN_TYPE_CONTINUE:
  827. cat_sprintf(buf, "%s%c%s", indent(token->lvl), gtd->tintin_char, command_table[token->cmd].name);
  828. break;
  829. case TOKEN_TYPE_COMMAND:
  830. case TOKEN_TYPE_RETURN:
  831. cat_sprintf(buf, "%s%c%s%s%s", indent(token->lvl), gtd->tintin_char, command_table[token->cmd].name, *token->str ? " " : "", token->str);
  832. break;
  833. case TOKEN_TYPE_ELSE:
  834. cat_sprintf(buf, "%s%c%s\n%s{\n", indent(token->lvl), gtd->tintin_char, token->str, indent(token->lvl));
  835. break;
  836. case TOKEN_TYPE_DEFAULT:
  837. cat_sprintf(buf, "%s%c%s\n%s{\n", indent(token->lvl), gtd->tintin_char, command_table[token->cmd].name, indent(token->lvl));
  838. break;
  839. case TOKEN_TYPE_FOREACH:
  840. case TOKEN_TYPE_LOOP:
  841. case TOKEN_TYPE_PARSE:
  842. case TOKEN_TYPE_SWITCH:
  843. 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));
  844. break;
  845. case TOKEN_TYPE_CASE:
  846. case TOKEN_TYPE_ELSEIF:
  847. case TOKEN_TYPE_IF:
  848. case TOKEN_TYPE_WHILE:
  849. 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));
  850. break;
  851. case TOKEN_TYPE_REGEX:
  852. 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));
  853. break;
  854. case TOKEN_TYPE_END:
  855. cat_sprintf(buf, "\n%s}", indent(token->lvl));
  856. break;
  857. case TOKEN_TYPE_SESSION:
  858. cat_sprintf(buf, "%s%c%s", indent(token->lvl), gtd->tintin_char, token->str);
  859. break;
  860. default:
  861. tintin_printf2(ses, "#WRITE: UNKNOWN TOKEN TYPE: %d", token->type);
  862. break;
  863. }
  864. if (token->next && token->lvl == token->next->lvl)
  865. {
  866. strcat(buf, ";\n");
  867. }
  868. token = token->next;
  869. }
  870. while (root->next)
  871. {
  872. deltoken(root, root->next);
  873. }
  874. free(root);
  875. return buf;
  876. }
  877. char *view_script(struct session *ses, struct scriptroot *root)
  878. {
  879. struct scriptnode *token;
  880. static char buf[STRING_SIZE];
  881. token = root->next;
  882. buf[0] = 0;
  883. while (token)
  884. {
  885. switch (token->type)
  886. {
  887. case TOKEN_TYPE_STRING:
  888. cat_sprintf(buf, "%s" COLOR_STRING "%s", indent(token->lvl), token->str);
  889. break;
  890. case TOKEN_TYPE_BREAK:
  891. case TOKEN_TYPE_CONTINUE:
  892. cat_sprintf(buf, "%s" COLOR_TINTIN "%c" COLOR_STATEMENT "%s", indent(token->lvl), gtd->tintin_char, command_table[token->cmd].name);
  893. break;
  894. case TOKEN_TYPE_RETURN:
  895. 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);
  896. break;
  897. case TOKEN_TYPE_COMMAND:
  898. 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);
  899. break;
  900. case TOKEN_TYPE_ELSE:
  901. 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));
  902. break;
  903. case TOKEN_TYPE_DEFAULT:
  904. 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));
  905. break;
  906. case TOKEN_TYPE_FOREACH:
  907. case TOKEN_TYPE_LOOP:
  908. case TOKEN_TYPE_PARSE:
  909. case TOKEN_TYPE_SWITCH:
  910. 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));
  911. break;
  912. case TOKEN_TYPE_CASE:
  913. case TOKEN_TYPE_ELSEIF:
  914. case TOKEN_TYPE_IF:
  915. case TOKEN_TYPE_WHILE:
  916. 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));
  917. break;
  918. case TOKEN_TYPE_REGEX:
  919. 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));
  920. break;
  921. case TOKEN_TYPE_END:
  922. cat_sprintf(buf, "\n%s" COLOR_BRACE "}" COLOR_STRING, indent(token->lvl));
  923. break;
  924. case TOKEN_TYPE_SESSION:
  925. cat_sprintf(buf, "%s" COLOR_TINTIN "%c" COLOR_STRING "%s", indent(token->lvl), gtd->tintin_char, token->str);
  926. break;
  927. default:
  928. tintin_printf2(ses, "#ERROR: UNKNOWN TOKEN TYPE: %d", token->type);
  929. break;
  930. }
  931. if (token->next && token->lvl == token->next->lvl)
  932. {
  933. strcat(buf, COLOR_SEPARATOR ";\n");
  934. }
  935. token = token->next;
  936. }
  937. while (root->next)
  938. {
  939. deltoken(root, root->next);
  940. }
  941. free(root);
  942. return buf;
  943. }
  944. struct session *script_driver(struct session *ses, int list, char *str)
  945. {
  946. struct scriptroot *root;
  947. push_call("script_driver(%p,%d,%p)",ses,list,str);
  948. root = push_script_stack(ses, list);
  949. gtd->level->input += list != LIST_COMMAND;
  950. tokenize_script(root, 0, str);
  951. ses = (struct session *) parse_script(root, 0, root->next, root->prev);
  952. gtd->level->input -= list != LIST_COMMAND;
  953. while (root->prev)
  954. {
  955. deltoken(root, root->prev);
  956. }
  957. pop_script_stack();
  958. if (HAS_BIT(ses->flags, SES_FLAG_CLOSED))
  959. {
  960. pop_call();
  961. return gtd->ses;
  962. }
  963. pop_call();
  964. return ses;
  965. }
  966. char *script_writer(struct session *ses, char *str)
  967. {
  968. struct scriptroot *root;
  969. root = (struct scriptroot *) calloc(1, sizeof(struct scriptroot));
  970. root->ses = ses;
  971. tokenize_script(root, 1, str);
  972. return write_script(ses, root);
  973. }
  974. char *script_viewer(struct session *ses, char *str)
  975. {
  976. struct scriptroot *root;
  977. root = (struct scriptroot *) calloc(1, sizeof(struct scriptroot));
  978. root->ses = ses;
  979. tokenize_script(root, 1, str);
  980. return view_script(ses, root);
  981. }