From a2cd46a5f342553c05ae6ef4e7a73069df0e1be1 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Fri, 3 Jan 2020 03:10:17 -0600 Subject: Add MAYFORK to "help", teach it to behave differently when called as a builtin, and add -u. --- lib/help.c | 11 ++++++++--- lib/lib.c | 2 +- lib/lib.h | 3 ++- main.c | 2 +- toys/other/help.c | 35 +++++++++++++++++++++++------------ 5 files changed, 35 insertions(+), 18 deletions(-) diff --git a/lib/help.c b/lib/help.c index 7b97e8b9..86d6392b 100644 --- a/lib/help.c +++ b/lib/help.c @@ -16,10 +16,10 @@ static char *help_data = #include "generated/newtoys.h" ; -void show_help(FILE *out) +void show_help(FILE *out, int full) { int i = toys.which-toy_list; - char *s; + char *s, *ss; if (CFG_TOYBOX_HELP) { for (;;) { @@ -30,6 +30,11 @@ void show_help(FILE *out) i = toy_find(++s)-toy_list; } - fprintf(out, "%s\n", s); + if (full) fprintf(out, "%s\n", s); + else { + strstart(&s, "usage: "); + for (ss = s; *ss && *ss!='\n'; ss++); + fprintf(out, "%.*s\n", (int)(ss-s), s); + } } } diff --git a/lib/lib.c b/lib/lib.c index 2250caf4..26ba546f 100644 --- a/lib/lib.c +++ b/lib/lib.c @@ -72,7 +72,7 @@ void help_exit(char *msg, ...) { va_list va; - if (!msg) show_help(stdout); + if (!msg) show_help(stdout, 1); else { va_start(va, msg); verror_msg(msg, -1, va); diff --git a/lib/lib.h b/lib/lib.h index 5ec648c8..398d261d 100644 --- a/lib/lib.h +++ b/lib/lib.h @@ -55,6 +55,7 @@ struct num_cache *add_num_cache(struct num_cache **cache, long long num, // args.c #define FLAGS_NODASH (1LL<<63) +#define FLAGS_BUILTIN (1LL<<62) void get_optflags(void); // dirtree.c @@ -107,7 +108,7 @@ struct dirtree *dirtree_read(char *path, int (*callback)(struct dirtree *node)); // help.c -void show_help(FILE *out); +void show_help(FILE *out, int full); // Tell xopen and friends to print warnings but return -1 as necessary // The largest O_BLAH flag so far is arch/alpha's O_PATH at 0x800000 so diff --git a/main.c b/main.c index 9d892e69..fc1461f6 100644 --- a/main.c +++ b/main.c @@ -89,7 +89,7 @@ static void toy_singleinit(struct toy_list *which, char *argv[]) if (!strcmp(argv[1], "--help")) { if (CFG_TOYBOX && toys.which == toy_list && toys.argv[2]) if (!(toys.which = toy_find(toys.argv[2]))) unknown(toys.argv[2]); - show_help(stdout); + show_help(stdout, 1); xexit(); } diff --git a/toys/other/help.c b/toys/other/help.c index 6ce1b02e..eba3cb1e 100644 --- a/toys/other/help.c +++ b/toys/other/help.c @@ -4,7 +4,7 @@ * * Often a shell builtin. -USE_HELP(NEWTOY(help, ""USE_HELP_EXTRAS("ah"), TOYFLAG_BIN)) +USE_HELP(NEWTOY(help, ""USE_HELP_EXTRAS("ahu"), TOYFLAG_BIN|TOYFLAG_MAYFORK)) config HELP bool "help" @@ -22,9 +22,10 @@ config HELP_EXTRAS depends on TOYBOX depends on HELP help - usage: help [-ah] + usage: help [-ah] [COMMAND] -a All commands + -u Usage only -h HTML output */ @@ -33,13 +34,13 @@ config HELP_EXTRAS static void do_help(struct toy_list *t) { - if (toys.optflags & FLAG_h) + if (FLAG(h)) xprintf("

%s

\n", t->name, t->name);
 
   toys.which = t;
-  show_help(stdout);
+  show_help(stdout, !FLAG(u));
 
-  if (toys.optflags & FLAG_h) xprintf("
\n"); + if (FLAG(h)) xprintf("\n"); } // The simple help is just toys.which = toy_find("name"); show_help(stdout); @@ -48,8 +49,18 @@ static void do_help(struct toy_list *t) void help_main(void) { int i; - - if (!(toys.optflags & FLAG_a)) { + + // If called with no arguments as a builtin form the shell, show all builtins + if (!*toys.optargs && (toys.optflags&FLAGS_BUILTIN)) { + 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, 0); + } + return; + } + + if (!FLAG(a)) { struct toy_list *t = toys.which; if (*toys.optargs && !(t = toy_find(*toys.optargs))) @@ -58,7 +69,7 @@ void help_main(void) return; } - if (toys.optflags & FLAG_h) { + if (FLAG(h)) { xprintf("\nToybox command list\n\n

\n"); for (i=0; i < toys.toycount; i++) xprintf("%s\n\n", toy_list[i].name, @@ -67,15 +78,15 @@ void help_main(void) } for (i = 0; i < toys.toycount; i++) { - if (toys.optflags & FLAG_h) xprintf("


\n
\n");
-    else {
+    if (FLAG(h)) xprintf("
\n
\n");
+    else if (!FLAG(u)) {
       memset(toybuf, '-', 78);
       memcpy(toybuf+3, toy_list[i].name, strlen(toy_list[i].name));
       printf("\n%s\n\n", toybuf);
     }
     do_help(toy_list+i);
-    if (toys.optflags & FLAG_h) xprintf("
\n"); + if (FLAG(h)) xprintf("
\n"); } - if (toys.optflags & FLAG_h) xprintf(""); + if (FLAG(h)) xprintf(""); } -- cgit v1.2.3