1 /*	$OpenBSD: sub.c,v 1.11 2009/10/27 23:59:21 deraadt Exp $	*/
2 /*	$NetBSD: sub.c,v 1.4 1995/03/21 09:04:50 cgd Exp $	*/
3 
4 /* sub.c: This file contains the substitution routines for the ed
5    line editor */
6 /*-
7  * Copyright (c) 1993 Andrew Moore, Talke Studio.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include "ed.h"
33 
34 
35 char *rhbuf;			/* rhs substitution buffer */
36 int rhbufsz;			/* rhs substitution buffer size */
37 int rhbufi;			/* rhs substitution buffer index */
38 
39 /* extract_subst_tail: extract substitution tail from the command buffer */
40 int
extract_subst_tail(int * flagp,int * np)41 extract_subst_tail(int *flagp, int *np)
42 {
43 	char delimiter;
44 
45 	*flagp = *np = 0;
46 	if ((delimiter = *ibufp) == '\n') {
47 		rhbufi = 0;
48 		*flagp = GPR;
49 		return 0;
50 	} else if (extract_subst_template() == NULL)
51 		return  ERR;
52 	else if (*ibufp == '\n') {
53 		*flagp = GPR;
54 		return 0;
55 	} else if (*ibufp == delimiter)
56 		ibufp++;
57 	if ('1' <= *ibufp && *ibufp <= '9') {
58 		STRTOI(*np, ibufp);
59 		return 0;
60 	} else if (*ibufp == 'g') {
61 		ibufp++;
62 		*flagp = GSG;
63 		return 0;
64 	}
65 	return 0;
66 }
67 
68 
69 /* extract_subst_template: return pointer to copy of substitution template
70    in the command buffer */
71 char *
extract_subst_template(void)72 extract_subst_template(void)
73 {
74 	int n = 0;
75 	int i = 0;
76 	char c;
77 	char delimiter = *ibufp++;
78 
79 	if (*ibufp == '%' && *(ibufp + 1) == delimiter) {
80 		ibufp++;
81 		if (!rhbuf)
82 			seterrmsg("no previous substitution");
83 		return rhbuf;
84 	}
85 	while (*ibufp != delimiter) {
86 		REALLOC(rhbuf, rhbufsz, i + 2, NULL);
87 		if ((c = rhbuf[i++] = *ibufp++) == '\n' && *ibufp == '\0') {
88 			i--, ibufp--;
89 			break;
90 		} else if (c != '\\')
91 			;
92 		else if ((rhbuf[i++] = *ibufp++) != '\n')
93 			;
94 		else if (!isglobal) {
95 			while ((n = get_tty_line()) == 0 ||
96 			    (n > 0 && ibuf[n - 1] != '\n'))
97 				clearerr(stdin);
98 			if (n < 0)
99 				return NULL;
100 		}
101 	}
102 	REALLOC(rhbuf, rhbufsz, i + 1, NULL);
103 	rhbuf[rhbufi = i] = '\0';
104 	return  rhbuf;
105 }
106 
107 
108 char *rbuf;			/* substitute_matching_text buffer */
109 int rbufsz;			/* substitute_matching_text buffer size */
110 
111 /* search_and_replace: for each line in a range, change text matching a pattern
112    according to a substitution template; return status  */
113 int
search_and_replace(pattern_t * pat,int gflag,int kth)114 search_and_replace(pattern_t *pat, int gflag, int kth)
115 {
116 	undo_t *up;
117 	char *txt;
118 	char *eot;
119 	int lc;
120 	int xa = current_addr;
121 	int nsubs = 0;
122 	line_t *lp;
123 	int len;
124 
125 	current_addr = first_addr - 1;
126 	for (lc = 0; lc <= second_addr - first_addr; lc++) {
127 		lp = get_addressed_line_node(++current_addr);
128 		if ((len = substitute_matching_text(pat, lp, gflag, kth)) < 0)
129 			return ERR;
130 		else if (len) {
131 			up = NULL;
132 			if (delete_lines(current_addr, current_addr) < 0)
133 				return ERR;
134 			txt = rbuf;
135 			eot = rbuf + len;
136 			SPL1();
137 			do {
138 				if ((txt = put_sbuf_line(txt)) == NULL) {
139 					SPL0();
140 					return ERR;
141 				} else if (up)
142 					up->t = get_addressed_line_node(current_addr);
143 				else if ((up = push_undo_stack(UADD,
144 				    current_addr, current_addr)) == NULL) {
145 					SPL0();
146 					return ERR;
147 				}
148 			} while (txt != eot);
149 			SPL0();
150 			nsubs++;
151 			xa = current_addr;
152 		}
153 	}
154 	current_addr = xa;
155 	if  (nsubs == 0 && !(gflag & GLB)) {
156 		seterrmsg("no match");
157 		return ERR;
158 	} else if ((gflag & (GPR | GLS | GNP)) &&
159 	    display_lines(current_addr, current_addr, gflag) < 0)
160 		return ERR;
161 	return 0;
162 }
163 
164 
165 /* substitute_matching_text: replace text matched by a pattern according to
166    a substitution template; return pointer to the modified text */
167 int
substitute_matching_text(pattern_t * pat,line_t * lp,int gflag,int kth)168 substitute_matching_text(pattern_t *pat, line_t *lp, int gflag, int kth)
169 {
170 	int off = 0;
171 	int changed = 0;
172 	int matchno = 0;
173 	int i = 0;
174 	regmatch_t rm[SE_MAX];
175 	char *txt;
176 	char *eot;
177 
178 	if ((txt = get_sbuf_line(lp)) == NULL)
179 		return ERR;
180 	if (isbinary)
181 		NUL_TO_NEWLINE(txt, lp->len);
182 	eot = txt + lp->len;
183 	if (!regexec(pat, txt, SE_MAX, rm, 0)) {
184 		do {
185 			if (!kth || kth == ++matchno) {
186 				changed++;
187 				i = rm[0].rm_so;
188 				REALLOC(rbuf, rbufsz, off + i, ERR);
189 				if (isbinary)
190 					NEWLINE_TO_NUL(txt, rm[0].rm_eo);
191 				memcpy(rbuf + off, txt, i);
192 				off += i;
193 				if ((off = apply_subst_template(txt, rm, off,
194 				    pat->re_nsub)) < 0)
195 					return ERR;
196 			} else {
197 				i = rm[0].rm_eo;
198 				REALLOC(rbuf, rbufsz, off + i, ERR);
199 				if (isbinary)
200 					NEWLINE_TO_NUL(txt, i);
201 				memcpy(rbuf + off, txt, i);
202 				off += i;
203 			}
204 			txt += rm[0].rm_eo;
205 		} while (*txt && (!changed || ((gflag & GSG) && rm[0].rm_eo)) &&
206 		    !regexec(pat, txt, SE_MAX, rm, REG_NOTBOL));
207 		i = eot - txt;
208 		REALLOC(rbuf, rbufsz, off + i + 2, ERR);
209 		if (i > 0 && !rm[0].rm_eo && (gflag & GSG)) {
210 			seterrmsg("infinite substitution loop");
211 			return  ERR;
212 		}
213 		if (isbinary)
214 			NEWLINE_TO_NUL(txt, i);
215 		memcpy(rbuf + off, txt, i);
216 		memcpy(rbuf + off + i, "\n", 2);
217 	}
218 	return changed ? off + i + 1 : 0;
219 }
220 
221 
222 /* apply_subst_template: modify text according to a substitution template;
223    return offset to end of modified text */
224 int
apply_subst_template(char * boln,regmatch_t * rm,int off,int re_nsub)225 apply_subst_template(char *boln, regmatch_t *rm, int off, int re_nsub)
226 {
227 	int j = 0;
228 	int k = 0;
229 	int n;
230 	char *sub = rhbuf;
231 
232 	for (; sub - rhbuf < rhbufi; sub++)
233 		if (*sub == '&') {
234 			j = rm[0].rm_so;
235 			k = rm[0].rm_eo;
236 			REALLOC(rbuf, rbufsz, off + k - j, ERR);
237 			while (j < k)
238 				rbuf[off++] = boln[j++];
239 		} else if (*sub == '\\' && '1' <= *++sub && *sub <= '9' &&
240 		    (n = *sub - '0') <= re_nsub) {
241 			j = rm[n].rm_so;
242 			k = rm[n].rm_eo;
243 			REALLOC(rbuf, rbufsz, off + k - j, ERR);
244 			while (j < k)
245 				rbuf[off++] = boln[j++];
246 		} else {
247 			REALLOC(rbuf, rbufsz, off + 1, ERR);
248 			rbuf[off++] = *sub;
249 		}
250 	REALLOC(rbuf, rbufsz, off + 1, ERR);
251 	rbuf[off] = '\0';
252 	return off;
253 }
254