Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Mini expr implementation for busybox |
| 4 | * |
| 5 | * based on GNU expr Mike Parker. |
| 6 | * Copyright (C) 86, 1991-1997, 1999 Free Software Foundation, Inc. |
| 7 | * |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 8 | * Busybox modifications |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 9 | * Copyright (c) 2000 Edward Betts <edward@debian.org>. |
"Vladimir N. Oleynik" | 8aa9e57 | 2006-01-25 13:56:03 +0000 | [diff] [blame] | 10 | * Copyright (C) 2003-2005 Vladimir Oleynik <dzo@simtreas.ru> |
| 11 | * - reduced 464 bytes. |
| 12 | * - 64 math support |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 13 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 14 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 15 | */ |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 16 | /* This program evaluates expressions. Each token (operator, operand, |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 17 | * parenthesis) of the expression must be a separate argument. The |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 18 | * parser used is a reasonably general one, though any incarnation of |
| 19 | * it is language-specific. It is especially nice for expressions. |
| 20 | * |
| 21 | * No parse tree is needed; a new node is evaluated immediately. |
| 22 | * One function can handle multiple operators all of equal precedence, |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 23 | * provided they all associate ((x op x) op x). |
| 24 | */ |
| 25 | //config:config EXPR |
| 26 | //config: bool "expr" |
| 27 | //config: default y |
| 28 | //config: help |
| 29 | //config: expr is used to calculate numbers and print the result |
| 30 | //config: to standard output. |
| 31 | //config: |
| 32 | //config:config EXPR_MATH_SUPPORT_64 |
| 33 | //config: bool "Extend Posix numbers support to 64 bit" |
| 34 | //config: default y |
| 35 | //config: depends on EXPR |
| 36 | //config: help |
| 37 | //config: Enable 64-bit math support in the expr applet. This will make |
| 38 | //config: the applet slightly larger, but will allow computation with very |
| 39 | //config: large numbers. |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 40 | |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 41 | //applet:IF_EXPR(APPLET(expr, BB_DIR_USR_BIN, BB_SUID_DROP)) |
| 42 | |
| 43 | //kbuild:lib-$(CONFIG_EXPR) += expr.o |
Mark Whitley | 827e45c | 2001-03-09 23:59:51 +0000 | [diff] [blame] | 44 | |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 45 | //usage:#define expr_trivial_usage |
| 46 | //usage: "EXPRESSION" |
| 47 | //usage:#define expr_full_usage "\n\n" |
| 48 | //usage: "Print the value of EXPRESSION to stdout\n" |
| 49 | //usage: "\n" |
| 50 | //usage: "EXPRESSION may be:\n" |
| 51 | //usage: " ARG1 | ARG2 ARG1 if it is neither null nor 0, otherwise ARG2\n" |
| 52 | //usage: " ARG1 & ARG2 ARG1 if neither argument is null or 0, otherwise 0\n" |
| 53 | //usage: " ARG1 < ARG2 1 if ARG1 is less than ARG2, else 0. Similarly:\n" |
| 54 | //usage: " ARG1 <= ARG2\n" |
| 55 | //usage: " ARG1 = ARG2\n" |
| 56 | //usage: " ARG1 != ARG2\n" |
| 57 | //usage: " ARG1 >= ARG2\n" |
| 58 | //usage: " ARG1 > ARG2\n" |
| 59 | //usage: " ARG1 + ARG2 Sum of ARG1 and ARG2. Similarly:\n" |
| 60 | //usage: " ARG1 - ARG2\n" |
| 61 | //usage: " ARG1 * ARG2\n" |
| 62 | //usage: " ARG1 / ARG2\n" |
| 63 | //usage: " ARG1 % ARG2\n" |
| 64 | //usage: " STRING : REGEXP Anchored pattern match of REGEXP in STRING\n" |
| 65 | //usage: " match STRING REGEXP Same as STRING : REGEXP\n" |
| 66 | //usage: " substr STRING POS LENGTH Substring of STRING, POS counted from 1\n" |
| 67 | //usage: " index STRING CHARS Index in STRING where any CHARS is found, or 0\n" |
| 68 | //usage: " length STRING Length of STRING\n" |
| 69 | //usage: " quote TOKEN Interpret TOKEN as a string, even if\n" |
| 70 | //usage: " it is a keyword like 'match' or an\n" |
| 71 | //usage: " operator like '/'\n" |
| 72 | //usage: " (EXPRESSION) Value of EXPRESSION\n" |
| 73 | //usage: "\n" |
| 74 | //usage: "Beware that many operators need to be escaped or quoted for shells.\n" |
| 75 | //usage: "Comparisons are arithmetic if both ARGs are numbers, else\n" |
| 76 | //usage: "lexicographical. Pattern matches return the string matched between\n" |
| 77 | //usage: "\\( and \\) or null; if \\( and \\) are not used, they return the number\n" |
| 78 | //usage: "of characters matched or 0." |
| 79 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 80 | #include "libbb.h" |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 81 | #include "common_bufsiz.h" |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 82 | #include "xregex.h" |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 83 | |
"Vladimir N. Oleynik" | 8aa9e57 | 2006-01-25 13:56:03 +0000 | [diff] [blame] | 84 | #if ENABLE_EXPR_MATH_SUPPORT_64 |
| 85 | typedef int64_t arith_t; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 86 | |
"Vladimir N. Oleynik" | 8aa9e57 | 2006-01-25 13:56:03 +0000 | [diff] [blame] | 87 | #define PF_REZ "ll" |
Eric Andersen | 5e67887 | 2006-01-30 19:48:23 +0000 | [diff] [blame] | 88 | #define PF_REZ_TYPE (long long) |
"Vladimir N. Oleynik" | 8aa9e57 | 2006-01-25 13:56:03 +0000 | [diff] [blame] | 89 | #define STRTOL(s, e, b) strtoll(s, e, b) |
| 90 | #else |
| 91 | typedef long arith_t; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 92 | |
"Vladimir N. Oleynik" | 8aa9e57 | 2006-01-25 13:56:03 +0000 | [diff] [blame] | 93 | #define PF_REZ "l" |
Eric Andersen | 5e67887 | 2006-01-30 19:48:23 +0000 | [diff] [blame] | 94 | #define PF_REZ_TYPE (long) |
"Vladimir N. Oleynik" | 8aa9e57 | 2006-01-25 13:56:03 +0000 | [diff] [blame] | 95 | #define STRTOL(s, e, b) strtol(s, e, b) |
| 96 | #endif |
| 97 | |
Denis Vlasenko | d686a04 | 2006-11-27 14:43:21 +0000 | [diff] [blame] | 98 | /* TODO: use bb_strtol[l]? It's easier to check for errors... */ |
| 99 | |
Denis Vlasenko | a7f4e4b | 2008-04-02 20:24:09 +0000 | [diff] [blame] | 100 | /* The kinds of value we can have. */ |
| 101 | enum { |
| 102 | INTEGER, |
| 103 | STRING |
| 104 | }; |
| 105 | |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 106 | /* A value is.... */ |
| 107 | struct valinfo { |
Denis Vlasenko | a7f4e4b | 2008-04-02 20:24:09 +0000 | [diff] [blame] | 108 | smallint type; /* Which kind. */ |
| 109 | union { /* The value itself. */ |
"Vladimir N. Oleynik" | 8aa9e57 | 2006-01-25 13:56:03 +0000 | [diff] [blame] | 110 | arith_t i; |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 111 | char *s; |
| 112 | } u; |
| 113 | }; |
| 114 | typedef struct valinfo VALUE; |
| 115 | |
| 116 | /* The arguments given to the program, minus the program name. */ |
Bernhard Reutner-Fischer | adb01b1 | 2007-04-02 16:38:13 +0000 | [diff] [blame] | 117 | struct globals { |
| 118 | char **args; |
Denys Vlasenko | 98a4c7c | 2010-02-04 15:00:15 +0100 | [diff] [blame] | 119 | } FIX_ALIASING; |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 120 | #define G (*(struct globals*)bb_common_bufsiz1) |
Denys Vlasenko | 47cfbf3 | 2016-04-21 18:18:48 +0200 | [diff] [blame] | 121 | #define INIT_G() do { setup_common_bufsiz(); } while (0) |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 122 | |
Bernhard Reutner-Fischer | 3c5929a | 2007-04-02 16:41:24 +0000 | [diff] [blame] | 123 | /* forward declarations */ |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 124 | static VALUE *eval(void); |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 125 | |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 126 | |
| 127 | /* Return a VALUE for I. */ |
| 128 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 129 | static VALUE *int_value(arith_t i) |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 130 | { |
| 131 | VALUE *v; |
| 132 | |
Denis Vlasenko | a7f4e4b | 2008-04-02 20:24:09 +0000 | [diff] [blame] | 133 | v = xzalloc(sizeof(VALUE)); |
Denys Vlasenko | 7373759 | 2016-09-17 20:58:22 +0200 | [diff] [blame] | 134 | if (INTEGER) /* otherwise xzalloc did it already */ |
Denis Vlasenko | a7f4e4b | 2008-04-02 20:24:09 +0000 | [diff] [blame] | 135 | v->type = INTEGER; |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 136 | v->u.i = i; |
| 137 | return v; |
| 138 | } |
| 139 | |
| 140 | /* Return a VALUE for S. */ |
| 141 | |
Denis Vlasenko | a41fdf3 | 2007-01-29 22:51:00 +0000 | [diff] [blame] | 142 | static VALUE *str_value(const char *s) |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 143 | { |
| 144 | VALUE *v; |
| 145 | |
Denis Vlasenko | a7f4e4b | 2008-04-02 20:24:09 +0000 | [diff] [blame] | 146 | v = xzalloc(sizeof(VALUE)); |
Denys Vlasenko | 7373759 | 2016-09-17 20:58:22 +0200 | [diff] [blame] | 147 | if (STRING) /* otherwise xzalloc did it already */ |
Denis Vlasenko | a7f4e4b | 2008-04-02 20:24:09 +0000 | [diff] [blame] | 148 | v->type = STRING; |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 149 | v->u.s = xstrdup(s); |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 150 | return v; |
| 151 | } |
| 152 | |
| 153 | /* Free VALUE V, including structure components. */ |
| 154 | |
Denis Vlasenko | a7f4e4b | 2008-04-02 20:24:09 +0000 | [diff] [blame] | 155 | static void freev(VALUE *v) |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 156 | { |
Denis Vlasenko | a7f4e4b | 2008-04-02 20:24:09 +0000 | [diff] [blame] | 157 | if (v->type == STRING) |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 158 | free(v->u.s); |
| 159 | free(v); |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | /* Return nonzero if V is a null-string or zero-number. */ |
| 163 | |
Denis Vlasenko | a7f4e4b | 2008-04-02 20:24:09 +0000 | [diff] [blame] | 164 | static int null(VALUE *v) |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 165 | { |
Denis Vlasenko | a7f4e4b | 2008-04-02 20:24:09 +0000 | [diff] [blame] | 166 | if (v->type == INTEGER) |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 167 | return v->u.i == 0; |
Denis Vlasenko | a7f4e4b | 2008-04-02 20:24:09 +0000 | [diff] [blame] | 168 | /* STRING: */ |
Denis Vlasenko | cd27c42 | 2007-03-08 13:37:43 +0000 | [diff] [blame] | 169 | return v->u.s[0] == '\0' || LONE_CHAR(v->u.s, '0'); |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Denis Vlasenko | a7f4e4b | 2008-04-02 20:24:09 +0000 | [diff] [blame] | 172 | /* Coerce V to a STRING value (can't fail). */ |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 173 | |
Denis Vlasenko | a7f4e4b | 2008-04-02 20:24:09 +0000 | [diff] [blame] | 174 | static void tostring(VALUE *v) |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 175 | { |
Denis Vlasenko | a7f4e4b | 2008-04-02 20:24:09 +0000 | [diff] [blame] | 176 | if (v->type == INTEGER) { |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 177 | v->u.s = xasprintf("%" PF_REZ "d", PF_REZ_TYPE v->u.i); |
Denis Vlasenko | a7f4e4b | 2008-04-02 20:24:09 +0000 | [diff] [blame] | 178 | v->type = STRING; |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | |
Denis Vlasenko | a7f4e4b | 2008-04-02 20:24:09 +0000 | [diff] [blame] | 182 | /* Coerce V to an INTEGER value. Return 1 on success, 0 on failure. */ |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 183 | |
Denis Vlasenko | a7f4e4b | 2008-04-02 20:24:09 +0000 | [diff] [blame] | 184 | static bool toarith(VALUE *v) |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 185 | { |
Denis Vlasenko | a7f4e4b | 2008-04-02 20:24:09 +0000 | [diff] [blame] | 186 | if (v->type == STRING) { |
"Vladimir N. Oleynik" | 8aa9e57 | 2006-01-25 13:56:03 +0000 | [diff] [blame] | 187 | arith_t i; |
Manuel Novoa III | 7018385 | 2004-01-25 19:47:10 +0000 | [diff] [blame] | 188 | char *e; |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 189 | |
Manuel Novoa III | 7018385 | 2004-01-25 19:47:10 +0000 | [diff] [blame] | 190 | /* Don't interpret the empty string as an integer. */ |
| 191 | /* Currently does not worry about overflow or int/long differences. */ |
"Vladimir N. Oleynik" | 8aa9e57 | 2006-01-25 13:56:03 +0000 | [diff] [blame] | 192 | i = STRTOL(v->u.s, &e, 10); |
Manuel Novoa III | 7018385 | 2004-01-25 19:47:10 +0000 | [diff] [blame] | 193 | if ((v->u.s == e) || *e) |
| 194 | return 0; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 195 | free(v->u.s); |
Manuel Novoa III | 7018385 | 2004-01-25 19:47:10 +0000 | [diff] [blame] | 196 | v->u.i = i; |
Denis Vlasenko | a7f4e4b | 2008-04-02 20:24:09 +0000 | [diff] [blame] | 197 | v->type = INTEGER; |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 198 | } |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 199 | return 1; |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Denis Vlasenko | a7f4e4b | 2008-04-02 20:24:09 +0000 | [diff] [blame] | 202 | /* Return str[0]+str[1] if the next token matches STR exactly. |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 203 | STR must not be NULL. */ |
| 204 | |
Denis Vlasenko | a7f4e4b | 2008-04-02 20:24:09 +0000 | [diff] [blame] | 205 | static int nextarg(const char *str) |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 206 | { |
Denis Vlasenko | a7f4e4b | 2008-04-02 20:24:09 +0000 | [diff] [blame] | 207 | if (*G.args == NULL || strcmp(*G.args, str) != 0) |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 208 | return 0; |
Denis Vlasenko | a7f4e4b | 2008-04-02 20:24:09 +0000 | [diff] [blame] | 209 | return (unsigned char)str[0] + (unsigned char)str[1]; |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | /* The comparison operator handling functions. */ |
| 213 | |
Denis Vlasenko | a7f4e4b | 2008-04-02 20:24:09 +0000 | [diff] [blame] | 214 | static int cmp_common(VALUE *l, VALUE *r, int op) |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 215 | { |
Denis Vlasenko | a7f4e4b | 2008-04-02 20:24:09 +0000 | [diff] [blame] | 216 | arith_t ll, rr; |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 217 | |
Denis Vlasenko | a7f4e4b | 2008-04-02 20:24:09 +0000 | [diff] [blame] | 218 | ll = l->u.i; |
| 219 | rr = r->u.i; |
| 220 | if (l->type == STRING || r->type == STRING) { |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 221 | tostring(l); |
| 222 | tostring(r); |
Denis Vlasenko | a7f4e4b | 2008-04-02 20:24:09 +0000 | [diff] [blame] | 223 | ll = strcmp(l->u.s, r->u.s); |
| 224 | rr = 0; |
| 225 | } |
| 226 | /* calculating ll - rr and checking the result is prone to overflows. |
| 227 | * We'll do it differently: */ |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 228 | if (op == '<') |
Denis Vlasenko | a7f4e4b | 2008-04-02 20:24:09 +0000 | [diff] [blame] | 229 | return ll < rr; |
| 230 | if (op == ('<' + '=')) |
| 231 | return ll <= rr; |
| 232 | if (op == '=' || (op == '=' + '=')) |
| 233 | return ll == rr; |
| 234 | if (op == '!' + '=') |
| 235 | return ll != rr; |
Denis Vlasenko | cd27c42 | 2007-03-08 13:37:43 +0000 | [diff] [blame] | 236 | if (op == '>') |
Denis Vlasenko | a7f4e4b | 2008-04-02 20:24:09 +0000 | [diff] [blame] | 237 | return ll > rr; |
Denis Vlasenko | cd27c42 | 2007-03-08 13:37:43 +0000 | [diff] [blame] | 238 | /* >= */ |
Denis Vlasenko | a7f4e4b | 2008-04-02 20:24:09 +0000 | [diff] [blame] | 239 | return ll >= rr; |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 240 | } |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 241 | |
| 242 | /* The arithmetic operator handling functions. */ |
| 243 | |
Denis Vlasenko | a7f4e4b | 2008-04-02 20:24:09 +0000 | [diff] [blame] | 244 | static arith_t arithmetic_common(VALUE *l, VALUE *r, int op) |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 245 | { |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 246 | arith_t li, ri; |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 247 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 248 | if (!toarith(l) || !toarith(r)) |
| 249 | bb_error_msg_and_die("non-numeric argument"); |
| 250 | li = l->u.i; |
| 251 | ri = r->u.i; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 252 | if (op == '+') |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 253 | return li + ri; |
Denis Vlasenko | a7f4e4b | 2008-04-02 20:24:09 +0000 | [diff] [blame] | 254 | if (op == '-') |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 255 | return li - ri; |
Denis Vlasenko | a7f4e4b | 2008-04-02 20:24:09 +0000 | [diff] [blame] | 256 | if (op == '*') |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 257 | return li * ri; |
Denis Vlasenko | a7f4e4b | 2008-04-02 20:24:09 +0000 | [diff] [blame] | 258 | if (ri == 0) |
| 259 | bb_error_msg_and_die("division by zero"); |
| 260 | if (op == '/') |
Glenn L McGrath | 7b8765c | 2003-08-29 07:29:30 +0000 | [diff] [blame] | 261 | return li / ri; |
Denis Vlasenko | a7f4e4b | 2008-04-02 20:24:09 +0000 | [diff] [blame] | 262 | return li % ri; |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 265 | /* Do the : operator. |
| 266 | SV is the VALUE for the lhs (the string), |
| 267 | PV is the VALUE for the rhs (the pattern). */ |
| 268 | |
Denis Vlasenko | a7f4e4b | 2008-04-02 20:24:09 +0000 | [diff] [blame] | 269 | static VALUE *docolon(VALUE *sv, VALUE *pv) |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 270 | { |
Denys Vlasenko | 043b1e5 | 2009-09-06 12:47:55 +0200 | [diff] [blame] | 271 | enum { NMATCH = 2 }; |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 272 | VALUE *v; |
Rob Landley | 540d3f6 | 2005-05-09 21:42:42 +0000 | [diff] [blame] | 273 | regex_t re_buffer; |
Rob Landley | 540d3f6 | 2005-05-09 21:42:42 +0000 | [diff] [blame] | 274 | regmatch_t re_regs[NMATCH]; |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 275 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 276 | tostring(sv); |
| 277 | tostring(pv); |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 278 | |
| 279 | if (pv->u.s[0] == '^') { |
Denis Vlasenko | 230997b | 2009-03-03 14:27:36 +0000 | [diff] [blame] | 280 | bb_error_msg( |
| 281 | "warning: '%s': using '^' as the first character\n" |
| 282 | "of a basic regular expression is not portable; it is ignored", pv->u.s); |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 285 | memset(&re_buffer, 0, sizeof(re_buffer)); |
Denis Vlasenko | 230997b | 2009-03-03 14:27:36 +0000 | [diff] [blame] | 286 | memset(re_regs, 0, sizeof(re_regs)); |
Bernhard Reutner-Fischer | 8025afa | 2007-04-02 16:54:41 +0000 | [diff] [blame] | 287 | xregcomp(&re_buffer, pv->u.s, 0); |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 288 | |
Rob Landley | 540d3f6 | 2005-05-09 21:42:42 +0000 | [diff] [blame] | 289 | /* expr uses an anchored pattern match, so check that there was a |
| 290 | * match and that the match starts at offset 0. */ |
Denis Vlasenko | a7f4e4b | 2008-04-02 20:24:09 +0000 | [diff] [blame] | 291 | if (regexec(&re_buffer, sv->u.s, NMATCH, re_regs, 0) != REG_NOMATCH |
| 292 | && re_regs[0].rm_so == 0 |
| 293 | ) { |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 294 | /* Were \(...\) used? */ |
Denis Vlasenko | 230997b | 2009-03-03 14:27:36 +0000 | [diff] [blame] | 295 | if (re_buffer.re_nsub > 0 && re_regs[1].rm_so >= 0) { |
Rob Landley | 540d3f6 | 2005-05-09 21:42:42 +0000 | [diff] [blame] | 296 | sv->u.s[re_regs[1].rm_eo] = '\0'; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 297 | v = str_value(sv->u.s + re_regs[1].rm_so); |
Denis Vlasenko | a7f4e4b | 2008-04-02 20:24:09 +0000 | [diff] [blame] | 298 | } else { |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 299 | v = int_value(re_regs[0].rm_eo); |
Denis Vlasenko | a7f4e4b | 2008-04-02 20:24:09 +0000 | [diff] [blame] | 300 | } |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 301 | } else { |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 302 | /* Match failed -- return the right kind of null. */ |
| 303 | if (re_buffer.re_nsub > 0) |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 304 | v = str_value(""); |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 305 | else |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 306 | v = int_value(0); |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 307 | } |
Denis Vlasenko | 230997b | 2009-03-03 14:27:36 +0000 | [diff] [blame] | 308 | regfree(&re_buffer); |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 309 | return v; |
| 310 | } |
| 311 | |
| 312 | /* Handle bare operands and ( expr ) syntax. */ |
| 313 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 314 | static VALUE *eval7(void) |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 315 | { |
| 316 | VALUE *v; |
| 317 | |
Bernhard Reutner-Fischer | adb01b1 | 2007-04-02 16:38:13 +0000 | [diff] [blame] | 318 | if (!*G.args) |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 319 | bb_error_msg_and_die("syntax error"); |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 320 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 321 | if (nextarg("(")) { |
Bernhard Reutner-Fischer | adb01b1 | 2007-04-02 16:38:13 +0000 | [diff] [blame] | 322 | G.args++; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 323 | v = eval(); |
| 324 | if (!nextarg(")")) |
| 325 | bb_error_msg_and_die("syntax error"); |
Bernhard Reutner-Fischer | adb01b1 | 2007-04-02 16:38:13 +0000 | [diff] [blame] | 326 | G.args++; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 327 | return v; |
| 328 | } |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 329 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 330 | if (nextarg(")")) |
| 331 | bb_error_msg_and_die("syntax error"); |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 332 | |
Bernhard Reutner-Fischer | adb01b1 | 2007-04-02 16:38:13 +0000 | [diff] [blame] | 333 | return str_value(*G.args++); |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | /* Handle match, substr, index, length, and quote keywords. */ |
| 337 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 338 | static VALUE *eval6(void) |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 339 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 340 | static const char keywords[] ALIGN1 = |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 341 | "quote\0""length\0""match\0""index\0""substr\0"; |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 342 | |
Denis Vlasenko | c6753c1 | 2007-07-01 18:33:35 +0000 | [diff] [blame] | 343 | VALUE *r, *i1, *i2; |
| 344 | VALUE *l = l; /* silence gcc */ |
| 345 | VALUE *v = v; /* silence gcc */ |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 346 | int key = *G.args ? index_in_strings(keywords, *G.args) + 1 : 0; |
Denis Vlasenko | c6753c1 | 2007-07-01 18:33:35 +0000 | [diff] [blame] | 347 | |
Bernhard Reutner-Fischer | adb01b1 | 2007-04-02 16:38:13 +0000 | [diff] [blame] | 348 | if (key == 0) /* not a keyword */ |
| 349 | return eval7(); |
| 350 | G.args++; /* We have a valid token, so get the next argument. */ |
| 351 | if (key == 1) { /* quote */ |
| 352 | if (!*G.args) |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 353 | bb_error_msg_and_die("syntax error"); |
Bernhard Reutner-Fischer | adb01b1 | 2007-04-02 16:38:13 +0000 | [diff] [blame] | 354 | return str_value(*G.args++); |
| 355 | } |
| 356 | if (key == 2) { /* length */ |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 357 | r = eval6(); |
| 358 | tostring(r); |
| 359 | v = int_value(strlen(r->u.s)); |
| 360 | freev(r); |
Bernhard Reutner-Fischer | adb01b1 | 2007-04-02 16:38:13 +0000 | [diff] [blame] | 361 | } else |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 362 | l = eval6(); |
Bernhard Reutner-Fischer | adb01b1 | 2007-04-02 16:38:13 +0000 | [diff] [blame] | 363 | |
| 364 | if (key == 3) { /* match */ |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 365 | r = eval6(); |
| 366 | v = docolon(l, r); |
| 367 | freev(l); |
| 368 | freev(r); |
Bernhard Reutner-Fischer | adb01b1 | 2007-04-02 16:38:13 +0000 | [diff] [blame] | 369 | } |
| 370 | if (key == 4) { /* index */ |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 371 | r = eval6(); |
| 372 | tostring(l); |
| 373 | tostring(r); |
| 374 | v = int_value(strcspn(l->u.s, r->u.s) + 1); |
| 375 | if (v->u.i == (arith_t) strlen(l->u.s) + 1) |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 376 | v->u.i = 0; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 377 | freev(l); |
| 378 | freev(r); |
Bernhard Reutner-Fischer | adb01b1 | 2007-04-02 16:38:13 +0000 | [diff] [blame] | 379 | } |
| 380 | if (key == 5) { /* substr */ |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 381 | i1 = eval6(); |
| 382 | i2 = eval6(); |
| 383 | tostring(l); |
| 384 | if (!toarith(i1) || !toarith(i2) |
Denis Vlasenko | c6753c1 | 2007-07-01 18:33:35 +0000 | [diff] [blame] | 385 | || i1->u.i > (arith_t) strlen(l->u.s) |
| 386 | || i1->u.i <= 0 || i2->u.i <= 0) |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 387 | v = str_value(""); |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 388 | else { |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 389 | v = xmalloc(sizeof(VALUE)); |
Denis Vlasenko | a7f4e4b | 2008-04-02 20:24:09 +0000 | [diff] [blame] | 390 | v->type = STRING; |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 391 | v->u.s = xstrndup(l->u.s + i1->u.i - 1, i2->u.i); |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 392 | } |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 393 | freev(l); |
| 394 | freev(i1); |
| 395 | freev(i2); |
Bernhard Reutner-Fischer | adb01b1 | 2007-04-02 16:38:13 +0000 | [diff] [blame] | 396 | } |
| 397 | return v; |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | /* Handle : operator (pattern matching). |
| 401 | Calls docolon to do the real work. */ |
| 402 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 403 | static VALUE *eval5(void) |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 404 | { |
| 405 | VALUE *l, *r, *v; |
| 406 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 407 | l = eval6(); |
| 408 | while (nextarg(":")) { |
Bernhard Reutner-Fischer | adb01b1 | 2007-04-02 16:38:13 +0000 | [diff] [blame] | 409 | G.args++; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 410 | r = eval6(); |
| 411 | v = docolon(l, r); |
| 412 | freev(l); |
| 413 | freev(r); |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 414 | l = v; |
| 415 | } |
| 416 | return l; |
| 417 | } |
| 418 | |
| 419 | /* Handle *, /, % operators. */ |
| 420 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 421 | static VALUE *eval4(void) |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 422 | { |
| 423 | VALUE *l, *r; |
"Vladimir N. Oleynik" | 8aa9e57 | 2006-01-25 13:56:03 +0000 | [diff] [blame] | 424 | int op; |
| 425 | arith_t val; |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 426 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 427 | l = eval5(); |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 428 | while (1) { |
Denis Vlasenko | a7f4e4b | 2008-04-02 20:24:09 +0000 | [diff] [blame] | 429 | op = nextarg("*"); |
| 430 | if (!op) { op = nextarg("/"); |
| 431 | if (!op) { op = nextarg("%"); |
| 432 | if (!op) return l; |
| 433 | }} |
Bernhard Reutner-Fischer | adb01b1 | 2007-04-02 16:38:13 +0000 | [diff] [blame] | 434 | G.args++; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 435 | r = eval5(); |
| 436 | val = arithmetic_common(l, r, op); |
| 437 | freev(l); |
| 438 | freev(r); |
| 439 | l = int_value(val); |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 440 | } |
| 441 | } |
| 442 | |
| 443 | /* Handle +, - operators. */ |
| 444 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 445 | static VALUE *eval3(void) |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 446 | { |
| 447 | VALUE *l, *r; |
"Vladimir N. Oleynik" | 8aa9e57 | 2006-01-25 13:56:03 +0000 | [diff] [blame] | 448 | int op; |
| 449 | arith_t val; |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 450 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 451 | l = eval4(); |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 452 | while (1) { |
Denis Vlasenko | a7f4e4b | 2008-04-02 20:24:09 +0000 | [diff] [blame] | 453 | op = nextarg("+"); |
| 454 | if (!op) { |
| 455 | op = nextarg("-"); |
| 456 | if (!op) return l; |
| 457 | } |
Bernhard Reutner-Fischer | adb01b1 | 2007-04-02 16:38:13 +0000 | [diff] [blame] | 458 | G.args++; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 459 | r = eval4(); |
| 460 | val = arithmetic_common(l, r, op); |
| 461 | freev(l); |
| 462 | freev(r); |
| 463 | l = int_value(val); |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 464 | } |
| 465 | } |
| 466 | |
| 467 | /* Handle comparisons. */ |
| 468 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 469 | static VALUE *eval2(void) |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 470 | { |
| 471 | VALUE *l, *r; |
"Vladimir N. Oleynik" | 8aa9e57 | 2006-01-25 13:56:03 +0000 | [diff] [blame] | 472 | int op; |
| 473 | arith_t val; |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 474 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 475 | l = eval3(); |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 476 | while (1) { |
Denis Vlasenko | a7f4e4b | 2008-04-02 20:24:09 +0000 | [diff] [blame] | 477 | op = nextarg("<"); |
| 478 | if (!op) { op = nextarg("<="); |
| 479 | if (!op) { op = nextarg("="); |
| 480 | if (!op) { op = nextarg("=="); |
| 481 | if (!op) { op = nextarg("!="); |
| 482 | if (!op) { op = nextarg(">="); |
| 483 | if (!op) { op = nextarg(">"); |
| 484 | if (!op) return l; |
| 485 | }}}}}} |
Bernhard Reutner-Fischer | adb01b1 | 2007-04-02 16:38:13 +0000 | [diff] [blame] | 486 | G.args++; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 487 | r = eval3(); |
| 488 | toarith(l); |
| 489 | toarith(r); |
| 490 | val = cmp_common(l, r, op); |
| 491 | freev(l); |
| 492 | freev(r); |
| 493 | l = int_value(val); |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 494 | } |
| 495 | } |
| 496 | |
| 497 | /* Handle &. */ |
| 498 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 499 | static VALUE *eval1(void) |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 500 | { |
| 501 | VALUE *l, *r; |
| 502 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 503 | l = eval2(); |
| 504 | while (nextarg("&")) { |
Bernhard Reutner-Fischer | adb01b1 | 2007-04-02 16:38:13 +0000 | [diff] [blame] | 505 | G.args++; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 506 | r = eval2(); |
| 507 | if (null(l) || null(r)) { |
| 508 | freev(l); |
| 509 | freev(r); |
| 510 | l = int_value(0); |
| 511 | } else |
| 512 | freev(r); |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 513 | } |
| 514 | return l; |
| 515 | } |
| 516 | |
| 517 | /* Handle |. */ |
| 518 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 519 | static VALUE *eval(void) |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 520 | { |
| 521 | VALUE *l, *r; |
| 522 | |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 523 | l = eval1(); |
| 524 | while (nextarg("|")) { |
Bernhard Reutner-Fischer | adb01b1 | 2007-04-02 16:38:13 +0000 | [diff] [blame] | 525 | G.args++; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 526 | r = eval1(); |
| 527 | if (null(l)) { |
| 528 | freev(l); |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 529 | l = r; |
Bernhard Reutner-Fischer | 73561cc | 2006-08-28 23:31:54 +0000 | [diff] [blame] | 530 | } else |
| 531 | freev(r); |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 532 | } |
| 533 | return l; |
| 534 | } |
Bernhard Reutner-Fischer | 3c5929a | 2007-04-02 16:41:24 +0000 | [diff] [blame] | 535 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 536 | int expr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | 272710f | 2008-11-11 22:36:58 +0000 | [diff] [blame] | 537 | int expr_main(int argc UNUSED_PARAM, char **argv) |
Bernhard Reutner-Fischer | 3c5929a | 2007-04-02 16:41:24 +0000 | [diff] [blame] | 538 | { |
| 539 | VALUE *v; |
| 540 | |
Denys Vlasenko | 1671424 | 2011-09-21 01:59:15 +0200 | [diff] [blame] | 541 | INIT_G(); |
| 542 | |
Denis Vlasenko | 272710f | 2008-11-11 22:36:58 +0000 | [diff] [blame] | 543 | xfunc_error_retval = 2; /* coreutils compat */ |
| 544 | G.args = argv + 1; |
| 545 | if (*G.args == NULL) { |
Bernhard Reutner-Fischer | 3c5929a | 2007-04-02 16:41:24 +0000 | [diff] [blame] | 546 | bb_error_msg_and_die("too few arguments"); |
| 547 | } |
Bernhard Reutner-Fischer | 3c5929a | 2007-04-02 16:41:24 +0000 | [diff] [blame] | 548 | v = eval(); |
| 549 | if (*G.args) |
| 550 | bb_error_msg_and_die("syntax error"); |
Denis Vlasenko | a7f4e4b | 2008-04-02 20:24:09 +0000 | [diff] [blame] | 551 | if (v->type == INTEGER) |
Bernhard Reutner-Fischer | 3c5929a | 2007-04-02 16:41:24 +0000 | [diff] [blame] | 552 | printf("%" PF_REZ "d\n", PF_REZ_TYPE v->u.i); |
| 553 | else |
| 554 | puts(v->u.s); |
Bernhard Reutner-Fischer | 3c5929a | 2007-04-02 16:41:24 +0000 | [diff] [blame] | 555 | fflush_stdout_and_exit(null(v)); |
| 556 | } |