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> |
Tao Bao | 59dcb9c | 2016-10-03 18:06:46 -0700 | [diff] [blame] | 21 | #include <string> |
Doug Zongker | 583fc12 | 2010-02-19 16:07:57 -0800 | [diff] [blame] | 22 | |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 23 | #include "error_code.h" |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 24 | |
Tao Bao | 59dcb9c | 2016-10-03 18:06:46 -0700 | [diff] [blame] | 25 | struct State { |
| 26 | State(const std::string& script, void* cookie); |
| 27 | |
| 28 | // The source of the original script. |
| 29 | const std::string& script; |
| 30 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 31 | // Optional pointer to app-specific data; the core of edify never |
| 32 | // uses this value. |
| 33 | void* cookie; |
| 34 | |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 35 | // The error message (if any) returned if the evaluation aborts. |
Tao Bao | 59dcb9c | 2016-10-03 18:06:46 -0700 | [diff] [blame] | 36 | // Should be empty initially, will be either empty or a string that |
| 37 | // Evaluate() returns. |
| 38 | std::string errmsg; |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 39 | |
| 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 Xu | 7ce287d | 2016-05-31 09:29:49 -0700 | [diff] [blame] | 48 | bool is_retry = false; |
Tao Bao | 59dcb9c | 2016-10-03 18:06:46 -0700 | [diff] [blame] | 49 | }; |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 50 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 51 | #define VAL_STRING 1 // data will be NULL-terminated; size doesn't count null |
| 52 | #define VAL_BLOB 2 |
| 53 | |
Tao Bao | 39119ad | 2016-10-10 22:52:18 -0700 | [diff] [blame] | 54 | struct Value { |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 55 | int type; |
| 56 | ssize_t size; |
| 57 | char* data; |
Tao Bao | 39119ad | 2016-10-10 22:52:18 -0700 | [diff] [blame] | 58 | }; |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 59 | |
Tao Bao | 39119ad | 2016-10-10 22:52:18 -0700 | [diff] [blame] | 60 | struct Expr; |
| 61 | |
| 62 | using Function = Value* (*)(const char* name, State* state, int argc, Expr* argv[]); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 63 | |
| 64 | struct Expr { |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 65 | Function fn; |
Tao Bao | 2a5a49d | 2015-08-20 12:10:46 -0700 | [diff] [blame] | 66 | const char* name; |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 67 | int argc; |
| 68 | Expr** argv; |
| 69 | int start, end; |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 70 | }; |
| 71 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 72 | // Take one of the Expr*s passed to the function as an argument, |
| 73 | // evaluate it, return the resulting Value. The caller takes |
| 74 | // ownership of the returned Value. |
| 75 | Value* EvaluateValue(State* state, Expr* expr); |
| 76 | |
| 77 | // Take one of the Expr*s passed to the function as an argument, |
| 78 | // evaluate it, assert that it is a string, and return the resulting |
| 79 | // char*. The caller takes ownership of the returned char*. This is |
| 80 | // a convenience function for older functions that want to deal only |
| 81 | // with strings. |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 82 | char* Evaluate(State* state, Expr* expr); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 83 | |
| 84 | // Glue to make an Expr out of a literal. |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 85 | Value* Literal(const char* name, State* state, int argc, Expr* argv[]); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 86 | |
| 87 | // Functions corresponding to various syntactic sugar operators. |
| 88 | // ("concat" is also available as a builtin function, to concatenate |
| 89 | // more than two strings.) |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 90 | Value* ConcatFn(const char* name, State* state, int argc, Expr* argv[]); |
| 91 | Value* LogicalAndFn(const char* name, State* state, int argc, Expr* argv[]); |
| 92 | Value* LogicalOrFn(const char* name, State* state, int argc, Expr* argv[]); |
| 93 | Value* LogicalNotFn(const char* name, State* state, int argc, Expr* argv[]); |
| 94 | Value* SubstringFn(const char* name, State* state, int argc, Expr* argv[]); |
| 95 | Value* EqualityFn(const char* name, State* state, int argc, Expr* argv[]); |
| 96 | Value* InequalityFn(const char* name, State* state, int argc, Expr* argv[]); |
| 97 | Value* SequenceFn(const char* name, State* state, int argc, Expr* argv[]); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 98 | |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 99 | // Global builtins, registered by RegisterBuiltins(). |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 100 | Value* IfElseFn(const char* name, State* state, int argc, Expr* argv[]); |
| 101 | Value* AssertFn(const char* name, State* state, int argc, Expr* argv[]); |
| 102 | Value* AbortFn(const char* name, State* state, int argc, Expr* argv[]); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 103 | |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 104 | // Register a new function. The same Function may be registered under |
| 105 | // multiple names, but a given name should only be used once. |
Tao Bao | 39119ad | 2016-10-10 22:52:18 -0700 | [diff] [blame] | 106 | void RegisterFunction(const std::string& name, Function fn); |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 107 | |
| 108 | // Register all the builtins. |
| 109 | void RegisterBuiltins(); |
| 110 | |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 111 | // Find the Function for a given name; return NULL if no such function |
| 112 | // exists. |
Tao Bao | 39119ad | 2016-10-10 22:52:18 -0700 | [diff] [blame] | 113 | Function FindFunction(const std::string& name); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 114 | |
| 115 | // --- convenience functions for use in functions --- |
| 116 | |
| 117 | // Evaluate the expressions in argv, giving 'count' char* (the ... is |
| 118 | // zero or more char** to put them in). If any expression evaluates |
| 119 | // to NULL, free the rest and return -1. Return 0 on success. |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 120 | int ReadArgs(State* state, Expr* argv[], int count, ...); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 121 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 122 | // Evaluate the expressions in argv, giving 'count' Value* (the ... is |
| 123 | // zero or more Value** to put them in). If any expression evaluates |
| 124 | // to NULL, free the rest and return -1. Return 0 on success. |
| 125 | int ReadValueArgs(State* state, Expr* argv[], int count, ...); |
| 126 | |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 127 | // Evaluate the expressions in argv, returning an array of char* |
| 128 | // results. If any evaluate to NULL, free the rest and return NULL. |
| 129 | // The caller is responsible for freeing the returned array and the |
| 130 | // strings it contains. |
Doug Zongker | d9c9d10 | 2009-06-12 12:24:39 -0700 | [diff] [blame] | 131 | char** ReadVarArgs(State* state, int argc, Expr* argv[]); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 132 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 133 | // Evaluate the expressions in argv, returning an array of Value* |
| 134 | // results. If any evaluate to NULL, free the rest and return NULL. |
| 135 | // The caller is responsible for freeing the returned array and the |
| 136 | // Values it contains. |
| 137 | Value** ReadValueVarArgs(State* state, int argc, Expr* argv[]); |
| 138 | |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 139 | // Use printf-style arguments to compose an error message to put into |
| 140 | // *state. Returns NULL. |
Tianjie Xu | 1625583 | 2016-04-30 11:49:59 -0700 | [diff] [blame] | 141 | Value* ErrorAbort(State* state, const char* format, ...) |
| 142 | __attribute__((format(printf, 2, 3), deprecated)); |
| 143 | |
| 144 | // ErrorAbort has an optional (but recommended) argument 'cause_code'. If the cause code |
| 145 | // is set, it will be logged into last_install and provides reason of OTA failures. |
| 146 | Value* ErrorAbort(State* state, CauseCode cause_code, const char* format, ...) |
| 147 | __attribute__((format(printf, 3, 4))); |
Doug Zongker | 47cace9 | 2009-06-18 10:11:50 -0700 | [diff] [blame] | 148 | |
Doug Zongker | 512536a | 2010-02-17 16:11:44 -0800 | [diff] [blame] | 149 | // Wrap a string into a Value, taking ownership of the string. |
| 150 | Value* StringValue(char* str); |
| 151 | |
| 152 | // Free a Value object. |
| 153 | void FreeValue(Value* v); |
Doug Zongker | 9931f7f | 2009-06-10 14:11:53 -0700 | [diff] [blame] | 154 | |
Doug Zongker | 0d32f25 | 2014-02-13 15:07:56 -0800 | [diff] [blame] | 155 | int parse_string(const char* str, Expr** root, int* error_count); |
| 156 | |
Doug Zongker | 37bee62 | 2009-06-08 17:35:39 -0700 | [diff] [blame] | 157 | #endif // _EXPRESSION_H |