tokenize.c 30 KB

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