Debian Codegraph @

res/ael/ael.flex

raw
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2006, Digium, Inc.
5 *
6 * Steve Murphy <[email protected]>
7 *
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
13 *
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
17 */
18 /*! \file
19 *
20 * \brief Flex scanner description of tokens used in AEL2 .
21 *
22 */
23
24 /*
25 * Start with flex options:
26 *
27 * %x describes the contexts we have: paren, semic and argg, plus INITIAL
28 */
29 %x paren semic argg comment curlystate wordstate brackstate
30
31 /* prefix used for various globally-visible functions and variables.
32 * This renames also yywrap, but since we do not use it, we just
33 * add option noyywrap to remove it.
34 */
35 %option prefix="ael_yy"
36 %option noyywrap 8bit
37
38 /* I specify this option to suppress flex generating code with ECHO
39 in it. This generates compiler warnings in some systems; We've
40 seen the fwrite generate Unused variable warnings with 4.1.2 gcc.
41 Some systems have tweaked flex ECHO macro to keep the compiler
42 happy. To keep the warning message from getting output, I added
43 a default rule at the end of the patterns section */
44 %option nodefault
45
46 /* yyfree normally just frees its arg. It can be null sometimes,
47 which some systems will complain about, so, we'll define our own version */
48 %option noyyfree
49
50 /* batch gives a bit more performance if we are using it in
51 * a non-interactive mode. We probably don't care much.
52 */
53 %option batch
54
55 /* outfile is the filename to be used instead of lex.yy.c */
56 %option outfile="ael_lex.c"
57
58 /*
59 * These are not supported in flex 2.5.4, but we need them
60 * at the moment:
61 * reentrant produces a thread-safe parser. Not 100% sure that
62 * we require it, though.
63 * bison-bridge passes an additional yylval argument to yylex().
64 * bison-locations is probably not needed.
65 */
66 %option reentrant
67 %option bison-bridge
68 %option bison-locations
69
70 %{
71 #include <sys/types.h>
72 #include <sys/stat.h>
73 #include <unistd.h>
74 #include <glob.h>
75
76 #if !defined(GLOB_ABORTED)
77 #define GLOB_ABORTED GLOB_ABEND
78 #endif
79 #if !defined(GLOB_BRACE)
80 #define GLOB_BRACE 0
81 #endif
82 #if !defined(GLOB_NOMAGIC)
83 #define GLOB_NOMAGIC 0
84 #endif
85
86 #include "asterisk/logger.h"
87 #include "asterisk/utils.h"
88 #include "asterisk/lock.h"
89 #include "asterisk/hashtab.h"
90 #include "ael/ael.tab.h"
91 #include "asterisk/ael_structs.h"
92
93 /*
94 * A stack to keep track of matching brackets ( [ { } ] )
95 */
96 static char pbcstack[400]; /* XXX missing size checks */
97 static int pbcpos = 0;
98 static void pbcpush(char x);
99 static int pbcpop(char x);
100 static int parencount = 0;
101
102 /*
103 * A similar stack to keep track of matching brackets ( [ { } ] ) in word tokens surrounded by ${ ... }
104 */
105 static char pbcstack2[400]; /* XXX missing size checks */
106 static int pbcpos2 = 0;
107 static void pbcpush2(char x);
108 static int pbcpop2(char x);
109 static int parencount2 = 0;
110
111 /*
112 * A similar stack to keep track of matching brackets ( [ { } ] ) in word tokens surrounded by $[ ... ]
113 */
114 static char pbcstack3[400]; /* XXX missing size checks */
115 static int pbcpos3 = 0;
116 static void pbcpush3(char x);
117 static int pbcpop3(char x);
118 static int parencount3 = 0;
119
120
121 /*
122 * current line, column and filename, updated as we read the input.
123 */
124 static int my_lineno = 1; /* current line in the source */
125 static int my_col = 1; /* current column in the source */
126 char *my_file = 0; /* used also in the bison code */
127 char *prev_word; /* XXX document it */
128
129 #define MAX_INCLUDE_DEPTH 50
130
131 /*
132 * flex is not too smart, and generates global functions
133 * without prototypes so the compiler may complain.
134 * To avoid that, we declare the prototypes here,
135 * even though these functions are not used.
136 */
137 int ael_yyget_column (yyscan_t yyscanner);
138 void ael_yyset_column (int column_no , yyscan_t yyscanner);
139
140 int ael_yyparse (struct parse_io *);
141
142 /*
143 * A stack to process include files.
144 * As we switch into the new file we need to store the previous
145 * state to restore it later.
146 */
147 struct stackelement {
148 char *fname;
149 int lineno;
150 int colno;
151 glob_t globbuf; /* the current globbuf */
152 int globbuf_pos; /* where we are in the current globbuf */
153 YY_BUFFER_STATE bufstate;
154 };
155
156 static struct stackelement include_stack[MAX_INCLUDE_DEPTH];
157 static int include_stack_index = 0;
158 static void setup_filestack(char *fnamebuf, int fnamebuf_siz, glob_t *globbuf, int globpos, yyscan_t xscan, int create);
159
160 /*
161 * if we use the @n feature of bison, we must supply the start/end
162 * location of tokens in the structure pointed by yylloc.
163 * Simple tokens are just assumed to be on the same line, so
164 * the line number is constant, and the column is incremented
165 * by the length of the token.
166 */
167 #ifdef FLEX_BETA /* set for 2.5.33 */
168
169 /* compute the total number of lines and columns in the text
170 * passed as argument.
171 */
172 static void pbcwhere(const char *text, int *line, int *col )
173 {
174 int loc_line = *line;
175 int loc_col = *col;
176 char c;
177 while ( (c = *text++) ) {
178 if ( c == '\t' ) {
179 loc_col += 8 - (loc_col % 8);
180 } else if ( c == '\n' ) {
181 loc_line++;
182 loc_col = 1;
183 } else
184 loc_col++;
185 }
186 *line = loc_line;
187 *col = loc_col;
188 }
189
190 #define STORE_POS do { \
191 yylloc->first_line = yylloc->last_line = my_lineno; \
192 yylloc->first_column=my_col; \
193 yylloc->last_column=my_col+yyleng-1; \
194 my_col+=yyleng; \
195 } while (0)
196
197 #define STORE_LOC do { \
198 yylloc->first_line = my_lineno; \
199 yylloc->first_column=my_col; \
200 pbcwhere(yytext, &my_lineno, &my_col); \
201 yylloc->last_line = my_lineno; \
202 yylloc->last_column = my_col - 1; \
203 } while (0)
204 #else
205 #define STORE_POS
206 #define STORE_LOC
207 #endif
208 %}
209
210 KEYWORD (context|abstract|extend|macro|globals|local|ignorepat|switch|if|ifTime|random|regexten|hint|else|goto|jump|return|break|continue|for|while|case|default|pattern|catch|switches|eswitches|includes)
211
212 NOPARENS ([^()\[\]\{\}]|\\[()\[\]\{\}])*
213
214 NOARGG ([^(),\{\}\[\]]|\\[,()\[\]\{\}])*
215
216 NOSEMIC ([^;()\{\}\[\]]|\\[;()\[\]\{\}])*
217
218 HIBIT [\x80-\xff]
219
220 %%
221
222 \{ { STORE_POS; return LC;}
223 \} { STORE_POS; return RC;}
224 \( { STORE_POS; return LP;}
225 \) { STORE_POS; return RP;}
226 \; { STORE_POS; return SEMI;}
227 \= { STORE_POS; return EQ;}
228 \, { STORE_POS; return COMMA;}
229 \: { STORE_POS; return COLON;}
230 \& { STORE_POS; return AMPER;}
231 \| { STORE_POS; return BAR;}
232 \=\> { STORE_POS; return EXTENMARK;}
233 \@ { STORE_POS; return AT;}
234 \/\/[^\n]* {/*comment*/}
235 context { STORE_POS; return KW_CONTEXT;}
236 abstract { STORE_POS; return KW_ABSTRACT;}
237 extend { STORE_POS; return KW_EXTEND;}
238 macro { STORE_POS; return KW_MACRO;};
239 globals { STORE_POS; return KW_GLOBALS;}
240 local { STORE_POS; return KW_LOCAL;}
241 ignorepat { STORE_POS; return KW_IGNOREPAT;}
242 switch { STORE_POS; return KW_SWITCH;}
243 if { STORE_POS; return KW_IF;}
244 ifTime { STORE_POS; return KW_IFTIME;}
245 random { STORE_POS; return KW_RANDOM;}
246 regexten { STORE_POS; return KW_REGEXTEN;}
247 hint { STORE_POS; return KW_HINT;}
248 else { STORE_POS; return KW_ELSE;}
249 goto { STORE_POS; return KW_GOTO;}
250 jump { STORE_POS; return KW_JUMP;}
251 return { STORE_POS; return KW_RETURN;}
252 break { STORE_POS; return KW_BREAK;}
253 continue { STORE_POS; return KW_CONTINUE;}
254 for { STORE_POS; return KW_FOR;}
255 while { STORE_POS; return KW_WHILE;}
256 case { STORE_POS; return KW_CASE;}
257 default { STORE_POS; return KW_DEFAULT;}
258 pattern { STORE_POS; return KW_PATTERN;}
259 catch { STORE_POS; return KW_CATCH;}
260 switches { STORE_POS; return KW_SWITCHES;}
261 eswitches { STORE_POS; return KW_ESWITCHES;}
262 includes { STORE_POS; return KW_INCLUDES;}
263 "/*" { BEGIN(comment); my_col += 2; }
264
265 <comment>[^*\n]* { my_col += yyleng; }
266 <comment>[^*\n]*\n { ++my_lineno; my_col=1;}
267 <comment>"*"+[^*/\n]* { my_col += yyleng; }
268 <comment>"*"+[^*/\n]*\n { ++my_lineno; my_col=1;}
269 <comment>"*/" { my_col += 2; BEGIN(INITIAL); } /* the nice thing about comments is that you know exactly what ends them */
270
271 \n { my_lineno++; my_col = 1; }
272 [ ]+ { my_col += yyleng; }
273 [\t]+ { my_col += (yyleng*8)-(my_col%8); }
274
275 ({KEYWORD}?[-a-zA-Z0-9'"_/.\<\>\*\+!$#\[\]]|{HIBIT}|(\\.)|(\$\{)|(\$\[)) {
276 /* boy did I open a can of worms when I changed the lexical token "word".
277 all the above keywords can be used as a beginning to a "word".-
278 before, a "word" would match a longer sequence than the above
279 keywords, and all would be well. But now "word" is a single char
280 and feeds into a statemachine sort of sequence from there on. So...
281 I added the {KEYWORD}? to the beginning of the word match sequence */
282
283 if (!strcmp(yytext,"${")) {
284 parencount2 = 0;
285 pbcpos2 = 0;
286 pbcpush2('{'); /* push '{' so the last pcbpop (parencount2 = -1) will succeed */
287 BEGIN(curlystate);
288 yymore();
289 } else if (!strcmp(yytext,"$[")) {
290 parencount3 = 0;
291 pbcpos3 = 0;
292 pbcpush3('['); /* push '[' so the last pcbpop (parencount3 = -1) will succeed */
293 BEGIN(brackstate);
294 yymore();
295 } else {
296 BEGIN(wordstate);
297 yymore();
298 }
299 }
300
301 <wordstate>[-a-zA-Z0-9'"_/.\<\>\*\+!$#\[\]] { yymore(); /* Keep going */ }
302 <wordstate>{HIBIT} { yymore(); /* Keep going */ }
303 <wordstate>(\\.) { yymore(); /* Keep Going */ }
304 <wordstate>(\$\{) { /* the beginning of a ${} construct. prepare and pop into curlystate */
305 parencount2 = 0;
306 pbcpos2 = 0;
307 pbcpush2('{'); /* push '{' so the last pcbpop (parencount2 = -1) will succeed */
308 BEGIN(curlystate);
309 yymore();
310 }
311 <wordstate>(\$\[) { /* the beginning of a $[] construct. prepare and pop into brackstate */
312 parencount3 = 0;
313 pbcpos3 = 0;
314 pbcpush3('['); /* push '[' so the last pcbpop (parencount3 = -1) will succeed */
315 BEGIN(brackstate);
316 yymore();
317 }
318 <wordstate>([^a-zA-Z0-9\x80-\xff\x2d'"_/.\<\>\*\+!$#\[\]]) {
319 /* a non-word constituent char, like a space, tab, curly, paren, etc */
320 char c = yytext[yyleng-1];
321 STORE_POS;
322 yylval->str = malloc(yyleng);
323 strncpy(yylval->str, yytext, yyleng);
324 yylval->str[yyleng-1] = 0;
325 unput(c); /* put this ending char back in the stream */
326 BEGIN(0);
327 return word;
328 }
329
330
331 <curlystate>{NOPARENS}\} {
332 if ( pbcpop2('}') ) { /* error */
333 STORE_LOC;
334 ast_log(LOG_ERROR,"File=%s, line=%d, column=%d: Mismatched ')' in expression: %s !\n", my_file, my_lineno, my_col, yytext);
335 BEGIN(0);
336 yylval->str = malloc(yyleng+1);
337 strncpy(yylval->str, yytext, yyleng);
338 yylval->str[yyleng] = 0;
339 return word;
340 }
341 parencount2--;
342 if ( parencount2 >= 0) {
343 yymore();
344 } else {
345 BEGIN(wordstate); /* Finished with the current ${} construct. Return to word gathering state */
346 yymore();
347 }
348 }
349
350 <curlystate>{NOPARENS}[\(\[\{] {
351 char c = yytext[yyleng-1];
352 if (c == '{')
353 parencount2++;
354 pbcpush2(c);
355 yymore();
356 }
357
358 <curlystate>{NOPARENS}[\]\)] {
359 char c = yytext[yyleng-1];
360 if ( pbcpop2(c)) { /* error */
361 STORE_LOC;
362 ast_log(LOG_ERROR,"File=%s, line=%d, column=%d: Mismatched '%c' in expression!\n",
363 my_file, my_lineno, my_col, c);
364 BEGIN(0);
365 yylval->str = malloc(yyleng+1);
366 strncpy(yylval->str, yytext, yyleng);
367 yylval->str[yyleng] = 0;
368 return word;
369 }
370 yymore();
371 }
372
373
374 <brackstate>{NOPARENS}\] {
375 if ( pbcpop3(']') ) { /* error */
376 STORE_LOC;
377 ast_log(LOG_ERROR,"File=%s, line=%d, column=%d: Mismatched ')' in expression: %s !\n", my_file, my_lineno, my_col, yytext);
378 BEGIN(0);
379 yylval->str = malloc(yyleng+1);
380 strncpy(yylval->str, yytext, yyleng);
381 yylval->str[yyleng] = 0;
382 return word;
383 }
384 parencount3--;
385 if ( parencount3 >= 0) {
386 yymore();
387 } else {
388 BEGIN(wordstate); /* Finished with the current ${} construct. Return to word gathering state */
389 yymore();
390 }
391 }
392
393 <brackstate>{NOPARENS}[\(\[\{] {
394 char c = yytext[yyleng-1];
395 if (c == '[')
396 parencount3++;
397 pbcpush3(c);
398 yymore();
399 }
400
401 <brackstate>{NOPARENS}[\}\)] {
402 char c = yytext[yyleng-1];
403 if ( pbcpop3(c)) { /* error */
404 STORE_LOC;
405 ast_log(LOG_ERROR,"File=%s, line=%d, column=%d: Mismatched '%c' in expression!\n",
406 my_file, my_lineno, my_col, c);
407 BEGIN(0);
408 yylval->str = malloc(yyleng+1);
409 strncpy(yylval->str, yytext, yyleng);
410 yylval->str[yyleng] = 0;
411 return word;
412 }
413 yymore();
414 }
415
416
417 /*
418 * context used for arguments of if_head, random_head, switch_head,
419 * for (last statement), while (XXX why not iftime_head ?).
420 * End with the matching parentheses.
421 * A comma at the top level is valid here, unlike in argg where it
422 * is an argument separator so it must be returned as a token.
423 */
424 <paren>{NOPARENS}\) {
425 if ( pbcpop(')') ) { /* error */
426 STORE_LOC;
427 ast_log(LOG_ERROR,"File=%s, line=%d, column=%d: Mismatched ')' in expression: %s !\n", my_file, my_lineno, my_col, yytext);
428 BEGIN(0);
429 yylval->str = malloc(yyleng+1);
430 strncpy(yylval->str, yytext, yyleng);
431 yylval->str[yyleng] = 0;
432 prev_word = 0;
433 return word;
434 }
435 parencount--;
436 if ( parencount >= 0) {
437 yymore();
438 } else {
439 STORE_LOC;
440 yylval->str = malloc(yyleng);
441 strncpy(yylval->str, yytext, yyleng);
442 yylval->str[yyleng-1] = 0;
443 unput(')');
444 BEGIN(0);
445 return word;
446 }
447 }
448
449 <paren>{NOPARENS}[\(\[\{] {
450 char c = yytext[yyleng-1];
451 if (c == '(')
452 parencount++;
453 pbcpush(c);
454 yymore();
455 }
456
457 <paren>{NOPARENS}[\]\}] {
458 char c = yytext[yyleng-1];
459 if ( pbcpop(c)) { /* error */
460 STORE_LOC;
461 ast_log(LOG_ERROR,"File=%s, line=%d, column=%d: Mismatched '%c' in expression!\n",
462 my_file, my_lineno, my_col, c);
463 BEGIN(0);
464 yylval->str = malloc(yyleng+1);
465 strncpy(yylval->str, yytext, yyleng);
466 yylval->str[yyleng] = 0;
467 return word;
468 }
469 yymore();
470 }
471
472
473 /*
474 * handlers for arguments to a macro or application calls.
475 * We enter this context when we find the initial '(' and
476 * stay here until we close all matching parentheses,
477 * and find the comma (argument separator) or the closing ')'
478 * of the (external) call, which happens when parencount == 0
479 * before the decrement.
480 */
481 <argg>{NOARGG}[\(\[\{] {
482 char c = yytext[yyleng-1];
483 if (c == '(')
484 parencount++;
485 pbcpush(c);
486 yymore();
487 }
488
489 <argg>{NOARGG}\) {
490 if ( pbcpop(')') ) { /* error */
491 STORE_LOC;
492 ast_log(LOG_ERROR,"File=%s, line=%d, column=%d: Mismatched ')' in expression!\n", my_file, my_lineno, my_col);
493 BEGIN(0);
494 yylval->str = malloc(yyleng+1);
495 strncpy(yylval->str, yytext, yyleng);
496 yylval->str[yyleng] = 0;
497 return word;
498 }
499
500 parencount--;
501 if( parencount >= 0){
502 yymore();
503 } else {
504 STORE_LOC;
505 BEGIN(0);
506 if ( !strcmp(yytext, ")") )
507 return RP;
508 yylval->str = malloc(yyleng);
509 strncpy(yylval->str, yytext, yyleng);
510 yylval->str[yyleng-1] = '\0'; /* trim trailing ')' */
511 unput(')');
512 return word;
513 }
514 }
515
516 <argg>{NOARGG}\, {
517 if( parencount != 0) { /* ast_log(LOG_NOTICE,"Folding in a comma!\n"); */
518 yymore();
519 } else {
520 STORE_LOC;
521 if( !strcmp(yytext,"," ) )
522 return COMMA;
523 yylval->str = malloc(yyleng);
524 strncpy(yylval->str, yytext, yyleng);
525 yylval->str[yyleng-1] = '\0'; /* trim trailing ',' */
526 unput(',');
527 return word;
528 }
529 }
530
531 <argg>{NOARGG}[\]\}] {
532 char c = yytext[yyleng-1];
533 if ( pbcpop(c) ) { /* error */
534 STORE_LOC;
535 ast_log(LOG_ERROR,"File=%s, line=%d, column=%d: Mismatched '%c' in expression!\n", my_file, my_lineno, my_col, c);
536 BEGIN(0);
537 yylval->str = malloc(yyleng+1);
538 strncpy(yylval->str, yytext, yyleng);
539 yylval->str[yyleng] = '\0';
540 return word;
541 }
542 yymore();
543 }
544
545 /*
546 * context used to find tokens in the right hand side of assignments,
547 * or in the first and second operand of a 'for'. As above, match
548 * commas and use ';' as a separator (hence return it as a separate token).
549 */
550 <semic>{NOSEMIC}[\(\[\{] {
551 char c = yytext[yyleng-1];
552 yymore();
553 pbcpush(c);
554 }
555
556 <semic>{NOSEMIC}[\)\]\}] {
557 char c = yytext[yyleng-1];
558 if ( pbcpop(c) ) { /* error */
559 STORE_LOC;
560 ast_log(LOG_ERROR,"File=%s, line=%d, column=%d: Mismatched '%c' in expression!\n", my_file, my_lineno, my_col, c);
561 BEGIN(0);
562 yylval->str = malloc(yyleng+1);
563 strncpy(yylval->str, yytext, yyleng);
564 yylval->str[yyleng] = '\0';
565 return word;
566 }
567 yymore();
568 }
569
570 <semic>{NOSEMIC}; {
571 STORE_LOC;
572 yylval->str = malloc(yyleng);
573 strncpy(yylval->str, yytext, yyleng);
574 yylval->str[yyleng-1] = '\0'; /* trim trailing ';' */
575 unput(';');
576 BEGIN(0);
577 return word;
578 }
579
580 \#include[ \t]+\"[^\"]+\" {
581 char fnamebuf[1024],*p1,*p2;
582 int glob_ret;
583 glob_t globbuf; /* the current globbuf */
584 int globbuf_pos = -1; /* where we are in the current globbuf */
585 globbuf.gl_offs = 0; /* initialize it to silence gcc */
586
587 p1 = strchr(yytext,'"');
588 p2 = strrchr(yytext,'"');
589 if ( include_stack_index >= MAX_INCLUDE_DEPTH ) {
590 ast_log(LOG_ERROR,"File=%s, line=%d, column=%d: Includes nested too deeply! Wow!!! How did you do that?\n", my_file, my_lineno, my_col);
591 } else if ( (int)(p2-p1) > sizeof(fnamebuf) - 1 ) {
592 ast_log(LOG_ERROR,"File=%s, line=%d, column=%d: Filename is incredibly way too long (%d chars!). Inclusion ignored!\n", my_file, my_lineno, my_col, yyleng - 10);
593 } else {
594 strncpy(fnamebuf, p1+1, p2-p1-1);
595 fnamebuf[p2-p1-1] = 0;
596 if (fnamebuf[0] != '/') {
597 char fnamebuf2[1024];
598 snprintf(fnamebuf2,sizeof(fnamebuf2), "%s/%s", (char *)ast_config_AST_CONFIG_DIR, fnamebuf);
599 ast_copy_string(fnamebuf,fnamebuf2,sizeof(fnamebuf));
600 }
601 #ifdef SOLARIS
602 glob_ret = glob(fnamebuf, GLOB_NOCHECK, NULL, &globbuf);
603 #else
604 glob_ret = glob(fnamebuf, GLOB_NOMAGIC|GLOB_BRACE, NULL, &globbuf);
605 #endif
606 if (glob_ret == GLOB_NOSPACE) {
607 ast_log(LOG_WARNING,
608 "Glob Expansion of pattern '%s' failed: Not enough memory\n", fnamebuf);
609 } else if (glob_ret == GLOB_ABORTED) {
610 ast_log(LOG_WARNING,
611 "Glob Expansion of pattern '%s' failed: Read error\n", fnamebuf);
612 } else if (glob_ret == GLOB_NOMATCH) {
613 ast_log(LOG_WARNING,
614 "Glob Expansion of pattern '%s' failed: No matches!\n", fnamebuf);
615 } else {
616 globbuf_pos = 0;
617 }
618 }
619 if (globbuf_pos > -1) {
620 setup_filestack(fnamebuf, sizeof(fnamebuf), &globbuf, 0, yyscanner, 1);
621 }
622 }
623
624
625 <<EOF>> {
626 char fnamebuf[2048];
627 if (include_stack_index > 0 && include_stack[include_stack_index-1].globbuf_pos < include_stack[include_stack_index-1].globbuf.gl_pathc-1) {
628 yy_delete_buffer( YY_CURRENT_BUFFER, yyscanner );
629 include_stack[include_stack_index-1].globbuf_pos++;
630 setup_filestack(fnamebuf, sizeof(fnamebuf), &include_stack[include_stack_index-1].globbuf, include_stack[include_stack_index-1].globbuf_pos, yyscanner, 0);
631 /* finish this */
632
633 } else {
634 if (include_stack[include_stack_index].fname) {
635 free(include_stack[include_stack_index].fname);
636 include_stack[include_stack_index].fname = 0;
637 }
638 if (my_file) {
639 free(my_file);
640 my_file = 0;
641 }
642 if ( --include_stack_index < 0 ) {
643 yyterminate();
644 } else {
645 globfree(&include_stack[include_stack_index].globbuf);
646 include_stack[include_stack_index].globbuf_pos = -1;
647
648 yy_delete_buffer( YY_CURRENT_BUFFER, yyscanner );
649 yy_switch_to_buffer(include_stack[include_stack_index].bufstate, yyscanner );
650 my_lineno = include_stack[include_stack_index].lineno;
651 my_col = include_stack[include_stack_index].colno;
652 my_file = strdup(include_stack[include_stack_index].fname);
653 }
654 }
655 }
656
657 <*>.|\n { /* default rule */ ast_log(LOG_ERROR,"Unhandled char(s): %s\n", yytext); }
658
659 %%
660
661 static void pbcpush(char x)
662 {
663 pbcstack[pbcpos++] = x;
664 }
665
666 void ael_yyfree(void *ptr, yyscan_t yyscanner)
667 {
668 if (ptr)
669 free( (char*) ptr );
670 }
671
672 static int pbcpop(char x)
673 {
674 if ( ( x == ')' && pbcstack[pbcpos-1] == '(' )
675 || ( x == ']' && pbcstack[pbcpos-1] == '[' )
676 || ( x == '}' && pbcstack[pbcpos-1] == '{' )) {
677 pbcpos--;
678 return 0;
679 }
680 return 1; /* error */
681 }
682
683 static void pbcpush2(char x)
684 {
685 pbcstack2[pbcpos2++] = x;
686 }
687
688 static int pbcpop2(char x)
689 {
690 if ( ( x == ')' && pbcstack2[pbcpos2-1] == '(' )
691 || ( x == ']' && pbcstack2[pbcpos2-1] == '[' )
692 || ( x == '}' && pbcstack2[pbcpos2-1] == '{' )) {
693 pbcpos2--;
694 return 0;
695 }
696 return 1; /* error */
697 }
698
699 static void pbcpush3(char x)
700 {
701 pbcstack3[pbcpos3++] = x;
702 }
703
704 static int pbcpop3(char x)
705 {
706 if ( ( x == ')' && pbcstack3[pbcpos3-1] == '(' )
707 || ( x == ']' && pbcstack3[pbcpos3-1] == '[' )
708 || ( x == '}' && pbcstack3[pbcpos3-1] == '{' )) {
709 pbcpos3--;
710 return 0;
711 }
712 return 1; /* error */
713 }
714
715 static int c_prevword(void)
716 {
717 char *c = prev_word;
718 if (c == NULL)
719 return 0;
720 while ( *c ) {
721 switch (*c) {
722 case '{':
723 case '[':
724 case '(':
725 pbcpush(*c);
726 break;
727 case '}':
728 case ']':
729 case ')':
730 if (pbcpop(*c))
731 return 1;
732 break;
733 }
734 c++;
735 }
736 return 0;
737 }
738
739
740 /*
741 * The following three functions, reset_*, are used in the bison
742 * code to switch context. As a consequence, we need to
743 * declare them global and add a prototype so that the
744 * compiler does not complain.
745 *
746 * NOTE: yyg is declared because it is used in the BEGIN macros,
747 * though that should be hidden as the macro changes
748 * depending on the flex options that we use - in particular,
749 * %reentrant changes the way the macro is declared;
750 * without %reentrant, BEGIN uses yystart instead of yyg
751 */
752
753 void reset_parencount(yyscan_t yyscanner );
754 void reset_parencount(yyscan_t yyscanner )
755 {
756 struct yyguts_t * yyg = (struct yyguts_t*)yyscanner;
757 parencount = 0;
758 pbcpos = 0;
759 pbcpush('('); /* push '(' so the last pcbpop (parencount= -1) will succeed */
760 c_prevword();
761 BEGIN(paren);
762 }
763
764 void reset_semicount(yyscan_t yyscanner );
765 void reset_semicount(yyscan_t yyscanner )
766 {
767 struct yyguts_t * yyg = (struct yyguts_t*)yyscanner;
768 pbcpos = 0;
769 BEGIN(semic);
770 }
771
772 void reset_argcount(yyscan_t yyscanner );
773 void reset_argcount(yyscan_t yyscanner )
774 {
775 struct yyguts_t * yyg = (struct yyguts_t*)yyscanner;
776 parencount = 0;
777 pbcpos = 0;
778 pbcpush('('); /* push '(' so the last pcbpop (parencount= -1) will succeed */
779 c_prevword();
780 BEGIN(argg);
781 }
782
783 /* used elsewhere, but some local vars */
784 struct pval *ael2_parse(char *filename, int *errors)
785 {
786 struct pval *pvalue;
787 struct parse_io *io;
788 char *buffer;
789 struct stat stats;
790 FILE *fin;
791
792 /* extern int ael_yydebug; */
793
794 io = calloc(sizeof(struct parse_io),1);
795 /* reset the global counters */
796 prev_word = 0;
797 my_lineno = 1;
798 include_stack_index=0;
799 my_col = 0;
800 /* ael_yydebug = 1; */
801 ael_yylex_init(&io->scanner);
802 fin = fopen(filename,"r");
803 if ( !fin ) {
804 ast_log(LOG_ERROR,"File %s could not be opened\n", filename);
805 *errors = 1;
806 return 0;
807 }
808 if (my_file)
809 free(my_file);
810 my_file = strdup(filename);
811 if (stat(filename, &stats)) {
812 ast_log(LOG_WARNING, "failed to populate stats from file '%s'\n", filename);
813 }
814 buffer = (char*)malloc(stats.st_size+2);
815 if (fread(buffer, 1, stats.st_size, fin) != stats.st_size) {
816 ast_log(LOG_ERROR, "fread() failed: %s\n", strerror(errno));
817 }
818 buffer[stats.st_size]=0;
819 fclose(fin);
820
821 ael_yy_scan_string (buffer ,io->scanner);
822 ael_yyset_lineno(1 , io->scanner);
823
824 /* ael_yyset_in (fin , io->scanner); OLD WAY */
825
826 ael_yyparse(io);
827
828
829 pvalue = io->pval;
830 *errors = io->syntax_error_count;
831
832 ael_yylex_destroy(io->scanner);
833 free(buffer);
834 free(io);
835
836 return pvalue;
837 }
838
839 static void setup_filestack(char *fnamebuf2, int fnamebuf_siz, glob_t *globbuf, int globpos, yyscan_t yyscanner, int create)
840 {
841 struct yyguts_t * yyg = (struct yyguts_t*)yyscanner;
842 int error, i;
843 FILE *in1;
844 char fnamebuf[2048];
845
846 if (globbuf && globbuf->gl_pathv && globbuf->gl_pathc > 0)
847 #if defined(STANDALONE) || defined(LOW_MEMORY) || defined(STANDALONE_AEL)
848 strncpy(fnamebuf, globbuf->gl_pathv[globpos], fnamebuf_siz);
849 #else
850 ast_copy_string(fnamebuf, globbuf->gl_pathv[globpos], fnamebuf_siz);
851 #endif
852 else {
853 ast_log(LOG_ERROR,"Include file name not present!\n");
854 return;
855 }
856 for (i=0; i<include_stack_index; i++) {
857 if ( !strcmp(fnamebuf,include_stack[i].fname )) {
858 ast_log(LOG_ERROR,"File=%s, line=%d, column=%d: Nice Try!!! But %s has already been included (perhaps by another file), and would cause an infinite loop of file inclusions!!! Include directive ignored\n",
859 my_file, my_lineno, my_col, fnamebuf);
860 break;
861 }
862 }
863 error = 1;
864 if (i == include_stack_index)
865 error = 0; /* we can use this file */
866 if ( !error ) { /* valid file name */
867 /* relative vs. absolute */
868 if (fnamebuf[0] != '/')
869 snprintf(fnamebuf2, fnamebuf_siz, "%s/%s", ast_config_AST_CONFIG_DIR, fnamebuf);
870 else
871 #if defined(STANDALONE) || defined(LOW_MEMORY) || defined(STANDALONE_AEL)
872 strncpy(fnamebuf2, fnamebuf, fnamebuf_siz);
873 #else
874 ast_copy_string(fnamebuf2, fnamebuf, fnamebuf_siz);
875 #endif
876 in1 = fopen( fnamebuf2, "r" );
877
878 if ( ! in1 ) {
879 ast_log(LOG_ERROR,"File=%s, line=%d, column=%d: Couldn't find the include file: %s; ignoring the Include directive!\n", my_file, my_lineno, my_col, fnamebuf2);
880 } else {
881 char *buffer;
882 struct stat stats;
883 if (stat(fnamebuf2, &stats)) {
884 ast_log(LOG_WARNING, "Failed to populate stats from file '%s'\n", fnamebuf2);
885 }
886 buffer = (char*)malloc(stats.st_size+1);
887 if (fread(buffer, 1, stats.st_size, in1) != stats.st_size) {
888 ast_log(LOG_ERROR, "fread() failed: %s\n", strerror(errno));
889 }
890 buffer[stats.st_size] = 0;
891 ast_debug(1, " --Read in included file %s, %d chars\n",fnamebuf2, (int)stats.st_size);
892 fclose(in1);
893 if (include_stack[include_stack_index].fname) {
894 free(include_stack[include_stack_index].fname);
895 include_stack[include_stack_index].fname = 0;
896 }
897 include_stack[include_stack_index].fname = strdup(S_OR(my_file, "<none>"));
898 include_stack[include_stack_index].lineno = my_lineno;
899 include_stack[include_stack_index].colno = my_col+yyleng;
900 if (my_file)
901 free(my_file);
902 my_file = strdup(fnamebuf2);
903 if (create)
904 include_stack[include_stack_index].globbuf = *globbuf;
905
906 include_stack[include_stack_index].globbuf_pos = 0;
907
908 include_stack[include_stack_index].bufstate = YY_CURRENT_BUFFER;
909 if (create)
910 include_stack_index++;
911 yy_switch_to_buffer(ael_yy_scan_string (buffer ,yyscanner),yyscanner);
912 free(buffer);
913 my_lineno = 1;
914 my_col = 1;
915 BEGIN(INITIAL);
916 }
917 }
918 }