Home | History | Annotate | Line # | Download | only in sed
process.c revision 1.25.4.1
      1  1.25.4.1  wrstuden /*	$NetBSD: process.c,v 1.25.4.1 1999/12/27 18:37:10 wrstuden Exp $	*/
      2      1.18       tls 
      3       1.1       alm /*-
      4       1.1       alm  * Copyright (c) 1992 Diomidis Spinellis.
      5      1.19       mrg  * Copyright (c) 1992, 1993, 1994
      6       1.8       cgd  *	The Regents of the University of California.  All rights reserved.
      7       1.1       alm  *
      8       1.1       alm  * This code is derived from software contributed to Berkeley by
      9       1.1       alm  * Diomidis Spinellis of Imperial College, University of London.
     10       1.1       alm  *
     11       1.1       alm  * Redistribution and use in source and binary forms, with or without
     12       1.1       alm  * modification, are permitted provided that the following conditions
     13       1.1       alm  * are met:
     14       1.1       alm  * 1. Redistributions of source code must retain the above copyright
     15       1.1       alm  *    notice, this list of conditions and the following disclaimer.
     16       1.1       alm  * 2. Redistributions in binary form must reproduce the above copyright
     17       1.1       alm  *    notice, this list of conditions and the following disclaimer in the
     18       1.1       alm  *    documentation and/or other materials provided with the distribution.
     19       1.1       alm  * 3. All advertising materials mentioning features or use of this software
     20       1.1       alm  *    must display the following acknowledgement:
     21       1.1       alm  *	This product includes software developed by the University of
     22       1.1       alm  *	California, Berkeley and its contributors.
     23       1.1       alm  * 4. Neither the name of the University nor the names of its contributors
     24       1.1       alm  *    may be used to endorse or promote products derived from this software
     25       1.1       alm  *    without specific prior written permission.
     26       1.1       alm  *
     27       1.1       alm  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     28       1.1       alm  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     29       1.1       alm  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     30       1.1       alm  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     31       1.1       alm  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     32       1.1       alm  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     33       1.1       alm  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     34       1.1       alm  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     35       1.1       alm  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     36       1.1       alm  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     37       1.1       alm  * SUCH DAMAGE.
     38       1.1       alm  */
     39       1.1       alm 
     40      1.20     lukem #include <sys/cdefs.h>
     41       1.1       alm #ifndef lint
     42      1.19       mrg #if 0
     43      1.19       mrg static char sccsid[] = "@(#)process.c	8.6 (Berkeley) 4/20/94";
     44      1.19       mrg #else
     45  1.25.4.1  wrstuden __RCSID("$NetBSD: process.c,v 1.25.4.1 1999/12/27 18:37:10 wrstuden Exp $");
     46      1.19       mrg #endif
     47       1.1       alm #endif /* not lint */
     48       1.1       alm 
     49       1.1       alm #include <sys/types.h>
     50       1.1       alm #include <sys/stat.h>
     51       1.1       alm #include <sys/ioctl.h>
     52       1.1       alm #include <sys/uio.h>
     53       1.1       alm 
     54       1.1       alm #include <ctype.h>
     55       1.1       alm #include <errno.h>
     56       1.1       alm #include <fcntl.h>
     57       1.1       alm #include <limits.h>
     58       1.1       alm #include <regex.h>
     59       1.1       alm #include <stdio.h>
     60       1.1       alm #include <stdlib.h>
     61       1.1       alm #include <string.h>
     62       1.1       alm #include <unistd.h>
     63       1.1       alm 
     64       1.1       alm #include "defs.h"
     65       1.1       alm #include "extern.h"
     66       1.1       alm 
     67       1.8       cgd static SPACE HS, PS, SS;
     68       1.1       alm #define	pd		PS.deleted
     69       1.1       alm #define	ps		PS.space
     70       1.1       alm #define	psl		PS.len
     71       1.1       alm #define	hs		HS.space
     72       1.1       alm #define	hsl		HS.len
     73       1.1       alm 
     74       1.1       alm static inline int	 applies __P((struct s_command *));
     75       1.1       alm static void		 flush_appends __P((void));
     76       1.1       alm static void		 lputs __P((char *));
     77       1.8       cgd static inline int	 regexec_e __P((regex_t *, const char *, int, int, size_t));
     78       1.1       alm static void		 regsub __P((SPACE *, char *, char *));
     79       1.1       alm static int		 substitute __P((struct s_command *));
     80       1.1       alm 
     81       1.1       alm struct s_appends *appends;	/* Array of pointers to strings to append. */
     82       1.1       alm static int appendx;		/* Index into appends array. */
     83       1.1       alm int appendnum;			/* Size of appends array. */
     84       1.1       alm 
     85       1.1       alm static int lastaddr;		/* Set by applies if last address of a range. */
     86       1.1       alm static int sdone;		/* If any substitutes since last line input. */
     87       1.1       alm 				/* Iov structure for 'w' commands. */
     88       1.1       alm static regex_t *defpreg;
     89       1.1       alm size_t maxnsub;
     90       1.1       alm regmatch_t *match;
     91       1.1       alm 
     92       1.8       cgd #define OUT(s) { fwrite(s, sizeof(u_char), psl, stdout); }
     93       1.8       cgd 
     94       1.1       alm void
     95       1.1       alm process()
     96       1.1       alm {
     97       1.1       alm 	struct s_command *cp;
     98       1.1       alm 	SPACE tspace;
     99      1.15   mycroft 	size_t len, oldpsl;
    100      1.15   mycroft 	char *p;
    101       1.1       alm 
    102      1.20     lukem 	oldpsl = 0;
    103       1.1       alm 	for (linenum = 0; mf_fgets(&PS, REPLACE);) {
    104       1.1       alm 		pd = 0;
    105      1.16   mycroft top:
    106       1.1       alm 		cp = prog;
    107       1.1       alm redirect:
    108       1.1       alm 		while (cp != NULL) {
    109       1.1       alm 			if (!applies(cp)) {
    110       1.1       alm 				cp = cp->next;
    111       1.1       alm 				continue;
    112       1.1       alm 			}
    113       1.1       alm 			switch (cp->code) {
    114       1.1       alm 			case '{':
    115       1.1       alm 				cp = cp->u.c;
    116       1.1       alm 				goto redirect;
    117       1.1       alm 			case 'a':
    118       1.1       alm 				if (appendx >= appendnum)
    119       1.1       alm 					appends = xrealloc(appends,
    120       1.1       alm 					    sizeof(struct s_appends) *
    121       1.1       alm 					    (appendnum *= 2));
    122       1.1       alm 				appends[appendx].type = AP_STRING;
    123       1.1       alm 				appends[appendx].s = cp->t;
    124       1.8       cgd 				appends[appendx].len = strlen(cp->t);
    125       1.1       alm 				appendx++;
    126       1.1       alm 				break;
    127       1.1       alm 			case 'b':
    128       1.1       alm 				cp = cp->u.c;
    129       1.1       alm 				goto redirect;
    130       1.1       alm 			case 'c':
    131       1.1       alm 				pd = 1;
    132       1.1       alm 				psl = 0;
    133       1.1       alm 				if (cp->a2 == NULL || lastaddr)
    134       1.1       alm 					(void)printf("%s", cp->t);
    135       1.1       alm 				break;
    136       1.1       alm 			case 'd':
    137       1.1       alm 				pd = 1;
    138       1.1       alm 				goto new;
    139       1.1       alm 			case 'D':
    140       1.1       alm 				if (pd)
    141       1.1       alm 					goto new;
    142      1.16   mycroft 				if ((p = memchr(ps, '\n', psl - 1)) == NULL) {
    143       1.1       alm 					pd = 1;
    144      1.16   mycroft 					goto new;
    145      1.16   mycroft 				} else {
    146      1.13   mycroft 					psl -= (p + 1) - ps;
    147       1.1       alm 					memmove(ps, p + 1, psl);
    148      1.16   mycroft 					goto top;
    149       1.1       alm 				}
    150       1.1       alm 			case 'g':
    151       1.1       alm 				cspace(&PS, hs, hsl, REPLACE);
    152       1.1       alm 				break;
    153       1.1       alm 			case 'G':
    154      1.25       kim 				if (hs == NULL)
    155      1.25       kim 					cspace(&HS, "\n", 1, REPLACE);
    156       1.8       cgd 				cspace(&PS, hs, hsl, 0);
    157       1.1       alm 				break;
    158       1.1       alm 			case 'h':
    159       1.1       alm 				cspace(&HS, ps, psl, REPLACE);
    160       1.1       alm 				break;
    161       1.1       alm 			case 'H':
    162       1.8       cgd 				cspace(&HS, ps, psl, 0);
    163       1.1       alm 				break;
    164       1.1       alm 			case 'i':
    165       1.1       alm 				(void)printf("%s", cp->t);
    166       1.1       alm 				break;
    167       1.1       alm 			case 'l':
    168       1.1       alm 				lputs(ps);
    169       1.1       alm 				break;
    170       1.1       alm 			case 'n':
    171       1.1       alm 				if (!nflag && !pd)
    172       1.8       cgd 					OUT(ps)
    173       1.1       alm 				flush_appends();
    174      1.14   mycroft 				if (!mf_fgets(&PS, REPLACE))
    175       1.1       alm 					exit(0);
    176       1.1       alm 				pd = 0;
    177       1.1       alm 				break;
    178       1.1       alm 			case 'N':
    179       1.1       alm 				flush_appends();
    180       1.8       cgd 				if (!mf_fgets(&PS, 0)) {
    181       1.1       alm 					if (!nflag && !pd)
    182       1.8       cgd 						OUT(ps)
    183       1.1       alm 					exit(0);
    184       1.1       alm 				}
    185       1.1       alm 				break;
    186       1.1       alm 			case 'p':
    187       1.1       alm 				if (pd)
    188       1.1       alm 					break;
    189       1.8       cgd 				OUT(ps)
    190       1.1       alm 				break;
    191       1.1       alm 			case 'P':
    192       1.1       alm 				if (pd)
    193       1.1       alm 					break;
    194      1.15   mycroft 				if ((p = memchr(ps, '\n', psl - 1)) != NULL) {
    195      1.15   mycroft 					oldpsl = psl;
    196      1.15   mycroft 					psl = (p + 1) - ps;
    197       1.1       alm 				}
    198       1.8       cgd 				OUT(ps)
    199       1.1       alm 				if (p != NULL)
    200      1.15   mycroft 					psl = oldpsl;
    201       1.1       alm 				break;
    202       1.1       alm 			case 'q':
    203       1.1       alm 				if (!nflag && !pd)
    204       1.8       cgd 					OUT(ps)
    205       1.1       alm 				flush_appends();
    206       1.1       alm 				exit(0);
    207       1.1       alm 			case 'r':
    208       1.1       alm 				if (appendx >= appendnum)
    209       1.1       alm 					appends = xrealloc(appends,
    210       1.1       alm 					    sizeof(struct s_appends) *
    211       1.1       alm 					    (appendnum *= 2));
    212       1.1       alm 				appends[appendx].type = AP_FILE;
    213       1.1       alm 				appends[appendx].s = cp->t;
    214       1.8       cgd 				appends[appendx].len = strlen(cp->t);
    215       1.1       alm 				appendx++;
    216       1.1       alm 				break;
    217       1.1       alm 			case 's':
    218       1.1       alm 				sdone |= substitute(cp);
    219       1.1       alm 				break;
    220       1.1       alm 			case 't':
    221       1.1       alm 				if (sdone) {
    222       1.1       alm 					sdone = 0;
    223       1.1       alm 					cp = cp->u.c;
    224       1.1       alm 					goto redirect;
    225       1.1       alm 				}
    226       1.1       alm 				break;
    227       1.1       alm 			case 'w':
    228       1.1       alm 				if (pd)
    229       1.1       alm 					break;
    230       1.1       alm 				if (cp->u.fd == -1 && (cp->u.fd = open(cp->t,
    231       1.1       alm 				    O_WRONLY|O_APPEND|O_CREAT|O_TRUNC,
    232       1.1       alm 				    DEFFILEMODE)) == -1)
    233       1.1       alm 					err(FATAL, "%s: %s\n",
    234       1.1       alm 					    cp->t, strerror(errno));
    235       1.8       cgd 				if (write(cp->u.fd, ps, psl) != psl)
    236       1.1       alm 					err(FATAL, "%s: %s\n",
    237       1.1       alm 					    cp->t, strerror(errno));
    238       1.1       alm 				break;
    239       1.1       alm 			case 'x':
    240       1.8       cgd 				if (hs == NULL)
    241      1.17       cgd 					cspace(&HS, "\n", 1, REPLACE);
    242       1.1       alm 				tspace = PS;
    243       1.1       alm 				PS = HS;
    244       1.1       alm 				HS = tspace;
    245       1.1       alm 				break;
    246       1.1       alm 			case 'y':
    247       1.1       alm 				if (pd)
    248       1.1       alm 					break;
    249       1.8       cgd 				for (p = ps, len = psl; --len; ++p)
    250      1.22       mrg 					*p = cp->u.y[(int)*p];
    251       1.1       alm 				break;
    252       1.1       alm 			case ':':
    253       1.1       alm 			case '}':
    254       1.1       alm 				break;
    255       1.1       alm 			case '=':
    256       1.1       alm 				(void)printf("%lu\n", linenum);
    257       1.1       alm 			}
    258       1.1       alm 			cp = cp->next;
    259       1.1       alm 		} /* for all cp */
    260       1.1       alm 
    261       1.1       alm new:		if (!nflag && !pd)
    262       1.8       cgd 			OUT(ps)
    263       1.1       alm 		flush_appends();
    264       1.1       alm 	} /* for all lines */
    265       1.1       alm }
    266       1.1       alm 
    267       1.1       alm /*
    268       1.1       alm  * TRUE if the address passed matches the current program state
    269       1.1       alm  * (lastline, linenumber, ps).
    270       1.1       alm  */
    271       1.8       cgd #define	MATCH(a)						\
    272       1.8       cgd 	(a)->type == AT_RE ? regexec_e((a)->u.r, ps, 0, 1, psl) :	\
    273       1.1       alm 	    (a)->type == AT_LINE ? linenum == (a)->u.l : lastline
    274       1.1       alm 
    275       1.1       alm /*
    276       1.1       alm  * Return TRUE if the command applies to the current line.  Sets the inrange
    277       1.1       alm  * flag to process ranges.  Interprets the non-select (``!'') flag.
    278       1.1       alm  */
    279       1.1       alm static inline int
    280       1.1       alm applies(cp)
    281       1.1       alm 	struct s_command *cp;
    282       1.1       alm {
    283       1.1       alm 	int r;
    284       1.1       alm 
    285       1.1       alm 	lastaddr = 0;
    286       1.1       alm 	if (cp->a1 == NULL && cp->a2 == NULL)
    287       1.1       alm 		r = 1;
    288      1.24  christos 	else if (cp->a2) {
    289       1.1       alm 		if (cp->inrange) {
    290       1.1       alm 			if (MATCH(cp->a2)) {
    291       1.1       alm 				cp->inrange = 0;
    292       1.1       alm 				lastaddr = 1;
    293       1.1       alm 			}
    294       1.1       alm 			r = 1;
    295       1.1       alm 		} else if (MATCH(cp->a1)) {
    296       1.1       alm 			/*
    297       1.1       alm 			 * If the second address is a number less than or
    298       1.1       alm 			 * equal to the line number first selected, only
    299       1.1       alm 			 * one line shall be selected.
    300       1.1       alm 			 *	-- POSIX 1003.2
    301       1.1       alm 			 */
    302       1.1       alm 			if (cp->a2->type == AT_LINE &&
    303       1.1       alm 			    linenum >= cp->a2->u.l)
    304       1.1       alm 				lastaddr = 1;
    305       1.1       alm 			else
    306       1.1       alm 				cp->inrange = 1;
    307       1.1       alm 			r = 1;
    308       1.1       alm 		} else
    309       1.1       alm 			r = 0;
    310      1.24  christos 	} else
    311       1.1       alm 		r = MATCH(cp->a1);
    312       1.1       alm 	return (cp->nonsel ? ! r : r);
    313       1.1       alm }
    314       1.1       alm 
    315       1.1       alm /*
    316       1.1       alm  * substitute --
    317       1.1       alm  *	Do substitutions in the pattern space.  Currently, we build a
    318       1.1       alm  *	copy of the new pattern space in the substitute space structure
    319       1.1       alm  *	and then swap them.
    320       1.1       alm  */
    321       1.1       alm static int
    322       1.1       alm substitute(cp)
    323       1.1       alm 	struct s_command *cp;
    324       1.1       alm {
    325       1.1       alm 	SPACE tspace;
    326       1.1       alm 	regex_t *re;
    327       1.8       cgd 	size_t re_off, slen;
    328      1.19       mrg 	int lastempty, n;
    329       1.1       alm 	char *s;
    330       1.1       alm 
    331       1.1       alm 	s = ps;
    332       1.1       alm 	re = cp->u.s->re;
    333       1.1       alm 	if (re == NULL) {
    334       1.1       alm 		if (defpreg != NULL && cp->u.s->maxbref > defpreg->re_nsub) {
    335       1.1       alm 			linenum = cp->u.s->linenum;
    336       1.1       alm 			err(COMPILE, "\\%d not defined in the RE",
    337       1.1       alm 			    cp->u.s->maxbref);
    338       1.1       alm 		}
    339       1.1       alm 	}
    340       1.8       cgd 	if (!regexec_e(re, s, 0, 0, psl))
    341       1.1       alm 		return (0);
    342       1.1       alm 
    343       1.1       alm 	SS.len = 0;				/* Clean substitute space. */
    344       1.8       cgd 	slen = psl;
    345       1.1       alm 	n = cp->u.s->n;
    346      1.12       cgd 	lastempty = 1;
    347      1.12       cgd 
    348       1.1       alm 	switch (n) {
    349       1.1       alm 	case 0:					/* Global */
    350       1.1       alm 		do {
    351      1.12       cgd 			if (lastempty || match[0].rm_so != match[0].rm_eo) {
    352      1.12       cgd 				/* Locate start of replaced string. */
    353      1.12       cgd 				re_off = match[0].rm_so;
    354      1.12       cgd 				/* Copy leading retained string. */
    355      1.12       cgd 				cspace(&SS, s, re_off, APPEND);
    356      1.12       cgd 				/* Add in regular expression. */
    357      1.12       cgd 				regsub(&SS, s, cp->u.s->new);
    358      1.12       cgd 			}
    359      1.12       cgd 
    360       1.1       alm 			/* Move past this match. */
    361      1.12       cgd 			if (match[0].rm_so != match[0].rm_eo) {
    362      1.12       cgd 				s += match[0].rm_eo;
    363      1.12       cgd 				slen -= match[0].rm_eo;
    364      1.12       cgd 				lastempty = 0;
    365      1.12       cgd 			} else {
    366      1.12       cgd 				if (match[0].rm_so == 0)
    367      1.19       mrg 					cspace(&SS,
    368      1.19       mrg 					    s, match[0].rm_so + 1, APPEND);
    369      1.12       cgd 				else
    370      1.19       mrg 					cspace(&SS,
    371      1.19       mrg 					    s + match[0].rm_so, 1, APPEND);
    372      1.12       cgd 				s += match[0].rm_so + 1;
    373      1.12       cgd 				slen -= match[0].rm_so + 1;
    374      1.12       cgd 				lastempty = 1;
    375      1.12       cgd 			}
    376      1.12       cgd 		} while (slen > 0 && regexec_e(re, s, REG_NOTBOL, 0, slen));
    377       1.1       alm 		/* Copy trailing retained string. */
    378      1.12       cgd 		if (slen > 0)
    379      1.12       cgd 			cspace(&SS, s, slen, APPEND);
    380       1.1       alm 		break;
    381       1.1       alm 	default:				/* Nth occurrence */
    382       1.1       alm 		while (--n) {
    383       1.1       alm 			s += match[0].rm_eo;
    384       1.8       cgd 			slen -= match[0].rm_eo;
    385       1.8       cgd 			if (!regexec_e(re, s, REG_NOTBOL, 0, slen))
    386       1.1       alm 				return (0);
    387       1.1       alm 		}
    388       1.1       alm 		/* FALLTHROUGH */
    389       1.1       alm 	case 1:					/* 1st occurrence */
    390       1.1       alm 		/* Locate start of replaced string. */
    391       1.1       alm 		re_off = match[0].rm_so + (s - ps);
    392       1.1       alm 		/* Copy leading retained string. */
    393       1.1       alm 		cspace(&SS, ps, re_off, APPEND);
    394       1.1       alm 		/* Add in regular expression. */
    395       1.1       alm 		regsub(&SS, s, cp->u.s->new);
    396       1.1       alm 		/* Copy trailing retained string. */
    397       1.1       alm 		s += match[0].rm_eo;
    398       1.8       cgd 		slen -= match[0].rm_eo;
    399       1.8       cgd 		cspace(&SS, s, slen, APPEND);
    400       1.1       alm 		break;
    401       1.1       alm 	}
    402       1.1       alm 
    403       1.1       alm 	/*
    404       1.1       alm 	 * Swap the substitute space and the pattern space, and make sure
    405       1.1       alm 	 * that any leftover pointers into stdio memory get lost.
    406       1.1       alm 	 */
    407       1.1       alm 	tspace = PS;
    408       1.1       alm 	PS = SS;
    409       1.1       alm 	SS = tspace;
    410       1.1       alm 	SS.space = SS.back;
    411       1.1       alm 
    412       1.1       alm 	/* Handle the 'p' flag. */
    413       1.1       alm 	if (cp->u.s->p)
    414       1.8       cgd 		OUT(ps)
    415       1.1       alm 
    416       1.1       alm 	/* Handle the 'w' flag. */
    417       1.1       alm 	if (cp->u.s->wfile && !pd) {
    418       1.1       alm 		if (cp->u.s->wfd == -1 && (cp->u.s->wfd = open(cp->u.s->wfile,
    419       1.1       alm 		    O_WRONLY|O_APPEND|O_CREAT|O_TRUNC, DEFFILEMODE)) == -1)
    420       1.1       alm 			err(FATAL, "%s: %s\n", cp->u.s->wfile, strerror(errno));
    421       1.8       cgd 		if (write(cp->u.s->wfd, ps, psl) != psl)
    422       1.1       alm 			err(FATAL, "%s: %s\n", cp->u.s->wfile, strerror(errno));
    423       1.1       alm 	}
    424       1.1       alm 	return (1);
    425       1.1       alm }
    426       1.1       alm 
    427       1.1       alm /*
    428       1.1       alm  * Flush append requests.  Always called before reading a line,
    429       1.1       alm  * therefore it also resets the substitution done (sdone) flag.
    430       1.1       alm  */
    431       1.1       alm static void
    432       1.1       alm flush_appends()
    433       1.1       alm {
    434       1.1       alm 	FILE *f;
    435       1.1       alm 	int count, i;
    436       1.1       alm 	char buf[8 * 1024];
    437       1.1       alm 
    438       1.1       alm 	for (i = 0; i < appendx; i++)
    439       1.1       alm 		switch (appends[i].type) {
    440       1.1       alm 		case AP_STRING:
    441       1.8       cgd 			fwrite(appends[i].s, sizeof(char), appends[i].len,
    442       1.8       cgd 			    stdout);
    443       1.1       alm 			break;
    444       1.1       alm 		case AP_FILE:
    445       1.1       alm 			/*
    446       1.1       alm 			 * Read files probably shouldn't be cached.  Since
    447       1.1       alm 			 * it's not an error to read a non-existent file,
    448       1.1       alm 			 * it's possible that another program is interacting
    449       1.1       alm 			 * with the sed script through the file system.  It
    450       1.1       alm 			 * would be truly bizarre, but possible.  It's probably
    451       1.1       alm 			 * not that big a performance win, anyhow.
    452       1.1       alm 			 */
    453       1.1       alm 			if ((f = fopen(appends[i].s, "r")) == NULL)
    454       1.1       alm 				break;
    455      1.20     lukem 			while ((count =
    456      1.21   thorpej 			    fread(buf, sizeof(char), sizeof(buf), f)) > 0)
    457       1.8       cgd 				(void)fwrite(buf, sizeof(char), count, stdout);
    458       1.1       alm 			(void)fclose(f);
    459       1.1       alm 			break;
    460       1.1       alm 		}
    461       1.1       alm 	if (ferror(stdout))
    462       1.1       alm 		err(FATAL, "stdout: %s", strerror(errno ? errno : EIO));
    463       1.1       alm 	appendx = sdone = 0;
    464       1.1       alm }
    465       1.1       alm 
    466       1.1       alm static void
    467       1.1       alm lputs(s)
    468      1.20     lukem 	char *s;
    469       1.1       alm {
    470      1.20     lukem 	int count;
    471      1.20     lukem 	char *escapes, *p;
    472       1.1       alm 	struct winsize win;
    473       1.1       alm 	static int termwidth = -1;
    474       1.1       alm 
    475      1.23      ross 	if (termwidth == -1) {
    476      1.20     lukem 		if ((p = getenv("COLUMNS")) != NULL)
    477       1.1       alm 			termwidth = atoi(p);
    478       1.1       alm 		else if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) == 0 &&
    479       1.1       alm 		    win.ws_col > 0)
    480       1.1       alm 			termwidth = win.ws_col;
    481       1.1       alm 		else
    482       1.1       alm 			termwidth = 60;
    483      1.23      ross 	}
    484       1.1       alm 	for (count = 0; *s; ++s) {
    485       1.1       alm 		if (count >= termwidth) {
    486       1.1       alm 			(void)printf("\\\n");
    487       1.1       alm 			count = 0;
    488       1.1       alm 		}
    489      1.24  christos 		if (isascii((unsigned char)*s) && isprint((unsigned char)*s) &&
    490      1.24  christos 		    *s != '\\') {
    491       1.1       alm 			(void)putchar(*s);
    492       1.1       alm 			count++;
    493       1.1       alm 		} else {
    494       1.1       alm 			escapes = "\\\a\b\f\n\r\t\v";
    495       1.1       alm 			(void)putchar('\\');
    496      1.20     lukem 			if ((p = strchr(escapes, *s)) != NULL) {
    497       1.1       alm 				(void)putchar("\\abfnrtv"[p - escapes]);
    498       1.1       alm 				count += 2;
    499       1.1       alm 			} else {
    500       1.8       cgd 				(void)printf("%03o", *(u_char *)s);
    501       1.1       alm 				count += 4;
    502       1.1       alm 			}
    503       1.1       alm 		}
    504       1.1       alm 	}
    505       1.1       alm 	(void)putchar('$');
    506       1.1       alm 	(void)putchar('\n');
    507       1.1       alm 	if (ferror(stdout))
    508       1.1       alm 		err(FATAL, "stdout: %s", strerror(errno ? errno : EIO));
    509       1.1       alm }
    510       1.1       alm 
    511       1.1       alm static inline int
    512       1.8       cgd regexec_e(preg, string, eflags, nomatch, slen)
    513       1.1       alm 	regex_t *preg;
    514       1.1       alm 	const char *string;
    515       1.1       alm 	int eflags, nomatch;
    516       1.8       cgd 	size_t slen;
    517       1.1       alm {
    518       1.1       alm 	int eval;
    519      1.24  christos #ifndef REG_STARTEND
    520      1.24  christos 	char *buf;
    521      1.24  christos #endif
    522       1.8       cgd 
    523       1.1       alm 	if (preg == NULL) {
    524       1.1       alm 		if (defpreg == NULL)
    525       1.1       alm 			err(FATAL, "first RE may not be empty");
    526       1.1       alm 	} else
    527       1.1       alm 		defpreg = preg;
    528       1.1       alm 
    529       1.8       cgd 	/* Set anchors, discounting trailing newline (if any). */
    530       1.8       cgd 	if (slen > 0 && string[slen - 1] == '\n')
    531       1.8       cgd 		slen--;
    532      1.24  christos 
    533      1.24  christos #ifndef REG_STARTEND
    534      1.24  christos 	if ((buf = malloc(slen + 1)) == NULL)
    535  1.25.4.1  wrstuden 		err(1, NULL);
    536      1.24  christos 	(void)memcpy(buf, string, slen);
    537      1.24  christos 	buf[slen] = '\0';
    538      1.24  christos 	eval = regexec(defpreg, buf,
    539      1.24  christos 	    nomatch ? 0 : maxnsub + 1, match, eflags);
    540      1.24  christos 	free(buf);
    541      1.24  christos #else
    542       1.8       cgd 	match[0].rm_so = 0;
    543       1.8       cgd 	match[0].rm_eo = slen;
    544       1.1       alm 	eval = regexec(defpreg, string,
    545       1.8       cgd 	    nomatch ? 0 : maxnsub + 1, match, eflags | REG_STARTEND);
    546      1.24  christos #endif
    547       1.1       alm 	switch(eval) {
    548       1.1       alm 	case 0:
    549       1.1       alm 		return (1);
    550       1.1       alm 	case REG_NOMATCH:
    551       1.1       alm 		return (0);
    552       1.1       alm 	}
    553       1.1       alm 	err(FATAL, "RE error: %s", strregerror(eval, defpreg));
    554       1.1       alm 	/* NOTREACHED */
    555      1.20     lukem 	return (0);
    556       1.1       alm }
    557       1.1       alm 
    558       1.1       alm /*
    559       1.1       alm  * regsub - perform substitutions after a regexp match
    560       1.1       alm  * Based on a routine by Henry Spencer
    561       1.1       alm  */
    562       1.1       alm static void
    563       1.1       alm regsub(sp, string, src)
    564       1.1       alm 	SPACE *sp;
    565       1.1       alm 	char *string, *src;
    566       1.1       alm {
    567      1.20     lukem 	int len, no;
    568      1.20     lukem 	char c, *dst;
    569       1.1       alm 
    570       1.1       alm #define	NEEDSP(reqlen)							\
    571       1.1       alm 	if (sp->len >= sp->blen - (reqlen) - 1) {			\
    572       1.1       alm 		sp->blen += (reqlen) + 1024;				\
    573       1.1       alm 		sp->space = sp->back = xrealloc(sp->back, sp->blen);	\
    574       1.1       alm 		dst = sp->space + sp->len;				\
    575       1.1       alm 	}
    576       1.1       alm 
    577       1.1       alm 	dst = sp->space + sp->len;
    578       1.1       alm 	while ((c = *src++) != '\0') {
    579       1.1       alm 		if (c == '&')
    580       1.1       alm 			no = 0;
    581      1.24  christos 		else if (c == '\\' && isdigit((unsigned char)*src))
    582       1.1       alm 			no = *src++ - '0';
    583       1.1       alm 		else
    584       1.1       alm 			no = -1;
    585       1.1       alm 		if (no < 0) {		/* Ordinary character. */
    586       1.1       alm  			if (c == '\\' && (*src == '\\' || *src == '&'))
    587       1.1       alm  				c = *src++;
    588       1.1       alm 			NEEDSP(1);
    589       1.1       alm  			*dst++ = c;
    590       1.1       alm 			++sp->len;
    591       1.1       alm  		} else if (match[no].rm_so != -1 && match[no].rm_eo != -1) {
    592       1.1       alm 			len = match[no].rm_eo - match[no].rm_so;
    593       1.1       alm 			NEEDSP(len);
    594       1.1       alm 			memmove(dst, string + match[no].rm_so, len);
    595       1.1       alm 			dst += len;
    596       1.1       alm 			sp->len += len;
    597       1.1       alm 		}
    598       1.1       alm 	}
    599       1.1       alm 	NEEDSP(1);
    600       1.1       alm 	*dst = '\0';
    601       1.1       alm }
    602       1.1       alm 
    603       1.1       alm /*
    604       1.1       alm  * aspace --
    605       1.1       alm  *	Append the source space to the destination space, allocating new
    606       1.1       alm  *	space as necessary.
    607       1.1       alm  */
    608       1.1       alm void
    609       1.1       alm cspace(sp, p, len, spflag)
    610       1.1       alm 	SPACE *sp;
    611       1.1       alm 	char *p;
    612       1.1       alm 	size_t len;
    613       1.1       alm 	enum e_spflag spflag;
    614       1.1       alm {
    615       1.1       alm 	size_t tlen;
    616       1.1       alm 
    617       1.8       cgd 	/* Make sure SPACE has enough memory and ramp up quickly. */
    618       1.8       cgd 	tlen = sp->len + len + 1;
    619       1.1       alm 	if (tlen > sp->blen) {
    620       1.1       alm 		sp->blen = tlen + 1024;
    621       1.1       alm 		sp->space = sp->back = xrealloc(sp->back, sp->blen);
    622       1.1       alm 	}
    623       1.1       alm 
    624       1.8       cgd 	if (spflag == REPLACE)
    625       1.1       alm 		sp->len = 0;
    626       1.1       alm 
    627       1.1       alm 	memmove(sp->space + sp->len, p, len);
    628       1.8       cgd 
    629       1.1       alm 	sp->space[sp->len += len] = '\0';
    630       1.1       alm }
    631       1.1       alm 
    632       1.1       alm /*
    633       1.1       alm  * Close all cached opened files and report any errors
    634       1.1       alm  */
    635       1.1       alm void
    636       1.1       alm cfclose(cp, end)
    637      1.20     lukem 	struct s_command *cp, *end;
    638       1.1       alm {
    639       1.1       alm 
    640       1.1       alm 	for (; cp != end; cp = cp->next)
    641       1.1       alm 		switch(cp->code) {
    642       1.1       alm 		case 's':
    643       1.1       alm 			if (cp->u.s->wfd != -1 && close(cp->u.s->wfd))
    644       1.1       alm 				err(FATAL,
    645       1.1       alm 				    "%s: %s", cp->u.s->wfile, strerror(errno));
    646       1.1       alm 			cp->u.s->wfd = -1;
    647       1.1       alm 			break;
    648       1.1       alm 		case 'w':
    649       1.1       alm 			if (cp->u.fd != -1 && close(cp->u.fd))
    650       1.1       alm 				err(FATAL, "%s: %s", cp->t, strerror(errno));
    651       1.1       alm 			cp->u.fd = -1;
    652       1.1       alm 			break;
    653       1.1       alm 		case '{':
    654       1.1       alm 			cfclose(cp->u.c, cp->next);
    655       1.1       alm 			break;
    656       1.1       alm 		}
    657       1.1       alm }
    658