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