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