ash: eval: Add assignment built-in support again
[oweals/busybox.git] / coreutils / expr.c
index d6cc82e3ee5cae81419ded7334fe66ed535a5314..b247f08db10801fdaaad5e8e7f98f638c8e3cedc 100644 (file)
  * based on GNU expr Mike Parker.
  * Copyright (C) 86, 1991-1997, 1999 Free Software Foundation, Inc.
  *
- * Busybox modifications 
+ * Busybox modifications
  * Copyright (c) 2000  Edward Betts <edward@debian.org>.
+ * Copyright (C) 2003-2005  Vladimir Oleynik <dzo@simtreas.ru>
+ *  - reduced 464 bytes.
+ *  - 64 math support
  *
- * this program is free software; you can redistribute it and/or modify
- * it under the terms of the gnu general public license as published by
- * the free software foundation; either version 2 of the license, or
- * (at your option) any later version.
- *
- * this program is distributed in the hope that it will be useful,
- * but without any warranty; without even the implied warranty of
- * merchantability or fitness for a particular purpose. see the gnu
- * general public license for more details.
- *
- * you should have received a copy of the gnu general public license
- * along with this program; if not, write to the free software
- * foundation, inc., 59 temple place, suite 330, boston, ma 02111-1307 usa
- *
+ * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  */
-
 /* This program evaluates expressions.  Each token (operator, operand,
- * parenthesis) of the expression must be a seperate argument.  The
+ * parenthesis) of the expression must be a separate argument.  The
  * parser used is a reasonably general one, though any incarnation of
  * it is language-specific.  It is especially nice for expressions.
  *
  * No parse tree is needed; a new node is evaluated immediately.
  * One function can handle multiple operators all of equal precedence,
- * provided they all associate ((x op x) op x). */
-
-/* no getopt needed */
-
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-#include <regex.h>
-#include <sys/types.h>
-#include "busybox.h"
-
+ * provided they all associate ((x op x) op x).
+ */
+//config:config EXPR
+//config:      bool "expr (6.6 kb)"
+//config:      default y
+//config:      help
+//config:      expr is used to calculate numbers and print the result
+//config:      to standard output.
+//config:
+//config:config EXPR_MATH_SUPPORT_64
+//config:      bool "Extend Posix numbers support to 64 bit"
+//config:      default y
+//config:      depends on EXPR
+//config:      help
+//config:      Enable 64-bit math support in the expr applet. This will make
+//config:      the applet slightly larger, but will allow computation with very
+//config:      large numbers.
+
+//applet:IF_EXPR(APPLET_NOEXEC(expr, expr, BB_DIR_USR_BIN, BB_SUID_DROP, expr))
+
+//kbuild:lib-$(CONFIG_EXPR) += expr.o
+
+//usage:#define expr_trivial_usage
+//usage:       "EXPRESSION"
+//usage:#define expr_full_usage "\n\n"
+//usage:       "Print the value of EXPRESSION to stdout\n"
+//usage:    "\n"
+//usage:       "EXPRESSION may be:\n"
+//usage:       "       ARG1 | ARG2     ARG1 if it is neither null nor 0, otherwise ARG2\n"
+//usage:       "       ARG1 & ARG2     ARG1 if neither argument is null or 0, otherwise 0\n"
+//usage:       "       ARG1 < ARG2     1 if ARG1 is less than ARG2, else 0. Similarly:\n"
+//usage:       "       ARG1 <= ARG2\n"
+//usage:       "       ARG1 = ARG2\n"
+//usage:       "       ARG1 != ARG2\n"
+//usage:       "       ARG1 >= ARG2\n"
+//usage:       "       ARG1 > ARG2\n"
+//usage:       "       ARG1 + ARG2     Sum of ARG1 and ARG2. Similarly:\n"
+//usage:       "       ARG1 - ARG2\n"
+//usage:       "       ARG1 * ARG2\n"
+//usage:       "       ARG1 / ARG2\n"
+//usage:       "       ARG1 % ARG2\n"
+//usage:       "       STRING : REGEXP         Anchored pattern match of REGEXP in STRING\n"
+//usage:       "       match STRING REGEXP     Same as STRING : REGEXP\n"
+//usage:       "       substr STRING POS LENGTH Substring of STRING, POS counted from 1\n"
+//usage:       "       index STRING CHARS      Index in STRING where any CHARS is found, or 0\n"
+//usage:       "       length STRING           Length of STRING\n"
+//usage:       "       quote TOKEN             Interpret TOKEN as a string, even if\n"
+//usage:       "                               it is a keyword like 'match' or an\n"
+//usage:       "                               operator like '/'\n"
+//usage:       "       (EXPRESSION)            Value of EXPRESSION\n"
+//usage:       "\n"
+//usage:       "Beware that many operators need to be escaped or quoted for shells.\n"
+//usage:       "Comparisons are arithmetic if both ARGs are numbers, else\n"
+//usage:       "lexicographical. Pattern matches return the string matched between\n"
+//usage:       "\\( and \\) or null; if \\( and \\) are not used, they return the number\n"
+//usage:       "of characters matched or 0."
+
+#include "libbb.h"
+#include "common_bufsiz.h"
+#include "xregex.h"
+
+#if ENABLE_EXPR_MATH_SUPPORT_64
+typedef int64_t arith_t;
+
+#define PF_REZ      "ll"
+#define PF_REZ_TYPE (long long)
+#define STRTOL(s, e, b) strtoll(s, e, b)
+#else
+typedef long arith_t;
+
+#define PF_REZ      "l"
+#define PF_REZ_TYPE (long)
+#define STRTOL(s, e, b) strtol(s, e, b)
+#endif
+
+/* TODO: use bb_strtol[l]? It's easier to check for errors... */
 
 /* The kinds of value we can have.  */
