3 * Asterisk -- An open source telephony toolkit.
5 * Copyright (C) 1999 - 2006, Digium, Inc.
7 * Mark Spencer <markster@digium.com>
9 * See http://www.asterisk.org for more information about
10 * the Asterisk project. Please do not directly contact
11 * any of the maintainers of this project for assistance;
12 * the project provides a web site, mailing lists and IRC
13 * channels for your use.
15 * This program is free software, distributed under the terms of
16 * the GNU General Public License Version 2. See the LICENSE file
17 * at the top of the source tree.
22 * \brief Dialplan Expression Lexical Scanner
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
31 #include <sys/types.h>
37 #if !defined(SOLARIS) && !defined(__CYGWIN__)
38 /* #include <err.h> */
40 #define quad_t int64_t
46 #include "asterisk/ast_expr.h"
47 #include "asterisk/logger.h"
48 #include "asterisk/strings.h"
51 AST_EXPR_integer, AST_EXPR_numeric_string, AST_EXPR_string
62 #include "ast_expr2.h" /* the o/p of the bison on ast_expr2.y */
64 #define SET_COLUMNS do { \
65 yylloc_param->first_column = (int)(yyg->yytext_r - YY_CURRENT_BUFFER_LVALUE->yy_ch_buf); \
66 yylloc_param->last_column += yyleng - 1; \
67 yylloc_param->first_line = yylloc_param->last_line = 1; \
70 #define SET_STRING do { \
71 yylval_param->val = calloc(1, sizeof(struct val)); \
72 yylval_param->val->type = AST_EXPR_string; \
73 yylval_param->val->u.s = strdup(yytext); \
76 #define SET_NUMERIC_STRING do { \
77 yylval_param->val = calloc(1, sizeof(struct val)); \
78 yylval_param->val->type = AST_EXPR_numeric_string; \
79 yylval_param->val->u.s = strdup(yytext); \
89 void ast_yyset_column(int column_no, yyscan_t yyscanner);
90 int ast_yyget_column(yyscan_t yyscanner);
91 static int curlycount = 0;
92 static char *expr2_token_subst(char *mess);
95 %option prefix="ast_yy"
97 %option outfile="ast_expr2f.c"
100 %option bison-locations
106 \| { SET_COLUMNS; SET_STRING; return TOK_OR;}
107 \& { SET_COLUMNS; SET_STRING; return TOK_AND;}
108 \= { SET_COLUMNS; SET_STRING; return TOK_EQ;}
109 \|\| { SET_COLUMNS; SET_STRING; return TOK_OR;}
110 \&\& { SET_COLUMNS; SET_STRING; return TOK_AND;}
111 \=\= { SET_COLUMNS; SET_STRING; return TOK_EQ;}
112 \=~ { SET_COLUMNS; SET_STRING; return TOK_EQTILDE;}
113 \> { SET_COLUMNS; SET_STRING; return TOK_GT;}
114 \< { SET_COLUMNS; SET_STRING; return TOK_LT;}
115 \>\= { SET_COLUMNS; SET_STRING; return TOK_GE;}
116 \<\= { SET_COLUMNS; SET_STRING; return TOK_LE;}
117 \!\= { SET_COLUMNS; SET_STRING; return TOK_NE;}
118 \+ { SET_COLUMNS; SET_STRING; return TOK_PLUS;}
119 \- { SET_COLUMNS; SET_STRING; return TOK_MINUS;}
120 \* { SET_COLUMNS; SET_STRING; return TOK_MULT;}
121 \/ { SET_COLUMNS; SET_STRING; return TOK_DIV;}
122 \% { SET_COLUMNS; SET_STRING; return TOK_MOD;}
123 \? { SET_COLUMNS; SET_STRING; return TOK_COND;}
124 \! { SET_COLUMNS; SET_STRING; return TOK_COMPL;}
125 \: { SET_COLUMNS; SET_STRING; return TOK_COLON;}
126 \:\: { SET_COLUMNS; SET_STRING; return TOK_COLONCOLON;}
127 \( { SET_COLUMNS; SET_STRING; return TOK_LP;}
128 \) { SET_COLUMNS; SET_STRING; return TOK_RP;}
130 /* gather the contents of ${} expressions, with trailing stuff,
131 * into a single TOKEN.
132 * They are much more complex now than they used to be
140 \"[^"]*\" {SET_COLUMNS; SET_STRING; return TOKEN;}
142 [\n] {/* what to do with eol */}
145 /* the original behavior of the expression parser was
146 * to bring in numbers as a numeric string
152 [a-zA-Z0-9,.';\\_^$#@]+ {
161 if (curlycount < 0) {
175 <trail>[^-\t\r \n$():?%/+=*<>!|&]* {
182 <trail>[-\t\r \n$():?%/+=*<>!|&] {
183 char c = yytext[yyleng-1];
202 /*actually, if an expr is only a variable ref, this could happen a LOT */
207 /* I'm putting the interface routine to the whole parse here in the flexer input file
208 mainly because of all the flexer initialization that has to be done. Shouldn't matter
209 where it is, as long as it's somewhere. I didn't want to define a prototype for the
210 ast_yy_scan_string in the .y file, because then, I'd have to define YY_BUFFER_STATE there...
211 UGH! that would be inappropriate. */
213 int ast_yyparse(void *); /* need to/should define this prototype for the call to yyparse */
214 int ast_yyerror(const char *, YYLTYPE *, struct parse_io *); /* likewise */
216 int ast_expr(char *expr, char *buf, int length)
219 int return_value = 0;
221 memset(&io, 0, sizeof(io));
222 io.string = expr; /* to pass to the error routine */
224 ast_yylex_init(&io.scanner);
226 ast_yy_scan_string(expr, io.scanner);
228 ast_yyparse ((void *) &io);
230 ast_yylex_destroy(io.scanner);
238 if (io.val->type == AST_EXPR_integer) {
241 res_length = snprintf(buf, length, "%ld", (long int) io.val->u.i);
242 return_value = (res_length <= length) ? res_length : length;
244 #if defined(STANDALONE) || defined(LOW_MEMORY)
245 strncpy(buf, io.val->u.s, length - 1);
246 #else /* !STANDALONE && !LOW_MEMORY */
247 ast_copy_string(buf, io.val->u.s, length);
248 #endif /* STANDALONE || LOW_MEMORY */
249 return_value = strlen(buf);
258 char extra_error_message[4095];
259 int extra_error_message_supplied = 0;
260 void ast_expr_register_extra_error_info(char *message);
261 void ast_expr_clear_extra_error_info(void);
263 void ast_expr_register_extra_error_info(char *message)
265 extra_error_message_supplied=1;
266 strcpy(extra_error_message, message);
269 void ast_expr_clear_extra_error_info(void)
271 extra_error_message_supplied=0;
272 extra_error_message[0] = 0;
275 static char *expr2_token_equivs1[] =
300 static char *expr2_token_equivs2[] =
326 static char *expr2_token_subst(char *mess)
328 /* calc a length, malloc, fill, and return; yyerror had better free it! */
332 int expr2_token_equivs_entries = sizeof(expr2_token_equivs1)/sizeof(char*);
334 for (p=mess; *p; p++) {
335 for (i=0; i<expr2_token_equivs_entries; i++) {
336 if ( strncmp(p,expr2_token_equivs1[i],strlen(expr2_token_equivs1[i])) == 0 )
338 len+=strlen(expr2_token_equivs2[i])+2;
339 p += strlen(expr2_token_equivs1[i])-1;
345 res = (char*)malloc(len+1);
350 for (i=0; i<expr2_token_equivs_entries; i++) {
351 if ( strncmp(p,expr2_token_equivs1[i],strlen(expr2_token_equivs1[i])) == 0 ) {
353 for (t=expr2_token_equivs2[i]; *t;) {
357 p += strlen(expr2_token_equivs1[i]);
369 int ast_yyerror (const char *s, yyltype *loc, struct parse_io *parseio )
371 struct yyguts_t * yyg = (struct yyguts_t*)(parseio->scanner);
372 char spacebuf[8000]; /* best safe than sorry */
373 char spacebuf2[8000]; /* best safe than sorry */
375 char *s2 = expr2_token_subst((char *)s);
378 for(i=0;i< (int)(yytext - YY_CURRENT_BUFFER_LVALUE->yy_ch_buf);i++) spacebuf2[i] = ' '; /* uh... assuming yyg is defined, then I can use the yycolumn macro,
379 which is the same thing as... get this:
380 yyg->yy_buffer_stack[yyg->yy_buffer_stack_top]->yy_bs_column
381 I was tempted to just use yy_buf_pos in the STATE, but..., well:
382 a. the yy_buf_pos is the current position in the buffer, which
383 may not relate to the entire string/buffer because of the
385 b. but, analysis of the situation is that when you use the
386 yy_scan_string func, it creates a single buffer the size of
387 string, so the two would be the same...
388 so, in the end, the yycolumn macro is available, shorter, therefore easier. */
393 /* easier to read in the standalone version */
394 printf("ast_yyerror(): %s syntax error: %s; Input:\n%s\n%s\n",
395 (extra_error_message_supplied?extra_error_message:""), s2, parseio->string,spacebuf2);
397 ast_log(LOG_WARNING,"ast_yyerror(): %s syntax error: %s; Input:\n%s\n%s\n",
398 (extra_error_message_supplied?extra_error_message:""), s2, parseio->string,spacebuf2);
401 ast_log(LOG_WARNING,"If you have questions, please refer to doc/channelvariables.txt in the asterisk source.\n");