blob: 1462531b070f3581b7266397dcb40b64f8856a5d [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 Zongkerd9c9d102009-06-12 12:24:39 -070020#include "yydefs.h"
21
Doug Zongker37bee622009-06-08 17:35:39 -070022#define MAX_STRING_LEN 1024
23
24typedef struct Expr Expr;
25
Doug Zongkerd9c9d102009-06-12 12:24:39 -070026typedef struct {
27 // Optional pointer to app-specific data; the core of edify never
28 // uses this value.
29 void* cookie;
30
31 // The source of the original script. Must be NULL-terminated,
32 // and in writable memory (Evaluate may make temporary changes to
33 // it but will restore it when done).
34 char* script;
35
36 // The error message (if any) returned if the evaluation aborts.
37 // Should be NULL initially, will be either NULL or a malloc'd
38 // pointer after Evaluate() returns.
39 char* errmsg;
40} State;
41
Doug Zongker512536a2010-02-17 16:11:44 -080042#define VAL_STRING 1 // data will be NULL-terminated; size doesn't count null
43#define VAL_BLOB 2
44
45typedef struct {
46 int type;
47 ssize_t size;
48 char* data;
49} Value;
50
51typedef Value* (*Function)(const char* name, State* state,
52 int argc, Expr* argv[]);
Doug Zongker37bee622009-06-08 17:35:39 -070053
54struct Expr {
Doug Zongkerd9c9d102009-06-12 12:24:39 -070055 Function fn;
56 char* name;
57 int argc;
58 Expr** argv;
59 int start, end;
Doug Zongker37bee622009-06-08 17:35:39 -070060};
61
Doug Zongker512536a2010-02-17 16:11:44 -080062// Take one of the Expr*s passed to the function as an argument,
63// evaluate it, return the resulting Value. The caller takes
64// ownership of the returned Value.
65Value* EvaluateValue(State* state, Expr* expr);
66
67// Take one of the Expr*s passed to the function as an argument,
68// evaluate it, assert that it is a string, and return the resulting
69// char*. The caller takes ownership of the returned char*. This is
70// a convenience function for older functions that want to deal only
71// with strings.
Doug Zongkerd9c9d102009-06-12 12:24:39 -070072char* Evaluate(State* state, Expr* expr);
Doug Zongker37bee622009-06-08 17:35:39 -070073
74// Glue to make an Expr out of a literal.
Doug Zongker512536a2010-02-17 16:11:44 -080075Value* Literal(const char* name, State* state, int argc, Expr* argv[]);
Doug Zongker37bee622009-06-08 17:35:39 -070076
77// Functions corresponding to various syntactic sugar operators.
78// ("concat" is also available as a builtin function, to concatenate
79// more than two strings.)
Doug Zongker512536a2010-02-17 16:11:44 -080080Value* ConcatFn(const char* name, State* state, int argc, Expr* argv[]);
81Value* LogicalAndFn(const char* name, State* state, int argc, Expr* argv[]);
82Value* LogicalOrFn(const char* name, State* state, int argc, Expr* argv[]);
83Value* LogicalNotFn(const char* name, State* state, int argc, Expr* argv[]);
84Value* SubstringFn(const char* name, State* state, int argc, Expr* argv[]);
85Value* EqualityFn(const char* name, State* state, int argc, Expr* argv[]);
86Value* InequalityFn(const char* name, State* state, int argc, Expr* argv[]);
87Value* SequenceFn(const char* name, State* state, int argc, Expr* argv[]);
Doug Zongker37bee622009-06-08 17:35:39 -070088
89// Convenience function for building expressions with a fixed number
90// of arguments.
Doug Zongkerd9c9d102009-06-12 12:24:39 -070091Expr* Build(Function fn, YYLTYPE loc, int count, ...);
Doug Zongker37bee622009-06-08 17:35:39 -070092
93// Global builtins, registered by RegisterBuiltins().
Doug Zongker512536a2010-02-17 16:11:44 -080094Value* IfElseFn(const char* name, State* state, int argc, Expr* argv[]);
95Value* AssertFn(const char* name, State* state, int argc, Expr* argv[]);
96Value* AbortFn(const char* name, State* state, int argc, Expr* argv[]);
Doug Zongker37bee622009-06-08 17:35:39 -070097
Doug Zongker9931f7f2009-06-10 14:11:53 -070098
99// For setting and getting the global error string (when returning
100// NULL from a function).
101void SetError(const char* message); // makes a copy
102const char* GetError(); // retains ownership
103void ClearError();
104
105
Doug Zongker37bee622009-06-08 17:35:39 -0700106typedef struct {
107 const char* name;
108 Function fn;
109} NamedFunction;
110
111// Register a new function. The same Function may be registered under
112// multiple names, but a given name should only be used once.
113void RegisterFunction(const char* name, Function fn);
114
115// Register all the builtins.
116void RegisterBuiltins();
117
118// Call this after all calls to RegisterFunction() but before parsing
119// any scripts to finish building the function table.
120void FinishRegistration();
121
122// Find the Function for a given name; return NULL if no such function
123// exists.
124Function FindFunction(const char* name);
125
Doug Zongker9931f7f2009-06-10 14:11:53 -0700126
127// --- convenience functions for use in functions ---
128
129// Evaluate the expressions in argv, giving 'count' char* (the ... is
130// zero or more char** to put them in). If any expression evaluates
131// to NULL, free the rest and return -1. Return 0 on success.
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700132int ReadArgs(State* state, Expr* argv[], int count, ...);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700133
Doug Zongker512536a2010-02-17 16:11:44 -0800134// Evaluate the expressions in argv, giving 'count' Value* (the ... is
135// zero or more Value** to put them in). If any expression evaluates
136// to NULL, free the rest and return -1. Return 0 on success.
137int ReadValueArgs(State* state, Expr* argv[], int count, ...);
138
Doug Zongker9931f7f2009-06-10 14:11:53 -0700139// Evaluate the expressions in argv, returning an array of char*
140// results. If any evaluate to NULL, free the rest and return NULL.
141// The caller is responsible for freeing the returned array and the
142// strings it contains.
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700143char** ReadVarArgs(State* state, int argc, Expr* argv[]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700144
Doug Zongker512536a2010-02-17 16:11:44 -0800145// Evaluate the expressions in argv, returning an array of Value*
146// results. If any evaluate to NULL, free the rest and return NULL.
147// The caller is responsible for freeing the returned array and the
148// Values it contains.
149Value** ReadValueVarArgs(State* state, int argc, Expr* argv[]);
150
Doug Zongker47cace92009-06-18 10:11:50 -0700151// Use printf-style arguments to compose an error message to put into
152// *state. Returns NULL.
Doug Zongker512536a2010-02-17 16:11:44 -0800153Value* ErrorAbort(State* state, char* format, ...);
Doug Zongker47cace92009-06-18 10:11:50 -0700154
Doug Zongker512536a2010-02-17 16:11:44 -0800155// Wrap a string into a Value, taking ownership of the string.
156Value* StringValue(char* str);
157
158// Free a Value object.
159void FreeValue(Value* v);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700160
Doug Zongker37bee622009-06-08 17:35:39 -0700161#endif // _EXPRESSION_H