parser.c

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

mercurial