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