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 * Aug 2003 Vladimir Oleynik - reduced 464 bytes.
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 /* This program evaluates expressions. Each token (operator, operand,
29 * parenthesis) of the expression must be a separate argument. The
30 * parser used is a reasonably general one, though any incarnation of
31 * it is language-specific. It is especially nice for expressions.
33 * No parse tree is needed; a new node is evaluated immediately.
34 * One function can handle multiple operators all of equal precedence,
35 * provided they all associate ((x op x) op x). */
37 /* no getopt needed */
43 #include <sys/types.h>
48 /* The kinds of value we can have. */
53 typedef enum valtype TYPE;
57 TYPE type; /* Which kind. */
58 union { /* The value itself. */
63 typedef struct valinfo VALUE;
65 /* The arguments given to the program, minus the program name. */
68 static VALUE *docolon (VALUE *sv, VALUE *pv);
69 static VALUE *eval (void);
70 static VALUE *int_value (int i);
71 static VALUE *str_value (char *s);
72 static int nextarg (char *str);
73 static int null (VALUE *v);
74 static int toarith (VALUE *v);
75 static void freev (VALUE *v);
76 static void tostring (VALUE *v);
78 int expr_main (int argc, char **argv)
83 bb_error_msg_and_die("too few arguments");
90 bb_error_msg_and_die ("syntax error");
92 if (v->type == integer)
93 printf ("%d\n", v->u.i);
100 /* Return a VALUE for I. */
102 static VALUE *int_value (int i)
106 v = xmalloc (sizeof(VALUE));
112 /* Return a VALUE for S. */
114 static VALUE *str_value (char *s)
118 v = xmalloc (sizeof(VALUE));
120 v->u.s = bb_xstrdup (s);
124 /* Free VALUE V, including structure components. */
126 static void freev (VALUE *v)
128 if (v->type == string)
133 /* Return nonzero if V is a null-string or zero-number. */
135 static int null (VALUE *v)
140 default: /* string: */
141 return v->u.s[0] == '\0' || strcmp (v->u.s, "0") == 0;
145 /* Coerce V to a string value (can't fail). */
147 static void tostring (VALUE *v)
149 if (v->type == integer) {
150 v->u.s = bb_xasprintf ("%d", v->u.i);
155 /* Coerce V to an integer value. Return 1 on success, 0 on failure. */
157 static int toarith (VALUE *v)
159 if(v->type == string) {
163 /* Don't interpret the empty string as an integer. */
164 /* Currently does not worry about overflow or int/long differences. */
165 i = (int) strtol(v->u.s, &e, 10);
166 if ((v->u.s == e) || *e)
175 /* Return nonzero if the next token matches STR exactly.
176 STR must not be NULL. */
183 return strcmp (*args, str) == 0;
186 /* The comparison operator handling functions. */
188 static int cmp_common (VALUE *l, VALUE *r, int op)
192 if (l->type == string || r->type == string) {
195 cmpval = strcmp (l->u.s, r->u.s);
198 cmpval = l->u.i - r->u.i;
215 /* The arithmetic operator handling functions. */
217 static int arithmetic_common (VALUE *l, VALUE *r, int op)
221 if (!toarith (l) || !toarith (r))
222 bb_error_msg_and_die ("non-numeric argument");
225 if((op == '/' || op == '%') && ri == 0)
226 bb_error_msg_and_die ( "division by zero");
241 /* Do the : operator.
242 SV is the VALUE for the lhs (the string),
243 PV is the VALUE for the rhs (the pattern). */
245 static VALUE *docolon (VALUE *sv, VALUE *pv)
249 const int NMATCH = 2;
250 regmatch_t re_regs[NMATCH];
255 if (pv->u.s[0] == '^') {
257 warning: unportable BRE: `%s': using `^' as the first character\n\
258 of a basic regular expression is not portable; it is being ignored",
262 memset (&re_buffer, 0, sizeof (re_buffer));
263 memset (re_regs, 0, sizeof (*re_regs));
264 if( regcomp (&re_buffer, pv->u.s, 0) != 0 )
265 bb_error_msg_and_die("Invalid regular expression");
267 /* expr uses an anchored pattern match, so check that there was a
268 * match and that the match starts at offset 0. */
269 if (regexec (&re_buffer, sv->u.s, NMATCH, re_regs, 0) != REG_NOMATCH &&
270 re_regs[0].rm_so == 0) {
271 /* Were \(...\) used? */
272 if (re_buffer.re_nsub > 0) {
273 sv->u.s[re_regs[1].rm_eo] = '\0';
274 v = str_value (sv->u.s + re_regs[1].rm_so);
277 v = int_value (re_regs[0].rm_eo);
280 /* Match failed -- return the right kind of null. */
281 if (re_buffer.re_nsub > 0)
289 /* Handle bare operands and ( expr ) syntax. */
291 static VALUE *eval7 (void)
296 bb_error_msg_and_die ( "syntax error");
302 bb_error_msg_and_die ( "syntax error");
308 bb_error_msg_and_die ( "syntax error");
310 return str_value (*args++);
313 /* Handle match, substr, index, length, and quote keywords. */
315 static VALUE *eval6 (void)
317 VALUE *l, *r, *v, *i1, *i2;
319 if (nextarg ("quote")) {
322 bb_error_msg_and_die ( "syntax error");
323 return str_value (*args++);
325 else if (nextarg ("length")) {
329 v = int_value (strlen (r->u.s));
333 else if (nextarg ("match")) {
342 else if (nextarg ("index")) {
348 v = int_value (strcspn (l->u.s, r->u.s) + 1);
349 if (v->u.i == (int) strlen (l->u.s) + 1)
355 else if (nextarg ("substr")) {
361 if (!toarith (i1) || !toarith (i2)
362 || i1->u.i > (int) strlen (l->u.s)
363 || i1->u.i <= 0 || i2->u.i <= 0)
366 v = xmalloc (sizeof(VALUE));
368 v->u.s = bb_xstrndup(l->u.s + i1->u.i - 1, i2->u.i);
379 /* Handle : operator (pattern matching).
380 Calls docolon to do the real work. */
382 static VALUE *eval5 (void)
387 while (nextarg (":")) {
398 /* Handle *, /, % operators. */
400 static VALUE *eval4 (void)
409 else if (nextarg ("/"))
411 else if (nextarg ("%"))
417 val = arithmetic_common (l, r, op);
424 /* Handle +, - operators. */
426 static VALUE *eval3 (void)
435 else if (nextarg ("-"))
441 val = arithmetic_common (l, r, op);
448 /* Handle comparisons. */
450 static VALUE *eval2 (void)
459 else if (nextarg ("<="))
461 else if (nextarg ("=") || nextarg ("=="))
463 else if (nextarg ("!="))
465 else if (nextarg (">="))
467 else if (nextarg (">"))
475 val = cmp_common (l, r, op);
484 static VALUE *eval1 (void)
489 while (nextarg ("&")) {
492 if (null (l) || null (r)) {
505 static VALUE *eval (void)
510 while (nextarg ("|")) {