re.c revision 1.15 1 /* $NetBSD: re.c,v 1.15 1997/07/20 06:35:40 thorpej Exp $ */
2
3 /* re.c: This file contains the regular expression interface routines for
4 the ed line editor. */
5 /*-
6 * Copyright (c) 1993 Andrew Moore, Talke Studio.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31 #include <sys/cdefs.h>
32 #ifndef lint
33 #if 0
34 static char *rcsid = "@(#)re.c,v 1.6 1994/02/01 00:34:43 alm Exp";
35 #else
36 __RCSID("$NetBSD: re.c,v 1.15 1997/07/20 06:35:40 thorpej Exp $");
37 #endif
38 #endif /* not lint */
39
40 #include "ed.h"
41
42
43 extern int patlock;
44
45 char errmsg[MAXPATHLEN + 40] = "";
46
47 /* get_compiled_pattern: return pointer to compiled pattern from command
48 buffer */
49 pattern_t *
50 get_compiled_pattern()
51 {
52 static pattern_t *exp = NULL;
53
54 char *exps;
55 char delimiter;
56 int n;
57
58 if ((delimiter = *ibufp) == ' ') {
59 sprintf(errmsg, "invalid pattern delimiter");
60 return NULL;
61 } else if (delimiter == '\n' || *++ibufp == '\n' || *ibufp == delimiter) {
62 if (!exp) sprintf(errmsg, "no previous pattern");
63 return exp;
64 } else if ((exps = extract_pattern(delimiter)) == NULL)
65 return NULL;
66 /* buffer alloc'd && not reserved */
67 if (exp && !patlock)
68 regfree(exp);
69 else if ((exp = (pattern_t *) malloc(sizeof(pattern_t))) == NULL) {
70 fprintf(stderr, "%s\n", strerror(errno));
71 sprintf(errmsg, "out of memory");
72 return NULL;
73 }
74 patlock = 0;
75 if ((n = regcomp(exp, exps, 0)) != 0) {
76 regerror(n, exp, errmsg, sizeof errmsg);
77 free(exp);
78 return exp = NULL;
79 }
80 return exp;
81 }
82
83
84 /* extract_pattern: copy a pattern string from the command buffer; return
85 pointer to the copy */
86 char *
87 extract_pattern(delimiter)
88 int delimiter;
89 {
90 static char *lhbuf = NULL; /* buffer */
91 static int lhbufsz = 0; /* buffer size */
92
93 char *nd;
94 int len;
95
96 for (nd = ibufp; *nd != delimiter && *nd != '\n'; nd++)
97 switch (*nd) {
98 default:
99 break;
100 case '[':
101 if ((nd = parse_char_class(++nd)) == NULL) {
102 sprintf(errmsg, "unbalanced brackets ([])");
103 return NULL;
104 }
105 break;
106 case '\\':
107 if (*++nd == '\n') {
108 sprintf(errmsg, "trailing backslash (\\)");
109 return NULL;
110 }
111 break;
112 }
113 len = nd - ibufp;
114 REALLOC(lhbuf, lhbufsz, len + 1, NULL);
115 memcpy(lhbuf, ibufp, len);
116 lhbuf[len] = '\0';
117 ibufp = nd;
118 return (isbinary) ? NUL_TO_NEWLINE(lhbuf, len) : lhbuf;
119 }
120
121
122 /* parse_char_class: expand a POSIX character class */
123 char *
124 parse_char_class(s)
125 char *s;
126 {
127 int c, d;
128
129 if (*s == '^')
130 s++;
131 if (*s == ']')
132 s++;
133 for (; *s != ']' && *s != '\n'; s++)
134 if (*s == '[' && ((d = *(s+1)) == '.' || d == ':' || d == '='))
135 for (s++, c = *++s; *s != ']' || c != d; s++)
136 if ((c = *s) == '\n')
137 return NULL;
138 return (*s == ']') ? s : NULL;
139 }
140