diff options
| author | David M. Syzdek <david@syzdek.net> | 2022-03-28 13:38:29 -0800 |
|---|---|---|
| committer | David M. Syzdek <david@syzdek.net> | 2022-03-28 14:19:04 -0800 |
| commit | 77568e2cc912644bada5ad3b406bedc34753fca9 (patch) | |
| tree | ba37b50aa2ed91acdf4af8076205fabda4ec29d8 | |
| parent | 78d3dd29179b1da6df63b35e28a9702df6690f49 (diff) | |
| download | tinyrad-77568e2cc912644bada5ad3b406bedc34753fca9.tar.gz tinyrad-77568e2cc912644bada5ad3b406bedc34753fca9.tar.bz2 tinyrad-77568e2cc912644bada5ad3b406bedc34753fca9.tar.xz tinyrad-77568e2cc912644bada5ad3b406bedc34753fca9.zip | |
tinyrad: adding support for widget aliases
| -rw-r--r-- | Makefile.am | 7 | ||||
| -rw-r--r-- | src/tru/tinyradutil.c | 121 | ||||
| -rw-r--r-- | src/tru/tinyradutil.h | 6 |
3 files changed, 106 insertions, 28 deletions
diff --git a/Makefile.am b/Makefile.am index 31d3d60..e6f3dfd 100644 --- a/Makefile.am +++ b/Makefile.am | |||
| @@ -478,6 +478,11 @@ install-exec-local: | |||
| 478 | install-data-local: | 478 | install-data-local: |
| 479 | 479 | ||
| 480 | install-data-hook: | 480 | install-data-hook: |
| 481 | if test -f $(DESTDIR)/$(bindir)/tinyrad;then\ | ||
| 482 | cd $(DESTDIR)/$(bindir) && \ | ||
| 483 | ln -fs tinyrad tinyrad-conf && \ | ||
| 484 | ln -fs tinyrad tinyrad-dict; \ | ||
| 485 | fi; | ||
| 481 | if test -f $(DESTDIR)/$(mandir)/man3/tinyrad_array.3;then\ | 486 | if test -f $(DESTDIR)/$(mandir)/man3/tinyrad_array.3;then\ |
| 482 | cd $(DESTDIR)/$(mandir)/man3 && \ | 487 | cd $(DESTDIR)/$(mandir)/man3 && \ |
| 483 | ln -fs tinyrad_array.3 tinyrad_array_get.3 && \ | 488 | ln -fs tinyrad_array.3 tinyrad_array_get.3 && \ |
| @@ -513,6 +518,8 @@ uninstall-local: | |||
| 513 | 518 | ||
| 514 | 519 | ||
| 515 | uninstall-hook: | 520 | uninstall-hook: |
| 521 | rm -f $(DESTDIR)/$(bindir)/tinyrad-conf | ||
| 522 | rm -f $(DESTDIR)/$(bindir)/tinyrad-dict | ||
| 516 | rm -f $(DESTDIR)/$(mandir)/man3/tinyrad_array_add.3 | 523 | rm -f $(DESTDIR)/$(mandir)/man3/tinyrad_array_add.3 |
| 517 | rm -f $(DESTDIR)/$(mandir)/man3/tinyrad_array_dequeue.3 | 524 | rm -f $(DESTDIR)/$(mandir)/man3/tinyrad_array_dequeue.3 |
| 518 | rm -f $(DESTDIR)/$(mandir)/man3/tinyrad_array_enqueue.3 | 525 | rm -f $(DESTDIR)/$(mandir)/man3/tinyrad_array_enqueue.3 |
diff --git a/src/tru/tinyradutil.c b/src/tru/tinyradutil.c index 9f4ab4f..b5c709e 100644 --- a/src/tru/tinyradutil.c +++ b/src/tru/tinyradutil.c | |||
| @@ -86,6 +86,11 @@ main( | |||
| 86 | char * argv[] ); | 86 | char * argv[] ); |
| 87 | 87 | ||
| 88 | 88 | ||
| 89 | const char * | ||
| 90 | tru_basename( | ||
| 91 | const char * path ); | ||
| 92 | |||
| 93 | |||
| 89 | void | 94 | void |
| 90 | tru_cleanup( | 95 | tru_cleanup( |
| 91 | TinyRadUtilConf * cnf ); | 96 | TinyRadUtilConf * cnf ); |
| @@ -111,14 +116,14 @@ const TinyRadUtilWidget tru_widget_map[] = | |||
| 111 | .name = "configuration", | 116 | .name = "configuration", |
| 112 | .desc = "print configuration", | 117 | .desc = "print configuration", |
| 113 | .usage = NULL, | 118 | .usage = NULL, |
| 114 | .aliases = NULL, | 119 | .aliases = (const char * const[]) { TRU_PREFIX"-conf", TRU_PREFIX"-configuration", NULL }, |
| 115 | .func = &tru_widget_config | 120 | .func = &tru_widget_config |
| 116 | }, | 121 | }, |
| 117 | { | 122 | { |
| 118 | .name = "dictionary", | 123 | .name = "dictionary", |
| 119 | .desc = "print processed dictionary", | 124 | .desc = "print processed dictionary", |
| 120 | .usage = NULL, | 125 | .usage = NULL, |
| 121 | .aliases = NULL, | 126 | .aliases = (const char * const[]) { TRU_PREFIX"-dict", TRU_PREFIX"-dictionary", NULL }, |
| 122 | .func = &tru_widget_dict | 127 | .func = &tru_widget_dict |
| 123 | }, | 128 | }, |
| 124 | { | 129 | { |
| @@ -156,31 +161,44 @@ int main(int argc, char * argv[]) | |||
| 156 | trutils_initialize(PROGRAM_NAME); | 161 | trutils_initialize(PROGRAM_NAME); |
| 157 | 162 | ||
| 158 | memset(&cnfdata, 0, sizeof(cnfdata)); | 163 | memset(&cnfdata, 0, sizeof(cnfdata)); |
| 159 | cnf = &cnfdata; | 164 | cnf = &cnfdata; |
| 160 | dict = NULL; | 165 | dict = NULL; |
| 166 | cnf->prog_name = tru_basename(argv[0]); | ||
| 161 | 167 | ||
| 162 | // process common cli options | 168 | // skip argument processing if called via alias |
| 163 | if ((rc = tru_cli_parse(cnf, argc, argv, short_opt, long_opt)) != 0) | 169 | if ((cnf->widget = tru_widget_lookup(cnf->prog_name, 1)) != NULL) |
| 164 | { | 170 | { |
| 165 | tru_cleanup(cnf); | 171 | cnf->argc = argc; |
| 166 | return((rc == 0) ? 0 : 1); | 172 | cnf->argv = argv; |
| 173 | cnf->widget_name = cnf->prog_name; | ||
| 167 | }; | 174 | }; |
| 168 | if ((argc - optind) < 1) | ||
| 169 | { | ||
| 170 | fprintf(stderr, "%s: missing required argument\n", PROGRAM_NAME); | ||
| 171 | fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME); | ||
| 172 | return(1); | ||
| 173 | }; | ||
| 174 | cnf->argc = (argc - optind); | ||
| 175 | cnf->argv = &argv[optind]; | ||
| 176 | cnf->widget_name = cnf->argv[0]; | ||
| 177 | 175 | ||
| 178 | // looks up widget | 176 | if (!(cnf->widget)) |
| 179 | if ((cnf->widget = tru_widget_lookup(cnf->argv[0], TRAD_NO)) == NULL) | ||
| 180 | { | 177 | { |
| 181 | fprintf(stderr, "%s: unknown or ambiguous widget -- \"%s\"\n", PROGRAM_NAME, cnf->argv[0]); | 178 | // process common cli options |
| 182 | fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME); | 179 | if ((rc = tru_cli_parse(cnf, argc, argv, short_opt, long_opt)) != 0) |
| 183 | return(1); | 180 | { |
| 181 | tru_cleanup(cnf); | ||
| 182 | return((rc == 0) ? 0 : 1); | ||
| 183 | }; | ||
| 184 | if ((argc - optind) < 1) | ||
| 185 | { | ||
| 186 | fprintf(stderr, "%s: missing required argument\n", PROGRAM_NAME); | ||
| 187 | fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME); | ||
| 188 | return(1); | ||
| 189 | }; | ||
| 190 | |||
| 191 | cnf->widget_name = argv[optind]; | ||
| 192 | cnf->argc = (argc - optind); | ||
| 193 | cnf->argv = &argv[optind]; | ||
| 194 | |||
| 195 | // looks up widget | ||
| 196 | if ((cnf->widget = tru_widget_lookup(argv[optind], TRAD_NO)) == NULL) | ||
| 197 | { | ||
| 198 | fprintf(stderr, "%s: unknown or ambiguous widget -- \"%s\"\n", PROGRAM_NAME, cnf->argv[0]); | ||
| 199 | fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME); | ||
| 200 | return(1); | ||
| 201 | }; | ||
| 184 | }; | 202 | }; |
| 185 | 203 | ||
| 186 | // call widget | 204 | // call widget |
| @@ -195,9 +213,21 @@ int main(int argc, char * argv[]) | |||
| 195 | //-------------------------// | 213 | //-------------------------// |
| 196 | #pragma mark miscellaneous functions | 214 | #pragma mark miscellaneous functions |
| 197 | 215 | ||
| 216 | const char * | ||
| 217 | tru_basename( | ||
| 218 | const char * path ) | ||
| 219 | { | ||
| 220 | const char * ptr; | ||
| 221 | assert(path != NULL); | ||
| 222 | if ((ptr = strrchr(path, '/'))) | ||
| 223 | return(&ptr[1]); | ||
| 224 | return(path); | ||
| 225 | } | ||
| 226 | |||
| 227 | |||
| 198 | void | 228 | void |
| 199 | tru_cleanup( | 229 | tru_cleanup( |
| 200 | TinyRadUtilConf * cnf ) | 230 | TinyRadUtilConf * cnf ) |
| 201 | { | 231 | { |
| 202 | tinyrad_free(cnf->tr); | 232 | tinyrad_free(cnf->tr); |
| 203 | tinyrad_strsfree(cnf->dict_files); | 233 | tinyrad_strsfree(cnf->dict_files); |
| @@ -377,16 +407,31 @@ tru_usage( | |||
| 377 | TinyRadUtilConf * cnf, | 407 | TinyRadUtilConf * cnf, |
| 378 | const char * short_opt ) | 408 | const char * short_opt ) |
| 379 | { | 409 | { |
| 380 | int i; | 410 | int x; |
| 411 | int y; | ||
| 412 | const TinyRadUtilWidget * widget; | ||
| 381 | 413 | ||
| 382 | tru_usage_summary(cnf); | 414 | tru_usage_summary(cnf); |
| 383 | tru_usage_options(short_opt); | 415 | tru_usage_options(short_opt); |
| 384 | if (!(cnf->widget)) | 416 | if (!(cnf->widget)) |
| 385 | { | 417 | { |
| 386 | printf("WIDGETS:\n"); | 418 | printf("WIDGETS:\n"); |
| 387 | for(i = 0; tru_widget_map[i].name != NULL; i++) | 419 | for(x = 0; tru_widget_map[x].name != NULL; x++) |
| 388 | if ((tru_widget_map[i].desc)) | 420 | { |
| 389 | printf(" %-25s %s\n", tru_widget_map[i].name, tru_widget_map[i].desc); | 421 | widget = &tru_widget_map[x]; |
| 422 | if ((widget->desc)) | ||
| 423 | printf(" %-25s %s\n", widget->name, widget->desc); | ||
| 424 | }; | ||
| 425 | |||
| 426 | printf("WIDGETS ALIASES:\n"); | ||
| 427 | for(x = 0; tru_widget_map[x].name != NULL; x++) | ||
| 428 | { | ||
| 429 | widget = &tru_widget_map[x]; | ||
| 430 | if (!(widget->aliases)) | ||
| 431 | continue; | ||
| 432 | for(y = 0; ((widget->aliases[y])); y++) | ||
| 433 | printf(" %-25s %s\n", ((!(y)) ? widget->name : ""), widget->aliases[y]); | ||
| 434 | }; | ||
| 390 | }; | 435 | }; |
| 391 | printf("\n"); | 436 | printf("\n"); |
| 392 | return(0); | 437 | return(0); |
| @@ -429,6 +474,9 @@ tru_usage_summary( | |||
| 429 | widget_help = ((cnf->widget->usage)) ? cnf->widget->usage : ""; | 474 | widget_help = ((cnf->widget->usage)) ? cnf->widget->usage : ""; |
| 430 | 475 | ||
| 431 | printf("Usage: %s [OPTIONS] %s [WIDGETOPTIONS]%s\n", PROGRAM_NAME, widget_name, widget_help); | 476 | printf("Usage: %s [OPTIONS] %s [WIDGETOPTIONS]%s\n", PROGRAM_NAME, widget_name, widget_help); |
| 477 | if ( (!(cnf->widget)) || (!(cnf->widget->aliases)) || (!(cnf->widget->aliases[0])) ) | ||
| 478 | return; | ||
| 479 | printf(" %s [WIDGETOPTIONS]%s\n", cnf->widget->aliases[0], widget_help); | ||
| 432 | 480 | ||
| 433 | return; | 481 | return; |
| 434 | } | 482 | } |
| @@ -441,6 +489,8 @@ tru_widget_lookup( | |||
| 441 | { | 489 | { |
| 442 | size_t wname_len; | 490 | size_t wname_len; |
| 443 | size_t x; | 491 | size_t x; |
| 492 | size_t y; | ||
| 493 | const char * alias; | ||
| 444 | const TinyRadUtilWidget * widget; | 494 | const TinyRadUtilWidget * widget; |
| 445 | const TinyRadUtilWidget * match; | 495 | const TinyRadUtilWidget * match; |
| 446 | 496 | ||
| @@ -462,6 +512,23 @@ tru_widget_lookup( | |||
| 462 | if (exact == TRAD_NO) | 512 | if (exact == TRAD_NO) |
| 463 | match = widget; | 513 | match = widget; |
| 464 | }; | 514 | }; |
| 515 | |||
| 516 | if (!(widget->aliases)) | ||
| 517 | continue; | ||
| 518 | |||
| 519 | for(y = 0; ((widget->aliases[y])); y++) | ||
| 520 | { | ||
| 521 | alias = widget->aliases[y]; | ||
| 522 | if (!(strncmp(alias, wname, wname_len))) | ||
| 523 | { | ||
| 524 | if (alias[wname_len] == '\0') | ||
| 525 | return(widget); | ||
| 526 | if ((match)) | ||
| 527 | return(NULL); | ||
| 528 | if (exact == TRAD_NO) | ||
| 529 | match = widget; | ||
| 530 | }; | ||
| 531 | }; | ||
| 465 | }; | 532 | }; |
| 466 | 533 | ||
| 467 | return(match); | 534 | return(match); |
diff --git a/src/tru/tinyradutil.h b/src/tru/tinyradutil.h index 3871c21..d816f73 100644 --- a/src/tru/tinyradutil.h +++ b/src/tru/tinyradutil.h | |||
| @@ -67,6 +67,10 @@ | |||
| 67 | #define PROGRAM_NAME "tinyrad" | 67 | #define PROGRAM_NAME "tinyrad" |
| 68 | 68 | ||
| 69 | 69 | ||
| 70 | #undef TRU_PREFIX | ||
| 71 | #define TRU_PREFIX "tinyrad" | ||
| 72 | |||
| 73 | |||
| 70 | #define TRU_GETOPT_MATCHED -2 | 74 | #define TRU_GETOPT_MATCHED -2 |
| 71 | #define TRU_GETOPT_ERROR -3 | 75 | #define TRU_GETOPT_ERROR -3 |
| 72 | #define TRU_GETOPT_EXIT -4 | 76 | #define TRU_GETOPT_EXIT -4 |
| @@ -120,7 +124,7 @@ struct tinyrad_util_config | |||
| 120 | char ** argv; | 124 | char ** argv; |
| 121 | const char * widget_name; | 125 | const char * widget_name; |
| 122 | size_t widget_name_len; | 126 | size_t widget_name_len; |
| 123 | void * padptr; | 127 | const char * prog_name; |
| 124 | const TinyRadUtilWidget * widget; | 128 | const TinyRadUtilWidget * widget; |
| 125 | TinyRad * tr; | 129 | TinyRad * tr; |
| 126 | const char * url; | 130 | const char * url; |
