sub.c revision 1.6 1 1.6 xtraeme /* $NetBSD: sub.c,v 1.6 2005/02/17 16:29:26 xtraeme 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.6 xtraeme __RCSID("$NetBSD: sub.c,v 1.6 2005/02/17 16:29:26 xtraeme 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.1 alm if (!rhbuf) sprintf(errmsg, "no previous substitution");
90 1.1 alm return rhbuf;
91 1.1 alm }
92 1.1 alm while (*ibufp != delimiter) {
93 1.1 alm REALLOC(rhbuf, rhbufsz, i + 2, NULL);
94 1.1 alm if ((c = rhbuf[i++] = *ibufp++) == '\n' && *ibufp == '\0') {
95 1.1 alm i--, ibufp--;
96 1.1 alm break;
97 1.1 alm } else if (c != '\\')
98 1.1 alm ;
99 1.1 alm else if ((rhbuf[i++] = *ibufp++) != '\n')
100 1.1 alm ;
101 1.1 alm else if (!isglobal) {
102 1.1 alm while ((n = get_tty_line()) == 0 ||
103 1.5 thorpej (n > 0 && ibuf[n - 1] != '\n'))
104 1.1 alm clearerr(stdin);
105 1.1 alm if (n < 0)
106 1.1 alm return NULL;
107 1.1 alm }
108 1.1 alm }
109 1.1 alm REALLOC(rhbuf, rhbufsz, i + 1, NULL);
110 1.1 alm rhbuf[rhbufi = i] = '\0';
111 1.1 alm return rhbuf;
112 1.1 alm }
113 1.1 alm
114 1.1 alm
115 1.1 alm char *rbuf; /* substitute_matching_text buffer */
116 1.1 alm int rbufsz; /* substitute_matching_text buffer size */
117 1.1 alm
118 1.1 alm /* search_and_replace: for each line in a range, change text matching a pattern
119 1.1 alm according to a substitution template; return status */
120 1.1 alm int
121 1.6 xtraeme search_and_replace(pattern_t *pat, int gflag, int kth)
122 1.1 alm {
123 1.1 alm undo_t *up;
124 1.1 alm char *txt;
125 1.1 alm char *eot;
126 1.1 alm long lc;
127 1.3 mycroft long xa = current_addr;
128 1.1 alm int nsubs = 0;
129 1.1 alm line_t *lp;
130 1.1 alm int len;
131 1.1 alm
132 1.1 alm current_addr = first_addr - 1;
133 1.1 alm for (lc = 0; lc <= second_addr - first_addr; lc++) {
134 1.1 alm lp = get_addressed_line_node(++current_addr);
135 1.1 alm if ((len = substitute_matching_text(pat, lp, gflag, kth)) < 0)
136 1.1 alm return ERR;
137 1.1 alm else if (len) {
138 1.1 alm up = NULL;
139 1.1 alm if (delete_lines(current_addr, current_addr) < 0)
140 1.1 alm return ERR;
141 1.1 alm txt = rbuf;
142 1.1 alm eot = rbuf + len;
143 1.1 alm SPL1();
144 1.1 alm do {
145 1.1 alm if ((txt = put_sbuf_line(txt)) == NULL) {
146 1.1 alm SPL0();
147 1.1 alm return ERR;
148 1.1 alm } else if (up)
149 1.1 alm up->t = get_addressed_line_node(current_addr);
150 1.1 alm else if ((up = push_undo_stack(UADD,
151 1.1 alm current_addr, current_addr)) == NULL) {
152 1.1 alm SPL0();
153 1.1 alm return ERR;
154 1.1 alm }
155 1.1 alm } while (txt != eot);
156 1.1 alm SPL0();
157 1.1 alm nsubs++;
158 1.3 mycroft xa = current_addr;
159 1.1 alm }
160 1.1 alm }
161 1.3 mycroft current_addr = xa;
162 1.1 alm if (nsubs == 0 && !(gflag & GLB)) {
163 1.1 alm sprintf(errmsg, "no match");
164 1.1 alm return ERR;
165 1.1 alm } else if ((gflag & (GPR | GLS | GNP)) &&
166 1.1 alm display_lines(current_addr, current_addr, gflag) < 0)
167 1.1 alm return ERR;
168 1.1 alm return 0;
169 1.1 alm }
170 1.1 alm
171 1.1 alm
172 1.1 alm /* substitute_matching_text: replace text matched by a pattern according to
173 1.1 alm a substitution template; return pointer to the modified text */
174 1.1 alm int
175 1.6 xtraeme substitute_matching_text(pattern_t *pat, line_t *lp, int gflag, int kth)
176 1.1 alm {
177 1.1 alm int off = 0;
178 1.1 alm int changed = 0;
179 1.1 alm int matchno = 0;
180 1.1 alm int i = 0;
181 1.1 alm regmatch_t rm[SE_MAX];
182 1.1 alm char *txt;
183 1.1 alm char *eot;
184 1.1 alm
185 1.1 alm if ((txt = get_sbuf_line(lp)) == NULL)
186 1.1 alm return ERR;
187 1.1 alm if (isbinary)
188 1.1 alm NUL_TO_NEWLINE(txt, lp->len);
189 1.1 alm eot = txt + lp->len;
190 1.1 alm if (!regexec(pat, txt, SE_MAX, rm, 0)) {
191 1.1 alm do {
192 1.1 alm if (!kth || kth == ++matchno) {
193 1.1 alm changed++;
194 1.1 alm i = rm[0].rm_so;
195 1.1 alm REALLOC(rbuf, rbufsz, off + i, ERR);
196 1.1 alm if (isbinary)
197 1.1 alm NEWLINE_TO_NUL(txt, rm[0].rm_eo);
198 1.1 alm memcpy(rbuf + off, txt, i);
199 1.1 alm off += i;
200 1.1 alm if ((off = apply_subst_template(txt, rm, off,
201 1.1 alm pat->re_nsub)) < 0)
202 1.1 alm return ERR;
203 1.1 alm } else {
204 1.1 alm i = rm[0].rm_eo;
205 1.1 alm REALLOC(rbuf, rbufsz, off + i, ERR);
206 1.1 alm if (isbinary)
207 1.1 alm NEWLINE_TO_NUL(txt, i);
208 1.1 alm memcpy(rbuf + off, txt, i);
209 1.1 alm off += i;
210 1.1 alm }
211 1.1 alm txt += rm[0].rm_eo;
212 1.5 thorpej } while (*txt && (!changed || ((gflag & GSG) && rm[0].rm_eo))
213 1.5 thorpej && !regexec(pat, txt, SE_MAX, rm, REG_NOTBOL));
214 1.1 alm i = eot - txt;
215 1.1 alm REALLOC(rbuf, rbufsz, off + i + 2, ERR);
216 1.1 alm if (i > 0 && !rm[0].rm_eo && (gflag & GSG)) {
217 1.1 alm sprintf(errmsg, "infinite substitution loop");
218 1.1 alm return ERR;
219 1.1 alm }
220 1.1 alm if (isbinary)
221 1.1 alm NEWLINE_TO_NUL(txt, i);
222 1.1 alm memcpy(rbuf + off, txt, i);
223 1.1 alm memcpy(rbuf + off + i, "\n", 2);
224 1.1 alm }
225 1.1 alm return changed ? off + i + 1 : 0;
226 1.1 alm }
227 1.1 alm
228 1.1 alm
229 1.1 alm /* apply_subst_template: modify text according to a substitution template;
230 1.1 alm return offset to end of modified text */
231 1.1 alm int
232 1.6 xtraeme apply_subst_template(char *boln, regmatch_t *rm, int off, int re_nsub)
233 1.1 alm {
234 1.1 alm int j = 0;
235 1.1 alm int k = 0;
236 1.1 alm int n;
237 1.1 alm char *sub = rhbuf;
238 1.1 alm
239 1.1 alm for (; sub - rhbuf < rhbufi; sub++)
240 1.1 alm if (*sub == '&') {
241 1.1 alm j = rm[0].rm_so;
242 1.1 alm k = rm[0].rm_eo;
243 1.1 alm REALLOC(rbuf, rbufsz, off + k - j, ERR);
244 1.1 alm while (j < k)
245 1.1 alm rbuf[off++] = boln[j++];
246 1.1 alm } else if (*sub == '\\' && '1' <= *++sub && *sub <= '9' &&
247 1.1 alm (n = *sub - '0') <= re_nsub) {
248 1.1 alm j = rm[n].rm_so;
249 1.1 alm k = rm[n].rm_eo;
250 1.1 alm REALLOC(rbuf, rbufsz, off + k - j, ERR);
251 1.1 alm while (j < k)
252 1.1 alm rbuf[off++] = boln[j++];
253 1.1 alm } else {
254 1.1 alm REALLOC(rbuf, rbufsz, off + 1, ERR);
255 1.1 alm rbuf[off++] = *sub;
256 1.1 alm }
257 1.1 alm REALLOC(rbuf, rbufsz, off + 1, ERR);
258 1.1 alm rbuf[off] = '\0';
259 1.1 alm return off;
260 1.1 alm }
261