blob: 19147344609fe8ed0e2af00713f428e6d7634ab4 [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
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. */
65static char **args;
66
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000067static VALUE *docolon(VALUE * sv, VALUE * pv);
68static VALUE *eval(void);
69static VALUE *int_value(arith_t i);
70static VALUE *str_value(char *s);
71static int nextarg(char *str);
72static int null(VALUE * v);
73static int toarith(VALUE * v);
74static void freev(VALUE * v);
75static void tostring(VALUE * v);
Eric Andersen1b355eb2000-09-05 17:37:48 +000076
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000077int expr_main(int argc, char **argv)
Eric Andersen1b355eb2000-09-05 17:37:48 +000078{
79 VALUE *v;
80
81 if (argc == 1) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000082 bb_error_msg_and_die("too few arguments");
Eric Andersen1b355eb2000-09-05 17:37:48 +000083 }
84
85 args = argv + 1;
86
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000087 v = eval();
Eric Andersen1b355eb2000-09-05 17:37:48 +000088 if (*args)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000089 bb_error_msg_and_die("syntax error");
Eric Andersen1b355eb2000-09-05 17:37:48 +000090
91 if (v->type == integer)
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000092 printf("%" PF_REZ "d\n", PF_REZ_TYPE v->u.i);
Matt Kraai59df6f72001-05-16 14:21:09 +000093 else
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000094 puts(v->u.s);
Eric Andersen1b355eb2000-09-05 17:37:48 +000095
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000096 fflush_stdout_and_exit(null(v));
Eric Andersen1b355eb2000-09-05 17:37:48 +000097}
98
99/* Return a VALUE for I. */
100
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000101static VALUE *int_value(arith_t i)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000102{
103 VALUE *v;
104
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000105 v = xmalloc(sizeof(VALUE));
Eric Andersen1b355eb2000-09-05 17:37:48 +0000106 v->type = integer;
107 v->u.i = i;
108 return v;
109}
110
111/* Return a VALUE for S. */
112
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000113static VALUE *str_value(char *s)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000114{
115 VALUE *v;
116
Rob Landleyd921b2e2006-08-03 15:41:12 +0000117 v = xmalloc(sizeof(VALUE));
Eric Andersen1b355eb2000-09-05 17:37:48 +0000118 v->type = string;
Rob Landleyd921b2e2006-08-03 15:41:12 +0000119 v->u.s = xstrdup(s);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000120 return v;
121}
122
123/* Free VALUE V, including structure components. */
124
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000125static void freev(VALUE * v)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000126{
127 if (v->type == string)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000128 free(v->u.s);
129 free(v);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000130}
131
132/* Return nonzero if V is a null-string or zero-number. */
133
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000134static int null(VALUE * v)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000135{
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000136 if (v->type == integer)
137 return v->u.i == 0;
138 else /* string: */
139 return v->u.s[0] == '\0' || strcmp(v->u.s, "0") == 0;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000140}
141
142/* Coerce V to a string value (can't fail). */
143
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000144static void tostring(VALUE * v)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000145{
Eric Andersen1b355eb2000-09-05 17:37:48 +0000146 if (v->type == integer) {
Rob Landleyd921b2e2006-08-03 15:41:12 +0000147 v->u.s = xasprintf("%" PF_REZ "d", PF_REZ_TYPE v->u.i);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000148 v->type = string;
149 }
150}
151
152/* Coerce V to an integer value. Return 1 on success, 0 on failure. */
153
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000154static int toarith(VALUE * v)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000155{
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000156 if (v->type == string) {
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +0000157 arith_t i;
Manuel Novoa III 70183852004-01-25 19:47:10 +0000158 char *e;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000159
Manuel Novoa III 70183852004-01-25 19:47:10 +0000160 /* Don't interpret the empty string as an integer. */
161 /* Currently does not worry about overflow or int/long differences. */
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +0000162 i = STRTOL(v->u.s, &e, 10);
Manuel Novoa III 70183852004-01-25 19:47:10 +0000163 if ((v->u.s == e) || *e)
164 return 0;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000165 free(v->u.s);
Manuel Novoa III 70183852004-01-25 19:47:10 +0000166 v->u.i = i;
167 v->type = integer;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000168 }
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000169 return 1;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000170}
171
172/* Return nonzero if the next token matches STR exactly.
173 STR must not be NULL. */
174
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000175static int nextarg(char *str)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000176{
177 if (*args == NULL)
178 return 0;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000179 return strcmp(*args, str) == 0;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000180}
181
182/* The comparison operator handling functions. */
183
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000184static int cmp_common(VALUE * l, VALUE * r, int op)
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000185{
186 int cmpval;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000187
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000188 if (l->type == string || r->type == string) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000189 tostring(l);
190 tostring(r);
191 cmpval = strcmp(l->u.s, r->u.s);
192 } else
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000193 cmpval = l->u.i - r->u.i;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000194 if (op == '<')
195 return cmpval < 0;
196 else if (op == ('L' + 'E'))
197 return cmpval <= 0;
198 else if (op == '=')
199 return cmpval == 0;
200 else if (op == '!')
201 return cmpval != 0;
202 else if (op == '>')
203 return cmpval > 0;
204 else /* >= */
205 return cmpval >= 0;
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000206}
Eric Andersen1b355eb2000-09-05 17:37:48 +0000207
208/* The arithmetic operator handling functions. */
209
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000210static arith_t arithmetic_common(VALUE * l, VALUE * r, int op)
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000211{
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000212 arith_t li, ri;
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000213
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000214 if (!toarith(l) || !toarith(r))
215 bb_error_msg_and_die("non-numeric argument");
216 li = l->u.i;
217 ri = r->u.i;
218 if ((op == '/' || op == '%') && ri == 0)
219 bb_error_msg_and_die("division by zero");
220 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 if (op == '/')
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000227 return li / ri;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000228 else
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000229 return li % ri;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000230}
231
Eric Andersen1b355eb2000-09-05 17:37:48 +0000232/* Do the : operator.
233 SV is the VALUE for the lhs (the string),
234 PV is the VALUE for the rhs (the pattern). */
235
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000236static VALUE *docolon(VALUE * sv, VALUE * pv)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000237{
238 VALUE *v;
Rob Landley540d3f62005-05-09 21:42:42 +0000239 regex_t re_buffer;
240 const int NMATCH = 2;
241 regmatch_t re_regs[NMATCH];
Eric Andersen1b355eb2000-09-05 17:37:48 +0000242
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000243 tostring(sv);
244 tostring(pv);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000245
246 if (pv->u.s[0] == '^') {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000247 fprintf(stderr, "\
Eric Andersen1b355eb2000-09-05 17:37:48 +0000248warning: unportable BRE: `%s': using `^' as the first character\n\
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000249of a basic regular expression is not portable; it is being ignored", pv->u.s);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000250 }
251
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000252 memset(&re_buffer, 0, sizeof(re_buffer));
253 memset(re_regs, 0, sizeof(*re_regs));
254 if (regcomp(&re_buffer, pv->u.s, 0) != 0)
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +0000255 bb_error_msg_and_die("invalid regular expression");
Eric Andersen1b355eb2000-09-05 17:37:48 +0000256
Rob Landley540d3f62005-05-09 21:42:42 +0000257 /* expr uses an anchored pattern match, so check that there was a
258 * match and that the match starts at offset 0. */
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000259 if (regexec(&re_buffer, sv->u.s, NMATCH, re_regs, 0) != REG_NOMATCH &&
260 re_regs[0].rm_so == 0) {
Eric Andersen1b355eb2000-09-05 17:37:48 +0000261 /* Were \(...\) used? */
Rob Landley540d3f62005-05-09 21:42:42 +0000262 if (re_buffer.re_nsub > 0) {
263 sv->u.s[re_regs[1].rm_eo] = '\0';
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000264 v = str_value(sv->u.s + re_regs[1].rm_so);
265 } else
266 v = int_value(re_regs[0].rm_eo);
267 } else {
Eric Andersen1b355eb2000-09-05 17:37:48 +0000268 /* Match failed -- return the right kind of null. */
269 if (re_buffer.re_nsub > 0)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000270 v = str_value("");
Eric Andersen1b355eb2000-09-05 17:37:48 +0000271 else
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000272 v = int_value(0);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000273 }
Eric Andersen1b355eb2000-09-05 17:37:48 +0000274 return v;
275}
276
277/* Handle bare operands and ( expr ) syntax. */
278
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000279static VALUE *eval7(void)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000280{
281 VALUE *v;
282
283 if (!*args)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000284 bb_error_msg_and_die("syntax error");
Eric Andersen1b355eb2000-09-05 17:37:48 +0000285
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000286 if (nextarg("(")) {
Eric Andersen1b355eb2000-09-05 17:37:48 +0000287 args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000288 v = eval();
289 if (!nextarg(")"))
290 bb_error_msg_and_die("syntax error");
291 args++;
292 return v;
293 }
Eric Andersen1b355eb2000-09-05 17:37:48 +0000294
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000295 if (nextarg(")"))
296 bb_error_msg_and_die("syntax error");
Eric Andersen1b355eb2000-09-05 17:37:48 +0000297
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000298 return str_value(*args++);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000299}
300
301/* Handle match, substr, index, length, and quote keywords. */
302
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000303static VALUE *eval6(void)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000304{
305 VALUE *l, *r, *v, *i1, *i2;
306
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000307 if (nextarg("quote")) {
Eric Andersen1b355eb2000-09-05 17:37:48 +0000308 args++;
309 if (!*args)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000310 bb_error_msg_and_die("syntax error");
311 return str_value(*args++);
312 } else if (nextarg("length")) {
Eric Andersen1b355eb2000-09-05 17:37:48 +0000313 args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000314 r = eval6();
315 tostring(r);
316 v = int_value(strlen(r->u.s));
317 freev(r);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000318 return v;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000319 } else if (nextarg("match")) {
Eric Andersen1b355eb2000-09-05 17:37:48 +0000320 args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000321 l = eval6();
322 r = eval6();
323 v = docolon(l, r);
324 freev(l);
325 freev(r);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000326 return v;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000327 } else if (nextarg("index")) {
Eric Andersen1b355eb2000-09-05 17:37:48 +0000328 args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000329 l = eval6();
330 r = eval6();
331 tostring(l);
332 tostring(r);
333 v = int_value(strcspn(l->u.s, r->u.s) + 1);
334 if (v->u.i == (arith_t) strlen(l->u.s) + 1)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000335 v->u.i = 0;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000336 freev(l);
337 freev(r);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000338 return v;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000339 } else if (nextarg("substr")) {
Eric Andersen1b355eb2000-09-05 17:37:48 +0000340 args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000341 l = eval6();
342 i1 = eval6();
343 i2 = eval6();
344 tostring(l);
345 if (!toarith(i1) || !toarith(i2)
346 || i1->u.i > (arith_t) strlen(l->u.s)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000347 || i1->u.i <= 0 || i2->u.i <= 0)
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000348 v = str_value("");
Eric Andersen1b355eb2000-09-05 17:37:48 +0000349 else {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000350 v = xmalloc(sizeof(VALUE));
Eric Andersen1b355eb2000-09-05 17:37:48 +0000351 v->type = string;
Rob Landleyd921b2e2006-08-03 15:41:12 +0000352 v->u.s = xstrndup(l->u.s + i1->u.i - 1, i2->u.i);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000353 }
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000354 freev(l);
355 freev(i1);
356 freev(i2);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000357 return v;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000358 } else
359 return eval7();
Eric Andersen1b355eb2000-09-05 17:37:48 +0000360}
361
362/* Handle : operator (pattern matching).
363 Calls docolon to do the real work. */
364
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000365static VALUE *eval5(void)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000366{
367 VALUE *l, *r, *v;
368
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000369 l = eval6();
370 while (nextarg(":")) {
Eric Andersen1b355eb2000-09-05 17:37:48 +0000371 args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000372 r = eval6();
373 v = docolon(l, r);
374 freev(l);
375 freev(r);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000376 l = v;
377 }
378 return l;
379}
380
381/* Handle *, /, % operators. */
382
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000383static VALUE *eval4(void)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000384{
385 VALUE *l, *r;
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +0000386 int op;
387 arith_t val;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000388
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000389 l = eval5();
Eric Andersen1b355eb2000-09-05 17:37:48 +0000390 while (1) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000391 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 = '/';
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000395 else if (nextarg("%"))
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000396 op = '%';
Eric Andersen1b355eb2000-09-05 17:37:48 +0000397 else
398 return l;
399 args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000400 r = eval5();
401 val = arithmetic_common(l, r, op);
402 freev(l);
403 freev(r);
404 l = int_value(val);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000405 }
406}
407
408/* Handle +, - operators. */
409
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000410static VALUE *eval3(void)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000411{
412 VALUE *l, *r;
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +0000413 int op;
414 arith_t val;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000415
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000416 l = eval4();
Eric Andersen1b355eb2000-09-05 17:37:48 +0000417 while (1) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000418 if (nextarg("+"))
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000419 op = '+';
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000420 else if (nextarg("-"))
Glenn L McGrath07f6b952003-09-08 23:19:12 +0000421 op = '-';
Eric Andersen1b355eb2000-09-05 17:37:48 +0000422 else
423 return l;
424 args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000425 r = eval4();
426 val = arithmetic_common(l, r, op);
427 freev(l);
428 freev(r);
429 l = int_value(val);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000430 }
431}
432
433/* Handle comparisons. */
434
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000435static VALUE *eval2(void)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000436{
437 VALUE *l, *r;
"Vladimir N. Oleynik"8aa9e572006-01-25 13:56:03 +0000438 int op;
439 arith_t val;
Eric Andersen1b355eb2000-09-05 17:37:48 +0000440
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000441 l = eval3();
Eric Andersen1b355eb2000-09-05 17:37:48 +0000442 while (1) {
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000443 if (nextarg("<"))
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000444 op = '<';
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000445 else if (nextarg("<="))
446 op = 'L' + 'E';
447 else if (nextarg("=") || nextarg("=="))
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000448 op = '=';
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000449 else if (nextarg("!="))
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000450 op = '!';
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000451 else if (nextarg(">="))
452 op = 'G' + 'E';
453 else if (nextarg(">"))
Glenn L McGrath7b8765c2003-08-29 07:29:30 +0000454 op = '>';
Eric Andersen1b355eb2000-09-05 17:37:48 +0000455 else
456 return l;
457 args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000458 r = eval3();
459 toarith(l);
460 toarith(r);
461 val = cmp_common(l, r, op);
462 freev(l);
463 freev(r);
464 l = int_value(val);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000465 }
466}
467
468/* Handle &. */
469
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000470static VALUE *eval1(void)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000471{
472 VALUE *l, *r;
473
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000474 l = eval2();
475 while (nextarg("&")) {
Eric Andersen1b355eb2000-09-05 17:37:48 +0000476 args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000477 r = eval2();
478 if (null(l) || null(r)) {
479 freev(l);
480 freev(r);
481 l = int_value(0);
482 } else
483 freev(r);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000484 }
485 return l;
486}
487
488/* Handle |. */
489
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000490static VALUE *eval(void)
Eric Andersen1b355eb2000-09-05 17:37:48 +0000491{
492 VALUE *l, *r;
493
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000494 l = eval1();
495 while (nextarg("|")) {
Eric Andersen1b355eb2000-09-05 17:37:48 +0000496 args++;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000497 r = eval1();
498 if (null(l)) {
499 freev(l);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000500 l = r;
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +0000501 } else
502 freev(r);
Eric Andersen1b355eb2000-09-05 17:37:48 +0000503 }
504 return l;
505}