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