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