From 5364b71b29fe95e12f426fc4b0b75b68389afa90 Mon Sep 17 00:00:00 2001 From: "David M. Syzdek" Date: Wed, 30 Mar 2022 15:08:24 -0800 Subject: tinyrad: refactoring URL widget --- src/tru/tinyradutil.c | 11 +++++--- src/tru/tinyradutil.h | 4 ++- src/tru/widget-url.c | 74 ++++++++++++++++++++++++++++++++++++++------------- 3 files changed, 66 insertions(+), 23 deletions(-) (limited to 'src') diff --git a/src/tru/tinyradutil.c b/src/tru/tinyradutil.c index 99c6def..1a853a8 100644 --- a/src/tru/tinyradutil.c +++ b/src/tru/tinyradutil.c @@ -129,7 +129,7 @@ const TinyRadUtilWidget tru_widget_map[] = { .name = "url", .desc = "process TinyRad URL", - .usage = NULL, + .usage = " [ url url ... url ]", .aliases = (const char * const[]) { TRU_PREFIX"-url", NULL }, .func = &tru_widget_url }, @@ -388,6 +388,10 @@ tru_getopt( tinyrad_set_option(NULL, TRAD_OPT_DEBUG_LEVEL, &opt); return(TRU_GETOPT_MATCHED); + case 'H': + cnf->url = optarg; + return(TRU_GETOPT_MATCHED); + case 'I': if (tinyrad_strsadd(&cnf->dict_paths, optarg) != TRAD_SUCCESS) { @@ -467,12 +471,13 @@ tru_usage_options( s = short_opt; printf("OPTIONS:\n"); - if ((strchr(s, '4'))) printf(" -4 use IPv4 addresses\n"); - if ((strchr(s, '6'))) printf(" -6 use IPv6 addresses\n"); + if ((strchr(s, '4'))) printf(" -4, --ipv4 use IPv4 addresses\n"); + if ((strchr(s, '6'))) printf(" -6, --ipv6 use IPv6 addresses\n"); if ((strchr(s, 'b'))) printf(" -b, --builtin-dict load built-in dictionary\n"); if ((strchr(s, 'D'))) printf(" -D dictionary include dictionary\n"); if ((strchr(s, 'd'))) printf(" -d level, --debug=level print debug messages\n"); if ((strchr(s, 'f'))) printf(" -f file, --file=file attribute list\n"); + if ((strchr(s, 'H'))) printf(" -H uri RADIUS URI\n"); if ((strchr(s, 'h'))) printf(" -h, --help print this help and exit\n"); if ((strchr(s, 'I'))) printf(" -I path add path to dictionary search paths\n"); if ((strchr(s, 'q'))) printf(" -q, --quiet, --silent do not print messages\n"); diff --git a/src/tru/tinyradutil.h b/src/tru/tinyradutil.h index 328505a..1b259a7 100644 --- a/src/tru/tinyradutil.h +++ b/src/tru/tinyradutil.h @@ -95,8 +95,10 @@ { "builtin-dict", no_argument, NULL, 'b' }, \ -#define TRU_NET_SHORT "46" +#define TRU_NET_SHORT "H:46" #define TRU_NET_LONG \ + { "ipv4", no_argument, NULL, '4' }, \ + { "ipv6", no_argument, NULL, '6' }, \ #define TRU_REQ_SHORT "f:" diff --git a/src/tru/widget-url.c b/src/tru/widget-url.c index 18d8137..4ff83d8 100644 --- a/src/tru/widget-url.c +++ b/src/tru/widget-url.c @@ -56,6 +56,13 @@ ////////////////// #pragma mark - Prototypes +int +tru_widget_url_parse( + TinyRadUtilConf * cnf, + const char * url, + TinyRadURLDesc ** trudpp ); + + int tru_widget_url_print( TinyRadURLDesc * trudp ); @@ -95,11 +102,12 @@ tru_widget_url( trudp = NULL; trudp_next = NULL; + trudpp = &trudp; // process widget cli options if ((rc = tru_cli_parse(cnf, cnf->argc, cnf->argv, short_opt, long_opt, &tru_widget_url_usage)) != 0) return((rc == TRU_GETOPT_EXIT) ? 0 : 1); - if (optind >= cnf->argc) + if ( (optind >= cnf->argc) && (!(cnf->url)) ) { fprintf(stderr, "%s: missing required argument\n", PROGRAM_NAME); fprintf(stderr, "Try `%s %s --help' for more information.\n", PROGRAM_NAME, cnf->widget_name); @@ -107,29 +115,21 @@ tru_widget_url( }; // parse and resolve URLs - trudpp = &trudp; - for(pos = optind; (pos < cnf->argc); pos++) + if ((cnf->url)) { - if ((rc = tinyrad_urldesc_parse(cnf->argv[pos], trudpp)) != TRAD_SUCCESS) + if ((tru_widget_url_parse(cnf, cnf->url, trudpp)) != TRAD_SUCCESS) { - if (!(cnf->opts & TRUTILS_OPT_QUIET)) - { - fprintf(stderr, "%s: %s: %s: %s\n", PROGRAM_NAME, cnf->widget_name, cnf->argv[pos], tinyrad_strerror(rc)); - fprintf(stderr, "Try `%s %s --help' for more information.\n", PROGRAM_NAME, cnf->widget_name); - }; + tinyrad_urldesc_free(trudp); return(1); }; - if ((cnf->opts & TRU_OPT_RESOLVE)) + trudpp = &(*trudpp)->trud_next; + }; + for(pos = optind; (pos < cnf->argc); pos++) + { + if ((tru_widget_url_parse(cnf, cnf->argv[pos], trudpp)) != TRAD_SUCCESS) { - if ((rc = tinyrad_urldesc_resolve(*trudpp, cnf->tr_opts)) != TRAD_SUCCESS) - { - if (!(cnf->opts & TRUTILS_OPT_QUIET)) - { - fprintf(stderr, "%s: %s: %s: %s\n", PROGRAM_NAME, cnf->widget_name, cnf->argv[pos], tinyrad_strerror(rc)); - fprintf(stderr, "Try `%s %s --help' for more information.\n", PROGRAM_NAME, cnf->widget_name); - }; - return(1); - }; + tinyrad_urldesc_free(trudp); + return(1); }; trudpp = &(*trudpp)->trud_next; }; @@ -159,6 +159,42 @@ tru_widget_url( } +int +tru_widget_url_parse( + TinyRadUtilConf * cnf, + const char * url, + TinyRadURLDesc ** trudpp ) +{ + int rc; + + // parse URL + if ((rc = tinyrad_urldesc_parse(url, trudpp)) != TRAD_SUCCESS) + { + if (!(cnf->opts & TRUTILS_OPT_QUIET)) + { + fprintf(stderr, "%s: %s: %s: %s\n", PROGRAM_NAME, cnf->widget_name, url, tinyrad_strerror(rc)); + fprintf(stderr, "Try `%s %s --help' for more information.\n", PROGRAM_NAME, cnf->widget_name); + }; + return(1); + }; + + // resolve URL + if (!(cnf->opts & TRU_OPT_RESOLVE)) + return(TRAD_SUCCESS); + if ((rc = tinyrad_urldesc_resolve(*trudpp, cnf->tr_opts)) != TRAD_SUCCESS) + { + if (!(cnf->opts & TRUTILS_OPT_QUIET)) + { + fprintf(stderr, "%s: %s: %s: %s\n", PROGRAM_NAME, cnf->widget_name, url, tinyrad_strerror(rc)); + fprintf(stderr, "Try `%s %s --help' for more information.\n", PROGRAM_NAME, cnf->widget_name); + }; + return(1); + }; + + return(TRAD_SUCCESS); +} + + int tru_widget_url_print( TinyRadURLDesc * trudp ) -- cgit v1.2.3