xref: /netbsd-src/bin/ed/sub.c (revision a5847cc334d9a7029f6352b847e9e8d71a0f9e0c)
1 /*	$NetBSD: sub.c,v 1.6 2005/02/17 16:29:26 xtraeme Exp $	*/
2 
3 /* sub.c: This file contains the substitution routines for the ed
4    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 = "@(#)sub.c,v 1.1 1994/02/01 00:34:44 alm Exp";
35 #else
36 __RCSID("$NetBSD: sub.c,v 1.6 2005/02/17 16:29:26 xtraeme Exp $");
37 #endif
38 #endif /* not lint */
39 
40 #include "ed.h"
41 
42 
43 char *rhbuf;			/* rhs substitution buffer */
44 int rhbufsz;			/* rhs substitution buffer size */
45 int rhbufi;			/* rhs substitution buffer index */
46 
47 /* extract_subst_tail: extract substitution tail from the command buffer */
48 int
49 extract_subst_tail(int *flagp, long *np)
50 {
51 	char delimiter;
52 
53 	*flagp = *np = 0;
54 	if ((delimiter = *ibufp) == '\n') {
55 		rhbufi = 0;
56 		*flagp = GPR;
57 		return 0;
58 	} else if (extract_subst_template() == NULL)
59 		return  ERR;
60 	else if (*ibufp == '\n') {
61 		*flagp = GPR;
62 		return 0;
63 	} else if (*ibufp == delimiter)
64 		ibufp++;
65 	if ('1' <= *ibufp && *ibufp <= '9') {
66 		STRTOL(*np, ibufp);
67 		return 0;
68 	} else if (*ibufp == 'g') {
69 		ibufp++;
70 		*flagp = GSG;
71 		return 0;
72 	}
73 	return 0;
74 }
75 
76 
77 /* extract_subst_template: return pointer to copy of substitution template
78    in the command buffer */
79 char *
80 extract_subst_template(void)
81 {
82 	int n = 0;
83 	int i = 0;
84 	char c;
85 	char delimiter = *ibufp++;
86 
87 	if (*ibufp == '%' && *(ibufp + 1) == delimiter) {
88 		ibufp++;
89 		if (!rhbuf) sprintf(errmsg, "no previous substitution");
90 		return rhbuf;
91 	}
92 	while (*ibufp != delimiter) {
93 		REALLOC(rhbuf, rhbufsz, i + 2, NULL);
94 		if ((c = rhbuf[i++] = *ibufp++) == '\n' && *ibufp == '\0') {
95 			i--, ibufp--;
96 			break;
97 		} else if (c != '\\')
98 			;
99 		else if ((rhbuf[i++] = *ibufp++) != '\n')
100 			;
101 		else if (!isglobal) {
102 			while ((n = get_tty_line()) == 0 ||
103 			    (n > 0 && ibuf[n - 1] != '\n'))
104 				clearerr(stdin);
105 			if (n < 0)
106 				return NULL;
107 		}
108 	}
109 	REALLOC(rhbuf, rhbufsz, i + 1, NULL);
110 	rhbuf[rhbufi = i] = '\0';
111 	return  rhbuf;
112 }
113 
114 
115 char *rbuf;			/* substitute_matching_text buffer */
116 int rbufsz;			/* substitute_matching_text buffer size */
117 
118 /* search_and_replace: for each line in a range, change text matching a pattern
119    according to a substitution template; return status  */
120 int
121 search_and_replace(pattern_t *pat, int gflag, int kth)
122 {
123 	undo_t *up;
124 	char *txt;
125 	char *eot;
126 	long lc;
127 	long xa = current_addr;
128 	int nsubs = 0;
129 	line_t *lp;
130 	int len;
131 
132 	current_addr = first_addr - 1;
133 	for (lc = 0; lc <= second_addr - first_addr; lc++) {
134 		lp = get_addressed_line_node(++current_addr);
135 		if ((len = substitute_matching_text(pat, lp, gflag, kth)) < 0)
136 			return ERR;
137 		else if (len) {
138 			up = NULL;
139 			if (delete_lines(current_addr, current_addr) < 0)
140 				return ERR;
141 			txt = rbuf;
142 			eot = rbuf + len;
143 			SPL1();
144 			do {
145 				if ((txt = put_sbuf_line(txt)) == NULL) {
146 					SPL0();
147 					return ERR;
148 				} else if (up)
149 					up->t = get_addressed_line_node(current_addr);
150 				else if ((up = push_undo_stack(UADD,
151 				    current_addr, current_addr)) == NULL) {
152 					SPL0();
153 					return ERR;
154 				}
155 			} while (txt != eot);
156 			SPL0();
157 			nsubs++;
158 			xa = current_addr;
159 		}
160 	}
161 	current_addr = xa;
162 	if  (nsubs == 0 && !(gflag & GLB)) {
163 		sprintf(errmsg, "no match");
164 		return ERR;
165 	} else if ((gflag & (GPR | GLS | GNP)) &&
166 	    display_lines(current_addr, current_addr, gflag) < 0)
167 		return ERR;
168 	return 0;
169 }
170 
171 
172 /* substitute_matching_text: replace text matched by a pattern according to
173    a substitution template; return pointer to the modified text */
174 int
175 substitute_matching_text(pattern_t *pat, line_t *lp, int gflag, int kth)
176 {
177 	int off = 0;
178 	int changed = 0;
179 	int matchno = 0;
180 	int i = 0;
181 	regmatch_t rm[SE_MAX];
182 	char *txt;
183 	char *eot;
184 
185 	if ((txt = get_sbuf_line(lp)) == NULL)
186 		return ERR;
187 	if (isbinary)
188 		NUL_TO_NEWLINE(txt, lp->len);
189 	eot = txt + lp->len;
190 	if (!regexec(pat, txt, SE_MAX, rm, 0)) {
191 		do {
192 			if (!kth || kth == ++matchno) {
193 				changed++;
194 				i = rm[0].rm_so;
195 				REALLOC(rbuf, rbufsz, off + i, ERR);
196 				if (isbinary)
197 					NEWLINE_TO_NUL(txt, rm[0].rm_eo);
198 				memcpy(rbuf + off, txt, i);
199 				off += i;
200 				if ((off = apply_subst_template(txt, rm, off,
201 				    pat->re_nsub)) < 0)
202 					return ERR;
203 			} else {
204 				i = rm[0].rm_eo;
205 				REALLOC(rbuf, rbufsz, off + i, ERR);
206 				if (isbinary)
207 					NEWLINE_TO_NUL(txt, i);
208 				memcpy(rbuf + off, txt, i);
209 				off += i;
210 			}
211 			txt += rm[0].rm_eo;
212 		} while (*txt && (!changed || ((gflag & GSG) && rm[0].rm_eo))
213 		    && !regexec(pat, txt, SE_MAX, rm, REG_NOTBOL));
214 		i = eot - txt;
215 		REALLOC(rbuf, rbufsz, off + i + 2, ERR);
216 		if (i > 0 && !rm[0].rm_eo && (gflag & GSG)) {
217 			sprintf(errmsg, "infinite substitution loop");
218 			return  ERR;
219 		}
220 		if (isbinary)
221 			NEWLINE_TO_NUL(txt, i);
222 		memcpy(rbuf + off, txt, i);
223 		memcpy(rbuf + off + i, "\n", 2);
224 	}
225 	return changed ? off + i + 1 : 0;
226 }
227 
228 
229 /* apply_subst_template: modify text according to a substitution template;
230    return offset to end of modified text */
231 int
232 apply_subst_template(char *boln, regmatch_t *rm, int off, int re_nsub)
233 {
234 	int j = 0;
235 	int k = 0;
236 	int n;
237 	char *sub = rhbuf;
238 
239 	for (; sub - rhbuf < rhbufi; sub++)
240 		if (*sub == '&') {
241 			j = rm[0].rm_so;
242 			k = rm[0].rm_eo;
243 			REALLOC(rbuf, rbufsz, off + k - j, ERR);
244 			while (j < k)
245 				rbuf[off++] = boln[j++];
246 		} else if (*sub == '\\' && '1' <= *++sub && *sub <= '9' &&
247 		    (n = *sub - '0') <= re_nsub) {
248 			j = rm[n].rm_so;
249 			k = rm[n].rm_eo;
250 			REALLOC(rbuf, rbufsz, off + k - j, ERR);
251 			while (j < k)
252 				rbuf[off++] = boln[j++];
253 		} else {
254 			REALLOC(rbuf, rbufsz, off + 1, ERR);
255 			rbuf[off++] = *sub;
256 		}
257 	REALLOC(rbuf, rbufsz, off + 1, ERR);
258 	rbuf[off] = '\0';
259 	return off;
260 }
261