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