files.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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 Peter Unold 1992 *
  24. * recoded by Igor van den Hoven 2004 *
  25. ******************************************************************************/
  26. #include "tintin.h"
  27. #include <sys/stat.h>
  28. DO_COMMAND(do_read)
  29. {
  30. char filename[BUFFER_SIZE];
  31. FILE *fp;
  32. sub_arg_in_braces(ses, arg, filename, GET_ONE, SUB_VAR|SUB_FUN);
  33. if ((fp = fopen(filename, "r")) == NULL)
  34. {
  35. check_all_events(ses, SUB_ARG, 0, 2, "READ ERROR", filename, "FILE NOT FOUND.");
  36. tintin_printf(ses, "#READ {%s} - FILE NOT FOUND.", filename);
  37. return ses;
  38. }
  39. return read_file(ses, fp, filename);
  40. }
  41. struct session *read_file(struct session *ses, FILE *fp, char *filename)
  42. {
  43. struct stat filedata;
  44. char *bufi, *bufo, temp[INPUT_SIZE], *pti, *pto, last = 0;
  45. int lvl, cnt, com, lnc, fix, ok;
  46. int counter[LIST_MAX];
  47. temp[0] = getc(fp);
  48. if (!ispunct((int) temp[0]))
  49. {
  50. check_all_events(ses, SUB_ARG, 0, 2, "READ ERROR", filename, "INVALID START OF FILE");
  51. tintin_printf(ses, "#ERROR: #READ {%s} - INVALID START OF FILE '%c'.", filename, temp[0]);
  52. fclose(fp);
  53. return ses;
  54. }
  55. ungetc(temp[0], fp);
  56. for (cnt = 0 ; cnt < LIST_MAX ; cnt++)
  57. {
  58. if (HAS_BIT(list_table[cnt].flags, LIST_FLAG_READ))
  59. {
  60. counter[cnt] = ses->list[cnt]->used;
  61. }
  62. }
  63. stat(filename, &filedata);
  64. if ((bufi = (char *) calloc(1, filedata.st_size + 2)) == NULL || (bufo = (char *) calloc(1, filedata.st_size + 2)) == NULL)
  65. {
  66. check_all_events(ses, SUB_ARG, 0, 2, "READ ERROR", filename, "FAILED TO ALLOCATE MEMORY");
  67. tintin_printf(ses, "#ERROR: #READ {%s} - FAILED TO ALLOCATE MEMORY.", filename);
  68. fclose(fp);
  69. return ses;
  70. }
  71. fread(bufi, 1, filedata.st_size, fp);
  72. pti = bufi;
  73. pto = bufo;
  74. lvl = com = lnc = fix = ok = 0;
  75. while (*pti)
  76. {
  77. if (com == 0)
  78. {
  79. if (HAS_BIT(ses->charset, CHARSET_FLAG_EUC) && is_euc_head(ses, pti))
  80. {
  81. *pto++ = *pti++;
  82. *pto++ = *pti++;
  83. continue;
  84. }
  85. switch (*pti)
  86. {
  87. case DEFAULT_OPEN:
  88. *pto++ = *pti++;
  89. lvl++;
  90. last = DEFAULT_OPEN;
  91. break;
  92. case DEFAULT_CLOSE:
  93. *pto++ = *pti++;
  94. lvl--;
  95. last = DEFAULT_CLOSE;
  96. break;
  97. case COMMAND_SEPARATOR:
  98. *pto++ = *pti++;
  99. last = COMMAND_SEPARATOR;
  100. break;
  101. case ' ':
  102. *pto++ = *pti++;
  103. break;
  104. case '/':
  105. if (lvl == 0 && pti[1] == '*')
  106. {
  107. pti += 2;
  108. com += 1;
  109. }
  110. else
  111. {
  112. *pto++ = *pti++;
  113. }
  114. break;
  115. case '\t':
  116. *pto++ = *pti++;
  117. break;
  118. case '\r':
  119. pti++;
  120. break;
  121. case '\n':
  122. lnc++;
  123. pto--;
  124. while (isspace((int) *pto))
  125. {
  126. pto--;
  127. }
  128. pto++;
  129. if (fix == 0 && pti[1] == gtd->tintin_char)
  130. {
  131. if (lvl == 0)
  132. {
  133. ok = lnc + 1;
  134. }
  135. else
  136. {
  137. fix = lnc;
  138. }
  139. }
  140. if (lvl)
  141. {
  142. pti++;
  143. while (isspace((int) *pti))
  144. {
  145. if (*pti == '\n')
  146. {
  147. lnc++;
  148. if (fix == 0 && pti[1] == gtd->tintin_char)
  149. {
  150. fix = lnc;
  151. }
  152. }
  153. pti++;
  154. }
  155. if (*pti != DEFAULT_CLOSE && last == 0)
  156. {
  157. *pto++ = ' ';
  158. }
  159. }
  160. else for (cnt = 1 ; ; cnt++)
  161. {
  162. if (pti[cnt] == 0)
  163. {
  164. *pto++ = *pti++;
  165. break;
  166. }
  167. if (pti[cnt] == DEFAULT_OPEN)
  168. {
  169. pti++;
  170. while (isspace((int) *pti))
  171. {
  172. pti++;
  173. }
  174. *pto++ = ' ';
  175. break;
  176. }
  177. if (!isspace((int) pti[cnt]))
  178. {
  179. *pto++ = *pti++;
  180. break;
  181. }
  182. }
  183. break;
  184. default:
  185. *pto++ = *pti++;
  186. last = 0;
  187. break;
  188. }
  189. }
  190. else
  191. {
  192. switch (*pti)
  193. {
  194. case '/':
  195. if (pti[1] == '*')
  196. {
  197. pti += 2;
  198. com += 1;
  199. }
  200. else
  201. {
  202. pti += 1;
  203. }
  204. break;
  205. case '*':
  206. if (pti[1] == '/')
  207. {
  208. pti += 2;
  209. com -= 1;
  210. }
  211. else
  212. {
  213. pti += 1;
  214. }
  215. break;
  216. case '\n':
  217. lnc++;
  218. pti++;
  219. break;
  220. default:
  221. pti++;
  222. break;
  223. }
  224. }
  225. }
  226. *pto++ = '\n';
  227. *pto = '\0';
  228. if (lvl)
  229. {
  230. check_all_events(ses, SUB_ARG, 0, 2, "READ ERROR", filename, "MISSING BRACE OPEN OR CLOSE");
  231. tintin_printf(ses, "#ERROR: #READ {%s} - MISSING %d '%c' BETWEEN LINE %d AND %d.", filename, abs(lvl), lvl < 0 ? DEFAULT_OPEN : DEFAULT_CLOSE, fix == 0 ? 1 : ok, fix == 0 ? lnc + 1 : fix);
  232. fclose(fp);
  233. free(bufi);
  234. free(bufo);
  235. return ses;
  236. }
  237. if (com)
  238. {
  239. check_all_events(ses, SUB_ARG, 0, 2, "READ ERROR", filename, "MISSING COMMENT OPEN OR CLOSE");
  240. tintin_printf(ses, "#ERROR: #READ {%s} - MISSING %d '%s'", filename, abs(com), com < 0 ? "/*" : "*/");
  241. fclose(fp);
  242. free(bufi);
  243. free(bufo);
  244. return ses;
  245. }
  246. sprintf(temp, "{TINTIN CHAR} {%c}", bufo[0]);
  247. if (bufo[0] != '#')
  248. {
  249. gtd->level->verbose++;
  250. gtd->level->debug++;
  251. show_error(ses, LIST_COMMAND, "\e[1;5;31mWARNING: SETTING THE COMMAND CHARACTER TO '%c' BECAUSE IT'S THE FIRST CHARACTER IN THE FILE.", bufo[0]);
  252. gtd->level->debug--;
  253. gtd->level->verbose--;
  254. }
  255. gtd->level->quiet++;
  256. do_configure(ses, temp);
  257. lvl = 0;
  258. lnc = 0;
  259. pti = bufo;
  260. pto = bufi;
  261. while (*pti)
  262. {
  263. if (*pti != '\n')
  264. {
  265. *pto++ = *pti++;
  266. continue;
  267. }
  268. lnc++;
  269. *pto = 0;
  270. if (strlen(bufi) >= BUFFER_SIZE)
  271. {
  272. tintin_printf(ses, "#ERROR: #READ {%s} - BUFFER OVERFLOW AT COMMAND: %.30s", filename, bufi);
  273. }
  274. if (bufi[0])
  275. {
  276. ses = script_driver(ses, LIST_COMMAND, bufi);
  277. }
  278. pto = bufi;
  279. pti++;
  280. }
  281. gtd->level->quiet--;
  282. if (!HAS_BIT(ses->flags, SES_FLAG_VERBOSE))
  283. {
  284. for (cnt = 0 ; cnt < LIST_MAX ; cnt++)
  285. {
  286. if (HAS_BIT(list_table[cnt].flags, LIST_FLAG_READ))
  287. {
  288. switch (ses->list[cnt]->used - counter[cnt])
  289. {
  290. case 0:
  291. break;
  292. case 1:
  293. show_message(ses, cnt, "#OK: %3d %s LOADED.", ses->list[cnt]->used - counter[cnt], list_table[cnt].name);
  294. break;
  295. default:
  296. show_message(ses, cnt, "#OK: %3d %s LOADED.", ses->list[cnt]->used - counter[cnt], list_table[cnt].name_multi);
  297. break;
  298. }
  299. }
  300. }
  301. }
  302. fclose(fp);
  303. free(bufi);
  304. free(bufo);
  305. return ses;
  306. }
  307. DO_COMMAND(do_write)
  308. {
  309. FILE *file;
  310. char filename[BUFFER_SIZE], forceful[BUFFER_SIZE];
  311. struct listroot *root;
  312. struct listnode *node;
  313. int i, j, fix, cnt = 0;
  314. arg = get_arg_in_braces(ses, arg, filename, GET_ONE);
  315. arg = get_arg_in_braces(ses, arg, forceful, GET_ONE);
  316. if (*filename == 0)
  317. {
  318. check_all_events(ses, SUB_ARG, 0, 2, "WRITE ERROR", filename, "INVALID FILE NAME");
  319. tintin_printf2(ses, "#SYNTAX: #WRITE {<filename>} {[FORCE]}");
  320. return ses;
  321. }
  322. if (!str_suffix(filename, ".map") && !is_abbrev(forceful, "FORCE"))
  323. {
  324. check_all_events(ses, SUB_ARG, 0, 2, "WRITE ERROR", filename, "INVALID FILE EXTENSION");
  325. tintin_printf2(ses, "#WRITE {%s}: USE {FORCE} TO OVERWRITE .map FILES.", filename);
  326. return ses;
  327. }
  328. if ((file = fopen(filename, "w")) == NULL)
  329. {
  330. check_all_events(ses, SUB_ARG, 0, 2, "WRITE ERROR", filename, "FAILED TO OPEN");
  331. tintin_printf(ses, "#ERROR: #WRITE: COULDN'T OPEN {%s} TO WRITE.", filename);
  332. return ses;
  333. }
  334. for (i = 0 ; i < LIST_MAX ; i++)
  335. {
  336. root = ses->list[i];
  337. if (!HAS_BIT(root->flags, LIST_FLAG_WRITE))
  338. {
  339. continue;
  340. }
  341. fix = 0;
  342. for (j = 0 ; j < root->used ; j++)
  343. {
  344. node = root->list[j];
  345. if (HAS_BIT(node->flags, NODE_FLAG_ONESHOT))
  346. {
  347. continue;
  348. }
  349. if (*node->group == 0)
  350. {
  351. write_node(ses, i, node, file);
  352. cnt++;
  353. fix++;
  354. }
  355. }
  356. if (fix)
  357. {
  358. fputs("\n", file);
  359. }
  360. }
  361. fclose(file);
  362. show_message(ses, LIST_COMMAND, "#WRITE: %d COMMANDS WRITTEN TO {%s}.", cnt, filename);
  363. return ses;
  364. }
  365. void write_node(struct session *ses, int list, struct listnode *node, FILE *file)
  366. {
  367. char *result, *str;
  368. int llen = UMAX(20, strlen(node->arg1));
  369. int rlen = UMAX(25, strlen(node->arg2));
  370. push_call("write_node(%d,%p,%p)",list,node,file);
  371. switch (list)
  372. {
  373. case LIST_EVENT:
  374. case LIST_FUNCTION:
  375. case LIST_MACRO:
  376. asprintf(&result, "%c%s {%s}\n{\n%s\n}\n\n", gtd->tintin_char, list_table[list].name, node->arg1, script_writer(ses, node->arg2));
  377. break;
  378. case LIST_ACTION:
  379. case LIST_ALIAS:
  380. case LIST_BUTTON:
  381. if (!strcmp(node->arg3, "5"))
  382. {
  383. asprintf(&result, "%c%s {%s}\n{\n%s\n}\n\n", gtd->tintin_char, list_table[list].name, node->arg1, script_writer(ses, node->arg2));
  384. }
  385. else
  386. {
  387. asprintf(&result, "%c%s {%s}\n{\n%s\n}\n{%s}\n\n", gtd->tintin_char, list_table[list].name, node->arg1, script_writer(ses, node->arg2), node->arg3);
  388. }
  389. break;
  390. case LIST_VARIABLE:
  391. str = str_dup("");
  392. show_nest_node(node, &str, 1);
  393. asprintf(&result, "%c%-16s {%s} %*s {%s}\n", gtd->tintin_char, list_table[list].name, node->arg1, 20 - llen, "", str);
  394. str_free(str);
  395. break;
  396. default:
  397. switch (list_table[list].args)
  398. {
  399. case 0:
  400. result = strdup("");
  401. break;
  402. case 1:
  403. asprintf(&result, "%c%-16s {%s}\n", gtd->tintin_char, list_table[list].name, node->arg1);
  404. break;
  405. case 2:
  406. asprintf(&result, "%c%-16s {%s} %*s {%s}\n", gtd->tintin_char, list_table[list].name, node->arg1, 20 - llen, "", node->arg2);
  407. break;
  408. case 3:
  409. asprintf(&result, "%c%-16s {%s} %*s {%s} %*s {%s}\n", gtd->tintin_char, list_table[list].name, node->arg1, 20 - llen, "", node->arg2, 25 - rlen, "", node->arg3);
  410. break;
  411. case 4:
  412. asprintf(&result, "%c%-16s {%s} %*s {%s} %*s {%s} {%s}\n", gtd->tintin_char, list_table[list].name, node->arg1, 20 - llen, "", node->arg2, 25 - rlen, "", node->arg3, node->arg4);
  413. break;
  414. }
  415. break;
  416. }
  417. fputs(result, file);
  418. free(result);
  419. pop_call();
  420. return;
  421. }