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. */
68 #define G (*(struct globals*)&bb_common_bufsiz1)
70 /* forward declarations */
71 static VALUE *eval(void);
74 /* Return a VALUE for I. */
76 static VALUE *int_value(arith_t i)
80 v = xmalloc(sizeof(VALUE));
86 /* Return a VALUE for S. */
88 static VALUE *str_value(const char *s)
92 v = xmalloc(sizeof(VALUE));
98 /* Free VALUE V, including structure components. */
100 static void freev(VALUE * v)
102 if (v->type == string)
107 /* Return nonzero if V is a null-string or zero-number. */
109 static int null(VALUE * v)
111 if (v->type == integer)
114 return v->u.s[0] == '\0' || LONE_CHAR(v->u.s, '0');
117 /* Coerce V to a string value (can't fail). */
119 static void tostring(VALUE * v)
121 if (v->type == integer) {
122 v->u.s = xasprintf("%" PF_REZ "d", PF_REZ_TYPE v->u.i);
127 /* Coerce V to an integer value. Return 1 on success, 0 on failure. */
129 static bool toarith(VALUE * v)
131 if (v->type == string) {
135 /* Don't interpret the empty string as an integer. */
136 /* Currently does not worry about overflow or int/long differences. */
137 i = STRTOL(v->u.s, &e, 10);
138 if ((v->u.s == e) || *e)
147 /* Return nonzero if the next token matches STR exactly.
148 STR must not be NULL. */
150 static bool nextarg(const char *str)
154 return strcmp(*G.args, str) == 0;
157 /* The comparison operator handling functions. */
159 static int cmp_common(VALUE * l, VALUE * r, int op)
163 if (l->type == string || r->type == string) {
166 cmpval = strcmp(l->u.s, r->u.s);
168 cmpval = l->u.i - r->u.i;
171 if (op == ('L' + 'E'))
183 /* The arithmetic operator handling functions. */
185 static arith_t arithmetic_common(VALUE * l, VALUE * r, int op)
189 if (!toarith(l) || !toarith(r))
190 bb_error_msg_and_die("non-numeric argument");
193 if ((op == '/' || op == '%') && ri == 0)
194 bb_error_msg_and_die("division by zero");
207 /* Do the : operator.
208 SV is the VALUE for the lhs (the string),
209 PV is the VALUE for the rhs (the pattern). */
211 static VALUE *docolon(VALUE * sv, VALUE * pv)
215 const int NMATCH = 2;
216 regmatch_t re_regs[NMATCH];
221 if (pv->u.s[0] == '^') {
223 warning: unportable BRE: `%s': using `^' as the first character\n\
224 of a basic regular expression is not portable; it is being ignored", pv->u.s);
227 memset(&re_buffer, 0, sizeof(re_buffer));
228 memset(re_regs, 0, sizeof(*re_regs));
229 xregcomp(&re_buffer, pv->u.s, 0);
231 /* expr uses an anchored pattern match, so check that there was a
232 * match and that the match starts at offset 0. */
233 if (regexec(&re_buffer, sv->u.s, NMATCH, re_regs, 0) != REG_NOMATCH &&
234 re_regs[0].rm_so == 0) {
235 /* Were \(...\) used? */
236 if (re_buffer.re_nsub > 0) {
237 sv->u.s[re_regs[1].rm_eo] = '\0';
238 v = str_value(sv->u.s + re_regs[1].rm_so);
240 v = int_value(re_regs[0].rm_eo);
242 /* Match failed -- return the right kind of null. */
243 if (re_buffer.re_nsub > 0)
248 //FIXME: sounds like here is a bit missing: regfree(&re_buffer);
252 /* Handle bare operands and ( expr ) syntax. */
254 static VALUE *eval7(void)
259 bb_error_msg_and_die("syntax error");
265 bb_error_msg_and_die("syntax error");
271 bb_error_msg_and_die("syntax error");
273 return str_value(*G.args++);
276 /* Handle match, substr, index, length, and quote keywords. */
278 static VALUE *eval6(void)
280 VALUE *l, *r, *v = NULL /* silence gcc */, *i1, *i2;
281 static const char * const keywords[] = {
282 "quote", "length", "match", "index", "substr", NULL
285 smalluint key = *G.args ? index_in_str_array(keywords, *G.args) + 1 : 0;
286 if (key == 0) /* not a keyword */
288 G.args++; /* We have a valid token, so get the next argument. */
289 if (key == 1) { /* quote */
291 bb_error_msg_and_die("syntax error");
292 return str_value(*G.args++);
294 if (key == 2) { /* length */
297 v = int_value(strlen(r->u.s));
302 if (key == 3) { /* match */
308 if (key == 4) { /* index */
312 v = int_value(strcspn(l->u.s, r->u.s) + 1);
313 if (v->u.i == (arith_t) strlen(l->u.s) + 1)
318 if (key == 5) { /* substr */
322 if (!toarith(i1) || !toarith(i2)
323 || i1->u.i > (arith_t) strlen(l->u.s)
324 || i1->u.i <= 0 || i2->u.i <= 0)
327 v = xmalloc(sizeof(VALUE));
329 v->u.s = xstrndup(l->u.s + i1->u.i - 1, i2->u.i);
339 /* Handle : operator (pattern matching).
340 Calls docolon to do the real work. */
342 static VALUE *eval5(void)
347 while (nextarg(":")) {
358 /* Handle *, /, % operators. */
360 static VALUE *eval4(void)
370 else if (nextarg("/"))
372 else if (nextarg("%"))
378 val = arithmetic_common(l, r, op);
385 /* Handle +, - operators. */
387 static VALUE *eval3(void)
397 else if (nextarg("-"))
403 val = arithmetic_common(l, r, op);
410 /* Handle comparisons. */
412 static VALUE *eval2(void)
422 else if (nextarg("<="))
424 else if (nextarg("=") || nextarg("=="))
426 else if (nextarg("!="))
428 else if (nextarg(">="))
430 else if (nextarg(">"))
438 val = cmp_common(l, r, op);
447 static VALUE *eval1(void)
452 while (nextarg("&")) {
455 if (null(l) || null(r)) {
467 static VALUE *eval(void)
472 while (nextarg("|")) {
484 int expr_main(int argc, char **argv);
485 int expr_main(int argc, char **argv)
490 bb_error_msg_and_die("too few arguments");
497 bb_error_msg_and_die("syntax error");
499 if (v->type == integer)
500 printf("%" PF_REZ "d\n", PF_REZ_TYPE v->u.i);
504 fflush_stdout_and_exit(null(v));