2014-11-13 18:07:15 -05:00
|
|
|
#include <inttypes.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <regex.h>
|
2014-09-29 08:43:41 -04:00
|
|
|
#include <stdio.h>
|
2014-11-13 18:07:15 -05:00
|
|
|
#include <stdint.h>
|
2014-09-29 08:43:41 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2014-11-13 18:07:15 -05:00
|
|
|
#include "util.h"
|
2014-09-29 08:43:41 -04:00
|
|
|
|
2014-11-13 18:07:15 -05:00
|
|
|
enum {
|
|
|
|
VAL = CHAR_MAX + 1, GE, LE, NE
|
2014-09-29 08:43:41 -04:00
|
|
|
};
|
|
|
|
|
2014-11-13 18:07:15 -05:00
|
|
|
typedef struct {
|
|
|
|
char *s;
|
|
|
|
intmax_t n;
|
|
|
|
} Val;
|
2014-09-29 08:43:41 -04:00
|
|
|
|
|
|
|
|
2014-11-13 18:07:15 -05:00
|
|
|
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 int yylex(void);
|
|
|
|
static int yyparse(int);
|
2014-09-29 08:43:41 -04:00
|
|
|
|
2014-11-13 18:07:15 -05:00
|
|
|
static char **args;
|
|
|
|
static size_t intlen;
|
|
|
|
static Val yylval;
|
2014-09-29 08:43:41 -04:00
|
|
|
|
2014-11-13 18:07:15 -05:00
|
|
|
// otop points to one past last op
|
|
|
|
// vtop points to one past last val
|
|
|
|
// guaranteed otop != ops
|
|
|
|
// pop two vals, pop op, apply op, push val
|
2014-10-16 07:29:02 -04:00
|
|
|
static void
|
2014-11-13 18:07:15 -05:00
|
|
|
doop(int *ops, int **otop, Val *vals, Val **vtop)
|
2014-09-29 08:43:41 -04:00
|
|
|
{
|
2014-11-13 18:07:15 -05:00
|
|
|
Val ret, a, b;
|
|
|
|
int op;
|
2014-09-29 08:43:41 -04:00
|
|
|
|
2014-11-13 18:07:15 -05:00
|
|
|
if((*otop)[-1] == '(')
|
|
|
|
enprintf(2, "syntax error: extra (\n");
|
|
|
|
if(*vtop - vals < 2)
|
|
|
|
enprintf(2, "syntax error: missing expression or extra operator\n");
|
2014-09-29 08:43:41 -04:00
|
|
|
|
2014-11-13 18:07:15 -05:00
|
|
|
a = (*vtop)[-2];
|
|
|
|
b = (*vtop)[-1];
|
|
|
|
op = (*otop)[-1];
|
2014-09-29 08:43:41 -04:00
|
|
|
|
2014-11-13 18:07:15 -05:00
|
|
|
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 };
|
|
|
|
break;
|
2014-09-29 08:43:41 -04:00
|
|
|
|
2014-11-13 18:07:15 -05:00
|
|
|
case '&':
|
|
|
|
if(((a.s && *a.s) || a.n) &&
|
|
|
|
((b.s && *b.s) || b.n)) ret = a;
|
|
|
|
else ret = (Val){ NULL, 0 };
|
|
|
|
break;
|
2014-09-29 08:43:41 -04:00
|
|
|
|
2014-11-13 18:07:15 -05:00
|
|
|
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;
|
|
|
|
case '<': ret = (Val){ NULL, valcmp(a, b) < 0 }; break;
|
|
|
|
case LE : ret = (Val){ NULL, valcmp(a, b) <= 0 }; break;
|
|
|
|
case NE : ret = (Val){ NULL, valcmp(a, b) != 0 }; break;
|
2014-09-29 08:43:41 -04:00
|
|
|
|
2014-11-13 18:07:15 -05:00
|
|
|
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;
|
2014-09-29 08:43:41 -04:00
|
|
|
|
2014-11-13 18:07:15 -05:00
|
|
|
case ':': ret = match(a, b); break;
|
2014-09-29 08:43:41 -04:00
|
|
|
}
|
|
|
|
|
2014-11-13 18:07:15 -05:00
|
|
|
(*vtop)[-2] = ret;
|
|
|
|
(*otop)--;
|
|
|
|
(*vtop)--;
|
2014-09-29 08:43:41 -04:00
|
|
|
}
|
|
|
|
|
2014-11-13 18:07:15 -05:00
|
|
|
static Val
|
|
|
|
match(Val vstr, Val vregx)
|
2014-09-29 08:43:41 -04:00
|
|
|
{
|
2014-11-13 18:07:15 -05:00
|
|
|
char b1[intlen], *str = valstr(vstr , b1);
|
|
|
|
char b2[intlen], *regx = valstr(vregx, b2);
|
2014-09-29 08:43:41 -04:00
|
|
|
|
2014-11-13 18:07:15 -05:00
|
|
|
regex_t re;
|
|
|
|
regmatch_t matches[2];
|
|
|
|
char anchreg[strlen(regx) + 2];
|
2014-09-29 08:43:41 -04:00
|
|
|
|
2014-11-13 18:07:15 -05:00
|
|
|
sprintf(anchreg, "^%s", regx);
|
2014-09-29 08:43:41 -04:00
|
|
|
|
2014-11-13 18:07:15 -05:00
|
|
|
if(regcomp(&re, anchreg, 0))
|
|
|
|
enprintf(3, "regcomp failed\n");
|
2014-09-29 08:43:41 -04:00
|
|
|
|
2014-11-13 18:07:15 -05:00
|
|
|
if(regexec(&re, str, 2, matches, 0))
|
|
|
|
return (Val){ (re.re_nsub ? "" : NULL), 0 };
|
2014-09-29 08:43:41 -04:00
|
|
|
|
2014-11-13 18:07:15 -05:00
|
|
|
if(re.re_nsub) {
|
|
|
|
intmax_t d;
|
|
|
|
char *ret, *p;
|
|
|
|
regoff_t len = matches[1].rm_eo - matches[1].rm_so + 1;
|
2014-09-29 08:43:41 -04:00
|
|
|
|
2014-11-13 18:07:15 -05:00
|
|
|
if(!(ret = malloc(len))) // FIXME: free
|
|
|
|
enprintf(3, "malloc failed\n");
|
2014-09-29 08:43:41 -04:00
|
|
|
|
2014-11-13 18:07:15 -05:00
|
|
|
d = strtoimax(ret, &p, 10);
|
|
|
|
strlcpy(ret, str + matches[1].rm_so, len);
|
2014-09-29 08:43:41 -04:00
|
|
|
|
2014-11-13 18:07:15 -05:00
|
|
|
if(*ret && !*p)
|
|
|
|
return (Val){ NULL, d };
|
|
|
|
return (Val){ ret, 0 };
|
2014-09-29 08:43:41 -04:00
|
|
|
}
|
2014-11-13 18:07:15 -05:00
|
|
|
return (Val){ NULL, matches[0].rm_eo - matches[0].rm_so };
|
2014-09-29 08:43:41 -04:00
|
|
|
}
|
|
|
|
|
2014-10-16 07:29:02 -04:00
|
|
|
static void
|
2014-11-13 18:07:15 -05:00
|
|
|
num(Val v)
|
2014-09-29 08:43:41 -04:00
|
|
|
{
|
2014-11-13 18:07:15 -05:00
|
|
|
if(v.s)
|
|
|
|
enprintf(2, "syntax error: expected integer got `%s'\n", v.s);
|
2014-09-29 08:43:41 -04:00
|
|
|
}
|
|
|
|
|
2014-11-13 18:07:15 -05:00
|
|
|
static int
|
|
|
|
valcmp(Val a, Val b)
|
2014-09-29 08:43:41 -04:00
|
|
|
{
|
2014-11-13 18:07:15 -05:00
|
|
|
char b1[intlen], *p = valstr(a, b1);
|
|
|
|
char b2[intlen], *q = valstr(b, b2);
|
2014-09-29 08:43:41 -04:00
|
|
|
|
2014-11-13 18:07:15 -05:00
|
|
|
if(!a.s && !b.s)
|
|
|
|
return (a.n > b.n) - (a.n < b.n);
|
|
|
|
return strcmp(p, q);
|
2014-09-29 08:43:41 -04:00
|
|
|
}
|
|
|
|
|
2014-11-13 18:07:15 -05:00
|
|
|
static char *
|
|
|
|
valstr(Val val, char *buf)
|
2014-09-29 08:43:41 -04:00
|
|
|
{
|
2014-11-13 18:07:15 -05:00
|
|
|
char *p = val.s;
|
|
|
|
if(!p) {
|
|
|
|
sprintf(buf, "%"PRIdMAX, val.n);
|
|
|
|
p = buf;
|
2014-09-29 08:43:41 -04:00
|
|
|
}
|
2014-11-13 18:07:15 -05:00
|
|
|
return p;
|
2014-09-29 08:43:41 -04:00
|
|
|
}
|
|
|
|
|
2014-11-13 18:07:15 -05:00
|
|
|
static int
|
|
|
|
yylex(void)
|
2014-09-29 08:43:41 -04:00
|
|
|
{
|
2014-11-13 18:07:15 -05:00
|
|
|
intmax_t d;
|
|
|
|
char *q, *p, *ops = "|&=><+-*/%():";
|
2014-09-29 08:43:41 -04:00
|
|
|
|
2014-11-13 18:07:15 -05:00
|
|
|
if(!(p = *args++))
|
|
|
|
return 0;
|
2014-09-29 08:43:41 -04:00
|
|
|
|
2014-11-13 18:07:15 -05:00
|
|
|
d = strtoimax(p, &q, 10);
|
|
|
|
if(*p && !*q) {
|
|
|
|
yylval = (Val){ NULL, d };
|
|
|
|
return VAL;
|
2014-09-29 08:43:41 -04:00
|
|
|
}
|
|
|
|
|
2014-11-13 18:07:15 -05:00
|
|
|
if(*p && !p[1] && strchr(ops, *p))
|
|
|
|
return *p;
|
2014-09-29 08:43:41 -04:00
|
|
|
|
2014-11-13 18:07:15 -05:00
|
|
|
if(strcmp(p, ">=") == 0) return GE;
|
|
|
|
if(strcmp(p, "<=") == 0) return LE;
|
|
|
|
if(strcmp(p, "!=") == 0) return NE;
|
2014-09-29 08:43:41 -04:00
|
|
|
|
2014-11-13 18:07:15 -05:00
|
|
|
yylval = (Val){ p, 0 };
|
|
|
|
return VAL;
|
2014-09-29 08:43:41 -04:00
|
|
|
}
|
|
|
|
|
2014-11-13 18:07:15 -05:00
|
|
|
static int
|
|
|
|
yyparse(int argc)
|
|
|
|
{
|
|
|
|
Val vals[argc], *vtop = vals;
|
|
|
|
int ops [argc], *otop = ops;
|
|
|
|
int type, last = 0;
|
|
|
|
char prec[] = {
|
|
|
|
['|'] = 1,
|
|
|
|
['&'] = 2,
|
|
|
|
['='] = 3, ['>'] = 3, [GE] = 3, ['<'] = 3, [LE] = 3, [NE] = 3,
|
|
|
|
['+'] = 4, ['-'] = 4,
|
|
|
|
['*'] = 5, ['/'] = 5, ['%'] = 5,
|
|
|
|
[':'] = 6,
|
|
|
|
};
|
|
|
|
|
|
|
|
while((type = yylex())) {
|
|
|
|
switch (type) {
|
|
|
|
case VAL: *vtop++ = yylval; break;
|
|
|
|
case '(': *otop++ = '(' ; break;
|
|
|
|
case ')':
|
|
|
|
if(last == '(')
|
|
|
|
enprintf(2, "syntax error: empty ( )\n");
|
|
|
|
while(otop > ops && otop[-1] != '(')
|
|
|
|
doop(ops, &otop, vals, &vtop);
|
|
|
|
if(otop == ops)
|
|
|
|
enprintf(2, "syntax error: extra )\n");
|
|
|
|
otop--;
|
2014-09-29 08:43:41 -04:00
|
|
|
break;
|
2014-11-13 18:07:15 -05:00
|
|
|
default :
|
|
|
|
if(prec[last])
|
|
|
|
enprintf(2, "syntax error: extra operator\n");
|
|
|
|
while(otop > ops && prec[otop[-1]] >= prec[type])
|
|
|
|
doop(ops, &otop, vals, &vtop);
|
|
|
|
*otop++ = type;
|
2014-09-29 08:43:41 -04:00
|
|
|
break;
|
|
|
|
}
|
2014-11-13 18:07:15 -05:00
|
|
|
last = type;
|
2014-09-29 08:43:41 -04:00
|
|
|
}
|
2014-11-13 18:07:15 -05:00
|
|
|
while(otop > ops)
|
|
|
|
doop(ops, &otop, vals, &vtop);
|
2014-09-29 08:43:41 -04:00
|
|
|
|
2014-11-13 18:07:15 -05:00
|
|
|
if(vtop == vals)
|
|
|
|
enprintf(2, "syntax error: missing expression\n");
|
|
|
|
if(vtop - vals > 1)
|
|
|
|
enprintf(2, "syntax error: extra expression\n");
|
2014-09-29 08:43:41 -04:00
|
|
|
|
2014-11-13 18:07:15 -05:00
|
|
|
vtop--;
|
|
|
|
if(vtop->s) printf("%s\n" , vtop->s);
|
|
|
|
else printf("%"PRIdMAX"\n", vtop->n);
|
2014-09-29 08:43:41 -04:00
|
|
|
|
2014-11-13 18:07:15 -05:00
|
|
|
return (vtop->s && *vtop->s) || vtop->n;
|
2014-09-29 08:43:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2014-11-13 18:07:15 -05:00
|
|
|
main(int argc, char **argv)
|
2014-09-29 08:43:41 -04:00
|
|
|
{
|
2014-11-13 18:07:15 -05:00
|
|
|
if(!(intlen = snprintf(NULL, 0, "%"PRIdMAX, INTMAX_MIN) + 1))
|
|
|
|
enprintf(3, "failed to get max digits\n");
|
2014-09-29 08:43:41 -04:00
|
|
|
|
2014-11-13 18:07:15 -05:00
|
|
|
args = argv + 1;
|
|
|
|
if(*args && !strcmp("--", *args))
|
|
|
|
++args;
|
2014-09-29 08:43:41 -04:00
|
|
|
|
2014-11-13 18:07:15 -05:00
|
|
|
return !yyparse(argc);
|
2014-09-29 08:43:41 -04:00
|
|
|
}
|