blob: 5c06de84687e6619cd73425c172cb8ee93fa4f4d [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
Doug Zongkerd9c9d102009-06-12 12:24:39 -070052} State;
53
Doug Zongker512536a2010-02-17 16:11:44 -080054#define VAL_STRING 1 // data will be NULL-terminated; size doesn't count null
55#define VAL_BLOB 2
56
57typedef struct {
58 int type;
59 ssize_t size;
60 char* data;
61} Value;
62
63typedef Value* (*Function)(const char* name, State* state,
64 int argc, Expr* argv[]);
Doug Zongker37bee622009-06-08 17:35:39 -070065
66struct Expr {
Doug Zongkerd9c9d102009-06-12 12:24:39 -070067 Function fn;
Tao Bao2a5a49d2015-08-20 12:10:46 -070068 const char* name;
Doug Zongkerd9c9d102009-06-12 12:24:39 -070069 int argc;
70 Expr** argv;
71 int start, end;
Doug Zongker37bee622009-06-08 17:35:39 -070072};
73
Doug Zongker512536a2010-02-17 16:11:44 -080074// Take one of the Expr*s passed to the function as an argument,
75// evaluate it, return the resulting Value. The caller takes
76// ownership of the returned Value.
77Value* EvaluateValue(State* state, Expr* expr);
78
79// Take one of the Expr*s passed to the function as an argument,
80// evaluate it, assert that it is a string, and return the resulting
81// char*. The caller takes ownership of the returned char*. This is
82// a convenience function for older functions that want to deal only
83// with strings.
Doug Zongkerd9c9d102009-06-12 12:24:39 -070084char* Evaluate(State* state, Expr* expr);
Doug Zongker37bee622009-06-08 17:35:39 -070085
86// Glue to make an Expr out of a literal.
Doug Zongker512536a2010-02-17 16:11:44 -080087Value* Literal(const char* name, State* state, int argc, Expr* argv[]);
Doug Zongker37bee622009-06-08 17:35:39 -070088
89// Functions corresponding to various syntactic sugar operators.
90// ("concat" is also available as a builtin function, to concatenate
91// more than two strings.)
Doug Zongker512536a2010-02-17 16:11:44 -080092Value* ConcatFn(const char* name, State* state, int argc, Expr* argv[]);
93Value* LogicalAndFn(const char* name, State* state, int argc, Expr* argv[]);
94Value* LogicalOrFn(const char* name, State* state, int argc, Expr* argv[]);
95Value* LogicalNotFn(const char* name, State* state, int argc, Expr* argv[]);
96Value* SubstringFn(const char* name, State* state, int argc, Expr* argv[]);
97Value* EqualityFn(const char* name, State* state, int argc, Expr* argv[]);
98Value* InequalityFn(const char* name, State* state, int argc, Expr* argv[]);
99Value* SequenceFn(const char* name, State* state, int argc, Expr* argv[]);
Doug Zongker37bee622009-06-08 17:35:39 -0700100
101// Convenience function for building expressions with a fixed number
102// of arguments.
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700103Expr* Build(Function fn, YYLTYPE loc, int count, ...);
Doug Zongker37bee622009-06-08 17:35:39 -0700104
105// Global builtins, registered by RegisterBuiltins().
Doug Zongker512536a2010-02-17 16:11:44 -0800106Value* IfElseFn(const char* name, State* state, int argc, Expr* argv[]);
107Value* AssertFn(const char* name, State* state, int argc, Expr* argv[]);
108Value* AbortFn(const char* name, State* state, int argc, Expr* argv[]);
Doug Zongker37bee622009-06-08 17:35:39 -0700109
Doug Zongker9931f7f2009-06-10 14:11:53 -0700110
111// For setting and getting the global error string (when returning
112// NULL from a function).
113void SetError(const char* message); // makes a copy
114const char* GetError(); // retains ownership
115void ClearError();
116
117
Doug Zongker37bee622009-06-08 17:35:39 -0700118typedef struct {
119 const char* name;
120 Function fn;
121} NamedFunction;
122
123// Register a new function. The same Function may be registered under
124// multiple names, but a given name should only be used once.
125void RegisterFunction(const char* name, Function fn);
126
127// Register all the builtins.
128void RegisterBuiltins();
129
130// Call this after all calls to RegisterFunction() but before parsing
131// any scripts to finish building the function table.
132void FinishRegistration();
133
134// Find the Function for a given name; return NULL if no such function
135// exists.
136Function FindFunction(const char* name);
137
Doug Zongker9931f7f2009-06-10 14:11:53 -0700138
139// --- convenience functions for use in functions ---
140
141// Evaluate the expressions in argv, giving 'count' char* (the ... is
142// zero or more char** to put them in). If any expression evaluates
143// to NULL, free the rest and return -1. Return 0 on success.
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700144int ReadArgs(State* state, Expr* argv[], int count, ...);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700145
Doug Zongker512536a2010-02-17 16:11:44 -0800146// Evaluate the expressions in argv, giving 'count' Value* (the ... is
147// zero or more Value** to put them in). If any expression evaluates
148// to NULL, free the rest and return -1. Return 0 on success.
149int ReadValueArgs(State* state, Expr* argv[], int count, ...);
150
Doug Zongker9931f7f2009-06-10 14:11:53 -0700151// Evaluate the expressions in argv, returning an array of char*
152// results. If any evaluate to NULL, free the rest and return NULL.
153// The caller is responsible for freeing the returned array and the
154// strings it contains.
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700155char** ReadVarArgs(State* state, int argc, Expr* argv[]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700156
Doug Zongker512536a2010-02-17 16:11:44 -0800157// Evaluate the expressions in argv, returning an array of Value*
158// results. If any evaluate to NULL, free the rest and return NULL.
159// The caller is responsible for freeing the returned array and the
160// Values it contains.
161Value** ReadValueVarArgs(State* state, int argc, Expr* argv[]);
162
Doug Zongker47cace92009-06-18 10:11:50 -0700163// Use printf-style arguments to compose an error message to put into
164// *state. Returns NULL.
Tianjie Xu16255832016-04-30 11:49:59 -0700165Value* ErrorAbort(State* state, const char* format, ...)
166 __attribute__((format(printf, 2, 3), deprecated));
167
168// ErrorAbort has an optional (but recommended) argument 'cause_code'. If the cause code
169// is set, it will be logged into last_install and provides reason of OTA failures.
170Value* ErrorAbort(State* state, CauseCode cause_code, const char* format, ...)
171 __attribute__((format(printf, 3, 4)));
Doug Zongker47cace92009-06-18 10:11:50 -0700172
Doug Zongker512536a2010-02-17 16:11:44 -0800173// Wrap a string into a Value, taking ownership of the string.
174Value* StringValue(char* str);
175
176// Free a Value object.
177void FreeValue(Value* v);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700178
Doug Zongker0d32f252014-02-13 15:07:56 -0800179int parse_string(const char* str, Expr** root, int* error_count);
180
Doug Zongker37bee622009-06-08 17:35:39 -0700181#endif // _EXPRESSION_H