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 * Copyright (C) 2003-2005 Vladimir Oleynik <dzo@simtreas.ru>
11 * - reduced 464 bytes.
14 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
17 /* This program evaluates expressions. Each token (operator, operand,
18 * parenthesis) of the expression must be a separate argument. The
19 * parser used is a reasonably general one, though any incarnation of
20 * it is language-specific. It is especially nice for expressions.
22 * No parse tree is needed; a new node is evaluated immediately.
23 * One function can handle multiple operators all of equal precedence,
24 * provided they all associate ((x op x) op x). */
26 /* no getopt needed */
31 /* The kinds of value we can have. */
36 typedef enum valtype TYPE;
38 #if ENABLE_EXPR_MATH_SUPPORT_64
39 typedef int64_t arith_t;
42 #define PF_REZ_TYPE (long long)
43 #define STRTOL(s, e, b) strtoll(s, e, b)
48 #define PF_REZ_TYPE (long)
49 #define STRTOL(s, e, b) strtol(s, e, b)
52 /* TODO: use bb_strtol[l]? It's easier to check for errors... */
56 TYPE type; /* Which kind. */
57 union { /* The value itself. */
62 typedef struct valinfo VALUE;
64 /* The arguments given to the program, minus the program name. */
67 static VALUE *docolon(VALUE * sv, VALUE * pv);
68 static VALUE *eval(void);
69 static VALUE *int_value(arith_t i);
70 static VALUE *str_value(char *s);
71 static int nextarg(char *str);
72 static int null(VALUE * v);
73 static int toarith(VALUE * v);
74 static void freev(VALUE * v);
75 static void tostring(VALUE * v);
77 int expr_main(int argc, char **argv)
82 bb_error_msg_and_die("too few arguments");
89 bb_error_msg_and_die("syntax error");
91 if (v->type == integer)
92 printf("%" PF_REZ "d\n", PF_REZ_TYPE v->u.i);
96 fflush_stdout_and_exit(null(v));
99 /* Return a VALUE for I. */
101 static VALUE *int_value(arith_t i)
105 v = xmalloc(sizeof(VALUE));
111 /* Return a VALUE for S. */
113 static VALUE *str_value(char *s)
117 v = xmalloc(sizeof(VALUE));
123 /* Free VALUE V, including structure components. */
125 static void freev(VALUE * v)
127 if (v->type == string)
132 /* Return nonzero if V is a null-string or zero-number. */
134 static int null(VALUE * v)
136 if (v->type == integer)
139 return v->u.s[0] == '\0' || strcmp(v->u.s, "0") == 0;
142 /* Coerce V to a string value (can't fail). */
144 static void tostring(VALUE * v)
146 if (v->type == integer) {
147 v->u.s = xasprintf("%" PF_REZ "d", PF_REZ_TYPE v->u.i);
152 /* Coerce V to an integer value. Return 1 on success, 0 on failure. */
154 static int toarith(VALUE * v)
156 if (v->type == string) {
160 /* Don't interpret the empty string as an integer. */
161 /* Currently does not worry about overflow or int/long differences. */
162 i = STRTOL(v->u.s, &e, 10);
163 if ((v->u.s == e) || *e)
172 /* Return nonzero if the next token matches STR exactly.
173 STR must not be NULL. */
175 static int nextarg(char *str)
179 return strcmp(*args, str) == 0;
182 /* The comparison operator handling functions. */
184 static int cmp_common(VALUE * l, VALUE * r, int op)
188 if (l->type == string || r->type == string) {
191 cmpval = strcmp(l->u.s, r->u.s);
193 cmpval = l->u.i - r->u.i;
196 else if (op == ('L' + 'E'))
208 /* The arithmetic operator handling functions. */
210 static arith_t arithmetic_common(VALUE * l, VALUE * r, int op)
214 if (!toarith(l) || !toarith(r))
215 bb_error_msg_and_die("non-numeric argument");
218 if ((op == '/' || op == '%') && ri == 0)
219 bb_error_msg_and_die("division by zero");
232 /* Do the : operator.
233 SV is the VALUE for the lhs (the string),
234 PV is the VALUE for the rhs (the pattern). */
236 static VALUE *docolon(VALUE * sv, VALUE * pv)
240 const int NMATCH = 2;
241 regmatch_t re_regs[NMATCH];
246 if (pv->u.s[0] == '^') {
248 warning: unportable BRE: `%s': using `^' as the first character\n\
249 of a basic regular expression is not portable; it is being ignored", pv->u.s);
252 memset(&re_buffer, 0, sizeof(re_buffer));
253 memset(re_regs, 0, sizeof(*re_regs));
254 if (regcomp(&re_buffer, pv->u.s, 0) != 0)
255 bb_error_msg_and_die("invalid regular expression");
257 /* expr uses an anchored pattern match, so check that there was a
258 * match and that the match starts at offset 0. */
259 if (regexec(&re_buffer, sv->u.s, NMATCH, re_regs, 0) != REG_NOMATCH &&
260 re_regs[0].rm_so == 0) {
261 /* Were \(...\) used? */
262 if (re_buffer.re_nsub > 0) {
263 sv->u.s[re_regs[1].rm_eo] = '\0';
264 v = str_value(sv->u.s + re_regs[1].rm_so);
266 v = int_value(re_regs[0].rm_eo);
268 /* Match failed -- return the right kind of null. */
269 if (re_buffer.re_nsub > 0)
277 /* Handle bare operands and ( expr ) syntax. */
279 static VALUE *eval7(void)
284 bb_error_msg_and_die("syntax error");
290 bb_error_msg_and_die("syntax error");
296 bb_error_msg_and_die("syntax error");
298 return str_value(*args++);
301 /* Handle match, substr, index, length, and quote keywords. */
303 static VALUE *eval6(void)
305 VALUE *l, *r, *v, *i1, *i2;
307 if (nextarg("quote")) {
310 bb_error_msg_and_die("syntax error");
311 return str_value(*args++);
312 } else if (nextarg("length")) {
316 v = int_value(strlen(r->u.s));
319 } else if (nextarg("match")) {
327 } else if (nextarg("index")) {
333 v = int_value(strcspn(l->u.s, r->u.s) + 1);
334 if (v->u.i == (arith_t) strlen(l->u.s) + 1)
339 } else if (nextarg("substr")) {
345 if (!toarith(i1) || !toarith(i2)
346 || i1->u.i > (arith_t) strlen(l->u.s)
347 || i1->u.i <= 0 || i2->u.i <= 0)
350 v = xmalloc(sizeof(VALUE));
352 v->u.s = xstrndup(l->u.s + i1->u.i - 1, i2->u.i);
362 /* Handle : operator (pattern matching).
363 Calls docolon to do the real work. */
365 static VALUE *eval5(void)
370 while (nextarg(":")) {
381 /* Handle *, /, % operators. */
383 static VALUE *eval4(void)
393 else if (nextarg("/"))
395 else if (nextarg("%"))
401 val = arithmetic_common(l, r, op);
408 /* Handle +, - operators. */
410 static VALUE *eval3(void)
420 else if (nextarg("-"))
426 val = arithmetic_common(l, r, op);
433 /* Handle comparisons. */
435 static VALUE *eval2(void)
445 else if (nextarg("<="))
447 else if (nextarg("=") || nextarg("=="))
449 else if (nextarg("!="))
451 else if (nextarg(">="))
453 else if (nextarg(">"))
461 val = cmp_common(l, r, op);
470 static VALUE *eval1(void)
475 while (nextarg("&")) {
478 if (null(l) || null(r)) {
490 static VALUE *eval(void)
495 while (nextarg("|")) {