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