| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416 |
- /******************************************************************************
- * This file is part of TinTin++ *
- * *
- * Copyright 2004-2019 Igor van den Hoven *
- * *
- * TinTin++ is free software; you can redistribute it and/or modify *
- * it under the terms of the GNU General Public License as published by *
- * the Free Software Foundation; either version 3 of the License, or *
- * (at your option) any later version. *
- * *
- * This program is distributed in the hope that it will be useful, *
- * but WITHOUT ANY WARRANTY; without even the implied warranty of *
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
- * GNU General Public License for more details. *
- * *
- * *
- * You should have received a copy of the GNU General Public License *
- * along with TinTin++. If not, see https://www.gnu.org/licenses. *
- ******************************************************************************/
- /******************************************************************************
- * (T)he K(I)cki(N) (T)ickin D(I)kumud Clie(N)t *
- * *
- * coded by Igor van den Hoven 2005 *
- ******************************************************************************/
- #include "tintin.h"
- #ifdef HAVE_PTY_H
- #include <pty.h>
- #else
- #ifdef HAVE_UTIL_H
- #include <util.h>
- #endif
- #endif
- /* support routines for comma separated value files */
- char *get_arg_in_quotes(struct session *ses, char *string, char *result, int flag)
- {
- char *pti, *pto;
- int nest = 0;
- pti = space_out(string);
- pto = result;
- if (*pti == '"')
- {
- nest = TRUE;
- pti++;
- }
- while (*pti)
- {
- if (HAS_BIT(ses->flags, SES_FLAG_BIG5) && *pti & 128 && pti[1] != 0)
- {
- *pto++ = *pti++;
- *pto++ = *pti++;
- continue;
- }
- if (*pti == '"')
- {
- if (pti[1] == '"')
- {
- *pto++ = *pti++;
- }
- else if (nest)
- {
- nest = FALSE;
- }
- pti++;
- continue;
- }
- else if (nest == TRUE)
- {
- *pto++ = *pti++;
- }
- else if (*pti == ' ' || *pti == '\t')
- {
- pti++;
- }
- else if (*pti == ',')
- {
- pti++;
- break;
- }
- else
- {
- *pto++ = *pti++;
- }
- }
- if (nest)
- {
- tintin_printf2(ses, "#SCAN CSV: GET QUOTED ARGUMENT: UNMATCHED QUOTE.");
- }
- *pto = 0;
- return pti;
- }
- struct session *scan_csv_file(struct session *ses, FILE *fp, char *filename)
- {
- char line[STRING_SIZE], temp[BUFFER_SIZE], *arg;
- int i, header = FALSE;
- SET_BIT(ses->flags, SES_FLAG_SCAN);
- while (fgets(line, BUFFER_SIZE, fp))
- {
- arg = strchr(line, '\r');
- if (arg)
- {
- *arg = 0;
- }
- else
- {
-
- arg = strchr(line, '\n');
- if (arg)
- {
- *arg = 0;
- }
- }
- RESTRING(gtd->vars[0], line);
- arg = line;
- for (i = 1 ; i < 100 ; i++)
- {
- arg = get_arg_in_quotes(ses, arg, temp, FALSE);
- RESTRING(gtd->vars[i], temp);
- if (*arg == 0)
- {
- while (++i < 100)
- {
- if (*gtd->vars[i])
- {
- RESTRING(gtd->vars[i], "");
- }
- }
- break;
- }
- }
- if (header == FALSE)
- {
- header = TRUE;
- check_all_events(ses, SUB_ARG|SUB_SEC, 0, 0, "SCAN CSV HEADER");
- }
- else
- {
- check_all_events(ses, SUB_ARG|SUB_SEC, 0, 0, "SCAN CSV LINE");
- }
- if (HAS_BIT(ses->flags, SES_FLAG_SCANABORT))
- {
- break;
- }
- }
- DEL_BIT(ses->flags, SES_FLAG_SCAN);
- if (HAS_BIT(ses->flags, SES_FLAG_SCANABORT))
- {
- DEL_BIT(ses->flags, SES_FLAG_SCANABORT);
- show_message(ses, LIST_COMMAND, "#SCAN CSV: FILE {%s} PARTIALLY SCANNED.", filename);
- }
- else
- {
- show_message(ses, LIST_COMMAND, "#SCAN CSV: FILE {%s} SCANNED.", filename);
- }
- fclose(fp);
- return ses;
- }
- /* support routines for tab separated value files */
- char *get_arg_stop_tabs(struct session *ses, char *string, char *result, int flag)
- {
- char *pti, *pto;
- pti = string;
- pto = result;
- while (*pti)
- {
- if (HAS_BIT(ses->flags, SES_FLAG_BIG5) && *pti & 128 && pti[1] != 0)
- {
- *pto++ = *pti++;
- *pto++ = *pti++;
- continue;
- }
- if (*pti == '\t')
- {
- pti++;
- break;
- }
- *pto++ = *pti++;
- }
- *pto = 0;
- return pti;
- }
- struct session *scan_tsv_file(struct session *ses, FILE *fp, char *filename)
- {
- char line[STRING_SIZE], temp[BUFFER_SIZE], *arg;
- int i, header = FALSE;
- SET_BIT(ses->flags, SES_FLAG_SCAN);
- while (fgets(line, BUFFER_SIZE, fp))
- {
- arg = strchr(line, '\r');
- if (arg)
- {
- *arg = 0;
- }
- else
- {
-
- arg = strchr(line, '\n');
- if (arg)
- {
- *arg = 0;
- }
- }
- RESTRING(gtd->vars[0], line);
- arg = line;
- for (i = 1 ; i < 100 ; i++)
- {
- arg = get_arg_stop_tabs(ses, arg, temp, FALSE);
- RESTRING(gtd->vars[i], temp);
- if (*arg == 0)
- {
- while (++i < 100)
- {
- if (*gtd->vars[i])
- {
- RESTRING(gtd->vars[i], "");
- }
- }
- break;
- }
- }
- if (header == FALSE)
- {
- header = TRUE;
- check_all_events(ses, SUB_ARG|SUB_SEC, 0, 0, "SCAN TSV HEADER");
- }
- else
- {
- check_all_events(ses, SUB_ARG|SUB_SEC, 0, 0, "SCAN TSV LINE");
- }
- if (HAS_BIT(ses->flags, SES_FLAG_SCANABORT))
- {
- break;
- }
- }
- DEL_BIT(ses->flags, SES_FLAG_SCAN);
- if (HAS_BIT(ses->flags, SES_FLAG_SCANABORT))
- {
- DEL_BIT(ses->flags, SES_FLAG_SCANABORT);
- show_message(ses, LIST_COMMAND, "#SCAN TSV: FILE {%s} PARTIALLY SCANNED.", filename);
- }
- else
- {
- show_message(ses, LIST_COMMAND, "#SCAN TSV: FILE {%s} SCANNED.", filename);
- }
- return ses;
- }
- /* support routines for text files */
- struct session *scan_txt_file(struct session *ses, FILE *fp, char *filename)
- {
- char line[STRING_SIZE], *arg;
- SET_BIT(ses->flags, SES_FLAG_SCAN);
- while (fgets(line, BUFFER_SIZE - 1, fp))
- {
- arg = strchr(line, '\r');
- if (arg)
- {
- *arg = 0;
- }
- else
- {
-
- arg = strchr(line, '\n');
- if (arg)
- {
- *arg = 0;
- }
- }
- process_mud_output(ses, line, FALSE);
- if (HAS_BIT(ses->flags, SES_FLAG_SCANABORT))
- {
- break;
- }
- }
- DEL_BIT(ses->flags, SES_FLAG_SCAN);
- if (HAS_BIT(ses->flags, SES_FLAG_SCANABORT))
- {
- DEL_BIT(ses->flags, SES_FLAG_SCANABORT);
- show_message(ses, LIST_COMMAND, "#SCAN TXT: FILE {%s} PARTIALLY SCANNED.", filename);
- }
- else
- {
- show_message(ses, LIST_COMMAND, "#SCAN TXT: FILE {%s} SCANNED.", filename);
- }
- return ses;
- }
- DO_COMMAND(do_scan)
- {
- FILE *fp;
- char arg1[BUFFER_SIZE], arg2[BUFFER_SIZE];
- arg = sub_arg_in_braces(ses, arg, arg1, GET_ONE, SUB_VAR|SUB_FUN);
- arg = sub_arg_in_braces(ses, arg, arg2, GET_ONE, SUB_VAR|SUB_FUN);
- if (*arg1 == 0)
- {
- show_error(ses, LIST_COMMAND, "#SYNTAX: #SCAN {ABORT|CSV|TXT} {<FILENAME>}");
- return ses;
- }
- if (is_abbrev(arg1, "ABORT"))
- {
- if (!HAS_BIT(ses->flags, SES_FLAG_SCAN))
- {
- show_error(ses, LIST_COMMAND, "#SCAN ABORT: NOT CURRENTLY SCANNING.");
- }
- else
- {
- SET_BIT(ses->flags, SES_FLAG_SCANABORT);
- }
- return ses;
- }
- if (*arg2 == 0)
- {
- show_error(ses, LIST_COMMAND, "#SYNTAX: #SCAN {ABORT|CSV|TXT} {<FILENAME>}");
- return ses;
- }
- if ((fp = fopen(arg2, "r")) == NULL)
- {
- show_error(ses, LIST_COMMAND, "#ERROR: #SCAN - FILE {%s} NOT FOUND.", arg2);
- return ses;
- }
- SET_BIT(ses->flags, SES_FLAG_SCAN);
- if (is_abbrev(arg1, "CSV"))
- {
- ses = scan_csv_file(ses, fp, arg2);
- }
- else if (is_abbrev(arg1, "TSV"))
- {
- ses = scan_tsv_file(ses, fp, arg2);
- }
- else if (is_abbrev(arg1, "TXT"))
- {
- ses = scan_txt_file(ses, fp, arg2);
- }
- else
- {
- DEL_BIT(ses->flags, SES_FLAG_SCAN);
- show_error(ses, LIST_COMMAND, "#SYNTAX: #SCAN {ABORT|CSV|TXT} {<FILENAME>}");
- }
- DEL_BIT(ses->flags, SES_FLAG_SCAN);
- fclose(fp);
- return ses;
- }
|