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