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). */
40 #include <sys/types.h>
44 /* The kinds of value we can have. */
49 typedef enum valtype TYPE;
53 TYPE type; /* Which kind. */
54 union { /* The value itself. */
59 typedef struct valinfo VALUE;
61 /* The arguments given to the program, minus the program name. */
64 static VALUE *docolon (VALUE *sv, VALUE *pv);
65 static VALUE *eval (void);
66 static VALUE *int_value (int i);
67 static VALUE *str_value (char *s);
68 static int nextarg (char *str);
69 static int null (VALUE *v);
70 static int toarith (VALUE *v);
71 static void freev (VALUE *v);
72 static void tostring (VALUE *v);
74 int expr_main (int argc, char **argv)
79 error_msg_and_die("too few arguments");
86 error_msg_and_die ("syntax error");
88 if (v->type == integer)
89 printf ("%d\n", v->u.i);
91 printf ("%s\n", v->u.s);
96 /* Return a VALUE for I. */
98 static VALUE *int_value (int i)
102 v = xmalloc (sizeof(VALUE));
108 /* Return a VALUE for S. */
110 static VALUE *str_value (char *s)
114 v = xmalloc (sizeof(VALUE));
120 /* Free VALUE V, including structure components. */
122 static void freev (VALUE *v)
124 if (v->type == string)
129 /* Return nonzero if V is a null-string or zero-number. */
131 static int null (VALUE *v)
137 return v->u.s[0] == '\0' || strcmp (v->u.s, "0") == 0;
143 /* Coerce V to a string value (can't fail). */
145 static void tostring (VALUE *v)
149 if (v->type == integer) {
150 temp = xmalloc (4 * (sizeof (int) / sizeof (char)));
151 sprintf (temp, "%d", v->u.i);
157 /* Coerce V to an integer value. Return 1 on success, 0 on failure. */
159 static int toarith (VALUE *v)
168 /* Don't interpret the empty string as an integer. */
181 /* Return nonzero if the next token matches STR exactly.
182 STR must not be NULL. */
189 return strcmp (*args, str) == 0;
192 /* The comparison operator handling functions. */
194 #define cmpf(name, rel) \
195 static int name (l, r) VALUE *l; VALUE *r; \
197 if (l->type == string || r->type == string) { \
200 return strcmp (l->u.s, r->u.s) rel 0; \
203 return l->u.i rel r->u.i; \
206 cmpf (less_equal, <=)
209 cmpf (greater_equal, >=)
210 cmpf (greater_than, >)
214 /* The arithmetic operator handling functions. */
216 #define arithf(name, op) \
218 int name (l, r) VALUE *l; VALUE *r; \
220 if (!toarith (l) || !toarith (r)) \
221 error_msg_and_die ("non-numeric argument"); \
222 return l->u.i op r->u.i; \
225 #define arithdivf(name, op) \
226 int name (l, r) VALUE *l; VALUE *r; \
228 if (!toarith (l) || !toarith (r)) \
229 error_msg_and_die ( "non-numeric argument"); \
231 error_msg_and_die ( "division by zero"); \
232 return l->u.i op r->u.i; \
238 arithdivf (divide, /)
244 /* Do the : operator.
245 SV is the VALUE for the lhs (the string),
246 PV is the VALUE for the rhs (the pattern). */
248 static VALUE *docolon (VALUE *sv, VALUE *pv)
252 struct re_pattern_buffer re_buffer;
253 struct re_registers re_regs;
259 if (pv->u.s[0] == '^') {
261 warning: unportable BRE: `%s': using `^' as the first character\n\
262 of a basic regular expression is not portable; it is being ignored",
266 len = strlen (pv->u.s);
267 memset (&re_buffer, 0, sizeof (re_buffer));
268 memset (&re_regs, 0, sizeof (re_regs));
269 re_buffer.allocated = 2 * len;
270 re_buffer.buffer = (unsigned char *) xmalloc (re_buffer.allocated);
271 re_buffer.translate = 0;
272 re_syntax_options = RE_SYNTAX_POSIX_BASIC;
273 errmsg = re_compile_pattern (pv->u.s, len, &re_buffer);
275 error_msg_and_die("%s", errmsg);
278 len = re_match (&re_buffer, sv->u.s, strlen (sv->u.s), 0, &re_regs);
280 /* Were \(...\) used? */
281 if (re_buffer.re_nsub > 0) { /* was (re_regs.start[1] >= 0) */
282 sv->u.s[re_regs.end[1]] = '\0';
283 v = str_value (sv->u.s + re_regs.start[1]);
289 /* Match failed -- return the right kind of null. */
290 if (re_buffer.re_nsub > 0)
295 free (re_buffer.buffer);
299 /* Handle bare operands and ( expr ) syntax. */
301 static VALUE *eval7 (void)
306 error_msg_and_die ( "syntax error");
312 error_msg_and_die ( "syntax error");
318 error_msg_and_die ( "syntax error");
320 return str_value (*args++);
323 /* Handle match, substr, index, length, and quote keywords. */
325 static VALUE *eval6 (void)
327 VALUE *l, *r, *v, *i1, *i2;
329 if (nextarg ("quote")) {
332 error_msg_and_die ( "syntax error");
333 return str_value (*args++);
335 else if (nextarg ("length")) {
339 v = int_value (strlen (r->u.s));
343 else if (nextarg ("match")) {
352 else if (nextarg ("index")) {
358 v = int_value (strcspn (l->u.s, r->u.s) + 1);
359 if (v->u.i == (int) strlen (l->u.s) + 1)
365 else if (nextarg ("substr")) {
371 if (!toarith (i1) || !toarith (i2)
372 || i1->u.i > (int) strlen (l->u.s)
373 || i1->u.i <= 0 || i2->u.i <= 0)
376 v = xmalloc (sizeof(VALUE));
378 v->u.s = strncpy ((char *) xmalloc (i2->u.i + 1),
379 l->u.s + i1->u.i - 1, i2->u.i);
391 /* Handle : operator (pattern matching).
392 Calls docolon to do the real work. */
394 static VALUE *eval5 (void)
399 while (nextarg (":")) {
410 /* Handle *, /, % operators. */
412 static VALUE *eval4 (void)
421 else if (nextarg ("/"))
423 else if (nextarg ("%"))
436 /* Handle +, - operators. */
438 static VALUE *eval3 (void)
447 else if (nextarg ("-"))
460 /* Handle comparisons. */
462 static VALUE *eval2 (void)
471 else if (nextarg ("<="))
473 else if (nextarg ("=") || nextarg ("=="))
475 else if (nextarg ("!="))
477 else if (nextarg (">="))
479 else if (nextarg (">"))
496 static VALUE *eval1 (void)
501 while (nextarg ("&")) {
504 if (null (l) || null (r)) {
517 static VALUE *eval (void)
522 while (nextarg ("|")) {