2 /* Written by Pace Willisson (pace@blitz.com)
3 * and placed in the public domain.
5 * Largely rewritten by J.T. Conklin (jtc@wimsey.com)
7 * $FreeBSD: src/bin/expr/expr.y,v 1.16 2000/07/22 10:59:36 se Exp $
10 #include <sys/types.h>
19 #define quad_t uint64_t
24 #include <asterisk/ast_expr.h>
25 #include <asterisk/logger.h>
28 #define QUAD_MIN LONG_LONG_MIN
31 #define QUAD_MAX LONG_LONG_MAX
34 # if ! defined(QUAD_MIN)
35 # define QUAD_MIN (-0x7fffffffffffffffL-1)
37 # if ! defined(QUAD_MAX)
38 # define QUAD_MAX (0x7fffffffffffffffL)
41 #define YYPARSE_PARAM kota
42 #define YYLEX_PARAM kota
44 /* #define ast_log fprintf
45 #define LOG_WARNING stderr */
52 integer, numeric_string, string
63 struct parser_control {
72 static int chk_div __P((quad_t, quad_t));
73 static int chk_minus __P((quad_t, quad_t, quad_t));
74 static int chk_plus __P((quad_t, quad_t, quad_t));
75 static int chk_times __P((quad_t, quad_t, quad_t));
76 static void free_value __P((struct val *));
77 static int is_zero_or_null __P((struct val *));
78 static int isstring __P((struct val *));
79 static struct val *make_integer __P((quad_t));
80 static struct val *make_str __P((const char *));
81 static struct val *op_and __P((struct val *, struct val *));
82 static struct val *op_colon __P((struct val *, struct val *));
83 static struct val *op_div __P((struct val *, struct val *));
84 static struct val *op_eq __P((struct val *, struct val *));
85 static struct val *op_ge __P((struct val *, struct val *));
86 static struct val *op_gt __P((struct val *, struct val *));
87 static struct val *op_le __P((struct val *, struct val *));
88 static struct val *op_lt __P((struct val *, struct val *));
89 static struct val *op_minus __P((struct val *, struct val *));
90 static struct val *op_ne __P((struct val *, struct val *));
91 static struct val *op_or __P((struct val *, struct val *));
92 static struct val *op_plus __P((struct val *, struct val *));
93 static struct val *op_rem __P((struct val *, struct val *));
94 static struct val *op_times __P((struct val *, struct val *));
95 static quad_t to_integer __P((struct val *));
96 static void to_string __P((struct val *));
98 /* uh, if I want to predeclare yylex with a YYLTYPE, I have to predeclare the yyltype... sigh */
99 typedef struct yyltype
108 # define YYLTYPE yyltype
109 # define YYLTYPE_IS_TRIVIAL 1
111 static int ast_yyerror __P((const char *,YYLTYPE *, struct parser_control *));
113 #define ast_yyerror(x) ast_yyerror(x,&yyloc,kota)
119 /* %debug for when you are having big problems */
121 /* %name-prefix="ast_yy" */
129 static int ast_yylex __P((YYSTYPE *, YYLTYPE *, struct parser_control *));
135 %left <val> '=' '>' '<' GE LE NE
137 %left <val> '*' '/' '%'
141 %type <val> start expr
145 start: expr { ((struct parser_control *)kota)->result = $$; }
149 | '(' expr ')' { $$ = $2; @$.first_column = @1.first_column; @$.last_column = @3.last_column; @$.first_line=0; @$.last_line=0;}
150 | expr '|' expr { $$ = op_or ($1, $3); @$.first_column = @1.first_column; @$.last_column = @3.last_column; @$.first_line=0; @$.last_line=0;}
151 | expr '&' expr { $$ = op_and ($1, $3); @$.first_column = @1.first_column; @$.last_column = @3.last_column; @$.first_line=0; @$.last_line=0;}
152 | expr '=' expr { $$ = op_eq ($1, $3); @$.first_column = @1.first_column; @$.last_column = @3.last_column; @$.first_line=0; @$.last_line=0;}
153 | expr '>' expr { $$ = op_gt ($1, $3); @$.first_column = @1.first_column; @$.last_column = @3.last_column; @$.first_line=0; @$.last_line=0;}
154 | expr '<' expr { $$ = op_lt ($1, $3); @$.first_column = @1.first_column; @$.last_column = @3.last_column; @$.first_line=0; @$.last_line=0;}
155 | expr GE expr { $$ = op_ge ($1, $3); @$.first_column = @1.first_column; @$.last_column = @3.last_column; @$.first_line=0; @$.last_line=0;}
156 | expr LE expr { $$ = op_le ($1, $3); @$.first_column = @1.first_column; @$.last_column = @3.last_column; @$.first_line=0; @$.last_line=0;}
157 | expr NE expr { $$ = op_ne ($1, $3); @$.first_column = @1.first_column; @$.last_column = @3.last_column; @$.first_line=0; @$.last_line=0;}
158 | expr '+' expr { $$ = op_plus ($1, $3); @$.first_column = @1.first_column; @$.last_column = @3.last_column; @$.first_line=0; @$.last_line=0;}
159 | expr '-' expr { $$ = op_minus ($1, $3); @$.first_column = @1.first_column; @$.last_column = @3.last_column; @$.first_line=0; @$.last_line=0;}
160 | expr '*' expr { $$ = op_times ($1, $3); @$.first_column = @1.first_column; @$.last_column = @3.last_column; @$.first_line=0; @$.last_line=0;}
161 | expr '/' expr { $$ = op_div ($1, $3); @$.first_column = @1.first_column; @$.last_column = @3.last_column; @$.first_line=0; @$.last_line=0;}
162 | expr '%' expr { $$ = op_rem ($1, $3); @$.first_column = @1.first_column; @$.last_column = @3.last_column; @$.first_line=0; @$.last_line=0;}
163 | expr ':' expr { $$ = op_colon ($1, $3); @$.first_column = @1.first_column; @$.last_column = @3.last_column; @$.first_line=0; @$.last_line=0;}
175 vp = (struct val *) malloc (sizeof (*vp));
177 ast_log(LOG_WARNING, "malloc() failed\n");
194 vp = (struct val *) malloc (sizeof (*vp));
195 if (vp == NULL || ((vp->u.s = strdup (s)) == NULL)) {
196 ast_log(LOG_WARNING,"malloc() failed\n");
200 for(i = 1, isint = isdigit(s[0]) || s[0] == '-';
201 isint && i < strlen(s);
209 vp->type = numeric_string;
224 if (vp->type == string || vp->type == numeric_string)
237 ast_log(LOG_WARNING,"vp==NULL in to_integer()\n");
241 if (vp->type == integer)
244 if (vp->type == string)
247 /* vp->type == numeric_string, make it numeric */
249 i = strtoq(vp->u.s, (char**)NULL, 10);
252 ast_log(LOG_WARNING,"overflow\n");
267 if (vp->type == string || vp->type == numeric_string)
270 tmp = malloc ((size_t)25);
272 ast_log(LOG_WARNING,"malloc() failed\n");
276 sprintf (tmp, "%lld", (long long)vp->u.i);
286 /* only TRUE if this string is not a valid integer */
287 return (vp->type == string);
291 ast_yylex (YYSTYPE *lvalp, YYLTYPE *yylloc, struct parser_control *karoto)
298 if (karoto->firsttoken==1) {
300 karoto->firsttoken = 0;
305 while(*t1 && *t1 == ' ' ) /* we can remove worries about leading/multiple spaces being present */
308 yylloc->first_column = t1 - karoto->argv;
310 while( *t1 && *t1 != ' ' && *t1 != '"') /* find the next space or quote */
316 karoto->ptrptr = t1+1;
317 yylloc->last_column = t1 - karoto->argv;
319 else if (*t1 == '"' )
321 /* opening quote. find the closing quote */
323 while( *t2 && *t2 != '"')
327 if( *(t2+1) == ' ' || *(t2+1) == 0 )
332 karoto->ptrptr = t2+2;
336 karoto->ptrptr = t2+1;
341 /* hmmm. what if another token is here? */
342 /* maybe we can insert a space? */
346 karoto->ptrptr = t2+1;
352 /* NOT GOOD -- no closing quote! */
356 yylloc->last_column = t2 - karoto->argv;
360 if( t1 != karoto->ptrptr )
362 /* this is the last token */
368 /* we are done. That was quick */
370 yylloc->last_column = t1 - karoto->argv;
381 if (strlen (p) == 1) {
382 if (strchr ("|&=<>+-*/%:()", *p))
384 } else if (strlen (p) == 2 && p[1] == '=') {
386 case '>': return (GE);
387 case '<': return (LE);
388 case '!': return (NE);
392 lvalp->val = make_str (p);
395 *savepp = savep; /* restore the null terminated string */
406 if (vp->type == integer) {
407 return (vp->u.i == 0);
409 return (*vp->u.s == 0 || (to_integer (vp) && vp->u.i == 0));
414 char *ast_expr (char *arg)
416 struct parser_control karoto;
422 karoto.result = NULL;
425 karoto.arg_orig = arg;
426 /* ast_yydebug = 1; */
428 ast_yyparse ((void *)&karoto);
432 if (karoto.result==NULL) {
436 if (karoto.result->type == integer) {
438 sprintf (pirouni,"%lld", (long long)karoto.result->u.i);
441 pirouni=strdup(karoto.result->u.s);
450 int main(int argc,char **argv) {
455 printf("=====%s======\n",s);
461 #define ast_yyerror(x) ast_yyerror(x, YYLTYPE *yylloc, struct parser_control *karoto)
464 ast_yyerror (const char *s)
466 char spacebuf[8000]; /* best safe than sorry */
467 char spacebuf2[8000]; /* best safe than sorry */
471 if( yylloc->first_column > 7990 ) /* if things get out of whack, why crash? */
472 yylloc->first_column = 7990;
473 if( yylloc->last_column > 7990 )
474 yylloc->last_column = 7990;
475 for(i=0;i<yylloc->first_column;i++) spacebuf[i] = ' ';
476 for( ;i<yylloc->last_column;i++) spacebuf[i] = '^';
479 for(i=0;i<karoto->ptrptr-karoto->argv;i++) spacebuf2[i] = ' ';
483 ast_log(LOG_WARNING,"ast_yyerror(): syntax error: %s; Input:\n%s\n%s\n%s\n",s,
484 karoto->arg_orig,spacebuf,spacebuf2);
493 if (is_zero_or_null (a)) {
506 if (is_zero_or_null (a) || is_zero_or_null (b)) {
509 return (make_integer ((quad_t)0));
522 if (isstring (a) || isstring (b)) {
525 r = make_integer ((quad_t)(strcoll (a->u.s, b->u.s) == 0));
529 r = make_integer ((quad_t)(a->u.i == b->u.i));
543 if (isstring (a) || isstring (b)) {
546 r = make_integer ((quad_t)(strcoll (a->u.s, b->u.s) > 0));
550 r = make_integer ((quad_t)(a->u.i > b->u.i));
564 if (isstring (a) || isstring (b)) {
567 r = make_integer ((quad_t)(strcoll (a->u.s, b->u.s) < 0));
571 r = make_integer ((quad_t)(a->u.i < b->u.i));
585 if (isstring (a) || isstring (b)) {
588 r = make_integer ((quad_t)(strcoll (a->u.s, b->u.s) >= 0));
592 r = make_integer ((quad_t)(a->u.i >= b->u.i));
606 if (isstring (a) || isstring (b)) {
609 r = make_integer ((quad_t)(strcoll (a->u.s, b->u.s) <= 0));
613 r = make_integer ((quad_t)(a->u.i <= b->u.i));
627 if (isstring (a) || isstring (b)) {
630 r = make_integer ((quad_t)(strcoll (a->u.s, b->u.s) != 0));
634 r = make_integer ((quad_t)(a->u.i != b->u.i));
646 /* sum of two positive numbers must be positive */
647 if (a > 0 && b > 0 && r <= 0)
649 /* sum of two negative numbers must be negative */
650 if (a < 0 && b < 0 && r >= 0)
652 /* all other cases are OK */
662 if (!to_integer (a)) {
663 ast_log(LOG_WARNING,"non-numeric argument\n");
664 if (!to_integer (b)) {
667 return make_integer(0);
672 } else if (!to_integer(b)) {
677 r = make_integer (/*(quad_t)*/(a->u.i + b->u.i));
678 if (chk_plus (a->u.i, b->u.i, r->u.i)) {
679 ast_log(LOG_WARNING,"overflow\n");
690 /* special case subtraction of QUAD_MIN */
697 /* this is allowed for b != QUAD_MIN */
698 return chk_plus (a, -b, r);
707 if (!to_integer (a)) {
708 ast_log(LOG_WARNING, "non-numeric argument\n");
709 if (!to_integer (b)) {
712 return make_integer(0);
714 r = make_integer(0 - b->u.i);
719 } else if (!to_integer(b)) {
720 ast_log(LOG_WARNING, "non-numeric argument\n");
725 r = make_integer (/*(quad_t)*/(a->u.i - b->u.i));
726 if (chk_minus (a->u.i, b->u.i, r->u.i)) {
727 ast_log(LOG_WARNING, "overflow\n");
738 /* special case: first operand is 0, no overflow possible */
741 /* cerify that result of division matches second operand */
753 if (!to_integer (a) || !to_integer (b)) {
756 ast_log(LOG_WARNING, "non-numeric argument\n");
757 return(make_integer(0));
760 r = make_integer (/*(quad_t)*/(a->u.i * b->u.i));
761 if (chk_times (a->u.i, b->u.i, r->u.i)) {
762 ast_log(LOG_WARNING, "overflow\n");
773 /* div by zero has been taken care of before */
774 /* only QUAD_MIN / -1 causes overflow */
775 if (a == QUAD_MIN && b == -1)
777 /* everything else is OK */
787 if (!to_integer (a)) {
790 ast_log(LOG_WARNING, "non-numeric argument\n");
791 return make_integer(0);
792 } else if (!to_integer (b)) {
795 ast_log(LOG_WARNING, "non-numeric argument\n");
796 return make_integer(INT_MAX);
800 ast_log(LOG_WARNING, "division by zero\n");
803 return make_integer(INT_MAX);
806 r = make_integer (/*(quad_t)*/(a->u.i / b->u.i));
807 if (chk_div (a->u.i, b->u.i)) {
808 ast_log(LOG_WARNING, "overflow\n");
821 if (!to_integer (a) || !to_integer (b)) {
822 ast_log(LOG_WARNING, "non-numeric argument\n");
825 return make_integer(0);
829 ast_log(LOG_WARNING, "div by zero\n");
834 r = make_integer (/*(quad_t)*/(a->u.i % b->u.i));
835 /* chk_rem necessary ??? */
851 /* coerce to both arguments to strings */
855 /* compile regular expression */
856 if ((eval = regcomp (&rp, b->u.s, REG_EXTENDED)) != 0) {
857 regerror (eval, &rp, errbuf, sizeof(errbuf));
858 ast_log(LOG_WARNING,"regcomp() error : %s",errbuf);
864 /* compare string against pattern */
865 /* remember that patterns are anchored to the beginning of the line */
866 if (regexec(&rp, a->u.s, (size_t)2, rm, 0) == 0 && rm[0].rm_so == 0) {
867 if (rm[1].rm_so >= 0) {
868 *(a->u.s + rm[1].rm_eo) = '\0';
869 v = make_str (a->u.s + rm[1].rm_so);
872 v = make_integer ((quad_t)(rm[0].rm_eo - rm[0].rm_so));
875 if (rp.re_nsub == 0) {
876 v = make_integer ((quad_t)0);
882 /* free arguments and pattern buffer */