BadVPN – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /**
2 * @file NCDAst.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  
30 #ifndef BADVPN_NCDAST_H
31 #define BADVPN_NCDAST_H
32  
33 #include <stdint.h>
34 #include <stddef.h>
35  
36 #include <misc/debug.h>
37 #include <structure/LinkedList1.h>
38  
39 typedef struct NCDValue_s NCDValue;
40 typedef struct NCDProgram_s NCDProgram;
41 typedef struct NCDProgramElem_s NCDProgramElem;
42 typedef struct NCDProcess_s NCDProcess;
43 typedef struct NCDBlock_s NCDBlock;
44 typedef struct NCDStatement_s NCDStatement;
45 typedef struct NCDIfBlock_s NCDIfBlock;
46 typedef struct NCDIf_s NCDIf;
47  
48 struct NCDValue_s {
49 int type;
50 union {
51 struct {
52 uint8_t *string;
53 size_t string_len;
54 };
55 struct {
56 LinkedList1 list;
57 size_t list_count;
58 };
59 struct {
60 LinkedList1 map_list;
61 size_t map_count;
62 };
63 struct {
64 char *var_name;
65 };
66 struct {
67 NCDValue *invoc_func;
68 NCDValue *invoc_arg;
69 };
70 };
71 };
72  
73 struct NCDProgram_s {
74 LinkedList1 elems_list;
75 size_t num_elems;
76 };
77  
78 struct NCDBlock_s {
79 LinkedList1 statements_list;
80 size_t count;
81 };
82  
83 struct NCDProcess_s {
84 int is_template;
85 char *name;
86 NCDBlock block;
87 };
88  
89 struct NCDProgramElem_s {
90 int type;
91 union {
92 NCDProcess process;
93 struct {
94 char *path_data;
95 size_t path_length;
96 } include;
97 struct {
98 char *id_data;
99 size_t id_length;
100 } include_guard;
101 };
102 };
103  
104 struct NCDIfBlock_s {
105 LinkedList1 ifs_list;
106 };
107  
108 struct NCDStatement_s {
109 int type;
110 char *name;
111 union {
112 struct {
113 char *objname;
114 char *cmdname;
115 NCDValue args;
116 } reg;
117 struct {
118 NCDIfBlock ifblock;
119 int have_else;
120 NCDBlock else_block;
121 int iftype;
122 } ifc;
123 struct {
124 NCDValue collection;
125 char *name1;
126 char *name2;
127 NCDBlock block;
128 int is_grabbed;
129 } foreach;
130 struct {
131 NCDBlock block;
132 int is_grabbed;
133 } block;
134 };
135 };
136  
137 struct NCDIf_s {
138 NCDValue cond;
139 NCDBlock block;
140 };
141  
142 //
143  
144 #define NCDVALUE_STRING 1
145 #define NCDVALUE_LIST 2
146 #define NCDVALUE_MAP 3
147 #define NCDVALUE_VAR 4
148 #define NCDVALUE_INVOC 5
149  
150 #define NCDPROGRAMELEM_PROCESS 1
151 #define NCDPROGRAMELEM_INCLUDE 2
152 #define NCDPROGRAMELEM_INCLUDE_GUARD 3
153  
154 #define NCDSTATEMENT_REG 1
155 #define NCDSTATEMENT_IF 2
156 #define NCDSTATEMENT_FOREACH 3
157 #define NCDSTATEMENT_BLOCK 4
158  
159 #define NCDIFTYPE_IF 1
160 #define NCDIFTYPE_DO 2
161  
162 void NCDValue_Free (NCDValue *o);
163 int NCDValue_Type (NCDValue *o);
164 int NCDValue_InitString (NCDValue *o, const char *str) WARN_UNUSED;
165 int NCDValue_InitStringBin (NCDValue *o, const uint8_t *str, size_t len) WARN_UNUSED;
166 const char * NCDValue_StringValue (NCDValue *o);
167 size_t NCDValue_StringLength (NCDValue *o);
168 void NCDValue_InitList (NCDValue *o);
169 size_t NCDValue_ListCount (NCDValue *o);
170 int NCDValue_ListAppend (NCDValue *o, NCDValue v) WARN_UNUSED;
171 int NCDValue_ListPrepend (NCDValue *o, NCDValue v) WARN_UNUSED;
172 NCDValue * NCDValue_ListFirst (NCDValue *o);
173 NCDValue * NCDValue_ListNext (NCDValue *o, NCDValue *ev);
174 void NCDValue_InitMap (NCDValue *o);
175 size_t NCDValue_MapCount (NCDValue *o);
176 int NCDValue_MapPrepend (NCDValue *o, NCDValue key, NCDValue val) WARN_UNUSED;
177 NCDValue * NCDValue_MapFirstKey (NCDValue *o);
178 NCDValue * NCDValue_MapNextKey (NCDValue *o, NCDValue *ekey);
179 NCDValue * NCDValue_MapKeyValue (NCDValue *o, NCDValue *ekey);
180 int NCDValue_InitVar (NCDValue *o, const char *var_name) WARN_UNUSED;
181 const char * NCDValue_VarName (NCDValue *o);
182 int NCDValue_InitInvoc (NCDValue *o, NCDValue func, NCDValue arg) WARN_UNUSED;
183 NCDValue * NCDValue_InvocFunc (NCDValue *o);
184 NCDValue * NCDValue_InvocArg (NCDValue *o);
185  
186 void NCDProgram_Init (NCDProgram *o);
187 void NCDProgram_Free (NCDProgram *o);
188 NCDProgramElem * NCDProgram_PrependElem (NCDProgram *o, NCDProgramElem elem) WARN_UNUSED;
189 NCDProgramElem * NCDProgram_FirstElem (NCDProgram *o);
190 NCDProgramElem * NCDProgram_NextElem (NCDProgram *o, NCDProgramElem *ee);
191 size_t NCDProgram_NumElems (NCDProgram *o);
192 int NCDProgram_ContainsElemType (NCDProgram *o, int elem_type);
193 void NCDProgram_RemoveElem (NCDProgram *o, NCDProgramElem *ee);
194 int NCDProgram_ReplaceElemWithProgram (NCDProgram *o, NCDProgramElem *ee, NCDProgram replace_prog) WARN_UNUSED;
195  
196 void NCDProgramElem_InitProcess (NCDProgramElem *o, NCDProcess process);
197 int NCDProgramElem_InitInclude (NCDProgramElem *o, const char *path_data, size_t path_length) WARN_UNUSED;
198 int NCDProgramElem_InitIncludeGuard (NCDProgramElem *o, const char *id_data, size_t id_length) WARN_UNUSED;
199 void NCDProgramElem_Free (NCDProgramElem *o);
200 int NCDProgramElem_Type (NCDProgramElem *o);
201 NCDProcess * NCDProgramElem_Process (NCDProgramElem *o);
202 const char * NCDProgramElem_IncludePathData (NCDProgramElem *o);
203 size_t NCDProgramElem_IncludePathLength (NCDProgramElem *o);
204 const char * NCDProgramElem_IncludeGuardIdData (NCDProgramElem *o);
205 size_t NCDProgramElem_IncludeGuardIdLength (NCDProgramElem *o);
206  
207 int NCDProcess_Init (NCDProcess *o, int is_template, const char *name, NCDBlock block) WARN_UNUSED;
208 void NCDProcess_Free (NCDProcess *o);
209 int NCDProcess_IsTemplate (NCDProcess *o);
210 const char * NCDProcess_Name (NCDProcess *o);
211 NCDBlock * NCDProcess_Block (NCDProcess *o);
212  
213 void NCDBlock_Init (NCDBlock *o);
214 void NCDBlock_Free (NCDBlock *o);
215 int NCDBlock_PrependStatement (NCDBlock *o, NCDStatement s) WARN_UNUSED;
216 int NCDBlock_InsertStatementAfter (NCDBlock *o, NCDStatement *after, NCDStatement s) WARN_UNUSED;
217 NCDStatement * NCDBlock_ReplaceStatement (NCDBlock *o, NCDStatement *es, NCDStatement s);
218 NCDStatement * NCDBlock_FirstStatement (NCDBlock *o);
219 NCDStatement * NCDBlock_NextStatement (NCDBlock *o, NCDStatement *es);
220 size_t NCDBlock_NumStatements (NCDBlock *o);
221  
222 int NCDStatement_InitReg (NCDStatement *o, const char *name, const char *objname, const char *cmdname, NCDValue args) WARN_UNUSED;
223 int NCDStatement_InitIf (NCDStatement *o, const char *name, NCDIfBlock ifblock, int iftype) WARN_UNUSED;
224 int NCDStatement_InitForeach (NCDStatement *o, const char *name, NCDValue collection, const char *name1, const char *name2, NCDBlock block) WARN_UNUSED;
225 int NCDStatement_InitBlock (NCDStatement *o, const char *name, NCDBlock block) WARN_UNUSED;
226 void NCDStatement_Free (NCDStatement *o);
227 int NCDStatement_Type (NCDStatement *o);
228 const char * NCDStatement_Name (NCDStatement *o);
229 const char * NCDStatement_RegObjName (NCDStatement *o);
230 const char * NCDStatement_RegCmdName (NCDStatement *o);
231 NCDValue * NCDStatement_RegArgs (NCDStatement *o);
232 int NCDStatement_IfType (NCDStatement *o);
233 NCDIfBlock * NCDStatement_IfBlock (NCDStatement *o);
234 void NCDStatement_IfAddElse (NCDStatement *o, NCDBlock else_block);
235 NCDBlock * NCDStatement_IfElse (NCDStatement *o);
236 NCDBlock NCDStatement_IfGrabElse (NCDStatement *o);
237 NCDValue * NCDStatement_ForeachCollection (NCDStatement *o);
238 const char * NCDStatement_ForeachName1 (NCDStatement *o);
239 const char * NCDStatement_ForeachName2 (NCDStatement *o);
240 NCDBlock * NCDStatement_ForeachBlock (NCDStatement *o);
241 void NCDStatement_ForeachGrab (NCDStatement *o, NCDValue *out_collection, NCDBlock *out_block);
242 NCDBlock * NCDStatement_BlockBlock (NCDStatement *o);
243 NCDBlock NCDStatement_BlockGrabBlock (NCDStatement *o);
244  
245 void NCDIfBlock_Init (NCDIfBlock *o);
246 void NCDIfBlock_Free (NCDIfBlock *o);
247 int NCDIfBlock_PrependIf (NCDIfBlock *o, NCDIf ifc) WARN_UNUSED;
248 NCDIf * NCDIfBlock_FirstIf (NCDIfBlock *o);
249 NCDIf * NCDIfBlock_NextIf (NCDIfBlock *o, NCDIf *ei);
250 NCDIf NCDIfBlock_GrabIf (NCDIfBlock *o, NCDIf *ei);
251  
252 void NCDIf_Init (NCDIf *o, NCDValue cond, NCDBlock block);
253 void NCDIf_InitBlock (NCDIf *o, NCDBlock block);
254 void NCDIf_Free (NCDIf *o);
255 void NCDIf_FreeGrab (NCDIf *o, NCDValue *out_cond, NCDBlock *out_block);
256 NCDBlock NCDIf_FreeGrabBlock (NCDIf *o);
257 NCDValue * NCDIf_Cond (NCDIf *o);
258 NCDBlock * NCDIf_Block (NCDIf *o);
259  
260 #endif