Rev 3386 | 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 | /* Checks if an entry function matching command exists in modules[], if so |
||
57 | * its position in the array is returned */ |
||
58 | int is_module(const char *command) |
||
59 | { |
||
60 | module_t *mod; |
||
61 | unsigned int i = 0; |
||
62 | |||
63 | if (NULL == command) |
||
64 | return -2; |
||
65 | |||
66 | for (mod = modules; mod->name != NULL; mod++, i++) { |
||
67 | if (!strcmp(mod->name, command)) |
||
68 | return i; |
||
69 | } |
||
70 | |||
71 | return -1; |
||
72 | } |
||
73 | |||
74 | /* Checks if a module is an alias (sharing an entry point with another |
||
75 | * module). Returns 1 if so */ |
||
76 | int is_module_alias(const char *command) |
||
77 | { |
||
78 | unsigned int i = 0; |
||
79 | |||
80 | if (NULL == command) |
||
81 | return -1; |
||
82 | |||
83 | for(i=0; mod_aliases[i] != NULL; i+=2) { |
||
84 | if (!strcmp(mod_aliases[i], command)) |
||
85 | return 1; |
||
86 | } |
||
87 | |||
88 | return 0; |
||
89 | } |
||
90 | |||
91 | /* Returns the name of the module that an alias points to */ |
||
92 | char *alias_for_module(const char *command) |
||
93 | { |
||
94 | unsigned int i = 0; |
||
95 | |||
96 | if (NULL == command) |
||
97 | return (char *)NULL; |
||
98 | |||
99 | for(i=0; mod_aliases[i] != NULL; i++) { |
||
100 | if (!strcmp(mod_aliases[i], command)) |
||
101 | return (char *)mod_aliases[++i]; |
||
102 | i++; |
||
103 | } |
||
104 | |||
105 | return (char *)NULL; |
||
106 | } |
||
107 | |||
108 | |||
109 | /* Invokes the 'help' entry function for the module at position (int) module, |
||
110 | * which wants an unsigned int to determine brief or extended display. */ |
||
111 | int help_module(int module, unsigned int extended) |
||
112 | { |
||
113 | module_t *mod = modules; |
||
114 | |||
115 | mod += module; |
||
116 | |||
117 | if (NULL != mod->help) { |
||
118 | mod->help(extended); |
||
119 | return CL_EOK; |
||
120 | } else |
||
121 | return CL_ENOENT; |
||
122 | } |
||
123 | |||
124 | /* Invokes the module entry point modules[module], passing argv[] as an argument |
||
125 | * stack. */ |
||
126 | int run_module(int module, char *argv[]) |
||
127 | { |
||
128 | module_t *mod = modules; |
||
129 | |||
130 | mod += module; |
||
131 | |||
132 | if (NULL != mod->entry) |
||
133 | return ((int)mod->entry(argv)); |
||
134 | |||
135 | return CL_ENOENT; |
||
136 | } |