blob: ecb1bea1a00f5bbd332d942a3f736a5263c9b88d [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
Tianjie Xu16255832016-04-30 11:49:59 -070024#include <string>
25
26#include <android-base/stringprintf.h>
27#include <android-base/strings.h>
28
Doug Zongker37bee622009-06-08 17:35:39 -070029#include "expr.h"
30
31// Functions should:
32//
33// - return a malloc()'d string
34// - if Evaluate() on any argument returns NULL, return NULL.
35
36int BooleanString(const char* s) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -070037 return s[0] != '\0';
Doug Zongker37bee622009-06-08 17:35:39 -070038}
39
Doug Zongkerd9c9d102009-06-12 12:24:39 -070040char* Evaluate(State* state, Expr* expr) {
Doug Zongker512536a2010-02-17 16:11:44 -080041 Value* v = expr->fn(expr->name, state, expr->argc, expr->argv);
42 if (v == NULL) return NULL;
43 if (v->type != VAL_STRING) {
Tianjie Xu16255832016-04-30 11:49:59 -070044 ErrorAbort(state, kArgsParsingFailure, "expecting string, got value type %d", v->type);
Doug Zongker512536a2010-02-17 16:11:44 -080045 FreeValue(v);
46 return NULL;
47 }
48 char* result = v->data;
49 free(v);
50 return result;
51}
52
53Value* EvaluateValue(State* state, Expr* expr) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -070054 return expr->fn(expr->name, state, expr->argc, expr->argv);
Doug Zongker37bee622009-06-08 17:35:39 -070055}
56
Doug Zongker512536a2010-02-17 16:11:44 -080057Value* StringValue(char* str) {
Doug Zongker5b695f32010-02-24 15:03:47 -080058 if (str == NULL) return NULL;
Tao Bao2a5a49d2015-08-20 12:10:46 -070059 Value* v = reinterpret_cast<Value*>(malloc(sizeof(Value)));
Doug Zongker512536a2010-02-17 16:11:44 -080060 v->type = VAL_STRING;
61 v->size = strlen(str);
62 v->data = str;
63 return v;
64}
65
66void FreeValue(Value* v) {
67 if (v == NULL) return;
68 free(v->data);
69 free(v);
70}
71
72Value* ConcatFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -070073 if (argc == 0) {
Doug Zongker512536a2010-02-17 16:11:44 -080074 return StringValue(strdup(""));
Doug Zongker37bee622009-06-08 17:35:39 -070075 }
Tao Bao2a5a49d2015-08-20 12:10:46 -070076 char** strings = reinterpret_cast<char**>(malloc(argc * sizeof(char*)));
Doug Zongkerd9c9d102009-06-12 12:24:39 -070077 int i;
78 for (i = 0; i < argc; ++i) {
79 strings[i] = NULL;
80 }
81 char* result = NULL;
82 int length = 0;
83 for (i = 0; i < argc; ++i) {
84 strings[i] = Evaluate(state, argv[i]);
85 if (strings[i] == NULL) {
86 goto done;
87 }
88 length += strlen(strings[i]);
89 }
Doug Zongker37bee622009-06-08 17:35:39 -070090
Tao Bao2a5a49d2015-08-20 12:10:46 -070091 result = reinterpret_cast<char*>(malloc(length+1));
92 int p;
93 p = 0;
Doug Zongkerd9c9d102009-06-12 12:24:39 -070094 for (i = 0; i < argc; ++i) {
95 strcpy(result+p, strings[i]);
96 p += strlen(strings[i]);
97 }
98 result[p] = '\0';
Doug Zongker37bee622009-06-08 17:35:39 -070099
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700100 done:
101 for (i = 0; i < argc; ++i) {
102 free(strings[i]);
103 }
Kenny Root21854cc2010-02-17 18:31:48 -0800104 free(strings);
Doug Zongker512536a2010-02-17 16:11:44 -0800105 return StringValue(result);
Doug Zongker37bee622009-06-08 17:35:39 -0700106}
107
Doug Zongker512536a2010-02-17 16:11:44 -0800108Value* IfElseFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700109 if (argc != 2 && argc != 3) {
Doug Zongkere3da02e2009-06-12 16:13:52 -0700110 free(state->errmsg);
111 state->errmsg = strdup("ifelse expects 2 or 3 arguments");
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700112 return NULL;
113 }
114 char* cond = Evaluate(state, argv[0]);
115 if (cond == NULL) {
116 return NULL;
117 }
Doug Zongker37bee622009-06-08 17:35:39 -0700118
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700119 if (BooleanString(cond) == true) {
120 free(cond);
Doug Zongker512536a2010-02-17 16:11:44 -0800121 return EvaluateValue(state, argv[1]);
Doug Zongker37bee622009-06-08 17:35:39 -0700122 } else {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700123 if (argc == 3) {
124 free(cond);
Doug Zongker512536a2010-02-17 16:11:44 -0800125 return EvaluateValue(state, argv[2]);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700126 } else {
Doug Zongker512536a2010-02-17 16:11:44 -0800127 return StringValue(cond);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700128 }
Doug Zongker37bee622009-06-08 17:35:39 -0700129 }
Doug Zongker37bee622009-06-08 17:35:39 -0700130}
131
Doug Zongker512536a2010-02-17 16:11:44 -0800132Value* AbortFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700133 char* msg = NULL;
134 if (argc > 0) {
135 msg = Evaluate(state, argv[0]);
Doug Zongker37bee622009-06-08 17:35:39 -0700136 }
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700137 free(state->errmsg);
138 if (msg) {
139 state->errmsg = msg;
140 } else {
141 state->errmsg = strdup("called abort()");
Doug Zongker37bee622009-06-08 17:35:39 -0700142 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700143 return NULL;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700144}
145
Doug Zongker512536a2010-02-17 16:11:44 -0800146Value* AssertFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700147 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 int b = BooleanString(v);
154 free(v);
155 if (!b) {
156 int prefix_len;
157 int len = argv[i]->end - argv[i]->start;
Tao Bao2a5a49d2015-08-20 12:10:46 -0700158 char* err_src = reinterpret_cast<char*>(malloc(len + 20));
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700159 strcpy(err_src, "assert failed: ");
160 prefix_len = strlen(err_src);
161 memcpy(err_src + prefix_len, state->script + argv[i]->start, len);
162 err_src[prefix_len + len] = '\0';
163 free(state->errmsg);
164 state->errmsg = err_src;
165 return NULL;
166 }
Doug Zongker37bee622009-06-08 17:35:39 -0700167 }
Doug Zongker512536a2010-02-17 16:11:44 -0800168 return StringValue(strdup(""));
Doug Zongker37bee622009-06-08 17:35:39 -0700169}
170
Doug Zongker512536a2010-02-17 16:11:44 -0800171Value* SleepFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700172 char* val = Evaluate(state, argv[0]);
173 if (val == NULL) {
174 return NULL;
175 }
176 int v = strtol(val, NULL, 10);
177 sleep(v);
Doug Zongker512536a2010-02-17 16:11:44 -0800178 return StringValue(val);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700179}
180
Doug Zongker512536a2010-02-17 16:11:44 -0800181Value* StdoutFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700182 int i;
183 for (i = 0; i < argc; ++i) {
184 char* v = Evaluate(state, argv[i]);
185 if (v == NULL) {
186 return NULL;
187 }
188 fputs(v, stdout);
189 free(v);
190 }
Doug Zongker512536a2010-02-17 16:11:44 -0800191 return StringValue(strdup(""));
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700192}
193
Doug Zongker512536a2010-02-17 16:11:44 -0800194Value* LogicalAndFn(const char* name, State* state,
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700195 int argc, Expr* argv[]) {
196 char* left = Evaluate(state, argv[0]);
197 if (left == NULL) return NULL;
198 if (BooleanString(left) == true) {
199 free(left);
Doug Zongker512536a2010-02-17 16:11:44 -0800200 return EvaluateValue(state, argv[1]);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700201 } else {
Doug Zongker512536a2010-02-17 16:11:44 -0800202 return StringValue(left);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700203 }
204}
205
Doug Zongker512536a2010-02-17 16:11:44 -0800206Value* LogicalOrFn(const char* name, State* state,
207 int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700208 char* left = Evaluate(state, argv[0]);
209 if (left == NULL) return NULL;
210 if (BooleanString(left) == false) {
211 free(left);
Doug Zongker512536a2010-02-17 16:11:44 -0800212 return EvaluateValue(state, argv[1]);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700213 } else {
Doug Zongker512536a2010-02-17 16:11:44 -0800214 return StringValue(left);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700215 }
216}
217
Doug Zongker512536a2010-02-17 16:11:44 -0800218Value* LogicalNotFn(const char* name, State* state,
219 int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700220 char* val = Evaluate(state, argv[0]);
221 if (val == NULL) return NULL;
222 bool bv = BooleanString(val);
223 free(val);
Doug Zongker512536a2010-02-17 16:11:44 -0800224 return StringValue(strdup(bv ? "" : "t"));
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700225}
226
Doug Zongker512536a2010-02-17 16:11:44 -0800227Value* SubstringFn(const char* name, State* state,
228 int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700229 char* needle = Evaluate(state, argv[0]);
230 if (needle == NULL) return NULL;
231 char* haystack = Evaluate(state, argv[1]);
232 if (haystack == NULL) {
233 free(needle);
234 return NULL;
235 }
236
237 char* result = strdup(strstr(haystack, needle) ? "t" : "");
Doug Zongker37bee622009-06-08 17:35:39 -0700238 free(needle);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700239 free(haystack);
Doug Zongker512536a2010-02-17 16:11:44 -0800240 return StringValue(result);
Doug Zongker37bee622009-06-08 17:35:39 -0700241}
242
Doug Zongker512536a2010-02-17 16:11:44 -0800243Value* EqualityFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700244 char* left = Evaluate(state, argv[0]);
245 if (left == NULL) return NULL;
246 char* right = Evaluate(state, argv[1]);
247 if (right == NULL) {
248 free(left);
249 return NULL;
250 }
251
252 char* result = strdup(strcmp(left, right) == 0 ? "t" : "");
Doug Zongker37bee622009-06-08 17:35:39 -0700253 free(left);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700254 free(right);
Doug Zongker512536a2010-02-17 16:11:44 -0800255 return StringValue(result);
Doug Zongker37bee622009-06-08 17:35:39 -0700256}
257
Doug Zongker512536a2010-02-17 16:11:44 -0800258Value* InequalityFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700259 char* left = Evaluate(state, argv[0]);
260 if (left == NULL) return NULL;
261 char* right = Evaluate(state, argv[1]);
262 if (right == NULL) {
263 free(left);
264 return NULL;
265 }
266
267 char* result = strdup(strcmp(left, right) != 0 ? "t" : "");
Doug Zongker37bee622009-06-08 17:35:39 -0700268 free(left);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700269 free(right);
Doug Zongker512536a2010-02-17 16:11:44 -0800270 return StringValue(result);
Doug Zongker37bee622009-06-08 17:35:39 -0700271}
272
Doug Zongker512536a2010-02-17 16:11:44 -0800273Value* SequenceFn(const char* name, State* state, int argc, Expr* argv[]) {
274 Value* left = EvaluateValue(state, argv[0]);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700275 if (left == NULL) return NULL;
Doug Zongker512536a2010-02-17 16:11:44 -0800276 FreeValue(left);
277 return EvaluateValue(state, argv[1]);
Doug Zongker37bee622009-06-08 17:35:39 -0700278}
279
Doug Zongker512536a2010-02-17 16:11:44 -0800280Value* LessThanIntFn(const char* name, State* state, int argc, Expr* argv[]) {
Doug Zongkere3da02e2009-06-12 16:13:52 -0700281 if (argc != 2) {
282 free(state->errmsg);
283 state->errmsg = strdup("less_than_int expects 2 arguments");
284 return NULL;
285 }
286
287 char* left;
288 char* right;
289 if (ReadArgs(state, argv, 2, &left, &right) < 0) return NULL;
290
291 bool result = false;
292 char* end;
293
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700294 // Parse up to at least long long or 64-bit integers.
295 int64_t l_int = static_cast<int64_t>(strtoll(left, &end, 10));
Doug Zongkere3da02e2009-06-12 16:13:52 -0700296 if (left[0] == '\0' || *end != '\0') {
Doug Zongkere3da02e2009-06-12 16:13:52 -0700297 goto done;
298 }
299
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -0700300 int64_t r_int;
301 r_int = static_cast<int64_t>(strtoll(right, &end, 10));
Doug Zongkere3da02e2009-06-12 16:13:52 -0700302 if (right[0] == '\0' || *end != '\0') {
Doug Zongkere3da02e2009-06-12 16:13:52 -0700303 goto done;
304 }
305
306 result = l_int < r_int;
307
308 done:
309 free(left);
310 free(right);
Doug Zongker512536a2010-02-17 16:11:44 -0800311 return StringValue(strdup(result ? "t" : ""));
Doug Zongkere3da02e2009-06-12 16:13:52 -0700312}
313
Doug Zongker512536a2010-02-17 16:11:44 -0800314Value* GreaterThanIntFn(const char* name, State* state,
315 int argc, Expr* argv[]) {
Doug Zongkere3da02e2009-06-12 16:13:52 -0700316 if (argc != 2) {
317 free(state->errmsg);
318 state->errmsg = strdup("greater_than_int expects 2 arguments");
319 return NULL;
320 }
321
322 Expr* temp[2];
323 temp[0] = argv[1];
324 temp[1] = argv[0];
325
326 return LessThanIntFn(name, state, 2, temp);
327}
328
Doug Zongker512536a2010-02-17 16:11:44 -0800329Value* Literal(const char* name, State* state, int argc, Expr* argv[]) {
330 return StringValue(strdup(name));
Doug Zongker37bee622009-06-08 17:35:39 -0700331}
332
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700333Expr* Build(Function fn, YYLTYPE loc, int count, ...) {
334 va_list v;
335 va_start(v, count);
Tao Bao2a5a49d2015-08-20 12:10:46 -0700336 Expr* e = reinterpret_cast<Expr*>(malloc(sizeof(Expr)));
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700337 e->fn = fn;
338 e->name = "(operator)";
339 e->argc = count;
Tao Bao2a5a49d2015-08-20 12:10:46 -0700340 e->argv = reinterpret_cast<Expr**>(malloc(count * sizeof(Expr*)));
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700341 int i;
342 for (i = 0; i < count; ++i) {
343 e->argv[i] = va_arg(v, Expr*);
344 }
345 va_end(v);
346 e->start = loc.start;
347 e->end = loc.end;
348 return e;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700349}
350
351// -----------------------------------------------------------------
352// the function table
353// -----------------------------------------------------------------
354
Doug Zongker37bee622009-06-08 17:35:39 -0700355static int fn_entries = 0;
356static int fn_size = 0;
357NamedFunction* fn_table = NULL;
358
359void RegisterFunction(const char* name, Function fn) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700360 if (fn_entries >= fn_size) {
361 fn_size = fn_size*2 + 1;
Tao Bao2a5a49d2015-08-20 12:10:46 -0700362 fn_table = reinterpret_cast<NamedFunction*>(realloc(fn_table, fn_size * sizeof(NamedFunction)));
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700363 }
364 fn_table[fn_entries].name = name;
365 fn_table[fn_entries].fn = fn;
366 ++fn_entries;
Doug Zongker37bee622009-06-08 17:35:39 -0700367}
368
369static int fn_entry_compare(const void* a, const void* b) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700370 const char* na = ((const NamedFunction*)a)->name;
371 const char* nb = ((const NamedFunction*)b)->name;
372 return strcmp(na, nb);
Doug Zongker37bee622009-06-08 17:35:39 -0700373}
374
375void FinishRegistration() {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700376 qsort(fn_table, fn_entries, sizeof(NamedFunction), fn_entry_compare);
Doug Zongker37bee622009-06-08 17:35:39 -0700377}
378
379Function FindFunction(const char* name) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700380 NamedFunction key;
381 key.name = name;
Tao Bao2a5a49d2015-08-20 12:10:46 -0700382 NamedFunction* nf = reinterpret_cast<NamedFunction*>(bsearch(&key, fn_table, fn_entries,
383 sizeof(NamedFunction), fn_entry_compare));
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700384 if (nf == NULL) {
385 return NULL;
386 }
387 return nf->fn;
Doug Zongker37bee622009-06-08 17:35:39 -0700388}
389
390void RegisterBuiltins() {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700391 RegisterFunction("ifelse", IfElseFn);
392 RegisterFunction("abort", AbortFn);
393 RegisterFunction("assert", AssertFn);
394 RegisterFunction("concat", ConcatFn);
395 RegisterFunction("is_substring", SubstringFn);
396 RegisterFunction("stdout", StdoutFn);
397 RegisterFunction("sleep", SleepFn);
Doug Zongkere3da02e2009-06-12 16:13:52 -0700398
399 RegisterFunction("less_than_int", LessThanIntFn);
400 RegisterFunction("greater_than_int", GreaterThanIntFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700401}
402
403
404// -----------------------------------------------------------------
405// convenience methods for functions
406// -----------------------------------------------------------------
407
408// Evaluate the expressions in argv, giving 'count' char* (the ... is
409// zero or more char** to put them in). If any expression evaluates
410// to NULL, free the rest and return -1. Return 0 on success.
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700411int ReadArgs(State* state, Expr* argv[], int count, ...) {
Tao Bao2a5a49d2015-08-20 12:10:46 -0700412 char** args = reinterpret_cast<char**>(malloc(count * sizeof(char*)));
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700413 va_list v;
414 va_start(v, count);
415 int i;
416 for (i = 0; i < count; ++i) {
417 args[i] = Evaluate(state, argv[i]);
418 if (args[i] == NULL) {
419 va_end(v);
420 int j;
421 for (j = 0; j < i; ++j) {
422 free(args[j]);
423 }
Kenny Root21854cc2010-02-17 18:31:48 -0800424 free(args);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700425 return -1;
426 }
427 *(va_arg(v, char**)) = args[i];
Doug Zongker9931f7f2009-06-10 14:11:53 -0700428 }
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700429 va_end(v);
Kenny Root21854cc2010-02-17 18:31:48 -0800430 free(args);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700431 return 0;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700432}
433
Doug Zongker512536a2010-02-17 16:11:44 -0800434// Evaluate the expressions in argv, giving 'count' Value* (the ... is
435// zero or more Value** to put them in). If any expression evaluates
436// to NULL, free the rest and return -1. Return 0 on success.
437int ReadValueArgs(State* state, Expr* argv[], int count, ...) {
Tao Bao2a5a49d2015-08-20 12:10:46 -0700438 Value** args = reinterpret_cast<Value**>(malloc(count * sizeof(Value*)));
Doug Zongker512536a2010-02-17 16:11:44 -0800439 va_list v;
440 va_start(v, count);
441 int i;
442 for (i = 0; i < count; ++i) {
443 args[i] = EvaluateValue(state, argv[i]);
444 if (args[i] == NULL) {
445 va_end(v);
446 int j;
447 for (j = 0; j < i; ++j) {
448 FreeValue(args[j]);
449 }
450 free(args);
451 return -1;
452 }
453 *(va_arg(v, Value**)) = args[i];
454 }
455 va_end(v);
456 free(args);
457 return 0;
458}
459
Doug Zongker9931f7f2009-06-10 14:11:53 -0700460// Evaluate the expressions in argv, returning an array of char*
461// results. If any evaluate to NULL, free the rest and return NULL.
462// The caller is responsible for freeing the returned array and the
463// strings it contains.
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700464char** ReadVarArgs(State* state, int argc, Expr* argv[]) {
465 char** args = (char**)malloc(argc * sizeof(char*));
466 int i = 0;
467 for (i = 0; i < argc; ++i) {
468 args[i] = Evaluate(state, argv[i]);
469 if (args[i] == NULL) {
470 int j;
471 for (j = 0; j < i; ++j) {
472 free(args[j]);
473 }
474 free(args);
475 return NULL;
476 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700477 }
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700478 return args;
Doug Zongker37bee622009-06-08 17:35:39 -0700479}
Doug Zongker47cace92009-06-18 10:11:50 -0700480
Doug Zongker512536a2010-02-17 16:11:44 -0800481// Evaluate the expressions in argv, returning an array of Value*
482// results. If any evaluate to NULL, free the rest and return NULL.
483// The caller is responsible for freeing the returned array and the
484// Values it contains.
485Value** ReadValueVarArgs(State* state, int argc, Expr* argv[]) {
486 Value** args = (Value**)malloc(argc * sizeof(Value*));
487 int i = 0;
488 for (i = 0; i < argc; ++i) {
489 args[i] = EvaluateValue(state, argv[i]);
490 if (args[i] == NULL) {
491 int j;
492 for (j = 0; j < i; ++j) {
493 FreeValue(args[j]);
494 }
495 free(args);
496 return NULL;
497 }
498 }
499 return args;
500}
501
Tianjie Xu16255832016-04-30 11:49:59 -0700502static void ErrorAbortV(State* state, const char* format, va_list ap) {
503 std::string buffer;
504 android::base::StringAppendV(&buffer, format, ap);
Doug Zongker47cace92009-06-18 10:11:50 -0700505 free(state->errmsg);
Tianjie Xu16255832016-04-30 11:49:59 -0700506 state->errmsg = strdup(buffer.c_str());
507 return;
508}
509
510// Use printf-style arguments to compose an error message to put into
511// *state. Returns nullptr.
512Value* ErrorAbort(State* state, const char* format, ...) {
513 va_list ap;
514 va_start(ap, format);
515 ErrorAbortV(state, format, ap);
516 va_end(ap);
517 return nullptr;
518}
519
520Value* ErrorAbort(State* state, CauseCode cause_code, const char* format, ...) {
521 va_list ap;
522 va_start(ap, format);
523 ErrorAbortV(state, format, ap);
524 va_end(ap);
525 state->cause_code = cause_code;
526 return nullptr;
Doug Zongker47cace92009-06-18 10:11:50 -0700527}