aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/tru
diff options
context:
space:
mode:
authorDavid M. Syzdek <david@syzdek.net>2022-03-31 12:28:33 -0800
committerDavid M. Syzdek <david@syzdek.net>2022-03-31 12:28:33 -0800
commitb86bd75f5bf15961d8c07bfe589a0087c71379b2 (patch)
treea47f811742835ef299ae713e3def47f43681e7be /src/tru
parentdb90beeb83143bb37fefecefc59de00c4b14bef8 (diff)
downloadtinyrad-b86bd75f5bf15961d8c07bfe589a0087c71379b2.tar.gz
tinyrad-b86bd75f5bf15961d8c07bfe589a0087c71379b2.tar.bz2
tinyrad-b86bd75f5bf15961d8c07bfe589a0087c71379b2.tar.xz
tinyrad-b86bd75f5bf15961d8c07bfe589a0087c71379b2.zip
tinyrad: refactoring tru_widget_lookup()
Diffstat (limited to 'src/tru')
-rw-r--r--src/tru/tinyradutil.c41
1 files changed, 19 insertions, 22 deletions
diff --git a/src/tru/tinyradutil.c b/src/tru/tinyradutil.c
index 8a95faf..fafd309 100644
--- a/src/tru/tinyradutil.c
+++ b/src/tru/tinyradutil.c
@@ -116,21 +116,21 @@ const TinyRadUtilWidget tru_widget_map[] =
116 .name = "config", 116 .name = "config",
117 .desc = "print configuration", 117 .desc = "print configuration",
118 .usage = NULL, 118 .usage = NULL,
119 .aliases = (const char * const[]) { TRU_PREFIX"-conf", TRU_PREFIX"-configuration", NULL }, 119 .aliases = (const char * const[]) { "configuration", NULL },
120 .func = &tru_widget_config 120 .func = &tru_widget_config
121 }, 121 },
122 { 122 {
123 .name = "dict", 123 .name = "dict",
124 .desc = "print processed dictionary", 124 .desc = "print processed dictionary",
125 .usage = NULL, 125 .usage = NULL,
126 .aliases = (const char * const[]) { TRU_PREFIX"-dict", TRU_PREFIX"-dictionary", NULL }, 126 .aliases = (const char * const[]) { "dictionary", NULL },
127 .func = &tru_widget_dict 127 .func = &tru_widget_dict
128 }, 128 },
129 { 129 {
130 .name = "url", 130 .name = "url",
131 .desc = "process TinyRad URL", 131 .desc = "process TinyRad URL",
132 .usage = " [ url url ... url ]", 132 .usage = " [ url url ... url ]",
133 .aliases = (const char * const[]) { TRU_PREFIX"-url", NULL }, 133 .aliases = NULL,
134 .func = &tru_widget_url 134 .func = &tru_widget_url
135 }, 135 },
136 { 136 {
@@ -174,7 +174,7 @@ int main(int argc, char * argv[])
174 cnf->prog_name = tru_basename(argv[0]); 174 cnf->prog_name = tru_basename(argv[0]);
175 175
176 // skip argument processing if called via alias 176 // skip argument processing if called via alias
177 if ((cnf->widget = tru_widget_lookup(cnf->prog_name, 1)) != NULL) 177 if ((cnf->widget = tru_widget_lookup(cnf->prog_name, TRAD_YES)) != NULL)
178 { 178 {
179 cnf->argc = argc; 179 cnf->argc = argc;
180 cnf->argv = argv; 180 cnf->argv = argv;
@@ -207,7 +207,7 @@ int main(int argc, char * argv[])
207 fprintf(stderr, "Try `%s --help' for more information.\n", cnf->prog_name); 207 fprintf(stderr, "Try `%s --help' for more information.\n", cnf->prog_name);
208 return(1); 208 return(1);
209 }; 209 };
210 snprintf(prog_name, sizeof(prog_name), "%s %s", cnf->prog_name, argv[optind]); 210 snprintf(prog_name, sizeof(prog_name), "%s %s", PROGRAM_NAME, cnf->widget->name);
211 cnf->prog_name = prog_name; 211 cnf->prog_name = prog_name;
212 }; 212 };
213 213
@@ -435,7 +435,6 @@ tru_usage(
435 const char * short_opt ) 435 const char * short_opt )
436{ 436{
437 int x; 437 int x;
438 int y;
439 const TinyRadUtilWidget * widget; 438 const TinyRadUtilWidget * widget;
440 439
441 tru_usage_summary(cnf); 440 tru_usage_summary(cnf);
@@ -449,16 +448,6 @@ tru_usage(
449 if ((widget->desc)) 448 if ((widget->desc))
450 printf(" %-25s %s\n", widget->name, widget->desc); 449 printf(" %-25s %s\n", widget->name, widget->desc);
451 }; 450 };
452
453 printf("WIDGETS ALIASES:\n");
454 for(x = 0; tru_widget_map[x].name != NULL; x++)
455 {
456 widget = &tru_widget_map[x];
457 if (!(widget->aliases))
458 continue;
459 for(y = 0; ((widget->aliases[y])); y++)
460 printf(" %-25s %s\n", ((!(y)) ? widget->name : ""), widget->aliases[y]);
461 };
462 }; 451 };
463 printf("\n"); 452 printf("\n");
464 return(0); 453 return(0);
@@ -504,10 +493,9 @@ tru_usage_summary(
504 if ((cnf->widget)) 493 if ((cnf->widget))
505 widget_help = ((cnf->widget->usage)) ? cnf->widget->usage : ""; 494 widget_help = ((cnf->widget->usage)) ? cnf->widget->usage : "";
506 495
507 printf("Usage: %s [OPTIONS] %s [WIDGETOPTIONS]%s\n", PROGRAM_NAME, widget_name, widget_help); 496 printf("Usage: %s [OPTIONS] %s [OPTIONS]%s\n", PROGRAM_NAME, widget_name, widget_help);
508 if ( (!(cnf->widget)) || (!(cnf->widget->aliases)) || (!(cnf->widget->aliases[0])) ) 497 printf(" %s-%s [OPTIONS]%s\n", PROGRAM_NAME, widget_name, widget_help);
509 return; 498 printf(" %s%s [OPTIONS]%s\n", PROGRAM_NAME, widget_name, widget_help);
510 printf(" %s [WIDGETOPTIONS]%s\n", cnf->widget->aliases[0], widget_help);
511 499
512 return; 500 return;
513} 501}
@@ -521,10 +509,19 @@ tru_widget_lookup(
521 size_t wname_len; 509 size_t wname_len;
522 size_t x; 510 size_t x;
523 size_t y; 511 size_t y;
512 size_t len;
524 const char * alias; 513 const char * alias;
525 const TinyRadUtilWidget * widget; 514 const TinyRadUtilWidget * widget;
526 const TinyRadUtilWidget * match; 515 const TinyRadUtilWidget * match;
527 516
517 len = strlen(PROGRAM_NAME);
518 if (!(strncasecmp(wname, PROGRAM_NAME, len)))
519 wname = &wname[len];
520 if (wname[0] == '-')
521 wname = &wname[1];
522 if (!(wname[0]))
523 return(NULL);
524
528 match = NULL; 525 match = NULL;
529 wname_len = strlen(wname); 526 wname_len = strlen(wname);
530 527
@@ -538,7 +535,7 @@ tru_widget_lookup(
538 { 535 {
539 if (widget->name[wname_len] == '\0') 536 if (widget->name[wname_len] == '\0')
540 return(widget); 537 return(widget);
541 if ((match)) 538 if ( ((match)) && (match != widget) )
542 return(NULL); 539 return(NULL);
543 if (exact == TRAD_NO) 540 if (exact == TRAD_NO)
544 match = widget; 541 match = widget;
@@ -554,7 +551,7 @@ tru_widget_lookup(
554 { 551 {
555 if (alias[wname_len] == '\0') 552 if (alias[wname_len] == '\0')
556 return(widget); 553 return(widget);
557 if ((match)) 554 if ( ((match)) && (match != widget) )
558 return(NULL); 555 return(NULL);
559 if (exact == TRAD_NO) 556 if (exact == TRAD_NO)
560 match = widget; 557 match = widget;