Subversion Repositories HelenOS

Rev

Rev 3364 | Rev 3376 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /* Copyright (c) 2008, Tim Post <tinkertim@gmail.com>
  2.  * All rights reserved.
  3.  *
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions are met:
  6.  *
  7.  * Redistributions of source code must retain the above copyright notice, this
  8.  * list of conditions and the following disclaimer.
  9.  *
  10.  * Redistributions in binary form must reproduce the above copyright notice,
  11.  * this list of conditions and the following disclaimer in the documentation
  12.  * and/or other materials provided with the distribution.
  13.  *
  14.  * Neither the name of the original program's authors nor the names of its
  15.  * contributors may be used to endorse or promote products derived from this
  16.  * software without specific prior written permission.
  17.  *
  18.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19.  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21.  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  22.  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  23.  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  24.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  25.  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  26.  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28.  * POSSIBILITY OF SUCH DAMAGE.
  29.  */
  30.  
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include "entry.h"
  35. #include "help.h"
  36. #include "cmds.h"
  37. #include "modules.h"
  38. #include "builtins.h"
  39. #include "errors.h"
  40.  
  41. static char *cmdname = "help";
  42. extern const char *progname;
  43.  
  44. #define HELP_IS_MODULE   1
  45. #define HELP_IS_BUILTIN  0
  46. #define HELP_IS_RUBBISH  -1
  47.  
  48. volatile int mod_switch = -1;
  49.  
  50. /* Just use a pointer here, no need for mod_switch */
  51. static int is_mod_or_builtin(char *cmd)
  52. {
  53.     int rc = HELP_IS_RUBBISH;
  54.  
  55.     rc = is_builtin(cmd);
  56.     if (rc > -1) {
  57.         mod_switch = rc;
  58.         return HELP_IS_BUILTIN;
  59.     }
  60.     rc = is_module(cmd);
  61.     if (rc > -1) {
  62.         mod_switch = rc;
  63.         return HELP_IS_MODULE;
  64.     }
  65.  
  66.     return HELP_IS_RUBBISH;
  67. }
  68.  
  69. void *help_cmd_help(unsigned int level)
  70. {
  71.     if (level == HELP_SHORT) {
  72.         printf(
  73.         "\n  %s [command] <extended>\n"
  74.         "  Use help [command] extended for detailed help on [command] "
  75.         ", even `help'\n\n", cmdname);
  76.     } else {
  77.         printf(
  78.         "\n  `%s' - shows help for commands\n"
  79.         "  Examples:\n"
  80.         "   %s [command]           Show help for [command]\n"
  81.         "   %s [command] extended  Show extended help for [command]\n"
  82.         "\n  If no argument is given to %s, a list of commands are shown\n\n",
  83.         cmdname, cmdname, cmdname, cmdname);
  84.     }
  85.  
  86.     return CMD_VOID;
  87. }
  88.  
  89. int *cmd_help(char *argv[])
  90. {
  91.     module_t *mod;
  92.     builtin_t *cmd;
  93.     unsigned int i = 0;
  94.     int rc = 0;
  95.     int argc;
  96.     int level = HELP_SHORT;
  97.  
  98.     for (argc = 0; argv[argc] != NULL; argc ++);
  99.  
  100.     if (argc > 3) {
  101.         printf("\nToo many arguments to `%s', try:\n", cmdname);
  102.         help_cmd_help(HELP_SHORT);
  103.         return CMD_FAILURE;
  104.     }
  105.  
  106.     if (argc == 3) {
  107.         if (!strcmp("extended", argv[2]))
  108.             level = HELP_LONG;
  109.         else
  110.             level = HELP_SHORT;
  111.     }
  112.  
  113.     if (argc > 1) {
  114.         rc = is_mod_or_builtin(argv[1]);
  115.         switch (rc) {
  116.         case HELP_IS_RUBBISH:
  117.             printf("Invalid command %s\n", argv[1]);
  118.             return CMD_FAILURE;
  119.         case HELP_IS_MODULE:
  120.             help_module(mod_switch, level);
  121.             return CMD_SUCCESS;
  122.         case HELP_IS_BUILTIN:
  123.             help_builtin(mod_switch, level);
  124.             return CMD_SUCCESS;
  125.         }
  126.     }
  127.  
  128.     printf("\n  Available commands are:\n");
  129.     printf("  ------------------------------------------------------------\n");
  130.  
  131.     /* First, show a list of built in commands that are available in this mode */
  132.     for (cmd = builtins; cmd->name != NULL; cmd++, i++) {
  133.         if (!builtin_is_restricted(i)) {
  134.             if (is_builtin_alias(cmd->name))
  135.                 printf("   %-16s\tAlias for `%s'\n", cmd->name,
  136.                     alias_for_builtin(cmd->name));
  137.             else
  138.                 printf("   %-16s\t%s\n", cmd->name, cmd->desc);
  139.         }
  140.     }
  141.  
  142.     i = 0;
  143.  
  144.     /* Now, show a list of module commands that are available in this mode */
  145.     for (mod = modules; mod->name != NULL; mod++, i++) {
  146.         if (!module_is_restricted(i)) {
  147.             if (is_module_alias(mod->name))
  148.                 printf("   %-16s\tAlias for `%s'\n", mod->name,
  149.                     alias_for_module(mod->name));
  150.             else
  151.                 printf("   %-16s\t%s\n", mod->name, mod->desc);
  152.         }
  153.     }
  154.  
  155.     printf("\n  Try %s %s for more information on how `%s' works.\n\n",
  156.         cmdname, cmdname, cmdname);
  157.  
  158.     return CMD_SUCCESS;
  159. }
  160.