blob: e7fdc5f1e55364f35938ba2bc4accfc160e5314d [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
Eric Andersencbe31da2001-02-20 06:14:08 +000028#include "busybox.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
Eric Andersen1b355eb2000-09-05 17:37:48 +000052/* A value is.... */
53struct valinfo {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000054 TYPE type; /* Which kind. */
55 union { /* The value itself. */
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +000056 arith_t i;
Eric Andersen1b355eb2000-09-05 17:37:48 +000057 char *s;
58 } u;
59};
60typedef struct valinfo VALUE;
61
62/* The arguments given to the program, minus the program name. */
63static char **args;
64
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000065static VALUE *docolon(VALUE * sv, VALUE * pv);
66static VALUE *eval(void);
67static VALUE *int_value(arith_t i);
68static VALUE *str_value(char *s);
69static int nextarg(char *str);
70static int null(VALUE * v);
71static int toarith(VALUE * v);
72static void freev(VALUE * v);
73static void tostring(VALUE * v);
Eric Andersen1b355eb2000-09-05 17:37:48 +000074
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000075int expr_main(int argc, char **argv)
Eric Andersen1b355eb2000-09-05 17:37:48 +000076{
77 VALUE *v;
78
79 if (argc == 1) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000080 bb_error_msg_and_die("too few arguments");
Eric Andersen1b355eb2000-09-05 17:37:48 +000081 }
82
83 args = argv + 1;
84
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000085 v = eval();
Eric Andersen1b355eb2000-09-05 17:37:48 +000086 if (*args)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000087 bb_error_msg_and_die("syntax error");
Eric Andersen1b355eb2000-09-05 17:37:48 +000088
89 if (v->type == integer)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000090 bb_printf("%" PF_REZ "d\n", PF_REZ_TYPE v->u.i);
Matt Kraai59df6f72001-05-16 14:21:09 +000091 else
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000092 puts(v->u.s);
Eric Andersen1b355eb2000-09-05 17:37:48 +000093
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000094 exit(null(v));
Eric Andersen1b355eb2000-09-05 17:37:48 +000095}
96
97/* Return a VALUE for I. */
98
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000099static VALUE *int_value(arith_t i)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000100{
101 VALUE *v;
102
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000103 v = xmalloc(sizeof(VALUE));
Eric Andersen1b355eb2000-09-05 17:37:48 +0000104 v->type = integer;
105 v->u.i = i;
106 return v;
107}
108
109/* Return a VALUE for S. */
110
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000111static VALUE *str_value(char *s)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000112{
113 VALUE *v;
114
Rob Landleyd921b2e2006-08-03 15:41:12 +0000115 v = xmalloc(sizeof(VALUE));
Eric Andersen1b355eb2000-09-05 17:37:48 +0000116 v->type = string;
Rob Landleyd921b2e2006-08-03 15:41:12 +0000117 v->u.s = xstrdup(s);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000118 return v;
119}
120
121/* Free VALUE V, including structure components. */
122
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000123static void freev(VALUE * v)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000124{
125 if (v->type == string)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000126 free(v->u.s);
127 free(v);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000128}
129
130/* Return nonzero if V is a null-string or zero-number. */
131
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000132static int null(VALUE * v)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000133{
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000134 if (v->type == integer)
135 return v->u.i == 0;
136 else /* string: */
137 return v->u.s[0] == '\0' || strcmp(v->u.s, "0") == 0;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000138}
139
140/* Coerce V to a string value (can't fail). */
141
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000142static void tostring(VALUE * v)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000143{
Eric Andersen1b355eb2000-09-05 17:37:48 +0000144 if (v->type == integer) {
Rob Landleyd921b2e2006-08-03 15:41:12 +0000145 v->u.s = xasprintf("%" PF_REZ "d", PF_REZ_TYPE v->u.i);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000146 v->type = string;
147 }
148}
149
150/* Coerce V to an integer value. Return 1 on success, 0 on failure. */
151
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000152static int toarith(VALUE * v)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000153{
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000154 if (v->type == string) {
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +0000155 arith_t i;
Manuel Novoa III 70183852004-01-25 19:47:10 +0000156 char *e;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000157
Manuel Novoa III 70183852004-01-25 19:47:10 +0000158 /* Don't interpret the empty string as an integer. */
159 /* Currently does not worry about overflow or int/long differences. */
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +0000160 i = STRTOL(v->u.s, &e, 10);
Manuel Novoa III 70183852004-01-25 19:47:10 +0000161 if ((v->u.s == e) || *e)
162 return 0;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000163 free(v->u.s);
Manuel Novoa III 70183852004-01-25 19:47:10 +0000164 v->u.i = i;
165 v->type = integer;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000166 }
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000167 return 1;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000168}
169
170/* Return nonzero if the next token matches STR exactly.
171 STR must not be NULL. */
172
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000173static int nextarg(char *str)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000174{
175 if (*args == NULL)
176 return 0;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000177 return strcmp(*args, str) == 0;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000178}
179
180/* The comparison operator handling functions. */
181
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000182static int cmp_common(VALUE * l, VALUE * r, int op)
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000183{
184 int cmpval;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000185
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000186 if (l->type == string || r->type == string) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000187 tostring(l);
188 tostring(r);
189 cmpval = strcmp(l->u.s, r->u.s);
190 } else
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000191 cmpval = l->u.i - r->u.i;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000192 if (op == '<')
193 return cmpval < 0;
194 else if (op == ('L' + 'E'))
195 return cmpval <= 0;
196 else if (op == '=')
197 return cmpval == 0;
198 else if (op == '!')
199 return cmpval != 0;
200 else if (op == '>')
201 return cmpval > 0;
202 else /* >= */
203 return cmpval >= 0;
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000204}
Eric Andersen1b355eb2000-09-05 17:37:48 +0000205
206/* The arithmetic operator handling functions. */
207
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000208static arith_t arithmetic_common(VALUE * l, VALUE * r, int op)
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000209{
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000210 arith_t li, ri;
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000211
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000212 if (!toarith(l) || !toarith(r))
213 bb_error_msg_and_die("non-numeric argument");
214 li = l->u.i;
215 ri = r->u.i;
216 if ((op == '/' || op == '%') && ri == 0)
217 bb_error_msg_and_die("division by zero");
218 if (op == '+')
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000219 return li + ri;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000220 else if (op == '-')
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000221 return li - ri;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000222 else if (op == '*')
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000223 return li * ri;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000224 else if (op == '/')
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000225 return li / ri;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000226 else
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000227 return li % ri;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000228}
229
Eric Andersen1b355eb2000-09-05 17:37:48 +0000230/* Do the : operator.
231 SV is the VALUE for the lhs (the string),
232 PV is the VALUE for the rhs (the pattern). */
233
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000234static VALUE *docolon(VALUE * sv, VALUE * pv)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000235{
236 VALUE *v;
Rob Landley540d3f62005-05-09 21:42:42 +0000237 regex_t re_buffer;
238 const int NMATCH = 2;
239 regmatch_t re_regs[NMATCH];
Eric Andersen1b355eb2000-09-05 17:37:48 +0000240
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000241 tostring(sv);
242 tostring(pv);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000243
244 if (pv->u.s[0] == '^') {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000245 fprintf(stderr, "\
Eric Andersen1b355eb2000-09-05 17:37:48 +0000246warning: unportable BRE: `%s': using `^' as the first character\n\
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000247of a basic regular expression is not portable; it is being ignored", pv->u.s);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000248 }
249
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000250 memset(&re_buffer, 0, sizeof(re_buffer));
251 memset(re_regs, 0, sizeof(*re_regs));
252 if (regcomp(&re_buffer, pv->u.s, 0) != 0)
Rob Landley540d3f62005-05-09 21:42:42 +0000253 bb_error_msg_and_die("Invalid regular expression");
Eric Andersen1b355eb2000-09-05 17:37:48 +0000254
Rob Landley540d3f62005-05-09 21:42:42 +0000255 /* expr uses an anchored pattern match, so check that there was a
256 * match and that the match starts at offset 0. */
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000257 if (regexec(&re_buffer, sv->u.s, NMATCH, re_regs, 0) != REG_NOMATCH &&
258 re_regs[0].rm_so == 0) {
Eric Andersen1b355eb2000-09-05 17:37:48 +0000259 /* Were \(...\) used? */
Rob Landley540d3f62005-05-09 21:42:42 +0000260 if (re_buffer.re_nsub > 0) {
261 sv->u.s[re_regs[1].rm_eo] = '\0';
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000262 v = str_value(sv->u.s + re_regs[1].rm_so);
263 } else
264 v = int_value(re_regs[0].rm_eo);
265 } else {
Eric Andersen1b355eb2000-09-05 17:37:48 +0000266 /* Match failed -- return the right kind of null. */
267 if (re_buffer.re_nsub > 0)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000268 v = str_value("");
Eric Andersen1b355eb2000-09-05 17:37:48 +0000269 else
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000270 v = int_value(0);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000271 }
Eric Andersen1b355eb2000-09-05 17:37:48 +0000272 return v;
273}
274
275/* Handle bare operands and ( expr ) syntax. */
276
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000277static VALUE *eval7(void)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000278{
279 VALUE *v;
280
281 if (!*args)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000282 bb_error_msg_and_die("syntax error");
Eric Andersen1b355eb2000-09-05 17:37:48 +0000283
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000284 if (nextarg("(")) {
Eric Andersen1b355eb2000-09-05 17:37:48 +0000285 args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000286 v = eval();
287 if (!nextarg(")"))
288 bb_error_msg_and_die("syntax error");
289 args++;
290 return v;
291 }
Eric Andersen1b355eb2000-09-05 17:37:48 +0000292
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000293 if (nextarg(")"))
294 bb_error_msg_and_die("syntax error");
Eric Andersen1b355eb2000-09-05 17:37:48 +0000295
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000296 return str_value(*args++);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000297}
298
299/* Handle match, substr, index, length, and quote keywords. */
300
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000301static VALUE *eval6(void)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000302{
303 VALUE *l, *r, *v, *i1, *i2;
304
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000305 if (nextarg("quote")) {
Eric Andersen1b355eb2000-09-05 17:37:48 +0000306 args++;
307 if (!*args)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000308 bb_error_msg_and_die("syntax error");
309 return str_value(*args++);
310 } else if (nextarg("length")) {
Eric Andersen1b355eb2000-09-05 17:37:48 +0000311 args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000312 r = eval6();
313 tostring(r);
314 v = int_value(strlen(r->u.s));
315 freev(r);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000316 return v;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000317 } else if (nextarg("match")) {
Eric Andersen1b355eb2000-09-05 17:37:48 +0000318 args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000319 l = eval6();
320 r = eval6();
321 v = docolon(l, r);
322 freev(l);
323 freev(r);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000324 return v;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000325 } else if (nextarg("index")) {
Eric Andersen1b355eb2000-09-05 17:37:48 +0000326 args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000327 l = eval6();
328 r = eval6();
329 tostring(l);
330 tostring(r);
331 v = int_value(strcspn(l->u.s, r->u.s) + 1);
332 if (v->u.i == (arith_t) strlen(l->u.s) + 1)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000333 v->u.i = 0;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000334 freev(l);
335 freev(r);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000336 return v;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000337 } else if (nextarg("substr")) {
Eric Andersen1b355eb2000-09-05 17:37:48 +0000338 args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000339 l = eval6();
340 i1 = eval6();
341 i2 = eval6();
342 tostring(l);
343 if (!toarith(i1) || !toarith(i2)
344 || i1->u.i > (arith_t) strlen(l->u.s)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000345 || i1->u.i <= 0 || i2->u.i <= 0)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000346 v = str_value("");
Eric Andersen1b355eb2000-09-05 17:37:48 +0000347 else {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000348 v = xmalloc(sizeof(VALUE));
Eric Andersen1b355eb2000-09-05 17:37:48 +0000349 v->type = string;
Rob Landleyd921b2e2006-08-03 15:41:12 +0000350 v->u.s = xstrndup(l->u.s + i1->u.i - 1, i2->u.i);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000351 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000352 freev(l);
353 freev(i1);
354 freev(i2);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000355 return v;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000356 } else
357 return eval7();
Eric Andersen1b355eb2000-09-05 17:37:48 +0000358}
359
360/* Handle : operator (pattern matching).
361 Calls docolon to do the real work. */
362
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000363static VALUE *eval5(void)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000364{
365 VALUE *l, *r, *v;
366
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000367 l = eval6();
368 while (nextarg(":")) {
Eric Andersen1b355eb2000-09-05 17:37:48 +0000369 args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000370 r = eval6();
371 v = docolon(l, r);
372 freev(l);
373 freev(r);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000374 l = v;
375 }
376 return l;
377}
378
379/* Handle *, /, % operators. */
380
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000381static VALUE *eval4(void)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000382{
383 VALUE *l, *r;
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +0000384 int op;
385 arith_t val;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000386
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000387 l = eval5();
Eric Andersen1b355eb2000-09-05 17:37:48 +0000388 while (1) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000389 if (nextarg("*"))
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000390 op = '*';
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000391 else if (nextarg("/"))
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000392 op = '/';
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000393 else if (nextarg("%"))
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000394 op = '%';
Eric Andersen1b355eb2000-09-05 17:37:48 +0000395 else
396 return l;
397 args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000398 r = eval5();
399 val = arithmetic_common(l, r, op);
400 freev(l);
401 freev(r);
402 l = int_value(val);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000403 }
404}
405
406/* Handle +, - operators. */
407
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000408static VALUE *eval3(void)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000409{
410 VALUE *l, *r;
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +0000411 int op;
412 arith_t val;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000413
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000414 l = eval4();
Eric Andersen1b355eb2000-09-05 17:37:48 +0000415 while (1) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000416 if (nextarg("+"))
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000417 op = '+';
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000418 else if (nextarg("-"))
Glenn L McGrath07f6b952003-09-08 23:19:12 +0000419 op = '-';
Eric Andersen1b355eb2000-09-05 17:37:48 +0000420 else
421 return l;
422 args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000423 r = eval4();
424 val = arithmetic_common(l, r, op);
425 freev(l);
426 freev(r);
427 l = int_value(val);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000428 }
429}
430
431/* Handle comparisons. */
432
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000433static VALUE *eval2(void)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000434{
435 VALUE *l, *r;
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +0000436 int op;
437 arith_t val;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000438
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000439 l = eval3();
Eric Andersen1b355eb2000-09-05 17:37:48 +0000440 while (1) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000441 if (nextarg("<"))
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000442 op = '<';
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000443 else if (nextarg("<="))
444 op = 'L' + 'E';
445 else if (nextarg("=") || nextarg("=="))
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000446 op = '=';
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000447 else if (nextarg("!="))
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000448 op = '!';
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000449 else if (nextarg(">="))
450 op = 'G' + 'E';
451 else if (nextarg(">"))
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000452 op = '>';
Eric Andersen1b355eb2000-09-05 17:37:48 +0000453 else
454 return l;
455 args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000456 r = eval3();
457 toarith(l);
458 toarith(r);
459 val = cmp_common(l, r, op);
460 freev(l);
461 freev(r);
462 l = int_value(val);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000463 }
464}
465
466/* Handle &. */
467
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000468static VALUE *eval1(void)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000469{
470 VALUE *l, *r;
471
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000472 l = eval2();
473 while (nextarg("&")) {
Eric Andersen1b355eb2000-09-05 17:37:48 +0000474 args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000475 r = eval2();
476 if (null(l) || null(r)) {
477 freev(l);
478 freev(r);
479 l = int_value(0);
480 } else
481 freev(r);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000482 }
483 return l;
484}
485
486/* Handle |. */
487
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000488static VALUE *eval(void)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000489{
490 VALUE *l, *r;
491
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000492 l = eval1();
493 while (nextarg("|")) {
Eric Andersen1b355eb2000-09-05 17:37:48 +0000494 args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000495 r = eval1();
496 if (null(l)) {
497 freev(l);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000498 l = r;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000499 } else
500 freev(r);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000501 }
502 return l;
503}