scan.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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 2005 *
  24. ******************************************************************************/
  25. #include "tintin.h"
  26. #ifdef HAVE_PTY_H
  27. #include <pty.h>
  28. #else
  29. #ifdef HAVE_UTIL_H
  30. #include <util.h>
  31. #endif
  32. #endif
  33. /* support routines for comma separated value files */
  34. char *get_arg_in_quotes(struct session *ses, char *string, char *result, int flag)
  35. {
  36. char *pti, *pto;
  37. int nest = 0;
  38. pti = space_out(string);
  39. pto = result;
  40. if (*pti == '"')
  41. {
  42. nest = TRUE;
  43. pti++;
  44. }
  45. while (*pti)
  46. {
  47. if (HAS_BIT(ses->flags, SES_FLAG_BIG5) && *pti & 128 && pti[1] != 0)
  48. {
  49. *pto++ = *pti++;
  50. *pto++ = *pti++;
  51. continue;
  52. }
  53. if (*pti == '"')
  54. {
  55. if (pti[1] == '"')
  56. {
  57. *pto++ = *pti++;
  58. }
  59. else if (nest)
  60. {
  61. nest = FALSE;
  62. }
  63. pti++;
  64. continue;
  65. }
  66. else if (nest == TRUE)
  67. {
  68. *pto++ = *pti++;
  69. }
  70. else if (*pti == ' ' || *pti == '\t')
  71. {
  72. pti++;
  73. }
  74. else if (*pti == ',')
  75. {
  76. pti++;
  77. break;
  78. }
  79. else
  80. {
  81. *pto++ = *pti++;
  82. }
  83. }
  84. if (nest)
  85. {
  86. tintin_printf2(ses, "#SCAN CSV: GET QUOTED ARGUMENT: UNMATCHED QUOTE.");
  87. }
  88. *pto = 0;
  89. return pti;
  90. }
  91. struct session *scan_csv_file(struct session *ses, FILE *fp, char *filename)
  92. {
  93. char line[STRING_SIZE], temp[BUFFER_SIZE], *arg;
  94. int i, header = FALSE;
  95. SET_BIT(ses->flags, SES_FLAG_SCAN);
  96. while (fgets(line, BUFFER_SIZE, fp))
  97. {
  98. arg = strchr(line, '\r');
  99. if (arg)
  100. {
  101. *arg = 0;
  102. }
  103. else
  104. {
  105. arg = strchr(line, '\n');
  106. if (arg)
  107. {
  108. *arg = 0;
  109. }
  110. }
  111. RESTRING(gtd->vars[0], line);
  112. arg = line;
  113. for (i = 1 ; i < 100 ; i++)
  114. {
  115. arg = get_arg_in_quotes(ses, arg, temp, FALSE);
  116. RESTRING(gtd->vars[i], temp);
  117. if (*arg == 0)
  118. {
  119. while (++i < 100)
  120. {
  121. if (*gtd->vars[i])
  122. {
  123. RESTRING(gtd->vars[i], "");
  124. }
  125. }
  126. break;
  127. }
  128. }
  129. if (header == FALSE)
  130. {
  131. header = TRUE;
  132. check_all_events(ses, SUB_ARG|SUB_SEC, 0, 0, "SCAN CSV HEADER");
  133. }
  134. else
  135. {
  136. check_all_events(ses, SUB_ARG|SUB_SEC, 0, 0, "SCAN CSV LINE");
  137. }
  138. if (HAS_BIT(ses->flags, SES_FLAG_SCANABORT))
  139. {
  140. break;
  141. }
  142. }
  143. DEL_BIT(ses->flags, SES_FLAG_SCAN);
  144. if (HAS_BIT(ses->flags, SES_FLAG_SCANABORT))
  145. {
  146. DEL_BIT(ses->flags, SES_FLAG_SCANABORT);
  147. show_message(ses, LIST_COMMAND, "#SCAN CSV: FILE {%s} PARTIALLY SCANNED.", filename);
  148. }
  149. else
  150. {
  151. show_message(ses, LIST_COMMAND, "#SCAN CSV: FILE {%s} SCANNED.", filename);
  152. }
  153. fclose(fp);
  154. return ses;
  155. }
  156. /* support routines for tab separated value files */
  157. char *get_arg_stop_tabs(struct session *ses, char *string, char *result, int flag)
  158. {
  159. char *pti, *pto;
  160. pti = string;
  161. pto = result;
  162. while (*pti)
  163. {
  164. if (HAS_BIT(ses->flags, SES_FLAG_BIG5) && *pti & 128 && pti[1] != 0)
  165. {
  166. *pto++ = *pti++;
  167. *pto++ = *pti++;
  168. continue;
  169. }
  170. if (*pti == '\t')
  171. {
  172. pti++;
  173. break;
  174. }
  175. *pto++ = *pti++;
  176. }
  177. *pto = 0;
  178. return pti;
  179. }
  180. struct session *scan_tsv_file(struct session *ses, FILE *fp, char *filename)
  181. {
  182. char line[STRING_SIZE], temp[BUFFER_SIZE], *arg;
  183. int i, header = FALSE;
  184. SET_BIT(ses->flags, SES_FLAG_SCAN);
  185. while (fgets(line, BUFFER_SIZE, fp))
  186. {
  187. arg = strchr(line, '\r');
  188. if (arg)
  189. {
  190. *arg = 0;
  191. }
  192. else
  193. {
  194. arg = strchr(line, '\n');
  195. if (arg)
  196. {
  197. *arg = 0;
  198. }
  199. }
  200. RESTRING(gtd->vars[0], line);
  201. arg = line;
  202. for (i = 1 ; i < 100 ; i++)
  203. {
  204. arg = get_arg_stop_tabs(ses, arg, temp, FALSE);
  205. RESTRING(gtd->vars[i], temp);
  206. if (*arg == 0)
  207. {
  208. while (++i < 100)
  209. {
  210. if (*gtd->vars[i])
  211. {
  212. RESTRING(gtd->vars[i], "");
  213. }
  214. }
  215. break;
  216. }
  217. }
  218. if (header == FALSE)
  219. {
  220. header = TRUE;
  221. check_all_events(ses, SUB_ARG|SUB_SEC, 0, 0, "SCAN TSV HEADER");
  222. }
  223. else
  224. {
  225. check_all_events(ses, SUB_ARG|SUB_SEC, 0, 0, "SCAN TSV LINE");
  226. }
  227. if (HAS_BIT(ses->flags, SES_FLAG_SCANABORT))
  228. {
  229. break;
  230. }
  231. }
  232. DEL_BIT(ses->flags, SES_FLAG_SCAN);
  233. if (HAS_BIT(ses->flags, SES_FLAG_SCANABORT))
  234. {
  235. DEL_BIT(ses->flags, SES_FLAG_SCANABORT);
  236. show_message(ses, LIST_COMMAND, "#SCAN TSV: FILE {%s} PARTIALLY SCANNED.", filename);
  237. }
  238. else
  239. {
  240. show_message(ses, LIST_COMMAND, "#SCAN TSV: FILE {%s} SCANNED.", filename);
  241. }
  242. return ses;
  243. }
  244. /* support routines for text files */
  245. struct session *scan_txt_file(struct session *ses, FILE *fp, char *filename)
  246. {
  247. char line[STRING_SIZE], *arg;
  248. SET_BIT(ses->flags, SES_FLAG_SCAN);
  249. while (fgets(line, BUFFER_SIZE - 1, fp))
  250. {
  251. arg = strchr(line, '\r');
  252. if (arg)
  253. {
  254. *arg = 0;
  255. }
  256. else
  257. {
  258. arg = strchr(line, '\n');
  259. if (arg)
  260. {
  261. *arg = 0;
  262. }
  263. }
  264. process_mud_output(ses, line, FALSE);
  265. if (HAS_BIT(ses->flags, SES_FLAG_SCANABORT))
  266. {
  267. break;
  268. }
  269. }
  270. DEL_BIT(ses->flags, SES_FLAG_SCAN);
  271. if (HAS_BIT(ses->flags, SES_FLAG_SCANABORT))
  272. {
  273. DEL_BIT(ses->flags, SES_FLAG_SCANABORT);
  274. show_message(ses, LIST_COMMAND, "#SCAN TXT: FILE {%s} PARTIALLY SCANNED.", filename);
  275. }
  276. else
  277. {
  278. show_message(ses, LIST_COMMAND, "#SCAN TXT: FILE {%s} SCANNED.", filename);
  279. }
  280. return ses;
  281. }
  282. DO_COMMAND(do_scan)
  283. {
  284. FILE *fp;
  285. char arg1[BUFFER_SIZE], arg2[BUFFER_SIZE];
  286. arg = sub_arg_in_braces(ses, arg, arg1, GET_ONE, SUB_VAR|SUB_FUN);
  287. arg = sub_arg_in_braces(ses, arg, arg2, GET_ONE, SUB_VAR|SUB_FUN);
  288. if (*arg1 == 0)
  289. {
  290. show_error(ses, LIST_COMMAND, "#SYNTAX: #SCAN {ABORT|CSV|TXT} {<FILENAME>}");
  291. return ses;
  292. }
  293. if (is_abbrev(arg1, "ABORT"))
  294. {
  295. if (!HAS_BIT(ses->flags, SES_FLAG_SCAN))
  296. {
  297. show_error(ses, LIST_COMMAND, "#SCAN ABORT: NOT CURRENTLY SCANNING.");
  298. }
  299. else
  300. {
  301. SET_BIT(ses->flags, SES_FLAG_SCANABORT);
  302. }
  303. return ses;
  304. }
  305. if (*arg2 == 0)
  306. {
  307. show_error(ses, LIST_COMMAND, "#SYNTAX: #SCAN {ABORT|CSV|TXT} {<FILENAME>}");
  308. return ses;
  309. }
  310. if ((fp = fopen(arg2, "r")) == NULL)
  311. {
  312. show_error(ses, LIST_COMMAND, "#ERROR: #SCAN - FILE {%s} NOT FOUND.", arg2);
  313. return ses;
  314. }
  315. SET_BIT(ses->flags, SES_FLAG_SCAN);
  316. if (is_abbrev(arg1, "CSV"))
  317. {
  318. ses = scan_csv_file(ses, fp, arg2);
  319. }
  320. else if (is_abbrev(arg1, "TSV"))
  321. {
  322. ses = scan_tsv_file(ses, fp, arg2);
  323. }
  324. else if (is_abbrev(arg1, "TXT"))
  325. {
  326. ses = scan_txt_file(ses, fp, arg2);
  327. }
  328. else
  329. {
  330. DEL_BIT(ses->flags, SES_FLAG_SCAN);
  331. show_error(ses, LIST_COMMAND, "#SYNTAX: #SCAN {ABORT|CSV|TXT} {<FILENAME>}");
  332. }
  333. DEL_BIT(ses->flags, SES_FLAG_SCAN);
  334. fclose(fp);
  335. return ses;
  336. }