files.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  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 *file;
  31. arg = sub_arg_in_braces(ses, arg, arg1, GET_ONE, SUB_VAR|SUB_FUN);
  32. arg = sub_arg_in_braces(ses, arg, arg2, GET_ONE, SUB_VAR|SUB_FUN);
  33. if ((file = fopen(arg1, "r")) == NULL)
  34. {
  35. check_all_events(ses, EVENT_FLAG_SYSTEM, 0, 2, "READ ERROR", arg1, "FILE NOT FOUND.");
  36. tintin_printf(ses, "#READ {%s}: FILE NOT FOUND.", arg1);
  37. return ses;
  38. }
  39. ses = read_file(ses, file, arg1);
  40. fclose(file);
  41. return ses;
  42. }
  43. struct session *read_file(struct session *ses, FILE *file, char *filename)
  44. {
  45. char *bufi, *bufo, temp[INPUT_SIZE], *pti, *pto, last = 0;
  46. int lvl, cnt, com, lnc, fix, ok, verbose, size;
  47. int counter[LIST_MAX];
  48. temp[0] = getc(file);
  49. if (!ispunct((int) temp[0]))
  50. {
  51. check_all_events(ses, EVENT_FLAG_SYSTEM, 0, 2, "READ ERROR", filename, "INVALID START OF FILE");
  52. tintin_printf(ses, "#ERROR: #READ {%s}: INVALID START OF FILE '%c'.", filename, temp[0]);
  53. return ses;
  54. }
  55. ungetc(temp[0], file);
  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. fseek(file, 0, SEEK_END);
  64. size = ftell(file);
  65. fseek(file, 0, SEEK_SET);
  66. if ((bufi = (char *) calloc(1, size + 2)) == NULL || (bufo = (char *) calloc(1, size + 2)) == NULL)
  67. {
  68. check_all_events(ses, EVENT_FLAG_SYSTEM, 0, 2, "READ ERROR", filename, "FAILED TO ALLOCATE MEMORY");
  69. tintin_printf(ses, "#ERROR: #READ {%s}: FAILED TO ALLOCATE %d BYTES OF MEMORY.", filename, size + 2);
  70. return ses;
  71. }
  72. if (fread(bufi, 1, size, file) <= 0)
  73. {
  74. check_all_events(ses, EVENT_FLAG_SYSTEM, 0, 2, "READ ERROR", filename, "FREAD FAILURE");
  75. tintin_printf(ses, "#ERROR: #READ {%s}: FREAD FAILURE.", filename);
  76. return ses;
  77. }
  78. pti = bufi;
  79. pto = bufo;
  80. lvl = com = lnc = fix = ok = 0;
  81. while (*pti)
  82. {
  83. if (com == 0)
  84. {
  85. if (HAS_BIT(ses->charset, CHARSET_FLAG_EUC) && is_euc_head(ses, pti))
  86. {
  87. *pto++ = *pti++;
  88. *pto++ = *pti++;
  89. continue;
  90. }
  91. switch (*pti)
  92. {
  93. case DEFAULT_OPEN:
  94. *pto++ = *pti++;
  95. lvl++;
  96. last = DEFAULT_OPEN;
  97. break;
  98. case DEFAULT_CLOSE:
  99. *pto++ = *pti++;
  100. lvl--;
  101. last = DEFAULT_CLOSE;
  102. break;
  103. case COMMAND_SEPARATOR:
  104. *pto++ = *pti++;
  105. last = COMMAND_SEPARATOR;
  106. break;
  107. case ' ':
  108. *pto++ = *pti++;
  109. break;
  110. case '/':
  111. if (lvl == 0 && pti[1] == '*')
  112. {
  113. pti += 2;
  114. com += 1;
  115. }
  116. else
  117. {
  118. *pto++ = *pti++;
  119. }
  120. break;
  121. case '\t':
  122. *pto++ = *pti++;
  123. break;
  124. case '\r':
  125. pti++;
  126. break;
  127. case '\n':
  128. lnc++;
  129. pto--;
  130. while (is_space(*pto))
  131. {
  132. pto--;
  133. }
  134. pto++;
  135. if (fix == 0 && pti[1] == gtd->tintin_char)
  136. {
  137. if (lvl == 0)
  138. {
  139. ok = lnc + 1;
  140. }
  141. else
  142. {
  143. fix = lnc;
  144. }
  145. }
  146. if (lvl)
  147. {
  148. pti++;
  149. while (is_space(*pti))
  150. {
  151. if (*pti == '\n')
  152. {
  153. lnc++;
  154. if (fix == 0 && pti[1] == gtd->tintin_char)
  155. {
  156. fix = lnc;
  157. }
  158. }
  159. pti++;
  160. }
  161. if (last != COMMAND_SEPARATOR && last != DEFAULT_OPEN)
  162. {
  163. if (*pti == gtd->tintin_char)
  164. {
  165. show_error(ses, LIST_COMMAND, "#WARNING: #READ {%s}: MISSING SEMICOLON ON LINE %d.", filename, lnc);
  166. }
  167. }
  168. if (*pti != DEFAULT_CLOSE && last == 0)
  169. {
  170. *pto++ = ' ';
  171. }
  172. }
  173. else for (cnt = 1 ; ; cnt++)
  174. {
  175. if (pti[cnt] == 0)
  176. {
  177. *pto++ = *pti++;
  178. break;
  179. }
  180. if (pti[cnt] == DEFAULT_OPEN)
  181. {
  182. pti++;
  183. while (is_space(*pti))
  184. {
  185. pti++;
  186. }
  187. *pto++ = ' ';
  188. break;
  189. }
  190. if (!is_space(pti[cnt]))
  191. {
  192. *pto++ = *pti++;
  193. break;
  194. }
  195. }
  196. break;
  197. default:
  198. *pto++ = *pti++;
  199. last = 0;
  200. break;
  201. }
  202. }
  203. else
  204. {
  205. switch (*pti)
  206. {
  207. case '/':
  208. if (pti[1] == '*')
  209. {
  210. pti += 2;
  211. com += 1;
  212. }
  213. else
  214. {
  215. pti += 1;
  216. }
  217. break;
  218. case '*':
  219. if (pti[1] == '/')
  220. {
  221. pti += 2;
  222. com -= 1;
  223. }
  224. else
  225. {
  226. pti += 1;
  227. }
  228. break;
  229. case '\n':
  230. lnc++;
  231. pti++;
  232. break;
  233. default:
  234. pti++;
  235. break;
  236. }
  237. }
  238. }
  239. *pto++ = '\n';
  240. *pto = '\0';
  241. if (lvl)
  242. {
  243. check_all_events(ses, EVENT_FLAG_SYSTEM, 0, 2, "READ ERROR", filename, "MISSING BRACE OPEN OR CLOSE");
  244. 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);
  245. free(bufi);
  246. free(bufo);
  247. return ses;
  248. }
  249. if (com)
  250. {
  251. check_all_events(ses, EVENT_FLAG_SYSTEM, 0, 2, "READ ERROR", filename, "MISSING COMMENT OPEN OR CLOSE");
  252. tintin_printf(ses, "#ERROR: #READ {%s}: MISSING %d '%s'", filename, abs(com), com < 0 ? "/*" : "*/");
  253. free(bufi);
  254. free(bufo);
  255. return ses;
  256. }
  257. sprintf(temp, "#CONFIG {TINTIN CHAR} {%c}", bufo[0]);
  258. if (bufo[0] != gtd->tintin_char)
  259. {
  260. gtd->level->verbose++;
  261. gtd->level->debug++;
  262. show_error(ses, LIST_COMMAND, "#WARNING: #READ {%s}: SETTING THE COMMAND CHARACTER TO '%c' BECAUSE IT'S THE FIRST CHARACTER IN THE FILE.", filename, bufo[0]);
  263. gtd->level->debug--;
  264. gtd->level->verbose--;
  265. gtd->level->quiet++;
  266. script_driver(ses, LIST_COMMAND, NULL, temp);
  267. gtd->level->quiet--;
  268. }
  269. verbose = HAS_BIT(ses->config_flags, CONFIG_FLAG_VERBOSE) ? 1 : 0;
  270. gtd->level->input++;
  271. gtd->level->verbose += verbose;
  272. lvl = 0;
  273. lnc = 0;
  274. pti = bufo;
  275. pto = bufi;
  276. while (*pti)
  277. {
  278. if (*pti != '\n')
  279. {
  280. *pto++ = *pti++;
  281. continue;
  282. }
  283. lnc++;
  284. *pto = 0;
  285. if (pto - bufi >= BUFFER_SIZE)
  286. {
  287. show_debug(ses, LIST_COMMAND, NULL, "#WARNING: #READ {%s}: POSSIBLE BUFFER OVERFLOW AT COMMAND: %.30s", filename, bufi);
  288. }
  289. if (bufi[0])
  290. {
  291. ses = script_driver(ses, LIST_COMMAND, NULL, bufi);
  292. }
  293. pto = bufi;
  294. pti++;
  295. }
  296. gtd->level->verbose -= verbose;
  297. gtd->level->input--;
  298. if (!HAS_BIT(ses->config_flags, CONFIG_FLAG_VERBOSE))
  299. {
  300. for (cnt = 0 ; cnt < LIST_MAX ; cnt++)
  301. {
  302. if (HAS_BIT(list_table[cnt].flags, LIST_FLAG_READ))
  303. {
  304. switch (ses->list[cnt]->used - counter[cnt])
  305. {
  306. case 0:
  307. break;
  308. case 1:
  309. show_message(ses, cnt, "#OK: %3d %s LOADED.", ses->list[cnt]->used - counter[cnt], list_table[cnt].name);
  310. break;
  311. default:
  312. show_message(ses, cnt, "#OK: %3d %s LOADED.", ses->list[cnt]->used - counter[cnt], list_table[cnt].name_multi);
  313. break;
  314. }
  315. }
  316. }
  317. }
  318. free(bufi);
  319. free(bufo);
  320. check_all_events(ses, EVENT_FLAG_SYSTEM, 0, 1, "READ FILE", filename);
  321. check_all_events(ses, EVENT_FLAG_SYSTEM, 1, 1, "READ FILE %s", filename, filename);
  322. return ses;
  323. }
  324. DO_COMMAND(do_write)
  325. {
  326. FILE *file;
  327. struct listroot *root;
  328. struct listnode *node;
  329. int i, j, fix, cnt = 0;
  330. arg = sub_arg_in_braces(ses, arg, arg1, GET_ONE, SUB_VAR|SUB_FUN);
  331. arg = get_arg_in_braces(ses, arg, arg2, GET_ONE);
  332. if (*arg1 == 0)
  333. {
  334. check_all_events(ses, EVENT_FLAG_SYSTEM, 0, 2, "WRITE ERROR", arg1, "INVALID FILE NAME");
  335. show_error(ses, LIST_COMMAND, "#SYNTAX: #WRITE <FILENAME> [FORCE]");
  336. return ses;
  337. }
  338. if (is_suffix(arg1, ".map") && !is_abbrev(arg2, "FORCE"))
  339. {
  340. check_all_events(ses, EVENT_FLAG_SYSTEM, 0, 2, "WRITE ERROR", arg1, "INVALID FILE EXTENSION");
  341. tintin_printf2(ses, "#WRITE {%s}: USE {FORCE} TO OVERWRITE .map FILES.", arg1);
  342. return ses;
  343. }
  344. if ((file = fopen(arg1, "w")) == NULL)
  345. {
  346. check_all_events(ses, EVENT_FLAG_SYSTEM, 0, 2, "WRITE ERROR", arg1, "FAILED TO OPEN");
  347. show_error(ses, LIST_COMMAND, "#ERROR: #WRITE: COULDN'T OPEN {%s} TO WRITE.", arg1);
  348. return ses;
  349. }
  350. for (i = 0 ; i < LIST_MAX ; i++)
  351. {
  352. root = ses->list[i];
  353. if (!HAS_BIT(root->flags, LIST_FLAG_WRITE))
  354. {
  355. continue;
  356. }
  357. fix = 0;
  358. for (j = 0 ; j < root->used ; j++)
  359. {
  360. node = root->list[j];
  361. if (node->shots)
  362. {
  363. continue;
  364. }
  365. if (*node->group == 0)
  366. {
  367. write_node(ses, i, node, file);
  368. cnt++;
  369. fix++;
  370. }
  371. }
  372. if (fix)
  373. {
  374. fputs("\n", file);
  375. }
  376. }
  377. fclose(file);
  378. check_all_events(ses, EVENT_FLAG_SYSTEM, 0, 1, "WRITE FILE", arg1);
  379. show_message(ses, LIST_COMMAND, "#WRITE: %d COMMANDS WRITTEN TO {%s}.", cnt, arg1);
  380. return ses;
  381. }
  382. void write_node(struct session *ses, int list, struct listnode *node, FILE *file)
  383. {
  384. char *result, *str;
  385. int val = 0;
  386. push_call("write_node(%d,%p,%p)",list,node,file);
  387. switch (list)
  388. {
  389. case LIST_EVENT:
  390. case LIST_FUNCTION:
  391. case LIST_MACRO:
  392. val = 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));
  393. break;
  394. case LIST_ACTION:
  395. case LIST_ALIAS:
  396. case LIST_BUTTON:
  397. if (!strcmp(node->arg3, "5"))
  398. {
  399. val = 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));
  400. }
  401. else
  402. {
  403. val = 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);
  404. }
  405. break;
  406. case LIST_VARIABLE:
  407. str = str_alloc_stack(0);
  408. // show_nest_node(node, &str, 1);
  409. view_nest_node(node, &str, 0, TRUE, FALSE);
  410. if (node->root)
  411. {
  412. val = asprintf(&result, "%c%s {%s}\n{\n%s}\n", gtd->tintin_char, list_table[list].name, node->arg1, str);
  413. }
  414. else
  415. {
  416. val = asprintf(&result, "%c%s {%s} {%s}\n", gtd->tintin_char, list_table[list].name, node->arg1, str);
  417. }
  418. break;
  419. default:
  420. switch (list_table[list].args)
  421. {
  422. case 0:
  423. result = strdup("");
  424. break;
  425. case 1:
  426. val = asprintf(&result, "%c%s {%s}\n", gtd->tintin_char, list_table[list].name, node->arg1);
  427. break;
  428. case 2:
  429. val = asprintf(&result, "%c%s {%s} {%s}\n", gtd->tintin_char, list_table[list].name, node->arg1, node->arg2);
  430. break;
  431. case 3:
  432. val = asprintf(&result, "%c%s {%s} {%s} {%s}\n", gtd->tintin_char, list_table[list].name, node->arg1, node->arg2, node->arg3);
  433. break;
  434. case 4:
  435. val = asprintf(&result, "%c%s {%s} {%s} {%s} {%s}\n", gtd->tintin_char, list_table[list].name, node->arg1, node->arg2, node->arg3, node->arg4);
  436. break;
  437. }
  438. break;
  439. }
  440. if (val != -1)
  441. {
  442. fputs(result, file);
  443. free(result);
  444. }
  445. else
  446. {
  447. syserr_printf(ses, "write_node: asprintf:");
  448. }
  449. pop_call();
  450. return;
  451. }
  452. char *fread_one_line(char **str, FILE *file)
  453. {
  454. int byte;
  455. str_cpy(str, "");
  456. while (TRUE)
  457. {
  458. byte = getc(file);
  459. switch (byte)
  460. {
  461. case ASCII_LF:
  462. return *str;
  463. case EOF:
  464. if (*(*str))
  465. {
  466. return *str;
  467. }
  468. return NULL;
  469. case ASCII_NUL:
  470. tintin_printf2(gtd->ses, "fread_one_line: encountered NULL character.");
  471. break;
  472. case ASCII_CR:
  473. break;
  474. case ASCII_HTAB:
  475. str_cat_printf(str, "%*s", gtd->ses->tab_width, "");
  476. break;
  477. default:
  478. str_cat_chr(str, byte);
  479. break;
  480. }
  481. }
  482. return *str;
  483. }