blob: 78121adf1150f566e0cde5eff007410f3b5ffdcf [file] [log] [blame]
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -08001/*
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
Doug Zongkerf28c9162009-06-02 15:30:11 -0700155callCommandInternal(CommandEntry *entry, int argc, const char *argv[])
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800156{
157 if (entry != NULL && entry->argType == CMD_ARGS_WORDS &&
158 (argc == 0 || (argc > 0 && argv != NULL)))
159 {
Doug Zongkerf28c9162009-06-02 15:30:11 -0700160 int i;
161 for (i = 0; i < argc; i++) {
162 if (argv[i] == NULL) {
163 goto bail;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800164 }
165 }
166 TRACE("calling command %s\n", entry->name);
Doug Zongkerf28c9162009-06-02 15:30:11 -0700167 return entry->hook(entry->name, entry->cookie, argc, argv);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800168 }
169bail:
170 return -1;
171}
172
173static int
Doug Zongkerf28c9162009-06-02 15:30:11 -0700174callBooleanCommandInternal(CommandEntry *entry, bool arg)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800175{
176 if (entry != NULL && entry->argType == CMD_ARGS_BOOLEAN) {
177 TRACE("calling boolean command %s\n", entry->name);
Doug Zongkerf28c9162009-06-02 15:30:11 -0700178 return entry->hook(entry->name, entry->cookie, arg ? 1 : 0, NULL);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800179 }
180 return -1;
181}
182
183int
184callCommand(Command *cmd, int argc, const char *argv[])
185{
Doug Zongkerf28c9162009-06-02 15:30:11 -0700186 return callCommandInternal((CommandEntry *)cmd, argc, argv);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800187}
188
189int
190callBooleanCommand(Command *cmd, bool arg)
191{
Doug Zongkerf28c9162009-06-02 15:30:11 -0700192 return callBooleanCommandInternal((CommandEntry *)cmd, arg);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800193}
194
195int
196callFunctionInternal(CommandEntry *entry, int argc, const char *argv[],
Doug Zongkerf28c9162009-06-02 15:30:11 -0700197 char **result, size_t *resultLen)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800198{
199 if (entry != NULL && entry->argType == CMD_ARGS_WORDS &&
200 (argc == 0 || (argc > 0 && argv != NULL)))
201 {
Doug Zongkerf28c9162009-06-02 15:30:11 -0700202 if (result != NULL)
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800203 {
Doug Zongkerf28c9162009-06-02 15:30:11 -0700204 /* This is the actual invocation of the function,
205 * which means that none of the arguments are allowed
206 * to be NULL.
207 */
208 int i;
209 for (i = 0; i < argc; i++) {
210 if (argv[i] == NULL) {
211 goto bail;
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800212 }
213 }
214 TRACE("calling function %s\n", entry->name);
215 return ((FunctionHook)entry->hook)(entry->name, entry->cookie,
Doug Zongkerf28c9162009-06-02 15:30:11 -0700216 argc, argv, result, resultLen);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800217 }
218 }
219bail:
220 return -1;
221}
222
223int
224callFunction(Function *fn, int argc, const char *argv[],
225 char **result, size_t *resultLen)
226{
227 return callFunctionInternal((CommandEntry *)fn, argc, argv,
Doug Zongkerf28c9162009-06-02 15:30:11 -0700228 result, resultLen);
The Android Open Source Projectc24a8e62009-03-03 19:28:42 -0800229}