class.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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. };
  43. struct class_type class_table[] =
  44. {
  45. { "ASSIGN", class_assign },
  46. { "CLEAR", class_clear },
  47. { "CLOSE", class_close },
  48. { "KILL", class_kill },
  49. { "LIST", class_list },
  50. { "LOAD", class_load },
  51. { "OPEN", class_open },
  52. { "READ", class_read },
  53. { "SAVE", class_save },
  54. { "SIZE", class_size },
  55. { "WRITE", class_write },
  56. { "", NULL },
  57. };
  58. int count_class(struct session *ses, struct listnode *group);
  59. DO_COMMAND(do_class)
  60. {
  61. int i;
  62. struct listroot *root;
  63. struct listnode *node;
  64. root = ses->list[LIST_CLASS];
  65. arg = sub_arg_in_braces(ses, arg, arg1, GET_ONE, SUB_VAR|SUB_FUN);
  66. arg = sub_arg_in_braces(ses, arg, arg2, GET_ONE, SUB_VAR|SUB_FUN);
  67. arg = sub_arg_in_braces(ses, arg, arg3, GET_ALL, SUB_VAR|SUB_FUN);
  68. if (*arg1 == 0)
  69. {
  70. tintin_header(ses, 80, " CLASSES ");
  71. for (root->update = 0 ; root->update < root->used ; root->update++)
  72. {
  73. node = root->list[root->update];
  74. 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));
  75. }
  76. }
  77. else if (*arg2 == 0)
  78. {
  79. class_list(ses, NULL, arg1, arg2);
  80. }
  81. else
  82. {
  83. for (i = 0 ; *class_table[i].name ; i++)
  84. {
  85. if (is_abbrev(arg2, class_table[i].name))
  86. {
  87. break;
  88. }
  89. }
  90. if (*class_table[i].name == 0)
  91. {
  92. show_error(ses, LIST_COMMAND, "#SYNTAX: CLASS {name} {OPEN|CLEAR|CLOSE|READ|SIZE|WRITE|KILL}.", arg1, capitalize(arg2));
  93. }
  94. else
  95. {
  96. node = search_node_list(ses->list[LIST_CLASS], arg1);
  97. if (node == NULL)
  98. {
  99. show_info(ses, LIST_CLASS, "#INFO: CLASS {%s} CREATED", arg1);
  100. check_all_events(ses, EVENT_FLAG_CLASS, 0, 1, "CLASS CREATED", arg1);
  101. check_all_events(ses, EVENT_FLAG_CLASS, 1, 1, "CLASS CREATED %s", arg1, arg1);
  102. node = update_node_list(ses->list[LIST_CLASS], arg1, "", arg3, "");
  103. }
  104. class_table[i].fun(ses, node, arg1, arg3);
  105. }
  106. }
  107. return ses;
  108. }
  109. int count_class(struct session *ses, struct listnode *group)
  110. {
  111. int list, cnt, index;
  112. for (cnt = list = 0 ; list < LIST_MAX ; list++)
  113. {
  114. if (!HAS_BIT(ses->list[list]->flags, LIST_FLAG_CLASS))
  115. {
  116. continue;
  117. }
  118. for (index = 0 ; index < ses->list[list]->used ; index++)
  119. {
  120. if (!strcmp(ses->list[list]->list[index]->group, group->arg1))
  121. {
  122. cnt++;
  123. }
  124. }
  125. }
  126. return cnt;
  127. }
  128. DO_CLASS(class_assign)
  129. {
  130. char *tmp = ses->group;
  131. ses->group = strdup(arg1);
  132. script_driver(ses, LIST_COMMAND, arg2);
  133. free(ses->group);
  134. ses->group = tmp;
  135. return ses;
  136. }
  137. DO_CLASS(class_clear)
  138. {
  139. int type, index;
  140. check_all_events(ses, EVENT_FLAG_CLASS, 0, 1, "CLASS CLEAR", ses->group);
  141. check_all_events(ses, EVENT_FLAG_CLASS, 1, 1, "CLASS CLEAR %s", ses->group, ses->group);
  142. for (type = 0 ; type < LIST_MAX ; type++)
  143. {
  144. if (!HAS_BIT(ses->list[type]->flags, LIST_FLAG_CLASS))
  145. {
  146. continue;
  147. }
  148. for (index = 0 ; index < ses->list[type]->used ; index++)
  149. {
  150. if (!strcmp(ses->list[type]->list[index]->group, arg1))
  151. {
  152. delete_index_list(ses->list[type], index--);
  153. }
  154. }
  155. }
  156. show_message(ses, LIST_CLASS, "#CLASS {%s} HAS BEEN CLEARED.", arg1);
  157. if (!strcmp(ses->group, arg1))
  158. {
  159. class_close(ses, node, arg1, arg2);
  160. }
  161. return ses;
  162. }
  163. DO_CLASS(class_close)
  164. {
  165. node = search_node_list(ses->list[LIST_CLASS], arg1);
  166. if (node == NULL)
  167. {
  168. show_message(ses, LIST_CLASS, "#CLASS {%s} NO LONGER EXIST.", arg1);
  169. }
  170. else
  171. {
  172. if (atoi(node->arg3) == 0)
  173. {
  174. show_message(ses, LIST_CLASS, "#CLASS {%s} IS ALREADY CLOSED.", arg1);
  175. }
  176. else
  177. {
  178. show_message(ses, LIST_CLASS, "#CLASS {%s} HAS BEEN CLOSED.", arg1);
  179. update_node_list(ses->list[LIST_CLASS], arg1, "", "0", node->arg4);
  180. if (!strcmp(ses->group, arg1))
  181. {
  182. check_all_events(ses, EVENT_FLAG_CLASS, 0, 1, "CLASS DEACTIVATED", ses->group);
  183. check_all_events(ses, EVENT_FLAG_CLASS, 1, 1, "CLASS DEACTIVATED %s", ses->group, ses->group);
  184. node = ses->list[LIST_CLASS]->list[0];
  185. if (atoi(node->arg3))
  186. {
  187. RESTRING(ses->group, node->arg1);
  188. show_message(ses, LIST_CLASS, "#CLASS {%s} HAS BEEN ACTIVATED.", node->arg1);
  189. check_all_events(ses, EVENT_FLAG_CLASS, 0, 1, "CLASS ACTIVATED", node->arg1);
  190. check_all_events(ses, EVENT_FLAG_CLASS, 1, 1, "CLASS ACTIVATED %s", arg1, arg1);
  191. }
  192. else
  193. {
  194. RESTRING(ses->group, "");
  195. }
  196. }
  197. }
  198. }
  199. return ses;
  200. }
  201. DO_CLASS(class_list)
  202. {
  203. int i, j;
  204. if (search_node_list(ses->list[LIST_CLASS], arg1))
  205. {
  206. tintin_header(ses, 80, " %s ", arg1);
  207. for (i = 0 ; i < LIST_MAX ; i++)
  208. {
  209. if (!HAS_BIT(ses->list[i]->flags, LIST_FLAG_CLASS))
  210. {
  211. continue;
  212. }
  213. if (*arg2 && !is_abbrev(arg2, list_table[i].name) && !is_abbrev(arg2, list_table[i].name_multi))
  214. {
  215. continue;
  216. }
  217. for (j = 0 ; j < ses->list[i]->used ; j++)
  218. {
  219. if (!strcmp(ses->list[i]->list[j]->group, arg1))
  220. {
  221. show_node(ses->list[i], ses->list[i]->list[j], 0);
  222. }
  223. }
  224. }
  225. }
  226. else
  227. {
  228. show_error(ses, LIST_CLASS, "#CLASS {%s} DOES NOT EXIST.", arg1);
  229. }
  230. return ses;
  231. }
  232. DO_CLASS(class_kill)
  233. {
  234. int group;
  235. class_clear(ses, node, arg1, arg2);
  236. group = search_index_list(ses->list[LIST_CLASS], arg1, NULL);
  237. delete_index_list(ses->list[LIST_CLASS], group);
  238. check_all_events(ses, EVENT_FLAG_CLASS, 0, 1, "CLASS DESTROYED", arg1);
  239. check_all_events(ses, EVENT_FLAG_CLASS, 1, 1, "CLASS DESTROYED %s", arg1, arg1);
  240. show_message(ses, LIST_CLASS, "#CLASS {%s} HAS BEEN KILLED.", arg1);
  241. return ses;
  242. }
  243. DO_CLASS(class_load)
  244. {
  245. FILE *file;
  246. if (node->data == NULL)
  247. {
  248. show_error(ses, LIST_CLASS, "#CLASS {%s} DOES NOT HAVE ANY DATA SAVED.", arg1);
  249. return ses;
  250. }
  251. file = fmemopen(node->data, (size_t) atoi(node->arg4), "r");
  252. read_file(ses, file, arg1);
  253. fclose(file);
  254. check_all_events(ses, EVENT_FLAG_CLASS, 0, 1, "CLASS LOAD", arg1);
  255. check_all_events(ses, EVENT_FLAG_CLASS, 1, 1, "CLASS LOAD %s", arg1, arg1);
  256. show_message(ses, LIST_CLASS, "#CLASS {%s} HAS BEEN LOADED FROM MEMORY.", arg1);
  257. return ses;
  258. }
  259. DO_CLASS(class_open)
  260. {
  261. int count;
  262. if (!strcmp(ses->group, arg1))
  263. {
  264. show_message(ses, LIST_CLASS, "#CLASS {%s} IS ALREADY OPENED AND ACTIVATED.", arg1);
  265. }
  266. else
  267. {
  268. if (*ses->group)
  269. {
  270. check_all_events(ses, EVENT_FLAG_CLASS, 0, 1, "CLASS DEACTIVATED", ses->group);
  271. check_all_events(ses, EVENT_FLAG_CLASS, 1, 1, "CLASS DEACTIVATED %s", ses->group, ses->group);
  272. }
  273. RESTRING(ses->group, arg1);
  274. count = atoi(ses->list[LIST_CLASS]->list[0]->arg3);
  275. update_node_list(ses->list[LIST_CLASS], arg1, "", ntos(--count), node->arg4);
  276. show_message(ses, LIST_CLASS, "#CLASS {%s} HAS BEEN OPENED AND ACTIVATED.", arg1);
  277. check_all_events(ses, EVENT_FLAG_CLASS, 0, 1, "CLASS ACTIVATED", arg1);
  278. check_all_events(ses, EVENT_FLAG_CLASS, 1, 1, "CLASS ACTIVATED %s", arg1, arg1);
  279. }
  280. return ses;
  281. }
  282. DO_CLASS(class_read)
  283. {
  284. class_open(ses, node, arg1, arg2);
  285. command(ses, do_read, "{%s}", arg2);
  286. class_close(ses, node, arg1, arg2);
  287. return ses;
  288. }
  289. DO_CLASS(class_save)
  290. {
  291. FILE *file;
  292. size_t len;
  293. int list, index;
  294. file = open_memstream(&node->data, (size_t *) &len);
  295. fprintf(file, "%cCLASS {%s} OPEN\n\n", gtd->tintin_char, arg1);
  296. for (list = 0 ; list < LIST_MAX ; list++)
  297. {
  298. if (!HAS_BIT(ses->list[list]->flags, LIST_FLAG_CLASS))
  299. {
  300. continue;
  301. }
  302. for (index = 0 ; index < ses->list[list]->used ; index++)
  303. {
  304. if (!strcmp(ses->list[list]->list[index]->group, arg1))
  305. {
  306. write_node(ses, list, ses->list[list]->list[index], file);
  307. }
  308. }
  309. }
  310. fprintf(file, "\n%cCLASS {%s} CLOSE\n", gtd->tintin_char, arg1);
  311. fclose(file);
  312. str_cpy_printf(&node->arg4, "%d", len);
  313. show_message(ses, LIST_CLASS, "#CLASS {%s} HAS BEEN SAVED TO MEMORY.", arg1);
  314. return ses;
  315. }
  316. DO_CLASS(class_size)
  317. {
  318. if (*arg1 == 0 || *arg2 == 0)
  319. {
  320. show_error(ses, LIST_CLASS, "#SYNTAX: #CLASS {<class name>} SIZE {<variable>}.");
  321. return ses;
  322. }
  323. set_nest_node_ses(ses, arg2, "%d", count_class(ses, node));
  324. return ses;
  325. }
  326. DO_CLASS(class_write)
  327. {
  328. FILE *file;
  329. int list, index;
  330. if (*arg2 == 0 || (file = fopen(arg2, "w")) == NULL)
  331. {
  332. show_error(ses, LIST_CLASS, "#ERROR: #CLASS WRITE {%s} - COULDN'T OPEN FILE TO WRITE.", arg2);
  333. return ses;
  334. }
  335. fprintf(file, "%cCLASS {%s} OPEN\n\n", gtd->tintin_char, arg1);
  336. for (list = 0 ; list < LIST_MAX ; list++)
  337. {
  338. if (!HAS_BIT(ses->list[list]->flags, LIST_FLAG_CLASS))
  339. {
  340. continue;
  341. }
  342. for (index = 0 ; index < ses->list[list]->used ; index++)
  343. {
  344. if (!strcmp(ses->list[list]->list[index]->group, arg1))
  345. {
  346. write_node(ses, list, ses->list[list]->list[index], file);
  347. }
  348. }
  349. }
  350. fprintf(file, "\n%cCLASS {%s} CLOSE\n", gtd->tintin_char, arg1);
  351. fclose(file);
  352. show_message(ses, LIST_CLASS, "#CLASS {%s} HAS BEEN WRITTEN TO FILE.", arg1);
  353. return ses;
  354. }