]>
Commit | Line | Data |
---|---|---|
212380e3 AC |
1 | /* This code is in the public domain. |
2 | * $Nightmare: nightmare/src/main/parser.y,v 1.2.2.1.2.1 2002/07/02 03:42:10 ejb Exp $ | |
3 | * $Id: ircd_parser.y 871 2006-02-18 21:56:00Z nenolod $ | |
4 | */ | |
5 | ||
6 | %{ | |
7 | #include <sys/types.h> | |
8 | #include <sys/stat.h> | |
9 | ||
10 | #include <netinet/in.h> | |
11 | ||
12 | #include <string.h> | |
13 | #include <stdlib.h> | |
14 | #include <stdarg.h> | |
15 | #include <stdio.h> | |
16 | #define WE_ARE_MEMORY_C | |
17 | #include "stdinc.h" | |
18 | #include "setup.h" | |
19 | #include "common.h" | |
20 | #include "ircd_defs.h" | |
21 | #include "config.h" | |
22 | #include "client.h" | |
23 | #include "modules.h" | |
24 | #include "newconf.h" | |
25 | ||
26 | #define YY_NO_UNPUT | |
27 | ||
28 | int yyparse(); | |
29 | int yyerror(const char *); | |
30 | int yylex(); | |
31 | ||
32 | static time_t conf_find_time(char*); | |
33 | ||
34 | static struct { | |
35 | const char * name; | |
36 | const char * plural; | |
37 | time_t val; | |
38 | } ircd_times[] = { | |
39 | {"second", "seconds", 1}, | |
40 | {"minute", "minutes", 60}, | |
41 | {"hour", "hours", 60 * 60}, | |
42 | {"day", "days", 60 * 60 * 24}, | |
43 | {"week", "weeks", 60 * 60 * 24 * 7}, | |
44 | {"fortnight", "fortnights", 60 * 60 * 24 * 14}, | |
45 | {"month", "months", 60 * 60 * 24 * 7 * 4}, | |
46 | {"year", "years", 60 * 60 * 24 * 365}, | |
47 | /* ok-- we now do sizes here too. they aren't times, but | |
48 | it's close enough */ | |
49 | {"byte", "bytes", 1}, | |
50 | {"kb", NULL, 1024}, | |
51 | {"kbyte", "kbytes", 1024}, | |
52 | {"kilobyte", "kilebytes", 1024}, | |
53 | {"mb", NULL, 1024 * 1024}, | |
54 | {"mbyte", "mbytes", 1024 * 1024}, | |
55 | {"megabyte", "megabytes", 1024 * 1024}, | |
56 | {NULL, NULL, 0}, | |
57 | }; | |
58 | ||
59 | time_t conf_find_time(char *name) | |
60 | { | |
61 | int i; | |
62 | ||
63 | for (i = 0; ircd_times[i].name; i++) | |
64 | { | |
65 | if (strcasecmp(ircd_times[i].name, name) == 0 || | |
66 | (ircd_times[i].plural && strcasecmp(ircd_times[i].plural, name) == 0)) | |
67 | return ircd_times[i].val; | |
68 | } | |
69 | ||
70 | return 0; | |
71 | } | |
72 | ||
73 | static struct | |
74 | { | |
75 | const char *word; | |
76 | int yesno; | |
77 | } yesno[] = { | |
78 | {"yes", 1}, | |
79 | {"no", 0}, | |
80 | {"true", 1}, | |
81 | {"false", 0}, | |
82 | {"on", 1}, | |
83 | {"off", 0}, | |
84 | {NULL, 0} | |
85 | }; | |
86 | ||
87 | static int conf_get_yesno_value(char *str) | |
88 | { | |
89 | int i; | |
90 | ||
91 | for (i = 0; yesno[i].word; i++) | |
92 | { | |
93 | if (strcasecmp(str, yesno[i].word) == 0) | |
94 | { | |
95 | return yesno[i].yesno; | |
96 | } | |
97 | } | |
98 | ||
99 | return -1; | |
100 | } | |
101 | ||
102 | static void free_cur_list(conf_parm_t* list) | |
103 | { | |
104 | switch (list->type & CF_MTYPE) | |
105 | { | |
106 | case CF_STRING: | |
107 | case CF_QSTRING: | |
108 | MyFree(list->v.string); | |
109 | break; | |
110 | case CF_LIST: | |
111 | free_cur_list(list->v.list); | |
112 | break; | |
113 | default: break; | |
114 | } | |
115 | ||
116 | if (list->next) | |
117 | free_cur_list(list->next); | |
118 | } | |
119 | ||
120 | ||
121 | conf_parm_t * cur_list = NULL; | |
122 | ||
123 | static void add_cur_list_cpt(conf_parm_t *new) | |
124 | { | |
125 | if (cur_list == NULL) | |
126 | { | |
127 | cur_list = MyMalloc(sizeof(conf_parm_t)); | |
128 | cur_list->type |= CF_FLIST; | |
129 | cur_list->v.list = new; | |
130 | } | |
131 | else | |
132 | { | |
133 | new->next = cur_list->v.list; | |
134 | cur_list->v.list = new; | |
135 | } | |
136 | } | |
137 | ||
138 | static void add_cur_list(int type, char *str, int number) | |
139 | { | |
140 | conf_parm_t *new; | |
141 | ||
142 | new = MyMalloc(sizeof(conf_parm_t)); | |
143 | new->next = NULL; | |
144 | new->type = type; | |
145 | ||
146 | switch(type) | |
147 | { | |
148 | case CF_INT: | |
149 | case CF_TIME: | |
150 | case CF_YESNO: | |
151 | new->v.number = number; | |
152 | break; | |
153 | case CF_STRING: | |
154 | case CF_QSTRING: | |
155 | DupString(new->v.string, str); | |
156 | break; | |
157 | } | |
158 | ||
159 | add_cur_list_cpt(new); | |
160 | } | |
161 | ||
162 | ||
163 | %} | |
164 | ||
165 | %union { | |
166 | int number; | |
167 | char string[IRCD_BUFSIZE + 1]; | |
168 | conf_parm_t * conf_parm; | |
169 | } | |
170 | ||
171 | %token LOADMODULE TWODOTS | |
172 | ||
173 | %token <string> QSTRING STRING | |
174 | %token <number> NUMBER | |
175 | ||
176 | %type <string> qstring string | |
177 | %type <number> number timespec | |
178 | %type <conf_parm> oneitem single itemlist | |
179 | ||
180 | %start conf | |
181 | ||
182 | %% | |
183 | ||
184 | conf: | |
185 | | conf conf_item | |
186 | | error | |
187 | ; | |
188 | ||
189 | conf_item: block | |
190 | | loadmodule | |
191 | ; | |
192 | ||
193 | block: string | |
194 | { | |
195 | conf_start_block($1, NULL); | |
196 | } | |
197 | '{' block_items '}' ';' | |
198 | { | |
199 | if (conf_cur_block) | |
200 | conf_end_block(conf_cur_block); | |
201 | } | |
202 | | string qstring | |
203 | { | |
204 | conf_start_block($1, $2); | |
205 | } | |
206 | '{' block_items '}' ';' | |
207 | { | |
208 | if (conf_cur_block) | |
209 | conf_end_block(conf_cur_block); | |
210 | } | |
211 | ; | |
212 | ||
213 | block_items: block_items block_item | |
214 | | block_item | |
215 | ; | |
216 | ||
217 | block_item: string '=' itemlist ';' | |
218 | { | |
219 | conf_call_set(conf_cur_block, $1, cur_list, CF_LIST); | |
220 | free_cur_list(cur_list); | |
221 | cur_list = NULL; | |
222 | } | |
223 | ; | |
224 | ||
225 | itemlist: itemlist ',' single | |
226 | | single | |
227 | ; | |
228 | ||
229 | single: oneitem | |
230 | { | |
231 | add_cur_list_cpt($1); | |
232 | } | |
233 | | oneitem TWODOTS oneitem | |
234 | { | |
235 | /* "1 .. 5" meaning 1,2,3,4,5 - only valid for integers */ | |
236 | if (($1->type & CF_MTYPE) != CF_INT || | |
237 | ($3->type & CF_MTYPE) != CF_INT) | |
238 | { | |
239 | conf_report_error("Both arguments in '..' notation must be integers."); | |
240 | break; | |
241 | } | |
242 | else | |
243 | { | |
244 | int i; | |
245 | ||
246 | for (i = $1->v.number; i <= $3->v.number; i++) | |
247 | { | |
248 | add_cur_list(CF_INT, 0, i); | |
249 | } | |
250 | } | |
251 | } | |
252 | ; | |
253 | ||
254 | oneitem: qstring | |
255 | { | |
256 | $$ = MyMalloc(sizeof(conf_parm_t)); | |
257 | $$->type = CF_QSTRING; | |
258 | DupString($$->v.string, $1); | |
259 | } | |
260 | | timespec | |
261 | { | |
262 | $$ = MyMalloc(sizeof(conf_parm_t)); | |
263 | $$->type = CF_TIME; | |
264 | $$->v.number = $1; | |
265 | } | |
266 | | number | |
267 | { | |
268 | $$ = MyMalloc(sizeof(conf_parm_t)); | |
269 | $$->type = CF_INT; | |
270 | $$->v.number = $1; | |
271 | } | |
272 | | string | |
273 | { | |
274 | /* a 'string' could also be a yes/no value .. | |
275 | so pass it as that, if so */ | |
276 | int val = conf_get_yesno_value($1); | |
277 | ||
278 | $$ = MyMalloc(sizeof(conf_parm_t)); | |
279 | ||
280 | if (val != -1) | |
281 | { | |
282 | $$->type = CF_YESNO; | |
283 | $$->v.number = val; | |
284 | } | |
285 | else | |
286 | { | |
287 | $$->type = CF_STRING; | |
288 | DupString($$->v.string, $1); | |
289 | } | |
290 | } | |
291 | ; | |
292 | ||
293 | loadmodule: | |
294 | LOADMODULE QSTRING | |
295 | { | |
296 | #ifndef STATIC_MODULES | |
297 | char *m_bn; | |
298 | ||
299 | m_bn = irc_basename((char *) $2); | |
300 | ||
301 | if (findmodule_byname(m_bn) == -1) | |
302 | load_one_module($2, 0); | |
303 | #endif | |
304 | } | |
305 | ';' | |
306 | ; | |
307 | ||
308 | qstring: QSTRING { strcpy($$, $1); } ; | |
309 | string: STRING { strcpy($$, $1); } ; | |
310 | number: NUMBER { $$ = $1; } ; | |
311 | ||
312 | timespec: number string | |
313 | { | |
314 | time_t t; | |
315 | ||
316 | if ((t = conf_find_time($2)) == 0) | |
317 | { | |
318 | conf_report_error("Unrecognised time type/size '%s'", $2); | |
319 | t = 1; | |
320 | } | |
321 | ||
322 | $$ = $1 * t; | |
323 | } | |
324 | | timespec timespec | |
325 | { | |
326 | $$ = $1 + $2; | |
327 | } | |
328 | | timespec number | |
329 | { | |
330 | $$ = $1 + $2; | |
331 | } | |
332 | ; |