1 /* vi: set sw=4 ts=4: */
3 * Mini expr implementation for busybox
5 * based on GNU expr Mike Parker.
6 * Copyright (C) 86, 1991-1997, 1999 Free Software Foundation, Inc.
8 * Busybox modifications
9 * Copyright (c) 2000 Edward Betts <edward@debian.org>.
10 * Copyright (C) 2003-2005 Vladimir Oleynik <dzo@simtreas.ru>
11 * - reduced 464 bytes.
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 /* This program evaluates expressions. Each token (operator, operand,
31 * parenthesis) of the expression must be a separate argument. The
32 * parser used is a reasonably general one, though any incarnation of
33 * it is language-specific. It is especially nice for expressions.
35 * No parse tree is needed; a new node is evaluated immediately.
36 * One function can handle multiple operators all of equal precedence,
37 * provided they all associate ((x op x) op x). */
39 /* no getopt needed */
45 #include <sys/types.h>
50 /* The kinds of value we can have. */
55 typedef enum valtype TYPE;
57 #if ENABLE_EXPR_MATH_SUPPORT_64
58 typedef int64_t arith_t;
60 #define PF_REZ_TYPE (long long)
61 #define STRTOL(s, e, b) strtoll(s, e, b)
65 #define PF_REZ_TYPE (long)
66 #define STRTOL(s, e, b) strtol(s, e, b)
71 TYPE type; /* Which kind. */
72 union { /* The value itself. */
77 typedef struct valinfo VALUE;
79 /* The arguments given to the program, minus the program name. */
82 static VALUE *docolon (VALUE *sv, VALUE *pv);
83 static VALUE *eval (void);
84 static VALUE *int_value (arith_t i);
85 static VALUE *str_value (char *s);
86 static int nextarg (char *str);
87 static int null (VALUE *v);
88 static int toarith (VALUE *v);
89 static void freev (VALUE *v);
90 static void tostring (VALUE *v);
92 int expr_main (int argc, char **argv)
97 bb_error_msg_and_die("too few arguments");
104 bb_error_msg_and_die ("syntax error");
106 if (v->type == integer)
107 printf ("%" PF_REZ "d\n", PF_REZ_TYPE v->u.i);
114 /* Return a VALUE for I. */
116 static VALUE *int_value (arith_t i)
120 v = xmalloc (sizeof(VALUE));
126 /* Return a VALUE for S. */
128 static VALUE *str_value (char *s)
132 v = xmalloc (sizeof(VALUE));
134 v->u.s = bb_xstrdup (s);
138 /* Free VALUE V, including structure components. */
140 static void freev (VALUE *v)
142 if (v->type == string)
147 /* Return nonzero if V is a null-string or zero-number. */
149 static int null (VALUE *v)
154 default: /* string: */
155 return v->u.s[0] == '\0' || strcmp (v->u.s, "0") == 0;
159 /* Coerce V to a string value (can't fail). */
161 static void tostring (VALUE *v)
163 if (v->type == integer) {
164 v->u.s = bb_xasprintf ("%" PF_REZ "d", PF_REZ_TYPE v->u.i);
169 /* Coerce V to an integer value. Return 1 on success, 0 on failure. */
171 static int toarith (VALUE *v)
173 if(v->type == string) {
177 /* Don't interpret the empty string as an integer. */
178 /* Currently does not worry about overflow or int/long differences. */
179 i = STRTOL(v->u.s, &e, 10);
180 if ((v->u.s == e) || *e)
189 /* Return nonzero if the next token matches STR exactly.
190 STR must not be NULL. */
197 return strcmp (*args, str) == 0;
200 /* The comparison operator handling functions. */
202 static int cmp_common (VALUE *l, VALUE *r, int op)
206 if (l->type == string || r->type == string) {
209 cmpval = strcmp (l->u.s, r->u.s);
212 cmpval = l->u.i - r->u.i;
229 /* The arithmetic operator handling functions. */
231 static arith_t arithmetic_common (VALUE *l, VALUE *r, int op)
235 if (!toarith (l) || !toarith (r))
236 bb_error_msg_and_die ("non-numeric argument");
239 if((op == '/' || op == '%') && ri == 0)
240 bb_error_msg_and_die ( "division by zero");
255 /* Do the : operator.
256 SV is the VALUE for the lhs (the string),
257 PV is the VALUE for the rhs (the pattern). */
259 static VALUE *docolon (VALUE *sv, VALUE *pv)
263 const int NMATCH = 2;
264 regmatch_t re_regs[NMATCH];
269 if (pv->u.s[0] == '^') {
271 warning: unportable BRE: `%s': using `^' as the first character\n\
272 of a basic regular expression is not portable; it is being ignored",
276 memset (&re_buffer, 0, sizeof (re_buffer));
277 memset (re_regs, 0, sizeof (*re_regs));
278 if( regcomp (&re_buffer, pv->u.s, 0) != 0 )
279 bb_error_msg_and_die("Invalid regular expression");
281 /* expr uses an anchored pattern match, so check that there was a
282 * match and that the match starts at offset 0. */
283 if (regexec (&re_buffer, sv->u.s, NMATCH, re_regs, 0) != REG_NOMATCH &&
284 re_regs[0].rm_so == 0) {
285 /* Were \(...\) used? */
286 if (re_buffer.re_nsub > 0) {
287 sv->u.s[re_regs[1].rm_eo] = '\0';
288 v = str_value (sv->u.s + re_regs[1].rm_so);
291 v = int_value (re_regs[0].rm_eo);
294 /* Match failed -- return the right kind of null. */
295 if (re_buffer.re_nsub > 0)
303 /* Handle bare operands and ( expr ) syntax. */
305 static VALUE *eval7 (void)
310 bb_error_msg_and_die ( "syntax error");
316 bb_error_msg_and_die ( "syntax error");
322 bb_error_msg_and_die ( "syntax error");
324 return str_value (*args++);
327 /* Handle match, substr, index, length, and quote keywords. */
329 static VALUE *eval6 (void)
331 VALUE *l, *r, *v, *i1, *i2;
333 if (nextarg ("quote")) {
336 bb_error_msg_and_die ( "syntax error");
337 return str_value (*args++);
339 else if (nextarg ("length")) {
343 v = int_value (strlen (r->u.s));
347 else if (nextarg ("match")) {
356 else if (nextarg ("index")) {
362 v = int_value (strcspn (l->u.s, r->u.s) + 1);
363 if (v->u.i == (arith_t) strlen (l->u.s) + 1)
369 else if (nextarg ("substr")) {
375 if (!toarith (i1) || !toarith (i2)
376 || i1->u.i > (arith_t) strlen (l->u.s)
377 || i1->u.i <= 0 || i2->u.i <= 0)
380 v = xmalloc (sizeof(VALUE));
382 v->u.s = bb_xstrndup(l->u.s + i1->u.i - 1, i2->u.i);
393 /* Handle : operator (pattern matching).
394 Calls docolon to do the real work. */
396 static VALUE *eval5 (void)
401 while (nextarg (":")) {
412 /* Handle *, /, % operators. */
414 static VALUE *eval4 (void)
424 else if (nextarg ("/"))
426 else if (nextarg ("%"))
432 val = arithmetic_common (l, r, op);
439 /* Handle +, - operators. */
441 static VALUE *eval3 (void)
451 else if (nextarg ("-"))
457 val = arithmetic_common (l, r, op);
464 /* Handle comparisons. */
466 static VALUE *eval2 (void)
476 else if (nextarg ("<="))
478 else if (nextarg ("=") || nextarg ("=="))
480 else if (nextarg ("!="))
482 else if (nextarg (">="))
484 else if (nextarg (">"))
492 val = cmp_common (l, r, op);
501 static VALUE *eval1 (void)
506 while (nextarg ("&")) {
509 if (null (l) || null (r)) {
522 static VALUE *eval (void)
527 while (nextarg ("|")) {