2009-06-09 02:35:39 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2009 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2017-10-04 18:33:01 +02:00
|
|
|
#include "edify/expr.h"
|
2016-10-11 07:52:18 +02:00
|
|
|
|
|
|
|
#include <stdarg.h>
|
2009-06-09 02:35:39 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2016-10-11 07:52:18 +02:00
|
|
|
#include <string.h>
|
2009-06-10 23:11:53 +02:00
|
|
|
#include <unistd.h>
|
2009-06-09 02:35:39 +02:00
|
|
|
|
2016-10-12 19:55:04 +02:00
|
|
|
#include <memory>
|
2016-04-30 20:49:59 +02:00
|
|
|
#include <string>
|
2016-10-11 07:52:18 +02:00
|
|
|
#include <unordered_map>
|
2016-10-12 19:55:04 +02:00
|
|
|
#include <vector>
|
2016-04-30 20:49:59 +02:00
|
|
|
|
2016-10-12 19:55:04 +02:00
|
|
|
#include <android-base/parseint.h>
|
2016-04-30 20:49:59 +02:00
|
|
|
#include <android-base/stringprintf.h>
|
|
|
|
#include <android-base/strings.h>
|
|
|
|
|
2017-10-06 16:43:41 +02:00
|
|
|
#include "otautil/error_code.h"
|
2017-10-05 21:06:49 +02:00
|
|
|
|
2009-06-09 02:35:39 +02:00
|
|
|
// Functions should:
|
|
|
|
//
|
|
|
|
// - return a malloc()'d string
|
2016-10-12 19:55:04 +02:00
|
|
|
// - if Evaluate() on any argument returns nullptr, return nullptr.
|
2009-06-09 02:35:39 +02:00
|
|
|
|
2016-10-12 19:55:04 +02:00
|
|
|
static bool BooleanString(const std::string& s) {
|
|
|
|
return !s.empty();
|
2009-06-09 02:35:39 +02:00
|
|
|
}
|
|
|
|
|
2017-03-06 23:44:59 +01:00
|
|
|
bool Evaluate(State* state, const std::unique_ptr<Expr>& expr, std::string* result) {
|
2016-10-12 19:55:04 +02:00
|
|
|
if (result == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-06 23:44:59 +01:00
|
|
|
std::unique_ptr<Value> v(expr->fn(expr->name.c_str(), state, expr->argv));
|
2016-10-12 19:55:04 +02:00
|
|
|
if (!v) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-06-20 00:56:49 +02:00
|
|
|
if (v->type != Value::Type::STRING) {
|
|
|
|
ErrorAbort(state, kArgsParsingFailure, "expecting string, got value type %d", v->type);
|
|
|
|
return false;
|
2010-02-18 01:11:44 +01:00
|
|
|
}
|
2016-10-12 19:55:04 +02:00
|
|
|
|
|
|
|
*result = v->data;
|
|
|
|
return true;
|
2010-02-18 01:11:44 +01:00
|
|
|
}
|
|
|
|
|
2017-03-06 23:44:59 +01:00
|
|
|
Value* EvaluateValue(State* state, const std::unique_ptr<Expr>& expr) {
|
|
|
|
return expr->fn(expr->name.c_str(), state, expr->argv);
|
2009-06-09 02:35:39 +02:00
|
|
|
}
|
|
|
|
|
2016-10-12 19:55:04 +02:00
|
|
|
Value* StringValue(const char* str) {
|
|
|
|
if (str == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-06-20 00:56:49 +02:00
|
|
|
return new Value(Value::Type::STRING, str);
|
2010-02-18 01:11:44 +01:00
|
|
|
}
|
|
|
|
|
2016-10-12 19:55:04 +02:00
|
|
|
Value* StringValue(const std::string& str) {
|
|
|
|
return StringValue(str.c_str());
|
2010-02-18 01:11:44 +01:00
|
|
|
}
|
|
|
|
|
2017-03-06 23:44:59 +01:00
|
|
|
Value* ConcatFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
|
|
|
|
if (argv.empty()) {
|
2016-10-12 19:55:04 +02:00
|
|
|
return StringValue("");
|
2009-06-12 21:24:39 +02:00
|
|
|
}
|
2016-10-12 19:55:04 +02:00
|
|
|
std::string result;
|
2017-03-06 23:44:59 +01:00
|
|
|
for (size_t i = 0; i < argv.size(); ++i) {
|
2016-10-12 19:55:04 +02:00
|
|
|
std::string str;
|
|
|
|
if (!Evaluate(state, argv[i], &str)) {
|
|
|
|
return nullptr;
|
2009-06-12 21:24:39 +02:00
|
|
|
}
|
2016-10-12 19:55:04 +02:00
|
|
|
result += str;
|
2009-06-12 21:24:39 +02:00
|
|
|
}
|
|
|
|
|
2010-02-18 01:11:44 +01:00
|
|
|
return StringValue(result);
|
2009-06-09 02:35:39 +02:00
|
|
|
}
|
|
|
|
|
2017-03-06 23:44:59 +01:00
|
|
|
Value* IfElseFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
|
|
|
|
if (argv.size() != 2 && argv.size() != 3) {
|
2016-10-04 03:06:46 +02:00
|
|
|
state->errmsg = "ifelse expects 2 or 3 arguments";
|
2016-10-12 19:55:04 +02:00
|
|
|
return nullptr;
|
2009-06-12 21:24:39 +02:00
|
|
|
}
|
2016-10-12 19:55:04 +02:00
|
|
|
|
|
|
|
std::string cond;
|
|
|
|
if (!Evaluate(state, argv[0], &cond)) {
|
|
|
|
return nullptr;
|
2009-06-12 21:24:39 +02:00
|
|
|
}
|
|
|
|
|
2016-10-12 19:55:04 +02:00
|
|
|
if (!cond.empty()) {
|
2010-02-18 01:11:44 +01:00
|
|
|
return EvaluateValue(state, argv[1]);
|
2017-03-06 23:44:59 +01:00
|
|
|
} else if (argv.size() == 3) {
|
2016-10-12 19:55:04 +02:00
|
|
|
return EvaluateValue(state, argv[2]);
|
2009-06-09 02:35:39 +02:00
|
|
|
}
|
2016-10-12 19:55:04 +02:00
|
|
|
|
|
|
|
return StringValue("");
|
2009-06-09 02:35:39 +02:00
|
|
|
}
|
|
|
|
|
2017-03-06 23:44:59 +01:00
|
|
|
Value* AbortFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
|
2016-10-12 19:55:04 +02:00
|
|
|
std::string msg;
|
2017-03-06 23:44:59 +01:00
|
|
|
if (!argv.empty() && Evaluate(state, argv[0], &msg)) {
|
2018-01-29 00:37:48 +01:00
|
|
|
state->errmsg += msg;
|
2009-06-12 21:24:39 +02:00
|
|
|
} else {
|
2018-01-29 00:37:48 +01:00
|
|
|
state->errmsg += "called abort()";
|
2009-06-12 21:24:39 +02:00
|
|
|
}
|
2016-10-12 19:55:04 +02:00
|
|
|
return nullptr;
|
2009-06-09 02:35:39 +02:00
|
|
|
}
|
|
|
|
|
2017-03-06 23:44:59 +01:00
|
|
|
Value* AssertFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
|
|
|
|
for (size_t i = 0; i < argv.size(); ++i) {
|
2016-10-12 19:55:04 +02:00
|
|
|
std::string result;
|
|
|
|
if (!Evaluate(state, argv[i], &result)) {
|
|
|
|
return nullptr;
|
2009-06-12 21:24:39 +02:00
|
|
|
}
|
2016-10-12 19:55:04 +02:00
|
|
|
if (result.empty()) {
|
2009-06-12 21:24:39 +02:00
|
|
|
int len = argv[i]->end - argv[i]->start;
|
2016-10-04 03:06:46 +02:00
|
|
|
state->errmsg = "assert failed: " + state->script.substr(argv[i]->start, len);
|
2016-10-12 19:55:04 +02:00
|
|
|
return nullptr;
|
2009-06-12 21:24:39 +02:00
|
|
|
}
|
2009-06-09 02:35:39 +02:00
|
|
|
}
|
2016-10-12 19:55:04 +02:00
|
|
|
return StringValue("");
|
2009-06-09 02:35:39 +02:00
|
|
|
}
|
|
|
|
|
2017-03-06 23:44:59 +01:00
|
|
|
Value* SleepFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
|
2016-10-12 19:55:04 +02:00
|
|
|
std::string val;
|
|
|
|
if (!Evaluate(state, argv[0], &val)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
int v;
|
|
|
|
if (!android::base::ParseInt(val.c_str(), &v, 0)) {
|
|
|
|
return nullptr;
|
2009-06-12 21:24:39 +02:00
|
|
|
}
|
|
|
|
sleep(v);
|
2016-10-12 19:55:04 +02:00
|
|
|
|
2010-02-18 01:11:44 +01:00
|
|
|
return StringValue(val);
|
2009-06-10 23:11:53 +02:00
|
|
|
}
|
|
|
|
|
2017-03-06 23:44:59 +01:00
|
|
|
Value* StdoutFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
|
|
|
|
for (size_t i = 0; i < argv.size(); ++i) {
|
2016-10-12 19:55:04 +02:00
|
|
|
std::string v;
|
|
|
|
if (!Evaluate(state, argv[i], &v)) {
|
|
|
|
return nullptr;
|
2009-06-12 21:24:39 +02:00
|
|
|
}
|
2016-10-12 19:55:04 +02:00
|
|
|
fputs(v.c_str(), stdout);
|
2009-06-09 02:35:39 +02:00
|
|
|
}
|
2016-10-12 19:55:04 +02:00
|
|
|
return StringValue("");
|
2009-06-09 02:35:39 +02:00
|
|
|
}
|
|
|
|
|
2010-02-18 01:11:44 +01:00
|
|
|
Value* LogicalAndFn(const char* name, State* state,
|
2017-03-06 23:44:59 +01:00
|
|
|
const std::vector<std::unique_ptr<Expr>>& argv) {
|
2016-10-12 19:55:04 +02:00
|
|
|
std::string left;
|
|
|
|
if (!Evaluate(state, argv[0], &left)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (BooleanString(left)) {
|
2010-02-18 01:11:44 +01:00
|
|
|
return EvaluateValue(state, argv[1]);
|
2009-06-12 21:24:39 +02:00
|
|
|
} else {
|
2016-10-12 19:55:04 +02:00
|
|
|
return StringValue("");
|
2009-06-12 21:24:39 +02:00
|
|
|
}
|
2009-06-09 02:35:39 +02:00
|
|
|
}
|
|
|
|
|
2010-02-18 01:11:44 +01:00
|
|
|
Value* LogicalOrFn(const char* name, State* state,
|
2017-03-06 23:44:59 +01:00
|
|
|
const std::vector<std::unique_ptr<Expr>>& argv) {
|
2016-10-12 19:55:04 +02:00
|
|
|
std::string left;
|
|
|
|
if (!Evaluate(state, argv[0], &left)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (!BooleanString(left)) {
|
2010-02-18 01:11:44 +01:00
|
|
|
return EvaluateValue(state, argv[1]);
|
2009-06-12 21:24:39 +02:00
|
|
|
} else {
|
2010-02-18 01:11:44 +01:00
|
|
|
return StringValue(left);
|
2009-06-12 21:24:39 +02:00
|
|
|
}
|
2009-06-09 02:35:39 +02:00
|
|
|
}
|
|
|
|
|
2010-02-18 01:11:44 +01:00
|
|
|
Value* LogicalNotFn(const char* name, State* state,
|
2017-03-06 23:44:59 +01:00
|
|
|
const std::vector<std::unique_ptr<Expr>>& argv) {
|
2016-10-12 19:55:04 +02:00
|
|
|
std::string val;
|
|
|
|
if (!Evaluate(state, argv[0], &val)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return StringValue(BooleanString(val) ? "" : "t");
|
2009-06-09 02:35:39 +02:00
|
|
|
}
|
|
|
|
|
2010-02-18 01:11:44 +01:00
|
|
|
Value* SubstringFn(const char* name, State* state,
|
2017-03-06 23:44:59 +01:00
|
|
|
const std::vector<std::unique_ptr<Expr>>& argv) {
|
2016-10-12 19:55:04 +02:00
|
|
|
std::string needle;
|
|
|
|
if (!Evaluate(state, argv[0], &needle)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string haystack;
|
|
|
|
if (!Evaluate(state, argv[1], &haystack)) {
|
|
|
|
return nullptr;
|
2009-06-12 21:24:39 +02:00
|
|
|
}
|
2009-06-09 02:35:39 +02:00
|
|
|
|
2016-10-12 19:55:04 +02:00
|
|
|
std::string result = (haystack.find(needle) != std::string::npos) ? "t" : "";
|
2010-02-18 01:11:44 +01:00
|
|
|
return StringValue(result);
|
2009-06-09 02:35:39 +02:00
|
|
|
}
|
|
|
|
|
2017-03-06 23:44:59 +01:00
|
|
|
Value* EqualityFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
|
2016-10-12 19:55:04 +02:00
|
|
|
std::string left;
|
|
|
|
if (!Evaluate(state, argv[0], &left)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
std::string right;
|
|
|
|
if (!Evaluate(state, argv[1], &right)) {
|
|
|
|
return nullptr;
|
2009-06-12 21:24:39 +02:00
|
|
|
}
|
2009-06-09 02:35:39 +02:00
|
|
|
|
2016-10-12 19:55:04 +02:00
|
|
|
const char* result = (left == right) ? "t" : "";
|
2010-02-18 01:11:44 +01:00
|
|
|
return StringValue(result);
|
2009-06-09 02:35:39 +02:00
|
|
|
}
|
|
|
|
|
2017-03-06 23:44:59 +01:00
|
|
|
Value* InequalityFn(const char* name, State* state,
|
|
|
|
const std::vector<std::unique_ptr<Expr>>& argv) {
|
2016-10-12 19:55:04 +02:00
|
|
|
std::string left;
|
|
|
|
if (!Evaluate(state, argv[0], &left)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
std::string right;
|
|
|
|
if (!Evaluate(state, argv[1], &right)) {
|
|
|
|
return nullptr;
|
2009-06-12 21:24:39 +02:00
|
|
|
}
|
2009-06-09 02:35:39 +02:00
|
|
|
|
2016-10-12 19:55:04 +02:00
|
|
|
const char* result = (left != right) ? "t" : "";
|
2010-02-18 01:11:44 +01:00
|
|
|
return StringValue(result);
|
2009-06-09 02:35:39 +02:00
|
|
|
}
|
|
|
|
|
2017-03-06 23:44:59 +01:00
|
|
|
Value* SequenceFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
|
2016-10-12 19:55:04 +02:00
|
|
|
std::unique_ptr<Value> left(EvaluateValue(state, argv[0]));
|
|
|
|
if (!left) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2010-02-18 01:11:44 +01:00
|
|
|
return EvaluateValue(state, argv[1]);
|
2009-06-10 23:11:53 +02:00
|
|
|
}
|
|
|
|
|
2017-03-06 23:44:59 +01:00
|
|
|
Value* LessThanIntFn(const char* name, State* state,
|
|
|
|
const std::vector<std::unique_ptr<Expr>>& argv) {
|
|
|
|
if (argv.size() != 2) {
|
2016-10-04 03:06:46 +02:00
|
|
|
state->errmsg = "less_than_int expects 2 arguments";
|
2016-10-12 19:55:04 +02:00
|
|
|
return nullptr;
|
2009-06-13 01:13:52 +02:00
|
|
|
}
|
|
|
|
|
2016-10-18 03:15:20 +02:00
|
|
|
std::vector<std::string> args;
|
2017-03-06 23:44:59 +01:00
|
|
|
if (!ReadArgs(state, argv, &args)) {
|
2016-10-18 03:15:20 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
2009-06-13 01:13:52 +02:00
|
|
|
|
2016-04-18 20:30:55 +02:00
|
|
|
// Parse up to at least long long or 64-bit integers.
|
2016-10-18 03:15:20 +02:00
|
|
|
int64_t l_int;
|
|
|
|
if (!android::base::ParseInt(args[0].c_str(), &l_int)) {
|
|
|
|
state->errmsg = "failed to parse int in " + args[0];
|
|
|
|
return nullptr;
|
2009-06-13 01:13:52 +02:00
|
|
|
}
|
|
|
|
|
2016-04-18 20:30:55 +02:00
|
|
|
int64_t r_int;
|
2016-10-18 03:15:20 +02:00
|
|
|
if (!android::base::ParseInt(args[1].c_str(), &r_int)) {
|
|
|
|
state->errmsg = "failed to parse int in " + args[1];
|
|
|
|
return nullptr;
|
2009-06-13 01:13:52 +02:00
|
|
|
}
|
|
|
|
|
2016-10-18 03:15:20 +02:00
|
|
|
return StringValue(l_int < r_int ? "t" : "");
|
2009-06-13 01:13:52 +02:00
|
|
|
}
|
|
|
|
|
2010-02-18 01:11:44 +01:00
|
|
|
Value* GreaterThanIntFn(const char* name, State* state,
|
2017-03-06 23:44:59 +01:00
|
|
|
const std::vector<std::unique_ptr<Expr>>& argv) {
|
|
|
|
if (argv.size() != 2) {
|
2016-10-04 03:06:46 +02:00
|
|
|
state->errmsg = "greater_than_int expects 2 arguments";
|
2016-10-12 19:55:04 +02:00
|
|
|
return nullptr;
|
2009-06-13 01:13:52 +02:00
|
|
|
}
|
|
|
|
|
2017-03-06 23:44:59 +01:00
|
|
|
std::vector<std::string> args;
|
|
|
|
if (!ReadArgs(state, argv, &args)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse up to at least long long or 64-bit integers.
|
|
|
|
int64_t l_int;
|
|
|
|
if (!android::base::ParseInt(args[0].c_str(), &l_int)) {
|
|
|
|
state->errmsg = "failed to parse int in " + args[0];
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t r_int;
|
|
|
|
if (!android::base::ParseInt(args[1].c_str(), &r_int)) {
|
|
|
|
state->errmsg = "failed to parse int in " + args[1];
|
|
|
|
return nullptr;
|
|
|
|
}
|
2009-06-13 01:13:52 +02:00
|
|
|
|
2017-03-06 23:44:59 +01:00
|
|
|
return StringValue(l_int > r_int ? "t" : "");
|
2009-06-13 01:13:52 +02:00
|
|
|
}
|
|
|
|
|
2017-03-06 23:44:59 +01:00
|
|
|
Value* Literal(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv) {
|
2016-10-12 19:55:04 +02:00
|
|
|
return StringValue(name);
|
2009-06-10 23:11:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------
|
|
|
|
// the function table
|
|
|
|
// -----------------------------------------------------------------
|
|
|
|
|
2016-10-11 07:52:18 +02:00
|
|
|
static std::unordered_map<std::string, Function> fn_table;
|
2009-06-09 02:35:39 +02:00
|
|
|
|
2016-10-11 07:52:18 +02:00
|
|
|
void RegisterFunction(const std::string& name, Function fn) {
|
|
|
|
fn_table[name] = fn;
|
2009-06-09 02:35:39 +02:00
|
|
|
}
|
|
|
|
|
2016-10-11 07:52:18 +02:00
|
|
|
Function FindFunction(const std::string& name) {
|
|
|
|
if (fn_table.find(name) == fn_table.end()) {
|
|
|
|
return nullptr;
|
|
|
|
} else {
|
|
|
|
return fn_table[name];
|
2009-06-12 21:24:39 +02:00
|
|
|
}
|
2009-06-09 02:35:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void RegisterBuiltins() {
|
2009-06-12 21:24:39 +02:00
|
|
|
RegisterFunction("ifelse", IfElseFn);
|
|
|
|
RegisterFunction("abort", AbortFn);
|
|
|
|
RegisterFunction("assert", AssertFn);
|
|
|
|
RegisterFunction("concat", ConcatFn);
|
|
|
|
RegisterFunction("is_substring", SubstringFn);
|
|
|
|
RegisterFunction("stdout", StdoutFn);
|
|
|
|
RegisterFunction("sleep", SleepFn);
|
2009-06-13 01:13:52 +02:00
|
|
|
|
|
|
|
RegisterFunction("less_than_int", LessThanIntFn);
|
|
|
|
RegisterFunction("greater_than_int", GreaterThanIntFn);
|
2009-06-10 23:11:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------
|
|
|
|
// convenience methods for functions
|
|
|
|
// -----------------------------------------------------------------
|
|
|
|
|
2017-03-06 23:44:59 +01:00
|
|
|
// Evaluate the expressions in argv, and put the results of strings in args. If any expression
|
|
|
|
// evaluates to nullptr, return false. Return true on success.
|
|
|
|
bool ReadArgs(State* state, const std::vector<std::unique_ptr<Expr>>& argv,
|
|
|
|
std::vector<std::string>* args) {
|
|
|
|
return ReadArgs(state, argv, args, 0, argv.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ReadArgs(State* state, const std::vector<std::unique_ptr<Expr>>& argv,
|
|
|
|
std::vector<std::string>* args, size_t start, size_t len) {
|
2016-10-12 19:55:04 +02:00
|
|
|
if (args == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-03-23 23:12:22 +01:00
|
|
|
if (start + len > argv.size()) {
|
2017-03-06 23:44:59 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (size_t i = start; i < start + len; ++i) {
|
2016-10-12 19:55:04 +02:00
|
|
|
std::string var;
|
|
|
|
if (!Evaluate(state, argv[i], &var)) {
|
|
|
|
args->clear();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
args->push_back(var);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-06 23:44:59 +01:00
|
|
|
// Evaluate the expressions in argv, and put the results of Value* in args. If any expression
|
|
|
|
// evaluate to nullptr, return false. Return true on success.
|
|
|
|
bool ReadValueArgs(State* state, const std::vector<std::unique_ptr<Expr>>& argv,
|
2016-10-12 19:55:04 +02:00
|
|
|
std::vector<std::unique_ptr<Value>>* args) {
|
2017-03-06 23:44:59 +01:00
|
|
|
return ReadValueArgs(state, argv, args, 0, argv.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ReadValueArgs(State* state, const std::vector<std::unique_ptr<Expr>>& argv,
|
|
|
|
std::vector<std::unique_ptr<Value>>* args, size_t start, size_t len) {
|
2016-10-12 19:55:04 +02:00
|
|
|
if (args == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-03-06 23:44:59 +01:00
|
|
|
if (len == 0 || start + len > argv.size()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (size_t i = start; i < start + len; ++i) {
|
2016-10-12 19:55:04 +02:00
|
|
|
std::unique_ptr<Value> v(EvaluateValue(state, argv[i]));
|
|
|
|
if (!v) {
|
|
|
|
args->clear();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
args->push_back(std::move(v));
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-06-18 19:11:50 +02:00
|
|
|
// Use printf-style arguments to compose an error message to put into
|
2016-04-30 20:49:59 +02:00
|
|
|
// *state. Returns nullptr.
|
2012-04-12 20:01:22 +02:00
|
|
|
Value* ErrorAbort(State* state, const char* format, ...) {
|
2016-04-30 20:49:59 +02:00
|
|
|
va_list ap;
|
|
|
|
va_start(ap, format);
|
2016-10-04 03:06:46 +02:00
|
|
|
android::base::StringAppendV(&state->errmsg, format, ap);
|
2016-04-30 20:49:59 +02:00
|
|
|
va_end(ap);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value* ErrorAbort(State* state, CauseCode cause_code, const char* format, ...) {
|
2018-01-29 00:37:48 +01:00
|
|
|
std::string err_message;
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, format);
|
|
|
|
android::base::StringAppendV(&err_message, format, ap);
|
|
|
|
va_end(ap);
|
|
|
|
// Ensure that there's exactly one line break at the end of the error message.
|
|
|
|
state->errmsg = android::base::Trim(err_message) + "\n";
|
|
|
|
state->cause_code = cause_code;
|
|
|
|
return nullptr;
|
2009-06-18 19:11:50 +02:00
|
|
|
}
|
2016-10-04 03:06:46 +02:00
|
|
|
|
2017-10-05 21:06:49 +02:00
|
|
|
State::State(const std::string& script, void* cookie)
|
|
|
|
: script(script), cookie(cookie), error_code(kNoError), cause_code(kNoCause) {}
|