BadVPN – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /** |
2 | * @file BPredicate.h |
||
3 | * @author Ambroz Bizjak <ambrop7@gmail.com> |
||
4 | * |
||
5 | * @section LICENSE |
||
6 | * |
||
7 | * Redistribution and use in source and binary forms, with or without |
||
8 | * modification, are permitted provided that the following conditions are met: |
||
9 | * 1. Redistributions of source code must retain the above copyright |
||
10 | * notice, this list of conditions and the following disclaimer. |
||
11 | * 2. Redistributions in binary form must reproduce the above copyright |
||
12 | * notice, this list of conditions and the following disclaimer in the |
||
13 | * documentation and/or other materials provided with the distribution. |
||
14 | * 3. Neither the name of the author nor the |
||
15 | * names of its contributors may be used to endorse or promote products |
||
16 | * derived from this software without specific prior written permission. |
||
17 | * |
||
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
||
19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
||
20 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
||
21 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
||
22 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
||
23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
||
24 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
||
25 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
||
27 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
28 | * |
||
29 | * @section DESCRIPTION |
||
30 | * |
||
31 | * Object that parses and evaluates a logical expression. |
||
32 | * Allows the user to define custom functions than can be |
||
33 | * used in the expression. |
||
34 | * |
||
35 | * Syntax and semantics for logical expressions: |
||
36 | * |
||
37 | * - true |
||
38 | * Logical true constant. Evaluates to 1. |
||
39 | * |
||
40 | * - false |
||
41 | * Logical false constant. Evaluates to 0. |
||
42 | * |
||
43 | * - NOT expression |
||
44 | * Logical negation. If the expression evaluates to error, the |
||
45 | * negation evaluates to error. |
||
46 | * |
||
47 | * - expression OR expression |
||
48 | * Logical disjunction. The second expression is only evaluated |
||
49 | * if the first expression evaluates to false. If a sub-expression |
||
50 | * evaluates to error, the disjunction evaluates to error. |
||
51 | * |
||
52 | * - expression AND expression |
||
53 | * Logical conjunction. The second expression is only evaluated |
||
54 | * if the first expression evaluates to true. If a sub-expression |
||
55 | * evaluates to error, the conjunction evaluates to error. |
||
56 | * |
||
57 | * - function(arg, ..., arg) |
||
58 | * Evaluation of a user-provided function (function is the name of the |
||
59 | * function, [a-zA-Z0-9_]+). |
||
60 | * If the function with the given name does not exist, it evaluates to |
||
61 | * error. |
||
62 | * Arguments are evaluated from left to right. Each argument can either |
||
63 | * be a logical expression or a string (characters enclosed in double |
||
64 | * quotes, without any double quote). |
||
65 | * If an argument is encountered, but all needed arguments have already |
||
66 | * been evaluated, the function evaluates to error. |
||
67 | * If an argument is of wrong type, it is not evaluated and the function |
||
68 | * evaluates to error. |
||
69 | * If an argument evaluates to error, the function evaluates to error. |
||
70 | * If after all arguments have been evaluated, the function needs more |
||
71 | * arguments, it evaluates to error. |
||
72 | * Then the handler function is called. If it returns anything other |
||
73 | * than 1 and 0, the function evaluates to error. Otherwise it evaluates |
||
74 | * to what the handler function returned. |
||
75 | */ |
||
76 | |||
77 | #ifndef BADVPN_PREDICATE_BPREDICATE_H |
||
78 | #define BADVPN_PREDICATE_BPREDICATE_H |
||
79 | |||
80 | #include <misc/debug.h> |
||
81 | #include <structure/BAVL.h> |
||
82 | #include <base/DebugObject.h> |
||
83 | |||
84 | #define PREDICATE_TYPE_BOOL 1 |
||
85 | #define PREDICATE_TYPE_STRING 2 |
||
86 | |||
87 | #define PREDICATE_MAX_NAME 16 |
||
88 | #define PREDICATE_MAX_ARGS 16 |
||
89 | |||
90 | /** |
||
91 | * Handler function called when evaluating a custom function in the predicate. |
||
92 | * |
||
93 | * @param user value passed to {@link BPredicateFunction_Init} |
||
94 | * @param args arguments to the function. Points to an array of pointers (as many as the |
||
95 | * function has arguments), where each pointer points to either to an int or |
||
96 | * a zero-terminated string (depending on the type of the argument). |
||
97 | * @return 1 for true, 0 for false, -1 for error |
||
98 | */ |
||
99 | typedef int (*BPredicate_callback) (void *user, void **args); |
||
100 | |||
101 | /** |
||
102 | * Object that parses and evaluates a logical expression. |
||
103 | * Allows the user to define custom functions than can be |
||
104 | * used in the expression. |
||
105 | */ |
||
106 | typedef struct { |
||
107 | DebugObject d_obj; |
||
108 | void *root; |
||
109 | BAVL functions_tree; |
||
110 | #ifndef NDEBUG |
||
111 | int in_function; |
||
112 | #endif |
||
113 | } BPredicate; |
||
114 | |||
115 | /** |
||
116 | * Object that represents a custom function in {@link BPredicate}. |
||
117 | */ |
||
118 | typedef struct { |
||
119 | DebugObject d_obj; |
||
120 | BPredicate *p; |
||
121 | char name[PREDICATE_MAX_NAME + 1]; |
||
122 | int args[PREDICATE_MAX_ARGS]; |
||
123 | int num_args; |
||
124 | BPredicate_callback callback; |
||
125 | void *user; |
||
126 | BAVLNode tree_node; |
||
127 | } BPredicateFunction; |
||
128 | |||
129 | /** |
||
130 | * Initializes the object. |
||
131 | * |
||
132 | * @param p the object |
||
133 | * @param str logical expression |
||
134 | * @return 1 on success, 0 on failure |
||
135 | */ |
||
136 | int BPredicate_Init (BPredicate *p, char *str) WARN_UNUSED; |
||
137 | |||
138 | /** |
||
139 | * Frees the object. |
||
140 | * Must have no custom functions. |
||
141 | * Must not be called from function handlers. |
||
142 | * |
||
143 | * @param p the object |
||
144 | */ |
||
145 | void BPredicate_Free (BPredicate *p); |
||
146 | |||
147 | /** |
||
148 | * Evaluates the logical expression. |
||
149 | * Must not be called from function handlers. |
||
150 | * |
||
151 | * @param p the object |
||
152 | * @return 1 for true, 0 for false, -1 for error |
||
153 | */ |
||
154 | int BPredicate_Eval (BPredicate *p); |
||
155 | |||
156 | /** |
||
157 | * Registers a custom function for {@link BPredicate}. |
||
158 | * Must not be called from function handlers. |
||
159 | * |
||
160 | * @param o the object |
||
161 | * @param p predicate to register the function for |
||
162 | * @param args array of argument types. Each type is either PREDICATE_TYPE_BOOL or PREDICATE_TYPE_STRING. |
||
163 | * @param num_args number of arguments for the function. Must be >=0 and <=PREDICATE_MAX_ARGS. |
||
164 | * @param callback handler to call to evaluate the function |
||
165 | * @param user value to pass to handler |
||
166 | */ |
||
167 | void BPredicateFunction_Init (BPredicateFunction *o, BPredicate *p, char *name, int *args, int num_args, BPredicate_callback callback, void *user); |
||
168 | |||
169 | /** |
||
170 | * Removes a custom function for {@link BPredicate}. |
||
171 | * Must not be called from function handlers. |
||
172 | * |
||
173 | * @param o the object |
||
174 | */ |
||
175 | void BPredicateFunction_Free (BPredicateFunction *o); |
||
176 | |||
177 | #endif |