aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTianjie Xu2016-10-17 20:15:20 -0500
committerTianjie Xu2016-11-01 17:03:06 -0500
commit5fe280ac96a2c13f613b7138bb239c3f36d35c9f (patch)
treec189e5f93d00db87e71b8c7401af545e02d4d809 /edify/expr.cpp
parent3f4030e0ef53c9e308b70788dc53bf0c5cfcd06a (diff)
downloadplatform-bootable-recovery-5fe280ac96a2c13f613b7138bb239c3f36d35c9f.tar.gz
platform-bootable-recovery-5fe280ac96a2c13f613b7138bb239c3f36d35c9f.tar.xz
platform-bootable-recovery-5fe280ac96a2c13f613b7138bb239c3f36d35c9f.zip
Cleanup ReadArgs & ReadValueArgs usage
ReadArgs will switch to using std::string and std::unique_ptr. Also cleanup the callers. Test: mma & component test passed. Change-Id: I4724406ae6c0c134a27bbd1cdd24ad5d343b2a3b
Diffstat (limited to 'edify/expr.cpp')
-rw-r--r--edify/expr.cpp123
1 files changed, 12 insertions, 111 deletions
diff --git a/edify/expr.cpp b/edify/expr.cpp
index ec240975..329cf3ac 100644
--- a/edify/expr.cpp
+++ b/edify/expr.cpp
@@ -254,31 +254,25 @@ Value* LessThanIntFn(const char* name, State* state, int argc, Expr* argv[]) {
254 return nullptr; 254 return nullptr;
255 } 255 }
256 256
257 char* left; 257 std::vector<std::string> args;
258 char* right; 258 if (!ReadArgs(state, 2, argv, &args)) {
259 if (ReadArgs(state, argv, 2, &left, &right) < 0) return nullptr; 259 return nullptr;
260 260 }
261 bool result = false;
262 char* end;
263 261
264 // Parse up to at least long long or 64-bit integers. 262 // Parse up to at least long long or 64-bit integers.
265 int64_t l_int = static_cast<int64_t>(strtoll(left, &end, 10)); 263 int64_t l_int;
266 if (left[0] == '\0' || *end != '\0') { 264 if (!android::base::ParseInt(args[0].c_str(), &l_int)) {
267 goto done; 265 state->errmsg = "failed to parse int in " + args[0];
266 return nullptr;
268 } 267 }
269 268
270 int64_t r_int; 269 int64_t r_int;
271 r_int = static_cast<int64_t>(strtoll(right, &end, 10)); 270 if (!android::base::ParseInt(args[1].c_str(), &r_int)) {
272 if (right[0] == '\0' || *end != '\0') { 271 state->errmsg = "failed to parse int in " + args[1];
273 goto done; 272 return nullptr;
274 } 273 }
275 274
276 result = l_int < r_int; 275 return StringValue(l_int < r_int ? "t" : "");
277
278 done:
279 free(left);
280 free(right);
281 return StringValue(result ? "t" : "");
282} 276}
283 277
284Value* GreaterThanIntFn(const char* name, State* state, 278Value* GreaterThanIntFn(const char* name, State* state,
@@ -372,99 +366,6 @@ bool ReadValueArgs(State* state, int argc, Expr* argv[],
372 return true; 366 return true;
373} 367}
374 368
375// Evaluate the expressions in argv, giving 'count' char* (the ... is
376// zero or more char** to put them in). If any expression evaluates
377// to NULL, free the rest and return -1. Return 0 on success.
378int ReadArgs(State* state, Expr* argv[], int count, ...) {
379 char** args = reinterpret_cast<char**>(malloc(count * sizeof(char*)));
380 va_list v;
381 va_start(v, count);
382 int i;
383 for (i = 0; i < count; ++i) {
384 std::string str;
385 if (!Evaluate(state, argv[i], &str) ||
386 (args[i] = strdup(str.c_str())) == nullptr) {
387 va_end(v);
388 int j;
389 for (j = 0; j < i; ++j) {
390 free(args[j]);
391 }
392 free(args);
393 return -1;
394 }
395 *(va_arg(v, char**)) = args[i];
396 }
397 va_end(v);
398 free(args);
399 return 0;
400}
401
402// Evaluate the expressions in argv, giving 'count' Value* (the ... is
403// zero or more Value** to put them in). If any expression evaluates
404// to NULL, free the rest and return -1. Return 0 on success.
405int ReadValueArgs(State* state, Expr* argv[], int count, ...) {
406 Value** args = new Value*[count];
407 va_list v;
408 va_start(v, count);
409 for (int i = 0; i < count; ++i) {
410 args[i] = EvaluateValue(state, argv[i]);
411 if (args[i] == NULL) {
412 va_end(v);
413 int j;
414 for (j = 0; j < i; ++j) {
415 delete args[j];
416 }
417 delete[] args;
418 return -1;
419 }
420 *(va_arg(v, Value**)) = args[i];
421 }
422 va_end(v);
423 delete[] args;
424 return 0;
425}
426
427// Evaluate the expressions in argv, returning an array of char*
428// results. If any evaluate to NULL, free the rest and return NULL.
429// The caller is responsible for freeing the returned array and the
430// strings it contains.
431char** ReadVarArgs(State* state, int argc, Expr* argv[]) {
432 char** args = (char**)malloc(argc * sizeof(char*));
433 for (int i = 0; i < argc; ++i) {
434 std::string str;
435 if (!Evaluate(state, argv[i], &str) ||
436 (args[i] = strdup(str.c_str())) == nullptr) {
437 for (int j = 0; j < i; ++j) {
438 free(args[j]);
439 }
440 free(args);
441 return NULL;
442 }
443 }
444 return args;
445}
446
447// Evaluate the expressions in argv, returning an array of Value*
448// results. If any evaluate to NULL, free the rest and return NULL.
449// The caller is responsible for freeing the returned array and the
450// Values it contains.
451Value** ReadValueVarArgs(State* state, int argc, Expr* argv[]) {
452 Value** args = new Value*[argc];
453 int i = 0;
454 for (i = 0; i < argc; ++i) {
455 args[i] = EvaluateValue(state, argv[i]);
456 if (args[i] == NULL) {
457 int j;
458 for (j = 0; j < i; ++j) {
459 delete args[j];
460 }
461 delete[] args;
462 return NULL;
463 }
464 }
465 return args;
466}
467
468// Use printf-style arguments to compose an error message to put into 369// Use printf-style arguments to compose an error message to put into
469// *state. Returns nullptr. 370// *state. Returns nullptr.
470Value* ErrorAbort(State* state, const char* format, ...) { 371Value* ErrorAbort(State* state, const char* format, ...) {