blob: df3c1ab76a112601aeb7c6ed8ece591dfc7715b5 [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) {
36 return expr->fn(expr->name, state, expr->argc, expr->argv);
Doug Zongker37bee622009-06-08 17:35:39 -070037}
38
Doug Zongkerd9c9d102009-06-12 12:24:39 -070039char* ConcatFn(const char* name, State* state, int argc, Expr* argv[]) {
40 if (argc == 0) {
41 return strdup("");
Doug Zongker37bee622009-06-08 17:35:39 -070042 }
Doug Zongkerd9c9d102009-06-12 12:24:39 -070043 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 Zongker37bee622009-06-08 17:35:39 -070057
Doug Zongkerd9c9d102009-06-12 12:24:39 -070058 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 Zongker37bee622009-06-08 17:35:39 -070065
Doug Zongkerd9c9d102009-06-12 12:24:39 -070066 done:
67 for (i = 0; i < argc; ++i) {
68 free(strings[i]);
69 }
Kenny Root21854cc2010-02-17 18:31:48 -080070 free(strings);
Doug Zongkerd9c9d102009-06-12 12:24:39 -070071 return result;
Doug Zongker37bee622009-06-08 17:35:39 -070072}
73
Doug Zongkerd9c9d102009-06-12 12:24:39 -070074char* IfElseFn(const char* name, State* state, int argc, Expr* argv[]) {
75 if (argc != 2 && argc != 3) {
Doug Zongkere3da02e2009-06-12 16:13:52 -070076 free(state->errmsg);
77 state->errmsg = strdup("ifelse expects 2 or 3 arguments");
Doug Zongkerd9c9d102009-06-12 12:24:39 -070078 return NULL;
79 }
80 char* cond = Evaluate(state, argv[0]);
81 if (cond == NULL) {
82 return NULL;
83 }
Doug Zongker37bee622009-06-08 17:35:39 -070084
Doug Zongkerd9c9d102009-06-12 12:24:39 -070085 if (BooleanString(cond) == true) {
86 free(cond);
87 return Evaluate(state, argv[1]);
Doug Zongker37bee622009-06-08 17:35:39 -070088 } else {
Doug Zongkerd9c9d102009-06-12 12:24:39 -070089 if (argc == 3) {
90 free(cond);
91 return Evaluate(state, argv[2]);
92 } else {
93 return cond;
94 }
Doug Zongker37bee622009-06-08 17:35:39 -070095 }
Doug Zongker37bee622009-06-08 17:35:39 -070096}
97
Doug Zongkerd9c9d102009-06-12 12:24:39 -070098char* AbortFn(const char* name, State* state, int argc, Expr* argv[]) {
99 char* msg = NULL;
100 if (argc > 0) {
101 msg = Evaluate(state, argv[0]);
Doug Zongker37bee622009-06-08 17:35:39 -0700102 }
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700103 free(state->errmsg);
104 if (msg) {
105 state->errmsg = msg;
106 } else {
107 state->errmsg = strdup("called abort()");
Doug Zongker37bee622009-06-08 17:35:39 -0700108 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700109 return NULL;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700110}
111
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700112char* AssertFn(const char* name, State* state, int argc, Expr* argv[]) {
113 int i;
114 for (i = 0; i < argc; ++i) {
115 char* v = Evaluate(state, argv[i]);
116 if (v == NULL) {
117 return NULL;
118 }
119 int b = BooleanString(v);
120 free(v);
121 if (!b) {
122 int prefix_len;
123 int len = argv[i]->end - argv[i]->start;
124 char* err_src = malloc(len + 20);
125 strcpy(err_src, "assert failed: ");
126 prefix_len = strlen(err_src);
127 memcpy(err_src + prefix_len, state->script + argv[i]->start, len);
128 err_src[prefix_len + len] = '\0';
129 free(state->errmsg);
130 state->errmsg = err_src;
131 return NULL;
132 }
Doug Zongker37bee622009-06-08 17:35:39 -0700133 }
Doug Zongker37bee622009-06-08 17:35:39 -0700134 return strdup("");
Doug Zongker37bee622009-06-08 17:35:39 -0700135}
136
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700137char* SleepFn(const char* name, State* state, int argc, Expr* argv[]) {
138 char* val = Evaluate(state, argv[0]);
139 if (val == NULL) {
140 return NULL;
141 }
142 int v = strtol(val, NULL, 10);
143 sleep(v);
144 return val;
145}
146
147char* StdoutFn(const char* name, State* state, int argc, Expr* argv[]) {
148 int i;
149 for (i = 0; i < argc; ++i) {
150 char* v = Evaluate(state, argv[i]);
151 if (v == NULL) {
152 return NULL;
153 }
154 fputs(v, stdout);
155 free(v);
156 }
157 return strdup("");
158}
159
160char* LogicalAndFn(const char* name, State* state,
161 int argc, Expr* argv[]) {
162 char* left = Evaluate(state, argv[0]);
163 if (left == NULL) return NULL;
164 if (BooleanString(left) == true) {
165 free(left);
166 return Evaluate(state, argv[1]);
167 } else {
168 return left;
169 }
170}
171
172char* LogicalOrFn(const char* name, State* state,
Doug Zongker37bee622009-06-08 17:35:39 -0700173 int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700174 char* left = Evaluate(state, argv[0]);
175 if (left == NULL) return NULL;
176 if (BooleanString(left) == false) {
177 free(left);
178 return Evaluate(state, argv[1]);
179 } else {
180 return left;
181 }
182}
183
184char* LogicalNotFn(const char* name, State* state,
185 int argc, Expr* argv[]) {
186 char* val = Evaluate(state, argv[0]);
187 if (val == NULL) return NULL;
188 bool bv = BooleanString(val);
189 free(val);
190 if (bv) {
191 return strdup("");
192 } else {
193 return strdup("t");
194 }
195}
196
197char* SubstringFn(const char* name, State* state,
198 int argc, Expr* argv[]) {
199 char* needle = Evaluate(state, argv[0]);
200 if (needle == NULL) return NULL;
201 char* haystack = Evaluate(state, argv[1]);
202 if (haystack == NULL) {
203 free(needle);
204 return NULL;
205 }
206
207 char* result = strdup(strstr(haystack, needle) ? "t" : "");
Doug Zongker37bee622009-06-08 17:35:39 -0700208 free(needle);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700209 free(haystack);
210 return result;
Doug Zongker37bee622009-06-08 17:35:39 -0700211}
212
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700213char* EqualityFn(const char* name, State* state, int argc, Expr* argv[]) {
214 char* left = Evaluate(state, argv[0]);
215 if (left == NULL) return NULL;
216 char* right = Evaluate(state, argv[1]);
217 if (right == NULL) {
218 free(left);
219 return NULL;
220 }
221
222 char* result = strdup(strcmp(left, right) == 0 ? "t" : "");
Doug Zongker37bee622009-06-08 17:35:39 -0700223 free(left);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700224 free(right);
225 return result;
Doug Zongker37bee622009-06-08 17:35:39 -0700226}
227
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700228char* InequalityFn(const char* name, State* state, int argc, Expr* argv[]) {
229 char* left = Evaluate(state, argv[0]);
230 if (left == NULL) return NULL;
231 char* right = Evaluate(state, argv[1]);
232 if (right == NULL) {
233 free(left);
234 return NULL;
235 }
236
237 char* result = strdup(strcmp(left, right) != 0 ? "t" : "");
Doug Zongker37bee622009-06-08 17:35:39 -0700238 free(left);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700239 free(right);
240 return result;
Doug Zongker37bee622009-06-08 17:35:39 -0700241}
242
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700243char* SequenceFn(const char* name, State* state, int argc, Expr* argv[]) {
244 char* left = Evaluate(state, argv[0]);
245 if (left == NULL) return NULL;
246 free(left);
247 return Evaluate(state, argv[1]);
Doug Zongker37bee622009-06-08 17:35:39 -0700248}
249
Doug Zongkere3da02e2009-06-12 16:13:52 -0700250char* LessThanIntFn(const char* name, State* state, int argc, Expr* argv[]) {
251 if (argc != 2) {
252 free(state->errmsg);
253 state->errmsg = strdup("less_than_int expects 2 arguments");
254 return NULL;
255 }
256
257 char* left;
258 char* right;
259 if (ReadArgs(state, argv, 2, &left, &right) < 0) return NULL;
260
261 bool result = false;
262 char* end;
263
264 long l_int = strtol(left, &end, 10);
265 if (left[0] == '\0' || *end != '\0') {
266 fprintf(stderr, "[%s] is not an int\n", left);
267 goto done;
268 }
269
270 long r_int = strtol(right, &end, 10);
271 if (right[0] == '\0' || *end != '\0') {
272 fprintf(stderr, "[%s] is not an int\n", right);
273 goto done;
274 }
275
276 result = l_int < r_int;
277
278 done:
279 free(left);
280 free(right);
281 return strdup(result ? "t" : "");
282}
283
284char* GreaterThanIntFn(const char* name, State* state, int argc, Expr* argv[]) {
285 if (argc != 2) {
286 free(state->errmsg);
287 state->errmsg = strdup("greater_than_int expects 2 arguments");
288 return NULL;
289 }
290
291 Expr* temp[2];
292 temp[0] = argv[1];
293 temp[1] = argv[0];
294
295 return LessThanIntFn(name, state, 2, temp);
296}
297
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700298char* Literal(const char* name, State* state, int argc, Expr* argv[]) {
299 return strdup(name);
Doug Zongker37bee622009-06-08 17:35:39 -0700300}
301
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700302Expr* Build(Function fn, YYLTYPE loc, int count, ...) {
303 va_list v;
304 va_start(v, count);
305 Expr* e = malloc(sizeof(Expr));
306 e->fn = fn;
307 e->name = "(operator)";
308 e->argc = count;
309 e->argv = malloc(count * sizeof(Expr*));
310 int i;
311 for (i = 0; i < count; ++i) {
312 e->argv[i] = va_arg(v, Expr*);
313 }
314 va_end(v);
315 e->start = loc.start;
316 e->end = loc.end;
317 return e;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700318}
319
320// -----------------------------------------------------------------
321// the function table
322// -----------------------------------------------------------------
323
Doug Zongker37bee622009-06-08 17:35:39 -0700324static int fn_entries = 0;
325static int fn_size = 0;
326NamedFunction* fn_table = NULL;
327
328void RegisterFunction(const char* name, Function fn) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700329 if (fn_entries >= fn_size) {
330 fn_size = fn_size*2 + 1;
331 fn_table = realloc(fn_table, fn_size * sizeof(NamedFunction));
332 }
333 fn_table[fn_entries].name = name;
334 fn_table[fn_entries].fn = fn;
335 ++fn_entries;
Doug Zongker37bee622009-06-08 17:35:39 -0700336}
337
338static int fn_entry_compare(const void* a, const void* b) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700339 const char* na = ((const NamedFunction*)a)->name;
340 const char* nb = ((const NamedFunction*)b)->name;
341 return strcmp(na, nb);
Doug Zongker37bee622009-06-08 17:35:39 -0700342}
343
344void FinishRegistration() {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700345 qsort(fn_table, fn_entries, sizeof(NamedFunction), fn_entry_compare);
Doug Zongker37bee622009-06-08 17:35:39 -0700346}
347
348Function FindFunction(const char* name) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700349 NamedFunction key;
350 key.name = name;
351 NamedFunction* nf = bsearch(&key, fn_table, fn_entries,
352 sizeof(NamedFunction), fn_entry_compare);
353 if (nf == NULL) {
354 return NULL;
355 }
356 return nf->fn;
Doug Zongker37bee622009-06-08 17:35:39 -0700357}
358
359void RegisterBuiltins() {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700360 RegisterFunction("ifelse", IfElseFn);
361 RegisterFunction("abort", AbortFn);
362 RegisterFunction("assert", AssertFn);
363 RegisterFunction("concat", ConcatFn);
364 RegisterFunction("is_substring", SubstringFn);
365 RegisterFunction("stdout", StdoutFn);
366 RegisterFunction("sleep", SleepFn);
Doug Zongkere3da02e2009-06-12 16:13:52 -0700367
368 RegisterFunction("less_than_int", LessThanIntFn);
369 RegisterFunction("greater_than_int", GreaterThanIntFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700370}
371
372
373// -----------------------------------------------------------------
374// convenience methods for functions
375// -----------------------------------------------------------------
376
377// Evaluate the expressions in argv, giving 'count' char* (the ... is
378// zero or more char** to put them in). If any expression evaluates
379// to NULL, free the rest and return -1. Return 0 on success.
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700380int ReadArgs(State* state, Expr* argv[], int count, ...) {
381 char** args = malloc(count * sizeof(char*));
382 va_list v;
383 va_start(v, count);
384 int i;
385 for (i = 0; i < count; ++i) {
386 args[i] = Evaluate(state, argv[i]);
387 if (args[i] == NULL) {
388 va_end(v);
389 int j;
390 for (j = 0; j < i; ++j) {
391 free(args[j]);
392 }
Kenny Root21854cc2010-02-17 18:31:48 -0800393 free(args);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700394 return -1;
395 }
396 *(va_arg(v, char**)) = args[i];
Doug Zongker9931f7f2009-06-10 14:11:53 -0700397 }
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700398 va_end(v);
Kenny Root21854cc2010-02-17 18:31:48 -0800399 free(args);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700400 return 0;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700401}
402
403// Evaluate the expressions in argv, returning an array of char*
404// results. If any evaluate to NULL, free the rest and return NULL.
405// The caller is responsible for freeing the returned array and the
406// strings it contains.
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700407char** ReadVarArgs(State* state, int argc, Expr* argv[]) {
408 char** args = (char**)malloc(argc * sizeof(char*));
409 int i = 0;
410 for (i = 0; i < argc; ++i) {
411 args[i] = Evaluate(state, argv[i]);
412 if (args[i] == NULL) {
413 int j;
414 for (j = 0; j < i; ++j) {
415 free(args[j]);
416 }
417 free(args);
418 return NULL;
419 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700420 }
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700421 return args;
Doug Zongker37bee622009-06-08 17:35:39 -0700422}
Doug Zongker47cace92009-06-18 10:11:50 -0700423
424// Use printf-style arguments to compose an error message to put into
425// *state. Returns NULL.
426char* ErrorAbort(State* state, char* format, ...) {
427 char* buffer = malloc(4096);
428 va_list v;
429 va_start(v, format);
430 vsnprintf(buffer, 4096, format, v);
431 va_end(v);
432 free(state->errmsg);
433 state->errmsg = buffer;
434 return NULL;
435}