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