blob: cd9c70120db0467f3889ef8bb0d113fc6c98da4f [file] [log] [blame]
Doug Zongker37bee622009-06-08 17:35:39 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef _EXPRESSION_H
18#define _EXPRESSION_H
19
Doug Zongker583fc122010-02-19 16:07:57 -080020#include <unistd.h>
Tianjie Xuc4447322017-03-06 14:44:59 -080021
22#include <memory>
Tao Bao59dcb9c2016-10-03 18:06:46 -070023#include <string>
Tianjie Xuc4447322017-03-06 14:44:59 -080024#include <vector>
Doug Zongker583fc122010-02-19 16:07:57 -080025
Tianjie Xu1536db82019-05-14 10:54:43 -070026#include "edify/updater_interface.h"
27
Tao Bao1fc5bf32017-10-06 07:43:41 -070028// Forward declaration to avoid including "otautil/error_code.h".
Tao Bao0bf20d52017-10-05 12:06:49 -070029enum ErrorCode : int;
30enum CauseCode : int;
Doug Zongker37bee622009-06-08 17:35:39 -070031
Tao Bao59dcb9c2016-10-03 18:06:46 -070032struct State {
Tianjie Xu1536db82019-05-14 10:54:43 -070033 State(const std::string& script, UpdaterInterface* cookie);
Tao Bao59dcb9c2016-10-03 18:06:46 -070034
Tao Bao0bf20d52017-10-05 12:06:49 -070035 // The source of the original script.
36 const std::string& script;
Tao Bao59dcb9c2016-10-03 18:06:46 -070037
Tianjie Xu1536db82019-05-14 10:54:43 -070038 // A pointer to app-specific data; the libedify doesn't use this value.
39 UpdaterInterface* updater;
Doug Zongkerd9c9d102009-06-12 12:24:39 -070040
Tao Bao0bf20d52017-10-05 12:06:49 -070041 // The error message (if any) returned if the evaluation aborts.
42 // Should be empty initially, will be either empty or a string that
43 // Evaluate() returns.
44 std::string errmsg;
Tianjie Xu16255832016-04-30 11:49:59 -070045
Tao Bao0bf20d52017-10-05 12:06:49 -070046 // error code indicates the type of failure (e.g. failure to update system image)
47 // during the OTA process.
48 ErrorCode error_code;
Tianjie Xu16255832016-04-30 11:49:59 -070049
Tao Bao0bf20d52017-10-05 12:06:49 -070050 // cause code provides more detailed reason of an OTA failure (e.g. fsync error)
51 // in addition to the error code.
52 CauseCode cause_code;
Tianjie Xu16255832016-04-30 11:49:59 -070053
Tao Bao0bf20d52017-10-05 12:06:49 -070054 bool is_retry = false;
Tao Bao59dcb9c2016-10-03 18:06:46 -070055};
Doug Zongkerd9c9d102009-06-12 12:24:39 -070056
Tao Bao39119ad2016-10-10 22:52:18 -070057struct Value {
Tao Bao511d7592018-06-19 15:56:49 -070058 enum class Type {
59 STRING = 1,
60 BLOB = 2,
61 };
Tianjie Xuaced5d92016-10-12 10:55:04 -070062
Tao Bao511d7592018-06-19 15:56:49 -070063 Value(Type type, const std::string& str) : type(type), data(str) {}
64
65 Type type;
66 std::string data;
Tao Bao39119ad2016-10-10 22:52:18 -070067};
Doug Zongker512536a2010-02-17 16:11:44 -080068
Tao Bao39119ad2016-10-10 22:52:18 -070069struct Expr;
70
Tianjie Xuc4447322017-03-06 14:44:59 -080071using Function = Value* (*)(const char* name, State* state,
72 const std::vector<std::unique_ptr<Expr>>& argv);
Doug Zongker37bee622009-06-08 17:35:39 -070073
74struct Expr {
Tianjie Xuc4447322017-03-06 14:44:59 -080075 Function fn;
76 std::string name;
77 std::vector<std::unique_ptr<Expr>> argv;
78 int start, end;
79
80 Expr(Function fn, const std::string& name, int start, int end) :
81 fn(fn),
82 name(name),
83 start(start),
84 end(end) {}
Doug Zongker37bee622009-06-08 17:35:39 -070085};
86
Tianjie Xuc4447322017-03-06 14:44:59 -080087// Evaluate the input expr, return the resulting Value.
88Value* EvaluateValue(State* state, const std::unique_ptr<Expr>& expr);
Doug Zongker512536a2010-02-17 16:11:44 -080089
Tianjie Xuc4447322017-03-06 14:44:59 -080090// Evaluate the input expr, assert that it is a string, and update the result parameter. This
91// function returns true if the evaluation succeeds. This is a convenience function for older
92// functions that want to deal only with strings.
93bool Evaluate(State* state, const std::unique_ptr<Expr>& expr, std::string* result);
Doug Zongker37bee622009-06-08 17:35:39 -070094
95// Glue to make an Expr out of a literal.
Tianjie Xuc4447322017-03-06 14:44:59 -080096Value* Literal(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv);
Doug Zongker37bee622009-06-08 17:35:39 -070097
98// Functions corresponding to various syntactic sugar operators.
99// ("concat" is also available as a builtin function, to concatenate
100// more than two strings.)
Tianjie Xuc4447322017-03-06 14:44:59 -0800101Value* ConcatFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv);
102Value* LogicalAndFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv);
103Value* LogicalOrFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv);
104Value* LogicalNotFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv);
105Value* SubstringFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv);
106Value* EqualityFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv);
107Value* InequalityFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv);
108Value* SequenceFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv);
Doug Zongker37bee622009-06-08 17:35:39 -0700109
Doug Zongker37bee622009-06-08 17:35:39 -0700110// Global builtins, registered by RegisterBuiltins().
Tianjie Xuc4447322017-03-06 14:44:59 -0800111Value* IfElseFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv);
112Value* AssertFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv);
113Value* AbortFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv);
Doug Zongker37bee622009-06-08 17:35:39 -0700114
Doug Zongker37bee622009-06-08 17:35:39 -0700115// Register a new function. The same Function may be registered under
116// multiple names, but a given name should only be used once.
Tao Bao39119ad2016-10-10 22:52:18 -0700117void RegisterFunction(const std::string& name, Function fn);
Doug Zongker37bee622009-06-08 17:35:39 -0700118
119// Register all the builtins.
120void RegisterBuiltins();
121
Doug Zongker37bee622009-06-08 17:35:39 -0700122// Find the Function for a given name; return NULL if no such function
123// exists.
Tao Bao39119ad2016-10-10 22:52:18 -0700124Function FindFunction(const std::string& name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700125
126// --- convenience functions for use in functions ---
127
Tianjie Xuc4447322017-03-06 14:44:59 -0800128// Evaluate the expressions in argv, and put the results of strings in args. If any expression
129// evaluates to nullptr, return false. Return true on success.
130bool ReadArgs(State* state, const std::vector<std::unique_ptr<Expr>>& argv,
131 std::vector<std::string>* args);
132bool ReadArgs(State* state, const std::vector<std::unique_ptr<Expr>>& argv,
133 std::vector<std::string>* args, size_t start, size_t len);
Tianjie Xuaced5d92016-10-12 10:55:04 -0700134
Tianjie Xuc4447322017-03-06 14:44:59 -0800135// Evaluate the expressions in argv, and put the results of Value* in args. If any
136// expression evaluate to nullptr, return false. Return true on success.
137bool ReadValueArgs(State* state, const std::vector<std::unique_ptr<Expr>>& argv,
138 std::vector<std::unique_ptr<Value>>* args);
139bool ReadValueArgs(State* state, const std::vector<std::unique_ptr<Expr>>& argv,
140 std::vector<std::unique_ptr<Value>>* args, size_t start, size_t len);
Doug Zongker512536a2010-02-17 16:11:44 -0800141
Doug Zongker47cace92009-06-18 10:11:50 -0700142// Use printf-style arguments to compose an error message to put into
143// *state. Returns NULL.
Tianjie Xu16255832016-04-30 11:49:59 -0700144Value* ErrorAbort(State* state, const char* format, ...)
145 __attribute__((format(printf, 2, 3), deprecated));
146
147// ErrorAbort has an optional (but recommended) argument 'cause_code'. If the cause code
148// is set, it will be logged into last_install and provides reason of OTA failures.
149Value* ErrorAbort(State* state, CauseCode cause_code, const char* format, ...)
150 __attribute__((format(printf, 3, 4)));
Doug Zongker47cace92009-06-18 10:11:50 -0700151
Tianjie Xuaced5d92016-10-12 10:55:04 -0700152// Copying the string into a Value.
153Value* StringValue(const char* str);
Doug Zongker512536a2010-02-17 16:11:44 -0800154
Tianjie Xuaced5d92016-10-12 10:55:04 -0700155Value* StringValue(const std::string& str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700156
Tao Baod8d514f2018-07-09 13:32:28 -0700157int ParseString(const std::string& str, std::unique_ptr<Expr>* root, int* error_count);
Doug Zongker0d32f252014-02-13 15:07:56 -0800158
Doug Zongker37bee622009-06-08 17:35:39 -0700159#endif // _EXPRESSION_H