aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTao Bao2017-10-04 11:33:01 -0500
committerTao Bao2017-10-09 16:08:00 -0500
commite6f7f95d3493b47514a6104e11d206a455a6433e (patch)
tree69a455f8598c31d11fa55a50975fdafef98ce93b /edify/include
parent1fc5bf353a8719d16fd9ba29a661d211bad4038f (diff)
downloadplatform-bootable-recovery-e6f7f95d3493b47514a6104e11d206a455a6433e.tar.gz
platform-bootable-recovery-e6f7f95d3493b47514a6104e11d206a455a6433e.tar.xz
platform-bootable-recovery-e6f7f95d3493b47514a6104e11d206a455a6433e.zip
edify: Export the header and move to Soong.
Also make matching changes to applypatch modules which include edify/expr.h. Test: mmma bootable/recovery Change-Id: Ia72be3caa010d7f56a70add2da345e631b306378
Diffstat (limited to 'edify/include')
-rw-r--r--edify/include/edify/expr.h161
1 files changed, 161 insertions, 0 deletions
diff --git a/edify/include/edify/expr.h b/edify/include/edify/expr.h
new file mode 100644
index 00000000..770d1cf0
--- /dev/null
+++ b/edify/include/edify/expr.h
@@ -0,0 +1,161 @@
1/*
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
20#include <unistd.h>
21
22#include <memory>
23#include <string>
24#include <vector>
25
26// Forward declaration to avoid including "otautil/error_code.h".
27enum ErrorCode : int;
28enum CauseCode : int;
29
30struct State {
31 State(const std::string& script, void* cookie);
32
33 // The source of the original script.
34 const std::string& script;
35
36 // Optional pointer to app-specific data; the core of edify never
37 // uses this value.
38 void* cookie;
39
40 // The error message (if any) returned if the evaluation aborts.
41 // Should be empty initially, will be either empty or a string that
42 // Evaluate() returns.
43 std::string errmsg;
44
45 // error code indicates the type of failure (e.g. failure to update system image)
46 // during the OTA process.
47 ErrorCode error_code;
48
49 // cause code provides more detailed reason of an OTA failure (e.g. fsync error)
50 // in addition to the error code.
51 CauseCode cause_code;
52
53 bool is_retry = false;
54};
55
56enum ValueType {
57 VAL_INVALID = -1,
58 VAL_STRING = 1,
59 VAL_BLOB = 2,
60};
61
62struct Value {
63 ValueType type;
64 std::string data;
65
66 Value(ValueType type, const std::string& str) :
67 type(type),
68 data(str) {}
69};
70
71struct Expr;
72
73using Function = Value* (*)(const char* name, State* state,
74 const std::vector<std::unique_ptr<Expr>>& argv);
75
76struct Expr {
77 Function fn;
78 std::string name;
79 std::vector<std::unique_ptr<Expr>> argv;
80 int start, end;
81
82 Expr(Function fn, const std::string& name, int start, int end) :
83 fn(fn),
84 name(name),
85 start(start),
86 end(end) {}
87};
88
89// Evaluate the input expr, return the resulting Value.
90Value* EvaluateValue(State* state, const std::unique_ptr<Expr>& expr);
91
92// Evaluate the input expr, assert that it is a string, and update the result parameter. This
93// function returns true if the evaluation succeeds. This is a convenience function for older
94// functions that want to deal only with strings.
95bool Evaluate(State* state, const std::unique_ptr<Expr>& expr, std::string* result);
96
97// Glue to make an Expr out of a literal.
98Value* Literal(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv);
99
100// Functions corresponding to various syntactic sugar operators.
101// ("concat" is also available as a builtin function, to concatenate
102// more than two strings.)
103Value* ConcatFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv);
104Value* LogicalAndFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv);
105Value* LogicalOrFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv);
106Value* LogicalNotFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv);
107Value* SubstringFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv);
108Value* EqualityFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv);
109Value* InequalityFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv);
110Value* SequenceFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv);
111
112// Global builtins, registered by RegisterBuiltins().
113Value* IfElseFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv);
114Value* AssertFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv);
115Value* AbortFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv);
116
117// Register a new function. The same Function may be registered under
118// multiple names, but a given name should only be used once.
119void RegisterFunction(const std::string& name, Function fn);
120
121// Register all the builtins.
122void RegisterBuiltins();
123
124// Find the Function for a given name; return NULL if no such function
125// exists.
126Function FindFunction(const std::string& name);
127
128// --- convenience functions for use in functions ---
129
130// Evaluate the expressions in argv, and put the results of strings in args. If any expression
131// evaluates to nullptr, return false. Return true on success.
132bool ReadArgs(State* state, const std::vector<std::unique_ptr<Expr>>& argv,
133 std::vector<std::string>* args);
134bool ReadArgs(State* state, const std::vector<std::unique_ptr<Expr>>& argv,
135 std::vector<std::string>* args, size_t start, size_t len);
136
137// Evaluate the expressions in argv, and put the results of Value* in args. If any
138// expression evaluate to nullptr, return false. Return true on success.
139bool ReadValueArgs(State* state, const std::vector<std::unique_ptr<Expr>>& argv,
140 std::vector<std::unique_ptr<Value>>* args);
141bool ReadValueArgs(State* state, const std::vector<std::unique_ptr<Expr>>& argv,
142 std::vector<std::unique_ptr<Value>>* args, size_t start, size_t len);
143
144// Use printf-style arguments to compose an error message to put into
145// *state. Returns NULL.
146Value* ErrorAbort(State* state, const char* format, ...)
147 __attribute__((format(printf, 2, 3), deprecated));
148
149// ErrorAbort has an optional (but recommended) argument 'cause_code'. If the cause code
150// is set, it will be logged into last_install and provides reason of OTA failures.
151Value* ErrorAbort(State* state, CauseCode cause_code, const char* format, ...)
152 __attribute__((format(printf, 3, 4)));
153
154// Copying the string into a Value.
155Value* StringValue(const char* str);
156
157Value* StringValue(const std::string& str);
158
159int parse_string(const char* str, std::unique_ptr<Expr>* root, int* error_count);
160
161#endif // _EXPRESSION_H