xref: /freebsd/bin/ed/re.c (revision d37eb51047221dc3322b34db1038ff3aa533883f)
1 /* re.c: This file contains the regular expression interface routines for
2    the ed line editor. */
3 /*-
4  * Copyright (c) 1993 Andrew Moore, Talke Studio.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 #include "ed.h"
31 
32 const char *errmsg = "";
33 
34 /* get_compiled_pattern: return pointer to compiled pattern from command
35    buffer */
36 pattern_t *
37 get_compiled_pattern(void)
38 {
39 	static pattern_t *expr = NULL;
40 	static char error[1024];
41 
42 	char *exprs;
43 	char delimiter;
44 	int n;
45 
46 	if ((delimiter = *ibufp) == ' ') {
47 		errmsg = "invalid pattern delimiter";
48 		return NULL;
49 	} else if (delimiter == '\n' || *++ibufp == '\n' || *ibufp == delimiter) {
50 		if (!expr)
51 			errmsg = "no previous pattern";
52 		return expr;
53 	} else if ((exprs = extract_pattern(delimiter)) == NULL)
54 		return NULL;
55 	/* buffer alloc'd && not reserved */
56 	if (expr && !patlock)
57 		regfree(expr);
58 	else if ((expr = (pattern_t *) malloc(sizeof(pattern_t))) == NULL) {
59 		fprintf(stderr, "%s\n", strerror(errno));
60 		errmsg = "out of memory";
61 		return NULL;
62 	}
63 	patlock = 0;
64 	if ((n = regcomp(expr, exprs, 0))) {
65 		regerror(n, expr, error, sizeof error);
66 		errmsg = error;
67 		free(expr);
68 		return expr = NULL;
69 	}
70 	return expr;
71 }
72 
73 
74 /* extract_pattern: copy a pattern string from the command buffer; return
75    pointer to the copy */
76 char *
77 extract_pattern(int delimiter)
78 {
79 	static char *lhbuf = NULL;	/* buffer */
80 	static int lhbufsz = 0;		/* buffer size */
81 
82 	char *nd;
83 	int len;
84 
85 	for (nd = ibufp; *nd != delimiter && *nd != '\n'; nd++)
86 		switch (*nd) {
87 		default:
88 			break;
89 		case '[':
90 			if ((nd = parse_char_class(nd + 1)) == NULL) {
91 				errmsg = "unbalanced brackets ([])";
92 				return NULL;
93 			}
94 			break;
95 		case '\\':
96 			if (*++nd == '\n') {
97 				errmsg = "trailing backslash (\\)";
98 				return NULL;
99 			}
100 			break;
101 		}
102 	len = nd - ibufp;
103 	REALLOC(lhbuf, lhbufsz, len + 1, NULL);
104 	memcpy(lhbuf, ibufp, len);
105 	lhbuf[len] = '\0';
106 	ibufp = nd;
107 	return (isbinary) ? NUL_TO_NEWLINE(lhbuf, len) : lhbuf;
108 }
109 
110 
111 /* parse_char_class: expand a POSIX character class */
112 char *
113 parse_char_class(char *s)
114 {
115 	int c, d;
116 
117 	if (*s == '^')
118 		s++;
119 	if (*s == ']')
120 		s++;
121 	for (; *s != ']' && *s != '\n'; s++)
122 		if (*s == '[' && ((d = *(s+1)) == '.' || d == ':' || d == '='))
123 			for (s++, c = *++s; *s != ']' || c != d; s++)
124 				if ((c = *s) == '\n')
125 					return NULL;
126 	return  (*s == ']') ? s : NULL;
127 }
128