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
27 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
29 #include <sys/types.h>
35 #if !defined(SOLARIS) && !defined(__CYGWIN__)
38 #define quad_t int64_t
44 #include "asterisk/ast_expr.h"
45 #include "asterisk/logger.h"
46 #include "asterisk/strings.h"
49 AST_EXPR_integer, AST_EXPR_numeric_string, AST_EXPR_string
60 #include "ast_expr2.h" /* the o/p of the bison on ast_expr2.y */
62 #define SET_COLUMNS do { \
63 yylloc_param->first_column = (int)(yyg->yytext_r - YY_CURRENT_BUFFER_LVALUE->yy_ch_buf); \
64 yylloc_param->last_column += yyleng - 1; \
65 yylloc_param->first_line = yylloc_param->last_line = 1; \
68 #define SET_STRING do { \
69 yylval_param->val = calloc(1, sizeof(struct val)); \
70 yylval_param->val->type = AST_EXPR_string; \
71 yylval_param->val->u.s = strdup(yytext); \
74 #define SET_NUMERIC_STRING do { \
75 yylval_param->val = calloc(1, sizeof(struct val)); \
76 yylval_param->val->type = AST_EXPR_numeric_string; \
77 yylval_param->val->u.s = strdup(yytext); \
87 void ast_yyset_column(int column_no, yyscan_t yyscanner);
88 int ast_yyget_column(yyscan_t yyscanner);
89 static int curlycount = 0;
90 static char *expr2_token_subst(char *mess);
93 %option prefix="ast_yy"
95 %option outfile="ast_expr2f.c"
98 %option bison-locations
104 \| { SET_COLUMNS; SET_STRING; return TOK_OR;}
105 \& { SET_COLUMNS; SET_STRING; return TOK_AND;}
106 \= { SET_COLUMNS; SET_STRING; return TOK_EQ;}
107 \|\| { SET_COLUMNS; SET_STRING; return TOK_OR;}
108 \&\& { SET_COLUMNS; SET_STRING; return TOK_AND;}
109 \=\= { SET_COLUMNS; SET_STRING; return TOK_EQ;}
110 \=~ { SET_COLUMNS; SET_STRING; return TOK_EQTILDE;}
111 \> { SET_COLUMNS; SET_STRING; return TOK_GT;}
112 \< { SET_COLUMNS; SET_STRING; return TOK_LT;}
113 \>\= { SET_COLUMNS; SET_STRING; return TOK_GE;}
114 \<\= { SET_COLUMNS; SET_STRING; return TOK_LE;}
115 \!\= { SET_COLUMNS; SET_STRING; return TOK_NE;}
116 \+ { SET_COLUMNS; SET_STRING; return TOK_PLUS;}
117 \- { SET_COLUMNS; SET_STRING; return TOK_MINUS;}
118 \* { SET_COLUMNS; SET_STRING; return TOK_MULT;}
119 \/ { SET_COLUMNS; SET_STRING; return TOK_DIV;}
120 \% { SET_COLUMNS; SET_STRING; return TOK_MOD;}
121 \? { SET_COLUMNS; SET_STRING; return TOK_COND;}
122 \! { SET_COLUMNS; SET_STRING; return TOK_COMPL;}
123 \: { SET_COLUMNS; SET_STRING; return TOK_COLON;}
124 \:\: { SET_COLUMNS; SET_STRING; return TOK_COLONCOLON;}
125 \( { SET_COLUMNS; SET_STRING; return TOK_LP;}
126 \) { SET_COLUMNS; SET_STRING; return TOK_RP;}
128 /* gather the contents of ${} expressions, with trailing stuff,
129 * into a single TOKEN.
130 * They are much more complex now than they used to be
138 \"[^"]*\" {SET_COLUMNS; SET_STRING; return TOKEN;}
140 [\n] {/* what to do with eol */}
143 /* the original behavior of the expression parser was
144 * to bring in numbers as a numeric string
150 [a-zA-Z0-9,.';\\_^$#@]+ {
159 if (curlycount < 0) {
173 <trail>[^-\t\r \n$():?%/+=*<>!|&]* {
180 <trail>[-\t\r \n$():?%/+=*<>!|&] {
181 char c = yytext[yyleng-1];
200 /*actually, if an expr is only a variable ref, this could happen a LOT */
205 /* I'm putting the interface routine to the whole parse here in the flexer input file
206 mainly because of all the flexer initialization that has to be done. Shouldn't matter
207 where it is, as long as it's somewhere. I didn't want to define a prototype for the
208 ast_yy_scan_string in the .y file, because then, I'd have to define YY_BUFFER_STATE there...
209 UGH! that would be inappropriate. */
211 int ast_yyparse(void *); /* need to/should define this prototype for the call to yyparse */
212 int ast_yyerror(const char *, YYLTYPE *, struct parse_io *); /* likewise */
214 int ast_expr(char *expr, char *buf, int length)
217 int return_value = 0;
219 memset(&io, 0, sizeof(io));
220 io.string = expr; /* to pass to the error routine */
222 ast_yylex_init(&io.scanner);
224 ast_yy_scan_string(expr, io.scanner);
226 ast_yyparse ((void *) &io);
228 ast_yylex_destroy(io.scanner);
236 if (io.val->type == AST_EXPR_integer) {
239 res_length = snprintf(buf, length, "%ld", (long int) io.val->u.i);
240 return_value = (res_length <= length) ? res_length : length;
243 strncpy(buf, io.val->u.s, length - 1);
244 #else /* !STANDALONE */
245 ast_copy_string(buf, io.val->u.s, length);
246 #endif /* STANDALONE */
247 return_value = strlen(buf);
256 char extra_error_message[4095];
257 int extra_error_message_supplied = 0;
258 void ast_expr_register_extra_error_info(char *message);
259 void ast_expr_clear_extra_error_info(void);
261 void ast_expr_register_extra_error_info(char *message)
263 extra_error_message_supplied=1;
264 strcpy(extra_error_message, message);
267 void ast_expr_clear_extra_error_info(void)
269 extra_error_message_supplied=0;
270 extra_error_message[0] = 0;
273 static char *expr2_token_equivs1[] =
298 static char *expr2_token_equivs2[] =
324 static char *expr2_token_subst(char *mess)
326 /* calc a length, malloc, fill, and return; yyerror had better free it! */
330 int expr2_token_equivs_entries = sizeof(expr2_token_equivs1)/sizeof(char*);
332 for (p=mess; *p; p++) {
333 for (i=0; i<expr2_token_equivs_entries; i++) {
334 if ( strncmp(p,expr2_token_equivs1[i],strlen(expr2_token_equivs1[i])) == 0 )
336 len+=strlen(expr2_token_equivs2[i])+2;
337 p += strlen(expr2_token_equivs1[i])-1;
343 res = (char*)malloc(len+1);
348 for (i=0; i<expr2_token_equivs_entries; i++) {
349 if ( strncmp(p,expr2_token_equivs1[i],strlen(expr2_token_equivs1[i])) == 0 ) {
351 for (t=expr2_token_equivs2[i]; *t;) {
355 p += strlen(expr2_token_equivs1[i]);
367 int ast_yyerror (const char *s, yyltype *loc, struct parse_io *parseio )
369 struct yyguts_t * yyg = (struct yyguts_t*)(parseio->scanner);
370 char spacebuf[8000]; /* best safe than sorry */
371 char spacebuf2[8000]; /* best safe than sorry */
373 char *s2 = expr2_token_subst((char *)s);
376 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,
377 which is the same thing as... get this:
378 yyg->yy_buffer_stack[yyg->yy_buffer_stack_top]->yy_bs_column
379 I was tempted to just use yy_buf_pos in the STATE, but..., well:
380 a. the yy_buf_pos is the current position in the buffer, which
381 may not relate to the entire string/buffer because of the
383 b. but, analysis of the situation is that when you use the
384 yy_scan_string func, it creates a single buffer the size of
385 string, so the two would be the same...
386 so, in the end, the yycolumn macro is available, shorter, therefore easier. */
391 /* easier to read in the standalone version */
392 printf("ast_yyerror(): %s syntax error: %s; Input:\n%s\n%s\n",
393 (extra_error_message_supplied?extra_error_message:""), s2, parseio->string,spacebuf2);
395 ast_log(LOG_WARNING,"ast_yyerror(): %s syntax error: %s; Input:\n%s\n%s\n",
396 (extra_error_message_supplied?extra_error_message:""), s2, parseio->string,spacebuf2);
399 ast_log(LOG_WARNING,"If you have questions, please refer to doc/channelvariables.txt in the asterisk source.\n");