blob: cfbef903bd2bea1a9c5f50f4307817153d32d9eb [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
20#define MAX_STRING_LEN 1024
21
22typedef struct Expr Expr;
23
24typedef char* (*Function)(const char* name, void* cookie,
25 int argc, Expr* argv[]);
26
27struct Expr {
28 Function fn;
29 char* name;
30 int argc;
31 Expr** argv;
32};
33
34char* Evaluate(void* cookie, Expr* expr);
35
36// Glue to make an Expr out of a literal.
37char* Literal(const char* name, void* cookie, int argc, Expr* argv[]);
38
39// Functions corresponding to various syntactic sugar operators.
40// ("concat" is also available as a builtin function, to concatenate
41// more than two strings.)
42char* ConcatFn(const char* name, void* cookie, int argc, Expr* argv[]);
43char* LogicalAndFn(const char* name, void* cookie, int argc, Expr* argv[]);
44char* LogicalOrFn(const char* name, void* cookie, int argc, Expr* argv[]);
45char* LogicalNotFn(const char* name, void* cookie, int argc, Expr* argv[]);
46char* SubstringFn(const char* name, void* cookie, int argc, Expr* argv[]);
47char* EqualityFn(const char* name, void* cookie, int argc, Expr* argv[]);
48char* InequalityFn(const char* name, void* cookie, int argc, Expr* argv[]);
49char* SequenceFn(const char* name, void* cookie, int argc, Expr* argv[]);
50
51// Convenience function for building expressions with a fixed number
52// of arguments.
53Expr* Build(Function fn, int count, ...);
54
55// Global builtins, registered by RegisterBuiltins().
56char* IfElseFn(const char* name, void* cookie, int argc, Expr* argv[]);
57char* AssertFn(const char* name, void* cookie, int argc, Expr* argv[]);
58char* AbortFn(const char* name, void* cookie, int argc, Expr* argv[]);
59
Doug Zongker9931f7f2009-06-10 14:11:53 -070060
61// For setting and getting the global error string (when returning
62// NULL from a function).
63void SetError(const char* message); // makes a copy
64const char* GetError(); // retains ownership
65void ClearError();
66
67
Doug Zongker37bee622009-06-08 17:35:39 -070068typedef struct {
69 const char* name;
70 Function fn;
71} NamedFunction;
72
73// Register a new function. The same Function may be registered under
74// multiple names, but a given name should only be used once.
75void RegisterFunction(const char* name, Function fn);
76
77// Register all the builtins.
78void RegisterBuiltins();
79
80// Call this after all calls to RegisterFunction() but before parsing
81// any scripts to finish building the function table.
82void FinishRegistration();
83
84// Find the Function for a given name; return NULL if no such function
85// exists.
86Function FindFunction(const char* name);
87
Doug Zongker9931f7f2009-06-10 14:11:53 -070088
89// --- convenience functions for use in functions ---
90
91// Evaluate the expressions in argv, giving 'count' char* (the ... is
92// zero or more char** to put them in). If any expression evaluates
93// to NULL, free the rest and return -1. Return 0 on success.
94int ReadArgs(void* cookie, Expr* argv[], int count, ...);
95
96// Evaluate the expressions in argv, returning an array of char*
97// results. If any evaluate to NULL, free the rest and return NULL.
98// The caller is responsible for freeing the returned array and the
99// strings it contains.
100char** ReadVarArgs(void* cookie, int argc, Expr* argv[]);
101
102
Doug Zongker37bee622009-06-08 17:35:39 -0700103#endif // _EXPRESSION_H