xref: /illumos-gate/usr/src/ucbcmd/sed/sed0.c (revision bdcaf82257ab2deb6b46efaaa4bc93a1a44b3885)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
22*bdcaf822Sbasabi /*
23*bdcaf822Sbasabi  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*bdcaf822Sbasabi  * Use is subject to license terms.
25*bdcaf822Sbasabi  */
26*bdcaf822Sbasabi 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate 
31*bdcaf822Sbasabi #pragma ident	"%Z%%M%	%I%	%E% SMI"
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include <stdio.h>
347c478bd9Sstevel@tonic-gate #include <sys/param.h>
357c478bd9Sstevel@tonic-gate #include "sed.h"
36*bdcaf822Sbasabi 
377c478bd9Sstevel@tonic-gate #define	NWFILES		11	/* 10 plus one for standard output */
387c478bd9Sstevel@tonic-gate FILE	*fin;
397c478bd9Sstevel@tonic-gate FILE    *fcode[NWFILES];
407c478bd9Sstevel@tonic-gate char    *lastre;
417c478bd9Sstevel@tonic-gate char    sseof;
427c478bd9Sstevel@tonic-gate union reptr     *ptrend;
437c478bd9Sstevel@tonic-gate int     eflag;
44*bdcaf822Sbasabi extern	int	nbra;
457c478bd9Sstevel@tonic-gate char    linebuf[LBSIZE+1];
467c478bd9Sstevel@tonic-gate int     gflag;
477c478bd9Sstevel@tonic-gate int     nlno;
487c478bd9Sstevel@tonic-gate char    *fname[NWFILES];
497c478bd9Sstevel@tonic-gate int     nfiles;
507c478bd9Sstevel@tonic-gate union reptr ptrspace[PTRSIZE];
517c478bd9Sstevel@tonic-gate union reptr *rep;
527c478bd9Sstevel@tonic-gate char    *cp;
537c478bd9Sstevel@tonic-gate char    respace[RESIZE];
547c478bd9Sstevel@tonic-gate struct label ltab[LABSIZE];
557c478bd9Sstevel@tonic-gate struct label    *lab;
567c478bd9Sstevel@tonic-gate struct label    *labend;
577c478bd9Sstevel@tonic-gate int     depth;
587c478bd9Sstevel@tonic-gate int     eargc;
597c478bd9Sstevel@tonic-gate char    **eargv;
607c478bd9Sstevel@tonic-gate union reptr     **cmpend[DEPTH];
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate #define CCEOF	22
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate struct label    *labtab = ltab;
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate char	ETMES[]		= "Extra text at end of command: %s";
677c478bd9Sstevel@tonic-gate char	SMMES[]		= "Space missing before filename: %s";
687c478bd9Sstevel@tonic-gate char    TMMES[]		= "Too much command text: %s";
697c478bd9Sstevel@tonic-gate char    LTL[]  		= "Label too long: %s";
707c478bd9Sstevel@tonic-gate char    AD0MES[]	= "No addresses allowed: %s";
717c478bd9Sstevel@tonic-gate char    AD1MES[]	= "Only one address allowed: %s";
727c478bd9Sstevel@tonic-gate char	TOOBIG[]	= "Suffix too large - 512 max: %s";
737c478bd9Sstevel@tonic-gate 
74*bdcaf822Sbasabi extern int sed;	  /* IMPORTANT flag !!! */
757c478bd9Sstevel@tonic-gate extern char *comple();
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate extern char *malloc();
787c478bd9Sstevel@tonic-gate 
79*bdcaf822Sbasabi static void dechain(void);
80*bdcaf822Sbasabi static void fcomp(void);
81*bdcaf822Sbasabi 
82*bdcaf822Sbasabi int
83*bdcaf822Sbasabi main(int argc, char *argv[])
847c478bd9Sstevel@tonic-gate {
857c478bd9Sstevel@tonic-gate 	int flag_found = 0;
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 	sed = 1;
887c478bd9Sstevel@tonic-gate 	eargc = argc;
897c478bd9Sstevel@tonic-gate 	eargv = argv;
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 	aptr = abuf;
927c478bd9Sstevel@tonic-gate 	lab = labtab + 1;       /* 0 reserved for end-pointer */
937c478bd9Sstevel@tonic-gate 	rep = ptrspace;
947c478bd9Sstevel@tonic-gate 	rep->r1.ad1 = respace;
957c478bd9Sstevel@tonic-gate 	lcomend = &genbuf[71];
967c478bd9Sstevel@tonic-gate 	ptrend = &ptrspace[PTRSIZE];
977c478bd9Sstevel@tonic-gate 	labend = &labtab[LABSIZE];
987c478bd9Sstevel@tonic-gate 	lnum = 0;
997c478bd9Sstevel@tonic-gate 	pending = 0;
1007c478bd9Sstevel@tonic-gate 	depth = 0;
1017c478bd9Sstevel@tonic-gate 	spend = linebuf;
1027c478bd9Sstevel@tonic-gate 	hspend = holdsp;	/* Avoid "bus error" under "H" cmd. */
1037c478bd9Sstevel@tonic-gate 	fcode[0] = stdout;
1047c478bd9Sstevel@tonic-gate 	fname[0] = "";
1057c478bd9Sstevel@tonic-gate 	nfiles = 1;
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	if(eargc == 1)
1087c478bd9Sstevel@tonic-gate 		exit(0);
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 	setlocale(LC_ALL, "");		/* get locale environment */
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 	while (--eargc > 0 && (++eargv)[0][0] == '-')
1147c478bd9Sstevel@tonic-gate 		switch (eargv[0][1]) {
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 		case 'n':
1177c478bd9Sstevel@tonic-gate 			nflag++;
1187c478bd9Sstevel@tonic-gate 			continue;
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 		case 'f':
1217c478bd9Sstevel@tonic-gate 			flag_found = 1;
1227c478bd9Sstevel@tonic-gate 			if(eargc-- <= 0)	exit(2);
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 			if((fin = fopen(*++eargv, "r")) == NULL) {
1257c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "sed: ");
1267c478bd9Sstevel@tonic-gate 				perror(*eargv);
1277c478bd9Sstevel@tonic-gate 				exit(2);
1287c478bd9Sstevel@tonic-gate 			}
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 			fcomp();
1317c478bd9Sstevel@tonic-gate 			(void) fclose(fin);
1327c478bd9Sstevel@tonic-gate 			continue;
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 		case 'e':
1357c478bd9Sstevel@tonic-gate 			flag_found = 1;
1367c478bd9Sstevel@tonic-gate 			eflag++;
1377c478bd9Sstevel@tonic-gate 			fcomp();
1387c478bd9Sstevel@tonic-gate 			eflag = 0;
1397c478bd9Sstevel@tonic-gate 			continue;
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 		case 'g':
1427c478bd9Sstevel@tonic-gate 			gflag++;
1437c478bd9Sstevel@tonic-gate 			continue;
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 		default:
1467c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "sed: Unknown flag: %c\n", eargv[0][1]);
1477c478bd9Sstevel@tonic-gate 			exit(2);
1487c478bd9Sstevel@tonic-gate 		}
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	if(rep == ptrspace && !flag_found) {
1527c478bd9Sstevel@tonic-gate 		eargv--;
1537c478bd9Sstevel@tonic-gate 		eargc++;
1547c478bd9Sstevel@tonic-gate 		eflag++;
1557c478bd9Sstevel@tonic-gate 		fcomp();
1567c478bd9Sstevel@tonic-gate 		eargv++;
1577c478bd9Sstevel@tonic-gate 		eargc--;
1587c478bd9Sstevel@tonic-gate 		eflag = 0;
1597c478bd9Sstevel@tonic-gate 	}
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	if(depth)
1627c478bd9Sstevel@tonic-gate 		comperr("Too many {'s");
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	labtab->address = rep;
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	dechain();
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	if(eargc <= 0)
1697c478bd9Sstevel@tonic-gate 		execute((char *)NULL);
1707c478bd9Sstevel@tonic-gate 	else while(--eargc >= 0) {
1717c478bd9Sstevel@tonic-gate 		execute(*eargv++);
1727c478bd9Sstevel@tonic-gate 	}
1737c478bd9Sstevel@tonic-gate 	(void) fclose(stdout);
174*bdcaf822Sbasabi 	return (0);
1757c478bd9Sstevel@tonic-gate }
1767c478bd9Sstevel@tonic-gate 
177*bdcaf822Sbasabi static void
178*bdcaf822Sbasabi fcomp(void)
1797c478bd9Sstevel@tonic-gate {
1807c478bd9Sstevel@tonic-gate 
181*bdcaf822Sbasabi 	char   *p, *op, *tp;
1827c478bd9Sstevel@tonic-gate 	char    *address();
1837c478bd9Sstevel@tonic-gate 	union reptr     *pt, *pt1;
1847c478bd9Sstevel@tonic-gate 	int     i, ii;
1857c478bd9Sstevel@tonic-gate 	struct label    *lpt;
1867c478bd9Sstevel@tonic-gate 	char fnamebuf[MAXPATHLEN];
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	op = lastre;
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	if(rline(linebuf, &linebuf[LBSIZE+1]) < 0)  return;
1917c478bd9Sstevel@tonic-gate 	if(*linebuf == '#') {
1927c478bd9Sstevel@tonic-gate 		if(linebuf[1] == 'n')
1937c478bd9Sstevel@tonic-gate 			nflag = 1;
1947c478bd9Sstevel@tonic-gate 	}
1957c478bd9Sstevel@tonic-gate 	else {
1967c478bd9Sstevel@tonic-gate 		cp = linebuf;
1977c478bd9Sstevel@tonic-gate 		goto comploop;
1987c478bd9Sstevel@tonic-gate 	}
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	for(;;) {
2017c478bd9Sstevel@tonic-gate 		if(rline(linebuf, &linebuf[LBSIZE+1]) < 0)  break;
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 		cp = linebuf;
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate comploop:
206*bdcaf822Sbasabi /*		(void) fprintf(stderr, "cp: %s\n", cp); DEBUG */
2077c478bd9Sstevel@tonic-gate 		while(*cp == ' ' || *cp == '\t')	cp++;
2087c478bd9Sstevel@tonic-gate 		if(*cp == '\0' || *cp == '#')	 continue;
2097c478bd9Sstevel@tonic-gate 		if(*cp == ';') {
2107c478bd9Sstevel@tonic-gate 			cp++;
2117c478bd9Sstevel@tonic-gate 			goto comploop;
2127c478bd9Sstevel@tonic-gate 		}
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 		p = address(rep->r1.ad1);
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 		if(p == rep->r1.ad1) {
2177c478bd9Sstevel@tonic-gate 			if(op)
2187c478bd9Sstevel@tonic-gate 				rep->r1.ad1 = op;
2197c478bd9Sstevel@tonic-gate 			else
2207c478bd9Sstevel@tonic-gate 				comperr("First RE may not be null: %s");
2217c478bd9Sstevel@tonic-gate 		} else if(p == 0) {
2227c478bd9Sstevel@tonic-gate 			p = rep->r1.ad1;
2237c478bd9Sstevel@tonic-gate 			rep->r1.ad1 = 0;
2247c478bd9Sstevel@tonic-gate 		} else {
2257c478bd9Sstevel@tonic-gate 			op = rep->r1.ad1;
2267c478bd9Sstevel@tonic-gate 			if(*cp == ',' || *cp == ';') {
2277c478bd9Sstevel@tonic-gate 				cp++;
2287c478bd9Sstevel@tonic-gate 				rep->r1.ad2 = p;
2297c478bd9Sstevel@tonic-gate 				p = address(rep->r1.ad2);
2307c478bd9Sstevel@tonic-gate 				if(p == 0)
2317c478bd9Sstevel@tonic-gate 					comperr("Illegal line number: %s");
2327c478bd9Sstevel@tonic-gate 				if(p == rep->r1.ad2)
2337c478bd9Sstevel@tonic-gate 					rep->r1.ad2 = op;
2347c478bd9Sstevel@tonic-gate 				else
2357c478bd9Sstevel@tonic-gate 					op = rep->r1.ad2;
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 			} else
2387c478bd9Sstevel@tonic-gate 				rep->r1.ad2 = 0;
2397c478bd9Sstevel@tonic-gate 		}
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 		if(p > &respace[RESIZE-1])
2427c478bd9Sstevel@tonic-gate 			comperr(TMMES);
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 		while(*cp == ' ' || *cp == '\t')	cp++;
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate swit:
2477c478bd9Sstevel@tonic-gate 		switch(*cp++) {
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 			default:
2507c478bd9Sstevel@tonic-gate 				comperr("Unrecognized command: %s");
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 			case '!':
2537c478bd9Sstevel@tonic-gate 				rep->r1.negfl = 1;
2547c478bd9Sstevel@tonic-gate 				goto swit;
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 			case '{':
2577c478bd9Sstevel@tonic-gate 				rep->r1.command = BCOM;
2587c478bd9Sstevel@tonic-gate 				rep->r1.negfl = !(rep->r1.negfl);
2597c478bd9Sstevel@tonic-gate 				cmpend[depth++] = &rep->r2.lb1;
2607c478bd9Sstevel@tonic-gate 				if(++rep >= ptrend)
2617c478bd9Sstevel@tonic-gate 					comperr("Too many commands: %s");
2627c478bd9Sstevel@tonic-gate 				rep->r1.ad1 = p;
2637c478bd9Sstevel@tonic-gate 				if(*cp == '\0') continue;
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 				goto comploop;
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 			case '}':
2687c478bd9Sstevel@tonic-gate 				if(rep->r1.ad1)
2697c478bd9Sstevel@tonic-gate 					comperr(AD0MES);
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 				if(--depth < 0)
2727c478bd9Sstevel@tonic-gate 					comperr("Too many }'s");
2737c478bd9Sstevel@tonic-gate 				*cmpend[depth] = rep;
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 				rep->r1.ad1 = p;
2767c478bd9Sstevel@tonic-gate 				continue;
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 			case '=':
2797c478bd9Sstevel@tonic-gate 				rep->r1.command = EQCOM;
2807c478bd9Sstevel@tonic-gate 				if(rep->r1.ad2)
2817c478bd9Sstevel@tonic-gate 					comperr(AD1MES);
2827c478bd9Sstevel@tonic-gate 				break;
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 			case ':':
2857c478bd9Sstevel@tonic-gate 				if(rep->r1.ad1)
2867c478bd9Sstevel@tonic-gate 					comperr(AD0MES);
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 				while(*cp++ == ' ');
2897c478bd9Sstevel@tonic-gate 				cp--;
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 				tp = lab->asc;
2937c478bd9Sstevel@tonic-gate 				while((*tp++ = *cp++))
2947c478bd9Sstevel@tonic-gate 					if(tp >= &(lab->asc[8]))
2957c478bd9Sstevel@tonic-gate 						comperr(LTL);
2967c478bd9Sstevel@tonic-gate 				*--tp = '\0';
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 				if(lpt = search(lab)) {
2997c478bd9Sstevel@tonic-gate 					if(lpt->address)
3007c478bd9Sstevel@tonic-gate 						comperr("Duplicate labels: %s");
3017c478bd9Sstevel@tonic-gate 				} else {
3027c478bd9Sstevel@tonic-gate 					lab->chain = 0;
3037c478bd9Sstevel@tonic-gate 					lpt = lab;
3047c478bd9Sstevel@tonic-gate 					if(++lab >= labend)
3057c478bd9Sstevel@tonic-gate 						comperr("Too many labels: %s");
3067c478bd9Sstevel@tonic-gate 				}
3077c478bd9Sstevel@tonic-gate 				lpt->address = rep;
3087c478bd9Sstevel@tonic-gate 				rep->r1.ad1 = p;
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 				continue;
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 			case 'a':
3137c478bd9Sstevel@tonic-gate 				rep->r1.command = ACOM;
3147c478bd9Sstevel@tonic-gate 				if(rep->r1.ad2)
3157c478bd9Sstevel@tonic-gate 					comperr(AD1MES);
3167c478bd9Sstevel@tonic-gate 				if(*cp == '\\') cp++;
3177c478bd9Sstevel@tonic-gate 				if(*cp++ != '\n')
3187c478bd9Sstevel@tonic-gate 					comperr(ETMES);
3197c478bd9Sstevel@tonic-gate 				rep->r1.re1 = p;
3207c478bd9Sstevel@tonic-gate 				if ((p = text(rep->r1.re1, &respace[RESIZE-1])) == NULL)
3217c478bd9Sstevel@tonic-gate 					comperr(TMMES);
3227c478bd9Sstevel@tonic-gate 				break;
3237c478bd9Sstevel@tonic-gate 			case 'c':
3247c478bd9Sstevel@tonic-gate 				rep->r1.command = CCOM;
3257c478bd9Sstevel@tonic-gate 				if(*cp == '\\') cp++;
3267c478bd9Sstevel@tonic-gate 				if(*cp++ != ('\n'))
3277c478bd9Sstevel@tonic-gate 					comperr(ETMES);
3287c478bd9Sstevel@tonic-gate 				rep->r1.re1 = p;
3297c478bd9Sstevel@tonic-gate 				if ((p = text(rep->r1.re1, &respace[RESIZE-1])) == NULL)
3307c478bd9Sstevel@tonic-gate 					comperr(TMMES);
3317c478bd9Sstevel@tonic-gate 				break;
3327c478bd9Sstevel@tonic-gate 			case 'i':
3337c478bd9Sstevel@tonic-gate 				rep->r1.command = ICOM;
3347c478bd9Sstevel@tonic-gate 				if(rep->r1.ad2)
3357c478bd9Sstevel@tonic-gate 					comperr(AD1MES);
3367c478bd9Sstevel@tonic-gate 				if(*cp == '\\') cp++;
3377c478bd9Sstevel@tonic-gate 				if(*cp++ != ('\n'))
3387c478bd9Sstevel@tonic-gate 					comperr(ETMES);
3397c478bd9Sstevel@tonic-gate 				rep->r1.re1 = p;
3407c478bd9Sstevel@tonic-gate 				if ((p = text(rep->r1.re1, &respace[RESIZE-1])) == NULL)
3417c478bd9Sstevel@tonic-gate 					comperr(TMMES);
3427c478bd9Sstevel@tonic-gate 				break;
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 			case 'g':
3457c478bd9Sstevel@tonic-gate 				rep->r1.command = GCOM;
3467c478bd9Sstevel@tonic-gate 				break;
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 			case 'G':
3497c478bd9Sstevel@tonic-gate 				rep->r1.command = CGCOM;
3507c478bd9Sstevel@tonic-gate 				break;
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 			case 'h':
3537c478bd9Sstevel@tonic-gate 				rep->r1.command = HCOM;
3547c478bd9Sstevel@tonic-gate 				break;
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 			case 'H':
3577c478bd9Sstevel@tonic-gate 				rep->r1.command = CHCOM;
3587c478bd9Sstevel@tonic-gate 				break;
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 			case 't':
3617c478bd9Sstevel@tonic-gate 				rep->r1.command = TCOM;
3627c478bd9Sstevel@tonic-gate 				goto jtcommon;
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 			case 'b':
3657c478bd9Sstevel@tonic-gate 				rep->r1.command = BCOM;
3667c478bd9Sstevel@tonic-gate jtcommon:
3677c478bd9Sstevel@tonic-gate 				while(*cp++ == ' ');
3687c478bd9Sstevel@tonic-gate 				cp--;
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 				if(*cp == '\0') {
3717c478bd9Sstevel@tonic-gate 					if(pt = labtab->chain) {
3727c478bd9Sstevel@tonic-gate 						while(pt1 = pt->r2.lb1)
3737c478bd9Sstevel@tonic-gate 							pt = pt1;
3747c478bd9Sstevel@tonic-gate 						pt->r2.lb1 = rep;
3757c478bd9Sstevel@tonic-gate 					} else
3767c478bd9Sstevel@tonic-gate 						labtab->chain = rep;
3777c478bd9Sstevel@tonic-gate 					break;
3787c478bd9Sstevel@tonic-gate 				}
3797c478bd9Sstevel@tonic-gate 				tp = lab->asc;
3807c478bd9Sstevel@tonic-gate 				while((*tp++ = *cp++))
3817c478bd9Sstevel@tonic-gate 					if(tp >= &(lab->asc[8]))
3827c478bd9Sstevel@tonic-gate 						comperr(LTL);
3837c478bd9Sstevel@tonic-gate 				cp--;
3847c478bd9Sstevel@tonic-gate 				*--tp = '\0';
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 				if(lpt = search(lab)) {
3877c478bd9Sstevel@tonic-gate 					if(lpt->address) {
3887c478bd9Sstevel@tonic-gate 						rep->r2.lb1 = lpt->address;
3897c478bd9Sstevel@tonic-gate 					} else {
3907c478bd9Sstevel@tonic-gate 						pt = lpt->chain;
3917c478bd9Sstevel@tonic-gate 						while(pt1 = pt->r2.lb1)
3927c478bd9Sstevel@tonic-gate 							pt = pt1;
3937c478bd9Sstevel@tonic-gate 						pt->r2.lb1 = rep;
3947c478bd9Sstevel@tonic-gate 					}
3957c478bd9Sstevel@tonic-gate 				} else {
3967c478bd9Sstevel@tonic-gate 					lab->chain = rep;
3977c478bd9Sstevel@tonic-gate 					lab->address = 0;
3987c478bd9Sstevel@tonic-gate 					if(++lab >= labend)
3997c478bd9Sstevel@tonic-gate 						comperr("Too many labels: %s");
4007c478bd9Sstevel@tonic-gate 				}
4017c478bd9Sstevel@tonic-gate 				break;
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 			case 'n':
4047c478bd9Sstevel@tonic-gate 				rep->r1.command = NCOM;
4057c478bd9Sstevel@tonic-gate 				break;
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 			case 'N':
4087c478bd9Sstevel@tonic-gate 				rep->r1.command = CNCOM;
4097c478bd9Sstevel@tonic-gate 				break;
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 			case 'p':
4127c478bd9Sstevel@tonic-gate 				rep->r1.command = PCOM;
4137c478bd9Sstevel@tonic-gate 				break;
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 			case 'P':
4167c478bd9Sstevel@tonic-gate 				rep->r1.command = CPCOM;
4177c478bd9Sstevel@tonic-gate 				break;
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate 			case 'r':
4207c478bd9Sstevel@tonic-gate 				rep->r1.command = RCOM;
4217c478bd9Sstevel@tonic-gate 				if(rep->r1.ad2)
4227c478bd9Sstevel@tonic-gate 					comperr(AD1MES);
4237c478bd9Sstevel@tonic-gate 				if(*cp++ != ' ')
4247c478bd9Sstevel@tonic-gate 					comperr(SMMES);
4257c478bd9Sstevel@tonic-gate 				rep->r1.re1 = p;
4267c478bd9Sstevel@tonic-gate 				if ((p = text(rep->r1.re1, &respace[RESIZE-1])) == NULL)
4277c478bd9Sstevel@tonic-gate 					comperr(TMMES);
4287c478bd9Sstevel@tonic-gate 				break;
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 			case 'd':
4317c478bd9Sstevel@tonic-gate 				rep->r1.command = DCOM;
4327c478bd9Sstevel@tonic-gate 				break;
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 			case 'D':
4357c478bd9Sstevel@tonic-gate 				rep->r1.command = CDCOM;
4367c478bd9Sstevel@tonic-gate 				rep->r2.lb1 = ptrspace;
4377c478bd9Sstevel@tonic-gate 				break;
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 			case 'q':
4407c478bd9Sstevel@tonic-gate 				rep->r1.command = QCOM;
4417c478bd9Sstevel@tonic-gate 				if(rep->r1.ad2)
4427c478bd9Sstevel@tonic-gate 					comperr(AD1MES);
4437c478bd9Sstevel@tonic-gate 				break;
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 			case 'l':
4467c478bd9Sstevel@tonic-gate 				rep->r1.command = LCOM;
4477c478bd9Sstevel@tonic-gate 				break;
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 			case 's':
4507c478bd9Sstevel@tonic-gate 				rep->r1.command = SCOM;
4517c478bd9Sstevel@tonic-gate 				sseof = *cp++;
4527c478bd9Sstevel@tonic-gate 				rep->r1.re1 = p;
4537c478bd9Sstevel@tonic-gate 				p = comple((char *) 0, rep->r1.re1, &respace[RESIZE-1], sseof);
4547c478bd9Sstevel@tonic-gate 				if(p == rep->r1.re1) {
4557c478bd9Sstevel@tonic-gate 					if(op)
4567c478bd9Sstevel@tonic-gate 						rep->r1.re1 = op;
4577c478bd9Sstevel@tonic-gate 					else
4587c478bd9Sstevel@tonic-gate 						comperr("First RE may not be null: %s");
4597c478bd9Sstevel@tonic-gate 				} else
4607c478bd9Sstevel@tonic-gate 					op = rep->r1.re1;
4617c478bd9Sstevel@tonic-gate 				rep->r1.rhs = p;
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 				p = compsub(rep->r1.rhs);
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 				if(*cp == 'g') {
4667c478bd9Sstevel@tonic-gate 					cp++;
4677c478bd9Sstevel@tonic-gate 					rep->r1.gfl = 999;
4687c478bd9Sstevel@tonic-gate 				} else if(gflag)
4697c478bd9Sstevel@tonic-gate 					rep->r1.gfl = 999;
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate 				if(*cp >= '1' && *cp <= '9')
4727c478bd9Sstevel@tonic-gate 					{i = *cp - '0';
4737c478bd9Sstevel@tonic-gate 					cp++;
4747c478bd9Sstevel@tonic-gate 					while(1)
4757c478bd9Sstevel@tonic-gate 						{ii = *cp;
4767c478bd9Sstevel@tonic-gate 						if(ii < '0' || ii > '9') break;
4777c478bd9Sstevel@tonic-gate 						i = i*10 + ii - '0';
4787c478bd9Sstevel@tonic-gate 						if(i > 512)
4797c478bd9Sstevel@tonic-gate 							comperr(TOOBIG);
4807c478bd9Sstevel@tonic-gate 						cp++;
4817c478bd9Sstevel@tonic-gate 						}
4827c478bd9Sstevel@tonic-gate 					rep->r1.gfl = i;
4837c478bd9Sstevel@tonic-gate 					}
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 				if(*cp == 'p') {
4867c478bd9Sstevel@tonic-gate 					cp++;
4877c478bd9Sstevel@tonic-gate 					rep->r1.pfl = 1;
4887c478bd9Sstevel@tonic-gate 				}
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate 				if(*cp == 'P') {
4917c478bd9Sstevel@tonic-gate 					cp++;
4927c478bd9Sstevel@tonic-gate 					rep->r1.pfl = 2;
4937c478bd9Sstevel@tonic-gate 				}
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate 				if(*cp == 'w') {
4967c478bd9Sstevel@tonic-gate 					cp++;
4977c478bd9Sstevel@tonic-gate 					if(*cp++ !=  ' ')
4987c478bd9Sstevel@tonic-gate 						comperr(SMMES);
4997c478bd9Sstevel@tonic-gate 					if (text(fnamebuf, &fnamebuf[MAXPATHLEN]) == NULL)
5007c478bd9Sstevel@tonic-gate 						comperr("File name too long: %s");
5017c478bd9Sstevel@tonic-gate 					for(i = nfiles - 1; i >= 0; i--)
5027c478bd9Sstevel@tonic-gate 						if(strcmp(fnamebuf,fname[i]) == 0) {
5037c478bd9Sstevel@tonic-gate 							rep->r1.fcode = fcode[i];
5047c478bd9Sstevel@tonic-gate 							goto done;
5057c478bd9Sstevel@tonic-gate 						}
5067c478bd9Sstevel@tonic-gate 					if(nfiles >= NWFILES)
5077c478bd9Sstevel@tonic-gate 						comperr("Too many files in w commands: %s");
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 					i = strlen(fnamebuf) + 1;
5107c478bd9Sstevel@tonic-gate 					if ((fname[nfiles] = malloc((unsigned)i)) == NULL) {
5117c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr, "sed: Out of memory\n");
5127c478bd9Sstevel@tonic-gate 						exit(2);
5137c478bd9Sstevel@tonic-gate 					}
5147c478bd9Sstevel@tonic-gate 					(void) strcpy(fname[nfiles], fnamebuf);
5157c478bd9Sstevel@tonic-gate 					if((rep->r1.fcode = fopen(fname[nfiles], "w")) == NULL) {
5167c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr, "sed: Cannot open ");
5177c478bd9Sstevel@tonic-gate 						perror(fname[nfiles]);
5187c478bd9Sstevel@tonic-gate 						exit(2);
5197c478bd9Sstevel@tonic-gate 					}
5207c478bd9Sstevel@tonic-gate 					fcode[nfiles++] = rep->r1.fcode;
5217c478bd9Sstevel@tonic-gate 				}
5227c478bd9Sstevel@tonic-gate 				break;
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 			case 'w':
5257c478bd9Sstevel@tonic-gate 				rep->r1.command = WCOM;
5267c478bd9Sstevel@tonic-gate 				if(*cp++ != ' ')
5277c478bd9Sstevel@tonic-gate 					comperr(SMMES);
5287c478bd9Sstevel@tonic-gate 				if (text(fnamebuf, &fnamebuf[MAXPATHLEN]) == NULL)
5297c478bd9Sstevel@tonic-gate 					comperr("File name too long: %s");
5307c478bd9Sstevel@tonic-gate 				for(i = nfiles - 1; i >= 0; i--)
5317c478bd9Sstevel@tonic-gate 					if(strcmp(fnamebuf, fname[i]) == 0) {
5327c478bd9Sstevel@tonic-gate 						rep->r1.fcode = fcode[i];
5337c478bd9Sstevel@tonic-gate 						goto done;
5347c478bd9Sstevel@tonic-gate 					}
5357c478bd9Sstevel@tonic-gate 				if(nfiles >= NWFILES)
5367c478bd9Sstevel@tonic-gate 					comperr("Too many files in w commands: %s");
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 				i = strlen(fnamebuf) + 1;
5397c478bd9Sstevel@tonic-gate 				if ((fname[nfiles] = malloc((unsigned)i)) == NULL) {
5407c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, "sed: Out of memory\n");
5417c478bd9Sstevel@tonic-gate 					exit(2);
5427c478bd9Sstevel@tonic-gate 				}
5437c478bd9Sstevel@tonic-gate 				(void) strcpy(fname[nfiles], fnamebuf);
5447c478bd9Sstevel@tonic-gate 				if((rep->r1.fcode = fopen(fname[nfiles], "w")) == NULL) {
5457c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, "sed: Cannot create ");
5467c478bd9Sstevel@tonic-gate 					perror(fname[nfiles]);
5477c478bd9Sstevel@tonic-gate 					exit(2);
5487c478bd9Sstevel@tonic-gate 				}
5497c478bd9Sstevel@tonic-gate 				fcode[nfiles++] = rep->r1.fcode;
5507c478bd9Sstevel@tonic-gate 				break;
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 			case 'x':
5537c478bd9Sstevel@tonic-gate 				rep->r1.command = XCOM;
5547c478bd9Sstevel@tonic-gate 				break;
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate 			case 'y':
5577c478bd9Sstevel@tonic-gate 				rep->r1.command = YCOM;
5587c478bd9Sstevel@tonic-gate 				sseof = *cp++;
5597c478bd9Sstevel@tonic-gate 				rep->r1.re1 = p;
5607c478bd9Sstevel@tonic-gate 				p = ycomp(rep->r1.re1);
5617c478bd9Sstevel@tonic-gate 				break;
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 		}
5647c478bd9Sstevel@tonic-gate done:
5657c478bd9Sstevel@tonic-gate 		if(++rep >= ptrend)
5667c478bd9Sstevel@tonic-gate 			comperr("Too many commands, last: %s");
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 		rep->r1.ad1 = p;
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate 		if(*cp++ != '\0') {
5717c478bd9Sstevel@tonic-gate 			if(cp[-1] == ';')
5727c478bd9Sstevel@tonic-gate 				goto comploop;
5737c478bd9Sstevel@tonic-gate 			comperr(ETMES);
5747c478bd9Sstevel@tonic-gate 		}
5757c478bd9Sstevel@tonic-gate 	}
5767c478bd9Sstevel@tonic-gate 	rep->r1.command = 0;
5777c478bd9Sstevel@tonic-gate 	lastre = op;
5787c478bd9Sstevel@tonic-gate }
579*bdcaf822Sbasabi 
5807c478bd9Sstevel@tonic-gate char    *compsub(rhsbuf)
5817c478bd9Sstevel@tonic-gate char    *rhsbuf;
5827c478bd9Sstevel@tonic-gate {
583*bdcaf822Sbasabi 	char   *p, *q;
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate 	p = rhsbuf;
5867c478bd9Sstevel@tonic-gate 	q = cp;
5877c478bd9Sstevel@tonic-gate 	for(;;) {
5887c478bd9Sstevel@tonic-gate 		if(p > &respace[RESIZE-1])
5897c478bd9Sstevel@tonic-gate 			comperr(TMMES);
5907c478bd9Sstevel@tonic-gate 		if((*p = *q++) == '\\') {
5917c478bd9Sstevel@tonic-gate 			p++;
5927c478bd9Sstevel@tonic-gate 			if(p > &respace[RESIZE-1])
5937c478bd9Sstevel@tonic-gate 				comperr(TMMES);
5947c478bd9Sstevel@tonic-gate 			*p = *q++;
5957c478bd9Sstevel@tonic-gate 			if(*p > nbra + '0' && *p <= '9')
5967c478bd9Sstevel@tonic-gate 				comperr("``\\digit'' out of range: %s");
5977c478bd9Sstevel@tonic-gate 			p++;
5987c478bd9Sstevel@tonic-gate 			continue;
5997c478bd9Sstevel@tonic-gate 		}
6007c478bd9Sstevel@tonic-gate 		if(*p == sseof) {
6017c478bd9Sstevel@tonic-gate 			*p++ = '\0';
6027c478bd9Sstevel@tonic-gate 			cp = q;
6037c478bd9Sstevel@tonic-gate 			return(p);
6047c478bd9Sstevel@tonic-gate 		}
6057c478bd9Sstevel@tonic-gate   		if(*p++ == '\0')
6067c478bd9Sstevel@tonic-gate 			comperr("Ending delimiter missing on substitution: %s");
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 	}
6097c478bd9Sstevel@tonic-gate }
6107c478bd9Sstevel@tonic-gate 
611*bdcaf822Sbasabi int
6127c478bd9Sstevel@tonic-gate rline(lbuf, lbend)
6137c478bd9Sstevel@tonic-gate char    *lbuf;
6147c478bd9Sstevel@tonic-gate char	*lbend;
6157c478bd9Sstevel@tonic-gate {
616*bdcaf822Sbasabi 	char   *p, *q;
617*bdcaf822Sbasabi 	int	t;
6187c478bd9Sstevel@tonic-gate 	static char     *saveq;
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate 	p = lbuf;
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate 	if(eflag) {
6237c478bd9Sstevel@tonic-gate 		if(eflag > 0) {
6247c478bd9Sstevel@tonic-gate 			eflag = -1;
6257c478bd9Sstevel@tonic-gate 			if(--eargc <= 0)
6267c478bd9Sstevel@tonic-gate 				exit(2);
6277c478bd9Sstevel@tonic-gate 			q = *++eargv;
6287c478bd9Sstevel@tonic-gate 			while((t = *q++) != '\0') {
6297c478bd9Sstevel@tonic-gate 				if(t == '\n') {
6307c478bd9Sstevel@tonic-gate 					saveq = q;
6317c478bd9Sstevel@tonic-gate 					goto out1;
6327c478bd9Sstevel@tonic-gate 				}
6337c478bd9Sstevel@tonic-gate 				if (p < lbend)
6347c478bd9Sstevel@tonic-gate 					*p++ = t;
6357c478bd9Sstevel@tonic-gate 				if(t == '\\') {
6367c478bd9Sstevel@tonic-gate 					if((t = *q++) == '\0') {
6377c478bd9Sstevel@tonic-gate 						saveq = 0;
6387c478bd9Sstevel@tonic-gate 						return(-1);
6397c478bd9Sstevel@tonic-gate 					}
6407c478bd9Sstevel@tonic-gate 					if (p < lbend)
6417c478bd9Sstevel@tonic-gate 						*p++ = t;
6427c478bd9Sstevel@tonic-gate 				}
6437c478bd9Sstevel@tonic-gate 			}
6447c478bd9Sstevel@tonic-gate 			saveq = 0;
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 		out1:
6477c478bd9Sstevel@tonic-gate 			if (p == lbend)
6487c478bd9Sstevel@tonic-gate 				comperr("Command line too long");
6497c478bd9Sstevel@tonic-gate 			*p = '\0';
6507c478bd9Sstevel@tonic-gate 			return(1);
6517c478bd9Sstevel@tonic-gate 		}
6527c478bd9Sstevel@tonic-gate 		if((q = saveq) == 0)    return(-1);
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate 		while((t = *q++) != '\0') {
6557c478bd9Sstevel@tonic-gate 			if(t == '\n') {
6567c478bd9Sstevel@tonic-gate 				saveq = q;
6577c478bd9Sstevel@tonic-gate 				goto out2;
6587c478bd9Sstevel@tonic-gate 			}
6597c478bd9Sstevel@tonic-gate 			if(p < lbend)
6607c478bd9Sstevel@tonic-gate 				*p++ = t;
6617c478bd9Sstevel@tonic-gate 			if(t == '\\') {
6627c478bd9Sstevel@tonic-gate 				if((t = *q++) == '\0') {
6637c478bd9Sstevel@tonic-gate 					saveq = 0;
6647c478bd9Sstevel@tonic-gate 					return(-1);
6657c478bd9Sstevel@tonic-gate 				}
6667c478bd9Sstevel@tonic-gate 				if (p < lbend)
6677c478bd9Sstevel@tonic-gate 					*p++ = t;
6687c478bd9Sstevel@tonic-gate 			}
6697c478bd9Sstevel@tonic-gate 		}
6707c478bd9Sstevel@tonic-gate 		saveq = 0;
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate 	out2:
6737c478bd9Sstevel@tonic-gate 		if (p == lbend)
6747c478bd9Sstevel@tonic-gate 			comperr("Command line too long");
6757c478bd9Sstevel@tonic-gate 		*p = '\0';
6767c478bd9Sstevel@tonic-gate 		return(1);
6777c478bd9Sstevel@tonic-gate 	}
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 	while((t = getc(fin)) != EOF) {
6807c478bd9Sstevel@tonic-gate 		if(t == '\n') {
6817c478bd9Sstevel@tonic-gate 			if (p == lbend)
6827c478bd9Sstevel@tonic-gate 				comperr("Command line too long");
6837c478bd9Sstevel@tonic-gate 			*p = '\0';
6847c478bd9Sstevel@tonic-gate 			return(1);
6857c478bd9Sstevel@tonic-gate 		}
6867c478bd9Sstevel@tonic-gate 		if (p < lbend)
6877c478bd9Sstevel@tonic-gate 			*p++ = t;
6887c478bd9Sstevel@tonic-gate 		if(t == '\\') {
6897c478bd9Sstevel@tonic-gate 			if((t = getc(fin)) == EOF)
6907c478bd9Sstevel@tonic-gate 				break;
6917c478bd9Sstevel@tonic-gate 			if(p < lbend)
6927c478bd9Sstevel@tonic-gate 				*p++ = t;
6937c478bd9Sstevel@tonic-gate 		}
6947c478bd9Sstevel@tonic-gate 	}
6957c478bd9Sstevel@tonic-gate 	if(ferror(fin)) {
6967c478bd9Sstevel@tonic-gate 		perror("sed: Error reading pattern file");
6977c478bd9Sstevel@tonic-gate 		exit(2);
6987c478bd9Sstevel@tonic-gate 	}
6997c478bd9Sstevel@tonic-gate 	return(-1);
7007c478bd9Sstevel@tonic-gate }
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate char    *address(expbuf)
7037c478bd9Sstevel@tonic-gate char    *expbuf;
7047c478bd9Sstevel@tonic-gate {
705*bdcaf822Sbasabi 	char   *rcp;
7067c478bd9Sstevel@tonic-gate 	long long	lno;
7077c478bd9Sstevel@tonic-gate 
7087c478bd9Sstevel@tonic-gate 	if(*cp == '$') {
7097c478bd9Sstevel@tonic-gate 		if (expbuf > &respace[RESIZE-2])
7107c478bd9Sstevel@tonic-gate 			comperr(TMMES);
7117c478bd9Sstevel@tonic-gate 		cp++;
7127c478bd9Sstevel@tonic-gate 		*expbuf++ = CEND;
7137c478bd9Sstevel@tonic-gate 		*expbuf++ = CCEOF;
7147c478bd9Sstevel@tonic-gate 		return(expbuf);
7157c478bd9Sstevel@tonic-gate 	}
7167c478bd9Sstevel@tonic-gate 	if (*cp == '/' || *cp == '\\' ) {
7177c478bd9Sstevel@tonic-gate 		if ( *cp == '\\' )
7187c478bd9Sstevel@tonic-gate 			cp++;
7197c478bd9Sstevel@tonic-gate 		sseof = *cp++;
7207c478bd9Sstevel@tonic-gate 		return(comple((char *) 0, expbuf, &respace[RESIZE-1], sseof));
7217c478bd9Sstevel@tonic-gate 	}
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate 	rcp = cp;
7247c478bd9Sstevel@tonic-gate 	lno = 0;
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate 	while(*rcp >= '0' && *rcp <= '9')
7277c478bd9Sstevel@tonic-gate 		lno = lno*10 + *rcp++ - '0';
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate 	if(rcp > cp) {
7307c478bd9Sstevel@tonic-gate 		if (expbuf > &respace[RESIZE-3])
7317c478bd9Sstevel@tonic-gate 			comperr(TMMES);
7327c478bd9Sstevel@tonic-gate 		*expbuf++ = CLNUM;
7337c478bd9Sstevel@tonic-gate 		*expbuf++ = nlno;
7347c478bd9Sstevel@tonic-gate 		tlno[nlno++] = lno;
7357c478bd9Sstevel@tonic-gate 		if(nlno >= NLINES)
7367c478bd9Sstevel@tonic-gate 			comperr("Too many line numbers: %s");
7377c478bd9Sstevel@tonic-gate 		*expbuf++ = CCEOF;
7387c478bd9Sstevel@tonic-gate 		cp = rcp;
7397c478bd9Sstevel@tonic-gate 		return(expbuf);
7407c478bd9Sstevel@tonic-gate 	}
7417c478bd9Sstevel@tonic-gate 	return(0);
7427c478bd9Sstevel@tonic-gate }
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate char    *text(textbuf, tbend)
7457c478bd9Sstevel@tonic-gate char    *textbuf;
7467c478bd9Sstevel@tonic-gate char	*tbend;
7477c478bd9Sstevel@tonic-gate {
748*bdcaf822Sbasabi 	char   *p, *q;
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 	p = textbuf;
7517c478bd9Sstevel@tonic-gate 	q = cp;
7527c478bd9Sstevel@tonic-gate #ifndef S5EMUL
7537c478bd9Sstevel@tonic-gate 	/*
7547c478bd9Sstevel@tonic-gate 	 * Strip off indentation from text to be inserted.
7557c478bd9Sstevel@tonic-gate 	 */
7567c478bd9Sstevel@tonic-gate 	while(*q == '\t' || *q == ' ')	q++;
7577c478bd9Sstevel@tonic-gate #endif
7587c478bd9Sstevel@tonic-gate 	for(;;) {
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate 		if(p > tbend)
7617c478bd9Sstevel@tonic-gate 			return(NULL);	/* overflowed the buffer */
7627c478bd9Sstevel@tonic-gate 		if((*p = *q++) == '\\')
7637c478bd9Sstevel@tonic-gate 			*p = *q++;
7647c478bd9Sstevel@tonic-gate 		if(*p == '\0') {
7657c478bd9Sstevel@tonic-gate 			cp = --q;
7667c478bd9Sstevel@tonic-gate 			return(++p);
7677c478bd9Sstevel@tonic-gate 		}
7687c478bd9Sstevel@tonic-gate #ifndef S5EMUL
7697c478bd9Sstevel@tonic-gate 		/*
7707c478bd9Sstevel@tonic-gate 		 * Strip off indentation from text to be inserted.
7717c478bd9Sstevel@tonic-gate 		 */
7727c478bd9Sstevel@tonic-gate 		if(*p == '\n') {
7737c478bd9Sstevel@tonic-gate 			while(*q == '\t' || *q == ' ')	q++;
7747c478bd9Sstevel@tonic-gate 		}
7757c478bd9Sstevel@tonic-gate #endif
7767c478bd9Sstevel@tonic-gate 		p++;
7777c478bd9Sstevel@tonic-gate 	}
7787c478bd9Sstevel@tonic-gate }
7797c478bd9Sstevel@tonic-gate 
7807c478bd9Sstevel@tonic-gate 
7817c478bd9Sstevel@tonic-gate struct label    *search(ptr)
7827c478bd9Sstevel@tonic-gate struct label    *ptr;
7837c478bd9Sstevel@tonic-gate {
7847c478bd9Sstevel@tonic-gate 	struct label    *rp;
7857c478bd9Sstevel@tonic-gate 
7867c478bd9Sstevel@tonic-gate 	rp = labtab;
7877c478bd9Sstevel@tonic-gate 	while(rp < ptr) {
7887c478bd9Sstevel@tonic-gate 		if(strcmp(rp->asc, ptr->asc) == 0)
7897c478bd9Sstevel@tonic-gate 			return(rp);
7907c478bd9Sstevel@tonic-gate 		rp++;
7917c478bd9Sstevel@tonic-gate 	}
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate 	return(0);
7947c478bd9Sstevel@tonic-gate }
7957c478bd9Sstevel@tonic-gate 
7967c478bd9Sstevel@tonic-gate 
797*bdcaf822Sbasabi static void
798*bdcaf822Sbasabi dechain(void)
7997c478bd9Sstevel@tonic-gate {
8007c478bd9Sstevel@tonic-gate 	struct label    *lptr;
8017c478bd9Sstevel@tonic-gate 	union reptr     *rptr, *trptr;
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate 	for(lptr = labtab; lptr < lab; lptr++) {
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate 		if(lptr->address == 0) {
8067c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "sed: Undefined label: %s\n", lptr->asc);
8077c478bd9Sstevel@tonic-gate 			exit(2);
8087c478bd9Sstevel@tonic-gate 		}
8097c478bd9Sstevel@tonic-gate 
8107c478bd9Sstevel@tonic-gate 		if(lptr->chain) {
8117c478bd9Sstevel@tonic-gate 			rptr = lptr->chain;
8127c478bd9Sstevel@tonic-gate 			while(trptr = rptr->r2.lb1) {
8137c478bd9Sstevel@tonic-gate 				rptr->r2.lb1 = lptr->address;
8147c478bd9Sstevel@tonic-gate 				rptr = trptr;
8157c478bd9Sstevel@tonic-gate 			}
8167c478bd9Sstevel@tonic-gate 			rptr->r2.lb1 = lptr->address;
8177c478bd9Sstevel@tonic-gate 		}
8187c478bd9Sstevel@tonic-gate 	}
8197c478bd9Sstevel@tonic-gate }
8207c478bd9Sstevel@tonic-gate 
8217c478bd9Sstevel@tonic-gate char *ycomp(expbuf)
8227c478bd9Sstevel@tonic-gate char    *expbuf;
8237c478bd9Sstevel@tonic-gate {
824*bdcaf822Sbasabi 	char	c;
825*bdcaf822Sbasabi 	char *ep, *tsp;
826*bdcaf822Sbasabi 	int i;
8277c478bd9Sstevel@tonic-gate 	char    *sp;
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate 	ep = expbuf;
8307c478bd9Sstevel@tonic-gate 	if(ep + 0377 > &respace[RESIZE-1])
8317c478bd9Sstevel@tonic-gate 		comperr(TMMES);
8327c478bd9Sstevel@tonic-gate 	sp = cp;
8337c478bd9Sstevel@tonic-gate 	for(tsp = cp; (c = *tsp) != sseof; tsp++) {
8347c478bd9Sstevel@tonic-gate 		if(c == '\\')
8357c478bd9Sstevel@tonic-gate 			tsp++;
8367c478bd9Sstevel@tonic-gate 		if(c == '\0' || c == '\n')
8377c478bd9Sstevel@tonic-gate 			comperr("Ending delimiter missing on string: %s");
8387c478bd9Sstevel@tonic-gate 	}
8397c478bd9Sstevel@tonic-gate 	tsp++;
8407c478bd9Sstevel@tonic-gate 
8417c478bd9Sstevel@tonic-gate 	while((c = *sp++) != sseof) {
8427c478bd9Sstevel@tonic-gate 		c &= 0377;
8437c478bd9Sstevel@tonic-gate 		if(c == '\\' && *sp == 'n') {
8447c478bd9Sstevel@tonic-gate 			sp++;
8457c478bd9Sstevel@tonic-gate 			c = '\n';
8467c478bd9Sstevel@tonic-gate 		}
8477c478bd9Sstevel@tonic-gate 		if((ep[c] = *tsp++) == '\\' && *tsp == 'n') {
8487c478bd9Sstevel@tonic-gate 			ep[c] = '\n';
8497c478bd9Sstevel@tonic-gate 			tsp++;
8507c478bd9Sstevel@tonic-gate 		}
8517c478bd9Sstevel@tonic-gate 		if(ep[c] == sseof || ep[c] == '\0')
8527c478bd9Sstevel@tonic-gate 			comperr("Transform strings not the same size: %s");
8537c478bd9Sstevel@tonic-gate 	}
8547c478bd9Sstevel@tonic-gate 	if(*tsp != sseof) {
8557c478bd9Sstevel@tonic-gate 		if(*tsp == '\0')
8567c478bd9Sstevel@tonic-gate 			comperr("Ending delimiter missing on string: %s");
8577c478bd9Sstevel@tonic-gate 		else
8587c478bd9Sstevel@tonic-gate 			comperr("Transform strings not the same size: %s");
8597c478bd9Sstevel@tonic-gate 	}
8607c478bd9Sstevel@tonic-gate 	cp = ++tsp;
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate 	for(i = 0; i < 0400; i++)
8637c478bd9Sstevel@tonic-gate 		if(ep[i] == 0)
8647c478bd9Sstevel@tonic-gate 			ep[i] = i;
8657c478bd9Sstevel@tonic-gate 
8667c478bd9Sstevel@tonic-gate 	return(ep + 0400);
8677c478bd9Sstevel@tonic-gate }
868*bdcaf822Sbasabi 
869*bdcaf822Sbasabi void
870*bdcaf822Sbasabi comperr(char *msg)
8717c478bd9Sstevel@tonic-gate {
8727c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "sed: ");
8737c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, msg, linebuf);
8747c478bd9Sstevel@tonic-gate 	(void) putc('\n', stderr);
8757c478bd9Sstevel@tonic-gate 	exit(2);
8767c478bd9Sstevel@tonic-gate }
877