xref: /minix/bin/ed/re.c (revision 0a6a1f1d)
1 /*	$NetBSD: re.c,v 1.21 2014/03/23 05:06:42 dholland 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.21 2014/03/23 05:06:42 dholland Exp $");
37 #endif
38 #endif /* not lint */
39 
40 #include <stdarg.h>
41 #include "ed.h"
42 
43 
44 char errmsg[MAXPATHLEN + 40] = "";
45 
46 void
seterrmsg(const char * fmt,...)47 seterrmsg(const char *fmt, ...)
48 {
49 	va_list ap;
50 
51 	va_start(ap, fmt);
52 	vsnprintf(errmsg, sizeof(errmsg), fmt, ap);
53 	va_end(ap);
54 }
55 
56 /* get_compiled_pattern: return pointer to compiled pattern from command
57    buffer */
58 pattern_t *
get_compiled_pattern(void)59 get_compiled_pattern(void)
60 {
61 	static pattern_t *expr = NULL;
62 
63 	char *exps;
64 	char delimiter;
65 	int n;
66 
67 	if ((delimiter = *ibufp) == ' ') {
68 		seterrmsg("invalid pattern delimiter");
69 		return NULL;
70 	} else if (delimiter == '\n' || *++ibufp == '\n' || *ibufp == delimiter) {
71 		if (!expr) seterrmsg("no previous pattern");
72 		return expr;
73 	} else if ((exps = extract_pattern(delimiter)) == NULL)
74 		return NULL;
75 	/* buffer alloc'd && not reserved */
76 	if (expr && !patlock)
77 		regfree(expr);
78 	else if ((expr = (pattern_t *) malloc(sizeof(pattern_t))) == NULL) {
79 		fprintf(stderr, "%s\n", strerror(errno));
80 		seterrmsg("out of memory");
81 		return NULL;
82 	}
83 	patlock = 0;
84 	if ((n = regcomp(expr, exps, ere)) != 0) {
85 		regerror(n, expr, errmsg, sizeof errmsg);
86 		free(expr);
87 		return expr = NULL;
88 	}
89 	return expr;
90 }
91 
92 
93 /* extract_pattern: copy a pattern string from the command buffer; return
94    pointer to the copy */
95 char *
extract_pattern(int delimiter)96 extract_pattern(int delimiter)
97 {
98 	static char *lhbuf = NULL;	/* buffer */
99 	static int lhbufsz = 0;		/* buffer size */
100 
101 	char *nd;
102 	int len;
103 
104 	for (nd = ibufp; *nd != delimiter && *nd != '\n'; nd++)
105 		switch (*nd) {
106 		default:
107 			break;
108 		case '[':
109 			if ((nd = parse_char_class(nd + 1)) == NULL) {
110 				seterrmsg("unbalanced brackets ([])");
111 				return NULL;
112 			}
113 			break;
114 		case '\\':
115 			if (*++nd == '\n') {
116 				seterrmsg("trailing backslash (\\)");
117 				return NULL;
118 			}
119 			break;
120 		}
121 	len = nd - ibufp;
122 	REALLOC(lhbuf, lhbufsz, len + 1, NULL);
123 	memcpy(lhbuf, ibufp, len);
124 	lhbuf[len] = '\0';
125 	ibufp = nd;
126 	return (isbinary) ? NUL_TO_NEWLINE(lhbuf, len) : lhbuf;
127 }
128 
129 
130 /* parse_char_class: expand a POSIX character class */
131 char *
parse_char_class(char * s)132 parse_char_class(char *s)
133 {
134 	int c, d;
135 
136 	if (*s == '^')
137 		s++;
138 	if (*s == ']')
139 		s++;
140 	for (; *s != ']' && *s != '\n'; s++)
141 		if (*s == '[' && ((d = *(s+1)) == '.' || d == ':' || d == '='))
142 			for (s++, c = *++s; *s != ']' || c != d; s++)
143 				if ((c = *s) == '\n')
144 					return NULL;
145 	return  (*s == ']') ? s : NULL;
146 }
147