GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: bin/ed/sub.c Lines: 0 135 0.0 %
Date: 2016-12-06 Branches: 0 280 0.0 %

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