2014-11-16 07:57:40 -05:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2014-11-13 18:07:15 -05:00
|
|
|
#include <inttypes.h>
|
2014-11-16 09:19:50 -05:00
|
|
|
#include <stdio.h>
|
2014-09-29 08:43:41 -04:00
|
|
|
#include <string.h>
|
2014-11-16 09:19:50 -05:00
|
|
|
|
2015-02-25 23:12:14 -05:00
|
|
|
#include "utf.h"
|
2014-11-13 18:07:15 -05:00
|
|
|
#include "util.h"
|
2014-09-29 08:43:41 -04:00
|
|
|
|
2015-02-25 23:12:14 -05:00
|
|
|
/* token types for lexing/parsing
|
|
|
|
* single character operators represent themselves */
|
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 {
|
2015-02-25 23:12:14 -05:00
|
|
|
char *s; /* iff s is NULL, Val is an integer */
|
2014-11-13 18:07:15 -05:00
|
|
|
intmax_t n;
|
|
|
|
} Val;
|
2014-09-29 08:43:41 -04:00
|
|
|
|
2014-11-13 18:07:15 -05:00
|
|
|
static size_t intlen;
|
2014-09-29 08:43:41 -04:00
|
|
|
|
2014-11-17 09:39:57 -05:00
|
|
|
static void
|
|
|
|
enan(Val v)
|
|
|
|
{
|
|
|
|
if (v.s)
|
|
|
|
enprintf(2, "syntax error: expected integer got `%s'\n", v.s);
|
|
|
|
}
|
|
|
|
|
2014-11-16 09:19:50 -05:00
|
|
|
static void
|
|
|
|
ezero(intmax_t n)
|
|
|
|
{
|
2014-11-16 14:29:29 -05:00
|
|
|
if (n == 0)
|
2014-11-16 09:19:50 -05:00
|
|
|
enprintf(2, "division by zero\n");
|
|
|
|
}
|
|
|
|
|
2015-02-25 23:12:14 -05:00
|
|
|
static char *
|
|
|
|
valstr(Val val, char *buf, size_t bufsiz)
|
|
|
|
{
|
|
|
|
if (val.s)
|
|
|
|
return val.s;
|
|
|
|
snprintf(buf, bufsiz, "%"PRIdMAX, val.n);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
valcmp(Val a, Val b)
|
|
|
|
{
|
|
|
|
char buf1[intlen], buf2[intlen];
|
|
|
|
char *astr = valstr(a, buf1, sizeof(buf1));
|
|
|
|
char *bstr = valstr(b, buf2, sizeof(buf2));
|
|
|
|
|
|
|
|
if (!a.s && !b.s)
|
|
|
|
return (a.n > b.n) - (a.n < b.n);
|
|
|
|
return strcmp(astr, bstr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* match vstr against BRE vregx (treat both values as strings)
|
|
|
|
* if there is at least one subexpression \(...\)
|
|
|
|
* then return the text matched by it \1 (empty string for no match)
|
|
|
|
* else return number of characters matched (0 for no match)
|
|
|
|
*/
|
|
|
|
static Val
|
|
|
|
match(Val vstr, Val vregx)
|
|
|
|
{
|
|
|
|
regex_t re;
|
|
|
|
regmatch_t matches[2];
|
2015-03-05 20:20:40 -05:00
|
|
|
intmax_t d;
|
|
|
|
char *s, *p, buf1[intlen], buf2[intlen];
|
2015-02-25 23:12:14 -05:00
|
|
|
char *str = valstr(vstr, buf1, sizeof(buf1));
|
|
|
|
char *regx = valstr(vregx, buf2, sizeof(buf2));;
|
|
|
|
char anchreg[strlen(regx) + 2];
|
|
|
|
|
|
|
|
/* expr(1p) "all patterns are anchored to the beginning of the string" */
|
|
|
|
snprintf(anchreg, sizeof(anchreg), "^%s", regx);
|
|
|
|
enregcomp(3, &re, anchreg, 0);
|
|
|
|
|
|
|
|
if (regexec(&re, str, 2, matches, 0)) {
|
|
|
|
regfree(&re);
|
|
|
|
return (Val){ (re.re_nsub ? "" : NULL), 0 };
|
|
|
|
}
|
|
|
|
|
|
|
|
if (re.re_nsub) {
|
|
|
|
regfree(&re);
|
2015-03-05 20:20:40 -05:00
|
|
|
s = str + matches[1].rm_so;
|
|
|
|
p = str + matches[1].rm_eo;
|
2015-02-25 23:12:14 -05:00
|
|
|
|
|
|
|
*p = '\0';
|
|
|
|
d = strtoimax(s, &p, 10);
|
|
|
|
if (*s && !*p) /* string matched by subexpression is an integer */
|
|
|
|
return (Val){ NULL, d };
|
|
|
|
|
|
|
|
/* FIXME? string is never free()d, worth fixing?
|
|
|
|
* need to allocate as it could be in buf1 instead of vstr.s */
|
|
|
|
return (Val){ enstrdup(3, s), 0 };
|
|
|
|
}
|
|
|
|
regfree(&re);
|
|
|
|
str += matches[0].rm_so;
|
|
|
|
return (Val){ NULL, utfnlen(str, matches[0].rm_eo - matches[0].rm_so) };
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ops points to a stack of operators, opp points to one past the last op
|
|
|
|
* vals points to a stack of values , valp points to one past the last val
|
|
|
|
* guaranteed that opp != ops
|
|
|
|
* ops is unused here, but still included for parity with vals
|
|
|
|
* pop operator, pop two values, apply operator, push result
|
|
|
|
*/
|
2014-10-16 07:29:02 -04:00
|
|
|
static void
|
2015-02-25 23:12:14 -05:00
|
|
|
doop(int *ops, int **opp, Val *vals, Val **valp)
|
2014-09-29 08:43:41 -04:00
|
|
|
{
|
2014-11-13 18:07:15 -05:00
|
|
|
Val ret, a, b;
|
2015-02-25 23:12:14 -05:00
|
|
|
int op;
|
2014-09-29 08:43:41 -04:00
|
|
|
|
2014-11-17 09:39:57 -05:00
|
|
|
/* For an operation, we need a valid operator
|
|
|
|
* and two values on the stack */
|
|
|
|
if ((*opp)[-1] == '(')
|
2014-11-13 18:07:15 -05:00
|
|
|
enprintf(2, "syntax error: extra (\n");
|
2015-02-25 23:12:14 -05:00
|
|
|
if (*valp - vals < 2)
|
2014-11-13 18:07:15 -05:00
|
|
|
enprintf(2, "syntax error: missing expression or extra operator\n");
|
2014-09-29 08:43:41 -04:00
|
|
|
|
2014-11-17 09:39:57 -05:00
|
|
|
a = (*valp)[-2];
|
|
|
|
b = (*valp)[-1];
|
2015-02-25 23:12:14 -05:00
|
|
|
op = (*opp)[-1];
|
2014-09-29 08:43:41 -04:00
|
|
|
|
2015-02-25 23:12:14 -05:00
|
|
|
switch (op) {
|
2014-11-16 09:19:50 -05:00
|
|
|
case '|':
|
2015-02-25 23:12:14 -05:00
|
|
|
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 };
|
2014-11-16 09:19:50 -05:00
|
|
|
break;
|
|
|
|
case '&':
|
2015-02-25 23:12:14 -05:00
|
|
|
if (((a.s && *a.s) || a.n) && ((b.s && *b.s) || b.n))
|
2014-11-16 09:19:50 -05:00
|
|
|
ret = a;
|
2015-02-25 23:12:14 -05:00
|
|
|
else
|
2014-11-16 09:19:50 -05:00
|
|
|
ret = (Val){ NULL, 0 };
|
|
|
|
break;
|
2015-02-25 23:12:14 -05:00
|
|
|
|
2014-11-16 09:19:50 -05:00
|
|
|
case '=': ret = (Val){ NULL, valcmp(a, b) == 0 }; break;
|
|
|
|
case '>': ret = (Val){ NULL, valcmp(a, b) > 0 }; break;
|
2015-02-25 23:12:14 -05:00
|
|
|
case GE : ret = (Val){ NULL, valcmp(a, b) >= 0 }; break;
|
2014-11-16 09:19:50 -05:00
|
|
|
case '<': ret = (Val){ NULL, valcmp(a, b) < 0 }; break;
|
2015-02-25 23:12:14 -05:00
|
|
|
case LE : ret = (Val){ NULL, valcmp(a, b) <= 0 }; break;
|
|
|
|
case NE : ret = (Val){ NULL, valcmp(a, b) != 0 }; break;
|
2014-11-16 09:19:50 -05:00
|
|
|
|
2015-02-25 23:12:14 -05:00
|
|
|
case '+': enan(a); enan(b); ret = (Val){ NULL, a.n + b.n }; break;
|
|
|
|
case '-': enan(a); enan(b); ret = (Val){ NULL, a.n - b.n }; break;
|
|
|
|
case '*': enan(a); enan(b); ret = (Val){ NULL, a.n * b.n }; break;
|
2014-11-17 09:39:57 -05:00
|
|
|
case '/': enan(a); enan(b); ezero(b.n); ret = (Val){ NULL, a.n / b.n }; break;
|
|
|
|
case '%': enan(a); enan(b); ezero(b.n); ret = (Val){ NULL, a.n % b.n }; break;
|
2014-11-16 09:19:50 -05:00
|
|
|
|
|
|
|
case ':': ret = match(a, b); break;
|
2014-09-29 08:43:41 -04:00
|
|
|
}
|
|
|
|
|
2014-11-17 09:39:57 -05:00
|
|
|
(*valp)[-2] = ret;
|
|
|
|
(*opp)--;
|
|
|
|
(*valp)--;
|
2014-09-29 08:43:41 -04:00
|
|
|
}
|
|
|
|
|
2015-02-25 23:12:14 -05:00
|
|
|
/* retrn the type of the next token, s
|
|
|
|
* if it is a value, place the value in v for use by parser
|
|
|
|
*/
|
2014-11-13 18:07:15 -05:00
|
|
|
static int
|
2015-02-25 23:12:14 -05:00
|
|
|
lex(char *s, Val *v)
|
2014-09-29 08:43:41 -04:00
|
|
|
{
|
2014-11-13 18:07:15 -05:00
|
|
|
intmax_t d;
|
2015-02-25 23:12:14 -05:00
|
|
|
char *p, *ops = "|&=><+-*/%():";
|
2014-09-29 08:43:41 -04:00
|
|
|
|
2014-11-17 09:39:57 -05:00
|
|
|
/* clean integer */
|
2015-02-25 23:12:14 -05:00
|
|
|
d = strtoimax(s, &p, 10);
|
|
|
|
if (*s && !*p) {
|
|
|
|
*v = (Val){ NULL, d };
|
2014-11-13 18:07:15 -05:00
|
|
|
return VAL;
|
2014-09-29 08:43:41 -04:00
|
|
|
}
|
|
|
|
|
2014-11-17 09:39:57 -05:00
|
|
|
/* one-char operand */
|
2015-02-25 23:12:14 -05:00
|
|
|
if (*s && !s[1] && strchr(ops, *s))
|
|
|
|
return *s;
|
2014-09-29 08:43:41 -04:00
|
|
|
|
2014-11-17 09:39:57 -05:00
|
|
|
/* two-char operand */
|
2015-02-25 23:12:14 -05:00
|
|
|
if (!strcmp(s, ">=")) return GE;
|
|
|
|
if (!strcmp(s, "<=")) return LE;
|
|
|
|
if (!strcmp(s, "!=")) return NE;
|
2014-09-29 08:43:41 -04:00
|
|
|
|
2014-11-17 09:39:57 -05:00
|
|
|
/* nothing matched, treat as string */
|
2015-02-25 23:12:14 -05:00
|
|
|
*v = (Val){ s, 0 };
|
2014-11-13 18:07:15 -05:00
|
|
|
return VAL;
|
2014-09-29 08:43:41 -04:00
|
|
|
}
|
|
|
|
|
2015-02-25 23:12:14 -05:00
|
|
|
/* using shunting-yard to convert from infix to rpn
|
|
|
|
* https://en.wikipedia.org/wiki/Shunting-yard_algorithm
|
|
|
|
* instead of creating rpn output to evaluate later, evaluate it immediately as
|
|
|
|
* it is created
|
|
|
|
* vals is the value stack, valp points to one past last value on the stack
|
|
|
|
* ops is the operator stack, opp points to one past last op on the stack
|
|
|
|
*/
|
2014-11-13 18:07:15 -05:00
|
|
|
static int
|
2015-03-05 20:20:40 -05:00
|
|
|
parse(char *expr[], int exprlen)
|
2014-11-13 18:07:15 -05:00
|
|
|
{
|
2015-03-05 20:20:40 -05:00
|
|
|
Val vals[exprlen], *valp = vals, v;
|
2015-02-25 23:12:14 -05:00
|
|
|
int ops[exprlen], *opp = ops;
|
2015-03-05 20:20:40 -05:00
|
|
|
int i, type, lasttype = 0;
|
2015-02-25 23:12:14 -05:00
|
|
|
char prec[] = { /* precedence of operators */
|
2014-11-13 18:07:15 -05:00
|
|
|
['|'] = 1,
|
|
|
|
['&'] = 2,
|
|
|
|
['='] = 3, ['>'] = 3, [GE] = 3, ['<'] = 3, [LE] = 3, [NE] = 3,
|
|
|
|
['+'] = 4, ['-'] = 4,
|
|
|
|
['*'] = 5, ['/'] = 5, ['%'] = 5,
|
|
|
|
[':'] = 6,
|
|
|
|
};
|
|
|
|
|
2014-11-16 14:29:29 -05:00
|
|
|
for (i = 0; i < exprlen; i++) {
|
2015-03-05 20:20:40 -05:00
|
|
|
switch ((type = lex(expr[i], &v))) {
|
2014-11-16 14:29:29 -05:00
|
|
|
case VAL:
|
2015-02-25 23:12:14 -05:00
|
|
|
*valp++ = v;
|
2014-11-16 14:29:29 -05:00
|
|
|
break;
|
|
|
|
case '(':
|
|
|
|
*opp++ = '(';
|
|
|
|
break;
|
2014-11-16 09:19:50 -05:00
|
|
|
case ')':
|
2014-11-16 14:29:29 -05:00
|
|
|
if (lasttype == '(')
|
2014-11-16 09:19:50 -05:00
|
|
|
enprintf(2, "syntax error: empty ( )\n");
|
2015-02-25 23:12:14 -05:00
|
|
|
while (opp > ops && opp[-1] != '(')
|
|
|
|
doop(ops, &opp, vals, &valp);
|
|
|
|
if (opp == ops)
|
2014-11-16 09:19:50 -05:00
|
|
|
enprintf(2, "syntax error: extra )\n");
|
2014-11-16 14:29:29 -05:00
|
|
|
opp--;
|
2014-11-16 09:19:50 -05:00
|
|
|
break;
|
2015-02-25 23:12:14 -05:00
|
|
|
default: /* operator */
|
2014-11-16 14:29:29 -05:00
|
|
|
if (prec[lasttype])
|
2014-11-16 09:19:50 -05:00
|
|
|
enprintf(2, "syntax error: extra operator\n");
|
2015-02-25 23:12:14 -05:00
|
|
|
while (opp > ops && prec[opp[-1]] >= prec[type])
|
|
|
|
doop(ops, &opp, vals, &valp);
|
2014-11-16 14:29:29 -05:00
|
|
|
*opp++ = type;
|
2014-11-16 09:19:50 -05:00
|
|
|
break;
|
2014-09-29 08:43:41 -04:00
|
|
|
}
|
2014-11-16 14:29:29 -05:00
|
|
|
lasttype = type;
|
2014-09-29 08:43:41 -04:00
|
|
|
}
|
2015-02-25 23:12:14 -05:00
|
|
|
while (opp > ops)
|
|
|
|
doop(ops, &opp, vals, &valp);
|
2014-09-29 08:43:41 -04:00
|
|
|
|
2015-02-25 23:12:14 -05:00
|
|
|
if (valp == vals)
|
2014-11-13 18:07:15 -05:00
|
|
|
enprintf(2, "syntax error: missing expression\n");
|
2015-02-25 23:12:14 -05:00
|
|
|
if (--valp != vals)
|
2014-11-13 18:07:15 -05:00
|
|
|
enprintf(2, "syntax error: extra expression\n");
|
2014-09-29 08:43:41 -04:00
|
|
|
|
2014-11-16 14:29:29 -05:00
|
|
|
if (valp->s)
|
|
|
|
printf("%s\n", valp->s);
|
2014-11-16 09:19:50 -05:00
|
|
|
else
|
2014-11-16 14:29:29 -05:00
|
|
|
printf("%"PRIdMAX"\n", valp->n);
|
2014-09-29 08:43:41 -04:00
|
|
|
|
2014-11-16 14:29:29 -05:00
|
|
|
return (valp->s && *valp->s) || valp->n;
|
|
|
|
}
|
|
|
|
|
2015-02-25 23:12:14 -05:00
|
|
|
/* the only way to get usage() is if the user didn't supply -- and expression
|
|
|
|
* begins with a -
|
|
|
|
* expr(1p): "... the conforming application must employ the -- construct ...
|
|
|
|
* if there is any chance the first operand might be a negative integer (or any
|
|
|
|
* string with a leading minus"
|
|
|
|
*/
|
2014-11-16 14:29:29 -05:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2015-02-25 23:12:14 -05:00
|
|
|
enprintf(3, "usage: %s [--] expression\n"
|
|
|
|
"note : the -- is mandatory if expression begins with a -\n", argv0);
|
2014-09-29 08:43:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2014-11-16 14:29:29 -05:00
|
|
|
main(int argc, char *argv[])
|
2014-09-29 08:43:41 -04:00
|
|
|
{
|
2014-11-16 14:29:29 -05:00
|
|
|
intmax_t n = INTMAX_MIN;
|
|
|
|
|
|
|
|
/* Get the maximum number of digits (+ sign) */
|
2015-02-25 23:12:14 -05:00
|
|
|
for (intlen = (n < 0); n; n /= 10, ++intlen)
|
|
|
|
;
|
2014-09-29 08:43:41 -04:00
|
|
|
|
2014-11-16 14:29:29 -05:00
|
|
|
ARGBEGIN {
|
2015-02-26 13:07:02 -05:00
|
|
|
default:
|
|
|
|
usage();
|
2014-11-16 14:29:29 -05:00
|
|
|
} ARGEND;
|
2014-09-29 08:43:41 -04:00
|
|
|
|
2014-11-16 14:29:29 -05:00
|
|
|
return !parse(argv, argc);
|
2014-09-29 08:43:41 -04:00
|
|
|
}
|