blob: 760b081f98be7548474dd157efbdd80fabeadbeb [file] [log] [blame]
Eric Andersen1b355eb2000-09-05 17:37:48 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Mini expr implementation for busybox
4 *
5 * based on GNU expr Mike Parker.
6 * Copyright (C) 86, 1991-1997, 1999 Free Software Foundation, Inc.
7 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00008 * Busybox modifications
Eric Andersen1b355eb2000-09-05 17:37:48 +00009 * Copyright (c) 2000 Edward Betts <edward@debian.org>.
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +000010 * Copyright (C) 2003-2005 Vladimir Oleynik <dzo@simtreas.ru>
11 * - reduced 464 bytes.
12 * - 64 math support
Eric Andersen1b355eb2000-09-05 17:37:48 +000013 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020014 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Eric Andersen1b355eb2000-09-05 17:37:48 +000015 */
Eric Andersen1b355eb2000-09-05 17:37:48 +000016/* This program evaluates expressions. Each token (operator, operand,
Eric Andersenaff114c2004-04-14 17:51:38 +000017 * parenthesis) of the expression must be a separate argument. The
Eric Andersen1b355eb2000-09-05 17:37:48 +000018 * parser used is a reasonably general one, though any incarnation of
19 * it is language-specific. It is especially nice for expressions.
20 *
21 * No parse tree is needed; a new node is evaluated immediately.
22 * One function can handle multiple operators all of equal precedence,
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010023 * provided they all associate ((x op x) op x).
24 */
25//config:config EXPR
Denys Vlasenkob097a842018-12-28 03:20:17 +010026//config: bool "expr (6.6 kb)"
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010027//config: default y
28//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020029//config: expr is used to calculate numbers and print the result
30//config: to standard output.
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010031//config:
32//config:config EXPR_MATH_SUPPORT_64
33//config: bool "Extend Posix numbers support to 64 bit"
34//config: default y
35//config: depends on EXPR
36//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020037//config: Enable 64-bit math support in the expr applet. This will make
38//config: the applet slightly larger, but will allow computation with very
39//config: large numbers.
Eric Andersen1b355eb2000-09-05 17:37:48 +000040
Denys Vlasenkoaf5d0082017-08-07 23:23:18 +020041//applet:IF_EXPR(APPLET_NOEXEC(expr, expr, BB_DIR_USR_BIN, BB_SUID_DROP, expr))
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010042
43//kbuild:lib-$(CONFIG_EXPR) += expr.o
Mark Whitley827e45c2001-03-09 23:59:51 +000044
Pere Orga34425382011-03-31 14:43:25 +020045//usage:#define expr_trivial_usage
46//usage: "EXPRESSION"
47//usage:#define expr_full_usage "\n\n"
Denys Vlasenko6b6826f2021-06-13 01:08:48 +020048//usage: "Print the value of EXPRESSION\n"
Pere Orga34425382011-03-31 14:43:25 +020049//usage: "\n"
50//usage: "EXPRESSION may be:\n"
51//usage: " ARG1 | ARG2 ARG1 if it is neither null nor 0, otherwise ARG2\n"
52//usage: " ARG1 & ARG2 ARG1 if neither argument is null or 0, otherwise 0\n"
53//usage: " ARG1 < ARG2 1 if ARG1 is less than ARG2, else 0. Similarly:\n"
54//usage: " ARG1 <= ARG2\n"
55//usage: " ARG1 = ARG2\n"
56//usage: " ARG1 != ARG2\n"
57//usage: " ARG1 >= ARG2\n"
58//usage: " ARG1 > ARG2\n"
59//usage: " ARG1 + ARG2 Sum of ARG1 and ARG2. Similarly:\n"
60//usage: " ARG1 - ARG2\n"
61//usage: " ARG1 * ARG2\n"
62//usage: " ARG1 / ARG2\n"
63//usage: " ARG1 % ARG2\n"
64//usage: " STRING : REGEXP Anchored pattern match of REGEXP in STRING\n"
65//usage: " match STRING REGEXP Same as STRING : REGEXP\n"
Denys Vlasenko6b6826f2021-06-13 01:08:48 +020066//usage: " substr STRING POS LEN Substring of STRING, POS counts from 1\n"
Pere Orga34425382011-03-31 14:43:25 +020067//usage: " index STRING CHARS Index in STRING where any CHARS is found, or 0\n"
68//usage: " length STRING Length of STRING\n"
69//usage: " quote TOKEN Interpret TOKEN as a string, even if\n"
70//usage: " it is a keyword like 'match' or an\n"
71//usage: " operator like '/'\n"
72//usage: " (EXPRESSION) Value of EXPRESSION\n"
73//usage: "\n"
74//usage: "Beware that many operators need to be escaped or quoted for shells.\n"
75//usage: "Comparisons are arithmetic if both ARGs are numbers, else\n"
76//usage: "lexicographical. Pattern matches return the string matched between\n"
77//usage: "\\( and \\) or null; if \\( and \\) are not used, they return the number\n"
78//usage: "of characters matched or 0."
79
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000080#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020081#include "common_bufsiz.h"
Rob Landleyd921b2e2006-08-03 15:41:12 +000082#include "xregex.h"
Eric Andersen1b355eb2000-09-05 17:37:48 +000083
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +000084#if ENABLE_EXPR_MATH_SUPPORT_64
85typedef int64_t arith_t;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000086
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +000087#define PF_REZ "ll"
Eric Andersen5e678872006-01-30 19:48:23 +000088#define PF_REZ_TYPE (long long)
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +000089#define STRTOL(s, e, b) strtoll(s, e, b)
90#else
91typedef long arith_t;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000092
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +000093#define PF_REZ "l"
Eric Andersen5e678872006-01-30 19:48:23 +000094#define PF_REZ_TYPE (long)
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +000095#define STRTOL(s, e, b) strtol(s, e, b)
96#endif
97
Denis Vlasenkod686a042006-11-27 14:43:21 +000098/* TODO: use bb_strtol[l]? It's easier to check for errors... */
99
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000100/* The kinds of value we can have. */
101enum {
102 INTEGER,
103 STRING
104};
105
Eric Andersen1b355eb2000-09-05 17:37:48 +0000106/* A value is.... */
107struct valinfo {
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000108 smallint type; /* Which kind. */
109 union { /* The value itself. */
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +0000110 arith_t i;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000111 char *s;
112 } u;
113};
114typedef struct valinfo VALUE;
115
116/* The arguments given to the program, minus the program name. */
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000117struct globals {
118 char **args;
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +0100119} FIX_ALIASING;
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200120#define G (*(struct globals*)bb_common_bufsiz1)
Denys Vlasenkoaf5d0082017-08-07 23:23:18 +0200121#define INIT_G() do { \
122 setup_common_bufsiz(); \
123 /* NB: noexec applet - globals not zeroed */ \
124} while (0)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000125
Bernhard Reutner-Fischer3c5929a2007-04-02 16:41:24 +0000126/* forward declarations */
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000127static VALUE *eval(void);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000128
Eric Andersen1b355eb2000-09-05 17:37:48 +0000129
130/* Return a VALUE for I. */
131
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000132static VALUE *int_value(arith_t i)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000133{
134 VALUE *v;
135
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000136 v = xzalloc(sizeof(VALUE));
Denys Vlasenko73737592016-09-17 20:58:22 +0200137 if (INTEGER) /* otherwise xzalloc did it already */
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000138 v->type = INTEGER;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000139 v->u.i = i;
140 return v;
141}
142
143/* Return a VALUE for S. */
144
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +0000145static VALUE *str_value(const char *s)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000146{
147 VALUE *v;
148
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000149 v = xzalloc(sizeof(VALUE));
Denys Vlasenko73737592016-09-17 20:58:22 +0200150 if (STRING) /* otherwise xzalloc did it already */
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000151 v->type = STRING;
Rob Landleyd921b2e2006-08-03 15:41:12 +0000152 v->u.s = xstrdup(s);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000153 return v;
154}
155
156/* Free VALUE V, including structure components. */
157
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000158static void freev(VALUE *v)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000159{
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000160 if (v->type == STRING)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000161 free(v->u.s);
162 free(v);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000163}
164
165/* Return nonzero if V is a null-string or zero-number. */
166
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000167static int null(VALUE *v)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000168{
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000169 if (v->type == INTEGER)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000170 return v->u.i == 0;
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000171 /* STRING: */
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000172 return v->u.s[0] == '\0' || LONE_CHAR(v->u.s, '0');
Eric Andersen1b355eb2000-09-05 17:37:48 +0000173}
174
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000175/* Coerce V to a STRING value (can't fail). */
Eric Andersen1b355eb2000-09-05 17:37:48 +0000176
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000177static void tostring(VALUE *v)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000178{
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000179 if (v->type == INTEGER) {
Rob Landleyd921b2e2006-08-03 15:41:12 +0000180 v->u.s = xasprintf("%" PF_REZ "d", PF_REZ_TYPE v->u.i);
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000181 v->type = STRING;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000182 }
183}
184
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000185/* Coerce V to an INTEGER value. Return 1 on success, 0 on failure. */
Eric Andersen1b355eb2000-09-05 17:37:48 +0000186
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000187static bool toarith(VALUE *v)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000188{
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000189 if (v->type == STRING) {
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +0000190 arith_t i;
Manuel Novoa III 70183852004-01-25 19:47:10 +0000191 char *e;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000192
Manuel Novoa III 70183852004-01-25 19:47:10 +0000193 /* Don't interpret the empty string as an integer. */
194 /* Currently does not worry about overflow or int/long differences. */
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +0000195 i = STRTOL(v->u.s, &e, 10);
Manuel Novoa III 70183852004-01-25 19:47:10 +0000196 if ((v->u.s == e) || *e)
197 return 0;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000198 free(v->u.s);
Manuel Novoa III 70183852004-01-25 19:47:10 +0000199 v->u.i = i;
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000200 v->type = INTEGER;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000201 }
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000202 return 1;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000203}
204
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000205/* Return str[0]+str[1] if the next token matches STR exactly.
Eric Andersen1b355eb2000-09-05 17:37:48 +0000206 STR must not be NULL. */
207
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000208static int nextarg(const char *str)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000209{
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000210 if (*G.args == NULL || strcmp(*G.args, str) != 0)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000211 return 0;
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000212 return (unsigned char)str[0] + (unsigned char)str[1];
Eric Andersen1b355eb2000-09-05 17:37:48 +0000213}
214
215/* The comparison operator handling functions. */
216
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000217static int cmp_common(VALUE *l, VALUE *r, int op)
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000218{
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000219 arith_t ll, rr;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000220
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000221 ll = l->u.i;
222 rr = r->u.i;
223 if (l->type == STRING || r->type == STRING) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000224 tostring(l);
225 tostring(r);
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000226 ll = strcmp(l->u.s, r->u.s);
227 rr = 0;
228 }
229 /* calculating ll - rr and checking the result is prone to overflows.
230 * We'll do it differently: */
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000231 if (op == '<')
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000232 return ll < rr;
233 if (op == ('<' + '='))
234 return ll <= rr;
235 if (op == '=' || (op == '=' + '='))
236 return ll == rr;
237 if (op == '!' + '=')
238 return ll != rr;
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000239 if (op == '>')
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000240 return ll > rr;
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000241 /* >= */
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000242 return ll >= rr;
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000243}
Eric Andersen1b355eb2000-09-05 17:37:48 +0000244
245/* The arithmetic operator handling functions. */
246
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000247static arith_t arithmetic_common(VALUE *l, VALUE *r, int op)
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000248{
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000249 arith_t li, ri;
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000250
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000251 if (!toarith(l) || !toarith(r))
James Byrne69374872019-07-02 11:35:03 +0200252 bb_simple_error_msg_and_die("non-numeric argument");
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000253 li = l->u.i;
254 ri = r->u.i;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000255 if (op == '+')
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000256 return li + ri;
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000257 if (op == '-')
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000258 return li - ri;
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000259 if (op == '*')
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000260 return li * ri;
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000261 if (ri == 0)
James Byrne69374872019-07-02 11:35:03 +0200262 bb_simple_error_msg_and_die("division by zero");
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000263 if (op == '/')
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000264 return li / ri;
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000265 return li % ri;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000266}
267
Eric Andersen1b355eb2000-09-05 17:37:48 +0000268/* Do the : operator.
269 SV is the VALUE for the lhs (the string),
270 PV is the VALUE for the rhs (the pattern). */
271
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000272static VALUE *docolon(VALUE *sv, VALUE *pv)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000273{
Denys Vlasenko043b1e52009-09-06 12:47:55 +0200274 enum { NMATCH = 2 };
Eric Andersen1b355eb2000-09-05 17:37:48 +0000275 VALUE *v;
Rob Landley540d3f62005-05-09 21:42:42 +0000276 regex_t re_buffer;
Rob Landley540d3f62005-05-09 21:42:42 +0000277 regmatch_t re_regs[NMATCH];
Eric Andersen1b355eb2000-09-05 17:37:48 +0000278
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000279 tostring(sv);
280 tostring(pv);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000281
282 if (pv->u.s[0] == '^') {
Denis Vlasenko230997b2009-03-03 14:27:36 +0000283 bb_error_msg(
284"warning: '%s': using '^' as the first character\n"
285"of a basic regular expression is not portable; it is ignored", pv->u.s);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000286 }
287
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000288 memset(&re_buffer, 0, sizeof(re_buffer));
Denis Vlasenko230997b2009-03-03 14:27:36 +0000289 memset(re_regs, 0, sizeof(re_regs));
Bernhard Reutner-Fischer8025afa2007-04-02 16:54:41 +0000290 xregcomp(&re_buffer, pv->u.s, 0);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000291
Rob Landley540d3f62005-05-09 21:42:42 +0000292 /* expr uses an anchored pattern match, so check that there was a
293 * match and that the match starts at offset 0. */
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000294 if (regexec(&re_buffer, sv->u.s, NMATCH, re_regs, 0) != REG_NOMATCH
295 && re_regs[0].rm_so == 0
296 ) {
Eric Andersen1b355eb2000-09-05 17:37:48 +0000297 /* Were \(...\) used? */
Denis Vlasenko230997b2009-03-03 14:27:36 +0000298 if (re_buffer.re_nsub > 0 && re_regs[1].rm_so >= 0) {
Rob Landley540d3f62005-05-09 21:42:42 +0000299 sv->u.s[re_regs[1].rm_eo] = '\0';
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000300 v = str_value(sv->u.s + re_regs[1].rm_so);
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000301 } else {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000302 v = int_value(re_regs[0].rm_eo);
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000303 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000304 } else {
Eric Andersen1b355eb2000-09-05 17:37:48 +0000305 /* Match failed -- return the right kind of null. */
306 if (re_buffer.re_nsub > 0)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000307 v = str_value("");
Eric Andersen1b355eb2000-09-05 17:37:48 +0000308 else
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000309 v = int_value(0);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000310 }
Denis Vlasenko230997b2009-03-03 14:27:36 +0000311 regfree(&re_buffer);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000312 return v;
313}
314
315/* Handle bare operands and ( expr ) syntax. */
316
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000317static VALUE *eval7(void)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000318{
319 VALUE *v;
320
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000321 if (!*G.args)
James Byrne69374872019-07-02 11:35:03 +0200322 bb_simple_error_msg_and_die("syntax error");
Eric Andersen1b355eb2000-09-05 17:37:48 +0000323
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000324 if (nextarg("(")) {
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000325 G.args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000326 v = eval();
327 if (!nextarg(")"))
James Byrne69374872019-07-02 11:35:03 +0200328 bb_simple_error_msg_and_die("syntax error");
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000329 G.args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000330 return v;
331 }
Eric Andersen1b355eb2000-09-05 17:37:48 +0000332
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000333 if (nextarg(")"))
James Byrne69374872019-07-02 11:35:03 +0200334 bb_simple_error_msg_and_die("syntax error");
Eric Andersen1b355eb2000-09-05 17:37:48 +0000335
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000336 return str_value(*G.args++);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000337}
338
339/* Handle match, substr, index, length, and quote keywords. */
340
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000341static VALUE *eval6(void)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000342{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000343 static const char keywords[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000344 "quote\0""length\0""match\0""index\0""substr\0";
Eric Andersen1b355eb2000-09-05 17:37:48 +0000345
Denis Vlasenkoc6753c12007-07-01 18:33:35 +0000346 VALUE *r, *i1, *i2;
347 VALUE *l = l; /* silence gcc */
348 VALUE *v = v; /* silence gcc */
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000349 int key = *G.args ? index_in_strings(keywords, *G.args) + 1 : 0;
Denis Vlasenkoc6753c12007-07-01 18:33:35 +0000350
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000351 if (key == 0) /* not a keyword */
352 return eval7();
353 G.args++; /* We have a valid token, so get the next argument. */
354 if (key == 1) { /* quote */
355 if (!*G.args)
James Byrne69374872019-07-02 11:35:03 +0200356 bb_simple_error_msg_and_die("syntax error");
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000357 return str_value(*G.args++);
358 }
359 if (key == 2) { /* length */
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000360 r = eval6();
361 tostring(r);
362 v = int_value(strlen(r->u.s));
363 freev(r);
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000364 } else
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000365 l = eval6();
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000366
367 if (key == 3) { /* match */
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000368 r = eval6();
369 v = docolon(l, r);
370 freev(l);
371 freev(r);
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000372 }
373 if (key == 4) { /* index */
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000374 r = eval6();
375 tostring(l);
376 tostring(r);
377 v = int_value(strcspn(l->u.s, r->u.s) + 1);
378 if (v->u.i == (arith_t) strlen(l->u.s) + 1)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000379 v->u.i = 0;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000380 freev(l);
381 freev(r);
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000382 }
383 if (key == 5) { /* substr */
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000384 i1 = eval6();
385 i2 = eval6();
386 tostring(l);
387 if (!toarith(i1) || !toarith(i2)
Denis Vlasenkoc6753c12007-07-01 18:33:35 +0000388 || i1->u.i > (arith_t) strlen(l->u.s)
389 || i1->u.i <= 0 || i2->u.i <= 0)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000390 v = str_value("");
Eric Andersen1b355eb2000-09-05 17:37:48 +0000391 else {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000392 v = xmalloc(sizeof(VALUE));
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000393 v->type = STRING;
Rob Landleyd921b2e2006-08-03 15:41:12 +0000394 v->u.s = xstrndup(l->u.s + i1->u.i - 1, i2->u.i);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000395 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000396 freev(l);
397 freev(i1);
398 freev(i2);
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000399 }
400 return v;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000401}
402
403/* Handle : operator (pattern matching).
404 Calls docolon to do the real work. */
405
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000406static VALUE *eval5(void)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000407{
408 VALUE *l, *r, *v;
409
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000410 l = eval6();
411 while (nextarg(":")) {
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000412 G.args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000413 r = eval6();
414 v = docolon(l, r);
415 freev(l);
416 freev(r);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000417 l = v;
418 }
419 return l;
420}
421
422/* Handle *, /, % operators. */
423
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000424static VALUE *eval4(void)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000425{
426 VALUE *l, *r;
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +0000427 int op;
428 arith_t val;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000429
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000430 l = eval5();
Eric Andersen1b355eb2000-09-05 17:37:48 +0000431 while (1) {
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000432 op = nextarg("*");
433 if (!op) { op = nextarg("/");
434 if (!op) { op = nextarg("%");
435 if (!op) return l;
436 }}
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000437 G.args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000438 r = eval5();
439 val = arithmetic_common(l, r, op);
440 freev(l);
441 freev(r);
442 l = int_value(val);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000443 }
444}
445
446/* Handle +, - operators. */
447
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000448static VALUE *eval3(void)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000449{
450 VALUE *l, *r;
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +0000451 int op;
452 arith_t val;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000453
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000454 l = eval4();
Eric Andersen1b355eb2000-09-05 17:37:48 +0000455 while (1) {
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000456 op = nextarg("+");
457 if (!op) {
458 op = nextarg("-");
459 if (!op) return l;
460 }
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000461 G.args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000462 r = eval4();
463 val = arithmetic_common(l, r, op);
464 freev(l);
465 freev(r);
466 l = int_value(val);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000467 }
468}
469
470/* Handle comparisons. */
471
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000472static VALUE *eval2(void)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000473{
474 VALUE *l, *r;
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +0000475 int op;
476 arith_t val;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000477
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000478 l = eval3();
Eric Andersen1b355eb2000-09-05 17:37:48 +0000479 while (1) {
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000480 op = nextarg("<");
481 if (!op) { op = nextarg("<=");
482 if (!op) { op = nextarg("=");
483 if (!op) { op = nextarg("==");
484 if (!op) { op = nextarg("!=");
485 if (!op) { op = nextarg(">=");
486 if (!op) { op = nextarg(">");
487 if (!op) return l;
488 }}}}}}
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000489 G.args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000490 r = eval3();
491 toarith(l);
492 toarith(r);
493 val = cmp_common(l, r, op);
494 freev(l);
495 freev(r);
496 l = int_value(val);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000497 }
498}
499
500/* Handle &. */
501
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000502static VALUE *eval1(void)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000503{
504 VALUE *l, *r;
505
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000506 l = eval2();
507 while (nextarg("&")) {
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000508 G.args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000509 r = eval2();
510 if (null(l) || null(r)) {
511 freev(l);
512 freev(r);
513 l = int_value(0);
514 } else
515 freev(r);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000516 }
517 return l;
518}
519
520/* Handle |. */
521
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000522static VALUE *eval(void)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000523{
524 VALUE *l, *r;
525
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000526 l = eval1();
527 while (nextarg("|")) {
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000528 G.args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000529 r = eval1();
530 if (null(l)) {
531 freev(l);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000532 l = r;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000533 } else
534 freev(r);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000535 }
536 return l;
537}
Bernhard Reutner-Fischer3c5929a2007-04-02 16:41:24 +0000538
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000539int expr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko272710f2008-11-11 22:36:58 +0000540int expr_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischer3c5929a2007-04-02 16:41:24 +0000541{
542 VALUE *v;
543
Denys Vlasenko16714242011-09-21 01:59:15 +0200544 INIT_G();
545
Denis Vlasenko272710f2008-11-11 22:36:58 +0000546 xfunc_error_retval = 2; /* coreutils compat */
547 G.args = argv + 1;
548 if (*G.args == NULL) {
James Byrne69374872019-07-02 11:35:03 +0200549 bb_simple_error_msg_and_die("too few arguments");
Bernhard Reutner-Fischer3c5929a2007-04-02 16:41:24 +0000550 }
Bernhard Reutner-Fischer3c5929a2007-04-02 16:41:24 +0000551 v = eval();
552 if (*G.args)
James Byrne69374872019-07-02 11:35:03 +0200553 bb_simple_error_msg_and_die("syntax error");
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000554 if (v->type == INTEGER)
Bernhard Reutner-Fischer3c5929a2007-04-02 16:41:24 +0000555 printf("%" PF_REZ "d\n", PF_REZ_TYPE v->u.i);
556 else
557 puts(v->u.s);
Bernhard Reutner-Fischer3c5929a2007-04-02 16:41:24 +0000558 fflush_stdout_and_exit(null(v));
559}