Home | History | Annotate | Download | only in edify
      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 #include "error_code.h"
     27 
     28 struct State {
     29     State(const std::string& script, void* cookie);
     30 
     31     // The source of the original script.
     32     const std::string& script;
     33 
     34     // Optional pointer to app-specific data; the core of edify never
     35     // uses this value.
     36     void* cookie;
     37 
     38     // The error message (if any) returned if the evaluation aborts.
     39     // Should be empty initially, will be either empty or a string that
     40     // Evaluate() returns.
     41     std::string errmsg;
     42 
     43     // error code indicates the type of failure (e.g. failure to update system image)
     44     // during the OTA process.
     45     ErrorCode error_code = kNoError;
     46 
     47     // cause code provides more detailed reason of an OTA failure (e.g. fsync error)
     48     // in addition to the error code.
     49     CauseCode cause_code = kNoCause;
     50 
     51     bool is_retry = false;
     52 };
     53 
     54 enum ValueType {
     55     VAL_INVALID = -1,
     56     VAL_STRING = 1,
     57     VAL_BLOB = 2,
     58 };
     59 
     60 struct Value {
     61     ValueType type;
     62     std::string data;
     63 
     64     Value(ValueType type, const std::string& str) :
     65         type(type),
     66         data(str) {}
     67 };
     68 
     69 struct Expr;
     70 
     71 using Function = Value* (*)(const char* name, State* state,
     72                             const std::vector<std::unique_ptr<Expr>>& argv);
     73 
     74 struct Expr {
     75   Function fn;
     76   std::string name;
     77   std::vector<std::unique_ptr<Expr>> argv;
     78   int start, end;
     79 
     80   Expr(Function fn, const std::string& name, int start, int end) :
     81     fn(fn),
     82     name(name),
     83     start(start),
     84     end(end) {}
     85 };
     86 
     87 // Evaluate the input expr, return the resulting Value.
     88 Value* EvaluateValue(State* state, const std::unique_ptr<Expr>& expr);
     89 
     90 // Evaluate the input expr, assert that it is a string, and update the result parameter. This
     91 // function returns true if the evaluation succeeds. This is a convenience function for older
     92 // functions that want to deal only with strings.
     93 bool Evaluate(State* state, const std::unique_ptr<Expr>& expr, std::string* result);
     94 
     95 // Glue to make an Expr out of a literal.
     96 Value* Literal(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv);
     97 
     98 // Functions corresponding to various syntactic sugar operators.
     99 // ("concat" is also available as a builtin function, to concatenate
    100 // more than two strings.)
    101 Value* ConcatFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv);
    102 Value* LogicalAndFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv);
    103 Value* LogicalOrFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv);
    104 Value* LogicalNotFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv);
    105 Value* SubstringFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv);
    106 Value* EqualityFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv);
    107 Value* InequalityFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv);
    108 Value* SequenceFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv);
    109 
    110 // Global builtins, registered by RegisterBuiltins().
    111 Value* IfElseFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv);
    112 Value* AssertFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv);
    113 Value* AbortFn(const char* name, State* state, const std::vector<std::unique_ptr<Expr>>& argv);
    114 
    115 // Register a new function.  The same Function may be registered under
    116 // multiple names, but a given name should only be used once.
    117 void RegisterFunction(const std::string& name, Function fn);
    118 
    119 // Register all the builtins.
    120 void RegisterBuiltins();
    121 
    122 // Find the Function for a given name; return NULL if no such function
    123 // exists.
    124 Function FindFunction(const std::string& name);
    125 
    126 // --- convenience functions for use in functions ---
    127 
    128 // Evaluate the expressions in argv, and put the results of strings in args. If any expression
    129 // evaluates to nullptr, return false. Return true on success.
    130 bool ReadArgs(State* state, const std::vector<std::unique_ptr<Expr>>& argv,
    131               std::vector<std::string>* args);
    132 bool ReadArgs(State* state, const std::vector<std::unique_ptr<Expr>>& argv,
    133               std::vector<std::string>* args, size_t start, size_t len);
    134 
    135 // Evaluate the expressions in argv, and put the results of Value* in args. If any
    136 // expression evaluate to nullptr, return false. Return true on success.
    137 bool ReadValueArgs(State* state, const std::vector<std::unique_ptr<Expr>>& argv,
    138                    std::vector<std::unique_ptr<Value>>* args);
    139 bool ReadValueArgs(State* state, const std::vector<std::unique_ptr<Expr>>& argv,
    140                    std::vector<std::unique_ptr<Value>>* args, size_t start, size_t len);
    141 
    142 // Use printf-style arguments to compose an error message to put into
    143 // *state.  Returns NULL.
    144 Value* ErrorAbort(State* state, const char* format, ...)
    145     __attribute__((format(printf, 2, 3), deprecated));
    146 
    147 // ErrorAbort has an optional (but recommended) argument 'cause_code'. If the cause code
    148 // is set, it will be logged into last_install and provides reason of OTA failures.
    149 Value* ErrorAbort(State* state, CauseCode cause_code, const char* format, ...)
    150     __attribute__((format(printf, 3, 4)));
    151 
    152 // Copying the string into a Value.
    153 Value* StringValue(const char* str);
    154 
    155 Value* StringValue(const std::string& str);
    156 
    157 int parse_string(const char* str, std::unique_ptr<Expr>* root, int* error_count);
    158 
    159 #endif  // _EXPRESSION_H
    160