diff options
| author | David M. Syzdek <david@syzdek.net> | 2022-03-30 15:08:24 -0800 |
|---|---|---|
| committer | David M. Syzdek <david@syzdek.net> | 2022-03-30 15:08:24 -0800 |
| commit | 5364b71b29fe95e12f426fc4b0b75b68389afa90 (patch) | |
| tree | 1f54c6a4c63c3616f79320a44f2d3809705ab807 | |
| parent | f0687e4e318555898342f3642d0c8635fd96f7e6 (diff) | |
| download | tinyrad-5364b71b29fe95e12f426fc4b0b75b68389afa90.tar.gz tinyrad-5364b71b29fe95e12f426fc4b0b75b68389afa90.tar.bz2 tinyrad-5364b71b29fe95e12f426fc4b0b75b68389afa90.tar.xz tinyrad-5364b71b29fe95e12f426fc4b0b75b68389afa90.zip | |
tinyrad: refactoring URL widget
| -rw-r--r-- | src/tru/tinyradutil.c | 11 | ||||
| -rw-r--r-- | src/tru/tinyradutil.h | 4 | ||||
| -rw-r--r-- | src/tru/widget-url.c | 74 |
3 files changed, 66 insertions, 23 deletions
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[] = | |||
| 129 | { | 129 | { |
| 130 | .name = "url", | 130 | .name = "url", |
| 131 | .desc = "process TinyRad URL", | 131 | .desc = "process TinyRad URL", |
| 132 | .usage = NULL, | 132 | .usage = " [ url url ... url ]", |
| 133 | .aliases = (const char * const[]) { TRU_PREFIX"-url", NULL }, | 133 | .aliases = (const char * const[]) { TRU_PREFIX"-url", NULL }, |
| 134 | .func = &tru_widget_url | 134 | .func = &tru_widget_url |
| 135 | }, | 135 | }, |
| @@ -388,6 +388,10 @@ tru_getopt( | |||
| 388 | tinyrad_set_option(NULL, TRAD_OPT_DEBUG_LEVEL, &opt); | 388 | tinyrad_set_option(NULL, TRAD_OPT_DEBUG_LEVEL, &opt); |
| 389 | return(TRU_GETOPT_MATCHED); | 389 | return(TRU_GETOPT_MATCHED); |
| 390 | 390 | ||
| 391 | case 'H': | ||
| 392 | cnf->url = optarg; | ||
| 393 | return(TRU_GETOPT_MATCHED); | ||
| 394 | |||
| 391 | case 'I': | 395 | case 'I': |
| 392 | if (tinyrad_strsadd(&cnf->dict_paths, optarg) != TRAD_SUCCESS) | 396 | if (tinyrad_strsadd(&cnf->dict_paths, optarg) != TRAD_SUCCESS) |
| 393 | { | 397 | { |
| @@ -467,12 +471,13 @@ tru_usage_options( | |||
| 467 | s = short_opt; | 471 | s = short_opt; |
| 468 | 472 | ||
| 469 | printf("OPTIONS:\n"); | 473 | printf("OPTIONS:\n"); |
| 470 | if ((strchr(s, '4'))) printf(" -4 use IPv4 addresses\n"); | 474 | if ((strchr(s, '4'))) printf(" -4, --ipv4 use IPv4 addresses\n"); |
| 471 | if ((strchr(s, '6'))) printf(" -6 use IPv6 addresses\n"); | 475 | if ((strchr(s, '6'))) printf(" -6, --ipv6 use IPv6 addresses\n"); |
| 472 | if ((strchr(s, 'b'))) printf(" -b, --builtin-dict load built-in dictionary\n"); | 476 | if ((strchr(s, 'b'))) printf(" -b, --builtin-dict load built-in dictionary\n"); |
| 473 | if ((strchr(s, 'D'))) printf(" -D dictionary include dictionary\n"); | 477 | if ((strchr(s, 'D'))) printf(" -D dictionary include dictionary\n"); |
| 474 | if ((strchr(s, 'd'))) printf(" -d level, --debug=level print debug messages\n"); | 478 | if ((strchr(s, 'd'))) printf(" -d level, --debug=level print debug messages\n"); |
| 475 | if ((strchr(s, 'f'))) printf(" -f file, --file=file attribute list\n"); | 479 | if ((strchr(s, 'f'))) printf(" -f file, --file=file attribute list\n"); |
| 480 | if ((strchr(s, 'H'))) printf(" -H uri RADIUS URI\n"); | ||
| 476 | if ((strchr(s, 'h'))) printf(" -h, --help print this help and exit\n"); | 481 | if ((strchr(s, 'h'))) printf(" -h, --help print this help and exit\n"); |
| 477 | if ((strchr(s, 'I'))) printf(" -I path add path to dictionary search paths\n"); | 482 | if ((strchr(s, 'I'))) printf(" -I path add path to dictionary search paths\n"); |
| 478 | if ((strchr(s, 'q'))) printf(" -q, --quiet, --silent do not print messages\n"); | 483 | 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 @@ | |||
| 95 | { "builtin-dict", no_argument, NULL, 'b' }, \ | 95 | { "builtin-dict", no_argument, NULL, 'b' }, \ |
| 96 | 96 | ||
| 97 | 97 | ||
| 98 | #define TRU_NET_SHORT "46" | 98 | #define TRU_NET_SHORT "H:46" |
| 99 | #define TRU_NET_LONG \ | 99 | #define TRU_NET_LONG \ |
| 100 | { "ipv4", no_argument, NULL, '4' }, \ | ||
| 101 | { "ipv6", no_argument, NULL, '6' }, \ | ||
| 100 | 102 | ||
| 101 | 103 | ||
| 102 | #define TRU_REQ_SHORT "f:" | 104 | #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 | |||
| @@ -57,6 +57,13 @@ | |||
| 57 | #pragma mark - Prototypes | 57 | #pragma mark - Prototypes |
| 58 | 58 | ||
| 59 | int | 59 | int |
| 60 | tru_widget_url_parse( | ||
| 61 | TinyRadUtilConf * cnf, | ||
| 62 | const char * url, | ||
| 63 | TinyRadURLDesc ** trudpp ); | ||
| 64 | |||
| 65 | |||
| 66 | int | ||
| 60 | tru_widget_url_print( | 67 | tru_widget_url_print( |
| 61 | TinyRadURLDesc * trudp ); | 68 | TinyRadURLDesc * trudp ); |
| 62 | 69 | ||
| @@ -95,11 +102,12 @@ tru_widget_url( | |||
| 95 | 102 | ||
| 96 | trudp = NULL; | 103 | trudp = NULL; |
| 97 | trudp_next = NULL; | 104 | trudp_next = NULL; |
| 105 | trudpp = &trudp; | ||
| 98 | 106 | ||
| 99 | // process widget cli options | 107 | // process widget cli options |
| 100 | if ((rc = tru_cli_parse(cnf, cnf->argc, cnf->argv, short_opt, long_opt, &tru_widget_url_usage)) != 0) | 108 | if ((rc = tru_cli_parse(cnf, cnf->argc, cnf->argv, short_opt, long_opt, &tru_widget_url_usage)) != 0) |
| 101 | return((rc == TRU_GETOPT_EXIT) ? 0 : 1); | 109 | return((rc == TRU_GETOPT_EXIT) ? 0 : 1); |
| 102 | if (optind >= cnf->argc) | 110 | if ( (optind >= cnf->argc) && (!(cnf->url)) ) |
| 103 | { | 111 | { |
| 104 | fprintf(stderr, "%s: missing required argument\n", PROGRAM_NAME); | 112 | fprintf(stderr, "%s: missing required argument\n", PROGRAM_NAME); |
| 105 | fprintf(stderr, "Try `%s %s --help' for more information.\n", PROGRAM_NAME, cnf->widget_name); | 113 | fprintf(stderr, "Try `%s %s --help' for more information.\n", PROGRAM_NAME, cnf->widget_name); |
| @@ -107,29 +115,21 @@ tru_widget_url( | |||
| 107 | }; | 115 | }; |
| 108 | 116 | ||
| 109 | // parse and resolve URLs | 117 | // parse and resolve URLs |
| 110 | trudpp = &trudp; | 118 | if ((cnf->url)) |
| 111 | for(pos = optind; (pos < cnf->argc); pos++) | ||
| 112 | { | 119 | { |
| 113 | if ((rc = tinyrad_urldesc_parse(cnf->argv[pos], trudpp)) != TRAD_SUCCESS) | 120 | if ((tru_widget_url_parse(cnf, cnf->url, trudpp)) != TRAD_SUCCESS) |
| 114 | { | 121 | { |
| 115 | if (!(cnf->opts & TRUTILS_OPT_QUIET)) | 122 | tinyrad_urldesc_free(trudp); |
| 116 | { | ||
| 117 | fprintf(stderr, "%s: %s: %s: %s\n", PROGRAM_NAME, cnf->widget_name, cnf->argv[pos], tinyrad_strerror(rc)); | ||
| 118 | fprintf(stderr, "Try `%s %s --help' for more information.\n", PROGRAM_NAME, cnf->widget_name); | ||
| 119 | }; | ||
| 120 | return(1); | 123 | return(1); |
| 121 | }; | 124 | }; |
| 122 | if ((cnf->opts & TRU_OPT_RESOLVE)) | 125 | trudpp = &(*trudpp)->trud_next; |
| 126 | }; | ||
| 127 | for(pos = optind; (pos < cnf->argc); pos++) | ||
| 128 | { | ||
| 129 | if ((tru_widget_url_parse(cnf, cnf->argv[pos], trudpp)) != TRAD_SUCCESS) | ||
| 123 | { | 130 | { |
| 124 | if ((rc = tinyrad_urldesc_resolve(*trudpp, cnf->tr_opts)) != TRAD_SUCCESS) | 131 | tinyrad_urldesc_free(trudp); |
| 125 | { | 132 | return(1); |
| 126 | if (!(cnf->opts & TRUTILS_OPT_QUIET)) | ||
| 127 | { | ||
| 128 | fprintf(stderr, "%s: %s: %s: %s\n", PROGRAM_NAME, cnf->widget_name, cnf->argv[pos], tinyrad_strerror(rc)); | ||
| 129 | fprintf(stderr, "Try `%s %s --help' for more information.\n", PROGRAM_NAME, cnf->widget_name); | ||
| 130 | }; | ||
| 131 | return(1); | ||
| 132 | }; | ||
| 133 | }; | 133 | }; |
| 134 | trudpp = &(*trudpp)->trud_next; | 134 | trudpp = &(*trudpp)->trud_next; |
| 135 | }; | 135 | }; |
| @@ -160,6 +160,42 @@ tru_widget_url( | |||
| 160 | 160 | ||
| 161 | 161 | ||
| 162 | int | 162 | int |
| 163 | tru_widget_url_parse( | ||
| 164 | TinyRadUtilConf * cnf, | ||
| 165 | const char * url, | ||
| 166 | TinyRadURLDesc ** trudpp ) | ||
| 167 | { | ||
| 168 | int rc; | ||
| 169 | |||
| 170 | // parse URL | ||
| 171 | if ((rc = tinyrad_urldesc_parse(url, trudpp)) != TRAD_SUCCESS) | ||
| 172 | { | ||
| 173 | if (!(cnf->opts & TRUTILS_OPT_QUIET)) | ||
| 174 | { | ||
| 175 | fprintf(stderr, "%s: %s: %s: %s\n", PROGRAM_NAME, cnf->widget_name, url, tinyrad_strerror(rc)); | ||
| 176 | fprintf(stderr, "Try `%s %s --help' for more information.\n", PROGRAM_NAME, cnf->widget_name); | ||
| 177 | }; | ||
| 178 | return(1); | ||
| 179 | }; | ||
| 180 | |||
| 181 | // resolve URL | ||
| 182 | if (!(cnf->opts & TRU_OPT_RESOLVE)) | ||
| 183 | return(TRAD_SUCCESS); | ||
| 184 | if ((rc = tinyrad_urldesc_resolve(*trudpp, cnf->tr_opts)) != TRAD_SUCCESS) | ||
| 185 | { | ||
| 186 | if (!(cnf->opts & TRUTILS_OPT_QUIET)) | ||
| 187 | { | ||
| 188 | fprintf(stderr, "%s: %s: %s: %s\n", PROGRAM_NAME, cnf->widget_name, url, tinyrad_strerror(rc)); | ||
| 189 | fprintf(stderr, "Try `%s %s --help' for more information.\n", PROGRAM_NAME, cnf->widget_name); | ||
| 190 | }; | ||
| 191 | return(1); | ||
| 192 | }; | ||
| 193 | |||
| 194 | return(TRAD_SUCCESS); | ||
| 195 | } | ||
| 196 | |||
| 197 | |||
| 198 | int | ||
| 163 | tru_widget_url_print( | 199 | tru_widget_url_print( |
| 164 | TinyRadURLDesc * trudp ) | 200 | TinyRadURLDesc * trudp ) |
| 165 | { | 201 | { |
