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 seperate 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));
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) {
162 /* Don't interpret the empty string as an integer. */
173 /* Return nonzero if the next token matches STR exactly.
174 STR must not be NULL. */
181 return strcmp (*args, str) == 0;
184 /* The comparison operator handling functions. */
186 static int cmp_common (VALUE *l, VALUE *r, int op)
190 if (l->type == string || r->type == string) {
193 cmpval = strcmp (l->u.s, r->u.s);
196 cmpval = l->u.i - r->u.i;
213 /* The arithmetic operator handling functions. */
215 static int arithmetic_common (VALUE *l, VALUE *r, int op)
219 if (!toarith (l) || !toarith (r))
220 bb_error_msg_and_die ("non-numeric argument");
223 if((op == '/' || op == '%') && ri == 0)
224 bb_error_msg_and_die ( "division by zero");
239 /* Do the : operator.
240 SV is the VALUE for the lhs (the string),
241 PV is the VALUE for the rhs (the pattern). */
243 static VALUE *docolon (VALUE *sv, VALUE *pv)
247 struct re_pattern_buffer re_buffer;
248 struct re_registers re_regs;
254 if (pv->u.s[0] == '^') {
256 warning: unportable BRE: `%s': using `^' as the first character\n\
257 of a basic regular expression is not portable; it is being ignored",
261 len = strlen (pv->u.s);
262 memset (&re_buffer, 0, sizeof (re_buffer));
263 memset (&re_regs, 0, sizeof (re_regs));
264 re_buffer.allocated = 2 * len;
265 re_buffer.buffer = (unsigned char *) xmalloc (re_buffer.allocated);
266 re_buffer.translate = 0;
267 re_syntax_options = RE_SYNTAX_POSIX_BASIC;
268 errmsg = re_compile_pattern (pv->u.s, len, &re_buffer);
270 bb_error_msg_and_die("%s", errmsg);
273 len = re_match (&re_buffer, sv->u.s, strlen (sv->u.s), 0, &re_regs);
275 /* Were \(...\) used? */
276 if (re_buffer.re_nsub > 0) { /* was (re_regs.start[1] >= 0) */
277 sv->u.s[re_regs.end[1]] = '\0';
278 v = str_value (sv->u.s + re_regs.start[1]);
284 /* Match failed -- return the right kind of null. */
285 if (re_buffer.re_nsub > 0)
290 free (re_buffer.buffer);
294 /* Handle bare operands and ( expr ) syntax. */
296 static VALUE *eval7 (void)
301 bb_error_msg_and_die ( "syntax error");
307 bb_error_msg_and_die ( "syntax error");
313 bb_error_msg_and_die ( "syntax error");
315 return str_value (*args++);
318 /* Handle match, substr, index, length, and quote keywords. */
320 static VALUE *eval6 (void)
322 VALUE *l, *r, *v, *i1, *i2;
324 if (nextarg ("quote")) {
327 bb_error_msg_and_die ( "syntax error");
328 return str_value (*args++);
330 else if (nextarg ("length")) {
334 v = int_value (strlen (r->u.s));
338 else if (nextarg ("match")) {
347 else if (nextarg ("index")) {
353 v = int_value (strcspn (l->u.s, r->u.s) + 1);
354 if (v->u.i == (int) strlen (l->u.s) + 1)
360 else if (nextarg ("substr")) {
366 if (!toarith (i1) || !toarith (i2)
367 || i1->u.i > (int) strlen (l->u.s)
368 || i1->u.i <= 0 || i2->u.i <= 0)
371 v = xmalloc (sizeof(VALUE));
373 v->u.s = bb_xstrndup(l->u.s + i1->u.i - 1, i2->u.i);
384 /* Handle : operator (pattern matching).
385 Calls docolon to do the real work. */
387 static VALUE *eval5 (void)
392 while (nextarg (":")) {
403 /* Handle *, /, % operators. */
405 static VALUE *eval4 (void)
414 else if (nextarg ("/"))
416 else if (nextarg ("%"))
422 val = arithmetic_common (l, r, op);
429 /* Handle +, - operators. */
431 static VALUE *eval3 (void)
440 else if (nextarg ("-"))
446 val = arithmetic_common (l, r, op);
453 /* Handle comparisons. */
455 static VALUE *eval2 (void)
464 else if (nextarg ("<="))
466 else if (nextarg ("=") || nextarg ("=="))
468 else if (nextarg ("!="))
470 else if (nextarg (">="))
472 else if (nextarg (">"))
480 val = cmp_common (l, r, op);
489 static VALUE *eval1 (void)
494 while (nextarg ("&")) {
497 if (null (l) || null (r)) {
510 static VALUE *eval (void)
515 while (nextarg ("|")) {