The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 29 | typedef enum { |
| 30 | CMD_TYPE_UNKNOWN = -1, |
| 31 | CMD_TYPE_COMMAND = 0, |
| 32 | CMD_TYPE_FUNCTION |
| 33 | } CommandType; |
| 34 | |
| 35 | typedef struct { |
| 36 | const char *name; |
| 37 | void *cookie; |
| 38 | CommandType type; |
| 39 | CommandArgumentType argType; |
| 40 | CommandHook hook; |
| 41 | } CommandEntry; |
| 42 | |
| 43 | static struct { |
| 44 | SymbolTable *symbolTable; |
| 45 | bool commandStateInitialized; |
| 46 | } gCommandState; |
| 47 | |
| 48 | int |
| 49 | commandInit() |
| 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 | |
| 62 | void |
| 63 | commandCleanup() |
| 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 | |
| 73 | static int |
| 74 | registerCommandInternal(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 | |
| 114 | int |
| 115 | registerCommand(const char *name, |
| 116 | CommandArgumentType argType, CommandHook hook, void *cookie) |
| 117 | { |
| 118 | return registerCommandInternal(name, |
| 119 | CMD_TYPE_COMMAND, argType, hook, cookie); |
| 120 | } |
| 121 | |
| 122 | int |
| 123 | registerFunction(const char *name, FunctionHook hook, void *cookie) |
| 124 | { |
| 125 | return registerCommandInternal(name, |
| 126 | CMD_TYPE_FUNCTION, CMD_ARGS_WORDS, (CommandHook)hook, cookie); |
| 127 | } |
| 128 | |
| 129 | Command * |
| 130 | findCommand(const char *name) |
| 131 | { |
| 132 | return (Command *)findInSymbolTable(gCommandState.symbolTable, |
| 133 | name, CMD_TYPE_COMMAND); |
| 134 | } |
| 135 | |
| 136 | Function * |
| 137 | findFunction(const char *name) |
| 138 | { |
| 139 | return (Function *)findInSymbolTable(gCommandState.symbolTable, |
| 140 | name, CMD_TYPE_FUNCTION); |
| 141 | } |
| 142 | |
| 143 | CommandArgumentType |
| 144 | getCommandArgumentType(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 | |
| 154 | static int |
Doug Zongker | f28c916 | 2009-06-02 15:30:11 -0700 | [diff] [blame^] | 155 | callCommandInternal(CommandEntry *entry, int argc, const char *argv[]) |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 156 | { |
| 157 | if (entry != NULL && entry->argType == CMD_ARGS_WORDS && |
| 158 | (argc == 0 || (argc > 0 && argv != NULL))) |
| 159 | { |
Doug Zongker | f28c916 | 2009-06-02 15:30:11 -0700 | [diff] [blame^] | 160 | int i; |
| 161 | for (i = 0; i < argc; i++) { |
| 162 | if (argv[i] == NULL) { |
| 163 | goto bail; |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | TRACE("calling command %s\n", entry->name); |
Doug Zongker | f28c916 | 2009-06-02 15:30:11 -0700 | [diff] [blame^] | 167 | return entry->hook(entry->name, entry->cookie, argc, argv); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 168 | } |
| 169 | bail: |
| 170 | return -1; |
| 171 | } |
| 172 | |
| 173 | static int |
Doug Zongker | f28c916 | 2009-06-02 15:30:11 -0700 | [diff] [blame^] | 174 | callBooleanCommandInternal(CommandEntry *entry, bool arg) |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 175 | { |
| 176 | if (entry != NULL && entry->argType == CMD_ARGS_BOOLEAN) { |
| 177 | TRACE("calling boolean command %s\n", entry->name); |
Doug Zongker | f28c916 | 2009-06-02 15:30:11 -0700 | [diff] [blame^] | 178 | return entry->hook(entry->name, entry->cookie, arg ? 1 : 0, NULL); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 179 | } |
| 180 | return -1; |
| 181 | } |
| 182 | |
| 183 | int |
| 184 | callCommand(Command *cmd, int argc, const char *argv[]) |
| 185 | { |
Doug Zongker | f28c916 | 2009-06-02 15:30:11 -0700 | [diff] [blame^] | 186 | return callCommandInternal((CommandEntry *)cmd, argc, argv); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | int |
| 190 | callBooleanCommand(Command *cmd, bool arg) |
| 191 | { |
Doug Zongker | f28c916 | 2009-06-02 15:30:11 -0700 | [diff] [blame^] | 192 | return callBooleanCommandInternal((CommandEntry *)cmd, arg); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | int |
| 196 | callFunctionInternal(CommandEntry *entry, int argc, const char *argv[], |
Doug Zongker | f28c916 | 2009-06-02 15:30:11 -0700 | [diff] [blame^] | 197 | char **result, size_t *resultLen) |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 198 | { |
| 199 | if (entry != NULL && entry->argType == CMD_ARGS_WORDS && |
| 200 | (argc == 0 || (argc > 0 && argv != NULL))) |
| 201 | { |
Doug Zongker | f28c916 | 2009-06-02 15:30:11 -0700 | [diff] [blame^] | 202 | if (result != NULL) |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 203 | { |
Doug Zongker | f28c916 | 2009-06-02 15:30:11 -0700 | [diff] [blame^] | 204 | /* 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 Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | TRACE("calling function %s\n", entry->name); |
| 215 | return ((FunctionHook)entry->hook)(entry->name, entry->cookie, |
Doug Zongker | f28c916 | 2009-06-02 15:30:11 -0700 | [diff] [blame^] | 216 | argc, argv, result, resultLen); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | bail: |
| 220 | return -1; |
| 221 | } |
| 222 | |
| 223 | int |
| 224 | callFunction(Function *fn, int argc, const char *argv[], |
| 225 | char **result, size_t *resultLen) |
| 226 | { |
| 227 | return callFunctionInternal((CommandEntry *)fn, argc, argv, |
Doug Zongker | f28c916 | 2009-06-02 15:30:11 -0700 | [diff] [blame^] | 228 | result, resultLen); |
The Android Open Source Project | c24a8e6 | 2009-03-03 19:28:42 -0800 | [diff] [blame] | 229 | } |