[216d6fc] | 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 | extern unsigned int cli_interactive;
|
---|
| 44 |
|
---|
| 45 | #define HELP_IS_MODULE 1
|
---|
| 46 | #define HELP_IS_BUILTIN 0
|
---|
| 47 | #define HELP_IS_RUBBISH -1
|
---|
| 48 |
|
---|
| 49 | volatile int mod_switch = -1;
|
---|
| 50 |
|
---|
| 51 | /* Just use a pointer here, no need for mod_switch */
|
---|
| 52 | int is_mod_or_builtin(char *cmd)
|
---|
| 53 | {
|
---|
| 54 | int rc = HELP_IS_RUBBISH;
|
---|
| 55 |
|
---|
| 56 | rc = is_builtin(cmd);
|
---|
| 57 | if (rc > -1) {
|
---|
| 58 | mod_switch = rc;
|
---|
| 59 | return HELP_IS_BUILTIN;
|
---|
| 60 | }
|
---|
| 61 | rc = is_module(cmd);
|
---|
| 62 | if (rc > -1) {
|
---|
| 63 | mod_switch = rc;
|
---|
| 64 | return HELP_IS_MODULE;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | return HELP_IS_RUBBISH;
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | void *help_cmd_help(unsigned int level)
|
---|
| 71 | {
|
---|
| 72 | if (level == HELP_SHORT) {
|
---|
| 73 | printf(
|
---|
| 74 | "\n %s [command] <extended>\n"
|
---|
| 75 | " Use help [command] extended for detailed help on [command] "
|
---|
| 76 | ", even `help'\n\n", cmdname);
|
---|
| 77 | } else {
|
---|
| 78 | printf(
|
---|
| 79 | "\n `%s' - shows help for commands\n"
|
---|
| 80 | " Examples:\n"
|
---|
| 81 | " %s [command] Show help for [command]\n"
|
---|
| 82 | " %s [command] extended Show extended help for [command]\n"
|
---|
| 83 | "\n If no argument is given to %s, a list of commands are shown\n\n",
|
---|
| 84 | cmdname, cmdname, cmdname, cmdname);
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | return CMD_VOID;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | int *cmd_help(char *argv[])
|
---|
| 91 | {
|
---|
| 92 | module_t *mod;
|
---|
| 93 | builtin_t *cmd;
|
---|
| 94 | unsigned int i = 0;
|
---|
| 95 | int rc = 0;
|
---|
| 96 | int argc;
|
---|
| 97 | int level = HELP_SHORT;
|
---|
| 98 |
|
---|
| 99 | for (argc = 0; argv[argc] != NULL; argc ++);
|
---|
| 100 |
|
---|
| 101 | if (argc > 3) {
|
---|
| 102 | printf("\nToo many arguments to `%s', try:\n", cmdname);
|
---|
| 103 | help_cmd_help(HELP_SHORT);
|
---|
| 104 | return CMD_FAILURE;
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | if (argc == 3) {
|
---|
| 108 | if (!strcmp("extended", argv[2]))
|
---|
| 109 | level = HELP_LONG;
|
---|
| 110 | else
|
---|
| 111 | level = HELP_SHORT;
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | if (argc > 1) {
|
---|
| 115 | rc = is_mod_or_builtin(argv[1]);
|
---|
| 116 | switch (rc) {
|
---|
| 117 | case HELP_IS_RUBBISH:
|
---|
| 118 | printf("Invalid command %s\n", argv[1]);
|
---|
| 119 | return CMD_FAILURE;
|
---|
| 120 | case HELP_IS_MODULE:
|
---|
| 121 | help_module(mod_switch, level);
|
---|
| 122 | return CMD_SUCCESS;
|
---|
| 123 | case HELP_IS_BUILTIN:
|
---|
| 124 | help_builtin(mod_switch, level);
|
---|
| 125 | return CMD_SUCCESS;
|
---|
| 126 | }
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | printf("%sAvailable commands are:\n", cli_interactive ? "\n " : "");
|
---|
| 130 | if (cli_interactive)
|
---|
| 131 | printf(
|
---|
| 132 | " ------------------------------------------------------------\n");
|
---|
| 133 |
|
---|
| 134 | /* First, show a list of built in commands that are available in this mode */
|
---|
| 135 | for (cmd = builtins; cmd->name != NULL; cmd++, i++) {
|
---|
| 136 | if (!builtin_is_restricted(i)) {
|
---|
| 137 | if (is_builtin_alias(cmd->name))
|
---|
| 138 | printf(" %-16s\tAlias for `%s'\n", cmd->name,
|
---|
| 139 | alias_for_builtin(cmd->name));
|
---|
| 140 | else
|
---|
| 141 | printf(" %-16s\t%s\n", cmd->name, cmd->desc);
|
---|
| 142 | }
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | i = 0;
|
---|
| 146 |
|
---|
| 147 | /* Now, show a list of module commands that are available in this mode */
|
---|
| 148 | for (mod = modules; mod->name != NULL; mod++, i++) {
|
---|
| 149 | if (!module_is_restricted(i)) {
|
---|
| 150 | if (is_module_alias(mod->name))
|
---|
| 151 | printf(" %-16s\tAlias for `%s'\n", mod->name,
|
---|
| 152 | alias_for_module(mod->name));
|
---|
| 153 | else
|
---|
| 154 | printf(" %-16s\t%s\n", mod->name, mod->desc);
|
---|
| 155 | }
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | /* Provide a little more information and inform them of history / line
|
---|
| 159 | * editing features if they are present */
|
---|
| 160 | if (cli_interactive)
|
---|
| 161 | printf("\n Try %s %s for more information on how `%s' works.\n\n",
|
---|
| 162 | cmdname, cmdname, cmdname);
|
---|
| 163 |
|
---|
| 164 | return CMD_SUCCESS;
|
---|
| 165 | }
|
---|