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 "error_code.h"
23 #include "yydefs.h"
24 
25 #define MAX_STRING_LEN 1024
26 
27 typedef struct Expr Expr;
28 
29 typedef struct {
30     // Optional pointer to app-specific data; the core of edify never
31     // uses this value.
32     void* cookie;
33 
34     // The source of the original script.  Must be NULL-terminated,
35     // and in writable memory (Evaluate may make temporary changes to
36     // it but will restore it when done).
37     char* script;
38 
39     // The error message (if any) returned if the evaluation aborts.
40     // Should be NULL initially, will be either NULL or a malloc'd
41     // pointer after Evaluate() returns.
42     char* errmsg;
43 
44     // error code indicates the type of failure (e.g. failure to update system image)
45     // during the OTA process.
46     ErrorCode error_code = kNoError;
47 
48     // cause code provides more detailed reason of an OTA failure (e.g. fsync error)
49     // in addition to the error code.
50     CauseCode cause_code = kNoCause;
51 
52     bool is_retry = false;
53 
54 } State;
55 
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[]);
67 
68 struct Expr {
69     Function fn;
70     const char* name;
71     int argc;
72     Expr** argv;
73     int start, end;
74 };
75 
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.
86 char* Evaluate(State* state, Expr* expr);
87 
88 // Glue to make an Expr out of a literal.
89 Value* Literal(const char* name, State* state, int argc, Expr* argv[]);
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.)
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[]);
102 
103 // Convenience function for building expressions with a fixed number
104 // of arguments.
105 Expr* Build(Function fn, YYLTYPE loc, int count, ...);
106 
107 // Global builtins, registered by RegisterBuiltins().
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[]);
111 
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 
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 
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.
146 int ReadArgs(State* state, Expr* argv[], int count, ...);
147 
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 
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.
157 char** ReadVarArgs(State* state, int argc, Expr* argv[]);
158 
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 
165 // Use printf-style arguments to compose an error message to put into
166 // *state.  Returns NULL.
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)));
174 
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);
180 
181 int parse_string(const char* str, Expr** root, int* error_count);
182 
183 #endif  // _EXPRESSION_H
184