help.c 2.1 KB

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