ael.flex 25 KB

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