files.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  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. FILE *fp;
  31. sub_arg_in_braces(ses, arg, arg1, GET_ONE, SUB_VAR|SUB_FUN);
  32. if ((fp = fopen(arg1, "r")) == NULL)
  33. {
  34. check_all_events(ses, SUB_ARG, 0, 2, "READ ERROR", arg1, "FILE NOT FOUND.");
  35. tintin_printf(ses, "#READ {%s} - FILE NOT FOUND.", arg1);
  36. return ses;
  37. }
  38. return read_file(ses, fp, arg1);
  39. }
  40. struct session *read_file(struct session *ses, FILE *fp, char *filename)
  41. {
  42. char *bufi, *bufo, temp[INPUT_SIZE], *pti, *pto, last = 0;
  43. int lvl, cnt, com, lnc, fix, ok, verbose, size;
  44. int counter[LIST_MAX];
  45. temp[0] = getc(fp);
  46. if (!ispunct((int) temp[0]))
  47. {
  48. check_all_events(ses, SUB_ARG, 0, 2, "READ ERROR", filename, "INVALID START OF FILE");
  49. tintin_printf(ses, "#ERROR: #READ {%s} - INVALID START OF FILE '%c'.", filename, temp[0]);
  50. fclose(fp);
  51. return ses;
  52. }
  53. ungetc(temp[0], fp);
  54. for (cnt = 0 ; cnt < LIST_MAX ; cnt++)
  55. {
  56. if (HAS_BIT(list_table[cnt].flags, LIST_FLAG_READ))
  57. {
  58. counter[cnt] = ses->list[cnt]->used;
  59. }
  60. }
  61. fseek(fp, 0, SEEK_END);
  62. size = ftell(fp);
  63. fseek(fp, 0, SEEK_SET);
  64. if ((bufi = (char *) calloc(1, size + 2)) == NULL || (bufo = (char *) calloc(1, 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 %d BYTES OF MEMORY.", filename, size + 2);
  68. fclose(fp);
  69. return ses;
  70. }
  71. fread(bufi, 1, 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 (last == 0)
  156. {
  157. if (*pti != DEFAULT_OPEN && *pti != DEFAULT_CLOSE)
  158. {
  159. show_error(ses, LIST_COMMAND, "#WARNING: #READ {%s} MISSING SEMICOLON ON LINE %d.", filename, lnc - 1);
  160. }
  161. }
  162. if (*pti != DEFAULT_CLOSE && last == 0)
  163. {
  164. *pto++ = ' ';
  165. }
  166. }
  167. else for (cnt = 1 ; ; cnt++)
  168. {
  169. if (pti[cnt] == 0)
  170. {
  171. *pto++ = *pti++;
  172. break;
  173. }
  174. if (pti[cnt] == DEFAULT_OPEN)
  175. {
  176. pti++;
  177. while (isspace((int) *pti))
  178. {
  179. pti++;
  180. }
  181. *pto++ = ' ';
  182. break;
  183. }
  184. if (!isspace((int) pti[cnt]))
  185. {
  186. *pto++ = *pti++;
  187. break;
  188. }
  189. }
  190. break;
  191. default:
  192. *pto++ = *pti++;
  193. last = 0;
  194. break;
  195. }
  196. }
  197. else
  198. {
  199. switch (*pti)
  200. {
  201. case '/':
  202. if (pti[1] == '*')
  203. {
  204. pti += 2;
  205. com += 1;
  206. }
  207. else
  208. {
  209. pti += 1;
  210. }
  211. break;
  212. case '*':
  213. if (pti[1] == '/')
  214. {
  215. pti += 2;
  216. com -= 1;
  217. }
  218. else
  219. {
  220. pti += 1;
  221. }
  222. break;
  223. case '\n':
  224. lnc++;
  225. pti++;
  226. break;
  227. default:
  228. pti++;
  229. break;
  230. }
  231. }
  232. }
  233. *pto++ = '\n';
  234. *pto = '\0';
  235. if (lvl)
  236. {
  237. check_all_events(ses, SUB_ARG, 0, 2, "READ ERROR", filename, "MISSING BRACE OPEN OR CLOSE");
  238. 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);
  239. fclose(fp);
  240. free(bufi);
  241. free(bufo);
  242. return ses;
  243. }
  244. if (com)
  245. {
  246. check_all_events(ses, SUB_ARG, 0, 2, "READ ERROR", filename, "MISSING COMMENT OPEN OR CLOSE");
  247. tintin_printf(ses, "#ERROR: #READ {%s} - MISSING %d '%s'", filename, abs(com), com < 0 ? "/*" : "*/");
  248. fclose(fp);
  249. free(bufi);
  250. free(bufo);
  251. return ses;
  252. }
  253. sprintf(temp, "#CONFIG {TINTIN CHAR} {%c}", bufo[0]);
  254. if (bufo[0] != gtd->tintin_char)
  255. {
  256. gtd->level->verbose++;
  257. gtd->level->debug++;
  258. 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]);
  259. gtd->level->debug--;
  260. gtd->level->verbose--;
  261. gtd->level->quiet++;
  262. script_driver(ses, LIST_COMMAND, temp);
  263. gtd->level->quiet--;
  264. }
  265. verbose = HAS_BIT(ses->flags, SES_FLAG_VERBOSE) ? 1 : 0;
  266. gtd->level->input++;
  267. gtd->level->verbose += verbose;
  268. lvl = 0;
  269. lnc = 0;
  270. pti = bufo;
  271. pto = bufi;
  272. while (*pti)
  273. {
  274. if (*pti != '\n')
  275. {
  276. *pto++ = *pti++;
  277. continue;
  278. }
  279. lnc++;
  280. *pto = 0;
  281. if (pto - bufi >= BUFFER_SIZE)
  282. {
  283. show_debug(ses, LIST_COMMAND, "#WARNING: #READ {%s} - POSSIBLE BUFFER OVERFLOW AT COMMAND: %.30s", filename, bufi);
  284. }
  285. if (bufi[0])
  286. {
  287. ses = script_driver(ses, LIST_COMMAND, bufi);
  288. }
  289. pto = bufi;
  290. pti++;
  291. }
  292. gtd->level->verbose -= verbose;
  293. gtd->level->input--;
  294. if (!HAS_BIT(ses->flags, SES_FLAG_VERBOSE))
  295. {
  296. for (cnt = 0 ; cnt < LIST_MAX ; cnt++)
  297. {
  298. if (HAS_BIT(list_table[cnt].flags, LIST_FLAG_READ))
  299. {
  300. switch (ses->list[cnt]->used - counter[cnt])
  301. {
  302. case 0:
  303. break;
  304. case 1:
  305. show_message(ses, cnt, "#OK: %3d %s LOADED.", ses->list[cnt]->used - counter[cnt], list_table[cnt].name);
  306. break;
  307. default:
  308. show_message(ses, cnt, "#OK: %3d %s LOADED.", ses->list[cnt]->used - counter[cnt], list_table[cnt].name_multi);
  309. break;
  310. }
  311. }
  312. }
  313. }
  314. fclose(fp);
  315. free(bufi);
  316. free(bufo);
  317. return ses;
  318. }
  319. DO_COMMAND(do_write)
  320. {
  321. FILE *file;
  322. struct listroot *root;
  323. struct listnode *node;
  324. int i, j, fix, cnt = 0;
  325. arg = sub_arg_in_braces(ses, arg, arg1, GET_ONE, SUB_VAR|SUB_FUN);
  326. arg = get_arg_in_braces(ses, arg, arg2, GET_ONE);
  327. if (*arg1 == 0)
  328. {
  329. check_all_events(ses, SUB_ARG, 0, 2, "WRITE ERROR", arg1, "INVALID FILE NAME");
  330. tintin_printf2(ses, "#SYNTAX: #WRITE {<filename>} {[FORCE]}");
  331. return ses;
  332. }
  333. if (!str_suffix(arg1, ".map") && !is_abbrev(arg2, "FORCE"))
  334. {
  335. check_all_events(ses, SUB_ARG, 0, 2, "WRITE ERROR", arg1, "INVALID FILE EXTENSION");
  336. tintin_printf2(ses, "#WRITE {%s}: USE {FORCE} TO OVERWRITE .map FILES.", arg1);
  337. return ses;
  338. }
  339. if ((file = fopen(arg1, "w")) == NULL)
  340. {
  341. check_all_events(ses, SUB_ARG, 0, 2, "WRITE ERROR", arg1, "FAILED TO OPEN");
  342. tintin_printf(ses, "#ERROR: #WRITE: COULDN'T OPEN {%s} TO WRITE.", arg1);
  343. return ses;
  344. }
  345. for (i = 0 ; i < LIST_MAX ; i++)
  346. {
  347. root = ses->list[i];
  348. if (!HAS_BIT(root->flags, LIST_FLAG_WRITE))
  349. {
  350. continue;
  351. }
  352. fix = 0;
  353. for (j = 0 ; j < root->used ; j++)
  354. {
  355. node = root->list[j];
  356. if (node->shots)//HAS_BIT(node->flags, NODE_FLAG_ONESHOT))
  357. {
  358. continue;
  359. }
  360. if (*node->group == 0)
  361. {
  362. write_node(ses, i, node, file);
  363. cnt++;
  364. fix++;
  365. }
  366. }
  367. if (fix)
  368. {
  369. fputs("\n", file);
  370. }
  371. }
  372. fclose(file);
  373. show_message(ses, LIST_COMMAND, "#WRITE: %d COMMANDS WRITTEN TO {%s}.", cnt, arg1);
  374. return ses;
  375. }
  376. void write_node(struct session *ses, int list, struct listnode *node, FILE *file)
  377. {
  378. char *result, *str;
  379. int llen = UMAX(20, strlen(node->arg1));
  380. int rlen = UMAX(25, strlen(node->arg2));
  381. push_call("write_node(%d,%p,%p)",list,node,file);
  382. switch (list)
  383. {
  384. case LIST_EVENT:
  385. case LIST_FUNCTION:
  386. case LIST_MACRO:
  387. 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));
  388. break;
  389. case LIST_ACTION:
  390. case LIST_ALIAS:
  391. case LIST_BUTTON:
  392. if (!strcmp(node->arg3, "5"))
  393. {
  394. 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));
  395. }
  396. else
  397. {
  398. 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);
  399. }
  400. break;
  401. case LIST_VARIABLE:
  402. str = str_dup("");
  403. show_nest_node(node, &str, 1);
  404. asprintf(&result, "%c%-16s {%s} %*s {%s}\n", gtd->tintin_char, list_table[list].name, node->arg1, 20 - llen, "", str);
  405. str_free(str);
  406. break;
  407. default:
  408. switch (list_table[list].args)
  409. {
  410. case 0:
  411. result = strdup("");
  412. break;
  413. case 1:
  414. asprintf(&result, "%c%-16s {%s}\n", gtd->tintin_char, list_table[list].name, node->arg1);
  415. break;
  416. case 2:
  417. asprintf(&result, "%c%-16s {%s} %*s {%s}\n", gtd->tintin_char, list_table[list].name, node->arg1, 20 - llen, "", node->arg2);
  418. break;
  419. case 3:
  420. 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);
  421. break;
  422. case 4:
  423. 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);
  424. break;
  425. }
  426. break;
  427. }
  428. fputs(result, file);
  429. free(result);
  430. pop_call();
  431. return;
  432. }