xref: /freebsd/bin/ed/sub.c (revision 1d386b48a555f61cb7325543adbbb5c3f3407a66)
195e6217eSAndrew Moore /* sub.c: This file contains the substitution routines for the ed
295e6217eSAndrew Moore    line editor */
395e6217eSAndrew Moore /*-
495e6217eSAndrew Moore  * Copyright (c) 1993 Andrew Moore, Talke Studio.
595e6217eSAndrew Moore  * All rights reserved.
695e6217eSAndrew Moore  *
795e6217eSAndrew Moore  * Redistribution and use in source and binary forms, with or without
895e6217eSAndrew Moore  * modification, are permitted provided that the following conditions
995e6217eSAndrew Moore  * are met:
1095e6217eSAndrew Moore  * 1. Redistributions of source code must retain the above copyright
1195e6217eSAndrew Moore  *    notice, this list of conditions and the following disclaimer.
1295e6217eSAndrew Moore  * 2. Redistributions in binary form must reproduce the above copyright
1395e6217eSAndrew Moore  *    notice, this list of conditions and the following disclaimer in the
1495e6217eSAndrew Moore  *    documentation and/or other materials provided with the distribution.
1595e6217eSAndrew Moore  *
1695e6217eSAndrew Moore  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1795e6217eSAndrew Moore  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1895e6217eSAndrew Moore  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1995e6217eSAndrew Moore  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2095e6217eSAndrew Moore  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2195e6217eSAndrew Moore  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2295e6217eSAndrew Moore  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2395e6217eSAndrew Moore  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2495e6217eSAndrew Moore  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2595e6217eSAndrew Moore  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2695e6217eSAndrew Moore  * SUCH DAMAGE.
2795e6217eSAndrew Moore  */
2895e6217eSAndrew Moore 
295eb43ac2SDavid E. O'Brien #include <sys/cdefs.h>
3095e6217eSAndrew Moore #include "ed.h"
3195e6217eSAndrew Moore 
3295e6217eSAndrew Moore 
33*ae824d80SEd Schouten static char *rhbuf;		/* rhs substitution buffer */
34*ae824d80SEd Schouten static int rhbufsz;		/* rhs substitution buffer size */
35*ae824d80SEd Schouten static int rhbufi;		/* rhs substitution buffer index */
3695e6217eSAndrew Moore 
3795e6217eSAndrew Moore /* extract_subst_tail: extract substitution tail from the command buffer */
3895e6217eSAndrew Moore int
extract_subst_tail(int * flagp,long * np)397669d0fcSWarner Losh extract_subst_tail(int *flagp, long *np)
4095e6217eSAndrew Moore {
4195e6217eSAndrew Moore 	char delimiter;
4295e6217eSAndrew Moore 
4395e6217eSAndrew Moore 	*flagp = *np = 0;
4495e6217eSAndrew Moore 	if ((delimiter = *ibufp) == '\n') {
4595e6217eSAndrew Moore 		rhbufi = 0;
4695e6217eSAndrew Moore 		*flagp = GPR;
4795e6217eSAndrew Moore 		return 0;
4895e6217eSAndrew Moore 	} else if (extract_subst_template() == NULL)
4995e6217eSAndrew Moore 		return  ERR;
5095e6217eSAndrew Moore 	else if (*ibufp == '\n') {
5195e6217eSAndrew Moore 		*flagp = GPR;
5295e6217eSAndrew Moore 		return 0;
5395e6217eSAndrew Moore 	} else if (*ibufp == delimiter)
5495e6217eSAndrew Moore 		ibufp++;
5595e6217eSAndrew Moore 	if ('1' <= *ibufp && *ibufp <= '9') {
5695e6217eSAndrew Moore 		STRTOL(*np, ibufp);
5795e6217eSAndrew Moore 		return 0;
5895e6217eSAndrew Moore 	} else if (*ibufp == 'g') {
5995e6217eSAndrew Moore 		ibufp++;
6095e6217eSAndrew Moore 		*flagp = GSG;
6195e6217eSAndrew Moore 		return 0;
6295e6217eSAndrew Moore 	}
6395e6217eSAndrew Moore 	return 0;
6495e6217eSAndrew Moore }
6595e6217eSAndrew Moore 
6695e6217eSAndrew Moore 
6795e6217eSAndrew Moore /* extract_subst_template: return pointer to copy of substitution template
6895e6217eSAndrew Moore    in the command buffer */
6995e6217eSAndrew Moore char *
extract_subst_template(void)707669d0fcSWarner Losh extract_subst_template(void)
7195e6217eSAndrew Moore {
7295e6217eSAndrew Moore 	int n = 0;
7395e6217eSAndrew Moore 	int i = 0;
7495e6217eSAndrew Moore 	char c;
7595e6217eSAndrew Moore 	char delimiter = *ibufp++;
7695e6217eSAndrew Moore 
7795e6217eSAndrew Moore 	if (*ibufp == '%' && *(ibufp + 1) == delimiter) {
7895e6217eSAndrew Moore 		ibufp++;
79a4616748SMike Barcroft 		if (!rhbuf)
80a4616748SMike Barcroft 			errmsg = "no previous substitution";
8195e6217eSAndrew Moore 		return rhbuf;
8295e6217eSAndrew Moore 	}
8395e6217eSAndrew Moore 	while (*ibufp != delimiter) {
8495e6217eSAndrew Moore 		REALLOC(rhbuf, rhbufsz, i + 2, NULL);
8595e6217eSAndrew Moore 		if ((c = rhbuf[i++] = *ibufp++) == '\n' && *ibufp == '\0') {
8695e6217eSAndrew Moore 			i--, ibufp--;
8795e6217eSAndrew Moore 			break;
8895e6217eSAndrew Moore 		} else if (c != '\\')
8995e6217eSAndrew Moore 			;
9095e6217eSAndrew Moore 		else if ((rhbuf[i++] = *ibufp++) != '\n')
9195e6217eSAndrew Moore 			;
9295e6217eSAndrew Moore 		else if (!isglobal) {
9395e6217eSAndrew Moore 			while ((n = get_tty_line()) == 0 ||
940fd510b7SJoerg Wunsch 			    (n > 0 && ibuf[n - 1] != '\n'))
9595e6217eSAndrew Moore 				clearerr(stdin);
9695e6217eSAndrew Moore 			if (n < 0)
9795e6217eSAndrew Moore 				return NULL;
9895e6217eSAndrew Moore 		}
9995e6217eSAndrew Moore 	}
10095e6217eSAndrew Moore 	REALLOC(rhbuf, rhbufsz, i + 1, NULL);
10195e6217eSAndrew Moore 	rhbuf[rhbufi = i] = '\0';
10295e6217eSAndrew Moore 	return  rhbuf;
10395e6217eSAndrew Moore }
10495e6217eSAndrew Moore 
10595e6217eSAndrew Moore 
106*ae824d80SEd Schouten static char *rbuf;		/* substitute_matching_text buffer */
107*ae824d80SEd Schouten static int rbufsz;		/* substitute_matching_text buffer size */
10895e6217eSAndrew Moore 
10995e6217eSAndrew Moore /* search_and_replace: for each line in a range, change text matching a pattern
11095e6217eSAndrew Moore    according to a substitution template; return status  */
11195e6217eSAndrew Moore int
search_and_replace(pattern_t * pat,int gflag,int kth)1127669d0fcSWarner Losh search_and_replace(pattern_t *pat, int gflag, int kth)
11395e6217eSAndrew Moore {
11495e6217eSAndrew Moore 	undo_t *up;
115a4616748SMike Barcroft 	const char *txt;
116a4616748SMike Barcroft 	const char *eot;
11795e6217eSAndrew Moore 	long lc;
118c7230830SAndrew Moore 	long xa = current_addr;
11995e6217eSAndrew Moore 	int nsubs = 0;
12095e6217eSAndrew Moore 	line_t *lp;
12195e6217eSAndrew Moore 	int len;
12295e6217eSAndrew Moore 
12395e6217eSAndrew Moore 	current_addr = first_addr - 1;
12495e6217eSAndrew Moore 	for (lc = 0; lc <= second_addr - first_addr; lc++) {
12595e6217eSAndrew Moore 		lp = get_addressed_line_node(++current_addr);
12695e6217eSAndrew Moore 		if ((len = substitute_matching_text(pat, lp, gflag, kth)) < 0)
12795e6217eSAndrew Moore 			return ERR;
12895e6217eSAndrew Moore 		else if (len) {
12995e6217eSAndrew Moore 			up = NULL;
13095e6217eSAndrew Moore 			if (delete_lines(current_addr, current_addr) < 0)
13195e6217eSAndrew Moore 				return ERR;
13295e6217eSAndrew Moore 			txt = rbuf;
13395e6217eSAndrew Moore 			eot = rbuf + len;
13495e6217eSAndrew Moore 			SPL1();
13595e6217eSAndrew Moore 			do {
13695e6217eSAndrew Moore 				if ((txt = put_sbuf_line(txt)) == NULL) {
13795e6217eSAndrew Moore 					SPL0();
13895e6217eSAndrew Moore 					return ERR;
13995e6217eSAndrew Moore 				} else if (up)
14095e6217eSAndrew Moore 					up->t = get_addressed_line_node(current_addr);
14195e6217eSAndrew Moore 				else if ((up = push_undo_stack(UADD,
14295e6217eSAndrew Moore 				    current_addr, current_addr)) == NULL) {
14395e6217eSAndrew Moore 					SPL0();
14495e6217eSAndrew Moore 					return ERR;
14595e6217eSAndrew Moore 				}
14695e6217eSAndrew Moore 			} while (txt != eot);
14795e6217eSAndrew Moore 			SPL0();
14895e6217eSAndrew Moore 			nsubs++;
149c7230830SAndrew Moore 			xa = current_addr;
15095e6217eSAndrew Moore 		}
15195e6217eSAndrew Moore 	}
152c7230830SAndrew Moore 	current_addr = xa;
15395e6217eSAndrew Moore 	if  (nsubs == 0 && !(gflag & GLB)) {
154a4616748SMike Barcroft 		errmsg = "no match";
15595e6217eSAndrew Moore 		return ERR;
15695e6217eSAndrew Moore 	} else if ((gflag & (GPR | GLS | GNP)) &&
15795e6217eSAndrew Moore 	    display_lines(current_addr, current_addr, gflag) < 0)
15895e6217eSAndrew Moore 		return ERR;
15995e6217eSAndrew Moore 	return 0;
16095e6217eSAndrew Moore }
16195e6217eSAndrew Moore 
16295e6217eSAndrew Moore 
16395e6217eSAndrew Moore /* substitute_matching_text: replace text matched by a pattern according to
16495e6217eSAndrew Moore    a substitution template; return pointer to the modified text */
16595e6217eSAndrew Moore int
substitute_matching_text(pattern_t * pat,line_t * lp,int gflag,int kth)1667669d0fcSWarner Losh substitute_matching_text(pattern_t *pat, line_t *lp, int gflag, int kth)
16795e6217eSAndrew Moore {
16895e6217eSAndrew Moore 	int off = 0;
16995e6217eSAndrew Moore 	int changed = 0;
17095e6217eSAndrew Moore 	int matchno = 0;
17195e6217eSAndrew Moore 	int i = 0;
17295e6217eSAndrew Moore 	regmatch_t rm[SE_MAX];
17395e6217eSAndrew Moore 	char *txt;
17495e6217eSAndrew Moore 	char *eot;
17595e6217eSAndrew Moore 
17695e6217eSAndrew Moore 	if ((txt = get_sbuf_line(lp)) == NULL)
17795e6217eSAndrew Moore 		return ERR;
17895e6217eSAndrew Moore 	if (isbinary)
17995e6217eSAndrew Moore 		NUL_TO_NEWLINE(txt, lp->len);
18095e6217eSAndrew Moore 	eot = txt + lp->len;
18195e6217eSAndrew Moore 	if (!regexec(pat, txt, SE_MAX, rm, 0)) {
18295e6217eSAndrew Moore 		do {
18395e6217eSAndrew Moore 			if (!kth || kth == ++matchno) {
18495e6217eSAndrew Moore 				changed++;
18595e6217eSAndrew Moore 				i = rm[0].rm_so;
18695e6217eSAndrew Moore 				REALLOC(rbuf, rbufsz, off + i, ERR);
18795e6217eSAndrew Moore 				if (isbinary)
18895e6217eSAndrew Moore 					NEWLINE_TO_NUL(txt, rm[0].rm_eo);
18995e6217eSAndrew Moore 				memcpy(rbuf + off, txt, i);
19095e6217eSAndrew Moore 				off += i;
19195e6217eSAndrew Moore 				if ((off = apply_subst_template(txt, rm, off,
19295e6217eSAndrew Moore 				    pat->re_nsub)) < 0)
19395e6217eSAndrew Moore 					return ERR;
19495e6217eSAndrew Moore 			} else {
19595e6217eSAndrew Moore 				i = rm[0].rm_eo;
19695e6217eSAndrew Moore 				REALLOC(rbuf, rbufsz, off + i, ERR);
19795e6217eSAndrew Moore 				if (isbinary)
19895e6217eSAndrew Moore 					NEWLINE_TO_NUL(txt, i);
19995e6217eSAndrew Moore 				memcpy(rbuf + off, txt, i);
20095e6217eSAndrew Moore 				off += i;
20195e6217eSAndrew Moore 			}
20295e6217eSAndrew Moore 			txt += rm[0].rm_eo;
2030fd510b7SJoerg Wunsch 		} while (*txt &&
2040fd510b7SJoerg Wunsch                         (!changed || ((gflag & GSG) && rm[0].rm_eo)) &&
20595e6217eSAndrew Moore 		        !regexec(pat, txt, SE_MAX, rm, REG_NOTBOL));
20695e6217eSAndrew Moore 		i = eot - txt;
20795e6217eSAndrew Moore 		REALLOC(rbuf, rbufsz, off + i + 2, ERR);
20895e6217eSAndrew Moore 		if (i > 0 && !rm[0].rm_eo && (gflag & GSG)) {
209a4616748SMike Barcroft 			errmsg = "infinite substitution loop";
21095e6217eSAndrew Moore 			return  ERR;
21195e6217eSAndrew Moore 		}
21295e6217eSAndrew Moore 		if (isbinary)
21395e6217eSAndrew Moore 			NEWLINE_TO_NUL(txt, i);
21495e6217eSAndrew Moore 		memcpy(rbuf + off, txt, i);
21595e6217eSAndrew Moore 		memcpy(rbuf + off + i, "\n", 2);
21695e6217eSAndrew Moore 	}
21795e6217eSAndrew Moore 	return changed ? off + i + 1 : 0;
21895e6217eSAndrew Moore }
21995e6217eSAndrew Moore 
22095e6217eSAndrew Moore 
22195e6217eSAndrew Moore /* apply_subst_template: modify text according to a substitution template;
22295e6217eSAndrew Moore    return offset to end of modified text */
22395e6217eSAndrew Moore int
apply_subst_template(const char * boln,regmatch_t * rm,int off,int re_nsub)2247669d0fcSWarner Losh apply_subst_template(const char *boln, regmatch_t *rm, int off, int re_nsub)
22595e6217eSAndrew Moore {
22695e6217eSAndrew Moore 	int j = 0;
22795e6217eSAndrew Moore 	int k = 0;
22895e6217eSAndrew Moore 	int n;
22995e6217eSAndrew Moore 	char *sub = rhbuf;
23095e6217eSAndrew Moore 
23195e6217eSAndrew Moore 	for (; sub - rhbuf < rhbufi; sub++)
23295e6217eSAndrew Moore 		if (*sub == '&') {
23395e6217eSAndrew Moore 			j = rm[0].rm_so;
23495e6217eSAndrew Moore 			k = rm[0].rm_eo;
23595e6217eSAndrew Moore 			REALLOC(rbuf, rbufsz, off + k - j, ERR);
23695e6217eSAndrew Moore 			while (j < k)
23795e6217eSAndrew Moore 				rbuf[off++] = boln[j++];
23895e6217eSAndrew Moore 		} else if (*sub == '\\' && '1' <= *++sub && *sub <= '9' &&
23995e6217eSAndrew Moore 		    (n = *sub - '0') <= re_nsub) {
24095e6217eSAndrew Moore 			j = rm[n].rm_so;
24195e6217eSAndrew Moore 			k = rm[n].rm_eo;
24295e6217eSAndrew Moore 			REALLOC(rbuf, rbufsz, off + k - j, ERR);
24395e6217eSAndrew Moore 			while (j < k)
24495e6217eSAndrew Moore 				rbuf[off++] = boln[j++];
24595e6217eSAndrew Moore 		} else {
24695e6217eSAndrew Moore 			REALLOC(rbuf, rbufsz, off + 1, ERR);
24795e6217eSAndrew Moore 			rbuf[off++] = *sub;
24895e6217eSAndrew Moore 		}
24995e6217eSAndrew Moore 	REALLOC(rbuf, rbufsz, off + 1, ERR);
25095e6217eSAndrew Moore 	rbuf[off] = '\0';
25195e6217eSAndrew Moore 	return off;
25295e6217eSAndrew Moore }
253