lineedit: do not hang on error, but return error indicator.
[oweals/busybox.git] / coreutils / expr.c
index d659b670bebc9dae0d269afa978e1f731af0eeb5..04d783f2b58fc21d050fdf9475abb8ef3e81a8f2 100644 (file)
@@ -11,7 +11,7 @@
  *  - reduced 464 bytes.
  *  - 64 math support
  *
- * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
+ * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  */
 
 /* This program evaluates expressions.  Each token (operator, operand,
@@ -63,7 +63,7 @@ typedef struct valinfo VALUE;
 /* The arguments given to the program, minus the program name.  */
 struct globals {
        char **args;
-};
+} FIX_ALIASING;
 #define G (*(struct globals*)&bb_common_bufsiz1)
 
 /* forward declarations */
@@ -214,22 +214,22 @@ static arith_t arithmetic_common(VALUE *l, VALUE *r, int op)
 
 static VALUE *docolon(VALUE *sv, VALUE *pv)
 {
+       enum { NMATCH = 2 };
        VALUE *v;
        regex_t re_buffer;
-       const int NMATCH = 2;
        regmatch_t re_regs[NMATCH];
 
        tostring(sv);
        tostring(pv);
 
        if (pv->u.s[0] == '^') {
-               bb_error_msg("\
-warning: unportable BRE: `%s': using `^' as the first character\n\
-of a basic regular expression is not portable; it is being ignored", pv->u.s);
+               bb_error_msg(
+"warning: '%s': using '^' as the first character\n"
+"of a basic regular expression is not portable; it is ignored", pv->u.s);
        }
 
        memset(&re_buffer, 0, sizeof(re_buffer));
-       memset(re_regs, 0, sizeof(*re_regs));
+       memset(re_regs, 0, sizeof(re_regs));
        xregcomp(&re_buffer, pv->u.s, 0);
 
        /* expr uses an anchored pattern match, so check that there was a
@@ -238,7 +238,7 @@ of a basic regular expression is not portable; it is being ignored", pv->u.s);
         && re_regs[0].rm_so == 0
        ) {
                /* Were \(...\) used? */
-               if (re_buffer.re_nsub > 0) {
+               if (re_buffer.re_nsub > 0 && re_regs[1].rm_so >= 0) {
                        sv->u.s[re_regs[1].rm_eo] = '\0';
                        v = str_value(sv->u.s + re_regs[1].rm_so);
                } else {
@@ -251,7 +251,7 @@ of a basic regular expression is not portable; it is being ignored", pv->u.s);
                else
                        v = int_value(0);
        }
-//FIXME: sounds like here is a bit missing: regfree(&re_buffer);
+       regfree(&re_buffer);
        return v;
 }
 
@@ -341,7 +341,6 @@ static VALUE *eval6(void)
                freev(i2);
        }
        return v;
-
 }
 
 /* Handle : operator (pattern matching).