xref: /csrg-svn/usr.bin/sed/process.c (revision 67039)
155997Sbostic /*-
255997Sbostic  * Copyright (c) 1992 Diomidis Spinellis.
366817Sbostic  * Copyright (c) 1992, 1993, 1994
462229Sbostic  *	The Regents of the University of California.  All rights reserved.
555997Sbostic  *
655997Sbostic  * This code is derived from software contributed to Berkeley by
755997Sbostic  * Diomidis Spinellis of Imperial College, University of London.
855997Sbostic  *
955997Sbostic  * %sccs.include.redist.c%
1055997Sbostic  */
1155997Sbostic 
1255997Sbostic #ifndef lint
13*67039Sbostic static char sccsid[] = "@(#)process.c	8.6 (Berkeley) 04/20/94";
1455997Sbostic #endif /* not lint */
1555997Sbostic 
1655997Sbostic #include <sys/types.h>
1755997Sbostic #include <sys/stat.h>
1855997Sbostic #include <sys/ioctl.h>
1955997Sbostic #include <sys/uio.h>
2055997Sbostic 
2155997Sbostic #include <ctype.h>
2255997Sbostic #include <errno.h>
2355997Sbostic #include <fcntl.h>
2455997Sbostic #include <limits.h>
2555997Sbostic #include <regex.h>
2655997Sbostic #include <stdio.h>
2755997Sbostic #include <stdlib.h>
2855997Sbostic #include <string.h>
2955997Sbostic #include <unistd.h>
3055997Sbostic 
3155997Sbostic #include "defs.h"
3255997Sbostic #include "extern.h"
3355997Sbostic 
3455997Sbostic static SPACE HS, PS, SS;
3555997Sbostic #define	pd		PS.deleted
3655997Sbostic #define	ps		PS.space
3755997Sbostic #define	psl		PS.len
3855997Sbostic #define	hs		HS.space
3955997Sbostic #define	hsl		HS.len
4055997Sbostic 
4155997Sbostic static inline int	 applies __P((struct s_command *));
4255997Sbostic static void		 flush_appends __P((void));
4355997Sbostic static void		 lputs __P((char *));
4457733Selan static inline int	 regexec_e __P((regex_t *, const char *, int, int, size_t));
4556079Sbostic static void		 regsub __P((SPACE *, char *, char *));
4655997Sbostic static int		 substitute __P((struct s_command *));
4755997Sbostic 
4855997Sbostic struct s_appends *appends;	/* Array of pointers to strings to append. */
4955997Sbostic static int appendx;		/* Index into appends array. */
5055997Sbostic int appendnum;			/* Size of appends array. */
5155997Sbostic 
5255997Sbostic static int lastaddr;		/* Set by applies if last address of a range. */
5355997Sbostic static int sdone;		/* If any substitutes since last line input. */
5455997Sbostic 				/* Iov structure for 'w' commands. */
5556019Sbostic static regex_t *defpreg;
5656079Sbostic size_t maxnsub;
5759073Selan regmatch_t *match;
5856019Sbostic 
5959073Selan #define OUT(s) { fwrite(s, sizeof(u_char), psl, stdout); }
6057733Selan 
6155997Sbostic void
process()6255997Sbostic process()
6355997Sbostic {
6455997Sbostic 	struct s_command *cp;
6555997Sbostic 	SPACE tspace;
6655997Sbostic 	size_t len;
6755997Sbostic 	char oldc, *p;
6855997Sbostic 
6956079Sbostic 	for (linenum = 0; mf_fgets(&PS, REPLACE);) {
7055997Sbostic 		pd = 0;
7155997Sbostic 		cp = prog;
7255997Sbostic redirect:
7355997Sbostic 		while (cp != NULL) {
7455997Sbostic 			if (!applies(cp)) {
7555997Sbostic 				cp = cp->next;
7655997Sbostic 				continue;
7755997Sbostic 			}
7855997Sbostic 			switch (cp->code) {
7955997Sbostic 			case '{':
8055997Sbostic 				cp = cp->u.c;
8155997Sbostic 				goto redirect;
8255997Sbostic 			case 'a':
8355997Sbostic 				if (appendx >= appendnum)
8455997Sbostic 					appends = xrealloc(appends,
8555997Sbostic 					    sizeof(struct s_appends) *
8655997Sbostic 					    (appendnum *= 2));
8755997Sbostic 				appends[appendx].type = AP_STRING;
8855997Sbostic 				appends[appendx].s = cp->t;
8957733Selan 				appends[appendx].len = strlen(cp->t);
9055997Sbostic 				appendx++;
9155997Sbostic 				break;
9255997Sbostic 			case 'b':
9355997Sbostic 				cp = cp->u.c;
9455997Sbostic 				goto redirect;
9555997Sbostic 			case 'c':
9655997Sbostic 				pd = 1;
9755997Sbostic 				psl = 0;
9855997Sbostic 				if (cp->a2 == NULL || lastaddr)
9955997Sbostic 					(void)printf("%s", cp->t);
10055997Sbostic 				break;
10155997Sbostic 			case 'd':
10256005Sbostic 				pd = 1;
10355997Sbostic 				goto new;
10455997Sbostic 			case 'D':
10555997Sbostic 				if (pd)
10655997Sbostic 					goto new;
10760120Selan 				if ((p = memchr(ps, '\n', psl)) == NULL)
10856005Sbostic 					pd = 1;
10956005Sbostic 				else {
11067036Sbostic 					psl -= (p - ps) + 1;
11155997Sbostic 					memmove(ps, p + 1, psl);
11255997Sbostic 				}
11355997Sbostic 				goto new;
11455997Sbostic 			case 'g':
11556079Sbostic 				cspace(&PS, hs, hsl, REPLACE);
11655997Sbostic 				break;
11755997Sbostic 			case 'G':
11859073Selan 				cspace(&PS, hs, hsl, 0);
11955997Sbostic 				break;
12055997Sbostic 			case 'h':
12156079Sbostic 				cspace(&HS, ps, psl, REPLACE);
12255997Sbostic 				break;
12355997Sbostic 			case 'H':
12459073Selan 				cspace(&HS, ps, psl, 0);
12555997Sbostic 				break;
12655997Sbostic 			case 'i':
12755997Sbostic 				(void)printf("%s", cp->t);
12855997Sbostic 				break;
12955997Sbostic 			case 'l':
13055997Sbostic 				lputs(ps);
13155997Sbostic 				break;
13255997Sbostic 			case 'n':
13355997Sbostic 				if (!nflag && !pd)
13457733Selan 					OUT(ps)
13555997Sbostic 				flush_appends();
136*67039Sbostic 				if (!mf_fgets(&PS, REPLACE))
13755997Sbostic 					exit(0);
13855997Sbostic 				pd = 0;
13955997Sbostic 				break;
14055997Sbostic 			case 'N':
14155997Sbostic 				flush_appends();
14259073Selan 				if (!mf_fgets(&PS, 0)) {
14355997Sbostic 					if (!nflag && !pd)
14457733Selan 						OUT(ps)
14555997Sbostic 					exit(0);
14655997Sbostic 				}
14755997Sbostic 				break;
14855997Sbostic 			case 'p':
14955997Sbostic 				if (pd)
15055997Sbostic 					break;
15157733Selan 				OUT(ps)
15255997Sbostic 				break;
15355997Sbostic 			case 'P':
15455997Sbostic 				if (pd)
15555997Sbostic 					break;
15660120Selan 				if ((p = memchr(ps, '\n', psl)) != NULL) {
15755997Sbostic 					oldc = *p;
15855997Sbostic 					*p = '\0';
15955997Sbostic 				}
16057733Selan 				OUT(ps)
16155997Sbostic 				if (p != NULL)
16255997Sbostic 					*p = oldc;
16355997Sbostic 				break;
16455997Sbostic 			case 'q':
16555997Sbostic 				if (!nflag && !pd)
16657733Selan 					OUT(ps)
16755997Sbostic 				flush_appends();
16855997Sbostic 				exit(0);
16955997Sbostic 			case 'r':
17055997Sbostic 				if (appendx >= appendnum)
17155997Sbostic 					appends = xrealloc(appends,
17255997Sbostic 					    sizeof(struct s_appends) *
17355997Sbostic 					    (appendnum *= 2));
17455997Sbostic 				appends[appendx].type = AP_FILE;
17555997Sbostic 				appends[appendx].s = cp->t;
17657733Selan 				appends[appendx].len = strlen(cp->t);
17755997Sbostic 				appendx++;
17855997Sbostic 				break;
17955997Sbostic 			case 's':
18056949Sbostic 				sdone |= substitute(cp);
18155997Sbostic 				break;
18255997Sbostic 			case 't':
18355997Sbostic 				if (sdone) {
18455997Sbostic 					sdone = 0;
18555997Sbostic 					cp = cp->u.c;
18655997Sbostic 					goto redirect;
18755997Sbostic 				}
18855997Sbostic 				break;
18955997Sbostic 			case 'w':
19055997Sbostic 				if (pd)
19155997Sbostic 					break;
19255997Sbostic 				if (cp->u.fd == -1 && (cp->u.fd = open(cp->t,
19355997Sbostic 				    O_WRONLY|O_APPEND|O_CREAT|O_TRUNC,
19455997Sbostic 				    DEFFILEMODE)) == -1)
19555997Sbostic 					err(FATAL, "%s: %s\n",
19655997Sbostic 					    cp->t, strerror(errno));
19759073Selan 				if (write(cp->u.fd, ps, psl) != psl)
19855997Sbostic 					err(FATAL, "%s: %s\n",
19955997Sbostic 					    cp->t, strerror(errno));
20055997Sbostic 				break;
20155997Sbostic 			case 'x':
20257421Sbostic 				if (hs == NULL)
20357421Sbostic 					cspace(&HS, "", 0, REPLACE);
20455997Sbostic 				tspace = PS;
20555997Sbostic 				PS = HS;
20655997Sbostic 				HS = tspace;
20755997Sbostic 				break;
20855997Sbostic 			case 'y':
20955997Sbostic 				if (pd)
21055997Sbostic 					break;
21159073Selan 				for (p = ps, len = psl; --len; ++p)
21255997Sbostic 					*p = cp->u.y[*p];
21355997Sbostic 				break;
21455997Sbostic 			case ':':
21555997Sbostic 			case '}':
21655997Sbostic 				break;
21755997Sbostic 			case '=':
21855997Sbostic 				(void)printf("%lu\n", linenum);
21955997Sbostic 			}
22055997Sbostic 			cp = cp->next;
22155997Sbostic 		} /* for all cp */
22255997Sbostic 
22355997Sbostic new:		if (!nflag && !pd)
22457733Selan 			OUT(ps)
22555997Sbostic 		flush_appends();
22655997Sbostic 	} /* for all lines */
22755997Sbostic }
22855997Sbostic 
22955997Sbostic /*
23056019Sbostic  * TRUE if the address passed matches the current program state
23156019Sbostic  * (lastline, linenumber, ps).
23256019Sbostic  */
23357733Selan #define	MATCH(a)						\
23457733Selan 	(a)->type == AT_RE ? regexec_e((a)->u.r, ps, 0, 1, psl) :	\
23556019Sbostic 	    (a)->type == AT_LINE ? linenum == (a)->u.l : lastline
23656019Sbostic 
23756019Sbostic /*
23855997Sbostic  * Return TRUE if the command applies to the current line.  Sets the inrange
23955997Sbostic  * flag to process ranges.  Interprets the non-select (``!'') flag.
24055997Sbostic  */
24155997Sbostic static inline int
applies(cp)24255997Sbostic applies(cp)
24355997Sbostic 	struct s_command *cp;
24455997Sbostic {
24555997Sbostic 	int r;
24655997Sbostic 
24755997Sbostic 	lastaddr = 0;
24855997Sbostic 	if (cp->a1 == NULL && cp->a2 == NULL)
24955997Sbostic 		r = 1;
25055997Sbostic 	else if (cp->a2)
25155997Sbostic 		if (cp->inrange) {
25256019Sbostic 			if (MATCH(cp->a2)) {
25355997Sbostic 				cp->inrange = 0;
25455997Sbostic 				lastaddr = 1;
25555997Sbostic 			}
25655997Sbostic 			r = 1;
25756019Sbostic 		} else if (MATCH(cp->a1)) {
25855997Sbostic 			/*
25955997Sbostic 			 * If the second address is a number less than or
26055997Sbostic 			 * equal to the line number first selected, only
26155997Sbostic 			 * one line shall be selected.
26255997Sbostic 			 *	-- POSIX 1003.2
26355997Sbostic 			 */
26455997Sbostic 			if (cp->a2->type == AT_LINE &&
26555997Sbostic 			    linenum >= cp->a2->u.l)
26655997Sbostic 				lastaddr = 1;
26755997Sbostic 			else
26855997Sbostic 				cp->inrange = 1;
26955997Sbostic 			r = 1;
27055997Sbostic 		} else
27155997Sbostic 			r = 0;
27255997Sbostic 	else
27356019Sbostic 		r = MATCH(cp->a1);
27455997Sbostic 	return (cp->nonsel ? ! r : r);
27555997Sbostic }
27655997Sbostic 
27755997Sbostic /*
27855997Sbostic  * substitute --
27955997Sbostic  *	Do substitutions in the pattern space.  Currently, we build a
28055997Sbostic  *	copy of the new pattern space in the substitute space structure
28155997Sbostic  *	and then swap them.
28255997Sbostic  */
28355997Sbostic static int
substitute(cp)28455997Sbostic substitute(cp)
28555997Sbostic 	struct s_command *cp;
28655997Sbostic {
28755997Sbostic 	SPACE tspace;
28856019Sbostic 	regex_t *re;
28957733Selan 	size_t re_off, slen;
29067038Sbostic 	int lastempty, n;
29156657Sbostic 	char *s;
29255997Sbostic 
29355997Sbostic 	s = ps;
29456019Sbostic 	re = cp->u.s->re;
29556019Sbostic 	if (re == NULL) {
29656079Sbostic 		if (defpreg != NULL && cp->u.s->maxbref > defpreg->re_nsub) {
29756019Sbostic 			linenum = cp->u.s->linenum;
29856019Sbostic 			err(COMPILE, "\\%d not defined in the RE",
29956019Sbostic 			    cp->u.s->maxbref);
30056019Sbostic 		}
30156079Sbostic 	}
30257733Selan 	if (!regexec_e(re, s, 0, 0, psl))
30355997Sbostic 		return (0);
30455997Sbostic 
30567038Sbostic   	SS.len = 0;				/* Clean substitute space. */
30667038Sbostic   	slen = psl;
30767038Sbostic   	n = cp->u.s->n;
30867038Sbostic 	lastempty = 1;
30966120Sbostic 
31067038Sbostic   	switch (n) {
31167038Sbostic   	case 0:					/* Global */
31267038Sbostic   		do {
31367038Sbostic 			if (lastempty || match[0].rm_so != match[0].rm_eo) {
31467038Sbostic 				/* Locate start of replaced string. */
31567038Sbostic 				re_off = match[0].rm_so;
31667038Sbostic 				/* Copy leading retained string. */
31767038Sbostic 				cspace(&SS, s, re_off, APPEND);
31867038Sbostic 				/* Add in regular expression. */
31967038Sbostic 				regsub(&SS, s, cp->u.s->new);
32067038Sbostic 			}
32167038Sbostic 
32267038Sbostic   			/* Move past this match. */
32367038Sbostic 			if (match[0].rm_so != match[0].rm_eo) {
32467038Sbostic 				s += match[0].rm_eo;
32567038Sbostic 				slen -= match[0].rm_eo;
32667038Sbostic 				lastempty = 0;
32767038Sbostic 			} else {
32867038Sbostic 				if (match[0].rm_so == 0)
32967038Sbostic 					cspace(&SS,
33067038Sbostic 					    s, match[0].rm_so + 1, APPEND);
33167038Sbostic 				else
33267038Sbostic 					cspace(&SS,
33367038Sbostic 					    s + match[0].rm_so, 1, APPEND);
33467038Sbostic 				s += match[0].rm_so + 1;
33567038Sbostic 				slen -= match[0].rm_so + 1;
33667038Sbostic 				lastempty = 1;
33767038Sbostic 			}
33867038Sbostic 		} while (slen > 0 && regexec_e(re, s, REG_NOTBOL, 0, slen));
33955997Sbostic 		/* Copy trailing retained string. */
34067038Sbostic 		if (slen > 0)
34167038Sbostic 			cspace(&SS, s, slen, APPEND);
34267038Sbostic   		break;
34355997Sbostic 	default:				/* Nth occurrence */
34455997Sbostic 		while (--n) {
34556079Sbostic 			s += match[0].rm_eo;
34657733Selan 			slen -= match[0].rm_eo;
34757733Selan 			if (!regexec_e(re, s, REG_NOTBOL, 0, slen))
34855997Sbostic 				return (0);
34955997Sbostic 		}
35055997Sbostic 		/* FALLTHROUGH */
35155997Sbostic 	case 1:					/* 1st occurrence */
35255997Sbostic 		/* Locate start of replaced string. */
35356079Sbostic 		re_off = match[0].rm_so + (s - ps);
35455997Sbostic 		/* Copy leading retained string. */
35556079Sbostic 		cspace(&SS, ps, re_off, APPEND);
35655997Sbostic 		/* Add in regular expression. */
35756079Sbostic 		regsub(&SS, s, cp->u.s->new);
35855997Sbostic 		/* Copy trailing retained string. */
35956079Sbostic 		s += match[0].rm_eo;
36057733Selan 		slen -= match[0].rm_eo;
36157733Selan 		cspace(&SS, s, slen, APPEND);
36255997Sbostic 		break;
36355997Sbostic 	}
36455997Sbostic 
36555997Sbostic 	/*
36655997Sbostic 	 * Swap the substitute space and the pattern space, and make sure
36755997Sbostic 	 * that any leftover pointers into stdio memory get lost.
36855997Sbostic 	 */
36955997Sbostic 	tspace = PS;
37055997Sbostic 	PS = SS;
37155997Sbostic 	SS = tspace;
37255997Sbostic 	SS.space = SS.back;
37355997Sbostic 
37455997Sbostic 	/* Handle the 'p' flag. */
37555997Sbostic 	if (cp->u.s->p)
37657733Selan 		OUT(ps)
37755997Sbostic 
37855997Sbostic 	/* Handle the 'w' flag. */
37955997Sbostic 	if (cp->u.s->wfile && !pd) {
38055997Sbostic 		if (cp->u.s->wfd == -1 && (cp->u.s->wfd = open(cp->u.s->wfile,
38155997Sbostic 		    O_WRONLY|O_APPEND|O_CREAT|O_TRUNC, DEFFILEMODE)) == -1)
38255997Sbostic 			err(FATAL, "%s: %s\n", cp->u.s->wfile, strerror(errno));
38359073Selan 		if (write(cp->u.s->wfd, ps, psl) != psl)
38455997Sbostic 			err(FATAL, "%s: %s\n", cp->u.s->wfile, strerror(errno));
38555997Sbostic 	}
38655997Sbostic 	return (1);
38755997Sbostic }
38855997Sbostic 
38955997Sbostic /*
39055997Sbostic  * Flush append requests.  Always called before reading a line,
39155997Sbostic  * therefore it also resets the substitution done (sdone) flag.
39255997Sbostic  */
39355997Sbostic static void
flush_appends()39455997Sbostic flush_appends()
39555997Sbostic {
39655997Sbostic 	FILE *f;
39755997Sbostic 	int count, i;
39855997Sbostic 	char buf[8 * 1024];
39955997Sbostic 
40055997Sbostic 	for (i = 0; i < appendx; i++)
40155997Sbostic 		switch (appends[i].type) {
40255997Sbostic 		case AP_STRING:
40357733Selan 			fwrite(appends[i].s, sizeof(char), appends[i].len,
40457733Selan 			    stdout);
40555997Sbostic 			break;
40655997Sbostic 		case AP_FILE:
40755997Sbostic 			/*
40855997Sbostic 			 * Read files probably shouldn't be cached.  Since
40955997Sbostic 			 * it's not an error to read a non-existent file,
41055997Sbostic 			 * it's possible that another program is interacting
41155997Sbostic 			 * with the sed script through the file system.  It
41255997Sbostic 			 * would be truly bizarre, but possible.  It's probably
41355997Sbostic 			 * not that big a performance win, anyhow.
41455997Sbostic 			 */
41555997Sbostic 			if ((f = fopen(appends[i].s, "r")) == NULL)
41655997Sbostic 				break;
41759073Selan 			while (count = fread(buf, sizeof(char), sizeof(buf), f))
41857733Selan 				(void)fwrite(buf, sizeof(char), count, stdout);
41955997Sbostic 			(void)fclose(f);
42055997Sbostic 			break;
42155997Sbostic 		}
42255997Sbostic 	if (ferror(stdout))
42355997Sbostic 		err(FATAL, "stdout: %s", strerror(errno ? errno : EIO));
42456949Sbostic 	appendx = sdone = 0;
42555997Sbostic }
42655997Sbostic 
42755997Sbostic static void
lputs(s)42855997Sbostic lputs(s)
42955997Sbostic 	register char *s;
43055997Sbostic {
43155997Sbostic 	register int count;
43255997Sbostic 	register char *escapes, *p;
43355997Sbostic 	struct winsize win;
43455997Sbostic 	static int termwidth = -1;
43555997Sbostic 
43655997Sbostic 	if (termwidth == -1)
43755997Sbostic 		if (p = getenv("COLUMNS"))
43855997Sbostic 			termwidth = atoi(p);
43955997Sbostic 		else if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) == 0 &&
44055997Sbostic 		    win.ws_col > 0)
44155997Sbostic 			termwidth = win.ws_col;
44255997Sbostic 		else
44355997Sbostic 			termwidth = 60;
44455997Sbostic 
44555997Sbostic 	for (count = 0; *s; ++s) {
44655997Sbostic 		if (count >= termwidth) {
44755997Sbostic 			(void)printf("\\\n");
44855997Sbostic 			count = 0;
44955997Sbostic 		}
45055997Sbostic 		if (isascii(*s) && isprint(*s) && *s != '\\') {
45155997Sbostic 			(void)putchar(*s);
45255997Sbostic 			count++;
45355997Sbostic 		} else {
45455997Sbostic 			escapes = "\\\a\b\f\n\r\t\v";
45555997Sbostic 			(void)putchar('\\');
45655997Sbostic 			if (p = strchr(escapes, *s)) {
45755997Sbostic 				(void)putchar("\\abfnrtv"[p - escapes]);
45855997Sbostic 				count += 2;
45955997Sbostic 			} else {
46059073Selan 				(void)printf("%03o", *(u_char *)s);
46155997Sbostic 				count += 4;
46255997Sbostic 			}
46355997Sbostic 		}
46455997Sbostic 	}
46555997Sbostic 	(void)putchar('$');
46655997Sbostic 	(void)putchar('\n');
46755997Sbostic 	if (ferror(stdout))
46855997Sbostic 		err(FATAL, "stdout: %s", strerror(errno ? errno : EIO));
46955997Sbostic }
47055997Sbostic 
47156019Sbostic static inline int
regexec_e(preg,string,eflags,nomatch,slen)47257733Selan regexec_e(preg, string, eflags, nomatch, slen)
47355997Sbostic 	regex_t *preg;
47455997Sbostic 	const char *string;
47556079Sbostic 	int eflags, nomatch;
47657733Selan 	size_t slen;
47755997Sbostic {
47855997Sbostic 	int eval;
47957733Selan 
48056019Sbostic 	if (preg == NULL) {
48156019Sbostic 		if (defpreg == NULL)
48256019Sbostic 			err(FATAL, "first RE may not be empty");
48356079Sbostic 	} else
48456019Sbostic 		defpreg = preg;
48556019Sbostic 
48659077Storek 	/* Set anchors, discounting trailing newline (if any). */
48759077Storek 	if (slen > 0 && string[slen - 1] == '\n')
48859077Storek 		slen--;
48959073Selan 	match[0].rm_so = 0;
49059077Storek 	match[0].rm_eo = slen;
49159073Selan 
49256079Sbostic 	eval = regexec(defpreg, string,
49359073Selan 	    nomatch ? 0 : maxnsub + 1, match, eflags | REG_STARTEND);
49456019Sbostic 	switch(eval) {
49555997Sbostic 	case 0:
49656019Sbostic 		return (1);
49756019Sbostic 	case REG_NOMATCH:
49855997Sbostic 		return (0);
49955997Sbostic 	}
50056019Sbostic 	err(FATAL, "RE error: %s", strregerror(eval, defpreg));
50155997Sbostic 	/* NOTREACHED */
50255997Sbostic }
50355997Sbostic 
50455997Sbostic /*
50555997Sbostic  * regsub - perform substitutions after a regexp match
50655997Sbostic  * Based on a routine by Henry Spencer
50755997Sbostic  */
50855997Sbostic static void
regsub(sp,string,src)50956079Sbostic regsub(sp, string, src)
51056079Sbostic 	SPACE *sp;
51155997Sbostic 	char *string, *src;
51255997Sbostic {
51355997Sbostic 	register int len, no;
51455997Sbostic 	register char c, *dst;
51555997Sbostic 
51655997Sbostic #define	NEEDSP(reqlen)							\
51755997Sbostic 	if (sp->len >= sp->blen - (reqlen) - 1) {			\
51855997Sbostic 		sp->blen += (reqlen) + 1024;				\
51955997Sbostic 		sp->space = sp->back = xrealloc(sp->back, sp->blen);	\
52055997Sbostic 		dst = sp->space + sp->len;				\
52155997Sbostic 	}
52255997Sbostic 
52355997Sbostic 	dst = sp->space + sp->len;
52455997Sbostic 	while ((c = *src++) != '\0') {
52555997Sbostic 		if (c == '&')
52655997Sbostic 			no = 0;
52755997Sbostic 		else if (c == '\\' && isdigit(*src))
52855997Sbostic 			no = *src++ - '0';
52955997Sbostic 		else
53055997Sbostic 			no = -1;
53155997Sbostic 		if (no < 0) {		/* Ordinary character. */
53255997Sbostic  			if (c == '\\' && (*src == '\\' || *src == '&'))
53355997Sbostic  				c = *src++;
53455997Sbostic 			NEEDSP(1);
53555997Sbostic  			*dst++ = c;
53655997Sbostic 			++sp->len;
53756079Sbostic  		} else if (match[no].rm_so != -1 && match[no].rm_eo != -1) {
53856079Sbostic 			len = match[no].rm_eo - match[no].rm_so;
53955997Sbostic 			NEEDSP(len);
54056079Sbostic 			memmove(dst, string + match[no].rm_so, len);
54155997Sbostic 			dst += len;
54255997Sbostic 			sp->len += len;
54355997Sbostic 		}
54455997Sbostic 	}
54555997Sbostic 	NEEDSP(1);
54655997Sbostic 	*dst = '\0';
54755997Sbostic }
54855997Sbostic 
54955997Sbostic /*
55055997Sbostic  * aspace --
55155997Sbostic  *	Append the source space to the destination space, allocating new
55255997Sbostic  *	space as necessary.
55355997Sbostic  */
55456079Sbostic void
cspace(sp,p,len,spflag)55556079Sbostic cspace(sp, p, len, spflag)
55655997Sbostic 	SPACE *sp;
55755997Sbostic 	char *p;
55855997Sbostic 	size_t len;
55956079Sbostic 	enum e_spflag spflag;
56055997Sbostic {
56155997Sbostic 	size_t tlen;
56255997Sbostic 
56359073Selan 	/* Make sure SPACE has enough memory and ramp up quickly. */
56459073Selan 	tlen = sp->len + len + 1;
56555997Sbostic 	if (tlen > sp->blen) {
56655997Sbostic 		sp->blen = tlen + 1024;
56756079Sbostic 		sp->space = sp->back = xrealloc(sp->back, sp->blen);
56855997Sbostic 	}
56955997Sbostic 
57059073Selan 	if (spflag == REPLACE)
57156079Sbostic 		sp->len = 0;
57255997Sbostic 
57356079Sbostic 	memmove(sp->space + sp->len, p, len);
57457733Selan 
57556079Sbostic 	sp->space[sp->len += len] = '\0';
57655997Sbostic }
57755997Sbostic 
57855997Sbostic /*
57955997Sbostic  * Close all cached opened files and report any errors
58055997Sbostic  */
58155997Sbostic void
cfclose(cp,end)58256093Sbostic cfclose(cp, end)
58356093Sbostic 	register struct s_command *cp, *end;
58455997Sbostic {
58555997Sbostic 
58656093Sbostic 	for (; cp != end; cp = cp->next)
58755997Sbostic 		switch(cp->code) {
58855997Sbostic 		case 's':
58955997Sbostic 			if (cp->u.s->wfd != -1 && close(cp->u.s->wfd))
59055997Sbostic 				err(FATAL,
59155997Sbostic 				    "%s: %s", cp->u.s->wfile, strerror(errno));
59256064Sbostic 			cp->u.s->wfd = -1;
59355997Sbostic 			break;
59455997Sbostic 		case 'w':
59555997Sbostic 			if (cp->u.fd != -1 && close(cp->u.fd))
59655997Sbostic 				err(FATAL, "%s: %s", cp->t, strerror(errno));
59756064Sbostic 			cp->u.fd = -1;
59855997Sbostic 			break;
59955997Sbostic 		case '{':
60056093Sbostic 			cfclose(cp->u.c, cp->next);
60155997Sbostic 			break;
60255997Sbostic 		}
60355997Sbostic }
604