1 /* help.c - Show help for toybox commands
2  *
3  * Copyright 2007 Rob Landley <rob@landley.net>
4  *
5  * Often a shell builtin.
6 
7 USE_HELP(NEWTOY(help, "ahu", TOYFLAG_BIN|TOYFLAG_MAYFORK))
8 
9 config HELP
10   bool "help"
11   default y
12   depends on TOYBOX_HELP
13   help
14     usage: help [-ahu] [COMMAND]
15 
16     -a	All commands
17     -u	Usage only
18     -h	HTML output
19 
20     Show usage information for toybox commands.
21     Run "toybox" with no arguments for a list of available commands.
22 */
23 
24 #define FOR_help
25 #include "toys.h"
26 
do_help(struct toy_list * t)27 static void do_help(struct toy_list *t)
28 {
29   if (FLAG(h))
30     xprintf("<a name=\"%s\"><h1>%s</h1><blockquote><pre>\n", t->name, t->name);
31 
32   toys.which = t;
33   show_help(stdout, FLAG(h)+!FLAG(u));
34 
35   if (FLAG(h)) xprintf("</blockquote></pre>\n");
36 }
37 
38 // Simple help is just toys.which = toy_find("name"); show_help(stdout, 1);
39 // but iterating through html output and all commands is a bit more
40 
help_main(void)41 void help_main(void)
42 {
43   int i;
44 
45   // If called with no arguments as a builtin from the shell, show all builtins
46   if (toys.rebound && !*toys.optargs && !toys.optflags) {
47     for (i = 0; i < toys.toycount; i++) {
48       if (!(toy_list[i].flags&(TOYFLAG_NOFORK|TOYFLAG_MAYFORK))) continue;
49       toys.which = toy_list+i;
50       show_help(stdout, FLAG(u));
51     }
52     return;
53   }
54 
55   if (!FLAG(a)) {
56     struct toy_list *t = toys.which;
57 
58     if (*toys.optargs && !(t = toy_find(*toys.optargs)))
59       error_exit("Unknown command '%s'", *toys.optargs);
60     do_help(t);
61     return;
62   }
63 
64   if (FLAG(h)) {
65     sprintf(toybuf, "Toybox %s command help", toybox_version);
66     xprintf("<html>\n<title>%s</title>\n<body>\n<h1>%s</h1><hr /><p>",
67             toybuf, toybuf);
68     for (i=0; i < toys.toycount; i++)
69       xprintf("<a href=\"#%s\">%s</a> \n", toy_list[i].name, toy_list[i].name);
70     xprintf("</p>\n");
71   }
72 
73   for (i = 0; i < toys.toycount; i++) {
74     if (FLAG(h)) xprintf("<hr>\n<pre>\n");
75     else if (!FLAG(u)) {
76       memset(toybuf, '-', 78);
77       memcpy(toybuf+3, toy_list[i].name, strlen(toy_list[i].name));
78       printf("\n%s\n\n", toybuf);
79     }
80     do_help(toy_list+i);
81     if (FLAG(h)) xprintf("</pre>\n");
82   }
83 
84   if (FLAG(h)) xprintf("</html>");
85 }
86