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