blob: 406c67ea610d13718ffe00ccd4f65e2cde946612 [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) {
75 return NULL;
76 }
77 char* cond = Evaluate(state, argv[0]);
78 if (cond == NULL) {
79 return NULL;
80 }
Doug Zongker37bee622009-06-08 17:35:39 -070081
Doug Zongkerd9c9d102009-06-12 12:24:39 -070082 if (BooleanString(cond) == true) {
83 free(cond);
84 return Evaluate(state, argv[1]);
Doug Zongker37bee622009-06-08 17:35:39 -070085 } else {
Doug Zongkerd9c9d102009-06-12 12:24:39 -070086 if (argc == 3) {
87 free(cond);
88 return Evaluate(state, argv[2]);
89 } else {
90 return cond;
91 }
Doug Zongker37bee622009-06-08 17:35:39 -070092 }
Doug Zongker37bee622009-06-08 17:35:39 -070093}
94
Doug Zongkerd9c9d102009-06-12 12:24:39 -070095char* 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 Zongker37bee622009-06-08 17:35:39 -070099 }
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700100 free(state->errmsg);
101 if (msg) {
102 state->errmsg = msg;
103 } else {
104 state->errmsg = strdup("called abort()");
Doug Zongker37bee622009-06-08 17:35:39 -0700105 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700106 return NULL;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700107}
108
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700109char* 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 Zongker37bee622009-06-08 17:35:39 -0700130 }
Doug Zongker37bee622009-06-08 17:35:39 -0700131 return strdup("");
Doug Zongker37bee622009-06-08 17:35:39 -0700132}
133
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700134char* 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
144char* 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
157char* 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
169char* LogicalOrFn(const char* name, State* state,
Doug Zongker37bee622009-06-08 17:35:39 -0700170 int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700171 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
181char* 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
194char* 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 Zongker37bee622009-06-08 17:35:39 -0700205 free(needle);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700206 free(haystack);
207 return result;
Doug Zongker37bee622009-06-08 17:35:39 -0700208}
209
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700210char* 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 Zongker37bee622009-06-08 17:35:39 -0700220 free(left);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700221 free(right);
222 return result;
Doug Zongker37bee622009-06-08 17:35:39 -0700223}
224
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700225char* 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 Zongker37bee622009-06-08 17:35:39 -0700235 free(left);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700236 free(right);
237 return result;
Doug Zongker37bee622009-06-08 17:35:39 -0700238}
239
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700240char* 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 Zongker37bee622009-06-08 17:35:39 -0700245}
246
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700247char* Literal(const char* name, State* state, int argc, Expr* argv[]) {
248 return strdup(name);
Doug Zongker37bee622009-06-08 17:35:39 -0700249}
250
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700251Expr* 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 Zongker9931f7f2009-06-10 14:11:53 -0700267}
268
269// -----------------------------------------------------------------
270// the function table
271// -----------------------------------------------------------------
272
Doug Zongker37bee622009-06-08 17:35:39 -0700273static int fn_entries = 0;
274static int fn_size = 0;
275NamedFunction* fn_table = NULL;
276
277void RegisterFunction(const char* name, Function fn) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700278 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 Zongker37bee622009-06-08 17:35:39 -0700285}
286
287static int fn_entry_compare(const void* a, const void* b) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700288 const char* na = ((const NamedFunction*)a)->name;
289 const char* nb = ((const NamedFunction*)b)->name;
290 return strcmp(na, nb);
Doug Zongker37bee622009-06-08 17:35:39 -0700291}
292
293void FinishRegistration() {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700294 qsort(fn_table, fn_entries, sizeof(NamedFunction), fn_entry_compare);
Doug Zongker37bee622009-06-08 17:35:39 -0700295}
296
297Function FindFunction(const char* name) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700298 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 Zongker37bee622009-06-08 17:35:39 -0700306}
307
308void RegisterBuiltins() {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700309 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 Zongker9931f7f2009-06-10 14:11:53 -0700316}
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 Zongkerd9c9d102009-06-12 12:24:39 -0700326int 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 Zongker9931f7f2009-06-10 14:11:53 -0700342 }
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700343 va_end(v);
344 return 0;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700345}
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 Zongkerd9c9d102009-06-12 12:24:39 -0700351char** 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 Zongker9931f7f2009-06-10 14:11:53 -0700364 }
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700365 return args;
Doug Zongker37bee622009-06-08 17:35:39 -0700366}