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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _EXPRESSION_H
|
|
|
|
#define _EXPRESSION_H
|
|
|
|
|
2010-02-20 01:07:57 +01:00
|
|
|
#include <unistd.h>
|
2017-03-06 23:44:59 +01:00
|
|
|
|
|
|
|
#include <memory>
|
2016-10-04 03:06:46 +02:00
|
|
|
#include <string>
|
2017-03-06 23:44:59 +01:00
|
|
|
#include <vector>
|
2010-02-20 01:07:57 +01:00
|
|
|
|
2016-04-30 20:49:59 +02:00
|
|
|
#include "error_code.h"
|
2009-06-09 02:35:39 +02:00
|
|
|
|
2016-10-04 03:06:46 +02:00
|
|
|
struct State {
|
|
|
|
State(const std::string& script, void* cookie);
|
|
|
|
|
|
|
|
// The source of the original script.
|
|
|
|
const std::string& script;
|
|
|
|
|
2009-06-12 21:24:39 +02:00
|
|
|
// Optional pointer to app-specific data; the core of edify never
|
|
|
|
// uses this value.
|
|
|
|
void* cookie;
|
|
|
|
|
|
|
|
// The error message (if any) returned if the evaluation aborts.
|
2016-10-04 03:06:46 +02:00
|
|
|
// Should be empty initially, will be either empty or a string that
|
|
|
|
// Evaluate() returns.
|
|
|
|
std::string errmsg;
|
2016-04-30 20:49:59 +02:00
|
|
|
|
|
|
|
// error code indicates the type of failure (e.g. failure to update system image)
|
|
|
|
// during the OTA process.
|
|
|
|
ErrorCode error_code = kNoError;
|
|
|
|
|
|
|
|
// cause code provides more detailed reason of an OTA failure (e.g. fsync error)
|
|
|
|
// in addition to the error code.
|
|
|
|
CauseCode cause_code = kNoCause;
|
|
|
|
|
2016-05-31 18:29:49 +02:00
|
|
|
bool is_retry = false;
|
2016-10-04 03:06:46 +02:00
|
|
|
};
|
2009-06-12 21:24:39 +02:00
|
|
|
|
2016-10-12 19:55:04 +02:00
|
|
|
enum ValueType {
|
|
|
|
VAL_INVALID = -1,
|
|
|
|
VAL_STRING = 1,
|
|
|
|
VAL_BLOB = 2,
|
|
|
|
};
|
2010-02-18 01:11:44 +01:00
|
|
|
|
2016-10-11 07:52:18 +02:00
|
|
|
struct Value {
|
2016-10-12 19:55:04 +02:00
|
|
|
ValueType type;
|
|
|
|
std::string data;
|
|
|
|
|
|
|
|
Value(ValueType type, const std::string& str) :
|
|
|
|
type(type),
|
|
|
|
data(str) {}
|
2016-10-11 07:52:18 +02:00
|
|
|
};
|
2010-02-18 01:11:44 +01:00
|
|
|
|
2016-10-11 07:52:18 +02:00
|
|
|
struct Expr;
|
|
|
|
|
2017-03-06 23:44:59 +01:00
|
|
|
using Function = Value* (*)(const char* name, State* state,
|
|
|
|
const std::vector<std::unique_ptr<Expr>>& argv);
|
2009-06-09 02:35:39 +02:00
|
|
|
|
|
|
|
struct Expr {
|
2017-03-06 23:44:59 +01:00
|
|
|
Function fn;
|
|
|
|
std::string name;
|
|
|
|
std::vector<std::unique_ptr<Expr>> argv;
|
|
|
|
int start, end;
|
|
|
|
|
|
|
|
Expr(Function fn, const std::string& name, int start, int end) :
|
|
|
|
fn(fn),
|
|
|
|
name(name),
|
|
|
|
start(start),
|
|
|
|
end(end) {}
|
2009-06-09 02:35:39 +02:00
|
|
|
};
|
|
|
|
|
2017-03-06 23:44:59 +01:00
|
|
|
// Evaluate the input expr, return the resulting Value.
|
|
|
|
Value* EvaluateValue(State* state, const std::unique_ptr<Expr>& expr);
|
2010-02-18 01:11:44 +01:00
|
|
|
|
2017-03-06 23:44:59 +01:00
|
|
|
// Evaluate the input expr, assert that it is a string, and update the result parameter. This
|
|
|
|
// function returns true if the evaluation succeeds. This is a convenience function for older
|
|
|
|
// functions that want to deal only with strings.
|
|
|
|
bool Evaluate(State* state, const std::unique_ptr<Expr>& expr, std::string* result);
|
2009-06-09 02:35:39 +02:00
|
|
|
|
|
|
|
// Glue to make an Expr out of a literal.
|
2017-03-06 23:44:59 +01:00
|
|
|
Value* Literal(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv);
|
2009-06-09 02:35:39 +02:00
|
|
|
|
|
|
|
// Functions corresponding to various syntactic sugar operators.
|
|
|
|
// ("concat" is also available as a builtin function, to concatenate
|
|
|
|
// more than two strings.)
|
2017-03-06 23:44:59 +01:00
|
|
|
Value* ConcatFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv);
|
|
|
|
Value* LogicalAndFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv);
|
|
|
|
Value* LogicalOrFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv);
|
|
|
|
Value* LogicalNotFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv);
|
|
|
|
Value* SubstringFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv);
|
|
|
|
Value* EqualityFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv);
|
|
|
|
Value* InequalityFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv);
|
|
|
|
Value* SequenceFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv);
|
2009-06-09 02:35:39 +02:00
|
|
|
|
|
|
|
// Global builtins, registered by RegisterBuiltins().
|
2017-03-06 23:44:59 +01:00
|
|
|
Value* IfElseFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv);
|
|
|
|
Value* AssertFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv);
|
|
|
|
Value* AbortFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv);
|
2009-06-09 02:35:39 +02:00
|
|
|
|
|
|
|
// Register a new function. The same Function may be registered under
|
|
|
|
// multiple names, but a given name should only be used once.
|
2016-10-11 07:52:18 +02:00
|
|
|
void RegisterFunction(const std::string& name, Function fn);
|
2009-06-09 02:35:39 +02:00
|
|
|
|
|
|
|
// Register all the builtins.
|
|
|
|
void RegisterBuiltins();
|
|
|
|
|
|
|
|
// Find the Function for a given name; return NULL if no such function
|
|
|
|
// exists.
|
2016-10-11 07:52:18 +02:00
|
|
|
Function FindFunction(const std::string& name);
|
2009-06-10 23:11:53 +02:00
|
|
|
|
|
|
|
// --- convenience functions for use in 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);
|
|
|
|
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
|
|
|
|
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,
|
|
|
|
std::vector<std::unique_ptr<Value>>* args);
|
|
|
|
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);
|
2010-02-18 01:11:44 +01:00
|
|
|
|
2009-06-18 19:11:50 +02:00
|
|
|
// Use printf-style arguments to compose an error message to put into
|
|
|
|
// *state. Returns NULL.
|
2016-04-30 20:49:59 +02:00
|
|
|
Value* ErrorAbort(State* state, const char* format, ...)
|
|
|
|
__attribute__((format(printf, 2, 3), deprecated));
|
|
|
|
|
|
|
|
// ErrorAbort has an optional (but recommended) argument 'cause_code'. If the cause code
|
|
|
|
// is set, it will be logged into last_install and provides reason of OTA failures.
|
|
|
|
Value* ErrorAbort(State* state, CauseCode cause_code, const char* format, ...)
|
|
|
|
__attribute__((format(printf, 3, 4)));
|
2010-02-18 01:11:44 +01:00
|
|
|
|
2016-10-12 19:55:04 +02:00
|
|
|
// Copying the string into a Value.
|
|
|
|
Value* StringValue(const char* str);
|
2009-06-18 19:11:50 +02:00
|
|
|
|
2016-10-12 19:55:04 +02:00
|
|
|
Value* StringValue(const std::string& str);
|
2009-06-10 23:11:53 +02:00
|
|
|
|
2017-03-06 23:44:59 +01:00
|
|
|
int parse_string(const char* str, std::unique_ptr<Expr>* root, int* error_count);
|
2014-02-14 00:07:56 +01:00
|
|
|
|
2009-06-09 02:35:39 +02:00
|
|
|
#endif // _EXPRESSION_H
|