list.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  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 2004 *
  24. ******************************************************************************/
  25. #include "tintin.h"
  26. #define DO_ARRAY(array) struct session *array (struct session *ses, struct listnode *list, char *arg, char *var, char *arg1, char *arg2)
  27. extern DO_ARRAY(array_add);
  28. extern DO_ARRAY(array_clear);
  29. extern DO_ARRAY(array_collapse);
  30. extern DO_ARRAY(array_create);
  31. extern DO_ARRAY(array_delete);
  32. extern DO_ARRAY(array_explode);
  33. extern DO_ARRAY(array_filter);
  34. extern DO_ARRAY(array_find);
  35. extern DO_ARRAY(array_get);
  36. extern DO_ARRAY(array_indexate);
  37. extern DO_ARRAY(array_insert);
  38. extern DO_ARRAY(array_numerate);
  39. extern DO_ARRAY(array_order);
  40. extern DO_ARRAY(array_reverse);
  41. extern DO_ARRAY(array_refine);
  42. extern DO_ARRAY(array_set);
  43. extern DO_ARRAY(array_shuffle);
  44. extern DO_ARRAY(array_simplify);
  45. extern DO_ARRAY(array_size);
  46. extern DO_ARRAY(array_sort);
  47. extern DO_ARRAY(array_tokenize);
  48. typedef struct session *ARRAY(struct session *ses, struct listnode *list, char *arg, char *var, char *arg1, char *arg2);
  49. struct array_type
  50. {
  51. char * name;
  52. ARRAY * fun;
  53. char * desc;
  54. };
  55. struct array_type array_table[] =
  56. {
  57. { "ADD", array_add, "Add items to a list" },
  58. { "CLEAR", array_clear, "Clear a list" },
  59. { "CLR", array_clear, NULL },
  60. { "COLLAPSE", array_collapse, "Collapse the list into a variable" },
  61. { "CREATE", array_create, "Create a list with given items" },
  62. { "DELETE", array_delete, "Delete a list item with given index" },
  63. { "EXPLODE", array_explode, "Explode the variable into a list" },
  64. { "FILTER", array_filter, "Filter a list with given regex" },
  65. { "FIND", array_find, "Find a list item with given regex" },
  66. { "FND", array_find, NULL },
  67. { "GET", array_get, "Retrieve a list item with given index" },
  68. { "INDEXATE", array_indexate, "Indexate a list table for sorting" },
  69. { "INSERT", array_insert, "Insert an item at given index" },
  70. { "NUMERATE", array_numerate, "Turn a table into a list" },
  71. { "ORDER", array_order, "Sort a list table numerically" },
  72. { "LENGTH", array_size, NULL },
  73. { "REFINE", array_refine, "Filter a list with given math" },
  74. { "REVERSE", array_reverse, "Sort a list table in reverse order" },
  75. { "SET", array_set, "Change a list item at given index" },
  76. { "SHUFFLE", array_shuffle, "Sort a list table in random order" },
  77. { "SIMPLIFY", array_simplify, "Turn a list table into a simple list" },
  78. { "SIZE", array_size, NULL },
  79. { "SORT", array_sort, "Sort a list table alphabetically" },
  80. { "SRT", array_sort, NULL },
  81. { "TOKENIZE", array_tokenize, "Create a list with given characters" },
  82. { "", NULL, "" }
  83. };
  84. DO_COMMAND(do_list)
  85. {
  86. struct listnode *node;
  87. int index, cnt;
  88. arg = sub_arg_in_braces(ses, arg, arg1, GET_NST, SUB_VAR|SUB_FUN);
  89. arg = sub_arg_in_braces(ses, arg, arg2, GET_ONE, SUB_VAR|SUB_FUN);
  90. if (*arg1 == 0)
  91. {
  92. info:
  93. tintin_header(ses, 80, " LIST OPTIONS ");
  94. for (index = 0 ; *array_table[index].fun ; index++)
  95. {
  96. if (array_table[index].desc && *array_table[index].name)
  97. {
  98. tintin_printf2(ses, " [%-24s] %s", array_table[index].name, array_table[index].desc);
  99. }
  100. }
  101. tintin_header(ses, 80, "");
  102. }
  103. else if (*arg2 == 0)
  104. {
  105. show_error(ses, LIST_VARIABLE, "#SYNTAX: #LIST {variable} {option} {argument}");
  106. }
  107. else
  108. {
  109. for (cnt = 0 ; *array_table[cnt].name ; cnt++)
  110. {
  111. if (is_abbrev(arg2, array_table[cnt].name))
  112. {
  113. break;
  114. }
  115. }
  116. if (*array_table[cnt].name == 0)
  117. {
  118. goto info;
  119. }
  120. else
  121. {
  122. if (!valid_variable(ses, arg1))
  123. {
  124. show_error(ses, LIST_VARIABLE, "#LIST: INVALID VARIABLE NAME {%s}.", arg1);
  125. return ses;
  126. }
  127. if ((node = search_nest_node_ses(ses, arg1)) == NULL)
  128. {
  129. node = set_nest_node_ses(ses, arg1, "");
  130. }
  131. array_table[cnt].fun(ses, node, arg, arg1, arg2, arg3);
  132. }
  133. }
  134. return ses;
  135. }
  136. int get_list_index(struct session *ses, struct listroot *root, char *arg)
  137. {
  138. int toi;
  139. toi = get_number(ses, arg);
  140. if (toi > 0)
  141. {
  142. if (toi <= root->used)
  143. {
  144. return toi - 1;
  145. }
  146. return -1;
  147. }
  148. if (toi < 0)
  149. {
  150. if (root->used + toi >= 0)
  151. {
  152. return root->used + toi;
  153. }
  154. return -1;
  155. }
  156. return -1;
  157. }
  158. DO_ARRAY(array_add)
  159. {
  160. char *str;
  161. int index;
  162. if (list->root == NULL)
  163. {
  164. list->root = init_list(ses, LIST_VARIABLE, LIST_SIZE);
  165. }
  166. index = list->root->used + 1;
  167. while (*arg)
  168. {
  169. arg = sub_arg_in_braces(ses, arg, arg1, GET_ONE, SUB_VAR|SUB_FUN);
  170. str = arg1;
  171. while (*str)
  172. {
  173. str = get_arg_in_braces(ses, str, arg2, GET_ALL);
  174. set_nest_node(list->root, ntos(index++), "%s", arg2);
  175. if (*str == COMMAND_SEPARATOR)
  176. {
  177. str++;
  178. }
  179. }
  180. if (*arg == COMMAND_SEPARATOR)
  181. {
  182. arg++;
  183. }
  184. }
  185. return ses;
  186. }
  187. DO_ARRAY(array_clear)
  188. {
  189. if (list->root)
  190. {
  191. free_list(list->root);
  192. list->root = NULL;
  193. }
  194. set_nest_node_ses(ses, var, "");
  195. return ses;
  196. }
  197. DO_ARRAY(array_collapse)
  198. {
  199. int index;
  200. arg = sub_arg_in_braces(ses, arg, arg1, GET_ONE, SUB_VAR|SUB_FUN|SUB_ESC);
  201. if (list->root)
  202. {
  203. if (list->root->used)
  204. {
  205. str_cpy(&list->arg2, list->root->list[0]->arg2);
  206. }
  207. for (index = 1 ; index < list->root->used ; index++)
  208. {
  209. str_cat(&list->arg2, arg1);
  210. str_cat(&list->arg2, list->root->list[index]->arg2);
  211. }
  212. free_list(list->root);
  213. list->root = NULL;
  214. }
  215. return ses;
  216. }
  217. DO_ARRAY(array_create)
  218. {
  219. char *buf, *str;
  220. int index = 1;
  221. buf = str_alloc_stack(0);
  222. substitute(ses, arg, buf, SUB_VAR|SUB_FUN);
  223. arg = buf;
  224. if (*arg == 0)
  225. {
  226. set_nest_node_ses(ses, arg1, "");
  227. return ses;
  228. }
  229. if (list->root)
  230. {
  231. free_list(list->root);
  232. }
  233. list->root = init_list(ses, LIST_VARIABLE, LIST_SIZE);
  234. while (*arg)
  235. {
  236. arg = get_arg_in_braces(ses, arg, arg1, GET_ONE);
  237. str = arg1;
  238. while (*str)
  239. {
  240. str = get_arg_in_braces(ses, str, arg2, GET_ALL);
  241. set_nest_node(list->root, ntos(index++), "%s", arg2);
  242. if (*str == COMMAND_SEPARATOR)
  243. {
  244. *str = ' ';
  245. // str++;
  246. }
  247. }
  248. if (*arg == COMMAND_SEPARATOR)
  249. {
  250. *arg = ' ';
  251. // arg++;
  252. }
  253. }
  254. return ses;
  255. }
  256. DO_ARRAY(array_delete)
  257. {
  258. int index, cnt, loop;
  259. if (list->root)
  260. {
  261. arg = sub_arg_in_braces(ses, arg, arg1, GET_ONE, SUB_VAR|SUB_FUN);
  262. arg = get_arg_in_braces(ses, arg, arg2, GET_ALL);
  263. index = get_list_index(ses, list->root, arg1);
  264. if (*arg2)
  265. {
  266. loop = URANGE(1, (int) get_number(ses, arg2), list->root->used - index);
  267. }
  268. else
  269. {
  270. loop = 1;
  271. }
  272. if (index == -1)
  273. {
  274. show_error(ses, LIST_VARIABLE, "#LIST {%s} DELETE: INVALID INDEX: {%s}.", var, arg1);
  275. return ses;
  276. }
  277. for (cnt = index + loop ; cnt < list->root->used ; cnt++)
  278. {
  279. str_cpy_printf(&list->root->list[cnt]->arg1, "%d", cnt + 1 - loop);
  280. }
  281. while (loop--)
  282. {
  283. delete_index_list(list->root, index);
  284. }
  285. if (list->root->used == 0)
  286. {
  287. array_clear(ses, list, arg, var, arg1, arg2);
  288. }
  289. }
  290. else
  291. {
  292. show_error(ses, LIST_VARIABLE, "#LIST DELETE: {%s} is not a list.", var);
  293. }
  294. return ses;
  295. }
  296. DO_ARRAY(array_explode)
  297. {
  298. char *str;
  299. int index = 1;
  300. arg = sub_arg_in_braces(ses, arg, arg1, GET_ONE, SUB_VAR|SUB_FUN|SUB_ESC);
  301. arg = sub_arg_in_braces(ses, arg, arg2, GET_ALL, SUB_VAR|SUB_FUN);
  302. if (*arg1 == 0)
  303. {
  304. show_error(ses, LIST_VARIABLE, "#SYNTAX: #LIST {%s} EXPLODE {<SEPARATOR>}.", var);
  305. return ses;
  306. }
  307. if (list->root)
  308. {
  309. if (*arg2 == 0)
  310. {
  311. show_error(ses, LIST_VARIABLE, "#LIST {%s} EXPLODE: VARIABLE %s IS ALREADY A LIST.", var, var);
  312. return ses;
  313. }
  314. free_list(list->root);
  315. list->root = NULL;
  316. }
  317. list->root = init_list(ses, LIST_VARIABLE, LIST_SIZE);
  318. if (*arg2)
  319. {
  320. str_cpy(&list->arg2, arg2);
  321. }
  322. arg = list->arg2;
  323. while (1)
  324. {
  325. str = strstr(arg, arg1);
  326. if (str == NULL)
  327. {
  328. if (*arg)
  329. {
  330. set_nest_node(list->root, ntos(index++), "%s", arg);
  331. }
  332. break;
  333. }
  334. *str = 0;
  335. set_nest_node(list->root, ntos(index++), "%s", arg);
  336. arg = str + strlen(arg1);
  337. }
  338. return ses;
  339. }
  340. DO_ARRAY(array_filter)
  341. {
  342. int index;
  343. arg = sub_arg_in_braces(ses, arg, arg1, GET_ONE, SUB_VAR|SUB_FUN);
  344. arg = sub_arg_in_braces(ses, arg, arg2, GET_ONE, SUB_VAR|SUB_FUN);
  345. if (*arg1 == 0 && *arg2 == 0)
  346. {
  347. show_error(ses, LIST_VARIABLE, "#SYNTAX: #LIST {variable} FILTER {keep} {remove}");
  348. return ses;
  349. }
  350. if (list->root && list->root->used)
  351. {
  352. int numerate = atoi(list->root->list[0]->arg1) == 1 && atoi(list->root->list[list->root->used - 1]->arg1) == list->root->used;
  353. int found = 0;
  354. if (*arg1)
  355. {
  356. for (index = 0 ; index < list->root->used ; index++)
  357. {
  358. if (!match(ses, list->root->list[index]->arg2, arg1, SUB_NONE))
  359. {
  360. delete_index_list(list->root, index--);
  361. found = 1;
  362. }
  363. }
  364. }
  365. if (*arg2)
  366. {
  367. for (index = 0 ; index < list->root->used ; index++)
  368. {
  369. if (match(ses, list->root->list[index]->arg2, arg2, SUB_NONE))
  370. {
  371. delete_index_list(list->root, index--);
  372. found = 1;
  373. }
  374. }
  375. }
  376. if (found && list->root->used == 0)
  377. {
  378. array_clear(ses, list, arg, var, arg1, arg2);
  379. }
  380. else if (found && numerate)
  381. {
  382. array_numerate(ses, list, arg, var, arg1, arg2);
  383. }
  384. }
  385. return ses;
  386. }
  387. DO_ARRAY(array_find)
  388. {
  389. int index;
  390. arg = sub_arg_in_braces(ses, arg, arg1, GET_ONE, SUB_VAR|SUB_FUN);
  391. arg = sub_arg_in_braces(ses, arg, arg2, GET_ONE, SUB_VAR|SUB_FUN);
  392. if (*arg2 == 0)
  393. {
  394. show_error(ses, LIST_VARIABLE, "#SYNTAX: #LIST {variable} FIND {string} {variable}");
  395. return ses;
  396. }
  397. if (list->root)
  398. {
  399. for (index = 0 ; index < list->root->used ; index++)
  400. {
  401. if (match(ses, list->root->list[index]->arg2, arg1, SUB_NONE))
  402. {
  403. set_nest_node_ses(ses, arg2, "%d", index + 1);
  404. return ses;
  405. }
  406. }
  407. }
  408. set_nest_node_ses(ses, arg2, "0");
  409. return ses;
  410. }
  411. DO_ARRAY(array_get)
  412. {
  413. int index;
  414. arg = sub_arg_in_braces(ses, arg, arg1, GET_ONE, SUB_VAR|SUB_FUN);
  415. arg = sub_arg_in_braces(ses, arg, arg2, GET_ALL, SUB_VAR|SUB_FUN);
  416. if (*arg2 == 0)
  417. {
  418. show_error(ses, LIST_VARIABLE, "#SYNTAX: #LIST {variable} GET {index} {variable}");
  419. return ses;
  420. }
  421. if (list->root)
  422. {
  423. index = get_list_index(ses, list->root, arg1);
  424. if (index != -1)
  425. {
  426. set_nest_node_ses(ses, arg2, "%s", list->root->list[index]->arg2);
  427. return ses;
  428. }
  429. }
  430. set_nest_node_ses(ses, arg2, "0");
  431. return ses;
  432. }
  433. DO_ARRAY(array_indexate)
  434. {
  435. int cnt;
  436. arg = sub_arg_in_braces(ses, arg, arg1, GET_ALL, SUB_VAR|SUB_FUN);
  437. if (list->root == NULL || list->root->list[0]->root == NULL)
  438. {
  439. show_error(ses, LIST_COMMAND, "#ERROR: #LIST {%s} INDEXATE: NOT AN INDEXABLE LIST TABLE.", var);
  440. return ses;
  441. }
  442. if (*arg1 == 0)
  443. {
  444. for (cnt = 0 ; cnt < list->root->used ; cnt++)
  445. {
  446. if (list->root->list[cnt]->root)
  447. {
  448. str_cpy(&list->root->list[cnt]->arg2, list->root->list[cnt]->arg1);
  449. }
  450. else
  451. {
  452. show_error(ses, LIST_COMMAND, "#ERROR: #LIST {%s} INDEXATE: FAILED TO POPULATE INDEX {%s}.", var, list->root->list[cnt]->arg1);
  453. break;
  454. }
  455. }
  456. return ses;
  457. }
  458. if (list->root->used)
  459. {
  460. int index = search_index_list(list->root->list[0]->root, arg1, "");
  461. if (index == -1)
  462. {
  463. show_error(ses, LIST_COMMAND, "#ERROR: #LIST {%s} INDEXATE: FAILED TO FIND NEST {%s}.", var, arg1);
  464. return ses;
  465. }
  466. for (cnt = 0 ; cnt < list->root->used ; cnt++)
  467. {
  468. if (list->root->list[cnt]->root && list->root->list[cnt]->root->used > index)
  469. {
  470. str_cpy(&list->root->list[cnt]->arg2, list->root->list[cnt]->root->list[index]->arg2);
  471. }
  472. else
  473. {
  474. show_error(ses, LIST_COMMAND, "#ERROR: #LIST {%s} INDEXATE: FAILED TO POPULATE INDEX {%s}.", var, list->root->list[cnt]->arg1);
  475. break;
  476. }
  477. }
  478. }
  479. return ses;
  480. }
  481. DO_ARRAY(array_insert)
  482. {
  483. int cnt, toi, index;
  484. arg = sub_arg_in_braces(ses, arg, arg1, GET_ONE, SUB_VAR|SUB_FUN);
  485. arg = sub_arg_in_braces(ses, arg, arg2, GET_ALL, SUB_VAR|SUB_FUN);
  486. if (list->root == NULL)
  487. {
  488. list->root = init_list(ses, LIST_VARIABLE, LIST_SIZE);
  489. }
  490. toi = get_number(ses, arg1);
  491. if (toi == 0)
  492. {
  493. show_error(ses, LIST_VARIABLE, "#LIST INSERT: INVALID INDEX: {%s}.", arg1);
  494. return ses;
  495. }
  496. index = get_list_index(ses, list->root, arg1);
  497. if (index == -1 || toi < 0)
  498. {
  499. index++;
  500. }
  501. for (cnt = index ; cnt < list->root->used ; cnt++)
  502. {
  503. str_cpy_printf(&list->root->list[cnt]->arg1, "%d", cnt + 2);
  504. }
  505. set_nest_node(list->root, ntos(index + 1), "%s", arg2);
  506. return ses;
  507. }
  508. DO_ARRAY(array_numerate)
  509. {
  510. int index;
  511. if (list->root == NULL)
  512. {
  513. show_error(ses, LIST_VARIABLE, "#LIST {%s} NUMERATE: VARIABLE IS NOT A TABLE.", var);
  514. return ses;
  515. }
  516. for (index = 0 ; index < list->root->used ; index++)
  517. {
  518. str_cpy_printf(&list->root->list[index]->arg1, "%d", index + 1);
  519. }
  520. return ses;
  521. }
  522. DO_ARRAY(array_order)
  523. {
  524. int cnt, val, len;
  525. char **arg2_buffer;
  526. array_add(ses, list, arg, var, arg1, arg2);
  527. if (list->root->used > 1)
  528. {
  529. /* if (*list->root->list[0]->arg2 == 0)
  530. {
  531. show_error(ses, LIST_COMMAND, "#ERROR: #LIST {%s} ORDER: LIST IS NOT INDEXED.", var);
  532. return ses;
  533. }
  534. */
  535. if (list->root->list[0]->root)
  536. {
  537. struct listroot **root_buffer;
  538. root_buffer = malloc(list->root->used * sizeof(struct listroot *));
  539. arg2_buffer = malloc(list->root->used * sizeof(char *));
  540. for (cnt = 0 ; cnt < list->root->used ; cnt++)
  541. {
  542. len = str_len(list->root->list[cnt]->arg2);
  543. root_buffer[cnt] = list->root->list[cnt]->root;
  544. arg2_buffer[cnt] = list->root->list[cnt]->arg2;
  545. str_resize(&arg2_buffer[cnt], 10);
  546. sprintf(arg2_buffer[cnt] + len + 1, "%x", cnt);
  547. }
  548. quadsort(arg2_buffer, list->root->used, sizeof(char *), cmp_num);
  549. for (cnt = 0 ; cnt < list->root->used ; cnt++)
  550. {
  551. val = hex_number_32bit(arg2_buffer[cnt] + str_len(arg2_buffer[cnt]) + 1);
  552. list->root->list[cnt]->root = root_buffer[val];
  553. list->root->list[cnt]->arg2 = arg2_buffer[cnt];
  554. }
  555. free(arg2_buffer);
  556. free(root_buffer);
  557. }
  558. else
  559. {
  560. arg2_buffer = malloc(list->root->used * sizeof(char *));
  561. for (cnt = 0 ; cnt < list->root->used ; cnt++)
  562. {
  563. arg2_buffer[cnt] = list->root->list[cnt]->arg2;
  564. }
  565. quadsort(arg2_buffer, list->root->used, sizeof(char *), cmp_num);
  566. for (cnt = 0 ; cnt < list->root->used ; cnt++)
  567. {
  568. list->root->list[cnt]->arg2 = arg2_buffer[cnt];
  569. }
  570. free(arg2_buffer);
  571. }
  572. }
  573. return ses;
  574. }
  575. DO_ARRAY(array_refine)
  576. {
  577. char arg3[BUFFER_SIZE];
  578. int index;
  579. arg = sub_arg_in_braces(ses, arg, arg1, GET_ONE, SUB_VAR|SUB_FUN);
  580. arg = sub_arg_in_braces(ses, arg, arg2, GET_ONE, SUB_VAR|SUB_FUN);
  581. if (*arg1 == 0 && *arg2 == 0)
  582. {
  583. show_error(ses, LIST_VARIABLE, "#SYNTAX: #LIST {variable} REFINE {keep} {remove}");
  584. return ses;
  585. }
  586. if (list->root && list->root->used)
  587. {
  588. int numerate = atoi(list->root->list[0]->arg1) == 1 && atoi(list->root->list[list->root->used - 1]->arg1) == list->root->used;
  589. int found = 0;
  590. if (*arg1)
  591. {
  592. for (index = 0 ; index < list->root->used ; index++)
  593. {
  594. RESTRING(gtd->cmds[0], list->root->list[index]->arg2);
  595. substitute(ses, arg1, arg3, SUB_CMD);
  596. if (!get_number(ses, arg3))
  597. {
  598. delete_index_list(list->root, index--);
  599. found = 1;
  600. }
  601. }
  602. }
  603. if (*arg2)
  604. {
  605. for (index = 0 ; index < list->root->used ; index++)
  606. {
  607. RESTRING(gtd->cmds[0], list->root->list[index]->arg2);
  608. substitute(ses, arg2, arg3, SUB_CMD);
  609. // printf("debug: (%s) (%d)\n", arg3, (int) get_number(ses, arg3));
  610. if (get_number(ses, arg3))
  611. {
  612. delete_index_list(list->root, index--);
  613. found = 1;
  614. }
  615. }
  616. }
  617. if (found && list->root->used == 0)
  618. {
  619. array_clear(ses, list, arg, var, arg1, arg2);
  620. }
  621. else if (found && numerate)
  622. {
  623. array_numerate(ses, list, arg, var, arg1, arg2);
  624. }
  625. }
  626. return ses;
  627. }
  628. DO_ARRAY(array_reverse)
  629. {
  630. struct listroot *toor;
  631. char *swap;
  632. int cnt, rev;
  633. array_add(ses, list, arg, var, arg1, arg2);
  634. for (cnt = 0 ; cnt < list->root->used / 2 ; cnt++)
  635. {
  636. rev = list->root->used - 1 - cnt;
  637. swap = list->root->list[cnt]->arg2;
  638. list->root->list[cnt]->arg2 = list->root->list[rev]->arg2;
  639. list->root->list[rev]->arg2 = swap;
  640. toor = list->root->list[cnt]->root;
  641. list->root->list[cnt]->root = list->root->list[rev]->root;
  642. list->root->list[rev]->root = toor;
  643. }
  644. return ses;
  645. }
  646. DO_ARRAY(array_simplify)
  647. {
  648. char *str;
  649. int index;
  650. array_add(ses, list, arg, var, arg1, arg2);
  651. str = str_alloc_stack(0);
  652. if (list->root)
  653. {
  654. if (list->root->used)
  655. {
  656. str_cpy(&str, list->root->list[0]->arg2);
  657. }
  658. for (index = 1 ; index < list->root->used ; index++)
  659. {
  660. str_cat_printf(&str, ";%s", list->root->list[index]->arg2);
  661. }
  662. set_nest_node_ses(ses, var, "%s", str);
  663. return ses;
  664. }
  665. else
  666. {
  667. show_error(ses, LIST_VARIABLE, "#LIST SIMPLIFY: {%s} is not a list.", list->arg1);
  668. }
  669. return ses;
  670. }
  671. DO_ARRAY(array_size)
  672. {
  673. arg = sub_arg_in_braces(ses, arg, arg1, GET_ALL, SUB_VAR|SUB_FUN);
  674. if (*arg1 == 0)
  675. {
  676. show_error(ses, LIST_VARIABLE, "#SYNTAX: #LIST {variable} SIZE {variable}");
  677. return ses;
  678. }
  679. if (list->root)
  680. {
  681. set_nest_node_ses(ses, arg1, "%d", list->root->used);
  682. }
  683. else
  684. {
  685. set_nest_node_ses(ses, arg1, "0");
  686. }
  687. return ses;
  688. }
  689. DO_ARRAY(array_set)
  690. {
  691. int index;
  692. arg = sub_arg_in_braces(ses, arg, arg1, GET_ONE, SUB_VAR|SUB_FUN);
  693. arg = sub_arg_in_braces(ses, arg, arg2, GET_ALL, SUB_VAR|SUB_FUN);
  694. if (list->root)
  695. {
  696. index = get_list_index(ses, list->root, arg1);
  697. if (index == -1)
  698. {
  699. show_error(ses, LIST_VARIABLE, "#LIST {%s} SET: Invalid index: %s", var, arg1);
  700. return ses;
  701. }
  702. str_cpy(&list->root->list[index]->arg2, arg2);
  703. return ses;
  704. }
  705. show_error(ses, LIST_VARIABLE, "#LIST SET: {%s} is not a list.", var);
  706. return ses;
  707. }
  708. DO_ARRAY(array_shuffle)
  709. {
  710. struct listroot *toor;
  711. char *swap;
  712. int cnt, rnd;
  713. array_add(ses, list, arg, var, arg1, arg2);
  714. for (cnt = 0 ; cnt < list->root->used ; cnt++)
  715. {
  716. rnd = generate_rand(ses) % list->root->used;
  717. swap = list->root->list[cnt]->arg2;
  718. list->root->list[cnt]->arg2 = list->root->list[rnd]->arg2;
  719. list->root->list[rnd]->arg2 = swap;
  720. toor = list->root->list[cnt]->root;
  721. list->root->list[cnt]->root = list->root->list[rnd]->root;
  722. list->root->list[rnd]->root = toor;
  723. }
  724. return ses;
  725. }
  726. DO_ARRAY(array_sort)
  727. {
  728. int cnt, val, len;
  729. char **arg2_buffer;
  730. array_add(ses, list, arg, var, arg1, arg2);
  731. if (list->root->used > 1)
  732. {
  733. if (*list->root->list[0]->arg2 == 0)
  734. {
  735. show_error(ses, LIST_COMMAND, "#ERROR: #LIST {%s} ORDER: LIST IS NOT INDEXED.", var);
  736. return ses;
  737. }
  738. if (list->root->list[0]->root)
  739. {
  740. struct listroot **root_buffer;
  741. root_buffer = malloc(list->root->used * sizeof(struct listroot *));
  742. arg2_buffer = malloc(list->root->used * sizeof(char *));
  743. for (cnt = 0 ; cnt < list->root->used ; cnt++)
  744. {
  745. len = str_len(list->root->list[cnt]->arg2);
  746. root_buffer[cnt] = list->root->list[cnt]->root;
  747. arg2_buffer[cnt] = list->root->list[cnt]->arg2;
  748. str_resize(&arg2_buffer[cnt], 10);
  749. sprintf(arg2_buffer[cnt] + len + 1, "%x", cnt);
  750. }
  751. quadsort(arg2_buffer, list->root->used, sizeof(char *), cmp_str);
  752. for (cnt = 0 ; cnt < list->root->used ; cnt++)
  753. {
  754. val = hex_number_32bit(arg2_buffer[cnt] + str_len(arg2_buffer[cnt]) + 1);
  755. list->root->list[cnt]->root = root_buffer[val];
  756. list->root->list[cnt]->arg2 = arg2_buffer[cnt];
  757. }
  758. free(arg2_buffer);
  759. free(root_buffer);
  760. }
  761. else
  762. {
  763. arg2_buffer = malloc(list->root->used * sizeof(char *));
  764. for (cnt = 0 ; cnt < list->root->used ; cnt++)
  765. {
  766. arg2_buffer[cnt] = list->root->list[cnt]->arg2;
  767. }
  768. quadsort(arg2_buffer, list->root->used, sizeof(char *), cmp_str);
  769. for (cnt = 0 ; cnt < list->root->used ; cnt++)
  770. {
  771. list->root->list[cnt]->arg2 = arg2_buffer[cnt];
  772. }
  773. free(arg2_buffer);
  774. }
  775. }
  776. return ses;
  777. }
  778. DO_ARRAY(array_tokenize)
  779. {
  780. char *buf;
  781. int index = 1, i;
  782. buf = str_alloc_stack(0);
  783. substitute(ses, arg, buf, SUB_VAR|SUB_FUN);
  784. arg = buf;
  785. if (list->root)
  786. {
  787. free_list(list->root);
  788. }
  789. list->root = init_list(ses, LIST_VARIABLE, LIST_SIZE);
  790. while (*arg)
  791. {
  792. arg = get_arg_in_braces(ses, arg, arg1, GET_ALL);
  793. i = 0;
  794. while (arg1[i] != 0)
  795. {
  796. if (arg1[i] == '{')
  797. {
  798. strcpy(arg2, "\\x7B"); i++;
  799. }
  800. else if (arg1[i] == '}')
  801. {
  802. strcpy(arg2, "\\x7D"); i++;
  803. }
  804. else if (arg1[i] == '\\' && arg1[i+1] == 'x' && is_hex(arg1[i+2]) && is_hex(arg1[i+3]))
  805. {
  806. i += sprintf(arg2, "%.4s", &arg1[i]);
  807. }
  808. else if (HAS_BIT(ses->charset, CHARSET_FLAG_EUC) && is_euc_head(ses, &arg1[i]))
  809. {
  810. i += sprintf(arg2, "%.*s", get_euc_size(ses, &arg1[i]), &arg1[i]);
  811. }
  812. else if (HAS_BIT(ses->charset, CHARSET_FLAG_UTF8) && is_utf8_head(&arg1[i]))
  813. {
  814. i += sprintf(arg2, "%.*s", get_utf8_size(&arg1[i]), &arg1[i]);
  815. }
  816. else
  817. {
  818. i += sprintf(arg2, "%c", arg1[i]);
  819. }
  820. set_nest_node(list->root, ntos(index++), "%s", arg2);
  821. }
  822. if (*arg == COMMAND_SEPARATOR)
  823. {
  824. arg++;
  825. }
  826. }
  827. return ses;
  828. }