fix Solaris compatibility issues (bug #4339)
[asterisk/asterisk.git] / ast_expr2.fl
1 %{
2 #include <sys/types.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <locale.h>
7 #include <ctype.h>
8 #ifndef SOLARIS
9 #include <err.h>
10 #else
11 #define quad_t uint64_t
12 #endif
13 #include <errno.h>
14 #include <regex.h>
15 #include <limits.h>
16 #include <asterisk/ast_expr.h>
17 #include <asterisk/logger.h>
18
19 enum valtype {
20         AST_EXPR_integer, AST_EXPR_numeric_string, AST_EXPR_string
21 } ;
22
23 struct val {
24         enum valtype type;
25         union {
26                 char *s;
27                 quad_t i;
28         } u;
29 } ;
30
31 #include "ast_expr2.h" /* the o/p of the bison on ast_expr2.y */
32
33 #define SET_COLUMNS yylloc_param->first_column = (int)(yyg->yytext_r - YY_CURRENT_BUFFER_LVALUE->yy_ch_buf);yylloc_param->last_column = yylloc_param->last_column + yyleng - 1; yylloc_param->first_line = yylloc_param->last_line = 1
34 #define SET_STRING yylval_param->val = (struct val *)calloc(sizeof(struct val),1); yylval_param->val->type = AST_EXPR_string; yylval_param->val->u.s = strdup(yytext);
35
36 struct parse_io
37 {
38         char *string;
39         struct val *val;
40         yyscan_t scanner;
41 };
42  
43
44 %}
45
46 %option prefix="ast_yy"
47 %option batch
48 %option outfile="ast_expr2f.c"
49 %option reentrant
50 %option bison-bridge
51 %option bison-locations
52 %option noyywrap
53
54 %%
55
56 \|      { SET_COLUMNS; SET_STRING; return TOK_OR;}
57 \&      { SET_COLUMNS; SET_STRING; return TOK_AND;}
58 \=      { SET_COLUMNS; SET_STRING; return TOK_EQ;}
59 \>      { SET_COLUMNS; SET_STRING; return TOK_GT;}
60 \<      { SET_COLUMNS; SET_STRING; return TOK_LT;}
61 \>\=    { SET_COLUMNS; SET_STRING; return TOK_GE;}
62 \<\=    { SET_COLUMNS; SET_STRING; return TOK_LE;}
63 \!\=    { SET_COLUMNS; SET_STRING; return TOK_NE;}
64 \+      { SET_COLUMNS; SET_STRING; return TOK_PLUS;}
65 \-      { SET_COLUMNS; SET_STRING; return TOK_MINUS;}
66 \*      { SET_COLUMNS; SET_STRING; return TOK_MULT;}
67 \/      { SET_COLUMNS; SET_STRING; return TOK_DIV;}
68 \%      { SET_COLUMNS; SET_STRING; return TOK_MOD;}
69 \:      { SET_COLUMNS; SET_STRING; return TOK_COLON;}
70 \(      { SET_COLUMNS; SET_STRING; return TOK_LP;}
71 \)      { SET_COLUMNS; SET_STRING; return TOK_RP;}
72
73 [       \r]             {}
74 \"[^"]*\"   {SET_COLUMNS; SET_STRING; return TOKEN;}
75
76 [\n]    {/* what to do with eol */}
77 [0-9]+          {   SET_COLUMNS; 
78                                 yylval_param->val = (struct val *)calloc(sizeof(struct val),1);
79                                 yylval_param->val->type = AST_EXPR_integer; 
80                                 yylval_param->val->u.i = atoi(yytext); 
81                                 return TOKEN;}
82 [a-zA-Z0-9,.?';{}\\_^%$#@!]+    {SET_COLUMNS; SET_STRING; return TOKEN;}
83
84 %%
85
86 /* I'm putting the interface routine to the whole parse here in the flexer input file
87    mainly because of all the flexer initialization that has to be done. Shouldn't matter
88    where it is, as long as it's somewhere. I didn't want to define a prototype for the
89    ast_yy_scan_string in the .y file, because then, I'd have to define YY_BUFFER_STATE there...
90         UGH! that would be inappropriate. */
91
92 int ast_yyparse( void *); /* need to/should define this prototype for the call to yyparse */
93 char *ast_expr(char *arg); /* and this prototype for the following func */
94 int             ast_yyerror(const char *,YYLTYPE *, struct parse_io *); /* likewise */
95
96 char *ast_expr (char *arg)
97 {
98         struct parse_io *io;
99         char *pirouni;
100         
101         io = (struct parse_io *)calloc(sizeof(struct parse_io),1);
102         io->string = arg;  /* to pass to the error routine */
103         
104         ast_yylex_init(&io->scanner);
105         
106         ast_yy_scan_string(arg,io->scanner);
107         
108         ast_yyparse ((void *)io);
109
110         ast_yylex_destroy(io->scanner);
111         
112
113         if (io->val==NULL) {
114                 pirouni=strdup("0");
115                 return(pirouni);
116         } else {
117                 if (io->val->type == AST_EXPR_integer) {
118                         pirouni=malloc(256);
119                         sprintf (pirouni,"%lld", (long long)io->val->u.i);
120                 }
121                 else {
122                         pirouni=strdup(io->val->u.s);
123                 }
124                 free(io->val);
125         }
126         free(io);
127         return(pirouni);
128 }
129
130 int ast_yyerror (const char *s,  yyltype *loc, struct parse_io *parseio )
131 {       
132         struct yyguts_t * yyg = (struct yyguts_t*)(parseio->scanner);
133         char spacebuf[8000]; /* best safe than sorry */
134         char spacebuf2[8000]; /* best safe than sorry */
135         int i=0;
136         spacebuf[0] = 0;
137         
138 #ifdef WHEN_LOC_MEANS_SOMETHING
139         if( loc->first_column > 7990 ) /* if things get out of whack, why crash? */
140                 loc->first_column = 7990;
141         if( loc->last_column > 7990 )
142                 loc->last_column = 7990;
143         for(i=0;i<loc->first_column;i++) spacebuf[i] = ' ';
144         for(   ;i<loc->last_column;i++) spacebuf[i] = '^';
145         spacebuf[i] = 0;
146 #endif
147         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,
148                                                                                                         which is the same thing as... get this:
149                                                                                                         yyg->yy_buffer_stack[yyg->yy_buffer_stack_top]->yy_bs_column
150                                                                                                         I was tempted to just use yy_buf_pos in the STATE, but..., well:
151                                                                                                                 a. the yy_buf_pos is the current position in the buffer, which
152                                                                                                                         may not relate to the entire string/buffer because of the
153                                                                                                                         buffering.
154                                                                                                                 b. but, analysis of the situation is that when you use the
155                                                                                                                         yy_scan_string func, it creates a single buffer the size of
156                                                                                                                         string, so the two would be the same... 
157                                                                                                         so, in the end, the yycolumn macro is available, shorter, therefore easier. */
158         spacebuf2[i++]='^';
159         spacebuf2[i]= 0;
160
161 #ifdef STANDALONE
162         /* easier to read in the standalone version */
163         printf("ast_yyerror(): syntax error: %s; Input:\n%s\n%s\n",  
164                         s, parseio->string,spacebuf2);
165 #else
166         ast_log(LOG_WARNING,"ast_yyerror(): syntax error: %s; Input:\n%s\n%s\n",  
167                         s, parseio->string,spacebuf2);
168         ast_log(LOG_WARNING,"If you have questions, please refer to doc/README.variables2 in the asterisk source.\n");
169 #endif
170         return(0);
171 }