class.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. DO_COMMAND(do_class)
  27. {
  28. char arg1[BUFFER_SIZE], arg2[BUFFER_SIZE], arg3[BUFFER_SIZE];
  29. int i;
  30. struct listroot *root;
  31. struct listnode *node;
  32. root = ses->list[LIST_CLASS];
  33. arg = sub_arg_in_braces(ses, arg, arg1, GET_ONE, SUB_VAR|SUB_FUN);
  34. arg = sub_arg_in_braces(ses, arg, arg2, GET_ONE, SUB_VAR|SUB_FUN);
  35. arg = sub_arg_in_braces(ses, arg, arg3, GET_ONE, SUB_VAR|SUB_FUN);
  36. if (*arg1 == 0)
  37. {
  38. tintin_header(ses, " CLASSES ");
  39. for (root->update = 0 ; root->update < root->used ; root->update++)
  40. {
  41. node = root->list[root->update];
  42. 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));
  43. }
  44. }
  45. else if (*arg2 == 0)
  46. {
  47. class_list(ses, arg1, arg2);
  48. }
  49. else
  50. {
  51. for (i = 0 ; *class_table[i].name ; i++)
  52. {
  53. if (is_abbrev(arg2, class_table[i].name))
  54. {
  55. break;
  56. }
  57. }
  58. if (*class_table[i].name == 0)
  59. {
  60. show_error(ses, LIST_COMMAND, "#SYNTAX: CLASS {name} {OPEN|CLOSE|READ|SIZE|WRITE|KILL}.", arg1, capitalize(arg2));
  61. }
  62. else
  63. {
  64. if (!search_node_list(ses->list[LIST_CLASS], arg1))
  65. {
  66. update_node_list(ses->list[LIST_CLASS], arg1, arg2, arg3, "");
  67. }
  68. class_table[i].group(ses, arg1, arg3);
  69. }
  70. }
  71. return ses;
  72. }
  73. int count_class(struct session *ses, struct listnode *group)
  74. {
  75. int list, cnt, index;
  76. for (cnt = list = 0 ; list < LIST_MAX ; list++)
  77. {
  78. if (!HAS_BIT(ses->list[list]->flags, LIST_FLAG_CLASS))
  79. {
  80. continue;
  81. }
  82. for (index = 0 ; index < ses->list[list]->used ; index++)
  83. {
  84. if (!strcmp(ses->list[list]->list[index]->group, group->arg1))
  85. {
  86. cnt++;
  87. }
  88. }
  89. }
  90. return cnt;
  91. }
  92. DO_CLASS(class_open)
  93. {
  94. int count;
  95. if (!strcmp(ses->group, arg1))
  96. {
  97. show_message(ses, LIST_CLASS, "#CLASS {%s} IS ALREADY OPENED AND ACTIVATED.", arg1);
  98. }
  99. else
  100. {
  101. if (*ses->group)
  102. {
  103. check_all_events(ses, SUB_ARG, 0, 1, "CLASS DEACTIVATED", ses->group);
  104. check_all_events(ses, SUB_ARG, 1, 1, "CLASS DEACTIVATED %s", ses->group, ses->group);
  105. }
  106. RESTRING(ses->group, arg1);
  107. count = atoi(ses->list[LIST_CLASS]->list[0]->arg3);
  108. update_node_list(ses->list[LIST_CLASS], arg1, "", ntos(--count), "");
  109. show_message(ses, LIST_CLASS, "#CLASS {%s} HAS BEEN OPENED AND ACTIVATED.", arg1);
  110. check_all_events(ses, SUB_ARG, 0, 1, "CLASS ACTIVATED", arg1);
  111. check_all_events(ses, SUB_ARG, 1, 1, "CLASS ACTIVATED %s", arg1, arg1);
  112. }
  113. return ses;
  114. }
  115. DO_CLASS(class_close)
  116. {
  117. struct listnode *node;
  118. node = search_node_list(ses->list[LIST_CLASS], arg1);
  119. if (node == NULL)
  120. {
  121. show_message(ses, LIST_CLASS, "#CLASS {%s} DOES NOT EXIST.", arg1);
  122. }
  123. else
  124. {
  125. if (atoi(node->arg3) == 0)
  126. {
  127. show_message(ses, LIST_CLASS, "#CLASS {%s} IS ALREADY CLOSED.", arg1);
  128. }
  129. else
  130. {
  131. show_message(ses, LIST_CLASS, "#CLASS {%s} HAS BEEN CLOSED.", arg1);
  132. update_node_list(ses->list[LIST_CLASS], arg1, "", "0","");
  133. if (!strcmp(ses->group, arg1))
  134. {
  135. check_all_events(ses, SUB_ARG, 0, 1, "CLASS DEACTIVATED", ses->group);
  136. check_all_events(ses, SUB_ARG, 1, 1, "CLASS DEACTIVATED %s", ses->group, ses->group);
  137. node = ses->list[LIST_CLASS]->list[0];
  138. if (atoi(node->arg3))
  139. {
  140. RESTRING(ses->group, node->arg1);
  141. show_message(ses, LIST_CLASS, "#CLASS {%s} HAS BEEN ACTIVATED.", node->arg1);
  142. check_all_events(ses, SUB_ARG, 0, 1, "CLASS ACTIVATED", node->arg1);
  143. check_all_events(ses, SUB_ARG, 1, 1, "CLASS ACTIVATED %s", arg1, arg1);
  144. }
  145. else
  146. {
  147. RESTRING(ses->group, "");
  148. }
  149. }
  150. }
  151. }
  152. return ses;
  153. }
  154. DO_CLASS(class_list)
  155. {
  156. int i, j;
  157. if (search_node_list(ses->list[LIST_CLASS], arg1))
  158. {
  159. tintin_header(ses, " %s ", arg1);
  160. for (i = 0 ; i < LIST_MAX ; i++)
  161. {
  162. if (!HAS_BIT(ses->list[i]->flags, LIST_FLAG_CLASS))
  163. {
  164. continue;
  165. }
  166. if (*arg2 && !is_abbrev(arg2, list_table[i].name) && !is_abbrev(arg2, list_table[i].name_multi))
  167. {
  168. continue;
  169. }
  170. for (j = 0 ; j < ses->list[i]->used ; j++)
  171. {
  172. if (!strcmp(ses->list[i]->list[j]->group, arg1))
  173. {
  174. show_node(ses->list[i], ses->list[i]->list[j], 0);
  175. }
  176. }
  177. }
  178. }
  179. else
  180. {
  181. show_error(ses, LIST_CLASS, "#CLASS {%s} DOES NOT EXIST.", arg1);
  182. }
  183. return ses;
  184. }
  185. DO_CLASS(class_read)
  186. {
  187. class_open(ses, arg1, arg2);
  188. do_read(ses, arg2);
  189. class_close(ses, arg1, arg2);
  190. return ses;
  191. }
  192. DO_CLASS(class_write)
  193. {
  194. FILE *file;
  195. int list, index;
  196. if (!search_node_list(ses->list[LIST_CLASS], arg1))
  197. {
  198. show_error(ses, LIST_CLASS, "#CLASS {%s} DOES NOT EXIST.", arg1);
  199. return ses;
  200. }
  201. if (*arg2 == 0 || (file = fopen(arg2, "w")) == NULL)
  202. {
  203. show_error(ses, LIST_CLASS, "#ERROR: #CLASS WRITE {%s} - COULDN'T OPEN FILE TO WRITE.", arg2);
  204. return ses;
  205. }
  206. fprintf(file, "%cCLASS {%s} OPEN\n\n", gtd->tintin_char, arg1);
  207. for (list = 0 ; list < LIST_MAX ; list++)
  208. {
  209. if (!HAS_BIT(ses->list[list]->flags, LIST_FLAG_CLASS))
  210. {
  211. continue;
  212. }
  213. for (index = 0 ; index < ses->list[list]->used ; index++)
  214. {
  215. if (!strcmp(ses->list[list]->list[index]->group, arg1))
  216. {
  217. write_node(ses, list, ses->list[list]->list[index], file);
  218. }
  219. }
  220. }
  221. fprintf(file, "\n%cCLASS {%s} CLOSE\n", gtd->tintin_char, arg1);
  222. fclose(file);
  223. show_message(ses, LIST_CLASS, "#CLASS {%s} HAS BEEN WRITTEN TO FILE.", arg1);
  224. return ses;
  225. }
  226. DO_CLASS(class_kill)
  227. {
  228. int type, index, group;
  229. if (!search_node_list(ses->list[LIST_CLASS], arg1))
  230. {
  231. update_node_list(ses->list[LIST_CLASS], arg1, "", "0", "");
  232. }
  233. group = search_index_list(ses->list[LIST_CLASS], arg1, NULL);
  234. for (type = 0 ; type < LIST_MAX ; type++)
  235. {
  236. if (!HAS_BIT(ses->list[type]->flags, LIST_FLAG_CLASS))
  237. {
  238. continue;
  239. }
  240. for (index = 0 ; index < ses->list[type]->used ; index++)
  241. {
  242. if (!strcmp(ses->list[type]->list[index]->group, arg1))
  243. {
  244. delete_index_list(ses->list[type], index--);
  245. }
  246. }
  247. }
  248. delete_index_list(ses->list[LIST_CLASS], group);
  249. show_message(ses, LIST_CLASS, "#CLASS {%s} HAS BEEN KILLED.", arg1);
  250. if (!strcmp(ses->group, arg1))
  251. {
  252. check_all_events(ses, SUB_ARG, 0, 1, "CLASS DEACTIVATED", ses->group);
  253. struct listnode *node = ses->list[LIST_CLASS]->list[0];
  254. if (node && atoi(node->arg3))
  255. {
  256. RESTRING(ses->group, node->arg1);
  257. show_message(ses, LIST_CLASS, "#CLASS {%s} HAS BEEN ACTIVATED.", node->arg1);
  258. check_all_events(ses, SUB_ARG, 0, 1, "CLASS ACTIVATED", arg1);
  259. }
  260. else
  261. {
  262. RESTRING(ses->group, "");
  263. }
  264. }
  265. return ses;
  266. }
  267. DO_CLASS(class_size)
  268. {
  269. struct listnode *node;
  270. if (*arg1 == 0 || *arg2 == 0)
  271. {
  272. show_error(ses, LIST_CLASS, "#SYNTAX: #CLASS {<class name>} SIZE {<variable>}.");
  273. return ses;
  274. }
  275. node = search_node_list(ses->list[LIST_CLASS], arg1);
  276. if (node == NULL)
  277. {
  278. set_nest_node(ses->list[LIST_VARIABLE], arg2, "0");
  279. }
  280. else
  281. {
  282. set_nest_node(ses->list[LIST_VARIABLE], arg2, "%d", count_class(ses, node));
  283. }
  284. return ses;
  285. }