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