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