Sat, 19 Jan 2002 19:14:36 +0100
trunk: changeset 38
Warning callbacks (thanks to Lukas Schroeder). libtu now depends on
the asprintf functions and one implementation is included in
snprintf_2.2/.
0 | 1 | /* |
2 | * libtu/parser.c | |
3 | * | |
9 | 4 | * Copyright (c) Tuomo Valkonen 1999-2000. |
0 | 5 | * See the included file LICENSE for details. |
6 | */ | |
7 | ||
8 | #include <string.h> | |
14 | 9 | #include <errno.h> |
0 | 10 | |
5 | 11 | #include <libtu/parser.h> |
12 | #include <libtu/misc.h> | |
13 | #include <libtu/output.h> | |
0 | 14 | |
13 | 15 | #define MAX_TOKENS 256 |
16 | #define MAX_NEST 256 | |
0 | 17 | |
18 | ||
19 | enum{ | |
2 | 20 | P_NONE=1, |
0 | 21 | P_EOF, |
2 | 22 | P_STMT, |
23 | P_STMT_NS, | |
24 | P_STMT_SECT, | |
0 | 25 | P_BEG_SECT, |
26 | P_END_SECT | |
27 | }; | |
28 | ||
29 | ||
20 | 30 | /* */ |
0 | 31 | |
32 | ||
1 | 33 | static bool opt_include(Tokenizer *tokz, int n, Token *toks); |
34 | ||
35 | ||
36 | static ConfOpt common_opts[]={ | |
37 | {"include", "s", opt_include, NULL}, | |
38 | {NULL, NULL, NULL, NULL} | |
39 | }; | |
40 | ||
41 | ||
42 | /* */ | |
43 | ||
0 | 44 | |
2 | 45 | static int read_statement(Tokenizer *tokz, Token *tokens, int *ntok_ret) |
0 | 46 | { |
47 | int ntokens=0; | |
48 | Token *tok=NULL; | |
2 | 49 | int had_comma=0; /* 0 - no, 1 - yes, 2 - not had, not expected */ |
50 | int retval=0; | |
0 | 51 | int e=0; |
52 | ||
53 | while(1){ | |
2 | 54 | tok=&tokens[ntokens]; |
55 | ||
56 | if(!tokz_get_token(tokz, tok)){ | |
57 | e=1; | |
58 | continue; | |
59 | } | |
60 | ||
0 | 61 | if(ntokens==MAX_TOKENS-1){ |
62 | e=E_TOKZ_TOKEN_LIMIT; | |
2 | 63 | tokz_warn_error(tokz, tok->line, e); |
64 | if(!(tokz->flags&TOKZ_ERROR_TOLERANT)) | |
65 | break; | |
66 | }else{ | |
67 | ntokens++; | |
0 | 68 | } |
69 | ||
70 | if(!TOK_IS_OP(tok)){ | |
71 | if(ntokens==1 && !had_comma){ | |
22 | 72 | /* first token */ |
0 | 73 | had_comma=2; |
74 | }else{ | |
2 | 75 | if(had_comma==0) |
76 | goto syntax; | |
0 | 77 | |
78 | had_comma=0; | |
79 | } | |
80 | continue; | |
81 | } | |
82 | ||
83 | /* It is an operator */ | |
2 | 84 | ntokens--; |
0 | 85 | |
86 | switch(TOK_OP_VAL(tok)){ | |
87 | case OP_SCOLON: | |
2 | 88 | retval=(ntokens==0 ? P_NONE : P_STMT_NS); |
0 | 89 | break; |
90 | ||
91 | case OP_NEXTLINE: | |
2 | 92 | retval=(ntokens==0 ? P_NONE : P_STMT); |
93 | break; | |
0 | 94 | |
2 | 95 | case OP_L_BRC: |
96 | retval=(ntokens==0 ? P_BEG_SECT : P_STMT_SECT); | |
0 | 97 | break; |
98 | ||
2 | 99 | case OP_R_BRC: |
100 | if(ntokens==0){ | |
101 | retval=P_END_SECT; | |
102 | }else{ | |
103 | tokz_unget_token(tokz, tok); | |
104 | retval=P_STMT_NS; | |
105 | } | |
106 | break; | |
107 | ||
0 | 108 | case OP_EOF: |
2 | 109 | retval=(ntokens==0 ? P_EOF : P_STMT_NS); |
110 | ||
0 | 111 | if(had_comma==1){ |
112 | e=E_TOKZ_UNEXPECTED_EOF; | |
2 | 113 | goto handle_error; |
0 | 114 | } |
115 | ||
2 | 116 | goto end; |
0 | 117 | |
118 | case OP_COMMA: | |
2 | 119 | if(had_comma!=0) |
120 | goto syntax; | |
121 | ||
0 | 122 | had_comma=1; |
2 | 123 | continue; |
0 | 124 | |
125 | default: | |
2 | 126 | goto syntax; |
0 | 127 | } |
128 | ||
2 | 129 | if(had_comma!=1) |
130 | break; | |
131 | ||
132 | syntax: | |
133 | e=E_TOKZ_SYNTAX; | |
134 | handle_error: | |
135 | tokz_warn_error(tokz, tok->line, e); | |
136 | ||
137 | if(!(tokz->flags&TOKZ_ERROR_TOLERANT) || retval!=0) | |
138 | break; | |
0 | 139 | } |
140 | ||
2 | 141 | end: |
142 | if(e!=0) | |
143 | retval=-retval; | |
144 | ||
0 | 145 | *ntok_ret=ntokens; |
146 | ||
147 | return retval; | |
148 | } | |
149 | ||
150 | ||
2 | 151 | static bool find_beg_sect(Tokenizer *tokz) |
152 | { | |
24 | 153 | Token tok=TOK_INIT; |
2 | 154 | |
155 | while(tokz_get_token(tokz, &tok)){ | |
156 | if(TOK_IS_OP(&tok)){ | |
157 | if(TOK_OP_VAL(&tok)==OP_NEXTLINE) | |
158 | continue; | |
159 | ||
160 | if(TOK_OP_VAL(&tok)==OP_SCOLON) | |
161 | return FALSE; | |
162 | ||
163 | if(TOK_OP_VAL(&tok)==OP_L_BRC) | |
164 | return TRUE; | |
165 | } | |
166 | ||
167 | tokz_unget_token(tokz, &tok); | |
168 | break; | |
169 | } | |
170 | return FALSE; | |
171 | } | |
172 | ||
173 | ||
0 | 174 | /* */ |
175 | ||
176 | ||
2 | 177 | static const ConfOpt* lookup_option(const ConfOpt *opts, const char *name) |
178 | { | |
179 | while(opts->optname!=NULL){ | |
180 | if(strcmp(opts->optname, name)==0) | |
181 | return opts; | |
182 | opts++; | |
183 | } | |
184 | return NULL; | |
185 | } | |
186 | ||
0 | 187 | |
2 | 188 | static bool call_end_sect(Tokenizer *tokz, const ConfOpt *opts) |
189 | { | |
190 | opts=lookup_option(opts, "#end"); | |
191 | if(opts!=NULL) | |
192 | return opts->fn(tokz, 0, NULL); | |
0 | 193 | |
2 | 194 | return TRUE; |
0 | 195 | } |
196 | ||
197 | ||
2 | 198 | static bool call_cancel_sect(Tokenizer *tokz, const ConfOpt *opts) |
199 | { | |
200 | opts=lookup_option(opts, "#cancel"); | |
201 | if(opts!=NULL) | |
202 | return opts->fn(tokz, 0, NULL); | |
203 | ||
204 | return TRUE; | |
0 | 205 | } |
206 | ||
207 | ||
208 | /* */ | |
209 | ||
210 | ||
211 | bool parse_config_tokz(Tokenizer *tokz, const ConfOpt *options) | |
212 | { | |
213 | Token tokens[MAX_TOKENS]; | |
214 | bool alloced_optstack=FALSE; | |
2 | 215 | int i, t, ntokens=0; |
0 | 216 | int init_nest_lvl; |
2 | 217 | bool had_error; |
218 | int errornest=0; | |
35 | 219 | bool is_default=FALSE; |
0 | 220 | |
221 | /* Allocate tokz->optstack if it does not yet exist (if it does, | |
222 | * we have been called from an option handler) | |
223 | */ | |
224 | if(!tokz->optstack){ | |
225 | tokz->optstack=ALLOC_N(const ConfOpt*, MAX_NEST); | |
226 | if(!tokz->optstack){ | |
227 | warn_err(); | |
228 | return FALSE; | |
229 | } | |
230 | ||
231 | memset(tokz->optstack, 0, sizeof(ConfOpt*)*MAX_NEST); | |
232 | init_nest_lvl=tokz->nest_lvl=0; | |
233 | alloced_optstack=TRUE; | |
234 | }else{ | |
235 | init_nest_lvl=tokz->nest_lvl; | |
236 | } | |
237 | ||
238 | tokz->optstack[init_nest_lvl]=options; | |
239 | ||
2 | 240 | for(i=0; i<MAX_TOKENS; i++) |
0 | 241 | tok_init(&tokens[i]); |
242 | ||
243 | ||
244 | while(1){ | |
2 | 245 | had_error=FALSE; |
0 | 246 | |
247 | /* free the tokens */ | |
248 | while(ntokens--) | |
249 | tok_free(&tokens[ntokens]); | |
250 | ||
2 | 251 | /* read the tokens */ |
252 | t=read_statement(tokz, tokens, &ntokens); | |
253 | ||
254 | if((had_error=t<0)) | |
255 | t=-t; | |
256 | ||
257 | switch(t){ | |
258 | case P_STMT: | |
259 | case P_STMT_NS: | |
260 | case P_STMT_SECT: | |
261 | ||
262 | if(errornest) | |
263 | had_error=TRUE; | |
264 | else if(tokz->flags&TOKZ_PARSER_INDENT_MODE) | |
265 | verbose_indent(tokz->nest_lvl); | |
266 | ||
267 | if(!TOK_IS_IDENT(tokens+0)){ | |
268 | had_error=TRUE; | |
269 | tokz_warn_error(tokz, tokens->line, | |
270 | E_TOKZ_IDENTIFIER_EXPECTED); | |
271 | } | |
272 | ||
273 | if(t==P_STMT){ | |
274 | if(find_beg_sect(tokz)) | |
275 | t=P_STMT_SECT; | |
276 | } | |
277 | ||
22 | 278 | if(had_error) |
279 | break; | |
280 | ||
281 | /* Got the statement and its type */ | |
282 | ||
2 | 283 | options=lookup_option(tokz->optstack[tokz->nest_lvl], |
284 | TOK_IDENT_VAL(tokens+0)); | |
285 | if(options==NULL) | |
286 | options=lookup_option(common_opts, TOK_IDENT_VAL(tokens+0)); | |
35 | 287 | if(options==NULL && (tokz->flags&TOKZ_DEFAULT_OPTION)){ |
288 | options=lookup_option(tokz->optstack[tokz->nest_lvl], "#default"); | |
289 | is_default=(options!=NULL); | |
290 | } | |
2 | 291 | |
292 | if(options==NULL){ | |
293 | had_error=TRUE; | |
294 | tokz_warn_error(tokz, tokens->line, E_TOKZ_UNKNOWN_OPTION); | |
35 | 295 | }else if(!is_default) { |
2 | 296 | had_error=!check_args(tokz, tokens, ntokens, options->argfmt); |
297 | } | |
298 | ||
299 | if(had_error) | |
300 | break; | |
301 | ||
22 | 302 | /* Found the option and arguments are ok */ |
303 | ||
2 | 304 | if(options->opts!=NULL){ |
305 | if(t!=P_STMT_SECT){ | |
306 | had_error=TRUE; | |
307 | tokz_warn_error(tokz, tokz->line, E_TOKZ_LBRACE_EXPECTED); | |
308 | }else if(tokz->nest_lvl==MAX_NEST-1){ | |
309 | tokz_warn_error(tokz, tokz->line, E_TOKZ_MAX_NEST); | |
310 | had_error=TRUE; | |
311 | }else{ | |
22 | 312 | tokz->nest_lvl++; |
313 | tokz->optstack[tokz->nest_lvl]=options->opts; | |
2 | 314 | } |
315 | }else if(t==P_STMT_SECT){ | |
316 | had_error=TRUE; | |
317 | tokz_warn_error(tokz, tokz->line, E_TOKZ_SYNTAX); | |
318 | } | |
319 | ||
320 | if(!had_error && options->fn!=NULL){ | |
321 | had_error=!options->fn(tokz, ntokens, tokens); | |
322 | if(t==P_STMT_SECT && had_error) | |
323 | tokz->nest_lvl--; | |
324 | } | |
0 | 325 | break; |
2 | 326 | |
0 | 327 | case P_EOF: |
1 | 328 | if(tokz_popf(tokz)){ |
2 | 329 | break; |
330 | }else if(tokz->nest_lvl>0 || errornest>0){ | |
0 | 331 | tokz_warn_error(tokz, 0, E_TOKZ_UNEXPECTED_EOF); |
332 | had_error=TRUE; | |
333 | } | |
2 | 334 | goto eof; |
335 | ||
336 | case P_BEG_SECT: | |
337 | had_error=TRUE; | |
338 | errornest++; | |
339 | tokz_warn_error(tokz, tokz->line, E_TOKZ_SYNTAX); | |
0 | 340 | break; |
2 | 341 | |
0 | 342 | case P_END_SECT: |
22 | 343 | if(tokz->nest_lvl+errornest==0){ |
0 | 344 | tokz_warn_error(tokz, tokz->line, E_TOKZ_SYNTAX); |
345 | had_error=TRUE; | |
22 | 346 | } |
347 | ||
348 | if(had_error) | |
0 | 349 | break; |
350 | ||
20 | 351 | if(errornest!=0){ |
352 | errornest--; | |
353 | }else{ | |
22 | 354 | had_error=!call_end_sect(tokz, tokz->optstack[tokz->nest_lvl]); |
20 | 355 | tokz->nest_lvl--; |
356 | } | |
0 | 357 | |
358 | if(tokz->nest_lvl<init_nest_lvl) | |
2 | 359 | goto eof; |
360 | } | |
361 | ||
362 | if(!had_error) | |
363 | continue; | |
364 | ||
22 | 365 | if(t==P_STMT_SECT) |
2 | 366 | errornest++; |
367 | ||
368 | if(!(tokz->flags&TOKZ_ERROR_TOLERANT)) | |
369 | break; | |
370 | } | |
0 | 371 | |
2 | 372 | eof: |
373 | /* free the tokens */ | |
374 | while(ntokens--) | |
375 | tok_free(&tokens[ntokens]); | |
376 | ||
377 | while(tokz->nest_lvl>=init_nest_lvl){ | |
378 | if(tokz->flags&TOKZ_ERROR_TOLERANT || !had_error) | |
379 | call_end_sect(tokz, tokz->optstack[tokz->nest_lvl]); | |
380 | else | |
381 | call_cancel_sect(tokz, tokz->optstack[tokz->nest_lvl]); | |
0 | 382 | tokz->nest_lvl--; |
383 | } | |
384 | ||
385 | /* Free optstack if it was alloced by this call */ | |
386 | if(alloced_optstack){ | |
387 | free(tokz->optstack); | |
388 | tokz->optstack=NULL; | |
389 | tokz->nest_lvl=0; | |
390 | } | |
391 | ||
392 | if(tokz->flags&TOKZ_PARSER_INDENT_MODE) | |
393 | verbose_indent(init_nest_lvl); | |
394 | ||
395 | return !had_error; | |
396 | } | |
397 | ||
398 | ||
399 | /* */ | |
400 | ||
401 | ||
2 | 402 | bool parse_config(const char *fname, const ConfOpt *options, int flags) |
0 | 403 | { |
404 | Tokenizer *tokz; | |
405 | bool ret; | |
406 | ||
407 | tokz=tokz_open(fname); | |
408 | ||
409 | if(tokz==NULL) | |
410 | return FALSE; | |
2 | 411 | |
412 | tokz->flags|=flags&~TOKZ_READ_COMMENTS; | |
0 | 413 | |
414 | ret=parse_config_tokz(tokz, options); | |
415 | ||
416 | tokz_close(tokz); | |
417 | ||
418 | return ret; | |
419 | } | |
420 | ||
421 | ||
2 | 422 | bool parse_config_file(FILE *file, const ConfOpt *options, int flags) |
0 | 423 | { |
424 | Tokenizer *tokz; | |
425 | bool ret; | |
426 | ||
14 | 427 | tokz=tokz_open_file(file, NULL); |
0 | 428 | |
429 | if(tokz==NULL) | |
430 | return FALSE; | |
431 | ||
2 | 432 | tokz->flags|=flags&~TOKZ_READ_COMMENTS; |
433 | ||
0 | 434 | ret=parse_config_tokz(tokz, options); |
435 | ||
436 | tokz_close(tokz); | |
437 | ||
438 | return ret; | |
439 | } | |
440 | ||
441 | ||
442 | /* | |
443 | * Argument validity checking stuff | |
444 | */ | |
445 | ||
446 | ||
2 | 447 | static int arg_match(Token *tok, char c) |
0 | 448 | { |
20 | 449 | char c2=tok->type; |
0 | 450 | |
451 | if(c=='.' || c=='*') | |
2 | 452 | return 0; |
0 | 453 | |
454 | if(c2==c) | |
2 | 455 | return 0; |
0 | 456 | |
457 | if(c2=='c' && c=='l'){ | |
458 | TOK_SET_LONG(tok, TOK_CHAR_VAL(tok)); | |
2 | 459 | return 0; |
0 | 460 | } |
461 | ||
462 | if(c2=='l' && c=='c'){ | |
463 | TOK_SET_CHAR(tok, TOK_LONG_VAL(tok)); | |
2 | 464 | return 0; |
0 | 465 | } |
466 | ||
467 | if(c2=='l' && c=='d'){ | |
468 | TOK_SET_DOUBLE(tok, TOK_LONG_VAL(tok)); | |
2 | 469 | return 0; |
0 | 470 | } |
471 | ||
17 | 472 | if(c=='b'){ |
473 | if(c2=='l'){ | |
474 | TOK_SET_BOOL(tok, TOK_LONG_VAL(tok)); | |
475 | return 0; | |
476 | }else if(c2=='i'){ | |
477 | if(strcmp(TOK_IDENT_VAL(tok), "TRUE")==0){ | |
478 | tok_free(tok); | |
479 | TOK_SET_BOOL(tok, TRUE); | |
480 | return 0; | |
481 | }else if(strcmp(TOK_IDENT_VAL(tok), "FALSE")==0){ | |
482 | tok_free(tok); | |
483 | TOK_SET_BOOL(tok, FALSE); | |
484 | return 0; | |
485 | } | |
486 | } | |
487 | } | |
488 | ||
2 | 489 | return E_TOKZ_INVALID_ARGUMENT; |
0 | 490 | } |
491 | ||
492 | ||
2 | 493 | static int check_argument(const char **pret, Token *tok, const char *p) |
0 | 494 | { |
2 | 495 | int mode; |
496 | int e=E_TOKZ_TOO_MANY_ARGS; | |
0 | 497 | |
2 | 498 | again: |
499 | mode=0; | |
500 | ||
0 | 501 | if(*p=='*'){ |
502 | *pret=p; | |
2 | 503 | return 0; |
0 | 504 | }else if(*p=='?'){ |
505 | mode=1; | |
506 | p++; | |
507 | }else if(*p==':'){ | |
508 | mode=2; | |
509 | p++; | |
510 | }else if(*p=='+'){ | |
511 | *pret=p; | |
512 | return arg_match(tok, *(p-1)); | |
513 | } | |
514 | ||
515 | while(*p!='\0'){ | |
2 | 516 | e=arg_match(tok, *p); |
517 | if(e==0){ | |
0 | 518 | p++; |
519 | while(mode==2 && *p==':'){ | |
520 | if(*++p=='\0') | |
2 | 521 | break; /* Invalid argument format string, though... */ |
0 | 522 | p++; |
523 | } | |
524 | *pret=p; | |
2 | 525 | return 0; |
0 | 526 | } |
527 | ||
528 | if(mode==0) | |
529 | break; | |
530 | ||
531 | p++; | |
532 | ||
2 | 533 | if(mode==1) |
534 | goto again; | |
535 | ||
536 | /* mode==2 */ | |
0 | 537 | |
538 | if(*p!=':') | |
539 | break; | |
540 | p++; | |
2 | 541 | e=E_TOKZ_TOO_MANY_ARGS; |
0 | 542 | } |
543 | ||
544 | *pret=p; | |
2 | 545 | return e; |
0 | 546 | } |
547 | ||
548 | ||
549 | static bool args_at_end(const char *p) | |
550 | { | |
551 | if(p==NULL) | |
552 | return TRUE; | |
553 | ||
554 | while(*p!='\0'){ | |
555 | if(*p=='*' || *p=='+') | |
556 | p++; | |
557 | else if(*p=='?') | |
558 | p+=2; | |
559 | else | |
560 | return FALSE; | |
561 | } | |
562 | ||
563 | return TRUE; | |
564 | } | |
565 | ||
566 | ||
20 | 567 | bool check_args(const Tokenizer *tokz, Token *tokens, int ntokens, |
568 | const char *fmt) | |
0 | 569 | { |
570 | int i; | |
2 | 571 | int e; |
572 | ||
573 | if(fmt==NULL){ | |
574 | if(ntokens!=1) | |
575 | tokz_warn_error(tokz, tokens[0].line, E_TOKZ_TOO_MANY_ARGS); | |
576 | return ntokens==1; | |
577 | } | |
0 | 578 | |
2 | 579 | for(i=1; i<ntokens; i++){ |
580 | e=check_argument(&fmt, &tokens[i], fmt); | |
581 | if(e!=0){ | |
582 | tokz_warn_error(tokz, tokens[i].line, e); | |
0 | 583 | return FALSE; |
584 | } | |
585 | } | |
586 | ||
587 | if(!args_at_end(fmt)){ | |
588 | tokz_warn_error(tokz, tokens[i].line, E_TOKZ_TOO_FEW_ARGS); | |
589 | return FALSE; | |
590 | } | |
591 | ||
592 | return TRUE; | |
593 | } | |
594 | ||
1 | 595 | |
596 | /* */ | |
597 | ||
598 | ||
14 | 599 | static bool try_include(Tokenizer *tokz, const char *fname) |
1 | 600 | { |
14 | 601 | FILE *f; |
602 | ||
603 | f=fopen(fname, "r"); | |
604 | ||
605 | if(f==NULL) | |
606 | return FALSE; | |
607 | ||
608 | if(!tokz_pushf_file(tokz, f, fname)){ | |
609 | fclose(f); | |
610 | return FALSE; | |
611 | } | |
612 | ||
613 | return TRUE; | |
614 | } | |
615 | ||
616 | ||
617 | static bool try_include_dir(Tokenizer *tokz, const char *dir, int dlen, | |
618 | const char *file) | |
619 | { | |
1 | 620 | char *tmpname; |
621 | bool retval; | |
622 | ||
14 | 623 | tmpname=scatn(dir, dlen, file, -1); |
1 | 624 | |
625 | if(tmpname==NULL){ | |
626 | warn_err(); | |
627 | return FALSE; | |
628 | } | |
629 | ||
14 | 630 | retval=try_include(tokz, tmpname); |
631 | ||
1 | 632 | free(tmpname); |
14 | 633 | |
1 | 634 | return retval; |
635 | } | |
636 | ||
14 | 637 | |
638 | static bool opt_include(Tokenizer *tokz, int n, Token *toks) | |
639 | { | |
640 | const char *fname=TOK_STRING_VAL(toks+1); | |
641 | const char *lastndx=NULL; | |
642 | bool retval, e; | |
643 | int i=0; | |
644 | ||
645 | if(fname[0]!='/' && tokz->name!=NULL) | |
646 | lastndx=strrchr(tokz->name, '/'); | |
647 | ||
648 | if(lastndx==NULL) | |
649 | retval=try_include(tokz, fname); | |
650 | else | |
651 | retval=try_include_dir(tokz, tokz->name, lastndx-tokz->name+1, fname); | |
652 | ||
653 | if(retval==TRUE) | |
654 | return TRUE; | |
655 | ||
656 | e=errno; | |
657 | ||
658 | if(tokz->includepaths!=NULL){ | |
659 | while(tokz->includepaths[i]!=NULL){ | |
660 | if(try_include_dir(tokz, tokz->includepaths[i], -1, fname)) | |
661 | return TRUE; | |
662 | i++; | |
663 | } | |
664 | } | |
665 | ||
666 | warn_obj(fname, "%s", strerror(e)); | |
667 | ||
668 | return FALSE; | |
669 | } | |
670 | ||
671 | ||
672 | extern void tokz_set_includepaths(Tokenizer *tokz, char **paths) | |
673 | { | |
674 | tokz->includepaths=paths; | |
675 | } | |
676 |