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