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". 27 enum ErrorCode : int; 28 enum CauseCode : int; 29 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 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 56 struct Value { 57 enum class Type { 58 STRING = 1, 59 BLOB = 2, 60 }; 61 ValueValue62 Value(Type type, const std::string& str) : type(type), data(str) {} 63 64 Type type; 65 std::string data; 66 }; 67 68 struct Expr; 69 70 using Function = Value* (*)(const char* name, State* state, 71 const std::vector<std::unique_ptr<Expr>>& argv); 72 73 struct Expr { 74 Function fn; 75 std::string name; 76 std::vector<std::unique_ptr<Expr>> argv; 77 int start, end; 78 ExprExpr79 Expr(Function fn, const std::string& name, int start, int end) : 80 fn(fn), 81 name(name), 82 start(start), 83 end(end) {} 84 }; 85 86 // Evaluate the input expr, return the resulting Value. 87 Value* EvaluateValue(State* state, const std::unique_ptr<Expr>& expr); 88 89 // Evaluate the input expr, assert that it is a string, and update the result parameter. This 90 // function returns true if the evaluation succeeds. This is a convenience function for older 91 // functions that want to deal only with strings. 92 bool Evaluate(State* state, const std::unique_ptr<Expr>& expr, std::string* result); 93 94 // Glue to make an Expr out of a literal. 95 Value* Literal(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv); 96 97 // Functions corresponding to various syntactic sugar operators. 98 // ("concat" is also available as a builtin function, to concatenate 99 // more than two strings.) 100 Value* ConcatFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv); 101 Value* LogicalAndFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv); 102 Value* LogicalOrFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv); 103 Value* LogicalNotFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv); 104 Value* SubstringFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv); 105 Value* EqualityFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv); 106 Value* InequalityFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv); 107 Value* SequenceFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv); 108 109 // Global builtins, registered by RegisterBuiltins(). 110 Value* IfElseFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv); 111 Value* AssertFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv); 112 Value* AbortFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv); 113 114 // Register a new function. The same Function may be registered under 115 // multiple names, but a given name should only be used once. 116 void RegisterFunction(const std::string& name, Function fn); 117 118 // Register all the builtins. 119 void RegisterBuiltins(); 120 121 // Find the Function for a given name; return NULL if no such function 122 // exists. 123 Function FindFunction(const std::string& name); 124 125 // --- convenience functions for use in functions --- 126 127 // Evaluate the expressions in argv, and put the results of strings in args. If any expression 128 // evaluates to nullptr, return false. Return true on success. 129 bool ReadArgs(State* state, const std::vector<std::unique_ptr<Expr>>& argv, 130 std::vector<std::string>* args); 131 bool ReadArgs(State* state, const std::vector<std::unique_ptr<Expr>>& argv, 132 std::vector<std::string>* args, size_t start, size_t len); 133 134 // Evaluate the expressions in argv, and put the results of Value* in args. If any 135 // expression evaluate to nullptr, return false. Return true on success. 136 bool ReadValueArgs(State* state, const std::vector<std::unique_ptr<Expr>>& argv, 137 std::vector<std::unique_ptr<Value>>* args); 138 bool ReadValueArgs(State* state, const std::vector<std::unique_ptr<Expr>>& argv, 139 std::vector<std::unique_ptr<Value>>* args, size_t start, size_t len); 140 141 // Use printf-style arguments to compose an error message to put into 142 // *state. Returns NULL. 143 Value* ErrorAbort(State* state, const char* format, ...) 144 __attribute__((format(printf, 2, 3), deprecated)); 145 146 // ErrorAbort has an optional (but recommended) argument 'cause_code'. If the cause code 147 // is set, it will be logged into last_install and provides reason of OTA failures. 148 Value* ErrorAbort(State* state, CauseCode cause_code, const char* format, ...) 149 __attribute__((format(printf, 3, 4))); 150 151 // Copying the string into a Value. 152 Value* StringValue(const char* str); 153 154 Value* StringValue(const std::string& str); 155 156 int ParseString(const std::string& str, std::unique_ptr<Expr>* root, int* error_count); 157 158 #endif // _EXPRESSION_H 159