1 /* 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 #include "yydefs.h" 21 22 #define MAX_STRING_LEN 1024 23 24 typedef struct Expr Expr; 25 26 typedef 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 42 typedef char* (*Function)(const char* name, State* state, 43 int argc, Expr* argv[]); 44 45 struct Expr { 46 Function fn; 47 char* name; 48 int argc; 49 Expr** argv; 50 int start, end; 51 }; 52 53 char* Evaluate(State* state, Expr* expr); 54 55 // Glue to make an Expr out of a literal. 56 char* Literal(const char* name, State* state, int argc, Expr* argv[]); 57 58 // Functions corresponding to various syntactic sugar operators. 59 // ("concat" is also available as a builtin function, to concatenate 60 // more than two strings.) 61 char* ConcatFn(const char* name, State* state, int argc, Expr* argv[]); 62 char* LogicalAndFn(const char* name, State* state, int argc, Expr* argv[]); 63 char* LogicalOrFn(const char* name, State* state, int argc, Expr* argv[]); 64 char* LogicalNotFn(const char* name, State* state, int argc, Expr* argv[]); 65 char* SubstringFn(const char* name, State* state, int argc, Expr* argv[]); 66 char* EqualityFn(const char* name, State* state, int argc, Expr* argv[]); 67 char* InequalityFn(const char* name, State* state, int argc, Expr* argv[]); 68 char* SequenceFn(const char* name, State* state, int argc, Expr* argv[]); 69 70 // Convenience function for building expressions with a fixed number 71 // of arguments. 72 Expr* Build(Function fn, YYLTYPE loc, int count, ...); 73 74 // Global builtins, registered by RegisterBuiltins(). 75 char* IfElseFn(const char* name, State* state, int argc, Expr* argv[]); 76 char* AssertFn(const char* name, State* state, int argc, Expr* argv[]); 77 char* AbortFn(const char* name, State* state, int argc, Expr* argv[]); 78 79 80 // For setting and getting the global error string (when returning 81 // NULL from a function). 82 void SetError(const char* message); // makes a copy 83 const char* GetError(); // retains ownership 84 void ClearError(); 85 86 87 typedef 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. 94 void RegisterFunction(const char* name, Function fn); 95 96 // Register all the builtins. 97 void RegisterBuiltins(); 98 99 // Call this after all calls to RegisterFunction() but before parsing 100 // any scripts to finish building the function table. 101 void FinishRegistration(); 102 103 // Find the Function for a given name; return NULL if no such function 104 // exists. 105 Function FindFunction(const char* name); 106 107 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. 113 int ReadArgs(State* state, Expr* argv[], int count, ...); 114 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. 119 char** ReadVarArgs(State* state, int argc, Expr* argv[]); 120 121 // Use printf-style arguments to compose an error message to put into 122 // *state. Returns NULL. 123 char* ErrorAbort(State* state, char* format, ...); 124 125 126 #endif // _EXPRESSION_H 127