1 /* sub.c: This file contains the substitution routines for the ed
4 * Copyright (c) 1993 Andrew Moore, Talke Studio.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * @(#)sub.c,v 1.1 1994/02/01 00:34:44 alm Exp
29 * $FreeBSD: src/bin/ed/sub.c,v 1.12 1999/08/27 23:14:15 peter Exp $
30 * $DragonFly: src/bin/ed/sub.c,v 1.2 2003/06/17 04:22:50 dillon Exp $
36 char *rhbuf; /* rhs substitution buffer */
37 int rhbufsz; /* rhs substitution buffer size */
38 int rhbufi; /* rhs substitution buffer index */
40 /* extract_subst_tail: extract substitution tail from the command buffer */
42 extract_subst_tail(flagp, np)
49 if ((delimiter = *ibufp) == '\n') {
53 } else if (extract_subst_template() == NULL)
55 else if (*ibufp == '\n') {
58 } else if (*ibufp == delimiter)
60 if ('1' <= *ibufp && *ibufp <= '9') {
63 } else if (*ibufp == 'g') {
72 /* extract_subst_template: return pointer to copy of substitution template
73 in the command buffer */
75 extract_subst_template()
80 char delimiter = *ibufp++;
82 if (*ibufp == '%' && *(ibufp + 1) == delimiter) {
84 if (!rhbuf) sprintf(errmsg, "no previous substitution");
87 while (*ibufp != delimiter) {
88 REALLOC(rhbuf, rhbufsz, i + 2, NULL);
89 if ((c = rhbuf[i++] = *ibufp++) == '\n' && *ibufp == '\0') {
94 else if ((rhbuf[i++] = *ibufp++) != '\n')
97 while ((n = get_tty_line()) == 0 ||
98 (n > 0 && ibuf[n - 1] != '\n'))
104 REALLOC(rhbuf, rhbufsz, i + 1, NULL);
105 rhbuf[rhbufi = i] = '\0';
110 char *rbuf; /* substitute_matching_text buffer */
111 int rbufsz; /* substitute_matching_text buffer size */
113 /* search_and_replace: for each line in a range, change text matching a pattern
114 according to a substitution template; return status */
116 search_and_replace(pat, gflag, kth)
125 long xa = current_addr;
130 current_addr = first_addr - 1;
131 for (lc = 0; lc <= second_addr - first_addr; lc++) {
132 lp = get_addressed_line_node(++current_addr);
133 if ((len = substitute_matching_text(pat, lp, gflag, kth)) < 0)
137 if (delete_lines(current_addr, current_addr) < 0)
143 if ((txt = put_sbuf_line(txt)) == NULL) {
147 up->t = get_addressed_line_node(current_addr);
148 else if ((up = push_undo_stack(UADD,
149 current_addr, current_addr)) == NULL) {
153 } while (txt != eot);
160 if (nsubs == 0 && !(gflag & GLB)) {
161 sprintf(errmsg, "no match");
163 } else if ((gflag & (GPR | GLS | GNP)) &&
164 display_lines(current_addr, current_addr, gflag) < 0)
170 /* substitute_matching_text: replace text matched by a pattern according to
171 a substitution template; return pointer to the modified text */
173 substitute_matching_text(pat, lp, gflag, kth)
183 regmatch_t rm[SE_MAX];
187 if ((txt = get_sbuf_line(lp)) == NULL)
190 NUL_TO_NEWLINE(txt, lp->len);
192 if (!regexec(pat, txt, SE_MAX, rm, 0)) {
194 if (!kth || kth == ++matchno) {
197 REALLOC(rbuf, rbufsz, off + i, ERR);
199 NEWLINE_TO_NUL(txt, rm[0].rm_eo);
200 memcpy(rbuf + off, txt, i);
202 if ((off = apply_subst_template(txt, rm, off,
207 REALLOC(rbuf, rbufsz, off + i, ERR);
209 NEWLINE_TO_NUL(txt, i);
210 memcpy(rbuf + off, txt, i);
215 (!changed || ((gflag & GSG) && rm[0].rm_eo)) &&
216 !regexec(pat, txt, SE_MAX, rm, REG_NOTBOL));
218 REALLOC(rbuf, rbufsz, off + i + 2, ERR);
219 if (i > 0 && !rm[0].rm_eo && (gflag & GSG)) {
220 sprintf(errmsg, "infinite substitution loop");
224 NEWLINE_TO_NUL(txt, i);
225 memcpy(rbuf + off, txt, i);
226 memcpy(rbuf + off + i, "\n", 2);
228 return changed ? off + i + 1 : 0;
232 /* apply_subst_template: modify text according to a substitution template;
233 return offset to end of modified text */
235 apply_subst_template(boln, rm, off, re_nsub)
246 for (; sub - rhbuf < rhbufi; sub++)
250 REALLOC(rbuf, rbufsz, off + k - j, ERR);
252 rbuf[off++] = boln[j++];
253 } else if (*sub == '\\' && '1' <= *++sub && *sub <= '9' &&
254 (n = *sub - '0') <= re_nsub) {
257 REALLOC(rbuf, rbufsz, off + k - j, ERR);
259 rbuf[off++] = boln[j++];
261 REALLOC(rbuf, rbufsz, off + 1, ERR);
264 REALLOC(rbuf, rbufsz, off + 1, ERR);