-enum valtype {
-       integer,
-       string
+enum {
+       INTEGER,
+       STRING
 };
-typedef enum valtype TYPE;
 
 /* A value is.... */
 struct valinfo {
-       TYPE type;                      /* Which kind. */
-       union {                         /* The value itself. */
-               int i;
+       smallint type;                  /* Which kind. */
+       union {                         /* The value itself. */
+               arith_t i;
                char *s;
        } u;
 };
 typedef struct valinfo VALUE;
 
 /* The arguments given to the program, minus the program name.  */
-static char **args;
-
-static VALUE *docolon (VALUE *sv, VALUE *pv);
-static VALUE *eval (void);
-static VALUE *int_value (int i);
-static VALUE *str_value (char *s);
-static int nextarg (char *str);
-static int null (VALUE *v);
-static int toarith (VALUE *v);
-static void freev (VALUE *v);
-static void tostring (VALUE *v);
-
-int expr_main (int argc, char **argv)
-{
-       VALUE *v;
-
-       if (argc == 1) {
-               error_msg_and_die("too few arguments");
-       }
-
-       args = argv + 1;
-
-       v = eval ();
-       if (*args)
-               error_msg_and_die ("syntax error");
+struct globals {
+       char **args;
+} FIX_ALIASING;
+#define G (*(struct globals*)bb_common_bufsiz1)
+#define INIT_G() do { \
+       setup_common_bufsiz(); \
+       /* NB: noexec applet - globals not zeroed */ \
+} while (0)
 
-       if (v->type == integer)
-               printf ("%d\n", v->u.i);
-       else
-               puts (v->u.s);
+/* forward declarations */
+static VALUE *eval(void);
 
-       exit (null (v));
-}
 
 /* Return a VALUE for I.  */
 
-static VALUE *int_value (int i)
+static VALUE *int_value(arith_t i)
 {
        VALUE *v;
 
-       v = xmalloc (sizeof(VALUE));
-       v->type = integer;
+       v = xzalloc(sizeof(VALUE));
+       if (INTEGER) /* otherwise xzalloc did it already */
+               v->type = INTEGER;
        v->u.i = i;
        return v;
 }
 
 /* Return a VALUE for S.  */
 
-static VALUE *str_value (char *s)
+static VALUE *str_value(const char *s)
 {
        VALUE *v;
 
-       v = xmalloc (sizeof(VALUE));
-       v->type = string;
-       v->u.s = strdup (s);
+       v = xzalloc(sizeof(VALUE));
+       if (STRING) /* otherwise xzalloc 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)
-               free (v->u.s);
-       free (v);
+       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)
 {
-       switch (v->type) {
-               case integer:
-                       return v->u.i == 0;
-               case string:
-                       return v->u.s[0] == '\0' || strcmp (v->u.s, "0") == 0;
-               default:
-                       abort ();
-       }
+       if (v->type == INTEGER)
+               return v->u.i == 0;
+       /* 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)
 {
-       char *temp;
-
-       if (v->type == integer) {
-               temp = xmalloc (4 * (sizeof (int) / sizeof (char)));
-               sprintf (temp, "%d", v->u.i);
-               v->u.s = temp;
-               v->type = string;
+       if (v->type == INTEGER) {
+               v->u.s = xasprintf("%" PF_REZ "d", PF_REZ_TYPE v->u.i);
+               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 int toarith (VALUE *v)
+static bool toarith(VALUE *v)
 {
-       int i;
-
-       switch (v->type) {
-               case integer:
-                       return 1;
-               case string:
-                       i = 0;
-                       /* Don't interpret the empty string as an integer.  */
-                       if (v->u.s == 0)
-                               return 0;
-                       i = atoi(v->u.s);
-                       free (v->u.s);
-                       v->u.i = i;
-                       v->type = integer;
-                       return 1;
-               default:
-                       abort ();
+       if (v->type == STRING) {
+               arith_t i;
+               char *e;
+
+               /* Don't interpret the empty string as an integer.  */
+               /* Currently does not worry about overflow or int/long differences. */
+               i = STRTOL(v->u.s, &e, 10);
+               if ((v->u.s == e) || *e)
+                       return 0;
+               free(v->u.s);
+               v->u.i = i;
+               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 int
-nextarg (char *str)
+static int nextarg(const char *str)
 {
-       if (*args == NULL)
+       if (*G.args == NULL || strcmp(*G.args, str) != 0)
                return 0;
-       return strcmp (*args, str) == 0;
+       return (unsigned char)str[0] + (unsigned char)str[1];
 }
 
 /* The comparison operator handling functions.  */
 
-#define cmpf(name, rel)                                        \
-static int name (l, r) VALUE *l; VALUE *r;             \
-{                                                      \
-       if (l->type == string || r->type == string) {           \
-               tostring (l);                           \
-               tostring (r);                           \
-               return strcmp (l->u.s, r->u.s) rel 0;   \
-       }                                               \
-       else                                            \
-               return l->u.i rel r->u.i;               \
+static int cmp_common(VALUE *l, VALUE *r, int op)
+{
+       arith_t ll, rr;
+
+       ll = l->u.i;
+       rr = r->u.i;
+       if (l->type == STRING || r->type == STRING) {
+               tostring(l);
+               tostring(r);
+               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 ll < rr;
+       if (op == ('<' + '='))
+               return ll <= rr;
+       if (op == '=' || (op == '=' + '='))
+               return ll == rr;
+       if (op == '!' + '=')
+               return ll != rr;
+       if (op == '>')
+               return ll > rr;
+       /* >= */
+       return ll >= rr;
 }
- cmpf (less_than, <)
- cmpf (less_equal, <=)
- cmpf (equal, ==)
- cmpf (not_equal, !=)
- cmpf (greater_equal, >=)
- cmpf (greater_than, >)
-
-#undef cmpf
 
 /* The arithmetic operator handling functions.  */
 
-#define arithf(name, op)                       \
-static                                         \
-int name (l, r) VALUE *l; VALUE *r;            \
-{                                              \
-  if (!toarith (l) || !toarith (r))            \
-    error_msg_and_die ("non-numeric argument");        \
-  return l->u.i op r->u.i;                     \
-}
-
-#define arithdivf(name, op)                    \
-static int name (l, r) VALUE *l; VALUE *r;             \
-{                                              \
-  if (!toarith (l) || !toarith (r))            \
-    error_msg_and_die ( "non-numeric argument");       \
-  if (r->u.i == 0)                             \
-    error_msg_and_die ( "division by zero");           \
-  return l->u.i op r->u.i;                     \
+static arith_t arithmetic_common(VALUE *l, VALUE *r, int op)
+{
+       arith_t li, ri;
+
+       if (!toarith(l) || !toarith(r))
+               bb_simple_error_msg_and_die("non-numeric argument");
+       li = l->u.i;
+       ri = r->u.i;
+       if (op == '+')
+               return li + ri;
+       if (op == '-')
+               return li - ri;
+       if (op == '*')
+               return li * ri;
+       if (ri == 0)
+               bb_simple_error_msg_and_die("division by zero");
+       if (op == '/')
+               return li / ri;
+       return li % ri;
 }
 
- arithf (plus, +)
- arithf (minus, -)
- arithf (multiply, *)
- arithdivf (divide, /)
- arithdivf (mod, %)
-
-#undef arithf
-#undef arithdivf
-
 /* 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)
 {
+       enum { NMATCH = 2 };
        VALUE *v;
-       const char *errmsg;
-       struct re_pattern_buffer re_buffer;
-       struct re_registers re_regs;
-       int len;
+       regex_t re_buffer;
+       regmatch_t re_regs[NMATCH];
 
-       tostring (sv);
-       tostring (pv);
+       tostring(sv);
+       tostring(pv);
 
        if (pv->u.s[0] == '^') {
-               fprintf (stderr, "\
-warning: unportable BRE: `%s': using `^' as the first character\n\
-of a basic regular expression is not portable; it is being ignored",
-               pv->u.s);
+               bb_error_msg(
+"warning: '%s': using '^' as the first character\n"
+"of a basic regular expression is not portable; it is ignored", pv->u.s);
        }
 
-       len = strlen (pv->u.s);
-       memset (&re_buffer, 0, sizeof (re_buffer));
-       memset (&re_regs, 0, sizeof (re_regs));
-       re_buffer.allocated = 2 * len;
-       re_buffer.buffer = (unsigned char *) xmalloc (re_buffer.allocated);
-       re_buffer.translate = 0;
-       re_syntax_options = RE_SYNTAX_POSIX_BASIC;
-       errmsg = re_compile_pattern (pv->u.s, len, &re_buffer);
-       if (errmsg) {
-               error_msg_and_die("%s", errmsg);
-       }
+       memset(&re_buffer, 0, sizeof(re_buffer));
+       memset(re_regs, 0, sizeof(re_regs));
+       xregcomp(&re_buffer, pv->u.s, 0);
 
-       len = re_match (&re_buffer, sv->u.s, strlen (sv->u.s), 0, &re_regs);
-       if (len >= 0) {
+       /* 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
+       ) {
                /* Were \(...\) used? */
-               if (re_buffer.re_nsub > 0) { /* was (re_regs.start[1] >= 0) */
-                       sv->u.s[re_regs.end[1]] = '\0';
-                       v = str_value (sv->u.s + re_regs.start[1]);
+               if (re_buffer.re_nsub > 0 && re_regs[1].rm_so >= 0) {
+                       sv->u.s[re_regs[1].rm_eo] = '\0';
+                       v = str_value(sv->u.s + re_regs[1].rm_so);
+               } else {
+                       v = int_value(re_regs[0].rm_eo);
                }
-               else
-                       v = int_value (len);
-       }
-       else {
+       } else {
                /* Match failed -- return the right kind of null.  */
                if (re_buffer.re_nsub > 0)
-                       v = str_value ("");
+                       v = str_value("");
                else
-                       v = int_value (0);
+                       v = int_value(0);
        }
-       free (re_buffer.buffer);
+       regfree(&re_buffer);
        return v;
 }
 
 /* Handle bare operands and ( expr ) syntax.  */
 
-static VALUE *eval7 (void)
+static VALUE *eval7(void)
 {
        VALUE *v;
 
-       if (!*args)
-               error_msg_and_die ( "syntax error");
+       if (!*G.args)
+               bb_simple_error_msg_and_die("syntax error");
 
-       if (nextarg ("(")) {
-               args++;
-               v = eval ();
-               if (!nextarg (")"))
-                       error_msg_and_die ( "syntax error");
-                       args++;
-                       return v;
-               }
+       if (nextarg("(")) {
+               G.args++;
+               v = eval();
+               if (!nextarg(")"))
+                       bb_simple_error_msg_and_die("syntax error");
+               G.args++;
+               return v;
+       }
 
-       if (nextarg (")"))
-               error_msg_and_die ( "syntax error");
+       if (nextarg(")"))
+               bb_simple_error_msg_and_die("syntax error");
 
-       return str_value (*args++);
+       return str_value(*G.args++);
 }
 
 /* Handle match, substr, index, length, and quote keywords.  */
 
-static VALUE *eval6 (void)
+static VALUE *eval6(void)
 {
-       VALUE *l, *r, *v, *i1, *i2;
-
-       if (nextarg ("quote")) {
-               args++;
-               if (!*args)
-                       error_msg_and_die ( "syntax error");
-               return str_value (*args++);
-       }
-       else if (nextarg ("length")) {
-               args++;
-               r = eval6 ();
-               tostring (r);
-               v = int_value (strlen (r->u.s));
-               freev (r);
-               return v;
+       static const char keywords[] ALIGN1 =
+               "quote\0""length\0""match\0""index\0""substr\0";
+
+       VALUE *r, *i1, *i2;
+       VALUE *l = l; /* silence gcc */
+       VALUE *v = v; /* silence gcc */
+       int key = *G.args ? index_in_strings(keywords, *G.args) + 1 : 0;
+
+       if (key == 0) /* not a keyword */
+               return eval7();
+       G.args++; /* We have a valid token, so get the next argument.  */
+       if (key == 1) { /* quote */
+               if (!*G.args)
+                       bb_simple_error_msg_and_die("syntax error");
+               return str_value(*G.args++);
        }
-       else if (nextarg ("match")) {
-               args++;
-               l = eval6 ();
-               r = eval6 ();
-               v = docolon (l, r);
-               freev (l);
-               freev (r);
-               return v;
+       if (key == 2) { /* length */
+               r = eval6();
+               tostring(r);
+               v = int_value(strlen(r->u.s));
+               freev(r);
+       } else
+               l = eval6();
+
+       if (key == 3) { /* match */
+               r = eval6();
+               v = docolon(l, r);
+               freev(l);
+               freev(r);
        }
-       else if (nextarg ("index")) {
-               args++;
-               l = eval6 ();
-               r = eval6 ();
-               tostring (l);
-               tostring (r);
-               v = int_value (strcspn (l->u.s, r->u.s) + 1);
-               if (v->u.i == (int) strlen (l->u.s) + 1)
+       if (key == 4) { /* index */
+               r = eval6();
+               tostring(l);
+               tostring(r);
+               v = int_value(strcspn(l->u.s, r->u.s) + 1);
+               if (v->u.i == (arith_t) strlen(l->u.s) + 1)
                        v->u.i = 0;
-               freev (l);
-               freev (r);
-               return v;
+               freev(l);
+               freev(r);
        }
-       else if (nextarg ("substr")) {
-               args++;
-               l = eval6 ();
-               i1 = eval6 ();
-               i2 = eval6 ();
-               tostring (l);
-               if (!toarith (i1) || !toarith (i2)
-                       || i1->u.i > (int) strlen (l->u.s)
-                       || i1->u.i <= 0 || i2->u.i <= 0)
-               v = str_value ("");
+       if (key == 5) { /* substr */
+               i1 = eval6();
+               i2 = eval6();
+               tostring(l);
+               if (!toarith(i1) || !toarith(i2)
+                || i1->u.i > (arith_t) strlen(l->u.s)
+                || i1->u.i <= 0 || i2->u.i <= 0)
+                       v = str_value("");
                else {
-                       v = xmalloc (sizeof(VALUE));
-                       v->type = string;
-                       v->u.s = strncpy ((char *) xmalloc (i2->u.i + 1),
-                               l->u.s + i1->u.i - 1, i2->u.i);
-                               v->u.s[i2->u.i] = 0;
+                       v = xmalloc(sizeof(VALUE));
+                       v->type = STRING;
+                       v->u.s = xstrndup(l->u.s + i1->u.i - 1, i2->u.i);
                }
-               freev (l);
-               freev (i1);
-               freev (i2);
-               return v;
+               freev(l);
+               freev(i1);
+               freev(i2);
        }
-       else
-               return eval7 ();
+       return v;
 }
 
 /* Handle : operator (pattern matching).
    Calls docolon to do the real work.  */
 
-static VALUE *eval5 (void)
+static VALUE *eval5(void)
 {
        VALUE *l, *r, *v;
 
-       l = eval6 ();
-       while (nextarg (":")) {
-               args++;
-               r = eval6 ();
-               v = docolon (l, r);
-               freev (l);
-               freev (r);
+       l = eval6();
+       while (nextarg(":")) {
+               G.args++;
+               r = eval6();
+               v = docolon(l, r);
+               freev(l);
+               freev(r);
                l = v;
        }
        return l;
@@ -411,125 +421,139 @@ static VALUE *eval5 (void)
 
 /* Handle *, /, % operators.  */
 
-static VALUE *eval4 (void)
+static VALUE *eval4(void)
 {
        VALUE *l, *r;
-       int (*fxn) (), val;
+       int op;
+       arith_t val;
 
-       l = eval5 ();
+       l = eval5();
        while (1) {
-               if (nextarg ("*"))
-                       fxn = multiply;
-               else if (nextarg ("/"))
-                       fxn = divide;
-               else if (nextarg ("%"))
-                       fxn = mod;
-               else
-                       return l;
-               args++;
-               r = eval5 ();
-               val = (*fxn) (l, r);
-               freev (l);
-               freev (r);
-               l = int_value (val);
+               op = nextarg("*");
+               if (!op) { op = nextarg("/");
+                if (!op) { op = nextarg("%");
+                 if (!op) return l;
+               }}
+               G.args++;
+               r = eval5();
+               val = arithmetic_common(l, r, op);
+               freev(l);
+               freev(r);
+               l = int_value(val);
        }
 }
 
 /* Handle +, - operators.  */
 
-static VALUE *eval3 (void)
+static VALUE *eval3(void)
 {
        VALUE *l, *r;
-       int (*fxn) (), val;
+       int op;
+       arith_t val;
 
-       l = eval4 ();
+       l = eval4();
        while (1) {
-               if (nextarg ("+"))
-                       fxn = plus;
-               else if (nextarg ("-"))
-                       fxn = minus;
-               else
-                       return l;
-               args++;
-               r = eval4 ();
-               val = (*fxn) (l, r);
-               freev (l);
-               freev (r);
-               l = int_value (val);
+               op = nextarg("+");
+               if (!op) {
+                       op = nextarg("-");
+                       if (!op) return l;
+               }
+               G.args++;
+               r = eval4();
+               val = arithmetic_common(l, r, op);
+               freev(l);
+               freev(r);
+               l = int_value(val);
        }
 }
 
 /* Handle comparisons.  */
 
-static VALUE *eval2 (void)
+static VALUE *eval2(void)
 {
        VALUE *l, *r;
-       int (*fxn) (), val;
+       int op;
+       arith_t val;
 
-       l = eval3 ();
+       l = eval3();
        while (1) {
-               if (nextarg ("<"))
-                       fxn = less_than;
-               else if (nextarg ("<="))
-                       fxn = less_equal;
-               else if (nextarg ("=") || nextarg ("=="))
-                       fxn = equal;
-               else if (nextarg ("!="))
-                       fxn = not_equal;
-               else if (nextarg (">="))
-                       fxn = greater_equal;
-               else if (nextarg (">"))
-                       fxn = greater_than;
-               else
-                       return l;
-               args++;
-               r = eval3 ();
-               toarith (l);
-               toarith (r);
-               val = (*fxn) (l, r);
-               freev (l);
-               freev (r);
-               l = int_value (val);
+               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);
+               toarith(r);
+               val = cmp_common(l, r, op);
+               freev(l);
+               freev(r);
+               l = int_value(val);
        }
 }
 
 /* Handle &.  */
 
-static VALUE *eval1 (void)
+static VALUE *eval1(void)
 {
        VALUE *l, *r;
 
-       l = eval2 ();
-       while (nextarg ("&")) {
-               args++;
-               r = eval2 ();
-               if (null (l) || null (r)) {
-                       freev (l);
-                       freev (r);
-                       l = int_value (0);
-               }
-               else
-                       freev (r);
+       l = eval2();
+       while (nextarg("&")) {
+               G.args++;
+               r = eval2();
+               if (null(l) || null(r)) {
+                       freev(l);
+                       freev(r);
+                       l = int_value(0);
+               } else
+                       freev(r);
        }
        return l;
 }
 
 /* Handle |.  */
 
-static VALUE *eval (void)
+static VALUE *eval(void)
 {
        VALUE *l, *r;
 
-       l = eval1 ();
-       while (nextarg ("|")) {
-               args++;
-               r = eval1 ();
-               if (null (l)) {
-                       freev (l);
+       l = eval1();
+       while (nextarg("|")) {
+               G.args++;
+               r = eval1();
+               if (null(l)) {
+                       freev(l);
                        l = r;
-               }
-               else
-                       freev (r);
+               } else
+                       freev(r);
        }
        return l;
 }
+
+int expr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int expr_main(int argc UNUSED_PARAM, char **argv)
+{
+       VALUE *v;
+
+       INIT_G();
+
+       xfunc_error_retval = 2; /* coreutils compat */
+       G.args = argv + 1;
+       if (*G.args == NULL) {
+               bb_simple_error_msg_and_die("too few arguments");
+       }
+       v = eval();
+       if (*G.args)
+               bb_simple_error_msg_and_die("syntax error");
+       if (v->type == INTEGER)
+               printf("%" PF_REZ "d\n", PF_REZ_TYPE v->u.i);
+       else
+               puts(v->u.s);
+       fflush_stdout_and_exit(null(v));
+}