xref: /csrg-svn/usr.bin/sed/process.c (revision 57733)
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*57733Selan static char sccsid[] = "@(#)process.c	5.13 (Berkeley) 01/27/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 *));
44*57733Selan 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. */
5555997Sbostic static struct iovec iov[2] = { NULL, 0, "\n", 1 };
5655997Sbostic 
5756019Sbostic static regex_t *defpreg;
5856079Sbostic size_t maxnsub;
59*57733Selan regmatch_t *match, startend;
6056019Sbostic 
61*57733Selan #define OUT(s) { fwrite(s, sizeof(u_char), psl, stdout); putchar('\n'); }
62*57733Selan 
6355997Sbostic void
6455997Sbostic process()
6555997Sbostic {
6655997Sbostic 	struct s_command *cp;
6755997Sbostic 	SPACE tspace;
6855997Sbostic 	size_t len;
6956079Sbostic 	int r;
7055997Sbostic 	char oldc, *p;
7155997Sbostic 
7256079Sbostic 	for (linenum = 0; mf_fgets(&PS, REPLACE);) {
7355997Sbostic 		pd = 0;
7455997Sbostic 		cp = prog;
7555997Sbostic redirect:
7655997Sbostic 		while (cp != NULL) {
7755997Sbostic 			if (!applies(cp)) {
7855997Sbostic 				cp = cp->next;
7955997Sbostic 				continue;
8055997Sbostic 			}
8155997Sbostic 			switch (cp->code) {
8255997Sbostic 			case '{':
8355997Sbostic 				cp = cp->u.c;
8455997Sbostic 				goto redirect;
8555997Sbostic 			case 'a':
8655997Sbostic 				if (appendx >= appendnum)
8755997Sbostic 					appends = xrealloc(appends,
8855997Sbostic 					    sizeof(struct s_appends) *
8955997Sbostic 					    (appendnum *= 2));
9055997Sbostic 				appends[appendx].type = AP_STRING;
9155997Sbostic 				appends[appendx].s = cp->t;
92*57733Selan 				appends[appendx].len = strlen(cp->t);
9355997Sbostic 				appendx++;
9455997Sbostic 				break;
9555997Sbostic 			case 'b':
9655997Sbostic 				cp = cp->u.c;
9755997Sbostic 				goto redirect;
9855997Sbostic 			case 'c':
9955997Sbostic 				pd = 1;
10055997Sbostic 				psl = 0;
10155997Sbostic 				if (cp->a2 == NULL || lastaddr)
10255997Sbostic 					(void)printf("%s", cp->t);
10355997Sbostic 				break;
10455997Sbostic 			case 'd':
10556005Sbostic 				pd = 1;
10655997Sbostic 				goto new;
10755997Sbostic 			case 'D':
10855997Sbostic 				if (pd)
10955997Sbostic 					goto new;
110*57733Selan 				if ((p = strnchr(ps, '\n', psl)) == NULL)
11156005Sbostic 					pd = 1;
11256005Sbostic 				else {
11355997Sbostic 					psl -= (p - ps) - 1;
11455997Sbostic 					memmove(ps, p + 1, psl);
11555997Sbostic 				}
11655997Sbostic 				goto new;
11755997Sbostic 			case 'g':
11856079Sbostic 				cspace(&PS, hs, hsl, REPLACE);
11955997Sbostic 				break;
12055997Sbostic 			case 'G':
12156079Sbostic 				cspace(&PS, hs, hsl, APPENDNL);
12255997Sbostic 				break;
12355997Sbostic 			case 'h':
12456079Sbostic 				cspace(&HS, ps, psl, REPLACE);
12555997Sbostic 				break;
12655997Sbostic 			case 'H':
12756079Sbostic 				cspace(&HS, ps, psl, APPENDNL);
12855997Sbostic 				break;
12955997Sbostic 			case 'i':
13055997Sbostic 				(void)printf("%s", cp->t);
13155997Sbostic 				break;
13255997Sbostic 			case 'l':
13355997Sbostic 				lputs(ps);
13455997Sbostic 				break;
13555997Sbostic 			case 'n':
13655997Sbostic 				if (!nflag && !pd)
137*57733Selan 					OUT(ps)
13855997Sbostic 				flush_appends();
13956079Sbostic 				r = mf_fgets(&PS, REPLACE);
14055997Sbostic #ifdef HISTORIC_PRACTICE
14156079Sbostic 				if (!r)
14255997Sbostic 					exit(0);
14355997Sbostic #endif
14455997Sbostic 				pd = 0;
14555997Sbostic 				break;
14655997Sbostic 			case 'N':
14755997Sbostic 				flush_appends();
14856079Sbostic 				if (!mf_fgets(&PS, APPENDNL)) {
14955997Sbostic 					if (!nflag && !pd)
150*57733Selan 						OUT(ps)
15155997Sbostic 					exit(0);
15255997Sbostic 				}
15355997Sbostic 				break;
15455997Sbostic 			case 'p':
15555997Sbostic 				if (pd)
15655997Sbostic 					break;
157*57733Selan 				OUT(ps)
15855997Sbostic 				break;
15955997Sbostic 			case 'P':
16055997Sbostic 				if (pd)
16155997Sbostic 					break;
162*57733Selan 				if ((p = strnchr(ps, '\n', psl)) != NULL) {
16355997Sbostic 					oldc = *p;
16455997Sbostic 					*p = '\0';
16555997Sbostic 				}
166*57733Selan 				OUT(ps)
16755997Sbostic 				if (p != NULL)
16855997Sbostic 					*p = oldc;
16955997Sbostic 				break;
17055997Sbostic 			case 'q':
17155997Sbostic 				if (!nflag && !pd)
172*57733Selan 					OUT(ps)
17355997Sbostic 				flush_appends();
17455997Sbostic 				exit(0);
17555997Sbostic 			case 'r':
17655997Sbostic 				if (appendx >= appendnum)
17755997Sbostic 					appends = xrealloc(appends,
17855997Sbostic 					    sizeof(struct s_appends) *
17955997Sbostic 					    (appendnum *= 2));
18055997Sbostic 				appends[appendx].type = AP_FILE;
18155997Sbostic 				appends[appendx].s = cp->t;
182*57733Selan 				appends[appendx].len = strlen(cp->t);
18355997Sbostic 				appendx++;
18455997Sbostic 				break;
18555997Sbostic 			case 's':
18656949Sbostic 				sdone |= substitute(cp);
18755997Sbostic 				break;
18855997Sbostic 			case 't':
18955997Sbostic 				if (sdone) {
19055997Sbostic 					sdone = 0;
19155997Sbostic 					cp = cp->u.c;
19255997Sbostic 					goto redirect;
19355997Sbostic 				}
19455997Sbostic 				break;
19555997Sbostic 			case 'w':
19655997Sbostic 				if (pd)
19755997Sbostic 					break;
19855997Sbostic 				if (cp->u.fd == -1 && (cp->u.fd = open(cp->t,
19955997Sbostic 				    O_WRONLY|O_APPEND|O_CREAT|O_TRUNC,
20055997Sbostic 				    DEFFILEMODE)) == -1)
20155997Sbostic 					err(FATAL, "%s: %s\n",
20255997Sbostic 					    cp->t, strerror(errno));
20355997Sbostic 				iov[0].iov_base = ps;
20455997Sbostic 				iov[0].iov_len = psl;
20555997Sbostic 				if (writev(cp->u.fd, iov, 2) != psl + 1)
20655997Sbostic 					err(FATAL, "%s: %s\n",
20755997Sbostic 					    cp->t, strerror(errno));
20855997Sbostic 				break;
20955997Sbostic 			case 'x':
21057421Sbostic 				if (hs == NULL)
21157421Sbostic 					cspace(&HS, "", 0, REPLACE);
21255997Sbostic 				tspace = PS;
21355997Sbostic 				PS = HS;
21455997Sbostic 				HS = tspace;
21555997Sbostic 				break;
21655997Sbostic 			case 'y':
21755997Sbostic 				if (pd)
21855997Sbostic 					break;
21955997Sbostic 				for (p = ps, len = psl; len--; ++p)
22055997Sbostic 					*p = cp->u.y[*p];
22155997Sbostic 				break;
22255997Sbostic 			case ':':
22355997Sbostic 			case '}':
22455997Sbostic 				break;
22555997Sbostic 			case '=':
22655997Sbostic 				(void)printf("%lu\n", linenum);
22755997Sbostic 			}
22855997Sbostic 			cp = cp->next;
22955997Sbostic 		} /* for all cp */
23055997Sbostic 
23155997Sbostic new:		if (!nflag && !pd)
232*57733Selan 			OUT(ps)
23355997Sbostic 		flush_appends();
23455997Sbostic 	} /* for all lines */
23555997Sbostic }
23655997Sbostic 
23755997Sbostic /*
23856019Sbostic  * TRUE if the address passed matches the current program state
23956019Sbostic  * (lastline, linenumber, ps).
24056019Sbostic  */
241*57733Selan #define	MATCH(a)						\
242*57733Selan 	(a)->type == AT_RE ? regexec_e((a)->u.r, ps, 0, 1, psl) :	\
24356019Sbostic 	    (a)->type == AT_LINE ? linenum == (a)->u.l : lastline
24456019Sbostic 
24556019Sbostic /*
24655997Sbostic  * Return TRUE if the command applies to the current line.  Sets the inrange
24755997Sbostic  * flag to process ranges.  Interprets the non-select (``!'') flag.
24855997Sbostic  */
24955997Sbostic static inline int
25055997Sbostic applies(cp)
25155997Sbostic 	struct s_command *cp;
25255997Sbostic {
25355997Sbostic 	int r;
25455997Sbostic 
25555997Sbostic 	lastaddr = 0;
25655997Sbostic 	if (cp->a1 == NULL && cp->a2 == NULL)
25755997Sbostic 		r = 1;
25855997Sbostic 	else if (cp->a2)
25955997Sbostic 		if (cp->inrange) {
26056019Sbostic 			if (MATCH(cp->a2)) {
26155997Sbostic 				cp->inrange = 0;
26255997Sbostic 				lastaddr = 1;
26355997Sbostic 			}
26455997Sbostic 			r = 1;
26556019Sbostic 		} else if (MATCH(cp->a1)) {
26655997Sbostic 			/*
26755997Sbostic 			 * If the second address is a number less than or
26855997Sbostic 			 * equal to the line number first selected, only
26955997Sbostic 			 * one line shall be selected.
27055997Sbostic 			 *	-- POSIX 1003.2
27155997Sbostic 			 */
27255997Sbostic 			if (cp->a2->type == AT_LINE &&
27355997Sbostic 			    linenum >= cp->a2->u.l)
27455997Sbostic 				lastaddr = 1;
27555997Sbostic 			else
27655997Sbostic 				cp->inrange = 1;
27755997Sbostic 			r = 1;
27855997Sbostic 		} else
27955997Sbostic 			r = 0;
28055997Sbostic 	else
28156019Sbostic 		r = MATCH(cp->a1);
28255997Sbostic 	return (cp->nonsel ? ! r : r);
28355997Sbostic }
28455997Sbostic 
28555997Sbostic /*
28655997Sbostic  * substitute --
28755997Sbostic  *	Do substitutions in the pattern space.  Currently, we build a
28855997Sbostic  *	copy of the new pattern space in the substitute space structure
28955997Sbostic  *	and then swap them.
29055997Sbostic  */
29155997Sbostic static int
29255997Sbostic substitute(cp)
29355997Sbostic 	struct s_command *cp;
29455997Sbostic {
29555997Sbostic 	SPACE tspace;
29656019Sbostic 	regex_t *re;
297*57733Selan 	size_t re_off, slen;
29856091Sbostic 	int n;
29956657Sbostic 	char *s;
30055997Sbostic 
30155997Sbostic 	s = ps;
30256019Sbostic 	re = cp->u.s->re;
30356019Sbostic 	if (re == NULL) {
30456079Sbostic 		if (defpreg != NULL && cp->u.s->maxbref > defpreg->re_nsub) {
30556019Sbostic 			linenum = cp->u.s->linenum;
30656019Sbostic 			err(COMPILE, "\\%d not defined in the RE",
30756019Sbostic 			    cp->u.s->maxbref);
30856019Sbostic 		}
30956079Sbostic 	}
310*57733Selan 	if (!regexec_e(re, s, 0, 0, psl))
31155997Sbostic 		return (0);
31255997Sbostic 
31355997Sbostic 	SS.len = 0;				/* Clean substitute space. */
314*57733Selan 	slen = psl;
31555997Sbostic 	n = cp->u.s->n;
31655997Sbostic 	switch (n) {
31755997Sbostic 	case 0:					/* Global */
31855997Sbostic 		do {
31955997Sbostic 			/* Locate start of replaced string. */
32056079Sbostic 			re_off = match[0].rm_so;
32155997Sbostic 			/* Copy leading retained string. */
32256079Sbostic 			cspace(&SS, s, re_off, APPEND);
32355997Sbostic 			/* Add in regular expression. */
32456079Sbostic 			regsub(&SS, s, cp->u.s->new);
32555997Sbostic 			/* Move past this match. */
32656079Sbostic 			s += match[0].rm_eo;
327*57733Selan 			slen -= match[0].rm_eo;
328*57733Selan 		} while(regexec_e(re, s, REG_NOTBOL, 0, slen));
32955997Sbostic 		/* Copy trailing retained string. */
330*57733Selan 		cspace(&SS, s, slen, APPEND);
33155997Sbostic 		break;
33255997Sbostic 	default:				/* Nth occurrence */
33355997Sbostic 		while (--n) {
33456079Sbostic 			s += match[0].rm_eo;
335*57733Selan 			slen -= match[0].rm_eo;
336*57733Selan 			if (!regexec_e(re, s, REG_NOTBOL, 0, slen))
33755997Sbostic 				return (0);
33855997Sbostic 		}
33955997Sbostic 		/* FALLTHROUGH */
34055997Sbostic 	case 1:					/* 1st occurrence */
34155997Sbostic 		/* Locate start of replaced string. */
34256079Sbostic 		re_off = match[0].rm_so + (s - ps);
34355997Sbostic 		/* Copy leading retained string. */
34456079Sbostic 		cspace(&SS, ps, re_off, APPEND);
34555997Sbostic 		/* Add in regular expression. */
34656079Sbostic 		regsub(&SS, s, cp->u.s->new);
34755997Sbostic 		/* Copy trailing retained string. */
34856079Sbostic 		s += match[0].rm_eo;
349*57733Selan 		slen -= match[0].rm_eo;
350*57733Selan 		cspace(&SS, s, slen, APPEND);
35155997Sbostic 		break;
35255997Sbostic 	}
35355997Sbostic 
35455997Sbostic 	/*
35555997Sbostic 	 * Swap the substitute space and the pattern space, and make sure
35655997Sbostic 	 * that any leftover pointers into stdio memory get lost.
35755997Sbostic 	 */
35855997Sbostic 	tspace = PS;
35955997Sbostic 	PS = SS;
36055997Sbostic 	SS = tspace;
36155997Sbostic 	SS.space = SS.back;
36255997Sbostic 
36355997Sbostic 	/* Handle the 'p' flag. */
36455997Sbostic 	if (cp->u.s->p)
365*57733Selan 		OUT(ps)
36655997Sbostic 
36755997Sbostic 	/* Handle the 'w' flag. */
36855997Sbostic 	if (cp->u.s->wfile && !pd) {
36955997Sbostic 		if (cp->u.s->wfd == -1 && (cp->u.s->wfd = open(cp->u.s->wfile,
37055997Sbostic 		    O_WRONLY|O_APPEND|O_CREAT|O_TRUNC, DEFFILEMODE)) == -1)
37155997Sbostic 			err(FATAL, "%s: %s\n", cp->u.s->wfile, strerror(errno));
37255997Sbostic 		iov[0].iov_base = ps;
37355997Sbostic 		iov[0].iov_len = psl;
37455997Sbostic 		if (writev(cp->u.s->wfd, iov, 2) != psl + 1)
37555997Sbostic 			err(FATAL, "%s: %s\n", cp->u.s->wfile, strerror(errno));
37655997Sbostic 	}
37755997Sbostic 	return (1);
37855997Sbostic }
37955997Sbostic 
38055997Sbostic /*
38155997Sbostic  * Flush append requests.  Always called before reading a line,
38255997Sbostic  * therefore it also resets the substitution done (sdone) flag.
38355997Sbostic  */
38455997Sbostic static void
38555997Sbostic flush_appends()
38655997Sbostic {
38755997Sbostic 	FILE *f;
38855997Sbostic 	int count, i;
38955997Sbostic 	char buf[8 * 1024];
39055997Sbostic 
39155997Sbostic 	for (i = 0; i < appendx; i++)
39255997Sbostic 		switch (appends[i].type) {
39355997Sbostic 		case AP_STRING:
394*57733Selan 			fwrite(appends[i].s, sizeof(char), appends[i].len,
395*57733Selan 			    stdout);
39655997Sbostic 			break;
39755997Sbostic 		case AP_FILE:
39855997Sbostic 			/*
39955997Sbostic 			 * Read files probably shouldn't be cached.  Since
40055997Sbostic 			 * it's not an error to read a non-existent file,
40155997Sbostic 			 * it's possible that another program is interacting
40255997Sbostic 			 * with the sed script through the file system.  It
40355997Sbostic 			 * would be truly bizarre, but possible.  It's probably
40455997Sbostic 			 * not that big a performance win, anyhow.
40555997Sbostic 			 */
40655997Sbostic 			if ((f = fopen(appends[i].s, "r")) == NULL)
40755997Sbostic 				break;
408*57733Selan 			while (count = fread(buf, sizeof(char), sizeof(buf),
409*57733Selan 			    f))
410*57733Selan 				(void)fwrite(buf, sizeof(char), count, stdout);
41155997Sbostic 			(void)fclose(f);
41255997Sbostic 			break;
41355997Sbostic 		}
41455997Sbostic 	if (ferror(stdout))
41555997Sbostic 		err(FATAL, "stdout: %s", strerror(errno ? errno : EIO));
41656949Sbostic 	appendx = sdone = 0;
41755997Sbostic }
41855997Sbostic 
41955997Sbostic static void
42055997Sbostic lputs(s)
42155997Sbostic 	register char *s;
42255997Sbostic {
42355997Sbostic 	register int count;
42455997Sbostic 	register char *escapes, *p;
42555997Sbostic 	struct winsize win;
42655997Sbostic 	static int termwidth = -1;
42755997Sbostic 
42855997Sbostic 	if (termwidth == -1)
42955997Sbostic 		if (p = getenv("COLUMNS"))
43055997Sbostic 			termwidth = atoi(p);
43155997Sbostic 		else if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) == 0 &&
43255997Sbostic 		    win.ws_col > 0)
43355997Sbostic 			termwidth = win.ws_col;
43455997Sbostic 		else
43555997Sbostic 			termwidth = 60;
43655997Sbostic 
43755997Sbostic 	for (count = 0; *s; ++s) {
43855997Sbostic 		if (count >= termwidth) {
43955997Sbostic 			(void)printf("\\\n");
44055997Sbostic 			count = 0;
44155997Sbostic 		}
44255997Sbostic 		if (isascii(*s) && isprint(*s) && *s != '\\') {
44355997Sbostic 			(void)putchar(*s);
44455997Sbostic 			count++;
44555997Sbostic 		} else {
44655997Sbostic 			escapes = "\\\a\b\f\n\r\t\v";
44755997Sbostic 			(void)putchar('\\');
44855997Sbostic 			if (p = strchr(escapes, *s)) {
44955997Sbostic 				(void)putchar("\\abfnrtv"[p - escapes]);
45055997Sbostic 				count += 2;
45155997Sbostic 			} else {
45255997Sbostic 				(void)printf("%03o", (u_char)*s);
45355997Sbostic 				count += 4;
45455997Sbostic 			}
45555997Sbostic 		}
45655997Sbostic 	}
45755997Sbostic 	(void)putchar('$');
45855997Sbostic 	(void)putchar('\n');
45955997Sbostic 	if (ferror(stdout))
46055997Sbostic 		err(FATAL, "stdout: %s", strerror(errno ? errno : EIO));
46155997Sbostic }
46255997Sbostic 
46356019Sbostic static inline int
464*57733Selan regexec_e(preg, string, eflags, nomatch, slen)
46555997Sbostic 	regex_t *preg;
46655997Sbostic 	const char *string;
46756079Sbostic 	int eflags, nomatch;
468*57733Selan 	size_t slen;
46955997Sbostic {
47055997Sbostic 	int eval;
471*57733Selan 
472*57733Selan 	/* So we can work with binary files */
473*57733Selan 	startend.rm_so = 0;
474*57733Selan 	startend.rm_eo = slen;
475*57733Selan 	match[0] = startend;
476*57733Selan 
47755997Sbostic 
478*57733Selan 	eflags |= REG_STARTEND;
479*57733Selan 
48056019Sbostic 	if (preg == NULL) {
48156019Sbostic 		if (defpreg == NULL)
48256019Sbostic 			err(FATAL, "first RE may not be empty");
48356079Sbostic 	} else
48456019Sbostic 		defpreg = preg;
48556019Sbostic 
48656079Sbostic 	eval = regexec(defpreg, string,
48756079Sbostic 	    nomatch ? 0 : maxnsub + 1, match, eflags);
48856019Sbostic 	switch(eval) {
48955997Sbostic 	case 0:
49056019Sbostic 		return (1);
49156019Sbostic 	case REG_NOMATCH:
49255997Sbostic 		return (0);
49355997Sbostic 	}
49456019Sbostic 	err(FATAL, "RE error: %s", strregerror(eval, defpreg));
49555997Sbostic 	/* NOTREACHED */
49655997Sbostic }
49755997Sbostic 
49855997Sbostic /*
49955997Sbostic  * regsub - perform substitutions after a regexp match
50055997Sbostic  * Based on a routine by Henry Spencer
50155997Sbostic  */
50255997Sbostic static void
50356079Sbostic regsub(sp, string, src)
50456079Sbostic 	SPACE *sp;
50555997Sbostic 	char *string, *src;
50655997Sbostic {
50755997Sbostic 	register int len, no;
50855997Sbostic 	register char c, *dst;
50955997Sbostic 
51055997Sbostic #define	NEEDSP(reqlen)							\
51155997Sbostic 	if (sp->len >= sp->blen - (reqlen) - 1) {			\
51255997Sbostic 		sp->blen += (reqlen) + 1024;				\
51355997Sbostic 		sp->space = sp->back = xrealloc(sp->back, sp->blen);	\
51455997Sbostic 		dst = sp->space + sp->len;				\
51555997Sbostic 	}
51655997Sbostic 
51755997Sbostic 	dst = sp->space + sp->len;
51855997Sbostic 	while ((c = *src++) != '\0') {
51955997Sbostic 		if (c == '&')
52055997Sbostic 			no = 0;
52155997Sbostic 		else if (c == '\\' && isdigit(*src))
52255997Sbostic 			no = *src++ - '0';
52355997Sbostic 		else
52455997Sbostic 			no = -1;
52555997Sbostic 		if (no < 0) {		/* Ordinary character. */
52655997Sbostic  			if (c == '\\' && (*src == '\\' || *src == '&'))
52755997Sbostic  				c = *src++;
52855997Sbostic 			NEEDSP(1);
52955997Sbostic  			*dst++ = c;
53055997Sbostic 			++sp->len;
53156079Sbostic  		} else if (match[no].rm_so != -1 && match[no].rm_eo != -1) {
53256079Sbostic 			len = match[no].rm_eo - match[no].rm_so;
53355997Sbostic 			NEEDSP(len);
53456079Sbostic 			memmove(dst, string + match[no].rm_so, len);
53555997Sbostic 			dst += len;
53655997Sbostic 			sp->len += len;
53755997Sbostic 		}
53855997Sbostic 	}
53955997Sbostic 	NEEDSP(1);
54055997Sbostic 	*dst = '\0';
54155997Sbostic }
54255997Sbostic 
54355997Sbostic /*
54455997Sbostic  * aspace --
54555997Sbostic  *	Append the source space to the destination space, allocating new
54655997Sbostic  *	space as necessary.
54755997Sbostic  */
54856079Sbostic void
54956079Sbostic cspace(sp, p, len, spflag)
55055997Sbostic 	SPACE *sp;
55155997Sbostic 	char *p;
55255997Sbostic 	size_t len;
55356079Sbostic 	enum e_spflag spflag;
55455997Sbostic {
55555997Sbostic 	size_t tlen;
55655997Sbostic 
55755997Sbostic 	/*
55856079Sbostic 	 * Make sure SPACE has enough memory and ramp up quickly.  Appends
55956079Sbostic 	 * need two extra bytes, one for the newline, one for a terminating
56056079Sbostic 	 * NULL.
56155997Sbostic 	 */
56257572Selan 	tlen = sp->len + len + (spflag == APPENDNL ? 2 : 1);
56355997Sbostic 	if (tlen > sp->blen) {
56455997Sbostic 		sp->blen = tlen + 1024;
56556079Sbostic 		sp->space = sp->back = xrealloc(sp->back, sp->blen);
56655997Sbostic 	}
56755997Sbostic 
56856079Sbostic 	if (spflag == APPENDNL)
56955997Sbostic 		sp->space[sp->len++] = '\n';
57056079Sbostic 	else if (spflag == REPLACE)
57156079Sbostic 		sp->len = 0;
57255997Sbostic 
57356079Sbostic 	memmove(sp->space + sp->len, p, len);
574*57733Selan 
57556079Sbostic 	sp->space[sp->len += len] = '\0';
57655997Sbostic }
57755997Sbostic 
57855997Sbostic /*
57955997Sbostic  * Close all cached opened files and report any errors
58055997Sbostic  */
58155997Sbostic void
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