blob: 318fee721a0e1edda3da29b287e1785cd231e0a4 [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
31/* The kinds of value we can have. */
32enum valtype {
33 integer,
34 string
35};
36typedef enum valtype TYPE;
37
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +000038#if ENABLE_EXPR_MATH_SUPPORT_64
39typedef int64_t arith_t;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000040
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +000041#define PF_REZ "ll"
Eric Andersen5e678872006-01-30 19:48:23 +000042#define PF_REZ_TYPE (long long)
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +000043#define STRTOL(s, e, b) strtoll(s, e, b)
44#else
45typedef long arith_t;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000046
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +000047#define PF_REZ "l"
Eric Andersen5e678872006-01-30 19:48:23 +000048#define PF_REZ_TYPE (long)
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +000049#define STRTOL(s, e, b) strtol(s, e, b)
50#endif
51
Denis Vlasenkod686a042006-11-27 14:43:21 +000052/* TODO: use bb_strtol[l]? It's easier to check for errors... */
53
Eric Andersen1b355eb2000-09-05 17:37:48 +000054/* A value is.... */
55struct valinfo {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000056 TYPE type; /* Which kind. */
57 union { /* The value itself. */
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +000058 arith_t i;
Eric Andersen1b355eb2000-09-05 17:37:48 +000059 char *s;
60 } u;
61};
62typedef struct valinfo VALUE;
63
64/* The arguments given to the program, minus the program name. */
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +000065struct globals {
66 char **args;
67};
68#define G (*(struct globals*)&bb_common_bufsiz1)
Eric Andersen1b355eb2000-09-05 17:37:48 +000069
Bernhard Reutner-Fischer3c5929a2007-04-02 16:41:24 +000070/* forward declarations */
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000071static VALUE *eval(void);
Eric Andersen1b355eb2000-09-05 17:37:48 +000072
Eric Andersen1b355eb2000-09-05 17:37:48 +000073
74/* Return a VALUE for I. */
75
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000076static VALUE *int_value(arith_t i)
Eric Andersen1b355eb2000-09-05 17:37:48 +000077{
78 VALUE *v;
79
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000080 v = xmalloc(sizeof(VALUE));
Eric Andersen1b355eb2000-09-05 17:37:48 +000081 v->type = integer;
82 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
Rob Landleyd921b2e2006-08-03 15:41:12 +000092 v = xmalloc(sizeof(VALUE));
Eric Andersen1b355eb2000-09-05 17:37:48 +000093 v->type = string;
Rob Landleyd921b2e2006-08-03 15:41:12 +000094 v->u.s = xstrdup(s);
Eric Andersen1b355eb2000-09-05 17:37:48 +000095 return v;
96}
97
98/* Free VALUE V, including structure components. */
99
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000100static void freev(VALUE * v)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000101{
102 if (v->type == string)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000103 free(v->u.s);
104 free(v);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000105}
106
107/* Return nonzero if V is a null-string or zero-number. */
108
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000109static int null(VALUE * v)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000110{
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000111 if (v->type == integer)
112 return v->u.i == 0;
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000113 /* string: */
114 return v->u.s[0] == '\0' || LONE_CHAR(v->u.s, '0');
Eric Andersen1b355eb2000-09-05 17:37:48 +0000115}
116
117/* Coerce V to a string value (can't fail). */
118
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000119static void tostring(VALUE * v)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000120{
Eric Andersen1b355eb2000-09-05 17:37:48 +0000121 if (v->type == integer) {
Rob Landleyd921b2e2006-08-03 15:41:12 +0000122 v->u.s = xasprintf("%" PF_REZ "d", PF_REZ_TYPE v->u.i);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000123 v->type = string;
124 }
125}
126
127/* Coerce V to an integer value. Return 1 on success, 0 on failure. */
128
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000129static bool toarith(VALUE * v)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000130{
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000131 if (v->type == string) {
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +0000132 arith_t i;
Manuel Novoa III 70183852004-01-25 19:47:10 +0000133 char *e;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000134
Manuel Novoa III 70183852004-01-25 19:47:10 +0000135 /* Don't interpret the empty string as an integer. */
136 /* Currently does not worry about overflow or int/long differences. */
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +0000137 i = STRTOL(v->u.s, &e, 10);
Manuel Novoa III 70183852004-01-25 19:47:10 +0000138 if ((v->u.s == e) || *e)
139 return 0;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000140 free(v->u.s);
Manuel Novoa III 70183852004-01-25 19:47:10 +0000141 v->u.i = i;
142 v->type = integer;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000143 }
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000144 return 1;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000145}
146
147/* Return nonzero if the next token matches STR exactly.
148 STR must not be NULL. */
149
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000150static bool nextarg(const char *str)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000151{
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000152 if (*G.args == NULL)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000153 return 0;
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000154 return strcmp(*G.args, str) == 0;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000155}
156
157/* The comparison operator handling functions. */
158
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000159static int cmp_common(VALUE * l, VALUE * r, int op)
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000160{
161 int cmpval;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000162
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000163 if (l->type == string || r->type == string) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000164 tostring(l);
165 tostring(r);
166 cmpval = strcmp(l->u.s, r->u.s);
167 } else
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000168 cmpval = l->u.i - r->u.i;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000169 if (op == '<')
170 return cmpval < 0;
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000171 if (op == ('L' + 'E'))
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000172 return cmpval <= 0;
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000173 if (op == '=')
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000174 return cmpval == 0;
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000175 if (op == '!')
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000176 return cmpval != 0;
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000177 if (op == '>')
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000178 return cmpval > 0;
Denis Vlasenkocd27c422007-03-08 13:37:43 +0000179 /* >= */
180 return cmpval >= 0;
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000181}
Eric Andersen1b355eb2000-09-05 17:37:48 +0000182
183/* The arithmetic operator handling functions. */
184
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000185static arith_t arithmetic_common(VALUE * l, VALUE * r, int op)
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000186{
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000187 arith_t li, ri;
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000188
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000189 if (!toarith(l) || !toarith(r))
190 bb_error_msg_and_die("non-numeric argument");
191 li = l->u.i;
192 ri = r->u.i;
193 if ((op == '/' || op == '%') && ri == 0)
194 bb_error_msg_and_die("division by zero");
195 if (op == '+')
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000196 return li + ri;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000197 else if (op == '-')
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000198 return li - ri;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000199 else if (op == '*')
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000200 return li * ri;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000201 else if (op == '/')
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000202 return li / ri;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000203 else
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000204 return li % ri;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000205}
206
Eric Andersen1b355eb2000-09-05 17:37:48 +0000207/* Do the : operator.
208 SV is the VALUE for the lhs (the string),
209 PV is the VALUE for the rhs (the pattern). */
210
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000211static VALUE *docolon(VALUE * sv, VALUE * pv)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000212{
213 VALUE *v;
Rob Landley540d3f62005-05-09 21:42:42 +0000214 regex_t re_buffer;
215 const int NMATCH = 2;
216 regmatch_t re_regs[NMATCH];
Eric Andersen1b355eb2000-09-05 17:37:48 +0000217
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000218 tostring(sv);
219 tostring(pv);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000220
221 if (pv->u.s[0] == '^') {
Bernhard Reutner-Fischercea0a8b2007-04-02 17:01:22 +0000222 bb_error_msg("\
Eric Andersen1b355eb2000-09-05 17:37:48 +0000223warning: unportable BRE: `%s': using `^' as the first character\n\
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000224of a basic regular expression is not portable; it is being ignored", pv->u.s);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000225 }
226
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000227 memset(&re_buffer, 0, sizeof(re_buffer));
228 memset(re_regs, 0, sizeof(*re_regs));
Bernhard Reutner-Fischer8025afa2007-04-02 16:54:41 +0000229 xregcomp(&re_buffer, pv->u.s, 0);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000230
Rob Landley540d3f62005-05-09 21:42:42 +0000231 /* expr uses an anchored pattern match, so check that there was a
232 * match and that the match starts at offset 0. */
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000233 if (regexec(&re_buffer, sv->u.s, NMATCH, re_regs, 0) != REG_NOMATCH &&
234 re_regs[0].rm_so == 0) {
Eric Andersen1b355eb2000-09-05 17:37:48 +0000235 /* Were \(...\) used? */
Rob Landley540d3f62005-05-09 21:42:42 +0000236 if (re_buffer.re_nsub > 0) {
237 sv->u.s[re_regs[1].rm_eo] = '\0';
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000238 v = str_value(sv->u.s + re_regs[1].rm_so);
239 } else
240 v = int_value(re_regs[0].rm_eo);
241 } else {
Eric Andersen1b355eb2000-09-05 17:37:48 +0000242 /* Match failed -- return the right kind of null. */
243 if (re_buffer.re_nsub > 0)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000244 v = str_value("");
Eric Andersen1b355eb2000-09-05 17:37:48 +0000245 else
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000246 v = int_value(0);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000247 }
Bernhard Reutner-Fischer8025afa2007-04-02 16:54:41 +0000248//FIXME: sounds like here is a bit missing: regfree(&re_buffer);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000249 return v;
250}
251
252/* Handle bare operands and ( expr ) syntax. */
253
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000254static VALUE *eval7(void)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000255{
256 VALUE *v;
257
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000258 if (!*G.args)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000259 bb_error_msg_and_die("syntax error");
Eric Andersen1b355eb2000-09-05 17:37:48 +0000260
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000261 if (nextarg("(")) {
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000262 G.args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000263 v = eval();
264 if (!nextarg(")"))
265 bb_error_msg_and_die("syntax error");
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000266 G.args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000267 return v;
268 }
Eric Andersen1b355eb2000-09-05 17:37:48 +0000269
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000270 if (nextarg(")"))
271 bb_error_msg_and_die("syntax error");
Eric Andersen1b355eb2000-09-05 17:37:48 +0000272
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000273 return str_value(*G.args++);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000274}
275
276/* Handle match, substr, index, length, and quote keywords. */
277
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000278static VALUE *eval6(void)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000279{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000280 static const char keywords[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000281 "quote\0""length\0""match\0""index\0""substr\0";
Eric Andersen1b355eb2000-09-05 17:37:48 +0000282
Denis Vlasenkoc6753c12007-07-01 18:33:35 +0000283 VALUE *r, *i1, *i2;
284 VALUE *l = l; /* silence gcc */
285 VALUE *v = v; /* silence gcc */
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000286 int key = *G.args ? index_in_strings(keywords, *G.args) + 1 : 0;
Denis Vlasenkoc6753c12007-07-01 18:33:35 +0000287
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000288 if (key == 0) /* not a keyword */
289 return eval7();
290 G.args++; /* We have a valid token, so get the next argument. */
291 if (key == 1) { /* quote */
292 if (!*G.args)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000293 bb_error_msg_and_die("syntax error");
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000294 return str_value(*G.args++);
295 }
296 if (key == 2) { /* length */
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000297 r = eval6();
298 tostring(r);
299 v = int_value(strlen(r->u.s));
300 freev(r);
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000301 } else
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000302 l = eval6();
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000303
304 if (key == 3) { /* match */
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000305 r = eval6();
306 v = docolon(l, r);
307 freev(l);
308 freev(r);
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000309 }
310 if (key == 4) { /* index */
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000311 r = eval6();
312 tostring(l);
313 tostring(r);
314 v = int_value(strcspn(l->u.s, r->u.s) + 1);
315 if (v->u.i == (arith_t) strlen(l->u.s) + 1)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000316 v->u.i = 0;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000317 freev(l);
318 freev(r);
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000319 }
320 if (key == 5) { /* substr */
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000321 i1 = eval6();
322 i2 = eval6();
323 tostring(l);
324 if (!toarith(i1) || !toarith(i2)
Denis Vlasenkoc6753c12007-07-01 18:33:35 +0000325 || i1->u.i > (arith_t) strlen(l->u.s)
326 || i1->u.i <= 0 || i2->u.i <= 0)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000327 v = str_value("");
Eric Andersen1b355eb2000-09-05 17:37:48 +0000328 else {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000329 v = xmalloc(sizeof(VALUE));
Eric Andersen1b355eb2000-09-05 17:37:48 +0000330 v->type = string;
Rob Landleyd921b2e2006-08-03 15:41:12 +0000331 v->u.s = xstrndup(l->u.s + i1->u.i - 1, i2->u.i);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000332 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000333 freev(l);
334 freev(i1);
335 freev(i2);
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000336 }
337 return v;
338
Eric Andersen1b355eb2000-09-05 17:37:48 +0000339}
340
341/* Handle : operator (pattern matching).
342 Calls docolon to do the real work. */
343
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000344static VALUE *eval5(void)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000345{
346 VALUE *l, *r, *v;
347
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000348 l = eval6();
349 while (nextarg(":")) {
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000350 G.args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000351 r = eval6();
352 v = docolon(l, r);
353 freev(l);
354 freev(r);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000355 l = v;
356 }
357 return l;
358}
359
360/* Handle *, /, % operators. */
361
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000362static VALUE *eval4(void)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000363{
364 VALUE *l, *r;
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +0000365 int op;
366 arith_t val;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000367
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000368 l = eval5();
Eric Andersen1b355eb2000-09-05 17:37:48 +0000369 while (1) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000370 if (nextarg("*"))
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000371 op = '*';
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000372 else if (nextarg("/"))
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000373 op = '/';
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000374 else if (nextarg("%"))
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000375 op = '%';
Eric Andersen1b355eb2000-09-05 17:37:48 +0000376 else
377 return l;
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000378 G.args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000379 r = eval5();
380 val = arithmetic_common(l, r, op);
381 freev(l);
382 freev(r);
383 l = int_value(val);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000384 }
385}
386
387/* Handle +, - operators. */
388
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000389static VALUE *eval3(void)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000390{
391 VALUE *l, *r;
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +0000392 int op;
393 arith_t val;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000394
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000395 l = eval4();
Eric Andersen1b355eb2000-09-05 17:37:48 +0000396 while (1) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000397 if (nextarg("+"))
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000398 op = '+';
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000399 else if (nextarg("-"))
Glenn L McGrath07f6b952003-09-08 23:19:12 +0000400 op = '-';
Eric Andersen1b355eb2000-09-05 17:37:48 +0000401 else
402 return l;
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000403 G.args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000404 r = eval4();
405 val = arithmetic_common(l, r, op);
406 freev(l);
407 freev(r);
408 l = int_value(val);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000409 }
410}
411
412/* Handle comparisons. */
413
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000414static VALUE *eval2(void)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000415{
416 VALUE *l, *r;
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +0000417 int op;
418 arith_t val;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000419
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000420 l = eval3();
Eric Andersen1b355eb2000-09-05 17:37:48 +0000421 while (1) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000422 if (nextarg("<"))
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000423 op = '<';
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000424 else if (nextarg("<="))
425 op = 'L' + 'E';
426 else if (nextarg("=") || nextarg("=="))
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000427 op = '=';
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000428 else if (nextarg("!="))
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000429 op = '!';
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000430 else if (nextarg(">="))
431 op = 'G' + 'E';
432 else if (nextarg(">"))
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000433 op = '>';
Eric Andersen1b355eb2000-09-05 17:37:48 +0000434 else
435 return l;
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000436 G.args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000437 r = eval3();
438 toarith(l);
439 toarith(r);
440 val = cmp_common(l, r, op);
441 freev(l);
442 freev(r);
443 l = int_value(val);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000444 }
445}
446
447/* Handle &. */
448
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000449static VALUE *eval1(void)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000450{
451 VALUE *l, *r;
452
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000453 l = eval2();
454 while (nextarg("&")) {
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000455 G.args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000456 r = eval2();
457 if (null(l) || null(r)) {
458 freev(l);
459 freev(r);
460 l = int_value(0);
461 } else
462 freev(r);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000463 }
464 return l;
465}
466
467/* Handle |. */
468
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000469static VALUE *eval(void)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000470{
471 VALUE *l, *r;
472
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000473 l = eval1();
474 while (nextarg("|")) {
Bernhard Reutner-Fischeradb01b12007-04-02 16:38:13 +0000475 G.args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000476 r = eval1();
477 if (null(l)) {
478 freev(l);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000479 l = r;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000480 } else
481 freev(r);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000482 }
483 return l;
484}
Bernhard Reutner-Fischer3c5929a2007-04-02 16:41:24 +0000485
486int expr_main(int argc, char **argv);
487int expr_main(int argc, char **argv)
488{
489 VALUE *v;
490
491 if (argc == 1) {
492 bb_error_msg_and_die("too few arguments");
493 }
494
495 G.args = argv + 1;
496
497 v = eval();
498 if (*G.args)
499 bb_error_msg_and_die("syntax error");
500
501 if (v->type == integer)
502 printf("%" PF_REZ "d\n", PF_REZ_TYPE v->u.i);
503 else
504 puts(v->u.s);
505
506 fflush_stdout_and_exit(null(v));
507}
508
509