source: MondoRescue/branches/2.2.9/mindi-busybox/coreutils/expr.c@ 2725

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