Sat, 19 Feb 2000 23:23:29 +0100
trunk: changeset 4
- Added include support in config file parser
- Added scatn()
- Fixed remalloczero()
- Fixed is_end() in numparser2.h -- EOF case was missing
0 | 1 | /* |
2 | * libtu/parser.c | |
3 | * | |
4 | * Copyright (c) Tuomo Valkonen 1999-2000. | |
5 | * | |
6 | * This file is distributed under the terms of the "Artistic License". | |
7 | * See the included file LICENSE for details. | |
8 | */ | |
9 | ||
10 | #include <string.h> | |
11 | ||
12 | #include "include/parser.h" | |
13 | #include "include/misc.h" | |
14 | #include "include/output.h" | |
15 | ||
16 | #define MAX_TOKENS 32 | |
17 | #define MAX_NEST 16 | |
18 | ||
19 | ||
20 | enum{ | |
21 | P_EOF, | |
22 | P_OK, | |
23 | P_ERROR, | |
24 | P_BEG_SECT, | |
25 | P_END_SECT | |
26 | }; | |
27 | ||
28 | ||
29 | static bool check_args(const Tokenizer *tokz, Token *tokens, int ntokens, | |
30 | const char *fmt); | |
31 | ||
32 | ||
33 | /* */ | |
34 | ||
1 | 35 | static bool opt_include(Tokenizer *tokz, int n, Token *toks); |
36 | ||
37 | ||
38 | static ConfOpt common_opts[]={ | |
39 | {"include", "s", opt_include, NULL}, | |
40 | {NULL, NULL, NULL, NULL} | |
41 | }; | |
42 | ||
43 | ||
44 | /* */ | |
45 | ||
0 | 46 | |
47 | static int read_statement(const ConfOpt **opt_ret, Token *tokens, | |
48 | int *ntok_ret, Tokenizer *tokz, | |
49 | const ConfOpt *options) | |
50 | { | |
51 | int ntokens=0; | |
52 | Token *tok=NULL; | |
53 | const ConfOpt *opt=NULL; | |
54 | int had_comma=0; | |
55 | int retval=P_OK; | |
56 | int e=0; | |
57 | int e_line=0; | |
58 | ||
59 | while(1){ | |
60 | if(ntokens==MAX_TOKENS-1){ | |
61 | e=E_TOKZ_TOKEN_LIMIT; | |
62 | goto ret_err; | |
63 | } | |
64 | ||
65 | tok=&tokens[ntokens]; | |
66 | ||
67 | if(!tokz_get_token(tokz, tok)) | |
68 | goto ret_err; | |
69 | ||
70 | ntokens++; | |
71 | ||
72 | if(!TOK_IS_OP(tok)){ | |
73 | if(ntokens==1 && !had_comma){ | |
74 | if(!TOK_IS_IDENT(tok)){ | |
75 | e=E_TOKZ_IDENTIFIER_EXPECTED; | |
76 | goto ret_err; | |
77 | } | |
78 | ||
79 | /* find the option */ | |
80 | for(opt=options; opt->optname; opt++){ | |
81 | if(strcmp(opt->optname, TOK_IDENT_VAL(tok))==0) | |
82 | break; | |
83 | } | |
84 | ||
1 | 85 | if(opt->optname==NULL){ |
86 | /* common opt? include, etc. */ | |
87 | for(opt=common_opts; opt->optname; opt++){ | |
88 | if(strcmp(opt->optname, TOK_IDENT_VAL(tok))==0) | |
89 | break; | |
90 | } | |
91 | if(opt->optname==NULL){ | |
92 | e=E_TOKZ_UNKNOWN_OPTION; | |
93 | e_line=tok->line; | |
94 | retval=P_ERROR; | |
95 | } | |
0 | 96 | } |
97 | ||
98 | had_comma=2; | |
99 | }else{ | |
100 | if(!had_comma){ | |
101 | e=E_TOKZ_SYNTAX; | |
102 | goto ret_err; | |
103 | } | |
104 | ||
105 | had_comma=0; | |
106 | } | |
107 | continue; | |
108 | } | |
109 | ||
110 | /* It is an operator */ | |
111 | ||
112 | switch(TOK_OP_VAL(tok)){ | |
113 | case OP_SCOLON: | |
114 | if(opt){ | |
115 | if(had_comma || opt->opts){ | |
116 | e=E_TOKZ_SYNTAX; | |
117 | goto ret_err; | |
118 | } | |
119 | goto ret_success; | |
120 | } | |
121 | break; | |
122 | ||
123 | case OP_NEXTLINE: | |
124 | if(had_comma==1){ | |
125 | e=E_TOKZ_SYNTAX; | |
126 | e_line=tok->line-1; | |
127 | goto ret_err2; | |
128 | } | |
129 | ||
130 | if(opt && !opt->opts) | |
131 | goto ret_success; | |
132 | break; | |
133 | ||
134 | case OP_EOF: | |
135 | if(had_comma==1){ | |
136 | e=E_TOKZ_UNEXPECTED_EOF; | |
137 | goto ret_err; | |
138 | } | |
139 | ||
140 | if(opt && opt->opts){ | |
141 | e=E_TOKZ_UNEXPECTED_EOF; | |
142 | goto ret_err; | |
143 | } | |
144 | ||
145 | retval=P_EOF; | |
146 | goto ret_success; | |
147 | ||
148 | case OP_R_BRC: | |
149 | if(had_comma==1){ | |
150 | e=E_TOKZ_SYNTAX; | |
151 | goto ret_err; | |
152 | } | |
153 | ||
154 | if(opt && opt->opts){ | |
155 | e=E_TOKZ_SYNTAX; | |
156 | goto ret_err; | |
157 | } | |
158 | ||
159 | retval=P_END_SECT; | |
160 | goto ret_success; | |
161 | ||
162 | case OP_L_BRC: | |
163 | if(had_comma==1 || !opt || !opt->opts){ | |
164 | e=E_TOKZ_SYNTAX; | |
165 | goto ret_err; | |
166 | } | |
167 | ||
168 | retval=P_BEG_SECT; | |
169 | goto ret_success; | |
170 | ||
171 | case OP_COMMA: | |
172 | if(had_comma){ | |
173 | e=E_TOKZ_SYNTAX; | |
174 | goto ret_err; | |
175 | } | |
176 | had_comma=1; | |
177 | break; | |
178 | ||
179 | default: | |
180 | e=E_TOKZ_SYNTAX; | |
181 | goto ret_err; | |
182 | } | |
183 | ||
184 | ntokens--; | |
185 | } | |
186 | ||
187 | ret_err: | |
188 | e_line=tok->line; | |
189 | ret_err2: | |
190 | retval=P_ERROR; | |
191 | ||
192 | ret_success: | |
193 | if(retval==P_ERROR && e!=0) | |
194 | tokz_warn_error(tokz, e_line, e); | |
195 | ||
196 | *opt_ret=opt; | |
197 | *ntok_ret=ntokens; | |
198 | ||
199 | return retval; | |
200 | } | |
201 | ||
202 | ||
203 | /* */ | |
204 | ||
205 | ||
206 | static bool call_end_sect(Tokenizer *tokz, const ConfOpt *options) | |
207 | { | |
208 | bool retval=TRUE; | |
209 | ||
210 | while(options->optname){ | |
211 | if(strcmp(options->optname, "#end")==0){ | |
212 | retval=options->fn(tokz, 0, NULL); | |
213 | break; | |
214 | } | |
215 | options++; | |
216 | } | |
217 | ||
218 | return retval; | |
219 | } | |
220 | ||
221 | ||
222 | static void call_cancel_sect(Tokenizer *tokz, const ConfOpt *options) | |
223 | { | |
224 | while(options->optname){ | |
225 | if(strcmp(options->optname, "#cancel")==0){ | |
226 | options->fn(tokz, 0, NULL); | |
227 | break; | |
228 | } | |
229 | options++; | |
230 | } | |
231 | } | |
232 | ||
233 | ||
234 | /* */ | |
235 | ||
236 | ||
237 | /* Does the parsing work | |
238 | */ | |
239 | bool parse_config_tokz(Tokenizer *tokz, const ConfOpt *options) | |
240 | { | |
241 | Token tokens[MAX_TOKENS]; | |
242 | bool alloced_optstack=FALSE; | |
243 | int i, t, ntokens; | |
244 | int init_nest_lvl; | |
245 | bool had_error=FALSE; | |
246 | ||
247 | /* Allocate tokz->optstack if it does not yet exist (if it does, | |
248 | * we have been called from an option handler) | |
249 | */ | |
250 | if(!tokz->optstack){ | |
251 | tokz->optstack=ALLOC_N(const ConfOpt*, MAX_NEST); | |
252 | if(!tokz->optstack){ | |
253 | warn_err(); | |
254 | return FALSE; | |
255 | } | |
256 | ||
257 | memset(tokz->optstack, 0, sizeof(ConfOpt*)*MAX_NEST); | |
258 | init_nest_lvl=tokz->nest_lvl=0; | |
259 | alloced_optstack=TRUE; | |
260 | }else{ | |
261 | init_nest_lvl=tokz->nest_lvl; | |
262 | } | |
263 | ||
264 | tokz->optstack[init_nest_lvl]=options; | |
265 | ||
266 | for(i=0;i<MAX_TOKENS;i++) | |
267 | tok_init(&tokens[i]); | |
268 | ||
269 | ||
270 | /* The loop | |
271 | */ | |
272 | while(1){ | |
273 | t=read_statement(&options, tokens, &ntokens, | |
274 | tokz, tokz->optstack[tokz->nest_lvl]); | |
275 | ||
276 | had_error=(t==P_ERROR); | |
277 | ||
278 | /* Check that arguments are ok */ | |
279 | if(!had_error && options) | |
280 | had_error=!check_args(tokz, tokens, ntokens, options->argfmt); | |
281 | ||
282 | if(tokz->flags&TOKZ_PARSER_INDENT_MODE) | |
283 | verbose_indent(tokz->nest_lvl); | |
284 | ||
285 | /* New section? */ | |
286 | if(t==P_BEG_SECT){ | |
287 | if(tokz->nest_lvl==MAX_NEST-1){ | |
288 | tokz_warn_error(tokz, tokz->line, E_TOKZ_MAX_NEST); | |
289 | had_error=TRUE; | |
290 | while(ntokens--) | |
291 | tok_free(&tokens[ntokens]); | |
292 | break; | |
293 | }else{ | |
294 | tokz->optstack[++tokz->nest_lvl]=options->opts; | |
295 | } | |
296 | } | |
297 | ||
298 | /* call the handler */ | |
299 | if(!had_error && options && options->fn) | |
300 | had_error=!options->fn(tokz, ntokens-1, tokens); | |
301 | ||
302 | /* free the tokens */ | |
303 | while(ntokens--) | |
304 | tok_free(&tokens[ntokens]); | |
305 | ||
306 | switch(t){ | |
307 | case P_BEG_SECT: | |
308 | if(!had_error) | |
309 | continue; | |
310 | /* #cancel handler should not be called when | |
311 | * error occured in section begin handler */ | |
312 | tokz->nest_lvl--; | |
313 | break; | |
314 | ||
315 | case P_EOF: | |
1 | 316 | if(tokz_popf(tokz)){ |
317 | if(!had_error) | |
318 | continue; | |
319 | }else if(tokz->nest_lvl>0){ | |
0 | 320 | tokz_warn_error(tokz, 0, E_TOKZ_UNEXPECTED_EOF); |
321 | had_error=TRUE; | |
322 | }else if(!had_error){ | |
323 | had_error=!call_end_sect(tokz, tokz->optstack[0]); | |
324 | } | |
325 | break; | |
326 | ||
327 | case P_END_SECT: | |
328 | if(tokz->nest_lvl==0){ | |
329 | tokz_warn_error(tokz, tokz->line, E_TOKZ_SYNTAX); | |
330 | had_error=TRUE; | |
331 | break; | |
332 | } | |
333 | ||
334 | if(!had_error) | |
335 | had_error=!call_end_sect(tokz, tokz->optstack[tokz->nest_lvl]); | |
336 | ||
337 | tokz->nest_lvl--; | |
338 | ||
339 | if(tokz->nest_lvl<init_nest_lvl) | |
340 | break; | |
341 | ||
342 | /* fall thru */ | |
343 | ||
344 | default: | |
345 | if(!had_error) | |
346 | continue; | |
347 | } | |
348 | break; | |
349 | } | |
350 | ||
351 | /* On error, call all the #cancel-handlers */ | |
352 | while(had_error && tokz->nest_lvl>=init_nest_lvl){ | |
353 | call_cancel_sect(tokz, tokz->optstack[tokz->nest_lvl]); | |
354 | tokz->nest_lvl--; | |
355 | } | |
356 | ||
357 | /* Free optstack if it was alloced by this call */ | |
358 | if(alloced_optstack){ | |
359 | free(tokz->optstack); | |
360 | tokz->optstack=NULL; | |
361 | tokz->nest_lvl=0; | |
362 | } | |
363 | ||
364 | if(tokz->flags&TOKZ_PARSER_INDENT_MODE) | |
365 | verbose_indent(init_nest_lvl); | |
366 | ||
367 | return !had_error; | |
368 | } | |
369 | ||
370 | ||
371 | /* */ | |
372 | ||
373 | ||
374 | bool parse_config(const char *fname, const ConfOpt *options) | |
375 | { | |
376 | Tokenizer *tokz; | |
377 | bool ret; | |
378 | ||
379 | tokz=tokz_open(fname); | |
380 | ||
381 | if(tokz==NULL) | |
382 | return FALSE; | |
383 | ||
384 | ret=parse_config_tokz(tokz, options); | |
385 | ||
386 | tokz_close(tokz); | |
387 | ||
388 | return ret; | |
389 | } | |
390 | ||
391 | ||
392 | bool parse_config_file(FILE *file, const ConfOpt *options) | |
393 | { | |
394 | Tokenizer *tokz; | |
395 | bool ret; | |
396 | ||
397 | tokz=tokz_open_file(file); | |
398 | ||
399 | if(tokz==NULL) | |
400 | return FALSE; | |
401 | ||
402 | ret=parse_config_tokz(tokz, options); | |
403 | ||
404 | tokz_close(tokz); | |
405 | ||
406 | return ret; | |
407 | } | |
408 | ||
409 | ||
410 | /* | |
411 | * Argument validity checking stuff | |
412 | */ | |
413 | ||
414 | ||
415 | static bool arg_match(Token *tok, char c) | |
416 | { | |
417 | static const char chs[]={0, 'l', 'd', 'c', 's', 'i', 0, 0}; | |
418 | char c2; | |
419 | ||
420 | if(c=='.' || c=='*') | |
421 | return TRUE; | |
422 | ||
423 | c2=chs[tok->type]; | |
424 | ||
425 | if(c2==c) | |
426 | return TRUE; | |
427 | ||
428 | if(c2=='c' && c=='l'){ | |
429 | TOK_SET_LONG(tok, TOK_CHAR_VAL(tok)); | |
430 | return TRUE; | |
431 | } | |
432 | ||
433 | if(c2=='l' && c=='c'){ | |
434 | TOK_SET_CHAR(tok, TOK_LONG_VAL(tok)); | |
435 | return TRUE; | |
436 | } | |
437 | ||
438 | if(c2=='l' && c=='d'){ | |
439 | TOK_SET_DOUBLE(tok, TOK_LONG_VAL(tok)); | |
440 | return TRUE; | |
441 | } | |
442 | ||
443 | return FALSE; | |
444 | } | |
445 | ||
446 | ||
447 | static bool check_argument(const char **pret, Token *tok, const char *p) | |
448 | { | |
449 | int mode=0; | |
450 | ||
451 | if(*p=='*'){ | |
452 | *pret=p; | |
453 | return TRUE; | |
454 | }else if(*p=='?'){ | |
455 | mode=1; | |
456 | p++; | |
457 | }else if(*p==':'){ | |
458 | mode=2; | |
459 | p++; | |
460 | }else if(*p=='+'){ | |
461 | *pret=p; | |
462 | return arg_match(tok, *(p-1)); | |
463 | } | |
464 | ||
465 | while(*p!='\0'){ | |
466 | if(arg_match(tok, *p)){ | |
467 | p++; | |
468 | while(mode==2 && *p==':'){ | |
469 | if(*++p=='\0') | |
470 | break; /* invalid argument format string it is... */ | |
471 | p++; | |
472 | } | |
473 | *pret=p; | |
474 | return TRUE; | |
475 | } | |
476 | ||
477 | if(mode==0) | |
478 | break; | |
479 | ||
480 | p++; | |
481 | ||
482 | if(mode==1){ | |
483 | *pret=p; | |
484 | return TRUE; | |
485 | } | |
486 | ||
487 | if(*p!=':') | |
488 | break; | |
489 | p++; | |
490 | } | |
491 | ||
492 | *pret=p; | |
493 | return FALSE; | |
494 | } | |
495 | ||
496 | ||
497 | static bool args_at_end(const char *p) | |
498 | { | |
499 | if(p==NULL) | |
500 | return TRUE; | |
501 | ||
502 | while(*p!='\0'){ | |
503 | if(*p=='*' || *p=='+') | |
504 | p++; | |
505 | else if(*p=='?') | |
506 | p+=2; | |
507 | else | |
508 | return FALSE; | |
509 | } | |
510 | ||
511 | return TRUE; | |
512 | } | |
513 | ||
514 | ||
515 | static bool check_args(const Tokenizer *tokz, Token *tokens, int ntokens, | |
516 | const char *fmt) | |
517 | { | |
518 | int i; | |
519 | ||
520 | if(fmt==NULL) | |
521 | return ntokens==2; | |
522 | ||
523 | for(i=1; i<ntokens-1; i++){ | |
524 | if(!check_argument(&fmt, &tokens[i], fmt)){ | |
525 | tokz_warn_error(tokz, tokens[i].line, | |
526 | *fmt!='\0' ? E_TOKZ_INVALID_ARGUMENT | |
527 | : E_TOKZ_TOO_MANY_ARGS); | |
528 | return FALSE; | |
529 | } | |
530 | } | |
531 | ||
532 | if(!args_at_end(fmt)){ | |
533 | tokz_warn_error(tokz, tokens[i].line, E_TOKZ_TOO_FEW_ARGS); | |
534 | return FALSE; | |
535 | } | |
536 | ||
537 | return TRUE; | |
538 | } | |
539 | ||
1 | 540 | |
541 | /* */ | |
542 | ||
543 | ||
544 | static bool opt_include(Tokenizer *tokz, int n, Token *toks) | |
545 | { | |
546 | const char *fname=TOK_STRING_VAL(toks+1); | |
547 | const char *lastndx; | |
548 | char *tmpname; | |
549 | bool retval; | |
550 | ||
551 | if(fname[0]=='/' || tokz->name==NULL) | |
552 | goto thisdir; | |
553 | ||
554 | lastndx=strrchr(tokz->name, '/'); | |
555 | ||
556 | if(lastndx==NULL) | |
557 | goto thisdir; | |
558 | ||
559 | tmpname=scatn(tokz->name, lastndx-tokz->name+1, fname, -1); | |
560 | ||
561 | if(tmpname==NULL){ | |
562 | warn_err(); | |
563 | return FALSE; | |
564 | } | |
565 | ||
566 | retval=tokz_pushf(tokz, tmpname); | |
567 | ||
568 | free(tmpname); | |
569 | ||
570 | return retval; | |
571 | ||
572 | thisdir: | |
573 | return tokz_pushf(tokz, fname); | |
574 | } | |
575 |