2000-09-05 23:07:48 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* Mini expr implementation for busybox
|
|
|
|
*
|
|
|
|
* based on GNU expr Mike Parker.
|
|
|
|
* Copyright (C) 86, 1991-1997, 1999 Free Software Foundation, Inc.
|
|
|
|
*
|
2004-03-15 13:59:22 +05:30
|
|
|
* Busybox modifications
|
2000-09-05 23:07:48 +05:30
|
|
|
* Copyright (c) 2000 Edward Betts <edward@debian.org>.
|
2006-01-25 19:26:03 +05:30
|
|
|
* Copyright (C) 2003-2005 Vladimir Oleynik <dzo@simtreas.ru>
|
|
|
|
* - reduced 464 bytes.
|
|
|
|
* - 64 math support
|
2000-09-05 23:07:48 +05:30
|
|
|
*
|
2010-08-16 23:44:46 +05:30
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
2000-09-05 23:07:48 +05:30
|
|
|
*/
|
|
|
|
/* This program evaluates expressions. Each token (operator, operand,
|
2004-04-14 23:21:38 +05:30
|
|
|
* parenthesis) of the expression must be a separate argument. The
|
2000-09-05 23:07:48 +05:30
|
|
|
* 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,
|
2016-11-23 19:16:56 +05:30
|
|
|
* provided they all associate ((x op x) op x).
|
|
|
|
*/
|
|
|
|
//config:config EXPR
|
2017-07-19 01:31:24 +05:30
|
|
|
//config: bool "expr (6.1 kb)"
|
2016-11-23 19:16:56 +05:30
|
|
|
//config: default y
|
|
|
|
//config: help
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: expr is used to calculate numbers and print the result
|
|
|
|
//config: to standard output.
|
2016-11-23 19:16:56 +05:30
|
|
|
//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
|
2017-07-21 13:20:55 +05:30
|
|
|
//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.
|
2016-11-23 19:16:56 +05:30
|
|
|
|
|
|
|
//applet:IF_EXPR(APPLET(expr, BB_DIR_USR_BIN, BB_SUID_DROP))
|
2000-09-05 23:07:48 +05:30
|
|
|
|
2016-11-23 19:16:56 +05:30
|
|
|
//kbuild:lib-$(CONFIG_EXPR) += expr.o
|
2001-03-10 05:29:51 +05:30
|
|
|
|
2011-03-31 18:13:25 +05:30
|
|
|
//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."
|
|
|
|
|
2007-05-27 00:30:18 +05:30
|
|
|
#include "libbb.h"
|
2016-04-21 19:56:30 +05:30
|
|
|
#include "common_bufsiz.h"
|
2006-08-03 21:11:12 +05:30
|
|
|
#include "xregex.h"
|
2000-09-05 23:07:48 +05:30
|
|
|
|
2006-01-25 19:26:03 +05:30
|
|
|
#if ENABLE_EXPR_MATH_SUPPORT_64
|
|
|
|
typedef int64_t arith_t;
|
2006-08-29 05:01:54 +05:30
|
|
|
|
2006-01-25 19:26:03 +05:30
|
|
|
#define PF_REZ "ll"
|
2006-01-31 01:18:23 +05:30
|
|
|
#define PF_REZ_TYPE (long long)
|
2006-01-25 19:26:03 +05:30
|
|
|
#define STRTOL(s, e, b) strtoll(s, e, b)
|
|
|
|
#else
|
|
|
|
typedef long arith_t;
|
2006-08-29 05:01:54 +05:30
|
|
|
|
2006-01-25 19:26:03 +05:30
|
|
|
#define PF_REZ "l"
|
2006-01-31 01:18:23 +05:30
|
|
|
#define PF_REZ_TYPE (long)
|
2006-01-25 19:26:03 +05:30
|
|
|
#define STRTOL(s, e, b) strtol(s, e, b)
|
|
|
|
#endif
|
|
|
|
|
2006-11-27 20:13:21 +05:30
|
|
|
/* TODO: use bb_strtol[l]? It's easier to check for errors... */
|
|
|
|
|
2008-04-03 01:54:09 +05:30
|
|
|
/* The kinds of value we can have. */
|
|
|
|
enum {
|
|
|
|
INTEGER,
|
|
|
|
STRING
|
|
|
|
};
|
|
|
|
|
2000-09-05 23:07:48 +05:30
|
|
|
/* A value is.... */
|
|
|
|
struct valinfo {
|
2008-04-03 01:54:09 +05:30
|
|
|
smallint type; /* Which kind. */
|
|
|
|
union { /* The value itself. */
|
2006-01-25 19:26:03 +05:30
|
|
|
arith_t i;
|
2000-09-05 23:07:48 +05:30
|
|
|
char *s;
|
|
|
|
} u;
|
|
|
|
};
|
|
|
|
typedef struct valinfo VALUE;
|
|
|
|
|
|
|
|
/* The arguments given to the program, minus the program name. */
|
2007-04-02 22:08:13 +05:30
|
|
|
struct globals {
|
|
|
|
char **args;
|
2010-02-04 19:30:15 +05:30
|
|
|
} FIX_ALIASING;
|
2016-04-21 19:56:30 +05:30
|
|
|
#define G (*(struct globals*)bb_common_bufsiz1)
|
2016-04-21 21:48:48 +05:30
|
|
|
#define INIT_G() do { setup_common_bufsiz(); } while (0)
|
2000-09-05 23:07:48 +05:30
|
|
|
|
2007-04-02 22:11:24 +05:30
|
|
|
/* forward declarations */
|
2006-08-29 05:01:54 +05:30
|
|
|
static VALUE *eval(void);
|
|
|
|
|
2000-09-05 23:07:48 +05:30
|
|
|
|
|
|
|
/* Return a VALUE for I. */
|
|
|
|
|
2006-08-29 05:01:54 +05:30
|
|
|
static VALUE *int_value(arith_t i)
|
2000-09-05 23:07:48 +05:30
|
|
|
{
|
|
|
|
VALUE *v;
|
|
|
|
|
2008-04-03 01:54:09 +05:30
|
|
|
v = xzalloc(sizeof(VALUE));
|
2016-09-18 00:28:22 +05:30
|
|
|
if (INTEGER) /* otherwise xzalloc did it already */
|
2008-04-03 01:54:09 +05:30
|
|
|
v->type = INTEGER;
|
2000-09-05 23:07:48 +05:30
|
|
|
v->u.i = i;
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return a VALUE for S. */
|
|
|
|
|
2007-01-30 04:21:00 +05:30
|
|
|
static VALUE *str_value(const char *s)
|
2000-09-05 23:07:48 +05:30
|
|
|
{
|
|
|
|
VALUE *v;
|
|
|
|
|
2008-04-03 01:54:09 +05:30
|
|
|
v = xzalloc(sizeof(VALUE));
|
2016-09-18 00:28:22 +05:30
|
|
|
if (STRING) /* otherwise xzalloc did it already */
|
2008-04-03 01:54:09 +05:30
|
|
|
v->type = STRING;
|
2006-08-03 21:11:12 +05:30
|
|
|
v->u.s = xstrdup(s);
|
2000-09-05 23:07:48 +05:30
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free VALUE V, including structure components. */
|
|
|
|
|
2008-04-03 01:54:09 +05:30
|
|
|
static void freev(VALUE *v)
|
2000-09-05 23:07:48 +05:30
|
|
|
{
|
2008-04-03 01:54:09 +05:30
|
|
|
if (v->type == STRING)
|
2006-08-29 05:01:54 +05:30
|
|
|
free(v->u.s);
|
|
|
|
free(v);
|
2000-09-05 23:07:48 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* Return nonzero if V is a null-string or zero-number. */
|
|
|
|
|
2008-04-03 01:54:09 +05:30
|
|
|
static int null(VALUE *v)
|
2000-09-05 23:07:48 +05:30
|
|
|
{
|
2008-04-03 01:54:09 +05:30
|
|
|
if (v->type == INTEGER)
|
2006-08-29 05:01:54 +05:30
|
|
|
return v->u.i == 0;
|
2008-04-03 01:54:09 +05:30
|
|
|
/* STRING: */
|
2007-03-08 19:07:43 +05:30
|
|
|
return v->u.s[0] == '\0' || LONE_CHAR(v->u.s, '0');
|
2000-09-05 23:07:48 +05:30
|
|
|
}
|
|
|
|
|
2008-04-03 01:54:09 +05:30
|
|
|
/* Coerce V to a STRING value (can't fail). */
|
2000-09-05 23:07:48 +05:30
|
|
|
|
2008-04-03 01:54:09 +05:30
|
|
|
static void tostring(VALUE *v)
|
2000-09-05 23:07:48 +05:30
|
|
|
{
|
2008-04-03 01:54:09 +05:30
|
|
|
if (v->type == INTEGER) {
|
2006-08-03 21:11:12 +05:30
|
|
|
v->u.s = xasprintf("%" PF_REZ "d", PF_REZ_TYPE v->u.i);
|
2008-04-03 01:54:09 +05:30
|
|
|
v->type = STRING;
|
2000-09-05 23:07:48 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-03 01:54:09 +05:30
|
|
|
/* Coerce V to an INTEGER value. Return 1 on success, 0 on failure. */
|
2000-09-05 23:07:48 +05:30
|
|
|
|
2008-04-03 01:54:09 +05:30
|
|
|
static bool toarith(VALUE *v)
|
2000-09-05 23:07:48 +05:30
|
|
|
{
|
2008-04-03 01:54:09 +05:30
|
|
|
if (v->type == STRING) {
|
2006-01-25 19:26:03 +05:30
|
|
|
arith_t i;
|
2004-01-26 01:17:10 +05:30
|
|
|
char *e;
|
|
|
|
|
|
|
|
/* Don't interpret the empty string as an integer. */
|
|
|
|
/* Currently does not worry about overflow or int/long differences. */
|
2006-01-25 19:26:03 +05:30
|
|
|
i = STRTOL(v->u.s, &e, 10);
|
2004-01-26 01:17:10 +05:30
|
|
|
if ((v->u.s == e) || *e)
|
|
|
|
return 0;
|
2006-08-29 05:01:54 +05:30
|
|
|
free(v->u.s);
|
2004-01-26 01:17:10 +05:30
|
|
|
v->u.i = i;
|
2008-04-03 01:54:09 +05:30
|
|
|
v->type = INTEGER;
|
2000-09-05 23:07:48 +05:30
|
|
|
}
|
2003-08-29 12:59:30 +05:30
|
|
|
return 1;
|
2000-09-05 23:07:48 +05:30
|
|
|
}
|
|
|
|
|
2008-04-03 01:54:09 +05:30
|
|
|
/* Return str[0]+str[1] if the next token matches STR exactly.
|
2000-09-05 23:07:48 +05:30
|
|
|
STR must not be NULL. */
|
|
|
|
|
2008-04-03 01:54:09 +05:30
|
|
|
static int nextarg(const char *str)
|
2000-09-05 23:07:48 +05:30
|
|
|
{
|
2008-04-03 01:54:09 +05:30
|
|
|
if (*G.args == NULL || strcmp(*G.args, str) != 0)
|
2000-09-05 23:07:48 +05:30
|
|
|
return 0;
|
2008-04-03 01:54:09 +05:30
|
|
|
return (unsigned char)str[0] + (unsigned char)str[1];
|
2000-09-05 23:07:48 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* The comparison operator handling functions. */
|
|
|
|
|
2008-04-03 01:54:09 +05:30
|
|
|
static int cmp_common(VALUE *l, VALUE *r, int op)
|
2003-08-29 12:59:30 +05:30
|
|
|
{
|
2008-04-03 01:54:09 +05:30
|
|
|
arith_t ll, rr;
|
2000-09-05 23:07:48 +05:30
|
|
|
|
2008-04-03 01:54:09 +05:30
|
|
|
ll = l->u.i;
|
|
|
|
rr = r->u.i;
|
|
|
|
if (l->type == STRING || r->type == STRING) {
|
2006-08-29 05:01:54 +05:30
|
|
|
tostring(l);
|
|
|
|
tostring(r);
|
2008-04-03 01:54:09 +05:30
|
|
|
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: */
|
2006-08-29 05:01:54 +05:30
|
|
|
if (op == '<')
|
2008-04-03 01:54:09 +05:30
|
|
|
return ll < rr;
|
|
|
|
if (op == ('<' + '='))
|
|
|
|
return ll <= rr;
|
|
|
|
if (op == '=' || (op == '=' + '='))
|
|
|
|
return ll == rr;
|
|
|
|
if (op == '!' + '=')
|
|
|
|
return ll != rr;
|
2007-03-08 19:07:43 +05:30
|
|
|
if (op == '>')
|
2008-04-03 01:54:09 +05:30
|
|
|
return ll > rr;
|
2007-03-08 19:07:43 +05:30
|
|
|
/* >= */
|
2008-04-03 01:54:09 +05:30
|
|
|
return ll >= rr;
|
2003-08-29 12:59:30 +05:30
|
|
|
}
|
2000-09-05 23:07:48 +05:30
|
|
|
|
|
|
|
/* The arithmetic operator handling functions. */
|
|
|
|
|
2008-04-03 01:54:09 +05:30
|
|
|
static arith_t arithmetic_common(VALUE *l, VALUE *r, int op)
|
2003-08-29 12:59:30 +05:30
|
|
|
{
|
2006-08-29 05:01:54 +05:30
|
|
|
arith_t li, ri;
|
|
|
|
|
|
|
|
if (!toarith(l) || !toarith(r))
|
|
|
|
bb_error_msg_and_die("non-numeric argument");
|
|
|
|
li = l->u.i;
|
|
|
|
ri = r->u.i;
|
|
|
|
if (op == '+')
|
2003-08-29 12:59:30 +05:30
|
|
|
return li + ri;
|
2008-04-03 01:54:09 +05:30
|
|
|
if (op == '-')
|
2003-08-29 12:59:30 +05:30
|
|
|
return li - ri;
|
2008-04-03 01:54:09 +05:30
|
|
|
if (op == '*')
|
2003-08-29 12:59:30 +05:30
|
|
|
return li * ri;
|
2008-04-03 01:54:09 +05:30
|
|
|
if (ri == 0)
|
|
|
|
bb_error_msg_and_die("division by zero");
|
|
|
|
if (op == '/')
|
2003-08-29 12:59:30 +05:30
|
|
|
return li / ri;
|
2008-04-03 01:54:09 +05:30
|
|
|
return li % ri;
|
2000-09-05 23:07:48 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* Do the : operator.
|
|
|
|
SV is the VALUE for the lhs (the string),
|
|
|
|
PV is the VALUE for the rhs (the pattern). */
|
|
|
|
|
2008-04-03 01:54:09 +05:30
|
|
|
static VALUE *docolon(VALUE *sv, VALUE *pv)
|
2000-09-05 23:07:48 +05:30
|
|
|
{
|
2009-09-06 16:17:55 +05:30
|
|
|
enum { NMATCH = 2 };
|
2000-09-05 23:07:48 +05:30
|
|
|
VALUE *v;
|
2005-05-10 03:12:42 +05:30
|
|
|
regex_t re_buffer;
|
|
|
|
regmatch_t re_regs[NMATCH];
|
2000-09-05 23:07:48 +05:30
|
|
|
|
2006-08-29 05:01:54 +05:30
|
|
|
tostring(sv);
|
|
|
|
tostring(pv);
|
2000-09-05 23:07:48 +05:30
|
|
|
|
|
|
|
if (pv->u.s[0] == '^') {
|
2009-03-03 19:57:36 +05:30
|
|
|
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);
|
2000-09-05 23:07:48 +05:30
|
|
|
}
|
|
|
|
|
2006-08-29 05:01:54 +05:30
|
|
|
memset(&re_buffer, 0, sizeof(re_buffer));
|
2009-03-03 19:57:36 +05:30
|
|
|
memset(re_regs, 0, sizeof(re_regs));
|
2007-04-02 22:24:41 +05:30
|
|
|
xregcomp(&re_buffer, pv->u.s, 0);
|
2005-05-10 03:12:42 +05:30
|
|
|
|
|
|
|
/* expr uses an anchored pattern match, so check that there was a
|
|
|
|
* match and that the match starts at offset 0. */
|
2008-04-03 01:54:09 +05:30
|
|
|
if (regexec(&re_buffer, sv->u.s, NMATCH, re_regs, 0) != REG_NOMATCH
|
|
|
|
&& re_regs[0].rm_so == 0
|
|
|
|
) {
|
2000-09-05 23:07:48 +05:30
|
|
|
/* Were \(...\) used? */
|
2009-03-03 19:57:36 +05:30
|
|
|
if (re_buffer.re_nsub > 0 && re_regs[1].rm_so >= 0) {
|
2005-05-10 03:12:42 +05:30
|
|
|
sv->u.s[re_regs[1].rm_eo] = '\0';
|
2006-08-29 05:01:54 +05:30
|
|
|
v = str_value(sv->u.s + re_regs[1].rm_so);
|
2008-04-03 01:54:09 +05:30
|
|
|
} else {
|
2006-08-29 05:01:54 +05:30
|
|
|
v = int_value(re_regs[0].rm_eo);
|
2008-04-03 01:54:09 +05:30
|
|
|
}
|
2006-08-29 05:01:54 +05:30
|
|
|
} else {
|
2000-09-05 23:07:48 +05:30
|
|
|
/* Match failed -- return the right kind of null. */
|
|
|
|
if (re_buffer.re_nsub > 0)
|
2006-08-29 05:01:54 +05:30
|
|
|
v = str_value("");
|
2000-09-05 23:07:48 +05:30
|
|
|
else
|
2006-08-29 05:01:54 +05:30
|
|
|
v = int_value(0);
|
2000-09-05 23:07:48 +05:30
|
|
|
}
|
2009-03-03 19:57:36 +05:30
|
|
|
regfree(&re_buffer);
|
2000-09-05 23:07:48 +05:30
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle bare operands and ( expr ) syntax. */
|
|
|
|
|
2006-08-29 05:01:54 +05:30
|
|
|
static VALUE *eval7(void)
|
2000-09-05 23:07:48 +05:30
|
|
|
{
|
|
|
|
VALUE *v;
|
|
|
|
|
2007-04-02 22:08:13 +05:30
|
|
|
if (!*G.args)
|
2006-08-29 05:01:54 +05:30
|
|
|
bb_error_msg_and_die("syntax error");
|
2000-09-05 23:07:48 +05:30
|
|
|
|
2006-08-29 05:01:54 +05:30
|
|
|
if (nextarg("(")) {
|
2007-04-02 22:08:13 +05:30
|
|
|
G.args++;
|
2006-08-29 05:01:54 +05:30
|
|
|
v = eval();
|
|
|
|
if (!nextarg(")"))
|
|
|
|
bb_error_msg_and_die("syntax error");
|
2007-04-02 22:08:13 +05:30
|
|
|
G.args++;
|
2006-08-29 05:01:54 +05:30
|
|
|
return v;
|
|
|
|
}
|
2000-09-05 23:07:48 +05:30
|
|
|
|
2006-08-29 05:01:54 +05:30
|
|
|
if (nextarg(")"))
|
|
|
|
bb_error_msg_and_die("syntax error");
|
2000-09-05 23:07:48 +05:30
|
|
|
|
2007-04-02 22:08:13 +05:30
|
|
|
return str_value(*G.args++);
|
2000-09-05 23:07:48 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle match, substr, index, length, and quote keywords. */
|
|
|
|
|
2006-08-29 05:01:54 +05:30
|
|
|
static VALUE *eval6(void)
|
2000-09-05 23:07:48 +05:30
|
|
|
{
|
2007-08-13 02:28:27 +05:30
|
|
|
static const char keywords[] ALIGN1 =
|
2007-07-24 21:24:42 +05:30
|
|
|
"quote\0""length\0""match\0""index\0""substr\0";
|
2000-09-05 23:07:48 +05:30
|
|
|
|
2007-07-02 00:03:35 +05:30
|
|
|
VALUE *r, *i1, *i2;
|
|
|
|
VALUE *l = l; /* silence gcc */
|
|
|
|
VALUE *v = v; /* silence gcc */
|
2007-07-24 21:24:42 +05:30
|
|
|
int key = *G.args ? index_in_strings(keywords, *G.args) + 1 : 0;
|
2007-07-02 00:03:35 +05:30
|
|
|
|
2007-04-02 22:08:13 +05:30
|
|
|
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)
|
2006-08-29 05:01:54 +05:30
|
|
|
bb_error_msg_and_die("syntax error");
|
2007-04-02 22:08:13 +05:30
|
|
|
return str_value(*G.args++);
|
|
|
|
}
|
|
|
|
if (key == 2) { /* length */
|
2006-08-29 05:01:54 +05:30
|
|
|
r = eval6();
|
|
|
|
tostring(r);
|
|
|
|
v = int_value(strlen(r->u.s));
|
|
|
|
freev(r);
|
2007-04-02 22:08:13 +05:30
|
|
|
} else
|
2006-08-29 05:01:54 +05:30
|
|
|
l = eval6();
|
2007-04-02 22:08:13 +05:30
|
|
|
|
|
|
|
if (key == 3) { /* match */
|
2006-08-29 05:01:54 +05:30
|
|
|
r = eval6();
|
|
|
|
v = docolon(l, r);
|
|
|
|
freev(l);
|
|
|
|
freev(r);
|
2007-04-02 22:08:13 +05:30
|
|
|
}
|
|
|
|
if (key == 4) { /* index */
|
2006-08-29 05:01:54 +05:30
|
|
|
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)
|
2000-09-05 23:07:48 +05:30
|
|
|
v->u.i = 0;
|
2006-08-29 05:01:54 +05:30
|
|
|
freev(l);
|
|
|
|
freev(r);
|
2007-04-02 22:08:13 +05:30
|
|
|
}
|
|
|
|
if (key == 5) { /* substr */
|
2006-08-29 05:01:54 +05:30
|
|
|
i1 = eval6();
|
|
|
|
i2 = eval6();
|
|
|
|
tostring(l);
|
|
|
|
if (!toarith(i1) || !toarith(i2)
|
2007-07-02 00:03:35 +05:30
|
|
|
|| i1->u.i > (arith_t) strlen(l->u.s)
|
|
|
|
|| i1->u.i <= 0 || i2->u.i <= 0)
|
2006-08-29 05:01:54 +05:30
|
|
|
v = str_value("");
|
2000-09-05 23:07:48 +05:30
|
|
|
else {
|
2006-08-29 05:01:54 +05:30
|
|
|
v = xmalloc(sizeof(VALUE));
|
2008-04-03 01:54:09 +05:30
|
|
|
v->type = STRING;
|
2006-08-03 21:11:12 +05:30
|
|
|
v->u.s = xstrndup(l->u.s + i1->u.i - 1, i2->u.i);
|
2000-09-05 23:07:48 +05:30
|
|
|
}
|
2006-08-29 05:01:54 +05:30
|
|
|
freev(l);
|
|
|
|
freev(i1);
|
|
|
|
freev(i2);
|
2007-04-02 22:08:13 +05:30
|
|
|
}
|
|
|
|
return v;
|
2000-09-05 23:07:48 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle : operator (pattern matching).
|
|
|
|
Calls docolon to do the real work. */
|
|
|
|
|
2006-08-29 05:01:54 +05:30
|
|
|
static VALUE *eval5(void)
|
2000-09-05 23:07:48 +05:30
|
|
|
{
|
|
|
|
VALUE *l, *r, *v;
|
|
|
|
|
2006-08-29 05:01:54 +05:30
|
|
|
l = eval6();
|
|
|
|
while (nextarg(":")) {
|
2007-04-02 22:08:13 +05:30
|
|
|
G.args++;
|
2006-08-29 05:01:54 +05:30
|
|
|
r = eval6();
|
|
|
|
v = docolon(l, r);
|
|
|
|
freev(l);
|
|
|
|
freev(r);
|
2000-09-05 23:07:48 +05:30
|
|
|
l = v;
|
|
|
|
}
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle *, /, % operators. */
|
|
|
|
|
2006-08-29 05:01:54 +05:30
|
|
|
static VALUE *eval4(void)
|
2000-09-05 23:07:48 +05:30
|
|
|
{
|
|
|
|
VALUE *l, *r;
|
2006-01-25 19:26:03 +05:30
|
|
|
int op;
|
|
|
|
arith_t val;
|
2000-09-05 23:07:48 +05:30
|
|
|
|
2006-08-29 05:01:54 +05:30
|
|
|
l = eval5();
|
2000-09-05 23:07:48 +05:30
|
|
|
while (1) {
|
2008-04-03 01:54:09 +05:30
|
|
|
op = nextarg("*");
|
|
|
|
if (!op) { op = nextarg("/");
|
|
|
|
if (!op) { op = nextarg("%");
|
|
|
|
if (!op) return l;
|
|
|
|
}}
|
2007-04-02 22:08:13 +05:30
|
|
|
G.args++;
|
2006-08-29 05:01:54 +05:30
|
|
|
r = eval5();
|
|
|
|
val = arithmetic_common(l, r, op);
|
|
|
|
freev(l);
|
|
|
|
freev(r);
|
|
|
|
l = int_value(val);
|
2000-09-05 23:07:48 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle +, - operators. */
|
|
|
|
|
2006-08-29 05:01:54 +05:30
|
|
|
static VALUE *eval3(void)
|
2000-09-05 23:07:48 +05:30
|
|
|
{
|
|
|
|
VALUE *l, *r;
|
2006-01-25 19:26:03 +05:30
|
|
|
int op;
|
|
|
|
arith_t val;
|
2000-09-05 23:07:48 +05:30
|
|
|
|
2006-08-29 05:01:54 +05:30
|
|
|
l = eval4();
|
2000-09-05 23:07:48 +05:30
|
|
|
while (1) {
|
2008-04-03 01:54:09 +05:30
|
|
|
op = nextarg("+");
|
|
|
|
if (!op) {
|
|
|
|
op = nextarg("-");
|
|
|
|
if (!op) return l;
|
|
|
|
}
|
2007-04-02 22:08:13 +05:30
|
|
|
G.args++;
|
2006-08-29 05:01:54 +05:30
|
|
|
r = eval4();
|
|
|
|
val = arithmetic_common(l, r, op);
|
|
|
|
freev(l);
|
|
|
|
freev(r);
|
|
|
|
l = int_value(val);
|
2000-09-05 23:07:48 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle comparisons. */
|
|
|
|
|
2006-08-29 05:01:54 +05:30
|
|
|
static VALUE *eval2(void)
|
2000-09-05 23:07:48 +05:30
|
|
|
{
|
|
|
|
VALUE *l, *r;
|
2006-01-25 19:26:03 +05:30
|
|
|
int op;
|
|
|
|
arith_t val;
|
2000-09-05 23:07:48 +05:30
|
|
|
|
2006-08-29 05:01:54 +05:30
|
|
|
l = eval3();
|
2000-09-05 23:07:48 +05:30
|
|
|
while (1) {
|
2008-04-03 01:54:09 +05:30
|
|
|
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;
|
|
|
|
}}}}}}
|
2007-04-02 22:08:13 +05:30
|
|
|
G.args++;
|
2006-08-29 05:01:54 +05:30
|
|
|
r = eval3();
|
|
|
|
toarith(l);
|
|
|
|
toarith(r);
|
|
|
|
val = cmp_common(l, r, op);
|
|
|
|
freev(l);
|
|
|
|
freev(r);
|
|
|
|
l = int_value(val);
|
2000-09-05 23:07:48 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle &. */
|
|
|
|
|
2006-08-29 05:01:54 +05:30
|
|
|
static VALUE *eval1(void)
|
2000-09-05 23:07:48 +05:30
|
|
|
{
|
|
|
|
VALUE *l, *r;
|
|
|
|
|
2006-08-29 05:01:54 +05:30
|
|
|
l = eval2();
|
|
|
|
while (nextarg("&")) {
|
2007-04-02 22:08:13 +05:30
|
|
|
G.args++;
|
2006-08-29 05:01:54 +05:30
|
|
|
r = eval2();
|
|
|
|
if (null(l) || null(r)) {
|
|
|
|
freev(l);
|
|
|
|
freev(r);
|
|
|
|
l = int_value(0);
|
|
|
|
} else
|
|
|
|
freev(r);
|
2000-09-05 23:07:48 +05:30
|
|
|
}
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle |. */
|
|
|
|
|
2006-08-29 05:01:54 +05:30
|
|
|
static VALUE *eval(void)
|
2000-09-05 23:07:48 +05:30
|
|
|
{
|
|
|
|
VALUE *l, *r;
|
|
|
|
|
2006-08-29 05:01:54 +05:30
|
|
|
l = eval1();
|
|
|
|
while (nextarg("|")) {
|
2007-04-02 22:08:13 +05:30
|
|
|
G.args++;
|
2006-08-29 05:01:54 +05:30
|
|
|
r = eval1();
|
|
|
|
if (null(l)) {
|
|
|
|
freev(l);
|
2000-09-05 23:07:48 +05:30
|
|
|
l = r;
|
2006-08-29 05:01:54 +05:30
|
|
|
} else
|
|
|
|
freev(r);
|
2000-09-05 23:07:48 +05:30
|
|
|
}
|
|
|
|
return l;
|
|
|
|
}
|
2007-04-02 22:11:24 +05:30
|
|
|
|
2007-10-11 15:35:36 +05:30
|
|
|
int expr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2008-11-12 04:06:58 +05:30
|
|
|
int expr_main(int argc UNUSED_PARAM, char **argv)
|
2007-04-02 22:11:24 +05:30
|
|
|
{
|
|
|
|
VALUE *v;
|
|
|
|
|
2011-09-21 05:29:15 +05:30
|
|
|
INIT_G();
|
|
|
|
|
2008-11-12 04:06:58 +05:30
|
|
|
xfunc_error_retval = 2; /* coreutils compat */
|
|
|
|
G.args = argv + 1;
|
|
|
|
if (*G.args == NULL) {
|
2007-04-02 22:11:24 +05:30
|
|
|
bb_error_msg_and_die("too few arguments");
|
|
|
|
}
|
|
|
|
v = eval();
|
|
|
|
if (*G.args)
|
|
|
|
bb_error_msg_and_die("syntax error");
|
2008-04-03 01:54:09 +05:30
|
|
|
if (v->type == INTEGER)
|
2007-04-02 22:11:24 +05:30
|
|
|
printf("%" PF_REZ "d\n", PF_REZ_TYPE v->u.i);
|
|
|
|
else
|
|
|
|
puts(v->u.s);
|
|
|
|
fflush_stdout_and_exit(null(v));
|
|
|
|
}
|