Rev 3346 | 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 | /* NOTES: |
||
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 |
||
34 | * would be economical. |
||
35 | * |
||
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 |
||
38 | * point. |
||
39 | * |
||
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. */ |
||
42 | |||
43 | /* TODO: |
||
44 | * Many of these could be unsigned, provided the modules and builtins themselves |
||
45 | * can follow suit. Long term goal. */ |
||
46 | |||
47 | #include <stdio.h> |
||
48 | #include <stdlib.h> |
||
49 | #include <string.h> |
||
50 | #include "errors.h" |
||
51 | #include "cmds.h" |
||
52 | #include "module_aliases.h" |
||
53 | |||
54 | extern volatile unsigned int cli_interactive; |
||
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 |
||
77 | * its position in the array is returned */ |
||
78 | int is_module(const char *command) |
||
79 | { |
||
80 | module_t *mod; |
||
81 | unsigned int i = 0; |
||
82 | |||
83 | if (NULL == command) |
||
84 | return -2; |
||
85 | |||
86 | for (mod = modules; mod->name != NULL; mod++, i++) { |
||
87 | if (!strcmp(mod->name, command)) |
||
88 | return i; |
||
89 | } |
||
90 | |||
91 | return -1; |
||
92 | } |
||
93 | |||
94 | /* Checks if a module is an alias (sharing an entry point with another |
||
95 | * module). Returns 1 if so */ |
||
96 | int is_module_alias(const char *command) |
||
97 | { |
||
98 | unsigned int i = 0; |
||
99 | |||
100 | if (NULL == command) |
||
101 | return -1; |
||
102 | |||
103 | for(i=0; mod_aliases[i] != NULL; i+=2) { |
||
104 | if (!strcmp(mod_aliases[i], command)) |
||
105 | return 1; |
||
106 | } |
||
107 | |||
108 | return 0; |
||
109 | } |
||
110 | |||
111 | /* Returns the name of the module that an alias points to */ |
||
112 | char *alias_for_module(const char *command) |
||
113 | { |
||
114 | unsigned int i = 0; |
||
115 | |||
116 | if (NULL == command) |
||
117 | return (char *)NULL; |
||
118 | |||
119 | for(i=0; mod_aliases[i] != NULL; i++) { |
||
120 | if (!strcmp(mod_aliases[i], command)) |
||
121 | return (char *)mod_aliases[++i]; |
||
122 | i++; |
||
123 | } |
||
124 | |||
125 | return (char *)NULL; |
||
126 | } |
||
127 | |||
128 | |||
129 | /* Invokes the 'help' entry function for the module at position (int) module, |
||
130 | * which wants an unsigned int to determine brief or extended display. */ |
||
131 | int help_module(int module, unsigned int extended) |
||
132 | { |
||
133 | module_t *mod = modules; |
||
134 | |||
135 | mod += module; |
||
136 | |||
137 | if (NULL != mod->help) { |
||
138 | mod->help(extended); |
||
139 | return CL_EOK; |
||
140 | } else |
||
141 | return CL_ENOENT; |
||
142 | } |
||
143 | |||
144 | /* Invokes the module entry point modules[module], passing argv[] as an argument |
||
145 | * stack. */ |
||
146 | int run_module(int module, char *argv[]) |
||
147 | { |
||
148 | module_t *mod = modules; |
||
149 | |||
150 | mod += module; |
||
151 | |||
152 | if (NULL != mod->entry) |
||
153 | return ((int)mod->entry(argv)); |
||
154 | |||
155 | return CL_ENOENT; |
||
156 | } |