1 /* re.c: This file contains the regular expression interface routines for
4 * Copyright (c) 1993 Andrew Moore, Talke Studio.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * @(#)re.c,v 1.6 1994/02/01 00:34:43 alm Exp
29 * $FreeBSD: src/bin/ed/re.c,v 1.20 2003/07/20 10:24:09 ru Exp $
30 * $DragonFly: src/bin/ed/re.c,v 1.4 2007/04/06 23:36:54 pavalos Exp $
38 const char *errmsg
= "";
40 /* get_compiled_pattern: return pointer to compiled pattern from command
43 get_compiled_pattern(void)
45 static pattern_t
*expr
= NULL
;
46 static char error
[1024];
52 if ((delimiter
= *ibufp
) == ' ') {
53 errmsg
= "invalid pattern delimiter";
55 } else if (delimiter
== '\n' || *++ibufp
== '\n' || *ibufp
== delimiter
) {
57 errmsg
= "no previous pattern";
59 } else if ((exprs
= extract_pattern(delimiter
)) == NULL
)
61 /* buffer alloc'd && not reserved */
64 else if ((expr
= (pattern_t
*) malloc(sizeof(pattern_t
))) == NULL
) {
65 fprintf(stderr
, "%s\n", strerror(errno
));
66 errmsg
= "out of memory";
70 if ((n
= regcomp(expr
, exprs
, 0))) {
71 regerror(n
, expr
, error
, sizeof error
);
80 /* extract_pattern: copy a pattern string from the command buffer; return
81 pointer to the copy */
83 extract_pattern(int delimiter
)
85 static char *lhbuf
= NULL
; /* buffer */
86 static int lhbufsz
= 0; /* buffer size */
91 for (nd
= ibufp
; *nd
!= delimiter
&& *nd
!= '\n'; nd
++)
96 if ((nd
= parse_char_class(++nd
)) == NULL
) {
97 errmsg
= "unbalanced brackets ([])";
103 errmsg
= "trailing backslash (\\)";
109 REALLOC(lhbuf
, lhbufsz
, len
+ 1, NULL
);
110 memcpy(lhbuf
, ibufp
, len
);
113 return (isbinary
) ? NUL_TO_NEWLINE(lhbuf
, len
) : lhbuf
;
117 /* parse_char_class: expand a POSIX character class */
119 parse_char_class(char *s
)
127 for (; *s
!= ']' && *s
!= '\n'; s
++)
128 if (*s
== '[' && ((d
= *(s
+1)) == '.' || d
== ':' || d
== '='))
129 for (s
++, c
= *++s
; *s
!= ']' || c
!= d
; s
++)
130 if ((c
= *s
) == '\n')
132 return (*s
== ']') ? s
: NULL
;