xref: /csrg-svn/usr.bin/sed/process.c (revision 57572)
155997Sbostic /*-
255997Sbostic  * Copyright (c) 1992 Diomidis Spinellis.
355997Sbostic  * Copyright (c) 1992 The Regents of the University of California.
455997Sbostic  * 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*57572Selan static char sccsid[] = "@(#)process.c	5.12 (Berkeley) 01/15/93";
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 *));
4456079Sbostic static inline int	 regexec_e __P((regex_t *, const char *, int, int));
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. */
5555997Sbostic static struct iovec iov[2] = { NULL, 0, "\n", 1 };
5655997Sbostic 
5756019Sbostic static regex_t *defpreg;
5856079Sbostic size_t maxnsub;
5956079Sbostic regmatch_t *match;
6056019Sbostic 
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;
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;
10756005Sbostic 				if ((p = strchr(ps, '\n')) == NULL)
10856005Sbostic 					pd = 1;
10956005Sbostic 				else {
11055997Sbostic 					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':
11856079Sbostic 				cspace(&PS, hs, hsl, APPENDNL);
11955997Sbostic 				break;
12055997Sbostic 			case 'h':
12156079Sbostic 				cspace(&HS, ps, psl, REPLACE);
12255997Sbostic 				break;
12355997Sbostic 			case 'H':
12456079Sbostic 				cspace(&HS, ps, psl, APPENDNL);
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)
13455997Sbostic 					(void)printf("%s\n", ps);
13555997Sbostic 				flush_appends();
13656079Sbostic 				r = mf_fgets(&PS, REPLACE);
13755997Sbostic #ifdef HISTORIC_PRACTICE
13856079Sbostic 				if (!r)
13955997Sbostic 					exit(0);
14055997Sbostic #endif
14155997Sbostic 				pd = 0;
14255997Sbostic 				break;
14355997Sbostic 			case 'N':
14455997Sbostic 				flush_appends();
14556079Sbostic 				if (!mf_fgets(&PS, APPENDNL)) {
14655997Sbostic 					if (!nflag && !pd)
14755997Sbostic 						(void)printf("%s\n", ps);
14855997Sbostic 					exit(0);
14955997Sbostic 				}
15055997Sbostic 				break;
15155997Sbostic 			case 'p':
15255997Sbostic 				if (pd)
15355997Sbostic 					break;
15455997Sbostic 				(void)printf("%s\n", ps);
15555997Sbostic 				break;
15655997Sbostic 			case 'P':
15755997Sbostic 				if (pd)
15855997Sbostic 					break;
15955997Sbostic 				if ((p = strchr(ps, '\n')) != NULL) {
16055997Sbostic 					oldc = *p;
16155997Sbostic 					*p = '\0';
16255997Sbostic 				}
16355997Sbostic 				(void)printf("%s\n", ps);
16455997Sbostic 				if (p != NULL)
16555997Sbostic 					*p = oldc;
16655997Sbostic 				break;
16755997Sbostic 			case 'q':
16855997Sbostic 				if (!nflag && !pd)
16955997Sbostic 					(void)printf("%s\n", ps);
17055997Sbostic 				flush_appends();
17155997Sbostic 				exit(0);
17255997Sbostic 			case 'r':
17355997Sbostic 				if (appendx >= appendnum)
17455997Sbostic 					appends = xrealloc(appends,
17555997Sbostic 					    sizeof(struct s_appends) *
17655997Sbostic 					    (appendnum *= 2));
17755997Sbostic 				appends[appendx].type = AP_FILE;
17855997Sbostic 				appends[appendx].s = cp->t;
17955997Sbostic 				appendx++;
18055997Sbostic 				break;
18155997Sbostic 			case 's':
18256949Sbostic 				sdone |= substitute(cp);
18355997Sbostic 				break;
18455997Sbostic 			case 't':
18555997Sbostic 				if (sdone) {
18655997Sbostic 					sdone = 0;
18755997Sbostic 					cp = cp->u.c;
18855997Sbostic 					goto redirect;
18955997Sbostic 				}
19055997Sbostic 				break;
19155997Sbostic 			case 'w':
19255997Sbostic 				if (pd)
19355997Sbostic 					break;
19455997Sbostic 				if (cp->u.fd == -1 && (cp->u.fd = open(cp->t,
19555997Sbostic 				    O_WRONLY|O_APPEND|O_CREAT|O_TRUNC,
19655997Sbostic 				    DEFFILEMODE)) == -1)
19755997Sbostic 					err(FATAL, "%s: %s\n",
19855997Sbostic 					    cp->t, strerror(errno));
19955997Sbostic 				iov[0].iov_base = ps;
20055997Sbostic 				iov[0].iov_len = psl;
20155997Sbostic 				if (writev(cp->u.fd, iov, 2) != psl + 1)
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;
21555997Sbostic 				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)
22855997Sbostic 			(void)printf("%s\n", 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  */
23756019Sbostic #define	MATCH(a)							\
23856079Sbostic 	(a)->type == AT_RE ? regexec_e((a)->u.r, ps, 0, 1) :		\
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;
29356091Sbostic 	size_t re_off;
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 	}
30656079Sbostic 	if (!regexec_e(re, s, 0, 0))
30755997Sbostic 		return (0);
30855997Sbostic 
30955997Sbostic 	SS.len = 0;				/* Clean substitute space. */
31055997Sbostic 	n = cp->u.s->n;
31155997Sbostic 	switch (n) {
31255997Sbostic 	case 0:					/* Global */
31355997Sbostic 		do {
31455997Sbostic 			/* Locate start of replaced string. */
31556079Sbostic 			re_off = match[0].rm_so;
31655997Sbostic 			/* Copy leading retained string. */
31756079Sbostic 			cspace(&SS, s, re_off, APPEND);
31855997Sbostic 			/* Add in regular expression. */
31956079Sbostic 			regsub(&SS, s, cp->u.s->new);
32055997Sbostic 			/* Move past this match. */
32156079Sbostic 			s += match[0].rm_eo;
32256079Sbostic 		} while(regexec_e(re, s, REG_NOTBOL, 0));
32355997Sbostic 		/* Copy trailing retained string. */
32456079Sbostic 		cspace(&SS, s, strlen(s), APPEND);
32555997Sbostic 		break;
32655997Sbostic 	default:				/* Nth occurrence */
32755997Sbostic 		while (--n) {
32856079Sbostic 			s += match[0].rm_eo;
32956079Sbostic 			if (!regexec_e(re, s, REG_NOTBOL, 0))
33055997Sbostic 				return (0);
33155997Sbostic 		}
33255997Sbostic 		/* FALLTHROUGH */
33355997Sbostic 	case 1:					/* 1st occurrence */
33455997Sbostic 		/* Locate start of replaced string. */
33556079Sbostic 		re_off = match[0].rm_so + (s - ps);
33655997Sbostic 		/* Copy leading retained string. */
33756079Sbostic 		cspace(&SS, ps, re_off, APPEND);
33855997Sbostic 		/* Add in regular expression. */
33956079Sbostic 		regsub(&SS, s, cp->u.s->new);
34055997Sbostic 		/* Copy trailing retained string. */
34156079Sbostic 		s += match[0].rm_eo;
34256079Sbostic 		cspace(&SS, s, strlen(s), APPEND);
34355997Sbostic 		break;
34455997Sbostic 	}
34555997Sbostic 
34655997Sbostic 	/*
34755997Sbostic 	 * Swap the substitute space and the pattern space, and make sure
34855997Sbostic 	 * that any leftover pointers into stdio memory get lost.
34955997Sbostic 	 */
35055997Sbostic 	tspace = PS;
35155997Sbostic 	PS = SS;
35255997Sbostic 	SS = tspace;
35355997Sbostic 	SS.space = SS.back;
35455997Sbostic 
35555997Sbostic 	/* Handle the 'p' flag. */
35655997Sbostic 	if (cp->u.s->p)
35755997Sbostic 		(void)printf("%s\n", ps);
35855997Sbostic 
35955997Sbostic 	/* Handle the 'w' flag. */
36055997Sbostic 	if (cp->u.s->wfile && !pd) {
36155997Sbostic 		if (cp->u.s->wfd == -1 && (cp->u.s->wfd = open(cp->u.s->wfile,
36255997Sbostic 		    O_WRONLY|O_APPEND|O_CREAT|O_TRUNC, DEFFILEMODE)) == -1)
36355997Sbostic 			err(FATAL, "%s: %s\n", cp->u.s->wfile, strerror(errno));
36455997Sbostic 		iov[0].iov_base = ps;
36555997Sbostic 		iov[0].iov_len = psl;
36655997Sbostic 		if (writev(cp->u.s->wfd, iov, 2) != psl + 1)
36755997Sbostic 			err(FATAL, "%s: %s\n", cp->u.s->wfile, strerror(errno));
36855997Sbostic 	}
36955997Sbostic 	return (1);
37055997Sbostic }
37155997Sbostic 
37255997Sbostic /*
37355997Sbostic  * Flush append requests.  Always called before reading a line,
37455997Sbostic  * therefore it also resets the substitution done (sdone) flag.
37555997Sbostic  */
37655997Sbostic static void
37755997Sbostic flush_appends()
37855997Sbostic {
37955997Sbostic 	FILE *f;
38055997Sbostic 	int count, i;
38155997Sbostic 	char buf[8 * 1024];
38255997Sbostic 
38355997Sbostic 	for (i = 0; i < appendx; i++)
38455997Sbostic 		switch (appends[i].type) {
38555997Sbostic 		case AP_STRING:
38655997Sbostic 			(void)printf("%s", appends[i].s);
38755997Sbostic 			break;
38855997Sbostic 		case AP_FILE:
38955997Sbostic 			/*
39055997Sbostic 			 * Read files probably shouldn't be cached.  Since
39155997Sbostic 			 * it's not an error to read a non-existent file,
39255997Sbostic 			 * it's possible that another program is interacting
39355997Sbostic 			 * with the sed script through the file system.  It
39455997Sbostic 			 * would be truly bizarre, but possible.  It's probably
39555997Sbostic 			 * not that big a performance win, anyhow.
39655997Sbostic 			 */
39755997Sbostic 			if ((f = fopen(appends[i].s, "r")) == NULL)
39855997Sbostic 				break;
39955997Sbostic 			while (count = fread(buf, 1, sizeof(buf), f))
40055997Sbostic 				(void)fwrite(buf, 1, count, stdout);
40155997Sbostic 			(void)fclose(f);
40255997Sbostic 			break;
40355997Sbostic 		}
40455997Sbostic 	if (ferror(stdout))
40555997Sbostic 		err(FATAL, "stdout: %s", strerror(errno ? errno : EIO));
40656949Sbostic 	appendx = sdone = 0;
40755997Sbostic }
40855997Sbostic 
40955997Sbostic static void
41055997Sbostic lputs(s)
41155997Sbostic 	register char *s;
41255997Sbostic {
41355997Sbostic 	register int count;
41455997Sbostic 	register char *escapes, *p;
41555997Sbostic 	struct winsize win;
41655997Sbostic 	static int termwidth = -1;
41755997Sbostic 
41855997Sbostic 	if (termwidth == -1)
41955997Sbostic 		if (p = getenv("COLUMNS"))
42055997Sbostic 			termwidth = atoi(p);
42155997Sbostic 		else if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) == 0 &&
42255997Sbostic 		    win.ws_col > 0)
42355997Sbostic 			termwidth = win.ws_col;
42455997Sbostic 		else
42555997Sbostic 			termwidth = 60;
42655997Sbostic 
42755997Sbostic 	for (count = 0; *s; ++s) {
42855997Sbostic 		if (count >= termwidth) {
42955997Sbostic 			(void)printf("\\\n");
43055997Sbostic 			count = 0;
43155997Sbostic 		}
43255997Sbostic 		if (isascii(*s) && isprint(*s) && *s != '\\') {
43355997Sbostic 			(void)putchar(*s);
43455997Sbostic 			count++;
43555997Sbostic 		} else {
43655997Sbostic 			escapes = "\\\a\b\f\n\r\t\v";
43755997Sbostic 			(void)putchar('\\');
43855997Sbostic 			if (p = strchr(escapes, *s)) {
43955997Sbostic 				(void)putchar("\\abfnrtv"[p - escapes]);
44055997Sbostic 				count += 2;
44155997Sbostic 			} else {
44255997Sbostic 				(void)printf("%03o", (u_char)*s);
44355997Sbostic 				count += 4;
44455997Sbostic 			}
44555997Sbostic 		}
44655997Sbostic 	}
44755997Sbostic 	(void)putchar('$');
44855997Sbostic 	(void)putchar('\n');
44955997Sbostic 	if (ferror(stdout))
45055997Sbostic 		err(FATAL, "stdout: %s", strerror(errno ? errno : EIO));
45155997Sbostic }
45255997Sbostic 
45356019Sbostic static inline int
45456079Sbostic regexec_e(preg, string, eflags, nomatch)
45555997Sbostic 	regex_t *preg;
45655997Sbostic 	const char *string;
45756079Sbostic 	int eflags, nomatch;
45855997Sbostic {
45955997Sbostic 	int eval;
46055997Sbostic 
46156019Sbostic 	if (preg == NULL) {
46256019Sbostic 		if (defpreg == NULL)
46356019Sbostic 			err(FATAL, "first RE may not be empty");
46456079Sbostic 	} else
46556019Sbostic 		defpreg = preg;
46656019Sbostic 
46756079Sbostic 	eval = regexec(defpreg, string,
46856079Sbostic 	    nomatch ? 0 : maxnsub + 1, match, eflags);
46956019Sbostic 	switch(eval) {
47055997Sbostic 	case 0:
47156019Sbostic 		return (1);
47256019Sbostic 	case REG_NOMATCH:
47355997Sbostic 		return (0);
47455997Sbostic 	}
47556019Sbostic 	err(FATAL, "RE error: %s", strregerror(eval, defpreg));
47655997Sbostic 	/* NOTREACHED */
47755997Sbostic }
47855997Sbostic 
47955997Sbostic /*
48055997Sbostic  * regsub - perform substitutions after a regexp match
48155997Sbostic  * Based on a routine by Henry Spencer
48255997Sbostic  */
48355997Sbostic static void
48456079Sbostic regsub(sp, string, src)
48556079Sbostic 	SPACE *sp;
48655997Sbostic 	char *string, *src;
48755997Sbostic {
48855997Sbostic 	register int len, no;
48955997Sbostic 	register char c, *dst;
49055997Sbostic 
49155997Sbostic #define	NEEDSP(reqlen)							\
49255997Sbostic 	if (sp->len >= sp->blen - (reqlen) - 1) {			\
49355997Sbostic 		sp->blen += (reqlen) + 1024;				\
49455997Sbostic 		sp->space = sp->back = xrealloc(sp->back, sp->blen);	\
49555997Sbostic 		dst = sp->space + sp->len;				\
49655997Sbostic 	}
49755997Sbostic 
49855997Sbostic 	dst = sp->space + sp->len;
49955997Sbostic 	while ((c = *src++) != '\0') {
50055997Sbostic 		if (c == '&')
50155997Sbostic 			no = 0;
50255997Sbostic 		else if (c == '\\' && isdigit(*src))
50355997Sbostic 			no = *src++ - '0';
50455997Sbostic 		else
50555997Sbostic 			no = -1;
50655997Sbostic 		if (no < 0) {		/* Ordinary character. */
50755997Sbostic  			if (c == '\\' && (*src == '\\' || *src == '&'))
50855997Sbostic  				c = *src++;
50955997Sbostic 			NEEDSP(1);
51055997Sbostic  			*dst++ = c;
51155997Sbostic 			++sp->len;
51256079Sbostic  		} else if (match[no].rm_so != -1 && match[no].rm_eo != -1) {
51356079Sbostic 			len = match[no].rm_eo - match[no].rm_so;
51455997Sbostic 			NEEDSP(len);
51556079Sbostic 			memmove(dst, string + match[no].rm_so, len);
51655997Sbostic 			dst += len;
51755997Sbostic 			sp->len += len;
51855997Sbostic 		}
51955997Sbostic 	}
52055997Sbostic 	NEEDSP(1);
52155997Sbostic 	*dst = '\0';
52255997Sbostic }
52355997Sbostic 
52455997Sbostic /*
52555997Sbostic  * aspace --
52655997Sbostic  *	Append the source space to the destination space, allocating new
52755997Sbostic  *	space as necessary.
52855997Sbostic  */
52956079Sbostic void
53056079Sbostic cspace(sp, p, len, spflag)
53155997Sbostic 	SPACE *sp;
53255997Sbostic 	char *p;
53355997Sbostic 	size_t len;
53456079Sbostic 	enum e_spflag spflag;
53555997Sbostic {
53655997Sbostic 	size_t tlen;
53755997Sbostic 
53855997Sbostic 	/*
53956079Sbostic 	 * Make sure SPACE has enough memory and ramp up quickly.  Appends
54056079Sbostic 	 * need two extra bytes, one for the newline, one for a terminating
54156079Sbostic 	 * NULL.
54255997Sbostic 	 */
543*57572Selan 	tlen = sp->len + len + (spflag == APPENDNL ? 2 : 1);
54455997Sbostic 	if (tlen > sp->blen) {
54555997Sbostic 		sp->blen = tlen + 1024;
54656079Sbostic 		sp->space = sp->back = xrealloc(sp->back, sp->blen);
54755997Sbostic 	}
54855997Sbostic 
54956079Sbostic 	if (spflag == APPENDNL)
55055997Sbostic 		sp->space[sp->len++] = '\n';
55156079Sbostic 	else if (spflag == REPLACE)
55256079Sbostic 		sp->len = 0;
55355997Sbostic 
55456079Sbostic 	memmove(sp->space + sp->len, p, len);
55556079Sbostic 	sp->space[sp->len += len] = '\0';
55655997Sbostic }
55755997Sbostic 
55855997Sbostic /*
55955997Sbostic  * Close all cached opened files and report any errors
56055997Sbostic  */
56155997Sbostic void
56256093Sbostic cfclose(cp, end)
56356093Sbostic 	register struct s_command *cp, *end;
56455997Sbostic {
56555997Sbostic 
56656093Sbostic 	for (; cp != end; cp = cp->next)
56755997Sbostic 		switch(cp->code) {
56855997Sbostic 		case 's':
56955997Sbostic 			if (cp->u.s->wfd != -1 && close(cp->u.s->wfd))
57055997Sbostic 				err(FATAL,
57155997Sbostic 				    "%s: %s", cp->u.s->wfile, strerror(errno));
57256064Sbostic 			cp->u.s->wfd = -1;
57355997Sbostic 			break;
57455997Sbostic 		case 'w':
57555997Sbostic 			if (cp->u.fd != -1 && close(cp->u.fd))
57655997Sbostic 				err(FATAL, "%s: %s", cp->t, strerror(errno));
57756064Sbostic 			cp->u.fd = -1;
57855997Sbostic 			break;
57955997Sbostic 		case '{':
58056093Sbostic 			cfclose(cp->u.c, cp->next);
58155997Sbostic 			break;
58255997Sbostic 		}
58355997Sbostic }
584