blob: d2e739201c17910e3c5b7736f5161957723dbcf5 [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
42typedef char* (*Function)(const char* name, State* state,
Doug Zongker37bee622009-06-08 17:35:39 -070043 int argc, Expr* argv[]);
44
45struct Expr {
Doug Zongkerd9c9d102009-06-12 12:24:39 -070046 Function fn;
47 char* name;
48 int argc;
49 Expr** argv;
50 int start, end;
Doug Zongker37bee622009-06-08 17:35:39 -070051};
52
Doug Zongkerd9c9d102009-06-12 12:24:39 -070053char* Evaluate(State* state, Expr* expr);
Doug Zongker37bee622009-06-08 17:35:39 -070054
55// Glue to make an Expr out of a literal.
Doug Zongkerd9c9d102009-06-12 12:24:39 -070056char* Literal(const char* name, State* state, int argc, Expr* argv[]);
Doug Zongker37bee622009-06-08 17:35:39 -070057
58// Functions corresponding to various syntactic sugar operators.
59// ("concat" is also available as a builtin function, to concatenate
60// more than two strings.)
Doug Zongkerd9c9d102009-06-12 12:24:39 -070061char* ConcatFn(const char* name, State* state, int argc, Expr* argv[]);
62char* LogicalAndFn(const char* name, State* state, int argc, Expr* argv[]);
63char* LogicalOrFn(const char* name, State* state, int argc, Expr* argv[]);
64char* LogicalNotFn(const char* name, State* state, int argc, Expr* argv[]);
65char* SubstringFn(const char* name, State* state, int argc, Expr* argv[]);
66char* EqualityFn(const char* name, State* state, int argc, Expr* argv[]);
67char* InequalityFn(const char* name, State* state, int argc, Expr* argv[]);
68char* SequenceFn(const char* name, State* state, int argc, Expr* argv[]);
Doug Zongker37bee622009-06-08 17:35:39 -070069
70// Convenience function for building expressions with a fixed number
71// of arguments.
Doug Zongkerd9c9d102009-06-12 12:24:39 -070072Expr* Build(Function fn, YYLTYPE loc, int count, ...);
Doug Zongker37bee622009-06-08 17:35:39 -070073
74// Global builtins, registered by RegisterBuiltins().
Doug Zongkerd9c9d102009-06-12 12:24:39 -070075char* IfElseFn(const char* name, State* state, int argc, Expr* argv[]);
76char* AssertFn(const char* name, State* state, int argc, Expr* argv[]);
77char* AbortFn(const char* name, State* state, int argc, Expr* argv[]);
Doug Zongker37bee622009-06-08 17:35:39 -070078
Doug Zongker9931f7f2009-06-10 14:11:53 -070079
80// For setting and getting the global error string (when returning
81// NULL from a function).
82void SetError(const char* message); // makes a copy
83const char* GetError(); // retains ownership
84void ClearError();
85
86
Doug Zongker37bee622009-06-08 17:35:39 -070087typedef struct {
88 const char* name;
89 Function fn;
90} NamedFunction;
91
92// Register a new function. The same Function may be registered under
93// multiple names, but a given name should only be used once.
94void RegisterFunction(const char* name, Function fn);
95
96// Register all the builtins.
97void RegisterBuiltins();
98
99// Call this after all calls to RegisterFunction() but before parsing
100// any scripts to finish building the function table.
101void FinishRegistration();
102
103// Find the Function for a given name; return NULL if no such function
104// exists.
105Function FindFunction(const char* name);
106
Doug Zongker9931f7f2009-06-10 14:11:53 -0700107
108// --- convenience functions for use in functions ---
109
110// Evaluate the expressions in argv, giving 'count' char* (the ... is
111// zero or more char** to put them in). If any expression evaluates
112// to NULL, free the rest and return -1. Return 0 on success.
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700113int ReadArgs(State* state, Expr* argv[], int count, ...);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700114
115// Evaluate the expressions in argv, returning an array of char*
116// results. If any evaluate to NULL, free the rest and return NULL.
117// The caller is responsible for freeing the returned array and the
118// strings it contains.
Doug Zongkerd9c9d102009-06-12 12:24:39 -0700119char** ReadVarArgs(State* state, int argc, Expr* argv[]);
Doug Zongker9931f7f2009-06-10 14:11:53 -0700120
Doug Zongker47cace92009-06-18 10:11:50 -0700121// Use printf-style arguments to compose an error message to put into
122// *state. Returns NULL.
123char* ErrorAbort(State* state, char* format, ...);
124
Doug Zongker9931f7f2009-06-10 14:11:53 -0700125
Doug Zongker37bee622009-06-08 17:35:39 -0700126#endif // _EXPRESSION_H