blob: 36f8e96125290a0a36391e2f8185ba92ef9b1eb5 [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
Doug Zongkerd9c9d102009-06-12 12:24:39 -070022#include "yydefs.h"
23
Doug Zongker37bee622009-06-08 17:35:39 -070024#define MAX_STRING_LEN 1024
25
26typedef struct Expr Expr;
27
Doug Zongkerd9c9d102009-06-12 12:24:39 -070028typedef struct {
29 // Optional pointer to app-specific data; the core of edify never
30 // uses this value.
31 void* cookie;
32
33 // The source of the original script. Must be NULL-terminated,
34 // and in writable memory (Evaluate may make temporary changes to
35 // it but will restore it when done).
36 char* script;
37
38 // The error message (if any) returned if the evaluation aborts.
39 // Should be NULL initially, will be either NULL or a malloc'd
40 // pointer after Evaluate() returns.
41 char* errmsg;
42} State;
43
Doug Zongker512536a2010-02-17 16:11:44 -080044#define VAL_STRING 1 // data will be NULL-terminated; size doesn't count null
45#define VAL_BLOB 2
46
47typedef struct {
48 int type;
49 ssize_t size;
50 char* data;
51} Value;
52
53typedef Value* (*Function)(const char* name, State* state,
54 int argc, Expr* argv[]);
Doug Zongker37bee622009-06-08 17:35:39 -070055
56struct Expr {
Doug Zongkerd9c9d102009-06-12 12:24:39 -070057 Function fn;
Tao Bao2a5a49d2015-08-20 12:10:46 -070058 const char* name;
Doug Zongkerd9c9d102009-06-12 12:24:39 -070059 int argc;
60 Expr** argv;
61 int start, end;
Doug Zongker37bee622009-06-08 17:35:39 -070062};
63
Doug Zongker512536a2010-02-17 16:11:44 -080064// Take one of the Expr*s passed to the function as an argument,
65// evaluate it, return the resulting Value. The caller takes
66// ownership of the returned Value.
67Value* EvaluateValue(State* state, Expr* expr);
68
69// Take one of the Expr*s passed to the function as an argument,
70// evaluate it, assert that it is a string, and return the resulting
71// char*. The caller takes ownership of the returned char*. This is
72// a convenience function for older functions that want to deal only
73// with strings.
Doug Zongkerd9c9d102009-06-12 12:24:39 -070074char* Evaluate(State* state, Expr* expr);
Doug Zongker37bee622009-06-08 17:35:39 -070075
76// Glue to make an Expr out of a literal.
Doug Zongker512536a2010-02-17 16:11:44 -080077Value* Literal(const char* name, State* state, int argc, Expr* argv[]);
Doug Zongker37bee622009-06-08 17:35:39 -070078
79// Functions corresponding to various syntactic sugar operators.
80// ("concat" is also available as a builtin function, to concatenate
81// more than two strings.)
Doug Zongker512536a2010-02-17 16:11:44 -080082Value* ConcatFn(const char* name, State* state, int argc, Expr* argv[]);
83Value* LogicalAndFn(const char* name, State* state, int argc, Expr* argv[]);
84Value* LogicalOrFn(const char* name, State* state, int argc, Expr* argv[]);
85Value* LogicalNotFn(const char* name, State* state, int argc, Expr* argv[]);
86Value* SubstringFn(const char* name, State* state, int argc, Expr* argv[]);
87Value* EqualityFn(const char* name, State* state, int argc, Expr* argv[]);
88Value* InequalityFn(const char* name, State* state, int argc, Expr* argv[]);
89Value* SequenceFn(const char* name, State* state, int argc, Expr* argv[]);
Doug Zongker37bee622009-06-08 17:35:39 -070090
91// Convenience function for building expressions with a fixed number
92// of arguments.
Doug Zongkerd9c9d102009-06-12 12:24:39 -070093Expr* Build(Function fn, YYLTYPE loc, int count, ...);
Doug Zongker37bee622009-06-08 17:35:39 -070094
95// Global builtins, registered by RegisterBuiltins().
Doug Zongker512536a2010-02-17 16:11:44 -080096Value* IfElseFn(const char* name, State* state, int argc, Expr* argv[]);
97Value* AssertFn(const char* name, State* state, int argc, Expr* argv[]);
98Value* AbortFn(const char* name, State* state, int argc, Expr* argv[]);
Doug Zongker37bee622009-06-08 17:35:39 -070099
Doug Zongker9931f7f2009-06-10 14:11:53 -0700100
101// For setting and getting the global error string (when returning
102// NULL from a function).
103void SetError(const char* message); // makes a copy
104const char* GetError(); // retains ownership
105void ClearError();
106
107
Doug Zongker37bee622009-06-08 17:35:39 -0700108typedef struct {
109 const char* name;
110 Function fn;
111} NamedFunction;
112
113// Register a new function. The same Function may be registered under
114// multiple names, but a given name should only be used once.
115void RegisterFunction(const char* name, Function fn);
116
117// Register all the builtins.
118void RegisterBuiltins();
119
120// Call this after all calls to RegisterFunction() but before parsing
121// any scripts to finish building the function table.
122void FinishRegistration();
123
124// Find the Function for a given name; return NULL if no such function
125// exists.
126Function FindFunction(const char* name);
127
Doug Zongker9931f7f2009-06-10 14:11:53 -0700128
129// --- convenience functions for use in functions ---
130
131// Evaluate the expressions in argv, giving 'count' char* (the ... is
132// zero or more char** to put them in). If any expression evaluates
133// to NULL, free the rest and return -1. Return 0 on success.
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700134int ReadArgs(State* state, Expr* argv[], int count, ...);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700135
Doug Zongker512536a2010-02-17 16:11:44 -0800136// Evaluate the expressions in argv, giving 'count' Value* (the ... is
137// zero or more Value** to put them in). If any expression evaluates
138// to NULL, free the rest and return -1. Return 0 on success.
139int ReadValueArgs(State* state, Expr* argv[], int count, ...);
140
Doug Zongker9931f7f2009-06-10 14:11:53 -0700141// Evaluate the expressions in argv, returning an array of char*
142// results. If any evaluate to NULL, free the rest and return NULL.
143// The caller is responsible for freeing the returned array and the
144// strings it contains.
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700145char** ReadVarArgs(State* state, int argc, Expr* argv[]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700146
Doug Zongker512536a2010-02-17 16:11:44 -0800147// Evaluate the expressions in argv, returning an array of Value*
148// results. If any evaluate to NULL, free the rest and return NULL.
149// The caller is responsible for freeing the returned array and the
150// Values it contains.
151Value** ReadValueVarArgs(State* state, int argc, Expr* argv[]);
152
Doug Zongker47cace92009-06-18 10:11:50 -0700153// Use printf-style arguments to compose an error message to put into
154// *state. Returns NULL.
Doug Zongkere5d5ac72012-04-12 11:01:22 -0700155Value* ErrorAbort(State* state, const char* format, ...) __attribute__((format(printf, 2, 3)));
Doug Zongker47cace92009-06-18 10:11:50 -0700156
Doug Zongker512536a2010-02-17 16:11:44 -0800157// Wrap a string into a Value, taking ownership of the string.
158Value* StringValue(char* str);
159
160// Free a Value object.
161void FreeValue(Value* v);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700162
Doug Zongker0d32f252014-02-13 15:07:56 -0800163int parse_string(const char* str, Expr** root, int* error_count);
164
Doug Zongker37bee622009-06-08 17:35:39 -0700165#endif // _EXPRESSION_H