toybox/toys/other/help.c
<<
>>
Prefs
   1/* help.c - Show help for toybox commands
   2 *
   3 * Copyright 2007 Rob Landley <rob@landley.net>
   4 *
   5 * Often a shell builtin.
   6
   7USE_HELP(NEWTOY(help, "ahu", TOYFLAG_BIN|TOYFLAG_MAYFORK))
   8
   9config 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
  27static 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(u)+(!!toys.argv[1]<<1)+(!!FLAG(h)<<2));
  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
  41void 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      if (toy_list[i].flags)
  70        xprintf("<a href=\"#%s\">%s</a> \n", toy_list[i].name,toy_list[i].name);
  71    xprintf("</p>\n");
  72  }
  73
  74  for (i = 0; i < toys.toycount; i++) {
  75    if (!toy_list[i].flags) continue;
  76    if (FLAG(h)) xprintf("<hr>\n<pre>\n");
  77    else if (!FLAG(u)) {
  78      memset(toybuf, '-', 78);
  79      memcpy(toybuf+3, toy_list[i].name, strlen(toy_list[i].name));
  80      printf("\n%s\n\n", toybuf);
  81    }
  82    do_help(toy_list+i);
  83    if (FLAG(h)) xprintf("</pre>\n");
  84  }
  85
  86  if (FLAG(h)) xprintf("</html>");
  87}
  88