xref: /csrg-svn/usr.bin/sed/process.c (revision 67036)
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*67036Sbostic static char sccsid[] = "@(#)process.c	8.4 (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
6255997Sbostic process()
6355997Sbostic {
6455997Sbostic 	struct s_command *cp;
6555997Sbostic 	SPACE tspace;
6655997Sbostic 	size_t len;
6756079Sbostic 	int r;
6855997Sbostic 	char oldc, *p;
6955997Sbostic 
7056079Sbostic 	for (linenum = 0; mf_fgets(&PS, REPLACE);) {
7155997Sbostic 		pd = 0;
7255997Sbostic 		cp = prog;
7355997Sbostic redirect:
7455997Sbostic 		while (cp != NULL) {
7555997Sbostic 			if (!applies(cp)) {
7655997Sbostic 				cp = cp->next;
7755997Sbostic 				continue;
7855997Sbostic 			}
7955997Sbostic 			switch (cp->code) {
8055997Sbostic 			case '{':
8155997Sbostic 				cp = cp->u.c;
8255997Sbostic 				goto redirect;
8355997Sbostic 			case 'a':
8455997Sbostic 				if (appendx >= appendnum)
8555997Sbostic 					appends = xrealloc(appends,
8655997Sbostic 					    sizeof(struct s_appends) *
8755997Sbostic 					    (appendnum *= 2));
8855997Sbostic 				appends[appendx].type = AP_STRING;
8955997Sbostic 				appends[appendx].s = cp->t;
9057733Selan 				appends[appendx].len = strlen(cp->t);
9155997Sbostic 				appendx++;
9255997Sbostic 				break;
9355997Sbostic 			case 'b':
9455997Sbostic 				cp = cp->u.c;
9555997Sbostic 				goto redirect;
9655997Sbostic 			case 'c':
9755997Sbostic 				pd = 1;
9855997Sbostic 				psl = 0;
9955997Sbostic 				if (cp->a2 == NULL || lastaddr)
10055997Sbostic 					(void)printf("%s", cp->t);
10155997Sbostic 				break;
10255997Sbostic 			case 'd':
10356005Sbostic 				pd = 1;
10455997Sbostic 				goto new;
10555997Sbostic 			case 'D':
10655997Sbostic 				if (pd)
10755997Sbostic 					goto new;
10860120Selan 				if ((p = memchr(ps, '\n', psl)) == NULL)
10956005Sbostic 					pd = 1;
11056005Sbostic 				else {
111*67036Sbostic 					psl -= (p - ps) + 1;
11255997Sbostic 					memmove(ps, p + 1, psl);
11355997Sbostic 				}
11455997Sbostic 				goto new;
11555997Sbostic 			case 'g':
11656079Sbostic 				cspace(&PS, hs, hsl, REPLACE);
11755997Sbostic 				break;
11855997Sbostic 			case 'G':
11959073Selan 				cspace(&PS, hs, hsl, 0);
12055997Sbostic 				break;
12155997Sbostic 			case 'h':
12256079Sbostic 				cspace(&HS, ps, psl, REPLACE);
12355997Sbostic 				break;
12455997Sbostic 			case 'H':
12559073Selan 				cspace(&HS, ps, psl, 0);
12655997Sbostic 				break;
12755997Sbostic 			case 'i':
12855997Sbostic 				(void)printf("%s", cp->t);
12955997Sbostic 				break;
13055997Sbostic 			case 'l':
13155997Sbostic 				lputs(ps);
13255997Sbostic 				break;
13355997Sbostic 			case 'n':
13455997Sbostic 				if (!nflag && !pd)
13557733Selan 					OUT(ps)
13655997Sbostic 				flush_appends();
13756079Sbostic 				r = mf_fgets(&PS, REPLACE);
13855997Sbostic #ifdef HISTORIC_PRACTICE
13956079Sbostic 				if (!r)
14055997Sbostic 					exit(0);
14155997Sbostic #endif
14255997Sbostic 				pd = 0;
14355997Sbostic 				break;
14455997Sbostic 			case 'N':
14555997Sbostic 				flush_appends();
14659073Selan 				if (!mf_fgets(&PS, 0)) {
14755997Sbostic 					if (!nflag && !pd)
14857733Selan 						OUT(ps)
14955997Sbostic 					exit(0);
15055997Sbostic 				}
15155997Sbostic 				break;
15255997Sbostic 			case 'p':
15355997Sbostic 				if (pd)
15455997Sbostic 					break;
15557733Selan 				OUT(ps)
15655997Sbostic 				break;
15755997Sbostic 			case 'P':
15855997Sbostic 				if (pd)
15955997Sbostic 					break;
16060120Selan 				if ((p = memchr(ps, '\n', psl)) != NULL) {
16155997Sbostic 					oldc = *p;
16255997Sbostic 					*p = '\0';
16355997Sbostic 				}
16457733Selan 				OUT(ps)
16555997Sbostic 				if (p != NULL)
16655997Sbostic 					*p = oldc;
16755997Sbostic 				break;
16855997Sbostic 			case 'q':
16955997Sbostic 				if (!nflag && !pd)
17057733Selan 					OUT(ps)
17155997Sbostic 				flush_appends();
17255997Sbostic 				exit(0);
17355997Sbostic 			case 'r':
17455997Sbostic 				if (appendx >= appendnum)
17555997Sbostic 					appends = xrealloc(appends,
17655997Sbostic 					    sizeof(struct s_appends) *
17755997Sbostic 					    (appendnum *= 2));
17855997Sbostic 				appends[appendx].type = AP_FILE;
17955997Sbostic 				appends[appendx].s = cp->t;
18057733Selan 				appends[appendx].len = strlen(cp->t);
18155997Sbostic 				appendx++;
18255997Sbostic 				break;
18355997Sbostic 			case 's':
18456949Sbostic 				sdone |= substitute(cp);
18555997Sbostic 				break;
18655997Sbostic 			case 't':
18755997Sbostic 				if (sdone) {
18855997Sbostic 					sdone = 0;
18955997Sbostic 					cp = cp->u.c;
19055997Sbostic 					goto redirect;
19155997Sbostic 				}
19255997Sbostic 				break;
19355997Sbostic 			case 'w':
19455997Sbostic 				if (pd)
19555997Sbostic 					break;
19655997Sbostic 				if (cp->u.fd == -1 && (cp->u.fd = open(cp->t,
19755997Sbostic 				    O_WRONLY|O_APPEND|O_CREAT|O_TRUNC,
19855997Sbostic 				    DEFFILEMODE)) == -1)
19955997Sbostic 					err(FATAL, "%s: %s\n",
20055997Sbostic 					    cp->t, strerror(errno));
20159073Selan 				if (write(cp->u.fd, ps, psl) != psl)
20255997Sbostic 					err(FATAL, "%s: %s\n",
20355997Sbostic 					    cp->t, strerror(errno));
20455997Sbostic 				break;
20555997Sbostic 			case 'x':
20657421Sbostic 				if (hs == NULL)
20757421Sbostic 					cspace(&HS, "", 0, REPLACE);
20855997Sbostic 				tspace = PS;
20955997Sbostic 				PS = HS;
21055997Sbostic 				HS = tspace;
21155997Sbostic 				break;
21255997Sbostic 			case 'y':
21355997Sbostic 				if (pd)
21455997Sbostic 					break;
21559073Selan 				for (p = ps, len = psl; --len; ++p)
21655997Sbostic 					*p = cp->u.y[*p];
21755997Sbostic 				break;
21855997Sbostic 			case ':':
21955997Sbostic 			case '}':
22055997Sbostic 				break;
22155997Sbostic 			case '=':
22255997Sbostic 				(void)printf("%lu\n", linenum);
22355997Sbostic 			}
22455997Sbostic 			cp = cp->next;
22555997Sbostic 		} /* for all cp */
22655997Sbostic 
22755997Sbostic new:		if (!nflag && !pd)
22857733Selan 			OUT(ps)
22955997Sbostic 		flush_appends();
23055997Sbostic 	} /* for all lines */
23155997Sbostic }
23255997Sbostic 
23355997Sbostic /*
23456019Sbostic  * TRUE if the address passed matches the current program state
23556019Sbostic  * (lastline, linenumber, ps).
23656019Sbostic  */
23757733Selan #define	MATCH(a)						\
23857733Selan 	(a)->type == AT_RE ? regexec_e((a)->u.r, ps, 0, 1, psl) :	\
23956019Sbostic 	    (a)->type == AT_LINE ? linenum == (a)->u.l : lastline
24056019Sbostic 
24156019Sbostic /*
24255997Sbostic  * Return TRUE if the command applies to the current line.  Sets the inrange
24355997Sbostic  * flag to process ranges.  Interprets the non-select (``!'') flag.
24455997Sbostic  */
24555997Sbostic static inline int
24655997Sbostic applies(cp)
24755997Sbostic 	struct s_command *cp;
24855997Sbostic {
24955997Sbostic 	int r;
25055997Sbostic 
25155997Sbostic 	lastaddr = 0;
25255997Sbostic 	if (cp->a1 == NULL && cp->a2 == NULL)
25355997Sbostic 		r = 1;
25455997Sbostic 	else if (cp->a2)
25555997Sbostic 		if (cp->inrange) {
25656019Sbostic 			if (MATCH(cp->a2)) {
25755997Sbostic 				cp->inrange = 0;
25855997Sbostic 				lastaddr = 1;
25955997Sbostic 			}
26055997Sbostic 			r = 1;
26156019Sbostic 		} else if (MATCH(cp->a1)) {
26255997Sbostic 			/*
26355997Sbostic 			 * If the second address is a number less than or
26455997Sbostic 			 * equal to the line number first selected, only
26555997Sbostic 			 * one line shall be selected.
26655997Sbostic 			 *	-- POSIX 1003.2
26755997Sbostic 			 */
26855997Sbostic 			if (cp->a2->type == AT_LINE &&
26955997Sbostic 			    linenum >= cp->a2->u.l)
27055997Sbostic 				lastaddr = 1;
27155997Sbostic 			else
27255997Sbostic 				cp->inrange = 1;
27355997Sbostic 			r = 1;
27455997Sbostic 		} else
27555997Sbostic 			r = 0;
27655997Sbostic 	else
27756019Sbostic 		r = MATCH(cp->a1);
27855997Sbostic 	return (cp->nonsel ? ! r : r);
27955997Sbostic }
28055997Sbostic 
28155997Sbostic /*
28255997Sbostic  * substitute --
28355997Sbostic  *	Do substitutions in the pattern space.  Currently, we build a
28455997Sbostic  *	copy of the new pattern space in the substitute space structure
28555997Sbostic  *	and then swap them.
28655997Sbostic  */
28755997Sbostic static int
28855997Sbostic substitute(cp)
28955997Sbostic 	struct s_command *cp;
29055997Sbostic {
29155997Sbostic 	SPACE tspace;
29256019Sbostic 	regex_t *re;
29357733Selan 	size_t re_off, slen;
29456091Sbostic 	int n;
29556657Sbostic 	char *s;
29655997Sbostic 
29755997Sbostic 	s = ps;
29856019Sbostic 	re = cp->u.s->re;
29956019Sbostic 	if (re == NULL) {
30056079Sbostic 		if (defpreg != NULL && cp->u.s->maxbref > defpreg->re_nsub) {
30156019Sbostic 			linenum = cp->u.s->linenum;
30256019Sbostic 			err(COMPILE, "\\%d not defined in the RE",
30356019Sbostic 			    cp->u.s->maxbref);
30456019Sbostic 		}
30556079Sbostic 	}
30657733Selan 	if (!regexec_e(re, s, 0, 0, psl))
30755997Sbostic 		return (0);
30855997Sbostic 
30955997Sbostic 	SS.len = 0;				/* Clean substitute space. */
31057733Selan 	slen = psl;
31155997Sbostic 	n = cp->u.s->n;
31255997Sbostic 	switch (n) {
31355997Sbostic 	case 0:					/* Global */
31455997Sbostic 		do {
31555997Sbostic 			/* Locate start of replaced string. */
31656079Sbostic 			re_off = match[0].rm_so;
31755997Sbostic 			/* Copy leading retained string. */
31856079Sbostic 			cspace(&SS, s, re_off, APPEND);
31955997Sbostic 			/* Add in regular expression. */
32056079Sbostic 			regsub(&SS, s, cp->u.s->new);
32155997Sbostic 			/* Move past this match. */
32256079Sbostic 			s += match[0].rm_eo;
32357733Selan 			slen -= match[0].rm_eo;
32466120Sbostic 		} while(match[0].rm_so != match[0].rm_eo &&
32566120Sbostic 		    regexec_e(re, s, REG_NOTBOL, 0, slen));
32666120Sbostic 
32755997Sbostic 		/* Copy trailing retained string. */
32857733Selan 		cspace(&SS, s, slen, APPEND);
32955997Sbostic 		break;
33055997Sbostic 	default:				/* Nth occurrence */
33155997Sbostic 		while (--n) {
33256079Sbostic 			s += match[0].rm_eo;
33357733Selan 			slen -= match[0].rm_eo;
33457733Selan 			if (!regexec_e(re, s, REG_NOTBOL, 0, slen))
33555997Sbostic 				return (0);
33655997Sbostic 		}
33755997Sbostic 		/* FALLTHROUGH */
33855997Sbostic 	case 1:					/* 1st occurrence */
33955997Sbostic 		/* Locate start of replaced string. */
34056079Sbostic 		re_off = match[0].rm_so + (s - ps);
34155997Sbostic 		/* Copy leading retained string. */
34256079Sbostic 		cspace(&SS, ps, re_off, APPEND);
34355997Sbostic 		/* Add in regular expression. */
34456079Sbostic 		regsub(&SS, s, cp->u.s->new);
34555997Sbostic 		/* Copy trailing retained string. */
34656079Sbostic 		s += match[0].rm_eo;
34757733Selan 		slen -= match[0].rm_eo;
34857733Selan 		cspace(&SS, s, slen, APPEND);
34955997Sbostic 		break;
35055997Sbostic 	}
35155997Sbostic 
35255997Sbostic 	/*
35355997Sbostic 	 * Swap the substitute space and the pattern space, and make sure
35455997Sbostic 	 * that any leftover pointers into stdio memory get lost.
35555997Sbostic 	 */
35655997Sbostic 	tspace = PS;
35755997Sbostic 	PS = SS;
35855997Sbostic 	SS = tspace;
35955997Sbostic 	SS.space = SS.back;
36055997Sbostic 
36155997Sbostic 	/* Handle the 'p' flag. */
36255997Sbostic 	if (cp->u.s->p)
36357733Selan 		OUT(ps)
36455997Sbostic 
36555997Sbostic 	/* Handle the 'w' flag. */
36655997Sbostic 	if (cp->u.s->wfile && !pd) {
36755997Sbostic 		if (cp->u.s->wfd == -1 && (cp->u.s->wfd = open(cp->u.s->wfile,
36855997Sbostic 		    O_WRONLY|O_APPEND|O_CREAT|O_TRUNC, DEFFILEMODE)) == -1)
36955997Sbostic 			err(FATAL, "%s: %s\n", cp->u.s->wfile, strerror(errno));
37059073Selan 		if (write(cp->u.s->wfd, ps, psl) != psl)
37155997Sbostic 			err(FATAL, "%s: %s\n", cp->u.s->wfile, strerror(errno));
37255997Sbostic 	}
37355997Sbostic 	return (1);
37455997Sbostic }
37555997Sbostic 
37655997Sbostic /*
37755997Sbostic  * Flush append requests.  Always called before reading a line,
37855997Sbostic  * therefore it also resets the substitution done (sdone) flag.
37955997Sbostic  */
38055997Sbostic static void
38155997Sbostic flush_appends()
38255997Sbostic {
38355997Sbostic 	FILE *f;
38455997Sbostic 	int count, i;
38555997Sbostic 	char buf[8 * 1024];
38655997Sbostic 
38755997Sbostic 	for (i = 0; i < appendx; i++)
38855997Sbostic 		switch (appends[i].type) {
38955997Sbostic 		case AP_STRING:
39057733Selan 			fwrite(appends[i].s, sizeof(char), appends[i].len,
39157733Selan 			    stdout);
39255997Sbostic 			break;
39355997Sbostic 		case AP_FILE:
39455997Sbostic 			/*
39555997Sbostic 			 * Read files probably shouldn't be cached.  Since
39655997Sbostic 			 * it's not an error to read a non-existent file,
39755997Sbostic 			 * it's possible that another program is interacting
39855997Sbostic 			 * with the sed script through the file system.  It
39955997Sbostic 			 * would be truly bizarre, but possible.  It's probably
40055997Sbostic 			 * not that big a performance win, anyhow.
40155997Sbostic 			 */
40255997Sbostic 			if ((f = fopen(appends[i].s, "r")) == NULL)
40355997Sbostic 				break;
40459073Selan 			while (count = fread(buf, sizeof(char), sizeof(buf), f))
40557733Selan 				(void)fwrite(buf, sizeof(char), count, stdout);
40655997Sbostic 			(void)fclose(f);
40755997Sbostic 			break;
40855997Sbostic 		}
40955997Sbostic 	if (ferror(stdout))
41055997Sbostic 		err(FATAL, "stdout: %s", strerror(errno ? errno : EIO));
41156949Sbostic 	appendx = sdone = 0;
41255997Sbostic }
41355997Sbostic 
41455997Sbostic static void
41555997Sbostic lputs(s)
41655997Sbostic 	register char *s;
41755997Sbostic {
41855997Sbostic 	register int count;
41955997Sbostic 	register char *escapes, *p;
42055997Sbostic 	struct winsize win;
42155997Sbostic 	static int termwidth = -1;
42255997Sbostic 
42355997Sbostic 	if (termwidth == -1)
42455997Sbostic 		if (p = getenv("COLUMNS"))
42555997Sbostic 			termwidth = atoi(p);
42655997Sbostic 		else if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) == 0 &&
42755997Sbostic 		    win.ws_col > 0)
42855997Sbostic 			termwidth = win.ws_col;
42955997Sbostic 		else
43055997Sbostic 			termwidth = 60;
43155997Sbostic 
43255997Sbostic 	for (count = 0; *s; ++s) {
43355997Sbostic 		if (count >= termwidth) {
43455997Sbostic 			(void)printf("\\\n");
43555997Sbostic 			count = 0;
43655997Sbostic 		}
43755997Sbostic 		if (isascii(*s) && isprint(*s) && *s != '\\') {
43855997Sbostic 			(void)putchar(*s);
43955997Sbostic 			count++;
44055997Sbostic 		} else {
44155997Sbostic 			escapes = "\\\a\b\f\n\r\t\v";
44255997Sbostic 			(void)putchar('\\');
44355997Sbostic 			if (p = strchr(escapes, *s)) {
44455997Sbostic 				(void)putchar("\\abfnrtv"[p - escapes]);
44555997Sbostic 				count += 2;
44655997Sbostic 			} else {
44759073Selan 				(void)printf("%03o", *(u_char *)s);
44855997Sbostic 				count += 4;
44955997Sbostic 			}
45055997Sbostic 		}
45155997Sbostic 	}
45255997Sbostic 	(void)putchar('$');
45355997Sbostic 	(void)putchar('\n');
45455997Sbostic 	if (ferror(stdout))
45555997Sbostic 		err(FATAL, "stdout: %s", strerror(errno ? errno : EIO));
45655997Sbostic }
45755997Sbostic 
45856019Sbostic static inline int
45957733Selan regexec_e(preg, string, eflags, nomatch, slen)
46055997Sbostic 	regex_t *preg;
46155997Sbostic 	const char *string;
46256079Sbostic 	int eflags, nomatch;
46357733Selan 	size_t slen;
46455997Sbostic {
46555997Sbostic 	int eval;
46657733Selan 
46756019Sbostic 	if (preg == NULL) {
46856019Sbostic 		if (defpreg == NULL)
46956019Sbostic 			err(FATAL, "first RE may not be empty");
47056079Sbostic 	} else
47156019Sbostic 		defpreg = preg;
47256019Sbostic 
47359077Storek 	/* Set anchors, discounting trailing newline (if any). */
47459077Storek 	if (slen > 0 && string[slen - 1] == '\n')
47559077Storek 		slen--;
47659073Selan 	match[0].rm_so = 0;
47759077Storek 	match[0].rm_eo = slen;
47859073Selan 
47956079Sbostic 	eval = regexec(defpreg, string,
48059073Selan 	    nomatch ? 0 : maxnsub + 1, match, eflags | REG_STARTEND);
48156019Sbostic 	switch(eval) {
48255997Sbostic 	case 0:
48356019Sbostic 		return (1);
48456019Sbostic 	case REG_NOMATCH:
48555997Sbostic 		return (0);
48655997Sbostic 	}
48756019Sbostic 	err(FATAL, "RE error: %s", strregerror(eval, defpreg));
48855997Sbostic 	/* NOTREACHED */
48955997Sbostic }
49055997Sbostic 
49155997Sbostic /*
49255997Sbostic  * regsub - perform substitutions after a regexp match
49355997Sbostic  * Based on a routine by Henry Spencer
49455997Sbostic  */
49555997Sbostic static void
49656079Sbostic regsub(sp, string, src)
49756079Sbostic 	SPACE *sp;
49855997Sbostic 	char *string, *src;
49955997Sbostic {
50055997Sbostic 	register int len, no;
50155997Sbostic 	register char c, *dst;
50255997Sbostic 
50355997Sbostic #define	NEEDSP(reqlen)							\
50455997Sbostic 	if (sp->len >= sp->blen - (reqlen) - 1) {			\
50555997Sbostic 		sp->blen += (reqlen) + 1024;				\
50655997Sbostic 		sp->space = sp->back = xrealloc(sp->back, sp->blen);	\
50755997Sbostic 		dst = sp->space + sp->len;				\
50855997Sbostic 	}
50955997Sbostic 
51055997Sbostic 	dst = sp->space + sp->len;
51155997Sbostic 	while ((c = *src++) != '\0') {
51255997Sbostic 		if (c == '&')
51355997Sbostic 			no = 0;
51455997Sbostic 		else if (c == '\\' && isdigit(*src))
51555997Sbostic 			no = *src++ - '0';
51655997Sbostic 		else
51755997Sbostic 			no = -1;
51855997Sbostic 		if (no < 0) {		/* Ordinary character. */
51955997Sbostic  			if (c == '\\' && (*src == '\\' || *src == '&'))
52055997Sbostic  				c = *src++;
52155997Sbostic 			NEEDSP(1);
52255997Sbostic  			*dst++ = c;
52355997Sbostic 			++sp->len;
52456079Sbostic  		} else if (match[no].rm_so != -1 && match[no].rm_eo != -1) {
52556079Sbostic 			len = match[no].rm_eo - match[no].rm_so;
52655997Sbostic 			NEEDSP(len);
52756079Sbostic 			memmove(dst, string + match[no].rm_so, len);
52855997Sbostic 			dst += len;
52955997Sbostic 			sp->len += len;
53055997Sbostic 		}
53155997Sbostic 	}
53255997Sbostic 	NEEDSP(1);
53355997Sbostic 	*dst = '\0';
53455997Sbostic }
53555997Sbostic 
53655997Sbostic /*
53755997Sbostic  * aspace --
53855997Sbostic  *	Append the source space to the destination space, allocating new
53955997Sbostic  *	space as necessary.
54055997Sbostic  */
54156079Sbostic void
54256079Sbostic cspace(sp, p, len, spflag)
54355997Sbostic 	SPACE *sp;
54455997Sbostic 	char *p;
54555997Sbostic 	size_t len;
54656079Sbostic 	enum e_spflag spflag;
54755997Sbostic {
54855997Sbostic 	size_t tlen;
54955997Sbostic 
55059073Selan 	/* Make sure SPACE has enough memory and ramp up quickly. */
55159073Selan 	tlen = sp->len + len + 1;
55255997Sbostic 	if (tlen > sp->blen) {
55355997Sbostic 		sp->blen = tlen + 1024;
55456079Sbostic 		sp->space = sp->back = xrealloc(sp->back, sp->blen);
55555997Sbostic 	}
55655997Sbostic 
55759073Selan 	if (spflag == REPLACE)
55856079Sbostic 		sp->len = 0;
55955997Sbostic 
56056079Sbostic 	memmove(sp->space + sp->len, p, len);
56157733Selan 
56256079Sbostic 	sp->space[sp->len += len] = '\0';
56355997Sbostic }
56455997Sbostic 
56555997Sbostic /*
56655997Sbostic  * Close all cached opened files and report any errors
56755997Sbostic  */
56855997Sbostic void
56956093Sbostic cfclose(cp, end)
57056093Sbostic 	register struct s_command *cp, *end;
57155997Sbostic {
57255997Sbostic 
57356093Sbostic 	for (; cp != end; cp = cp->next)
57455997Sbostic 		switch(cp->code) {
57555997Sbostic 		case 's':
57655997Sbostic 			if (cp->u.s->wfd != -1 && close(cp->u.s->wfd))
57755997Sbostic 				err(FATAL,
57855997Sbostic 				    "%s: %s", cp->u.s->wfile, strerror(errno));
57956064Sbostic 			cp->u.s->wfd = -1;
58055997Sbostic 			break;
58155997Sbostic 		case 'w':
58255997Sbostic 			if (cp->u.fd != -1 && close(cp->u.fd))
58355997Sbostic 				err(FATAL, "%s: %s", cp->t, strerror(errno));
58456064Sbostic 			cp->u.fd = -1;
58555997Sbostic 			break;
58655997Sbostic 		case '{':
58756093Sbostic 			cfclose(cp->u.c, cp->next);
58855997Sbostic 			break;
58955997Sbostic 		}
59055997Sbostic }
591