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>
20 #include <asterisk/ast_expr.h>
21 #include <asterisk/logger.h>
23 # if ! defined(QUAD_MIN)
24 # define QUAD_MIN (-0x7fffffffffffffffL-1)
26 # if ! defined(QUAD_MAX)
27 # define QUAD_MAX (0x7fffffffffffffffL)
30 #define YYPARSE_PARAM kota
31 #define YYLEX_PARAM kota
33 /* #define ast_log fprintf
34 #define LOG_WARNING stderr */
37 integer, numeric_string, string
48 struct parser_control {
56 static int chk_div __P((quad_t, quad_t));
57 static int chk_minus __P((quad_t, quad_t, quad_t));
58 static int chk_plus __P((quad_t, quad_t, quad_t));
59 static int chk_times __P((quad_t, quad_t, quad_t));
60 static void free_value __P((struct val *));
61 static int is_zero_or_null __P((struct val *));
62 static int isstring __P((struct val *));
63 static struct val *make_integer __P((quad_t));
64 static struct val *make_str __P((const char *));
65 static struct val *op_and __P((struct val *, struct val *));
66 static struct val *op_colon __P((struct val *, struct val *));
67 static struct val *op_div __P((struct val *, struct val *));
68 static struct val *op_eq __P((struct val *, struct val *));
69 static struct val *op_ge __P((struct val *, struct val *));
70 static struct val *op_gt __P((struct val *, struct val *));
71 static struct val *op_le __P((struct val *, struct val *));
72 static struct val *op_lt __P((struct val *, struct val *));
73 static struct val *op_minus __P((struct val *, struct val *));
74 static struct val *op_ne __P((struct val *, struct val *));
75 static struct val *op_or __P((struct val *, struct val *));
76 static struct val *op_plus __P((struct val *, struct val *));
77 static struct val *op_rem __P((struct val *, struct val *));
78 static struct val *op_times __P((struct val *, struct val *));
79 static quad_t to_integer __P((struct val *));
80 static void to_string __P((struct val *));
81 static int ast_yyerror __P((const char *));
85 /* %name-prefix="ast_yy" */
93 static int ast_yylex __P((YYSTYPE *, struct parser_control *));
99 %left <val> '=' '>' '<' GE LE NE
101 %left <val> '*' '/' '%'
105 %type <val> start expr
109 start: expr { ((struct parser_control *)kota)->result = $$; }
113 | '(' expr ')' { $$ = $2; }
114 | expr '|' expr { $$ = op_or ($1, $3); }
115 | expr '&' expr { $$ = op_and ($1, $3); }
116 | expr '=' expr { $$ = op_eq ($1, $3); }
117 | expr '>' expr { $$ = op_gt ($1, $3); }
118 | expr '<' expr { $$ = op_lt ($1, $3); }
119 | expr GE expr { $$ = op_ge ($1, $3); }
120 | expr LE expr { $$ = op_le ($1, $3); }
121 | expr NE expr { $$ = op_ne ($1, $3); }
122 | expr '+' expr { $$ = op_plus ($1, $3); }
123 | expr '-' expr { $$ = op_minus ($1, $3); }
124 | expr '*' expr { $$ = op_times ($1, $3); }
125 | expr '/' expr { $$ = op_div ($1, $3); }
126 | expr '%' expr { $$ = op_rem ($1, $3); }
127 | expr ':' expr { $$ = op_colon ($1, $3); }
139 vp = (struct val *) malloc (sizeof (*vp));
141 ast_log(LOG_WARNING, "malloc() failed\n");
158 vp = (struct val *) malloc (sizeof (*vp));
159 if (vp == NULL || ((vp->u.s = strdup (s)) == NULL)) {
160 ast_log(LOG_WARNING,"malloc() failed\n");
164 for(i = 1, isint = isdigit(s[0]) || s[0] == '-';
165 isint && i < strlen(s);
173 vp->type = numeric_string;
188 if (vp->type == string || vp->type == numeric_string)
200 ast_log(LOG_WARNING,"vp==NULL in to_integer()\n");
204 if (vp->type == integer)
207 if (vp->type == string)
210 /* vp->type == numeric_string, make it numeric */
212 i = strtoq(vp->u.s, (char**)NULL, 10);
215 ast_log(LOG_WARNING,"overflow\n");
230 if (vp->type == string || vp->type == numeric_string)
233 tmp = malloc ((size_t)25);
235 ast_log(LOG_WARNING,"malloc() failed\n");
239 sprintf (tmp, "%lld", (long long)vp->u.i);
249 /* only TRUE if this string is not a valid integer */
250 return (vp->type == string);
255 ast_yylex (YYSTYPE *lvalp, struct parser_control *karoto)
259 if (karoto->firsttoken==1) {
260 p=strtok_r(karoto->argv," ",&(karoto->ptrptr));
261 karoto->firsttoken=0;
263 p=strtok_r(NULL," ",&(karoto->ptrptr));
271 if (strlen (p) == 1) {
272 if (strchr ("|&=<>+-*/%:()", *p))
274 } else if (strlen (p) == 2 && p[1] == '=') {
276 case '>': return (GE);
277 case '<': return (LE);
278 case '!': return (NE);
282 lvalp->val = make_str (p);
290 if (vp->type == integer) {
291 return (vp->u.i == 0);
293 return (*vp->u.s == 0 || (to_integer (vp) && vp->u.i == 0));
298 char *ast_expr (char *arg)
300 struct parser_control karoto;
306 karoto.result = NULL;
310 ast_yyparse ((void *)&karoto);
314 if (karoto.result==NULL) {
318 if (karoto.result->type == integer) {
320 sprintf (pirouni,"%lld", (long long)karoto.result->u.i);
323 pirouni=strdup(karoto.result->u.s);
332 int main(int argc,char **argv) {
337 printf("=====%s======\n",s);
346 ast_log(LOG_WARNING,"ast_yyerror(): syntax error: %s\n",s);
355 if (is_zero_or_null (a)) {
368 if (is_zero_or_null (a) || is_zero_or_null (b)) {
371 return (make_integer ((quad_t)0));
384 if (isstring (a) || isstring (b)) {
387 r = make_integer ((quad_t)(strcoll (a->u.s, b->u.s) == 0));
391 r = make_integer ((quad_t)(a->u.i == b->u.i));
405 if (isstring (a) || isstring (b)) {
408 r = make_integer ((quad_t)(strcoll (a->u.s, b->u.s) > 0));
412 r = make_integer ((quad_t)(a->u.i > b->u.i));
426 if (isstring (a) || isstring (b)) {
429 r = make_integer ((quad_t)(strcoll (a->u.s, b->u.s) < 0));
433 r = make_integer ((quad_t)(a->u.i < b->u.i));
447 if (isstring (a) || isstring (b)) {
450 r = make_integer ((quad_t)(strcoll (a->u.s, b->u.s) >= 0));
454 r = make_integer ((quad_t)(a->u.i >= b->u.i));
468 if (isstring (a) || isstring (b)) {
471 r = make_integer ((quad_t)(strcoll (a->u.s, b->u.s) <= 0));
475 r = make_integer ((quad_t)(a->u.i <= b->u.i));
489 if (isstring (a) || isstring (b)) {
492 r = make_integer ((quad_t)(strcoll (a->u.s, b->u.s) != 0));
496 r = make_integer ((quad_t)(a->u.i != b->u.i));
508 /* sum of two positive numbers must be positive */
509 if (a > 0 && b > 0 && r <= 0)
511 /* sum of two negative numbers must be negative */
512 if (a < 0 && b < 0 && r >= 0)
514 /* all other cases are OK */
524 if (!to_integer (a) || !to_integer (b)) {
525 ast_log(LOG_WARNING,"non-numeric argument\n");
531 r = make_integer (/*(quad_t)*/(a->u.i + b->u.i));
532 if (chk_plus (a->u.i, b->u.i, r->u.i)) {
533 ast_log(LOG_WARNING,"overflow\n");
547 /* special case subtraction of QUAD_MIN */
554 /* this is allowed for b != QUAD_MIN */
555 return chk_plus (a, -b, r);
564 if (!to_integer (a) || !to_integer (b)) {
567 ast_log(LOG_WARNING, "non-numeric argument\n");
571 r = make_integer (/*(quad_t)*/(a->u.i - b->u.i));
572 if (chk_minus (a->u.i, b->u.i, r->u.i)) {
575 ast_log(LOG_WARNING, "overload\n");
587 /* special case: first operand is 0, no overflow possible */
590 /* cerify that result of division matches second operand */
602 if (!to_integer (a) || !to_integer (b)) {
605 ast_log(LOG_WARNING, "non-numeric argument\n");
609 r = make_integer (/*(quad_t)*/(a->u.i * b->u.i));
610 if (chk_times (a->u.i, b->u.i, r->u.i)) {
611 ast_log(LOG_WARNING, "overflow\n");
625 /* div by zero has been taken care of before */
626 /* only QUAD_MIN / -1 causes overflow */
627 if (a == QUAD_MIN && b == -1)
629 /* everything else is OK */
639 if (!to_integer (a) || !to_integer (b)) {
642 ast_log(LOG_WARNING, "non-numeric argument\n");
647 ast_log(LOG_WARNING, "division by zero\n");
653 r = make_integer (/*(quad_t)*/(a->u.i / b->u.i));
654 if (chk_div (a->u.i, b->u.i)) {
655 ast_log(LOG_WARNING, "overflow\n");
671 if (!to_integer (a) || !to_integer (b)) {
672 ast_log(LOG_WARNING, "non-numeric argument\n");
679 ast_log(LOG_WARNING, "div by zero\n");
685 r = make_integer (/*(quad_t)*/(a->u.i % b->u.i));
686 /* chk_rem necessary ??? */
702 /* coerce to both arguments to strings */
706 /* compile regular expression */
707 if ((eval = regcomp (&rp, b->u.s, 0)) != 0) {
708 regerror (eval, &rp, errbuf, sizeof(errbuf));
709 ast_log(LOG_WARNING,"regcomp() error : %s",errbuf);
715 /* compare string against pattern */
716 /* remember that patterns are anchored to the beginning of the line */
717 if (regexec(&rp, a->u.s, (size_t)2, rm, 0) == 0 && rm[0].rm_so == 0) {
718 if (rm[1].rm_so >= 0) {
719 *(a->u.s + rm[1].rm_eo) = '\0';
720 v = make_str (a->u.s + rm[1].rm_so);
723 v = make_integer ((quad_t)(rm[0].rm_eo - rm[0].rm_so));
726 if (rp.re_nsub == 0) {
727 v = make_integer ((quad_t)0);
733 /* free arguments and pattern buffer */