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