tokenize.c 30 KB

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