Mon, 24 Jul 2000 16:38:12 +0200
trunk: changeset 22
Fixed a bug with OPT_NO_DASH
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 | ||
30 | static bool check_args(const Tokenizer *tokz, Token *tokens, int ntokens, | |
31 | const char *fmt); | |
32 | ||
33 | ||
34 | /* */ | |
35 | ||
1 | 36 | static bool opt_include(Tokenizer *tokz, int n, Token *toks); |
37 | ||
38 | ||
39 | static ConfOpt common_opts[]={ | |
40 | {"include", "s", opt_include, NULL}, | |
41 | {NULL, NULL, NULL, NULL} | |
42 | }; | |
43 | ||
44 | ||
45 | /* */ | |
46 | ||
0 | 47 | |
2 | 48 | static int read_statement(Tokenizer *tokz, Token *tokens, int *ntok_ret) |
0 | 49 | { |
50 | int ntokens=0; | |
51 | Token *tok=NULL; | |
2 | 52 | int had_comma=0; /* 0 - no, 1 - yes, 2 - not had, not expected */ |
53 | int retval=0; | |
0 | 54 | int e=0; |
55 | ||
56 | while(1){ | |
2 | 57 | tok=&tokens[ntokens]; |
58 | ||
59 | if(!tokz_get_token(tokz, tok)){ | |
60 | e=1; | |
61 | continue; | |
62 | } | |
63 | ||
0 | 64 | if(ntokens==MAX_TOKENS-1){ |
65 | e=E_TOKZ_TOKEN_LIMIT; | |
2 | 66 | tokz_warn_error(tokz, tok->line, e); |
67 | if(!(tokz->flags&TOKZ_ERROR_TOLERANT)) | |
68 | break; | |
69 | }else{ | |
70 | ntokens++; | |
0 | 71 | } |
72 | ||
73 | if(!TOK_IS_OP(tok)){ | |
74 | if(ntokens==1 && !had_comma){ | |
2 | 75 | /*if(!TOK_IS_IDENT(tok)){ |
0 | 76 | e=E_TOKZ_IDENTIFIER_EXPECTED; |
2 | 77 | goto handle_error; |
78 | }*/ | |
0 | 79 | |
80 | had_comma=2; | |
81 | }else{ | |
2 | 82 | if(had_comma==0) |
83 | goto syntax; | |
0 | 84 | |
85 | had_comma=0; | |
86 | } | |
87 | continue; | |
88 | } | |
89 | ||
90 | /* It is an operator */ | |
2 | 91 | ntokens--; |
0 | 92 | |
93 | switch(TOK_OP_VAL(tok)){ | |
94 | case OP_SCOLON: | |
2 | 95 | retval=(ntokens==0 ? P_NONE : P_STMT_NS); |
0 | 96 | break; |
97 | ||
98 | case OP_NEXTLINE: | |
2 | 99 | retval=(ntokens==0 ? P_NONE : P_STMT); |
100 | break; | |
0 | 101 | |
2 | 102 | case OP_L_BRC: |
103 | retval=(ntokens==0 ? P_BEG_SECT : P_STMT_SECT); | |
0 | 104 | break; |
105 | ||
2 | 106 | case OP_R_BRC: |
107 | if(ntokens==0){ | |
108 | retval=P_END_SECT; | |
109 | }else{ | |
110 | tokz_unget_token(tokz, tok); | |
111 | retval=P_STMT_NS; | |
112 | } | |
113 | break; | |
114 | ||
0 | 115 | case OP_EOF: |
2 | 116 | retval=(ntokens==0 ? P_EOF : P_STMT_NS); |
117 | ||
0 | 118 | if(had_comma==1){ |
119 | e=E_TOKZ_UNEXPECTED_EOF; | |
2 | 120 | goto handle_error; |
0 | 121 | } |
122 | ||
2 | 123 | goto end; |
0 | 124 | |
125 | case OP_COMMA: | |
2 | 126 | if(had_comma!=0) |
127 | goto syntax; | |
128 | ||
0 | 129 | had_comma=1; |
2 | 130 | continue; |
0 | 131 | |
132 | default: | |
2 | 133 | goto syntax; |
0 | 134 | } |
135 | ||
2 | 136 | if(had_comma!=1) |
137 | break; | |
138 | ||
139 | syntax: | |
140 | e=E_TOKZ_SYNTAX; | |
141 | handle_error: | |
142 | tokz_warn_error(tokz, tok->line, e); | |
143 | ||
144 | if(!(tokz->flags&TOKZ_ERROR_TOLERANT) || retval!=0) | |
145 | break; | |
0 | 146 | } |
147 | ||
2 | 148 | end: |
149 | if(e!=0) | |
150 | retval=-retval; | |
151 | ||
0 | 152 | *ntok_ret=ntokens; |
153 | ||
154 | return retval; | |
155 | } | |
156 | ||
157 | ||
2 | 158 | static bool find_beg_sect(Tokenizer *tokz) |
159 | { | |
160 | Token tok; | |
161 | ||
162 | while(tokz_get_token(tokz, &tok)){ | |
163 | if(TOK_IS_OP(&tok)){ | |
164 | if(TOK_OP_VAL(&tok)==OP_NEXTLINE) | |
165 | continue; | |
166 | ||
167 | if(TOK_OP_VAL(&tok)==OP_SCOLON) | |
168 | return FALSE; | |
169 | ||
170 | if(TOK_OP_VAL(&tok)==OP_L_BRC) | |
171 | return TRUE; | |
172 | } | |
173 | ||
174 | tokz_unget_token(tokz, &tok); | |
175 | break; | |
176 | } | |
177 | return FALSE; | |
178 | } | |
179 | ||
180 | ||
0 | 181 | /* */ |
182 | ||
183 | ||
2 | 184 | static const ConfOpt* lookup_option(const ConfOpt *opts, const char *name) |
185 | { | |
186 | while(opts->optname!=NULL){ | |
187 | if(strcmp(opts->optname, name)==0) | |
188 | return opts; | |
189 | opts++; | |
190 | } | |
191 | return NULL; | |
192 | } | |
193 | ||
0 | 194 | |
2 | 195 | static bool call_end_sect(Tokenizer *tokz, const ConfOpt *opts) |
196 | { | |
197 | opts=lookup_option(opts, "#end"); | |
198 | if(opts!=NULL) | |
199 | return opts->fn(tokz, 0, NULL); | |
0 | 200 | |
2 | 201 | return TRUE; |
0 | 202 | } |
203 | ||
204 | ||
2 | 205 | static bool call_cancel_sect(Tokenizer *tokz, const ConfOpt *opts) |
206 | { | |
207 | opts=lookup_option(opts, "#cancel"); | |
208 | if(opts!=NULL) | |
209 | return opts->fn(tokz, 0, NULL); | |
210 | ||
211 | return TRUE; | |
0 | 212 | } |
213 | ||
214 | ||
215 | /* */ | |
216 | ||
217 | ||
218 | bool parse_config_tokz(Tokenizer *tokz, const ConfOpt *options) | |
219 | { | |
220 | Token tokens[MAX_TOKENS]; | |
221 | bool alloced_optstack=FALSE; | |
2 | 222 | int i, t, ntokens=0; |
0 | 223 | int init_nest_lvl; |
2 | 224 | bool had_error; |
225 | int errornest=0; | |
0 | 226 | |
227 | /* Allocate tokz->optstack if it does not yet exist (if it does, | |
228 | * we have been called from an option handler) | |
229 | */ | |
230 | if(!tokz->optstack){ | |
231 | tokz->optstack=ALLOC_N(const ConfOpt*, MAX_NEST); | |
232 | if(!tokz->optstack){ | |
233 | warn_err(); | |
234 | return FALSE; | |
235 | } | |
236 | ||
237 | memset(tokz->optstack, 0, sizeof(ConfOpt*)*MAX_NEST); | |
238 | init_nest_lvl=tokz->nest_lvl=0; | |
239 | alloced_optstack=TRUE; | |
240 | }else{ | |
241 | init_nest_lvl=tokz->nest_lvl; | |
242 | } | |
243 | ||
244 | tokz->optstack[init_nest_lvl]=options; | |
245 | ||
2 | 246 | for(i=0; i<MAX_TOKENS; i++) |
0 | 247 | tok_init(&tokens[i]); |
248 | ||
249 | ||
250 | /* The loop | |
251 | */ | |
252 | while(1){ | |
2 | 253 | had_error=FALSE; |
0 | 254 | |
255 | /* free the tokens */ | |
256 | while(ntokens--) | |
257 | tok_free(&tokens[ntokens]); | |
258 | ||
2 | 259 | /* read the tokens */ |
260 | t=read_statement(tokz, tokens, &ntokens); | |
261 | ||
262 | if((had_error=t<0)) | |
263 | t=-t; | |
264 | ||
265 | switch(t){ | |
266 | case P_STMT: | |
267 | case P_STMT_NS: | |
268 | case P_STMT_SECT: | |
269 | ||
270 | if(errornest) | |
271 | had_error=TRUE; | |
272 | else if(tokz->flags&TOKZ_PARSER_INDENT_MODE) | |
273 | verbose_indent(tokz->nest_lvl); | |
274 | ||
275 | if(!TOK_IS_IDENT(tokens+0)){ | |
276 | had_error=TRUE; | |
277 | tokz_warn_error(tokz, tokens->line, | |
278 | E_TOKZ_IDENTIFIER_EXPECTED); | |
279 | } | |
280 | ||
281 | if(had_error) | |
282 | break; | |
283 | ||
284 | if(t==P_STMT){ | |
285 | if(find_beg_sect(tokz)) | |
286 | t=P_STMT_SECT; | |
287 | } | |
288 | ||
289 | options=lookup_option(tokz->optstack[tokz->nest_lvl], | |
290 | TOK_IDENT_VAL(tokens+0)); | |
291 | if(options==NULL) | |
292 | options=lookup_option(common_opts, TOK_IDENT_VAL(tokens+0)); | |
293 | ||
294 | if(options==NULL){ | |
295 | had_error=TRUE; | |
296 | tokz_warn_error(tokz, tokens->line, E_TOKZ_UNKNOWN_OPTION); | |
297 | }else{ | |
298 | had_error=!check_args(tokz, tokens, ntokens, options->argfmt); | |
299 | } | |
300 | ||
301 | if(had_error) | |
302 | break; | |
303 | ||
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{ | |
312 | tokz->optstack[++tokz->nest_lvl]=options->opts; | |
313 | } | |
314 | }else if(t==P_STMT_SECT){ | |
315 | had_error=TRUE; | |
316 | tokz_warn_error(tokz, tokz->line, E_TOKZ_SYNTAX); | |
317 | } | |
318 | ||
319 | if(!had_error && options->fn!=NULL){ | |
320 | had_error=!options->fn(tokz, ntokens, tokens); | |
321 | if(t==P_STMT_SECT && had_error) | |
322 | tokz->nest_lvl--; | |
323 | } | |
0 | 324 | break; |
2 | 325 | |
0 | 326 | case P_EOF: |
1 | 327 | if(tokz_popf(tokz)){ |
2 | 328 | break; |
329 | }else if(tokz->nest_lvl>0 || errornest>0){ | |
0 | 330 | tokz_warn_error(tokz, 0, E_TOKZ_UNEXPECTED_EOF); |
331 | had_error=TRUE; | |
332 | } | |
2 | 333 | goto eof; |
334 | ||
335 | case P_BEG_SECT: | |
336 | had_error=TRUE; | |
337 | errornest++; | |
338 | tokz_warn_error(tokz, tokz->line, E_TOKZ_SYNTAX); | |
0 | 339 | break; |
2 | 340 | |
0 | 341 | case P_END_SECT: |
2 | 342 | if(errornest!=0){ |
343 | errornest--; | |
344 | break; | |
345 | } | |
346 | ||
0 | 347 | if(tokz->nest_lvl==0){ |
348 | tokz_warn_error(tokz, tokz->line, E_TOKZ_SYNTAX); | |
349 | had_error=TRUE; | |
350 | break; | |
351 | } | |
352 | ||
353 | if(!had_error) | |
354 | had_error=!call_end_sect(tokz, tokz->optstack[tokz->nest_lvl]); | |
355 | ||
356 | tokz->nest_lvl--; | |
357 | ||
358 | if(tokz->nest_lvl<init_nest_lvl) | |
2 | 359 | goto eof; |
360 | } | |
361 | ||
362 | if(!had_error) | |
363 | continue; | |
364 | ||
365 | if(t==P_STMT_SECT) | |
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 | { |
17 | 449 | static const char chs[]={0, 'l', 'd', 'c', 's', 'i', 'b', 0, 0}; |
0 | 450 | char c2; |
451 | ||
452 | if(c=='.' || c=='*') | |
2 | 453 | return 0; |
0 | 454 | |
455 | c2=chs[tok->type]; | |
456 | ||
457 | if(c2==c) | |
2 | 458 | return 0; |
0 | 459 | |
460 | if(c2=='c' && c=='l'){ | |
461 | TOK_SET_LONG(tok, TOK_CHAR_VAL(tok)); | |
2 | 462 | return 0; |
0 | 463 | } |
464 | ||
465 | if(c2=='l' && c=='c'){ | |
466 | TOK_SET_CHAR(tok, TOK_LONG_VAL(tok)); | |
2 | 467 | return 0; |
0 | 468 | } |
469 | ||
470 | if(c2=='l' && c=='d'){ | |
471 | TOK_SET_DOUBLE(tok, TOK_LONG_VAL(tok)); | |
2 | 472 | return 0; |
0 | 473 | } |
474 | ||
17 | 475 | if(c=='b'){ |
476 | if(c2=='l'){ | |
477 | TOK_SET_BOOL(tok, TOK_LONG_VAL(tok)); | |
478 | return 0; | |
479 | }else if(c2=='i'){ | |
480 | if(strcmp(TOK_IDENT_VAL(tok), "TRUE")==0){ | |
481 | tok_free(tok); | |
482 | TOK_SET_BOOL(tok, TRUE); | |
483 | return 0; | |
484 | }else if(strcmp(TOK_IDENT_VAL(tok), "FALSE")==0){ | |
485 | tok_free(tok); | |
486 | TOK_SET_BOOL(tok, FALSE); | |
487 | return 0; | |
488 | } | |
489 | } | |
490 | } | |
491 | ||
2 | 492 | return E_TOKZ_INVALID_ARGUMENT; |
0 | 493 | } |
494 | ||
495 | ||
2 | 496 | static int check_argument(const char **pret, Token *tok, const char *p) |
0 | 497 | { |
2 | 498 | int mode; |
499 | int e=E_TOKZ_TOO_MANY_ARGS; | |
0 | 500 | |
2 | 501 | again: |
502 | mode=0; | |
503 | ||
0 | 504 | if(*p=='*'){ |
505 | *pret=p; | |
2 | 506 | return 0; |
0 | 507 | }else if(*p=='?'){ |
508 | mode=1; | |
509 | p++; | |
510 | }else if(*p==':'){ | |
511 | mode=2; | |
512 | p++; | |
513 | }else if(*p=='+'){ | |
514 | *pret=p; | |
515 | return arg_match(tok, *(p-1)); | |
516 | } | |
517 | ||
518 | while(*p!='\0'){ | |
2 | 519 | e=arg_match(tok, *p); |
520 | if(e==0){ | |
0 | 521 | p++; |
522 | while(mode==2 && *p==':'){ | |
523 | if(*++p=='\0') | |
2 | 524 | break; /* Invalid argument format string, though... */ |
0 | 525 | p++; |
526 | } | |
527 | *pret=p; | |
2 | 528 | return 0; |
0 | 529 | } |
530 | ||
531 | if(mode==0) | |
532 | break; | |
533 | ||
534 | p++; | |
535 | ||
2 | 536 | if(mode==1) |
537 | goto again; | |
538 | ||
539 | /* mode==2 */ | |
0 | 540 | |
541 | if(*p!=':') | |
542 | break; | |
543 | p++; | |
2 | 544 | e=E_TOKZ_TOO_MANY_ARGS; |
0 | 545 | } |
546 | ||
547 | *pret=p; | |
2 | 548 | return e; |
0 | 549 | } |
550 | ||
551 | ||
552 | static bool args_at_end(const char *p) | |
553 | { | |
554 | if(p==NULL) | |
555 | return TRUE; | |
556 | ||
557 | while(*p!='\0'){ | |
558 | if(*p=='*' || *p=='+') | |
559 | p++; | |
560 | else if(*p=='?') | |
561 | p+=2; | |
562 | else | |
563 | return FALSE; | |
564 | } | |
565 | ||
566 | return TRUE; | |
567 | } | |
568 | ||
569 | ||
570 | static bool check_args(const Tokenizer *tokz, Token *tokens, int ntokens, | |
571 | const char *fmt) | |
572 | { | |
573 | int i; | |
2 | 574 | int e; |
575 | ||
576 | if(fmt==NULL){ | |
577 | if(ntokens!=1) | |
578 | tokz_warn_error(tokz, tokens[0].line, E_TOKZ_TOO_MANY_ARGS); | |
579 | return ntokens==1; | |
580 | } | |
0 | 581 | |
2 | 582 | for(i=1; i<ntokens; i++){ |
583 | e=check_argument(&fmt, &tokens[i], fmt); | |
584 | if(e!=0){ | |
585 | tokz_warn_error(tokz, tokens[i].line, e); | |
0 | 586 | return FALSE; |
587 | } | |
588 | } | |
589 | ||
590 | if(!args_at_end(fmt)){ | |
591 | tokz_warn_error(tokz, tokens[i].line, E_TOKZ_TOO_FEW_ARGS); | |
592 | return FALSE; | |
593 | } | |
594 | ||
595 | return TRUE; | |
596 | } | |
597 | ||
1 | 598 | |
599 | /* */ | |
600 | ||
601 | ||
14 | 602 | static bool try_include(Tokenizer *tokz, const char *fname) |
1 | 603 | { |
14 | 604 | FILE *f; |
605 | ||
606 | f=fopen(fname, "r"); | |
607 | ||
608 | if(f==NULL) | |
609 | return FALSE; | |
610 | ||
611 | if(!tokz_pushf_file(tokz, f, fname)){ | |
612 | fclose(f); | |
613 | return FALSE; | |
614 | } | |
615 | ||
616 | return TRUE; | |
617 | } | |
618 | ||
619 | ||
620 | static bool try_include_dir(Tokenizer *tokz, const char *dir, int dlen, | |
621 | const char *file) | |
622 | { | |
1 | 623 | char *tmpname; |
624 | bool retval; | |
625 | ||
14 | 626 | tmpname=scatn(dir, dlen, file, -1); |
1 | 627 | |
628 | if(tmpname==NULL){ | |
629 | warn_err(); | |
630 | return FALSE; | |
631 | } | |
632 | ||
14 | 633 | retval=try_include(tokz, tmpname); |
634 | ||
1 | 635 | free(tmpname); |
14 | 636 | |
1 | 637 | return retval; |
638 | } | |
639 | ||
14 | 640 | |
641 | static bool opt_include(Tokenizer *tokz, int n, Token *toks) | |
642 | { | |
643 | const char *fname=TOK_STRING_VAL(toks+1); | |
644 | const char *lastndx=NULL; | |
645 | bool retval, e; | |
646 | int i=0; | |
647 | ||
648 | if(fname[0]!='/' && tokz->name!=NULL) | |
649 | lastndx=strrchr(tokz->name, '/'); | |
650 | ||
651 | if(lastndx==NULL) | |
652 | retval=try_include(tokz, fname); | |
653 | else | |
654 | retval=try_include_dir(tokz, tokz->name, lastndx-tokz->name+1, fname); | |
655 | ||
656 | if(retval==TRUE) | |
657 | return TRUE; | |
658 | ||
659 | e=errno; | |
660 | ||
661 | if(tokz->includepaths!=NULL){ | |
662 | while(tokz->includepaths[i]!=NULL){ | |
663 | if(try_include_dir(tokz, tokz->includepaths[i], -1, fname)) | |
664 | return TRUE; | |
665 | i++; | |
666 | } | |
667 | } | |
668 | ||
669 | warn_obj(fname, "%s", strerror(e)); | |
670 | ||
671 | return FALSE; | |
672 | } | |
673 | ||
674 | ||
675 | extern void tokz_set_includepaths(Tokenizer *tokz, char **paths) | |
676 | { | |
677 | tokz->includepaths=paths; | |
678 | } | |
679 |