expr: improvements
- handle divide by zero. - use eregcomp(). - use emalloc(). - use snprintf() for safety and add a buffer size argument to valstr() just to be sure. - code-style fixes.
This commit is contained in:
parent
1822f70d12
commit
b3ae1a7b4b
81
expr.c
81
expr.c
|
@ -2,10 +2,11 @@
|
|||
#include <inttypes.h>
|
||||
#include <limits.h>
|
||||
#include <regex.h>
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "util.h"
|
||||
|
||||
enum {
|
||||
|
@ -21,7 +22,7 @@ static void doop(int*, int**, Val*, Val**);
|
|||
static Val match(Val, Val);
|
||||
static void num(Val);
|
||||
static int valcmp(Val, Val);
|
||||
static char *valstr(Val, char*);
|
||||
static char *valstr(Val, char*, size_t);
|
||||
static int yylex(void);
|
||||
static int yyparse(int);
|
||||
|
||||
|
@ -29,6 +30,13 @@ static char **args;
|
|||
static size_t intlen;
|
||||
static Val yylval;
|
||||
|
||||
static void
|
||||
ezero(intmax_t n)
|
||||
{
|
||||
if(n == 0)
|
||||
enprintf(2, "division by zero\n");
|
||||
}
|
||||
|
||||
/* otop points to one past last op
|
||||
* vtop points to one past last val
|
||||
* guaranteed otop != ops
|
||||
|
@ -51,18 +59,22 @@ doop(int *ops, int **otop, Val *vals, Val **vtop)
|
|||
|
||||
switch (op) {
|
||||
case '|':
|
||||
if ( a.s && *a.s) ret = (Val){ a.s , 0 };
|
||||
else if(!a.s && a.n) ret = (Val){ NULL, a.n };
|
||||
else if( b.s && *b.s) ret = (Val){ b.s , 0 };
|
||||
else ret = (Val){ NULL, b.n };
|
||||
if (a.s && *a.s)
|
||||
ret = (Val){ a.s, 0 };
|
||||
else if (!a.s && a.n)
|
||||
ret = (Val){ NULL, a.n };
|
||||
else if (b.s && *b.s)
|
||||
ret = (Val){ b.s, 0 };
|
||||
else
|
||||
ret = (Val){ NULL, b.n };
|
||||
break;
|
||||
|
||||
case '&':
|
||||
if (((a.s && *a.s) || a.n) &&
|
||||
((b.s && *b.s) || b.n)) ret = a;
|
||||
else ret = (Val){ NULL, 0 };
|
||||
((b.s && *b.s) || b.n))
|
||||
ret = a;
|
||||
else
|
||||
ret = (Val){ NULL, 0 };
|
||||
break;
|
||||
|
||||
case '=': ret = (Val){ NULL, valcmp(a, b) == 0 }; break;
|
||||
case '>': ret = (Val){ NULL, valcmp(a, b) > 0 }; break;
|
||||
case GE : ret = (Val){ NULL, valcmp(a, b) >= 0 }; break;
|
||||
|
@ -73,8 +85,8 @@ doop(int *ops, int **otop, Val *vals, Val **vtop)
|
|||
case '+': num(a); num(b); ret = (Val){ NULL, a.n + b.n }; break;
|
||||
case '-': num(a); num(b); ret = (Val){ NULL, a.n - b.n }; break;
|
||||
case '*': num(a); num(b); ret = (Val){ NULL, a.n * b.n }; break;
|
||||
case '/': num(a); num(b); ret = (Val){ NULL, a.n / b.n }; break;
|
||||
case '%': num(a); num(b); ret = (Val){ NULL, a.n % b.n }; break;
|
||||
case '/': num(a); num(b); ezero(b.n); ret = (Val){ NULL, a.n / b.n }; break;
|
||||
case '%': num(a); num(b); ezero(b.n); ret = (Val){ NULL, a.n % b.n }; break;
|
||||
|
||||
case ':': ret = match(a, b); break;
|
||||
}
|
||||
|
@ -87,29 +99,25 @@ doop(int *ops, int **otop, Val *vals, Val **vtop)
|
|||
static Val
|
||||
match(Val vstr, Val vregx)
|
||||
{
|
||||
char b1[intlen], *str = valstr(vstr , b1);
|
||||
char b2[intlen], *regx = valstr(vregx, b2);
|
||||
intmax_t d;
|
||||
char *ret, *p;
|
||||
regoff_t len;
|
||||
char b1[intlen], *str = valstr(vstr, b1, sizeof(b1));
|
||||
char b2[intlen], *regx = valstr(vregx, b2, sizeof(b2));
|
||||
|
||||
regex_t re;
|
||||
regmatch_t matches[2];
|
||||
char anchreg[strlen(regx) + 2];
|
||||
|
||||
sprintf(anchreg, "^%s", regx);
|
||||
|
||||
if(regcomp(&re, anchreg, 0))
|
||||
enprintf(3, "regcomp failed\n");
|
||||
snprintf(anchreg, sizeof(anchreg), "^%s", regx);
|
||||
enregcomp(3, &re, anchreg, 0);
|
||||
|
||||
if (regexec(&re, str, 2, matches, 0))
|
||||
return (Val){ (re.re_nsub ? "" : NULL), 0 };
|
||||
|
||||
if (re.re_nsub) {
|
||||
intmax_t d;
|
||||
char *ret, *p;
|
||||
regoff_t len = matches[1].rm_eo - matches[1].rm_so + 1;
|
||||
|
||||
if(!(ret = malloc(len))) // FIXME: free
|
||||
enprintf(3, "malloc failed\n");
|
||||
|
||||
len = matches[1].rm_eo - matches[1].rm_so + 1;
|
||||
ret = emalloc(len); /* TODO: free ret */
|
||||
d = strtoimax(ret, &p, 10);
|
||||
strlcpy(ret, str + matches[1].rm_so, len);
|
||||
|
||||
|
@ -130,8 +138,8 @@ num(Val v)
|
|||
static int
|
||||
valcmp(Val a, Val b)
|
||||
{
|
||||
char b1[intlen], *p = valstr(a, b1);
|
||||
char b2[intlen], *q = valstr(b, b2);
|
||||
char b1[intlen], *p = valstr(a, b1, sizeof(b1));
|
||||
char b2[intlen], *q = valstr(b, b2, sizeof(b2));
|
||||
|
||||
if (!a.s && !b.s)
|
||||
return (a.n > b.n) - (a.n < b.n);
|
||||
|
@ -139,11 +147,11 @@ valcmp(Val a, Val b)
|
|||
}
|
||||
|
||||
static char *
|
||||
valstr(Val val, char *buf)
|
||||
valstr(Val val, char *buf, size_t bufsiz)
|
||||
{
|
||||
char *p = val.s;
|
||||
if (!p) {
|
||||
sprintf(buf, "%"PRIdMAX, val.n);
|
||||
snprintf(buf, bufsiz, "%"PRIdMAX, val.n);
|
||||
p = buf;
|
||||
}
|
||||
return p;
|
||||
|
@ -167,9 +175,12 @@ yylex(void)
|
|||
if (*p && !p[1] && strchr(ops, *p))
|
||||
return *p;
|
||||
|
||||
if(strcmp(p, ">=") == 0) return GE;
|
||||
if(strcmp(p, "<=") == 0) return LE;
|
||||
if(strcmp(p, "!=") == 0) return NE;
|
||||
if (strcmp(p, ">=") == 0)
|
||||
return GE;
|
||||
if (strcmp(p, "<=") == 0)
|
||||
return LE;
|
||||
if (strcmp(p, "!=") == 0)
|
||||
return NE;
|
||||
|
||||
yylval = (Val){ p, 0 };
|
||||
return VAL;
|
||||
|
@ -222,8 +233,10 @@ yyparse(int argc)
|
|||
enprintf(2, "syntax error: extra expression\n");
|
||||
|
||||
vtop--;
|
||||
if(vtop->s) printf("%s\n" , vtop->s);
|
||||
else printf("%"PRIdMAX"\n", vtop->n);
|
||||
if (vtop->s)
|
||||
printf("%s\n", vtop->s);
|
||||
else
|
||||
printf("%"PRIdMAX"\n", vtop->n);
|
||||
|
||||
return (vtop->s && *vtop->s) || vtop->n;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user