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