diff options
Diffstat (limited to 'edify/expr.cpp')
-rw-r--r-- | edify/expr.cpp | 39 |
1 files changed, 29 insertions, 10 deletions
diff --git a/edify/expr.cpp b/edify/expr.cpp index c34342f7..ecb1bea1 100644 --- a/edify/expr.cpp +++ b/edify/expr.cpp | |||
@@ -21,6 +21,11 @@ | |||
21 | #include <stdarg.h> | 21 | #include <stdarg.h> |
22 | #include <unistd.h> | 22 | #include <unistd.h> |
23 | 23 | ||
24 | #include <string> | ||
25 | |||
26 | #include <android-base/stringprintf.h> | ||
27 | #include <android-base/strings.h> | ||
28 | |||
24 | #include "expr.h" | 29 | #include "expr.h" |
25 | 30 | ||
26 | // Functions should: | 31 | // Functions should: |
@@ -36,7 +41,7 @@ char* Evaluate(State* state, Expr* expr) { | |||
36 | Value* v = expr->fn(expr->name, state, expr->argc, expr->argv); | 41 | Value* v = expr->fn(expr->name, state, expr->argc, expr->argv); |
37 | if (v == NULL) return NULL; | 42 | if (v == NULL) return NULL; |
38 | if (v->type != VAL_STRING) { | 43 | if (v->type != VAL_STRING) { |
39 | ErrorAbort(state, "expecting string, got value type %d", v->type); | 44 | ErrorAbort(state, kArgsParsingFailure, "expecting string, got value type %d", v->type); |
40 | FreeValue(v); | 45 | FreeValue(v); |
41 | return NULL; | 46 | return NULL; |
42 | } | 47 | } |
@@ -494,15 +499,29 @@ Value** ReadValueVarArgs(State* state, int argc, Expr* argv[]) { | |||
494 | return args; | 499 | return args; |
495 | } | 500 | } |
496 | 501 | ||
502 | static void ErrorAbortV(State* state, const char* format, va_list ap) { | ||
503 | std::string buffer; | ||
504 | android::base::StringAppendV(&buffer, format, ap); | ||
505 | free(state->errmsg); | ||
506 | state->errmsg = strdup(buffer.c_str()); | ||
507 | return; | ||
508 | } | ||
509 | |||
497 | // Use printf-style arguments to compose an error message to put into | 510 | // Use printf-style arguments to compose an error message to put into |
498 | // *state. Returns NULL. | 511 | // *state. Returns nullptr. |
499 | Value* ErrorAbort(State* state, const char* format, ...) { | 512 | Value* ErrorAbort(State* state, const char* format, ...) { |
500 | char* buffer = reinterpret_cast<char*>(malloc(4096)); | 513 | va_list ap; |
501 | va_list v; | 514 | va_start(ap, format); |
502 | va_start(v, format); | 515 | ErrorAbortV(state, format, ap); |
503 | vsnprintf(buffer, 4096, format, v); | 516 | va_end(ap); |
504 | va_end(v); | 517 | return nullptr; |
505 | free(state->errmsg); | 518 | } |
506 | state->errmsg = buffer; | 519 | |
507 | return NULL; | 520 | Value* 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; | ||
508 | } | 527 | } |