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