blob: ce6b2d18910b962fe199b54629be44316111307f [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 */
16
17/* This program evaluates expressions. Each token (operator, operand,
Eric Andersenaff114c2004-04-14 17:51:38 +000018 * parenthesis) of the expression must be a separate argument. The
Eric Andersen1b355eb2000-09-05 17:37:48 +000019 * parser used is a reasonably general one, though any incarnation of
20 * it is language-specific. It is especially nice for expressions.
21 *
22 * No parse tree is needed; a new node is evaluated immediately.
23 * One function can handle multiple operators all of equal precedence,
24 * provided they all associate ((x op x) op x). */
25
Mark Whitley827e45c2001-03-09 23:59:51 +000026/* no getopt needed */
27
Pere Orga34425382011-03-31 14:43:25 +020028//usage:#define expr_trivial_usage
29//usage: "EXPRESSION"
30//usage:#define expr_full_usage "\n\n"
31//usage: "Print the value of EXPRESSION to stdout\n"
32//usage: "\n"
33//usage: "EXPRESSION may be:\n"
34//usage: " ARG1 | ARG2 ARG1 if it is neither null nor 0, otherwise ARG2\n"
35//usage: " ARG1 & ARG2 ARG1 if neither argument is null or 0, otherwise 0\n"
36//usage: " ARG1 < ARG2 1 if ARG1 is less than ARG2, else 0. Similarly:\n"
37//usage: " ARG1 <= ARG2\n"
38//usage: " ARG1 = ARG2\n"
39//usage: " ARG1 != ARG2\n"
40//usage: " ARG1 >= ARG2\n"
41//usage: " ARG1 > ARG2\n"
42//usage: " ARG1 + ARG2 Sum of ARG1 and ARG2. Similarly:\n"
43//usage: " ARG1 - ARG2\n"
44//usage: " ARG1 * ARG2\n"
45//usage: " ARG1 / ARG2\n"
46//usage: " ARG1 % ARG2\n"
47//usage: " STRING : REGEXP Anchored pattern match of REGEXP in STRING\n"
48//usage: " match STRING REGEXP Same as STRING : REGEXP\n"
49//usage: " substr STRING POS LENGTH Substring of STRING, POS counted from 1\n"
50//usage: " index STRING CHARS Index in STRING where any CHARS is found, or 0\n"
51//usage: " length STRING Length of STRING\n"
52//usage: " quote TOKEN Interpret TOKEN as a string, even if\n"
53//usage: " it is a keyword like 'match' or an\n"
54//usage: " operator like '/'\n"
55//usage: " (EXPRESSION) Value of EXPRESSION\n"
56//usage: "\n"
57//usage: "Beware that many operators need to be escaped or quoted for shells.\n"
58//usage: "Comparisons are arithmetic if both ARGs are numbers, else\n"
59//usage: "lexicographical. Pattern matches return the string matched between\n"
60//usage: "\\( and \\) or null; if \\( and \\) are not used, they return the number\n"
61//usage: "of characters matched or 0."
62
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000063#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020064#include "common_bufsiz.h"
Rob Landleyd921b2e2006-08-03 15:41:12 +000065#include "xregex.h"
Eric Andersen1b355eb2000-09-05 17:37:48 +000066
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +000067#if ENABLE_EXPR_MATH_SUPPORT_64
68typedef int64_t arith_t;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000069
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +000070#define PF_REZ "ll"
Eric Andersen5e678872006-01-30 19:48:23 +000071#define PF_REZ_TYPE (long long)
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +000072#define STRTOL(s, e, b) strtoll(s, e, b)
73#else
74typedef long arith_t;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000075
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +000076#define PF_REZ "l"
Eric Andersen5e678872006-01-30 19:48:23 +000077#define PF_REZ_TYPE (long)
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +000078#define STRTOL(s, e, b) strtol(s, e, b)
79#endif
80
Denis Vlasenkod686a042006-11-27 14:43:21 +000081/* TODO: use bb_strtol[l]? It's easier to check for errors... */
82
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +000083/* The kinds of value we can have. */
84enum {
85 INTEGER,
86 STRING
87};
88
Eric Andersen1b355eb2000-09-05 17:37:48 +000089/* A value is.... */
90struct valinfo {
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +000091 smallint type; /* Which kind. */
92 union { /* The value itself. */
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +000093 arith_t i;
Eric Andersen1b355eb2000-09-05 17:37:48 +000094 char *s;
95 } u;
96};
97typedef struct valinfo VALUE;
98
99/* The arguments given to the program, minus the program name. */
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000100struct globals {
101 char **args;
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +0100102} FIX_ALIASING;
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200103#define G (*(struct globals*)bb_common_bufsiz1)
Denys Vlasenko47cfbf32016-04-21 18:18:48 +0200104#define INIT_G() do { setup_common_bufsiz(); } while (0)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000105
Bernhard Reutner-Fischer3c5929a2007-04-02 16:41:24 +0000106/* forward declarations */
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000107static VALUE *eval(void);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000108
Eric Andersen1b355eb2000-09-05 17:37:48 +0000109
110/* Return a VALUE for I. */
111
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000112static VALUE *int_value(arith_t i)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000113{
114 VALUE *v;
115
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000116 v = xzalloc(sizeof(VALUE));
117 if (INTEGER) /* otherwise xzaaloc did it already */
118 v->type = INTEGER;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000119 v->u.i = i;
120 return v;
121}
122
123/* Return a VALUE for S. */
124
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +0000125static VALUE *str_value(const char *s)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000126{
127 VALUE *v;
128
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000129 v = xzalloc(sizeof(VALUE));
130 if (STRING) /* otherwise xzaaloc did it already */
131 v->type = STRING;
Rob Landleyd921b2e2006-08-03 15:41:12 +0000132 v->u.s = xstrdup(s);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000133 return v;
134}
135
136/* Free VALUE V, including structure components. */
137
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000138static void freev(VALUE *v)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000139{
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000140 if (v->type == STRING)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000141 free(v->u.s);
142 free(v);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000143}
144
145/* Return nonzero if V is a null-string or zero-number. */
146
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000147static int null(VALUE *v)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000148{
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000149 if (v->type == INTEGER)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000150 return v->u.i == 0;
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000151 /* STRING: */
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000152 return v->u.s[0] == '\0' || LONE_CHAR(v->u.s, '0');
Eric Andersen1b355eb2000-09-05 17:37:48 +0000153}
154
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000155/* Coerce V to a STRING value (can't fail). */
Eric Andersen1b355eb2000-09-05 17:37:48 +0000156
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000157static void tostring(VALUE *v)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000158{
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000159 if (v->type == INTEGER) {
Rob Landleyd921b2e2006-08-03 15:41:12 +0000160 v->u.s = xasprintf("%" PF_REZ "d", PF_REZ_TYPE v->u.i);
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000161 v->type = STRING;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000162 }
163}
164
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000165/* Coerce V to an INTEGER value. Return 1 on success, 0 on failure. */
Eric Andersen1b355eb2000-09-05 17:37:48 +0000166
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000167static bool toarith(VALUE *v)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000168{
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000169 if (v->type == STRING) {
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +0000170 arith_t i;
Manuel Novoa III 70183852004-01-25 19:47:10 +0000171 char *e;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000172
Manuel Novoa III 70183852004-01-25 19:47:10 +0000173 /* Don't interpret the empty string as an integer. */
174 /* Currently does not worry about overflow or int/long differences. */
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +0000175 i = STRTOL(v->u.s, &e, 10);
Manuel Novoa III 70183852004-01-25 19:47:10 +0000176 if ((v->u.s == e) || *e)
177 return 0;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000178 free(v->u.s);
Manuel Novoa III 70183852004-01-25 19:47:10 +0000179 v->u.i = i;
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000180 v->type = INTEGER;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000181 }
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000182 return 1;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000183}
184
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000185/* Return str[0]+str[1] if the next token matches STR exactly.
Eric Andersen1b355eb2000-09-05 17:37:48 +0000186 STR must not be NULL. */
187
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000188static int nextarg(const char *str)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000189{
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000190 if (*G.args == NULL || strcmp(*G.args, str) != 0)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000191 return 0;
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000192 return (unsigned char)str[0] + (unsigned char)str[1];
Eric Andersen1b355eb2000-09-05 17:37:48 +0000193}
194
195/* The comparison operator handling functions. */
196
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000197static int cmp_common(VALUE *l, VALUE *r, int op)
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000198{
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000199 arith_t ll, rr;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000200
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000201 ll = l->u.i;
202 rr = r->u.i;
203 if (l->type == STRING || r->type == STRING) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000204 tostring(l);
205 tostring(r);
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000206 ll = strcmp(l->u.s, r->u.s);
207 rr = 0;
208 }
209 /* calculating ll - rr and checking the result is prone to overflows.
210 * We'll do it differently: */
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000211 if (op == '<')
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000212 return ll < rr;
213 if (op == ('<' + '='))
214 return ll <= rr;
215 if (op == '=' || (op == '=' + '='))
216 return ll == rr;
217 if (op == '!' + '=')
218 return ll != rr;
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000219 if (op == '>')
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000220 return ll > rr;
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000221 /* >= */
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000222 return ll >= rr;
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000223}
Eric Andersen1b355eb2000-09-05 17:37:48 +0000224
225/* The arithmetic operator handling functions. */
226
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000227static arith_t arithmetic_common(VALUE *l, VALUE *r, int op)
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000228{
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000229 arith_t li, ri;
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000230
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000231 if (!toarith(l) || !toarith(r))
232 bb_error_msg_and_die("non-numeric argument");
233 li = l->u.i;
234 ri = r->u.i;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000235 if (op == '+')
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000236 return li + ri;
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000237 if (op == '-')
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000238 return li - ri;
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000239 if (op == '*')
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000240 return li * ri;
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000241 if (ri == 0)
242 bb_error_msg_and_die("division by zero");
243 if (op == '/')
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000244 return li / ri;
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000245 return li % ri;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000246}
247
Eric Andersen1b355eb2000-09-05 17:37:48 +0000248/* Do the : operator.
249 SV is the VALUE for the lhs (the string),
250 PV is the VALUE for the rhs (the pattern). */
251
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000252static VALUE *docolon(VALUE *sv, VALUE *pv)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000253{
Denys Vlasenko043b1e52009-09-06 12:47:55 +0200254 enum { NMATCH = 2 };
Eric Andersen1b355eb2000-09-05 17:37:48 +0000255 VALUE *v;
Rob Landley540d3f62005-05-09 21:42:42 +0000256 regex_t re_buffer;
Rob Landley540d3f62005-05-09 21:42:42 +0000257 regmatch_t re_regs[NMATCH];
Eric Andersen1b355eb2000-09-05 17:37:48 +0000258
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000259 tostring(sv);
260 tostring(pv);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000261
262 if (pv->u.s[0] == '^') {
Denis Vlasenko230997b2009-03-03 14:27:36 +0000263 bb_error_msg(
264"warning: '%s': using '^' as the first character\n"
265"of a basic regular expression is not portable; it is ignored", pv->u.s);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000266 }
267
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000268 memset(&re_buffer, 0, sizeof(re_buffer));
Denis Vlasenko230997b2009-03-03 14:27:36 +0000269 memset(re_regs, 0, sizeof(re_regs));
Bernhard Reutner-Fischer8025afa2007-04-02 16:54:41 +0000270 xregcomp(&re_buffer, pv->u.s, 0);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000271
Rob Landley540d3f62005-05-09 21:42:42 +0000272 /* expr uses an anchored pattern match, so check that there was a
273 * match and that the match starts at offset 0. */
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000274 if (regexec(&re_buffer, sv->u.s, NMATCH, re_regs, 0) != REG_NOMATCH
275 && re_regs[0].rm_so == 0
276 ) {
Eric Andersen1b355eb2000-09-05 17:37:48 +0000277 /* Were \(...\) used? */
Denis Vlasenko230997b2009-03-03 14:27:36 +0000278 if (re_buffer.re_nsub > 0 && re_regs[1].rm_so >= 0) {
Rob Landley540d3f62005-05-09 21:42:42 +0000279 sv->u.s[re_regs[1].rm_eo] = '\0';
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000280 v = str_value(sv->u.s + re_regs[1].rm_so);
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000281 } else {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000282 v = int_value(re_regs[0].rm_eo);
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000283 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000284 } else {
Eric Andersen1b355eb2000-09-05 17:37:48 +0000285 /* Match failed -- return the right kind of null. */
286 if (re_buffer.re_nsub > 0)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000287 v = str_value("");
Eric Andersen1b355eb2000-09-05 17:37:48 +0000288 else
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000289 v = int_value(0);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000290 }
Denis Vlasenko230997b2009-03-03 14:27:36 +0000291 regfree(&re_buffer);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000292 return v;
293}
294
295/* Handle bare operands and ( expr ) syntax. */
296
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000297static VALUE *eval7(void)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000298{
299 VALUE *v;
300
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000301 if (!*G.args)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000302 bb_error_msg_and_die("syntax error");
Eric Andersen1b355eb2000-09-05 17:37:48 +0000303
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000304 if (nextarg("(")) {
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000305 G.args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000306 v = eval();
307 if (!nextarg(")"))
308 bb_error_msg_and_die("syntax error");
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000309 G.args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000310 return v;
311 }
Eric Andersen1b355eb2000-09-05 17:37:48 +0000312
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000313 if (nextarg(")"))
314 bb_error_msg_and_die("syntax error");
Eric Andersen1b355eb2000-09-05 17:37:48 +0000315
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000316 return str_value(*G.args++);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000317}
318
319/* Handle match, substr, index, length, and quote keywords. */
320
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000321static VALUE *eval6(void)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000322{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000323 static const char keywords[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000324 "quote\0""length\0""match\0""index\0""substr\0";
Eric Andersen1b355eb2000-09-05 17:37:48 +0000325
Denis Vlasenkoc6753c12007-07-01 18:33:35 +0000326 VALUE *r, *i1, *i2;
327 VALUE *l = l; /* silence gcc */
328 VALUE *v = v; /* silence gcc */
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000329 int key = *G.args ? index_in_strings(keywords, *G.args) + 1 : 0;
Denis Vlasenkoc6753c12007-07-01 18:33:35 +0000330
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000331 if (key == 0) /* not a keyword */
332 return eval7();
333 G.args++; /* We have a valid token, so get the next argument. */
334 if (key == 1) { /* quote */
335 if (!*G.args)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000336 bb_error_msg_and_die("syntax error");
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000337 return str_value(*G.args++);
338 }
339 if (key == 2) { /* length */
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000340 r = eval6();
341 tostring(r);
342 v = int_value(strlen(r->u.s));
343 freev(r);
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000344 } else
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000345 l = eval6();
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000346
347 if (key == 3) { /* match */
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000348 r = eval6();
349 v = docolon(l, r);
350 freev(l);
351 freev(r);
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000352 }
353 if (key == 4) { /* index */
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000354 r = eval6();
355 tostring(l);
356 tostring(r);
357 v = int_value(strcspn(l->u.s, r->u.s) + 1);
358 if (v->u.i == (arith_t) strlen(l->u.s) + 1)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000359 v->u.i = 0;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000360 freev(l);
361 freev(r);
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000362 }
363 if (key == 5) { /* substr */
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000364 i1 = eval6();
365 i2 = eval6();
366 tostring(l);
367 if (!toarith(i1) || !toarith(i2)
Denis Vlasenkoc6753c12007-07-01 18:33:35 +0000368 || i1->u.i > (arith_t) strlen(l->u.s)
369 || i1->u.i <= 0 || i2->u.i <= 0)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000370 v = str_value("");
Eric Andersen1b355eb2000-09-05 17:37:48 +0000371 else {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000372 v = xmalloc(sizeof(VALUE));
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000373 v->type = STRING;
Rob Landleyd921b2e2006-08-03 15:41:12 +0000374 v->u.s = xstrndup(l->u.s + i1->u.i - 1, i2->u.i);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000375 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000376 freev(l);
377 freev(i1);
378 freev(i2);
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000379 }
380 return v;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000381}
382
383/* Handle : operator (pattern matching).
384 Calls docolon to do the real work. */
385
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000386static VALUE *eval5(void)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000387{
388 VALUE *l, *r, *v;
389
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000390 l = eval6();
391 while (nextarg(":")) {
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000392 G.args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000393 r = eval6();
394 v = docolon(l, r);
395 freev(l);
396 freev(r);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000397 l = v;
398 }
399 return l;
400}
401
402/* Handle *, /, % operators. */
403
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000404static VALUE *eval4(void)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000405{
406 VALUE *l, *r;
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +0000407 int op;
408 arith_t val;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000409
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000410 l = eval5();
Eric Andersen1b355eb2000-09-05 17:37:48 +0000411 while (1) {
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000412 op = nextarg("*");
413 if (!op) { op = nextarg("/");
414 if (!op) { op = nextarg("%");
415 if (!op) return l;
416 }}
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000417 G.args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000418 r = eval5();
419 val = arithmetic_common(l, r, op);
420 freev(l);
421 freev(r);
422 l = int_value(val);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000423 }
424}
425
426/* Handle +, - operators. */
427
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000428static VALUE *eval3(void)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000429{
430 VALUE *l, *r;
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +0000431 int op;
432 arith_t val;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000433
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000434 l = eval4();
Eric Andersen1b355eb2000-09-05 17:37:48 +0000435 while (1) {
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000436 op = nextarg("+");
437 if (!op) {
438 op = nextarg("-");
439 if (!op) return l;
440 }
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000441 G.args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000442 r = eval4();
443 val = arithmetic_common(l, r, op);
444 freev(l);
445 freev(r);
446 l = int_value(val);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000447 }
448}
449
450/* Handle comparisons. */
451
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000452static VALUE *eval2(void)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000453{
454 VALUE *l, *r;
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +0000455 int op;
456 arith_t val;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000457
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000458 l = eval3();
Eric Andersen1b355eb2000-09-05 17:37:48 +0000459 while (1) {
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000460 op = nextarg("<");
461 if (!op) { op = nextarg("<=");
462 if (!op) { op = nextarg("=");
463 if (!op) { op = nextarg("==");
464 if (!op) { op = nextarg("!=");
465 if (!op) { op = nextarg(">=");
466 if (!op) { op = nextarg(">");
467 if (!op) return l;
468 }}}}}}
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000469 G.args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000470 r = eval3();
471 toarith(l);
472 toarith(r);
473 val = cmp_common(l, r, op);
474 freev(l);
475 freev(r);
476 l = int_value(val);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000477 }
478}
479
480/* Handle &. */
481
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000482static VALUE *eval1(void)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000483{
484 VALUE *l, *r;
485
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000486 l = eval2();
487 while (nextarg("&")) {
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000488 G.args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000489 r = eval2();
490 if (null(l) || null(r)) {
491 freev(l);
492 freev(r);
493 l = int_value(0);
494 } else
495 freev(r);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000496 }
497 return l;
498}
499
500/* Handle |. */
501
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000502static VALUE *eval(void)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000503{
504 VALUE *l, *r;
505
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000506 l = eval1();
507 while (nextarg("|")) {
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000508 G.args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000509 r = eval1();
510 if (null(l)) {
511 freev(l);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000512 l = r;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000513 } else
514 freev(r);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000515 }
516 return l;
517}
Bernhard Reutner-Fischer3c5929a2007-04-02 16:41:24 +0000518
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000519int expr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko272710f2008-11-11 22:36:58 +0000520int expr_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischer3c5929a2007-04-02 16:41:24 +0000521{
522 VALUE *v;
523
Denys Vlasenko16714242011-09-21 01:59:15 +0200524 INIT_G();
525
Denis Vlasenko272710f2008-11-11 22:36:58 +0000526 xfunc_error_retval = 2; /* coreutils compat */
527 G.args = argv + 1;
528 if (*G.args == NULL) {
Bernhard Reutner-Fischer3c5929a2007-04-02 16:41:24 +0000529 bb_error_msg_and_die("too few arguments");
530 }
Bernhard Reutner-Fischer3c5929a2007-04-02 16:41:24 +0000531 v = eval();
532 if (*G.args)
533 bb_error_msg_and_die("syntax error");
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000534 if (v->type == INTEGER)
Bernhard Reutner-Fischer3c5929a2007-04-02 16:41:24 +0000535 printf("%" PF_REZ "d\n", PF_REZ_TYPE v->u.i);
536 else
537 puts(v->u.s);
Bernhard Reutner-Fischer3c5929a2007-04-02 16:41:24 +0000538 fflush_stdout_and_exit(null(v));
539}