blob: cc14fbe93188e0a117420a722033b789db49f3fd [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
294 long l_int = strtol(left, &end, 10);
295 if (left[0] == '\0' || *end != '\0') {
Doug Zongkere3da02e2009-06-12 16:13:52 -0700296 goto done;
297 }
298
Tao Bao2a5a49d2015-08-20 12:10:46 -0700299 long r_int;
300 r_int = strtol(right, &end, 10);
Doug Zongkere3da02e2009-06-12 16:13:52 -0700301 if (right[0] == '\0' || *end != '\0') {
Doug Zongkere3da02e2009-06-12 16:13:52 -0700302 goto done;
303 }
304
305 result = l_int < r_int;
306
307 done:
308 free(left);
309 free(right);
Doug Zongker512536a2010-02-17 16:11:44 -0800310 return StringValue(strdup(result ? "t" : ""));
Doug Zongkere3da02e2009-06-12 16:13:52 -0700311}
312
Doug Zongker512536a2010-02-17 16:11:44 -0800313Value* GreaterThanIntFn(const char* name, State* state,
314 int argc, Expr* argv[]) {
Doug Zongkere3da02e2009-06-12 16:13:52 -0700315 if (argc != 2) {
316 free(state->errmsg);
317 state->errmsg = strdup("greater_than_int expects 2 arguments");
318 return NULL;
319 }
320
321 Expr* temp[2];
322 temp[0] = argv[1];
323 temp[1] = argv[0];
324
325 return LessThanIntFn(name, state, 2, temp);
326}
327
Doug Zongker512536a2010-02-17 16:11:44 -0800328Value* Literal(const char* name, State* state, int argc, Expr* argv[]) {
329 return StringValue(strdup(name));
Doug Zongker37bee622009-06-08 17:35:39 -0700330}
331
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700332Expr* Build(Function fn, YYLTYPE loc, int count, ...) {
333 va_list v;
334 va_start(v, count);
Tao Bao2a5a49d2015-08-20 12:10:46 -0700335 Expr* e = reinterpret_cast<Expr*>(malloc(sizeof(Expr)));
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700336 e->fn = fn;
337 e->name = "(operator)";
338 e->argc = count;
Tao Bao2a5a49d2015-08-20 12:10:46 -0700339 e->argv = reinterpret_cast<Expr**>(malloc(count * sizeof(Expr*)));
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700340 int i;
341 for (i = 0; i < count; ++i) {
342 e->argv[i] = va_arg(v, Expr*);
343 }
344 va_end(v);
345 e->start = loc.start;
346 e->end = loc.end;
347 return e;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700348}
349
350// -----------------------------------------------------------------
351// the function table
352// -----------------------------------------------------------------
353
Doug Zongker37bee622009-06-08 17:35:39 -0700354static int fn_entries = 0;
355static int fn_size = 0;
356NamedFunction* fn_table = NULL;
357
358void RegisterFunction(const char* name, Function fn) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700359 if (fn_entries >= fn_size) {
360 fn_size = fn_size*2 + 1;
Tao Bao2a5a49d2015-08-20 12:10:46 -0700361 fn_table = reinterpret_cast<NamedFunction*>(realloc(fn_table, fn_size * sizeof(NamedFunction)));
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700362 }
363 fn_table[fn_entries].name = name;
364 fn_table[fn_entries].fn = fn;
365 ++fn_entries;
Doug Zongker37bee622009-06-08 17:35:39 -0700366}
367
368static int fn_entry_compare(const void* a, const void* b) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700369 const char* na = ((const NamedFunction*)a)->name;
370 const char* nb = ((const NamedFunction*)b)->name;
371 return strcmp(na, nb);
Doug Zongker37bee622009-06-08 17:35:39 -0700372}
373
374void FinishRegistration() {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700375 qsort(fn_table, fn_entries, sizeof(NamedFunction), fn_entry_compare);
Doug Zongker37bee622009-06-08 17:35:39 -0700376}
377
378Function FindFunction(const char* name) {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700379 NamedFunction key;
380 key.name = name;
Tao Bao2a5a49d2015-08-20 12:10:46 -0700381 NamedFunction* nf = reinterpret_cast<NamedFunction*>(bsearch(&key, fn_table, fn_entries,
382 sizeof(NamedFunction), fn_entry_compare));
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700383 if (nf == NULL) {
384 return NULL;
385 }
386 return nf->fn;
Doug Zongker37bee622009-06-08 17:35:39 -0700387}
388
389void RegisterBuiltins() {
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700390 RegisterFunction("ifelse", IfElseFn);
391 RegisterFunction("abort", AbortFn);
392 RegisterFunction("assert", AssertFn);
393 RegisterFunction("concat", ConcatFn);
394 RegisterFunction("is_substring", SubstringFn);
395 RegisterFunction("stdout", StdoutFn);
396 RegisterFunction("sleep", SleepFn);
Doug Zongkere3da02e2009-06-12 16:13:52 -0700397
398 RegisterFunction("less_than_int", LessThanIntFn);
399 RegisterFunction("greater_than_int", GreaterThanIntFn);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700400}
401
402
403// -----------------------------------------------------------------
404// convenience methods for functions
405// -----------------------------------------------------------------
406
407// Evaluate the expressions in argv, giving 'count' char* (the ... is
408// zero or more char** to put them in). If any expression evaluates
409// to NULL, free the rest and return -1. Return 0 on success.
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700410int ReadArgs(State* state, Expr* argv[], int count, ...) {
Tao Bao2a5a49d2015-08-20 12:10:46 -0700411 char** args = reinterpret_cast<char**>(malloc(count * sizeof(char*)));
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700412 va_list v;
413 va_start(v, count);
414 int i;
415 for (i = 0; i < count; ++i) {
416 args[i] = Evaluate(state, argv[i]);
417 if (args[i] == NULL) {
418 va_end(v);
419 int j;
420 for (j = 0; j < i; ++j) {
421 free(args[j]);
422 }
Kenny Root21854cc2010-02-17 18:31:48 -0800423 free(args);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700424 return -1;
425 }
426 *(va_arg(v, char**)) = args[i];
Doug Zongker9931f7f2009-06-10 14:11:53 -0700427 }
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700428 va_end(v);
Kenny Root21854cc2010-02-17 18:31:48 -0800429 free(args);
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700430 return 0;
Doug Zongker9931f7f2009-06-10 14:11:53 -0700431}
432
Doug Zongker512536a2010-02-17 16:11:44 -0800433// Evaluate the expressions in argv, giving 'count' Value* (the ... is
434// zero or more Value** to put them in). If any expression evaluates
435// to NULL, free the rest and return -1. Return 0 on success.
436int ReadValueArgs(State* state, Expr* argv[], int count, ...) {
Tao Bao2a5a49d2015-08-20 12:10:46 -0700437 Value** args = reinterpret_cast<Value**>(malloc(count * sizeof(Value*)));
Doug Zongker512536a2010-02-17 16:11:44 -0800438 va_list v;
439 va_start(v, count);
440 int i;
441 for (i = 0; i < count; ++i) {
442 args[i] = EvaluateValue(state, argv[i]);
443 if (args[i] == NULL) {
444 va_end(v);
445 int j;
446 for (j = 0; j < i; ++j) {
447 FreeValue(args[j]);
448 }
449 free(args);
450 return -1;
451 }
452 *(va_arg(v, Value**)) = args[i];
453 }
454 va_end(v);
455 free(args);
456 return 0;
457}
458
Doug Zongker9931f7f2009-06-10 14:11:53 -0700459// Evaluate the expressions in argv, returning an array of char*
460// results. If any evaluate to NULL, free the rest and return NULL.
461// The caller is responsible for freeing the returned array and the
462// strings it contains.
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700463char** ReadVarArgs(State* state, int argc, Expr* argv[]) {
464 char** args = (char**)malloc(argc * sizeof(char*));
465 int i = 0;
466 for (i = 0; i < argc; ++i) {
467 args[i] = Evaluate(state, argv[i]);
468 if (args[i] == NULL) {
469 int j;
470 for (j = 0; j < i; ++j) {
471 free(args[j]);
472 }
473 free(args);
474 return NULL;
475 }
Doug Zongker9931f7f2009-06-10 14:11:53 -0700476 }
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700477 return args;
Doug Zongker37bee622009-06-08 17:35:39 -0700478}
Doug Zongker47cace92009-06-18 10:11:50 -0700479
Doug Zongker512536a2010-02-17 16:11:44 -0800480// Evaluate the expressions in argv, returning an array of Value*
481// results. If any evaluate to NULL, free the rest and return NULL.
482// The caller is responsible for freeing the returned array and the
483// Values it contains.
484Value** ReadValueVarArgs(State* state, int argc, Expr* argv[]) {
485 Value** args = (Value**)malloc(argc * sizeof(Value*));
486 int i = 0;
487 for (i = 0; i < argc; ++i) {
488 args[i] = EvaluateValue(state, argv[i]);
489 if (args[i] == NULL) {
490 int j;
491 for (j = 0; j < i; ++j) {
492 FreeValue(args[j]);
493 }
494 free(args);
495 return NULL;
496 }
497 }
498 return args;
499}
500
Tianjie Xu16255832016-04-30 11:49:59 -0700501static void ErrorAbortV(State* state, const char* format, va_list ap) {
502 std::string buffer;
503 android::base::StringAppendV(&buffer, format, ap);
Doug Zongker47cace92009-06-18 10:11:50 -0700504 free(state->errmsg);
Tianjie Xu16255832016-04-30 11:49:59 -0700505 state->errmsg = strdup(buffer.c_str());
506 return;
507}
508
509// Use printf-style arguments to compose an error message to put into
510// *state. Returns nullptr.
511Value* ErrorAbort(State* state, const char* format, ...) {
512 va_list ap;
513 va_start(ap, format);
514 ErrorAbortV(state, format, ap);
515 va_end(ap);
516 return nullptr;
517}
518
519Value* ErrorAbort(State* state, CauseCode cause_code, const char* format, ...) {
520 va_list ap;
521 va_start(ap, format);
522 ErrorAbortV(state, format, ap);
523 va_end(ap);
524 state->cause_code = cause_code;
525 return nullptr;
Doug Zongker47cace92009-06-18 10:11:50 -0700526}