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