#include "libbb.h"
#include "xregex.h"
-/* The kinds of value we can have. */
-enum valtype {
- integer,
- string
-};
-typedef enum valtype TYPE;
-
#if ENABLE_EXPR_MATH_SUPPORT_64
typedef int64_t arith_t;
/* TODO: use bb_strtol[l]? It's easier to check for errors... */
+/* The kinds of value we can have. */
+enum {
+ INTEGER,
+ STRING
+};
+
/* A value is.... */
struct valinfo {
- TYPE type; /* Which kind. */
- union { /* The value itself. */
+ smallint type; /* Which kind. */
+ union { /* The value itself. */
arith_t i;
char *s;
} u;
{
VALUE *v;
- v = xmalloc(sizeof(VALUE));
- v->type = integer;
+ v = xzalloc(sizeof(VALUE));
+ if (INTEGER) /* otherwise xzaaloc did it already */
+ v->type = INTEGER;
v->u.i = i;
return v;
}
{
VALUE *v;
- v = xmalloc(sizeof(VALUE));
- v->type = string;
+ v = xzalloc(sizeof(VALUE));
+ if (STRING) /* otherwise xzaaloc did it already */
+ v->type = STRING;
v->u.s = xstrdup(s);
return v;
}
/* Free VALUE V, including structure components. */
-static void freev(VALUE * v)
+static void freev(VALUE *v)
{
- if (v->type == string)
+ if (v->type == STRING)
free(v->u.s);
free(v);
}
/* Return nonzero if V is a null-string or zero-number. */
-static int null(VALUE * v)
+static int null(VALUE *v)
{
- if (v->type == integer)
+ if (v->type == INTEGER)
return v->u.i == 0;
- /* string: */
+ /* STRING: */
return v->u.s[0] == '\0' || LONE_CHAR(v->u.s, '0');
}
-/* Coerce V to a string value (can't fail). */
+/* Coerce V to a STRING value (can't fail). */
-static void tostring(VALUE * v)
+static void tostring(VALUE *v)
{
- if (v->type == integer) {
+ if (v->type == INTEGER) {
v->u.s = xasprintf("%" PF_REZ "d", PF_REZ_TYPE v->u.i);
- v->type = string;
+ v->type = STRING;
}
}
-/* Coerce V to an integer value. Return 1 on success, 0 on failure. */
+/* Coerce V to an INTEGER value. Return 1 on success, 0 on failure. */
-static bool toarith(VALUE * v)
+static bool toarith(VALUE *v)
{
- if (v->type == string) {
+ if (v->type == STRING) {
arith_t i;
char *e;
return 0;
free(v->u.s);
v->u.i = i;
- v->type = integer;
+ v->type = INTEGER;
}
return 1;
}
-/* Return nonzero if the next token matches STR exactly.
+/* Return str[0]+str[1] if the next token matches STR exactly.
STR must not be NULL. */
-static bool nextarg(const char *str)
+static int nextarg(const char *str)
{
- if (*G.args == NULL)
+ if (*G.args == NULL || strcmp(*G.args, str) != 0)
return 0;
- return strcmp(*G.args, str) == 0;
+ return (unsigned char)str[0] + (unsigned char)str[1];
}
/* The comparison operator handling functions. */
-static int cmp_common(VALUE * l, VALUE * r, int op)
+static int cmp_common(VALUE *l, VALUE *r, int op)
{
- int cmpval;
+ arith_t ll, rr;
- if (l->type == string || r->type == string) {
+ ll = l->u.i;
+ rr = r->u.i;
+ if (l->type == STRING || r->type == STRING) {
tostring(l);
tostring(r);
- cmpval = strcmp(l->u.s, r->u.s);
- } else
- cmpval = l->u.i - r->u.i;
+ ll = strcmp(l->u.s, r->u.s);
+ rr = 0;
+ }
+ /* calculating ll - rr and checking the result is prone to overflows.
+ * We'll do it differently: */
if (op == '<')
- return cmpval < 0;
- if (op == ('L' + 'E'))
- return cmpval <= 0;
- if (op == '=')
- return cmpval == 0;
- if (op == '!')
- return cmpval != 0;
+ return ll < rr;
+ if (op == ('<' + '='))
+ return ll <= rr;
+ if (op == '=' || (op == '=' + '='))
+ return ll == rr;
+ if (op == '!' + '=')
+ return ll != rr;
if (op == '>')
- return cmpval > 0;
+ return ll > rr;
/* >= */
- return cmpval >= 0;
+ return ll >= rr;
}
/* The arithmetic operator handling functions. */
-static arith_t arithmetic_common(VALUE * l, VALUE * r, int op)
+static arith_t arithmetic_common(VALUE *l, VALUE *r, int op)
{
arith_t li, ri;
bb_error_msg_and_die("non-numeric argument");
li = l->u.i;
ri = r->u.i;
- if ((op == '/' || op == '%') && ri == 0)
- bb_error_msg_and_die("division by zero");
if (op == '+')
return li + ri;
- else if (op == '-')
+ if (op == '-')
return li - ri;
- else if (op == '*')
+ if (op == '*')
return li * ri;
- else if (op == '/')
+ if (ri == 0)
+ bb_error_msg_and_die("division by zero");
+ if (op == '/')
return li / ri;
- else
- return li % ri;
+ return li % ri;
}
/* Do the : operator.
SV is the VALUE for the lhs (the string),
PV is the VALUE for the rhs (the pattern). */
-static VALUE *docolon(VALUE * sv, VALUE * pv)
+static VALUE *docolon(VALUE *sv, VALUE *pv)
{
VALUE *v;
regex_t re_buffer;
/* expr uses an anchored pattern match, so check that there was a
* match and that the match starts at offset 0. */
- if (regexec(&re_buffer, sv->u.s, NMATCH, re_regs, 0) != REG_NOMATCH &&
- re_regs[0].rm_so == 0) {
+ if (regexec(&re_buffer, sv->u.s, NMATCH, re_regs, 0) != REG_NOMATCH
+ && re_regs[0].rm_so == 0
+ ) {
/* Were \(...\) used? */
if (re_buffer.re_nsub > 0) {
sv->u.s[re_regs[1].rm_eo] = '\0';
v = str_value(sv->u.s + re_regs[1].rm_so);
- } else
+ } else {
v = int_value(re_regs[0].rm_eo);
+ }
} else {
/* Match failed -- return the right kind of null. */
if (re_buffer.re_nsub > 0)
v = str_value("");
else {
v = xmalloc(sizeof(VALUE));
- v->type = string;
+ v->type = STRING;
v->u.s = xstrndup(l->u.s + i1->u.i - 1, i2->u.i);
}
freev(l);
l = eval5();
while (1) {
- if (nextarg("*"))
- op = '*';
- else if (nextarg("/"))
- op = '/';
- else if (nextarg("%"))
- op = '%';
- else
- return l;
+ op = nextarg("*");
+ if (!op) { op = nextarg("/");
+ if (!op) { op = nextarg("%");
+ if (!op) return l;
+ }}
G.args++;
r = eval5();
val = arithmetic_common(l, r, op);
l = eval4();
while (1) {
- if (nextarg("+"))
- op = '+';
- else if (nextarg("-"))
- op = '-';
- else
- return l;
+ op = nextarg("+");
+ if (!op) {
+ op = nextarg("-");
+ if (!op) return l;
+ }
G.args++;
r = eval4();
val = arithmetic_common(l, r, op);
l = eval3();
while (1) {
- if (nextarg("<"))
- op = '<';
- else if (nextarg("<="))
- op = 'L' + 'E';
- else if (nextarg("=") || nextarg("=="))
- op = '=';
- else if (nextarg("!="))
- op = '!';
- else if (nextarg(">="))
- op = 'G' + 'E';
- else if (nextarg(">"))
- op = '>';
- else
- return l;
+ op = nextarg("<");
+ if (!op) { op = nextarg("<=");
+ if (!op) { op = nextarg("=");
+ if (!op) { op = nextarg("==");
+ if (!op) { op = nextarg("!=");
+ if (!op) { op = nextarg(">=");
+ if (!op) { op = nextarg(">");
+ if (!op) return l;
+ }}}}}}
G.args++;
r = eval3();
toarith(l);
if (*G.args)
bb_error_msg_and_die("syntax error");
- if (v->type == integer)
+ if (v->type == INTEGER)
printf("%" PF_REZ "d\n", PF_REZ_TYPE v->u.i);
else
puts(v->u.s);