Merge "edify: Move State.script and State.errmsg to std::string." am: 8810634773

am: 6674f22a2f

Change-Id: I7da4df259a8155fbdab6e696fab4ddfb45cb0fbc
This commit is contained in:
Tao Bao
2016-10-05 06:16:22 +00:00
committed by android-build-merger
5 changed files with 57 additions and 88 deletions
+13 -23
View File
@@ -24,21 +24,20 @@
* makes the tool less useful. We should either extend the tool or remove it. * makes the tool less useful. We should either extend the tool or remove it.
*/ */
#include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h> #include <string>
#include <android-base/file.h>
#include "expr.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, ""); printf("%*s", depth*2, "");
char temp = script[n->end];
script[n->end] = '\0';
printf("%s %p (%d-%d) \"%s\"\n", printf("%s %p (%d-%d) \"%s\"\n",
n->name == NULL ? "(NULL)" : n->name, n->fn, n->start, n->end, n->name == NULL ? "(NULL)" : n->name, n->fn, n->start, n->end,
script+n->start); script.substr(n->start, n->end - n->start).c_str());
script[n->end] = temp;
for (int i = 0; i < n->argc; ++i) { for (int i = 0; i < n->argc; ++i) {
ExprDump(depth+1, n->argv[i], script); ExprDump(depth+1, n->argv[i], script);
} }
@@ -53,34 +52,25 @@ int main(int argc, char** argv) {
return 1; return 1;
} }
FILE* f = fopen(argv[1], "r"); std::string buffer;
if (f == NULL) { if (!android::base::ReadFileToString(argv[1], &buffer)) {
printf("%s: %s: No such file or directory\n", argv[0], argv[1]); printf("%s: failed to read %s: %s\n", argv[0], argv[1], strerror(errno));
return 1; return 1;
} }
char buffer[8192];
int size = fread(buffer, 1, 8191, f);
fclose(f);
buffer[size] = '\0';
Expr* root; Expr* root;
int error_count = 0; 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); printf("parse returned %d; %d errors encountered\n", error, error_count);
if (error == 0 || error_count > 0) { if (error == 0 || error_count > 0) {
ExprDump(0, root, buffer); ExprDump(0, root, buffer);
State state; State state(buffer, nullptr);
state.cookie = NULL;
state.script = buffer;
state.errmsg = NULL;
char* result = Evaluate(&state, root); char* result = Evaluate(&state, root);
if (result == NULL) { if (result == NULL) {
printf("result was NULL, message is: %s\n", printf("result was NULL, message is: %s\n",
(state.errmsg == NULL ? "(NULL)" : state.errmsg)); (state.errmsg.empty() ? "(NULL)" : state.errmsg.c_str()));
free(state.errmsg);
} else { } else {
printf("result is [%s]\n", result); printf("result is [%s]\n", result);
} }
+13 -26
View File
@@ -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[]) { Value* IfElseFn(const char* name, State* state, int argc, Expr* argv[]) {
if (argc != 2 && argc != 3) { if (argc != 2 && argc != 3) {
free(state->errmsg); state->errmsg = "ifelse expects 2 or 3 arguments";
state->errmsg = strdup("ifelse expects 2 or 3 arguments");
return NULL; return NULL;
} }
char* cond = Evaluate(state, argv[0]); char* cond = Evaluate(state, argv[0]);
@@ -134,11 +133,10 @@ Value* AbortFn(const char* name, State* state, int argc, Expr* argv[]) {
if (argc > 0) { if (argc > 0) {
msg = Evaluate(state, argv[0]); msg = Evaluate(state, argv[0]);
} }
free(state->errmsg);
if (msg) { if (msg) {
state->errmsg = msg; state->errmsg = msg;
} else { } else {
state->errmsg = strdup("called abort()"); state->errmsg = "called abort()";
} }
return NULL; return NULL;
} }
@@ -153,15 +151,8 @@ Value* AssertFn(const char* name, State* state, int argc, Expr* argv[]) {
int b = BooleanString(v); int b = BooleanString(v);
free(v); free(v);
if (!b) { if (!b) {
int prefix_len;
int len = argv[i]->end - argv[i]->start; int len = argv[i]->end - argv[i]->start;
char* err_src = reinterpret_cast<char*>(malloc(len + 20)); state->errmsg = "assert failed: " + state->script.substr(argv[i]->start, len);
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;
return NULL; 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[]) { Value* LessThanIntFn(const char* name, State* state, int argc, Expr* argv[]) {
if (argc != 2) { if (argc != 2) {
free(state->errmsg); state->errmsg = "less_than_int expects 2 arguments";
state->errmsg = strdup("less_than_int expects 2 arguments");
return NULL; return NULL;
} }
@@ -314,8 +304,7 @@ Value* LessThanIntFn(const char* name, State* state, int argc, Expr* argv[]) {
Value* GreaterThanIntFn(const char* name, State* state, Value* GreaterThanIntFn(const char* name, State* state,
int argc, Expr* argv[]) { int argc, Expr* argv[]) {
if (argc != 2) { if (argc != 2) {
free(state->errmsg); state->errmsg = "greater_than_int expects 2 arguments";
state->errmsg = strdup("greater_than_int expects 2 arguments");
return NULL; return NULL;
} }
@@ -499,20 +488,12 @@ Value** ReadValueVarArgs(State* state, int argc, Expr* argv[]) {
return args; 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 // Use printf-style arguments to compose an error message to put into
// *state. Returns nullptr. // *state. Returns nullptr.
Value* ErrorAbort(State* state, const char* format, ...) { Value* ErrorAbort(State* state, const char* format, ...) {
va_list ap; va_list ap;
va_start(ap, format); va_start(ap, format);
ErrorAbortV(state, format, ap); android::base::StringAppendV(&state->errmsg, format, ap);
va_end(ap); va_end(ap);
return nullptr; return nullptr;
} }
@@ -520,8 +501,14 @@ Value* ErrorAbort(State* state, const char* format, ...) {
Value* ErrorAbort(State* state, CauseCode cause_code, const char* format, ...) { Value* ErrorAbort(State* state, CauseCode cause_code, const char* format, ...) {
va_list ap; va_list ap;
va_start(ap, format); va_start(ap, format);
ErrorAbortV(state, format, ap); android::base::StringAppendV(&state->errmsg, format, ap);
va_end(ap); va_end(ap);
state->cause_code = cause_code; state->cause_code = cause_code;
return nullptr; return nullptr;
} }
State::State(const std::string& script, void* cookie) :
script(script),
cookie(cookie) {
}
+11 -11
View File
@@ -18,6 +18,7 @@
#define _EXPRESSION_H #define _EXPRESSION_H
#include <unistd.h> #include <unistd.h>
#include <string>
#include "error_code.h" #include "error_code.h"
#include "yydefs.h" #include "yydefs.h"
@@ -26,20 +27,20 @@
typedef struct Expr Expr; 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 // Optional pointer to app-specific data; the core of edify never
// uses this value. // uses this value.
void* cookie; 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. // The error message (if any) returned if the evaluation aborts.
// Should be NULL initially, will be either NULL or a malloc'd // Should be empty initially, will be either empty or a string that
// pointer after Evaluate() returns. // Evaluate() returns.
char* errmsg; std::string errmsg;
// error code indicates the type of failure (e.g. failure to update system image) // error code indicates the type of failure (e.g. failure to update system image)
// during the OTA process. // during the OTA process.
@@ -50,8 +51,7 @@ typedef struct {
CauseCode cause_code = kNoCause; CauseCode cause_code = kNoCause;
bool is_retry = false; bool is_retry = false;
};
} State;
#define VAL_STRING 1 // data will be NULL-terminated; size doesn't count null #define VAL_STRING 1 // data will be NULL-terminated; size doesn't count null
#define VAL_BLOB 2 #define VAL_BLOB 2
+1 -6
View File
@@ -25,10 +25,7 @@ static void expect(const char* expr_str, const char* expected) {
int error_count; int error_count;
EXPECT_EQ(parse_string(expr_str, &e, &error_count), 0); EXPECT_EQ(parse_string(expr_str, &e, &error_count), 0);
State state; State state(expr_str, nullptr);
state.cookie = nullptr;
state.errmsg = nullptr;
state.script = strdup(expr_str);
char* result = Evaluate(&state, e); char* result = Evaluate(&state, e);
@@ -38,8 +35,6 @@ static void expect(const char* expr_str, const char* expected) {
EXPECT_STREQ(result, expected); EXPECT_STREQ(result, expected);
} }
free(state.errmsg);
free(state.script);
free(result); free(result);
} }
+19 -22
View File
@@ -14,22 +14,24 @@
* limitations under the License. * limitations under the License.
*/ */
#include "updater.h"
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "edify/expr.h" #include <android-base/strings.h>
#include "updater.h"
#include "install.h"
#include "blockimg.h"
#include "minzip/Zip.h"
#include "minzip/SysUtil.h"
#include "config.h"
#include <selinux/label.h> #include <selinux/label.h>
#include <selinux/selinux.h> #include <selinux/selinux.h>
#include "config.h"
#include "edify/expr.h"
#include "blockimg.h"
#include "install.h"
#include "minzip/Zip.h"
#include "minzip/SysUtil.h"
// Generated by the makefile, this function defines the // Generated by the makefile, this function defines the
// RegisterDeviceExtensions() function, which calls all the // RegisterDeviceExtensions() function, which calls all the
// registration functions for device-specific extensions. // registration functions for device-specific extensions.
@@ -140,10 +142,7 @@ int main(int argc, char** argv) {
updater_info.package_zip_addr = map.addr; updater_info.package_zip_addr = map.addr;
updater_info.package_zip_len = map.length; updater_info.package_zip_len = map.length;
State state; State state(script, &updater_info);
state.cookie = &updater_info;
state.script = script;
state.errmsg = NULL;
if (argc == 5) { if (argc == 5) {
if (strcmp(argv[4], "retry") == 0) { if (strcmp(argv[4], "retry") == 0) {
@@ -160,22 +159,21 @@ int main(int argc, char** argv) {
} }
if (result == NULL) { if (result == NULL) {
if (state.errmsg == NULL) { if (state.errmsg.empty()) {
printf("script aborted (no error message)\n"); printf("script aborted (no error message)\n");
fprintf(cmd_pipe, "ui_print script aborted (no error message)\n"); fprintf(cmd_pipe, "ui_print script aborted (no error message)\n");
} else { } else {
printf("script aborted: %s\n", state.errmsg); printf("script aborted: %s\n", state.errmsg.c_str());
char* line = strtok(state.errmsg, "\n"); const std::vector<std::string> lines = android::base::Split(state.errmsg, "\n");
while (line) { for (const std::string& line : lines) {
// Parse the error code in abort message. // Parse the error code in abort message.
// Example: "E30: This package is for bullhead devices." // Example: "E30: This package is for bullhead devices."
if (*line == 'E') { if (!line.empty() && line[0] == 'E') {
if (sscanf(line, "E%u: ", &state.error_code) != 1) { if (sscanf(line.c_str(), "E%u: ", &state.error_code) != 1) {
printf("Failed to parse error code: [%s]\n", line); printf("Failed to parse error code: [%s]\n", line.c_str());
} }
} }
fprintf(cmd_pipe, "ui_print %s\n", line); fprintf(cmd_pipe, "ui_print %s\n", line.c_str());
line = strtok(NULL, "\n");
} }
fprintf(cmd_pipe, "ui_print\n"); fprintf(cmd_pipe, "ui_print\n");
} }
@@ -189,7 +187,6 @@ int main(int argc, char** argv) {
} }
} }
free(state.errmsg);
return 7; return 7;
} else { } else {
fprintf(cmd_pipe, "ui_print script succeeded: result was [%s]\n", result); fprintf(cmd_pipe, "ui_print script succeeded: result was [%s]\n", result);