diff options
author | Ed Schouten <ed@FreeBSD.org> | 2010-03-14 10:18:58 +0000 |
---|---|---|
committer | Ed Schouten <ed@FreeBSD.org> | 2010-03-14 10:18:58 +0000 |
commit | 763ed733711a5bb66d39de4d0ed4bcaae77e2461 (patch) | |
tree | c365d6fb8c2688382e96810f92cb4dc54afb61d5 /lib/libcompat/4.3 | |
parent | c9bb666937cdecbe2dc8b68103edd79f0289da07 (diff) | |
download | src-763ed733711a5bb66d39de4d0ed4bcaae77e2461.tar.gz src-763ed733711a5bb66d39de4d0ed4bcaae77e2461.zip |
Trim down libcompat by removing <regexp.h>.
Erwin ran an exp-run with libcompat and <regexp.h> removed. It turns out
the regexp library is almost entirely unused. In fact, it looks like it
is sometimes used by accident. Because these function names clash with
libc's <regex.h>, some application use both <regex.h> and libcompat,
which means they link against the wrong regex library.
This commit removes the regexp library and reimplements re_comp() and
re_exec() using <regex.h>. It seems the grammar of the regular
expressions accepted by these functions is similar to POSIX EREs.
After this commit, 1 low-profile port will be broken, but the maintainer
already has a patch for it sitting in his mailbox.
Notes
Notes:
svn path=/head/; revision=205146
Diffstat (limited to 'lib/libcompat/4.3')
-rw-r--r-- | lib/libcompat/4.3/re_comp.3 | 13 | ||||
-rw-r--r-- | lib/libcompat/4.3/re_comp.c (renamed from lib/libcompat/4.3/regex.c) | 62 |
2 files changed, 35 insertions, 40 deletions
diff --git a/lib/libcompat/4.3/re_comp.3 b/lib/libcompat/4.3/re_comp.3 index e3b595a4457a..4d970facb091 100644 --- a/lib/libcompat/4.3/re_comp.3 +++ b/lib/libcompat/4.3/re_comp.3 @@ -100,15 +100,10 @@ returns \-1 for an internal error. The .Fn re_comp function -returns one of the following strings if an error occurs: -.Bd -unfilled -offset indent -No previous regular expression, -Regular expression too long, -unmatched \e(, -missing ], -too many \e(\e) pairs, -unmatched \e). -.Ed +returns +.Dq no previous regular expression +or one of the strings generated by +.Xr regerror 3 . .Sh SEE ALSO .Xr ed 1 , .Xr egrep 1 , diff --git a/lib/libcompat/4.3/regex.c b/lib/libcompat/4.3/re_comp.c index 470cab52e779..dbe57b129973 100644 --- a/lib/libcompat/4.3/regex.c +++ b/lib/libcompat/4.3/re_comp.c @@ -44,49 +44,49 @@ __FBSDID("$FreeBSD$"); static char sccsid[] = "@(#)regex.c 5.1 (Berkeley) 3/29/92"; #endif /* LIBC_SCCS and not lint */ -#include <sys/types.h> +#include <regex.h> #include <stddef.h> -#include <regexp.h> -#include <string.h> -#include <stdlib.h> -#include <string.h> +#include <unistd.h> -static regexp *re_regexp; -static int re_goterr; -static char *re_errstr; +static regex_t re_regexp; +static int re_gotexp; +static char re_errstr[100]; char * -re_comp(char *s) +re_comp(const char *s) { + int rc; + if (s == NULL || *s == '\0') { - if (re_regexp == NULL) - return "no previous regular expression"; + if (!re_gotexp) + return __DECONST(char *, + "no previous regular expression"); return (NULL); } - if (re_regexp) - free(re_regexp); - if (re_errstr) - free(re_errstr); - re_goterr = 0; - re_regexp = regcomp(s); - return (re_goterr ? re_errstr : NULL); + + if (re_gotexp) { + regfree(&re_regexp); + re_gotexp = 0; + } + + rc = regcomp(&re_regexp, s, REG_EXTENDED); + if (rc == 0) { + re_gotexp = 1; + return (NULL); + } + + regerror(rc, &re_regexp, re_errstr, sizeof(re_errstr)); + re_errstr[sizeof(re_errstr) - 1] = '\0'; + return (re_errstr); } int -re_exec(char *s) +re_exec(const char *s) { int rc; - re_goterr = 0; - rc = regexec(re_regexp, s); - return (re_goterr ? -1 : rc); -} - -void -regerror(const char *s) -{ - re_goterr = 1; - if (re_errstr) - free(re_errstr); - re_errstr = strdup(s); + if (!re_gotexp) + return (-1); + rc = regexec(&re_regexp, s, 0, NULL, 0); + return (rc == 0 ? 1 : 0); } |