Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 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 | |
Doug Zongker | 583fc12 | 2010-02-19 16:07:57 -0800 | [diff] [blame] | 20 | #include <unistd.h> |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 21 | |
| 22 | #include <memory> |
Tao Bao | 59dcb9c | 2016-10-03 18:06:46 -0700 | [diff] [blame] | 23 | #include <string> |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 24 | #include <vector> |
Doug Zongker | 583fc12 | 2010-02-19 16:07:57 -0800 | [diff] [blame] | 25 | |
Tao Bao | 1fc5bf3 | 2017-10-06 07:43:41 -0700 | [diff] [blame] | 26 | // Forward declaration to avoid including "otautil/error_code.h". |
Tao Bao | 0bf20d5 | 2017-10-05 12:06:49 -0700 | [diff] [blame] | 27 | enum ErrorCode : int; |
| 28 | enum CauseCode : int; |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 29 | |
Tao Bao | 59dcb9c | 2016-10-03 18:06:46 -0700 | [diff] [blame] | 30 | struct State { |
Tao Bao | 0bf20d5 | 2017-10-05 12:06:49 -0700 | [diff] [blame] | 31 | State(const std::string& script, void* cookie); |
Tao Bao | 59dcb9c | 2016-10-03 18:06:46 -0700 | [diff] [blame] | 32 | |
Tao Bao | 0bf20d5 | 2017-10-05 12:06:49 -0700 | [diff] [blame] | 33 | // The source of the original script. |
| 34 | const std::string& script; |
Tao Bao | 59dcb9c | 2016-10-03 18:06:46 -0700 | [diff] [blame] | 35 | |
Tao Bao | 0bf20d5 | 2017-10-05 12:06:49 -0700 | [diff] [blame] | 36 | // Optional pointer to app-specific data; the core of edify never |
| 37 | // uses this value. |
| 38 | void* cookie; |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 39 | |
Tao Bao | 0bf20d5 | 2017-10-05 12:06:49 -0700 | [diff] [blame] | 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; |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 44 | |
Tao Bao | 0bf20d5 | 2017-10-05 12:06:49 -0700 | [diff] [blame] | 45 | // error code indicates the type of failure (e.g. failure to update system image) |
| 46 | // during the OTA process. |
| 47 | ErrorCode error_code; |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 48 | |
Tao Bao | 0bf20d5 | 2017-10-05 12:06:49 -0700 | [diff] [blame] | 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; |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 52 | |
Tao Bao | 0bf20d5 | 2017-10-05 12:06:49 -0700 | [diff] [blame] | 53 | bool is_retry = false; |
Tao Bao | 59dcb9c | 2016-10-03 18:06:46 -0700 | [diff] [blame] | 54 | }; |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 55 | |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 56 | enum ValueType { |
| 57 | VAL_INVALID = -1, |
| 58 | VAL_STRING = 1, |
| 59 | VAL_BLOB = 2, |
| 60 | }; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 61 | |
Tao Bao | 39119ad | 2016-10-10 22:52:18 -0700 | [diff] [blame] | 62 | struct Value { |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 63 | ValueType type; |
| 64 | std::string data; |
| 65 | |
| 66 | Value(ValueType type, const std::string& str) : |
| 67 | type(type), |
| 68 | data(str) {} |
Tao Bao | 39119ad | 2016-10-10 22:52:18 -0700 | [diff] [blame] | 69 | }; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 70 | |
Tao Bao | 39119ad | 2016-10-10 22:52:18 -0700 | [diff] [blame] | 71 | struct Expr; |
| 72 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 73 | using Function = Value* (*)(const char* name, State* state, |
| 74 | const std::vector<std::unique_ptr<Expr>>& argv); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 75 | |
| 76 | struct Expr { |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 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) {} |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 87 | }; |
| 88 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 89 | // Evaluate the input expr, return the resulting Value. |
| 90 | Value* EvaluateValue(State* state, const std::unique_ptr<Expr>& expr); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 91 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 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. |
| 95 | bool Evaluate(State* state, const std::unique_ptr<Expr>& expr, std::string* result); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 96 | |
| 97 | // Glue to make an Expr out of a literal. |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 98 | Value* Literal(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 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.) |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 103 | Value* ConcatFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv); |
| 104 | Value* LogicalAndFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv); |
| 105 | Value* LogicalOrFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv); |
| 106 | Value* LogicalNotFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv); |
| 107 | Value* SubstringFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv); |
| 108 | Value* EqualityFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv); |
| 109 | Value* InequalityFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv); |
| 110 | Value* SequenceFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 111 | |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 112 | // Global builtins, registered by RegisterBuiltins(). |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 113 | Value* IfElseFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv); |
| 114 | Value* AssertFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv); |
| 115 | Value* AbortFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 116 | |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 117 | // Register a new function. The same Function may be registered under |
| 118 | // multiple names, but a given name should only be used once. |
Tao Bao | 39119ad | 2016-10-10 22:52:18 -0700 | [diff] [blame] | 119 | void RegisterFunction(const std::string& name, Function fn); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 120 | |
| 121 | // Register all the builtins. |
| 122 | void RegisterBuiltins(); |
| 123 | |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 124 | // Find the Function for a given name; return NULL if no such function |
| 125 | // exists. |
Tao Bao | 39119ad | 2016-10-10 22:52:18 -0700 | [diff] [blame] | 126 | Function FindFunction(const std::string& name); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 127 | |
| 128 | // --- convenience functions for use in functions --- |
| 129 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 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. |
| 132 | bool ReadArgs(State* state, const std::vector<std::unique_ptr<Expr>>& argv, |
| 133 | std::vector<std::string>* args); |
| 134 | bool ReadArgs(State* state, const std::vector<std::unique_ptr<Expr>>& argv, |
| 135 | std::vector<std::string>* args, size_t start, size_t len); |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 136 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 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. |
| 139 | bool ReadValueArgs(State* state, const std::vector<std::unique_ptr<Expr>>& argv, |
| 140 | std::vector<std::unique_ptr<Value>>* args); |
| 141 | bool 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); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 143 | |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 144 | // Use printf-style arguments to compose an error message to put into |
| 145 | // *state. Returns NULL. |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 146 | Value* 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. |
| 151 | Value* ErrorAbort(State* state, CauseCode cause_code, const char* format, ...) |
| 152 | __attribute__((format(printf, 3, 4))); |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 153 | |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 154 | // Copying the string into a Value. |
| 155 | Value* StringValue(const char* str); |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 156 | |
Tianjie Xu | aced5d9 | 2016-10-12 10:55:04 -0700 | [diff] [blame] | 157 | Value* StringValue(const std::string& str); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 158 | |
Tianjie Xu | c444732 | 2017-03-06 14:44:59 -0800 | [diff] [blame] | 159 | int parse_string(const char* str, std::unique_ptr<Expr>* root, int* error_count); |
Doug Zongker | 0d32f25 | 2014-02-13 15:07:56 -0800 | [diff] [blame] | 160 | |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 161 | #endif // _EXPRESSION_H |