From 53272482ef40a4343c39f666ca2ff7c56e446762 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Thu, 9 Mar 2023 22:57:24 -0600 Subject: [PATCH] Fix up help plumbing. It's got HELP_FLAGS now. Last release "help toybox", "help -u", and "help -au" didn't work, and the "see" logic was all wrong. New rule: command --help and toybox --help command show toybox URL banner, but "help command" doesn't. --- main.c | 25 ++++++++++++++----------- toys.h | 5 +++++ toys/other/help.c | 26 ++++++++++++++------------ 3 files changed, 33 insertions(+), 23 deletions(-) diff --git a/main.c b/main.c index f43fad47..b4971bcd 100644 --- a/main.c +++ b/main.c @@ -76,31 +76,32 @@ static char *help_data = #include "generated/newtoys.h" ; -void show_help(FILE *out, int full) +void show_help(FILE *out, int flags) { int i = toys.which-toy_list; char *s, *ss; - if (!(full&2)) - fprintf(out, "Toybox %s"USE_TOYBOX(" multicall binary")"%s\n\n", - toybox_version, (CFG_TOYBOX && i) ? " (see toybox --help)" - : " (see https://landley.net/toybox)"); - if (CFG_TOYBOX_HELP) { + if (flags & HELP_HEADER) + fprintf(out, "Toybox %s"USE_TOYBOX(" multicall binary")"%s\n\n", + toybox_version, (CFG_TOYBOX && i) ? " (see toybox --help)" + : " (see https://landley.net/toybox)"); + for (;;) { s = help_data; while (i--) s += strlen(s) + 1; // If it's an alias, restart search for real name if (*s != 255) break; i = toy_find(++s)-toy_list; - if ((full&4) && toy_list[i].flags) { - fprintf(out, "See %s\n", s, s); + if ((flags & HELP_SEE) && toy_list[i].flags) { + if (flags & HELP_HTML) fprintf(out, "See %s\n", s, s); + else fprintf(out, "%s see %s\n", toys.which->name, s); return; } } - if (full) fprintf(out, "%s\n", s); + if (!(flags & HELP_USAGE)) fprintf(out, "%s\n", s); else { strstart(&s, "usage: "); for (ss = s; *ss && *ss!='\n'; ss++); @@ -124,9 +125,11 @@ void check_help(char **arg) if (toys.which->flags&TOYFLAG_NOHELP) return; if (!strcmp(*arg, "--help")) { - if (CFG_TOYBOX && toys.which == toy_list && arg[1]) + if (CFG_TOYBOX && toys.which == toy_list && arg[1]) { + toys.which = 0; if (!(toys.which = toy_find(arg[1]))) unknown(arg[1]); - show_help(stdout, 1); + } + show_help(stdout, HELP_HEADER); xexit(); } diff --git a/toys.h b/toys.h index d0ec2fd4..ace3b260 100644 --- a/toys.h +++ b/toys.h @@ -82,6 +82,11 @@ // These live in main.c +#define HELP_USAGE 1 // usage: line only +#define HELP_HEADER 2 // Add Toybox header line to help output +#define HELP_SEE 4 // "See COMMAND" instead of dereferencing alias +#define HELP_HTML 8 // Output HTML + struct toy_list *toy_find(char *name); void show_help(FILE *out, int full); void check_help(char **arg); diff --git a/toys/other/help.c b/toys/other/help.c index 70885e83..18fbed0d 100644 --- a/toys/other/help.c +++ b/toys/other/help.c @@ -30,44 +30,46 @@ static void do_help(struct toy_list *t) xprintf("

%s

\n", t->name, t->name);
 
   toys.which = t;
-  show_help(stdout, !FLAG(u)+(!!toys.argv[1]<<1)+(FLAG(h)<<2));
+  show_help(stdout, HELP_USAGE*FLAG(u) + (HELP_SEE|HELP_HTML)*FLAG(h));
 
   if (FLAG(h)) xprintf("
\n"); } -// Simple help is just toys.which = toy_find("name"); show_help(stdout, 1); +// Simple help is just toys.which = toy_find("name"); show_help(stdout, 0); // but iterating through html output and all commands is a bit more void help_main(void) { - int i; + long i; // If called with no arguments as a builtin from the shell, show all builtins if (toys.rebound && !*toys.optargs && !toys.optflags) { for (i = 0; i < toys.toycount; i++) { if (!(toy_list[i].flags&(TOYFLAG_NOFORK|TOYFLAG_MAYFORK))) continue; toys.which = toy_list+i; - show_help(stdout, FLAG(u)); + show_help(stdout, HELP_SEE|HELP_USAGE*!toys.optflags); } return; } if (!FLAG(a)) { - struct toy_list *t = toys.which; + struct toy_list *t = toys.which, *tt; - if (*toys.optargs && !(t = toy_find(*toys.optargs))) - error_exit("Unknown command '%s'", *toys.optargs); - do_help(t); - return; + // Zero which to include "toybox" in search, put it back for error msg + toys.which = 0; + tt = *toys.optargs ? toy_find(*toys.optargs) : t; + toys.which = t; + + if (tt) return do_help(tt); + else error_exit("Unknown command '%s'", *toys.optargs); } if (FLAG(h)) { sprintf(toybuf, "Toybox %s command help", toybox_version); xprintf("\n%s\n\n

%s


", toybuf, toybuf); - for (i=0; i%s \n", toy_list[i].name,toy_list[i].name); + for (i = 0; i%s\n", toy_list[i].name,toy_list[i].name); xprintf("

\n"); } -- 2.39.2