From 5ce66a3ce8c8628e57556a5ae9dae4462f77253d Mon Sep 17 00:00:00 2001 From: "David M. Syzdek" Date: Mon, 28 Mar 2022 12:59:51 -0800 Subject: tinyrad: moving widget CLI parsing to widget --- src/tru/tinyradutil.c | 19 ------------------- src/tru/widget-config.c | 18 ++++++++++++++++++ src/tru/widget-dict.c | 18 ++++++++++++++++++ 3 files changed, 36 insertions(+), 19 deletions(-) diff --git a/src/tru/tinyradutil.c b/src/tru/tinyradutil.c index 2b5956d..0e1bef7 100644 --- a/src/tru/tinyradutil.c +++ b/src/tru/tinyradutil.c @@ -203,25 +203,6 @@ int main(int argc, char * argv[]) return(1); }; - // process widget cli options - if ((rc = tru_cli_parse(cnf, cnf->argc, cnf->argv, cnf->widget->shortopts, cnf->widget->longopts)) != 0) - { - tru_cleanup(cnf); - return((rc == 0) ? 0 : 1); - }; - if ((cnf->argc - optind) < cnf->widget->min_arg) - { - fprintf(stderr, "%s: missing required argument\n", PROGRAM_NAME); - fprintf(stderr, "Try `%s %s --help' for more information.\n", PROGRAM_NAME, cnf->widget_name); - return(1); - }; - if ((optind+cnf->widget->max_arg) > cnf->argc) - { - fprintf(stderr, "%s: unrecognized argument `-- %s'\n", PROGRAM_NAME, cnf->argv[optind+1]); - fprintf(stderr, "Try `%s %s --help' for more information.\n", PROGRAM_NAME, cnf->widget_name); - return(1); - }; - // call widget rc = cnf->widget->func(cnf); tru_cleanup(cnf); diff --git a/src/tru/widget-config.c b/src/tru/widget-config.c index 0f2e643..e1eaa68 100644 --- a/src/tru/widget-config.c +++ b/src/tru/widget-config.c @@ -56,11 +56,29 @@ tru_widget_config( { int rc; TinyRadDict * dict; + + // getopt options + static char short_opt[] = TRU_COMMON_SHORT TRU_DICT_SHORT; + static struct option long_opt[] = { TRU_DICT_LONG TRU_COMMON_LONG }; + + // process widget cli options + if ((rc = tru_cli_parse(cnf, cnf->argc, cnf->argv, short_opt, long_opt)) != 0) + { + return((rc == 0) ? 0 : 1); + }; + if (optind < cnf->argc) + { + fprintf(stderr, "%s: unrecognized argument `-- %s'\n", PROGRAM_NAME, cnf->argv[optind+1]); + fprintf(stderr, "Try `%s %s --help' for more information.\n", PROGRAM_NAME, cnf->widget_name); + return(1); + }; + if ((rc = tru_load_tinyrad(cnf)) != TRAD_SUCCESS) return(rc); tinyrad_get_option(cnf->tr, TRAD_OPT_DICTIONARY, &dict); tinyrad_conf_print(cnf->tr, dict); tinyrad_free(dict); + return(TRAD_SUCCESS); } diff --git a/src/tru/widget-dict.c b/src/tru/widget-dict.c index 793f308..8318af4 100644 --- a/src/tru/widget-dict.c +++ b/src/tru/widget-dict.c @@ -56,11 +56,29 @@ tru_widget_dict( { int rc; TinyRadDict * dict; + + // getopt options + static char short_opt[] = TRU_COMMON_SHORT TRU_DICT_SHORT; + static struct option long_opt[] = { TRU_DICT_LONG TRU_COMMON_LONG }; + + // process widget cli options + if ((rc = tru_cli_parse(cnf, cnf->argc, cnf->argv, short_opt, long_opt)) != 0) + { + return((rc == 0) ? 0 : 1); + }; + if (optind < cnf->argc) + { + fprintf(stderr, "%s: unrecognized argument `-- %s'\n", PROGRAM_NAME, cnf->argv[optind+1]); + fprintf(stderr, "Try `%s %s --help' for more information.\n", PROGRAM_NAME, cnf->widget_name); + return(1); + }; + if ((rc = tru_load_tinyrad(cnf)) != TRAD_SUCCESS) return(rc); tinyrad_get_option(cnf->tr, TRAD_OPT_DICTIONARY, &dict); tinyrad_dict_print(dict, 0xffff); tinyrad_free(dict); + return(TRAD_SUCCESS); } -- cgit v1.2.3