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