Rev 3397 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
3265 | post | 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> |
||
3376 | post | 34 | |
35 | #include "config.h" |
||
3265 | post | 36 | #include "entry.h" |
37 | #include "help.h" |
||
38 | #include "cmds.h" |
||
39 | #include "modules.h" |
||
40 | #include "builtins.h" |
||
41 | #include "errors.h" |
||
3376 | post | 42 | #include "util.h" |
3265 | post | 43 | |
3269 | post | 44 | static char *cmdname = "help"; |
3265 | post | 45 | extern const char *progname; |
46 | |||
47 | #define HELP_IS_MODULE 1 |
||
48 | #define HELP_IS_BUILTIN 0 |
||
49 | #define HELP_IS_RUBBISH -1 |
||
50 | |||
51 | volatile int mod_switch = -1; |
||
52 | |||
53 | /* Just use a pointer here, no need for mod_switch */ |
||
3364 | post | 54 | static int is_mod_or_builtin(char *cmd) |
3265 | post | 55 | { |
56 | int rc = HELP_IS_RUBBISH; |
||
57 | |||
58 | rc = is_builtin(cmd); |
||
59 | if (rc > -1) { |
||
60 | mod_switch = rc; |
||
61 | return HELP_IS_BUILTIN; |
||
62 | } |
||
63 | rc = is_module(cmd); |
||
64 | if (rc > -1) { |
||
65 | mod_switch = rc; |
||
66 | return HELP_IS_MODULE; |
||
67 | } |
||
68 | |||
69 | return HELP_IS_RUBBISH; |
||
70 | } |
||
71 | |||
3492 | rimsky | 72 | void help_cmd_help(unsigned int level) |
3265 | post | 73 | { |
74 | if (level == HELP_SHORT) { |
||
75 | printf( |
||
76 | "\n %s [command] <extended>\n" |
||
77 | " Use help [command] extended for detailed help on [command] " |
||
78 | ", even `help'\n\n", cmdname); |
||
79 | } else { |
||
80 | printf( |
||
81 | "\n `%s' - shows help for commands\n" |
||
82 | " Examples:\n" |
||
83 | " %s [command] Show help for [command]\n" |
||
84 | " %s [command] extended Show extended help for [command]\n" |
||
85 | "\n If no argument is given to %s, a list of commands are shown\n\n", |
||
86 | cmdname, cmdname, cmdname, cmdname); |
||
87 | } |
||
88 | |||
3492 | rimsky | 89 | return; |
3265 | post | 90 | } |
91 | |||
3492 | rimsky | 92 | int cmd_help(char *argv[]) |
3265 | post | 93 | { |
94 | module_t *mod; |
||
95 | builtin_t *cmd; |
||
96 | unsigned int i = 0; |
||
97 | int rc = 0; |
||
98 | int argc; |
||
99 | int level = HELP_SHORT; |
||
100 | |||
3376 | post | 101 | argc = cli_count_args(argv); |
3265 | post | 102 | |
103 | if (argc > 3) { |
||
104 | printf("\nToo many arguments to `%s', try:\n", cmdname); |
||
105 | help_cmd_help(HELP_SHORT); |
||
106 | return CMD_FAILURE; |
||
107 | } |
||
108 | |||
109 | if (argc == 3) { |
||
110 | if (!strcmp("extended", argv[2])) |
||
111 | level = HELP_LONG; |
||
112 | else |
||
113 | level = HELP_SHORT; |
||
114 | } |
||
115 | |||
116 | if (argc > 1) { |
||
117 | rc = is_mod_or_builtin(argv[1]); |
||
118 | switch (rc) { |
||
119 | case HELP_IS_RUBBISH: |
||
120 | printf("Invalid command %s\n", argv[1]); |
||
121 | return CMD_FAILURE; |
||
122 | case HELP_IS_MODULE: |
||
123 | help_module(mod_switch, level); |
||
124 | return CMD_SUCCESS; |
||
125 | case HELP_IS_BUILTIN: |
||
126 | help_builtin(mod_switch, level); |
||
127 | return CMD_SUCCESS; |
||
128 | } |
||
129 | } |
||
130 | |||
3366 | post | 131 | printf("\n Available commands are:\n"); |
132 | printf(" ------------------------------------------------------------\n"); |
||
3265 | post | 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 | |||
3366 | post | 158 | printf("\n Try %s %s for more information on how `%s' works.\n\n", |
159 | cmdname, cmdname, cmdname); |
||
3265 | post | 160 | |
161 | return CMD_SUCCESS; |
||
162 | } |