Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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 <string.h> |
| 18 | #include <stdbool.h> |
| 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <stdarg.h> |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 22 | #include <unistd.h> |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 23 | |
| 24 | #include "expr.h" |
| 25 | |
| 26 | // Functions should: |
| 27 | // |
| 28 | // - return a malloc()'d string |
| 29 | // - if Evaluate() on any argument returns NULL, return NULL. |
| 30 | |
| 31 | int BooleanString(const char* s) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 32 | return s[0] != '\0'; |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 33 | } |
| 34 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 35 | char* Evaluate(State* state, Expr* expr) { |
| 36 | return expr->fn(expr->name, state, expr->argc, expr->argv); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 37 | } |
| 38 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 39 | char* ConcatFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 40 | if (argc == 0) { |
| 41 | return strdup(""); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 42 | } |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 43 | char** strings = malloc(argc * sizeof(char*)); |
| 44 | int i; |
| 45 | for (i = 0; i < argc; ++i) { |
| 46 | strings[i] = NULL; |
| 47 | } |
| 48 | char* result = NULL; |
| 49 | int length = 0; |
| 50 | for (i = 0; i < argc; ++i) { |
| 51 | strings[i] = Evaluate(state, argv[i]); |
| 52 | if (strings[i] == NULL) { |
| 53 | goto done; |
| 54 | } |
| 55 | length += strlen(strings[i]); |
| 56 | } |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 57 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 58 | result = malloc(length+1); |
| 59 | int p = 0; |
| 60 | for (i = 0; i < argc; ++i) { |
| 61 | strcpy(result+p, strings[i]); |
| 62 | p += strlen(strings[i]); |
| 63 | } |
| 64 | result[p] = '\0'; |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 65 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 66 | done: |
| 67 | for (i = 0; i < argc; ++i) { |
| 68 | free(strings[i]); |
| 69 | } |
| 70 | return result; |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 73 | char* IfElseFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 74 | if (argc != 2 && argc != 3) { |
| 75 | return NULL; |
| 76 | } |
| 77 | char* cond = Evaluate(state, argv[0]); |
| 78 | if (cond == NULL) { |
| 79 | return NULL; |
| 80 | } |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 81 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 82 | if (BooleanString(cond) == true) { |
| 83 | free(cond); |
| 84 | return Evaluate(state, argv[1]); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 85 | } else { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 86 | if (argc == 3) { |
| 87 | free(cond); |
| 88 | return Evaluate(state, argv[2]); |
| 89 | } else { |
| 90 | return cond; |
| 91 | } |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 92 | } |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 95 | char* AbortFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 96 | char* msg = NULL; |
| 97 | if (argc > 0) { |
| 98 | msg = Evaluate(state, argv[0]); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 99 | } |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 100 | free(state->errmsg); |
| 101 | if (msg) { |
| 102 | state->errmsg = msg; |
| 103 | } else { |
| 104 | state->errmsg = strdup("called abort()"); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 105 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 106 | return NULL; |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 109 | char* AssertFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 110 | int i; |
| 111 | for (i = 0; i < argc; ++i) { |
| 112 | char* v = Evaluate(state, argv[i]); |
| 113 | if (v == NULL) { |
| 114 | return NULL; |
| 115 | } |
| 116 | int b = BooleanString(v); |
| 117 | free(v); |
| 118 | if (!b) { |
| 119 | int prefix_len; |
| 120 | int len = argv[i]->end - argv[i]->start; |
| 121 | char* err_src = malloc(len + 20); |
| 122 | strcpy(err_src, "assert failed: "); |
| 123 | prefix_len = strlen(err_src); |
| 124 | memcpy(err_src + prefix_len, state->script + argv[i]->start, len); |
| 125 | err_src[prefix_len + len] = '\0'; |
| 126 | free(state->errmsg); |
| 127 | state->errmsg = err_src; |
| 128 | return NULL; |
| 129 | } |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 130 | } |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 131 | return strdup(""); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 134 | char* SleepFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 135 | char* val = Evaluate(state, argv[0]); |
| 136 | if (val == NULL) { |
| 137 | return NULL; |
| 138 | } |
| 139 | int v = strtol(val, NULL, 10); |
| 140 | sleep(v); |
| 141 | return val; |
| 142 | } |
| 143 | |
| 144 | char* StdoutFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 145 | int i; |
| 146 | for (i = 0; i < argc; ++i) { |
| 147 | char* v = Evaluate(state, argv[i]); |
| 148 | if (v == NULL) { |
| 149 | return NULL; |
| 150 | } |
| 151 | fputs(v, stdout); |
| 152 | free(v); |
| 153 | } |
| 154 | return strdup(""); |
| 155 | } |
| 156 | |
| 157 | char* LogicalAndFn(const char* name, State* state, |
| 158 | int argc, Expr* argv[]) { |
| 159 | char* left = Evaluate(state, argv[0]); |
| 160 | if (left == NULL) return NULL; |
| 161 | if (BooleanString(left) == true) { |
| 162 | free(left); |
| 163 | return Evaluate(state, argv[1]); |
| 164 | } else { |
| 165 | return left; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | char* LogicalOrFn(const char* name, State* state, |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 170 | int argc, Expr* argv[]) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 171 | char* left = Evaluate(state, argv[0]); |
| 172 | if (left == NULL) return NULL; |
| 173 | if (BooleanString(left) == false) { |
| 174 | free(left); |
| 175 | return Evaluate(state, argv[1]); |
| 176 | } else { |
| 177 | return left; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | char* LogicalNotFn(const char* name, State* state, |
| 182 | int argc, Expr* argv[]) { |
| 183 | char* val = Evaluate(state, argv[0]); |
| 184 | if (val == NULL) return NULL; |
| 185 | bool bv = BooleanString(val); |
| 186 | free(val); |
| 187 | if (bv) { |
| 188 | return strdup(""); |
| 189 | } else { |
| 190 | return strdup("t"); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | char* SubstringFn(const char* name, State* state, |
| 195 | int argc, Expr* argv[]) { |
| 196 | char* needle = Evaluate(state, argv[0]); |
| 197 | if (needle == NULL) return NULL; |
| 198 | char* haystack = Evaluate(state, argv[1]); |
| 199 | if (haystack == NULL) { |
| 200 | free(needle); |
| 201 | return NULL; |
| 202 | } |
| 203 | |
| 204 | char* result = strdup(strstr(haystack, needle) ? "t" : ""); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 205 | free(needle); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 206 | free(haystack); |
| 207 | return result; |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 210 | char* EqualityFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 211 | char* left = Evaluate(state, argv[0]); |
| 212 | if (left == NULL) return NULL; |
| 213 | char* right = Evaluate(state, argv[1]); |
| 214 | if (right == NULL) { |
| 215 | free(left); |
| 216 | return NULL; |
| 217 | } |
| 218 | |
| 219 | char* result = strdup(strcmp(left, right) == 0 ? "t" : ""); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 220 | free(left); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 221 | free(right); |
| 222 | return result; |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 225 | char* InequalityFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 226 | char* left = Evaluate(state, argv[0]); |
| 227 | if (left == NULL) return NULL; |
| 228 | char* right = Evaluate(state, argv[1]); |
| 229 | if (right == NULL) { |
| 230 | free(left); |
| 231 | return NULL; |
| 232 | } |
| 233 | |
| 234 | char* result = strdup(strcmp(left, right) != 0 ? "t" : ""); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 235 | free(left); |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 236 | free(right); |
| 237 | return result; |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 238 | } |
| 239 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 240 | char* SequenceFn(const char* name, State* state, int argc, Expr* argv[]) { |
| 241 | char* left = Evaluate(state, argv[0]); |
| 242 | if (left == NULL) return NULL; |
| 243 | free(left); |
| 244 | return Evaluate(state, argv[1]); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 245 | } |
| 246 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 247 | char* Literal(const char* name, State* state, int argc, Expr* argv[]) { |
| 248 | return strdup(name); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 251 | Expr* Build(Function fn, YYLTYPE loc, int count, ...) { |
| 252 | va_list v; |
| 253 | va_start(v, count); |
| 254 | Expr* e = malloc(sizeof(Expr)); |
| 255 | e->fn = fn; |
| 256 | e->name = "(operator)"; |
| 257 | e->argc = count; |
| 258 | e->argv = malloc(count * sizeof(Expr*)); |
| 259 | int i; |
| 260 | for (i = 0; i < count; ++i) { |
| 261 | e->argv[i] = va_arg(v, Expr*); |
| 262 | } |
| 263 | va_end(v); |
| 264 | e->start = loc.start; |
| 265 | e->end = loc.end; |
| 266 | return e; |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | // ----------------------------------------------------------------- |
| 270 | // the function table |
| 271 | // ----------------------------------------------------------------- |
| 272 | |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 273 | static int fn_entries = 0; |
| 274 | static int fn_size = 0; |
| 275 | NamedFunction* fn_table = NULL; |
| 276 | |
| 277 | void RegisterFunction(const char* name, Function fn) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 278 | if (fn_entries >= fn_size) { |
| 279 | fn_size = fn_size*2 + 1; |
| 280 | fn_table = realloc(fn_table, fn_size * sizeof(NamedFunction)); |
| 281 | } |
| 282 | fn_table[fn_entries].name = name; |
| 283 | fn_table[fn_entries].fn = fn; |
| 284 | ++fn_entries; |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | static int fn_entry_compare(const void* a, const void* b) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 288 | const char* na = ((const NamedFunction*)a)->name; |
| 289 | const char* nb = ((const NamedFunction*)b)->name; |
| 290 | return strcmp(na, nb); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | void FinishRegistration() { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 294 | qsort(fn_table, fn_entries, sizeof(NamedFunction), fn_entry_compare); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | Function FindFunction(const char* name) { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 298 | NamedFunction key; |
| 299 | key.name = name; |
| 300 | NamedFunction* nf = bsearch(&key, fn_table, fn_entries, |
| 301 | sizeof(NamedFunction), fn_entry_compare); |
| 302 | if (nf == NULL) { |
| 303 | return NULL; |
| 304 | } |
| 305 | return nf->fn; |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | void RegisterBuiltins() { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 309 | RegisterFunction("ifelse", IfElseFn); |
| 310 | RegisterFunction("abort", AbortFn); |
| 311 | RegisterFunction("assert", AssertFn); |
| 312 | RegisterFunction("concat", ConcatFn); |
| 313 | RegisterFunction("is_substring", SubstringFn); |
| 314 | RegisterFunction("stdout", StdoutFn); |
| 315 | RegisterFunction("sleep", SleepFn); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | |
| 319 | // ----------------------------------------------------------------- |
| 320 | // convenience methods for functions |
| 321 | // ----------------------------------------------------------------- |
| 322 | |
| 323 | // Evaluate the expressions in argv, giving 'count' char* (the ... is |
| 324 | // zero or more char** to put them in). If any expression evaluates |
| 325 | // to NULL, free the rest and return -1. Return 0 on success. |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 326 | int ReadArgs(State* state, Expr* argv[], int count, ...) { |
| 327 | char** args = malloc(count * sizeof(char*)); |
| 328 | va_list v; |
| 329 | va_start(v, count); |
| 330 | int i; |
| 331 | for (i = 0; i < count; ++i) { |
| 332 | args[i] = Evaluate(state, argv[i]); |
| 333 | if (args[i] == NULL) { |
| 334 | va_end(v); |
| 335 | int j; |
| 336 | for (j = 0; j < i; ++j) { |
| 337 | free(args[j]); |
| 338 | } |
| 339 | return -1; |
| 340 | } |
| 341 | *(va_arg(v, char**)) = args[i]; |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 342 | } |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 343 | va_end(v); |
| 344 | return 0; |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | // Evaluate the expressions in argv, returning an array of char* |
| 348 | // results. If any evaluate to NULL, free the rest and return NULL. |
| 349 | // The caller is responsible for freeing the returned array and the |
| 350 | // strings it contains. |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 351 | char** ReadVarArgs(State* state, int argc, Expr* argv[]) { |
| 352 | char** args = (char**)malloc(argc * sizeof(char*)); |
| 353 | int i = 0; |
| 354 | for (i = 0; i < argc; ++i) { |
| 355 | args[i] = Evaluate(state, argv[i]); |
| 356 | if (args[i] == NULL) { |
| 357 | int j; |
| 358 | for (j = 0; j < i; ++j) { |
| 359 | free(args[j]); |
| 360 | } |
| 361 | free(args); |
| 362 | return NULL; |
| 363 | } |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 364 | } |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 365 | return args; |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 366 | } |