xref: /onnv-gate/usr/src/cmd/pg/pg.c (revision 13093:48f2dbca79a2)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
54321Scasper  * Common Development and Distribution License (the "License").
64321Scasper  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate 
220Sstevel@tonic-gate /*
23*13093SRoger.Faulkner@Oracle.COM  * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
26213Smuffin /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
27213Smuffin /*	  All Rights Reserved  	*/
28213Smuffin 
290Sstevel@tonic-gate #include <signal.h>
300Sstevel@tonic-gate #include <setjmp.h>
310Sstevel@tonic-gate #include <sys/types.h>
320Sstevel@tonic-gate #include <sys/dirent.h>
330Sstevel@tonic-gate #include <sys/stat.h>
340Sstevel@tonic-gate #include <fcntl.h>
350Sstevel@tonic-gate #include <ctype.h>
360Sstevel@tonic-gate #include <stdio.h>
370Sstevel@tonic-gate #include <wchar.h>
380Sstevel@tonic-gate #include <curses.h>
390Sstevel@tonic-gate #include <term.h>
400Sstevel@tonic-gate #include <errno.h>
410Sstevel@tonic-gate #include <stdlib.h>
420Sstevel@tonic-gate #include <regexpr.h>
430Sstevel@tonic-gate #include <limits.h>
440Sstevel@tonic-gate #include <locale.h>
450Sstevel@tonic-gate #include <wctype.h> /* iswprint() */
460Sstevel@tonic-gate #include <string.h>
470Sstevel@tonic-gate #include <unistd.h>
480Sstevel@tonic-gate #include <wait.h>
490Sstevel@tonic-gate #include <libw.h>
500Sstevel@tonic-gate #include <regexpr.h>
510Sstevel@tonic-gate 
520Sstevel@tonic-gate 
530Sstevel@tonic-gate /*
540Sstevel@tonic-gate  *	pg -- paginator for crt terminals
550Sstevel@tonic-gate  *
560Sstevel@tonic-gate  *	Includes the ability to display pages that have
570Sstevel@tonic-gate  *	already passed by. Also gives the user the ability
580Sstevel@tonic-gate  *	to search forward and backwards for regular expressions.
590Sstevel@tonic-gate  *	This works for piped input by copying to a temporary file,
600Sstevel@tonic-gate  *	and resolving backreferences from there.
610Sstevel@tonic-gate  *
620Sstevel@tonic-gate  *	Note:	The reason that there are so many commands to do
630Sstevel@tonic-gate  *		the same types of things is to try to accommodate
640Sstevel@tonic-gate  *		users of other paginators.
650Sstevel@tonic-gate  */
660Sstevel@tonic-gate 
670Sstevel@tonic-gate #define	LINSIZ	1024
680Sstevel@tonic-gate #define	QUIT	'\034'
690Sstevel@tonic-gate #define	BOF	(EOF - 1)	/* Begining of File */
700Sstevel@tonic-gate #define	STOP    (EOF - 2)
710Sstevel@tonic-gate #define	PROMPTSIZE	256
720Sstevel@tonic-gate 
730Sstevel@tonic-gate /*
740Sstevel@tonic-gate  * Function definitions
750Sstevel@tonic-gate  */
760Sstevel@tonic-gate static	void	lineset(int);
770Sstevel@tonic-gate static	char	*setprompt();
780Sstevel@tonic-gate static	int	set_state(int *, wchar_t, char *);
790Sstevel@tonic-gate static	void	help();
800Sstevel@tonic-gate static	void	copy_file(FILE *, FILE *);
810Sstevel@tonic-gate static	void	re_error(int);
820Sstevel@tonic-gate static	void	save_input(FILE *);
830Sstevel@tonic-gate static	void	save_pipe();
840Sstevel@tonic-gate static	void	newdol(FILE *);
850Sstevel@tonic-gate static	void	erase_line(int);
860Sstevel@tonic-gate static	void	kill_line();
870Sstevel@tonic-gate static	void	doclear();
880Sstevel@tonic-gate static	void	sopr(char *, int);
890Sstevel@tonic-gate static	void	prompt(char *);
900Sstevel@tonic-gate static	void	error(char *);
910Sstevel@tonic-gate static	void	terminit();
920Sstevel@tonic-gate static	void	compact();
93*13093SRoger.Faulkner@Oracle.COM static	off_t	getaline(FILE *);
940Sstevel@tonic-gate static	int	mrdchar();
950Sstevel@tonic-gate static	off_t	find(int, off_t);
960Sstevel@tonic-gate static	int	search(char *, off_t);
970Sstevel@tonic-gate static	FILE	*checkf(char *);
980Sstevel@tonic-gate static	int	skipf(int);
990Sstevel@tonic-gate static	int	readch();
1000Sstevel@tonic-gate static	int	ttyin();
1010Sstevel@tonic-gate static	int	number();
1020Sstevel@tonic-gate static	int	command(char *);
1030Sstevel@tonic-gate static	int	screen(char *);
1040Sstevel@tonic-gate static	int	fgetputc();
1050Sstevel@tonic-gate static 	char	*pg_strchr();
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate struct line {			/* how line addresses are stored */
1090Sstevel@tonic-gate 	off_t	l_addr;		/* file offset */
1100Sstevel@tonic-gate 	off_t	l_no;		/* line number in file */
1110Sstevel@tonic-gate };
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate typedef	struct line	LINE;
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate static	LINE	*zero = NULL,	/* first line */
1160Sstevel@tonic-gate 		*dot,		/* current line */
1170Sstevel@tonic-gate 		*dol,		/* last line */
1180Sstevel@tonic-gate 		*contig;	/* where contiguous (non-aged) lines start */
1190Sstevel@tonic-gate static	long	nlall;		/* room for how many LINEs in memory */
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate static	FILE	*in_file,	/* current input stream */
1220Sstevel@tonic-gate 		*tmp_fin,	/* pipe temporary file in */
1230Sstevel@tonic-gate 		*tmp_fou;	/* pipe temporary file out */
1240Sstevel@tonic-gate static	char	tmp_name[] = "/tmp/pgXXXXXX";
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate static	short	sign;		/* sign of command input */
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate static	int	fnum,		/* which file argument we're in */
1290Sstevel@tonic-gate 		pipe_in,	/* set when stdin is a pipe */
1300Sstevel@tonic-gate 		out_is_tty;	/* set if stdout is a tty */
1314321Scasper static	pid_t	my_pgid;
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate static	void	on_brk(),
1340Sstevel@tonic-gate 		end_it();
1350Sstevel@tonic-gate static	short	brk_hit;	/* interrupt handling is pending flag */
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate static	int	window = 0;	/* window size in lines */
1380Sstevel@tonic-gate static	short	eof_pause = 1;	/* pause w/ prompt at end of files */
1390Sstevel@tonic-gate static	short	rmode = 0;	/* deny shell escape in restricted mode */
1400Sstevel@tonic-gate static	short	soflag = 0;	/* output all messages in standout mode */
1410Sstevel@tonic-gate static	short	promptlen;	/* length of the current prompt */
1420Sstevel@tonic-gate static	short	firstf = 1;	/* set before first file has been processed */
1430Sstevel@tonic-gate static	short	inwait,		/* set while waiting for user input */
1440Sstevel@tonic-gate 		errors;		/* set if error message has been printed. */
1450Sstevel@tonic-gate 				/* if so, need to erase it and prompt */
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate static	char	**fnames;
1480Sstevel@tonic-gate static	short	status = 0;	/* set > 0 if error detected */
1490Sstevel@tonic-gate static	short	fflag = 0;	/* set if the f option is used */
1500Sstevel@tonic-gate static	short	nflag = 0;	/* set for "no newline" input option */
1510Sstevel@tonic-gate static	short	clropt = 0;	/* set if the clear option is used */
1520Sstevel@tonic-gate static	int	initopt = 0;	/* set if the line option is used */
1530Sstevel@tonic-gate static	int	srchopt = 0;	/* set if the search option is used */
1540Sstevel@tonic-gate static	int	initline;
1550Sstevel@tonic-gate static	char	initbuf[BUFSIZ];
1560Sstevel@tonic-gate static	wchar_t	leave_search = L't';
1570Sstevel@tonic-gate 				/* where on the page to leave a found string */
1580Sstevel@tonic-gate static	short	nfiles;
1590Sstevel@tonic-gate static	char	*shell;
1600Sstevel@tonic-gate static	char	*promptstr = ":";
161*13093SRoger.Faulkner@Oracle.COM static  off_t	nchars;			/* return from getaline in find() */
1620Sstevel@tonic-gate static	jmp_buf	restore;
1630Sstevel@tonic-gate static	char	Line[LINSIZ+2];
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate static	int	catch_susp;
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate static	void	onsusp();
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate struct screen_stat {
1700Sstevel@tonic-gate 	off_t	first_line;
1710Sstevel@tonic-gate 	off_t	last_line;
1720Sstevel@tonic-gate 	short	is_eof;
1730Sstevel@tonic-gate 	};
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate static	struct screen_stat old_ss = { 0, 0, 0 };
1760Sstevel@tonic-gate static	struct screen_stat new_ss;
1770Sstevel@tonic-gate static	struct termio otty;	/* to save old terminal settings */
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate static	short	termflg = 0;	/* set once terminal is initialized */
1800Sstevel@tonic-gate static	short	eoflag;		/* set whenever at end of current file */
1810Sstevel@tonic-gate static	short	doliseof;	/* set when last line of file is known */
1820Sstevel@tonic-gate static	off_t	eofl_no;	/* what the last line of the file is */
1830Sstevel@tonic-gate static	void	usage(void);
1840Sstevel@tonic-gate static FILE	*pg_stdin;
1850Sstevel@tonic-gate 
186213Smuffin int
main(int argc,char ** argv)187213Smuffin main(int argc, char **argv)
1880Sstevel@tonic-gate {
1890Sstevel@tonic-gate 	char	*s;
1900Sstevel@tonic-gate 	char	*p;
1910Sstevel@tonic-gate 	int		prnames = 0;
1920Sstevel@tonic-gate 	int		opt;
1930Sstevel@tonic-gate 	int		i;
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1960Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
1970Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
1980Sstevel@tonic-gate #endif
1990Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 	/* check for non-standard "-#" option */
2020Sstevel@tonic-gate 	for (i = 1; i < argc; i++) {
2030Sstevel@tonic-gate 		if (strcmp(argv[i], "--") == 0)
2040Sstevel@tonic-gate 			break;
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 		if ((argv[i][0] == '-') && isdigit(argv[i][1])) {
2070Sstevel@tonic-gate 			if (strlen(&argv[i][1]) !=
2080Sstevel@tonic-gate 			    strspn(&argv[i][1], "0123456789")) {
2090Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
2100Sstevel@tonic-gate 				    "pg: Badly formed number\n"));
2110Sstevel@tonic-gate 				usage();
2120Sstevel@tonic-gate 			}
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate 			window = (int)strtol(&argv[i][1], (char **)NULL, 10);
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 			while (i < argc) {
2170Sstevel@tonic-gate 				argv[i] = argv[i + 1];
2180Sstevel@tonic-gate 				i++;
2190Sstevel@tonic-gate 			}
2200Sstevel@tonic-gate 			i--;
2210Sstevel@tonic-gate 			argc--;
2220Sstevel@tonic-gate 		}
2230Sstevel@tonic-gate 	}
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 	/* check for non-standard + option */
2260Sstevel@tonic-gate 	for (i = 1; i < argc; i++) {
2270Sstevel@tonic-gate 		if (strcmp(argv[i], "--") == 0)
2280Sstevel@tonic-gate 		break;
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 		if (argv[i][0] == '+') {
2310Sstevel@tonic-gate 			if (argv[i][1] == '/') {
2320Sstevel@tonic-gate 				srchopt++;
2330Sstevel@tonic-gate 				initopt = 0;
2340Sstevel@tonic-gate 				for (s = &argv[i][2], p = initbuf; *s != '\0'; )
2350Sstevel@tonic-gate 					if (p < initbuf + sizeof (initbuf))
2360Sstevel@tonic-gate 						*p++ = *s++;
2370Sstevel@tonic-gate 					else {
2380Sstevel@tonic-gate 						(void) fprintf(stderr, gettext(
2390Sstevel@tonic-gate 						    "pg: pattern too long\n"));
2400Sstevel@tonic-gate 						return (1);
2410Sstevel@tonic-gate 					}
2420Sstevel@tonic-gate 				*p = '\0';
2430Sstevel@tonic-gate 			} else {
2440Sstevel@tonic-gate 				initopt++;
2450Sstevel@tonic-gate 				srchopt = 0;
2460Sstevel@tonic-gate 				s = &argv[i][2];
2470Sstevel@tonic-gate 				for (; isdigit(*s); s++)
2480Sstevel@tonic-gate 					initline = initline*10 + *s -'0';
2490Sstevel@tonic-gate 				if (*s != '\0')
2500Sstevel@tonic-gate 					usage();
2510Sstevel@tonic-gate 			}
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 			while (i < argc) {
2540Sstevel@tonic-gate 				argv[i] = argv[i + 1];
2550Sstevel@tonic-gate 				i++;
2560Sstevel@tonic-gate 			}
2570Sstevel@tonic-gate 			i--;
2580Sstevel@tonic-gate 			argc--;
2590Sstevel@tonic-gate 		}
2600Sstevel@tonic-gate 	}
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate 	while ((opt = getopt(argc, argv, "cefnrsp:")) != EOF) {
2630Sstevel@tonic-gate 		switch (opt) {
2640Sstevel@tonic-gate 		case 'c':
2650Sstevel@tonic-gate 			clropt = 1;
2660Sstevel@tonic-gate 			break;
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate 		case 'e':
2690Sstevel@tonic-gate 			eof_pause = 0;
2700Sstevel@tonic-gate 			break;
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 		case 'f':
2730Sstevel@tonic-gate 			fflag = 1;
2740Sstevel@tonic-gate 			break;
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate 		case 'n':
2770Sstevel@tonic-gate 			nflag = 1;
2780Sstevel@tonic-gate 			break;
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate 		case 'r':
2810Sstevel@tonic-gate 			rmode = 1;	/* restricted mode */
2820Sstevel@tonic-gate 			break;
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate 		case 's':
2850Sstevel@tonic-gate 			soflag = 1;	/* standout mode */
2860Sstevel@tonic-gate 			break;
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate 		case 'p':
2890Sstevel@tonic-gate 			promptstr = setprompt(optarg);
2900Sstevel@tonic-gate 			break;
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 		default:
2930Sstevel@tonic-gate 			usage();
2940Sstevel@tonic-gate 		}
2950Sstevel@tonic-gate 	}
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate 	nfiles = argc - optind;
2980Sstevel@tonic-gate 	fnames = &argv[optind];
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 	(void) signal(SIGQUIT, end_it);
3010Sstevel@tonic-gate 	(void) signal(SIGINT, end_it);
3020Sstevel@tonic-gate 	out_is_tty = isatty(1);
3030Sstevel@tonic-gate 	my_pgid = getpgrp();
3040Sstevel@tonic-gate 	if (out_is_tty) {
3050Sstevel@tonic-gate 		terminit();
3060Sstevel@tonic-gate 		(void) signal(SIGQUIT, on_brk);
3070Sstevel@tonic-gate 		(void) signal(SIGINT, on_brk);
3080Sstevel@tonic-gate 		if (signal(SIGTSTP, SIG_IGN) == SIG_DFL) {
3090Sstevel@tonic-gate 			(void) signal(SIGTSTP, onsusp);
3100Sstevel@tonic-gate 			catch_susp++;
3110Sstevel@tonic-gate 		}
3120Sstevel@tonic-gate 	}
3130Sstevel@tonic-gate 	if (window == 0)
3140Sstevel@tonic-gate 		window = lines - 1;
3150Sstevel@tonic-gate 	if (window <= 1)
3160Sstevel@tonic-gate 		window = 2;
3170Sstevel@tonic-gate 	if (initline <= 0)
3180Sstevel@tonic-gate 		initline = 1;
3190Sstevel@tonic-gate 	if (nfiles > 1)
3200Sstevel@tonic-gate 		prnames++;
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate 	if (nfiles == 0) {
3230Sstevel@tonic-gate 		fnames[0] = "-";
3240Sstevel@tonic-gate 		nfiles++;
3250Sstevel@tonic-gate 	}
3260Sstevel@tonic-gate 	while (fnum < nfiles) {
3270Sstevel@tonic-gate 		if (strcmp(fnames[fnum], "") == 0)
3280Sstevel@tonic-gate 			fnames[fnum] = "-";
329213Smuffin 		if ((in_file = checkf(fnames[fnum])) == NULL) {
3300Sstevel@tonic-gate 			status = 2;
3310Sstevel@tonic-gate 			fnum++;
3320Sstevel@tonic-gate 		} else {
3330Sstevel@tonic-gate 			status = 0;
3340Sstevel@tonic-gate 			if (out_is_tty)
3350Sstevel@tonic-gate 				fnum += screen(fnames[fnum]);
3360Sstevel@tonic-gate 			else {
3370Sstevel@tonic-gate 				if (prnames) {
3380Sstevel@tonic-gate 					(void) fputs("::::::::::::::\n",
3390Sstevel@tonic-gate 					    stdout);
3400Sstevel@tonic-gate 					(void) fputs(fnames[fnum], stdout);
3410Sstevel@tonic-gate 					(void) fputs("\n::::::::::::::\n",
3420Sstevel@tonic-gate 					    stdout);
3430Sstevel@tonic-gate 				}
3440Sstevel@tonic-gate 				copy_file(in_file, stdout);
3450Sstevel@tonic-gate 				fnum++;
3460Sstevel@tonic-gate 			}
3470Sstevel@tonic-gate 			(void) fflush(stdout);
3480Sstevel@tonic-gate 			if (pipe_in)
3490Sstevel@tonic-gate 				save_pipe();
3500Sstevel@tonic-gate 			else
3510Sstevel@tonic-gate 			if (in_file != tmp_fin)
3520Sstevel@tonic-gate 				(void) fclose(in_file);
3530Sstevel@tonic-gate 		}
3540Sstevel@tonic-gate 	}
3550Sstevel@tonic-gate 	end_it();
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate 	/*NOTREACHED*/
3580Sstevel@tonic-gate 	return (0);
3590Sstevel@tonic-gate }
3600Sstevel@tonic-gate 
3610Sstevel@tonic-gate static	char *
setprompt(s)3620Sstevel@tonic-gate setprompt(s)
3630Sstevel@tonic-gate char *s;
3640Sstevel@tonic-gate {
3650Sstevel@tonic-gate 	int i = 0;
3660Sstevel@tonic-gate 	int pct_d = 0;
3670Sstevel@tonic-gate 	static char pstr[PROMPTSIZE];
3680Sstevel@tonic-gate 
3690Sstevel@tonic-gate 	while (i < PROMPTSIZE - 2)
3700Sstevel@tonic-gate 		switch (pstr[i++] = *s++) {
3710Sstevel@tonic-gate 		case '\0':
3720Sstevel@tonic-gate 			return (pstr);
3730Sstevel@tonic-gate 		case '%':
3740Sstevel@tonic-gate 			if (*s == 'd' && !pct_d) {
3750Sstevel@tonic-gate 				pct_d++;
3760Sstevel@tonic-gate 			} else if (*s != '%')
3770Sstevel@tonic-gate 				pstr[i++] = '%';
3780Sstevel@tonic-gate 			if ((pstr[i++] = *s++) == '\0')
3790Sstevel@tonic-gate 				return (pstr);
3800Sstevel@tonic-gate 			break;
3810Sstevel@tonic-gate 		default:
3820Sstevel@tonic-gate 			break;
3830Sstevel@tonic-gate 		}
3840Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("pg: prompt too long\n"));
3850Sstevel@tonic-gate 	exit(1);
3860Sstevel@tonic-gate 	/*NOTREACHED*/
3870Sstevel@tonic-gate }
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate /*
3910Sstevel@tonic-gate  * Print out the contents of the file f, one screenful at a time.
3920Sstevel@tonic-gate  */
3930Sstevel@tonic-gate 
3940Sstevel@tonic-gate static int
screen(file_name)3950Sstevel@tonic-gate screen(file_name)
3960Sstevel@tonic-gate char *file_name;
3970Sstevel@tonic-gate {
3980Sstevel@tonic-gate 	int cmd_ret = 0;
3990Sstevel@tonic-gate 	off_t start;
4000Sstevel@tonic-gate 	short hadchance = 0;
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate 	old_ss.is_eof = 0;
4030Sstevel@tonic-gate 	old_ss.first_line = 0;
4040Sstevel@tonic-gate 	old_ss.last_line = 0;
4050Sstevel@tonic-gate 	new_ss = old_ss;
4060Sstevel@tonic-gate 	if (!firstf)
4070Sstevel@tonic-gate 		cmd_ret = command(file_name);
4080Sstevel@tonic-gate 	else {
4090Sstevel@tonic-gate 		firstf = 0;
4100Sstevel@tonic-gate 		if (initopt) {
4110Sstevel@tonic-gate 			initopt = 0;
4120Sstevel@tonic-gate 			new_ss.first_line = initline;
4130Sstevel@tonic-gate 			new_ss.last_line = initline + (off_t)window - 1;
4140Sstevel@tonic-gate 		} else if (srchopt) {
4150Sstevel@tonic-gate 			srchopt = 0;
4160Sstevel@tonic-gate 			if (!search(initbuf, (off_t)1))
4170Sstevel@tonic-gate 				cmd_ret = command(file_name);
4180Sstevel@tonic-gate 		} else {
4190Sstevel@tonic-gate 			new_ss.first_line = 1;
4200Sstevel@tonic-gate 			new_ss.last_line = (off_t)window;
4210Sstevel@tonic-gate 		}
4220Sstevel@tonic-gate 	}
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate 	for (;;) {
4250Sstevel@tonic-gate 		if (cmd_ret)
4260Sstevel@tonic-gate 			return (cmd_ret);
4270Sstevel@tonic-gate 		if (hadchance && new_ss.last_line >= eofl_no)
4280Sstevel@tonic-gate 			return (1);
4290Sstevel@tonic-gate 		hadchance = 0;
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 		if (new_ss.last_line < (off_t)window)
4320Sstevel@tonic-gate 			new_ss.last_line = (off_t)window;
4330Sstevel@tonic-gate 		if (find(0, new_ss.last_line + 1) != EOF)
4340Sstevel@tonic-gate 			new_ss.is_eof = 0;
4350Sstevel@tonic-gate 		else {
4360Sstevel@tonic-gate 			new_ss.is_eof = 1;
4370Sstevel@tonic-gate 			new_ss.last_line = eofl_no - 1;
4380Sstevel@tonic-gate 			new_ss.first_line = new_ss.last_line -
4390Sstevel@tonic-gate 			    (off_t)window + 1;
4400Sstevel@tonic-gate 		}
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate 		if (new_ss.first_line < 1)
4430Sstevel@tonic-gate 			new_ss.first_line = 1;
4440Sstevel@tonic-gate 		if (clropt) {
4450Sstevel@tonic-gate 			doclear();
4460Sstevel@tonic-gate 			start = new_ss.first_line;
4470Sstevel@tonic-gate 		} else {
4480Sstevel@tonic-gate 			if (new_ss.first_line == old_ss.last_line)
4490Sstevel@tonic-gate 				start = new_ss.first_line + 1;
4500Sstevel@tonic-gate 			else
4510Sstevel@tonic-gate 			if (new_ss.first_line > old_ss.last_line)
4520Sstevel@tonic-gate 				start = new_ss.first_line;
4530Sstevel@tonic-gate 			else
4540Sstevel@tonic-gate 			if (old_ss.first_line < new_ss.first_line)
4550Sstevel@tonic-gate 				start = old_ss.last_line + 1;
4560Sstevel@tonic-gate 			else
4570Sstevel@tonic-gate 				start = new_ss.first_line;
4580Sstevel@tonic-gate 
4590Sstevel@tonic-gate 			if (start < old_ss.first_line)
4600Sstevel@tonic-gate 				sopr(gettext("...skipping backward\n"), 0);
4610Sstevel@tonic-gate 			else
4620Sstevel@tonic-gate 			if (start > old_ss.last_line + 1)
4630Sstevel@tonic-gate 				sopr(gettext("...skipping forward\n"), 0);
4640Sstevel@tonic-gate 		}
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate 		for (; start <= new_ss.last_line; start++) {
4670Sstevel@tonic-gate 			(void) find(0, start);
4680Sstevel@tonic-gate 			(void) fputs(Line, stdout);
4690Sstevel@tonic-gate 			if (brk_hit) {
4700Sstevel@tonic-gate 				new_ss.last_line = find(1, 0);
4710Sstevel@tonic-gate 				new_ss.is_eof = 0;
4720Sstevel@tonic-gate 				break;
4730Sstevel@tonic-gate 			}
4740Sstevel@tonic-gate 		}
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate 		brk_hit = 0;
4770Sstevel@tonic-gate 		(void) fflush(stdout);
4780Sstevel@tonic-gate 		if (new_ss.is_eof) {
4790Sstevel@tonic-gate 			if (!eof_pause || eofl_no == 1)
4800Sstevel@tonic-gate 				return (1);
4810Sstevel@tonic-gate 			hadchance++;
4820Sstevel@tonic-gate 			error("(EOF)");
4830Sstevel@tonic-gate 		}
4840Sstevel@tonic-gate 		old_ss = new_ss;
4850Sstevel@tonic-gate 		cmd_ret = command((char *)NULL);
4860Sstevel@tonic-gate 	}
4870Sstevel@tonic-gate }
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate static	char	cmdbuf[LINSIZ], *cmdptr;
4900Sstevel@tonic-gate #define	BEEP()		if (bell) { (void) putp(bell); (void) fflush(stdout); }
4910Sstevel@tonic-gate #define	BLANKS(p)	while (*p == ' ' || *p == '\t') p++
4920Sstevel@tonic-gate #define	CHECKEND()	BLANKS(cmdptr); if (*cmdptr) { BEEP(); break; }
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate /*
4950Sstevel@tonic-gate  * Read a command and do it. A command consists of an optional integer
4960Sstevel@tonic-gate  * argument followed by the command character.  Return the number of files
4970Sstevel@tonic-gate  * to skip, 0 if we're still talking about the same file.
4980Sstevel@tonic-gate  */
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate static int
command(filename)5010Sstevel@tonic-gate command(filename)
5020Sstevel@tonic-gate char *filename;
5030Sstevel@tonic-gate {
5040Sstevel@tonic-gate 	off_t nlines;
5050Sstevel@tonic-gate 	FILE *sf;
5060Sstevel@tonic-gate 	char *cmdend;
5070Sstevel@tonic-gate 	pid_t id;
5080Sstevel@tonic-gate 	int skip;
5090Sstevel@tonic-gate 	int	len;
5100Sstevel@tonic-gate 	wchar_t	wc;
5110Sstevel@tonic-gate 	wchar_t	wc_e;
5120Sstevel@tonic-gate 	wchar_t	wc_e1;
5130Sstevel@tonic-gate 	char	*p;
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate 	for (;;) {
5160Sstevel@tonic-gate 		/*
5170Sstevel@tonic-gate 		 * Wait for output to drain before going on.
5180Sstevel@tonic-gate 		 * This is done so that the user will not hit
5190Sstevel@tonic-gate 		 * break and quit before he has seen the prompt.
5200Sstevel@tonic-gate 		 */
5210Sstevel@tonic-gate 		(void) ioctl(1, TCSBRK, 1);
5220Sstevel@tonic-gate 		if (setjmp(restore) > 0)
5230Sstevel@tonic-gate 			end_it();
5240Sstevel@tonic-gate 		inwait = 1;
5250Sstevel@tonic-gate 		brk_hit = 0;
5260Sstevel@tonic-gate 		if (errors)
5270Sstevel@tonic-gate 			errors = 0;
5280Sstevel@tonic-gate 		else {
5290Sstevel@tonic-gate 			kill_line();
5300Sstevel@tonic-gate 			prompt(filename);
5310Sstevel@tonic-gate 		}
5320Sstevel@tonic-gate 		(void) fflush(stdout);
5330Sstevel@tonic-gate 		if (ttyin())
5340Sstevel@tonic-gate 			continue;
5350Sstevel@tonic-gate 		cmdptr = cmdbuf;
5360Sstevel@tonic-gate 		nlines = number();
5370Sstevel@tonic-gate 		BLANKS(cmdptr);
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate 		if ((len = mbtowc(&wc, cmdptr, MB_CUR_MAX)) <= 0) {
5400Sstevel@tonic-gate 			wc = *cmdptr;
5410Sstevel@tonic-gate 			len = 1;
5420Sstevel@tonic-gate 		}
5430Sstevel@tonic-gate 		cmdptr += len;
5440Sstevel@tonic-gate 		switch (wc) {
5450Sstevel@tonic-gate 		case 'h':
5460Sstevel@tonic-gate 			CHECKEND();
5470Sstevel@tonic-gate 			help();
5480Sstevel@tonic-gate 			break;
5490Sstevel@tonic-gate 		case '\014': /* ^L */
5500Sstevel@tonic-gate 		case '.':	/* redisplay current window */
5510Sstevel@tonic-gate 			CHECKEND();
5520Sstevel@tonic-gate 			new_ss.first_line = old_ss.first_line;
5530Sstevel@tonic-gate 			new_ss.last_line = old_ss.last_line;
5540Sstevel@tonic-gate 			inwait = 0;
5550Sstevel@tonic-gate 			return (0);
5560Sstevel@tonic-gate 		case 'w':	/* set window size */
5570Sstevel@tonic-gate 		case 'z':
5580Sstevel@tonic-gate 			if (sign == -1) {
5590Sstevel@tonic-gate 				BEEP();
5600Sstevel@tonic-gate 				break;
5610Sstevel@tonic-gate 			}
5620Sstevel@tonic-gate 			CHECKEND();
5630Sstevel@tonic-gate 			if (nlines == 0)
5640Sstevel@tonic-gate 				nlines = (off_t)window;
5650Sstevel@tonic-gate 			else
5660Sstevel@tonic-gate 			if (nlines > 1)
5670Sstevel@tonic-gate 				window = (int)nlines;
5680Sstevel@tonic-gate 			else {
5690Sstevel@tonic-gate 				BEEP();
5700Sstevel@tonic-gate 				break;
5710Sstevel@tonic-gate 			}
5720Sstevel@tonic-gate 			new_ss.first_line = old_ss.last_line;
5730Sstevel@tonic-gate 			new_ss.last_line = new_ss.first_line +
5740Sstevel@tonic-gate 			    (off_t)window - 1;
5750Sstevel@tonic-gate 			inwait = 0;
5760Sstevel@tonic-gate 			return (0);
5770Sstevel@tonic-gate 		case '\004': /* ^D */
5780Sstevel@tonic-gate 		case 'd':
5790Sstevel@tonic-gate 			CHECKEND();
5800Sstevel@tonic-gate 			if (sign == 0)
5810Sstevel@tonic-gate 				sign = 1;
5820Sstevel@tonic-gate 			new_ss.last_line = old_ss.last_line +
5830Sstevel@tonic-gate 			    (off_t)sign*window/2;
5840Sstevel@tonic-gate 			new_ss.first_line = new_ss.last_line -
5850Sstevel@tonic-gate 			    (off_t)window + 1;
5860Sstevel@tonic-gate 			inwait = 0;
5870Sstevel@tonic-gate 			return (0);
5880Sstevel@tonic-gate 		case 's':
5890Sstevel@tonic-gate 			/*
5900Sstevel@tonic-gate 			 * save input in filename.
5910Sstevel@tonic-gate 			 * Check for filename, access, etc.
5920Sstevel@tonic-gate 			 */
5930Sstevel@tonic-gate 			BLANKS(cmdptr);
5940Sstevel@tonic-gate 			if (!*cmdptr) {
5950Sstevel@tonic-gate 				BEEP();
5960Sstevel@tonic-gate 				break;
5970Sstevel@tonic-gate 			}
5980Sstevel@tonic-gate 			if (setjmp(restore) > 0) {
5990Sstevel@tonic-gate 				BEEP();
6000Sstevel@tonic-gate 			} else {
6010Sstevel@tonic-gate 				char outstr[PROMPTSIZE];
6020Sstevel@tonic-gate 				if ((sf = fopen(cmdptr, "w")) == NULL) {
6030Sstevel@tonic-gate 					error("cannot open save file");
6040Sstevel@tonic-gate 					break;
6050Sstevel@tonic-gate 				}
6060Sstevel@tonic-gate 				kill_line();
6070Sstevel@tonic-gate 				(void) sprintf(outstr, gettext(
6080Sstevel@tonic-gate 				    "saving file %s"), cmdptr);
6090Sstevel@tonic-gate 				sopr(outstr, 1);
6100Sstevel@tonic-gate 				(void) fflush(stdout);
6110Sstevel@tonic-gate 				save_input(sf);
6120Sstevel@tonic-gate 				error("saved");
6130Sstevel@tonic-gate 			}
6140Sstevel@tonic-gate 			(void) fclose(sf);
6150Sstevel@tonic-gate 			break;
6160Sstevel@tonic-gate 		case 'q':
6170Sstevel@tonic-gate 		case 'Q':
6180Sstevel@tonic-gate 			CHECKEND();
6190Sstevel@tonic-gate 			inwait = 0;
6200Sstevel@tonic-gate 			end_it();
6210Sstevel@tonic-gate 			/*FALLTHROUGH*/
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate 		case 'f':	/* skip forward screenfuls */
6240Sstevel@tonic-gate 			CHECKEND();
6250Sstevel@tonic-gate 			if (sign == 0)
6260Sstevel@tonic-gate 				sign++;	/* skips are always relative */
6270Sstevel@tonic-gate 			if (nlines == 0)
6280Sstevel@tonic-gate 				nlines++;
6290Sstevel@tonic-gate 			nlines = nlines * (window - 1);
6300Sstevel@tonic-gate 			if (sign == 1)
6310Sstevel@tonic-gate 				new_ss.first_line = old_ss.last_line + nlines;
6320Sstevel@tonic-gate 			else
6330Sstevel@tonic-gate 				new_ss.first_line = old_ss.first_line - nlines;
6340Sstevel@tonic-gate 			new_ss.last_line = new_ss.first_line +
6350Sstevel@tonic-gate 			    (off_t)window - 1;
6360Sstevel@tonic-gate 			inwait = 0;
6370Sstevel@tonic-gate 			return (0);
6380Sstevel@tonic-gate 		case 'l':	/* get a line */
6390Sstevel@tonic-gate 			CHECKEND();
6400Sstevel@tonic-gate 			if (nlines == 0) {
6410Sstevel@tonic-gate 				nlines++;
6420Sstevel@tonic-gate 				if (sign == 0)
6430Sstevel@tonic-gate 					sign = 1;
6440Sstevel@tonic-gate 			}
6450Sstevel@tonic-gate 			switch (sign) {
6460Sstevel@tonic-gate 			case 1:
6470Sstevel@tonic-gate 				new_ss.last_line = old_ss.last_line + nlines;
6480Sstevel@tonic-gate 				new_ss.first_line =
6490Sstevel@tonic-gate 				    new_ss.last_line - (off_t)window + 1;
6500Sstevel@tonic-gate 				break;
6510Sstevel@tonic-gate 			case 0:  /* leave addressed line at top */
6520Sstevel@tonic-gate 				new_ss.first_line = nlines;
6530Sstevel@tonic-gate 				new_ss.last_line = nlines + (off_t)window - 1;
6540Sstevel@tonic-gate 				break;
6550Sstevel@tonic-gate 			case -1:
6560Sstevel@tonic-gate 				new_ss.first_line = old_ss.first_line - nlines;
6570Sstevel@tonic-gate 				new_ss.last_line =
6580Sstevel@tonic-gate 				    new_ss.first_line + (off_t)window - 1;
6590Sstevel@tonic-gate 				break;
6600Sstevel@tonic-gate 			}
6610Sstevel@tonic-gate 			inwait = 0;
6620Sstevel@tonic-gate 			return (0);
6630Sstevel@tonic-gate 		case '\0': /* \n or blank */
6640Sstevel@tonic-gate 			if (nlines == 0) {
6650Sstevel@tonic-gate 				nlines++;
6660Sstevel@tonic-gate 				if (sign == 0)
6670Sstevel@tonic-gate 					sign = 1;
6680Sstevel@tonic-gate 			}
6690Sstevel@tonic-gate 			nlines = (nlines - 1) * (window - 1);
6700Sstevel@tonic-gate 			switch (sign) {
6710Sstevel@tonic-gate 			case 1:
6720Sstevel@tonic-gate 				new_ss.first_line = old_ss.last_line + nlines;
6730Sstevel@tonic-gate 				new_ss.last_line =
6740Sstevel@tonic-gate 				    new_ss.first_line + (off_t)window - 1;
6750Sstevel@tonic-gate 				break;
6760Sstevel@tonic-gate 			case 0:
6770Sstevel@tonic-gate 				new_ss.first_line = nlines + 1;
6780Sstevel@tonic-gate 				new_ss.last_line = nlines + (off_t)window;
6790Sstevel@tonic-gate 				/*
6800Sstevel@tonic-gate 				 * This if statement is to fix the obscure bug
6810Sstevel@tonic-gate 				 * where you have a file that has less lines
6820Sstevel@tonic-gate 				 * than a screen holds, and the user types '1',
6830Sstevel@tonic-gate 				 * expecting to have the 1st page (re)displayed.
6840Sstevel@tonic-gate 				 * If we didn't set the new last_line to
6850Sstevel@tonic-gate 				 * eofl_no-1, the screen() routine
6860Sstevel@tonic-gate 				 * would cause pg to exit.
6870Sstevel@tonic-gate 				 */
6880Sstevel@tonic-gate 				if (new_ss.first_line == 1 &&
6890Sstevel@tonic-gate 				    new_ss.last_line >= eofl_no)
6900Sstevel@tonic-gate 					new_ss.last_line = eofl_no - 1;
6910Sstevel@tonic-gate 				break;
6920Sstevel@tonic-gate 			case -1:
6930Sstevel@tonic-gate 				new_ss.last_line = old_ss.first_line - nlines;
6940Sstevel@tonic-gate 				new_ss.first_line =
6950Sstevel@tonic-gate 				    new_ss.last_line - (off_t)window + 1;
6960Sstevel@tonic-gate 				break;
6970Sstevel@tonic-gate 			}
6980Sstevel@tonic-gate 			inwait = 0;
6990Sstevel@tonic-gate 			return (0);
7000Sstevel@tonic-gate 		case 'n':	/* switch to next file in arglist */
7010Sstevel@tonic-gate 			CHECKEND();
7020Sstevel@tonic-gate 			if (sign == 0)
7030Sstevel@tonic-gate 				sign = 1;
7040Sstevel@tonic-gate 			if (nlines == 0)
7050Sstevel@tonic-gate 				nlines++;
7060Sstevel@tonic-gate 			if ((skip = skipf(sign *nlines)) == 0) {
7070Sstevel@tonic-gate 				BEEP();
7080Sstevel@tonic-gate 				break;
7090Sstevel@tonic-gate 			}
7100Sstevel@tonic-gate 			inwait = 0;
7110Sstevel@tonic-gate 			return (skip);
7120Sstevel@tonic-gate 		case 'p':	/* switch to previous file in arglist */
7130Sstevel@tonic-gate 			CHECKEND();
7140Sstevel@tonic-gate 			if (sign == 0)
7150Sstevel@tonic-gate 				sign = 1;
7160Sstevel@tonic-gate 			if (nlines == 0)
7170Sstevel@tonic-gate 				nlines++;
7180Sstevel@tonic-gate 			if ((skip = skipf(-sign * nlines)) == 0) {
7190Sstevel@tonic-gate 				BEEP();
7200Sstevel@tonic-gate 				break;
7210Sstevel@tonic-gate 			}
7220Sstevel@tonic-gate 			inwait = 0;
7230Sstevel@tonic-gate 			return (skip);
7240Sstevel@tonic-gate 		case '$':	/* go to end of file */
7250Sstevel@tonic-gate 			CHECKEND();
7260Sstevel@tonic-gate 			sign = 1;
7270Sstevel@tonic-gate 			while (find(1, (off_t)10000) != EOF)
7280Sstevel@tonic-gate 				/* any large number will do */;
7290Sstevel@tonic-gate 			new_ss.last_line = eofl_no - 1;
7300Sstevel@tonic-gate 			new_ss.first_line = eofl_no - (off_t)window;
7310Sstevel@tonic-gate 			inwait = 0;
7320Sstevel@tonic-gate 			return (0);
7330Sstevel@tonic-gate 		case '/':	/* search forward for r.e. */
7340Sstevel@tonic-gate 		case '?':	/*   "  backwards */
7350Sstevel@tonic-gate 		case '^':	/* this ones a ? for regent100s */
7360Sstevel@tonic-gate 			if (sign < 0) {
7370Sstevel@tonic-gate 				BEEP();
7380Sstevel@tonic-gate 				break;
7390Sstevel@tonic-gate 			}
7400Sstevel@tonic-gate 			if (nlines == 0)
7410Sstevel@tonic-gate 				nlines++;
7420Sstevel@tonic-gate 			cmdptr--;
7430Sstevel@tonic-gate 			cmdend = cmdptr + (strlen(cmdptr) - 1);
7440Sstevel@tonic-gate 			wc_e1 = -1;
7450Sstevel@tonic-gate 			wc_e = -1;
7460Sstevel@tonic-gate 			for (p = cmdptr; p <= cmdend; p += len) {
7470Sstevel@tonic-gate 				wc_e1 = wc_e;
7480Sstevel@tonic-gate 				if ((len = mbtowc(&wc_e, p, MB_CUR_MAX)) <= 0) {
7490Sstevel@tonic-gate 					wc_e = *p;
7500Sstevel@tonic-gate 					len = 1;
7510Sstevel@tonic-gate 				}
7520Sstevel@tonic-gate 			}
7530Sstevel@tonic-gate 
7540Sstevel@tonic-gate 			if (cmdend > cmdptr + 1) {
7550Sstevel@tonic-gate 				if ((wc_e1 == *cmdptr) &&
7560Sstevel@tonic-gate 				    ((wc_e == L't') ||
7570Sstevel@tonic-gate 					(wc_e == L'm') || (wc_e == L'b'))) {
7580Sstevel@tonic-gate 					leave_search = wc_e;
7590Sstevel@tonic-gate 					wc_e = wc_e1;
7600Sstevel@tonic-gate 					cmdend--;
7610Sstevel@tonic-gate 				}
7620Sstevel@tonic-gate 			}
7630Sstevel@tonic-gate 			if ((cmdptr < cmdend) && (wc_e == *cmdptr))
7640Sstevel@tonic-gate 				*cmdend = '\0';
7650Sstevel@tonic-gate 			if (*cmdptr != '/')  /* signify back search by - */
7660Sstevel@tonic-gate 				nlines = -nlines;
7670Sstevel@tonic-gate 			if (!search(++cmdptr, (off_t)nlines))
7680Sstevel@tonic-gate 				break;
7690Sstevel@tonic-gate 			else {
7700Sstevel@tonic-gate 				inwait = 0;
7710Sstevel@tonic-gate 				return (0);
7720Sstevel@tonic-gate 			}
7730Sstevel@tonic-gate 		case '!':	/* shell escape */
7740Sstevel@tonic-gate 			if (rmode) {	/* restricted mode */
7750Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
7760Sstevel@tonic-gate 				"!command not allowed in restricted mode.\n"));
7770Sstevel@tonic-gate 				break;
7780Sstevel@tonic-gate 			}
7790Sstevel@tonic-gate 			if (!hard_copy) { /* redisplay the command */
7800Sstevel@tonic-gate 				(void) fputs(cmdbuf, stdout);
7810Sstevel@tonic-gate 				(void) fputs("\n", stdout);
7820Sstevel@tonic-gate 			}
7830Sstevel@tonic-gate 			if ((id = fork()) < 0) {
7840Sstevel@tonic-gate 				error("cannot fork, try again later");
7850Sstevel@tonic-gate 				break;
7860Sstevel@tonic-gate 			}
7870Sstevel@tonic-gate 			if (id == (pid_t)0) {
7880Sstevel@tonic-gate 				/*
7890Sstevel@tonic-gate 				 * if stdin is a pipe, need to close it so
7900Sstevel@tonic-gate 				 * that the terminal is really stdin for
7910Sstevel@tonic-gate 				 * the command
7920Sstevel@tonic-gate 				 */
7930Sstevel@tonic-gate 				(void) fclose(stdin);
7940Sstevel@tonic-gate 				(void) fclose(pg_stdin);
7950Sstevel@tonic-gate 				(void) dup(fileno(stdout));
7960Sstevel@tonic-gate 				(void) execl(shell, shell, "-c", cmdptr, 0);
7970Sstevel@tonic-gate 				(void) perror("exec");
7980Sstevel@tonic-gate 				exit(1);
7990Sstevel@tonic-gate 			}
8000Sstevel@tonic-gate 			(void) signal(SIGINT, SIG_IGN);
8010Sstevel@tonic-gate 			(void) signal(SIGQUIT, SIG_IGN);
8020Sstevel@tonic-gate 			if (catch_susp)
8030Sstevel@tonic-gate 				(void) signal(SIGTSTP, SIG_DFL);
8040Sstevel@tonic-gate 			while (wait((int *)0) != id);
8050Sstevel@tonic-gate 			{
8060Sstevel@tonic-gate 				if (errno == ECHILD)
8070Sstevel@tonic-gate 					break;
8080Sstevel@tonic-gate 				else
8090Sstevel@tonic-gate 					errno = 0;
8100Sstevel@tonic-gate 			}
8110Sstevel@tonic-gate 			(void) fputs("!\n", stdout);
8120Sstevel@tonic-gate 			(void) fflush(stdout);
8130Sstevel@tonic-gate 			(void) signal(SIGINT, on_brk);
8140Sstevel@tonic-gate 			(void) signal(SIGQUIT, on_brk);
8150Sstevel@tonic-gate 			if (catch_susp)
8160Sstevel@tonic-gate 				(void) signal(SIGTSTP, onsusp);
8170Sstevel@tonic-gate 			break;
8180Sstevel@tonic-gate 		default:
8190Sstevel@tonic-gate 			BEEP();
8200Sstevel@tonic-gate 			break;
8210Sstevel@tonic-gate 		}
8220Sstevel@tonic-gate 	}
8230Sstevel@tonic-gate }
8240Sstevel@tonic-gate 
8250Sstevel@tonic-gate static int
number()8260Sstevel@tonic-gate number()
8270Sstevel@tonic-gate {
8280Sstevel@tonic-gate 	int i;
8290Sstevel@tonic-gate 	char *p;
8300Sstevel@tonic-gate 
8310Sstevel@tonic-gate 	i = 0;
8320Sstevel@tonic-gate 	sign = 0;
8330Sstevel@tonic-gate 	p = cmdptr;
8340Sstevel@tonic-gate 	BLANKS(p);
8350Sstevel@tonic-gate 	if (*p == '+') {
8360Sstevel@tonic-gate 		p++;
8370Sstevel@tonic-gate 		sign = 1;
8380Sstevel@tonic-gate 	}
8390Sstevel@tonic-gate 	else
8400Sstevel@tonic-gate 	if (*p == '-') {
8410Sstevel@tonic-gate 		p++;
8420Sstevel@tonic-gate 		sign = -1;
8430Sstevel@tonic-gate 	}
8440Sstevel@tonic-gate 	while (isdigit(*p))
8450Sstevel@tonic-gate 		i = i * 10 + *p++ - '0';
8460Sstevel@tonic-gate 	cmdptr = p;
8470Sstevel@tonic-gate 	return (i);
8480Sstevel@tonic-gate }
8490Sstevel@tonic-gate 
8500Sstevel@tonic-gate static int
ttyin()8510Sstevel@tonic-gate ttyin()
8520Sstevel@tonic-gate {
8530Sstevel@tonic-gate 	char *sptr, *p;
8540Sstevel@tonic-gate 	wchar_t ch;
8550Sstevel@tonic-gate 	int slash = 0;
8560Sstevel@tonic-gate 	int state = 0;
8570Sstevel@tonic-gate 	int width, length;
8580Sstevel@tonic-gate 	char multic[MB_LEN_MAX];
8590Sstevel@tonic-gate 	int 	len;
8600Sstevel@tonic-gate 
8610Sstevel@tonic-gate 	(void) fixterm();
8620Sstevel@tonic-gate 	/* initialize state processing */
8630Sstevel@tonic-gate 	(void) set_state(&state, ' ', (char *)0);
8640Sstevel@tonic-gate 	sptr = cmdbuf;
8650Sstevel@tonic-gate 	while (state != 10) {
8660Sstevel@tonic-gate 		if ((ch = readch()) < 0 || !iswascii(ch) && !iswprint(ch)) {
8670Sstevel@tonic-gate 			BEEP();
8680Sstevel@tonic-gate 			continue;
8690Sstevel@tonic-gate 		}
8700Sstevel@tonic-gate 
8710Sstevel@tonic-gate 		if ((length = wctomb(multic, ch)) < 0)
8720Sstevel@tonic-gate 			length = 0;
8730Sstevel@tonic-gate 		multic[length] = 0;
8740Sstevel@tonic-gate 
8750Sstevel@tonic-gate 		if (ch == '\n' && !slash)
8760Sstevel@tonic-gate 			break;
8770Sstevel@tonic-gate 		if (ch == erasechar() && !slash) {
8780Sstevel@tonic-gate 			if (sptr > cmdbuf) {
8790Sstevel@tonic-gate 				char *oldp = cmdbuf;
8800Sstevel@tonic-gate 				wchar_t wchar;
8810Sstevel@tonic-gate 				p = cmdbuf;
8820Sstevel@tonic-gate 				while (p  < sptr) {
8830Sstevel@tonic-gate 					oldp = p;
8840Sstevel@tonic-gate 					len = mbtowc(&wchar, p, MB_CUR_MAX);
8850Sstevel@tonic-gate 					if (len <= 0) {
8860Sstevel@tonic-gate 						wchar = (unsigned char)*p;
8870Sstevel@tonic-gate 						len = 1;
8880Sstevel@tonic-gate 					}
8890Sstevel@tonic-gate 					p += len;
8900Sstevel@tonic-gate 				}
8910Sstevel@tonic-gate 				if ((width = wcwidth(wchar)) <= 0)
8920Sstevel@tonic-gate 					/* ascii control character */
8930Sstevel@tonic-gate 					width = 2;
8940Sstevel@tonic-gate 				promptlen -= width;
8950Sstevel@tonic-gate 				while (width--)
8960Sstevel@tonic-gate 					(void) fputs("\b \b", stdout);
8970Sstevel@tonic-gate 				sptr = oldp;
8980Sstevel@tonic-gate 			}
8990Sstevel@tonic-gate 			(void) set_state(&state, ch, sptr);
9000Sstevel@tonic-gate 			(void) fflush(stdout);
9010Sstevel@tonic-gate 			continue;
9020Sstevel@tonic-gate 		}
9030Sstevel@tonic-gate 		else
9040Sstevel@tonic-gate 		if (ch == killchar() && !slash) {
9050Sstevel@tonic-gate 			if (hard_copy)
9060Sstevel@tonic-gate 				(void) putwchar(ch);
9070Sstevel@tonic-gate 			(void) resetterm();
9080Sstevel@tonic-gate 			return (1);
9090Sstevel@tonic-gate 		}
9100Sstevel@tonic-gate 		if (ch < ' ')
9110Sstevel@tonic-gate 			width = 2;
9120Sstevel@tonic-gate 		else
9130Sstevel@tonic-gate 			if ((width = wcwidth(ch)) <= 0)
9140Sstevel@tonic-gate 				width = 0;
9150Sstevel@tonic-gate 		if (slash) {
9160Sstevel@tonic-gate 			slash = 0;
9170Sstevel@tonic-gate 			(void) fputs("\b \b", stdout);
9180Sstevel@tonic-gate 			sptr--;
9190Sstevel@tonic-gate 			promptlen--;
9200Sstevel@tonic-gate 		} else /* is there room to keep this character? */
9210Sstevel@tonic-gate 		if (sptr >= cmdbuf + sizeof (cmdbuf) ||
9220Sstevel@tonic-gate 		    promptlen + width >= columns) {
9230Sstevel@tonic-gate 			BEEP();
9240Sstevel@tonic-gate 			continue;
9250Sstevel@tonic-gate 		}
9260Sstevel@tonic-gate 		else
9270Sstevel@tonic-gate 		if (ch == '\\')
9280Sstevel@tonic-gate 			slash++;
9290Sstevel@tonic-gate 		if (set_state(&state, ch, sptr) == 0) {
9300Sstevel@tonic-gate 			BEEP();
9310Sstevel@tonic-gate 			continue;
9320Sstevel@tonic-gate 		}
9330Sstevel@tonic-gate 		(void) strncpy(sptr, multic, (size_t)length);
9340Sstevel@tonic-gate 		sptr += length;
9350Sstevel@tonic-gate 		if (ch < ' ') {
9360Sstevel@tonic-gate 			ch += 0100;
9370Sstevel@tonic-gate 			multic[0] = '^';
9380Sstevel@tonic-gate 			multic[1] = ch;
9390Sstevel@tonic-gate 			length = 2;
9400Sstevel@tonic-gate 		}
9410Sstevel@tonic-gate 		p = multic;
9420Sstevel@tonic-gate 		while (length--)
9430Sstevel@tonic-gate 			(void) putchar(*p++);
9440Sstevel@tonic-gate 		promptlen += width;
9450Sstevel@tonic-gate 		(void) fflush(stdout);
9460Sstevel@tonic-gate 	}
9470Sstevel@tonic-gate 
9480Sstevel@tonic-gate 	*sptr = '\0';
9490Sstevel@tonic-gate 	kill_line();
9500Sstevel@tonic-gate 	(void) fflush(stdout);
9510Sstevel@tonic-gate 	(void) resetterm();
9520Sstevel@tonic-gate 	return (0);
9530Sstevel@tonic-gate }
9540Sstevel@tonic-gate 
9550Sstevel@tonic-gate static	int
set_state(pstate,c,pc)9560Sstevel@tonic-gate set_state(pstate, c, pc)
9570Sstevel@tonic-gate int *pstate;
9580Sstevel@tonic-gate wchar_t c;
9590Sstevel@tonic-gate char *pc;
9600Sstevel@tonic-gate {
9610Sstevel@tonic-gate 	static char *psign;
9620Sstevel@tonic-gate 	static char *pnumber;
9630Sstevel@tonic-gate 	static char *pcommand;
9640Sstevel@tonic-gate 	static int slash;
9650Sstevel@tonic-gate 
9660Sstevel@tonic-gate 	if (*pstate == 0) {
9670Sstevel@tonic-gate 		psign = (char *)NULL;
9680Sstevel@tonic-gate 		pnumber = (char *)NULL;
9690Sstevel@tonic-gate 		pcommand = (char *)NULL;
9700Sstevel@tonic-gate 		*pstate = 1;
9710Sstevel@tonic-gate 		slash = 0;
9720Sstevel@tonic-gate 		return (1);
9730Sstevel@tonic-gate 	}
9740Sstevel@tonic-gate 	if (c == '\\' && !slash) {
9750Sstevel@tonic-gate 		slash++;
9760Sstevel@tonic-gate 		return (1);
9770Sstevel@tonic-gate 	}
9780Sstevel@tonic-gate 	if (c == erasechar() && !slash)
9790Sstevel@tonic-gate 		switch (*pstate) {
9800Sstevel@tonic-gate 		case 4:
9810Sstevel@tonic-gate 			if (pc > pcommand)
9820Sstevel@tonic-gate 				return (1);
9830Sstevel@tonic-gate 			pcommand = (char *)NULL;
9840Sstevel@tonic-gate 			/*FALLTHROUGH*/
9850Sstevel@tonic-gate 
9860Sstevel@tonic-gate 		case 3:
9870Sstevel@tonic-gate 			if (pnumber && pc > pnumber) {
9880Sstevel@tonic-gate 				*pstate = 3;
9890Sstevel@tonic-gate 				return (1);
9900Sstevel@tonic-gate 			}
9910Sstevel@tonic-gate 			pnumber = (char *)NULL;
9920Sstevel@tonic-gate 			/*FALLTHROUGH*/
9930Sstevel@tonic-gate 
9940Sstevel@tonic-gate 		case 2:
9950Sstevel@tonic-gate 			if (psign && pc > psign) {
9960Sstevel@tonic-gate 				*pstate = 2;
9970Sstevel@tonic-gate 				return (1);
9980Sstevel@tonic-gate 			}
9990Sstevel@tonic-gate 			psign = (char *)NULL;
10000Sstevel@tonic-gate 			/*FALLTHROUGH*/
10010Sstevel@tonic-gate 
10020Sstevel@tonic-gate 		case 1:
10030Sstevel@tonic-gate 			*pstate = 1;
10040Sstevel@tonic-gate 			return (1);
10050Sstevel@tonic-gate 		}
10060Sstevel@tonic-gate 
10070Sstevel@tonic-gate 	slash = 0;
10080Sstevel@tonic-gate 	switch (*pstate) {
10090Sstevel@tonic-gate 	case 1: /* before recieving anything interesting */
10100Sstevel@tonic-gate 		if (c == '\t' || (!nflag && c == ' '))
10110Sstevel@tonic-gate 			return (1);
10120Sstevel@tonic-gate 		if (c == '+' || c == '-') {
10130Sstevel@tonic-gate 			psign = pc;
10140Sstevel@tonic-gate 			*pstate = 2;
10150Sstevel@tonic-gate 			return (1);
10160Sstevel@tonic-gate 		}
10170Sstevel@tonic-gate 		/*FALLTHROUGH*/
10180Sstevel@tonic-gate 
10190Sstevel@tonic-gate 	case 2: /* recieved sign, waiting for digit */
10200Sstevel@tonic-gate 		if (iswascii(c) && isdigit(c)) {
10210Sstevel@tonic-gate 			pnumber = pc;
10220Sstevel@tonic-gate 			*pstate = 3;
10230Sstevel@tonic-gate 			return (1);
10240Sstevel@tonic-gate 		}
10250Sstevel@tonic-gate 		/*FALLTHROUGH*/
10260Sstevel@tonic-gate 
10270Sstevel@tonic-gate 	case 3: /* recieved digit, waiting for the rest of the number */
10280Sstevel@tonic-gate 		if (iswascii(c) && isdigit(c))
10290Sstevel@tonic-gate 			return (1);
10300Sstevel@tonic-gate 		if (iswascii(c) && pg_strchr("h\014.wz\004dqQfl np$", c)) {
10310Sstevel@tonic-gate 			pcommand = pc;
10320Sstevel@tonic-gate 			if (nflag)
10330Sstevel@tonic-gate 				*pstate = 10;
10340Sstevel@tonic-gate 			else
10350Sstevel@tonic-gate 				*pstate = 4;
10360Sstevel@tonic-gate 			return (1);
10370Sstevel@tonic-gate 		}
10380Sstevel@tonic-gate 		if (iswascii(c) && pg_strchr("s/^?!", c)) {
10390Sstevel@tonic-gate 			pcommand = pc;
10400Sstevel@tonic-gate 			*pstate = 4;
10410Sstevel@tonic-gate 			return (1);
10420Sstevel@tonic-gate 		}
10430Sstevel@tonic-gate 		return (0);
10440Sstevel@tonic-gate 	case 4:
10450Sstevel@tonic-gate 		return (1);
10460Sstevel@tonic-gate 	}
10470Sstevel@tonic-gate 	return (0);
10480Sstevel@tonic-gate }
10490Sstevel@tonic-gate 
10500Sstevel@tonic-gate static	int
readch()10510Sstevel@tonic-gate readch()
10520Sstevel@tonic-gate {
10530Sstevel@tonic-gate 	return (fgetwc(pg_stdin));
10540Sstevel@tonic-gate }
10550Sstevel@tonic-gate 
10560Sstevel@tonic-gate static void
help()10570Sstevel@tonic-gate help()
10580Sstevel@tonic-gate {
10590Sstevel@tonic-gate 	if (clropt)
10600Sstevel@tonic-gate 		doclear();
10610Sstevel@tonic-gate 
10620Sstevel@tonic-gate 	(void) fputs(gettext(
10630Sstevel@tonic-gate "-------------------------------------------------------\n"
10640Sstevel@tonic-gate "  h                     help\n"
10650Sstevel@tonic-gate "  q or Q                quit\n"
10660Sstevel@tonic-gate "  <blank> or <newline>  next page\n"
10670Sstevel@tonic-gate "  l                     next line\n"
10680Sstevel@tonic-gate "  d or <^D>             display half a page more\n"
10690Sstevel@tonic-gate "  . or <^L>             redisplay current page\n"
10700Sstevel@tonic-gate "  f                     skip the next page forward\n"
10710Sstevel@tonic-gate "  n                     next file\n"
10720Sstevel@tonic-gate "  p                     previous file\n"
10730Sstevel@tonic-gate "  $                     last page\n"
10740Sstevel@tonic-gate "  w or z                set window size and display next page\n"
10750Sstevel@tonic-gate "  s savefile            save current file in savefile\n"
10760Sstevel@tonic-gate "  /pattern/             search forward for pattern\n"
10770Sstevel@tonic-gate "  ?pattern? or\n"
10780Sstevel@tonic-gate "  ^pattern^             search backward for pattern\n"
10790Sstevel@tonic-gate "  !command              execute command\n"
10800Sstevel@tonic-gate "\n"
10810Sstevel@tonic-gate "Most commands can be preceeded by a number, as in:\n"
10820Sstevel@tonic-gate "+1<newline> (next page); -1<newline> (previous page); 1<newline> (page 1).\n"
10830Sstevel@tonic-gate "\n"
10840Sstevel@tonic-gate "See the manual page for more detail.\n"
10850Sstevel@tonic-gate "-------------------------------------------------------\n"),
1086213Smuffin 	    stdout);
10870Sstevel@tonic-gate }
10880Sstevel@tonic-gate 
10890Sstevel@tonic-gate /*
10900Sstevel@tonic-gate  * Skip nskip files in the file list (from the command line). Nskip may be
10910Sstevel@tonic-gate  * negative.
10920Sstevel@tonic-gate  */
10930Sstevel@tonic-gate 
10940Sstevel@tonic-gate static int
skipf(nskip)10950Sstevel@tonic-gate skipf(nskip)
10960Sstevel@tonic-gate int nskip;
10970Sstevel@tonic-gate {
10980Sstevel@tonic-gate 	if (fnum + nskip < 0) {
10990Sstevel@tonic-gate 		nskip = -fnum;
11000Sstevel@tonic-gate 		if (nskip == 0)
11010Sstevel@tonic-gate 			error("No previous file");
11020Sstevel@tonic-gate 	}
11030Sstevel@tonic-gate 	else
11040Sstevel@tonic-gate 	if (fnum + nskip > nfiles - 1) {
11050Sstevel@tonic-gate 		nskip = (nfiles - 1) - fnum;
11060Sstevel@tonic-gate 		if (nskip == 0)
11070Sstevel@tonic-gate 			error("No next file");
11080Sstevel@tonic-gate 	}
11090Sstevel@tonic-gate 	return (nskip);
11100Sstevel@tonic-gate }
11110Sstevel@tonic-gate 
11120Sstevel@tonic-gate /*
11130Sstevel@tonic-gate  * Check whether the file named by fs is a file which the user may
11140Sstevel@tonic-gate  * access.  If it is, return the opened file. Otherwise return NULL.
11150Sstevel@tonic-gate  */
11160Sstevel@tonic-gate 
11170Sstevel@tonic-gate static FILE *
checkf(fs)11180Sstevel@tonic-gate checkf(fs)
11190Sstevel@tonic-gate char *fs;
11200Sstevel@tonic-gate {
11210Sstevel@tonic-gate 	struct stat stbuf;
11220Sstevel@tonic-gate 	FILE *f;
11230Sstevel@tonic-gate 	int fd;
11240Sstevel@tonic-gate 	int f_was_opened;
11250Sstevel@tonic-gate 
11260Sstevel@tonic-gate 	pipe_in = 0;
11270Sstevel@tonic-gate 	if (strcmp(fs, "-") == 0) {
11280Sstevel@tonic-gate 		if (tmp_fin == NULL)
11290Sstevel@tonic-gate 			f = stdin;
11300Sstevel@tonic-gate 		else {
11310Sstevel@tonic-gate 			rewind(tmp_fin);
11320Sstevel@tonic-gate 			f = tmp_fin;
11330Sstevel@tonic-gate 		}
11340Sstevel@tonic-gate 		f_was_opened = 0;
11350Sstevel@tonic-gate 	} else {
11360Sstevel@tonic-gate 		if ((f = fopen(fs, "r")) == (FILE *)NULL) {
11370Sstevel@tonic-gate 			(void) fflush(stdout);
11380Sstevel@tonic-gate 			perror(fs);
11390Sstevel@tonic-gate 			return ((FILE *)NULL);
11400Sstevel@tonic-gate 		}
11410Sstevel@tonic-gate 		f_was_opened = 1;
11420Sstevel@tonic-gate 	}
11430Sstevel@tonic-gate 	if (fstat(fileno(f), &stbuf) == -1) {
11440Sstevel@tonic-gate 		if (f_was_opened)
11450Sstevel@tonic-gate 			(void) fclose(f);
11460Sstevel@tonic-gate 		(void) fflush(stdout);
11470Sstevel@tonic-gate 		perror(fs);
11480Sstevel@tonic-gate 		return ((FILE *)NULL);
11490Sstevel@tonic-gate 	}
11500Sstevel@tonic-gate 	if ((stbuf.st_mode & S_IFMT) == S_IFDIR) {
11510Sstevel@tonic-gate 		if (f_was_opened)
11520Sstevel@tonic-gate 			(void) fclose(f);
11530Sstevel@tonic-gate 		(void) fprintf(stderr, "pg: ");
11540Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is a directory\n"), fs);
11550Sstevel@tonic-gate 		return ((FILE *)NULL);
11560Sstevel@tonic-gate 	}
11570Sstevel@tonic-gate 	if ((stbuf.st_mode & S_IFMT) == S_IFREG) {
11580Sstevel@tonic-gate 		if (f == stdin)		/* It may have been read from */
11590Sstevel@tonic-gate 			rewind(f);	/* already, and not reopened  */
11600Sstevel@tonic-gate 	} else {
11610Sstevel@tonic-gate 		if (f != stdin) {
11620Sstevel@tonic-gate 			if (f_was_opened)
11630Sstevel@tonic-gate 				(void) fclose(f);
11640Sstevel@tonic-gate 			(void) fprintf(stderr, "pg: ");
11650Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
11660Sstevel@tonic-gate 			"special files only handled as standard input\n"));
11670Sstevel@tonic-gate 			return ((FILE *)NULL);
11680Sstevel@tonic-gate 		} else {
11690Sstevel@tonic-gate 			if ((fd = mkstemp(tmp_name)) < 0) {
11700Sstevel@tonic-gate 			    (void) perror(tmp_name);
11710Sstevel@tonic-gate 			    return ((FILE *)NULL);
11720Sstevel@tonic-gate 			}
11730Sstevel@tonic-gate 			(void) close(fd);
11740Sstevel@tonic-gate 			if ((tmp_fou = fopen(tmp_name, "w")) == NULL) {
11750Sstevel@tonic-gate 				(void) perror(tmp_name);
11760Sstevel@tonic-gate 				return ((FILE *)NULL);
11770Sstevel@tonic-gate 			}
11780Sstevel@tonic-gate 			if ((tmp_fin = fopen(tmp_name, "r")) == NULL) {
11790Sstevel@tonic-gate 				(void) perror(tmp_name);
11800Sstevel@tonic-gate 				return ((FILE *)NULL);
11810Sstevel@tonic-gate 			}
11820Sstevel@tonic-gate 			pipe_in = 1;
11830Sstevel@tonic-gate 		}
11840Sstevel@tonic-gate 	}
11850Sstevel@tonic-gate 	lineset(BOF);
11860Sstevel@tonic-gate 	return (f);
11870Sstevel@tonic-gate }
11880Sstevel@tonic-gate 
11890Sstevel@tonic-gate static void
copy_file(f,out)11900Sstevel@tonic-gate copy_file(f, out)
11910Sstevel@tonic-gate FILE *f, *out;
11920Sstevel@tonic-gate {
11930Sstevel@tonic-gate 	int c;
11940Sstevel@tonic-gate 
11950Sstevel@tonic-gate 	while ((c = getc(f)) != EOF)
11960Sstevel@tonic-gate 		(void) putc(c, out);
11970Sstevel@tonic-gate 
11980Sstevel@tonic-gate }
11990Sstevel@tonic-gate 
12000Sstevel@tonic-gate static void
re_error(i)12010Sstevel@tonic-gate re_error(i)
12020Sstevel@tonic-gate int i;
12030Sstevel@tonic-gate {
12040Sstevel@tonic-gate 	int j;
12050Sstevel@tonic-gate 	static struct messages {
12060Sstevel@tonic-gate 		char *message;
12070Sstevel@tonic-gate 		int number;
12080Sstevel@tonic-gate 		} re_errmsg[] = {
12090Sstevel@tonic-gate 		"Pattern not found",				1,
12100Sstevel@tonic-gate 		"Range endpoint too large",			11,
12110Sstevel@tonic-gate 		"Bad number",					16,
12120Sstevel@tonic-gate 		"`\\digit' out of range",			25,
12130Sstevel@tonic-gate 		"No remembered search string",  		41,
12140Sstevel@tonic-gate 		"\\( \\) imbalance",				42,
12150Sstevel@tonic-gate 		"Too many \\(",					43,
12160Sstevel@tonic-gate 		"More than two numbers given in \\{ \\}",  	44,
12170Sstevel@tonic-gate 		"} expected after \\",				45,
12180Sstevel@tonic-gate 		"First number exceeds second in \\{ \\}",  	46,
12190Sstevel@tonic-gate 		"[] imbalance",					49,
12200Sstevel@tonic-gate 		"Regular expression overflow",			50,
12210Sstevel@tonic-gate 		"Illegal byte sequence",			67,
12220Sstevel@tonic-gate 		"Bad regular expression",			0
12230Sstevel@tonic-gate 		};
12240Sstevel@tonic-gate 
12250Sstevel@tonic-gate 	for (j = 0; re_errmsg[j].number != 0; j++)
12260Sstevel@tonic-gate 		if (re_errmsg[j].number == i)
12270Sstevel@tonic-gate 			break;
12280Sstevel@tonic-gate 	error(re_errmsg[j].message);
12290Sstevel@tonic-gate 	longjmp(restore, 1);  /* restore to search() */
12300Sstevel@tonic-gate }
12310Sstevel@tonic-gate 
12320Sstevel@tonic-gate /*
12330Sstevel@tonic-gate  * Search for nth ocurrence of regular expression contained in buf in the file
12340Sstevel@tonic-gate  *	negative n implies backward search
12350Sstevel@tonic-gate  *	n 'guaranteed' non-zero
12360Sstevel@tonic-gate  */
12370Sstevel@tonic-gate 
12380Sstevel@tonic-gate 
12390Sstevel@tonic-gate static int
search(buf,n)12400Sstevel@tonic-gate search(buf, n)
12410Sstevel@tonic-gate char buf[];
12420Sstevel@tonic-gate off_t n;
12430Sstevel@tonic-gate {
12440Sstevel@tonic-gate 	int direction;
12450Sstevel@tonic-gate 	static char *expbuf;
12460Sstevel@tonic-gate 	char *nexpbuf;
12470Sstevel@tonic-gate 	int END_COND;
12480Sstevel@tonic-gate 
12490Sstevel@tonic-gate 	if (setjmp(restore) <= 0) {
12500Sstevel@tonic-gate 		nexpbuf = compile(buf, (char *)0, (char *)0);
12510Sstevel@tonic-gate 		if (regerrno) {
12520Sstevel@tonic-gate 			if (regerrno != 41 || expbuf == NULL)
12530Sstevel@tonic-gate 				re_error(regerrno);
12540Sstevel@tonic-gate 		} else {
12550Sstevel@tonic-gate 			if (expbuf)
12560Sstevel@tonic-gate 				free(expbuf);
12570Sstevel@tonic-gate 			expbuf = nexpbuf;
12580Sstevel@tonic-gate 		}
12590Sstevel@tonic-gate 
12600Sstevel@tonic-gate 		if (n < 0) {	/* search back */
12610Sstevel@tonic-gate 			direction = -1;
12620Sstevel@tonic-gate 			(void) find(0, old_ss.first_line);
12630Sstevel@tonic-gate 			END_COND = BOF;
12640Sstevel@tonic-gate 		} else {
12650Sstevel@tonic-gate 			direction = 1;
12660Sstevel@tonic-gate 			(void) find(0, old_ss.last_line);
12670Sstevel@tonic-gate 			END_COND = EOF;
12680Sstevel@tonic-gate 		}
12690Sstevel@tonic-gate 
12700Sstevel@tonic-gate 		while (find(1, direction) != END_COND) {
12710Sstevel@tonic-gate 			if (brk_hit)
12720Sstevel@tonic-gate 				break;
12730Sstevel@tonic-gate 			if (step(Line, expbuf))
12740Sstevel@tonic-gate 				if ((n -= direction) == 0) {
12750Sstevel@tonic-gate 					switch (leave_search) {
12760Sstevel@tonic-gate 					case 't':
12770Sstevel@tonic-gate 						new_ss.first_line =
12780Sstevel@tonic-gate 						    find(1, (off_t)0);
12790Sstevel@tonic-gate 						new_ss.last_line =
12800Sstevel@tonic-gate 						    new_ss.first_line +
12810Sstevel@tonic-gate 						    (off_t)window
12820Sstevel@tonic-gate 						    - 1;
12830Sstevel@tonic-gate 						break;
12840Sstevel@tonic-gate 					case 'b':
12850Sstevel@tonic-gate 						new_ss.last_line =
12860Sstevel@tonic-gate 						    find(1, (off_t)0);
12870Sstevel@tonic-gate 						new_ss.first_line =
12880Sstevel@tonic-gate 						    new_ss.last_line -
12890Sstevel@tonic-gate 						    (off_t)window
12900Sstevel@tonic-gate 						    + 1;
12910Sstevel@tonic-gate 						break;
12920Sstevel@tonic-gate 					case 'm':
12930Sstevel@tonic-gate 						new_ss.first_line =
12940Sstevel@tonic-gate 						    find(1, (off_t)0) -
12950Sstevel@tonic-gate 						    ((off_t)window - 1)/2;
12960Sstevel@tonic-gate 						new_ss.last_line =
12970Sstevel@tonic-gate 						    new_ss.first_line +
12980Sstevel@tonic-gate 						    (off_t)window
12990Sstevel@tonic-gate 						    - 1;
13000Sstevel@tonic-gate 						break;
13010Sstevel@tonic-gate 					}
13020Sstevel@tonic-gate 					return (1);
13030Sstevel@tonic-gate 				}
13040Sstevel@tonic-gate 		}
13050Sstevel@tonic-gate 		re_error(1); /* Pattern not found */
13060Sstevel@tonic-gate 	}
13070Sstevel@tonic-gate 	BEEP();
13080Sstevel@tonic-gate 	return (0);
13090Sstevel@tonic-gate }
13100Sstevel@tonic-gate 
13110Sstevel@tonic-gate /*
13120Sstevel@tonic-gate  *	find -- find line in file f, subject to certain constraints.
13130Sstevel@tonic-gate  *
13140Sstevel@tonic-gate  *	This is the reason for all the funny stuff with sign and nlines.
13150Sstevel@tonic-gate  *	We need to be able to differentiate between relative and abosolute
13160Sstevel@tonic-gate  *	address specifications.
13170Sstevel@tonic-gate  *
13180Sstevel@tonic-gate  *	So...there are basically three cases that this routine
13190Sstevel@tonic-gate  *	handles. Either line is zero, which  means there is to be
13200Sstevel@tonic-gate  *	no motion (because line numbers start at one), or
13210Sstevel@tonic-gate  *	how and line specify a number, or line itself is negative,
13220Sstevel@tonic-gate  *	which is the same as having how == -1 and line == abs(line).
13230Sstevel@tonic-gate  *
13240Sstevel@tonic-gate  *	Then, figure where exactly it is that we are going (an absolute
13250Sstevel@tonic-gate  *	line number). Find out if it is within what we have read,
13260Sstevel@tonic-gate  *	if so, go there without further ado. Otherwise, do some
13270Sstevel@tonic-gate  *	magic to get there, saving all the intervening lines,
13280Sstevel@tonic-gate  *	in case the user wants to see them some time later.
13290Sstevel@tonic-gate  *
13300Sstevel@tonic-gate  *	In any case, return the line number that we end up at.
13310Sstevel@tonic-gate  *	(This is used by search() and screen()). If we go past EOF,
13320Sstevel@tonic-gate  *	return EOF.
13330Sstevel@tonic-gate  *	This EOF will go away eventually, as pg is expanded to
13340Sstevel@tonic-gate  *	handle multiple files as one huge one. Then EOF will
13350Sstevel@tonic-gate  *	mean we have run off the file list.
13360Sstevel@tonic-gate  *	If the requested line number is too far back, return BOF.
13370Sstevel@tonic-gate  */
13380Sstevel@tonic-gate 
13390Sstevel@tonic-gate static off_t
find(how,line)13400Sstevel@tonic-gate find(how, line)	/* find the line and seek there */
13410Sstevel@tonic-gate int how;
13420Sstevel@tonic-gate off_t line;
13430Sstevel@tonic-gate {
13440Sstevel@tonic-gate 	/* no compacted memory yet */
13450Sstevel@tonic-gate 	FILE *f = in_file;
13460Sstevel@tonic-gate 	off_t where;
13470Sstevel@tonic-gate 
13480Sstevel@tonic-gate 	if (how == 0)
13490Sstevel@tonic-gate 		where = line;
13500Sstevel@tonic-gate 	else
13510Sstevel@tonic-gate 		if (dot == zero - 1)
13520Sstevel@tonic-gate 			where = how * line;
13530Sstevel@tonic-gate 		else
13540Sstevel@tonic-gate 			where = how * line + dot->l_no;
13550Sstevel@tonic-gate 
13560Sstevel@tonic-gate 	/* now, where is either at, before, or after dol */
13570Sstevel@tonic-gate 	/* most likely case is after, so do it first */
13580Sstevel@tonic-gate 
13590Sstevel@tonic-gate 	eoflag = 0;
13600Sstevel@tonic-gate 	if (where >= dol->l_no) {
13610Sstevel@tonic-gate 		if (doliseof) {
13620Sstevel@tonic-gate 			dot = dol;
13630Sstevel@tonic-gate 			eoflag++;
13640Sstevel@tonic-gate 			return (EOF);
13650Sstevel@tonic-gate 		}
13660Sstevel@tonic-gate 		if (pipe_in)
13670Sstevel@tonic-gate 			in_file = f = stdin;
13680Sstevel@tonic-gate 		else
13690Sstevel@tonic-gate 			(void) fseeko(f, (off_t)dol->l_addr, SEEK_SET);
13700Sstevel@tonic-gate 		dot = dol - 1;
1371*13093SRoger.Faulkner@Oracle.COM 		while ((nchars = getaline(f)) != EOF) {
13720Sstevel@tonic-gate 			dot++;
13730Sstevel@tonic-gate 			newdol(f);
13740Sstevel@tonic-gate 			if (where == dot->l_no || brk_hit)
13750Sstevel@tonic-gate 				break;
13760Sstevel@tonic-gate 		}
13770Sstevel@tonic-gate 		if (nchars != EOF)
13780Sstevel@tonic-gate 			return (dot->l_no);
13790Sstevel@tonic-gate 		else { /* EOF */
13800Sstevel@tonic-gate 			dot = dol;
13810Sstevel@tonic-gate 			eoflag++;
13820Sstevel@tonic-gate 			doliseof++;
13830Sstevel@tonic-gate 			eofl_no = dol->l_no;
13840Sstevel@tonic-gate 			return (EOF);
13850Sstevel@tonic-gate 		}
13860Sstevel@tonic-gate 	} else { /* where < dol->l_no */
13870Sstevel@tonic-gate 		if (pipe_in) {
13880Sstevel@tonic-gate 			(void) fflush(tmp_fou);
13890Sstevel@tonic-gate 			in_file = f = tmp_fin;
13900Sstevel@tonic-gate 		}
13910Sstevel@tonic-gate 		if (where < zero->l_no) {
13920Sstevel@tonic-gate 			(void) fseeko(f, (off_t)zero->l_addr, SEEK_SET);
13930Sstevel@tonic-gate 			dot = zero - 1;
13940Sstevel@tonic-gate 			return (BOF);
13950Sstevel@tonic-gate 		} else {
13960Sstevel@tonic-gate 			dot = zero + where - 1;
13970Sstevel@tonic-gate 			(void) fseeko(f, (off_t)dot->l_addr, SEEK_SET);
1398*13093SRoger.Faulkner@Oracle.COM 			nchars = getaline(f);
13990Sstevel@tonic-gate 			return (dot->l_no);
14000Sstevel@tonic-gate 		}
14010Sstevel@tonic-gate 	}
14020Sstevel@tonic-gate }
14030Sstevel@tonic-gate 
14040Sstevel@tonic-gate static FILE *fileptr;
14050Sstevel@tonic-gate static int (*rdchar)();
14060Sstevel@tonic-gate 
14070Sstevel@tonic-gate static int
mrdchar()14080Sstevel@tonic-gate mrdchar()
14090Sstevel@tonic-gate {
14100Sstevel@tonic-gate 	return (rdchar(fileptr));
14110Sstevel@tonic-gate }
14120Sstevel@tonic-gate 
14130Sstevel@tonic-gate /*
14140Sstevel@tonic-gate  * Get a logical line
14150Sstevel@tonic-gate  */
14160Sstevel@tonic-gate 
14170Sstevel@tonic-gate static off_t
getaline(f)1418*13093SRoger.Faulkner@Oracle.COM getaline(f)
14190Sstevel@tonic-gate FILE *f;
14200Sstevel@tonic-gate {
14210Sstevel@tonic-gate 	char	*p;
14220Sstevel@tonic-gate 	int	column;
14230Sstevel@tonic-gate 	static char multic[MB_LEN_MAX];
14240Sstevel@tonic-gate 	static int savlength;
14250Sstevel@tonic-gate 	wchar_t c;
14260Sstevel@tonic-gate 	int length, width;
14270Sstevel@tonic-gate 
14280Sstevel@tonic-gate 	if (pipe_in && f == stdin)
14290Sstevel@tonic-gate 		rdchar = fgetputc;
14300Sstevel@tonic-gate 	else
14310Sstevel@tonic-gate 		rdchar = (int (*)())fgetwc;
14320Sstevel@tonic-gate 
14330Sstevel@tonic-gate 	fileptr = f;
1434*13093SRoger.Faulkner@Oracle.COM 	/* copy overlap from previous call to getaline */
14350Sstevel@tonic-gate 	if (savlength)
14360Sstevel@tonic-gate 		(void) strncpy(Line, multic, (size_t)savlength);
14370Sstevel@tonic-gate 	for (column = 0, p = Line + savlength; ; ) {
14380Sstevel@tonic-gate 		if ((c = mrdchar()) <= 0) {
14390Sstevel@tonic-gate 			clearerr(f);
14400Sstevel@tonic-gate 			if (p > Line) {	/* last line doesn't have '\n', */
14410Sstevel@tonic-gate 				*p++ = '\n';
14420Sstevel@tonic-gate 				*p = '\0';	/* print it any way */
14430Sstevel@tonic-gate 				return (column);
14440Sstevel@tonic-gate 			}
14450Sstevel@tonic-gate 			return (EOF);
14460Sstevel@tonic-gate 		}
14470Sstevel@tonic-gate 		length = wctomb(multic, c);
14480Sstevel@tonic-gate 		if (length < 0) {
14490Sstevel@tonic-gate 			length = -length;
14500Sstevel@tonic-gate 			c = 0;
14510Sstevel@tonic-gate 		}
14520Sstevel@tonic-gate 		if ((width = wcwidth(c)) < 0)
14530Sstevel@tonic-gate 			width = 0;
14540Sstevel@tonic-gate 		if (column + width > columns && !fflag)
14550Sstevel@tonic-gate 			break;
14560Sstevel@tonic-gate 
14570Sstevel@tonic-gate 		if (p + length > &Line[LINSIZ - 2] && c != '\n')
14580Sstevel@tonic-gate 			break;
14590Sstevel@tonic-gate 		(void) strncpy(p, multic, (size_t)length);
14600Sstevel@tonic-gate 		p += length;
14610Sstevel@tonic-gate 		column += width;
14620Sstevel@tonic-gate 		/* don't have any overlap here */
14630Sstevel@tonic-gate 		length = 0;
14640Sstevel@tonic-gate 		switch (c) {
14650Sstevel@tonic-gate 		case '\t': /* just a guess */
14660Sstevel@tonic-gate 			column = 1 + (column | 7);
14670Sstevel@tonic-gate 			break;
14680Sstevel@tonic-gate 		case '\b':
14690Sstevel@tonic-gate 			if (column > 0)
14700Sstevel@tonic-gate 				column--;
14710Sstevel@tonic-gate 			break;
14720Sstevel@tonic-gate 		case '\r':
14730Sstevel@tonic-gate 			column = 0;
14740Sstevel@tonic-gate 			break;
14750Sstevel@tonic-gate 		}
14760Sstevel@tonic-gate 		if (c == '\n')
14770Sstevel@tonic-gate 			break;
14780Sstevel@tonic-gate 		if (column >= columns && !fflag)
14790Sstevel@tonic-gate 			break;
14800Sstevel@tonic-gate 	}
14810Sstevel@tonic-gate 	if (c != '\n') { /* We're stopping in the middle of the line */
14820Sstevel@tonic-gate 		if (column != columns || !auto_right_margin)
14830Sstevel@tonic-gate 			*p++ = '\n';	/* for the display */
1484*13093SRoger.Faulkner@Oracle.COM 		/* save overlap for next call to getaline */
14850Sstevel@tonic-gate 		savlength = length;
14860Sstevel@tonic-gate 		if (savlength == 0) {
14870Sstevel@tonic-gate 			/*
14880Sstevel@tonic-gate 			 * check if following byte is newline and get
14890Sstevel@tonic-gate 			 * it if it is
14900Sstevel@tonic-gate 			 */
14910Sstevel@tonic-gate 			c = fgetwc(f);
14920Sstevel@tonic-gate 			if (c == '\n') {
14930Sstevel@tonic-gate 				/* gobble and copy (if necessary) newline */
14940Sstevel@tonic-gate 				(void) ungetwc(c, f);
14950Sstevel@tonic-gate 				(void) (*rdchar)(f);
14960Sstevel@tonic-gate 			} else if (c == EOF)
14970Sstevel@tonic-gate 				clearerr(f);
14980Sstevel@tonic-gate 			else
14990Sstevel@tonic-gate 				(void) ungetwc(c, f);
15000Sstevel@tonic-gate 		}
15010Sstevel@tonic-gate 	} else
15020Sstevel@tonic-gate 		savlength = 0;
15030Sstevel@tonic-gate 	*p = 0;
15040Sstevel@tonic-gate 	return (column);
15050Sstevel@tonic-gate }
15060Sstevel@tonic-gate 
15070Sstevel@tonic-gate static void
save_input(f)15080Sstevel@tonic-gate save_input(f)
15090Sstevel@tonic-gate FILE *f;
15100Sstevel@tonic-gate {
15110Sstevel@tonic-gate 	if (pipe_in) {
15120Sstevel@tonic-gate 		save_pipe();
15130Sstevel@tonic-gate 		in_file = tmp_fin;
15140Sstevel@tonic-gate 		pipe_in = 0;
15150Sstevel@tonic-gate 	}
15160Sstevel@tonic-gate 	(void) fseeko(in_file, (off_t)0, SEEK_SET);
15170Sstevel@tonic-gate 	copy_file(in_file, f);
15180Sstevel@tonic-gate }
15190Sstevel@tonic-gate 
15200Sstevel@tonic-gate static void
save_pipe()15210Sstevel@tonic-gate save_pipe()
15220Sstevel@tonic-gate {
15230Sstevel@tonic-gate 	if (!doliseof)
15240Sstevel@tonic-gate 		while (fgetputc(stdin) != EOF)
15250Sstevel@tonic-gate 			if (brk_hit) {
15260Sstevel@tonic-gate 				brk_hit = 0;
15270Sstevel@tonic-gate 				error("Piped input only partially saved");
15280Sstevel@tonic-gate 				break;
15290Sstevel@tonic-gate 			}
15300Sstevel@tonic-gate 	(void) fclose(tmp_fou);
15310Sstevel@tonic-gate }
15320Sstevel@tonic-gate 
15330Sstevel@tonic-gate static int
fgetputc(f)15340Sstevel@tonic-gate fgetputc(f)	/* copy anything read from a pipe to tmp_fou */
15350Sstevel@tonic-gate FILE *f;
15360Sstevel@tonic-gate {
15370Sstevel@tonic-gate 	int c;
15380Sstevel@tonic-gate 
15390Sstevel@tonic-gate 	if ((c = fgetwc(f)) != EOF)
15400Sstevel@tonic-gate 		(void) fputwc(c, tmp_fou);
15410Sstevel@tonic-gate 	return (c);
15420Sstevel@tonic-gate }
15430Sstevel@tonic-gate 
15440Sstevel@tonic-gate static	void
lineset(how)15450Sstevel@tonic-gate lineset(how)	/* initialize line memory */
15460Sstevel@tonic-gate int how;
15470Sstevel@tonic-gate {
15480Sstevel@tonic-gate 	if (zero == NULL) {
15490Sstevel@tonic-gate 		nlall = 128;
15500Sstevel@tonic-gate 		zero = (LINE *) malloc(nlall * sizeof (LINE));
15510Sstevel@tonic-gate 	}
15520Sstevel@tonic-gate 	dol = contig = zero;
15530Sstevel@tonic-gate 	zero->l_no = 1;
15540Sstevel@tonic-gate 	zero->l_addr = 0l;
15550Sstevel@tonic-gate 	if (how == BOF) {
15560Sstevel@tonic-gate 		dot = zero - 1;
15570Sstevel@tonic-gate 		eoflag = 0;
15580Sstevel@tonic-gate 		doliseof = 0;
15590Sstevel@tonic-gate 		eofl_no = -1;
15600Sstevel@tonic-gate 	} else {
15610Sstevel@tonic-gate 		dot = dol;
15620Sstevel@tonic-gate 		eoflag = 1;
15630Sstevel@tonic-gate 		doliseof = 1;
15640Sstevel@tonic-gate 		eofl_no = 1;
15650Sstevel@tonic-gate 	}
15660Sstevel@tonic-gate }
15670Sstevel@tonic-gate 
15680Sstevel@tonic-gate static void
newdol(f)15690Sstevel@tonic-gate newdol(f)	/* add address of new 'dol' */
15700Sstevel@tonic-gate 		/* assumes that f is currently at beginning of said line */
15710Sstevel@tonic-gate 		/* updates dol */
15720Sstevel@tonic-gate FILE *f;
15730Sstevel@tonic-gate {
15740Sstevel@tonic-gate 	int diff;
15750Sstevel@tonic-gate 
15760Sstevel@tonic-gate 	if ((dol - zero) + 1 >= nlall) {
15770Sstevel@tonic-gate 		LINE *ozero = zero;
15780Sstevel@tonic-gate 
15790Sstevel@tonic-gate 		nlall += 512;
15800Sstevel@tonic-gate 		if ((zero = (LINE *)realloc((char *)zero,
15810Sstevel@tonic-gate 		    (unsigned)(nlall * sizeof (LINE)))) == NULL) {
15820Sstevel@tonic-gate 			zero = ozero;
15830Sstevel@tonic-gate 			compact();
15840Sstevel@tonic-gate 		}
15850Sstevel@tonic-gate 		diff = (int)((int)zero - (int)ozero);
15860Sstevel@tonic-gate 		dot = (LINE *)((int)dot + diff);
15870Sstevel@tonic-gate 		dol = (LINE *)((int)dol + diff);
15880Sstevel@tonic-gate 		contig = (LINE *)((int)contig + diff);
15890Sstevel@tonic-gate 	}
15900Sstevel@tonic-gate 	dol++;
15910Sstevel@tonic-gate 	if (!pipe_in)
15920Sstevel@tonic-gate 		dol->l_addr = (off_t)ftello(f);
15930Sstevel@tonic-gate 	else {
15940Sstevel@tonic-gate 		(void) fflush(tmp_fou);
15950Sstevel@tonic-gate 		dol->l_addr = (off_t)ftello(tmp_fou);
15960Sstevel@tonic-gate 	}
15970Sstevel@tonic-gate 	dol->l_no = (dol-1)->l_no + 1;
15980Sstevel@tonic-gate }
15990Sstevel@tonic-gate 
16000Sstevel@tonic-gate static void
compact()16010Sstevel@tonic-gate compact()
16020Sstevel@tonic-gate {
16030Sstevel@tonic-gate 	(void) perror("realloc");
16040Sstevel@tonic-gate 	end_it();
16050Sstevel@tonic-gate 
16060Sstevel@tonic-gate }
16070Sstevel@tonic-gate 
16080Sstevel@tonic-gate static void
terminit()16090Sstevel@tonic-gate terminit()	/* set up terminal dependencies from termlib */
16100Sstevel@tonic-gate {
16110Sstevel@tonic-gate 	int err_ret;
16120Sstevel@tonic-gate 	struct termio ntty;
16130Sstevel@tonic-gate 
16140Sstevel@tonic-gate 	for (;;) {
16154321Scasper 		pid_t my_tgid;
16164321Scasper 		my_tgid = tcgetpgrp(1);
16174321Scasper 		if (my_tgid == -1 || my_tgid == my_pgid)
16180Sstevel@tonic-gate 			break;
16190Sstevel@tonic-gate 		(void) kill(-my_pgid, SIGTTOU);
16200Sstevel@tonic-gate 	}
16210Sstevel@tonic-gate 
16220Sstevel@tonic-gate 	if ((freopen("/dev/tty", "r+", stdout)) == NULL) {
16230Sstevel@tonic-gate 		(void) perror("open");
16240Sstevel@tonic-gate 		exit(1);
16250Sstevel@tonic-gate 	}
16260Sstevel@tonic-gate 	(void) ioctl(fileno(stdout), TCGETA, &otty);
16270Sstevel@tonic-gate 	termflg = 1;
16280Sstevel@tonic-gate 
16290Sstevel@tonic-gate 	(void) setupterm(0, fileno(stdout), &err_ret);
16300Sstevel@tonic-gate 	(void) ioctl(fileno(stdout), TCGETA, &ntty);
16310Sstevel@tonic-gate 	ntty.c_lflag &= ~(ECHONL | ECHO | ICANON);
16320Sstevel@tonic-gate 	ntty.c_cc[VMIN] = 1;
16330Sstevel@tonic-gate 	ntty.c_cc[VTIME] = 1;
16340Sstevel@tonic-gate 	(void) ioctl(fileno(stdout), TCSETAW, &ntty);
16350Sstevel@tonic-gate 	pg_stdin = fdopen(dup(fileno(stdout)), "r");
16360Sstevel@tonic-gate 	(void) saveterm();
16370Sstevel@tonic-gate 	(void) resetterm();
16380Sstevel@tonic-gate 	if (lines <= 0 || hard_copy) {
16390Sstevel@tonic-gate 		hard_copy = 1;
16400Sstevel@tonic-gate 		lines = 24;
16410Sstevel@tonic-gate 	}
16420Sstevel@tonic-gate 	if (columns <= 0)
16430Sstevel@tonic-gate 		columns = 80;
16440Sstevel@tonic-gate 	if (clropt && !clear_screen)
16450Sstevel@tonic-gate 		clropt = 0;
16460Sstevel@tonic-gate 	if ((shell = getenv("SHELL")) == (char *)NULL)
16470Sstevel@tonic-gate 			shell = "/usr/bin/sh";
16480Sstevel@tonic-gate }
16490Sstevel@tonic-gate 
16500Sstevel@tonic-gate static void
error(mess)16510Sstevel@tonic-gate error(mess)
16520Sstevel@tonic-gate char *mess;
16530Sstevel@tonic-gate {
16540Sstevel@tonic-gate 	kill_line();
16550Sstevel@tonic-gate 	sopr(gettext(mess), 1);
16560Sstevel@tonic-gate 	prompt((char *)NULL);
16570Sstevel@tonic-gate 	errors++;
16580Sstevel@tonic-gate }
16590Sstevel@tonic-gate 
16600Sstevel@tonic-gate static void
prompt(filename)16610Sstevel@tonic-gate prompt(filename)
16620Sstevel@tonic-gate char *filename;
16630Sstevel@tonic-gate {
16640Sstevel@tonic-gate 	char outstr[PROMPTSIZE+6];
16650Sstevel@tonic-gate 	int pagenum;
16660Sstevel@tonic-gate 	if (filename != NULL) {
16670Sstevel@tonic-gate 		/*
16680Sstevel@tonic-gate 		 * TRANSLATION_NOTE
16690Sstevel@tonic-gate 		 * 	%s is a filename.
16700Sstevel@tonic-gate 		 */
16710Sstevel@tonic-gate 		(void) sprintf(outstr, gettext("(Next file: %s)"), filename);
16720Sstevel@tonic-gate 	} else {
16730Sstevel@tonic-gate 		if ((pagenum = (int)((new_ss.last_line-2)/(window-1)+1))
16740Sstevel@tonic-gate 						> 999999)
16750Sstevel@tonic-gate 			pagenum = 999999;
16760Sstevel@tonic-gate 		(void) sprintf(outstr, promptstr, pagenum);
16770Sstevel@tonic-gate 	}
16780Sstevel@tonic-gate 	sopr(outstr, 1);
16790Sstevel@tonic-gate 	(void) fflush(stdout);
16800Sstevel@tonic-gate }
16810Sstevel@tonic-gate 
16820Sstevel@tonic-gate /*
16830Sstevel@tonic-gate  *  sopr puts out the message (please no \n's) surrounded by standout
16840Sstevel@tonic-gate  *  begins and ends
16850Sstevel@tonic-gate  */
16860Sstevel@tonic-gate 
16870Sstevel@tonic-gate static void
sopr(m,count)16880Sstevel@tonic-gate sopr(m, count)
16890Sstevel@tonic-gate 	char *m;
16900Sstevel@tonic-gate 	int count;
16910Sstevel@tonic-gate {
16920Sstevel@tonic-gate 	wchar_t	wc;
16930Sstevel@tonic-gate 	int	len, n;
16940Sstevel@tonic-gate 	char	*p;
16950Sstevel@tonic-gate 
16960Sstevel@tonic-gate 	if (count) {
16970Sstevel@tonic-gate 		p = m;
16980Sstevel@tonic-gate 		for (; *p; p += len) {
16990Sstevel@tonic-gate 			if ((len = mbtowc(&wc, p, MB_CUR_MAX)) <= 0) {
17000Sstevel@tonic-gate 				len = 1;
17010Sstevel@tonic-gate 				continue;
17020Sstevel@tonic-gate 			}
17030Sstevel@tonic-gate 			if ((n = wcwidth(wc)) > 0)
17040Sstevel@tonic-gate 				promptlen += n;
17050Sstevel@tonic-gate 		}
17060Sstevel@tonic-gate 	}
17070Sstevel@tonic-gate 	if (soflag && enter_standout_mode && exit_standout_mode) {
17080Sstevel@tonic-gate 		(void) putp(enter_standout_mode);
17090Sstevel@tonic-gate 		(void) fputs(m, stdout);
17100Sstevel@tonic-gate 		(void) putp(exit_standout_mode);
17110Sstevel@tonic-gate 	}
17120Sstevel@tonic-gate 	else
17130Sstevel@tonic-gate 		(void) fputs(m, stdout);
17140Sstevel@tonic-gate }
17150Sstevel@tonic-gate 
17160Sstevel@tonic-gate static void
doclear()17170Sstevel@tonic-gate doclear()
17180Sstevel@tonic-gate {
17190Sstevel@tonic-gate 	if (clear_screen)
17200Sstevel@tonic-gate 		(void) putp(clear_screen);
17210Sstevel@tonic-gate 	(void) putchar('\r');  /* this resets the terminal drivers character */
17220Sstevel@tonic-gate 			/* count in case it is trying to expand tabs  */
17230Sstevel@tonic-gate 
17240Sstevel@tonic-gate }
17250Sstevel@tonic-gate 
17260Sstevel@tonic-gate static void
kill_line()17270Sstevel@tonic-gate kill_line()
17280Sstevel@tonic-gate {
17290Sstevel@tonic-gate 	erase_line(0);
17300Sstevel@tonic-gate 	if (!clr_eol) (void) putchar('\r');
17310Sstevel@tonic-gate 
17320Sstevel@tonic-gate }
17330Sstevel@tonic-gate 
17340Sstevel@tonic-gate /* erase from after col to end of prompt */
17350Sstevel@tonic-gate static void
erase_line(col)17360Sstevel@tonic-gate erase_line(col)
17370Sstevel@tonic-gate int col;
17380Sstevel@tonic-gate {
17390Sstevel@tonic-gate 
17400Sstevel@tonic-gate 	if (promptlen == 0)
17410Sstevel@tonic-gate 		return;
17420Sstevel@tonic-gate 	if (hard_copy)
17430Sstevel@tonic-gate 		(void) putchar('\n');
17440Sstevel@tonic-gate 	else {
17450Sstevel@tonic-gate 		if (col == 0)
17460Sstevel@tonic-gate 			(void) putchar('\r');
17470Sstevel@tonic-gate 		if (clr_eol) {
17480Sstevel@tonic-gate 			(void) putp(clr_eol);
17490Sstevel@tonic-gate 			/* for the terminal driver again */
17500Sstevel@tonic-gate 			(void) putchar('\r');
17510Sstevel@tonic-gate 		}
17520Sstevel@tonic-gate 		else
17530Sstevel@tonic-gate 			for (col = promptlen - col; col > 0; col--)
17540Sstevel@tonic-gate 				(void) putchar(' ');
17550Sstevel@tonic-gate 	}
17560Sstevel@tonic-gate 	promptlen = 0;
17570Sstevel@tonic-gate }
17580Sstevel@tonic-gate 
17590Sstevel@tonic-gate /*
17600Sstevel@tonic-gate  * Come here if a quit or interrupt signal is received
17610Sstevel@tonic-gate  */
17620Sstevel@tonic-gate 
17630Sstevel@tonic-gate static void
on_brk(sno)17640Sstevel@tonic-gate on_brk(sno)
17650Sstevel@tonic-gate 	int sno;	/* signal number generated */
17660Sstevel@tonic-gate {
17670Sstevel@tonic-gate 	(void) signal(sno, on_brk);
17680Sstevel@tonic-gate 	if (!inwait) {
17690Sstevel@tonic-gate 		BEEP();
17700Sstevel@tonic-gate 		brk_hit = 1;
17710Sstevel@tonic-gate 	} else {
17720Sstevel@tonic-gate 		brk_hit = 0;
17730Sstevel@tonic-gate 		longjmp(restore, 1);
17740Sstevel@tonic-gate 	}
17750Sstevel@tonic-gate }
17760Sstevel@tonic-gate 
17770Sstevel@tonic-gate /*
17780Sstevel@tonic-gate  * Clean up terminal state and exit.
17790Sstevel@tonic-gate  */
17800Sstevel@tonic-gate 
17810Sstevel@tonic-gate void
end_it()17820Sstevel@tonic-gate end_it()
17830Sstevel@tonic-gate {
17840Sstevel@tonic-gate 
17850Sstevel@tonic-gate 	if (out_is_tty) {
17860Sstevel@tonic-gate 		kill_line();
17870Sstevel@tonic-gate 		(void) resetterm();
17880Sstevel@tonic-gate 		if (termflg)
17890Sstevel@tonic-gate 			(void) ioctl(fileno(stdout), TCSETAW, &otty);
17900Sstevel@tonic-gate 	}
17910Sstevel@tonic-gate 	if (tmp_fin)
17920Sstevel@tonic-gate 		(void) fclose(tmp_fin);
17930Sstevel@tonic-gate 	if (tmp_fou)
17940Sstevel@tonic-gate 		(void) fclose(tmp_fou);
17950Sstevel@tonic-gate 	if (tmp_fou || tmp_fin)
17960Sstevel@tonic-gate 		(void) unlink(tmp_name);
17970Sstevel@tonic-gate 	exit(status);
17980Sstevel@tonic-gate }
17990Sstevel@tonic-gate 
18000Sstevel@tonic-gate void
onsusp()18010Sstevel@tonic-gate onsusp()
18020Sstevel@tonic-gate {
18030Sstevel@tonic-gate 	int ttou_is_dfl;
18040Sstevel@tonic-gate 
18050Sstevel@tonic-gate 	/* ignore SIGTTOU so following resetterm and flush works */
18060Sstevel@tonic-gate 	ttou_is_dfl = (signal(SIGTTOU, SIG_IGN) == SIG_DFL);
18070Sstevel@tonic-gate 	(void) resetterm();
18080Sstevel@tonic-gate 	(void) fflush(stdout);
18090Sstevel@tonic-gate 	if (ttou_is_dfl)
18100Sstevel@tonic-gate 		(void) signal(SIGTTOU, SIG_DFL);
18110Sstevel@tonic-gate 
18120Sstevel@tonic-gate 	/* send SIGTSTP to stop this process group */
18130Sstevel@tonic-gate 	(void) signal(SIGTSTP, SIG_DFL);
18140Sstevel@tonic-gate 	(void) kill(-my_pgid, SIGTSTP);
18150Sstevel@tonic-gate 
18160Sstevel@tonic-gate 	/* continued - reset the terminal */
18170Sstevel@tonic-gate #ifdef __STDC__
18180Sstevel@tonic-gate 	(void) signal(SIGTSTP, (void (*)(int))onsusp);
18190Sstevel@tonic-gate #else
18200Sstevel@tonic-gate 	(void) signal(SIGTSTP, (void (*))onsusp);
18210Sstevel@tonic-gate #endif
18220Sstevel@tonic-gate 	(void) resetterm();
18230Sstevel@tonic-gate 	if (inwait)
18240Sstevel@tonic-gate 		longjmp(restore, -1);
18250Sstevel@tonic-gate 
18260Sstevel@tonic-gate }
18270Sstevel@tonic-gate 
18280Sstevel@tonic-gate static char *
pg_strchr(str,c)18290Sstevel@tonic-gate pg_strchr(str, c)
18300Sstevel@tonic-gate char	*str;
18310Sstevel@tonic-gate wchar_t	c;
18320Sstevel@tonic-gate {
18330Sstevel@tonic-gate 	while (*str) {
18340Sstevel@tonic-gate 		if (c == *str)
18350Sstevel@tonic-gate 			return (str);
18360Sstevel@tonic-gate 		str++;
18370Sstevel@tonic-gate 	}
18380Sstevel@tonic-gate 	return (0);
18390Sstevel@tonic-gate }
18400Sstevel@tonic-gate 
18410Sstevel@tonic-gate void
usage()18420Sstevel@tonic-gate usage()
18430Sstevel@tonic-gate {
18440Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
18450Sstevel@tonic-gate "Usage: pg [-number] [-p string] [-cefnrs] [+line] [+/pattern/] files\n"));
18460Sstevel@tonic-gate 	exit(1);
18470Sstevel@tonic-gate }
1848