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(const char *s);
71 static int nextarg(const 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);
78 int expr_main(int argc, char **argv)
83 bb_error_msg_and_die("too few arguments");
90 bb_error_msg_and_die("syntax error");
92 if (v->type == integer)
93 printf("%" PF_REZ "d\n", PF_REZ_TYPE v->u.i);
97 fflush_stdout_and_exit(null(v));
100 /* Return a VALUE for I. */
102 static VALUE *int_value(arith_t i)
106 v = xmalloc(sizeof(VALUE));
112 /* Return a VALUE for S. */
114 static VALUE *str_value(const char *s)
118 v = xmalloc(sizeof(VALUE));
124 /* Free VALUE V, including structure components. */
126 static void freev(VALUE * v)
128 if (v->type == string)
133 /* Return nonzero if V is a null-string or zero-number. */
135 static int null(VALUE * v)
137 if (v->type == integer)
140 return v->u.s[0] == '\0' || LONE_CHAR(v->u.s, '0');
143 /* Coerce V to a string value (can't fail). */
145 static void tostring(VALUE * v)
147 if (v->type == integer) {
148 v->u.s = xasprintf("%" PF_REZ "d", PF_REZ_TYPE v->u.i);
153 /* Coerce V to an integer value. Return 1 on success, 0 on failure. */
155 static int toarith(VALUE * v)
157 if (v->type == string) {
161 /* Don't interpret the empty string as an integer. */
162 /* Currently does not worry about overflow or int/long differences. */
163 i = STRTOL(v->u.s, &e, 10);
164 if ((v->u.s == e) || *e)
173 /* Return nonzero if the next token matches STR exactly.
174 STR must not be NULL. */
176 static int nextarg(const char *str)
180 return strcmp(*args, str) == 0;
183 /* The comparison operator handling functions. */
185 static int cmp_common(VALUE * l, VALUE * r, int op)
189 if (l->type == string || r->type == string) {
192 cmpval = strcmp(l->u.s, r->u.s);
194 cmpval = l->u.i - r->u.i;
197 if (op == ('L' + 'E'))
209 /* The arithmetic operator handling functions. */
211 static arith_t arithmetic_common(VALUE * l, VALUE * r, int op)
215 if (!toarith(l) || !toarith(r))
216 bb_error_msg_and_die("non-numeric argument");
219 if ((op == '/' || op == '%') && ri == 0)
220 bb_error_msg_and_die("division by zero");
233 /* Do the : operator.
234 SV is the VALUE for the lhs (the string),
235 PV is the VALUE for the rhs (the pattern). */
237 static VALUE *docolon(VALUE * sv, VALUE * pv)
241 const int NMATCH = 2;
242 regmatch_t re_regs[NMATCH];
247 if (pv->u.s[0] == '^') {
249 warning: unportable BRE: `%s': using `^' as the first character\n\
250 of a basic regular expression is not portable; it is being ignored", pv->u.s);
253 memset(&re_buffer, 0, sizeof(re_buffer));
254 memset(re_regs, 0, sizeof(*re_regs));
255 if (regcomp(&re_buffer, pv->u.s, 0) != 0)
256 bb_error_msg_and_die("invalid regular expression");
258 /* expr uses an anchored pattern match, so check that there was a
259 * match and that the match starts at offset 0. */
260 if (regexec(&re_buffer, sv->u.s, NMATCH, re_regs, 0) != REG_NOMATCH &&
261 re_regs[0].rm_so == 0) {
262 /* Were \(...\) used? */
263 if (re_buffer.re_nsub > 0) {
264 sv->u.s[re_regs[1].rm_eo] = '\0';
265 v = str_value(sv->u.s + re_regs[1].rm_so);
267 v = int_value(re_regs[0].rm_eo);
269 /* Match failed -- return the right kind of null. */
270 if (re_buffer.re_nsub > 0)
278 /* Handle bare operands and ( expr ) syntax. */
280 static VALUE *eval7(void)
285 bb_error_msg_and_die("syntax error");
291 bb_error_msg_and_die("syntax error");
297 bb_error_msg_and_die("syntax error");
299 return str_value(*args++);
302 /* Handle match, substr, index, length, and quote keywords. */
304 static VALUE *eval6(void)
306 VALUE *l, *r, *v, *i1, *i2;
308 if (nextarg("quote")) {
311 bb_error_msg_and_die("syntax error");
312 return str_value(*args++);
313 } else if (nextarg("length")) {
317 v = int_value(strlen(r->u.s));
320 } else if (nextarg("match")) {
328 } else if (nextarg("index")) {
334 v = int_value(strcspn(l->u.s, r->u.s) + 1);
335 if (v->u.i == (arith_t) strlen(l->u.s) + 1)
340 } else if (nextarg("substr")) {
346 if (!toarith(i1) || !toarith(i2)
347 || i1->u.i > (arith_t) strlen(l->u.s)
348 || i1->u.i <= 0 || i2->u.i <= 0)
351 v = xmalloc(sizeof(VALUE));
353 v->u.s = xstrndup(l->u.s + i1->u.i - 1, i2->u.i);
363 /* Handle : operator (pattern matching).
364 Calls docolon to do the real work. */
366 static VALUE *eval5(void)
371 while (nextarg(":")) {
382 /* Handle *, /, % operators. */
384 static VALUE *eval4(void)
394 else if (nextarg("/"))
396 else if (nextarg("%"))
402 val = arithmetic_common(l, r, op);
409 /* Handle +, - operators. */
411 static VALUE *eval3(void)
421 else if (nextarg("-"))
427 val = arithmetic_common(l, r, op);
434 /* Handle comparisons. */
436 static VALUE *eval2(void)
446 else if (nextarg("<="))
448 else if (nextarg("=") || nextarg("=="))
450 else if (nextarg("!="))
452 else if (nextarg(">="))
454 else if (nextarg(">"))
462 val = cmp_common(l, r, op);
471 static VALUE *eval1(void)
476 while (nextarg("&")) {
479 if (null(l) || null(r)) {
491 static VALUE *eval(void)
496 while (nextarg("|")) {