blob: 85306542d268e96dd4f800dfe4955793ed7a97d2 [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>
Tao Bao59dcb9c2016-10-03 18:06:46 -070021#include <string>
Doug Zongker583fc122010-02-19 16:07:57 -080022
Tianjie Xu16255832016-04-30 11:49:59 -070023#include "error_code.h"
Doug Zongker37bee622009-06-08 17:35:39 -070024
Tao Bao59dcb9c2016-10-03 18:06:46 -070025struct State {
26 State(const std::string& script, void* cookie);
27
28 // The source of the original script.
29 const std::string& script;
30
Doug Zongkerd9c9d102009-06-12 12:24:39 -070031 // Optional pointer to app-specific data; the core of edify never
32 // uses this value.
33 void* cookie;
34
Doug Zongkerd9c9d102009-06-12 12:24:39 -070035 // The error message (if any) returned if the evaluation aborts.
Tao Bao59dcb9c2016-10-03 18:06:46 -070036 // Should be empty initially, will be either empty or a string that
37 // Evaluate() returns.
38 std::string errmsg;
Tianjie Xu16255832016-04-30 11:49:59 -070039
40 // error code indicates the type of failure (e.g. failure to update system image)
41 // during the OTA process.
42 ErrorCode error_code = kNoError;
43
44 // cause code provides more detailed reason of an OTA failure (e.g. fsync error)
45 // in addition to the error code.
46 CauseCode cause_code = kNoCause;
47
Tianjie Xu7ce287d2016-05-31 09:29:49 -070048 bool is_retry = false;
Tao Bao59dcb9c2016-10-03 18:06:46 -070049};
Doug Zongkerd9c9d102009-06-12 12:24:39 -070050
Tianjie Xuaced5d92016-10-12 10:55:04 -070051enum ValueType {
52 VAL_INVALID = -1,
53 VAL_STRING = 1,
54 VAL_BLOB = 2,
55};
Doug Zongker512536a2010-02-17 16:11:44 -080056
Tao Bao39119ad2016-10-10 22:52:18 -070057struct Value {
Tianjie Xuaced5d92016-10-12 10:55:04 -070058 ValueType type;
59 std::string data;
60
61 Value(ValueType type, const std::string& str) :
62 type(type),
63 data(str) {}
Tao Bao39119ad2016-10-10 22:52:18 -070064};
Doug Zongker512536a2010-02-17 16:11:44 -080065
Tao Bao39119ad2016-10-10 22:52:18 -070066struct Expr;
67
68using Function = Value* (*)(const char* name, State* state, int argc, Expr* argv[]);
Doug Zongker37bee622009-06-08 17:35:39 -070069
70struct Expr {
Doug Zongkerd9c9d102009-06-12 12:24:39 -070071 Function fn;
Tao Bao2a5a49d2015-08-20 12:10:46 -070072 const char* name;
Doug Zongkerd9c9d102009-06-12 12:24:39 -070073 int argc;
74 Expr** argv;
75 int start, end;
Doug Zongker37bee622009-06-08 17:35:39 -070076};
77
Doug Zongker512536a2010-02-17 16:11:44 -080078// Take one of the Expr*s passed to the function as an argument,
79// evaluate it, return the resulting Value. The caller takes
80// ownership of the returned Value.
81Value* EvaluateValue(State* state, Expr* expr);
82
83// Take one of the Expr*s passed to the function as an argument,
Tianjie Xuaced5d92016-10-12 10:55:04 -070084// evaluate it, assert that it is a string, and update the result
85// parameter. This function returns true if the evaluation succeeds.
86// This is a convenience function for older functions that want to
87// deal only with strings.
88bool Evaluate(State* state, Expr* expr, std::string* result);
Doug Zongker37bee622009-06-08 17:35:39 -070089
90// Glue to make an Expr out of a literal.
Doug Zongker512536a2010-02-17 16:11:44 -080091Value* Literal(const char* name, State* state, int argc, Expr* argv[]);
Doug Zongker37bee622009-06-08 17:35:39 -070092
93// Functions corresponding to various syntactic sugar operators.
94// ("concat" is also available as a builtin function, to concatenate
95// more than two strings.)
Doug Zongker512536a2010-02-17 16:11:44 -080096Value* ConcatFn(const char* name, State* state, int argc, Expr* argv[]);
97Value* LogicalAndFn(const char* name, State* state, int argc, Expr* argv[]);
98Value* LogicalOrFn(const char* name, State* state, int argc, Expr* argv[]);
99Value* LogicalNotFn(const char* name, State* state, int argc, Expr* argv[]);
100Value* SubstringFn(const char* name, State* state, int argc, Expr* argv[]);
101Value* EqualityFn(const char* name, State* state, int argc, Expr* argv[]);
102Value* InequalityFn(const char* name, State* state, int argc, Expr* argv[]);
103Value* SequenceFn(const char* name, State* state, int argc, Expr* argv[]);
Doug Zongker37bee622009-06-08 17:35:39 -0700104
Doug Zongker37bee622009-06-08 17:35:39 -0700105// Global builtins, registered by RegisterBuiltins().
Doug Zongker512536a2010-02-17 16:11:44 -0800106Value* IfElseFn(const char* name, State* state, int argc, Expr* argv[]);
107Value* AssertFn(const char* name, State* state, int argc, Expr* argv[]);
108Value* AbortFn(const char* name, State* state, int argc, Expr* argv[]);
Doug Zongker37bee622009-06-08 17:35:39 -0700109
Doug Zongker37bee622009-06-08 17:35:39 -0700110// Register a new function. The same Function may be registered under
111// multiple names, but a given name should only be used once.
Tao Bao39119ad2016-10-10 22:52:18 -0700112void RegisterFunction(const std::string& name, Function fn);
Doug Zongker37bee622009-06-08 17:35:39 -0700113
114// Register all the builtins.
115void RegisterBuiltins();
116
Doug Zongker37bee622009-06-08 17:35:39 -0700117// Find the Function for a given name; return NULL if no such function
118// exists.
Tao Bao39119ad2016-10-10 22:52:18 -0700119Function FindFunction(const std::string& name);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700120
121// --- convenience functions for use in functions ---
122
Tianjie Xuaced5d92016-10-12 10:55:04 -0700123// Evaluate the expressions in argv, and put the results of strings in
124// args. If any expression evaluates to nullptr, free the rest and return
125// false. Return true on success.
126bool ReadArgs(State* state, int argc, Expr* argv[], std::vector<std::string>* args);
127
128// Evaluate the expressions in argv, and put the results of Value* in
129// args. If any expression evaluate to nullptr, free the rest and return
130// false. Return true on success.
131bool ReadValueArgs(State* state, int argc, Expr* argv[],
132 std::vector<std::unique_ptr<Value>>* args);
133
Doug Zongker9931f7f2009-06-10 14:11:53 -0700134// Evaluate the expressions in argv, giving 'count' char* (the ... is
135// zero or more char** to put them in). If any expression evaluates
136// to NULL, free the rest and return -1. Return 0 on success.
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700137int ReadArgs(State* state, Expr* argv[], int count, ...);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700138
Doug Zongker512536a2010-02-17 16:11:44 -0800139// Evaluate the expressions in argv, giving 'count' Value* (the ... is
140// zero or more Value** to put them in). If any expression evaluates
141// to NULL, free the rest and return -1. Return 0 on success.
142int ReadValueArgs(State* state, Expr* argv[], int count, ...);
143
Doug Zongker9931f7f2009-06-10 14:11:53 -0700144// Evaluate the expressions in argv, returning an array of char*
145// results. If any evaluate to NULL, free the rest and return NULL.
146// The caller is responsible for freeing the returned array and the
147// strings it contains.
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700148char** ReadVarArgs(State* state, int argc, Expr* argv[]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700149
Doug Zongker512536a2010-02-17 16:11:44 -0800150// Evaluate the expressions in argv, returning an array of Value*
151// results. If any evaluate to NULL, free the rest and return NULL.
152// The caller is responsible for freeing the returned array and the
153// Values it contains.
154Value** ReadValueVarArgs(State* state, int argc, Expr* argv[]);
155
Doug Zongker47cace92009-06-18 10:11:50 -0700156// Use printf-style arguments to compose an error message to put into
157// *state. Returns NULL.
Tianjie Xu16255832016-04-30 11:49:59 -0700158Value* ErrorAbort(State* state, const char* format, ...)
159 __attribute__((format(printf, 2, 3), deprecated));
160
161// ErrorAbort has an optional (but recommended) argument 'cause_code'. If the cause code
162// is set, it will be logged into last_install and provides reason of OTA failures.
163Value* ErrorAbort(State* state, CauseCode cause_code, const char* format, ...)
164 __attribute__((format(printf, 3, 4)));
Doug Zongker47cace92009-06-18 10:11:50 -0700165
Tianjie Xuaced5d92016-10-12 10:55:04 -0700166// Copying the string into a Value.
167Value* StringValue(const char* str);
Doug Zongker512536a2010-02-17 16:11:44 -0800168
Tianjie Xuaced5d92016-10-12 10:55:04 -0700169Value* StringValue(const std::string& str);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700170
Doug Zongker0d32f252014-02-13 15:07:56 -0800171int parse_string(const char* str, Expr** root, int* error_count);
172
Doug Zongker37bee622009-06-08 17:35:39 -0700173#endif // _EXPRESSION_H