blob: 54c2ee1655510614319d1f4b49ac4b80c683065e [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 *
"Robert P. J. Day"801ab142006-07-12 07:56:04 +000014 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
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
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000028#include "libbb.h"
Rob Landleyd921b2e2006-08-03 15:41:12 +000029#include "xregex.h"
Eric Andersen1b355eb2000-09-05 17:37:48 +000030
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +000031#if ENABLE_EXPR_MATH_SUPPORT_64
32typedef int64_t arith_t;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000033
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +000034#define PF_REZ "ll"
Eric Andersen5e678872006-01-30 19:48:23 +000035#define PF_REZ_TYPE (long long)
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +000036#define STRTOL(s, e, b) strtoll(s, e, b)
37#else
38typedef long arith_t;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000039
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +000040#define PF_REZ "l"
Eric Andersen5e678872006-01-30 19:48:23 +000041#define PF_REZ_TYPE (long)
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +000042#define STRTOL(s, e, b) strtol(s, e, b)
43#endif
44
Denis Vlasenkod686a042006-11-27 14:43:21 +000045/* TODO: use bb_strtol[l]? It's easier to check for errors... */
46
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +000047/* The kinds of value we can have. */
48enum {
49 INTEGER,
50 STRING
51};
52
Eric Andersen1b355eb2000-09-05 17:37:48 +000053/* A value is.... */
54struct valinfo {
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +000055 smallint type; /* Which kind. */
56 union { /* The value itself. */
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +000057 arith_t i;
Eric Andersen1b355eb2000-09-05 17:37:48 +000058 char *s;
59 } u;
60};
61typedef struct valinfo VALUE;
62
63/* The arguments given to the program, minus the program name. */
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +000064struct globals {
65 char **args;
66};
67#define G (*(struct globals*)&bb_common_bufsiz1)
Eric Andersen1b355eb2000-09-05 17:37:48 +000068
Bernhard Reutner-Fischer3c5929a2007-04-02 16:41:24 +000069/* forward declarations */
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000070static VALUE *eval(void);
Eric Andersen1b355eb2000-09-05 17:37:48 +000071
Eric Andersen1b355eb2000-09-05 17:37:48 +000072
73/* Return a VALUE for I. */
74
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000075static VALUE *int_value(arith_t i)
Eric Andersen1b355eb2000-09-05 17:37:48 +000076{
77 VALUE *v;
78
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +000079 v = xzalloc(sizeof(VALUE));
80 if (INTEGER) /* otherwise xzaaloc did it already */
81 v->type = INTEGER;
Eric Andersen1b355eb2000-09-05 17:37:48 +000082 v->u.i = i;
83 return v;
84}
85
86/* Return a VALUE for S. */
87
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +000088static VALUE *str_value(const char *s)
Eric Andersen1b355eb2000-09-05 17:37:48 +000089{
90 VALUE *v;
91
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +000092 v = xzalloc(sizeof(VALUE));
93 if (STRING) /* otherwise xzaaloc did it already */
94 v->type = STRING;
Rob Landleyd921b2e2006-08-03 15:41:12 +000095 v->u.s = xstrdup(s);
Eric Andersen1b355eb2000-09-05 17:37:48 +000096 return v;
97}
98
99/* Free VALUE V, including structure components. */
100
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000101static void freev(VALUE *v)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000102{
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000103 if (v->type == STRING)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000104 free(v->u.s);
105 free(v);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000106}
107
108/* Return nonzero if V is a null-string or zero-number. */
109
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000110static int null(VALUE *v)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000111{
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000112 if (v->type == INTEGER)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000113 return v->u.i == 0;
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000114 /* STRING: */
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000115 return v->u.s[0] == '\0' || LONE_CHAR(v->u.s, '0');
Eric Andersen1b355eb2000-09-05 17:37:48 +0000116}
117
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000118/* Coerce V to a STRING value (can't fail). */
Eric Andersen1b355eb2000-09-05 17:37:48 +0000119
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000120static void tostring(VALUE *v)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000121{
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000122 if (v->type == INTEGER) {
Rob Landleyd921b2e2006-08-03 15:41:12 +0000123 v->u.s = xasprintf("%" PF_REZ "d", PF_REZ_TYPE v->u.i);
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000124 v->type = STRING;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000125 }
126}
127
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000128/* Coerce V to an INTEGER value. Return 1 on success, 0 on failure. */
Eric Andersen1b355eb2000-09-05 17:37:48 +0000129
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000130static bool toarith(VALUE *v)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000131{
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000132 if (v->type == STRING) {
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +0000133 arith_t i;
Manuel Novoa III 70183852004-01-25 19:47:10 +0000134 char *e;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000135
Manuel Novoa III 70183852004-01-25 19:47:10 +0000136 /* Don't interpret the empty string as an integer. */
137 /* Currently does not worry about overflow or int/long differences. */
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +0000138 i = STRTOL(v->u.s, &e, 10);
Manuel Novoa III 70183852004-01-25 19:47:10 +0000139 if ((v->u.s == e) || *e)
140 return 0;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000141 free(v->u.s);
Manuel Novoa III 70183852004-01-25 19:47:10 +0000142 v->u.i = i;
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000143 v->type = INTEGER;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000144 }
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000145 return 1;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000146}
147
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000148/* Return str[0]+str[1] if the next token matches STR exactly.
Eric Andersen1b355eb2000-09-05 17:37:48 +0000149 STR must not be NULL. */
150
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000151static int nextarg(const char *str)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000152{
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000153 if (*G.args == NULL || strcmp(*G.args, str) != 0)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000154 return 0;
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000155 return (unsigned char)str[0] + (unsigned char)str[1];
Eric Andersen1b355eb2000-09-05 17:37:48 +0000156}
157
158/* The comparison operator handling functions. */
159
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000160static int cmp_common(VALUE *l, VALUE *r, int op)
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000161{
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000162 arith_t ll, rr;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000163
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000164 ll = l->u.i;
165 rr = r->u.i;
166 if (l->type == STRING || r->type == STRING) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000167 tostring(l);
168 tostring(r);
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000169 ll = strcmp(l->u.s, r->u.s);
170 rr = 0;
171 }
172 /* calculating ll - rr and checking the result is prone to overflows.
173 * We'll do it differently: */
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000174 if (op == '<')
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000175 return ll < rr;
176 if (op == ('<' + '='))
177 return ll <= rr;
178 if (op == '=' || (op == '=' + '='))
179 return ll == rr;
180 if (op == '!' + '=')
181 return ll != rr;
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000182 if (op == '>')
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000183 return ll > rr;
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000184 /* >= */
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000185 return ll >= rr;
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000186}
Eric Andersen1b355eb2000-09-05 17:37:48 +0000187
188/* The arithmetic operator handling functions. */
189
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000190static arith_t arithmetic_common(VALUE *l, VALUE *r, int op)
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000191{
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000192 arith_t li, ri;
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000193
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000194 if (!toarith(l) || !toarith(r))
195 bb_error_msg_and_die("non-numeric argument");
196 li = l->u.i;
197 ri = r->u.i;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000198 if (op == '+')
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000199 return li + ri;
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000200 if (op == '-')
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000201 return li - ri;
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000202 if (op == '*')
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000203 return li * ri;
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000204 if (ri == 0)
205 bb_error_msg_and_die("division by zero");
206 if (op == '/')
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000207 return li / ri;
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000208 return li % ri;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000209}
210
Eric Andersen1b355eb2000-09-05 17:37:48 +0000211/* Do the : operator.
212 SV is the VALUE for the lhs (the string),
213 PV is the VALUE for the rhs (the pattern). */
214
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000215static VALUE *docolon(VALUE *sv, VALUE *pv)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000216{
217 VALUE *v;
Rob Landley540d3f62005-05-09 21:42:42 +0000218 regex_t re_buffer;
219 const int NMATCH = 2;
220 regmatch_t re_regs[NMATCH];
Eric Andersen1b355eb2000-09-05 17:37:48 +0000221
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000222 tostring(sv);
223 tostring(pv);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000224
225 if (pv->u.s[0] == '^') {
Denis Vlasenko230997b2009-03-03 14:27:36 +0000226 bb_error_msg(
227"warning: '%s': using '^' as the first character\n"
228"of a basic regular expression is not portable; it is ignored", pv->u.s);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000229 }
230
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000231 memset(&re_buffer, 0, sizeof(re_buffer));
Denis Vlasenko230997b2009-03-03 14:27:36 +0000232 memset(re_regs, 0, sizeof(re_regs));
Bernhard Reutner-Fischer8025afa2007-04-02 16:54:41 +0000233 xregcomp(&re_buffer, pv->u.s, 0);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000234
Rob Landley540d3f62005-05-09 21:42:42 +0000235 /* expr uses an anchored pattern match, so check that there was a
236 * match and that the match starts at offset 0. */
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000237 if (regexec(&re_buffer, sv->u.s, NMATCH, re_regs, 0) != REG_NOMATCH
238 && re_regs[0].rm_so == 0
239 ) {
Eric Andersen1b355eb2000-09-05 17:37:48 +0000240 /* Were \(...\) used? */
Denis Vlasenko230997b2009-03-03 14:27:36 +0000241 if (re_buffer.re_nsub > 0 && re_regs[1].rm_so >= 0) {
Rob Landley540d3f62005-05-09 21:42:42 +0000242 sv->u.s[re_regs[1].rm_eo] = '\0';
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000243 v = str_value(sv->u.s + re_regs[1].rm_so);
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000244 } else {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000245 v = int_value(re_regs[0].rm_eo);
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000246 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000247 } else {
Eric Andersen1b355eb2000-09-05 17:37:48 +0000248 /* Match failed -- return the right kind of null. */
249 if (re_buffer.re_nsub > 0)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000250 v = str_value("");
Eric Andersen1b355eb2000-09-05 17:37:48 +0000251 else
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000252 v = int_value(0);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000253 }
Denis Vlasenko230997b2009-03-03 14:27:36 +0000254 regfree(&re_buffer);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000255 return v;
256}
257
258/* Handle bare operands and ( expr ) syntax. */
259
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000260static VALUE *eval7(void)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000261{
262 VALUE *v;
263
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000264 if (!*G.args)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000265 bb_error_msg_and_die("syntax error");
Eric Andersen1b355eb2000-09-05 17:37:48 +0000266
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000267 if (nextarg("(")) {
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000268 G.args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000269 v = eval();
270 if (!nextarg(")"))
271 bb_error_msg_and_die("syntax error");
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000272 G.args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000273 return v;
274 }
Eric Andersen1b355eb2000-09-05 17:37:48 +0000275
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000276 if (nextarg(")"))
277 bb_error_msg_and_die("syntax error");
Eric Andersen1b355eb2000-09-05 17:37:48 +0000278
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000279 return str_value(*G.args++);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000280}
281
282/* Handle match, substr, index, length, and quote keywords. */
283
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000284static VALUE *eval6(void)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000285{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000286 static const char keywords[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000287 "quote\0""length\0""match\0""index\0""substr\0";
Eric Andersen1b355eb2000-09-05 17:37:48 +0000288
Denis Vlasenkoc6753c12007-07-01 18:33:35 +0000289 VALUE *r, *i1, *i2;
290 VALUE *l = l; /* silence gcc */
291 VALUE *v = v; /* silence gcc */
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000292 int key = *G.args ? index_in_strings(keywords, *G.args) + 1 : 0;
Denis Vlasenkoc6753c12007-07-01 18:33:35 +0000293
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000294 if (key == 0) /* not a keyword */
295 return eval7();
296 G.args++; /* We have a valid token, so get the next argument. */
297 if (key == 1) { /* quote */
298 if (!*G.args)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000299 bb_error_msg_and_die("syntax error");
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000300 return str_value(*G.args++);
301 }
302 if (key == 2) { /* length */
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000303 r = eval6();
304 tostring(r);
305 v = int_value(strlen(r->u.s));
306 freev(r);
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000307 } else
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000308 l = eval6();
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000309
310 if (key == 3) { /* match */
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000311 r = eval6();
312 v = docolon(l, r);
313 freev(l);
314 freev(r);
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000315 }
316 if (key == 4) { /* index */
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000317 r = eval6();
318 tostring(l);
319 tostring(r);
320 v = int_value(strcspn(l->u.s, r->u.s) + 1);
321 if (v->u.i == (arith_t) strlen(l->u.s) + 1)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000322 v->u.i = 0;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000323 freev(l);
324 freev(r);
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000325 }
326 if (key == 5) { /* substr */
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000327 i1 = eval6();
328 i2 = eval6();
329 tostring(l);
330 if (!toarith(i1) || !toarith(i2)
Denis Vlasenkoc6753c12007-07-01 18:33:35 +0000331 || i1->u.i > (arith_t) strlen(l->u.s)
332 || i1->u.i <= 0 || i2->u.i <= 0)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000333 v = str_value("");
Eric Andersen1b355eb2000-09-05 17:37:48 +0000334 else {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000335 v = xmalloc(sizeof(VALUE));
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000336 v->type = STRING;
Rob Landleyd921b2e2006-08-03 15:41:12 +0000337 v->u.s = xstrndup(l->u.s + i1->u.i - 1, i2->u.i);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000338 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000339 freev(l);
340 freev(i1);
341 freev(i2);
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000342 }
343 return v;
344
Eric Andersen1b355eb2000-09-05 17:37:48 +0000345}
346
347/* Handle : operator (pattern matching).
348 Calls docolon to do the real work. */
349
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000350static VALUE *eval5(void)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000351{
352 VALUE *l, *r, *v;
353
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000354 l = eval6();
355 while (nextarg(":")) {
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000356 G.args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000357 r = eval6();
358 v = docolon(l, r);
359 freev(l);
360 freev(r);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000361 l = v;
362 }
363 return l;
364}
365
366/* Handle *, /, % operators. */
367
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000368static VALUE *eval4(void)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000369{
370 VALUE *l, *r;
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +0000371 int op;
372 arith_t val;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000373
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000374 l = eval5();
Eric Andersen1b355eb2000-09-05 17:37:48 +0000375 while (1) {
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000376 op = nextarg("*");
377 if (!op) { op = nextarg("/");
378 if (!op) { op = nextarg("%");
379 if (!op) return l;
380 }}
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000381 G.args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000382 r = eval5();
383 val = arithmetic_common(l, r, op);
384 freev(l);
385 freev(r);
386 l = int_value(val);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000387 }
388}
389
390/* Handle +, - operators. */
391
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000392static VALUE *eval3(void)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000393{
394 VALUE *l, *r;
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +0000395 int op;
396 arith_t val;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000397
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000398 l = eval4();
Eric Andersen1b355eb2000-09-05 17:37:48 +0000399 while (1) {
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000400 op = nextarg("+");
401 if (!op) {
402 op = nextarg("-");
403 if (!op) return l;
404 }
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000405 G.args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000406 r = eval4();
407 val = arithmetic_common(l, r, op);
408 freev(l);
409 freev(r);
410 l = int_value(val);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000411 }
412}
413
414/* Handle comparisons. */
415
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000416static VALUE *eval2(void)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000417{
418 VALUE *l, *r;
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +0000419 int op;
420 arith_t val;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000421
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000422 l = eval3();
Eric Andersen1b355eb2000-09-05 17:37:48 +0000423 while (1) {
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000424 op = nextarg("<");
425 if (!op) { op = nextarg("<=");
426 if (!op) { op = nextarg("=");
427 if (!op) { op = nextarg("==");
428 if (!op) { op = nextarg("!=");
429 if (!op) { op = nextarg(">=");
430 if (!op) { op = nextarg(">");
431 if (!op) return l;
432 }}}}}}
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000433 G.args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000434 r = eval3();
435 toarith(l);
436 toarith(r);
437 val = cmp_common(l, r, op);
438 freev(l);
439 freev(r);
440 l = int_value(val);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000441 }
442}
443
444/* Handle &. */
445
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000446static VALUE *eval1(void)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000447{
448 VALUE *l, *r;
449
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000450 l = eval2();
451 while (nextarg("&")) {
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000452 G.args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000453 r = eval2();
454 if (null(l) || null(r)) {
455 freev(l);
456 freev(r);
457 l = int_value(0);
458 } else
459 freev(r);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000460 }
461 return l;
462}
463
464/* Handle |. */
465
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000466static VALUE *eval(void)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000467{
468 VALUE *l, *r;
469
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000470 l = eval1();
471 while (nextarg("|")) {
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000472 G.args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000473 r = eval1();
474 if (null(l)) {
475 freev(l);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000476 l = r;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000477 } else
478 freev(r);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000479 }
480 return l;
481}
Bernhard Reutner-Fischer3c5929a2007-04-02 16:41:24 +0000482
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000483int expr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko272710f2008-11-11 22:36:58 +0000484int expr_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischer3c5929a2007-04-02 16:41:24 +0000485{
486 VALUE *v;
487
Denis Vlasenko272710f2008-11-11 22:36:58 +0000488 xfunc_error_retval = 2; /* coreutils compat */
489 G.args = argv + 1;
490 if (*G.args == NULL) {
Bernhard Reutner-Fischer3c5929a2007-04-02 16:41:24 +0000491 bb_error_msg_and_die("too few arguments");
492 }
Bernhard Reutner-Fischer3c5929a2007-04-02 16:41:24 +0000493 v = eval();
494 if (*G.args)
495 bb_error_msg_and_die("syntax error");
Denis Vlasenkoa7f4e4b2008-04-02 20:24:09 +0000496 if (v->type == INTEGER)
Bernhard Reutner-Fischer3c5929a2007-04-02 16:41:24 +0000497 printf("%" PF_REZ "d\n", PF_REZ_TYPE v->u.i);
498 else
499 puts(v->u.s);
Bernhard Reutner-Fischer3c5929a2007-04-02 16:41:24 +0000500 fflush_stdout_and_exit(null(v));
501}