scan.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  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 2005 *
  23. ******************************************************************************/
  24. #include "tintin.h"
  25. #ifdef HAVE_PTY_H
  26. #include <pty.h>
  27. #else
  28. #ifdef HAVE_UTIL_H
  29. #include <util.h>
  30. #endif
  31. #endif
  32. #include <dirent.h>
  33. #define DO_SCAN(scan) struct session *scan(struct session *ses, FILE *fp, char *arg, char *arg1, char *arg2)
  34. DO_SCAN(scan_abort);
  35. DO_SCAN(scan_csv);
  36. DO_SCAN(scan_dir);
  37. DO_SCAN(scan_file);
  38. DO_SCAN(scan_forward);
  39. DO_SCAN(scan_tsv);
  40. DO_SCAN(scan_txt);
  41. #define SCAN_FLAG_NONE 0
  42. #define SCAN_FLAG_FILE BV01
  43. #define SCAN_FLAG_SCAN BV02
  44. typedef struct session *SCAN(struct session *ses, FILE *fp, char *arg, char *arg1, char *arg2);
  45. struct scan_type
  46. {
  47. char * name;
  48. SCAN * fun;
  49. int flags;
  50. char * desc;
  51. };
  52. struct scan_type scan_table[] =
  53. {
  54. { "ABORT", scan_abort, SCAN_FLAG_NONE, "Abort a scan currently in progress."},
  55. { "CSV", scan_csv, SCAN_FLAG_FILE|SCAN_FLAG_SCAN, "Scan a comma separated value file." },
  56. { "DIR", scan_dir, SCAN_FLAG_FILE, "Scan a directory to a variable." },
  57. { "FILE", scan_file, SCAN_FLAG_FILE, "Scan a file all at once." },
  58. { "FORWARD", scan_forward, SCAN_FLAG_FILE, "Scan a file and send each line." },
  59. { "TSV", scan_tsv, SCAN_FLAG_FILE|SCAN_FLAG_SCAN, "Scan a tab separated value file." },
  60. { "TXT", scan_txt, SCAN_FLAG_FILE|SCAN_FLAG_SCAN, "Scan a text file line by line." },
  61. { "", NULL, 0, "" }
  62. };
  63. DO_COMMAND(do_scan)
  64. {
  65. char cmd[BUFFER_SIZE];
  66. FILE *fp = NULL;
  67. int cnt;
  68. arg = sub_arg_in_braces(ses, arg, cmd, GET_ONE, SUB_VAR|SUB_FUN);
  69. if (*cmd == 0)
  70. {
  71. tintin_header(ses, 80, " SCAN OPTIONS ");
  72. for (cnt = 0 ; *scan_table[cnt].name != 0 ; cnt++)
  73. {
  74. tintin_printf2(ses, " [%-13s] %s", scan_table[cnt].name, scan_table[cnt].desc);
  75. }
  76. tintin_header(ses, 80, "");
  77. return ses;
  78. }
  79. for (cnt = 0 ; *scan_table[cnt].name != 0 ; cnt++)
  80. {
  81. if (!is_abbrev(cmd, scan_table[cnt].name))
  82. {
  83. continue;
  84. }
  85. arg = sub_arg_in_braces(ses, arg, arg1, GET_ONE, SUB_VAR|SUB_FUN);
  86. if (HAS_BIT(scan_table[cnt].flags, SCAN_FLAG_FILE))
  87. {
  88. if (*arg1 == 0)
  89. {
  90. show_error(ses, LIST_COMMAND, "#SYNTAX: #SCAN {%s} {<FILENAME>}", scan_table[cnt].name);
  91. return ses;
  92. }
  93. if ((fp = fopen(arg1, "r")) == NULL)
  94. {
  95. show_error(ses, LIST_COMMAND, "#ERROR: #SCAN {%s} FILE {%s} NOT FOUND.", scan_table[cnt].name, arg1);
  96. return ses;
  97. }
  98. }
  99. if (HAS_BIT(scan_table[cnt].flags, SCAN_FLAG_SCAN))
  100. {
  101. gtd->level->scan++;
  102. }
  103. ses = scan_table[cnt].fun(ses, fp, arg, arg1, arg2);
  104. if (HAS_BIT(scan_table[cnt].flags, SCAN_FLAG_SCAN))
  105. {
  106. gtd->level->scan--;
  107. }
  108. if (HAS_BIT(scan_table[cnt].flags, SCAN_FLAG_FILE))
  109. {
  110. fclose(fp);
  111. }
  112. return ses;
  113. }
  114. show_error(ses, LIST_COMMAND, "\e[1;31mTHE SCAN COMMAND HAS CHANGED, EXECUTING #SCAN {TXT} {%s} INSTEAD.", cmd);
  115. ses = command(ses, do_scan, "{TXT} {%s}", cmd);
  116. return ses;
  117. }
  118. DO_SCAN(scan_abort)
  119. {
  120. if (gtd->level->scan)
  121. {
  122. SET_BIT(ses->flags, SES_FLAG_SCANABORT);
  123. }
  124. else
  125. {
  126. show_error(ses, LIST_COMMAND, "#SCAN ABORT: NOT CURRENTLY SCANNING.");
  127. }
  128. return ses;
  129. }
  130. /* support routines for comma separated value files */
  131. char *get_arg_in_quotes(struct session *ses, char *string, char *result, int flag)
  132. {
  133. char *pti, *pto;
  134. int nest = 0;
  135. pti = space_out(string);
  136. pto = result;
  137. if (*pti == '"')
  138. {
  139. nest = TRUE;
  140. pti++;
  141. }
  142. while (*pti)
  143. {
  144. if (HAS_BIT(ses->charset, CHARSET_FLAG_EUC) && is_euc_head(ses, pti))
  145. {
  146. *pto++ = *pti++;
  147. *pto++ = *pti++;
  148. continue;
  149. }
  150. if (*pti == '"')
  151. {
  152. if (pti[1] == '"')
  153. {
  154. *pto++ = *pti++;
  155. }
  156. else if (nest)
  157. {
  158. nest = FALSE;
  159. }
  160. pti++;
  161. continue;
  162. }
  163. else if (nest == TRUE)
  164. {
  165. *pto++ = *pti++;
  166. }
  167. else if (*pti == ' ' || *pti == '\t')
  168. {
  169. pti++;
  170. }
  171. else if (*pti == ',')
  172. {
  173. pti++;
  174. break;
  175. }
  176. else
  177. {
  178. *pto++ = *pti++;
  179. }
  180. }
  181. if (nest)
  182. {
  183. tintin_printf2(ses, "#SCAN CSV: GET QUOTED ARGUMENT: UNMATCHED QUOTE.");
  184. }
  185. *pto = 0;
  186. return pti;
  187. }
  188. DO_SCAN(scan_csv)
  189. {
  190. char line[STRING_SIZE];
  191. int i, header = FALSE;
  192. while (fgets(line, BUFFER_SIZE, fp))
  193. {
  194. arg = strchr(line, '\r');
  195. if (arg)
  196. {
  197. *arg = 0;
  198. }
  199. else
  200. {
  201. arg = strchr(line, '\n');
  202. if (arg)
  203. {
  204. *arg = 0;
  205. }
  206. }
  207. RESTRING(gtd->vars[0], line);
  208. arg = line;
  209. for (i = 1 ; i < 100 ; i++)
  210. {
  211. arg = get_arg_in_quotes(ses, arg, arg2, FALSE);
  212. RESTRING(gtd->vars[i], arg2);
  213. if (*arg == 0)
  214. {
  215. while (++i < 100)
  216. {
  217. if (*gtd->vars[i])
  218. {
  219. RESTRING(gtd->vars[i], "");
  220. }
  221. }
  222. break;
  223. }
  224. }
  225. if (header == FALSE)
  226. {
  227. header = TRUE;
  228. check_all_events(ses, SUB_SEC|EVENT_FLAG_SCAN, 0, 0, "SCAN CSV HEADER");
  229. }
  230. else
  231. {
  232. check_all_events(ses, SUB_SEC|EVENT_FLAG_SCAN, 0, 0, "SCAN CSV LINE");
  233. }
  234. if (HAS_BIT(ses->flags, SES_FLAG_SCANABORT))
  235. {
  236. break;
  237. }
  238. }
  239. if (HAS_BIT(ses->flags, SES_FLAG_SCANABORT))
  240. {
  241. DEL_BIT(ses->flags, SES_FLAG_SCANABORT);
  242. show_message(ses, LIST_COMMAND, "#SCAN CSV: FILE {%s} PARTIALLY SCANNED.", arg1);
  243. }
  244. else
  245. {
  246. show_message(ses, LIST_COMMAND, "#SCAN CSV: FILE {%s} SCANNED.", arg1);
  247. }
  248. return ses;
  249. }
  250. DO_SCAN(scan_dir)
  251. {
  252. char filename[PATH_SIZE];
  253. struct dirent **dirlist;
  254. struct stat info;
  255. int size, index;
  256. arg = get_arg_in_braces(ses, arg, arg2, GET_ALL);
  257. if (*arg2 == 0)
  258. {
  259. show_error(ses, LIST_COMMAND, "SYNTAX: #SCAN DIR {%s} <VARIABLE NAME>", arg1);
  260. return ses;
  261. }
  262. set_nest_node_ses(ses, arg2, "");
  263. size = scandir(arg1, &dirlist, 0, NULL);
  264. if (size == -1)
  265. {
  266. if (stat(arg1, &info) == -1)
  267. {
  268. syserr_printf(ses, "scan_dir: stat:");
  269. return ses;
  270. }
  271. arg = arg1;
  272. while (strchr(arg, '\\'))
  273. {
  274. arg = strchr(arg, '\\');
  275. }
  276. add_nest_node_ses(ses, arg2, "{%s}{{FILE}{%d}{MODE}{%u}{SIZE}{%u}{TIME}{%lld}}",
  277. arg,
  278. !S_ISDIR(info.st_mode),
  279. info.st_mode,
  280. info.st_size,
  281. info.st_mtime);
  282. return ses;
  283. }
  284. for (index = 0 ; index < size ; index++)
  285. {
  286. sprintf(filename, "%s%s%s", arg1, is_suffix(arg1, "/") ? "" : "/", dirlist[index]->d_name);
  287. if (stat(filename, &info) == -1)
  288. {
  289. syserr_printf(ses, "scan_dir: stat:");
  290. return ses;
  291. }
  292. add_nest_node_ses(ses, arg2, "{%s}{{FILE}{%d}{MODE}{%u}{SIZE}{%u}{TIME}{%lld}}",
  293. dirlist[index]->d_name,
  294. !S_ISDIR(info.st_mode),
  295. info.st_mode,
  296. info.st_size,
  297. info.st_mtime);
  298. }
  299. for (index = 0 ; index < size ; index++)
  300. {
  301. free(dirlist[index]);
  302. }
  303. free(dirlist);
  304. show_message(ses, LIST_COMMAND, "#SCAN DIR: DIRECTORY {%s} SAVED TO {%s}.", arg1, arg2);
  305. return ses;
  306. }
  307. DO_SCAN(scan_file)
  308. {
  309. char line[STRING_SIZE], *str_out, *str_rip, *str_sub;
  310. int cnt = 0;
  311. arg = get_arg_in_braces(ses, arg, arg2, GET_ALL);
  312. str_out = str_alloc_stack(0);
  313. while (fgets(line, BUFFER_SIZE - 1, fp))
  314. {
  315. cnt++;
  316. str_cat(&str_out, line);
  317. }
  318. str_rip = str_alloc_stack(str_len(str_out));
  319. strip_vt102_codes(str_out, str_rip);
  320. RESTRING(gtd->cmds[0], str_out);
  321. RESTRING(gtd->cmds[1], str_rip);
  322. RESTRING(gtd->cmds[2], ntos(str_len(str_out)));
  323. RESTRING(gtd->cmds[3], ntos(strlen(str_rip)));
  324. RESTRING(gtd->cmds[4], ntos(cnt));
  325. str_sub = str_alloc_stack(strlen(arg) * 2);
  326. substitute(ses, arg2, str_sub, SUB_CMD);
  327. show_message(ses, LIST_COMMAND, "#SCAN FILE: FILE {%s} SCANNED.", arg1);
  328. ses = script_driver(ses, LIST_COMMAND, str_sub);
  329. return ses;
  330. }
  331. DO_SCAN(scan_forward)
  332. {
  333. char line[STRING_SIZE], *lnf;
  334. float delay = 0;
  335. arg = sub_arg_in_braces(ses, arg, arg2, GET_ALL, SUB_VAR|SUB_FUN);
  336. if (!HAS_BIT(ses->flags, SES_FLAG_CONNECTED))
  337. {
  338. show_error(ses, LIST_COMMAND, "#SCAN FORWARD: SESSION {%s} IS NOT CONNECTED.", ses->name);
  339. return ses;
  340. }
  341. while (fgets(line, BUFFER_SIZE - 1, fp))
  342. {
  343. lnf = strchr(line, '\n');
  344. if (lnf)
  345. {
  346. *lnf = 0;
  347. }
  348. if (*arg2)
  349. {
  350. delay += get_number(ses, arg2);
  351. command(ses, do_delay, "%.3f #send {%s}", delay, line);
  352. }
  353. else
  354. {
  355. write_mud(ses, line, SUB_EOL);
  356. }
  357. }
  358. show_message(ses, LIST_COMMAND, "#SCAN FORWARD: FILE {%s} FORWARDED.", arg1);
  359. return ses;
  360. }
  361. /* support routines for tab separated value files */
  362. char *get_arg_stop_tabs(struct session *ses, char *string, char *result, int flag)
  363. {
  364. char *pti, *pto;
  365. pti = string;
  366. pto = result;
  367. while (*pti)
  368. {
  369. if (*pti == '\t')
  370. {
  371. pti++;
  372. break;
  373. }
  374. *pto++ = *pti++;
  375. }
  376. *pto = 0;
  377. return pti;
  378. }
  379. DO_SCAN(scan_tsv)
  380. {
  381. char line[STRING_SIZE];
  382. int i, header = FALSE;
  383. arg = get_arg_in_braces(ses, arg, arg2, GET_ALL);
  384. while (fgets(line, BUFFER_SIZE, fp))
  385. {
  386. arg = strchr(line, '\r');
  387. if (arg)
  388. {
  389. *arg = 0;
  390. }
  391. else
  392. {
  393. arg = strchr(line, '\n');
  394. if (arg)
  395. {
  396. *arg = 0;
  397. }
  398. }
  399. RESTRING(gtd->vars[0], line);
  400. arg = line;
  401. for (i = 1 ; i < 100 ; i++)
  402. {
  403. arg = get_arg_stop_tabs(ses, arg, arg2, FALSE);
  404. RESTRING(gtd->vars[i], arg2);
  405. if (*arg == 0)
  406. {
  407. while (++i < 100)
  408. {
  409. if (*gtd->vars[i])
  410. {
  411. RESTRING(gtd->vars[i], "");
  412. }
  413. }
  414. break;
  415. }
  416. }
  417. if (header == FALSE)
  418. {
  419. header = TRUE;
  420. check_all_events(ses, SUB_SEC|EVENT_FLAG_SCAN, 0, 0, "SCAN TSV HEADER");
  421. }
  422. else
  423. {
  424. check_all_events(ses, SUB_SEC|EVENT_FLAG_SCAN, 0, 0, "SCAN TSV LINE");
  425. }
  426. if (HAS_BIT(ses->flags, SES_FLAG_SCANABORT))
  427. {
  428. break;
  429. }
  430. }
  431. if (HAS_BIT(ses->flags, SES_FLAG_SCANABORT))
  432. {
  433. DEL_BIT(ses->flags, SES_FLAG_SCANABORT);
  434. show_message(ses, LIST_COMMAND, "#SCAN TSV: FILE {%s} PARTIALLY SCANNED.", arg1);
  435. }
  436. else
  437. {
  438. show_message(ses, LIST_COMMAND, "#SCAN TSV: FILE {%s} SCANNED.", arg1);
  439. }
  440. return ses;
  441. }
  442. /* support routines for text files */
  443. DO_SCAN(scan_txt)
  444. {
  445. char line[STRING_SIZE];
  446. while (fgets(line, BUFFER_SIZE - 1, fp))
  447. {
  448. arg = strchr(line, '\r');
  449. if (arg)
  450. {
  451. *arg = 0;
  452. }
  453. else
  454. {
  455. arg = strchr(line, '\n');
  456. if (arg)
  457. {
  458. *arg = 0;
  459. }
  460. }
  461. process_mud_output(ses, line, FALSE);
  462. if (HAS_BIT(ses->flags, SES_FLAG_SCANABORT))
  463. {
  464. break;
  465. }
  466. }
  467. if (HAS_BIT(ses->flags, SES_FLAG_SCANABORT))
  468. {
  469. DEL_BIT(ses->flags, SES_FLAG_SCANABORT);
  470. show_message(ses, LIST_COMMAND, "#SCAN TXT: FILE {%s} PARTIALLY SCANNED.", arg1);
  471. }
  472. else
  473. {
  474. show_message(ses, LIST_COMMAND, "#SCAN TXT: FILE {%s} SCANNED.", arg1);
  475. }
  476. return ses;
  477. }