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