2014-11-16 07:57:40 -05:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2015-03-22 09:32:56 -04:00
|
|
|
#include <limits.h>
|
2014-11-16 09:19:50 -05:00
|
|
|
#include <stdio.h>
|
2015-03-22 09:32:56 -04:00
|
|
|
#include <stdlib.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-03-22 09:32:56 -04:00
|
|
|
/* tokens, one-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
|
|
|
};
|
|
|
|
|
2015-03-17 13:59:16 -04:00
|
|
|
struct val {
|
2015-03-22 09:32:56 -04:00
|
|
|
char *str;
|
|
|
|
long long num;
|
2015-03-17 13:59:16 -04:00
|
|
|
};
|
2014-09-29 08:43:41 -04:00
|
|
|
|
2014-11-17 09:39:57 -05:00
|
|
|
static void
|
2015-03-22 09:32:56 -04:00
|
|
|
enan(struct val *v)
|
2014-11-17 09:39:57 -05:00
|
|
|
{
|
2015-03-22 09:32:56 -04:00
|
|
|
if (!v->str)
|
|
|
|
return;
|
|
|
|
enprintf(2, "syntax error: expected integer, got %s\n", v->str);
|
2014-11-17 09:39:57 -05:00
|
|
|
}
|
|
|
|
|
2014-11-16 09:19:50 -05:00
|
|
|
static void
|
2015-03-22 09:32:56 -04:00
|
|
|
ezero(struct val *v)
|
2014-11-16 09:19:50 -05:00
|
|
|
{
|
2015-03-22 09:32:56 -04:00
|
|
|
if (v->num != 0)
|
|
|
|
return;
|
|
|
|
enprintf(2, "division by zero\n");
|
2015-02-25 23:12:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2015-03-22 09:32:56 -04:00
|
|
|
valcmp(struct val *a, struct val *b)
|
2015-02-25 23:12:14 -05:00
|
|
|
{
|
2015-03-22 09:32:56 -04:00
|
|
|
int ret;
|
2015-03-23 17:56:26 -04:00
|
|
|
char buf[BUFSIZ];
|
2015-03-22 09:32:56 -04:00
|
|
|
|
|
|
|
if (!a->str && !b->str) {
|
|
|
|
ret = (a->num > b->num) - (a->num < b->num);
|
|
|
|
} else if (a->str && !b->str) {
|
|
|
|
snprintf(buf, sizeof(buf), "%lld", b->num);
|
|
|
|
ret = strcmp(a->str, buf);
|
|
|
|
} else if (!a->str && b->str) {
|
|
|
|
snprintf(buf, sizeof(buf), "%lld", a->num);
|
|
|
|
ret = strcmp(buf, b->str);
|
|
|
|
} else {
|
|
|
|
ret = strcmp(a->str, b->str);
|
|
|
|
}
|
2015-02-25 23:12:14 -05:00
|
|
|
|
2015-03-22 09:32:56 -04:00
|
|
|
return ret;
|
2015-02-25 23:12:14 -05:00
|
|
|
}
|
|
|
|
|
2015-03-22 09:32:56 -04:00
|
|
|
static void
|
|
|
|
match(struct val *vstr, struct val *vregx, struct val *ret)
|
2015-02-25 23:12:14 -05:00
|
|
|
{
|
|
|
|
regex_t re;
|
|
|
|
regmatch_t matches[2];
|
2015-03-22 09:32:56 -04:00
|
|
|
long long d;
|
2015-03-23 17:31:36 -04:00
|
|
|
size_t anchlen;
|
2015-03-23 17:56:26 -04:00
|
|
|
char strbuf[BUFSIZ], regxbuf[BUFSIZ],
|
2015-03-22 09:32:56 -04:00
|
|
|
*s, *p, *anchreg, *str, *regx;
|
|
|
|
const char *errstr;
|
|
|
|
|
|
|
|
if (!vstr->str) {
|
|
|
|
snprintf(strbuf, sizeof(strbuf), "%lld", vstr->num);
|
|
|
|
str = strbuf;
|
|
|
|
} else {
|
|
|
|
str = vstr->str;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!vregx->str) {
|
|
|
|
snprintf(regxbuf, sizeof(regxbuf), "%lld", vregx->num);
|
|
|
|
regx = regxbuf;
|
|
|
|
} else {
|
|
|
|
regx = vregx->str;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* anchored regex */
|
2015-03-23 17:31:36 -04:00
|
|
|
anchlen = strlen(regx) + 1 + 1;
|
|
|
|
anchreg = emalloc(anchlen);
|
|
|
|
estrlcpy(anchreg, "^", anchlen);
|
|
|
|
estrlcat(anchreg, regx, anchlen);
|
2015-02-25 23:12:14 -05:00
|
|
|
enregcomp(3, &re, anchreg, 0);
|
2015-03-22 09:32:56 -04:00
|
|
|
free(anchreg);
|
2015-02-25 23:12:14 -05:00
|
|
|
|
|
|
|
if (regexec(&re, str, 2, matches, 0)) {
|
|
|
|
regfree(&re);
|
2015-03-22 09:32:56 -04:00
|
|
|
ret->str = re.re_nsub ? "" : NULL;
|
|
|
|
return;
|
|
|
|
} else if (re.re_nsub) {
|
2015-02-25 23:12:14 -05:00
|
|
|
regfree(&re);
|
2015-03-22 09:32:56 -04:00
|
|
|
|
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';
|
|
|
|
|
2015-03-22 09:32:56 -04:00
|
|
|
d = strtonum(s, LLONG_MIN, LLONG_MAX, &errstr);
|
|
|
|
if (!errstr) {
|
|
|
|
ret->num = d;
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
ret->str = enstrdup(3, s);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
regfree(&re);
|
|
|
|
str += matches[0].rm_so;
|
|
|
|
ret->num = utfnlen(str, matches[0].rm_eo - matches[0].rm_so);
|
|
|
|
return;
|
2015-02-25 23:12:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-16 07:29:02 -04:00
|
|
|
static void
|
2015-03-22 09:32:56 -04:00
|
|
|
doop(int *ophead, int *opp, struct val *valhead, struct val *valp)
|
2014-09-29 08:43:41 -04:00
|
|
|
{
|
2015-03-22 09:32:56 -04:00
|
|
|
struct val ret = { .str = NULL, .num = 0 }, *a, *b;
|
2015-02-25 23:12:14 -05:00
|
|
|
int op;
|
2014-09-29 08:43:41 -04:00
|
|
|
|
2015-03-22 09:32:56 -04:00
|
|
|
/* an operation "a op b" needs an operator and two values */
|
|
|
|
if (opp[-1] == '(')
|
2014-11-13 18:07:15 -05:00
|
|
|
enprintf(2, "syntax error: extra (\n");
|
2015-03-22 09:32:56 -04:00
|
|
|
if (valp - valhead < 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
|
|
|
|
2015-03-22 09:32:56 -04:00
|
|
|
a = valp - 2;
|
|
|
|
b = valp - 1;
|
|
|
|
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-03-22 09:32:56 -04:00
|
|
|
if ( a->str && *a->str) ret.str = a->str;
|
|
|
|
else if (!a->str && a->num) ret.num = a->num;
|
|
|
|
else if ( b->str && *b->str) ret.str = b->str;
|
|
|
|
else ret.num = b->num;
|
2014-11-16 09:19:50 -05:00
|
|
|
break;
|
|
|
|
case '&':
|
2015-03-22 09:32:56 -04:00
|
|
|
if (((a->str && *a->str) || a->num) &&
|
|
|
|
((b->str && *b->str) || b->num)) {
|
|
|
|
ret.str = a->str;
|
|
|
|
ret.num = a->num;
|
|
|
|
}
|
2014-11-16 09:19:50 -05:00
|
|
|
break;
|
2015-02-25 23:12:14 -05:00
|
|
|
|
2015-03-22 09:32:56 -04:00
|
|
|
case '=': ret.num = (valcmp(a, b) == 0); break;
|
|
|
|
case '>': ret.num = (valcmp(a, b) > 0); break;
|
|
|
|
case GE : ret.num = (valcmp(a, b) >= 0); break;
|
|
|
|
case '<': ret.num = (valcmp(a, b) < 0); break;
|
|
|
|
case LE : ret.num = (valcmp(a, b) <= 0); break;
|
|
|
|
case NE : ret.num = (valcmp(a, b) != 0); break;
|
2014-11-16 09:19:50 -05:00
|
|
|
|
2015-03-22 09:32:56 -04:00
|
|
|
case '+': enan(a); enan(b); ret.num = a->num + b->num; break;
|
|
|
|
case '-': enan(a); enan(b); ret.num = a->num - b->num; break;
|
|
|
|
case '*': enan(a); enan(b); ret.num = a->num * b->num; break;
|
|
|
|
case '/': enan(a); enan(b); ezero(b); ret.num = a->num / b->num; break;
|
|
|
|
case '%': enan(a); enan(b); ezero(b); ret.num = a->num % b->num; break;
|
2014-11-16 09:19:50 -05:00
|
|
|
|
2015-03-22 09:32:56 -04:00
|
|
|
case ':': match(a, b, &ret); break;
|
2014-09-29 08:43:41 -04:00
|
|
|
}
|
|
|
|
|
2015-03-22 09:32:56 -04:00
|
|
|
valp[-2] = ret;
|
2014-09-29 08:43:41 -04:00
|
|
|
}
|
|
|
|
|
2014-11-13 18:07:15 -05:00
|
|
|
static int
|
2015-03-17 13:59:16 -04:00
|
|
|
lex(char *s, struct val *v)
|
2014-09-29 08:43:41 -04:00
|
|
|
{
|
2015-03-22 09:32:56 -04:00
|
|
|
long long d;
|
|
|
|
int type = VAL;
|
|
|
|
char *ops = "|&=><+-*/%():";
|
|
|
|
const char *errstr;
|
|
|
|
|
|
|
|
d = strtonum(s, LLONG_MIN, LLONG_MAX, &errstr);
|
|
|
|
|
|
|
|
if (!errstr) {
|
|
|
|
/* integer */
|
|
|
|
v->num = d;
|
|
|
|
} else if (s[0] && strchr(ops, s[0]) && !s[1]) {
|
|
|
|
/* one-char operand */
|
|
|
|
type = s[0];
|
|
|
|
} else if (s[0] && strchr("><!", s[0]) && s[1] == '=' && !s[2]) {
|
|
|
|
/* two-char operand */
|
|
|
|
type = (s[0] == '>') ? GE : (s[0] == '<') ? LE : NE;
|
|
|
|
} else {
|
|
|
|
/* string */
|
|
|
|
v->str = s;
|
2014-09-29 08:43:41 -04:00
|
|
|
}
|
|
|
|
|
2015-03-22 09:32:56 -04:00
|
|
|
return type;
|
2014-09-29 08:43:41 -04:00
|
|
|
}
|
|
|
|
|
2014-11-13 18:07:15 -05:00
|
|
|
static int
|
2015-03-22 09:32:56 -04:00
|
|
|
parse(char *expr[], int numexpr)
|
2014-11-13 18:07:15 -05:00
|
|
|
{
|
2015-03-22 09:32:56 -04:00
|
|
|
struct val valhead[numexpr], *valp = valhead, v = { .str = NULL, .num = 0 };
|
|
|
|
int ophead[numexpr], *opp = ophead, type, lasttype = 0;
|
|
|
|
char prec[] = {
|
|
|
|
[ 0 ] = 0, [VAL] = 0, ['('] = 0, [')'] = 0,
|
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,
|
|
|
|
};
|
|
|
|
|
2015-03-22 09:32:56 -04:00
|
|
|
for (; *expr; expr++) {
|
|
|
|
switch ((type = lex(*expr, &v))) {
|
2014-11-16 14:29:29 -05:00
|
|
|
case VAL:
|
2015-03-23 17:44:07 -04:00
|
|
|
valp->str = v.str;
|
|
|
|
valp->num = v.num;
|
2015-03-22 09:32:56 -04:00
|
|
|
valp++;
|
2014-11-16 14:29:29 -05:00
|
|
|
break;
|
|
|
|
case '(':
|
2015-03-22 09:32:56 -04:00
|
|
|
*opp++ = type;
|
2014-11-16 14:29:29 -05:00
|
|
|
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-03-22 09:32:56 -04:00
|
|
|
while (opp > ophead && opp[-1] != '(')
|
|
|
|
doop(ophead, opp--, valhead, valp--);
|
|
|
|
if (opp == ophead)
|
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-03-22 09:32:56 -04:00
|
|
|
while (opp > ophead && prec[opp[-1]] >= prec[type])
|
|
|
|
doop(ophead, opp--, valhead, 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;
|
2015-03-22 09:32:56 -04:00
|
|
|
v.str = NULL;
|
|
|
|
v.num = 0;
|
2014-09-29 08:43:41 -04:00
|
|
|
}
|
2015-03-22 09:32:56 -04:00
|
|
|
while (opp > ophead)
|
|
|
|
doop(ophead, opp--, valhead, valp--);
|
|
|
|
if (valp == valhead)
|
2014-11-13 18:07:15 -05:00
|
|
|
enprintf(2, "syntax error: missing expression\n");
|
2015-03-22 09:32:56 -04:00
|
|
|
if (--valp > valhead)
|
2014-11-13 18:07:15 -05:00
|
|
|
enprintf(2, "syntax error: extra expression\n");
|
2014-09-29 08:43:41 -04:00
|
|
|
|
2015-03-22 09:32:56 -04:00
|
|
|
if (valp->str)
|
|
|
|
puts(valp->str);
|
2014-11-16 09:19:50 -05:00
|
|
|
else
|
2015-03-22 09:32:56 -04:00
|
|
|
printf("%lld\n", valp->num);
|
2014-09-29 08:43:41 -04:00
|
|
|
|
2015-03-22 09:32:56 -04:00
|
|
|
return (valp->str && *valp->str) || valp->num;
|
2014-11-16 14:29:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2015-03-22 09:32:56 -04:00
|
|
|
enprintf(3, "usage: %s expression\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
|
|
|
{
|
2015-04-20 17:28:21 -04:00
|
|
|
int ret;
|
|
|
|
|
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
|
|
|
|
2015-04-20 17:28:21 -04:00
|
|
|
ret = !parse(argv, argc);
|
Add *fshut() functions to properly flush file streams
This has been a known issue for a long time. Example:
printf "word" > /dev/full
wouldn't report there's not enough space on the device.
This is due to the fact that every libc has internal buffers
for stdout which store fragments of written data until they reach
a certain size or on some callback to flush them all at once to the
kernel.
You can force the libc to flush them with fflush(). In case flushing
fails, you can check the return value of fflush() and report an error.
However, previously, sbase didn't have such checks and without fflush(),
the libc silently flushes the buffers on exit without checking the errors.
No offense, but there's no way for the libc to report errors in the exit-
condition.
GNU coreutils solve this by having onexit-callbacks to handle the flushing
and report issues, but they have obvious deficiencies.
After long discussions on IRC, we came to the conclusion that checking the
return value of every io-function would be a bit too much, and having a
general-purpose fclose-wrapper would be the best way to go.
It turned out that fclose() alone is not enough to detect errors. The right
way to do it is to fflush() + check ferror on the fp and then to a fclose().
This is what fshut does and that's how it's done before each return.
The return value is obviously affected, reporting an error in case a flush
or close failed, but also when reading failed for some reason, the error-
state is caught.
the !!( ... + ...) construction is used to call all functions inside the
brackets and not "terminating" on the first.
We want errors to be reported, but there's no reason to stop flushing buffers
when one other file buffer has issues.
Obviously, functionales come before the flush and ret-logic comes after to
prevent early exits as well without reporting warnings if there are any.
One more advantage of fshut() is that it is even able to report errors
on obscure NFS-setups which the other coreutils are unable to detect,
because they only check the return-value of fflush() and fclose(),
not ferror() as well.
2015-04-04 15:25:17 -04:00
|
|
|
enfshut(3, stdout, "<stdout>");
|
2015-04-20 17:28:21 -04:00
|
|
|
|
|
|
|
return ret;
|
2014-09-29 08:43:41 -04:00
|
|
|
}
|