blob: f045d93868cc1385805ea67a552015a056940610 [file] [log] [blame]
Doug Zongker37bee622009-06-08 17:35:39 -07001/*
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 Zongker583fc122010-02-19 16:07:57 -080020#include <unistd.h>
Tao Bao59dcb9c2016-10-03 18:06:46 -070021#include <string>
Doug Zongker583fc122010-02-19 16:07:57 -080022
Tianjie Xu16255832016-04-30 11:49:59 -070023#include "error_code.h"
Doug Zongkerd9c9d102009-06-12 12:24:39 -070024#include "yydefs.h"
25
Doug Zongker37bee622009-06-08 17:35:39 -070026#define MAX_STRING_LEN 1024
27
28typedef struct Expr Expr;
29
Tao Bao59dcb9c2016-10-03 18:06:46 -070030struct State {
31 State(const std::string& script, void* cookie);
32
33 // The source of the original script.
34 const std::string& script;
35
Doug Zongkerd9c9d102009-06-12 12:24:39 -070036 // Optional pointer to app-specific data; the core of edify never
37 // uses this value.
38 void* cookie;
39
Doug Zongkerd9c9d102009-06-12 12:24:39 -070040 // The error message (if any) returned if the evaluation aborts.
Tao Bao59dcb9c2016-10-03 18:06:46 -070041 // Should be empty initially, will be either empty or a string that
42 // Evaluate() returns.
43 std::string errmsg;
Tianjie Xu16255832016-04-30 11:49:59 -070044
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 Xu7ce287d2016-05-31 09:29:49 -070053 bool is_retry = false;
Tao Bao59dcb9c2016-10-03 18:06:46 -070054};
Doug Zongkerd9c9d102009-06-12 12:24:39 -070055
Doug Zongker512536a2010-02-17 16:11:44 -080056#define VAL_STRING 1 // data will be NULL-terminated; size doesn't count null
57#define VAL_BLOB 2
58
59typedef struct {
60 int type;
61 ssize_t size;
62 char* data;
63} Value;
64
65typedef Value* (*Function)(const char* name, State* state,
66 int argc, Expr* argv[]);
Doug Zongker37bee622009-06-08 17:35:39 -070067
68struct Expr {
Doug Zongkerd9c9d102009-06-12 12:24:39 -070069 Function fn;
Tao Bao2a5a49d2015-08-20 12:10:46 -070070 const char* name;
Doug Zongkerd9c9d102009-06-12 12:24:39 -070071 int argc;
72 Expr** argv;
73 int start, end;
Doug Zongker37bee622009-06-08 17:35:39 -070074};
75
Doug Zongker512536a2010-02-17 16:11:44 -080076// 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.
79Value* 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 Zongkerd9c9d102009-06-12 12:24:39 -070086char* Evaluate(State* state, Expr* expr);
Doug Zongker37bee622009-06-08 17:35:39 -070087
88// Glue to make an Expr out of a literal.
Doug Zongker512536a2010-02-17 16:11:44 -080089Value* Literal(const char* name, State* state, int argc, Expr* argv[]);
Doug Zongker37bee622009-06-08 17:35:39 -070090
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 Zongker512536a2010-02-17 16:11:44 -080094Value* ConcatFn(const char* name, State* state, int argc, Expr* argv[]);
95Value* LogicalAndFn(const char* name, State* state, int argc, Expr* argv[]);
96Value* LogicalOrFn(const char* name, State* state, int argc, Expr* argv[]);
97Value* LogicalNotFn(const char* name, State* state, int argc, Expr* argv[]);
98Value* SubstringFn(const char* name, State* state, int argc, Expr* argv[]);
99Value* EqualityFn(const char* name, State* state, int argc, Expr* argv[]);
100Value* InequalityFn(const char* name, State* state, int argc, Expr* argv[]);
101Value* SequenceFn(const char* name, State* state, int argc, Expr* argv[]);
Doug Zongker37bee622009-06-08 17:35:39 -0700102
103// Convenience function for building expressions with a fixed number
104// of arguments.
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700105Expr* Build(Function fn, YYLTYPE loc, int count, ...);
Doug Zongker37bee622009-06-08 17:35:39 -0700106
107// Global builtins, registered by RegisterBuiltins().
Doug Zongker512536a2010-02-17 16:11:44 -0800108Value* IfElseFn(const char* name, State* state, int argc, Expr* argv[]);
109Value* AssertFn(const char* name, State* state, int argc, Expr* argv[]);
110Value* AbortFn(const char* name, State* state, int argc, Expr* argv[]);
Doug Zongker37bee622009-06-08 17:35:39 -0700111
Doug Zongker9931f7f2009-06-10 14:11:53 -0700112
113// For setting and getting the global error string (when returning
114// NULL from a function).
115void SetError(const char* message); // makes a copy
116const char* GetError(); // retains ownership
117void ClearError();
118
119
Doug Zongker37bee622009-06-08 17:35:39 -0700120typedef 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.
127void RegisterFunction(const char* name, Function fn);
128
129// Register all the builtins.
130void RegisterBuiltins();
131
132// Call this after all calls to RegisterFunction() but before parsing
133// any scripts to finish building the function table.
134void FinishRegistration();
135
136// Find the Function for a given name; return NULL if no such function
137// exists.
138Function FindFunction(const char* name);
139
Doug Zongker9931f7f2009-06-10 14:11:53 -0700140
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 Zongkerd9c9d102009-06-12 12:24:39 -0700146int ReadArgs(State* state, Expr* argv[], int count, ...);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700147
Doug Zongker512536a2010-02-17 16:11:44 -0800148// 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.
151int ReadValueArgs(State* state, Expr* argv[], int count, ...);
152
Doug Zongker9931f7f2009-06-10 14:11:53 -0700153// 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 Zongkerd9c9d102009-06-12 12:24:39 -0700157char** ReadVarArgs(State* state, int argc, Expr* argv[]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700158
Doug Zongker512536a2010-02-17 16:11:44 -0800159// 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.
163Value** ReadValueVarArgs(State* state, int argc, Expr* argv[]);
164
Doug Zongker47cace92009-06-18 10:11:50 -0700165// Use printf-style arguments to compose an error message to put into
166// *state. Returns NULL.
Tianjie Xu16255832016-04-30 11:49:59 -0700167Value* 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.
172Value* ErrorAbort(State* state, CauseCode cause_code, const char* format, ...)
173 __attribute__((format(printf, 3, 4)));
Doug Zongker47cace92009-06-18 10:11:50 -0700174
Doug Zongker512536a2010-02-17 16:11:44 -0800175// Wrap a string into a Value, taking ownership of the string.
176Value* StringValue(char* str);
177
178// Free a Value object.
179void FreeValue(Value* v);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700180
Doug Zongker0d32f252014-02-13 15:07:56 -0800181int parse_string(const char* str, Expr** root, int* error_count);
182
Doug Zongker37bee622009-06-08 17:35:39 -0700183#endif // _EXPRESSION_H