blob: 72e5100f3a53d7f1224bbaf86c5b61f06f80821a [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 }
70 return result;
Doug Zongker37bee622009-06-08 17:35:39 -070071}
72
Doug Zongkerd9c9d102009-06-12 12:24:39 -070073char* IfElseFn(const char* name, State* state, int argc, Expr* argv[]) {
74 if (argc != 2 && argc != 3) {
Doug Zongkere3da02e2009-06-12 16:13:52 -070075 free(state->errmsg);
76 state->errmsg = strdup("ifelse expects 2 or 3 arguments");
Doug Zongkerd9c9d102009-06-12 12:24:39 -070077 return NULL;
78 }
79 char* cond = Evaluate(state, argv[0]);
80 if (cond == NULL) {
81 return NULL;
82 }
Doug Zongker37bee622009-06-08 17:35:39 -070083
Doug Zongkerd9c9d102009-06-12 12:24:39 -070084 if (BooleanString(cond) == true) {
85 free(cond);
86 return Evaluate(state, argv[1]);
Doug Zongker37bee622009-06-08 17:35:39 -070087 } else {
Doug Zongkerd9c9d102009-06-12 12:24:39 -070088 if (argc == 3) {
89 free(cond);
90 return Evaluate(state, argv[2]);
91 } else {
92 return cond;
93 }
Doug Zongker37bee622009-06-08 17:35:39 -070094 }
Doug Zongker37bee622009-06-08 17:35:39 -070095}
96
Doug Zongkerd9c9d102009-06-12 12:24:39 -070097char* AbortFn(const char* name, State* state, int argc, Expr* argv[]) {
98 char* msg = NULL;
99 if (argc > 0) {
100 msg = Evaluate(state, argv[0]);
Doug Zongker37bee622009-06-08 17:35:39 -0700101 }
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700102 free(state->errmsg);
103 if (msg) {
104 state->errmsg = msg;
105 } else {
106 state->errmsg = strdup("called abort()");
Doug Zongker37bee622009-06-08 17:35:39 -0700107 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700108 return NULL;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700109}
110
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700111char* AssertFn(const char* name, State* state, int argc, Expr* argv[]) {
112 int i;
113 for (i = 0; i < argc; ++i) {
114 char* v = Evaluate(state, argv[i]);
115 if (v == NULL) {
116 return NULL;
117 }
118 int b = BooleanString(v);
119 free(v);
120 if (!b) {
121 int prefix_len;
122 int len = argv[i]->end - argv[i]->start;
123 char* err_src = malloc(len + 20);
124 strcpy(err_src, "assert failed: ");
125 prefix_len = strlen(err_src);
126 memcpy(err_src + prefix_len, state->script + argv[i]->start, len);
127 err_src[prefix_len + len] = '\0';
128 free(state->errmsg);
129 state->errmsg = err_src;
130 return NULL;
131 }
Doug Zongker37bee622009-06-08 17:35:39 -0700132 }
Doug Zongker37bee622009-06-08 17:35:39 -0700133 return strdup("");
Doug Zongker37bee622009-06-08 17:35:39 -0700134}
135
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700136char* SleepFn(const char* name, State* state, int argc, Expr* argv[]) {
137 char* val = Evaluate(state, argv[0]);
138 if (val == NULL) {
139 return NULL;
140 }
141 int v = strtol(val, NULL, 10);
142 sleep(v);
143 return val;
144}
145
146char* StdoutFn(const char* name, State* state, int argc, Expr* argv[]) {
147 int i;
148 for (i = 0; i < argc; ++i) {
149 char* v = Evaluate(state, argv[i]);
150 if (v == NULL) {
151 return NULL;
152 }
153 fputs(v, stdout);
154 free(v);
155 }
156 return strdup("");
157}
158
159char* LogicalAndFn(const char* name, State* state,
160 int argc, Expr* argv[]) {
161 char* left = Evaluate(state, argv[0]);
162 if (left == NULL) return NULL;
163 if (BooleanString(left) == true) {
164 free(left);
165 return Evaluate(state, argv[1]);
166 } else {
167 return left;
168 }
169}
170
171char* LogicalOrFn(const char* name, State* state,
Doug Zongker37bee622009-06-08 17:35:39 -0700172 int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700173 char* left = Evaluate(state, argv[0]);
174 if (left == NULL) return NULL;
175 if (BooleanString(left) == false) {
176 free(left);
177 return Evaluate(state, argv[1]);
178 } else {
179 return left;
180 }
181}
182
183char* LogicalNotFn(const char* name, State* state,
184 int argc, Expr* argv[]) {
185 char* val = Evaluate(state, argv[0]);
186 if (val == NULL) return NULL;
187 bool bv = BooleanString(val);
188 free(val);
189 if (bv) {
190 return strdup("");
191 } else {
192 return strdup("t");
193 }
194}
195
196char* SubstringFn(const char* name, State* state,
197 int argc, Expr* argv[]) {
198 char* needle = Evaluate(state, argv[0]);
199 if (needle == NULL) return NULL;
200 char* haystack = Evaluate(state, argv[1]);
201 if (haystack == NULL) {
202 free(needle);
203 return NULL;
204 }
205
206 char* result = strdup(strstr(haystack, needle) ? "t" : "");
Doug Zongker37bee622009-06-08 17:35:39 -0700207 free(needle);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700208 free(haystack);
209 return result;
Doug Zongker37bee622009-06-08 17:35:39 -0700210}
211
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700212char* EqualityFn(const char* name, State* state, int argc, Expr* argv[]) {
213 char* left = Evaluate(state, argv[0]);
214 if (left == NULL) return NULL;
215 char* right = Evaluate(state, argv[1]);
216 if (right == NULL) {
217 free(left);
218 return NULL;
219 }
220
221 char* result = strdup(strcmp(left, right) == 0 ? "t" : "");
Doug Zongker37bee622009-06-08 17:35:39 -0700222 free(left);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700223 free(right);
224 return result;
Doug Zongker37bee622009-06-08 17:35:39 -0700225}
226
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700227char* InequalityFn(const char* name, State* state, int argc, Expr* argv[]) {
228 char* left = Evaluate(state, argv[0]);
229 if (left == NULL) return NULL;
230 char* right = Evaluate(state, argv[1]);
231 if (right == NULL) {
232 free(left);
233 return NULL;
234 }
235
236 char* result = strdup(strcmp(left, right) != 0 ? "t" : "");
Doug Zongker37bee622009-06-08 17:35:39 -0700237 free(left);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700238 free(right);
239 return result;
Doug Zongker37bee622009-06-08 17:35:39 -0700240}
241
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700242char* SequenceFn(const char* name, State* state, int argc, Expr* argv[]) {
243 char* left = Evaluate(state, argv[0]);
244 if (left == NULL) return NULL;
245 free(left);
246 return Evaluate(state, argv[1]);
Doug Zongker37bee622009-06-08 17:35:39 -0700247}
248
Doug Zongkere3da02e2009-06-12 16:13:52 -0700249char* LessThanIntFn(const char* name, State* state, int argc, Expr* argv[]) {
250 if (argc != 2) {
251 free(state->errmsg);
252 state->errmsg = strdup("less_than_int expects 2 arguments");
253 return NULL;
254 }
255
256 char* left;
257 char* right;
258 if (ReadArgs(state, argv, 2, &left, &right) < 0) return NULL;
259
260 bool result = false;
261 char* end;
262
263 long l_int = strtol(left, &end, 10);
264 if (left[0] == '\0' || *end != '\0') {
265 fprintf(stderr, "[%s] is not an int\n", left);
266 goto done;
267 }
268
269 long r_int = strtol(right, &end, 10);
270 if (right[0] == '\0' || *end != '\0') {
271 fprintf(stderr, "[%s] is not an int\n", right);
272 goto done;
273 }
274
275 result = l_int < r_int;
276
277 done:
278 free(left);
279 free(right);
280 return strdup(result ? "t" : "");
281}
282
283char* GreaterThanIntFn(const char* name, State* state, int argc, Expr* argv[]) {
284 if (argc != 2) {
285 free(state->errmsg);
286 state->errmsg = strdup("greater_than_int expects 2 arguments");
287 return NULL;
288 }
289
290 Expr* temp[2];
291 temp[0] = argv[1];
292 temp[1] = argv[0];
293
294 return LessThanIntFn(name, state, 2, temp);
295}
296
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700297char* Literal(const char* name, State* state, int argc, Expr* argv[]) {
298 return strdup(name);
Doug Zongker37bee622009-06-08 17:35:39 -0700299}
300
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700301Expr* Build(Function fn, YYLTYPE loc, int count, ...) {
302 va_list v;
303 va_start(v, count);
304 Expr* e = malloc(sizeof(Expr));
305 e->fn = fn;
306 e->name = "(operator)";
307 e->argc = count;
308 e->argv = malloc(count * sizeof(Expr*));
309 int i;
310 for (i = 0; i < count; ++i) {
311 e->argv[i] = va_arg(v, Expr*);
312 }
313 va_end(v);
314 e->start = loc.start;
315 e->end = loc.end;
316 return e;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700317}
318
319// -----------------------------------------------------------------
320// the function table
321// -----------------------------------------------------------------
322
Doug Zongker37bee622009-06-08 17:35:39 -0700323static int fn_entries = 0;
324static int fn_size = 0;
325NamedFunction* fn_table = NULL;
326
327void RegisterFunction(const char* name, Function fn) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700328 if (fn_entries >= fn_size) {
329 fn_size = fn_size*2 + 1;
330 fn_table = realloc(fn_table, fn_size * sizeof(NamedFunction));
331 }
332 fn_table[fn_entries].name = name;
333 fn_table[fn_entries].fn = fn;
334 ++fn_entries;
Doug Zongker37bee622009-06-08 17:35:39 -0700335}
336
337static int fn_entry_compare(const void* a, const void* b) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700338 const char* na = ((const NamedFunction*)a)->name;
339 const char* nb = ((const NamedFunction*)b)->name;
340 return strcmp(na, nb);
Doug Zongker37bee622009-06-08 17:35:39 -0700341}
342
343void FinishRegistration() {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700344 qsort(fn_table, fn_entries, sizeof(NamedFunction), fn_entry_compare);
Doug Zongker37bee622009-06-08 17:35:39 -0700345}
346
347Function FindFunction(const char* name) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700348 NamedFunction key;
349 key.name = name;
350 NamedFunction* nf = bsearch(&key, fn_table, fn_entries,
351 sizeof(NamedFunction), fn_entry_compare);
352 if (nf == NULL) {
353 return NULL;
354 }
355 return nf->fn;
Doug Zongker37bee622009-06-08 17:35:39 -0700356}
357
358void RegisterBuiltins() {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700359 RegisterFunction("ifelse", IfElseFn);
360 RegisterFunction("abort", AbortFn);
361 RegisterFunction("assert", AssertFn);
362 RegisterFunction("concat", ConcatFn);
363 RegisterFunction("is_substring", SubstringFn);
364 RegisterFunction("stdout", StdoutFn);
365 RegisterFunction("sleep", SleepFn);
Doug Zongkere3da02e2009-06-12 16:13:52 -0700366
367 RegisterFunction("less_than_int", LessThanIntFn);
368 RegisterFunction("greater_than_int", GreaterThanIntFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700369}
370
371
372// -----------------------------------------------------------------
373// convenience methods for functions
374// -----------------------------------------------------------------
375
376// Evaluate the expressions in argv, giving 'count' char* (the ... is
377// zero or more char** to put them in). If any expression evaluates
378// to NULL, free the rest and return -1. Return 0 on success.
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700379int ReadArgs(State* state, Expr* argv[], int count, ...) {
380 char** args = malloc(count * sizeof(char*));
381 va_list v;
382 va_start(v, count);
383 int i;
384 for (i = 0; i < count; ++i) {
385 args[i] = Evaluate(state, argv[i]);
386 if (args[i] == NULL) {
387 va_end(v);
388 int j;
389 for (j = 0; j < i; ++j) {
390 free(args[j]);
391 }
392 return -1;
393 }
394 *(va_arg(v, char**)) = args[i];
Doug Zongker9931f7f2009-06-10 14:11:53 -0700395 }
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700396 va_end(v);
397 return 0;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700398}
399
400// Evaluate the expressions in argv, returning an array of char*
401// results. If any evaluate to NULL, free the rest and return NULL.
402// The caller is responsible for freeing the returned array and the
403// strings it contains.
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700404char** ReadVarArgs(State* state, int argc, Expr* argv[]) {
405 char** args = (char**)malloc(argc * sizeof(char*));
406 int i = 0;
407 for (i = 0; i < argc; ++i) {
408 args[i] = Evaluate(state, argv[i]);
409 if (args[i] == NULL) {
410 int j;
411 for (j = 0; j < i; ++j) {
412 free(args[j]);
413 }
414 free(args);
415 return NULL;
416 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700417 }
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700418 return args;
Doug Zongker37bee622009-06-08 17:35:39 -0700419}
Doug Zongker47cace92009-06-18 10:11:50 -0700420
421// Use printf-style arguments to compose an error message to put into
422// *state. Returns NULL.
423char* ErrorAbort(State* state, char* format, ...) {
424 char* buffer = malloc(4096);
425 va_list v;
426 va_start(v, format);
427 vsnprintf(buffer, 4096, format, v);
428 va_end(v);
429 free(state->errmsg);
430 state->errmsg = buffer;
431 return NULL;
432}