Rev 3346 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3346 | Rev 3809 | ||
---|---|---|---|
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 | /* NOTES: |
31 | /* NOTES: |
32 | * module_* functions are pretty much identical to builtin_* functions at this |
32 | * module_* functions are pretty much identical to builtin_* functions at this |
33 | * point. On the surface, it would appear that making each function dual purpose |
33 | * point. On the surface, it would appear that making each function dual purpose |
34 | * would be economical. |
34 | * would be economical. |
35 | * |
35 | * |
36 | * These are kept separate because the structures (module_t and builtin_t) may |
36 | * These are kept separate because the structures (module_t and builtin_t) may |
37 | * grow apart and become rather different, even though they're identical at this |
37 | * grow apart and become rather different, even though they're identical at this |
38 | * point. |
38 | * point. |
39 | * |
39 | * |
40 | * To keep things easy to hack, everything is separated. In reality this only adds |
40 | * To keep things easy to hack, everything is separated. In reality this only adds |
41 | * 6 - 8 extra functions, but keeps each function very easy to read and modify. */ |
41 | * 6 - 8 extra functions, but keeps each function very easy to read and modify. */ |
42 | 42 | ||
43 | /* TODO: |
43 | /* TODO: |
44 | * Many of these could be unsigned, provided the modules and builtins themselves |
44 | * Many of these could be unsigned, provided the modules and builtins themselves |
45 | * can follow suit. Long term goal. */ |
45 | * can follow suit. Long term goal. */ |
46 | 46 | ||
47 | #include <stdio.h> |
47 | #include <stdio.h> |
48 | #include <stdlib.h> |
48 | #include <stdlib.h> |
49 | #include <string.h> |
49 | #include <string.h> |
50 | #include "errors.h" |
50 | #include "errors.h" |
51 | #include "cmds.h" |
51 | #include "cmds.h" |
52 | #include "module_aliases.h" |
52 | #include "module_aliases.h" |
53 | 53 | ||
54 | extern volatile unsigned int cli_interactive; |
54 | extern volatile unsigned int cli_interactive; |
55 | 55 | ||
56 | int module_is_restricted(int pos) |
- | |
57 | { |
- | |
58 | /* Restriction Levels: |
- | |
59 | * -1 -> Available only in interactive mode |
- | |
60 | * 0 -> Available in any mode |
- | |
61 | * 1 -> Available only in non-interactive mode */ |
- | |
62 | - | ||
63 | module_t *mod = modules; |
- | |
64 | mod += pos; |
- | |
65 | /* We're interactive, and the module is OK to run */ |
- | |
66 | if (cli_interactive && mod->restricted <= 0) |
- | |
67 | return 0; |
- | |
68 | /* We're not interactive, and the module is OK to run */ |
- | |
69 | if (!cli_interactive && mod->restricted >= 0) |
- | |
70 | return 0; |
- | |
71 | - | ||
72 | /* Anything else is just a big fat no :) */ |
- | |
73 | return 1; |
- | |
74 | } |
- | |
75 | - | ||
76 | /* Checks if an entry function matching command exists in modules[], if so |
56 | /* Checks if an entry function matching command exists in modules[], if so |
77 | * its position in the array is returned */ |
57 | * its position in the array is returned */ |
78 | int is_module(const char *command) |
58 | int is_module(const char *command) |
79 | { |
59 | { |
80 | module_t *mod; |
60 | module_t *mod; |
81 | unsigned int i = 0; |
61 | unsigned int i = 0; |
82 | 62 | ||
83 | if (NULL == command) |
63 | if (NULL == command) |
84 | return -2; |
64 | return -2; |
85 | 65 | ||
86 | for (mod = modules; mod->name != NULL; mod++, i++) { |
66 | for (mod = modules; mod->name != NULL; mod++, i++) { |
87 | if (!strcmp(mod->name, command)) |
67 | if (!strcmp(mod->name, command)) |
88 | return i; |
68 | return i; |
89 | } |
69 | } |
90 | 70 | ||
91 | return -1; |
71 | return -1; |
92 | } |
72 | } |
93 | 73 | ||
94 | /* Checks if a module is an alias (sharing an entry point with another |
74 | /* Checks if a module is an alias (sharing an entry point with another |
95 | * module). Returns 1 if so */ |
75 | * module). Returns 1 if so */ |
96 | int is_module_alias(const char *command) |
76 | int is_module_alias(const char *command) |
97 | { |
77 | { |
98 | unsigned int i = 0; |
78 | unsigned int i = 0; |
99 | 79 | ||
100 | if (NULL == command) |
80 | if (NULL == command) |
101 | return -1; |
81 | return -1; |
102 | 82 | ||
103 | for(i=0; mod_aliases[i] != NULL; i+=2) { |
83 | for(i=0; mod_aliases[i] != NULL; i+=2) { |
104 | if (!strcmp(mod_aliases[i], command)) |
84 | if (!strcmp(mod_aliases[i], command)) |
105 | return 1; |
85 | return 1; |
106 | } |
86 | } |
107 | 87 | ||
108 | return 0; |
88 | return 0; |
109 | } |
89 | } |
110 | 90 | ||
111 | /* Returns the name of the module that an alias points to */ |
91 | /* Returns the name of the module that an alias points to */ |
112 | char *alias_for_module(const char *command) |
92 | char *alias_for_module(const char *command) |
113 | { |
93 | { |
114 | unsigned int i = 0; |
94 | unsigned int i = 0; |
115 | 95 | ||
116 | if (NULL == command) |
96 | if (NULL == command) |
117 | return (char *)NULL; |
97 | return (char *)NULL; |
118 | 98 | ||
119 | for(i=0; mod_aliases[i] != NULL; i++) { |
99 | for(i=0; mod_aliases[i] != NULL; i++) { |
120 | if (!strcmp(mod_aliases[i], command)) |
100 | if (!strcmp(mod_aliases[i], command)) |
121 | return (char *)mod_aliases[++i]; |
101 | return (char *)mod_aliases[++i]; |
122 | i++; |
102 | i++; |
123 | } |
103 | } |
124 | 104 | ||
125 | return (char *)NULL; |
105 | return (char *)NULL; |
126 | } |
106 | } |
127 | 107 | ||
128 | 108 | ||
129 | /* Invokes the 'help' entry function for the module at position (int) module, |
109 | /* Invokes the 'help' entry function for the module at position (int) module, |
130 | * which wants an unsigned int to determine brief or extended display. */ |
110 | * which wants an unsigned int to determine brief or extended display. */ |
131 | int help_module(int module, unsigned int extended) |
111 | int help_module(int module, unsigned int extended) |
132 | { |
112 | { |
133 | module_t *mod = modules; |
113 | module_t *mod = modules; |
134 | 114 | ||
135 | mod += module; |
115 | mod += module; |
136 | 116 | ||
137 | if (NULL != mod->help) { |
117 | if (NULL != mod->help) { |
138 | mod->help(extended); |
118 | mod->help(extended); |
139 | return CL_EOK; |
119 | return CL_EOK; |
140 | } else |
120 | } else |
141 | return CL_ENOENT; |
121 | return CL_ENOENT; |
142 | } |
122 | } |
143 | 123 | ||
144 | /* Invokes the module entry point modules[module], passing argv[] as an argument |
124 | /* Invokes the module entry point modules[module], passing argv[] as an argument |
145 | * stack. */ |
125 | * stack. */ |
146 | int run_module(int module, char *argv[]) |
126 | int run_module(int module, char *argv[]) |
147 | { |
127 | { |
148 | module_t *mod = modules; |
128 | module_t *mod = modules; |
149 | 129 | ||
150 | mod += module; |
130 | mod += module; |
151 | 131 | ||
152 | if (NULL != mod->entry) |
132 | if (NULL != mod->entry) |
153 | return ((int)mod->entry(argv)); |
133 | return ((int)mod->entry(argv)); |
154 | 134 | ||
155 | return CL_ENOENT; |
135 | return CL_ENOENT; |
156 | } |
136 | } |
157 | 137 |