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.15 2002/06/30 05:13:53 obrien Exp $
30 * $DragonFly: src/bin/ed/sub.c,v 1.4 2007/04/06 23:36:54 pavalos 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(int *flagp
, long *np
)
47 if ((delimiter
= *ibufp
) == '\n') {
51 } else if (extract_subst_template() == NULL
)
53 else if (*ibufp
== '\n') {
56 } else if (*ibufp
== delimiter
)
58 if ('1' <= *ibufp
&& *ibufp
<= '9') {
61 } else if (*ibufp
== 'g') {
70 /* extract_subst_template: return pointer to copy of substitution template
71 in the command buffer */
73 extract_subst_template(void)
78 char delimiter
= *ibufp
++;
80 if (*ibufp
== '%' && *(ibufp
+ 1) == delimiter
) {
83 errmsg
= "no previous substitution";
86 while (*ibufp
!= delimiter
) {
87 REALLOC(rhbuf
, rhbufsz
, i
+ 2, NULL
);
88 if ((c
= rhbuf
[i
++] = *ibufp
++) == '\n' && *ibufp
== '\0') {
93 else if ((rhbuf
[i
++] = *ibufp
++) != '\n')
96 while ((n
= get_tty_line()) == 0 ||
97 (n
> 0 && ibuf
[n
- 1] != '\n'))
103 REALLOC(rhbuf
, rhbufsz
, i
+ 1, NULL
);
104 rhbuf
[rhbufi
= i
] = '\0';
109 char *rbuf
; /* substitute_matching_text buffer */
110 int rbufsz
; /* substitute_matching_text buffer size */
112 /* search_and_replace: for each line in a range, change text matching a pattern
113 according to a substitution template; return status */
115 search_and_replace(pattern_t
*pat
, int gflag
, int kth
)
121 long xa
= current_addr
;
126 current_addr
= first_addr
- 1;
127 for (lc
= 0; lc
<= second_addr
- first_addr
; lc
++) {
128 lp
= get_addressed_line_node(++current_addr
);
129 if ((len
= substitute_matching_text(pat
, lp
, gflag
, kth
)) < 0)
133 if (delete_lines(current_addr
, current_addr
) < 0)
139 if ((txt
= put_sbuf_line(txt
)) == NULL
) {
143 up
->t
= get_addressed_line_node(current_addr
);
144 else if ((up
= push_undo_stack(UADD
,
145 current_addr
, current_addr
)) == NULL
) {
149 } while (txt
!= eot
);
156 if (nsubs
== 0 && !(gflag
& GLB
)) {
159 } else if ((gflag
& (GPR
| GLS
| GNP
)) &&
160 display_lines(current_addr
, current_addr
, gflag
) < 0)
166 /* substitute_matching_text: replace text matched by a pattern according to
167 a substitution template; return pointer to the modified text */
169 substitute_matching_text(pattern_t
*pat
, line_t
*lp
, int gflag
, int kth
)
175 regmatch_t rm
[SE_MAX
];
179 if ((txt
= get_sbuf_line(lp
)) == NULL
)
182 NUL_TO_NEWLINE(txt
, lp
->len
);
184 if (!regexec(pat
, txt
, SE_MAX
, rm
, 0)) {
186 if (!kth
|| kth
== ++matchno
) {
189 REALLOC(rbuf
, rbufsz
, off
+ i
, ERR
);
191 NEWLINE_TO_NUL(txt
, rm
[0].rm_eo
);
192 memcpy(rbuf
+ off
, txt
, i
);
194 if ((off
= apply_subst_template(txt
, rm
, off
,
199 REALLOC(rbuf
, rbufsz
, off
+ i
, ERR
);
201 NEWLINE_TO_NUL(txt
, i
);
202 memcpy(rbuf
+ off
, txt
, i
);
207 (!changed
|| ((gflag
& GSG
) && rm
[0].rm_eo
)) &&
208 !regexec(pat
, txt
, SE_MAX
, rm
, REG_NOTBOL
));
210 REALLOC(rbuf
, rbufsz
, off
+ i
+ 2, ERR
);
211 if (i
> 0 && !rm
[0].rm_eo
&& (gflag
& GSG
)) {
212 errmsg
= "infinite substitution loop";
216 NEWLINE_TO_NUL(txt
, i
);
217 memcpy(rbuf
+ off
, txt
, i
);
218 memcpy(rbuf
+ off
+ i
, "\n", 2);
220 return changed
? off
+ i
+ 1 : 0;
224 /* apply_subst_template: modify text according to a substitution template;
225 return offset to end of modified text */
227 apply_subst_template(const char *boln
, regmatch_t
*rm
, int off
, int re_nsub
)
234 for (; sub
- rhbuf
< rhbufi
; sub
++)
238 REALLOC(rbuf
, rbufsz
, off
+ k
- j
, ERR
);
240 rbuf
[off
++] = boln
[j
++];
241 } else if (*sub
== '\\' && '1' <= *++sub
&& *sub
<= '9' &&
242 (n
= *sub
- '0') <= re_nsub
) {
245 REALLOC(rbuf
, rbufsz
, off
+ k
- j
, ERR
);
247 rbuf
[off
++] = boln
[j
++];
249 REALLOC(rbuf
, rbufsz
, off
+ 1, ERR
);
252 REALLOC(rbuf
, rbufsz
, off
+ 1, ERR
);