xref: /titanic_44/usr/src/cmd/ed/ed.c (revision 6a5408e613dc0b372f722907d6c0b18e99c182dd)
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
523a1cceaSRoger A. Faulkner  * Common Development and Distribution License (the "License").
623a1cceaSRoger A. Faulkner  * 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  */
217c478bd9Sstevel@tonic-gate 
227c478bd9Sstevel@tonic-gate /*
2323a1cceaSRoger A. Faulkner  * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
2623a1cceaSRoger A. Faulkner /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
2723a1cceaSRoger A. Faulkner /*	  All Rights Reserved  	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * Editor
317c478bd9Sstevel@tonic-gate  */
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include	<crypt.h>
347c478bd9Sstevel@tonic-gate #include	<libgen.h>
357c478bd9Sstevel@tonic-gate #include	<wait.h>
367c478bd9Sstevel@tonic-gate #include	<string.h>
377c478bd9Sstevel@tonic-gate #include	<sys/types.h>
387c478bd9Sstevel@tonic-gate #include	<locale.h>
397c478bd9Sstevel@tonic-gate #include	<regexpr.h>
407c478bd9Sstevel@tonic-gate #include	<regex.h>
417c478bd9Sstevel@tonic-gate #include	<errno.h>
42*6a5408e6SRichard Lowe #include	<paths.h>
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate static const 	char	*msgtab[] =
457c478bd9Sstevel@tonic-gate {
467c478bd9Sstevel@tonic-gate 	"write or open on pipe failed",			/*  0 */
477c478bd9Sstevel@tonic-gate 	"warning: expecting `w'",			/*  1 */
487c478bd9Sstevel@tonic-gate 	"mark not lower case ascii",			/*  2 */
497c478bd9Sstevel@tonic-gate 	"Cannot open input file",			/*  3 */
507c478bd9Sstevel@tonic-gate 	"PWB spec problem",				/*  4 */
517c478bd9Sstevel@tonic-gate 	"nothing to undo",				/*  5 */
527c478bd9Sstevel@tonic-gate 	"restricted shell",				/*  6 */
537c478bd9Sstevel@tonic-gate 	"cannot create output file",			/*  7 */
547c478bd9Sstevel@tonic-gate 	"filesystem out of space!",			/*  8 */
557c478bd9Sstevel@tonic-gate 	"cannot open file",				/*  9 */
567c478bd9Sstevel@tonic-gate 	"cannot link",					/* 10 */
577c478bd9Sstevel@tonic-gate 	"Range endpoint too large",			/* 11 */
587c478bd9Sstevel@tonic-gate 	"unknown command",				/* 12 */
597c478bd9Sstevel@tonic-gate 	"search string not found",			/* 13 */
607c478bd9Sstevel@tonic-gate 	"-",						/* 14 */
617c478bd9Sstevel@tonic-gate 	"line out of range",				/* 15 */
627c478bd9Sstevel@tonic-gate 	"bad number",					/* 16 */
637c478bd9Sstevel@tonic-gate 	"bad range",					/* 17 */
647c478bd9Sstevel@tonic-gate 	"Illegal address count",			/* 18 */
657c478bd9Sstevel@tonic-gate 	"incomplete global expression",			/* 19 */
667c478bd9Sstevel@tonic-gate 	"illegal suffix",				/* 20 */
677c478bd9Sstevel@tonic-gate 	"illegal or missing filename",			/* 21 */
687c478bd9Sstevel@tonic-gate 	"no space after command",			/* 22 */
697c478bd9Sstevel@tonic-gate 	"fork failed - try again",			/* 23 */
707c478bd9Sstevel@tonic-gate 	"maximum of 64 characters in file names",	/* 24 */
717c478bd9Sstevel@tonic-gate 	"`\\digit' out of range",			/* 25 */
727c478bd9Sstevel@tonic-gate 	"interrupt",					/* 26 */
737c478bd9Sstevel@tonic-gate 	"line too long",				/* 27 */
747c478bd9Sstevel@tonic-gate 	"illegal character in input file",		/* 28 */
757c478bd9Sstevel@tonic-gate 	"write error",					/* 29 */
767c478bd9Sstevel@tonic-gate 	"out of memory for append",			/* 30 */
777c478bd9Sstevel@tonic-gate 	"temp file too big",				/* 31 */
787c478bd9Sstevel@tonic-gate 	"I/O error on temp file",			/* 32 */
797c478bd9Sstevel@tonic-gate 	"multiple globals not allowed",			/* 33 */
807c478bd9Sstevel@tonic-gate 	"global too long",				/* 34 */
817c478bd9Sstevel@tonic-gate 	"no match",					/* 35 */
827c478bd9Sstevel@tonic-gate 	"illegal or missing delimiter",			/* 36 */
837c478bd9Sstevel@tonic-gate 	"-",						/* 37 */
847c478bd9Sstevel@tonic-gate 	"replacement string too long",			/* 38 */
857c478bd9Sstevel@tonic-gate 	"illegal move destination",			/* 39 */
867c478bd9Sstevel@tonic-gate 	"-",						/* 40 */
877c478bd9Sstevel@tonic-gate 	"no remembered search string",			/* 41 */
887c478bd9Sstevel@tonic-gate 	"'\\( \\)' imbalance",				/* 42 */
897c478bd9Sstevel@tonic-gate 	"Too many `\\(' s",				/* 43 */
907c478bd9Sstevel@tonic-gate 	"more than 2 numbers given",			/* 44 */
917c478bd9Sstevel@tonic-gate 	"'\\}' expected",				/* 45 */
927c478bd9Sstevel@tonic-gate 	"first number exceeds second",			/* 46 */
937c478bd9Sstevel@tonic-gate 	"incomplete substitute",			/* 47 */
947c478bd9Sstevel@tonic-gate 	"newline unexpected",				/* 48 */
957c478bd9Sstevel@tonic-gate 	"'[ ]' imbalance",				/* 49 */
967c478bd9Sstevel@tonic-gate 	"regular expression overflow",			/* 50 */
977c478bd9Sstevel@tonic-gate 	"regular expression error",			/* 51 */
987c478bd9Sstevel@tonic-gate 	"command expected",				/* 52 */
997c478bd9Sstevel@tonic-gate 	"a, i, or c not allowed in G",			/* 53 */
1007c478bd9Sstevel@tonic-gate 	"end of line expected",				/* 54 */
1017c478bd9Sstevel@tonic-gate 	"no remembered replacement string",		/* 55 */
1027c478bd9Sstevel@tonic-gate 	"no remembered command",			/* 56 */
1037c478bd9Sstevel@tonic-gate 	"illegal redirection",				/* 57 */
1047c478bd9Sstevel@tonic-gate 	"possible concurrent update",			/* 58 */
1057c478bd9Sstevel@tonic-gate 	"-",						/* 59 */
1067c478bd9Sstevel@tonic-gate 	"the x command has become X (upper case)",	/* 60 */
1077c478bd9Sstevel@tonic-gate 	"Warning: 'w' may destroy input file "
1087c478bd9Sstevel@tonic-gate 	"(due to `illegal char' read earlier)",
1097c478bd9Sstevel@tonic-gate 							/* 61 */
1107c478bd9Sstevel@tonic-gate 	"Caution: 'q' may lose data in buffer;"
1117c478bd9Sstevel@tonic-gate 	" 'w' may destroy input file",
1127c478bd9Sstevel@tonic-gate 							/* 62 */
1137c478bd9Sstevel@tonic-gate 	"Encryption of string failed",			/* 63 */
1147c478bd9Sstevel@tonic-gate 	"Encryption facility not available",		/* 64 */
1157c478bd9Sstevel@tonic-gate 	"Cannot encrypt temporary file",		/* 65 */
1167c478bd9Sstevel@tonic-gate 	"Enter key:",					/* 66 */
1177c478bd9Sstevel@tonic-gate 	"Illegal byte sequence",			/* 67 */
1187c478bd9Sstevel@tonic-gate 	"File does not exist",				/* 68 */
1197c478bd9Sstevel@tonic-gate 	"tempnam failed",				/* 69 */
1207c478bd9Sstevel@tonic-gate 	"Cannot open temporary file",			/* 70 */
1217c478bd9Sstevel@tonic-gate 	0
1227c478bd9Sstevel@tonic-gate };
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate #include <stdlib.h>
1257c478bd9Sstevel@tonic-gate #include <limits.h>
1267c478bd9Sstevel@tonic-gate #include <stdio.h>
1277c478bd9Sstevel@tonic-gate #include <signal.h>
1287c478bd9Sstevel@tonic-gate #include <sys/types.h>
1297c478bd9Sstevel@tonic-gate #include <sys/stat.h>
1307c478bd9Sstevel@tonic-gate #include <sys/statvfs.h>
1317c478bd9Sstevel@tonic-gate #include <unistd.h>
1327c478bd9Sstevel@tonic-gate #include <termio.h>
1337c478bd9Sstevel@tonic-gate #include <ctype.h>
1347c478bd9Sstevel@tonic-gate #include <setjmp.h>
1357c478bd9Sstevel@tonic-gate #include <fcntl.h>
1367c478bd9Sstevel@tonic-gate #include <wchar.h>	/* I18N */
1377c478bd9Sstevel@tonic-gate #include <wctype.h>	/* I18N */
1387c478bd9Sstevel@tonic-gate #include <widec.h>	/* I18N */
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate #define	FTYPE(A)	(A.st_mode)
1417c478bd9Sstevel@tonic-gate #define	FMODE(A)	(A.st_mode)
1427c478bd9Sstevel@tonic-gate #define	IDENTICAL(A, B)	(A.st_dev == B.st_dev && A.st_ino == B.st_ino)
1437c478bd9Sstevel@tonic-gate #define	ISBLK(A)	((A.st_mode & S_IFMT) == S_IFBLK)
1447c478bd9Sstevel@tonic-gate #define	ISCHR(A)	((A.st_mode & S_IFMT) == S_IFCHR)
1457c478bd9Sstevel@tonic-gate #define	ISDIR(A)	((A.st_mode & S_IFMT) == S_IFDIR)
1467c478bd9Sstevel@tonic-gate #define	ISFIFO(A)	((A.st_mode & S_IFMT) == S_IFIFO)
1477c478bd9Sstevel@tonic-gate #define	ISREG(A)	((A.st_mode & S_IFMT) == S_IFREG)
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate #define	PUTM()	if (xcode >= 0) puts(gettext(msgtab[xcode]))
1507c478bd9Sstevel@tonic-gate #define	UNGETC(c)	(peekc = c)
1517c478bd9Sstevel@tonic-gate #define	FNSIZE	PATH_MAX
1527c478bd9Sstevel@tonic-gate #define	LBSIZE	LINE_MAX
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate /* size of substitution replacement pattern buffer */
1557c478bd9Sstevel@tonic-gate #define	RHSIZE	(LINE_MAX*2)
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate #define	KSIZE	8
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate #define	READ	0
1607c478bd9Sstevel@tonic-gate #define	WRITE	1
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate extern  char	*optarg;	/* Value of argument */
1637c478bd9Sstevel@tonic-gate extern  int	optind;		/* Indicator of argument */
1647c478bd9Sstevel@tonic-gate extern	int __xpg4;	/* defined in xpg4.c; 0 if not xpg4-compiled program */
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate struct  Fspec   {
1677c478bd9Sstevel@tonic-gate 	char    Ftabs[22];
1687c478bd9Sstevel@tonic-gate 	char    Fdel;
1697c478bd9Sstevel@tonic-gate 	unsigned char   Flim;
1707c478bd9Sstevel@tonic-gate 	char    Fmov;
1717c478bd9Sstevel@tonic-gate 	char    Ffill;
1727c478bd9Sstevel@tonic-gate };
1737c478bd9Sstevel@tonic-gate static struct  Fspec   fss;
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate static char	*fsp;
1767c478bd9Sstevel@tonic-gate static int	fsprtn;
1777c478bd9Sstevel@tonic-gate static char	line[70];
1787c478bd9Sstevel@tonic-gate static char	*linp = line;
1797c478bd9Sstevel@tonic-gate static int	sig;
1807c478bd9Sstevel@tonic-gate static int	Xqt = 0;
1817c478bd9Sstevel@tonic-gate static int	lastc;
1827c478bd9Sstevel@tonic-gate static char	savedfile[FNSIZE];
1837c478bd9Sstevel@tonic-gate static char	file[FNSIZE];
1847c478bd9Sstevel@tonic-gate static char	funny[FNSIZE];
1857c478bd9Sstevel@tonic-gate static int	funlink = 0;
1867c478bd9Sstevel@tonic-gate static char	linebuf[LBSIZE];
1877c478bd9Sstevel@tonic-gate static char	*tstring = linebuf;
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate static char *expbuf;
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate static char	rhsbuf[RHSIZE];
1927c478bd9Sstevel@tonic-gate struct	lin	{
1937c478bd9Sstevel@tonic-gate 	long cur;
1947c478bd9Sstevel@tonic-gate 	long sav;
1957c478bd9Sstevel@tonic-gate };
1967c478bd9Sstevel@tonic-gate typedef struct lin *LINE;
1977c478bd9Sstevel@tonic-gate static LINE	zero;
1987c478bd9Sstevel@tonic-gate static LINE	dot;
1997c478bd9Sstevel@tonic-gate static LINE	dol;
2007c478bd9Sstevel@tonic-gate static LINE	endcore;
2017c478bd9Sstevel@tonic-gate static LINE	fendcore;
2027c478bd9Sstevel@tonic-gate static LINE	addr1;
2037c478bd9Sstevel@tonic-gate static LINE	addr2;
2047c478bd9Sstevel@tonic-gate static LINE	savdol, savdot;
2057c478bd9Sstevel@tonic-gate static int	globflg;
2067c478bd9Sstevel@tonic-gate static int	initflg;
2077c478bd9Sstevel@tonic-gate static char	genbuf[LBSIZE];
2087c478bd9Sstevel@tonic-gate static long	count;
2097c478bd9Sstevel@tonic-gate static int	numpass;	/* Number of passes thru dosub(). */
2107c478bd9Sstevel@tonic-gate static int	gsubf;		/* Occurrence value. LBSIZE-1=all. */
2117c478bd9Sstevel@tonic-gate static int	ocerr1;	/* Allows lines NOT changed by dosub() to NOT be put */
2127c478bd9Sstevel@tonic-gate 			/* out. Retains last line changed as current line. */
2137c478bd9Sstevel@tonic-gate static int	ocerr2;	/* Flags if ANY line changed by substitute(). 0=nc. */
2147c478bd9Sstevel@tonic-gate static char	*nextip;
2157c478bd9Sstevel@tonic-gate static char	*linebp;
2167c478bd9Sstevel@tonic-gate static int	ninbuf;
2177c478bd9Sstevel@tonic-gate static int	peekc;
2187c478bd9Sstevel@tonic-gate static int	io;
2197c478bd9Sstevel@tonic-gate static void	(*oldhup)(), (*oldintr)();
2207c478bd9Sstevel@tonic-gate static void	(*oldquit)(), (*oldpipe)();
2217c478bd9Sstevel@tonic-gate static void	quit(int);
2227c478bd9Sstevel@tonic-gate static int	vflag = 1;
2237c478bd9Sstevel@tonic-gate static int	xflag;
2247c478bd9Sstevel@tonic-gate static int	xtflag;
2257c478bd9Sstevel@tonic-gate static int	kflag;
2267c478bd9Sstevel@tonic-gate static int	crflag;
2277c478bd9Sstevel@tonic-gate 		/* Flag for determining if file being read is encrypted */
2287c478bd9Sstevel@tonic-gate static int	hflag;
2297c478bd9Sstevel@tonic-gate static int	xcode = -1;
2307c478bd9Sstevel@tonic-gate static char	crbuf[LBSIZE];
2317c478bd9Sstevel@tonic-gate static	int	perm[2];
2327c478bd9Sstevel@tonic-gate static int	tperm[2];
2337c478bd9Sstevel@tonic-gate static int	permflag;
2347c478bd9Sstevel@tonic-gate static int	tpermflag;
2357c478bd9Sstevel@tonic-gate static int	col;
2367c478bd9Sstevel@tonic-gate static char	*globp;
2377c478bd9Sstevel@tonic-gate static int	tfile = -1;
2387c478bd9Sstevel@tonic-gate static int	tline;
2397c478bd9Sstevel@tonic-gate static char	*tfname;
2407c478bd9Sstevel@tonic-gate extern char	*locs;
2417c478bd9Sstevel@tonic-gate static char	ibuff[LBSIZE];
2427c478bd9Sstevel@tonic-gate static int	iblock = -1;
2437c478bd9Sstevel@tonic-gate static char	obuff[LBSIZE];
2447c478bd9Sstevel@tonic-gate static int	oblock = -1;
2457c478bd9Sstevel@tonic-gate static int	ichanged;
2467c478bd9Sstevel@tonic-gate static int	nleft;
2477c478bd9Sstevel@tonic-gate static long	savnames[26], names[26];
2487c478bd9Sstevel@tonic-gate static int	anymarks;
2497c478bd9Sstevel@tonic-gate static long	subnewa;
2507c478bd9Sstevel@tonic-gate static int	fchange;
2517c478bd9Sstevel@tonic-gate static int	nline;
2527c478bd9Sstevel@tonic-gate static int	fflg, shflg;
2537c478bd9Sstevel@tonic-gate static char	prompt[16] = "*";
2547c478bd9Sstevel@tonic-gate static int	rflg;
2557c478bd9Sstevel@tonic-gate static int	readflg;
2567c478bd9Sstevel@tonic-gate static int 	eflg;
2577c478bd9Sstevel@tonic-gate static int 	qflg = 0;
2587c478bd9Sstevel@tonic-gate static int 	ncflg;
2597c478bd9Sstevel@tonic-gate static int 	listn;
2607c478bd9Sstevel@tonic-gate static int 	listf;
2617c478bd9Sstevel@tonic-gate static int 	pflag;
2627c478bd9Sstevel@tonic-gate static int 	flag28 = 0; /* Prevents write after a partial read */
2637c478bd9Sstevel@tonic-gate static int 	save28 = 0; /* Flag whether buffer empty at start of read */
2647c478bd9Sstevel@tonic-gate static long 	savtime;
2657c478bd9Sstevel@tonic-gate static char	*name = "SHELL";
2667c478bd9Sstevel@tonic-gate static char	*rshell = "/usr/lib/rsh";
2677c478bd9Sstevel@tonic-gate static char	*val;
2687c478bd9Sstevel@tonic-gate static char	*home;
2697c478bd9Sstevel@tonic-gate static int	nodelim;
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate int	makekey(int *);
2727c478bd9Sstevel@tonic-gate int	_mbftowc(char *, wchar_t *, int (*)(), int *);
2737c478bd9Sstevel@tonic-gate static int	error(int code);
2747c478bd9Sstevel@tonic-gate static void	tlist(struct Fspec *);
2757c478bd9Sstevel@tonic-gate static void	tstd(struct Fspec *);
2767c478bd9Sstevel@tonic-gate static void	gdelete(void);
2777c478bd9Sstevel@tonic-gate static void	delete(void);
2787c478bd9Sstevel@tonic-gate static void	exfile(void);
2797c478bd9Sstevel@tonic-gate static void	filename(int comm);
2807c478bd9Sstevel@tonic-gate static void	newline(void);
2817c478bd9Sstevel@tonic-gate static int	gettty(void);
2827c478bd9Sstevel@tonic-gate static void	commands(void);
2837c478bd9Sstevel@tonic-gate static void	undo(void);
2847c478bd9Sstevel@tonic-gate static void	save(void);
2857c478bd9Sstevel@tonic-gate static void	strcopy(char *source, char *dest);
2867c478bd9Sstevel@tonic-gate static int	strequal(char **scan1, char *str);
2877c478bd9Sstevel@tonic-gate static int	stdtab(char *, char *);
2887c478bd9Sstevel@tonic-gate static int	lenchk(char *, struct Fspec *);
2897c478bd9Sstevel@tonic-gate static void	clear(struct Fspec *);
2907c478bd9Sstevel@tonic-gate static int	expnd(char *, char *, int *, struct Fspec *);
2917c478bd9Sstevel@tonic-gate static void	tincr(int, struct Fspec *);
2927c478bd9Sstevel@tonic-gate static void	targ(struct Fspec *);
2937c478bd9Sstevel@tonic-gate static int	numb(void);
2947c478bd9Sstevel@tonic-gate static int	fspec(char *, struct Fspec *, int);
2957c478bd9Sstevel@tonic-gate static void	red(char *);
2967c478bd9Sstevel@tonic-gate static void	newtime(void);
2977c478bd9Sstevel@tonic-gate static void	chktime(void);
2987c478bd9Sstevel@tonic-gate static void	getime(void);
2997c478bd9Sstevel@tonic-gate static void	mkfunny(void);
3007c478bd9Sstevel@tonic-gate static int	eopen(char *, int);
3017c478bd9Sstevel@tonic-gate static void	eclose(int f);
3027c478bd9Sstevel@tonic-gate static void	globaln(int);
3037c478bd9Sstevel@tonic-gate static char	*getkey(const char *);
3047c478bd9Sstevel@tonic-gate static int	execute(int, LINE);
3057c478bd9Sstevel@tonic-gate static void	error1(int);
3067c478bd9Sstevel@tonic-gate static int	getcopy(void);
3077c478bd9Sstevel@tonic-gate static void	move(int);
3087c478bd9Sstevel@tonic-gate static void	dosub(void);
3097c478bd9Sstevel@tonic-gate static int	getsub(void);
3107c478bd9Sstevel@tonic-gate static int	compsub(void);
3117c478bd9Sstevel@tonic-gate static void	substitute(int);
3127c478bd9Sstevel@tonic-gate static void	join(void);
3137c478bd9Sstevel@tonic-gate static void	global(int);
3147c478bd9Sstevel@tonic-gate static void	init(void);
3157c478bd9Sstevel@tonic-gate static void	rdelete(LINE, LINE);
3167c478bd9Sstevel@tonic-gate static void	append(int (*)(void), LINE);
3177c478bd9Sstevel@tonic-gate static int	getfile(void);
3187c478bd9Sstevel@tonic-gate static void	putfile(void);
3197c478bd9Sstevel@tonic-gate static void	onpipe(int);
3207c478bd9Sstevel@tonic-gate static void	onhup(int);
3217c478bd9Sstevel@tonic-gate static void	onintr(int);
3227c478bd9Sstevel@tonic-gate static void	setdot(void);
3237c478bd9Sstevel@tonic-gate static void	setall(void);
3247c478bd9Sstevel@tonic-gate static void	setnoaddr(void);
3257c478bd9Sstevel@tonic-gate static void	nonzero(void);
3267c478bd9Sstevel@tonic-gate static void	setzeroasone(void);
3277c478bd9Sstevel@tonic-gate static long	putline(void);
3287c478bd9Sstevel@tonic-gate static LINE	address(void);
32923a1cceaSRoger A. Faulkner static char	*getaline(long);
3307c478bd9Sstevel@tonic-gate static char	*getblock(long, long);
3317c478bd9Sstevel@tonic-gate static char	*place(char *, char *, char *);
3327c478bd9Sstevel@tonic-gate static void	comple(wchar_t);
3337c478bd9Sstevel@tonic-gate static void	putchr(unsigned char);
3347c478bd9Sstevel@tonic-gate static void	putwchr(wchar_t);
3357c478bd9Sstevel@tonic-gate static int	getchr(void);
3367c478bd9Sstevel@tonic-gate static void	unixcom(void);
3377c478bd9Sstevel@tonic-gate static void	blkio(int, char *, ssize_t (*)());
3387c478bd9Sstevel@tonic-gate static void	reverse(LINE, LINE);
3397c478bd9Sstevel@tonic-gate static void	putd();
3407c478bd9Sstevel@tonic-gate static wchar_t	get_wchr(void);
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate static struct stat	Fl, Tf;
3437c478bd9Sstevel@tonic-gate #ifndef RESEARCH
3447c478bd9Sstevel@tonic-gate static struct statvfs	U;
3457c478bd9Sstevel@tonic-gate static int	Short = 0;
3467c478bd9Sstevel@tonic-gate static mode_t	oldmask; /* No umask while writing */
3477c478bd9Sstevel@tonic-gate #endif
3487c478bd9Sstevel@tonic-gate static jmp_buf	savej;
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate #ifdef	NULLS
3517c478bd9Sstevel@tonic-gate int	nulls;	/* Null count */
3527c478bd9Sstevel@tonic-gate #endif
3537c478bd9Sstevel@tonic-gate static long	ccount;
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate static int	errcnt = 0;
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate static void
onpipe(int sig)3597c478bd9Sstevel@tonic-gate onpipe(int sig)
3607c478bd9Sstevel@tonic-gate {
3617c478bd9Sstevel@tonic-gate 	(int)error(0);
3627c478bd9Sstevel@tonic-gate }
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)3657c478bd9Sstevel@tonic-gate main(int argc, char **argv)
3667c478bd9Sstevel@tonic-gate {
3677c478bd9Sstevel@tonic-gate 	char *p1, *p2;
3687c478bd9Sstevel@tonic-gate 	int c;
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
3717c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
3727c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
3737c478bd9Sstevel@tonic-gate #endif
3747c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	oldquit = signal(SIGQUIT, SIG_IGN);
3777c478bd9Sstevel@tonic-gate 	oldhup = signal(SIGHUP, SIG_IGN);
3787c478bd9Sstevel@tonic-gate 	oldintr = signal(SIGINT, SIG_IGN);
3797c478bd9Sstevel@tonic-gate 	oldpipe = signal(SIGPIPE, onpipe);
3807c478bd9Sstevel@tonic-gate 	if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
3817c478bd9Sstevel@tonic-gate 		signal(SIGTERM, quit);
3827c478bd9Sstevel@tonic-gate 	p1 = *argv;
3837c478bd9Sstevel@tonic-gate 	while (*p1++);
3847c478bd9Sstevel@tonic-gate 	while (--p1 >= *argv)
3857c478bd9Sstevel@tonic-gate 		if (*p1 == '/')
3867c478bd9Sstevel@tonic-gate 			break;
3877c478bd9Sstevel@tonic-gate 	*argv = p1 + 1;
3887c478bd9Sstevel@tonic-gate 	/* if SHELL set in environment and is /usr/lib/rsh, set rflg */
3897c478bd9Sstevel@tonic-gate 	if ((val = getenv(name)) != NULL)
3907c478bd9Sstevel@tonic-gate 		if (strcmp(val, rshell) == 0)
3917c478bd9Sstevel@tonic-gate 			rflg++;
3927c478bd9Sstevel@tonic-gate 	if (**argv == 'r')
3937c478bd9Sstevel@tonic-gate 		rflg++;
3947c478bd9Sstevel@tonic-gate 	home = getenv("HOME");
3957c478bd9Sstevel@tonic-gate 	while (1) {
3967c478bd9Sstevel@tonic-gate 		while ((c = getopt(argc, argv, "sp:qxC")) != EOF) {
3977c478bd9Sstevel@tonic-gate 			switch (c) {
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate 			case 's':
4007c478bd9Sstevel@tonic-gate 				vflag = 0;
4017c478bd9Sstevel@tonic-gate 				break;
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 			case 'p':
4047c478bd9Sstevel@tonic-gate 				strncpy(prompt, optarg, sizeof (prompt)-1);
4057c478bd9Sstevel@tonic-gate 				shflg = 1;
4067c478bd9Sstevel@tonic-gate 				break;
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 			case 'q':
4097c478bd9Sstevel@tonic-gate 				signal(SIGQUIT, SIG_DFL);
4107c478bd9Sstevel@tonic-gate 				vflag = 1;
4117c478bd9Sstevel@tonic-gate 				break;
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 			case 'x':
4147c478bd9Sstevel@tonic-gate 				crflag = -1;
4157c478bd9Sstevel@tonic-gate 				xflag = 1;
4167c478bd9Sstevel@tonic-gate 				break;
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 			case 'C':
4197c478bd9Sstevel@tonic-gate 				crflag = 1;
4207c478bd9Sstevel@tonic-gate 				xflag = 1;
4217c478bd9Sstevel@tonic-gate 				break;
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 			case '?':
4247c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
4257c478bd9Sstevel@tonic-gate 		"Usage:   ed [- | -s] [-p string] [-x] [-C] [file]\n"
4267c478bd9Sstevel@tonic-gate 		"	  red [- | -s] [-p string] [-x] [-C] [file]\n"));
4277c478bd9Sstevel@tonic-gate 				exit(2);
4287c478bd9Sstevel@tonic-gate 			}
4297c478bd9Sstevel@tonic-gate 		}
4307c478bd9Sstevel@tonic-gate 		if (argv[optind] && strcmp(argv[optind], "-") == 0 &&
4317c478bd9Sstevel@tonic-gate 			strcmp(argv[optind-1], "--") != 0) {
4327c478bd9Sstevel@tonic-gate 			vflag = 0;
4337c478bd9Sstevel@tonic-gate 			optind++;
4347c478bd9Sstevel@tonic-gate 			continue;
4357c478bd9Sstevel@tonic-gate 		}
4367c478bd9Sstevel@tonic-gate 		break;
4377c478bd9Sstevel@tonic-gate 	}
4387c478bd9Sstevel@tonic-gate 	argc = argc - optind;
4397c478bd9Sstevel@tonic-gate 	argv = &argv[optind];
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 	if (xflag) {
4427c478bd9Sstevel@tonic-gate 		if (permflag)
4437c478bd9Sstevel@tonic-gate 			crypt_close(perm);
4447c478bd9Sstevel@tonic-gate 		permflag = 1;
4457c478bd9Sstevel@tonic-gate 		if ((kflag = run_setkey(&perm[0], getkey(msgtab[66])))
4467c478bd9Sstevel@tonic-gate 			== -1) {
4477c478bd9Sstevel@tonic-gate 			puts(gettext(msgtab[64]));
4487c478bd9Sstevel@tonic-gate 			xflag = 0;
4497c478bd9Sstevel@tonic-gate 			kflag = 0;
4507c478bd9Sstevel@tonic-gate 		}
4517c478bd9Sstevel@tonic-gate 		if (kflag == 0)
4527c478bd9Sstevel@tonic-gate 			crflag = 0;
4537c478bd9Sstevel@tonic-gate 	}
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 	if (argc > 0) {
4567c478bd9Sstevel@tonic-gate 		p1 = *argv;
4577c478bd9Sstevel@tonic-gate 		if (strlen(p1) >= (size_t)FNSIZE) {
4587c478bd9Sstevel@tonic-gate 			puts(gettext("file name too long"));
4597c478bd9Sstevel@tonic-gate 			if (kflag)
4607c478bd9Sstevel@tonic-gate 				crypt_close(perm);
4617c478bd9Sstevel@tonic-gate 			exit(2);
4627c478bd9Sstevel@tonic-gate 		}
4637c478bd9Sstevel@tonic-gate 		p2 = savedfile;
4647c478bd9Sstevel@tonic-gate 		while (*p2++ = *p1++);
4657c478bd9Sstevel@tonic-gate 		globp = "e";
4667c478bd9Sstevel@tonic-gate 		fflg++;
4677c478bd9Sstevel@tonic-gate 	} else 	/* editing with no file so set savtime to 0 */
4687c478bd9Sstevel@tonic-gate 		savtime = 0;
4697c478bd9Sstevel@tonic-gate 	eflg++;
4707c478bd9Sstevel@tonic-gate 	if ((tfname = tempnam("", "ea")) == NULL) {
4717c478bd9Sstevel@tonic-gate 		puts(gettext(msgtab[69]));
4727c478bd9Sstevel@tonic-gate 		exit(2);
4737c478bd9Sstevel@tonic-gate 	}
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 	fendcore = (LINE)sbrk(0);
4767c478bd9Sstevel@tonic-gate 	init();
4777c478bd9Sstevel@tonic-gate 	if (oldintr != SIG_IGN)
4787c478bd9Sstevel@tonic-gate 		signal(SIGINT, onintr);
4797c478bd9Sstevel@tonic-gate 	if (oldhup != SIG_IGN)
4807c478bd9Sstevel@tonic-gate 		signal(SIGHUP, onhup);
4817c478bd9Sstevel@tonic-gate 	setjmp(savej);
4827c478bd9Sstevel@tonic-gate 	commands();
4837c478bd9Sstevel@tonic-gate 	quit(sig);
4847c478bd9Sstevel@tonic-gate 	return (0);
4857c478bd9Sstevel@tonic-gate }
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate static void
commands(void)4887c478bd9Sstevel@tonic-gate commands(void)
4897c478bd9Sstevel@tonic-gate {
4907c478bd9Sstevel@tonic-gate 	LINE a1;
4917c478bd9Sstevel@tonic-gate 	int c;
4927c478bd9Sstevel@tonic-gate 	char *p1, *p2;
4937c478bd9Sstevel@tonic-gate 	int fsave, m, n;
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate 	for (;;) {
4967c478bd9Sstevel@tonic-gate 	nodelim = 0;
4977c478bd9Sstevel@tonic-gate 	if (pflag) {
4987c478bd9Sstevel@tonic-gate 		pflag = 0;
4997c478bd9Sstevel@tonic-gate 		addr1 = addr2 = dot;
5007c478bd9Sstevel@tonic-gate 		goto print;
5017c478bd9Sstevel@tonic-gate 	}
5027c478bd9Sstevel@tonic-gate 	if (shflg && globp == 0)
5037c478bd9Sstevel@tonic-gate 		write(1, gettext(prompt), strlen(gettext(prompt)));
5047c478bd9Sstevel@tonic-gate 	addr1 = 0;
5057c478bd9Sstevel@tonic-gate 	addr2 = 0;
5067c478bd9Sstevel@tonic-gate 	if ((c = getchr()) == ',') {
5077c478bd9Sstevel@tonic-gate 		addr1 = zero + 1;
5087c478bd9Sstevel@tonic-gate 		addr2 = dol;
5097c478bd9Sstevel@tonic-gate #ifdef XPG6
5107c478bd9Sstevel@tonic-gate 	/* XPG4 - it was an error if the second address was */
5117c478bd9Sstevel@tonic-gate 	/* input and the first address was ommitted	*/
5127c478bd9Sstevel@tonic-gate 	/* Parse second address	*/
5137c478bd9Sstevel@tonic-gate 		if ((a1 = address()) != 0) {
5147c478bd9Sstevel@tonic-gate 			addr2 = a1;
5157c478bd9Sstevel@tonic-gate 		}
5167c478bd9Sstevel@tonic-gate #endif
5177c478bd9Sstevel@tonic-gate 		c = getchr();
5187c478bd9Sstevel@tonic-gate 		goto swch;
5197c478bd9Sstevel@tonic-gate 	} else if (c == ';') {
5207c478bd9Sstevel@tonic-gate 		addr1 = dot;
5217c478bd9Sstevel@tonic-gate 		addr2 = dol;
5227c478bd9Sstevel@tonic-gate #ifdef XPG6
5237c478bd9Sstevel@tonic-gate 	/* XPG4 - it was an error if the second address was */
5247c478bd9Sstevel@tonic-gate 	/* input and the first address was ommitted	*/
5257c478bd9Sstevel@tonic-gate 	/* Parse second address	*/
5267c478bd9Sstevel@tonic-gate 		if ((a1 = address()) != 0) {
5277c478bd9Sstevel@tonic-gate 			addr2 = a1;
5287c478bd9Sstevel@tonic-gate 		}
5297c478bd9Sstevel@tonic-gate #endif
5307c478bd9Sstevel@tonic-gate 		c = getchr();
5317c478bd9Sstevel@tonic-gate 		goto swch;
5327c478bd9Sstevel@tonic-gate 	} else
5337c478bd9Sstevel@tonic-gate 		peekc = c;
5347c478bd9Sstevel@tonic-gate 	do {
5357c478bd9Sstevel@tonic-gate 		addr1 = addr2;
5367c478bd9Sstevel@tonic-gate 		if ((a1 = address()) == 0) {
5377c478bd9Sstevel@tonic-gate 			c = getchr();
5387c478bd9Sstevel@tonic-gate 			break;
5397c478bd9Sstevel@tonic-gate 		}
5407c478bd9Sstevel@tonic-gate 		addr2 = a1;
5417c478bd9Sstevel@tonic-gate 		if ((c = getchr()) == ';') {
5427c478bd9Sstevel@tonic-gate 			c = ',';
5437c478bd9Sstevel@tonic-gate 			dot = a1;
5447c478bd9Sstevel@tonic-gate 		}
5457c478bd9Sstevel@tonic-gate 	} while (c == ',');
5467c478bd9Sstevel@tonic-gate 	if (addr1 == 0)
5477c478bd9Sstevel@tonic-gate 		addr1 = addr2;
5487c478bd9Sstevel@tonic-gate swch:
5497c478bd9Sstevel@tonic-gate 	switch (c) {
5507c478bd9Sstevel@tonic-gate 
5517c478bd9Sstevel@tonic-gate 	case 'a':
5527c478bd9Sstevel@tonic-gate 		setdot();
5537c478bd9Sstevel@tonic-gate 		newline();
5547c478bd9Sstevel@tonic-gate 		if (!globflg) save();
5557c478bd9Sstevel@tonic-gate 		append(gettty, addr2);
5567c478bd9Sstevel@tonic-gate 		continue;
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate 	case 'c':
5597c478bd9Sstevel@tonic-gate #ifdef XPG6
5607c478bd9Sstevel@tonic-gate 		setzeroasone();
5617c478bd9Sstevel@tonic-gate #endif
5627c478bd9Sstevel@tonic-gate 		delete();
5637c478bd9Sstevel@tonic-gate 		append(gettty, addr1-1);
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate 		/* XPG4 - If no new lines are inserted, then the current */
5667c478bd9Sstevel@tonic-gate 		/* line becomes the line after the lines deleted. */
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 		if (((linebuf[0] != '.') || (dot == (addr1-1))) &&
5697c478bd9Sstevel@tonic-gate 			(addr2 <= dol))
5707c478bd9Sstevel@tonic-gate 			dot = addr1;
5717c478bd9Sstevel@tonic-gate 		continue;
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 	case 'd':
5747c478bd9Sstevel@tonic-gate 		delete();
5757c478bd9Sstevel@tonic-gate 		continue;
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 	case 'E':
5787c478bd9Sstevel@tonic-gate 		fchange = 0;
5797c478bd9Sstevel@tonic-gate 		c = 'e';
5807c478bd9Sstevel@tonic-gate 	case 'e':
5817c478bd9Sstevel@tonic-gate 		fflg++;
5827c478bd9Sstevel@tonic-gate 		setnoaddr();
5837c478bd9Sstevel@tonic-gate 		if (vflag && fchange) {
5847c478bd9Sstevel@tonic-gate 			fchange = 0;
5857c478bd9Sstevel@tonic-gate 			(void) error(1);
5867c478bd9Sstevel@tonic-gate 		}
5877c478bd9Sstevel@tonic-gate 		filename(c);
5887c478bd9Sstevel@tonic-gate 		eflg++;
5897c478bd9Sstevel@tonic-gate 		init();
5907c478bd9Sstevel@tonic-gate 		addr2 = zero;
5917c478bd9Sstevel@tonic-gate 		goto caseread;
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 	case 'f':
5947c478bd9Sstevel@tonic-gate 		setnoaddr();
5957c478bd9Sstevel@tonic-gate 		filename(c);
5967c478bd9Sstevel@tonic-gate 		if (!ncflg)  /* there is a filename */
5977c478bd9Sstevel@tonic-gate 			getime();
5987c478bd9Sstevel@tonic-gate 		else
5997c478bd9Sstevel@tonic-gate 			ncflg--;
6007c478bd9Sstevel@tonic-gate 		puts(savedfile);
6017c478bd9Sstevel@tonic-gate 		continue;
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 	case 'g':
6047c478bd9Sstevel@tonic-gate 		global(1);
6057c478bd9Sstevel@tonic-gate 		continue;
6067c478bd9Sstevel@tonic-gate 	case 'G':
6077c478bd9Sstevel@tonic-gate 		globaln(1);
6087c478bd9Sstevel@tonic-gate 		continue;
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 	case 'h':
6117c478bd9Sstevel@tonic-gate 		newline();
6127c478bd9Sstevel@tonic-gate 		setnoaddr();
6137c478bd9Sstevel@tonic-gate 		PUTM();
6147c478bd9Sstevel@tonic-gate 		continue;
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate 	case 'H':
6177c478bd9Sstevel@tonic-gate 		newline();
6187c478bd9Sstevel@tonic-gate 		setnoaddr();
6197c478bd9Sstevel@tonic-gate 		if (!hflag) {
6207c478bd9Sstevel@tonic-gate 			hflag = 1;
6217c478bd9Sstevel@tonic-gate 			PUTM();
6227c478bd9Sstevel@tonic-gate 		}
6237c478bd9Sstevel@tonic-gate 		else
6247c478bd9Sstevel@tonic-gate 			hflag = 0;
6257c478bd9Sstevel@tonic-gate 		continue;
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate 	case 'i':
6287c478bd9Sstevel@tonic-gate #ifdef XPG6
6297c478bd9Sstevel@tonic-gate 		setzeroasone();
6307c478bd9Sstevel@tonic-gate #endif
6317c478bd9Sstevel@tonic-gate 		setdot();
6327c478bd9Sstevel@tonic-gate 		nonzero();
6337c478bd9Sstevel@tonic-gate 		newline();
6347c478bd9Sstevel@tonic-gate 		if (!globflg) save();
6357c478bd9Sstevel@tonic-gate 		append(gettty, addr2-1);
6367c478bd9Sstevel@tonic-gate 		if (dot == addr2-1)
6377c478bd9Sstevel@tonic-gate 			dot += 1;
6387c478bd9Sstevel@tonic-gate 		continue;
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate 	case 'j':
6417c478bd9Sstevel@tonic-gate 		if (addr2 == 0) {
6427c478bd9Sstevel@tonic-gate 			addr1 = dot;
6437c478bd9Sstevel@tonic-gate 			addr2 = dot+1;
6447c478bd9Sstevel@tonic-gate 		}
6457c478bd9Sstevel@tonic-gate 		setdot();
6467c478bd9Sstevel@tonic-gate 		newline();
6477c478bd9Sstevel@tonic-gate 		nonzero();
6487c478bd9Sstevel@tonic-gate 		if (!globflg) save();
6497c478bd9Sstevel@tonic-gate 		join();
6507c478bd9Sstevel@tonic-gate 		continue;
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate 	case 'k':
6537c478bd9Sstevel@tonic-gate 		if ((c = getchr()) < 'a' || c > 'z')
6547c478bd9Sstevel@tonic-gate 			(void) error(2);
6557c478bd9Sstevel@tonic-gate 		newline();
6567c478bd9Sstevel@tonic-gate 		setdot();
6577c478bd9Sstevel@tonic-gate 		nonzero();
6587c478bd9Sstevel@tonic-gate 		names[c-'a'] = addr2->cur & ~01;
6597c478bd9Sstevel@tonic-gate 		anymarks |= 01;
6607c478bd9Sstevel@tonic-gate 		continue;
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate 	case 'm':
6637c478bd9Sstevel@tonic-gate 		move(0);
6647c478bd9Sstevel@tonic-gate 		continue;
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate 	case '\n':
6677c478bd9Sstevel@tonic-gate 		if (addr2 == 0)
6687c478bd9Sstevel@tonic-gate 			addr2 = dot+1;
6697c478bd9Sstevel@tonic-gate 		addr1 = addr2;
6707c478bd9Sstevel@tonic-gate 		goto print;
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate 	case 'n':
6737c478bd9Sstevel@tonic-gate 		listn++;
6747c478bd9Sstevel@tonic-gate 		newline();
6757c478bd9Sstevel@tonic-gate 		goto print;
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 	case 'l':
6787c478bd9Sstevel@tonic-gate 		listf++;
6797c478bd9Sstevel@tonic-gate 	case 'p':
6807c478bd9Sstevel@tonic-gate 		newline();
6817c478bd9Sstevel@tonic-gate 	print:
6827c478bd9Sstevel@tonic-gate 		setdot();
6837c478bd9Sstevel@tonic-gate 		nonzero();
6847c478bd9Sstevel@tonic-gate 		a1 = addr1;
6857c478bd9Sstevel@tonic-gate 		do {
6867c478bd9Sstevel@tonic-gate 			if (listn) {
6877c478bd9Sstevel@tonic-gate 				count = a1 - zero;
6887c478bd9Sstevel@tonic-gate 				putd();
6897c478bd9Sstevel@tonic-gate 				putchr('\t');
6907c478bd9Sstevel@tonic-gate 			}
69123a1cceaSRoger A. Faulkner 			puts(getaline((a1++)->cur));
6927c478bd9Sstevel@tonic-gate 		}
6937c478bd9Sstevel@tonic-gate 		while (a1 <= addr2);
6947c478bd9Sstevel@tonic-gate 		dot = addr2;
6957c478bd9Sstevel@tonic-gate 		pflag = 0;
6967c478bd9Sstevel@tonic-gate 		listn = 0;
6977c478bd9Sstevel@tonic-gate 		listf = 0;
6987c478bd9Sstevel@tonic-gate 		continue;
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate 	case 'Q':
7017c478bd9Sstevel@tonic-gate 		fchange = 0;
7027c478bd9Sstevel@tonic-gate 	case 'q':
7037c478bd9Sstevel@tonic-gate 		setnoaddr();
7047c478bd9Sstevel@tonic-gate 		newline();
7057c478bd9Sstevel@tonic-gate 		quit(sig);
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate 	case 'r':
7087c478bd9Sstevel@tonic-gate 		filename(c);
7097c478bd9Sstevel@tonic-gate 	caseread:
7107c478bd9Sstevel@tonic-gate 		readflg = 1;
7117c478bd9Sstevel@tonic-gate 		save28 = (dol != fendcore);
7127c478bd9Sstevel@tonic-gate 		if (crflag == 2 || crflag == -2)
7137c478bd9Sstevel@tonic-gate 			crflag = -1; /* restore crflag for next file */
7147c478bd9Sstevel@tonic-gate 		errno = 0;
7157c478bd9Sstevel@tonic-gate 		if ((io = eopen(file, O_RDONLY)) < 0) {
7167c478bd9Sstevel@tonic-gate 			lastc = '\n';
7177c478bd9Sstevel@tonic-gate 			/* if first entering editor and file does not exist */
7187c478bd9Sstevel@tonic-gate 			/* set saved access time to 0 */
7197c478bd9Sstevel@tonic-gate 			if (eflg) {
7207c478bd9Sstevel@tonic-gate 				savtime = 0;
7217c478bd9Sstevel@tonic-gate 				eflg  = 0;
7227c478bd9Sstevel@tonic-gate 				if (c == 'e' && vflag == 0)
7237c478bd9Sstevel@tonic-gate 					qflg = 1;
7247c478bd9Sstevel@tonic-gate 			}
7257c478bd9Sstevel@tonic-gate 			if (errno == ENOENT) {
7267c478bd9Sstevel@tonic-gate 				(void) error(68);
7277c478bd9Sstevel@tonic-gate 			} else {
7287c478bd9Sstevel@tonic-gate 				(void) error(3);
7297c478bd9Sstevel@tonic-gate 			}
7307c478bd9Sstevel@tonic-gate 		}
7317c478bd9Sstevel@tonic-gate 		/* get last mod time of file */
7327c478bd9Sstevel@tonic-gate 		/* eflg - entered editor with ed or e  */
7337c478bd9Sstevel@tonic-gate 		if (eflg) {
7347c478bd9Sstevel@tonic-gate 			eflg = 0;
7357c478bd9Sstevel@tonic-gate 			getime();
7367c478bd9Sstevel@tonic-gate 		}
7377c478bd9Sstevel@tonic-gate 		setall();
7387c478bd9Sstevel@tonic-gate 		ninbuf = 0;
7397c478bd9Sstevel@tonic-gate 		n = zero != dol;
7407c478bd9Sstevel@tonic-gate #ifdef NULLS
7417c478bd9Sstevel@tonic-gate 		nulls = 0;
7427c478bd9Sstevel@tonic-gate #endif
7437c478bd9Sstevel@tonic-gate 		if (!globflg && (c == 'r')) save();
7447c478bd9Sstevel@tonic-gate 		append(getfile, addr2);
7457c478bd9Sstevel@tonic-gate 		exfile();
7467c478bd9Sstevel@tonic-gate 		readflg = 0;
7477c478bd9Sstevel@tonic-gate 		fchange = n;
7487c478bd9Sstevel@tonic-gate 		continue;
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 	case 's':
7517c478bd9Sstevel@tonic-gate 		setdot();
7527c478bd9Sstevel@tonic-gate 		nonzero();
7537c478bd9Sstevel@tonic-gate 		if (!globflg) save();
7547c478bd9Sstevel@tonic-gate 		substitute(globp != 0);
7557c478bd9Sstevel@tonic-gate 		continue;
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate 	case 't':
7587c478bd9Sstevel@tonic-gate 		move(1);
7597c478bd9Sstevel@tonic-gate 		continue;
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate 	case 'u':
7627c478bd9Sstevel@tonic-gate 		setdot();
7637c478bd9Sstevel@tonic-gate 		newline();
7647c478bd9Sstevel@tonic-gate 		if (!initflg)
7657c478bd9Sstevel@tonic-gate 			undo();
7667c478bd9Sstevel@tonic-gate 		else
7677c478bd9Sstevel@tonic-gate 			(void) error(5);
7687c478bd9Sstevel@tonic-gate 		fchange = 1;
7697c478bd9Sstevel@tonic-gate 		continue;
7707c478bd9Sstevel@tonic-gate 
7717c478bd9Sstevel@tonic-gate 	case 'v':
7727c478bd9Sstevel@tonic-gate 		global(0);
7737c478bd9Sstevel@tonic-gate 		continue;
7747c478bd9Sstevel@tonic-gate 	case 'V':
7757c478bd9Sstevel@tonic-gate 		globaln(0);
7767c478bd9Sstevel@tonic-gate 		continue;
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate 	case 'W':
7797c478bd9Sstevel@tonic-gate 	case 'w':
7807c478bd9Sstevel@tonic-gate 		if (flag28) {
7817c478bd9Sstevel@tonic-gate 			flag28 = 0;
7827c478bd9Sstevel@tonic-gate 			fchange = 0;
7837c478bd9Sstevel@tonic-gate 			(void) error(61);
7847c478bd9Sstevel@tonic-gate 		}
7857c478bd9Sstevel@tonic-gate 		setall();
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate 		/* on NULL-RE condition do not generate error */
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate 		if ((linebuf[0] != '.') && (zero != dol) &&
7907c478bd9Sstevel@tonic-gate 			(addr1 <= zero || addr2 > dol))
7917c478bd9Sstevel@tonic-gate 			(void) error(15);
7927c478bd9Sstevel@tonic-gate 		filename(c);
7937c478bd9Sstevel@tonic-gate 		if (Xqt) {
7947c478bd9Sstevel@tonic-gate 			io = eopen(file, O_WRONLY);
7957c478bd9Sstevel@tonic-gate 			n = 1;	/* set n so newtime will not execute */
7967c478bd9Sstevel@tonic-gate 		} else {
7977c478bd9Sstevel@tonic-gate 			struct stat lFl;
7987c478bd9Sstevel@tonic-gate 			fstat(tfile, &Tf);
7997c478bd9Sstevel@tonic-gate 			if (stat(file, &Fl) < 0) {
8007c478bd9Sstevel@tonic-gate 				if ((io = creat(file, S_IRUSR|S_IWUSR|S_IRGRP
8017c478bd9Sstevel@tonic-gate 					|S_IWGRP|S_IROTH|S_IWOTH)) < 0)
8027c478bd9Sstevel@tonic-gate 					(void) error(7);
8037c478bd9Sstevel@tonic-gate 				fstat(io, &Fl);
8047c478bd9Sstevel@tonic-gate 				Fl.st_mtime = 0;
8057c478bd9Sstevel@tonic-gate 				lFl = Fl;
8067c478bd9Sstevel@tonic-gate 				close(io);
8077c478bd9Sstevel@tonic-gate 			} else {
8087c478bd9Sstevel@tonic-gate #ifndef	RESEARCH
8097c478bd9Sstevel@tonic-gate 				oldmask = umask(0);
8107c478bd9Sstevel@tonic-gate 				/*
8117c478bd9Sstevel@tonic-gate 				 * Must determine if file is
8127c478bd9Sstevel@tonic-gate 				 * a symbolic link
8137c478bd9Sstevel@tonic-gate 				 */
8147c478bd9Sstevel@tonic-gate 				lstat(file, &lFl);
8157c478bd9Sstevel@tonic-gate #endif
8167c478bd9Sstevel@tonic-gate 			}
8177c478bd9Sstevel@tonic-gate #ifndef RESEARCH
8187c478bd9Sstevel@tonic-gate 			/*
8197c478bd9Sstevel@tonic-gate 			 * Determine if there are enough free blocks on system
8207c478bd9Sstevel@tonic-gate 			 */
8217c478bd9Sstevel@tonic-gate 			if (!Short && statvfs(file, &U) == 0 &&
8227c478bd9Sstevel@tonic-gate 			    U.f_bfree < ((Tf.st_size/U.f_frsize) + 100)) {
8237c478bd9Sstevel@tonic-gate 				Short = 1;
8247c478bd9Sstevel@tonic-gate 				(void) error(8);
8257c478bd9Sstevel@tonic-gate 			}
8267c478bd9Sstevel@tonic-gate 			Short = 0;
8277c478bd9Sstevel@tonic-gate #endif
8287c478bd9Sstevel@tonic-gate 			p1 = savedfile;		/* The current filename */
8297c478bd9Sstevel@tonic-gate 			p2 = file;
8307c478bd9Sstevel@tonic-gate 			m = strcmp(p1, p2);
8317c478bd9Sstevel@tonic-gate 			if (c == 'w' && Fl.st_nlink == 1 && ISREG(lFl)) {
8327c478bd9Sstevel@tonic-gate 				if (close(open(file, O_WRONLY)) < 0)
8337c478bd9Sstevel@tonic-gate 					(void) error(9);
8347c478bd9Sstevel@tonic-gate 				if (!(n = m))
8357c478bd9Sstevel@tonic-gate 					chktime();
8367c478bd9Sstevel@tonic-gate 				mkfunny();
8377c478bd9Sstevel@tonic-gate 				/*
8387c478bd9Sstevel@tonic-gate 				 * If funlink equals one it means that
8397c478bd9Sstevel@tonic-gate 				 * funny points to a valid file which must
8407c478bd9Sstevel@tonic-gate 				 * be unlinked when interrupted.
8417c478bd9Sstevel@tonic-gate 				 */
8427c478bd9Sstevel@tonic-gate 
8437c478bd9Sstevel@tonic-gate 				funlink = 1;
8447c478bd9Sstevel@tonic-gate 				if ((io = creat(funny, FMODE(Fl))) >= 0) {
8457c478bd9Sstevel@tonic-gate 					chown(funny, Fl.st_uid, Fl.st_gid);
8467c478bd9Sstevel@tonic-gate 					chmod(funny, FMODE(Fl));
8477c478bd9Sstevel@tonic-gate 					putfile();
8487c478bd9Sstevel@tonic-gate 					exfile();
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate 					if (rename(funny, file))
8517c478bd9Sstevel@tonic-gate 						(void) error(10);
8527c478bd9Sstevel@tonic-gate 					funlink = 0;
8537c478bd9Sstevel@tonic-gate 					/* if filenames are the same */
8547c478bd9Sstevel@tonic-gate 					if (!n)
8557c478bd9Sstevel@tonic-gate 						newtime();
8567c478bd9Sstevel@tonic-gate 					/* check if entire buffer was written */
8577c478bd9Sstevel@tonic-gate 					fsave = fchange;
8587c478bd9Sstevel@tonic-gate 		fchange = (((addr1 == zero) || (addr1 == (zero + 1))) &&
8597c478bd9Sstevel@tonic-gate 			(addr2 == dol)) ? 0 : 1;
8607c478bd9Sstevel@tonic-gate 			if (fchange == 1 && m != 0) fchange = fsave;
8617c478bd9Sstevel@tonic-gate 					continue;
8627c478bd9Sstevel@tonic-gate 				}
8637c478bd9Sstevel@tonic-gate 			} else
8647c478bd9Sstevel@tonic-gate 				n = 1;	/* set n so newtime will not execute */
8657c478bd9Sstevel@tonic-gate 			if ((io = open(file,
8667c478bd9Sstevel@tonic-gate 				(c == 'w') ? O_WRONLY|O_CREAT|O_TRUNC
8677c478bd9Sstevel@tonic-gate 				: O_WRONLY|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR
8687c478bd9Sstevel@tonic-gate 				|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)) < 0)
8697c478bd9Sstevel@tonic-gate 				(void) error(7);
8707c478bd9Sstevel@tonic-gate 		}
8717c478bd9Sstevel@tonic-gate 		putfile();
8727c478bd9Sstevel@tonic-gate 		exfile();
8737c478bd9Sstevel@tonic-gate 		if (!n) newtime();
8747c478bd9Sstevel@tonic-gate 		fsave = fchange;
8757c478bd9Sstevel@tonic-gate 		fchange = (((addr1 == zero) || (addr1 == (zero + 1))) &&
8767c478bd9Sstevel@tonic-gate 			(addr2 == dol)) ? 0 : 1;
8777c478bd9Sstevel@tonic-gate 	/* Leave fchange alone if partial write was to another file */
8787c478bd9Sstevel@tonic-gate 		if (fchange == 1 && m != 0) fchange = fsave;
8797c478bd9Sstevel@tonic-gate 		continue;
8807c478bd9Sstevel@tonic-gate 
8817c478bd9Sstevel@tonic-gate 	case 'C':
8827c478bd9Sstevel@tonic-gate 		crflag = 1;
8837c478bd9Sstevel@tonic-gate 			/*
8847c478bd9Sstevel@tonic-gate 			 * C is same as X, but always assume input files are
8857c478bd9Sstevel@tonic-gate 			 * ciphertext
8867c478bd9Sstevel@tonic-gate 			 */
8877c478bd9Sstevel@tonic-gate 		goto encrypt;
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate 	case 'X':
8907c478bd9Sstevel@tonic-gate 		crflag = -1;
8917c478bd9Sstevel@tonic-gate encrypt:
8927c478bd9Sstevel@tonic-gate 		setnoaddr();
8937c478bd9Sstevel@tonic-gate 		newline();
8947c478bd9Sstevel@tonic-gate 		xflag = 1;
8957c478bd9Sstevel@tonic-gate 		if (permflag)
8967c478bd9Sstevel@tonic-gate 			(void) crypt_close(perm);
8977c478bd9Sstevel@tonic-gate 		permflag = 1;
8987c478bd9Sstevel@tonic-gate 		if ((kflag = run_setkey(&perm[0], getkey(msgtab[66])))
8997c478bd9Sstevel@tonic-gate 			== -1) {
9007c478bd9Sstevel@tonic-gate 			xflag = 0;
9017c478bd9Sstevel@tonic-gate 			kflag = 0;
9027c478bd9Sstevel@tonic-gate 			crflag = 0;
9037c478bd9Sstevel@tonic-gate 			(void) error(64);
9047c478bd9Sstevel@tonic-gate 		}
9057c478bd9Sstevel@tonic-gate 		if (kflag == 0)
9067c478bd9Sstevel@tonic-gate 			crflag = 0;
9077c478bd9Sstevel@tonic-gate 		continue;
9087c478bd9Sstevel@tonic-gate 
9097c478bd9Sstevel@tonic-gate 	case '=':
9107c478bd9Sstevel@tonic-gate 		setall();
9117c478bd9Sstevel@tonic-gate 		newline();
9127c478bd9Sstevel@tonic-gate 		count = (addr2-zero)&077777;
9137c478bd9Sstevel@tonic-gate 		putd();
9147c478bd9Sstevel@tonic-gate 		putchr('\n');
9157c478bd9Sstevel@tonic-gate 		continue;
9167c478bd9Sstevel@tonic-gate 
9177c478bd9Sstevel@tonic-gate 	case '!':
9187c478bd9Sstevel@tonic-gate 		unixcom();
9197c478bd9Sstevel@tonic-gate 		continue;
9207c478bd9Sstevel@tonic-gate 
9217c478bd9Sstevel@tonic-gate 	case EOF:
9227c478bd9Sstevel@tonic-gate 		return;
9237c478bd9Sstevel@tonic-gate 
9247c478bd9Sstevel@tonic-gate 	case 'P':
9257c478bd9Sstevel@tonic-gate 		setnoaddr();
9267c478bd9Sstevel@tonic-gate 		newline();
9277c478bd9Sstevel@tonic-gate 		if (shflg)
9287c478bd9Sstevel@tonic-gate 			shflg = 0;
9297c478bd9Sstevel@tonic-gate 		else
9307c478bd9Sstevel@tonic-gate 			shflg++;
9317c478bd9Sstevel@tonic-gate 		continue;
9327c478bd9Sstevel@tonic-gate 	}
9337c478bd9Sstevel@tonic-gate 	if (c == 'x')
9347c478bd9Sstevel@tonic-gate 		(void) error(60);
9357c478bd9Sstevel@tonic-gate 	else
9367c478bd9Sstevel@tonic-gate 		(void) error(12);
9377c478bd9Sstevel@tonic-gate 	}
9387c478bd9Sstevel@tonic-gate }
9397c478bd9Sstevel@tonic-gate 
9407c478bd9Sstevel@tonic-gate LINE
address(void)9417c478bd9Sstevel@tonic-gate address(void)
9427c478bd9Sstevel@tonic-gate {
9437c478bd9Sstevel@tonic-gate 	int minus, c;
9447c478bd9Sstevel@tonic-gate 	LINE a1;
9457c478bd9Sstevel@tonic-gate 	int n, relerr, retval;
9467c478bd9Sstevel@tonic-gate 
9477c478bd9Sstevel@tonic-gate 	minus = 0;
9487c478bd9Sstevel@tonic-gate 	a1 = 0;
9497c478bd9Sstevel@tonic-gate 	for (;;) {
9507c478bd9Sstevel@tonic-gate 		c = getchr();
9517c478bd9Sstevel@tonic-gate 		if ('0' <= c && c <= '9') {
9527c478bd9Sstevel@tonic-gate 			n = 0;
9537c478bd9Sstevel@tonic-gate 			do {
9547c478bd9Sstevel@tonic-gate 				n *= 10;
9557c478bd9Sstevel@tonic-gate 				n += c - '0';
9567c478bd9Sstevel@tonic-gate 			} while ((c = getchr()) >= '0' && c <= '9');
9577c478bd9Sstevel@tonic-gate 			peekc = c;
9587c478bd9Sstevel@tonic-gate 			if (a1 == 0)
9597c478bd9Sstevel@tonic-gate 				a1 = zero;
9607c478bd9Sstevel@tonic-gate 			if (minus < 0)
9617c478bd9Sstevel@tonic-gate 				n = -n;
9627c478bd9Sstevel@tonic-gate 			a1 += n;
9637c478bd9Sstevel@tonic-gate 			minus = 0;
9647c478bd9Sstevel@tonic-gate 			continue;
9657c478bd9Sstevel@tonic-gate 		}
9667c478bd9Sstevel@tonic-gate 		relerr = 0;
9677c478bd9Sstevel@tonic-gate 		if (a1 || minus)
9687c478bd9Sstevel@tonic-gate 			relerr++;
9697c478bd9Sstevel@tonic-gate 		switch (c) {
9707c478bd9Sstevel@tonic-gate 		case ' ':
9717c478bd9Sstevel@tonic-gate 		case '\t':
9727c478bd9Sstevel@tonic-gate 			continue;
9737c478bd9Sstevel@tonic-gate 
9747c478bd9Sstevel@tonic-gate 		case '+':
9757c478bd9Sstevel@tonic-gate 			minus++;
9767c478bd9Sstevel@tonic-gate 			if (a1 == 0)
9777c478bd9Sstevel@tonic-gate 				a1 = dot;
9787c478bd9Sstevel@tonic-gate 			continue;
9797c478bd9Sstevel@tonic-gate 
9807c478bd9Sstevel@tonic-gate 		case '-':
9817c478bd9Sstevel@tonic-gate 		case '^':
9827c478bd9Sstevel@tonic-gate 			minus--;
9837c478bd9Sstevel@tonic-gate 			if (a1 == 0)
9847c478bd9Sstevel@tonic-gate 				a1 = dot;
9857c478bd9Sstevel@tonic-gate 			continue;
9867c478bd9Sstevel@tonic-gate 
9877c478bd9Sstevel@tonic-gate 		case '?':
9887c478bd9Sstevel@tonic-gate 		case '/':
9897c478bd9Sstevel@tonic-gate 			comple(c);
9907c478bd9Sstevel@tonic-gate 			a1 = dot;
9917c478bd9Sstevel@tonic-gate 			for (;;) {
9927c478bd9Sstevel@tonic-gate 				if (c == '/') {
9937c478bd9Sstevel@tonic-gate 					a1++;
9947c478bd9Sstevel@tonic-gate 					if (a1 > dol)
9957c478bd9Sstevel@tonic-gate 						a1 = zero;
9967c478bd9Sstevel@tonic-gate 				} else {
9977c478bd9Sstevel@tonic-gate 					a1--;
9987c478bd9Sstevel@tonic-gate 					if (a1 < zero)
9997c478bd9Sstevel@tonic-gate 						a1 = dol;
10007c478bd9Sstevel@tonic-gate 				}
10017c478bd9Sstevel@tonic-gate 
10027c478bd9Sstevel@tonic-gate 				if (execute(0, a1))
10037c478bd9Sstevel@tonic-gate 					break;
10047c478bd9Sstevel@tonic-gate 				if (a1 == dot)
10057c478bd9Sstevel@tonic-gate 					(void) error(13);
10067c478bd9Sstevel@tonic-gate 			}
10077c478bd9Sstevel@tonic-gate 			break;
10087c478bd9Sstevel@tonic-gate 
10097c478bd9Sstevel@tonic-gate 		case '$':
10107c478bd9Sstevel@tonic-gate 			a1 = dol;
10117c478bd9Sstevel@tonic-gate 			break;
10127c478bd9Sstevel@tonic-gate 
10137c478bd9Sstevel@tonic-gate 		case '.':
10147c478bd9Sstevel@tonic-gate 			a1 = dot;
10157c478bd9Sstevel@tonic-gate 			break;
10167c478bd9Sstevel@tonic-gate 
10177c478bd9Sstevel@tonic-gate 		case '\'':
10187c478bd9Sstevel@tonic-gate 			if ((c = getchr()) < 'a' || c > 'z')
10197c478bd9Sstevel@tonic-gate 				(void) error(2);
10207c478bd9Sstevel@tonic-gate 			for (a1 = zero; a1 <= dol; a1++)
10217c478bd9Sstevel@tonic-gate 				if (names[c-'a'] == (a1->cur & ~01))
10227c478bd9Sstevel@tonic-gate 					break;
10237c478bd9Sstevel@tonic-gate 			break;
10247c478bd9Sstevel@tonic-gate 
10257c478bd9Sstevel@tonic-gate 		default:
10267c478bd9Sstevel@tonic-gate 			peekc = c;
10277c478bd9Sstevel@tonic-gate 			if (a1 == 0)
10287c478bd9Sstevel@tonic-gate 				return (0);
10297c478bd9Sstevel@tonic-gate 			a1 += minus;
10307c478bd9Sstevel@tonic-gate 
10317c478bd9Sstevel@tonic-gate 			/* on NULL-RE condition do not generate error */
10327c478bd9Sstevel@tonic-gate 
10337c478bd9Sstevel@tonic-gate 			if ((linebuf[0] != '.') && (a1 < zero || a1 > dol))
10347c478bd9Sstevel@tonic-gate 				(void) error(15);
10357c478bd9Sstevel@tonic-gate 			return (a1);
10367c478bd9Sstevel@tonic-gate 		}
10377c478bd9Sstevel@tonic-gate 		if (relerr)
10387c478bd9Sstevel@tonic-gate 			(void) error(16);
10397c478bd9Sstevel@tonic-gate 	}
10407c478bd9Sstevel@tonic-gate }
10417c478bd9Sstevel@tonic-gate 
10427c478bd9Sstevel@tonic-gate static void
setdot(void)10437c478bd9Sstevel@tonic-gate setdot(void)
10447c478bd9Sstevel@tonic-gate {
10457c478bd9Sstevel@tonic-gate 	if (addr2 == 0)
10467c478bd9Sstevel@tonic-gate 		addr1 = addr2 = dot;
10477c478bd9Sstevel@tonic-gate 	if (addr1 > addr2)
10487c478bd9Sstevel@tonic-gate 		(void) error(17);
10497c478bd9Sstevel@tonic-gate }
10507c478bd9Sstevel@tonic-gate 
10517c478bd9Sstevel@tonic-gate static void
setall(void)10527c478bd9Sstevel@tonic-gate setall(void)
10537c478bd9Sstevel@tonic-gate {
10547c478bd9Sstevel@tonic-gate 	if (addr2 == 0) {
10557c478bd9Sstevel@tonic-gate 		addr1 = zero+1;
10567c478bd9Sstevel@tonic-gate 		addr2 = dol;
10577c478bd9Sstevel@tonic-gate 		if (dol == zero)
10587c478bd9Sstevel@tonic-gate 			addr1 = zero;
10597c478bd9Sstevel@tonic-gate 	}
10607c478bd9Sstevel@tonic-gate 	setdot();
10617c478bd9Sstevel@tonic-gate }
10627c478bd9Sstevel@tonic-gate 
10637c478bd9Sstevel@tonic-gate static void
setnoaddr(void)10647c478bd9Sstevel@tonic-gate setnoaddr(void)
10657c478bd9Sstevel@tonic-gate {
10667c478bd9Sstevel@tonic-gate 	if (addr2)
10677c478bd9Sstevel@tonic-gate 		(void) error(18);
10687c478bd9Sstevel@tonic-gate }
10697c478bd9Sstevel@tonic-gate 
10707c478bd9Sstevel@tonic-gate static void
nonzero(void)10717c478bd9Sstevel@tonic-gate nonzero(void)
10727c478bd9Sstevel@tonic-gate {
10737c478bd9Sstevel@tonic-gate 	/* on NULL-RE condition do not generate error */
10747c478bd9Sstevel@tonic-gate 
10757c478bd9Sstevel@tonic-gate 	if ((linebuf[0] != '.') && (addr1 <= zero || addr2 > dol))
10767c478bd9Sstevel@tonic-gate 		(void) error(15);
10777c478bd9Sstevel@tonic-gate }
10787c478bd9Sstevel@tonic-gate 
10797c478bd9Sstevel@tonic-gate static void
setzeroasone(void)10807c478bd9Sstevel@tonic-gate setzeroasone(void)
10817c478bd9Sstevel@tonic-gate {
10827c478bd9Sstevel@tonic-gate /* for the c and i commands 0 equal to 1 address */
10837c478bd9Sstevel@tonic-gate 	if (addr1 == zero) {
10847c478bd9Sstevel@tonic-gate 		addr1 = zero+1;
10857c478bd9Sstevel@tonic-gate 	}
10867c478bd9Sstevel@tonic-gate 	if (addr2 == zero) {
10877c478bd9Sstevel@tonic-gate 		addr2 = zero+1;
10887c478bd9Sstevel@tonic-gate 	}
10897c478bd9Sstevel@tonic-gate }
10907c478bd9Sstevel@tonic-gate 
10917c478bd9Sstevel@tonic-gate 
10927c478bd9Sstevel@tonic-gate static void
newline(void)10937c478bd9Sstevel@tonic-gate newline(void)
10947c478bd9Sstevel@tonic-gate {
10957c478bd9Sstevel@tonic-gate 	int c;
10967c478bd9Sstevel@tonic-gate 
10977c478bd9Sstevel@tonic-gate 	if ((c = getchr()) == '\n')
10987c478bd9Sstevel@tonic-gate 		return;
10997c478bd9Sstevel@tonic-gate 	if (c == 'p' || c == 'l' || c == 'n') {
11007c478bd9Sstevel@tonic-gate 		pflag++;
11017c478bd9Sstevel@tonic-gate 		if (c == 'l') listf++;
11027c478bd9Sstevel@tonic-gate 		if (c == 'n') listn++;
11037c478bd9Sstevel@tonic-gate 		if ((c = getchr()) == '\n')
11047c478bd9Sstevel@tonic-gate 			return;
11057c478bd9Sstevel@tonic-gate 	}
11067c478bd9Sstevel@tonic-gate 	(void) error(20);
11077c478bd9Sstevel@tonic-gate }
11087c478bd9Sstevel@tonic-gate 
11097c478bd9Sstevel@tonic-gate static void
filename(int comm)11107c478bd9Sstevel@tonic-gate filename(int comm)
11117c478bd9Sstevel@tonic-gate {
11127c478bd9Sstevel@tonic-gate 	char *p1, *p2;
11137c478bd9Sstevel@tonic-gate 	int c;
11147c478bd9Sstevel@tonic-gate 	int i = 0;
11157c478bd9Sstevel@tonic-gate 
11167c478bd9Sstevel@tonic-gate 	count = 0;
11177c478bd9Sstevel@tonic-gate 	c = getchr();
11187c478bd9Sstevel@tonic-gate 	if (c == '\n' || c == EOF) {
11197c478bd9Sstevel@tonic-gate 		p1 = savedfile;
11207c478bd9Sstevel@tonic-gate 		if (*p1 == 0 && comm != 'f')
11217c478bd9Sstevel@tonic-gate 			(void) error(21);
11227c478bd9Sstevel@tonic-gate 		/* ncflg set means do not get mod time of file */
11237c478bd9Sstevel@tonic-gate 		/* since no filename followed f */
11247c478bd9Sstevel@tonic-gate 		if (comm == 'f')
11257c478bd9Sstevel@tonic-gate 			ncflg++;
11267c478bd9Sstevel@tonic-gate 		p2 = file;
11277c478bd9Sstevel@tonic-gate 		while (*p2++ = *p1++);
11287c478bd9Sstevel@tonic-gate 		red(savedfile);
11297c478bd9Sstevel@tonic-gate 		return;
11307c478bd9Sstevel@tonic-gate 	}
11317c478bd9Sstevel@tonic-gate 	if (c != ' ')
11327c478bd9Sstevel@tonic-gate 		(void) error(22);
11337c478bd9Sstevel@tonic-gate 	while ((c = getchr()) == ' ');
11347c478bd9Sstevel@tonic-gate 	if (c == '!')
11357c478bd9Sstevel@tonic-gate 		++Xqt, c = getchr();
11367c478bd9Sstevel@tonic-gate 	if (c == '\n')
11377c478bd9Sstevel@tonic-gate 		(void) error(21);
11387c478bd9Sstevel@tonic-gate 	p1 = file;
11397c478bd9Sstevel@tonic-gate 	do {
11407c478bd9Sstevel@tonic-gate 		if (++i >= FNSIZE)
11417c478bd9Sstevel@tonic-gate 			(void) error(24);
11427c478bd9Sstevel@tonic-gate 		*p1++ = c;
11437c478bd9Sstevel@tonic-gate 		if (c == EOF || (c == ' ' && !Xqt))
11447c478bd9Sstevel@tonic-gate 			(void) error(21);
11457c478bd9Sstevel@tonic-gate 	} while ((c = getchr()) != '\n');
11467c478bd9Sstevel@tonic-gate 	*p1++ = 0;
11477c478bd9Sstevel@tonic-gate 	if (Xqt)
11487c478bd9Sstevel@tonic-gate 		if (comm == 'f') {
11497c478bd9Sstevel@tonic-gate 			--Xqt;
11507c478bd9Sstevel@tonic-gate 			(void) error(57);
11517c478bd9Sstevel@tonic-gate 		}
11527c478bd9Sstevel@tonic-gate 		else
11537c478bd9Sstevel@tonic-gate 			return;
11547c478bd9Sstevel@tonic-gate 	if (savedfile[0] == 0 || comm == 'e' || comm == 'f') {
11557c478bd9Sstevel@tonic-gate 		p1 = savedfile;
11567c478bd9Sstevel@tonic-gate 		p2 = file;
11577c478bd9Sstevel@tonic-gate 		while (*p1++ = *p2++);
11587c478bd9Sstevel@tonic-gate 	}
11597c478bd9Sstevel@tonic-gate 	red(file);
11607c478bd9Sstevel@tonic-gate }
11617c478bd9Sstevel@tonic-gate 
11627c478bd9Sstevel@tonic-gate 
11637c478bd9Sstevel@tonic-gate static void
exfile(void)11647c478bd9Sstevel@tonic-gate exfile(void)
11657c478bd9Sstevel@tonic-gate {
11667c478bd9Sstevel@tonic-gate #ifdef NULLS
11677c478bd9Sstevel@tonic-gate 	int c;
11687c478bd9Sstevel@tonic-gate #endif
11697c478bd9Sstevel@tonic-gate 
11707c478bd9Sstevel@tonic-gate #ifndef RESEARCH
11717c478bd9Sstevel@tonic-gate 	if (oldmask) {
11727c478bd9Sstevel@tonic-gate 		umask(oldmask);
11737c478bd9Sstevel@tonic-gate 		oldmask = 0;
11747c478bd9Sstevel@tonic-gate 	}
11757c478bd9Sstevel@tonic-gate #endif
11767c478bd9Sstevel@tonic-gate 	eclose(io);
11777c478bd9Sstevel@tonic-gate 	io = -1;
11787c478bd9Sstevel@tonic-gate 	if (vflag) {
11797c478bd9Sstevel@tonic-gate 		putd();
11807c478bd9Sstevel@tonic-gate 		putchr('\n');
11817c478bd9Sstevel@tonic-gate #ifdef NULLS
11827c478bd9Sstevel@tonic-gate 		if (nulls) {
11837c478bd9Sstevel@tonic-gate 			c = count;
11847c478bd9Sstevel@tonic-gate 			count = nulls;
11857c478bd9Sstevel@tonic-gate 			nulls = 0;
11867c478bd9Sstevel@tonic-gate 			putd();
11877c478bd9Sstevel@tonic-gate 			puts(gettext(" nulls replaced by '\\0'"));
11887c478bd9Sstevel@tonic-gate 			count = c;
11897c478bd9Sstevel@tonic-gate 		}
11907c478bd9Sstevel@tonic-gate #endif
11917c478bd9Sstevel@tonic-gate 	}
11927c478bd9Sstevel@tonic-gate }
11937c478bd9Sstevel@tonic-gate 
11947c478bd9Sstevel@tonic-gate static void
onintr(int sig)11957c478bd9Sstevel@tonic-gate onintr(int sig)
11967c478bd9Sstevel@tonic-gate {
11977c478bd9Sstevel@tonic-gate 	signal(SIGINT, onintr);
11987c478bd9Sstevel@tonic-gate 	putchr('\n');
11997c478bd9Sstevel@tonic-gate 	lastc = '\n';
12007c478bd9Sstevel@tonic-gate 	globflg = 0;
12017c478bd9Sstevel@tonic-gate 	if (funlink) unlink(funny); /* remove tmp file */
12027c478bd9Sstevel@tonic-gate 	/* if interrupted a read, only part of file may be in buffer */
12037c478bd9Sstevel@tonic-gate 	if (readflg) {
12047c478bd9Sstevel@tonic-gate 		sprintf(tstring, "\007read may be incomplete - beware!\007");
12057c478bd9Sstevel@tonic-gate 		puts(gettext(tstring));
12067c478bd9Sstevel@tonic-gate 		fchange = 0;
12077c478bd9Sstevel@tonic-gate 	}
12087c478bd9Sstevel@tonic-gate 	(void) error(26);
12097c478bd9Sstevel@tonic-gate }
12107c478bd9Sstevel@tonic-gate 
12117c478bd9Sstevel@tonic-gate static void
onhup(int sig)12127c478bd9Sstevel@tonic-gate onhup(int sig)
12137c478bd9Sstevel@tonic-gate {
12147c478bd9Sstevel@tonic-gate 	signal(SIGINT, SIG_IGN);
12157c478bd9Sstevel@tonic-gate 	signal(SIGHUP, SIG_IGN);
12167c478bd9Sstevel@tonic-gate 	/*
12177c478bd9Sstevel@tonic-gate 	 * if there are lines in file and file was not written
12187c478bd9Sstevel@tonic-gate 	 * since last update, save in ed.hup, or $HOME/ed.hup
12197c478bd9Sstevel@tonic-gate 	 */
12207c478bd9Sstevel@tonic-gate 	if (dol > zero && fchange == 1) {
12217c478bd9Sstevel@tonic-gate 		addr1 = zero+1;
12227c478bd9Sstevel@tonic-gate 		addr2 = dol;
12237c478bd9Sstevel@tonic-gate 		io = creat("ed.hup",
12247c478bd9Sstevel@tonic-gate 			S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
12257c478bd9Sstevel@tonic-gate 		if (io < 0 && home) {
12267c478bd9Sstevel@tonic-gate 			char	*fn;
12277c478bd9Sstevel@tonic-gate 
12287c478bd9Sstevel@tonic-gate 			fn = (char *)calloc(strlen(home) + 8, sizeof (char));
12297c478bd9Sstevel@tonic-gate 			if (fn) {
12307c478bd9Sstevel@tonic-gate 				strcpy(fn, home);
12317c478bd9Sstevel@tonic-gate 				strcat(fn, "/ed.hup");
12327c478bd9Sstevel@tonic-gate 				io = creat(fn, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP
12337c478bd9Sstevel@tonic-gate 						|S_IROTH|S_IWOTH);
12347c478bd9Sstevel@tonic-gate 				free(fn);
12357c478bd9Sstevel@tonic-gate 			}
12367c478bd9Sstevel@tonic-gate 		}
12377c478bd9Sstevel@tonic-gate 		if (io > 0)
12387c478bd9Sstevel@tonic-gate 			putfile();
12397c478bd9Sstevel@tonic-gate 	}
12407c478bd9Sstevel@tonic-gate 	fchange = 0;
12417c478bd9Sstevel@tonic-gate 	++errcnt;
12427c478bd9Sstevel@tonic-gate 	quit(sig);
12437c478bd9Sstevel@tonic-gate }
12447c478bd9Sstevel@tonic-gate 
12457c478bd9Sstevel@tonic-gate static int
error(int code)12467c478bd9Sstevel@tonic-gate error(int code)
12477c478bd9Sstevel@tonic-gate {
12487c478bd9Sstevel@tonic-gate 	int c;
12497c478bd9Sstevel@tonic-gate 
12507c478bd9Sstevel@tonic-gate 	if (code == 28 && save28 == 0) {
12517c478bd9Sstevel@tonic-gate 		fchange = 0;
12527c478bd9Sstevel@tonic-gate 		flag28++;
12537c478bd9Sstevel@tonic-gate 	}
12547c478bd9Sstevel@tonic-gate 	readflg = 0;
12557c478bd9Sstevel@tonic-gate 	++errcnt;
12567c478bd9Sstevel@tonic-gate 	listf = listn = 0;
12577c478bd9Sstevel@tonic-gate 	pflag = 0;
12587c478bd9Sstevel@tonic-gate #ifndef RESEARCH
12597c478bd9Sstevel@tonic-gate 	if (oldmask) {
12607c478bd9Sstevel@tonic-gate 		umask(oldmask);
12617c478bd9Sstevel@tonic-gate 		oldmask = 0;
12627c478bd9Sstevel@tonic-gate 	}
12637c478bd9Sstevel@tonic-gate #endif
12647c478bd9Sstevel@tonic-gate #ifdef NULLS	/* Not really nulls, but close enough */
12657c478bd9Sstevel@tonic-gate 	/* This is a bug because of buffering */
12667c478bd9Sstevel@tonic-gate 	if (code == 28) /* illegal char. */
12677c478bd9Sstevel@tonic-gate 		putd();
12687c478bd9Sstevel@tonic-gate #endif
12697c478bd9Sstevel@tonic-gate 	/* Cant open file or file does not exist */
12707c478bd9Sstevel@tonic-gate 	if ((code == 3) || (code == 68)) {
12717c478bd9Sstevel@tonic-gate 		if (qflg == 0) {
12727c478bd9Sstevel@tonic-gate 			putchr('?');
12737c478bd9Sstevel@tonic-gate 			puts(file);
12747c478bd9Sstevel@tonic-gate 		}
12757c478bd9Sstevel@tonic-gate 		else
12767c478bd9Sstevel@tonic-gate 			qflg = 0;
12777c478bd9Sstevel@tonic-gate 	}
12787c478bd9Sstevel@tonic-gate 	else
12797c478bd9Sstevel@tonic-gate 	{
12807c478bd9Sstevel@tonic-gate 		putchr('?');
12817c478bd9Sstevel@tonic-gate 		putchr('\n');
12827c478bd9Sstevel@tonic-gate 	}
12837c478bd9Sstevel@tonic-gate 	count = 0;
12847c478bd9Sstevel@tonic-gate 	lseek(0, (long)0, 2);
12857c478bd9Sstevel@tonic-gate 	if (globp)
12867c478bd9Sstevel@tonic-gate 		lastc = '\n';
12877c478bd9Sstevel@tonic-gate 	globp = 0;
12887c478bd9Sstevel@tonic-gate 	peekc = lastc;
12897c478bd9Sstevel@tonic-gate 	if (lastc)
12907c478bd9Sstevel@tonic-gate 		while ((c = getchr()) != '\n' && c != EOF);
12917c478bd9Sstevel@tonic-gate 	if (io) {
12927c478bd9Sstevel@tonic-gate 		eclose(io);
12937c478bd9Sstevel@tonic-gate 		io = -1;
12947c478bd9Sstevel@tonic-gate 	}
12957c478bd9Sstevel@tonic-gate 	xcode = code;
12967c478bd9Sstevel@tonic-gate 	if (hflag)
12977c478bd9Sstevel@tonic-gate 		PUTM();
12987c478bd9Sstevel@tonic-gate 	if (code == 4)
12997c478bd9Sstevel@tonic-gate 		return (0);	/* Non-fatal error. */
13007c478bd9Sstevel@tonic-gate 	longjmp(savej, 1);
13017c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
13027c478bd9Sstevel@tonic-gate }
13037c478bd9Sstevel@tonic-gate 
13047c478bd9Sstevel@tonic-gate static int
getchr(void)13057c478bd9Sstevel@tonic-gate getchr(void)
13067c478bd9Sstevel@tonic-gate {
13077c478bd9Sstevel@tonic-gate 	char c;
13087c478bd9Sstevel@tonic-gate 	if (lastc = peekc) {
13097c478bd9Sstevel@tonic-gate 		peekc = 0;
13107c478bd9Sstevel@tonic-gate 		return (lastc);
13117c478bd9Sstevel@tonic-gate 	}
13127c478bd9Sstevel@tonic-gate 	if (globp) {
13137c478bd9Sstevel@tonic-gate 		if ((lastc = (unsigned char)*globp++) != 0)
13147c478bd9Sstevel@tonic-gate 			return (lastc);
13157c478bd9Sstevel@tonic-gate 		globp = 0;
13167c478bd9Sstevel@tonic-gate 		return (EOF);
13177c478bd9Sstevel@tonic-gate 	}
13187c478bd9Sstevel@tonic-gate 	if (read(0, &c, 1) <= 0)
13197c478bd9Sstevel@tonic-gate 		return (lastc = EOF);
13207c478bd9Sstevel@tonic-gate 	lastc = (unsigned char)c;
13217c478bd9Sstevel@tonic-gate 	return (lastc);
13227c478bd9Sstevel@tonic-gate }
13237c478bd9Sstevel@tonic-gate 
13247c478bd9Sstevel@tonic-gate static int
gettty(void)13257c478bd9Sstevel@tonic-gate gettty(void)
13267c478bd9Sstevel@tonic-gate {
13277c478bd9Sstevel@tonic-gate 	int c;
13287c478bd9Sstevel@tonic-gate 	char *gf;
13297c478bd9Sstevel@tonic-gate 	char *p;
13307c478bd9Sstevel@tonic-gate 
13317c478bd9Sstevel@tonic-gate 	p = linebuf;
13327c478bd9Sstevel@tonic-gate 	gf = globp;
13337c478bd9Sstevel@tonic-gate 	while ((c = getchr()) != '\n') {
13347c478bd9Sstevel@tonic-gate 		if (c == EOF) {
13357c478bd9Sstevel@tonic-gate 			if (gf)
13367c478bd9Sstevel@tonic-gate 				peekc = c;
13377c478bd9Sstevel@tonic-gate 			return (c);
13387c478bd9Sstevel@tonic-gate 		}
13397c478bd9Sstevel@tonic-gate 		if (c == 0)
13407c478bd9Sstevel@tonic-gate 			continue;
13417c478bd9Sstevel@tonic-gate 		*p++ = c;
13427c478bd9Sstevel@tonic-gate 
13437c478bd9Sstevel@tonic-gate 		if (p > &linebuf[LBSIZE-1])
13447c478bd9Sstevel@tonic-gate 			(void) error(27);
13457c478bd9Sstevel@tonic-gate 	}
13467c478bd9Sstevel@tonic-gate 	*p++ = 0;
13477c478bd9Sstevel@tonic-gate 	if (linebuf[0] == '.' && linebuf[1] == 0)
13487c478bd9Sstevel@tonic-gate 		return (EOF);
13497c478bd9Sstevel@tonic-gate 
13507c478bd9Sstevel@tonic-gate 	/*
13517c478bd9Sstevel@tonic-gate 	 * POSIX.2/XPG4 explicitly says no to this:
13527c478bd9Sstevel@tonic-gate 	 *
13537c478bd9Sstevel@tonic-gate 	 * in Solaris backslash followed by special character "." is
13547c478bd9Sstevel@tonic-gate 	 * special character "." itself; (so terminating input mode can be
13557c478bd9Sstevel@tonic-gate 	 * "\.\n").
13567c478bd9Sstevel@tonic-gate 	 *
13577c478bd9Sstevel@tonic-gate 	 * however, POSIX2/XPG4 says, input mode is terminated by
13587c478bd9Sstevel@tonic-gate 	 * entering line consisting of only 2 characters: ".\n"
13597c478bd9Sstevel@tonic-gate 	 *
13607c478bd9Sstevel@tonic-gate 	 * if (linebuf[0]=='\\' && linebuf[1]=='.' && linebuf[2]==0) {
13617c478bd9Sstevel@tonic-gate 	 *	linebuf[0] = '.';
13627c478bd9Sstevel@tonic-gate 	 *	linebuf[1] = 0;
13637c478bd9Sstevel@tonic-gate 	 * }
13647c478bd9Sstevel@tonic-gate 	 */
13657c478bd9Sstevel@tonic-gate 	return (0);
13667c478bd9Sstevel@tonic-gate }
13677c478bd9Sstevel@tonic-gate 
13687c478bd9Sstevel@tonic-gate static int
getfile(void)13697c478bd9Sstevel@tonic-gate getfile(void)
13707c478bd9Sstevel@tonic-gate {
13717c478bd9Sstevel@tonic-gate 	char c;
13727c478bd9Sstevel@tonic-gate 	char *lp, *fp;
13737c478bd9Sstevel@tonic-gate 
13747c478bd9Sstevel@tonic-gate 	lp = linebuf;
13757c478bd9Sstevel@tonic-gate 	fp = nextip;
13767c478bd9Sstevel@tonic-gate 	do {
13777c478bd9Sstevel@tonic-gate 		if (--ninbuf < 0) {
13787c478bd9Sstevel@tonic-gate 			if ((ninbuf = read(io, genbuf, LBSIZE)-1) < 0)
13797c478bd9Sstevel@tonic-gate 				if (lp > linebuf) {
13807c478bd9Sstevel@tonic-gate 					puts(gettext("'\\n' appended"));
13817c478bd9Sstevel@tonic-gate 					*genbuf = '\n';
13827c478bd9Sstevel@tonic-gate 				}
13837c478bd9Sstevel@tonic-gate 				else
13847c478bd9Sstevel@tonic-gate 					return (EOF);
13857c478bd9Sstevel@tonic-gate 			if (crflag == -1) {
13867c478bd9Sstevel@tonic-gate 				if (isencrypt(genbuf, ninbuf + 1))
13877c478bd9Sstevel@tonic-gate 					crflag = 2;
13887c478bd9Sstevel@tonic-gate 				else
13897c478bd9Sstevel@tonic-gate 					crflag = -2;
13907c478bd9Sstevel@tonic-gate 			}
13917c478bd9Sstevel@tonic-gate 			fp = genbuf;
13927c478bd9Sstevel@tonic-gate 			if (crflag > 0)
13937c478bd9Sstevel@tonic-gate 			if (run_crypt(count, genbuf, ninbuf+1, perm) == -1)
13947c478bd9Sstevel@tonic-gate 				(void) error(63);
13957c478bd9Sstevel@tonic-gate 		}
13967c478bd9Sstevel@tonic-gate 		if (lp >= &linebuf[LBSIZE]) {
13977c478bd9Sstevel@tonic-gate 			lastc = '\n';
13987c478bd9Sstevel@tonic-gate 			(void) error(27);
13997c478bd9Sstevel@tonic-gate 		}
14007c478bd9Sstevel@tonic-gate 		if ((*lp++ = c = *fp++) == 0) {
14017c478bd9Sstevel@tonic-gate #ifdef NULLS
14027c478bd9Sstevel@tonic-gate 			lp[-1] = '\\';
14037c478bd9Sstevel@tonic-gate 			*lp++ = '0';
14047c478bd9Sstevel@tonic-gate 			nulls++;
14057c478bd9Sstevel@tonic-gate #else
14067c478bd9Sstevel@tonic-gate 			lp--;
14077c478bd9Sstevel@tonic-gate 			continue;
14087c478bd9Sstevel@tonic-gate #endif
14097c478bd9Sstevel@tonic-gate 		}
14107c478bd9Sstevel@tonic-gate 		count++;
14117c478bd9Sstevel@tonic-gate 	} while (c != '\n');
14127c478bd9Sstevel@tonic-gate 	*--lp = 0;
14137c478bd9Sstevel@tonic-gate 	nextip = fp;
14147c478bd9Sstevel@tonic-gate 	if (fss.Ffill && fss.Flim && lenchk(linebuf, &fss) < 0) {
14157c478bd9Sstevel@tonic-gate 		write(1, gettext("line too long: lno = "),
14167c478bd9Sstevel@tonic-gate 			strlen(gettext("line too long: lno = ")));
14177c478bd9Sstevel@tonic-gate 		ccount = count;
14187c478bd9Sstevel@tonic-gate 		count = (++dot-zero)&077777;
14197c478bd9Sstevel@tonic-gate 		dot--;
14207c478bd9Sstevel@tonic-gate 		putd();
14217c478bd9Sstevel@tonic-gate 		count = ccount;
14227c478bd9Sstevel@tonic-gate 		putchr('\n');
14237c478bd9Sstevel@tonic-gate 	}
14247c478bd9Sstevel@tonic-gate 	return (0);
14257c478bd9Sstevel@tonic-gate }
14267c478bd9Sstevel@tonic-gate 
14277c478bd9Sstevel@tonic-gate static void
putfile(void)14287c478bd9Sstevel@tonic-gate putfile(void)
14297c478bd9Sstevel@tonic-gate {
14307c478bd9Sstevel@tonic-gate 	int n;
14317c478bd9Sstevel@tonic-gate 	LINE a1;
14327c478bd9Sstevel@tonic-gate 	char *fp, *lp;
14337c478bd9Sstevel@tonic-gate 	int nib;
14347c478bd9Sstevel@tonic-gate 
14357c478bd9Sstevel@tonic-gate 	nib = LBSIZE;
14367c478bd9Sstevel@tonic-gate 	fp = genbuf;
14377c478bd9Sstevel@tonic-gate 	a1 = addr1;
14387c478bd9Sstevel@tonic-gate 	do {
143923a1cceaSRoger A. Faulkner 		lp = getaline(a1++->cur);
14407c478bd9Sstevel@tonic-gate 		if (fss.Ffill && fss.Flim && lenchk(linebuf, &fss) < 0) {
14417c478bd9Sstevel@tonic-gate 			write(1, gettext("line too long: lno = "),
14427c478bd9Sstevel@tonic-gate 				strlen(gettext("line too long: lno = ")));
14437c478bd9Sstevel@tonic-gate 			ccount = count;
14447c478bd9Sstevel@tonic-gate 			count = (a1-zero-1)&077777;
14457c478bd9Sstevel@tonic-gate 			putd();
14467c478bd9Sstevel@tonic-gate 			count = ccount;
14477c478bd9Sstevel@tonic-gate 			putchr('\n');
14487c478bd9Sstevel@tonic-gate 		}
14497c478bd9Sstevel@tonic-gate 		for (;;) {
14507c478bd9Sstevel@tonic-gate 			if (--nib < 0) {
14517c478bd9Sstevel@tonic-gate 				n = fp-genbuf;
14527c478bd9Sstevel@tonic-gate 				if (kflag)
14537c478bd9Sstevel@tonic-gate 				if (run_crypt(count-n, genbuf, n, perm) == -1)
14547c478bd9Sstevel@tonic-gate 					(void) error(63);
14557c478bd9Sstevel@tonic-gate 				if (write(io, genbuf, n) != n)
14567c478bd9Sstevel@tonic-gate 					(void) error(29);
14577c478bd9Sstevel@tonic-gate 				nib = LBSIZE - 1;
14587c478bd9Sstevel@tonic-gate 				fp = genbuf;
14597c478bd9Sstevel@tonic-gate 			}
14607c478bd9Sstevel@tonic-gate 			if (dol->cur == 0L)break; /* Allow write of null file */
14617c478bd9Sstevel@tonic-gate 			count++;
14627c478bd9Sstevel@tonic-gate 			if ((*fp++ = *lp++) == 0) {
14637c478bd9Sstevel@tonic-gate 				fp[-1] = '\n';
14647c478bd9Sstevel@tonic-gate 				break;
14657c478bd9Sstevel@tonic-gate 			}
14667c478bd9Sstevel@tonic-gate 		}
14677c478bd9Sstevel@tonic-gate 	} while (a1 <= addr2);
14687c478bd9Sstevel@tonic-gate 	n = fp-genbuf;
14697c478bd9Sstevel@tonic-gate 	if (kflag)
14707c478bd9Sstevel@tonic-gate 		if (run_crypt(count-n, genbuf, n, perm) == -1)
14717c478bd9Sstevel@tonic-gate 			(void) error(63);
14727c478bd9Sstevel@tonic-gate 	if (write(io, genbuf, n) != n)
14737c478bd9Sstevel@tonic-gate 		(void) error(29);
14747c478bd9Sstevel@tonic-gate }
14757c478bd9Sstevel@tonic-gate 
14767c478bd9Sstevel@tonic-gate static void
append(int (* f)(void),LINE a)14777c478bd9Sstevel@tonic-gate append(int (*f)(void), LINE a)
14787c478bd9Sstevel@tonic-gate {
14797c478bd9Sstevel@tonic-gate 	LINE a1, a2, rdot;
14807c478bd9Sstevel@tonic-gate 	long tl;
14817c478bd9Sstevel@tonic-gate 
14827c478bd9Sstevel@tonic-gate 	nline = 0;
14837c478bd9Sstevel@tonic-gate 	dot = a;
14847c478bd9Sstevel@tonic-gate 	while ((*f)() == 0) {
14857c478bd9Sstevel@tonic-gate 		if (dol >= endcore) {
14867c478bd9Sstevel@tonic-gate 			if ((int)sbrk(512 * sizeof (struct lin)) == -1) {
14877c478bd9Sstevel@tonic-gate 				lastc = '\n';
14887c478bd9Sstevel@tonic-gate 				(void) error(30);
14897c478bd9Sstevel@tonic-gate 			}
14907c478bd9Sstevel@tonic-gate 			endcore += 512;
14917c478bd9Sstevel@tonic-gate 		}
14927c478bd9Sstevel@tonic-gate 		tl = putline();
14937c478bd9Sstevel@tonic-gate 		nline++;
14947c478bd9Sstevel@tonic-gate 		a1 = ++dol;
14957c478bd9Sstevel@tonic-gate 		a2 = a1+1;
14967c478bd9Sstevel@tonic-gate 		rdot = ++dot;
14977c478bd9Sstevel@tonic-gate 		while (a1 > rdot)
14987c478bd9Sstevel@tonic-gate 			(--a2)->cur = (--a1)->cur;
14997c478bd9Sstevel@tonic-gate 		rdot->cur = tl;
15007c478bd9Sstevel@tonic-gate 	}
15017c478bd9Sstevel@tonic-gate }
15027c478bd9Sstevel@tonic-gate 
15037c478bd9Sstevel@tonic-gate static void
unixcom(void)15047c478bd9Sstevel@tonic-gate unixcom(void)
15057c478bd9Sstevel@tonic-gate {
15067c478bd9Sstevel@tonic-gate 	void (*savint)();
15077c478bd9Sstevel@tonic-gate 	pid_t	pid, rpid;
15087c478bd9Sstevel@tonic-gate 	int retcode;
15097c478bd9Sstevel@tonic-gate 	static char savcmd[LBSIZE];	/* last command */
15107c478bd9Sstevel@tonic-gate 	char curcmd[LBSIZE];		/* current command */
15117c478bd9Sstevel@tonic-gate 	char *psavcmd, *pcurcmd, *psavedfile;
15127c478bd9Sstevel@tonic-gate 	int endflg = 1, shflg = 0;
15137c478bd9Sstevel@tonic-gate 	wchar_t	c;
15147c478bd9Sstevel@tonic-gate 	int	len;
15157c478bd9Sstevel@tonic-gate 
15167c478bd9Sstevel@tonic-gate 	setnoaddr();
15177c478bd9Sstevel@tonic-gate 	if (rflg)
15187c478bd9Sstevel@tonic-gate 		(void) error(6);
15197c478bd9Sstevel@tonic-gate 	pcurcmd = curcmd;
15207c478bd9Sstevel@tonic-gate 	/* read command til end */
15217c478bd9Sstevel@tonic-gate 
15227c478bd9Sstevel@tonic-gate 	/*
15237c478bd9Sstevel@tonic-gate 	 * a '!' found in beginning of command is replaced with the saved
15247c478bd9Sstevel@tonic-gate 	 * command.  a '%' found in command is replaced with the current
15257c478bd9Sstevel@tonic-gate 	 * filename
15267c478bd9Sstevel@tonic-gate 	 */
15277c478bd9Sstevel@tonic-gate 
15287c478bd9Sstevel@tonic-gate 	c = getchr();
15297c478bd9Sstevel@tonic-gate 	if (c == '!') {
15307c478bd9Sstevel@tonic-gate 		if (savcmd[0] == 0)
15317c478bd9Sstevel@tonic-gate 			(void) error(56);
15327c478bd9Sstevel@tonic-gate 		else {
15337c478bd9Sstevel@tonic-gate 			psavcmd = savcmd;
15347c478bd9Sstevel@tonic-gate 			while (*pcurcmd++ = *psavcmd++);
15357c478bd9Sstevel@tonic-gate 			--pcurcmd;
15367c478bd9Sstevel@tonic-gate 			shflg = 1;
15377c478bd9Sstevel@tonic-gate 		}
15387c478bd9Sstevel@tonic-gate 	} else
15397c478bd9Sstevel@tonic-gate 		UNGETC(c);  /* put c back */
15407c478bd9Sstevel@tonic-gate 	while (endflg == 1) {
15417c478bd9Sstevel@tonic-gate 		while ((c = get_wchr()) != '\n' && c != '%' && c != '\\') {
15427c478bd9Sstevel@tonic-gate 			if ((len = wctomb(pcurcmd, c)) <= 0) {
15437c478bd9Sstevel@tonic-gate 				*pcurcmd = (unsigned char)c;
15447c478bd9Sstevel@tonic-gate 				len = 1;
15457c478bd9Sstevel@tonic-gate 			}
15467c478bd9Sstevel@tonic-gate 			pcurcmd += len;
15477c478bd9Sstevel@tonic-gate 		}
15487c478bd9Sstevel@tonic-gate 
15497c478bd9Sstevel@tonic-gate 		if (c == '%') {
15507c478bd9Sstevel@tonic-gate 			if (savedfile[0] == 0)
15517c478bd9Sstevel@tonic-gate 				(void) error(21);
15527c478bd9Sstevel@tonic-gate 			else {
15537c478bd9Sstevel@tonic-gate 				psavedfile = savedfile;
15547c478bd9Sstevel@tonic-gate 				while (pcurcmd < curcmd + LBSIZE &&
15557c478bd9Sstevel@tonic-gate 					(*pcurcmd++ = *psavedfile++));
15567c478bd9Sstevel@tonic-gate 				--pcurcmd;
15577c478bd9Sstevel@tonic-gate 				shflg = 1;
15587c478bd9Sstevel@tonic-gate 			}
15597c478bd9Sstevel@tonic-gate 		} else if (c == '\\') {
15607c478bd9Sstevel@tonic-gate 			c = get_wchr();
15617c478bd9Sstevel@tonic-gate 			if (c != '%')
15627c478bd9Sstevel@tonic-gate 				*pcurcmd++ = '\\';
15637c478bd9Sstevel@tonic-gate 			if ((len = wctomb(pcurcmd, c)) <= 0) {
15647c478bd9Sstevel@tonic-gate 				*pcurcmd = (unsigned char)c;
15657c478bd9Sstevel@tonic-gate 				len = 1;
15667c478bd9Sstevel@tonic-gate 			}
15677c478bd9Sstevel@tonic-gate 			pcurcmd += len;
15687c478bd9Sstevel@tonic-gate 		}
15697c478bd9Sstevel@tonic-gate 		else
15707c478bd9Sstevel@tonic-gate 			/* end of command hit */
15717c478bd9Sstevel@tonic-gate 			endflg = 0;
15727c478bd9Sstevel@tonic-gate 	}
15737c478bd9Sstevel@tonic-gate 	*pcurcmd++ = 0;
15747c478bd9Sstevel@tonic-gate 	if (shflg == 1)
15757c478bd9Sstevel@tonic-gate 		puts(curcmd);
15767c478bd9Sstevel@tonic-gate 	/* save command */
15777c478bd9Sstevel@tonic-gate 	strcpy(savcmd, curcmd);
15787c478bd9Sstevel@tonic-gate 
15797c478bd9Sstevel@tonic-gate 	if ((pid = fork()) == 0) {
15807c478bd9Sstevel@tonic-gate 		signal(SIGHUP, oldhup);
15817c478bd9Sstevel@tonic-gate 		signal(SIGQUIT, oldquit);
15827c478bd9Sstevel@tonic-gate 		close(tfile);
1583*6a5408e6SRichard Lowe 		execlp(_PATH_BSHELL, "sh", "-c", curcmd, (char *)0);
15847c478bd9Sstevel@tonic-gate 		exit(0100);
15857c478bd9Sstevel@tonic-gate 	}
15867c478bd9Sstevel@tonic-gate 	savint = signal(SIGINT, SIG_IGN);
15877c478bd9Sstevel@tonic-gate 	while ((rpid = wait(&retcode)) != pid && rpid != (pid_t)-1);
15887c478bd9Sstevel@tonic-gate 	signal(SIGINT, savint);
15897c478bd9Sstevel@tonic-gate 	if (vflag) puts("!");
15907c478bd9Sstevel@tonic-gate }
15917c478bd9Sstevel@tonic-gate 
15927c478bd9Sstevel@tonic-gate static void
quit(int sig)15937c478bd9Sstevel@tonic-gate quit(int sig)
15947c478bd9Sstevel@tonic-gate {
15957c478bd9Sstevel@tonic-gate 	if (vflag && fchange) {
15967c478bd9Sstevel@tonic-gate 		fchange = 0;
15977c478bd9Sstevel@tonic-gate 		if (flag28) {
15987c478bd9Sstevel@tonic-gate 			flag28 = 0;
15997c478bd9Sstevel@tonic-gate 			(void) error(62);
16007c478bd9Sstevel@tonic-gate 		}
16017c478bd9Sstevel@tonic-gate 
16027c478bd9Sstevel@tonic-gate 		/*
16037c478bd9Sstevel@tonic-gate 		 * For case where user reads in BOTH a good
16047c478bd9Sstevel@tonic-gate 		 * file & a bad file
16057c478bd9Sstevel@tonic-gate 		 */
16067c478bd9Sstevel@tonic-gate 		(void) error(1);
16077c478bd9Sstevel@tonic-gate 	}
16087c478bd9Sstevel@tonic-gate 	unlink(tfname);
16097c478bd9Sstevel@tonic-gate 	if (kflag)
16107c478bd9Sstevel@tonic-gate 		crypt_close(perm);
16117c478bd9Sstevel@tonic-gate 	if (xtflag)
16127c478bd9Sstevel@tonic-gate 		crypt_close(tperm);
16137c478bd9Sstevel@tonic-gate 	exit(errcnt? 2: 0);
16147c478bd9Sstevel@tonic-gate }
16157c478bd9Sstevel@tonic-gate 
16167c478bd9Sstevel@tonic-gate static void
delete(void)16177c478bd9Sstevel@tonic-gate delete(void)
16187c478bd9Sstevel@tonic-gate {
16197c478bd9Sstevel@tonic-gate 	setdot();
16207c478bd9Sstevel@tonic-gate 	newline();
16217c478bd9Sstevel@tonic-gate 	nonzero();
16227c478bd9Sstevel@tonic-gate 	if (!globflg) save();
16237c478bd9Sstevel@tonic-gate 	rdelete(addr1, addr2);
16247c478bd9Sstevel@tonic-gate }
16257c478bd9Sstevel@tonic-gate 
16267c478bd9Sstevel@tonic-gate static void
rdelete(LINE ad1,LINE ad2)16277c478bd9Sstevel@tonic-gate rdelete(LINE ad1, LINE ad2)
16287c478bd9Sstevel@tonic-gate {
16297c478bd9Sstevel@tonic-gate 	LINE a1, a2, a3;
16307c478bd9Sstevel@tonic-gate 
16317c478bd9Sstevel@tonic-gate 	a1 = ad1;
16327c478bd9Sstevel@tonic-gate 	a2 = ad2+1;
16337c478bd9Sstevel@tonic-gate 	a3 = dol;
16347c478bd9Sstevel@tonic-gate 	dol -= a2 - a1;
16357c478bd9Sstevel@tonic-gate 	do
16367c478bd9Sstevel@tonic-gate 		(a1++)->cur = (a2++)->cur;
16377c478bd9Sstevel@tonic-gate 	while (a2 <= a3);
16387c478bd9Sstevel@tonic-gate 	a1 = ad1;
16397c478bd9Sstevel@tonic-gate 	if (a1 > dol)
16407c478bd9Sstevel@tonic-gate 		a1 = dol;
16417c478bd9Sstevel@tonic-gate 	dot = a1;
16427c478bd9Sstevel@tonic-gate 	fchange = 1;
16437c478bd9Sstevel@tonic-gate }
16447c478bd9Sstevel@tonic-gate 
16457c478bd9Sstevel@tonic-gate static void
gdelete(void)16467c478bd9Sstevel@tonic-gate gdelete(void)
16477c478bd9Sstevel@tonic-gate {
16487c478bd9Sstevel@tonic-gate 	LINE a1, a2, a3;
16497c478bd9Sstevel@tonic-gate 
16507c478bd9Sstevel@tonic-gate 	a3 = dol;
16517c478bd9Sstevel@tonic-gate 	for (a1 = zero+1; (a1->cur&01) == 0; a1++)
16527c478bd9Sstevel@tonic-gate 		if (a1 >= a3)
16537c478bd9Sstevel@tonic-gate 			return;
16547c478bd9Sstevel@tonic-gate 	for (a2 = a1 + 1; a2 <= a3; ) {
16557c478bd9Sstevel@tonic-gate 		if (a2->cur & 01) {
16567c478bd9Sstevel@tonic-gate 			a2++;
16577c478bd9Sstevel@tonic-gate 			dot = a1;
16587c478bd9Sstevel@tonic-gate 		} else
16597c478bd9Sstevel@tonic-gate 			(a1++)->cur = (a2++)->cur;
16607c478bd9Sstevel@tonic-gate 	}
16617c478bd9Sstevel@tonic-gate 	dol = a1-1;
16627c478bd9Sstevel@tonic-gate 	if (dot > dol)
16637c478bd9Sstevel@tonic-gate 		dot = dol;
16647c478bd9Sstevel@tonic-gate 	fchange = 1;
16657c478bd9Sstevel@tonic-gate }
16667c478bd9Sstevel@tonic-gate 
16677c478bd9Sstevel@tonic-gate static char *
getaline(long tl)166823a1cceaSRoger A. Faulkner getaline(long tl)
16697c478bd9Sstevel@tonic-gate {
16707c478bd9Sstevel@tonic-gate 	char *bp, *lp;
16717c478bd9Sstevel@tonic-gate 	int nl;
16727c478bd9Sstevel@tonic-gate 
16737c478bd9Sstevel@tonic-gate 	lp = linebuf;
16747c478bd9Sstevel@tonic-gate 	bp = getblock(tl, READ);
16757c478bd9Sstevel@tonic-gate 	nl = nleft;
16767c478bd9Sstevel@tonic-gate 	tl &= ~0377;
16777c478bd9Sstevel@tonic-gate 	while (*lp++ = *bp++)
16787c478bd9Sstevel@tonic-gate 		if (--nl == 0) {
16797c478bd9Sstevel@tonic-gate 			bp = getblock(tl += 0400, READ);
16807c478bd9Sstevel@tonic-gate 			nl = nleft;
16817c478bd9Sstevel@tonic-gate 		}
16827c478bd9Sstevel@tonic-gate 	return (linebuf);
16837c478bd9Sstevel@tonic-gate }
16847c478bd9Sstevel@tonic-gate 
16857c478bd9Sstevel@tonic-gate static long
putline(void)16867c478bd9Sstevel@tonic-gate putline(void)
16877c478bd9Sstevel@tonic-gate {
16887c478bd9Sstevel@tonic-gate 	char *bp, *lp;
16897c478bd9Sstevel@tonic-gate 	int nl;
16907c478bd9Sstevel@tonic-gate 	long tl;
16917c478bd9Sstevel@tonic-gate 
16927c478bd9Sstevel@tonic-gate 	fchange = 1;
16937c478bd9Sstevel@tonic-gate 	lp = linebuf;
16947c478bd9Sstevel@tonic-gate 	tl = tline;
16957c478bd9Sstevel@tonic-gate 	bp = getblock(tl, WRITE);
16967c478bd9Sstevel@tonic-gate 	nl = nleft;
16977c478bd9Sstevel@tonic-gate 	tl &= ~0377;
16987c478bd9Sstevel@tonic-gate 	while (*bp = *lp++) {
16997c478bd9Sstevel@tonic-gate 		if (*bp++ == '\n') {
17007c478bd9Sstevel@tonic-gate 			*--bp = 0;
17017c478bd9Sstevel@tonic-gate 			linebp = lp;
17027c478bd9Sstevel@tonic-gate 			break;
17037c478bd9Sstevel@tonic-gate 		}
17047c478bd9Sstevel@tonic-gate 		if (--nl == 0) {
17057c478bd9Sstevel@tonic-gate 			bp = getblock(tl += 0400, WRITE);
17067c478bd9Sstevel@tonic-gate 			nl = nleft;
17077c478bd9Sstevel@tonic-gate 		}
17087c478bd9Sstevel@tonic-gate 	}
17097c478bd9Sstevel@tonic-gate 	nl = tline;
17107c478bd9Sstevel@tonic-gate 	tline += (((lp-linebuf)+03)>>1)&077776;
17117c478bd9Sstevel@tonic-gate 	return (nl);
17127c478bd9Sstevel@tonic-gate }
17137c478bd9Sstevel@tonic-gate 
17147c478bd9Sstevel@tonic-gate static char *
getblock(long atl,long iof)17157c478bd9Sstevel@tonic-gate getblock(long atl, long iof)
17167c478bd9Sstevel@tonic-gate {
17177c478bd9Sstevel@tonic-gate 	int bno, off;
17187c478bd9Sstevel@tonic-gate 	char *p1, *p2;
17197c478bd9Sstevel@tonic-gate 	int n;
17207c478bd9Sstevel@tonic-gate 
17217c478bd9Sstevel@tonic-gate 	bno = atl >> 8;
17227c478bd9Sstevel@tonic-gate 	off = (atl<<1)&0774;
17237c478bd9Sstevel@tonic-gate 
17247c478bd9Sstevel@tonic-gate 	/* bno is limited to 16 bits */
17257c478bd9Sstevel@tonic-gate 	if (bno >= 65535) {
17267c478bd9Sstevel@tonic-gate 		lastc = '\n';
17277c478bd9Sstevel@tonic-gate 		(void) error(31);
17287c478bd9Sstevel@tonic-gate 	}
17297c478bd9Sstevel@tonic-gate 	nleft = 512 - off;
17307c478bd9Sstevel@tonic-gate 	if (bno == iblock) {
17317c478bd9Sstevel@tonic-gate 		ichanged |= iof;
17327c478bd9Sstevel@tonic-gate 		return (ibuff+off);
17337c478bd9Sstevel@tonic-gate 	}
17347c478bd9Sstevel@tonic-gate 	if (bno == oblock)
17357c478bd9Sstevel@tonic-gate 		return (obuff+off);
17367c478bd9Sstevel@tonic-gate 	if (iof == READ) {
17377c478bd9Sstevel@tonic-gate 		if (ichanged) {
17387c478bd9Sstevel@tonic-gate 			if (xtflag)
17397c478bd9Sstevel@tonic-gate 				if (run_crypt(0L, ibuff, 512, tperm) == -1)
17407c478bd9Sstevel@tonic-gate 					(void) error(63);
17417c478bd9Sstevel@tonic-gate 			blkio(iblock, ibuff, write);
17427c478bd9Sstevel@tonic-gate 		}
17437c478bd9Sstevel@tonic-gate 		ichanged = 0;
17447c478bd9Sstevel@tonic-gate 		iblock = bno;
17457c478bd9Sstevel@tonic-gate 		blkio(bno, ibuff, read);
17467c478bd9Sstevel@tonic-gate 		if (xtflag)
17477c478bd9Sstevel@tonic-gate 			if (run_crypt(0L, ibuff, 512, tperm) == -1)
17487c478bd9Sstevel@tonic-gate 				(void) error(63);
17497c478bd9Sstevel@tonic-gate 		return (ibuff+off);
17507c478bd9Sstevel@tonic-gate 	}
17517c478bd9Sstevel@tonic-gate 	if (oblock >= 0) {
17527c478bd9Sstevel@tonic-gate 		if (xtflag) {
17537c478bd9Sstevel@tonic-gate 			p1 = obuff;
17547c478bd9Sstevel@tonic-gate 			p2 = crbuf;
17557c478bd9Sstevel@tonic-gate 			n = 512;
17567c478bd9Sstevel@tonic-gate 			while (n--)
17577c478bd9Sstevel@tonic-gate 				*p2++ = *p1++;
17587c478bd9Sstevel@tonic-gate 			if (run_crypt(0L, crbuf, 512, tperm) == -1)
17597c478bd9Sstevel@tonic-gate 				(void) error(63);
17607c478bd9Sstevel@tonic-gate 			blkio(oblock, crbuf, write);
17617c478bd9Sstevel@tonic-gate 		} else
17627c478bd9Sstevel@tonic-gate 			blkio(oblock, obuff, write);
17637c478bd9Sstevel@tonic-gate 	}
17647c478bd9Sstevel@tonic-gate 	oblock = bno;
17657c478bd9Sstevel@tonic-gate 	return (obuff+off);
17667c478bd9Sstevel@tonic-gate }
17677c478bd9Sstevel@tonic-gate 
17687c478bd9Sstevel@tonic-gate static void
blkio(int b,char * buf,ssize_t (* iofcn)())17697c478bd9Sstevel@tonic-gate blkio(int b, char *buf, ssize_t (*iofcn)())
17707c478bd9Sstevel@tonic-gate {
17717c478bd9Sstevel@tonic-gate 	lseek(tfile, (long)b<<9, 0);
17727c478bd9Sstevel@tonic-gate 	if ((*iofcn)(tfile, buf, 512) != 512) {
17737c478bd9Sstevel@tonic-gate 		if (dol != zero)
17747c478bd9Sstevel@tonic-gate 			(void) error(32); /* Bypass this if writing null file */
17757c478bd9Sstevel@tonic-gate 	}
17767c478bd9Sstevel@tonic-gate }
17777c478bd9Sstevel@tonic-gate 
17787c478bd9Sstevel@tonic-gate static void
init(void)17797c478bd9Sstevel@tonic-gate init(void)
17807c478bd9Sstevel@tonic-gate {
17817c478bd9Sstevel@tonic-gate 	long *markp;
17827c478bd9Sstevel@tonic-gate 	mode_t omask;
17837c478bd9Sstevel@tonic-gate 
17847c478bd9Sstevel@tonic-gate 	if (tfile != -1) {
17857c478bd9Sstevel@tonic-gate 		(void) close(tfile);
17867c478bd9Sstevel@tonic-gate 		(void) unlink(tfname);
17877c478bd9Sstevel@tonic-gate 	}
17887c478bd9Sstevel@tonic-gate 
17897c478bd9Sstevel@tonic-gate 	tline = 2;
17907c478bd9Sstevel@tonic-gate 	for (markp = names; markp < &names[26]; )
17917c478bd9Sstevel@tonic-gate 		*markp++ = 0L;
17927c478bd9Sstevel@tonic-gate 	subnewa = 0L;
17937c478bd9Sstevel@tonic-gate 	anymarks = 0;
17947c478bd9Sstevel@tonic-gate 	iblock = -1;
17957c478bd9Sstevel@tonic-gate 	oblock = -1;
17967c478bd9Sstevel@tonic-gate 	ichanged = 0;
17977c478bd9Sstevel@tonic-gate 	initflg = 1;
17987c478bd9Sstevel@tonic-gate 	omask = umask(0);
17997c478bd9Sstevel@tonic-gate 
18007c478bd9Sstevel@tonic-gate 	if ((tfile = open(tfname, O_CREAT|O_EXCL|O_RDWR,
18017c478bd9Sstevel@tonic-gate 	    S_IRUSR|S_IWUSR)) < 0) {
18027c478bd9Sstevel@tonic-gate 		puts(gettext(msgtab[70]));
18037c478bd9Sstevel@tonic-gate 		exit(2);
18047c478bd9Sstevel@tonic-gate 	}
18057c478bd9Sstevel@tonic-gate 
18067c478bd9Sstevel@tonic-gate 	umask(omask);
18077c478bd9Sstevel@tonic-gate 	if (xflag) {
18087c478bd9Sstevel@tonic-gate 		xtflag = 1;
18097c478bd9Sstevel@tonic-gate 		if (tpermflag)
18107c478bd9Sstevel@tonic-gate 			(void) crypt_close(tperm);
18117c478bd9Sstevel@tonic-gate 		tpermflag = 1;
18127c478bd9Sstevel@tonic-gate 		if (makekey(tperm)) {
18137c478bd9Sstevel@tonic-gate 			xtflag = 0;
18147c478bd9Sstevel@tonic-gate 			puts(gettext(msgtab[65]));
18157c478bd9Sstevel@tonic-gate 		}
18167c478bd9Sstevel@tonic-gate 	}
18177c478bd9Sstevel@tonic-gate 	brk((char *)fendcore);
18187c478bd9Sstevel@tonic-gate 	dot = zero = dol = savdot = savdol = fendcore;
18197c478bd9Sstevel@tonic-gate 	flag28 = save28 = 0;
18207c478bd9Sstevel@tonic-gate 	endcore = fendcore - sizeof (struct lin);
18217c478bd9Sstevel@tonic-gate }
18227c478bd9Sstevel@tonic-gate 
18237c478bd9Sstevel@tonic-gate static void
global(int k)18247c478bd9Sstevel@tonic-gate global(int k)
18257c478bd9Sstevel@tonic-gate {
18267c478bd9Sstevel@tonic-gate 	char *gp;
18277c478bd9Sstevel@tonic-gate 	wchar_t l;
18287c478bd9Sstevel@tonic-gate 	char multic[MB_LEN_MAX];
18297c478bd9Sstevel@tonic-gate 	wchar_t c;
18307c478bd9Sstevel@tonic-gate 	LINE a1;
18317c478bd9Sstevel@tonic-gate 	char globuf[LBSIZE];
18327c478bd9Sstevel@tonic-gate 	int n;
18337c478bd9Sstevel@tonic-gate 	int	len;
18347c478bd9Sstevel@tonic-gate 
18357c478bd9Sstevel@tonic-gate 	if (globp)
18367c478bd9Sstevel@tonic-gate 		(void) error(33);
18377c478bd9Sstevel@tonic-gate 	setall();
18387c478bd9Sstevel@tonic-gate 	nonzero();
18397c478bd9Sstevel@tonic-gate 	if ((n = _mbftowc(multic, &l, getchr, &peekc)) <= 0)
18407c478bd9Sstevel@tonic-gate 		(void) error(67);
18417c478bd9Sstevel@tonic-gate 	if (l == '\n')
18427c478bd9Sstevel@tonic-gate 		(void) error(19);
18437c478bd9Sstevel@tonic-gate 	save();
18447c478bd9Sstevel@tonic-gate 	comple(l);
18457c478bd9Sstevel@tonic-gate 	gp = globuf;
18467c478bd9Sstevel@tonic-gate 	while ((c = get_wchr()) != '\n') {
18477c478bd9Sstevel@tonic-gate 		if (c == EOF)
18487c478bd9Sstevel@tonic-gate 			(void) error(19);
18497c478bd9Sstevel@tonic-gate 
18507c478bd9Sstevel@tonic-gate 		/* '\\' has special meaning only if preceding a '\n' */
18517c478bd9Sstevel@tonic-gate 		if (c == '\\') {
18527c478bd9Sstevel@tonic-gate 			c = get_wchr();
18537c478bd9Sstevel@tonic-gate 			if (c != '\n')
18547c478bd9Sstevel@tonic-gate 				*gp++ = '\\';
18557c478bd9Sstevel@tonic-gate 		}
18567c478bd9Sstevel@tonic-gate 		if ((gp + (unsigned int)MB_CUR_MAX) >= &globuf[LBSIZE-1])
18577c478bd9Sstevel@tonic-gate 			(void) error(34);
18587c478bd9Sstevel@tonic-gate 		if ((len = wctomb(gp, c)) <= 0) {
18597c478bd9Sstevel@tonic-gate 			*gp = (unsigned char)c;
18607c478bd9Sstevel@tonic-gate 			len = 1;
18617c478bd9Sstevel@tonic-gate 		}
18627c478bd9Sstevel@tonic-gate 		gp += len;
18637c478bd9Sstevel@tonic-gate 	}
18647c478bd9Sstevel@tonic-gate 	if (gp == globuf)
18657c478bd9Sstevel@tonic-gate 		*gp++ = 'p';
18667c478bd9Sstevel@tonic-gate 	*gp++ = '\n';
18677c478bd9Sstevel@tonic-gate 	*gp++ = 0;
18687c478bd9Sstevel@tonic-gate 	for (a1 = zero; a1 <= dol; a1++) {
18697c478bd9Sstevel@tonic-gate 		a1->cur &= ~01;
18707c478bd9Sstevel@tonic-gate 		if (a1 >= addr1 && a1 <= addr2 && execute(0, a1) == k)
18717c478bd9Sstevel@tonic-gate 			a1->cur |= 01;
18727c478bd9Sstevel@tonic-gate 	}
18737c478bd9Sstevel@tonic-gate 	/*
18747c478bd9Sstevel@tonic-gate 	 * Special case: g/.../d (avoid n^2 algorithm)
18757c478bd9Sstevel@tonic-gate 	 */
18767c478bd9Sstevel@tonic-gate 	if (globuf[0] == 'd' && globuf[1] == '\n' && globuf[2] == '\0') {
18777c478bd9Sstevel@tonic-gate 		gdelete();
18787c478bd9Sstevel@tonic-gate 		return;
18797c478bd9Sstevel@tonic-gate 	}
18807c478bd9Sstevel@tonic-gate 	for (a1 = zero; a1 <= dol; a1++) {
18817c478bd9Sstevel@tonic-gate 		if (a1->cur & 01) {
18827c478bd9Sstevel@tonic-gate 			a1->cur &= ~01;
18837c478bd9Sstevel@tonic-gate 			dot = a1;
18847c478bd9Sstevel@tonic-gate 			globp = globuf;
18857c478bd9Sstevel@tonic-gate 			globflg = 1;
18867c478bd9Sstevel@tonic-gate 			commands();
18877c478bd9Sstevel@tonic-gate 			globflg = 0;
18887c478bd9Sstevel@tonic-gate 			a1 = zero;
18897c478bd9Sstevel@tonic-gate 		}
18907c478bd9Sstevel@tonic-gate 	}
18917c478bd9Sstevel@tonic-gate }
18927c478bd9Sstevel@tonic-gate 
18937c478bd9Sstevel@tonic-gate static void
join(void)18947c478bd9Sstevel@tonic-gate join(void)
18957c478bd9Sstevel@tonic-gate {
18967c478bd9Sstevel@tonic-gate 	char *gp, *lp;
18977c478bd9Sstevel@tonic-gate 	LINE a1;
18987c478bd9Sstevel@tonic-gate 
18997c478bd9Sstevel@tonic-gate 	if (addr1 == addr2)
19007c478bd9Sstevel@tonic-gate 		return;
19017c478bd9Sstevel@tonic-gate 	gp = genbuf;
19027c478bd9Sstevel@tonic-gate 	for (a1 = addr1; a1 <= addr2; a1++) {
190323a1cceaSRoger A. Faulkner 		lp = getaline(a1->cur);
19047c478bd9Sstevel@tonic-gate 		while (*gp = *lp++)
19057c478bd9Sstevel@tonic-gate 			if (gp++ > &genbuf[LBSIZE-1])
19067c478bd9Sstevel@tonic-gate 				(void) error(27);
19077c478bd9Sstevel@tonic-gate 	}
19087c478bd9Sstevel@tonic-gate 	lp = linebuf;
19097c478bd9Sstevel@tonic-gate 	gp = genbuf;
19107c478bd9Sstevel@tonic-gate 	while (*lp++ = *gp++);
19117c478bd9Sstevel@tonic-gate 	addr1->cur = putline();
19127c478bd9Sstevel@tonic-gate 	if (addr1 < addr2)
19137c478bd9Sstevel@tonic-gate 		rdelete(addr1+1, addr2);
19147c478bd9Sstevel@tonic-gate 	dot = addr1;
19157c478bd9Sstevel@tonic-gate }
19167c478bd9Sstevel@tonic-gate 
19177c478bd9Sstevel@tonic-gate static void
substitute(int inglob)19187c478bd9Sstevel@tonic-gate substitute(int inglob)
19197c478bd9Sstevel@tonic-gate {
19207c478bd9Sstevel@tonic-gate 	int nl;
19217c478bd9Sstevel@tonic-gate 	LINE a1;
19227c478bd9Sstevel@tonic-gate 	long *markp;
19237c478bd9Sstevel@tonic-gate 	int ingsav;		/* For saving arg. */
19247c478bd9Sstevel@tonic-gate 
19257c478bd9Sstevel@tonic-gate 	ingsav = inglob;
19267c478bd9Sstevel@tonic-gate 	ocerr2 = 0;
19277c478bd9Sstevel@tonic-gate 	gsubf = compsub();
19287c478bd9Sstevel@tonic-gate 	for (a1 = addr1; a1 <= addr2; a1++) {
19297c478bd9Sstevel@tonic-gate 		if (execute(0, a1) == 0)
19307c478bd9Sstevel@tonic-gate 			continue;
19317c478bd9Sstevel@tonic-gate 		numpass = 0;
19327c478bd9Sstevel@tonic-gate 		ocerr1 = 0;
19337c478bd9Sstevel@tonic-gate 		inglob |= 01;
19347c478bd9Sstevel@tonic-gate 		dosub();
19357c478bd9Sstevel@tonic-gate 		if (gsubf) {
19367c478bd9Sstevel@tonic-gate 			while (*loc2) {
19377c478bd9Sstevel@tonic-gate 				if (execute(1, (LINE)0) == 0)
19387c478bd9Sstevel@tonic-gate 					break;
19397c478bd9Sstevel@tonic-gate 				dosub();
19407c478bd9Sstevel@tonic-gate 			}
19417c478bd9Sstevel@tonic-gate 		}
19427c478bd9Sstevel@tonic-gate 		if (ocerr1 == 0)continue;	/* Don't put out-not changed. */
19437c478bd9Sstevel@tonic-gate 		subnewa = putline();
19447c478bd9Sstevel@tonic-gate 		a1->cur &= ~01;
19457c478bd9Sstevel@tonic-gate 		if (anymarks) {
19467c478bd9Sstevel@tonic-gate 			for (markp = names; markp < &names[26]; markp++)
19477c478bd9Sstevel@tonic-gate 				if (*markp == a1->cur)
19487c478bd9Sstevel@tonic-gate 					*markp = subnewa;
19497c478bd9Sstevel@tonic-gate 		}
19507c478bd9Sstevel@tonic-gate 		a1->cur = subnewa;
19517c478bd9Sstevel@tonic-gate 		append(getsub, a1);
19527c478bd9Sstevel@tonic-gate 		nl = nline;
19537c478bd9Sstevel@tonic-gate 		a1 += nl;
19547c478bd9Sstevel@tonic-gate 		addr2 += nl;
19557c478bd9Sstevel@tonic-gate 	}
19567c478bd9Sstevel@tonic-gate 	if (ingsav)
19577c478bd9Sstevel@tonic-gate 		return;	/* Was in global-no error msg allowed. */
19587c478bd9Sstevel@tonic-gate 	if (inglob == 0)
19597c478bd9Sstevel@tonic-gate 		(void) error(35);	/* Not in global, but not found. */
19607c478bd9Sstevel@tonic-gate 	if (ocerr2 == 0)
19617c478bd9Sstevel@tonic-gate 		(void) error(35); /* RE found, but occurrence match failed. */
19627c478bd9Sstevel@tonic-gate }
19637c478bd9Sstevel@tonic-gate 
19647c478bd9Sstevel@tonic-gate static int
compsub(void)19657c478bd9Sstevel@tonic-gate compsub(void)
19667c478bd9Sstevel@tonic-gate {
19677c478bd9Sstevel@tonic-gate 	int c;
19687c478bd9Sstevel@tonic-gate 	wchar_t seof;
19697c478bd9Sstevel@tonic-gate 	char *p;
19707c478bd9Sstevel@tonic-gate 	char multic[MB_LEN_MAX];
19717c478bd9Sstevel@tonic-gate 	int n;
19727c478bd9Sstevel@tonic-gate 	static char remem[RHSIZE];
19737c478bd9Sstevel@tonic-gate 	static int remflg = -1;
19747c478bd9Sstevel@tonic-gate 	int i;
19757c478bd9Sstevel@tonic-gate 
19767c478bd9Sstevel@tonic-gate 	if ((n = _mbftowc(multic, &seof, getchr, &peekc)) <= 0)
19777c478bd9Sstevel@tonic-gate 		(void) error(67);
19787c478bd9Sstevel@tonic-gate 	if (seof == '\n' || seof == ' ')
19797c478bd9Sstevel@tonic-gate 		(void) error(36);
19807c478bd9Sstevel@tonic-gate 	comple(seof);
19817c478bd9Sstevel@tonic-gate 	p = rhsbuf;
19827c478bd9Sstevel@tonic-gate 	for (;;) {
19837c478bd9Sstevel@tonic-gate 		wchar_t cl;
19847c478bd9Sstevel@tonic-gate 		if ((n = _mbftowc(multic, &cl, getchr, &peekc)) <= 0)
19857c478bd9Sstevel@tonic-gate 			(void) error(67);
19867c478bd9Sstevel@tonic-gate 		if (cl == '\\') {
19877c478bd9Sstevel@tonic-gate 			*p++ = '\\';
19887c478bd9Sstevel@tonic-gate 			if (p >= &rhsbuf[RHSIZE])
19897c478bd9Sstevel@tonic-gate 				(void) error(38);
19907c478bd9Sstevel@tonic-gate 			if ((n = _mbftowc(multic, &cl, getchr, &peekc)) <= 0)
19917c478bd9Sstevel@tonic-gate 				(void) error(67);
19927c478bd9Sstevel@tonic-gate 		} else if (cl == '\n') {
19937c478bd9Sstevel@tonic-gate 			if (nodelim == 1) {
19947c478bd9Sstevel@tonic-gate 				nodelim = 0;
19957c478bd9Sstevel@tonic-gate 				(void) error(36);
19967c478bd9Sstevel@tonic-gate 			}
19977c478bd9Sstevel@tonic-gate 			if (!(globp && globp[0])) {
19987c478bd9Sstevel@tonic-gate 				UNGETC('\n');
19997c478bd9Sstevel@tonic-gate 				pflag++;
20007c478bd9Sstevel@tonic-gate 				break;
20017c478bd9Sstevel@tonic-gate 			}
20027c478bd9Sstevel@tonic-gate 		} else if (cl == seof)
20037c478bd9Sstevel@tonic-gate 			break;
20047c478bd9Sstevel@tonic-gate 		if (p + n > &rhsbuf[RHSIZE])
20057c478bd9Sstevel@tonic-gate 			(void) error(38);
20067c478bd9Sstevel@tonic-gate 		(void) strncpy(p, multic, n);
20077c478bd9Sstevel@tonic-gate 		p += n;
20087c478bd9Sstevel@tonic-gate 	}
20097c478bd9Sstevel@tonic-gate 	*p++ = 0;
20107c478bd9Sstevel@tonic-gate 	if (rhsbuf[0] == '%' && rhsbuf[1] == 0)
20117c478bd9Sstevel@tonic-gate 		/*
20127c478bd9Sstevel@tonic-gate 		 * If there isn't a remembered string, it is an error;
20137c478bd9Sstevel@tonic-gate 		 * otherwise the right hand side is the previous right
20147c478bd9Sstevel@tonic-gate 		 * hand side.
20157c478bd9Sstevel@tonic-gate 		 */
20167c478bd9Sstevel@tonic-gate 
20177c478bd9Sstevel@tonic-gate 		if (remflg == -1)
20187c478bd9Sstevel@tonic-gate 			(void) error(55);
20197c478bd9Sstevel@tonic-gate 		else
20207c478bd9Sstevel@tonic-gate 			strcpy(rhsbuf, remem);
20217c478bd9Sstevel@tonic-gate 	else {
20227c478bd9Sstevel@tonic-gate 		strcpy(remem, rhsbuf);
20237c478bd9Sstevel@tonic-gate 		remflg = 0;
20247c478bd9Sstevel@tonic-gate 	}
20257c478bd9Sstevel@tonic-gate 	c = 0;
20267c478bd9Sstevel@tonic-gate 	peekc = getchr();	/* Gets char after third delimiter. */
20277c478bd9Sstevel@tonic-gate 	if (peekc == 'g') {
20287c478bd9Sstevel@tonic-gate 		c = LBSIZE; peekc = 0;
20297c478bd9Sstevel@tonic-gate 	}
20307c478bd9Sstevel@tonic-gate 	if (peekc >= '1' && peekc <= '9') {
20317c478bd9Sstevel@tonic-gate 		c = peekc-'0';
20327c478bd9Sstevel@tonic-gate 		peekc = 0;	/* Allows getchr() to get next char. */
20337c478bd9Sstevel@tonic-gate 		while (1) {
20347c478bd9Sstevel@tonic-gate 			i = getchr();
20357c478bd9Sstevel@tonic-gate 			if (i < '0' || i > '9')
20367c478bd9Sstevel@tonic-gate 				break;
20377c478bd9Sstevel@tonic-gate 			c = c*10 + i-'0';
20387c478bd9Sstevel@tonic-gate 			if (c > LBSIZE-1)
20397c478bd9Sstevel@tonic-gate 				(void) error(20);	/* "Illegal suffix" */
20407c478bd9Sstevel@tonic-gate 			}
20417c478bd9Sstevel@tonic-gate 		peekc = i;	/* Effectively an unget. */
20427c478bd9Sstevel@tonic-gate 		}
20437c478bd9Sstevel@tonic-gate 	newline();
20447c478bd9Sstevel@tonic-gate 	return (c);
20457c478bd9Sstevel@tonic-gate 
20467c478bd9Sstevel@tonic-gate 	/*
20477c478bd9Sstevel@tonic-gate 	 * Returns occurrence value. 0 & 1 both do first occurrence
20487c478bd9Sstevel@tonic-gate 	 * only: c = 0 if ordinary substitute; c = 1
20497c478bd9Sstevel@tonic-gate 	 * if use 1 in global sub(s/a/b/1). 0 in global form is illegal.
20507c478bd9Sstevel@tonic-gate 	 */
20517c478bd9Sstevel@tonic-gate }
20527c478bd9Sstevel@tonic-gate 
20537c478bd9Sstevel@tonic-gate static int
getsub(void)20547c478bd9Sstevel@tonic-gate getsub(void)
20557c478bd9Sstevel@tonic-gate {
20567c478bd9Sstevel@tonic-gate 	char *p1, *p2;
20577c478bd9Sstevel@tonic-gate 
20587c478bd9Sstevel@tonic-gate 	p1 = linebuf;
20597c478bd9Sstevel@tonic-gate 	if ((p2 = linebp) == 0)
20607c478bd9Sstevel@tonic-gate 		return (EOF);
20617c478bd9Sstevel@tonic-gate 	while (*p1++ = *p2++);
20627c478bd9Sstevel@tonic-gate 	linebp = 0;
20637c478bd9Sstevel@tonic-gate 	return (0);
20647c478bd9Sstevel@tonic-gate }
20657c478bd9Sstevel@tonic-gate 
20667c478bd9Sstevel@tonic-gate static void
dosub(void)20677c478bd9Sstevel@tonic-gate dosub(void)
20687c478bd9Sstevel@tonic-gate {
20697c478bd9Sstevel@tonic-gate 	char *lp, *sp, *rp;
20707c478bd9Sstevel@tonic-gate 	int c;
20717c478bd9Sstevel@tonic-gate 
20727c478bd9Sstevel@tonic-gate 	if (gsubf > 0 && gsubf < LBSIZE) {
20737c478bd9Sstevel@tonic-gate 		numpass++;
20747c478bd9Sstevel@tonic-gate 		if (gsubf != numpass)
20757c478bd9Sstevel@tonic-gate 			return;
20767c478bd9Sstevel@tonic-gate 	}
20777c478bd9Sstevel@tonic-gate 	ocerr1++;
20787c478bd9Sstevel@tonic-gate 	ocerr2++;
20797c478bd9Sstevel@tonic-gate 	lp = linebuf;
20807c478bd9Sstevel@tonic-gate 	sp = genbuf;
20817c478bd9Sstevel@tonic-gate 	rp = rhsbuf;
20827c478bd9Sstevel@tonic-gate 	while (lp < loc1)
20837c478bd9Sstevel@tonic-gate 		*sp++ = *lp++;
20847c478bd9Sstevel@tonic-gate 	while (c = *rp++) {
20857c478bd9Sstevel@tonic-gate 		if (c == '&') {
20867c478bd9Sstevel@tonic-gate 			sp = place(sp, loc1, loc2);
20877c478bd9Sstevel@tonic-gate 			continue;
20887c478bd9Sstevel@tonic-gate 		} else if (c == '\\') {
20897c478bd9Sstevel@tonic-gate 			c = *rp++;
20907c478bd9Sstevel@tonic-gate 			if (c >= '1' && c < nbra + '1') {
20917c478bd9Sstevel@tonic-gate 			sp = place(sp, braslist[c-'1'], braelist[c-'1']);
20927c478bd9Sstevel@tonic-gate 				continue;
20937c478bd9Sstevel@tonic-gate 			}
20947c478bd9Sstevel@tonic-gate 		}
20957c478bd9Sstevel@tonic-gate 		*sp++ = c;
20967c478bd9Sstevel@tonic-gate 		if (sp >= &genbuf[LBSIZE])
20977c478bd9Sstevel@tonic-gate 			(void) error(27);
20987c478bd9Sstevel@tonic-gate 	}
20997c478bd9Sstevel@tonic-gate 	lp = loc2;
21007c478bd9Sstevel@tonic-gate 	loc2 = sp - genbuf + linebuf;
21017c478bd9Sstevel@tonic-gate 	while (*sp++ = *lp++)
21027c478bd9Sstevel@tonic-gate 		if (sp >= &genbuf[LBSIZE])
21037c478bd9Sstevel@tonic-gate 			(void) error(27);
21047c478bd9Sstevel@tonic-gate 	lp = linebuf;
21057c478bd9Sstevel@tonic-gate 	sp = genbuf;
21067c478bd9Sstevel@tonic-gate 	while (*lp++ = *sp++);
21077c478bd9Sstevel@tonic-gate }
21087c478bd9Sstevel@tonic-gate 
21097c478bd9Sstevel@tonic-gate static char *
place(char * sp,char * l1,char * l2)21107c478bd9Sstevel@tonic-gate place(char *sp, char *l1, char *l2)
21117c478bd9Sstevel@tonic-gate {
21127c478bd9Sstevel@tonic-gate 
21137c478bd9Sstevel@tonic-gate 	while (l1 < l2) {
21147c478bd9Sstevel@tonic-gate 		*sp++ = *l1++;
21157c478bd9Sstevel@tonic-gate 		if (sp >= &genbuf[LBSIZE])
21167c478bd9Sstevel@tonic-gate 			(void) error(27);
21177c478bd9Sstevel@tonic-gate 	}
21187c478bd9Sstevel@tonic-gate 	return (sp);
21197c478bd9Sstevel@tonic-gate }
21207c478bd9Sstevel@tonic-gate 
21217c478bd9Sstevel@tonic-gate static void
comple(wchar_t seof)21227c478bd9Sstevel@tonic-gate comple(wchar_t seof)
21237c478bd9Sstevel@tonic-gate {
21247c478bd9Sstevel@tonic-gate 	int cclass = 0;
21257c478bd9Sstevel@tonic-gate 	wchar_t c;
21267c478bd9Sstevel@tonic-gate 	int n;
21277c478bd9Sstevel@tonic-gate 	char *cp = genbuf;
21287c478bd9Sstevel@tonic-gate 	char multic[MB_LEN_MAX];
21297c478bd9Sstevel@tonic-gate 
21307c478bd9Sstevel@tonic-gate 	while (1) {
21317c478bd9Sstevel@tonic-gate 		if ((n = _mbftowc(multic, &c, getchr, &peekc)) < 0)
21327c478bd9Sstevel@tonic-gate 			error1(67);
21337c478bd9Sstevel@tonic-gate 		if (n == 0 || c == '\n') {
21347c478bd9Sstevel@tonic-gate 			if (cclass)
21357c478bd9Sstevel@tonic-gate 				error1(49);
21367c478bd9Sstevel@tonic-gate 			else
21377c478bd9Sstevel@tonic-gate 				break;
21387c478bd9Sstevel@tonic-gate 		}
21397c478bd9Sstevel@tonic-gate 		if (c == seof && !cclass)
21407c478bd9Sstevel@tonic-gate 			break;
21417c478bd9Sstevel@tonic-gate 		if (cclass && c == ']') {
21427c478bd9Sstevel@tonic-gate 			cclass = 0;
21437c478bd9Sstevel@tonic-gate 			if (cp > &genbuf[LBSIZE-1])
21447c478bd9Sstevel@tonic-gate 				error1(50);
21457c478bd9Sstevel@tonic-gate 			*cp++ = ']';
21467c478bd9Sstevel@tonic-gate 			continue;
21477c478bd9Sstevel@tonic-gate 		}
21487c478bd9Sstevel@tonic-gate 		if (c == '[' && !cclass) {
21497c478bd9Sstevel@tonic-gate 			cclass = 1;
21507c478bd9Sstevel@tonic-gate 			if (cp > &genbuf[LBSIZE-1])
21517c478bd9Sstevel@tonic-gate 				error1(50);
21527c478bd9Sstevel@tonic-gate 			*cp++ = '[';
21537c478bd9Sstevel@tonic-gate 			if ((n = _mbftowc(multic, &c, getchr, &peekc)) < 0)
21547c478bd9Sstevel@tonic-gate 				error1(67);
21557c478bd9Sstevel@tonic-gate 			if (n == 0 || c == '\n')
21567c478bd9Sstevel@tonic-gate 				error1(49);
21577c478bd9Sstevel@tonic-gate 		}
21587c478bd9Sstevel@tonic-gate 		if (c == '\\' && !cclass) {
21597c478bd9Sstevel@tonic-gate 			if (cp > &genbuf[LBSIZE-1])
21607c478bd9Sstevel@tonic-gate 				error1(50);
21617c478bd9Sstevel@tonic-gate 			*cp++ = '\\';
21627c478bd9Sstevel@tonic-gate 			if ((n = _mbftowc(multic, &c, getchr, &peekc)) < 0)
21637c478bd9Sstevel@tonic-gate 				error1(67);
21647c478bd9Sstevel@tonic-gate 			if (n == 0 || c == '\n')
21657c478bd9Sstevel@tonic-gate 				error1(36);
21667c478bd9Sstevel@tonic-gate 		}
21677c478bd9Sstevel@tonic-gate 		if (cp + n > &genbuf[LBSIZE-1])
21687c478bd9Sstevel@tonic-gate 			error1(50);
21697c478bd9Sstevel@tonic-gate 		(void) strncpy(cp, multic, n);
21707c478bd9Sstevel@tonic-gate 		cp += n;
21717c478bd9Sstevel@tonic-gate 	}
21727c478bd9Sstevel@tonic-gate 	*cp = '\0';
21737c478bd9Sstevel@tonic-gate 	if (n != 0 && c == '\n')
21747c478bd9Sstevel@tonic-gate 		UNGETC('\n');
21757c478bd9Sstevel@tonic-gate 	if (n == 0 || c == '\n')
21767c478bd9Sstevel@tonic-gate 		nodelim = 1;
21777c478bd9Sstevel@tonic-gate 
21787c478bd9Sstevel@tonic-gate 	/*
21797c478bd9Sstevel@tonic-gate 	 * NULL RE: do not compile a null regular expression; but process
21807c478bd9Sstevel@tonic-gate 	 * input with last regular expression encountered
21817c478bd9Sstevel@tonic-gate 	 */
21827c478bd9Sstevel@tonic-gate 
21837c478bd9Sstevel@tonic-gate 	if (genbuf[0] != '\0') {
21847c478bd9Sstevel@tonic-gate 		if (expbuf)
21857c478bd9Sstevel@tonic-gate 			free(expbuf);
21867c478bd9Sstevel@tonic-gate 		expbuf = compile(genbuf, (char *)0, (char *)0);
21877c478bd9Sstevel@tonic-gate 	}
21887c478bd9Sstevel@tonic-gate 	if (regerrno)
21897c478bd9Sstevel@tonic-gate 		error1(regerrno);
21907c478bd9Sstevel@tonic-gate }
21917c478bd9Sstevel@tonic-gate 
21927c478bd9Sstevel@tonic-gate static void
move(int cflag)21937c478bd9Sstevel@tonic-gate move(int cflag)
21947c478bd9Sstevel@tonic-gate {
21957c478bd9Sstevel@tonic-gate 	LINE adt, ad1, ad2;
21967c478bd9Sstevel@tonic-gate 
21977c478bd9Sstevel@tonic-gate 	setdot();
21987c478bd9Sstevel@tonic-gate 	nonzero();
21997c478bd9Sstevel@tonic-gate 	if ((adt = address()) == 0)
22007c478bd9Sstevel@tonic-gate 		(void) error(39);
22017c478bd9Sstevel@tonic-gate 	newline();
22027c478bd9Sstevel@tonic-gate 	if (!globflg) save();
22037c478bd9Sstevel@tonic-gate 	if (cflag) {
22047c478bd9Sstevel@tonic-gate 		ad1 = dol;
22057c478bd9Sstevel@tonic-gate 		append(getcopy, ad1++);
22067c478bd9Sstevel@tonic-gate 		ad2 = dol;
22077c478bd9Sstevel@tonic-gate 	} else {
22087c478bd9Sstevel@tonic-gate 		ad2 = addr2;
22097c478bd9Sstevel@tonic-gate 		for (ad1 = addr1; ad1 <= ad2; )
22107c478bd9Sstevel@tonic-gate 			(ad1++)->cur &= ~01;
22117c478bd9Sstevel@tonic-gate 		ad1 = addr1;
22127c478bd9Sstevel@tonic-gate 	}
22137c478bd9Sstevel@tonic-gate 	ad2++;
22147c478bd9Sstevel@tonic-gate 	if (adt < ad1) {
22157c478bd9Sstevel@tonic-gate 		dot = adt + (ad2-ad1);
22167c478bd9Sstevel@tonic-gate 		if ((++adt) == ad1)
22177c478bd9Sstevel@tonic-gate 			return;
22187c478bd9Sstevel@tonic-gate 		reverse(adt, ad1);
22197c478bd9Sstevel@tonic-gate 		reverse(ad1, ad2);
22207c478bd9Sstevel@tonic-gate 		reverse(adt, ad2);
22217c478bd9Sstevel@tonic-gate 	} else if (adt >= ad2) {
22227c478bd9Sstevel@tonic-gate 		dot = adt++;
22237c478bd9Sstevel@tonic-gate 		reverse(ad1, ad2);
22247c478bd9Sstevel@tonic-gate 		reverse(ad2, adt);
22257c478bd9Sstevel@tonic-gate 		reverse(ad1, adt);
22267c478bd9Sstevel@tonic-gate 	} else
22277c478bd9Sstevel@tonic-gate 		(void) error(39);
22287c478bd9Sstevel@tonic-gate 	fchange = 1;
22297c478bd9Sstevel@tonic-gate }
22307c478bd9Sstevel@tonic-gate 
22317c478bd9Sstevel@tonic-gate static void
reverse(LINE a1,LINE a2)22327c478bd9Sstevel@tonic-gate reverse(LINE a1, LINE a2)
22337c478bd9Sstevel@tonic-gate {
22347c478bd9Sstevel@tonic-gate 	long t;
22357c478bd9Sstevel@tonic-gate 
22367c478bd9Sstevel@tonic-gate 	for (;;) {
22377c478bd9Sstevel@tonic-gate 		t = (--a2)->cur;
22387c478bd9Sstevel@tonic-gate 		if (a2 <= a1)
22397c478bd9Sstevel@tonic-gate 			return;
22407c478bd9Sstevel@tonic-gate 		a2->cur = a1->cur;
22417c478bd9Sstevel@tonic-gate 		(a1++)->cur = t;
22427c478bd9Sstevel@tonic-gate 	}
22437c478bd9Sstevel@tonic-gate }
22447c478bd9Sstevel@tonic-gate 
22457c478bd9Sstevel@tonic-gate static int
getcopy(void)22467c478bd9Sstevel@tonic-gate getcopy(void)
22477c478bd9Sstevel@tonic-gate {
22487c478bd9Sstevel@tonic-gate 
22497c478bd9Sstevel@tonic-gate 	if (addr1 > addr2)
22507c478bd9Sstevel@tonic-gate 		return (EOF);
225123a1cceaSRoger A. Faulkner 	(void) getaline((addr1++)->cur);
22527c478bd9Sstevel@tonic-gate 	return (0);
22537c478bd9Sstevel@tonic-gate }
22547c478bd9Sstevel@tonic-gate 
22557c478bd9Sstevel@tonic-gate 
22567c478bd9Sstevel@tonic-gate /*
22577c478bd9Sstevel@tonic-gate  * Handles error code returned from comple() routine: regular expression
22587c478bd9Sstevel@tonic-gate  * compile and match routines
22597c478bd9Sstevel@tonic-gate  */
22607c478bd9Sstevel@tonic-gate 
22617c478bd9Sstevel@tonic-gate static void
error1(int code)22627c478bd9Sstevel@tonic-gate error1(int code)
22637c478bd9Sstevel@tonic-gate {
22647c478bd9Sstevel@tonic-gate 	nbra = 0;
22657c478bd9Sstevel@tonic-gate 	(void) error(code);
22667c478bd9Sstevel@tonic-gate }
22677c478bd9Sstevel@tonic-gate 
22687c478bd9Sstevel@tonic-gate 
22697c478bd9Sstevel@tonic-gate static int
execute(int gf,LINE addr)22707c478bd9Sstevel@tonic-gate execute(int gf, LINE addr)
22717c478bd9Sstevel@tonic-gate {
22727c478bd9Sstevel@tonic-gate 	char *p1;
22737c478bd9Sstevel@tonic-gate 	int c;
22747c478bd9Sstevel@tonic-gate 
22757c478bd9Sstevel@tonic-gate 	for (c = 0; c < nbra; c++) {
22767c478bd9Sstevel@tonic-gate 		braslist[c] = 0;
22777c478bd9Sstevel@tonic-gate 		braelist[c] = 0;
22787c478bd9Sstevel@tonic-gate 	}
22797c478bd9Sstevel@tonic-gate 	if (gf)
22807c478bd9Sstevel@tonic-gate 		locs = p1 = loc2;
22817c478bd9Sstevel@tonic-gate 	else {
22827c478bd9Sstevel@tonic-gate 		if (addr == zero)
22837c478bd9Sstevel@tonic-gate 			return (0);
228423a1cceaSRoger A. Faulkner 		p1 = getaline(addr->cur);
22857c478bd9Sstevel@tonic-gate 		locs = 0;
22867c478bd9Sstevel@tonic-gate 	}
22877c478bd9Sstevel@tonic-gate 	return (step(p1, expbuf));
22887c478bd9Sstevel@tonic-gate }
22897c478bd9Sstevel@tonic-gate 
22907c478bd9Sstevel@tonic-gate 
22917c478bd9Sstevel@tonic-gate static void
putd()22927c478bd9Sstevel@tonic-gate putd()
22937c478bd9Sstevel@tonic-gate {
22947c478bd9Sstevel@tonic-gate 	int r;
22957c478bd9Sstevel@tonic-gate 
22967c478bd9Sstevel@tonic-gate 	r = (int)(count%10);
22977c478bd9Sstevel@tonic-gate 	count /= 10;
22987c478bd9Sstevel@tonic-gate 	if (count)
22997c478bd9Sstevel@tonic-gate 		putd();
23007c478bd9Sstevel@tonic-gate 	putchr(r + '0');
23017c478bd9Sstevel@tonic-gate }
23027c478bd9Sstevel@tonic-gate 
23037c478bd9Sstevel@tonic-gate 
23047c478bd9Sstevel@tonic-gate int
puts(const char * sp)23057c478bd9Sstevel@tonic-gate puts(const char *sp)
23067c478bd9Sstevel@tonic-gate {
23077c478bd9Sstevel@tonic-gate 	int n;
23087c478bd9Sstevel@tonic-gate 	wchar_t c;
23097c478bd9Sstevel@tonic-gate 	int sz, i;
23107c478bd9Sstevel@tonic-gate 	if (fss.Ffill && (listf == 0)) {
23117c478bd9Sstevel@tonic-gate 
23127c478bd9Sstevel@tonic-gate 		/* deliberate attempt to remove constness of sp because */
23137c478bd9Sstevel@tonic-gate 		/* it needs to be expanded */
23147c478bd9Sstevel@tonic-gate 
23157c478bd9Sstevel@tonic-gate 		if ((i = expnd((char *)sp, funny, &sz, &fss)) == -1) {
23167c478bd9Sstevel@tonic-gate 			write(1, funny, fss.Flim & 0377);
23177c478bd9Sstevel@tonic-gate 			putchr('\n');
23187c478bd9Sstevel@tonic-gate 			write(1, gettext("too long"),
23197c478bd9Sstevel@tonic-gate 				strlen(gettext("too long")));
23207c478bd9Sstevel@tonic-gate 		}
23217c478bd9Sstevel@tonic-gate 		else
23227c478bd9Sstevel@tonic-gate 			write(1, funny, sz);
23237c478bd9Sstevel@tonic-gate 		putchr('\n');
23247c478bd9Sstevel@tonic-gate 		if (i == -2)
23257c478bd9Sstevel@tonic-gate 			write(1, gettext("tab count\n"),
23267c478bd9Sstevel@tonic-gate 				strlen(gettext("tab count\n")));
23277c478bd9Sstevel@tonic-gate 		return (0);
23287c478bd9Sstevel@tonic-gate 	}
23297c478bd9Sstevel@tonic-gate 	col = 0;
23307c478bd9Sstevel@tonic-gate 	while (*sp) {
23317c478bd9Sstevel@tonic-gate 		n = mbtowc(&c, sp, MB_LEN_MAX);
23327c478bd9Sstevel@tonic-gate 		if (listf) {
23337c478bd9Sstevel@tonic-gate 			if (n < 1)
23347c478bd9Sstevel@tonic-gate 				(void) error(28);
23357c478bd9Sstevel@tonic-gate 			else if (n == 1)
23367c478bd9Sstevel@tonic-gate 				putchr((unsigned char)*sp++);
23377c478bd9Sstevel@tonic-gate 			else {
23387c478bd9Sstevel@tonic-gate 				sp += n;
23397c478bd9Sstevel@tonic-gate 				putwchr(c);
23407c478bd9Sstevel@tonic-gate 			}
23417c478bd9Sstevel@tonic-gate 		} else {
23427c478bd9Sstevel@tonic-gate 			putchr((unsigned char)*sp++);
23437c478bd9Sstevel@tonic-gate 		}
23447c478bd9Sstevel@tonic-gate 	}
23457c478bd9Sstevel@tonic-gate #ifndef XPG6
23467c478bd9Sstevel@tonic-gate 	if (listf)
23477c478bd9Sstevel@tonic-gate 		putchr('$');    /* end of line is marked with a $ */
23487c478bd9Sstevel@tonic-gate #else
23497c478bd9Sstevel@tonic-gate 	if (listf) {
23507c478bd9Sstevel@tonic-gate 	/* xpg6 - ensure that the end of line $ is not preceeded with a "\" */
23517c478bd9Sstevel@tonic-gate 	/* by doing a putchr() with listf=0, thereby avoiding the $ case */
23527c478bd9Sstevel@tonic-gate 	/* statement  in putchr() */
23537c478bd9Sstevel@tonic-gate 		listf = 0;
23547c478bd9Sstevel@tonic-gate 		putchr('$');    /* end of line is marked with a $ */
23557c478bd9Sstevel@tonic-gate 		listf++;
23567c478bd9Sstevel@tonic-gate 	}
23577c478bd9Sstevel@tonic-gate #endif
23587c478bd9Sstevel@tonic-gate 	putchr('\n');
23597c478bd9Sstevel@tonic-gate 	return (1);
23607c478bd9Sstevel@tonic-gate }
23617c478bd9Sstevel@tonic-gate 
23627c478bd9Sstevel@tonic-gate 
23637c478bd9Sstevel@tonic-gate static void
putwchr(wchar_t ac)23647c478bd9Sstevel@tonic-gate putwchr(wchar_t ac)
23657c478bd9Sstevel@tonic-gate {
23667c478bd9Sstevel@tonic-gate 	char buf[MB_LEN_MAX], *p;
23677c478bd9Sstevel@tonic-gate 	char *lp;
23687c478bd9Sstevel@tonic-gate 	wchar_t c;
23697c478bd9Sstevel@tonic-gate 	short len;
23707c478bd9Sstevel@tonic-gate 
23717c478bd9Sstevel@tonic-gate 	lp = linp;
23727c478bd9Sstevel@tonic-gate 	c = ac;
23737c478bd9Sstevel@tonic-gate 	if (listf) {
23747c478bd9Sstevel@tonic-gate 		if (!iswprint(c)) {
23757c478bd9Sstevel@tonic-gate 			p = &buf[0];
23767c478bd9Sstevel@tonic-gate 			if ((len = wctomb(p, c)) <= 0) {
23777c478bd9Sstevel@tonic-gate 				*p = (unsigned char)c;
23787c478bd9Sstevel@tonic-gate 				len = 1;
23797c478bd9Sstevel@tonic-gate 			};
23807c478bd9Sstevel@tonic-gate 			while (len--) {
23817c478bd9Sstevel@tonic-gate 				if (col + 4 >= 72) {
23827c478bd9Sstevel@tonic-gate 					col = 0;
23837c478bd9Sstevel@tonic-gate 					*lp++ = '\\';
23847c478bd9Sstevel@tonic-gate 					*lp++ = '\n';
23857c478bd9Sstevel@tonic-gate 				}
23867c478bd9Sstevel@tonic-gate 				(void) sprintf(lp, "\\%03o",
23877c478bd9Sstevel@tonic-gate 					*(unsigned char *)p++);
23887c478bd9Sstevel@tonic-gate 				col += 4;
23897c478bd9Sstevel@tonic-gate 				lp += 4;
23907c478bd9Sstevel@tonic-gate 			}
23917c478bd9Sstevel@tonic-gate 		} else {
23927c478bd9Sstevel@tonic-gate 			if ((len = wcwidth(c)) <= 0)
23937c478bd9Sstevel@tonic-gate 				len = 0;
23947c478bd9Sstevel@tonic-gate 			if (col + len >= 72) {
23957c478bd9Sstevel@tonic-gate 				col = 0;
23967c478bd9Sstevel@tonic-gate 				*lp++ = '\\';
23977c478bd9Sstevel@tonic-gate 				*lp++ = '\n';
23987c478bd9Sstevel@tonic-gate 			}
23997c478bd9Sstevel@tonic-gate 			col += len;
24007c478bd9Sstevel@tonic-gate 			if ((len = wctomb(lp, c)) <= 0) {
24017c478bd9Sstevel@tonic-gate 				*lp = (unsigned char)c;
24027c478bd9Sstevel@tonic-gate 				len = 1;
24037c478bd9Sstevel@tonic-gate 			}
24047c478bd9Sstevel@tonic-gate 			lp += len;
24057c478bd9Sstevel@tonic-gate 		}
24067c478bd9Sstevel@tonic-gate 	} else {
24077c478bd9Sstevel@tonic-gate 		if ((len = wctomb(lp, c)) <= 0) {
24087c478bd9Sstevel@tonic-gate 			*lp = (unsigned char)c;
24097c478bd9Sstevel@tonic-gate 			len = 1;
24107c478bd9Sstevel@tonic-gate 		}
24117c478bd9Sstevel@tonic-gate 		lp += len;
24127c478bd9Sstevel@tonic-gate 	}
24137c478bd9Sstevel@tonic-gate 	if (c == '\n' || lp >= &line[64]) {
24147c478bd9Sstevel@tonic-gate 		linp = line;
24157c478bd9Sstevel@tonic-gate 		len = lp - line;
24167c478bd9Sstevel@tonic-gate 		write(1, line, len);
24177c478bd9Sstevel@tonic-gate 		return;
24187c478bd9Sstevel@tonic-gate 	}
24197c478bd9Sstevel@tonic-gate 	linp = lp;
24207c478bd9Sstevel@tonic-gate }
24217c478bd9Sstevel@tonic-gate 
24227c478bd9Sstevel@tonic-gate 
24237c478bd9Sstevel@tonic-gate static void
putchr(unsigned char c)24247c478bd9Sstevel@tonic-gate putchr(unsigned char c)
24257c478bd9Sstevel@tonic-gate {
24267c478bd9Sstevel@tonic-gate 	char *lp;
24277c478bd9Sstevel@tonic-gate 	int len;
24287c478bd9Sstevel@tonic-gate 
24297c478bd9Sstevel@tonic-gate 	lp = linp;
24307c478bd9Sstevel@tonic-gate 	if (listf && c != '\n') {
24317c478bd9Sstevel@tonic-gate 		switch (c) {
24327c478bd9Sstevel@tonic-gate 			case '\\' :
24337c478bd9Sstevel@tonic-gate 				*lp++ = '\\';
24347c478bd9Sstevel@tonic-gate 				*lp++ = '\\';
24357c478bd9Sstevel@tonic-gate 				col += 2;
24367c478bd9Sstevel@tonic-gate 				break;
24377c478bd9Sstevel@tonic-gate 			case '\007' :
24387c478bd9Sstevel@tonic-gate 				*lp++ = '\\';
24397c478bd9Sstevel@tonic-gate 				*lp++ = 'a';
24407c478bd9Sstevel@tonic-gate 				col += 2;
24417c478bd9Sstevel@tonic-gate 				break;
24427c478bd9Sstevel@tonic-gate 			case '\b' :
24437c478bd9Sstevel@tonic-gate 				*lp++ = '\\';
24447c478bd9Sstevel@tonic-gate 				*lp++ = 'b';
24457c478bd9Sstevel@tonic-gate 				col += 2;
24467c478bd9Sstevel@tonic-gate 				break;
24477c478bd9Sstevel@tonic-gate 			case '\f' :
24487c478bd9Sstevel@tonic-gate 				*lp++ = '\\';
24497c478bd9Sstevel@tonic-gate 				*lp++ = 'f';
24507c478bd9Sstevel@tonic-gate 				col += 2;
24517c478bd9Sstevel@tonic-gate 				break;
24527c478bd9Sstevel@tonic-gate 			case '\r' :
24537c478bd9Sstevel@tonic-gate 				*lp++ = '\\';
24547c478bd9Sstevel@tonic-gate 				*lp++ = 'r';
24557c478bd9Sstevel@tonic-gate 				col += 2;
24567c478bd9Sstevel@tonic-gate 				break;
24577c478bd9Sstevel@tonic-gate 			case '\t' :
24587c478bd9Sstevel@tonic-gate 				*lp++ = '\\';
24597c478bd9Sstevel@tonic-gate 				*lp++ = 't';
24607c478bd9Sstevel@tonic-gate 				col += 2;
24617c478bd9Sstevel@tonic-gate 				break;
24627c478bd9Sstevel@tonic-gate 			case '\v' :
24637c478bd9Sstevel@tonic-gate 				*lp++ = '\\';
24647c478bd9Sstevel@tonic-gate 				*lp++ = 'v';
24657c478bd9Sstevel@tonic-gate 				col += 2;
24667c478bd9Sstevel@tonic-gate 				break;
24677c478bd9Sstevel@tonic-gate #ifdef XPG6
24687c478bd9Sstevel@tonic-gate 		/* if $ characters are within the line preceed with \ */
24697c478bd9Sstevel@tonic-gate 			case '$' :
24707c478bd9Sstevel@tonic-gate 				*lp++ = '\\';
24717c478bd9Sstevel@tonic-gate 				*lp++ = '$';
24727c478bd9Sstevel@tonic-gate 				col += 2;
24737c478bd9Sstevel@tonic-gate 				break;
24747c478bd9Sstevel@tonic-gate #endif
24757c478bd9Sstevel@tonic-gate 			default:
24767c478bd9Sstevel@tonic-gate 				if (isprint(c)) {
24777c478bd9Sstevel@tonic-gate 					*lp++ = c;
24787c478bd9Sstevel@tonic-gate 					col += 1;
24797c478bd9Sstevel@tonic-gate 				} else {
24807c478bd9Sstevel@tonic-gate 					(void) sprintf(lp, "\\%03o", c);
24817c478bd9Sstevel@tonic-gate 					col += 4;
24827c478bd9Sstevel@tonic-gate 					lp += 4;
24837c478bd9Sstevel@tonic-gate 				}
24847c478bd9Sstevel@tonic-gate 				break;
24857c478bd9Sstevel@tonic-gate 		}
24867c478bd9Sstevel@tonic-gate 
24877c478bd9Sstevel@tonic-gate 	/*
24887c478bd9Sstevel@tonic-gate 	 * long lines are folded w/ pt of folding indicated by writing
24897c478bd9Sstevel@tonic-gate 	 * backslash/newline character
24907c478bd9Sstevel@tonic-gate 	 */
24917c478bd9Sstevel@tonic-gate 
24927c478bd9Sstevel@tonic-gate 		if (col + 1 >= 72) {
24937c478bd9Sstevel@tonic-gate 			col = 0;
24947c478bd9Sstevel@tonic-gate 			*lp++ = '\\';
24957c478bd9Sstevel@tonic-gate 			*lp++ = '\n';
24967c478bd9Sstevel@tonic-gate 		}
24977c478bd9Sstevel@tonic-gate 	} else
24987c478bd9Sstevel@tonic-gate 		*lp++ = c;
24997c478bd9Sstevel@tonic-gate 	if (c == '\n' || lp >= &line[64]) {
25007c478bd9Sstevel@tonic-gate 		linp = line;
25017c478bd9Sstevel@tonic-gate 		len = lp - line;
25027c478bd9Sstevel@tonic-gate 		(void) write(1, line, len);
25037c478bd9Sstevel@tonic-gate 		return;
25047c478bd9Sstevel@tonic-gate 	}
25057c478bd9Sstevel@tonic-gate 	linp = lp;
25067c478bd9Sstevel@tonic-gate }
25077c478bd9Sstevel@tonic-gate 
25087c478bd9Sstevel@tonic-gate 
25097c478bd9Sstevel@tonic-gate static char *
getkey(const char * prompt)25107c478bd9Sstevel@tonic-gate getkey(const char *prompt)
25117c478bd9Sstevel@tonic-gate {
25127c478bd9Sstevel@tonic-gate 	struct termio b;
25137c478bd9Sstevel@tonic-gate 	int save;
25147c478bd9Sstevel@tonic-gate 	void (*sig)();
25157c478bd9Sstevel@tonic-gate 	static char key[KSIZE+1];
25167c478bd9Sstevel@tonic-gate 	char *p;
25177c478bd9Sstevel@tonic-gate 	int c;
25187c478bd9Sstevel@tonic-gate 
25197c478bd9Sstevel@tonic-gate 	sig = signal(SIGINT, SIG_IGN);
25207c478bd9Sstevel@tonic-gate 	ioctl(0, TCGETA, &b);
25217c478bd9Sstevel@tonic-gate 	save = b.c_lflag;
25227c478bd9Sstevel@tonic-gate 	b.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
25237c478bd9Sstevel@tonic-gate 	ioctl(0, TCSETAW, &b);
25247c478bd9Sstevel@tonic-gate 	write(1, gettext(prompt), strlen(gettext(prompt)));
25257c478bd9Sstevel@tonic-gate 	p = key;
25267c478bd9Sstevel@tonic-gate 	while (((c = getchr()) != EOF) && (c != '\n')) {
25277c478bd9Sstevel@tonic-gate 		if (p < &key[KSIZE])
25287c478bd9Sstevel@tonic-gate 			*p++ = c;
25297c478bd9Sstevel@tonic-gate 	}
25307c478bd9Sstevel@tonic-gate 	*p = 0;
25317c478bd9Sstevel@tonic-gate 	write(1, "\n", 1);
25327c478bd9Sstevel@tonic-gate 	b.c_lflag = save;
25337c478bd9Sstevel@tonic-gate 	ioctl(0, TCSETAW, &b);
25347c478bd9Sstevel@tonic-gate 	signal(SIGINT, sig);
25357c478bd9Sstevel@tonic-gate 	return (key);
25367c478bd9Sstevel@tonic-gate }
25377c478bd9Sstevel@tonic-gate 
25387c478bd9Sstevel@tonic-gate 
25397c478bd9Sstevel@tonic-gate static void
globaln(int k)25407c478bd9Sstevel@tonic-gate globaln(int k)
25417c478bd9Sstevel@tonic-gate {
25427c478bd9Sstevel@tonic-gate 	char *gp;
25437c478bd9Sstevel@tonic-gate 	int c;
25447c478bd9Sstevel@tonic-gate 	int n;
25457c478bd9Sstevel@tonic-gate 	wchar_t cl;
25467c478bd9Sstevel@tonic-gate 	LINE a1;
25477c478bd9Sstevel@tonic-gate 	int  nfirst;
25487c478bd9Sstevel@tonic-gate 	char globuf[LBSIZE];
25497c478bd9Sstevel@tonic-gate 	char multic[MB_LEN_MAX];
25507c478bd9Sstevel@tonic-gate 	int	len;
25517c478bd9Sstevel@tonic-gate 	int pflag_save = 0;
25527c478bd9Sstevel@tonic-gate 	int listf_save = 0;
25537c478bd9Sstevel@tonic-gate 	int listn_save = 0;
25547c478bd9Sstevel@tonic-gate 
25557c478bd9Sstevel@tonic-gate 	if (globp)
25567c478bd9Sstevel@tonic-gate 		(void) error(33);
25577c478bd9Sstevel@tonic-gate 	setall();
25587c478bd9Sstevel@tonic-gate 	nonzero();
25597c478bd9Sstevel@tonic-gate 	if ((n = _mbftowc(multic, &cl, getchr, &peekc)) <= 0)
25607c478bd9Sstevel@tonic-gate 		(void) error(67);
25617c478bd9Sstevel@tonic-gate 	if (cl == '\n')
25627c478bd9Sstevel@tonic-gate 		(void) error(19);
25637c478bd9Sstevel@tonic-gate 	save();
25647c478bd9Sstevel@tonic-gate 	comple(cl);
25657c478bd9Sstevel@tonic-gate 	for (a1 = zero; a1 <= dol; a1++) {
25667c478bd9Sstevel@tonic-gate 		a1->cur &= ~01;
25677c478bd9Sstevel@tonic-gate 		if (a1 >= addr1 && a1 <= addr2 && execute(0, a1) == k)
25687c478bd9Sstevel@tonic-gate 			a1->cur |= 01;
25697c478bd9Sstevel@tonic-gate 	}
25707c478bd9Sstevel@tonic-gate 	nfirst = 0;
25717c478bd9Sstevel@tonic-gate 	newline();
25727c478bd9Sstevel@tonic-gate 	/*
25737c478bd9Sstevel@tonic-gate 	 * preserve the p, l, and n suffix commands of the G and V
25747c478bd9Sstevel@tonic-gate 	 * commands during the interactive section and restore
25757c478bd9Sstevel@tonic-gate 	 * on completion of the G and V command.
25767c478bd9Sstevel@tonic-gate 	 */
25777c478bd9Sstevel@tonic-gate 	pflag_save = pflag;
25787c478bd9Sstevel@tonic-gate 	listf_save = listf;
25797c478bd9Sstevel@tonic-gate 	listn_save = listn;
25807c478bd9Sstevel@tonic-gate 	pflag = 0;
25817c478bd9Sstevel@tonic-gate 	listf = 0;
25827c478bd9Sstevel@tonic-gate 	listn = 0;
25837c478bd9Sstevel@tonic-gate 	for (a1 = zero; a1 <= dol; a1++) {
25847c478bd9Sstevel@tonic-gate 		if (a1->cur & 01) {
25857c478bd9Sstevel@tonic-gate 			a1->cur &= ~01;
25867c478bd9Sstevel@tonic-gate 			dot = a1;
258723a1cceaSRoger A. Faulkner 			puts(getaline(a1->cur));
25887c478bd9Sstevel@tonic-gate 			if ((c = get_wchr()) == EOF)
25897c478bd9Sstevel@tonic-gate 				(void) error(52);
25907c478bd9Sstevel@tonic-gate 			if (c == 'a' || c == 'i' || c == 'c')
25917c478bd9Sstevel@tonic-gate 				(void) error(53);
25927c478bd9Sstevel@tonic-gate 			if (c == '\n') {
25937c478bd9Sstevel@tonic-gate 				a1 = zero;
25947c478bd9Sstevel@tonic-gate 				continue;
25957c478bd9Sstevel@tonic-gate 			}
25967c478bd9Sstevel@tonic-gate 			if (c != '&') {
25977c478bd9Sstevel@tonic-gate 				gp = globuf;
25987c478bd9Sstevel@tonic-gate 				if ((len = wctomb(gp, c)) <= 0) {
25997c478bd9Sstevel@tonic-gate 					*gp = (unsigned char)c;
26007c478bd9Sstevel@tonic-gate 					len = 1;
26017c478bd9Sstevel@tonic-gate 				}
26027c478bd9Sstevel@tonic-gate 				gp += len;
26037c478bd9Sstevel@tonic-gate 				while ((c = get_wchr()) != '\n') {
26047c478bd9Sstevel@tonic-gate 
26057c478bd9Sstevel@tonic-gate 			/* '\\' has special meaning only if preceding a '\n' */
26067c478bd9Sstevel@tonic-gate 					if (c == '\\') {
26077c478bd9Sstevel@tonic-gate 						c = get_wchr();
26087c478bd9Sstevel@tonic-gate 						if (c != '\n')
26097c478bd9Sstevel@tonic-gate 							*gp++ = '\\';
26107c478bd9Sstevel@tonic-gate 					}
26117c478bd9Sstevel@tonic-gate 					if ((gp + (unsigned int)MB_CUR_MAX) >=
26127c478bd9Sstevel@tonic-gate 							&globuf[LBSIZE-1])
26137c478bd9Sstevel@tonic-gate 						(void) error(34);
26147c478bd9Sstevel@tonic-gate 
26157c478bd9Sstevel@tonic-gate 					if ((len = wctomb(gp, c)) <= 0) {
26167c478bd9Sstevel@tonic-gate 						*gp = (unsigned char)c;
26177c478bd9Sstevel@tonic-gate 						len = 1;
26187c478bd9Sstevel@tonic-gate 					}
26197c478bd9Sstevel@tonic-gate 					gp += len;
26207c478bd9Sstevel@tonic-gate 				}
26217c478bd9Sstevel@tonic-gate 				*gp++ = '\n';
26227c478bd9Sstevel@tonic-gate 				*gp++ = 0;
26237c478bd9Sstevel@tonic-gate 				nfirst = 1;
26247c478bd9Sstevel@tonic-gate 			} else if ((c = get_wchr()) != '\n')
26257c478bd9Sstevel@tonic-gate 				(void) error(54);
26267c478bd9Sstevel@tonic-gate 			globp = globuf;
26277c478bd9Sstevel@tonic-gate 			if (nfirst) {
26287c478bd9Sstevel@tonic-gate 				globflg = 1;
26297c478bd9Sstevel@tonic-gate 				commands();
26307c478bd9Sstevel@tonic-gate 				globflg = 0;
26317c478bd9Sstevel@tonic-gate 			} else
26327c478bd9Sstevel@tonic-gate 				(void) error(56);
26337c478bd9Sstevel@tonic-gate 			globp = 0;
26347c478bd9Sstevel@tonic-gate 			a1 = zero;
26357c478bd9Sstevel@tonic-gate 		}
26367c478bd9Sstevel@tonic-gate 	}
26377c478bd9Sstevel@tonic-gate 	pflag = pflag_save;
26387c478bd9Sstevel@tonic-gate 	listf = listf_save;
26397c478bd9Sstevel@tonic-gate 	listn = listn_save;
26407c478bd9Sstevel@tonic-gate }
26417c478bd9Sstevel@tonic-gate 
26427c478bd9Sstevel@tonic-gate 
26437c478bd9Sstevel@tonic-gate static int
eopen(char * string,int rw)26447c478bd9Sstevel@tonic-gate eopen(char *string, int rw)
26457c478bd9Sstevel@tonic-gate {
26467c478bd9Sstevel@tonic-gate #define	w_or_r(a, b) (rw ? a : b)
26477c478bd9Sstevel@tonic-gate 	int pf[2];
26487c478bd9Sstevel@tonic-gate 	pid_t i;
26497c478bd9Sstevel@tonic-gate 	int io;
26507c478bd9Sstevel@tonic-gate 	int chcount;	/* # of char read. */
26517c478bd9Sstevel@tonic-gate 
26527c478bd9Sstevel@tonic-gate 	if (rflg) {	/* restricted shell */
26537c478bd9Sstevel@tonic-gate 		if (Xqt) {
26547c478bd9Sstevel@tonic-gate 			Xqt = 0;
26557c478bd9Sstevel@tonic-gate 			(void) error(6);
26567c478bd9Sstevel@tonic-gate 		}
26577c478bd9Sstevel@tonic-gate 	}
26587c478bd9Sstevel@tonic-gate 	if (!Xqt) {
26597c478bd9Sstevel@tonic-gate 		if ((io = open(string, rw)) >= 0) {
26607c478bd9Sstevel@tonic-gate 			if (fflg) {
26617c478bd9Sstevel@tonic-gate 				chcount = read(io, crbuf, LBSIZE);
26627c478bd9Sstevel@tonic-gate 				if (crflag == -1) {
26637c478bd9Sstevel@tonic-gate 					if (isencrypt(crbuf, chcount))
26647c478bd9Sstevel@tonic-gate 						crflag = 2;
26657c478bd9Sstevel@tonic-gate 					else
26667c478bd9Sstevel@tonic-gate 						crflag = -2;
26677c478bd9Sstevel@tonic-gate 				}
26687c478bd9Sstevel@tonic-gate 				if (crflag > 0)
26697c478bd9Sstevel@tonic-gate 				if (run_crypt(0L, crbuf, chcount, perm) == -1)
26707c478bd9Sstevel@tonic-gate 						(void) error(63);
26717c478bd9Sstevel@tonic-gate 				if (fspec(crbuf, &fss, 0) < 0) {
26727c478bd9Sstevel@tonic-gate 					fss.Ffill = 0;
26737c478bd9Sstevel@tonic-gate 					fflg = 0;
26747c478bd9Sstevel@tonic-gate 					(void) error(4);
26757c478bd9Sstevel@tonic-gate 				}
26767c478bd9Sstevel@tonic-gate 				lseek(io, 0L, 0);
26777c478bd9Sstevel@tonic-gate 			}
26787c478bd9Sstevel@tonic-gate 		}
26797c478bd9Sstevel@tonic-gate 		fflg = 0;
26807c478bd9Sstevel@tonic-gate 		return (io);
26817c478bd9Sstevel@tonic-gate 	}
26827c478bd9Sstevel@tonic-gate 	if (pipe(pf) < 0)
26837c478bd9Sstevel@tonic-gate xerr:		(void) error(0);
26847c478bd9Sstevel@tonic-gate 	if ((i = fork()) == 0) {
26857c478bd9Sstevel@tonic-gate 		signal(SIGHUP, oldhup);
26867c478bd9Sstevel@tonic-gate 		signal(SIGQUIT, oldquit);
26877c478bd9Sstevel@tonic-gate 		signal(SIGPIPE, oldpipe);
26887c478bd9Sstevel@tonic-gate 		signal(SIGINT, (void (*)()) 0);
26897c478bd9Sstevel@tonic-gate 		close(w_or_r(pf[1], pf[0]));
26907c478bd9Sstevel@tonic-gate 		close(w_or_r(0, 1));
26917c478bd9Sstevel@tonic-gate 		dup(w_or_r(pf[0], pf[1]));
26927c478bd9Sstevel@tonic-gate 		close(w_or_r(pf[0], pf[1]));
2693*6a5408e6SRichard Lowe 		execlp(_PATH_BSHELL, "sh", "-c", string, (char *)0);
26947c478bd9Sstevel@tonic-gate 		exit(1);
26957c478bd9Sstevel@tonic-gate 	}
26967c478bd9Sstevel@tonic-gate 	if (i == (pid_t)-1)
26977c478bd9Sstevel@tonic-gate 		goto xerr;
26987c478bd9Sstevel@tonic-gate 	close(w_or_r(pf[0], pf[1]));
26997c478bd9Sstevel@tonic-gate 	return (w_or_r(pf[1], pf[0]));
27007c478bd9Sstevel@tonic-gate }
27017c478bd9Sstevel@tonic-gate 
27027c478bd9Sstevel@tonic-gate 
27037c478bd9Sstevel@tonic-gate static void
eclose(int f)27047c478bd9Sstevel@tonic-gate eclose(int f)
27057c478bd9Sstevel@tonic-gate {
27067c478bd9Sstevel@tonic-gate 	close(f);
27077c478bd9Sstevel@tonic-gate 	if (Xqt)
27087c478bd9Sstevel@tonic-gate 		Xqt = 0, wait((int *)0);
27097c478bd9Sstevel@tonic-gate }
27107c478bd9Sstevel@tonic-gate 
27117c478bd9Sstevel@tonic-gate 
27127c478bd9Sstevel@tonic-gate static void
mkfunny(void)27137c478bd9Sstevel@tonic-gate mkfunny(void)
27147c478bd9Sstevel@tonic-gate {
27157c478bd9Sstevel@tonic-gate 	char *p, *p1, *p2;
27167c478bd9Sstevel@tonic-gate 
27177c478bd9Sstevel@tonic-gate 	p2 = p1 = funny;
27187c478bd9Sstevel@tonic-gate 	p = file;
27197c478bd9Sstevel@tonic-gate 	/*
27207c478bd9Sstevel@tonic-gate 	 * Go to end of file name
27217c478bd9Sstevel@tonic-gate 	 */
27227c478bd9Sstevel@tonic-gate 	while (*p)
27237c478bd9Sstevel@tonic-gate 		p++;
27247c478bd9Sstevel@tonic-gate 	while (*--p  == '/')	/* delete trailing slashes */
27257c478bd9Sstevel@tonic-gate 		*p = '\0';
27267c478bd9Sstevel@tonic-gate 	/*
27277c478bd9Sstevel@tonic-gate 	 * go back to beginning of file
27287c478bd9Sstevel@tonic-gate 	 */
27297c478bd9Sstevel@tonic-gate 	p = file;
27307c478bd9Sstevel@tonic-gate 	/*
27317c478bd9Sstevel@tonic-gate 	 * Copy file name to funny setting p2 at
27327c478bd9Sstevel@tonic-gate 	 * basename of file.
27337c478bd9Sstevel@tonic-gate 	 */
27347c478bd9Sstevel@tonic-gate 	while (*p1++ = *p)
27357c478bd9Sstevel@tonic-gate 		if (*p++ == '/') p2 = p1;
27367c478bd9Sstevel@tonic-gate 	/*
27377c478bd9Sstevel@tonic-gate 	 * Set p1 to point to basename of tfname.
27387c478bd9Sstevel@tonic-gate 	 */
27397c478bd9Sstevel@tonic-gate 	p1 = strrchr(tfname, '/');
27407c478bd9Sstevel@tonic-gate 	if (strlen(tfname) > (size_t)6)
27417c478bd9Sstevel@tonic-gate 		p1 = &tfname[strlen(tfname)-6];
27427c478bd9Sstevel@tonic-gate 	p1++;
27437c478bd9Sstevel@tonic-gate 	*p2 = '\007'; /* add unprintable char for funny  a unique name */
27447c478bd9Sstevel@tonic-gate 	/*
27457c478bd9Sstevel@tonic-gate 	 * Copy tfname to file.
27467c478bd9Sstevel@tonic-gate 	 */
27477c478bd9Sstevel@tonic-gate 	while (*++p2 = *p1++);
27487c478bd9Sstevel@tonic-gate }
27497c478bd9Sstevel@tonic-gate 
27507c478bd9Sstevel@tonic-gate 
27517c478bd9Sstevel@tonic-gate static void
getime(void)27527c478bd9Sstevel@tonic-gate getime(void) /* get modified time of file and save */
27537c478bd9Sstevel@tonic-gate {
27547c478bd9Sstevel@tonic-gate 	if (stat(file, &Fl) < 0)
27557c478bd9Sstevel@tonic-gate 		savtime = 0;
27567c478bd9Sstevel@tonic-gate 	else
27577c478bd9Sstevel@tonic-gate 		savtime = Fl.st_mtime;
27587c478bd9Sstevel@tonic-gate }
27597c478bd9Sstevel@tonic-gate 
27607c478bd9Sstevel@tonic-gate 
27617c478bd9Sstevel@tonic-gate static void
chktime(void)27627c478bd9Sstevel@tonic-gate chktime(void) /* check saved mod time against current mod time */
27637c478bd9Sstevel@tonic-gate {
27647c478bd9Sstevel@tonic-gate 	if (savtime != 0 && Fl.st_mtime != 0) {
27657c478bd9Sstevel@tonic-gate 		if (savtime != Fl.st_mtime)
27667c478bd9Sstevel@tonic-gate 			(void) error(58);
27677c478bd9Sstevel@tonic-gate 	}
27687c478bd9Sstevel@tonic-gate }
27697c478bd9Sstevel@tonic-gate 
27707c478bd9Sstevel@tonic-gate 
27717c478bd9Sstevel@tonic-gate static void
newtime(void)27727c478bd9Sstevel@tonic-gate newtime(void) /* get new mod time and save */
27737c478bd9Sstevel@tonic-gate {
27747c478bd9Sstevel@tonic-gate 	stat(file, &Fl);
27757c478bd9Sstevel@tonic-gate 	savtime = Fl.st_mtime;
27767c478bd9Sstevel@tonic-gate }
27777c478bd9Sstevel@tonic-gate 
27787c478bd9Sstevel@tonic-gate 
27797c478bd9Sstevel@tonic-gate static void
red(char * op)27807c478bd9Sstevel@tonic-gate red(char *op)	/* restricted - check for '/' in name */
27817c478bd9Sstevel@tonic-gate 		/* and delete trailing '/' */
27827c478bd9Sstevel@tonic-gate {
27837c478bd9Sstevel@tonic-gate 	char *p;
27847c478bd9Sstevel@tonic-gate 
27857c478bd9Sstevel@tonic-gate 	p = op;
27867c478bd9Sstevel@tonic-gate 	while (*p)
27877c478bd9Sstevel@tonic-gate 		if (*p++ == '/'&& rflg) {
27887c478bd9Sstevel@tonic-gate 			*op = 0;
27897c478bd9Sstevel@tonic-gate 			(void) error(6);
27907c478bd9Sstevel@tonic-gate 		}
27917c478bd9Sstevel@tonic-gate 	/* delete trailing '/' */
27927c478bd9Sstevel@tonic-gate 	while (p > op) {
27937c478bd9Sstevel@tonic-gate 		if (*--p == '/')
27947c478bd9Sstevel@tonic-gate 			*p = '\0';
27957c478bd9Sstevel@tonic-gate 		else break;
27967c478bd9Sstevel@tonic-gate 	}
27977c478bd9Sstevel@tonic-gate }
27987c478bd9Sstevel@tonic-gate 
27997c478bd9Sstevel@tonic-gate 
28007c478bd9Sstevel@tonic-gate /*
28017c478bd9Sstevel@tonic-gate  * Searches thru beginning of file looking for a string of the form
28027c478bd9Sstevel@tonic-gate  *	<: values... :>
28037c478bd9Sstevel@tonic-gate  *
28047c478bd9Sstevel@tonic-gate  * where "values" are
28057c478bd9Sstevel@tonic-gate  *
28067c478bd9Sstevel@tonic-gate  *	\b      ignored
28077c478bd9Sstevel@tonic-gate  *	s<num>  sets the Flim to <num>
28087c478bd9Sstevel@tonic-gate  *	t???    sets tab stop stuff
28097c478bd9Sstevel@tonic-gate  *	d       ignored
28107c478bd9Sstevel@tonic-gate  *	m<num>  ignored
28117c478bd9Sstevel@tonic-gate  *	e       ignored
28127c478bd9Sstevel@tonic-gate  */
28137c478bd9Sstevel@tonic-gate 
28147c478bd9Sstevel@tonic-gate static int
fspec(char line[],struct Fspec * f,int up)28157c478bd9Sstevel@tonic-gate fspec(char line[], struct Fspec *f, int up)
28167c478bd9Sstevel@tonic-gate {
28177c478bd9Sstevel@tonic-gate 	struct termio arg;
28187c478bd9Sstevel@tonic-gate 	int havespec, n;
28197c478bd9Sstevel@tonic-gate 	int	len;
28207c478bd9Sstevel@tonic-gate 
28217c478bd9Sstevel@tonic-gate 	if (!up) clear(f);
28227c478bd9Sstevel@tonic-gate 
28237c478bd9Sstevel@tonic-gate 	havespec = fsprtn = 0;
28247c478bd9Sstevel@tonic-gate 	for (fsp = line; *fsp && *fsp != '\n'; fsp += len) {
28257c478bd9Sstevel@tonic-gate 		if ((len = mblen(fsp, MB_CUR_MAX)) <= 0)
28267c478bd9Sstevel@tonic-gate 			len = 1;
28277c478bd9Sstevel@tonic-gate 		switch (*fsp) {
28287c478bd9Sstevel@tonic-gate 
28297c478bd9Sstevel@tonic-gate 			case '<':	if (havespec)
28307c478bd9Sstevel@tonic-gate 						return (-1);
28317c478bd9Sstevel@tonic-gate 					if (*(fsp+1) == ':') {
28327c478bd9Sstevel@tonic-gate 						havespec = 1;
28337c478bd9Sstevel@tonic-gate 						clear(f);
28347c478bd9Sstevel@tonic-gate 						if (!ioctl(1, TCGETA, &arg) &&
28357c478bd9Sstevel@tonic-gate 						((arg.c_oflag&TAB3) == TAB3))
28367c478bd9Sstevel@tonic-gate 						    f->Ffill = 1;
28377c478bd9Sstevel@tonic-gate 						fsp++;
28387c478bd9Sstevel@tonic-gate 						continue;
28397c478bd9Sstevel@tonic-gate 					}
28407c478bd9Sstevel@tonic-gate 
28417c478bd9Sstevel@tonic-gate 			case ' ':	continue;
28427c478bd9Sstevel@tonic-gate 
28437c478bd9Sstevel@tonic-gate 			case 's':	if (havespec && (n = numb()) >= 0)
28447c478bd9Sstevel@tonic-gate 						f->Flim = n;
28457c478bd9Sstevel@tonic-gate 					continue;
28467c478bd9Sstevel@tonic-gate 
28477c478bd9Sstevel@tonic-gate 			case 't':	if (havespec) targ(f);
28487c478bd9Sstevel@tonic-gate 					continue;
28497c478bd9Sstevel@tonic-gate 
28507c478bd9Sstevel@tonic-gate 			case 'd':	continue;
28517c478bd9Sstevel@tonic-gate 
28527c478bd9Sstevel@tonic-gate 			case 'm':	if (havespec)  n = numb();
28537c478bd9Sstevel@tonic-gate 					continue;
28547c478bd9Sstevel@tonic-gate 
28557c478bd9Sstevel@tonic-gate 			case 'e':	continue;
28567c478bd9Sstevel@tonic-gate 			case ':':	if (!havespec) continue;
28577c478bd9Sstevel@tonic-gate 					if (*(fsp+1) != '>') fsprtn = -1;
28587c478bd9Sstevel@tonic-gate 					return (fsprtn);
28597c478bd9Sstevel@tonic-gate 
28607c478bd9Sstevel@tonic-gate 			default:	if (!havespec) continue;
28617c478bd9Sstevel@tonic-gate 					return (-1);
28627c478bd9Sstevel@tonic-gate 		}
28637c478bd9Sstevel@tonic-gate 	}
28647c478bd9Sstevel@tonic-gate 	return (1);
28657c478bd9Sstevel@tonic-gate }
28667c478bd9Sstevel@tonic-gate 
28677c478bd9Sstevel@tonic-gate 
28687c478bd9Sstevel@tonic-gate static int
numb(void)28697c478bd9Sstevel@tonic-gate numb(void)
28707c478bd9Sstevel@tonic-gate {
28717c478bd9Sstevel@tonic-gate 	int n;
28727c478bd9Sstevel@tonic-gate 
28737c478bd9Sstevel@tonic-gate 	n = 0;
28747c478bd9Sstevel@tonic-gate 	while (*++fsp >= '0' && *fsp <= '9')
28757c478bd9Sstevel@tonic-gate 		n = 10*n + *fsp-'0';
28767c478bd9Sstevel@tonic-gate 	fsp--;
28777c478bd9Sstevel@tonic-gate 	return (n);
28787c478bd9Sstevel@tonic-gate }
28797c478bd9Sstevel@tonic-gate 
28807c478bd9Sstevel@tonic-gate 
28817c478bd9Sstevel@tonic-gate static void
targ(struct Fspec * f)28827c478bd9Sstevel@tonic-gate targ(struct Fspec *f)
28837c478bd9Sstevel@tonic-gate {
28847c478bd9Sstevel@tonic-gate 
28857c478bd9Sstevel@tonic-gate 	if (*++fsp == '-') {
28867c478bd9Sstevel@tonic-gate 		if (*(fsp + 1) >= '0' && *(fsp+1) <= '9') tincr(numb(), f);
28877c478bd9Sstevel@tonic-gate 		else tstd(f);
28887c478bd9Sstevel@tonic-gate 		return;
28897c478bd9Sstevel@tonic-gate 	}
28907c478bd9Sstevel@tonic-gate 	if (*fsp >= '0' && *fsp <= '9') {
28917c478bd9Sstevel@tonic-gate 		tlist(f);
28927c478bd9Sstevel@tonic-gate 		return;
28937c478bd9Sstevel@tonic-gate 	}
28947c478bd9Sstevel@tonic-gate 	fsprtn = -1;
28957c478bd9Sstevel@tonic-gate 	fsp--;
28967c478bd9Sstevel@tonic-gate }
28977c478bd9Sstevel@tonic-gate 
28987c478bd9Sstevel@tonic-gate 
28997c478bd9Sstevel@tonic-gate static void
tincr(int n,struct Fspec * f)29007c478bd9Sstevel@tonic-gate tincr(int n, struct Fspec *f)
29017c478bd9Sstevel@tonic-gate {
29027c478bd9Sstevel@tonic-gate 	int l, i;
29037c478bd9Sstevel@tonic-gate 
29047c478bd9Sstevel@tonic-gate 	l = 1;
29057c478bd9Sstevel@tonic-gate 	for (i = 0; i < 20; i++)
29067c478bd9Sstevel@tonic-gate 		f->Ftabs[i] = l += n;
29077c478bd9Sstevel@tonic-gate 	f->Ftabs[i] = 0;
29087c478bd9Sstevel@tonic-gate }
29097c478bd9Sstevel@tonic-gate 
29107c478bd9Sstevel@tonic-gate 
29117c478bd9Sstevel@tonic-gate static void
tstd(struct Fspec * f)29127c478bd9Sstevel@tonic-gate tstd(struct Fspec *f)
29137c478bd9Sstevel@tonic-gate {
29147c478bd9Sstevel@tonic-gate 	char std[3];
29157c478bd9Sstevel@tonic-gate 
29167c478bd9Sstevel@tonic-gate 	std[0] = *++fsp;
29177c478bd9Sstevel@tonic-gate 	if (*(fsp+1) >= '0' && *(fsp+1) <= '9')  {
29187c478bd9Sstevel@tonic-gate 						std[1] = *++fsp;
29197c478bd9Sstevel@tonic-gate 						std[2] = '\0';
29207c478bd9Sstevel@tonic-gate 	} else std[1] = '\0';
29217c478bd9Sstevel@tonic-gate 	fsprtn = stdtab(std, f->Ftabs);
29227c478bd9Sstevel@tonic-gate }
29237c478bd9Sstevel@tonic-gate 
29247c478bd9Sstevel@tonic-gate 
29257c478bd9Sstevel@tonic-gate static void
tlist(struct Fspec * f)29267c478bd9Sstevel@tonic-gate tlist(struct Fspec *f)
29277c478bd9Sstevel@tonic-gate {
29287c478bd9Sstevel@tonic-gate 	int n, last, i;
29297c478bd9Sstevel@tonic-gate 
29307c478bd9Sstevel@tonic-gate 	fsp--;
29317c478bd9Sstevel@tonic-gate 	last = i = 0;
29327c478bd9Sstevel@tonic-gate 
29337c478bd9Sstevel@tonic-gate 	do {
29347c478bd9Sstevel@tonic-gate 		if ((n = numb()) <= last || i >= 20) {
29357c478bd9Sstevel@tonic-gate 			fsprtn = -1;
29367c478bd9Sstevel@tonic-gate 			return;
29377c478bd9Sstevel@tonic-gate 		}
29387c478bd9Sstevel@tonic-gate 		f->Ftabs[i++] = last = n;
29397c478bd9Sstevel@tonic-gate 	} while (*++fsp == ',');
29407c478bd9Sstevel@tonic-gate 
29417c478bd9Sstevel@tonic-gate 	f->Ftabs[i] = 0;
29427c478bd9Sstevel@tonic-gate 	fsp--;
29437c478bd9Sstevel@tonic-gate }
29447c478bd9Sstevel@tonic-gate 
29457c478bd9Sstevel@tonic-gate 
29467c478bd9Sstevel@tonic-gate static int
expnd(char line[],char buf[],int * sz,struct Fspec * f)29477c478bd9Sstevel@tonic-gate expnd(char line[], char buf[], int *sz, struct Fspec *f)
29487c478bd9Sstevel@tonic-gate {
29497c478bd9Sstevel@tonic-gate 	char *l, *t;
29507c478bd9Sstevel@tonic-gate 	int b;
29517c478bd9Sstevel@tonic-gate 
29527c478bd9Sstevel@tonic-gate 	l = line - 1;
29537c478bd9Sstevel@tonic-gate 	b = 1;
29547c478bd9Sstevel@tonic-gate 	t = f->Ftabs;
29557c478bd9Sstevel@tonic-gate 	fsprtn = 0;
29567c478bd9Sstevel@tonic-gate 
29577c478bd9Sstevel@tonic-gate 	while (*++l && *l != '\n' && b < 511) {
29587c478bd9Sstevel@tonic-gate 		if (*l == '\t') {
29597c478bd9Sstevel@tonic-gate 			while (*t && b >= *t) t++;
29607c478bd9Sstevel@tonic-gate 			if (*t == 0) fsprtn = -2;
29617c478bd9Sstevel@tonic-gate 			do buf[b-1] = ' '; while (++b < *t);
29627c478bd9Sstevel@tonic-gate 		} else buf[b++ - 1] = *l;
29637c478bd9Sstevel@tonic-gate 	}
29647c478bd9Sstevel@tonic-gate 
29657c478bd9Sstevel@tonic-gate 	buf[b] = '\0';
29667c478bd9Sstevel@tonic-gate 	*sz = b;
29677c478bd9Sstevel@tonic-gate 	if (*l != '\0' && *l != '\n') {
29687c478bd9Sstevel@tonic-gate 		buf[b-1] = '\n';
29697c478bd9Sstevel@tonic-gate 		return (-1);
29707c478bd9Sstevel@tonic-gate 	}
29717c478bd9Sstevel@tonic-gate 	buf[b-1] = *l;
29727c478bd9Sstevel@tonic-gate 	if (f->Flim && (b-1 > (int)f->Flim))
29737c478bd9Sstevel@tonic-gate 		return (-1);
29747c478bd9Sstevel@tonic-gate 	return (fsprtn);
29757c478bd9Sstevel@tonic-gate }
29767c478bd9Sstevel@tonic-gate 
29777c478bd9Sstevel@tonic-gate 
29787c478bd9Sstevel@tonic-gate static void
clear(struct Fspec * f)29797c478bd9Sstevel@tonic-gate clear(struct Fspec *f)
29807c478bd9Sstevel@tonic-gate {
29817c478bd9Sstevel@tonic-gate 	f->Ftabs[0] = f->Fdel = f->Fmov = f->Ffill = 0;
29827c478bd9Sstevel@tonic-gate 	f->Flim = 0;
29837c478bd9Sstevel@tonic-gate }
29847c478bd9Sstevel@tonic-gate 
29857c478bd9Sstevel@tonic-gate 
29867c478bd9Sstevel@tonic-gate static int
lenchk(char line[],struct Fspec * f)29877c478bd9Sstevel@tonic-gate lenchk(char line[], struct Fspec *f)
29887c478bd9Sstevel@tonic-gate {
29897c478bd9Sstevel@tonic-gate 	char *l, *t;
29907c478bd9Sstevel@tonic-gate 	int b;
29917c478bd9Sstevel@tonic-gate 
29927c478bd9Sstevel@tonic-gate 	l = line - 1;
29937c478bd9Sstevel@tonic-gate 	b = 1;
29947c478bd9Sstevel@tonic-gate 	t = f->Ftabs;
29957c478bd9Sstevel@tonic-gate 
29967c478bd9Sstevel@tonic-gate 	while (*++l && *l != '\n' && b < 511) {
29977c478bd9Sstevel@tonic-gate 		if (*l == '\t') {
29987c478bd9Sstevel@tonic-gate 			while (*t && b >= *t) t++;
29997c478bd9Sstevel@tonic-gate 			while (++b < *t);
30007c478bd9Sstevel@tonic-gate 		} else b++;
30017c478bd9Sstevel@tonic-gate 	}
30027c478bd9Sstevel@tonic-gate 
30037c478bd9Sstevel@tonic-gate 	if ((*l != '\0' && *l != '\n') || (f->Flim && (b-1 > (int)f->Flim)))
30047c478bd9Sstevel@tonic-gate 		return (-1);
30057c478bd9Sstevel@tonic-gate 	return (0);
30067c478bd9Sstevel@tonic-gate }
30077c478bd9Sstevel@tonic-gate #define	NTABS 21
30087c478bd9Sstevel@tonic-gate 
30097c478bd9Sstevel@tonic-gate 
30107c478bd9Sstevel@tonic-gate /*
30117c478bd9Sstevel@tonic-gate  *	stdtabs: standard tabs table
30127c478bd9Sstevel@tonic-gate  *	format: option code letter(s), null, tabs, null
30137c478bd9Sstevel@tonic-gate  */
30147c478bd9Sstevel@tonic-gate 
30157c478bd9Sstevel@tonic-gate static char stdtabs[] = {
30167c478bd9Sstevel@tonic-gate 'a', 0, 1, 10, 16, 36, 72, 0,			/* IBM 370 Assembler */
30177c478bd9Sstevel@tonic-gate 'a', '2', 0, 1, 10, 16, 40, 72, 0,		/* IBM Assembler alternative */
30187c478bd9Sstevel@tonic-gate 'c', 0, 1, 8, 12, 16, 20, 55, 0,		/* COBOL, normal */
30197c478bd9Sstevel@tonic-gate 'c', '2', 0, 1, 6, 10, 14, 49, 0,		/* COBOL, crunched */
30207c478bd9Sstevel@tonic-gate 'c', '3', 0, 1, 6, 10, 14, 18, 22, 26, 30, 34, 38, 42, 46, 50,
30217c478bd9Sstevel@tonic-gate 	54, 58, 62, 67, 0,
30227c478bd9Sstevel@tonic-gate 'f', 0, 1, 7, 11, 15, 19, 23, 0,		/* FORTRAN */
30237c478bd9Sstevel@tonic-gate 'p', 0, 1, 5, 9, 13, 17, 21, 25, 29, 33, 37, 41, 45, 49, 53, 57, 61, 0,
30247c478bd9Sstevel@tonic-gate 						/* PL/I */
30257c478bd9Sstevel@tonic-gate 's', 0, 1, 10, 55, 0,    			/* SNOBOL */
30267c478bd9Sstevel@tonic-gate 'u', 0, 1, 12, 20, 44, 0, 			/* UNIVAC ASM */
30277c478bd9Sstevel@tonic-gate 0 };
30287c478bd9Sstevel@tonic-gate 
30297c478bd9Sstevel@tonic-gate 
30307c478bd9Sstevel@tonic-gate /*
30317c478bd9Sstevel@tonic-gate  *	stdtab: return tab list for any "canned" tab option.
30327c478bd9Sstevel@tonic-gate  *		entry: option points to null-terminated option string
30337c478bd9Sstevel@tonic-gate  *		tabvect points to vector to be filled in
30347c478bd9Sstevel@tonic-gate  *	exit: return (0) if legal, tabvect filled, ending with zero
30357c478bd9Sstevel@tonic-gate  *		return (-1) if unknown option
30367c478bd9Sstevel@tonic-gate  */
30377c478bd9Sstevel@tonic-gate 
30387c478bd9Sstevel@tonic-gate 
30397c478bd9Sstevel@tonic-gate static int
stdtab(char option[],char tabvect[NTABS])30407c478bd9Sstevel@tonic-gate stdtab(char option[], char tabvect[NTABS])
30417c478bd9Sstevel@tonic-gate {
30427c478bd9Sstevel@tonic-gate 	char *scan;
30437c478bd9Sstevel@tonic-gate 	tabvect[0] = 0;
30447c478bd9Sstevel@tonic-gate 	scan = stdtabs;
30457c478bd9Sstevel@tonic-gate 	while (*scan) {
30467c478bd9Sstevel@tonic-gate 		if (strequal(&scan, option)) {
30477c478bd9Sstevel@tonic-gate 			strcopy(scan, tabvect);
30487c478bd9Sstevel@tonic-gate 			break;
30497c478bd9Sstevel@tonic-gate 		} else
30507c478bd9Sstevel@tonic-gate 			while (*scan++);    /* skip over tab specs */
30517c478bd9Sstevel@tonic-gate 	}
30527c478bd9Sstevel@tonic-gate 
30537c478bd9Sstevel@tonic-gate /*	later: look up code in /etc/something */
30547c478bd9Sstevel@tonic-gate 	return (tabvect[0] ? 0 : -1);
30557c478bd9Sstevel@tonic-gate }
30567c478bd9Sstevel@tonic-gate 
30577c478bd9Sstevel@tonic-gate 
30587c478bd9Sstevel@tonic-gate /*
30597c478bd9Sstevel@tonic-gate  *	strequal: checks strings for equality
30607c478bd9Sstevel@tonic-gate  *		entry: scan1 points to scan pointer, str points to string
30617c478bd9Sstevel@tonic-gate  *	exit: return (1) if equal, return (0) if not
30627c478bd9Sstevel@tonic-gate  *		*scan1 is advanced to next nonzero byte after null
30637c478bd9Sstevel@tonic-gate  */
30647c478bd9Sstevel@tonic-gate 
30657c478bd9Sstevel@tonic-gate 
30667c478bd9Sstevel@tonic-gate static int
strequal(char ** scan1,char * str)30677c478bd9Sstevel@tonic-gate strequal(char **scan1, char *str)
30687c478bd9Sstevel@tonic-gate {
30697c478bd9Sstevel@tonic-gate 	char c, *scan;
30707c478bd9Sstevel@tonic-gate 	scan = *scan1;
30717c478bd9Sstevel@tonic-gate 	while ((c = *scan++) == *str && c) str++;
30727c478bd9Sstevel@tonic-gate 	*scan1 = scan;
30737c478bd9Sstevel@tonic-gate 	if (c == 0 && *str == 0)
30747c478bd9Sstevel@tonic-gate 		return (1);
30757c478bd9Sstevel@tonic-gate 	if (c)
30767c478bd9Sstevel@tonic-gate 		while (*scan++);
30777c478bd9Sstevel@tonic-gate 			*scan1 = scan;
30787c478bd9Sstevel@tonic-gate 	return (0);
30797c478bd9Sstevel@tonic-gate }
30807c478bd9Sstevel@tonic-gate 
30817c478bd9Sstevel@tonic-gate 
30827c478bd9Sstevel@tonic-gate /*	strcopy: copy source to destination */
30837c478bd9Sstevel@tonic-gate 
30847c478bd9Sstevel@tonic-gate 
30857c478bd9Sstevel@tonic-gate static void
strcopy(char * source,char * dest)30867c478bd9Sstevel@tonic-gate strcopy(char *source, char *dest)
30877c478bd9Sstevel@tonic-gate {
30887c478bd9Sstevel@tonic-gate 	while (*dest++ = *source++);
30897c478bd9Sstevel@tonic-gate }
30907c478bd9Sstevel@tonic-gate 
30917c478bd9Sstevel@tonic-gate 
30927c478bd9Sstevel@tonic-gate /* This is called before a buffer modifying command so that the */
30937c478bd9Sstevel@tonic-gate /* current array of line ptrs is saved in sav and dot and dol are saved */
30947c478bd9Sstevel@tonic-gate 
30957c478bd9Sstevel@tonic-gate 
30967c478bd9Sstevel@tonic-gate static void
save(void)30977c478bd9Sstevel@tonic-gate save(void) {
30987c478bd9Sstevel@tonic-gate 	LINE i;
30997c478bd9Sstevel@tonic-gate 	int	j;
31007c478bd9Sstevel@tonic-gate 
31017c478bd9Sstevel@tonic-gate 	savdot = dot;
31027c478bd9Sstevel@tonic-gate 	savdol = dol;
31037c478bd9Sstevel@tonic-gate 	for (j = 0; j <= 25; j++)
31047c478bd9Sstevel@tonic-gate 		savnames[j] = names[j];
31057c478bd9Sstevel@tonic-gate 
31067c478bd9Sstevel@tonic-gate 	for (i = zero + 1; i <= dol; i++)
31077c478bd9Sstevel@tonic-gate 		i->sav = i->cur;
31087c478bd9Sstevel@tonic-gate 	initflg = 0;
31097c478bd9Sstevel@tonic-gate }
31107c478bd9Sstevel@tonic-gate 
31117c478bd9Sstevel@tonic-gate 
31127c478bd9Sstevel@tonic-gate /* The undo command calls this to restore the previous ptr array sav */
31137c478bd9Sstevel@tonic-gate /* and swap with cur - dot and dol are swapped also. This allows user to */
31147c478bd9Sstevel@tonic-gate /* undo an undo */
31157c478bd9Sstevel@tonic-gate 
31167c478bd9Sstevel@tonic-gate 
31177c478bd9Sstevel@tonic-gate static void
undo(void)31187c478bd9Sstevel@tonic-gate undo(void) {
31197c478bd9Sstevel@tonic-gate 	int j;
31207c478bd9Sstevel@tonic-gate 	long tmp;
31217c478bd9Sstevel@tonic-gate 	LINE i, tmpdot, tmpdol;
31227c478bd9Sstevel@tonic-gate 
31237c478bd9Sstevel@tonic-gate 	tmpdot = dot; dot = savdot; savdot = tmpdot;
31247c478bd9Sstevel@tonic-gate 	tmpdol = dol; dol = savdol; savdol = tmpdol;
31257c478bd9Sstevel@tonic-gate 	/* swap arrays using the greater of dol or savdol as upper limit */
31267c478bd9Sstevel@tonic-gate 	for (i = zero + 1; i <= ((dol > savdol) ? dol : savdol); i++) {
31277c478bd9Sstevel@tonic-gate 		tmp = i->cur;
31287c478bd9Sstevel@tonic-gate 		i->cur = i->sav;
31297c478bd9Sstevel@tonic-gate 		i->sav = tmp;
31307c478bd9Sstevel@tonic-gate 	}
31317c478bd9Sstevel@tonic-gate 		/*
31327c478bd9Sstevel@tonic-gate 		 * If the current text lines are swapped with the
31337c478bd9Sstevel@tonic-gate 		 * text lines in the save buffer, then swap the current
31347c478bd9Sstevel@tonic-gate 		 * marks with those in the save area.
31357c478bd9Sstevel@tonic-gate 		 */
31367c478bd9Sstevel@tonic-gate 
31377c478bd9Sstevel@tonic-gate 		for (j = 0; j <= 25; j++) {
31387c478bd9Sstevel@tonic-gate 			tmp = names[j];
31397c478bd9Sstevel@tonic-gate 			names[j] = savnames[j];
31407c478bd9Sstevel@tonic-gate 			savnames[j] = tmp;
31417c478bd9Sstevel@tonic-gate 		}
31427c478bd9Sstevel@tonic-gate }
31437c478bd9Sstevel@tonic-gate 
31447c478bd9Sstevel@tonic-gate static wchar_t
get_wchr()31457c478bd9Sstevel@tonic-gate get_wchr()
31467c478bd9Sstevel@tonic-gate {
31477c478bd9Sstevel@tonic-gate 	wchar_t	wc;
31487c478bd9Sstevel@tonic-gate 	char	multi[MB_LEN_MAX];
31497c478bd9Sstevel@tonic-gate 
31507c478bd9Sstevel@tonic-gate 	if (_mbftowc(multi, &wc, getchr, &peekc) <= 0)
31517c478bd9Sstevel@tonic-gate 		wc = getchr();
31527c478bd9Sstevel@tonic-gate 	return (wc);
31537c478bd9Sstevel@tonic-gate }
3154