blob: 886347991c9e711b98773e0440972c37beb1f3ac [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>
21
Tianjie Xu16255832016-04-30 11:49:59 -070022#include "error_code.h"
Doug Zongkerd9c9d102009-06-12 12:24:39 -070023#include "yydefs.h"
24
Doug Zongker37bee622009-06-08 17:35:39 -070025#define MAX_STRING_LEN 1024
26
27typedef struct Expr Expr;
28
Doug Zongkerd9c9d102009-06-12 12:24:39 -070029typedef 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;
Tianjie Xu16255832016-04-30 11:49:59 -070043
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
Tianjie Xu7ce287d2016-05-31 09:29:49 -070052 bool is_retry = false;
53
Doug Zongkerd9c9d102009-06-12 12:24:39 -070054} State;
55
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