regex.c revision 1.12 1 1.1 nate /*-
2 1.1 nate * Copyright (c) 1992 The Regents of the University of California.
3 1.1 nate * All rights reserved.
4 1.1 nate *
5 1.1 nate * This code is derived from software contributed to Berkeley by
6 1.1 nate * James da Silva at the University of Maryland at College Park.
7 1.1 nate *
8 1.1 nate * Redistribution and use in source and binary forms, with or without
9 1.1 nate * modification, are permitted provided that the following conditions
10 1.1 nate * are met:
11 1.1 nate * 1. Redistributions of source code must retain the above copyright
12 1.1 nate * notice, this list of conditions and the following disclaimer.
13 1.1 nate * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 nate * notice, this list of conditions and the following disclaimer in the
15 1.1 nate * documentation and/or other materials provided with the distribution.
16 1.12 agc * 3. Neither the name of the University nor the names of its contributors
17 1.1 nate * may be used to endorse or promote products derived from this software
18 1.1 nate * without specific prior written permission.
19 1.1 nate *
20 1.1 nate * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 1.1 nate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 1.1 nate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 1.1 nate * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 1.1 nate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 1.1 nate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 1.1 nate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 1.1 nate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 1.1 nate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 1.1 nate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 1.1 nate * SUCH DAMAGE.
31 1.1 nate */
32 1.1 nate
33 1.1 nate /*
34 1.1 nate * Compatibility routines that implement the old re_comp/re_exec interface in
35 1.1 nate * terms of the regcomp/regexec interface. It's possible that some programs
36 1.1 nate * rely on dark corners of re_comp/re_exec and won't work with this version,
37 1.1 nate * but most programs should be fine.
38 1.1 nate */
39 1.1 nate
40 1.7 lukem #include <sys/cdefs.h>
41 1.1 nate #if defined(LIBC_SCCS) && !defined(lint)
42 1.7 lukem #if 0
43 1.7 lukem static char sccsid[] = "from: @(#)regex.c 5.1 (Berkeley) 3/29/92";*/
44 1.7 lukem #else
45 1.12 agc __RCSID("$NetBSD: regex.c,v 1.12 2003/08/07 16:44:16 agc Exp $");
46 1.7 lukem #endif
47 1.1 nate #endif /* LIBC_SCCS and not lint */
48 1.1 nate
49 1.1 nate #include <sys/types.h>
50 1.1 nate #include <stddef.h>
51 1.9 christos #ifdef __lint__
52 1.9 christos #define __compat_regerror __compat43_regerror
53 1.9 christos #endif
54 1.10 lukem #include <assert.h>
55 1.4 jtc #include <regexp.h>
56 1.8 kleink #include <re_comp.h>
57 1.4 jtc #include <string.h>
58 1.3 jtc #include <stdlib.h>
59 1.7 lukem #include <unistd.h>
60 1.1 nate
61 1.4 jtc static regexp *re_regexp;
62 1.4 jtc static int re_goterr;
63 1.4 jtc static char *re_errstr;
64 1.1 nate
65 1.1 nate char *
66 1.1 nate re_comp(s)
67 1.5 pk const char *s;
68 1.1 nate {
69 1.6 jtc if (s == NULL || *s == '\0')
70 1.1 nate return (NULL);
71 1.4 jtc if (re_regexp)
72 1.4 jtc free(re_regexp);
73 1.4 jtc if (re_errstr)
74 1.4 jtc free(re_errstr);
75 1.4 jtc re_goterr = 0;
76 1.4 jtc re_regexp = regcomp(s);
77 1.4 jtc return (re_goterr ? re_errstr : NULL);
78 1.1 nate }
79 1.1 nate
80 1.1 nate int
81 1.1 nate re_exec(s)
82 1.5 pk const char *s;
83 1.1 nate {
84 1.4 jtc int rc;
85 1.1 nate
86 1.10 lukem
87 1.4 jtc re_goterr = 0;
88 1.4 jtc rc = regexec(re_regexp, s);
89 1.4 jtc return (re_goterr ? -1 : rc);
90 1.4 jtc }
91 1.1 nate
92 1.4 jtc void
93 1.4 jtc regerror(s)
94 1.4 jtc const char *s;
95 1.4 jtc {
96 1.10 lukem
97 1.10 lukem _DIAGASSERT(s != NULL);
98 1.10 lukem
99 1.4 jtc re_goterr = 1;
100 1.4 jtc if (re_errstr)
101 1.4 jtc free(re_errstr);
102 1.4 jtc re_errstr = strdup(s);
103 1.1 nate }
104