summaryrefslogtreecommitdiffstats
path: root/edify
diff options
context:
space:
mode:
Diffstat (limited to 'edify')
-rw-r--r--edify/edify_parser.cpp36
-rw-r--r--edify/expr.cpp39
-rw-r--r--edify/expr.h22
3 files changed, 37 insertions, 60 deletions
diff --git a/edify/edify_parser.cpp b/edify/edify_parser.cpp
index 16ad2ab71..15342d3fa 100644
--- a/edify/edify_parser.cpp
+++ b/edify/edify_parser.cpp
@@ -24,21 +24,20 @@
* makes the tool less useful. We should either extend the tool or remove it.
*/
+#include <errno.h>
#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
+
+#include <string>
+
+#include <android-base/file.h>
#include "expr.h"
-#include "parser.h"
-void ExprDump(int depth, Expr* n, char* script) {
+static void ExprDump(int depth, const Expr* n, const std::string& script) {
printf("%*s", depth*2, "");
- char temp = script[n->end];
- script[n->end] = '\0';
printf("%s %p (%d-%d) \"%s\"\n",
n->name == NULL ? "(NULL)" : n->name, n->fn, n->start, n->end,
- script+n->start);
- script[n->end] = temp;
+ script.substr(n->start, n->end - n->start).c_str());
for (int i = 0; i < n->argc; ++i) {
ExprDump(depth+1, n->argv[i], script);
}
@@ -53,34 +52,25 @@ int main(int argc, char** argv) {
return 1;
}
- FILE* f = fopen(argv[1], "r");
- if (f == NULL) {
- printf("%s: %s: No such file or directory\n", argv[0], argv[1]);
+ std::string buffer;
+ if (!android::base::ReadFileToString(argv[1], &buffer)) {
+ printf("%s: failed to read %s: %s\n", argv[0], argv[1], strerror(errno));
return 1;
}
- char buffer[8192];
- int size = fread(buffer, 1, 8191, f);
- fclose(f);
- buffer[size] = '\0';
Expr* root;
int error_count = 0;
- int error = parse_string(buffer, &root, &error_count);
+ int error = parse_string(buffer.data(), &root, &error_count);
printf("parse returned %d; %d errors encountered\n", error, error_count);
if (error == 0 || error_count > 0) {
ExprDump(0, root, buffer);
- State state;
- state.cookie = NULL;
- state.script = buffer;
- state.errmsg = NULL;
-
+ State state(buffer, nullptr);
char* result = Evaluate(&state, root);
if (result == NULL) {
printf("result was NULL, message is: %s\n",
- (state.errmsg == NULL ? "(NULL)" : state.errmsg));
- free(state.errmsg);
+ (state.errmsg.empty() ? "(NULL)" : state.errmsg.c_str()));
} else {
printf("result is [%s]\n", result);
}
diff --git a/edify/expr.cpp b/edify/expr.cpp
index ecb1bea1a..aa3a55a87 100644
--- a/edify/expr.cpp
+++ b/edify/expr.cpp
@@ -107,8 +107,7 @@ Value* ConcatFn(const char* name, State* state, int argc, Expr* argv[]) {
Value* IfElseFn(const char* name, State* state, int argc, Expr* argv[]) {
if (argc != 2 && argc != 3) {
- free(state->errmsg);
- state->errmsg = strdup("ifelse expects 2 or 3 arguments");
+ state->errmsg = "ifelse expects 2 or 3 arguments";
return NULL;
}
char* cond = Evaluate(state, argv[0]);
@@ -134,11 +133,10 @@ Value* AbortFn(const char* name, State* state, int argc, Expr* argv[]) {
if (argc > 0) {
msg = Evaluate(state, argv[0]);
}
- free(state->errmsg);
if (msg) {
state->errmsg = msg;
} else {
- state->errmsg = strdup("called abort()");
+ state->errmsg = "called abort()";
}
return NULL;
}
@@ -153,15 +151,8 @@ Value* AssertFn(const char* name, State* state, int argc, Expr* argv[]) {
int b = BooleanString(v);
free(v);
if (!b) {
- int prefix_len;
int len = argv[i]->end - argv[i]->start;
- char* err_src = reinterpret_cast<char*>(malloc(len + 20));
- strcpy(err_src, "assert failed: ");
- prefix_len = strlen(err_src);
- memcpy(err_src + prefix_len, state->script + argv[i]->start, len);
- err_src[prefix_len + len] = '\0';
- free(state->errmsg);
- state->errmsg = err_src;
+ state->errmsg = "assert failed: " + state->script.substr(argv[i]->start, len);
return NULL;
}
}
@@ -279,8 +270,7 @@ Value* SequenceFn(const char* name, State* state, int argc, Expr* argv[]) {
Value* LessThanIntFn(const char* name, State* state, int argc, Expr* argv[]) {
if (argc != 2) {
- free(state->errmsg);
- state->errmsg = strdup("less_than_int expects 2 arguments");
+ state->errmsg = "less_than_int expects 2 arguments";
return NULL;
}
@@ -314,8 +304,7 @@ Value* LessThanIntFn(const char* name, State* state, int argc, Expr* argv[]) {
Value* GreaterThanIntFn(const char* name, State* state,
int argc, Expr* argv[]) {
if (argc != 2) {
- free(state->errmsg);
- state->errmsg = strdup("greater_than_int expects 2 arguments");
+ state->errmsg = "greater_than_int expects 2 arguments";
return NULL;
}
@@ -499,20 +488,12 @@ Value** ReadValueVarArgs(State* state, int argc, Expr* argv[]) {
return args;
}
-static void ErrorAbortV(State* state, const char* format, va_list ap) {
- std::string buffer;
- android::base::StringAppendV(&buffer, format, ap);
- free(state->errmsg);
- state->errmsg = strdup(buffer.c_str());
- return;
-}
-
// Use printf-style arguments to compose an error message to put into
// *state. Returns nullptr.
Value* ErrorAbort(State* state, const char* format, ...) {
va_list ap;
va_start(ap, format);
- ErrorAbortV(state, format, ap);
+ android::base::StringAppendV(&state->errmsg, format, ap);
va_end(ap);
return nullptr;
}
@@ -520,8 +501,14 @@ Value* ErrorAbort(State* state, const char* format, ...) {
Value* ErrorAbort(State* state, CauseCode cause_code, const char* format, ...) {
va_list ap;
va_start(ap, format);
- ErrorAbortV(state, format, ap);
+ android::base::StringAppendV(&state->errmsg, format, ap);
va_end(ap);
state->cause_code = cause_code;
return nullptr;
}
+
+State::State(const std::string& script, void* cookie) :
+ script(script),
+ cookie(cookie) {
+}
+
diff --git a/edify/expr.h b/edify/expr.h
index 886347991..f045d9386 100644
--- a/edify/expr.h
+++ b/edify/expr.h
@@ -18,6 +18,7 @@
#define _EXPRESSION_H
#include <unistd.h>
+#include <string>
#include "error_code.h"
#include "yydefs.h"
@@ -26,20 +27,20 @@
typedef struct Expr Expr;
-typedef struct {
+struct State {
+ State(const std::string& script, void* cookie);
+
+ // The source of the original script.
+ const std::string& script;
+
// Optional pointer to app-specific data; the core of edify never
// uses this value.
void* cookie;
- // The source of the original script. Must be NULL-terminated,
- // and in writable memory (Evaluate may make temporary changes to
- // it but will restore it when done).
- char* script;
-
// The error message (if any) returned if the evaluation aborts.
- // Should be NULL initially, will be either NULL or a malloc'd
- // pointer after Evaluate() returns.
- char* errmsg;
+ // Should be empty initially, will be either empty or a string that
+ // Evaluate() returns.
+ std::string errmsg;
// error code indicates the type of failure (e.g. failure to update system image)
// during the OTA process.
@@ -50,8 +51,7 @@ typedef struct {
CauseCode cause_code = kNoCause;
bool is_retry = false;
-
-} State;
+};
#define VAL_STRING 1 // data will be NULL-terminated; size doesn't count null
#define VAL_BLOB 2