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 bb_xasprintf (&(v->u.s), "%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 struct re_pattern_buffer re_buffer;
250 struct re_registers re_regs;
256 if (pv->u.s[0] == '^') {
258 warning: unportable BRE: `%s': using `^' as the first character\n\
259 of a basic regular expression is not portable; it is being ignored",
263 len = strlen (pv->u.s);
264 memset (&re_buffer, 0, sizeof (re_buffer));
265 memset (&re_regs, 0, sizeof (re_regs));
266 re_buffer.allocated = 2 * len;
267 re_buffer.buffer = (unsigned char *) xmalloc (re_buffer.allocated);
268 re_buffer.translate = 0;
269 re_syntax_options = RE_SYNTAX_POSIX_BASIC;
270 errmsg = re_compile_pattern (pv->u.s, len, &re_buffer);
272 bb_error_msg_and_die("%s", errmsg);
275 len = re_match (&re_buffer, sv->u.s, strlen (sv->u.s), 0, &re_regs);
277 /* Were \(...\) used? */
278 if (re_buffer.re_nsub > 0) { /* was (re_regs.start[1] >= 0) */
279 sv->u.s[re_regs.end[1]] = '\0';
280 v = str_value (sv->u.s + re_regs.start[1]);
286 /* Match failed -- return the right kind of null. */
287 if (re_buffer.re_nsub > 0)
292 free (re_buffer.buffer);
296 /* Handle bare operands and ( expr ) syntax. */
298 static VALUE *eval7 (void)
303 bb_error_msg_and_die ( "syntax error");
309 bb_error_msg_and_die ( "syntax error");
315 bb_error_msg_and_die ( "syntax error");
317 return str_value (*args++);
320 /* Handle match, substr, index, length, and quote keywords. */
322 static VALUE *eval6 (void)
324 VALUE *l, *r, *v, *i1, *i2;
326 if (nextarg ("quote")) {
329 bb_error_msg_and_die ( "syntax error");
330 return str_value (*args++);
332 else if (nextarg ("length")) {
336 v = int_value (strlen (r->u.s));
340 else if (nextarg ("match")) {
349 else if (nextarg ("index")) {
355 v = int_value (strcspn (l->u.s, r->u.s) + 1);
356 if (v->u.i == (int) strlen (l->u.s) + 1)
362 else if (nextarg ("substr")) {
368 if (!toarith (i1) || !toarith (i2)
369 || i1->u.i > (int) strlen (l->u.s)
370 || i1->u.i <= 0 || i2->u.i <= 0)
373 v = xmalloc (sizeof(VALUE));
375 v->u.s = bb_xstrndup(l->u.s + i1->u.i - 1, i2->u.i);
386 /* Handle : operator (pattern matching).
387 Calls docolon to do the real work. */
389 static VALUE *eval5 (void)
394 while (nextarg (":")) {
405 /* Handle *, /, % operators. */
407 static VALUE *eval4 (void)
416 else if (nextarg ("/"))
418 else if (nextarg ("%"))
424 val = arithmetic_common (l, r, op);
431 /* Handle +, - operators. */
433 static VALUE *eval3 (void)
442 else if (nextarg ("-"))
448 val = arithmetic_common (l, r, op);
455 /* Handle comparisons. */
457 static VALUE *eval2 (void)
466 else if (nextarg ("<="))
468 else if (nextarg ("=") || nextarg ("=="))
470 else if (nextarg ("!="))
472 else if (nextarg (">="))
474 else if (nextarg (">"))
482 val = cmp_common (l, r, op);
491 static VALUE *eval1 (void)
496 while (nextarg ("&")) {
499 if (null (l) || null (r)) {
512 static VALUE *eval (void)
517 while (nextarg ("|")) {