blob: c986f93274f7e57e4070b135958c10e97df744ce [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"
Rob Landleyd921b2e2006-08-03 15:41:12 +000064#include "xregex.h"
Eric Andersen1b355eb2000-09-05 17:37:48 +000065
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +000066#if ENABLE_EXPR_MATH_SUPPORT_64
67typedef int64_t arith_t;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000068
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +000069#define PF_REZ "ll"
Eric Andersen5e678872006-01-30 19:48:23 +000070#define PF_REZ_TYPE (long long)
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +000071#define STRTOL(s, e, b) strtoll(s, e, b)
72#else
73typedef long arith_t;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000074
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +000075#define PF_REZ "l"
Eric Andersen5e678872006-01-30 19:48:23 +000076#define PF_REZ_TYPE (long)
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +000077#define STRTOL(s, e, b) strtol(s, e, b)
78#endif
79
Denis Vlasenkod686a042006-11-27 14:43:21 +000080/* TODO: use bb_strtol[l]? It's easier to check for errors... */
81
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +000082/* The kinds of value we can have. */
83enum {
84 INTEGER,
85 STRING
86};
87
Eric Andersen1b355eb2000-09-05 17:37:48 +000088/* A value is.... */
89struct valinfo {
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +000090 smallint type; /* Which kind. */
91 union { /* The value itself. */
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +000092 arith_t i;
Eric Andersen1b355eb2000-09-05 17:37:48 +000093 char *s;
94 } u;
95};
96typedef struct valinfo VALUE;
97
98/* The arguments given to the program, minus the program name. */
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +000099struct globals {
100 char **args;
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +0100101} FIX_ALIASING;
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000102#define G (*(struct globals*)&bb_common_bufsiz1)
Denys Vlasenko16714242011-09-21 01:59:15 +0200103#define INIT_G() do { } while (0)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000104
Bernhard Reutner-Fischer3c5929a2007-04-02 16:41:24 +0000105/* forward declarations */
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000106static VALUE *eval(void);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000107
Eric Andersen1b355eb2000-09-05 17:37:48 +0000108
109/* Return a VALUE for I. */
110
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000111static VALUE *int_value(arith_t i)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000112{
113 VALUE *v;
114
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000115 v = xzalloc(sizeof(VALUE));
116 if (INTEGER) /* otherwise xzaaloc did it already */
117 v->type = INTEGER;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000118 v->u.i = i;
119 return v;
120}
121
122/* Return a VALUE for S. */
123
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +0000124static VALUE *str_value(const char *s)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000125{
126 VALUE *v;
127
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000128 v = xzalloc(sizeof(VALUE));
129 if (STRING) /* otherwise xzaaloc did it already */
130 v->type = STRING;
Rob Landleyd921b2e2006-08-03 15:41:12 +0000131 v->u.s = xstrdup(s);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000132 return v;
133}
134
135/* Free VALUE V, including structure components. */
136
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000137static void freev(VALUE *v)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000138{
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000139 if (v->type == STRING)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000140 free(v->u.s);
141 free(v);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000142}
143
144/* Return nonzero if V is a null-string or zero-number. */
145
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000146static int null(VALUE *v)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000147{
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000148 if (v->type == INTEGER)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000149 return v->u.i == 0;
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000150 /* STRING: */
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000151 return v->u.s[0] == '\0' || LONE_CHAR(v->u.s, '0');
Eric Andersen1b355eb2000-09-05 17:37:48 +0000152}
153
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000154/* Coerce V to a STRING value (can't fail). */
Eric Andersen1b355eb2000-09-05 17:37:48 +0000155
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000156static void tostring(VALUE *v)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000157{
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000158 if (v->type == INTEGER) {
Rob Landleyd921b2e2006-08-03 15:41:12 +0000159 v->u.s = xasprintf("%" PF_REZ "d", PF_REZ_TYPE v->u.i);
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000160 v->type = STRING;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000161 }
162}
163
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000164/* Coerce V to an INTEGER value. Return 1 on success, 0 on failure. */
Eric Andersen1b355eb2000-09-05 17:37:48 +0000165
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000166static bool toarith(VALUE *v)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000167{
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000168 if (v->type == STRING) {
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +0000169 arith_t i;
Manuel Novoa III 70183852004-01-25 19:47:10 +0000170 char *e;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000171
Manuel Novoa III 70183852004-01-25 19:47:10 +0000172 /* Don't interpret the empty string as an integer. */
173 /* Currently does not worry about overflow or int/long differences. */
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +0000174 i = STRTOL(v->u.s, &e, 10);
Manuel Novoa III 70183852004-01-25 19:47:10 +0000175 if ((v->u.s == e) || *e)
176 return 0;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000177 free(v->u.s);
Manuel Novoa III 70183852004-01-25 19:47:10 +0000178 v->u.i = i;
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000179 v->type = INTEGER;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000180 }
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000181 return 1;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000182}
183
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000184/* Return str[0]+str[1] if the next token matches STR exactly.
Eric Andersen1b355eb2000-09-05 17:37:48 +0000185 STR must not be NULL. */
186
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000187static int nextarg(const char *str)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000188{
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000189 if (*G.args == NULL || strcmp(*G.args, str) != 0)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000190 return 0;
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000191 return (unsigned char)str[0] + (unsigned char)str[1];
Eric Andersen1b355eb2000-09-05 17:37:48 +0000192}
193
194/* The comparison operator handling functions. */
195
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000196static int cmp_common(VALUE *l, VALUE *r, int op)
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000197{
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000198 arith_t ll, rr;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000199
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000200 ll = l->u.i;
201 rr = r->u.i;
202 if (l->type == STRING || r->type == STRING) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000203 tostring(l);
204 tostring(r);
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000205 ll = strcmp(l->u.s, r->u.s);
206 rr = 0;
207 }
208 /* calculating ll - rr and checking the result is prone to overflows.
209 * We'll do it differently: */
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000210 if (op == '<')
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000211 return ll < rr;
212 if (op == ('<' + '='))
213 return ll <= rr;
214 if (op == '=' || (op == '=' + '='))
215 return ll == rr;
216 if (op == '!' + '=')
217 return ll != rr;
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000218 if (op == '>')
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000219 return ll > rr;
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000220 /* >= */
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000221 return ll >= rr;
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000222}
Eric Andersen1b355eb2000-09-05 17:37:48 +0000223
224/* The arithmetic operator handling functions. */
225
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000226static arith_t arithmetic_common(VALUE *l, VALUE *r, int op)
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000227{
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000228 arith_t li, ri;
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000229
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000230 if (!toarith(l) || !toarith(r))
231 bb_error_msg_and_die("non-numeric argument");
232 li = l->u.i;
233 ri = r->u.i;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000234 if (op == '+')
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000235 return li + ri;
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000236 if (op == '-')
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000237 return li - ri;
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000238 if (op == '*')
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000239 return li * ri;
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000240 if (ri == 0)
241 bb_error_msg_and_die("division by zero");
242 if (op == '/')
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000243 return li / ri;
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000244 return li % ri;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000245}
246
Eric Andersen1b355eb2000-09-05 17:37:48 +0000247/* Do the : operator.
248 SV is the VALUE for the lhs (the string),
249 PV is the VALUE for the rhs (the pattern). */
250
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000251static VALUE *docolon(VALUE *sv, VALUE *pv)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000252{
Denys Vlasenko043b1e52009-09-06 12:47:55 +0200253 enum { NMATCH = 2 };
Eric Andersen1b355eb2000-09-05 17:37:48 +0000254 VALUE *v;
Rob Landley540d3f62005-05-09 21:42:42 +0000255 regex_t re_buffer;
Rob Landley540d3f62005-05-09 21:42:42 +0000256 regmatch_t re_regs[NMATCH];
Eric Andersen1b355eb2000-09-05 17:37:48 +0000257
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000258 tostring(sv);
259 tostring(pv);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000260
261 if (pv->u.s[0] == '^') {
Denis Vlasenko230997b2009-03-03 14:27:36 +0000262 bb_error_msg(
263"warning: '%s': using '^' as the first character\n"
264"of a basic regular expression is not portable; it is ignored", pv->u.s);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000265 }
266
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000267 memset(&re_buffer, 0, sizeof(re_buffer));
Denis Vlasenko230997b2009-03-03 14:27:36 +0000268 memset(re_regs, 0, sizeof(re_regs));
Bernhard Reutner-Fischer8025afa2007-04-02 16:54:41 +0000269 xregcomp(&re_buffer, pv->u.s, 0);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000270
Rob Landley540d3f62005-05-09 21:42:42 +0000271 /* expr uses an anchored pattern match, so check that there was a
272 * match and that the match starts at offset 0. */
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000273 if (regexec(&re_buffer, sv->u.s, NMATCH, re_regs, 0) != REG_NOMATCH
274 && re_regs[0].rm_so == 0
275 ) {
Eric Andersen1b355eb2000-09-05 17:37:48 +0000276 /* Were \(...\) used? */
Denis Vlasenko230997b2009-03-03 14:27:36 +0000277 if (re_buffer.re_nsub > 0 && re_regs[1].rm_so >= 0) {
Rob Landley540d3f62005-05-09 21:42:42 +0000278 sv->u.s[re_regs[1].rm_eo] = '\0';
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000279 v = str_value(sv->u.s + re_regs[1].rm_so);
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000280 } else {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000281 v = int_value(re_regs[0].rm_eo);
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000282 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000283 } else {
Eric Andersen1b355eb2000-09-05 17:37:48 +0000284 /* Match failed -- return the right kind of null. */
285 if (re_buffer.re_nsub > 0)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000286 v = str_value("");
Eric Andersen1b355eb2000-09-05 17:37:48 +0000287 else
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000288 v = int_value(0);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000289 }
Denis Vlasenko230997b2009-03-03 14:27:36 +0000290 regfree(&re_buffer);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000291 return v;
292}
293
294/* Handle bare operands and ( expr ) syntax. */
295
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000296static VALUE *eval7(void)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000297{
298 VALUE *v;
299
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000300 if (!*G.args)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000301 bb_error_msg_and_die("syntax error");
Eric Andersen1b355eb2000-09-05 17:37:48 +0000302
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000303 if (nextarg("(")) {
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000304 G.args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000305 v = eval();
306 if (!nextarg(")"))
307 bb_error_msg_and_die("syntax error");
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000308 G.args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000309 return v;
310 }
Eric Andersen1b355eb2000-09-05 17:37:48 +0000311
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000312 if (nextarg(")"))
313 bb_error_msg_and_die("syntax error");
Eric Andersen1b355eb2000-09-05 17:37:48 +0000314
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000315 return str_value(*G.args++);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000316}
317
318/* Handle match, substr, index, length, and quote keywords. */
319
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000320static VALUE *eval6(void)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000321{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000322 static const char keywords[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000323 "quote\0""length\0""match\0""index\0""substr\0";
Eric Andersen1b355eb2000-09-05 17:37:48 +0000324
Denis Vlasenkoc6753c12007-07-01 18:33:35 +0000325 VALUE *r, *i1, *i2;
326 VALUE *l = l; /* silence gcc */
327 VALUE *v = v; /* silence gcc */
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000328 int key = *G.args ? index_in_strings(keywords, *G.args) + 1 : 0;
Denis Vlasenkoc6753c12007-07-01 18:33:35 +0000329
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000330 if (key == 0) /* not a keyword */
331 return eval7();
332 G.args++; /* We have a valid token, so get the next argument. */
333 if (key == 1) { /* quote */
334 if (!*G.args)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000335 bb_error_msg_and_die("syntax error");
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000336 return str_value(*G.args++);
337 }
338 if (key == 2) { /* length */
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000339 r = eval6();
340 tostring(r);
341 v = int_value(strlen(r->u.s));
342 freev(r);
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000343 } else
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000344 l = eval6();
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000345
346 if (key == 3) { /* match */
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000347 r = eval6();
348 v = docolon(l, r);
349 freev(l);
350 freev(r);
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000351 }
352 if (key == 4) { /* index */
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000353 r = eval6();
354 tostring(l);
355 tostring(r);
356 v = int_value(strcspn(l->u.s, r->u.s) + 1);
357 if (v->u.i == (arith_t) strlen(l->u.s) + 1)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000358 v->u.i = 0;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000359 freev(l);
360 freev(r);
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000361 }
362 if (key == 5) { /* substr */
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000363 i1 = eval6();
364 i2 = eval6();
365 tostring(l);
366 if (!toarith(i1) || !toarith(i2)
Denis Vlasenkoc6753c12007-07-01 18:33:35 +0000367 || i1->u.i > (arith_t) strlen(l->u.s)
368 || i1->u.i <= 0 || i2->u.i <= 0)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000369 v = str_value("");
Eric Andersen1b355eb2000-09-05 17:37:48 +0000370 else {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000371 v = xmalloc(sizeof(VALUE));
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000372 v->type = STRING;
Rob Landleyd921b2e2006-08-03 15:41:12 +0000373 v->u.s = xstrndup(l->u.s + i1->u.i - 1, i2->u.i);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000374 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000375 freev(l);
376 freev(i1);
377 freev(i2);
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000378 }
379 return v;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000380}
381
382/* Handle : operator (pattern matching).
383 Calls docolon to do the real work. */
384
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000385static VALUE *eval5(void)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000386{
387 VALUE *l, *r, *v;
388
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000389 l = eval6();
390 while (nextarg(":")) {
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000391 G.args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000392 r = eval6();
393 v = docolon(l, r);
394 freev(l);
395 freev(r);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000396 l = v;
397 }
398 return l;
399}
400
401/* Handle *, /, % operators. */
402
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000403static VALUE *eval4(void)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000404{
405 VALUE *l, *r;
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +0000406 int op;
407 arith_t val;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000408
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000409 l = eval5();
Eric Andersen1b355eb2000-09-05 17:37:48 +0000410 while (1) {
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000411 op = nextarg("*");
412 if (!op) { op = nextarg("/");
413 if (!op) { op = nextarg("%");
414 if (!op) return l;
415 }}
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000416 G.args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000417 r = eval5();
418 val = arithmetic_common(l, r, op);
419 freev(l);
420 freev(r);
421 l = int_value(val);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000422 }
423}
424
425/* Handle +, - operators. */
426
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000427static VALUE *eval3(void)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000428{
429 VALUE *l, *r;
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +0000430 int op;
431 arith_t val;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000432
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000433 l = eval4();
Eric Andersen1b355eb2000-09-05 17:37:48 +0000434 while (1) {
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000435 op = nextarg("+");
436 if (!op) {
437 op = nextarg("-");
438 if (!op) return l;
439 }
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000440 G.args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000441 r = eval4();
442 val = arithmetic_common(l, r, op);
443 freev(l);
444 freev(r);
445 l = int_value(val);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000446 }
447}
448
449/* Handle comparisons. */
450
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000451static VALUE *eval2(void)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000452{
453 VALUE *l, *r;
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +0000454 int op;
455 arith_t val;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000456
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000457 l = eval3();
Eric Andersen1b355eb2000-09-05 17:37:48 +0000458 while (1) {
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000459 op = nextarg("<");
460 if (!op) { 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) return l;
467 }}}}}}
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000468 G.args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000469 r = eval3();
470 toarith(l);
471 toarith(r);
472 val = cmp_common(l, r, op);
473 freev(l);
474 freev(r);
475 l = int_value(val);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000476 }
477}
478
479/* Handle &. */
480
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000481static VALUE *eval1(void)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000482{
483 VALUE *l, *r;
484
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000485 l = eval2();
486 while (nextarg("&")) {
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000487 G.args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000488 r = eval2();
489 if (null(l) || null(r)) {
490 freev(l);
491 freev(r);
492 l = int_value(0);
493 } else
494 freev(r);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000495 }
496 return l;
497}
498
499/* Handle |. */
500
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000501static VALUE *eval(void)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000502{
503 VALUE *l, *r;
504
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000505 l = eval1();
506 while (nextarg("|")) {
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000507 G.args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000508 r = eval1();
509 if (null(l)) {
510 freev(l);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000511 l = r;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000512 } else
513 freev(r);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000514 }
515 return l;
516}
Bernhard Reutner-Fischer3c5929a2007-04-02 16:41:24 +0000517
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000518int expr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko272710f2008-11-11 22:36:58 +0000519int expr_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischer3c5929a2007-04-02 16:41:24 +0000520{
521 VALUE *v;
522
Denys Vlasenko16714242011-09-21 01:59:15 +0200523 INIT_G();
524
Denis Vlasenko272710f2008-11-11 22:36:58 +0000525 xfunc_error_retval = 2; /* coreutils compat */
526 G.args = argv + 1;
527 if (*G.args == NULL) {
Bernhard Reutner-Fischer3c5929a2007-04-02 16:41:24 +0000528 bb_error_msg_and_die("too few arguments");
529 }
Bernhard Reutner-Fischer3c5929a2007-04-02 16:41:24 +0000530 v = eval();
531 if (*G.args)
532 bb_error_msg_and_die("syntax error");
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000533 if (v->type == INTEGER)
Bernhard Reutner-Fischer3c5929a2007-04-02 16:41:24 +0000534 printf("%" PF_REZ "d\n", PF_REZ_TYPE v->u.i);
535 else
536 puts(v->u.s);
Bernhard Reutner-Fischer3c5929a2007-04-02 16:41:24 +0000537 fflush_stdout_and_exit(null(v));
538}