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>.
11 * this program is free software; you can redistribute it and/or modify
12 * it under the terms of the gnu general public license as published by
13 * the free software foundation; either version 2 of the license, or
14 * (at your option) any later version.
16 * this program is distributed in the hope that it will be useful,
17 * but without any warranty; without even the implied warranty of
18 * merchantability or fitness for a particular purpose. see the gnu
19 * general public license for more details.
21 * you should have received a copy of the gnu general public license
22 * along with this program; if not, write to the free software
23 * foundation, inc., 59 temple place, suite 330, boston, ma 02111-1307 usa
27 /* This program evaluates expressions. Each token (operator, operand,
28 * parenthesis) of the expression must be a seperate argument. The
29 * parser used is a reasonably general one, though any incarnation of
30 * it is language-specific. It is especially nice for expressions.
32 * No parse tree is needed; a new node is evaluated immediately.
33 * One function can handle multiple operators all of equal precedence,
34 * provided they all associate ((x op x) op x). */
36 /* no getopt needed */
42 #include <sys/types.h>
46 /* The kinds of value we can have. */
51 typedef enum valtype TYPE;
55 TYPE type; /* Which kind. */
56 union { /* The value itself. */
61 typedef struct valinfo VALUE;
63 /* The arguments given to the program, minus the program name. */
66 static VALUE *docolon (VALUE *sv, VALUE *pv);
67 static VALUE *eval (void);
68 static VALUE *int_value (int i);
69 static VALUE *str_value (char *s);
70 static int nextarg (char *str);
71 static int null (VALUE *v);
72 static int toarith (VALUE *v);
73 static void freev (VALUE *v);
74 static void tostring (VALUE *v);
76 int expr_main (int argc, char **argv)
81 error_msg_and_die("too few arguments");
88 error_msg_and_die ("syntax error");
90 if (v->type == integer)
91 printf ("%d\n", v->u.i);
98 /* Return a VALUE for I. */
100 static VALUE *int_value (int i)
104 v = xmalloc (sizeof(VALUE));
110 /* Return a VALUE for S. */
112 static VALUE *str_value (char *s)
116 v = xmalloc (sizeof(VALUE));
122 /* Free VALUE V, including structure components. */
124 static void freev (VALUE *v)
126 if (v->type == string)
131 /* Return nonzero if V is a null-string or zero-number. */
133 static int null (VALUE *v)
139 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_asprintf (&(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)
166 /* Don't interpret the empty string as an integer. */
179 /* Return nonzero if the next token matches STR exactly.
180 STR must not be NULL. */
187 return strcmp (*args, str) == 0;
190 /* The comparison operator handling functions. */
192 #define cmpf(name, rel) \
193 static int name (VALUE *l, VALUE *r) \
195 if (l->type == string || r->type == string) { \
198 return strcmp (l->u.s, r->u.s) rel 0; \
201 return l->u.i rel r->u.i; \
204 cmpf (less_equal, <=)
207 cmpf (greater_equal, >=)
208 cmpf (greater_than, >)
212 /* The arithmetic operator handling functions. */
214 #define arithf(name, op) \
216 int name (VALUE *l, VALUE *r) \
218 if (!toarith (l) || !toarith (r)) \
219 error_msg_and_die ("non-numeric argument"); \
220 return l->u.i op r->u.i; \
223 #define arithdivf(name, op) \
224 static int name (VALUE *l, VALUE *r) \
226 if (!toarith (l) || !toarith (r)) \
227 error_msg_and_die ( "non-numeric argument"); \
229 error_msg_and_die ( "division by zero"); \
230 return l->u.i op r->u.i; \
236 arithdivf (divide, /)
242 /* Do the : operator.
243 SV is the VALUE for the lhs (the string),
244 PV is the VALUE for the rhs (the pattern). */
246 static VALUE *docolon (VALUE *sv, VALUE *pv)
250 struct re_pattern_buffer re_buffer;
251 struct re_registers re_regs;
257 if (pv->u.s[0] == '^') {
259 warning: unportable BRE: `%s': using `^' as the first character\n\
260 of a basic regular expression is not portable; it is being ignored",
264 len = strlen (pv->u.s);
265 memset (&re_buffer, 0, sizeof (re_buffer));
266 memset (&re_regs, 0, sizeof (re_regs));
267 re_buffer.allocated = 2 * len;
268 re_buffer.buffer = (unsigned char *) xmalloc (re_buffer.allocated);
269 re_buffer.translate = 0;
270 re_syntax_options = RE_SYNTAX_POSIX_BASIC;
271 errmsg = re_compile_pattern (pv->u.s, len, &re_buffer);
273 error_msg_and_die("%s", errmsg);
276 len = re_match (&re_buffer, sv->u.s, strlen (sv->u.s), 0, &re_regs);
278 /* Were \(...\) used? */
279 if (re_buffer.re_nsub > 0) { /* was (re_regs.start[1] >= 0) */
280 sv->u.s[re_regs.end[1]] = '\0';
281 v = str_value (sv->u.s + re_regs.start[1]);
287 /* Match failed -- return the right kind of null. */
288 if (re_buffer.re_nsub > 0)
293 free (re_buffer.buffer);
297 /* Handle bare operands and ( expr ) syntax. */
299 static VALUE *eval7 (void)
304 error_msg_and_die ( "syntax error");
310 error_msg_and_die ( "syntax error");
316 error_msg_and_die ( "syntax error");
318 return str_value (*args++);
321 /* Handle match, substr, index, length, and quote keywords. */
323 static VALUE *eval6 (void)
325 VALUE *l, *r, *v, *i1, *i2;
327 if (nextarg ("quote")) {
330 error_msg_and_die ( "syntax error");
331 return str_value (*args++);
333 else if (nextarg ("length")) {
337 v = int_value (strlen (r->u.s));
341 else if (nextarg ("match")) {
350 else if (nextarg ("index")) {
356 v = int_value (strcspn (l->u.s, r->u.s) + 1);
357 if (v->u.i == (int) strlen (l->u.s) + 1)
363 else if (nextarg ("substr")) {
369 if (!toarith (i1) || !toarith (i2)
370 || i1->u.i > (int) strlen (l->u.s)
371 || i1->u.i <= 0 || i2->u.i <= 0)
374 v = xmalloc (sizeof(VALUE));
376 v->u.s = xstrndup(l->u.s + i1->u.i - 1, i2->u.i);
387 /* Handle : operator (pattern matching).
388 Calls docolon to do the real work. */
390 static VALUE *eval5 (void)
395 while (nextarg (":")) {
406 /* Handle *, /, % operators. */
408 static VALUE *eval4 (void)
411 int (*fxn) (VALUE *, VALUE *), val;
417 else if (nextarg ("/"))
419 else if (nextarg ("%"))
432 /* Handle +, - operators. */
434 static VALUE *eval3 (void)
437 int (*fxn) (VALUE *, VALUE *), val;
443 else if (nextarg ("-"))
456 /* Handle comparisons. */
458 static VALUE *eval2 (void)
461 int (*fxn) (VALUE *, VALUE *), val;
467 else if (nextarg ("<="))
469 else if (nextarg ("=") || nextarg ("=="))
471 else if (nextarg ("!="))
473 else if (nextarg (">="))
475 else if (nextarg (">"))
492 static VALUE *eval1 (void)
497 while (nextarg ("&")) {
500 if (null (l) || null (r)) {
513 static VALUE *eval (void)
518 while (nextarg ("|")) {