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