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