# HG changeset patch # User Rob Landley # Date 1396046882 18000 # Node ID 4654f241ccbe14d67c496716f83610bd7a770963 # Parent ee5a6875d695548ca9fb99f7b28a3aca17ac751d Add help -a (to show all commands) and -h (to produce HTML output). diff -r ee5a6875d695 -r 4654f241ccbe main.c --- a/main.c Thu Mar 27 07:02:01 2014 -0500 +++ b/main.c Fri Mar 28 17:48:02 2014 -0500 @@ -162,6 +162,8 @@ { if (CFG_TOYBOX_I18N) setlocale(LC_ALL, ""); + toys.toycount = ARRAY_LEN(toy_list); + if (CFG_TOYBOX) { // Trim path off of command name *argv = basename(*argv); diff -r ee5a6875d695 -r 4654f241ccbe toys.h --- a/toys.h Thu Mar 27 07:02:01 2014 -0500 +++ b/toys.h Fri Mar 28 17:48:02 2014 -0500 @@ -115,14 +115,15 @@ extern struct toy_context { struct toy_list *which; // Which entry in toy_list is this one? + char **argv; // Original command line arguments + char **optargs; // Arguments left over from get_optflags() + jmp_buf *rebound; // longjmp here instead of exit when do_rebound set + unsigned optflags; // Command line option flags from get_optflags() int exitval; // Value error_exit feeds to exit() - char **argv; // Original command line arguments - unsigned optflags; // Command line option flags from get_optflags() - char **optargs; // Arguments left over from get_optflags() int optc; // Count of optargs int exithelp; // Should error_exit print a usage message first? int old_umask; // Old umask preserved by TOYFLAG_UMASK - jmp_buf *rebound; // longjmp here instead of exit when do_rebound set + int toycount; // Total number of commands in this build } toys; // Two big temporary buffers: one for use by commands, one for library functions diff -r ee5a6875d695 -r 4654f241ccbe toys/other/help.c --- a/toys/other/help.c Thu Mar 27 07:02:01 2014 -0500 +++ b/toys/other/help.c Fri Mar 28 17:48:02 2014 -0500 @@ -4,7 +4,7 @@ * * Often a shell builtin. -USE_HELP(NEWTOY(help, "<1", TOYFLAG_BIN)) +USE_HELP(NEWTOY(help, ""USE_HELP_EXTRAS("ah"), TOYFLAG_BIN)) config HELP bool "help" @@ -15,16 +15,62 @@ Show usage information for toybox commands. Run "toybox" with no arguments for a list of available commands. + +config HELP_EXTRAS + bool "help -ah" + default y + depends on TOYBOX + depends on HELP + help + usage: help [-ah] + + -a All commands + -h HTML output */ +#define FOR_help +#include "toys.h" -#include "toys.h" +static void do_help(struct toy_list *t) +{ + if (toys.optflags & FLAG_h) + xprintf("

%s

\n", t->name, t->name);
+
+  toys.which = t;
+  show_help();
+
+  if (toys.optflags & FLAG_h) xprintf("
\n"); +} + +// The simple help is just toys.which = toy_find("name"); show_help(); +// But iterating through html output and all commands is a big more void help_main(void) { - struct toy_list *t = toy_find(*toys.optargs); + int i; + + if (!(toys.optflags & FLAG_a)) { + struct toy_list *t = toys.which; + + if (*toys.optargs && !(t = toy_find(*toys.optargs))) + error_exit("Unknown command '%s'", *toys.optargs); + do_help(t); + return; + } - if (!t) error_exit("Unknown command '%s'", *toys.optargs); - toys.which = t; - show_help(); + if (toys.optflags & FLAG_h) { + xprintf("\nToybox command list\n\n

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

\n"); + } + + for (i = 0; i < toys.toycount; i++) { + if (toys.optflags & FLAG_h) xprintf("
\n
\n");
+    do_help(toy_list+i);
+    if (toys.optflags & FLAG_h) xprintf("
\n"); + } + + if (toys.optflags & FLAG_h) xprintf(""); }