[BusyBox] expr: use POSIX regex [PATCH]
Shaun Jackman
sjackman at gmail.com
Tue Apr 5 23:14:24 UTC 2005
On Apr 5, 2005 2:10 PM, Rainer Weikusat <rainer.weikusat at sncag.com> wrote:
> Shaun Jackman <sjackman at gmail.com> writes:
> > On Apr 5, 2005 10:15 AM, Rob Landley <rob at landley.net> wrote:
> >> On Tuesday 05 April 2005 01:48 pm, Shaun Jackman wrote:
> >> > This patch modfies expr to use portable POSIX regex rather than BSD regex.
> >>
> >> Sounds cool. Is there a functional difference, or is this just syntax? (I
> >> suspect that in uclibc both are wrappers over the same functionality, but
> >> haven't confirmed it.)
> >>
> >> I just checked, and sed.c is using the posix regex already...
> >>
> >> Rob
> >
> > I believe it's primarily a syntax difference. I just noticed though
> > that expr match behaves as if it has an implicit ^ before the regex,
> > i.e. it's an anchored regex.
>
> That's what it's supposed to be according to POSIX.
This updated patch implements an anchored regex by checking that the
match starts at offset 0.
Cheers,
Shaun
2005-04-05 Shaun Jackman <sjackman at gmail.com>
* coreutils/expr.c (docolon): Use POSIX regex.
Index: coreutils/expr.c
===================================================================
--- coreutils/expr.c (revision 10071)
+++ coreutils/expr.c (working copy)
@@ -245,10 +245,9 @@
static VALUE *docolon (VALUE *sv, VALUE *pv)
{
VALUE *v;
- const char *errmsg;
- struct re_pattern_buffer re_buffer;
- struct re_registers re_regs;
- int len;
+ regex_t re_buffer;
+ const int NMATCH = 2;
+ regmatch_t re_regs[NMATCH];
tostring (sv);
tostring (pv);
@@ -260,27 +259,22 @@
pv->u.s);
}
- len = strlen (pv->u.s);
memset (&re_buffer, 0, sizeof (re_buffer));
- memset (&re_regs, 0, sizeof (re_regs));
- re_buffer.allocated = 2 * len;
- re_buffer.buffer = (unsigned char *) xmalloc (re_buffer.allocated);
- re_buffer.translate = 0;
- re_syntax_options = RE_SYNTAX_POSIX_BASIC;
- errmsg = re_compile_pattern (pv->u.s, len, &re_buffer);
- if (errmsg) {
- bb_error_msg_and_die("%s", errmsg);
- }
+ memset (re_regs, 0, sizeof (*re_regs));
+ if( regcomp (&re_buffer, pv->u.s, 0) != 0 )
+ bb_error_msg_and_die("Invalid regular expression");
- len = re_match (&re_buffer, sv->u.s, strlen (sv->u.s), 0, &re_regs);
- if (len >= 0) {
+ /* expr uses an anchored pattern match, so check that there was a
+ * match and that the match starts at offset 0. */
+ if (regexec (&re_buffer, sv->u.s, NMATCH, re_regs, 0) != REG_NOMATCH &&
+ re_regs[0].rm_so == 0) {
/* Were \(...\) used? */
- if (re_buffer.re_nsub > 0) { /* was (re_regs.start[1] >= 0) */
- sv->u.s[re_regs.end[1]] = '\0';
- v = str_value (sv->u.s + re_regs.start[1]);
+ if (re_buffer.re_nsub > 0) {
+ sv->u.s[re_regs[1].rm_eo] = '\0';
+ v = str_value (sv->u.s + re_regs[1].rm_so);
}
else
- v = int_value (len);
+ v = int_value (re_regs[0].rm_eo);
}
else {
/* Match failed -- return the right kind of null. */
@@ -289,7 +283,6 @@
else
v = int_value (0);
}
- free (re_buffer.buffer);
return v;
}
More information about the busybox
mailing list