blob: 7a5b2fbf81b7ce3e4f1c0492262b03eac4726db7 [file] [log] [blame]
Doug Zongker37bee622009-06-08 17:35:39 -07001/*
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 Zongker9931f7f2009-06-10 14:11:53 -070022#include <unistd.h>
Doug Zongker37bee622009-06-08 17:35:39 -070023
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
31int BooleanString(const char* s) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -070032 return s[0] != '\0';
Doug Zongker37bee622009-06-08 17:35:39 -070033}
34
Doug Zongkerd9c9d102009-06-12 12:24:39 -070035char* Evaluate(State* state, Expr* expr) {
Doug Zongker512536a2010-02-17 16:11:44 -080036 Value* v = expr->fn(expr->name, state, expr->argc, expr->argv);
37 if (v == NULL) return NULL;
38 if (v->type != VAL_STRING) {
39 ErrorAbort(state, "expecting string, got value type %d", v->type);
40 FreeValue(v);
41 return NULL;
42 }
43 char* result = v->data;
44 free(v);
45 return result;
46}
47
48Value* EvaluateValue(State* state, Expr* expr) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -070049 return expr->fn(expr->name, state, expr->argc, expr->argv);
Doug Zongker37bee622009-06-08 17:35:39 -070050}
51
Doug Zongker512536a2010-02-17 16:11:44 -080052Value* StringValue(char* str) {
53 Value* v = malloc(sizeof(Value));
54 v->type = VAL_STRING;
55 v->size = strlen(str);
56 v->data = str;
57 return v;
58}
59
60void FreeValue(Value* v) {
61 if (v == NULL) return;
62 free(v->data);
63 free(v);
64}
65
66Value* ConcatFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -070067 if (argc == 0) {
Doug Zongker512536a2010-02-17 16:11:44 -080068 return StringValue(strdup(""));
Doug Zongker37bee622009-06-08 17:35:39 -070069 }
Doug Zongkerd9c9d102009-06-12 12:24:39 -070070 char** strings = malloc(argc * sizeof(char*));
71 int i;
72 for (i = 0; i < argc; ++i) {
73 strings[i] = NULL;
74 }
75 char* result = NULL;
76 int length = 0;
77 for (i = 0; i < argc; ++i) {
78 strings[i] = Evaluate(state, argv[i]);
79 if (strings[i] == NULL) {
80 goto done;
81 }
82 length += strlen(strings[i]);
83 }
Doug Zongker37bee622009-06-08 17:35:39 -070084
Doug Zongkerd9c9d102009-06-12 12:24:39 -070085 result = malloc(length+1);
86 int p = 0;
87 for (i = 0; i < argc; ++i) {
88 strcpy(result+p, strings[i]);
89 p += strlen(strings[i]);
90 }
91 result[p] = '\0';
Doug Zongker37bee622009-06-08 17:35:39 -070092
Doug Zongkerd9c9d102009-06-12 12:24:39 -070093 done:
94 for (i = 0; i < argc; ++i) {
95 free(strings[i]);
96 }
Kenny Root21854cc2010-02-17 18:31:48 -080097 free(strings);
Doug Zongker512536a2010-02-17 16:11:44 -080098 return StringValue(result);
Doug Zongker37bee622009-06-08 17:35:39 -070099}
100
Doug Zongker512536a2010-02-17 16:11:44 -0800101Value* IfElseFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700102 if (argc != 2 && argc != 3) {
Doug Zongkere3da02e2009-06-12 16:13:52 -0700103 free(state->errmsg);
104 state->errmsg = strdup("ifelse expects 2 or 3 arguments");
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700105 return NULL;
106 }
107 char* cond = Evaluate(state, argv[0]);
108 if (cond == NULL) {
109 return NULL;
110 }
Doug Zongker37bee622009-06-08 17:35:39 -0700111
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700112 if (BooleanString(cond) == true) {
113 free(cond);
Doug Zongker512536a2010-02-17 16:11:44 -0800114 return EvaluateValue(state, argv[1]);
Doug Zongker37bee622009-06-08 17:35:39 -0700115 } else {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700116 if (argc == 3) {
117 free(cond);
Doug Zongker512536a2010-02-17 16:11:44 -0800118 return EvaluateValue(state, argv[2]);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700119 } else {
Doug Zongker512536a2010-02-17 16:11:44 -0800120 return StringValue(cond);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700121 }
Doug Zongker37bee622009-06-08 17:35:39 -0700122 }
Doug Zongker37bee622009-06-08 17:35:39 -0700123}
124
Doug Zongker512536a2010-02-17 16:11:44 -0800125Value* AbortFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700126 char* msg = NULL;
127 if (argc > 0) {
128 msg = Evaluate(state, argv[0]);
Doug Zongker37bee622009-06-08 17:35:39 -0700129 }
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700130 free(state->errmsg);
131 if (msg) {
132 state->errmsg = msg;
133 } else {
134 state->errmsg = strdup("called abort()");
Doug Zongker37bee622009-06-08 17:35:39 -0700135 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700136 return NULL;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700137}
138
Doug Zongker512536a2010-02-17 16:11:44 -0800139Value* AssertFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700140 int i;
141 for (i = 0; i < argc; ++i) {
142 char* v = Evaluate(state, argv[i]);
143 if (v == NULL) {
144 return NULL;
145 }
146 int b = BooleanString(v);
147 free(v);
148 if (!b) {
149 int prefix_len;
150 int len = argv[i]->end - argv[i]->start;
151 char* err_src = malloc(len + 20);
152 strcpy(err_src, "assert failed: ");
153 prefix_len = strlen(err_src);
154 memcpy(err_src + prefix_len, state->script + argv[i]->start, len);
155 err_src[prefix_len + len] = '\0';
156 free(state->errmsg);
157 state->errmsg = err_src;
158 return NULL;
159 }
Doug Zongker37bee622009-06-08 17:35:39 -0700160 }
Doug Zongker512536a2010-02-17 16:11:44 -0800161 return StringValue(strdup(""));
Doug Zongker37bee622009-06-08 17:35:39 -0700162}
163
Doug Zongker512536a2010-02-17 16:11:44 -0800164Value* SleepFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700165 char* val = Evaluate(state, argv[0]);
166 if (val == NULL) {
167 return NULL;
168 }
169 int v = strtol(val, NULL, 10);
170 sleep(v);
Doug Zongker512536a2010-02-17 16:11:44 -0800171 return StringValue(val);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700172}
173
Doug Zongker512536a2010-02-17 16:11:44 -0800174Value* StdoutFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700175 int i;
176 for (i = 0; i < argc; ++i) {
177 char* v = Evaluate(state, argv[i]);
178 if (v == NULL) {
179 return NULL;
180 }
181 fputs(v, stdout);
182 free(v);
183 }
Doug Zongker512536a2010-02-17 16:11:44 -0800184 return StringValue(strdup(""));
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700185}
186
Doug Zongker512536a2010-02-17 16:11:44 -0800187Value* LogicalAndFn(const char* name, State* state,
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700188 int argc, Expr* argv[]) {
189 char* left = Evaluate(state, argv[0]);
190 if (left == NULL) return NULL;
191 if (BooleanString(left) == true) {
192 free(left);
Doug Zongker512536a2010-02-17 16:11:44 -0800193 return EvaluateValue(state, argv[1]);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700194 } else {
Doug Zongker512536a2010-02-17 16:11:44 -0800195 return StringValue(left);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700196 }
197}
198
Doug Zongker512536a2010-02-17 16:11:44 -0800199Value* LogicalOrFn(const char* name, State* state,
200 int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700201 char* left = Evaluate(state, argv[0]);
202 if (left == NULL) return NULL;
203 if (BooleanString(left) == false) {
204 free(left);
Doug Zongker512536a2010-02-17 16:11:44 -0800205 return EvaluateValue(state, argv[1]);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700206 } else {
Doug Zongker512536a2010-02-17 16:11:44 -0800207 return StringValue(left);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700208 }
209}
210
Doug Zongker512536a2010-02-17 16:11:44 -0800211Value* LogicalNotFn(const char* name, State* state,
212 int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700213 char* val = Evaluate(state, argv[0]);
214 if (val == NULL) return NULL;
215 bool bv = BooleanString(val);
216 free(val);
Doug Zongker512536a2010-02-17 16:11:44 -0800217 return StringValue(strdup(bv ? "" : "t"));
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700218}
219
Doug Zongker512536a2010-02-17 16:11:44 -0800220Value* SubstringFn(const char* name, State* state,
221 int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700222 char* needle = Evaluate(state, argv[0]);
223 if (needle == NULL) return NULL;
224 char* haystack = Evaluate(state, argv[1]);
225 if (haystack == NULL) {
226 free(needle);
227 return NULL;
228 }
229
230 char* result = strdup(strstr(haystack, needle) ? "t" : "");
Doug Zongker37bee622009-06-08 17:35:39 -0700231 free(needle);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700232 free(haystack);
Doug Zongker512536a2010-02-17 16:11:44 -0800233 return StringValue(result);
Doug Zongker37bee622009-06-08 17:35:39 -0700234}
235
Doug Zongker512536a2010-02-17 16:11:44 -0800236Value* EqualityFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700237 char* left = Evaluate(state, argv[0]);
238 if (left == NULL) return NULL;
239 char* right = Evaluate(state, argv[1]);
240 if (right == NULL) {
241 free(left);
242 return NULL;
243 }
244
245 char* result = strdup(strcmp(left, right) == 0 ? "t" : "");
Doug Zongker37bee622009-06-08 17:35:39 -0700246 free(left);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700247 free(right);
Doug Zongker512536a2010-02-17 16:11:44 -0800248 return StringValue(result);
Doug Zongker37bee622009-06-08 17:35:39 -0700249}
250
Doug Zongker512536a2010-02-17 16:11:44 -0800251Value* InequalityFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700252 char* left = Evaluate(state, argv[0]);
253 if (left == NULL) return NULL;
254 char* right = Evaluate(state, argv[1]);
255 if (right == NULL) {
256 free(left);
257 return NULL;
258 }
259
260 char* result = strdup(strcmp(left, right) != 0 ? "t" : "");
Doug Zongker37bee622009-06-08 17:35:39 -0700261 free(left);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700262 free(right);
Doug Zongker512536a2010-02-17 16:11:44 -0800263 return StringValue(result);
Doug Zongker37bee622009-06-08 17:35:39 -0700264}
265
Doug Zongker512536a2010-02-17 16:11:44 -0800266Value* SequenceFn(const char* name, State* state, int argc, Expr* argv[]) {
267 Value* left = EvaluateValue(state, argv[0]);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700268 if (left == NULL) return NULL;
Doug Zongker512536a2010-02-17 16:11:44 -0800269 FreeValue(left);
270 return EvaluateValue(state, argv[1]);
Doug Zongker37bee622009-06-08 17:35:39 -0700271}
272
Doug Zongker512536a2010-02-17 16:11:44 -0800273Value* LessThanIntFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkere3da02e2009-06-12 16:13:52 -0700274 if (argc != 2) {
275 free(state->errmsg);
276 state->errmsg = strdup("less_than_int expects 2 arguments");
277 return NULL;
278 }
279
280 char* left;
281 char* right;
282 if (ReadArgs(state, argv, 2, &left, &right) < 0) return NULL;
283
284 bool result = false;
285 char* end;
286
287 long l_int = strtol(left, &end, 10);
288 if (left[0] == '\0' || *end != '\0') {
289 fprintf(stderr, "[%s] is not an int\n", left);
290 goto done;
291 }
292
293 long r_int = strtol(right, &end, 10);
294 if (right[0] == '\0' || *end != '\0') {
295 fprintf(stderr, "[%s] is not an int\n", right);
296 goto done;
297 }
298
299 result = l_int < r_int;
300
301 done:
302 free(left);
303 free(right);
Doug Zongker512536a2010-02-17 16:11:44 -0800304 return StringValue(strdup(result ? "t" : ""));
Doug Zongkere3da02e2009-06-12 16:13:52 -0700305}
306
Doug Zongker512536a2010-02-17 16:11:44 -0800307Value* GreaterThanIntFn(const char* name, State* state,
308 int argc, Expr* argv[]) {
Doug Zongkere3da02e2009-06-12 16:13:52 -0700309 if (argc != 2) {
310 free(state->errmsg);
311 state->errmsg = strdup("greater_than_int expects 2 arguments");
312 return NULL;
313 }
314
315 Expr* temp[2];
316 temp[0] = argv[1];
317 temp[1] = argv[0];
318
319 return LessThanIntFn(name, state, 2, temp);
320}
321
Doug Zongker512536a2010-02-17 16:11:44 -0800322Value* Literal(const char* name, State* state, int argc, Expr* argv[]) {
323 return StringValue(strdup(name));
Doug Zongker37bee622009-06-08 17:35:39 -0700324}
325
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700326Expr* Build(Function fn, YYLTYPE loc, int count, ...) {
327 va_list v;
328 va_start(v, count);
329 Expr* e = malloc(sizeof(Expr));
330 e->fn = fn;
331 e->name = "(operator)";
332 e->argc = count;
333 e->argv = malloc(count * sizeof(Expr*));
334 int i;
335 for (i = 0; i < count; ++i) {
336 e->argv[i] = va_arg(v, Expr*);
337 }
338 va_end(v);
339 e->start = loc.start;
340 e->end = loc.end;
341 return e;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700342}
343
344// -----------------------------------------------------------------
345// the function table
346// -----------------------------------------------------------------
347
Doug Zongker37bee622009-06-08 17:35:39 -0700348static int fn_entries = 0;
349static int fn_size = 0;
350NamedFunction* fn_table = NULL;
351
352void RegisterFunction(const char* name, Function fn) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700353 if (fn_entries >= fn_size) {
354 fn_size = fn_size*2 + 1;
355 fn_table = realloc(fn_table, fn_size * sizeof(NamedFunction));
356 }
357 fn_table[fn_entries].name = name;
358 fn_table[fn_entries].fn = fn;
359 ++fn_entries;
Doug Zongker37bee622009-06-08 17:35:39 -0700360}
361
362static int fn_entry_compare(const void* a, const void* b) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700363 const char* na = ((const NamedFunction*)a)->name;
364 const char* nb = ((const NamedFunction*)b)->name;
365 return strcmp(na, nb);
Doug Zongker37bee622009-06-08 17:35:39 -0700366}
367
368void FinishRegistration() {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700369 qsort(fn_table, fn_entries, sizeof(NamedFunction), fn_entry_compare);
Doug Zongker37bee622009-06-08 17:35:39 -0700370}
371
372Function FindFunction(const char* name) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700373 NamedFunction key;
374 key.name = name;
375 NamedFunction* nf = bsearch(&key, fn_table, fn_entries,
376 sizeof(NamedFunction), fn_entry_compare);
377 if (nf == NULL) {
378 return NULL;
379 }
380 return nf->fn;
Doug Zongker37bee622009-06-08 17:35:39 -0700381}
382
383void RegisterBuiltins() {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700384 RegisterFunction("ifelse", IfElseFn);
385 RegisterFunction("abort", AbortFn);
386 RegisterFunction("assert", AssertFn);
387 RegisterFunction("concat", ConcatFn);
388 RegisterFunction("is_substring", SubstringFn);
389 RegisterFunction("stdout", StdoutFn);
390 RegisterFunction("sleep", SleepFn);
Doug Zongkere3da02e2009-06-12 16:13:52 -0700391
392 RegisterFunction("less_than_int", LessThanIntFn);
393 RegisterFunction("greater_than_int", GreaterThanIntFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700394}
395
396
397// -----------------------------------------------------------------
398// convenience methods for functions
399// -----------------------------------------------------------------
400
401// Evaluate the expressions in argv, giving 'count' char* (the ... is
402// zero or more char** to put them in). If any expression evaluates
403// to NULL, free the rest and return -1. Return 0 on success.
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700404int ReadArgs(State* state, Expr* argv[], int count, ...) {
405 char** args = malloc(count * sizeof(char*));
406 va_list v;
407 va_start(v, count);
408 int i;
409 for (i = 0; i < count; ++i) {
410 args[i] = Evaluate(state, argv[i]);
411 if (args[i] == NULL) {
412 va_end(v);
413 int j;
414 for (j = 0; j < i; ++j) {
415 free(args[j]);
416 }
Kenny Root21854cc2010-02-17 18:31:48 -0800417 free(args);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700418 return -1;
419 }
420 *(va_arg(v, char**)) = args[i];
Doug Zongker9931f7f2009-06-10 14:11:53 -0700421 }
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700422 va_end(v);
Kenny Root21854cc2010-02-17 18:31:48 -0800423 free(args);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700424 return 0;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700425}
426
Doug Zongker512536a2010-02-17 16:11:44 -0800427// Evaluate the expressions in argv, giving 'count' Value* (the ... is
428// zero or more Value** to put them in). If any expression evaluates
429// to NULL, free the rest and return -1. Return 0 on success.
430int ReadValueArgs(State* state, Expr* argv[], int count, ...) {
431 Value** args = malloc(count * sizeof(Value*));
432 va_list v;
433 va_start(v, count);
434 int i;
435 for (i = 0; i < count; ++i) {
436 args[i] = EvaluateValue(state, argv[i]);
437 if (args[i] == NULL) {
438 va_end(v);
439 int j;
440 for (j = 0; j < i; ++j) {
441 FreeValue(args[j]);
442 }
443 free(args);
444 return -1;
445 }
446 *(va_arg(v, Value**)) = args[i];
447 }
448 va_end(v);
449 free(args);
450 return 0;
451}
452
Doug Zongker9931f7f2009-06-10 14:11:53 -0700453// Evaluate the expressions in argv, returning an array of char*
454// results. If any evaluate to NULL, free the rest and return NULL.
455// The caller is responsible for freeing the returned array and the
456// strings it contains.
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700457char** ReadVarArgs(State* state, int argc, Expr* argv[]) {
458 char** args = (char**)malloc(argc * sizeof(char*));
459 int i = 0;
460 for (i = 0; i < argc; ++i) {
461 args[i] = Evaluate(state, argv[i]);
462 if (args[i] == NULL) {
463 int j;
464 for (j = 0; j < i; ++j) {
465 free(args[j]);
466 }
467 free(args);
468 return NULL;
469 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700470 }
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700471 return args;
Doug Zongker37bee622009-06-08 17:35:39 -0700472}
Doug Zongker47cace92009-06-18 10:11:50 -0700473
Doug Zongker512536a2010-02-17 16:11:44 -0800474// Evaluate the expressions in argv, returning an array of Value*
475// results. If any evaluate to NULL, free the rest and return NULL.
476// The caller is responsible for freeing the returned array and the
477// Values it contains.
478Value** ReadValueVarArgs(State* state, int argc, Expr* argv[]) {
479 Value** args = (Value**)malloc(argc * sizeof(Value*));
480 int i = 0;
481 for (i = 0; i < argc; ++i) {
482 args[i] = EvaluateValue(state, argv[i]);
483 if (args[i] == NULL) {
484 int j;
485 for (j = 0; j < i; ++j) {
486 FreeValue(args[j]);
487 }
488 free(args);
489 return NULL;
490 }
491 }
492 return args;
493}
494
Doug Zongker47cace92009-06-18 10:11:50 -0700495// Use printf-style arguments to compose an error message to put into
496// *state. Returns NULL.
Doug Zongker512536a2010-02-17 16:11:44 -0800497Value* ErrorAbort(State* state, char* format, ...) {
Doug Zongker47cace92009-06-18 10:11:50 -0700498 char* buffer = malloc(4096);
499 va_list v;
500 va_start(v, format);
501 vsnprintf(buffer, 4096, format, v);
502 va_end(v);
503 free(state->errmsg);
504 state->errmsg = buffer;
505 return NULL;
506}