BadVPN – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /**
2 * @file BPredicate_internal.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 * {@link BPredicate} expression tree definitions and functions.
32 */
33  
34 #ifndef BADVPN_PREDICATE_BPREDICATE_INTERNAL_H
35 #define BADVPN_PREDICATE_BPREDICATE_INTERNAL_H
36  
37 #include <misc/debug.h>
38  
39 #define NODE_CONSTANT 0
40 #define NODE_NEG 2
41 #define NODE_CONJUNCT 3
42 #define NODE_DISJUNCT 4
43 #define NODE_FUNCTION 5
44  
45 struct arguments_node;
46  
47 struct predicate_node {
48 int type;
49 union {
50 struct {
51 int val;
52 } constant;
53 struct {
54 struct predicate_node *op;
55 } neg;
56 struct {
57 struct predicate_node *op1;
58 struct predicate_node *op2;
59 } conjunct;
60 struct {
61 struct predicate_node *op1;
62 struct predicate_node *op2;
63 } disjunct;
64 struct {
65 char *name;
66 struct arguments_node *args;
67 } function;
68 };
69 int eval_value;
70 };
71  
72 #define ARGUMENT_INVALID 0
73 #define ARGUMENT_PREDICATE 1
74 #define ARGUMENT_STRING 2
75  
76 struct arguments_arg {
77 int type;
78 union {
79 struct predicate_node *predicate;
80 char *string;
81 };
82 };
83  
84 struct arguments_node {
85 struct arguments_arg arg;
86 struct arguments_node *next;
87 };
88  
89 static void free_predicate_node (struct predicate_node *root);
90 static void free_argument (struct arguments_arg arg);
91 static void free_arguments_node (struct arguments_node *n);
92  
93 void free_predicate_node (struct predicate_node *root)
94 {
95 ASSERT(root)
96  
97 switch (root->type) {
98 case NODE_CONSTANT:
99 break;
100 case NODE_NEG:
101 free_predicate_node(root->neg.op);
102 break;
103 case NODE_CONJUNCT:
104 free_predicate_node(root->conjunct.op1);
105 free_predicate_node(root->conjunct.op2);
106 break;
107 case NODE_DISJUNCT:
108 free_predicate_node(root->disjunct.op1);
109 free_predicate_node(root->disjunct.op2);
110 break;
111 case NODE_FUNCTION:
112 free(root->function.name);
113 if (root->function.args) {
114 free_arguments_node(root->function.args);
115 }
116 break;
117 default:
118 ASSERT(0)
119 break;
120 }
121  
122 free(root);
123 }
124  
125 void free_argument (struct arguments_arg arg)
126 {
127 switch (arg.type) {
128 case ARGUMENT_INVALID:
129 break;
130 case ARGUMENT_PREDICATE:
131 free_predicate_node(arg.predicate);
132 break;
133 case ARGUMENT_STRING:
134 free(arg.string);
135 break;
136 default:
137 ASSERT(0);
138 }
139 }
140  
141 void free_arguments_node (struct arguments_node *n)
142 {
143 ASSERT(n)
144  
145 free_argument(n->arg);
146  
147 if (n->next) {
148 free_arguments_node(n->next);
149 }
150  
151 free(n);
152 }
153  
154 #endif