xref: /illumos-gate/usr/src/ucbcmd/sed/sed0.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984 AT&T	*/
23*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*7c478bd9Sstevel@tonic-gate 
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate /*
27*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1996, by Sun Microsystems, Inc.
28*7c478bd9Sstevel@tonic-gate  * All rights reserved.
29*7c478bd9Sstevel@tonic-gate  */
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #ident	"%Z%%M%	%I%	%E% SMI"	/* from S5R3.1 1.10 */
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #include <stdio.h>
34*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
35*7c478bd9Sstevel@tonic-gate #include "sed.h"
36*7c478bd9Sstevel@tonic-gate #define	NWFILES		11	/* 10 plus one for standard output */
37*7c478bd9Sstevel@tonic-gate FILE	*fin;
38*7c478bd9Sstevel@tonic-gate FILE    *fcode[NWFILES];
39*7c478bd9Sstevel@tonic-gate char    *lastre;
40*7c478bd9Sstevel@tonic-gate char    sseof;
41*7c478bd9Sstevel@tonic-gate union reptr     *ptrend;
42*7c478bd9Sstevel@tonic-gate int     eflag;
43*7c478bd9Sstevel@tonic-gate extern     nbra;
44*7c478bd9Sstevel@tonic-gate char    linebuf[LBSIZE+1];
45*7c478bd9Sstevel@tonic-gate int     gflag;
46*7c478bd9Sstevel@tonic-gate int     nlno;
47*7c478bd9Sstevel@tonic-gate char    *fname[NWFILES];
48*7c478bd9Sstevel@tonic-gate int     nfiles;
49*7c478bd9Sstevel@tonic-gate union reptr ptrspace[PTRSIZE];
50*7c478bd9Sstevel@tonic-gate union reptr *rep;
51*7c478bd9Sstevel@tonic-gate char    *cp;
52*7c478bd9Sstevel@tonic-gate char    respace[RESIZE];
53*7c478bd9Sstevel@tonic-gate struct label ltab[LABSIZE];
54*7c478bd9Sstevel@tonic-gate struct label    *lab;
55*7c478bd9Sstevel@tonic-gate struct label    *labend;
56*7c478bd9Sstevel@tonic-gate int     depth;
57*7c478bd9Sstevel@tonic-gate int     eargc;
58*7c478bd9Sstevel@tonic-gate char    **eargv;
59*7c478bd9Sstevel@tonic-gate union reptr     **cmpend[DEPTH];
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate #define CCEOF	22
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate struct label    *labtab = ltab;
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate char	ETMES[]		= "Extra text at end of command: %s";
66*7c478bd9Sstevel@tonic-gate char	SMMES[]		= "Space missing before filename: %s";
67*7c478bd9Sstevel@tonic-gate char    TMMES[]		= "Too much command text: %s";
68*7c478bd9Sstevel@tonic-gate char    LTL[]  		= "Label too long: %s";
69*7c478bd9Sstevel@tonic-gate char    AD0MES[]	= "No addresses allowed: %s";
70*7c478bd9Sstevel@tonic-gate char    AD1MES[]	= "Only one address allowed: %s";
71*7c478bd9Sstevel@tonic-gate char	TOOBIG[]	= "Suffix too large - 512 max: %s";
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate extern	sed;	  /* IMPORTANT flag !!! */
74*7c478bd9Sstevel@tonic-gate extern char *comple();
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate extern char *malloc();
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate main(argc, argv)
79*7c478bd9Sstevel@tonic-gate char    *argv[];
80*7c478bd9Sstevel@tonic-gate {
81*7c478bd9Sstevel@tonic-gate 	int flag_found = 0;
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate 	sed = 1;
84*7c478bd9Sstevel@tonic-gate 	eargc = argc;
85*7c478bd9Sstevel@tonic-gate 	eargv = argv;
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate 	aptr = abuf;
88*7c478bd9Sstevel@tonic-gate 	lab = labtab + 1;       /* 0 reserved for end-pointer */
89*7c478bd9Sstevel@tonic-gate 	rep = ptrspace;
90*7c478bd9Sstevel@tonic-gate 	rep->r1.ad1 = respace;
91*7c478bd9Sstevel@tonic-gate 	lcomend = &genbuf[71];
92*7c478bd9Sstevel@tonic-gate 	ptrend = &ptrspace[PTRSIZE];
93*7c478bd9Sstevel@tonic-gate 	labend = &labtab[LABSIZE];
94*7c478bd9Sstevel@tonic-gate 	lnum = 0;
95*7c478bd9Sstevel@tonic-gate 	pending = 0;
96*7c478bd9Sstevel@tonic-gate 	depth = 0;
97*7c478bd9Sstevel@tonic-gate 	spend = linebuf;
98*7c478bd9Sstevel@tonic-gate 	hspend = holdsp;	/* Avoid "bus error" under "H" cmd. */
99*7c478bd9Sstevel@tonic-gate 	fcode[0] = stdout;
100*7c478bd9Sstevel@tonic-gate 	fname[0] = "";
101*7c478bd9Sstevel@tonic-gate 	nfiles = 1;
102*7c478bd9Sstevel@tonic-gate 
103*7c478bd9Sstevel@tonic-gate 	if(eargc == 1)
104*7c478bd9Sstevel@tonic-gate 		exit(0);
105*7c478bd9Sstevel@tonic-gate 
106*7c478bd9Sstevel@tonic-gate 
107*7c478bd9Sstevel@tonic-gate 	setlocale(LC_ALL, "");		/* get locale environment */
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate 	while (--eargc > 0 && (++eargv)[0][0] == '-')
110*7c478bd9Sstevel@tonic-gate 		switch (eargv[0][1]) {
111*7c478bd9Sstevel@tonic-gate 
112*7c478bd9Sstevel@tonic-gate 		case 'n':
113*7c478bd9Sstevel@tonic-gate 			nflag++;
114*7c478bd9Sstevel@tonic-gate 			continue;
115*7c478bd9Sstevel@tonic-gate 
116*7c478bd9Sstevel@tonic-gate 		case 'f':
117*7c478bd9Sstevel@tonic-gate 			flag_found = 1;
118*7c478bd9Sstevel@tonic-gate 			if(eargc-- <= 0)	exit(2);
119*7c478bd9Sstevel@tonic-gate 
120*7c478bd9Sstevel@tonic-gate 			if((fin = fopen(*++eargv, "r")) == NULL) {
121*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, "sed: ");
122*7c478bd9Sstevel@tonic-gate 				perror(*eargv);
123*7c478bd9Sstevel@tonic-gate 				exit(2);
124*7c478bd9Sstevel@tonic-gate 			}
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate 			fcomp();
127*7c478bd9Sstevel@tonic-gate 			(void) fclose(fin);
128*7c478bd9Sstevel@tonic-gate 			continue;
129*7c478bd9Sstevel@tonic-gate 
130*7c478bd9Sstevel@tonic-gate 		case 'e':
131*7c478bd9Sstevel@tonic-gate 			flag_found = 1;
132*7c478bd9Sstevel@tonic-gate 			eflag++;
133*7c478bd9Sstevel@tonic-gate 			fcomp();
134*7c478bd9Sstevel@tonic-gate 			eflag = 0;
135*7c478bd9Sstevel@tonic-gate 			continue;
136*7c478bd9Sstevel@tonic-gate 
137*7c478bd9Sstevel@tonic-gate 		case 'g':
138*7c478bd9Sstevel@tonic-gate 			gflag++;
139*7c478bd9Sstevel@tonic-gate 			continue;
140*7c478bd9Sstevel@tonic-gate 
141*7c478bd9Sstevel@tonic-gate 		default:
142*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "sed: Unknown flag: %c\n", eargv[0][1]);
143*7c478bd9Sstevel@tonic-gate 			exit(2);
144*7c478bd9Sstevel@tonic-gate 		}
145*7c478bd9Sstevel@tonic-gate 
146*7c478bd9Sstevel@tonic-gate 
147*7c478bd9Sstevel@tonic-gate 	if(rep == ptrspace && !flag_found) {
148*7c478bd9Sstevel@tonic-gate 		eargv--;
149*7c478bd9Sstevel@tonic-gate 		eargc++;
150*7c478bd9Sstevel@tonic-gate 		eflag++;
151*7c478bd9Sstevel@tonic-gate 		fcomp();
152*7c478bd9Sstevel@tonic-gate 		eargv++;
153*7c478bd9Sstevel@tonic-gate 		eargc--;
154*7c478bd9Sstevel@tonic-gate 		eflag = 0;
155*7c478bd9Sstevel@tonic-gate 	}
156*7c478bd9Sstevel@tonic-gate 
157*7c478bd9Sstevel@tonic-gate 	if(depth)
158*7c478bd9Sstevel@tonic-gate 		comperr("Too many {'s");
159*7c478bd9Sstevel@tonic-gate 
160*7c478bd9Sstevel@tonic-gate 	labtab->address = rep;
161*7c478bd9Sstevel@tonic-gate 
162*7c478bd9Sstevel@tonic-gate 	dechain();
163*7c478bd9Sstevel@tonic-gate 
164*7c478bd9Sstevel@tonic-gate 	if(eargc <= 0)
165*7c478bd9Sstevel@tonic-gate 		execute((char *)NULL);
166*7c478bd9Sstevel@tonic-gate 	else while(--eargc >= 0) {
167*7c478bd9Sstevel@tonic-gate 		execute(*eargv++);
168*7c478bd9Sstevel@tonic-gate 	}
169*7c478bd9Sstevel@tonic-gate 	(void) fclose(stdout);
170*7c478bd9Sstevel@tonic-gate 	exit(0);
171*7c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
172*7c478bd9Sstevel@tonic-gate }
173*7c478bd9Sstevel@tonic-gate 
174*7c478bd9Sstevel@tonic-gate fcomp()
175*7c478bd9Sstevel@tonic-gate {
176*7c478bd9Sstevel@tonic-gate 
177*7c478bd9Sstevel@tonic-gate 	register char   *p, *op, *tp;
178*7c478bd9Sstevel@tonic-gate 	char    *address();
179*7c478bd9Sstevel@tonic-gate 	union reptr     *pt, *pt1;
180*7c478bd9Sstevel@tonic-gate 	int     i, ii;
181*7c478bd9Sstevel@tonic-gate 	struct label    *lpt;
182*7c478bd9Sstevel@tonic-gate 	char fnamebuf[MAXPATHLEN];
183*7c478bd9Sstevel@tonic-gate 
184*7c478bd9Sstevel@tonic-gate 	op = lastre;
185*7c478bd9Sstevel@tonic-gate 
186*7c478bd9Sstevel@tonic-gate 	if(rline(linebuf, &linebuf[LBSIZE+1]) < 0)  return;
187*7c478bd9Sstevel@tonic-gate 	if(*linebuf == '#') {
188*7c478bd9Sstevel@tonic-gate 		if(linebuf[1] == 'n')
189*7c478bd9Sstevel@tonic-gate 			nflag = 1;
190*7c478bd9Sstevel@tonic-gate 	}
191*7c478bd9Sstevel@tonic-gate 	else {
192*7c478bd9Sstevel@tonic-gate 		cp = linebuf;
193*7c478bd9Sstevel@tonic-gate 		goto comploop;
194*7c478bd9Sstevel@tonic-gate 	}
195*7c478bd9Sstevel@tonic-gate 
196*7c478bd9Sstevel@tonic-gate 	for(;;) {
197*7c478bd9Sstevel@tonic-gate 		if(rline(linebuf, &linebuf[LBSIZE+1]) < 0)  break;
198*7c478bd9Sstevel@tonic-gate 
199*7c478bd9Sstevel@tonic-gate 		cp = linebuf;
200*7c478bd9Sstevel@tonic-gate 
201*7c478bd9Sstevel@tonic-gate comploop:
202*7c478bd9Sstevel@tonic-gate /*      (void) fprintf(stderr, "cp: %s\n", cp);	/*DEBUG*/
203*7c478bd9Sstevel@tonic-gate 		while(*cp == ' ' || *cp == '\t')	cp++;
204*7c478bd9Sstevel@tonic-gate 		if(*cp == '\0' || *cp == '#')	 continue;
205*7c478bd9Sstevel@tonic-gate 		if(*cp == ';') {
206*7c478bd9Sstevel@tonic-gate 			cp++;
207*7c478bd9Sstevel@tonic-gate 			goto comploop;
208*7c478bd9Sstevel@tonic-gate 		}
209*7c478bd9Sstevel@tonic-gate 
210*7c478bd9Sstevel@tonic-gate 		p = address(rep->r1.ad1);
211*7c478bd9Sstevel@tonic-gate 
212*7c478bd9Sstevel@tonic-gate 		if(p == rep->r1.ad1) {
213*7c478bd9Sstevel@tonic-gate 			if(op)
214*7c478bd9Sstevel@tonic-gate 				rep->r1.ad1 = op;
215*7c478bd9Sstevel@tonic-gate 			else
216*7c478bd9Sstevel@tonic-gate 				comperr("First RE may not be null: %s");
217*7c478bd9Sstevel@tonic-gate 		} else if(p == 0) {
218*7c478bd9Sstevel@tonic-gate 			p = rep->r1.ad1;
219*7c478bd9Sstevel@tonic-gate 			rep->r1.ad1 = 0;
220*7c478bd9Sstevel@tonic-gate 		} else {
221*7c478bd9Sstevel@tonic-gate 			op = rep->r1.ad1;
222*7c478bd9Sstevel@tonic-gate 			if(*cp == ',' || *cp == ';') {
223*7c478bd9Sstevel@tonic-gate 				cp++;
224*7c478bd9Sstevel@tonic-gate 				rep->r1.ad2 = p;
225*7c478bd9Sstevel@tonic-gate 				p = address(rep->r1.ad2);
226*7c478bd9Sstevel@tonic-gate 				if(p == 0)
227*7c478bd9Sstevel@tonic-gate 					comperr("Illegal line number: %s");
228*7c478bd9Sstevel@tonic-gate 				if(p == rep->r1.ad2)
229*7c478bd9Sstevel@tonic-gate 					rep->r1.ad2 = op;
230*7c478bd9Sstevel@tonic-gate 				else
231*7c478bd9Sstevel@tonic-gate 					op = rep->r1.ad2;
232*7c478bd9Sstevel@tonic-gate 
233*7c478bd9Sstevel@tonic-gate 			} else
234*7c478bd9Sstevel@tonic-gate 				rep->r1.ad2 = 0;
235*7c478bd9Sstevel@tonic-gate 		}
236*7c478bd9Sstevel@tonic-gate 
237*7c478bd9Sstevel@tonic-gate 		if(p > &respace[RESIZE-1])
238*7c478bd9Sstevel@tonic-gate 			comperr(TMMES);
239*7c478bd9Sstevel@tonic-gate 
240*7c478bd9Sstevel@tonic-gate 		while(*cp == ' ' || *cp == '\t')	cp++;
241*7c478bd9Sstevel@tonic-gate 
242*7c478bd9Sstevel@tonic-gate swit:
243*7c478bd9Sstevel@tonic-gate 		switch(*cp++) {
244*7c478bd9Sstevel@tonic-gate 
245*7c478bd9Sstevel@tonic-gate 			default:
246*7c478bd9Sstevel@tonic-gate 				comperr("Unrecognized command: %s");
247*7c478bd9Sstevel@tonic-gate 
248*7c478bd9Sstevel@tonic-gate 			case '!':
249*7c478bd9Sstevel@tonic-gate 				rep->r1.negfl = 1;
250*7c478bd9Sstevel@tonic-gate 				goto swit;
251*7c478bd9Sstevel@tonic-gate 
252*7c478bd9Sstevel@tonic-gate 			case '{':
253*7c478bd9Sstevel@tonic-gate 				rep->r1.command = BCOM;
254*7c478bd9Sstevel@tonic-gate 				rep->r1.negfl = !(rep->r1.negfl);
255*7c478bd9Sstevel@tonic-gate 				cmpend[depth++] = &rep->r2.lb1;
256*7c478bd9Sstevel@tonic-gate 				if(++rep >= ptrend)
257*7c478bd9Sstevel@tonic-gate 					comperr("Too many commands: %s");
258*7c478bd9Sstevel@tonic-gate 				rep->r1.ad1 = p;
259*7c478bd9Sstevel@tonic-gate 				if(*cp == '\0') continue;
260*7c478bd9Sstevel@tonic-gate 
261*7c478bd9Sstevel@tonic-gate 				goto comploop;
262*7c478bd9Sstevel@tonic-gate 
263*7c478bd9Sstevel@tonic-gate 			case '}':
264*7c478bd9Sstevel@tonic-gate 				if(rep->r1.ad1)
265*7c478bd9Sstevel@tonic-gate 					comperr(AD0MES);
266*7c478bd9Sstevel@tonic-gate 
267*7c478bd9Sstevel@tonic-gate 				if(--depth < 0)
268*7c478bd9Sstevel@tonic-gate 					comperr("Too many }'s");
269*7c478bd9Sstevel@tonic-gate 				*cmpend[depth] = rep;
270*7c478bd9Sstevel@tonic-gate 
271*7c478bd9Sstevel@tonic-gate 				rep->r1.ad1 = p;
272*7c478bd9Sstevel@tonic-gate 				continue;
273*7c478bd9Sstevel@tonic-gate 
274*7c478bd9Sstevel@tonic-gate 			case '=':
275*7c478bd9Sstevel@tonic-gate 				rep->r1.command = EQCOM;
276*7c478bd9Sstevel@tonic-gate 				if(rep->r1.ad2)
277*7c478bd9Sstevel@tonic-gate 					comperr(AD1MES);
278*7c478bd9Sstevel@tonic-gate 				break;
279*7c478bd9Sstevel@tonic-gate 
280*7c478bd9Sstevel@tonic-gate 			case ':':
281*7c478bd9Sstevel@tonic-gate 				if(rep->r1.ad1)
282*7c478bd9Sstevel@tonic-gate 					comperr(AD0MES);
283*7c478bd9Sstevel@tonic-gate 
284*7c478bd9Sstevel@tonic-gate 				while(*cp++ == ' ');
285*7c478bd9Sstevel@tonic-gate 				cp--;
286*7c478bd9Sstevel@tonic-gate 
287*7c478bd9Sstevel@tonic-gate 
288*7c478bd9Sstevel@tonic-gate 				tp = lab->asc;
289*7c478bd9Sstevel@tonic-gate 				while((*tp++ = *cp++))
290*7c478bd9Sstevel@tonic-gate 					if(tp >= &(lab->asc[8]))
291*7c478bd9Sstevel@tonic-gate 						comperr(LTL);
292*7c478bd9Sstevel@tonic-gate 				*--tp = '\0';
293*7c478bd9Sstevel@tonic-gate 
294*7c478bd9Sstevel@tonic-gate 				if(lpt = search(lab)) {
295*7c478bd9Sstevel@tonic-gate 					if(lpt->address)
296*7c478bd9Sstevel@tonic-gate 						comperr("Duplicate labels: %s");
297*7c478bd9Sstevel@tonic-gate 				} else {
298*7c478bd9Sstevel@tonic-gate 					lab->chain = 0;
299*7c478bd9Sstevel@tonic-gate 					lpt = lab;
300*7c478bd9Sstevel@tonic-gate 					if(++lab >= labend)
301*7c478bd9Sstevel@tonic-gate 						comperr("Too many labels: %s");
302*7c478bd9Sstevel@tonic-gate 				}
303*7c478bd9Sstevel@tonic-gate 				lpt->address = rep;
304*7c478bd9Sstevel@tonic-gate 				rep->r1.ad1 = p;
305*7c478bd9Sstevel@tonic-gate 
306*7c478bd9Sstevel@tonic-gate 				continue;
307*7c478bd9Sstevel@tonic-gate 
308*7c478bd9Sstevel@tonic-gate 			case 'a':
309*7c478bd9Sstevel@tonic-gate 				rep->r1.command = ACOM;
310*7c478bd9Sstevel@tonic-gate 				if(rep->r1.ad2)
311*7c478bd9Sstevel@tonic-gate 					comperr(AD1MES);
312*7c478bd9Sstevel@tonic-gate 				if(*cp == '\\') cp++;
313*7c478bd9Sstevel@tonic-gate 				if(*cp++ != '\n')
314*7c478bd9Sstevel@tonic-gate 					comperr(ETMES);
315*7c478bd9Sstevel@tonic-gate 				rep->r1.re1 = p;
316*7c478bd9Sstevel@tonic-gate 				if ((p = text(rep->r1.re1, &respace[RESIZE-1])) == NULL)
317*7c478bd9Sstevel@tonic-gate 					comperr(TMMES);
318*7c478bd9Sstevel@tonic-gate 				break;
319*7c478bd9Sstevel@tonic-gate 			case 'c':
320*7c478bd9Sstevel@tonic-gate 				rep->r1.command = CCOM;
321*7c478bd9Sstevel@tonic-gate 				if(*cp == '\\') cp++;
322*7c478bd9Sstevel@tonic-gate 				if(*cp++ != ('\n'))
323*7c478bd9Sstevel@tonic-gate 					comperr(ETMES);
324*7c478bd9Sstevel@tonic-gate 				rep->r1.re1 = p;
325*7c478bd9Sstevel@tonic-gate 				if ((p = text(rep->r1.re1, &respace[RESIZE-1])) == NULL)
326*7c478bd9Sstevel@tonic-gate 					comperr(TMMES);
327*7c478bd9Sstevel@tonic-gate 				break;
328*7c478bd9Sstevel@tonic-gate 			case 'i':
329*7c478bd9Sstevel@tonic-gate 				rep->r1.command = ICOM;
330*7c478bd9Sstevel@tonic-gate 				if(rep->r1.ad2)
331*7c478bd9Sstevel@tonic-gate 					comperr(AD1MES);
332*7c478bd9Sstevel@tonic-gate 				if(*cp == '\\') cp++;
333*7c478bd9Sstevel@tonic-gate 				if(*cp++ != ('\n'))
334*7c478bd9Sstevel@tonic-gate 					comperr(ETMES);
335*7c478bd9Sstevel@tonic-gate 				rep->r1.re1 = p;
336*7c478bd9Sstevel@tonic-gate 				if ((p = text(rep->r1.re1, &respace[RESIZE-1])) == NULL)
337*7c478bd9Sstevel@tonic-gate 					comperr(TMMES);
338*7c478bd9Sstevel@tonic-gate 				break;
339*7c478bd9Sstevel@tonic-gate 
340*7c478bd9Sstevel@tonic-gate 			case 'g':
341*7c478bd9Sstevel@tonic-gate 				rep->r1.command = GCOM;
342*7c478bd9Sstevel@tonic-gate 				break;
343*7c478bd9Sstevel@tonic-gate 
344*7c478bd9Sstevel@tonic-gate 			case 'G':
345*7c478bd9Sstevel@tonic-gate 				rep->r1.command = CGCOM;
346*7c478bd9Sstevel@tonic-gate 				break;
347*7c478bd9Sstevel@tonic-gate 
348*7c478bd9Sstevel@tonic-gate 			case 'h':
349*7c478bd9Sstevel@tonic-gate 				rep->r1.command = HCOM;
350*7c478bd9Sstevel@tonic-gate 				break;
351*7c478bd9Sstevel@tonic-gate 
352*7c478bd9Sstevel@tonic-gate 			case 'H':
353*7c478bd9Sstevel@tonic-gate 				rep->r1.command = CHCOM;
354*7c478bd9Sstevel@tonic-gate 				break;
355*7c478bd9Sstevel@tonic-gate 
356*7c478bd9Sstevel@tonic-gate 			case 't':
357*7c478bd9Sstevel@tonic-gate 				rep->r1.command = TCOM;
358*7c478bd9Sstevel@tonic-gate 				goto jtcommon;
359*7c478bd9Sstevel@tonic-gate 
360*7c478bd9Sstevel@tonic-gate 			case 'b':
361*7c478bd9Sstevel@tonic-gate 				rep->r1.command = BCOM;
362*7c478bd9Sstevel@tonic-gate jtcommon:
363*7c478bd9Sstevel@tonic-gate 				while(*cp++ == ' ');
364*7c478bd9Sstevel@tonic-gate 				cp--;
365*7c478bd9Sstevel@tonic-gate 
366*7c478bd9Sstevel@tonic-gate 				if(*cp == '\0') {
367*7c478bd9Sstevel@tonic-gate 					if(pt = labtab->chain) {
368*7c478bd9Sstevel@tonic-gate 						while(pt1 = pt->r2.lb1)
369*7c478bd9Sstevel@tonic-gate 							pt = pt1;
370*7c478bd9Sstevel@tonic-gate 						pt->r2.lb1 = rep;
371*7c478bd9Sstevel@tonic-gate 					} else
372*7c478bd9Sstevel@tonic-gate 						labtab->chain = rep;
373*7c478bd9Sstevel@tonic-gate 					break;
374*7c478bd9Sstevel@tonic-gate 				}
375*7c478bd9Sstevel@tonic-gate 				tp = lab->asc;
376*7c478bd9Sstevel@tonic-gate 				while((*tp++ = *cp++))
377*7c478bd9Sstevel@tonic-gate 					if(tp >= &(lab->asc[8]))
378*7c478bd9Sstevel@tonic-gate 						comperr(LTL);
379*7c478bd9Sstevel@tonic-gate 				cp--;
380*7c478bd9Sstevel@tonic-gate 				*--tp = '\0';
381*7c478bd9Sstevel@tonic-gate 
382*7c478bd9Sstevel@tonic-gate 				if(lpt = search(lab)) {
383*7c478bd9Sstevel@tonic-gate 					if(lpt->address) {
384*7c478bd9Sstevel@tonic-gate 						rep->r2.lb1 = lpt->address;
385*7c478bd9Sstevel@tonic-gate 					} else {
386*7c478bd9Sstevel@tonic-gate 						pt = lpt->chain;
387*7c478bd9Sstevel@tonic-gate 						while(pt1 = pt->r2.lb1)
388*7c478bd9Sstevel@tonic-gate 							pt = pt1;
389*7c478bd9Sstevel@tonic-gate 						pt->r2.lb1 = rep;
390*7c478bd9Sstevel@tonic-gate 					}
391*7c478bd9Sstevel@tonic-gate 				} else {
392*7c478bd9Sstevel@tonic-gate 					lab->chain = rep;
393*7c478bd9Sstevel@tonic-gate 					lab->address = 0;
394*7c478bd9Sstevel@tonic-gate 					if(++lab >= labend)
395*7c478bd9Sstevel@tonic-gate 						comperr("Too many labels: %s");
396*7c478bd9Sstevel@tonic-gate 				}
397*7c478bd9Sstevel@tonic-gate 				break;
398*7c478bd9Sstevel@tonic-gate 
399*7c478bd9Sstevel@tonic-gate 			case 'n':
400*7c478bd9Sstevel@tonic-gate 				rep->r1.command = NCOM;
401*7c478bd9Sstevel@tonic-gate 				break;
402*7c478bd9Sstevel@tonic-gate 
403*7c478bd9Sstevel@tonic-gate 			case 'N':
404*7c478bd9Sstevel@tonic-gate 				rep->r1.command = CNCOM;
405*7c478bd9Sstevel@tonic-gate 				break;
406*7c478bd9Sstevel@tonic-gate 
407*7c478bd9Sstevel@tonic-gate 			case 'p':
408*7c478bd9Sstevel@tonic-gate 				rep->r1.command = PCOM;
409*7c478bd9Sstevel@tonic-gate 				break;
410*7c478bd9Sstevel@tonic-gate 
411*7c478bd9Sstevel@tonic-gate 			case 'P':
412*7c478bd9Sstevel@tonic-gate 				rep->r1.command = CPCOM;
413*7c478bd9Sstevel@tonic-gate 				break;
414*7c478bd9Sstevel@tonic-gate 
415*7c478bd9Sstevel@tonic-gate 			case 'r':
416*7c478bd9Sstevel@tonic-gate 				rep->r1.command = RCOM;
417*7c478bd9Sstevel@tonic-gate 				if(rep->r1.ad2)
418*7c478bd9Sstevel@tonic-gate 					comperr(AD1MES);
419*7c478bd9Sstevel@tonic-gate 				if(*cp++ != ' ')
420*7c478bd9Sstevel@tonic-gate 					comperr(SMMES);
421*7c478bd9Sstevel@tonic-gate 				rep->r1.re1 = p;
422*7c478bd9Sstevel@tonic-gate 				if ((p = text(rep->r1.re1, &respace[RESIZE-1])) == NULL)
423*7c478bd9Sstevel@tonic-gate 					comperr(TMMES);
424*7c478bd9Sstevel@tonic-gate 				break;
425*7c478bd9Sstevel@tonic-gate 
426*7c478bd9Sstevel@tonic-gate 			case 'd':
427*7c478bd9Sstevel@tonic-gate 				rep->r1.command = DCOM;
428*7c478bd9Sstevel@tonic-gate 				break;
429*7c478bd9Sstevel@tonic-gate 
430*7c478bd9Sstevel@tonic-gate 			case 'D':
431*7c478bd9Sstevel@tonic-gate 				rep->r1.command = CDCOM;
432*7c478bd9Sstevel@tonic-gate 				rep->r2.lb1 = ptrspace;
433*7c478bd9Sstevel@tonic-gate 				break;
434*7c478bd9Sstevel@tonic-gate 
435*7c478bd9Sstevel@tonic-gate 			case 'q':
436*7c478bd9Sstevel@tonic-gate 				rep->r1.command = QCOM;
437*7c478bd9Sstevel@tonic-gate 				if(rep->r1.ad2)
438*7c478bd9Sstevel@tonic-gate 					comperr(AD1MES);
439*7c478bd9Sstevel@tonic-gate 				break;
440*7c478bd9Sstevel@tonic-gate 
441*7c478bd9Sstevel@tonic-gate 			case 'l':
442*7c478bd9Sstevel@tonic-gate 				rep->r1.command = LCOM;
443*7c478bd9Sstevel@tonic-gate 				break;
444*7c478bd9Sstevel@tonic-gate 
445*7c478bd9Sstevel@tonic-gate 			case 's':
446*7c478bd9Sstevel@tonic-gate 				rep->r1.command = SCOM;
447*7c478bd9Sstevel@tonic-gate 				sseof = *cp++;
448*7c478bd9Sstevel@tonic-gate 				rep->r1.re1 = p;
449*7c478bd9Sstevel@tonic-gate 				p = comple((char *) 0, rep->r1.re1, &respace[RESIZE-1], sseof);
450*7c478bd9Sstevel@tonic-gate 				if(p == rep->r1.re1) {
451*7c478bd9Sstevel@tonic-gate 					if(op)
452*7c478bd9Sstevel@tonic-gate 						rep->r1.re1 = op;
453*7c478bd9Sstevel@tonic-gate 					else
454*7c478bd9Sstevel@tonic-gate 						comperr("First RE may not be null: %s");
455*7c478bd9Sstevel@tonic-gate 				} else
456*7c478bd9Sstevel@tonic-gate 					op = rep->r1.re1;
457*7c478bd9Sstevel@tonic-gate 				rep->r1.rhs = p;
458*7c478bd9Sstevel@tonic-gate 
459*7c478bd9Sstevel@tonic-gate 				p = compsub(rep->r1.rhs);
460*7c478bd9Sstevel@tonic-gate 
461*7c478bd9Sstevel@tonic-gate 				if(*cp == 'g') {
462*7c478bd9Sstevel@tonic-gate 					cp++;
463*7c478bd9Sstevel@tonic-gate 					rep->r1.gfl = 999;
464*7c478bd9Sstevel@tonic-gate 				} else if(gflag)
465*7c478bd9Sstevel@tonic-gate 					rep->r1.gfl = 999;
466*7c478bd9Sstevel@tonic-gate 
467*7c478bd9Sstevel@tonic-gate 				if(*cp >= '1' && *cp <= '9')
468*7c478bd9Sstevel@tonic-gate 					{i = *cp - '0';
469*7c478bd9Sstevel@tonic-gate 					cp++;
470*7c478bd9Sstevel@tonic-gate 					while(1)
471*7c478bd9Sstevel@tonic-gate 						{ii = *cp;
472*7c478bd9Sstevel@tonic-gate 						if(ii < '0' || ii > '9') break;
473*7c478bd9Sstevel@tonic-gate 						i = i*10 + ii - '0';
474*7c478bd9Sstevel@tonic-gate 						if(i > 512)
475*7c478bd9Sstevel@tonic-gate 							comperr(TOOBIG);
476*7c478bd9Sstevel@tonic-gate 						cp++;
477*7c478bd9Sstevel@tonic-gate 						}
478*7c478bd9Sstevel@tonic-gate 					rep->r1.gfl = i;
479*7c478bd9Sstevel@tonic-gate 					}
480*7c478bd9Sstevel@tonic-gate 
481*7c478bd9Sstevel@tonic-gate 				if(*cp == 'p') {
482*7c478bd9Sstevel@tonic-gate 					cp++;
483*7c478bd9Sstevel@tonic-gate 					rep->r1.pfl = 1;
484*7c478bd9Sstevel@tonic-gate 				}
485*7c478bd9Sstevel@tonic-gate 
486*7c478bd9Sstevel@tonic-gate 				if(*cp == 'P') {
487*7c478bd9Sstevel@tonic-gate 					cp++;
488*7c478bd9Sstevel@tonic-gate 					rep->r1.pfl = 2;
489*7c478bd9Sstevel@tonic-gate 				}
490*7c478bd9Sstevel@tonic-gate 
491*7c478bd9Sstevel@tonic-gate 				if(*cp == 'w') {
492*7c478bd9Sstevel@tonic-gate 					cp++;
493*7c478bd9Sstevel@tonic-gate 					if(*cp++ !=  ' ')
494*7c478bd9Sstevel@tonic-gate 						comperr(SMMES);
495*7c478bd9Sstevel@tonic-gate 					if (text(fnamebuf, &fnamebuf[MAXPATHLEN]) == NULL)
496*7c478bd9Sstevel@tonic-gate 						comperr("File name too long: %s");
497*7c478bd9Sstevel@tonic-gate 					for(i = nfiles - 1; i >= 0; i--)
498*7c478bd9Sstevel@tonic-gate 						if(strcmp(fnamebuf,fname[i]) == 0) {
499*7c478bd9Sstevel@tonic-gate 							rep->r1.fcode = fcode[i];
500*7c478bd9Sstevel@tonic-gate 							goto done;
501*7c478bd9Sstevel@tonic-gate 						}
502*7c478bd9Sstevel@tonic-gate 					if(nfiles >= NWFILES)
503*7c478bd9Sstevel@tonic-gate 						comperr("Too many files in w commands: %s");
504*7c478bd9Sstevel@tonic-gate 
505*7c478bd9Sstevel@tonic-gate 					i = strlen(fnamebuf) + 1;
506*7c478bd9Sstevel@tonic-gate 					if ((fname[nfiles] = malloc((unsigned)i)) == NULL) {
507*7c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr, "sed: Out of memory\n");
508*7c478bd9Sstevel@tonic-gate 						exit(2);
509*7c478bd9Sstevel@tonic-gate 					}
510*7c478bd9Sstevel@tonic-gate 					(void) strcpy(fname[nfiles], fnamebuf);
511*7c478bd9Sstevel@tonic-gate 					if((rep->r1.fcode = fopen(fname[nfiles], "w")) == NULL) {
512*7c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr, "sed: Cannot open ");
513*7c478bd9Sstevel@tonic-gate 						perror(fname[nfiles]);
514*7c478bd9Sstevel@tonic-gate 						exit(2);
515*7c478bd9Sstevel@tonic-gate 					}
516*7c478bd9Sstevel@tonic-gate 					fcode[nfiles++] = rep->r1.fcode;
517*7c478bd9Sstevel@tonic-gate 				}
518*7c478bd9Sstevel@tonic-gate 				break;
519*7c478bd9Sstevel@tonic-gate 
520*7c478bd9Sstevel@tonic-gate 			case 'w':
521*7c478bd9Sstevel@tonic-gate 				rep->r1.command = WCOM;
522*7c478bd9Sstevel@tonic-gate 				if(*cp++ != ' ')
523*7c478bd9Sstevel@tonic-gate 					comperr(SMMES);
524*7c478bd9Sstevel@tonic-gate 				if (text(fnamebuf, &fnamebuf[MAXPATHLEN]) == NULL)
525*7c478bd9Sstevel@tonic-gate 					comperr("File name too long: %s");
526*7c478bd9Sstevel@tonic-gate 				for(i = nfiles - 1; i >= 0; i--)
527*7c478bd9Sstevel@tonic-gate 					if(strcmp(fnamebuf, fname[i]) == 0) {
528*7c478bd9Sstevel@tonic-gate 						rep->r1.fcode = fcode[i];
529*7c478bd9Sstevel@tonic-gate 						goto done;
530*7c478bd9Sstevel@tonic-gate 					}
531*7c478bd9Sstevel@tonic-gate 				if(nfiles >= NWFILES)
532*7c478bd9Sstevel@tonic-gate 					comperr("Too many files in w commands: %s");
533*7c478bd9Sstevel@tonic-gate 
534*7c478bd9Sstevel@tonic-gate 				i = strlen(fnamebuf) + 1;
535*7c478bd9Sstevel@tonic-gate 				if ((fname[nfiles] = malloc((unsigned)i)) == NULL) {
536*7c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, "sed: Out of memory\n");
537*7c478bd9Sstevel@tonic-gate 					exit(2);
538*7c478bd9Sstevel@tonic-gate 				}
539*7c478bd9Sstevel@tonic-gate 				(void) strcpy(fname[nfiles], fnamebuf);
540*7c478bd9Sstevel@tonic-gate 				if((rep->r1.fcode = fopen(fname[nfiles], "w")) == NULL) {
541*7c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, "sed: Cannot create ");
542*7c478bd9Sstevel@tonic-gate 					perror(fname[nfiles]);
543*7c478bd9Sstevel@tonic-gate 					exit(2);
544*7c478bd9Sstevel@tonic-gate 				}
545*7c478bd9Sstevel@tonic-gate 				fcode[nfiles++] = rep->r1.fcode;
546*7c478bd9Sstevel@tonic-gate 				break;
547*7c478bd9Sstevel@tonic-gate 
548*7c478bd9Sstevel@tonic-gate 			case 'x':
549*7c478bd9Sstevel@tonic-gate 				rep->r1.command = XCOM;
550*7c478bd9Sstevel@tonic-gate 				break;
551*7c478bd9Sstevel@tonic-gate 
552*7c478bd9Sstevel@tonic-gate 			case 'y':
553*7c478bd9Sstevel@tonic-gate 				rep->r1.command = YCOM;
554*7c478bd9Sstevel@tonic-gate 				sseof = *cp++;
555*7c478bd9Sstevel@tonic-gate 				rep->r1.re1 = p;
556*7c478bd9Sstevel@tonic-gate 				p = ycomp(rep->r1.re1);
557*7c478bd9Sstevel@tonic-gate 				break;
558*7c478bd9Sstevel@tonic-gate 
559*7c478bd9Sstevel@tonic-gate 		}
560*7c478bd9Sstevel@tonic-gate done:
561*7c478bd9Sstevel@tonic-gate 		if(++rep >= ptrend)
562*7c478bd9Sstevel@tonic-gate 			comperr("Too many commands, last: %s");
563*7c478bd9Sstevel@tonic-gate 
564*7c478bd9Sstevel@tonic-gate 		rep->r1.ad1 = p;
565*7c478bd9Sstevel@tonic-gate 
566*7c478bd9Sstevel@tonic-gate 		if(*cp++ != '\0') {
567*7c478bd9Sstevel@tonic-gate 			if(cp[-1] == ';')
568*7c478bd9Sstevel@tonic-gate 				goto comploop;
569*7c478bd9Sstevel@tonic-gate 			comperr(ETMES);
570*7c478bd9Sstevel@tonic-gate 		}
571*7c478bd9Sstevel@tonic-gate 	}
572*7c478bd9Sstevel@tonic-gate 	rep->r1.command = 0;
573*7c478bd9Sstevel@tonic-gate 	lastre = op;
574*7c478bd9Sstevel@tonic-gate }
575*7c478bd9Sstevel@tonic-gate char    *compsub(rhsbuf)
576*7c478bd9Sstevel@tonic-gate char    *rhsbuf;
577*7c478bd9Sstevel@tonic-gate {
578*7c478bd9Sstevel@tonic-gate 	register char   *p, *q;
579*7c478bd9Sstevel@tonic-gate 
580*7c478bd9Sstevel@tonic-gate 	p = rhsbuf;
581*7c478bd9Sstevel@tonic-gate 	q = cp;
582*7c478bd9Sstevel@tonic-gate 	for(;;) {
583*7c478bd9Sstevel@tonic-gate 		if(p > &respace[RESIZE-1])
584*7c478bd9Sstevel@tonic-gate 			comperr(TMMES);
585*7c478bd9Sstevel@tonic-gate 		if((*p = *q++) == '\\') {
586*7c478bd9Sstevel@tonic-gate 			p++;
587*7c478bd9Sstevel@tonic-gate 			if(p > &respace[RESIZE-1])
588*7c478bd9Sstevel@tonic-gate 				comperr(TMMES);
589*7c478bd9Sstevel@tonic-gate 			*p = *q++;
590*7c478bd9Sstevel@tonic-gate 			if(*p > nbra + '0' && *p <= '9')
591*7c478bd9Sstevel@tonic-gate 				comperr("``\\digit'' out of range: %s");
592*7c478bd9Sstevel@tonic-gate 			p++;
593*7c478bd9Sstevel@tonic-gate 			continue;
594*7c478bd9Sstevel@tonic-gate 		}
595*7c478bd9Sstevel@tonic-gate 		if(*p == sseof) {
596*7c478bd9Sstevel@tonic-gate 			*p++ = '\0';
597*7c478bd9Sstevel@tonic-gate 			cp = q;
598*7c478bd9Sstevel@tonic-gate 			return(p);
599*7c478bd9Sstevel@tonic-gate 		}
600*7c478bd9Sstevel@tonic-gate   		if(*p++ == '\0')
601*7c478bd9Sstevel@tonic-gate 			comperr("Ending delimiter missing on substitution: %s");
602*7c478bd9Sstevel@tonic-gate 
603*7c478bd9Sstevel@tonic-gate 	}
604*7c478bd9Sstevel@tonic-gate }
605*7c478bd9Sstevel@tonic-gate 
606*7c478bd9Sstevel@tonic-gate rline(lbuf, lbend)
607*7c478bd9Sstevel@tonic-gate char    *lbuf;
608*7c478bd9Sstevel@tonic-gate char	*lbend;
609*7c478bd9Sstevel@tonic-gate {
610*7c478bd9Sstevel@tonic-gate 	register char   *p, *q;
611*7c478bd9Sstevel@tonic-gate 	register	t;
612*7c478bd9Sstevel@tonic-gate 	static char     *saveq;
613*7c478bd9Sstevel@tonic-gate 
614*7c478bd9Sstevel@tonic-gate 	p = lbuf;
615*7c478bd9Sstevel@tonic-gate 
616*7c478bd9Sstevel@tonic-gate 	if(eflag) {
617*7c478bd9Sstevel@tonic-gate 		if(eflag > 0) {
618*7c478bd9Sstevel@tonic-gate 			eflag = -1;
619*7c478bd9Sstevel@tonic-gate 			if(--eargc <= 0)
620*7c478bd9Sstevel@tonic-gate 				exit(2);
621*7c478bd9Sstevel@tonic-gate 			q = *++eargv;
622*7c478bd9Sstevel@tonic-gate 			while((t = *q++) != '\0') {
623*7c478bd9Sstevel@tonic-gate 				if(t == '\n') {
624*7c478bd9Sstevel@tonic-gate 					saveq = q;
625*7c478bd9Sstevel@tonic-gate 					goto out1;
626*7c478bd9Sstevel@tonic-gate 				}
627*7c478bd9Sstevel@tonic-gate 				if (p < lbend)
628*7c478bd9Sstevel@tonic-gate 					*p++ = t;
629*7c478bd9Sstevel@tonic-gate 				if(t == '\\') {
630*7c478bd9Sstevel@tonic-gate 					if((t = *q++) == '\0') {
631*7c478bd9Sstevel@tonic-gate 						saveq = 0;
632*7c478bd9Sstevel@tonic-gate 						return(-1);
633*7c478bd9Sstevel@tonic-gate 					}
634*7c478bd9Sstevel@tonic-gate 					if (p < lbend)
635*7c478bd9Sstevel@tonic-gate 						*p++ = t;
636*7c478bd9Sstevel@tonic-gate 				}
637*7c478bd9Sstevel@tonic-gate 			}
638*7c478bd9Sstevel@tonic-gate 			saveq = 0;
639*7c478bd9Sstevel@tonic-gate 
640*7c478bd9Sstevel@tonic-gate 		out1:
641*7c478bd9Sstevel@tonic-gate 			if (p == lbend)
642*7c478bd9Sstevel@tonic-gate 				comperr("Command line too long");
643*7c478bd9Sstevel@tonic-gate 			*p = '\0';
644*7c478bd9Sstevel@tonic-gate 			return(1);
645*7c478bd9Sstevel@tonic-gate 		}
646*7c478bd9Sstevel@tonic-gate 		if((q = saveq) == 0)    return(-1);
647*7c478bd9Sstevel@tonic-gate 
648*7c478bd9Sstevel@tonic-gate 		while((t = *q++) != '\0') {
649*7c478bd9Sstevel@tonic-gate 			if(t == '\n') {
650*7c478bd9Sstevel@tonic-gate 				saveq = q;
651*7c478bd9Sstevel@tonic-gate 				goto out2;
652*7c478bd9Sstevel@tonic-gate 			}
653*7c478bd9Sstevel@tonic-gate 			if(p < lbend)
654*7c478bd9Sstevel@tonic-gate 				*p++ = t;
655*7c478bd9Sstevel@tonic-gate 			if(t == '\\') {
656*7c478bd9Sstevel@tonic-gate 				if((t = *q++) == '\0') {
657*7c478bd9Sstevel@tonic-gate 					saveq = 0;
658*7c478bd9Sstevel@tonic-gate 					return(-1);
659*7c478bd9Sstevel@tonic-gate 				}
660*7c478bd9Sstevel@tonic-gate 				if (p < lbend)
661*7c478bd9Sstevel@tonic-gate 					*p++ = t;
662*7c478bd9Sstevel@tonic-gate 			}
663*7c478bd9Sstevel@tonic-gate 		}
664*7c478bd9Sstevel@tonic-gate 		saveq = 0;
665*7c478bd9Sstevel@tonic-gate 
666*7c478bd9Sstevel@tonic-gate 	out2:
667*7c478bd9Sstevel@tonic-gate 		if (p == lbend)
668*7c478bd9Sstevel@tonic-gate 			comperr("Command line too long");
669*7c478bd9Sstevel@tonic-gate 		*p = '\0';
670*7c478bd9Sstevel@tonic-gate 		return(1);
671*7c478bd9Sstevel@tonic-gate 	}
672*7c478bd9Sstevel@tonic-gate 
673*7c478bd9Sstevel@tonic-gate 	while((t = getc(fin)) != EOF) {
674*7c478bd9Sstevel@tonic-gate 		if(t == '\n') {
675*7c478bd9Sstevel@tonic-gate 			if (p == lbend)
676*7c478bd9Sstevel@tonic-gate 				comperr("Command line too long");
677*7c478bd9Sstevel@tonic-gate 			*p = '\0';
678*7c478bd9Sstevel@tonic-gate 			return(1);
679*7c478bd9Sstevel@tonic-gate 		}
680*7c478bd9Sstevel@tonic-gate 		if (p < lbend)
681*7c478bd9Sstevel@tonic-gate 			*p++ = t;
682*7c478bd9Sstevel@tonic-gate 		if(t == '\\') {
683*7c478bd9Sstevel@tonic-gate 			if((t = getc(fin)) == EOF)
684*7c478bd9Sstevel@tonic-gate 				break;
685*7c478bd9Sstevel@tonic-gate 			if(p < lbend)
686*7c478bd9Sstevel@tonic-gate 				*p++ = t;
687*7c478bd9Sstevel@tonic-gate 		}
688*7c478bd9Sstevel@tonic-gate 	}
689*7c478bd9Sstevel@tonic-gate 	if(ferror(fin)) {
690*7c478bd9Sstevel@tonic-gate 		perror("sed: Error reading pattern file");
691*7c478bd9Sstevel@tonic-gate 		exit(2);
692*7c478bd9Sstevel@tonic-gate 	}
693*7c478bd9Sstevel@tonic-gate 	return(-1);
694*7c478bd9Sstevel@tonic-gate }
695*7c478bd9Sstevel@tonic-gate 
696*7c478bd9Sstevel@tonic-gate char    *address(expbuf)
697*7c478bd9Sstevel@tonic-gate char    *expbuf;
698*7c478bd9Sstevel@tonic-gate {
699*7c478bd9Sstevel@tonic-gate 	register char   *rcp;
700*7c478bd9Sstevel@tonic-gate 	long long	lno;
701*7c478bd9Sstevel@tonic-gate 
702*7c478bd9Sstevel@tonic-gate 	if(*cp == '$') {
703*7c478bd9Sstevel@tonic-gate 		if (expbuf > &respace[RESIZE-2])
704*7c478bd9Sstevel@tonic-gate 			comperr(TMMES);
705*7c478bd9Sstevel@tonic-gate 		cp++;
706*7c478bd9Sstevel@tonic-gate 		*expbuf++ = CEND;
707*7c478bd9Sstevel@tonic-gate 		*expbuf++ = CCEOF;
708*7c478bd9Sstevel@tonic-gate 		return(expbuf);
709*7c478bd9Sstevel@tonic-gate 	}
710*7c478bd9Sstevel@tonic-gate 	if (*cp == '/' || *cp == '\\' ) {
711*7c478bd9Sstevel@tonic-gate 		if ( *cp == '\\' )
712*7c478bd9Sstevel@tonic-gate 			cp++;
713*7c478bd9Sstevel@tonic-gate 		sseof = *cp++;
714*7c478bd9Sstevel@tonic-gate 		return(comple((char *) 0, expbuf, &respace[RESIZE-1], sseof));
715*7c478bd9Sstevel@tonic-gate 	}
716*7c478bd9Sstevel@tonic-gate 
717*7c478bd9Sstevel@tonic-gate 	rcp = cp;
718*7c478bd9Sstevel@tonic-gate 	lno = 0;
719*7c478bd9Sstevel@tonic-gate 
720*7c478bd9Sstevel@tonic-gate 	while(*rcp >= '0' && *rcp <= '9')
721*7c478bd9Sstevel@tonic-gate 		lno = lno*10 + *rcp++ - '0';
722*7c478bd9Sstevel@tonic-gate 
723*7c478bd9Sstevel@tonic-gate 	if(rcp > cp) {
724*7c478bd9Sstevel@tonic-gate 		if (expbuf > &respace[RESIZE-3])
725*7c478bd9Sstevel@tonic-gate 			comperr(TMMES);
726*7c478bd9Sstevel@tonic-gate 		*expbuf++ = CLNUM;
727*7c478bd9Sstevel@tonic-gate 		*expbuf++ = nlno;
728*7c478bd9Sstevel@tonic-gate 		tlno[nlno++] = lno;
729*7c478bd9Sstevel@tonic-gate 		if(nlno >= NLINES)
730*7c478bd9Sstevel@tonic-gate 			comperr("Too many line numbers: %s");
731*7c478bd9Sstevel@tonic-gate 		*expbuf++ = CCEOF;
732*7c478bd9Sstevel@tonic-gate 		cp = rcp;
733*7c478bd9Sstevel@tonic-gate 		return(expbuf);
734*7c478bd9Sstevel@tonic-gate 	}
735*7c478bd9Sstevel@tonic-gate 	return(0);
736*7c478bd9Sstevel@tonic-gate }
737*7c478bd9Sstevel@tonic-gate 
738*7c478bd9Sstevel@tonic-gate char    *text(textbuf, tbend)
739*7c478bd9Sstevel@tonic-gate char    *textbuf;
740*7c478bd9Sstevel@tonic-gate char	*tbend;
741*7c478bd9Sstevel@tonic-gate {
742*7c478bd9Sstevel@tonic-gate 	register char   *p, *q;
743*7c478bd9Sstevel@tonic-gate 
744*7c478bd9Sstevel@tonic-gate 	p = textbuf;
745*7c478bd9Sstevel@tonic-gate 	q = cp;
746*7c478bd9Sstevel@tonic-gate #ifndef S5EMUL
747*7c478bd9Sstevel@tonic-gate 	/*
748*7c478bd9Sstevel@tonic-gate 	 * Strip off indentation from text to be inserted.
749*7c478bd9Sstevel@tonic-gate 	 */
750*7c478bd9Sstevel@tonic-gate 	while(*q == '\t' || *q == ' ')	q++;
751*7c478bd9Sstevel@tonic-gate #endif
752*7c478bd9Sstevel@tonic-gate 	for(;;) {
753*7c478bd9Sstevel@tonic-gate 
754*7c478bd9Sstevel@tonic-gate 		if(p > tbend)
755*7c478bd9Sstevel@tonic-gate 			return(NULL);	/* overflowed the buffer */
756*7c478bd9Sstevel@tonic-gate 		if((*p = *q++) == '\\')
757*7c478bd9Sstevel@tonic-gate 			*p = *q++;
758*7c478bd9Sstevel@tonic-gate 		if(*p == '\0') {
759*7c478bd9Sstevel@tonic-gate 			cp = --q;
760*7c478bd9Sstevel@tonic-gate 			return(++p);
761*7c478bd9Sstevel@tonic-gate 		}
762*7c478bd9Sstevel@tonic-gate #ifndef S5EMUL
763*7c478bd9Sstevel@tonic-gate 		/*
764*7c478bd9Sstevel@tonic-gate 		 * Strip off indentation from text to be inserted.
765*7c478bd9Sstevel@tonic-gate 		 */
766*7c478bd9Sstevel@tonic-gate 		if(*p == '\n') {
767*7c478bd9Sstevel@tonic-gate 			while(*q == '\t' || *q == ' ')	q++;
768*7c478bd9Sstevel@tonic-gate 		}
769*7c478bd9Sstevel@tonic-gate #endif
770*7c478bd9Sstevel@tonic-gate 		p++;
771*7c478bd9Sstevel@tonic-gate 	}
772*7c478bd9Sstevel@tonic-gate }
773*7c478bd9Sstevel@tonic-gate 
774*7c478bd9Sstevel@tonic-gate 
775*7c478bd9Sstevel@tonic-gate struct label    *search(ptr)
776*7c478bd9Sstevel@tonic-gate struct label    *ptr;
777*7c478bd9Sstevel@tonic-gate {
778*7c478bd9Sstevel@tonic-gate 	struct label    *rp;
779*7c478bd9Sstevel@tonic-gate 
780*7c478bd9Sstevel@tonic-gate 	rp = labtab;
781*7c478bd9Sstevel@tonic-gate 	while(rp < ptr) {
782*7c478bd9Sstevel@tonic-gate 		if(strcmp(rp->asc, ptr->asc) == 0)
783*7c478bd9Sstevel@tonic-gate 			return(rp);
784*7c478bd9Sstevel@tonic-gate 		rp++;
785*7c478bd9Sstevel@tonic-gate 	}
786*7c478bd9Sstevel@tonic-gate 
787*7c478bd9Sstevel@tonic-gate 	return(0);
788*7c478bd9Sstevel@tonic-gate }
789*7c478bd9Sstevel@tonic-gate 
790*7c478bd9Sstevel@tonic-gate 
791*7c478bd9Sstevel@tonic-gate dechain()
792*7c478bd9Sstevel@tonic-gate {
793*7c478bd9Sstevel@tonic-gate 	struct label    *lptr;
794*7c478bd9Sstevel@tonic-gate 	union reptr     *rptr, *trptr;
795*7c478bd9Sstevel@tonic-gate 
796*7c478bd9Sstevel@tonic-gate 	for(lptr = labtab; lptr < lab; lptr++) {
797*7c478bd9Sstevel@tonic-gate 
798*7c478bd9Sstevel@tonic-gate 		if(lptr->address == 0) {
799*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "sed: Undefined label: %s\n", lptr->asc);
800*7c478bd9Sstevel@tonic-gate 			exit(2);
801*7c478bd9Sstevel@tonic-gate 		}
802*7c478bd9Sstevel@tonic-gate 
803*7c478bd9Sstevel@tonic-gate 		if(lptr->chain) {
804*7c478bd9Sstevel@tonic-gate 			rptr = lptr->chain;
805*7c478bd9Sstevel@tonic-gate 			while(trptr = rptr->r2.lb1) {
806*7c478bd9Sstevel@tonic-gate 				rptr->r2.lb1 = lptr->address;
807*7c478bd9Sstevel@tonic-gate 				rptr = trptr;
808*7c478bd9Sstevel@tonic-gate 			}
809*7c478bd9Sstevel@tonic-gate 			rptr->r2.lb1 = lptr->address;
810*7c478bd9Sstevel@tonic-gate 		}
811*7c478bd9Sstevel@tonic-gate 	}
812*7c478bd9Sstevel@tonic-gate }
813*7c478bd9Sstevel@tonic-gate 
814*7c478bd9Sstevel@tonic-gate char *ycomp(expbuf)
815*7c478bd9Sstevel@tonic-gate char    *expbuf;
816*7c478bd9Sstevel@tonic-gate {
817*7c478bd9Sstevel@tonic-gate 	register char   c;
818*7c478bd9Sstevel@tonic-gate 	register char *ep, *tsp;
819*7c478bd9Sstevel@tonic-gate 	register int i;
820*7c478bd9Sstevel@tonic-gate 	char    *sp;
821*7c478bd9Sstevel@tonic-gate 
822*7c478bd9Sstevel@tonic-gate 	ep = expbuf;
823*7c478bd9Sstevel@tonic-gate 	if(ep + 0377 > &respace[RESIZE-1])
824*7c478bd9Sstevel@tonic-gate 		comperr(TMMES);
825*7c478bd9Sstevel@tonic-gate 	sp = cp;
826*7c478bd9Sstevel@tonic-gate 	for(tsp = cp; (c = *tsp) != sseof; tsp++) {
827*7c478bd9Sstevel@tonic-gate 		if(c == '\\')
828*7c478bd9Sstevel@tonic-gate 			tsp++;
829*7c478bd9Sstevel@tonic-gate 		if(c == '\0' || c == '\n')
830*7c478bd9Sstevel@tonic-gate 			comperr("Ending delimiter missing on string: %s");
831*7c478bd9Sstevel@tonic-gate 	}
832*7c478bd9Sstevel@tonic-gate 	tsp++;
833*7c478bd9Sstevel@tonic-gate 
834*7c478bd9Sstevel@tonic-gate 	while((c = *sp++) != sseof) {
835*7c478bd9Sstevel@tonic-gate 		c &= 0377;
836*7c478bd9Sstevel@tonic-gate 		if(c == '\\' && *sp == 'n') {
837*7c478bd9Sstevel@tonic-gate 			sp++;
838*7c478bd9Sstevel@tonic-gate 			c = '\n';
839*7c478bd9Sstevel@tonic-gate 		}
840*7c478bd9Sstevel@tonic-gate 		if((ep[c] = *tsp++) == '\\' && *tsp == 'n') {
841*7c478bd9Sstevel@tonic-gate 			ep[c] = '\n';
842*7c478bd9Sstevel@tonic-gate 			tsp++;
843*7c478bd9Sstevel@tonic-gate 		}
844*7c478bd9Sstevel@tonic-gate 		if(ep[c] == sseof || ep[c] == '\0')
845*7c478bd9Sstevel@tonic-gate 			comperr("Transform strings not the same size: %s");
846*7c478bd9Sstevel@tonic-gate 	}
847*7c478bd9Sstevel@tonic-gate 	if(*tsp != sseof) {
848*7c478bd9Sstevel@tonic-gate 		if(*tsp == '\0')
849*7c478bd9Sstevel@tonic-gate 			comperr("Ending delimiter missing on string: %s");
850*7c478bd9Sstevel@tonic-gate 		else
851*7c478bd9Sstevel@tonic-gate 			comperr("Transform strings not the same size: %s");
852*7c478bd9Sstevel@tonic-gate 	}
853*7c478bd9Sstevel@tonic-gate 	cp = ++tsp;
854*7c478bd9Sstevel@tonic-gate 
855*7c478bd9Sstevel@tonic-gate 	for(i = 0; i < 0400; i++)
856*7c478bd9Sstevel@tonic-gate 		if(ep[i] == 0)
857*7c478bd9Sstevel@tonic-gate 			ep[i] = i;
858*7c478bd9Sstevel@tonic-gate 
859*7c478bd9Sstevel@tonic-gate 	return(ep + 0400);
860*7c478bd9Sstevel@tonic-gate }
861*7c478bd9Sstevel@tonic-gate comperr(msg)
862*7c478bd9Sstevel@tonic-gate char *msg;
863*7c478bd9Sstevel@tonic-gate {
864*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "sed: ");
865*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, msg, linebuf);
866*7c478bd9Sstevel@tonic-gate 	(void) putc('\n', stderr);
867*7c478bd9Sstevel@tonic-gate 	exit(2);
868*7c478bd9Sstevel@tonic-gate }
869