xref: /illumos-gate/usr/src/cmd/sed/process.c (revision f641a59486e69100969f92a7ec309574f76b238d)
1 /*
2  * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
3  * Copyright 2020 OmniOS Community Edition (OmniOSce) Association.
4  * Copyright (c) 1992 Diomidis Spinellis.
5  * Copyright (c) 1992, 1993, 1994
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Diomidis Spinellis of Imperial College, University of London.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <sys/ccompile.h>
39 
40 #include <ctype.h>
41 #include <err.h>
42 #include <errno.h>
43 #include <fcntl.h>
44 #include <limits.h>
45 #include <regex.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <unistd.h>
50 #include <wchar.h>
51 #include <wctype.h>
52 #include <termio.h>
53 #include <libintl.h>
54 #include <note.h>
55 
56 #include "defs.h"
57 #include "extern.h"
58 
59 static SPACE HS, PS, SS, YS;
60 #define	pd		PS.deleted
61 #define	ps		PS.space
62 #define	psl		PS.len
63 #define	hs		HS.space
64 #define	hsl		HS.len
65 
66 static int	applies(struct s_command *);
67 static void	do_tr(struct s_tr *);
68 static void	flush_appends(void);
69 static void	lputs(char *, size_t);
70 static int	 regexec_e(regex_t *, const char *, int, int, size_t);
71 static void	regsub(SPACE *, char *, char *);
72 static int	substitute(struct s_command *);
73 
74 struct s_appends *appends;	/* Array of pointers to strings to append. */
75 static int appendx;		/* Index into appends array. */
76 int appendnum;			/* Size of appends array. */
77 
78 static int lastaddr;		/* Set by applies if last address of a range. */
79 static int sdone;		/* If any substitutes since last line input. */
80 				/* Iov structure for 'w' commands. */
81 static regex_t *defpreg;
82 size_t maxnsub;
83 regmatch_t *match;
84 
85 #define	OUT() do {					\
86 	(void) fwrite(ps, 1, psl, outfile);		\
87 	(void) fputc('\n', outfile);			\
88 	_NOTE(CONSTCOND)				\
89 } while (0)
90 
91 void
92 process(void)
93 {
94 	struct s_command *cp;
95 	SPACE tspace;
96 	size_t oldpsl = 0;
97 	char *p;
98 
99 	p = NULL;
100 
101 	for (linenum = 0; mf_fgets(&PS, REPLACE); /* NOP */) {
102 		pd = 0;
103 top:
104 		cp = prog;
105 redirect:
106 		while (cp != NULL) {
107 			if (!applies(cp)) {
108 				cp = cp->next;
109 				continue;
110 			}
111 			switch (cp->code) {
112 			case '{':
113 				cp = cp->u.c;
114 				goto redirect;
115 			case 'a':
116 				if (appendx >= appendnum)
117 					if ((appends = realloc(appends,
118 					    sizeof (struct s_appends) *
119 					    (appendnum *= 2))) == NULL)
120 						err(1, "realloc");
121 				appends[appendx].type = AP_STRING;
122 				appends[appendx].s = cp->t;
123 				appends[appendx].len = strlen(cp->t);
124 				appendx++;
125 				break;
126 			case 'b':
127 				cp = cp->u.c;
128 				goto redirect;
129 			case 'c':
130 				pd = 1;
131 				psl = 0;
132 				if (cp->a2 == NULL || lastaddr || lastline())
133 					(void) fprintf(outfile, "%s", cp->t);
134 				break;
135 			case 'd':
136 				pd = 1;
137 				goto new;
138 			case 'D':
139 				if (pd)
140 					goto new;
141 				if (psl == 0 ||
142 				    (p = memchr(ps, '\n', psl)) == NULL) {
143 					pd = 1;
144 					goto new;
145 				} else {
146 					psl -=
147 					    (uintptr_t)(p + 1) - (uintptr_t)ps;
148 					(void) memmove(ps, p + 1, psl);
149 					goto top;
150 				}
151 			case 'g':
152 				cspace(&PS, hs, hsl, REPLACE);
153 				break;
154 			case 'G':
155 				cspace(&PS, "\n", 1, APPEND);
156 				cspace(&PS, hs, hsl, APPEND);
157 				break;
158 			case 'h':
159 				cspace(&HS, ps, psl, REPLACE);
160 				break;
161 			case 'H':
162 				cspace(&HS, "\n", 1, APPEND);
163 				cspace(&HS, ps, psl, APPEND);
164 				break;
165 			case 'i':
166 				(void) fprintf(outfile, "%s", cp->t);
167 				break;
168 			case 'l':
169 				lputs(ps, psl);
170 				break;
171 			case 'n':
172 				if (!nflag && !pd)
173 					OUT();
174 				flush_appends();
175 				if (!mf_fgets(&PS, REPLACE))
176 					exit(0);
177 				pd = 0;
178 				break;
179 			case 'N':
180 				flush_appends();
181 				cspace(&PS, "\n", 1, APPEND);
182 				if (!mf_fgets(&PS, APPEND))
183 					exit(0);
184 				break;
185 			case 'p':
186 				if (pd)
187 					break;
188 				OUT();
189 				break;
190 			case 'P':
191 				if (pd)
192 					break;
193 				if ((p = memchr(ps, '\n', psl)) != NULL) {
194 					oldpsl = psl;
195 					psl = (uintptr_t)p - (uintptr_t)ps;
196 				}
197 				OUT();
198 				if (p != NULL)
199 					psl = oldpsl;
200 				break;
201 			case 'q':
202 				if (!nflag && !pd)
203 					OUT();
204 				flush_appends();
205 				exit(0);
206 				/*NOTREACHED*/
207 			case 'r':
208 				if (appendx >= appendnum)
209 					if ((appends = realloc(appends,
210 					    sizeof (struct s_appends) *
211 					    (appendnum *= 2))) == NULL)
212 						err(1, "realloc");
213 				appends[appendx].type = AP_FILE;
214 				appends[appendx].s = cp->t;
215 				appends[appendx].len = strlen(cp->t);
216 				appendx++;
217 				break;
218 			case 's':
219 				sdone |= substitute(cp);
220 				break;
221 			case 't':
222 				if (sdone) {
223 					sdone = 0;
224 					cp = cp->u.c;
225 					goto redirect;
226 				}
227 				break;
228 			case 'w':
229 				if (pd)
230 					break;
231 				if (cp->u.fd == -1 && (cp->u.fd = open(cp->t,
232 				    O_WRONLY|O_APPEND|O_CREAT|O_TRUNC, 0666))
233 				    == -1)
234 					err(1, "%s", cp->t);
235 				if (write(cp->u.fd, ps, psl) != (ssize_t)psl ||
236 				    write(cp->u.fd, "\n", 1) != 1)
237 					err(1, "%s", cp->t);
238 				break;
239 			case 'x':
240 				/*
241 				 * If the hold space is null, make it empty
242 				 * but not null.  Otherwise the pattern space
243 				 * will become null after the swap, which is
244 				 * an abnormal condition.
245 				 */
246 				if (hs == NULL)
247 					cspace(&HS, "", 0, REPLACE);
248 				tspace = PS;
249 				PS = HS;
250 				HS = tspace;
251 				break;
252 			case 'y':
253 				if (pd || psl == 0)
254 					break;
255 				do_tr(cp->u.y);
256 				break;
257 			case ':':
258 			case '}':
259 				break;
260 			case '=':
261 				(void) fprintf(outfile, "%lu\n", linenum);
262 			}
263 			cp = cp->next;
264 		} /* for all cp */
265 
266 new:		if (!nflag && !pd)
267 			OUT();
268 		flush_appends();
269 	} /* for all lines */
270 }
271 
272 /*
273  * TRUE if the address passed matches the current program state
274  * (lastline, linenumber, ps).
275  */
276 static __GNU_INLINE int
277 MATCH(struct s_command *cp, struct s_addr *a)
278 {
279 	switch (a->type) {
280 	case AT_RE:
281 		return (regexec_e(a->u.r, ps, 0, 1, psl));
282 	case AT_LINE:
283 		return (linenum == a->u.l);
284 	case AT_RELLINE:
285 		return (linenum - cp->startline == a->u.l);
286 	case AT_LAST:
287 		return (lastline());
288 	}
289 	fatal(_("Unhandled match type"));
290 	return (0);
291 }
292 
293 /*
294  * Return TRUE if the command applies to the current line.  Sets the start
295  * line for process ranges.  Interprets the non-select (``!'') flag.
296  */
297 static int
298 applies(struct s_command *cp)
299 {
300 	int r;
301 
302 	lastaddr = 0;
303 	if (cp->a1 == NULL && cp->a2 == NULL)
304 		r = 1;
305 	else if (cp->a2)
306 		if (cp->startline > 0) {
307 			if (MATCH(cp, cp->a2)) {
308 				cp->startline = 0;
309 				lastaddr = 1;
310 				r = 1;
311 			} else if ((cp->a2->type == AT_LINE &&
312 			    linenum > cp->a2->u.l) ||
313 			    (cp->a2->type == AT_RELLINE &&
314 			    linenum - cp->startline > cp->a2->u.l)) {
315 				/*
316 				 * We missed the 2nd address due to a branch,
317 				 * so just close the range and return false.
318 				 */
319 				cp->startline = 0;
320 				r = 0;
321 			} else {
322 				r = 1;
323 			}
324 		} else if (MATCH(cp, cp->a1)) {
325 			/*
326 			 * If the second address is a number less than or
327 			 * equal to the line number first selected, only
328 			 * one line shall be selected.
329 			 *	-- POSIX 1003.2
330 			 * Likewise if the relative second line address is zero.
331 			 */
332 			if ((cp->a2->type == AT_LINE &&
333 			    linenum >= cp->a2->u.l) ||
334 			    (cp->a2->type == AT_RELLINE && cp->a2->u.l == 0))
335 				lastaddr = 1;
336 			else {
337 				cp->startline = linenum;
338 			}
339 			r = 1;
340 		} else
341 			r = 0;
342 	else
343 		r = MATCH(cp, cp->a1);
344 	return (cp->nonsel ? ! r : r);
345 }
346 
347 /*
348  * Reset the sed processor to its initial state.
349  */
350 void
351 resetstate(void)
352 {
353 	struct s_command *cp;
354 
355 	/*
356 	 * Reset all in-range markers.
357 	 */
358 	for (cp = prog; cp; cp = cp->code == '{' ? cp->u.c : cp->next)
359 		if (cp->a2)
360 			cp->startline = 0;
361 
362 	/*
363 	 * Clear out the hold space.
364 	 */
365 	cspace(&HS, "", 0, REPLACE);
366 }
367 
368 /*
369  * substitute --
370  *	Do substitutions in the pattern space.  Currently, we build a
371  *	copy of the new pattern space in the substitute space structure
372  *	and then swap them.
373  */
374 static int
375 substitute(struct s_command *cp)
376 {
377 	SPACE tspace;
378 	regex_t *re;
379 	regoff_t re_off, slen;
380 	int lastempty, n;
381 	char *s;
382 
383 	s = ps;
384 	re = cp->u.s->re;
385 	if (re == NULL) {
386 		if (defpreg != NULL && cp->u.s->maxbref > defpreg->re_nsub) {
387 			linenum = cp->u.s->linenum;
388 			fatal(_("\\%u not defined in the RE"),
389 			    cp->u.s->maxbref);
390 		}
391 	}
392 	if (!regexec_e(re, s, 0, 0, psl))
393 		return (0);
394 
395 	SS.len = 0;				/* Clean substitute space. */
396 	slen = psl;
397 	n = cp->u.s->n;
398 	lastempty = 1;
399 
400 	switch (n) {
401 	case 0:					/* Global */
402 		do {
403 			if (lastempty || match[0].rm_so != match[0].rm_eo) {
404 				/* Locate start of replaced string. */
405 				re_off = match[0].rm_so;
406 				/* Copy leading retained string. */
407 				cspace(&SS, s, re_off, APPEND);
408 				/* Add in regular expression. */
409 				regsub(&SS, s, cp->u.s->new);
410 			}
411 
412 			/* Move past this match. */
413 			if (match[0].rm_so != match[0].rm_eo) {
414 				s += match[0].rm_eo;
415 				slen -= match[0].rm_eo;
416 				lastempty = 0;
417 			} else {
418 				if (match[0].rm_so < slen)
419 					cspace(&SS, s + match[0].rm_so, 1,
420 					    APPEND);
421 				s += match[0].rm_so + 1;
422 				slen -= match[0].rm_so + 1;
423 				lastempty = 1;
424 			}
425 		} while (slen >= 0 && regexec_e(re, s, REG_NOTBOL, 0, slen));
426 		/* Copy trailing retained string. */
427 		if (slen > 0)
428 			cspace(&SS, s, slen, APPEND);
429 		break;
430 	default:				/* Nth occurrence */
431 		while (--n) {
432 			if (match[0].rm_eo == match[0].rm_so)
433 				match[0].rm_eo = match[0].rm_so + 1;
434 			s += match[0].rm_eo;
435 			slen -= match[0].rm_eo;
436 			if (slen < 0)
437 				return (0);
438 			if (!regexec_e(re, s, REG_NOTBOL, 0, slen))
439 				return (0);
440 		}
441 		/* FALLTHROUGH */
442 	case 1:					/* 1st occurrence */
443 		/* Locate start of replaced string. */
444 		re_off = match[0].rm_so + ((uintptr_t)s - (uintptr_t)ps);
445 		/* Copy leading retained string. */
446 		cspace(&SS, ps, re_off, APPEND);
447 		/* Add in regular expression. */
448 		regsub(&SS, s, cp->u.s->new);
449 		/* Copy trailing retained string. */
450 		s += match[0].rm_eo;
451 		slen -= match[0].rm_eo;
452 		cspace(&SS, s, slen, APPEND);
453 		break;
454 	}
455 
456 	/*
457 	 * Swap the substitute space and the pattern space, and make sure
458 	 * that any leftover pointers into stdio memory get lost.
459 	 */
460 	tspace = PS;
461 	PS = SS;
462 	SS = tspace;
463 	SS.space = SS.back;
464 
465 	/* Handle the 'p' flag. */
466 	if (cp->u.s->p)
467 		OUT();
468 
469 	/* Handle the 'w' flag. */
470 	if (cp->u.s->wfile && !pd) {
471 		if (cp->u.s->wfd == -1 && (cp->u.s->wfd = open(cp->u.s->wfile,
472 		    O_WRONLY|O_APPEND|O_CREAT|O_TRUNC, 0666)) == -1)
473 			err(1, "%s", cp->u.s->wfile);
474 		if (write(cp->u.s->wfd, ps, psl) != (ssize_t)psl ||
475 		    write(cp->u.s->wfd, "\n", 1) != 1)
476 			err(1, "%s", cp->u.s->wfile);
477 	}
478 	return (1);
479 }
480 
481 /*
482  * do_tr --
483  *	Perform translation ('y' command) in the pattern space.
484  */
485 static void
486 do_tr(struct s_tr *y)
487 {
488 	SPACE tmp;
489 	char c, *p;
490 	size_t clen, left;
491 	int i;
492 
493 	if (MB_CUR_MAX == 1) {
494 		/*
495 		 * Single-byte encoding: perform in-place translation
496 		 * of the pattern space.
497 		 */
498 		for (p = ps; p < &ps[psl]; p++)
499 			*p = y->bytetab[(uchar_t)*p];
500 	} else {
501 		/*
502 		 * Multi-byte encoding: perform translation into the
503 		 * translation space, then swap the translation and
504 		 * pattern spaces.
505 		 */
506 		/* Clean translation space. */
507 		YS.len = 0;
508 		for (p = ps, left = psl; left > 0; p += clen, left -= clen) {
509 			if ((c = y->bytetab[(uchar_t)*p]) != '\0') {
510 				cspace(&YS, &c, 1, APPEND);
511 				clen = 1;
512 				continue;
513 			}
514 			for (i = 0; i < y->nmultis; i++)
515 				if (left >= y->multis[i].fromlen &&
516 				    memcmp(p, y->multis[i].from,
517 				    y->multis[i].fromlen) == 0)
518 					break;
519 			if (i < y->nmultis) {
520 				cspace(&YS, y->multis[i].to,
521 				    y->multis[i].tolen, APPEND);
522 				clen = y->multis[i].fromlen;
523 			} else {
524 				cspace(&YS, p, 1, APPEND);
525 				clen = 1;
526 			}
527 		}
528 		/* Swap the translation space and the pattern space. */
529 		tmp = PS;
530 		PS = YS;
531 		YS = tmp;
532 		YS.space = YS.back;
533 	}
534 }
535 
536 /*
537  * Flush append requests.  Always called before reading a line,
538  * therefore it also resets the substitution done (sdone) flag.
539  */
540 static void
541 flush_appends(void)
542 {
543 	FILE *f;
544 	int count, i;
545 	char buf[8 * 1024];
546 
547 	for (i = 0; i < appendx; i++)
548 		switch (appends[i].type) {
549 		case AP_STRING:
550 			(void) fwrite(appends[i].s, sizeof (char),
551 			    appends[i].len, outfile);
552 			break;
553 		case AP_FILE:
554 			/*
555 			 * Read files probably shouldn't be cached.  Since
556 			 * it's not an error to read a non-existent file,
557 			 * it's possible that another program is interacting
558 			 * with the sed script through the filesystem.  It
559 			 * would be truly bizarre, but possible.  It's probably
560 			 * not that big a performance win, anyhow.
561 			 */
562 			if ((f = fopen(appends[i].s, "r")) == NULL)
563 				break;
564 			while ((count =
565 			    fread(buf, sizeof (char), sizeof (buf), f)))
566 				(void) fwrite(buf, sizeof (char), count,
567 				    outfile);
568 			(void) fclose(f);
569 			break;
570 		}
571 	if (ferror(outfile))
572 		errx(1, "%s: %s", outfname, strerror(errno ? errno : EIO));
573 	appendx = sdone = 0;
574 }
575 
576 static void
577 lputs(char *s, size_t len)
578 {
579 	static const char escapes[] = "\\\a\b\f\r\t\v";
580 	int c, col, width;
581 	const char *p;
582 	struct winsize win;
583 	static int termwidth = -1;
584 	size_t clen, i;
585 	wchar_t wc;
586 	mbstate_t mbs;
587 
588 	if (outfile != stdout)
589 		termwidth = 60;
590 	if (termwidth == -1) {
591 		if (((p = getenv("COLUMNS")) != NULL) && (*p != '\0'))
592 			termwidth = atoi(p);
593 		else if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) == 0 &&
594 		    win.ws_col > 0)
595 			termwidth = win.ws_col;
596 		else
597 			termwidth = 60;
598 	}
599 	if (termwidth <= 0)
600 		termwidth = 1;
601 
602 	(void) memset(&mbs, 0, sizeof (mbs));
603 	col = 0;
604 	while (len != 0) {
605 		clen = mbrtowc(&wc, s, len, &mbs);
606 		if (clen == 0)
607 			clen = 1;
608 		if (clen == (size_t)-1 || clen == (size_t)-2) {
609 			wc = (unsigned char)*s;
610 			clen = 1;
611 			(void) memset(&mbs, 0, sizeof (mbs));
612 		}
613 		if (wc == '\n') {
614 			if (col + 1 >= termwidth)
615 				(void) fprintf(outfile, "\\\n");
616 			(void) fputc('$', outfile);
617 			(void) fputc('\n', outfile);
618 			col = 0;
619 		} else if (iswprint(wc)) {
620 			width = wcwidth(wc);
621 			if (col + width >= termwidth) {
622 				(void) fprintf(outfile, "\\\n");
623 				col = 0;
624 			}
625 			(void) fwrite(s, 1, clen, outfile);
626 			col += width;
627 		} else if (wc != L'\0' && (c = wctob(wc)) != EOF &&
628 		    (p = strchr(escapes, c)) != NULL) {
629 			if (col + 2 >= termwidth) {
630 				(void) fprintf(outfile, "\\\n");
631 				col = 0;
632 			}
633 			(void) fprintf(outfile, "\\%c",
634 			    "\\abfrtv"[(uintptr_t)p - (uintptr_t)escapes]);
635 			col += 2;
636 		} else {
637 			if (col + 4 * clen >= (unsigned)termwidth) {
638 				(void) fprintf(outfile, "\\\n");
639 				col = 0;
640 			}
641 			for (i = 0; i < clen; i++)
642 				(void) fprintf(outfile, "\\%03o",
643 				    (int)(unsigned char)s[i]);
644 			col += 4 * clen;
645 		}
646 		s += clen;
647 		len -= clen;
648 	}
649 	if (col + 1 >= termwidth)
650 		(void) fprintf(outfile, "\\\n");
651 	(void) fputc('$', outfile);
652 	(void) fputc('\n', outfile);
653 	if (ferror(outfile))
654 		errx(1, "%s: %s", outfname, strerror(errno ? errno : EIO));
655 }
656 
657 static int
658 regexec_e(regex_t *preg, const char *string, int eflags, int nomatch,
659     size_t slen)
660 {
661 	int eval;
662 
663 	if (preg == NULL) {
664 		if (defpreg == NULL)
665 			fatal(_("first RE may not be empty"));
666 	} else
667 		defpreg = preg;
668 
669 	/* Set anchors */
670 	match[0].rm_so = 0;
671 	match[0].rm_eo = slen;
672 
673 	eval = regexec(defpreg, string,
674 	    nomatch ? 0 : maxnsub + 1, match, eflags | REG_STARTEND);
675 	switch (eval) {
676 	case 0:
677 		return (1);
678 	case REG_NOMATCH:
679 		return (0);
680 	}
681 	fatal(_("RE error: %s"), strregerror(eval, defpreg));
682 	return (0);
683 }
684 
685 /*
686  * regsub - perform substitutions after a regexp match
687  * Based on a routine by Henry Spencer
688  */
689 static void
690 regsub(SPACE *sp, char *string, char *src)
691 {
692 	int len, no;
693 	char c, *dst;
694 
695 #define	NEEDSP(reqlen)							\
696 	/* XXX What is the +1 for? */					\
697 	if (sp->len + (reqlen) + 1 >= sp->blen) {			\
698 		sp->blen += (reqlen) + 1024;				\
699 		if ((sp->back = realloc(sp->back, sp->blen)) == NULL)	\
700 			err(1, "realloc");				\
701 		sp->space = sp->back;					\
702 		dst = sp->space + sp->len;				\
703 	}
704 
705 	dst = sp->space + sp->len;
706 	while ((c = *src++) != '\0') {
707 		if (c == '&')
708 			no = 0;
709 		else if (c == '\\' && isdigit((unsigned char)*src))
710 			no = *src++ - '0';
711 		else
712 			no = -1;
713 		if (no < 0) {		/* Ordinary character. */
714 			if (c == '\\' && (*src == '\\' || *src == '&'))
715 				c = *src++;
716 			NEEDSP(1);
717 			*dst++ = c;
718 			++sp->len;
719 		} else if (match[no].rm_so != -1 && match[no].rm_eo != -1) {
720 			len = match[no].rm_eo - match[no].rm_so;
721 			NEEDSP(len);
722 			(void) memmove(dst, string + match[no].rm_so, len);
723 			dst += len;
724 			sp->len += len;
725 		}
726 	}
727 	NEEDSP(1);
728 	*dst = '\0';
729 }
730 
731 /*
732  * cspace --
733  *	Concatenate space: append the source space to the destination space,
734  *	allocating new space as necessary.
735  */
736 void
737 cspace(SPACE *sp, const char *p, size_t len, enum e_spflag spflag)
738 {
739 	size_t tlen;
740 
741 	/* Make sure SPACE has enough memory and ramp up quickly. */
742 	tlen = sp->len + len + 1;
743 	if (tlen > sp->blen) {
744 		sp->blen = tlen + 1024;
745 		if ((sp->space = sp->back = realloc(sp->back, sp->blen)) ==
746 		    NULL)
747 			err(1, "realloc");
748 	}
749 
750 	if (spflag == REPLACE)
751 		sp->len = 0;
752 
753 	(void) memmove(sp->space + sp->len, p, len);
754 
755 	sp->space[sp->len += len] = '\0';
756 }
757 
758 /*
759  * Close all cached opened files and report any errors
760  */
761 void
762 cfclose(struct s_command *cp, struct s_command *end)
763 {
764 
765 	for (; cp != end; cp = cp->next)
766 		switch (cp->code) {
767 		case 's':
768 			if (cp->u.s->wfd != -1 && close(cp->u.s->wfd))
769 				err(1, "%s", cp->u.s->wfile);
770 			cp->u.s->wfd = -1;
771 			break;
772 		case 'w':
773 			if (cp->u.fd != -1 && close(cp->u.fd))
774 				err(1, "%s", cp->t);
775 			cp->u.fd = -1;
776 			break;
777 		case '{':
778 			cfclose(cp->u.c, cp->next);
779 			break;
780 		}
781 }
782