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)
54 TYPE type; /* Which kind. */
55 union { /* The value itself. */
60 typedef struct valinfo VALUE;
62 /* The arguments given to the program, minus the program name. */
65 static VALUE *docolon(VALUE * sv, VALUE * pv);
66 static VALUE *eval(void);
67 static VALUE *int_value(arith_t i);
68 static VALUE *str_value(char *s);
69 static int nextarg(char *str);
70 static int null(VALUE * v);
71 static int toarith(VALUE * v);
72 static void freev(VALUE * v);
73 static void tostring(VALUE * v);
75 int expr_main(int argc, char **argv)
80 bb_error_msg_and_die("too few arguments");
87 bb_error_msg_and_die("syntax error");
89 if (v->type == integer)
90 printf("%" PF_REZ "d\n", PF_REZ_TYPE v->u.i);
94 fflush_stdout_and_exit(null(v));
97 /* Return a VALUE for I. */
99 static VALUE *int_value(arith_t i)
103 v = xmalloc(sizeof(VALUE));
109 /* Return a VALUE for S. */
111 static VALUE *str_value(char *s)
115 v = xmalloc(sizeof(VALUE));
121 /* Free VALUE V, including structure components. */
123 static void freev(VALUE * v)
125 if (v->type == string)
130 /* Return nonzero if V is a null-string or zero-number. */
132 static int null(VALUE * v)
134 if (v->type == integer)
137 return v->u.s[0] == '\0' || strcmp(v->u.s, "0") == 0;
140 /* Coerce V to a string value (can't fail). */
142 static void tostring(VALUE * v)
144 if (v->type == integer) {
145 v->u.s = xasprintf("%" PF_REZ "d", PF_REZ_TYPE v->u.i);
150 /* Coerce V to an integer value. Return 1 on success, 0 on failure. */
152 static int toarith(VALUE * v)
154 if (v->type == string) {
158 /* Don't interpret the empty string as an integer. */
159 /* Currently does not worry about overflow or int/long differences. */
160 i = STRTOL(v->u.s, &e, 10);
161 if ((v->u.s == e) || *e)
170 /* Return nonzero if the next token matches STR exactly.
171 STR must not be NULL. */
173 static int nextarg(char *str)
177 return strcmp(*args, str) == 0;
180 /* The comparison operator handling functions. */
182 static int cmp_common(VALUE * l, VALUE * r, int op)
186 if (l->type == string || r->type == string) {
189 cmpval = strcmp(l->u.s, r->u.s);
191 cmpval = l->u.i - r->u.i;
194 else if (op == ('L' + 'E'))
206 /* The arithmetic operator handling functions. */
208 static arith_t arithmetic_common(VALUE * l, VALUE * r, int op)
212 if (!toarith(l) || !toarith(r))
213 bb_error_msg_and_die("non-numeric argument");
216 if ((op == '/' || op == '%') && ri == 0)
217 bb_error_msg_and_die("division by zero");
230 /* Do the : operator.
231 SV is the VALUE for the lhs (the string),
232 PV is the VALUE for the rhs (the pattern). */
234 static VALUE *docolon(VALUE * sv, VALUE * pv)
238 const int NMATCH = 2;
239 regmatch_t re_regs[NMATCH];
244 if (pv->u.s[0] == '^') {
246 warning: unportable BRE: `%s': using `^' as the first character\n\
247 of a basic regular expression is not portable; it is being ignored", pv->u.s);
250 memset(&re_buffer, 0, sizeof(re_buffer));
251 memset(re_regs, 0, sizeof(*re_regs));
252 if (regcomp(&re_buffer, pv->u.s, 0) != 0)
253 bb_error_msg_and_die("invalid regular expression");
255 /* expr uses an anchored pattern match, so check that there was a
256 * match and that the match starts at offset 0. */
257 if (regexec(&re_buffer, sv->u.s, NMATCH, re_regs, 0) != REG_NOMATCH &&
258 re_regs[0].rm_so == 0) {
259 /* Were \(...\) used? */
260 if (re_buffer.re_nsub > 0) {
261 sv->u.s[re_regs[1].rm_eo] = '\0';
262 v = str_value(sv->u.s + re_regs[1].rm_so);
264 v = int_value(re_regs[0].rm_eo);
266 /* Match failed -- return the right kind of null. */
267 if (re_buffer.re_nsub > 0)
275 /* Handle bare operands and ( expr ) syntax. */
277 static VALUE *eval7(void)
282 bb_error_msg_and_die("syntax error");
288 bb_error_msg_and_die("syntax error");
294 bb_error_msg_and_die("syntax error");
296 return str_value(*args++);
299 /* Handle match, substr, index, length, and quote keywords. */
301 static VALUE *eval6(void)
303 VALUE *l, *r, *v, *i1, *i2;
305 if (nextarg("quote")) {
308 bb_error_msg_and_die("syntax error");
309 return str_value(*args++);
310 } else if (nextarg("length")) {
314 v = int_value(strlen(r->u.s));
317 } else if (nextarg("match")) {
325 } else if (nextarg("index")) {
331 v = int_value(strcspn(l->u.s, r->u.s) + 1);
332 if (v->u.i == (arith_t) strlen(l->u.s) + 1)
337 } else if (nextarg("substr")) {
343 if (!toarith(i1) || !toarith(i2)
344 || i1->u.i > (arith_t) strlen(l->u.s)
345 || i1->u.i <= 0 || i2->u.i <= 0)
348 v = xmalloc(sizeof(VALUE));
350 v->u.s = xstrndup(l->u.s + i1->u.i - 1, i2->u.i);
360 /* Handle : operator (pattern matching).
361 Calls docolon to do the real work. */
363 static VALUE *eval5(void)
368 while (nextarg(":")) {
379 /* Handle *, /, % operators. */
381 static VALUE *eval4(void)
391 else if (nextarg("/"))
393 else if (nextarg("%"))
399 val = arithmetic_common(l, r, op);
406 /* Handle +, - operators. */
408 static VALUE *eval3(void)
418 else if (nextarg("-"))
424 val = arithmetic_common(l, r, op);
431 /* Handle comparisons. */
433 static VALUE *eval2(void)
443 else if (nextarg("<="))
445 else if (nextarg("=") || nextarg("=="))
447 else if (nextarg("!="))
449 else if (nextarg(">="))
451 else if (nextarg(">"))
459 val = cmp_common(l, r, op);
468 static VALUE *eval1(void)
473 while (nextarg("&")) {
476 if (null(l) || null(r)) {
488 static VALUE *eval(void)
493 while (nextarg("|")) {