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