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