xref: /third_party/toybox/toys/other/help.c (revision 0f66f451)
10f66f451Sopenharmony_ci/* help.c - Show help for toybox commands
20f66f451Sopenharmony_ci *
30f66f451Sopenharmony_ci * Copyright 2007 Rob Landley <rob@landley.net>
40f66f451Sopenharmony_ci *
50f66f451Sopenharmony_ci * Often a shell builtin.
60f66f451Sopenharmony_ci
70f66f451Sopenharmony_ciUSE_HELP(NEWTOY(help, ""USE_HELP_EXTRAS("ah"), TOYFLAG_BIN))
80f66f451Sopenharmony_ci
90f66f451Sopenharmony_ciconfig HELP
100f66f451Sopenharmony_ci  bool "help"
110f66f451Sopenharmony_ci  default y
120f66f451Sopenharmony_ci  depends on TOYBOX_HELP
130f66f451Sopenharmony_ci  help
140f66f451Sopenharmony_ci    usage: help [command]
150f66f451Sopenharmony_ci
160f66f451Sopenharmony_ci    Show usage information for toybox commands.
170f66f451Sopenharmony_ci    Run "toybox" with no arguments for a list of available commands.
180f66f451Sopenharmony_ci
190f66f451Sopenharmony_ciconfig HELP_EXTRAS
200f66f451Sopenharmony_ci  bool "help -ah"
210f66f451Sopenharmony_ci  default y
220f66f451Sopenharmony_ci  depends on TOYBOX
230f66f451Sopenharmony_ci  depends on HELP
240f66f451Sopenharmony_ci  help
250f66f451Sopenharmony_ci    usage: help [-ah]
260f66f451Sopenharmony_ci
270f66f451Sopenharmony_ci    -a	All commands
280f66f451Sopenharmony_ci    -h	HTML output
290f66f451Sopenharmony_ci*/
300f66f451Sopenharmony_ci
310f66f451Sopenharmony_ci#define FOR_help
320f66f451Sopenharmony_ci#include "toys.h"
330f66f451Sopenharmony_ci
340f66f451Sopenharmony_cistatic void do_help(struct toy_list *t)
350f66f451Sopenharmony_ci{
360f66f451Sopenharmony_ci  if (toys.optflags & FLAG_h)
370f66f451Sopenharmony_ci    xprintf("<a name=\"%s\"><h1>%s</h1><blockquote><pre>\n", t->name, t->name);
380f66f451Sopenharmony_ci
390f66f451Sopenharmony_ci  toys.which = t;
400f66f451Sopenharmony_ci  show_help(stdout);
410f66f451Sopenharmony_ci
420f66f451Sopenharmony_ci  if (toys.optflags & FLAG_h) xprintf("</blockquote></pre>\n");
430f66f451Sopenharmony_ci}
440f66f451Sopenharmony_ci
450f66f451Sopenharmony_ci// The simple help is just toys.which = toy_find("name"); show_help(stdout);
460f66f451Sopenharmony_ci// But iterating through html output and all commands is a big more
470f66f451Sopenharmony_ci
480f66f451Sopenharmony_civoid help_main(void)
490f66f451Sopenharmony_ci{
500f66f451Sopenharmony_ci  int i;
510f66f451Sopenharmony_ci
520f66f451Sopenharmony_ci  if (!(toys.optflags & FLAG_a)) {
530f66f451Sopenharmony_ci    struct toy_list *t = toys.which;
540f66f451Sopenharmony_ci
550f66f451Sopenharmony_ci    if (*toys.optargs && !(t = toy_find(*toys.optargs)))
560f66f451Sopenharmony_ci      error_exit("Unknown command '%s'", *toys.optargs);
570f66f451Sopenharmony_ci    do_help(t);
580f66f451Sopenharmony_ci    return;
590f66f451Sopenharmony_ci  }
600f66f451Sopenharmony_ci
610f66f451Sopenharmony_ci  if (toys.optflags & FLAG_h) {
620f66f451Sopenharmony_ci    xprintf("<html>\n<title>Toybox command list</title>\n<body>\n<p>\n");
630f66f451Sopenharmony_ci    for (i=0; i < toys.toycount; i++)
640f66f451Sopenharmony_ci      xprintf("<a href=\"#%s\">%s\n</a>\n", toy_list[i].name,
650f66f451Sopenharmony_ci              toy_list[i].name);
660f66f451Sopenharmony_ci    xprintf("</p>\n");
670f66f451Sopenharmony_ci  }
680f66f451Sopenharmony_ci
690f66f451Sopenharmony_ci  for (i = 0; i < toys.toycount; i++) {
700f66f451Sopenharmony_ci    if (toys.optflags & FLAG_h) xprintf("<hr>\n<pre>\n");
710f66f451Sopenharmony_ci    else {
720f66f451Sopenharmony_ci      memset(toybuf, '-', 78);
730f66f451Sopenharmony_ci      memcpy(toybuf+3, toy_list[i].name, strlen(toy_list[i].name));
740f66f451Sopenharmony_ci      printf("\n%s\n\n", toybuf);
750f66f451Sopenharmony_ci    }
760f66f451Sopenharmony_ci    do_help(toy_list+i);
770f66f451Sopenharmony_ci    if (toys.optflags & FLAG_h) xprintf("</pre>\n");
780f66f451Sopenharmony_ci  }
790f66f451Sopenharmony_ci
800f66f451Sopenharmony_ci  if (toys.optflags & FLAG_h) xprintf("</html>");
810f66f451Sopenharmony_ci}
82