blob: 75ff82840139b658b2e4d6b0f42fd24db675684a [file] [log] [blame]
The Android Open Source Project23580ca2008-10-21 07:00:00 -07001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <stdlib.h>
18#include <string.h>
19#include <stdio.h>
20#include "symtab.h"
21#include "commands.h"
22
23#if 1
24#define TRACE(...) printf(__VA_ARGS__)
25#else
26#define TRACE(...) /**/
27#endif
28
29typedef enum {
30 CMD_TYPE_UNKNOWN = -1,
31 CMD_TYPE_COMMAND = 0,
32 CMD_TYPE_FUNCTION
33} CommandType;
34
35typedef struct {
36 const char *name;
37 void *cookie;
38 CommandType type;
39 CommandArgumentType argType;
40 CommandHook hook;
41} CommandEntry;
42
43static struct {
44 SymbolTable *symbolTable;
45 bool commandStateInitialized;
46} gCommandState;
47
48int
49commandInit()
50{
51 if (gCommandState.commandStateInitialized) {
52 return -1;
53 }
54 gCommandState.symbolTable = createSymbolTable();
55 if (gCommandState.symbolTable == NULL) {
56 return -1;
57 }
58 gCommandState.commandStateInitialized = true;
59 return 0;
60}
61
62void
63commandCleanup()
64{
65 if (gCommandState.commandStateInitialized) {
66 gCommandState.commandStateInitialized = false;
67 deleteSymbolTable(gCommandState.symbolTable);
68 gCommandState.symbolTable = NULL;
69//xxx need to free the entries and names in the symbol table
70 }
71}
72
73static int
74registerCommandInternal(const char *name, CommandType type,
75 CommandArgumentType argType, CommandHook hook, void *cookie)
76{
77 CommandEntry *entry;
78
79 if (!gCommandState.commandStateInitialized) {
80 return -1;
81 }
82 if (name == NULL || hook == NULL) {
83 return -1;
84 }
85 if (type != CMD_TYPE_COMMAND && type != CMD_TYPE_FUNCTION) {
86 return -1;
87 }
88 if (argType != CMD_ARGS_BOOLEAN && argType != CMD_ARGS_WORDS) {
89 return -1;
90 }
91
92 entry = (CommandEntry *)malloc(sizeof(CommandEntry));
93 if (entry != NULL) {
94 entry->name = strdup(name);
95 if (entry->name != NULL) {
96 int ret;
97
98 entry->cookie = cookie;
99 entry->type = type;
100 entry->argType = argType;
101 entry->hook = hook;
102 ret = addToSymbolTable(gCommandState.symbolTable,
103 entry->name, entry->type, entry);
104 if (ret == 0) {
105 return 0;
106 }
107 }
108 free(entry);
109 }
110
111 return -1;
112}
113
114int
115registerCommand(const char *name,
116 CommandArgumentType argType, CommandHook hook, void *cookie)
117{
118 return registerCommandInternal(name,
119 CMD_TYPE_COMMAND, argType, hook, cookie);
120}
121
122int
123registerFunction(const char *name, FunctionHook hook, void *cookie)
124{
125 return registerCommandInternal(name,
126 CMD_TYPE_FUNCTION, CMD_ARGS_WORDS, (CommandHook)hook, cookie);
127}
128
129Command *
130findCommand(const char *name)
131{
132 return (Command *)findInSymbolTable(gCommandState.symbolTable,
133 name, CMD_TYPE_COMMAND);
134}
135
136Function *
137findFunction(const char *name)
138{
139 return (Function *)findInSymbolTable(gCommandState.symbolTable,
140 name, CMD_TYPE_FUNCTION);
141}
142
143CommandArgumentType
144getCommandArgumentType(Command *cmd)
145{
146 CommandEntry *entry = (CommandEntry *)cmd;
147
148 if (entry != NULL) {
149 return entry->argType;
150 }
151 return CMD_ARGS_UNKNOWN;
152}
153
154static int
155callCommandInternal(CommandEntry *entry, int argc, const char *argv[],
156 PermissionRequestList *permissions)
157{
158 if (entry != NULL && entry->argType == CMD_ARGS_WORDS &&
159 (argc == 0 || (argc > 0 && argv != NULL)))
160 {
161 if (permissions == NULL) {
162 int i;
163 for (i = 0; i < argc; i++) {
164 if (argv[i] == NULL) {
165 goto bail;
166 }
167 }
168 }
169 TRACE("calling command %s\n", entry->name);
170 return entry->hook(entry->name, entry->cookie, argc, argv, permissions);
171//xxx if permissions, make sure the entry has added at least one element.
172 }
173bail:
174 return -1;
175}
176
177static int
178callBooleanCommandInternal(CommandEntry *entry, bool arg,
179 PermissionRequestList *permissions)
180{
181 if (entry != NULL && entry->argType == CMD_ARGS_BOOLEAN) {
182 TRACE("calling boolean command %s\n", entry->name);
183 return entry->hook(entry->name, entry->cookie, arg ? 1 : 0, NULL,
184 permissions);
185//xxx if permissions, make sure the entry has added at least one element.
186 }
187 return -1;
188}
189
190int
191callCommand(Command *cmd, int argc, const char *argv[])
192{
193 return callCommandInternal((CommandEntry *)cmd, argc, argv, NULL);
194}
195
196int
197callBooleanCommand(Command *cmd, bool arg)
198{
199 return callBooleanCommandInternal((CommandEntry *)cmd, arg, NULL);
200}
201
202int
203getCommandPermissions(Command *cmd, int argc, const char *argv[],
204 PermissionRequestList *permissions)
205{
206 if (permissions != NULL) {
207 return callCommandInternal((CommandEntry *)cmd, argc, argv,
208 permissions);
209 }
210 return -1;
211}
212
213int
214getBooleanCommandPermissions(Command *cmd, bool arg,
215 PermissionRequestList *permissions)
216{
217 if (permissions != NULL) {
218 return callBooleanCommandInternal((CommandEntry *)cmd, arg,
219 permissions);
220 }
221 return -1;
222}
223
224int
225callFunctionInternal(CommandEntry *entry, int argc, const char *argv[],
226 char **result, size_t *resultLen, PermissionRequestList *permissions)
227{
228 if (entry != NULL && entry->argType == CMD_ARGS_WORDS &&
229 (argc == 0 || (argc > 0 && argv != NULL)))
230 {
231 if ((permissions == NULL && result != NULL) ||
232 (permissions != NULL && result == NULL))
233 {
234 if (permissions == NULL) {
235 /* This is the actual invocation of the function,
236 * which means that none of the arguments are allowed
237 * to be NULL.
238 */
239 int i;
240 for (i = 0; i < argc; i++) {
241 if (argv[i] == NULL) {
242 goto bail;
243 }
244 }
245 }
246 TRACE("calling function %s\n", entry->name);
247 return ((FunctionHook)entry->hook)(entry->name, entry->cookie,
248 argc, argv, result, resultLen, permissions);
249//xxx if permissions, make sure the entry has added at least one element.
250 }
251 }
252bail:
253 return -1;
254}
255
256int
257callFunction(Function *fn, int argc, const char *argv[],
258 char **result, size_t *resultLen)
259{
260 return callFunctionInternal((CommandEntry *)fn, argc, argv,
261 result, resultLen, NULL);
262}
263
264int
265getFunctionPermissions(Function *fn, int argc, const char *argv[],
266 PermissionRequestList *permissions)
267{
268 if (permissions != NULL) {
269 return callFunctionInternal((CommandEntry *)fn, argc, argv,
270 NULL, NULL, permissions);
271 }
272 return -1;
273}