From cc38472e32a2a6060be040510b7beb24a2f31d2b Mon Sep 17 00:00:00 2001 From: bol-van Date: Fri, 1 Nov 2024 14:36:14 +0300 Subject: [PATCH] nfqws,tpws: move config parse to separate function --- nfq/nfqws.c | 46 +++++++++++++++++++++++++--------------------- tpws/tpws.c | 45 ++++++++++++++++++++++++--------------------- 2 files changed, 49 insertions(+), 42 deletions(-) diff --git a/nfq/nfqws.c b/nfq/nfqws.c index b35e065..7a9debe 100644 --- a/nfq/nfqws.c +++ b/nfq/nfqws.c @@ -982,6 +982,30 @@ bool parse_tlspos(const char *s, enum tlspos *pos) return true; } +static void config_from_file(const char *filename) +{ + // config from a file + char buf[MAX_CONFIG_FILE_SIZE]; + buf[0]='x'; // fake argv[0] + buf[1]=' '; + size_t bufsize=sizeof(buf)-3; + if (!load_file(filename,buf+2,&bufsize)) + { + DLOG_ERR("could not load config file '%s'\n",filename); + exit_clean(1); + } + buf[bufsize+2]=0; + // wordexp fails if it sees \t \n \r between args + replace_char(buf,'\n',' '); + replace_char(buf,'\r',' '); + replace_char(buf,'\t',' '); + if (wordexp(buf, ¶ms.wexp, WRDE_NOCMD)) + { + DLOG_ERR("failed to split command line options from file '%s'\n",filename); + exit_clean(1); + } +} + int main(int argc, char **argv) { #ifdef __CYGWIN__ @@ -1048,27 +1072,7 @@ int main(int argc, char **argv) if (argc>=2 && argv[1][0]=='@') { - // config from a file - - char buf[MAX_CONFIG_FILE_SIZE]; - buf[0]='x'; // fake argv[0] - buf[1]=' '; - size_t bufsize=sizeof(buf)-3; - if (!load_file(argv[1]+1,buf+2,&bufsize)) - { - DLOG_ERR("could not load config file '%s'\n",argv[1]+1); - exit_clean(1); - } - buf[bufsize+2]=0; - // wordexp fails if it sees \t \n \r between args - replace_char(buf,'\n',' '); - replace_char(buf,'\r',' '); - replace_char(buf,'\t',' '); - if (wordexp(buf, ¶ms.wexp, WRDE_NOCMD)) - { - DLOG_ERR("failed to split command line options from file '%s'\n",argv[1]+1); - exit_clean(1); - } + config_from_file(argv[1]+1); argv=params.wexp.we_wordv; argc=params.wexp.we_wordc; } diff --git a/tpws/tpws.c b/tpws/tpws.c index 0bb4cf8..6f125d2 100644 --- a/tpws/tpws.c +++ b/tpws/tpws.c @@ -364,6 +364,29 @@ static bool parse_pf_list(char *opt, struct port_filters_head *pfl) return true; } +static void config_from_file(const char *filename) +{ + // config from a file + char buf[MAX_CONFIG_FILE_SIZE]; + buf[0]='x'; // fake argv[0] + buf[1]=' '; + size_t bufsize=sizeof(buf)-3; + if (!load_file(filename,buf+2,&bufsize)) + { + DLOG_ERR("could not load config file '%s'\n",filename); + exit_clean(1); + } + buf[bufsize+2]=0; + // wordexp fails if it sees \t \n \r between args + replace_char(buf,'\n',' '); + replace_char(buf,'\r',' '); + replace_char(buf,'\t',' '); + if (wordexp(buf, ¶ms.wexp, WRDE_NOCMD)) + { + DLOG_ERR("failed to split command line options from file '%s'\n",filename); + exit_clean(1); + } +} void parse_params(int argc, char *argv[]) { @@ -405,27 +428,7 @@ void parse_params(int argc, char *argv[]) if (argc>=2 && argv[1][0]=='@') { - // config from a file - - char buf[MAX_CONFIG_FILE_SIZE]; - buf[0]='x'; // fake argv[0] - buf[1]=' '; - size_t bufsize=sizeof(buf)-3; - if (!load_file(argv[1]+1,buf+2,&bufsize)) - { - DLOG_ERR("could not load config file '%s'\n",argv[1]+1); - exit_clean(1); - } - buf[bufsize+2]=0; - // wordexp fails if it sees \t \n \r between args - replace_char(buf,'\n',' '); - replace_char(buf,'\r',' '); - replace_char(buf,'\t',' '); - if (wordexp(buf, ¶ms.wexp, WRDE_NOCMD)) - { - DLOG_ERR("failed to split command line options from file '%s'\n",argv[1]+1); - exit_clean(1); - } + config_from_file(argv[1]+1); argv=params.wexp.we_wordv; argc=params.wexp.we_wordc; }