class.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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 2004 *
  23. ******************************************************************************/
  24. #include "tintin.h"
  25. #define DO_CLASS(class) struct session *class(struct session *ses, struct listnode *node, char *arg1, char *arg2)
  26. extern DO_CLASS(class_assign);
  27. extern DO_CLASS(class_clear);
  28. extern DO_CLASS(class_close);
  29. extern DO_CLASS(class_kill);
  30. extern DO_CLASS(class_list);
  31. extern DO_CLASS(class_load);
  32. extern DO_CLASS(class_open);
  33. extern DO_CLASS(class_read);
  34. extern DO_CLASS(class_save);
  35. extern DO_CLASS(class_size);
  36. extern DO_CLASS(class_write);
  37. typedef struct session *CLASS(struct session *ses, struct listnode *node, char *arg1, char *arg2);
  38. struct class_type
  39. {
  40. char * name;
  41. CLASS * fun;
  42. char * desc;
  43. };
  44. struct class_type class_table[] =
  45. {
  46. { "ASSIGN", class_assign, "Execute command with given class opened." },
  47. { "CLEAR", class_clear, "Delete triggers associated with given class." },
  48. { "CLOSE", class_close, "Close given class." },
  49. { "KILL", class_kill, "Clear, close, and remove given class." },
  50. { "LIST", class_list, "List triggers associated with given class." },
  51. { "LOAD", class_load, "Load saved data of given class." },
  52. { "OPEN", class_open, "Open given class." },
  53. { "READ", class_read, "Read file with given class opened." },
  54. { "SAVE", class_save, "Save given class to memory." },
  55. { "SIZE", class_size, "Store size of given class to a variable." },
  56. { "WRITE", class_write, "Write given class to file." },
  57. { "", NULL, NULL }
  58. };
  59. int count_class(struct session *ses, struct listnode *group);
  60. DO_COMMAND(do_class)
  61. {
  62. int i, cnt;
  63. struct listroot *root;
  64. struct listnode *node;
  65. root = ses->list[LIST_CLASS];
  66. arg = sub_arg_in_braces(ses, arg, arg1, GET_ONE, SUB_VAR|SUB_FUN);
  67. arg = sub_arg_in_braces(ses, arg, arg2, GET_ONE, SUB_VAR|SUB_FUN);
  68. arg = sub_arg_in_braces(ses, arg, arg3, GET_ALL, SUB_VAR|SUB_FUN);
  69. if (*arg1 == 0)
  70. {
  71. tintin_header(ses, 80, " CLASSES ");
  72. for (root->update = 0 ; root->update < root->used ; root->update++)
  73. {
  74. node = root->list[root->update];
  75. tintin_printf2(ses, "%-20s %4d %6s %6s %3d", node->arg1, count_class(ses, node), !strcmp(ses->group, node->arg1) ? "ACTIVE" : "", atoi(node->arg3) ? "OPEN" : "CLOSED", atoi(node->arg3));
  76. }
  77. }
  78. else if (*arg2 == 0)
  79. {
  80. node = search_node_list(ses->list[LIST_CLASS], arg1);
  81. class_list(ses, node, arg1, arg2);
  82. }
  83. else
  84. {
  85. for (i = 0 ; *class_table[i].name ; i++)
  86. {
  87. if (is_abbrev(arg2, class_table[i].name))
  88. {
  89. break;
  90. }
  91. }
  92. if (*class_table[i].name == 0)
  93. {
  94. tintin_header(ses, 80, " CLASS OPTIONS ");
  95. for (cnt = 0 ; *class_table[cnt].fun != NULL ; cnt++)
  96. {
  97. if (*class_table[cnt].desc)
  98. {
  99. tintin_printf2(ses, " [%-13s] %s", class_table[cnt].name, class_table[cnt].desc);
  100. }
  101. }
  102. tintin_header(ses, 80, "");
  103. }
  104. else
  105. {
  106. node = search_node_list(ses->list[LIST_CLASS], arg1);
  107. class_table[i].fun(ses, node, arg1, arg3);
  108. }
  109. }
  110. return ses;
  111. }
  112. struct listnode *create_class(struct session *ses, char *arg1, char *arg3)
  113. {
  114. show_info(ses, LIST_CLASS, "#INFO: CLASS {%s} CREATED", arg1);
  115. check_all_events(ses, EVENT_FLAG_CLASS, 0, 1, "CLASS CREATED", arg1);
  116. check_all_events(ses, EVENT_FLAG_CLASS, 1, 1, "CLASS CREATED %s", arg1, arg1);
  117. return update_node_list(ses->list[LIST_CLASS], arg1, "", arg3, "");
  118. }
  119. int count_class(struct session *ses, struct listnode *group)
  120. {
  121. int list, cnt, index;
  122. for (cnt = list = 0 ; list < LIST_MAX ; list++)
  123. {
  124. if (!HAS_BIT(ses->list[list]->flags, LIST_FLAG_CLASS))
  125. {
  126. continue;
  127. }
  128. for (index = 0 ; index < ses->list[list]->used ; index++)
  129. {
  130. if (!strcmp(ses->list[list]->list[index]->group, group->arg1))
  131. {
  132. cnt++;
  133. }
  134. }
  135. }
  136. return cnt;
  137. }
  138. void clear_class(struct session *ses, struct listnode *group)
  139. {
  140. size_t type, index;
  141. check_all_events(ses, EVENT_FLAG_CLASS, 0, 1, "CLASS CLEAR", group->arg1);
  142. check_all_events(ses, EVENT_FLAG_CLASS, 1, 1, "CLASS CLEAR %s", group->arg1, group->arg1);
  143. for (type = 0 ; type < LIST_MAX ; type++)
  144. {
  145. if (!HAS_BIT(ses->list[type]->flags, LIST_FLAG_CLASS))
  146. {
  147. continue;
  148. }
  149. for (index = 0 ; index < ses->list[type]->used ; index++)
  150. {
  151. if (!strcmp(ses->list[type]->list[index]->group, group->arg1))
  152. {
  153. delete_index_list(ses->list[type], index--);
  154. }
  155. }
  156. }
  157. }
  158. DO_CLASS(class_assign)
  159. {
  160. if (node == NULL)
  161. {
  162. node = create_class(ses, arg1, arg2);
  163. }
  164. char *tmp = ses->group;
  165. ses->group = strdup(arg1);
  166. script_driver(ses, LIST_COMMAND, arg2);
  167. free(ses->group);
  168. ses->group = tmp;
  169. return ses;
  170. }
  171. DO_CLASS(class_clear)
  172. {
  173. if (node == NULL)
  174. {
  175. show_message(ses, LIST_CLASS, "#CLASS {%s} DOES NOT EXIST.", arg1);
  176. return ses;
  177. }
  178. clear_class(ses, node);
  179. show_message(ses, LIST_CLASS, "#CLASS {%s} HAS BEEN CLEARED.", arg1);
  180. if (!strcmp(ses->group, arg1))
  181. {
  182. class_close(ses, node, arg1, arg2);
  183. }
  184. return ses;
  185. }
  186. DO_CLASS(class_close)
  187. {
  188. if (node == NULL)
  189. {
  190. node = search_node_list(ses->list[LIST_CLASS], arg1);
  191. if (node == NULL)
  192. {
  193. show_message(ses, LIST_CLASS, "#CLASS {%s} DOES NOT EXIST.", arg1);
  194. return ses;
  195. }
  196. }
  197. if (atoi(node->arg3) == 0)
  198. {
  199. show_message(ses, LIST_CLASS, "#CLASS {%s} IS ALREADY CLOSED.", arg1);
  200. }
  201. else
  202. {
  203. show_message(ses, LIST_CLASS, "#CLASS {%s} HAS BEEN CLOSED.", arg1);
  204. update_node_list(ses->list[LIST_CLASS], arg1, "", "0", node->arg4);
  205. if (!strcmp(ses->group, arg1))
  206. {
  207. check_all_events(ses, EVENT_FLAG_CLASS, 0, 1, "CLASS DEACTIVATED", ses->group);
  208. check_all_events(ses, EVENT_FLAG_CLASS, 1, 1, "CLASS DEACTIVATED %s", ses->group, ses->group);
  209. node = ses->list[LIST_CLASS]->list[0];
  210. if (atoi(node->arg3))
  211. {
  212. RESTRING(ses->group, node->arg1);
  213. show_message(ses, LIST_CLASS, "#CLASS {%s} HAS BEEN ACTIVATED.", node->arg1);
  214. check_all_events(ses, EVENT_FLAG_CLASS, 0, 1, "CLASS ACTIVATED", node->arg1);
  215. check_all_events(ses, EVENT_FLAG_CLASS, 1, 1, "CLASS ACTIVATED %s", arg1, arg1);
  216. }
  217. else
  218. {
  219. RESTRING(ses->group, "");
  220. }
  221. }
  222. }
  223. return ses;
  224. }
  225. DO_CLASS(class_list)
  226. {
  227. int i, j;
  228. if (node)
  229. {
  230. tintin_header(ses, 80, " %s ", arg1);
  231. for (i = 0 ; i < LIST_MAX ; i++)
  232. {
  233. if (!HAS_BIT(ses->list[i]->flags, LIST_FLAG_CLASS))
  234. {
  235. continue;
  236. }
  237. if (*arg2 && !is_abbrev(arg2, list_table[i].name) && !is_abbrev(arg2, list_table[i].name_multi))
  238. {
  239. continue;
  240. }
  241. for (j = 0 ; j < ses->list[i]->used ; j++)
  242. {
  243. if (!strcmp(ses->list[i]->list[j]->group, arg1))
  244. {
  245. show_node(ses->list[i], ses->list[i]->list[j], 0);
  246. }
  247. }
  248. }
  249. }
  250. else
  251. {
  252. show_error(ses, LIST_CLASS, "#CLASS {%s} DOES NOT EXIST.", arg1);
  253. }
  254. return ses;
  255. }
  256. DO_CLASS(class_kill)
  257. {
  258. if (node == NULL)
  259. {
  260. show_message(ses, LIST_CLASS, "#CLASS {%s} DOES NOT EXIST.", arg1);
  261. return ses;
  262. }
  263. // Best to handle this in delete_node so it works with #kill
  264. // class_clear(ses, node, arg1, arg2);
  265. delete_node_list(ses, LIST_CLASS, node);
  266. // group = search_index_list(ses->list[LIST_CLASS], arg1, NULL);
  267. // delete_index_list(ses->list[LIST_CLASS], group);
  268. // check_all_events(ses, EVENT_FLAG_CLASS, 0, 1, "CLASS DESTROYED", arg1);
  269. // check_all_events(ses, EVENT_FLAG_CLASS, 1, 1, "CLASS DESTROYED %s", arg1, arg1);
  270. show_message(ses, LIST_CLASS, "#CLASS {%s} HAS BEEN KILLED.", arg1);
  271. return ses;
  272. }
  273. DO_CLASS(class_load)
  274. {
  275. FILE *file;
  276. if (node == NULL)
  277. {
  278. show_error(ses, LIST_CLASS, "#CLASS {%s} DOES NOT EXIST.", arg1);
  279. return ses;
  280. }
  281. if (node->data == NULL)
  282. {
  283. show_error(ses, LIST_CLASS, "#CLASS {%s} DOES NOT HAVE ANY DATA SAVED.", arg1);
  284. return ses;
  285. }
  286. file = fmemopen(node->data, (size_t) atoi(node->arg4), "r");
  287. read_file(ses, file, arg1);
  288. fclose(file);
  289. check_all_events(ses, EVENT_FLAG_CLASS, 0, 1, "CLASS LOAD", arg1);
  290. check_all_events(ses, EVENT_FLAG_CLASS, 1, 1, "CLASS LOAD %s", arg1, arg1);
  291. show_message(ses, LIST_CLASS, "#CLASS {%s} HAS BEEN LOADED FROM MEMORY.", arg1);
  292. return ses;
  293. }
  294. DO_CLASS(class_open)
  295. {
  296. int count;
  297. if (node == NULL)
  298. {
  299. node = create_class(ses, arg1, arg2);
  300. }
  301. if (!strcmp(ses->group, arg1))
  302. {
  303. show_message(ses, LIST_CLASS, "#CLASS {%s} IS ALREADY OPENED AND ACTIVATED.", arg1);
  304. }
  305. else
  306. {
  307. if (*ses->group)
  308. {
  309. check_all_events(ses, EVENT_FLAG_CLASS, 0, 1, "CLASS DEACTIVATED", ses->group);
  310. check_all_events(ses, EVENT_FLAG_CLASS, 1, 1, "CLASS DEACTIVATED %s", ses->group, ses->group);
  311. }
  312. RESTRING(ses->group, arg1);
  313. count = atoi(ses->list[LIST_CLASS]->list[0]->arg3);
  314. update_node_list(ses->list[LIST_CLASS], arg1, "", ntos(--count), node->arg4);
  315. show_message(ses, LIST_CLASS, "#CLASS {%s} HAS BEEN OPENED AND ACTIVATED.", arg1);
  316. check_all_events(ses, EVENT_FLAG_CLASS, 0, 1, "CLASS ACTIVATED", arg1);
  317. check_all_events(ses, EVENT_FLAG_CLASS, 1, 1, "CLASS ACTIVATED %s", arg1, arg1);
  318. }
  319. return ses;
  320. }
  321. DO_CLASS(class_read)
  322. {
  323. class_open(ses, node, arg1, arg2);
  324. command(ses, do_read, "{%s}", arg2);
  325. class_close(ses, node, arg1, arg2);
  326. return ses;
  327. }
  328. DO_CLASS(class_save)
  329. {
  330. FILE *file;
  331. size_t len;
  332. int list, index;
  333. if (node == NULL)
  334. {
  335. show_error(ses, LIST_CLASS, "#ERROR: #CLASS {%s} DOES NOT EXIST.", arg1);
  336. return ses;
  337. }
  338. file = open_memstream(&node->data, (size_t *) &len);
  339. fprintf(file, "%cCLASS {%s} OPEN\n\n", gtd->tintin_char, arg1);
  340. for (list = 0 ; list < LIST_MAX ; list++)
  341. {
  342. if (!HAS_BIT(ses->list[list]->flags, LIST_FLAG_CLASS))
  343. {
  344. continue;
  345. }
  346. for (index = 0 ; index < ses->list[list]->used ; index++)
  347. {
  348. if (!strcmp(ses->list[list]->list[index]->group, arg1))
  349. {
  350. write_node(ses, list, ses->list[list]->list[index], file);
  351. }
  352. }
  353. }
  354. fprintf(file, "\n%cCLASS {%s} CLOSE\n", gtd->tintin_char, arg1);
  355. fclose(file);
  356. str_cpy_printf(&node->arg4, "%d", len);
  357. show_message(ses, LIST_CLASS, "#CLASS {%s} HAS BEEN SAVED TO MEMORY.", arg1);
  358. return ses;
  359. }
  360. DO_CLASS(class_size)
  361. {
  362. if (*arg1 == 0 || *arg2 == 0)
  363. {
  364. show_error(ses, LIST_CLASS, "#SYNTAX: #CLASS <NAME> SIZE <VARIABLE>");
  365. return ses;
  366. }
  367. set_nest_node_ses(ses, arg2, "%d", node ? count_class(ses, node) : 0);
  368. return ses;
  369. }
  370. DO_CLASS(class_write)
  371. {
  372. FILE *file;
  373. int list, index;
  374. if (node == NULL)
  375. {
  376. show_error(ses, LIST_CLASS, "#ERROR: #CLASS {%s} DOES NOT EXIST.", arg1);
  377. return ses;
  378. }
  379. if (*arg2 == 0 || (file = fopen(arg2, "w")) == NULL)
  380. {
  381. show_error(ses, LIST_CLASS, "#ERROR: #CLASS WRITE {%s}: COULDN'T OPEN FILE.", arg2);
  382. return ses;
  383. }
  384. fprintf(file, "%cCLASS {%s} OPEN\n\n", gtd->tintin_char, arg1);
  385. for (list = 0 ; list < LIST_MAX ; list++)
  386. {
  387. if (!HAS_BIT(ses->list[list]->flags, LIST_FLAG_CLASS))
  388. {
  389. continue;
  390. }
  391. for (index = 0 ; index < ses->list[list]->used ; index++)
  392. {
  393. if (!strcmp(ses->list[list]->list[index]->group, arg1))
  394. {
  395. write_node(ses, list, ses->list[list]->list[index], file);
  396. }
  397. }
  398. }
  399. fprintf(file, "\n%cCLASS {%s} CLOSE\n", gtd->tintin_char, arg1);
  400. fclose(file);
  401. show_message(ses, LIST_CLASS, "#CLASS {%s} HAS BEEN WRITTEN TO FILE.", arg1);
  402. return ses;
  403. }