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). */
38 #include <sys/types.h>
42 /* The kinds of value we can have. */
47 typedef enum valtype TYPE;
51 TYPE type; /* Which kind. */
52 union { /* The value itself. */
57 typedef struct valinfo VALUE;
59 /* The arguments given to the program, minus the program name. */
62 static VALUE *docolon (VALUE *sv, VALUE *pv);
63 static VALUE *eval (void);
64 static VALUE *int_value (int i);
65 static VALUE *str_value (char *s);
66 static int nextarg (char *str);
67 static int null (VALUE *v);
68 static int toarith (VALUE *v);
69 static void freev (VALUE *v);
70 static void tostring (VALUE *v);
72 int expr_main (int argc, char **argv)
77 error_msg_and_die("too few arguments\n");
84 error_msg_and_die ("syntax error\n");
86 if (v->type == integer)
87 printf ("%d\n", v->u.i);
89 printf ("%s\n", v->u.s);
94 /* Return a VALUE for I. */
96 static VALUE *int_value (int i)
100 v = xmalloc (sizeof(VALUE));
106 /* Return a VALUE for S. */
108 static VALUE *str_value (char *s)
112 v = xmalloc (sizeof(VALUE));
118 /* Free VALUE V, including structure components. */
120 static void freev (VALUE *v)
122 if (v->type == string)
127 /* Return nonzero if V is a null-string or zero-number. */
129 static int null (VALUE *v)
135 return v->u.s[0] == '\0' || strcmp (v->u.s, "0") == 0;
141 /* Coerce V to a string value (can't fail). */
143 static void tostring (VALUE *v)
147 if (v->type == integer) {
148 temp = xmalloc (4 * (sizeof (int) / sizeof (char)));
149 sprintf (temp, "%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 (l, r) 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 (l, r) VALUE *l; VALUE *r; \
218 if (!toarith (l) || !toarith (r)) \
219 error_msg_and_die ("non-numeric argument\n"); \
220 return l->u.i op r->u.i; \
223 #define arithdivf(name, op) \
224 int name (l, r) VALUE *l; VALUE *r; \
226 if (!toarith (l) || !toarith (r)) \
227 error_msg_and_die ( "non-numeric argument\n"); \
229 error_msg_and_die ( "division by zero\n"); \
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\n", 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\n");
310 error_msg_and_die ( "syntax error\n");
316 error_msg_and_die ( "syntax error\n");
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\n");
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 = strncpy ((char *) xmalloc (i2->u.i + 1),
377 l->u.s + i1->u.i - 1, i2->u.i);
389 /* Handle : operator (pattern matching).
390 Calls docolon to do the real work. */
392 static VALUE *eval5 (void)
397 while (nextarg (":")) {
408 /* Handle *, /, % operators. */
410 static VALUE *eval4 (void)
419 else if (nextarg ("/"))
421 else if (nextarg ("%"))
434 /* Handle +, - operators. */
436 static VALUE *eval3 (void)
445 else if (nextarg ("-"))
458 /* Handle comparisons. */
460 static VALUE *eval2 (void)
469 else if (nextarg ("<="))
471 else if (nextarg ("=") || nextarg ("=="))
473 else if (nextarg ("!="))
475 else if (nextarg (">="))
477 else if (nextarg (">"))
494 static VALUE *eval1 (void)
499 while (nextarg ("&")) {
502 if (null (l) || null (r)) {
515 static VALUE *eval (void)
520 while (nextarg ("|")) {