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