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)
151 if (v->type == integer) {
152 temp = xmalloc (4 * (sizeof (int) / sizeof (char)));
153 sprintf (temp, "%d", v->u.i);
159 /* Coerce V to an integer value. Return 1 on success, 0 on failure. */
161 static int toarith (VALUE *v)
170 /* Don't interpret the empty string as an integer. */
183 /* Return nonzero if the next token matches STR exactly.
184 STR must not be NULL. */
191 return strcmp (*args, str) == 0;
194 /* The comparison operator handling functions. */
196 #define cmpf(name, rel) \
197 static int name (l, r) VALUE *l; VALUE *r; \
199 if (l->type == string || r->type == string) { \
202 return strcmp (l->u.s, r->u.s) rel 0; \
205 return l->u.i rel r->u.i; \
208 cmpf (less_equal, <=)
211 cmpf (greater_equal, >=)
212 cmpf (greater_than, >)
216 /* The arithmetic operator handling functions. */
218 #define arithf(name, op) \
220 int name (l, r) VALUE *l; VALUE *r; \
222 if (!toarith (l) || !toarith (r)) \
223 error_msg_and_die ("non-numeric argument"); \
224 return l->u.i op r->u.i; \
227 #define arithdivf(name, op) \
228 static int name (l, r) VALUE *l; VALUE *r; \
230 if (!toarith (l) || !toarith (r)) \
231 error_msg_and_die ( "non-numeric argument"); \
233 error_msg_and_die ( "division by zero"); \
234 return l->u.i op r->u.i; \
240 arithdivf (divide, /)
246 /* Do the : operator.
247 SV is the VALUE for the lhs (the string),
248 PV is the VALUE for the rhs (the pattern). */
250 static VALUE *docolon (VALUE *sv, VALUE *pv)
254 struct re_pattern_buffer re_buffer;
255 struct re_registers re_regs;
261 if (pv->u.s[0] == '^') {
263 warning: unportable BRE: `%s': using `^' as the first character\n\
264 of a basic regular expression is not portable; it is being ignored",
268 len = strlen (pv->u.s);
269 memset (&re_buffer, 0, sizeof (re_buffer));
270 memset (&re_regs, 0, sizeof (re_regs));
271 re_buffer.allocated = 2 * len;
272 re_buffer.buffer = (unsigned char *) xmalloc (re_buffer.allocated);
273 re_buffer.translate = 0;
274 re_syntax_options = RE_SYNTAX_POSIX_BASIC;
275 errmsg = re_compile_pattern (pv->u.s, len, &re_buffer);
277 error_msg_and_die("%s", errmsg);
280 len = re_match (&re_buffer, sv->u.s, strlen (sv->u.s), 0, &re_regs);
282 /* Were \(...\) used? */
283 if (re_buffer.re_nsub > 0) { /* was (re_regs.start[1] >= 0) */
284 sv->u.s[re_regs.end[1]] = '\0';
285 v = str_value (sv->u.s + re_regs.start[1]);
291 /* Match failed -- return the right kind of null. */
292 if (re_buffer.re_nsub > 0)
297 free (re_buffer.buffer);
301 /* Handle bare operands and ( expr ) syntax. */
303 static VALUE *eval7 (void)
308 error_msg_and_die ( "syntax error");
314 error_msg_and_die ( "syntax error");
320 error_msg_and_die ( "syntax error");
322 return str_value (*args++);
325 /* Handle match, substr, index, length, and quote keywords. */
327 static VALUE *eval6 (void)
329 VALUE *l, *r, *v, *i1, *i2;
331 if (nextarg ("quote")) {
334 error_msg_and_die ( "syntax error");
335 return str_value (*args++);
337 else if (nextarg ("length")) {
341 v = int_value (strlen (r->u.s));
345 else if (nextarg ("match")) {
354 else if (nextarg ("index")) {
360 v = int_value (strcspn (l->u.s, r->u.s) + 1);
361 if (v->u.i == (int) strlen (l->u.s) + 1)
367 else if (nextarg ("substr")) {
373 if (!toarith (i1) || !toarith (i2)
374 || i1->u.i > (int) strlen (l->u.s)
375 || i1->u.i <= 0 || i2->u.i <= 0)
378 v = xmalloc (sizeof(VALUE));
380 v->u.s = strncpy ((char *) xmalloc (i2->u.i + 1),
381 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)
423 else if (nextarg ("/"))
425 else if (nextarg ("%"))
438 /* Handle +, - operators. */
440 static VALUE *eval3 (void)
449 else if (nextarg ("-"))
462 /* Handle comparisons. */
464 static VALUE *eval2 (void)
473 else if (nextarg ("<="))
475 else if (nextarg ("=") || nextarg ("=="))
477 else if (nextarg ("!="))
479 else if (nextarg (">="))
481 else if (nextarg (">"))
498 static VALUE *eval1 (void)
503 while (nextarg ("&")) {
506 if (null (l) || null (r)) {
519 static VALUE *eval (void)
524 while (nextarg ("|")) {