xref: /illumos-gate/usr/src/cmd/pg/pg.c (revision bc54f855)
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
5f48205beScasper  * Common Development and Distribution License (the "License").
6f48205beScasper  * 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) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
2448bbca81SDaniel Hoffman  * Copyright (c) 2016 by Delphix. All rights reserved.
25*bc54f855SJohn Levon  * Copyright (c) 2018, Joyent, Inc.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
284fce32e1Smuffin /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
294fce32e1Smuffin /*	  All Rights Reserved  	*/
304fce32e1Smuffin 
317c478bd9Sstevel@tonic-gate #include <signal.h>
327c478bd9Sstevel@tonic-gate #include <setjmp.h>
337c478bd9Sstevel@tonic-gate #include <sys/types.h>
347c478bd9Sstevel@tonic-gate #include <sys/dirent.h>
357c478bd9Sstevel@tonic-gate #include <sys/stat.h>
367c478bd9Sstevel@tonic-gate #include <fcntl.h>
377c478bd9Sstevel@tonic-gate #include <ctype.h>
387c478bd9Sstevel@tonic-gate #include <stdio.h>
397c478bd9Sstevel@tonic-gate #include <wchar.h>
407c478bd9Sstevel@tonic-gate #include <curses.h>
417c478bd9Sstevel@tonic-gate #include <term.h>
427c478bd9Sstevel@tonic-gate #include <errno.h>
437c478bd9Sstevel@tonic-gate #include <stdlib.h>
447c478bd9Sstevel@tonic-gate #include <regexpr.h>
457c478bd9Sstevel@tonic-gate #include <limits.h>
467c478bd9Sstevel@tonic-gate #include <locale.h>
477c478bd9Sstevel@tonic-gate #include <wctype.h> /* iswprint() */
487c478bd9Sstevel@tonic-gate #include <string.h>
497c478bd9Sstevel@tonic-gate #include <unistd.h>
507c478bd9Sstevel@tonic-gate #include <wait.h>
517c478bd9Sstevel@tonic-gate #include <libw.h>
527c478bd9Sstevel@tonic-gate #include <regexpr.h>
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate /*
567c478bd9Sstevel@tonic-gate  *	pg -- paginator for crt terminals
577c478bd9Sstevel@tonic-gate  *
587c478bd9Sstevel@tonic-gate  *	Includes the ability to display pages that have
597c478bd9Sstevel@tonic-gate  *	already passed by. Also gives the user the ability
607c478bd9Sstevel@tonic-gate  *	to search forward and backwards for regular expressions.
617c478bd9Sstevel@tonic-gate  *	This works for piped input by copying to a temporary file,
627c478bd9Sstevel@tonic-gate  *	and resolving backreferences from there.
637c478bd9Sstevel@tonic-gate  *
647c478bd9Sstevel@tonic-gate  *	Note:	The reason that there are so many commands to do
657c478bd9Sstevel@tonic-gate  *		the same types of things is to try to accommodate
667c478bd9Sstevel@tonic-gate  *		users of other paginators.
677c478bd9Sstevel@tonic-gate  */
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate #define	LINSIZ	1024
707c478bd9Sstevel@tonic-gate #define	QUIT	'\034'
717c478bd9Sstevel@tonic-gate #define	BOF	(EOF - 1)	/* Begining of File */
727c478bd9Sstevel@tonic-gate #define	STOP    (EOF - 2)
737c478bd9Sstevel@tonic-gate #define	PROMPTSIZE	256
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate /*
767c478bd9Sstevel@tonic-gate  * Function definitions
777c478bd9Sstevel@tonic-gate  */
787c478bd9Sstevel@tonic-gate static	void	lineset(int);
797c478bd9Sstevel@tonic-gate static	char	*setprompt();
807c478bd9Sstevel@tonic-gate static	int	set_state(int *, wchar_t, char *);
817c478bd9Sstevel@tonic-gate static	void	help();
827c478bd9Sstevel@tonic-gate static	void	copy_file(FILE *, FILE *);
837c478bd9Sstevel@tonic-gate static	void	re_error(int);
847c478bd9Sstevel@tonic-gate static	void	save_input(FILE *);
857c478bd9Sstevel@tonic-gate static	void	save_pipe();
867c478bd9Sstevel@tonic-gate static	void	newdol(FILE *);
877c478bd9Sstevel@tonic-gate static	void	erase_line(int);
887c478bd9Sstevel@tonic-gate static	void	kill_line();
897c478bd9Sstevel@tonic-gate static	void	doclear();
907c478bd9Sstevel@tonic-gate static	void	sopr(char *, int);
917c478bd9Sstevel@tonic-gate static	void	prompt(char *);
927c478bd9Sstevel@tonic-gate static	void	error(char *);
937c478bd9Sstevel@tonic-gate static	void	terminit();
947c478bd9Sstevel@tonic-gate static	void	compact();
9523a1cceaSRoger A. Faulkner static	off_t	getaline(FILE *);
967c478bd9Sstevel@tonic-gate static	int	mrdchar();
977c478bd9Sstevel@tonic-gate static	off_t	find(int, off_t);
987c478bd9Sstevel@tonic-gate static	int	search(char *, off_t);
997c478bd9Sstevel@tonic-gate static	FILE	*checkf(char *);
1007c478bd9Sstevel@tonic-gate static	int	skipf(int);
1017c478bd9Sstevel@tonic-gate static	int	readch();
1027c478bd9Sstevel@tonic-gate static	int	ttyin();
1037c478bd9Sstevel@tonic-gate static	int	number();
1047c478bd9Sstevel@tonic-gate static	int	command(char *);
1057c478bd9Sstevel@tonic-gate static	int	screen(char *);
1067c478bd9Sstevel@tonic-gate static	int	fgetputc();
1077c478bd9Sstevel@tonic-gate static 	char	*pg_strchr();
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate struct line {			/* how line addresses are stored */
1117c478bd9Sstevel@tonic-gate 	off_t	l_addr;		/* file offset */
1127c478bd9Sstevel@tonic-gate 	off_t	l_no;		/* line number in file */
1137c478bd9Sstevel@tonic-gate };
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate typedef	struct line	LINE;
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate static	LINE	*zero = NULL,	/* first line */
1187c478bd9Sstevel@tonic-gate 		*dot,		/* current line */
1197c478bd9Sstevel@tonic-gate 		*dol,		/* last line */
1207c478bd9Sstevel@tonic-gate 		*contig;	/* where contiguous (non-aged) lines start */
1217c478bd9Sstevel@tonic-gate static	long	nlall;		/* room for how many LINEs in memory */
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate static	FILE	*in_file,	/* current input stream */
1247c478bd9Sstevel@tonic-gate 		*tmp_fin,	/* pipe temporary file in */
1257c478bd9Sstevel@tonic-gate 		*tmp_fou;	/* pipe temporary file out */
1267c478bd9Sstevel@tonic-gate static	char	tmp_name[] = "/tmp/pgXXXXXX";
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate static	short	sign;		/* sign of command input */
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate static	int	fnum,		/* which file argument we're in */
1317c478bd9Sstevel@tonic-gate 		pipe_in,	/* set when stdin is a pipe */
1327c478bd9Sstevel@tonic-gate 		out_is_tty;	/* set if stdout is a tty */
133f48205beScasper static	pid_t	my_pgid;
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate static	void	on_brk(),
1367c478bd9Sstevel@tonic-gate 		end_it();
1377c478bd9Sstevel@tonic-gate static	short	brk_hit;	/* interrupt handling is pending flag */
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate static	int	window = 0;	/* window size in lines */
1407c478bd9Sstevel@tonic-gate static	short	eof_pause = 1;	/* pause w/ prompt at end of files */
1417c478bd9Sstevel@tonic-gate static	short	rmode = 0;	/* deny shell escape in restricted mode */
1427c478bd9Sstevel@tonic-gate static	short	soflag = 0;	/* output all messages in standout mode */
1437c478bd9Sstevel@tonic-gate static	short	promptlen;	/* length of the current prompt */
1447c478bd9Sstevel@tonic-gate static	short	firstf = 1;	/* set before first file has been processed */
1457c478bd9Sstevel@tonic-gate static	short	inwait,		/* set while waiting for user input */
1467c478bd9Sstevel@tonic-gate 		errors;		/* set if error message has been printed. */
1477c478bd9Sstevel@tonic-gate 				/* if so, need to erase it and prompt */
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate static	char	**fnames;
1507c478bd9Sstevel@tonic-gate static	short	status = 0;	/* set > 0 if error detected */
1517c478bd9Sstevel@tonic-gate static	short	fflag = 0;	/* set if the f option is used */
1527c478bd9Sstevel@tonic-gate static	short	nflag = 0;	/* set for "no newline" input option */
1537c478bd9Sstevel@tonic-gate static	short	clropt = 0;	/* set if the clear option is used */
1547c478bd9Sstevel@tonic-gate static	int	initopt = 0;	/* set if the line option is used */
1557c478bd9Sstevel@tonic-gate static	int	srchopt = 0;	/* set if the search option is used */
1567c478bd9Sstevel@tonic-gate static	int	initline;
1577c478bd9Sstevel@tonic-gate static	char	initbuf[BUFSIZ];
1587c478bd9Sstevel@tonic-gate static	wchar_t	leave_search = L't';
1597c478bd9Sstevel@tonic-gate 				/* where on the page to leave a found string */
1607c478bd9Sstevel@tonic-gate static	short	nfiles;
1617c478bd9Sstevel@tonic-gate static	char	*shell;
1627c478bd9Sstevel@tonic-gate static	char	*promptstr = ":";
16323a1cceaSRoger A. Faulkner static  off_t	nchars;			/* return from getaline in find() */
1647c478bd9Sstevel@tonic-gate static	jmp_buf	restore;
1657c478bd9Sstevel@tonic-gate static	char	Line[LINSIZ+2];
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate static	int	catch_susp;
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate static	void	onsusp();
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate struct screen_stat {
1727c478bd9Sstevel@tonic-gate 	off_t	first_line;
1737c478bd9Sstevel@tonic-gate 	off_t	last_line;
1747c478bd9Sstevel@tonic-gate 	short	is_eof;
1757c478bd9Sstevel@tonic-gate 	};
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate static	struct screen_stat old_ss = { 0, 0, 0 };
1787c478bd9Sstevel@tonic-gate static	struct screen_stat new_ss;
1797c478bd9Sstevel@tonic-gate static	struct termio otty;	/* to save old terminal settings */
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate static	short	termflg = 0;	/* set once terminal is initialized */
1827c478bd9Sstevel@tonic-gate static	short	eoflag;		/* set whenever at end of current file */
1837c478bd9Sstevel@tonic-gate static	short	doliseof;	/* set when last line of file is known */
1847c478bd9Sstevel@tonic-gate static	off_t	eofl_no;	/* what the last line of the file is */
1857c478bd9Sstevel@tonic-gate static	void	usage(void);
1867c478bd9Sstevel@tonic-gate static FILE	*pg_stdin;
1877c478bd9Sstevel@tonic-gate 
1884fce32e1Smuffin int
main(int argc,char ** argv)1894fce32e1Smuffin main(int argc, char **argv)
1907c478bd9Sstevel@tonic-gate {
1917c478bd9Sstevel@tonic-gate 	char	*s;
1927c478bd9Sstevel@tonic-gate 	char	*p;
1937c478bd9Sstevel@tonic-gate 	int		prnames = 0;
1947c478bd9Sstevel@tonic-gate 	int		opt;
1957c478bd9Sstevel@tonic-gate 	int		i;
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1987c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
1997c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
2007c478bd9Sstevel@tonic-gate #endif
2017c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	/* check for non-standard "-#" option */
2047c478bd9Sstevel@tonic-gate 	for (i = 1; i < argc; i++) {
2057c478bd9Sstevel@tonic-gate 		if (strcmp(argv[i], "--") == 0)
2067c478bd9Sstevel@tonic-gate 			break;
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 		if ((argv[i][0] == '-') && isdigit(argv[i][1])) {
2097c478bd9Sstevel@tonic-gate 			if (strlen(&argv[i][1]) !=
2107c478bd9Sstevel@tonic-gate 			    strspn(&argv[i][1], "0123456789")) {
2117c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
2127c478bd9Sstevel@tonic-gate 				    "pg: Badly formed number\n"));
2137c478bd9Sstevel@tonic-gate 				usage();
2147c478bd9Sstevel@tonic-gate 			}
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 			window = (int)strtol(&argv[i][1], (char **)NULL, 10);
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 			while (i < argc) {
2197c478bd9Sstevel@tonic-gate 				argv[i] = argv[i + 1];
2207c478bd9Sstevel@tonic-gate 				i++;
2217c478bd9Sstevel@tonic-gate 			}
2227c478bd9Sstevel@tonic-gate 			i--;
2237c478bd9Sstevel@tonic-gate 			argc--;
2247c478bd9Sstevel@tonic-gate 		}
2257c478bd9Sstevel@tonic-gate 	}
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 	/* check for non-standard + option */
2287c478bd9Sstevel@tonic-gate 	for (i = 1; i < argc; i++) {
2297c478bd9Sstevel@tonic-gate 		if (strcmp(argv[i], "--") == 0)
230*bc54f855SJohn Levon 			break;
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 		if (argv[i][0] == '+') {
2337c478bd9Sstevel@tonic-gate 			if (argv[i][1] == '/') {
2347c478bd9Sstevel@tonic-gate 				srchopt++;
2357c478bd9Sstevel@tonic-gate 				initopt = 0;
2367c478bd9Sstevel@tonic-gate 				for (s = &argv[i][2], p = initbuf; *s != '\0'; )
2377c478bd9Sstevel@tonic-gate 					if (p < initbuf + sizeof (initbuf))
2387c478bd9Sstevel@tonic-gate 						*p++ = *s++;
2397c478bd9Sstevel@tonic-gate 					else {
2407c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr, gettext(
2417c478bd9Sstevel@tonic-gate 						    "pg: pattern too long\n"));
2427c478bd9Sstevel@tonic-gate 						return (1);
2437c478bd9Sstevel@tonic-gate 					}
2447c478bd9Sstevel@tonic-gate 				*p = '\0';
2457c478bd9Sstevel@tonic-gate 			} else {
2467c478bd9Sstevel@tonic-gate 				initopt++;
2477c478bd9Sstevel@tonic-gate 				srchopt = 0;
2487c478bd9Sstevel@tonic-gate 				s = &argv[i][2];
2497c478bd9Sstevel@tonic-gate 				for (; isdigit(*s); s++)
2507c478bd9Sstevel@tonic-gate 					initline = initline*10 + *s -'0';
2517c478bd9Sstevel@tonic-gate 				if (*s != '\0')
2527c478bd9Sstevel@tonic-gate 					usage();
2537c478bd9Sstevel@tonic-gate 			}
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 			while (i < argc) {
2567c478bd9Sstevel@tonic-gate 				argv[i] = argv[i + 1];
2577c478bd9Sstevel@tonic-gate 				i++;
2587c478bd9Sstevel@tonic-gate 			}
2597c478bd9Sstevel@tonic-gate 			i--;
2607c478bd9Sstevel@tonic-gate 			argc--;
2617c478bd9Sstevel@tonic-gate 		}
2627c478bd9Sstevel@tonic-gate 	}
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	while ((opt = getopt(argc, argv, "cefnrsp:")) != EOF) {
2657c478bd9Sstevel@tonic-gate 		switch (opt) {
2667c478bd9Sstevel@tonic-gate 		case 'c':
2677c478bd9Sstevel@tonic-gate 			clropt = 1;
2687c478bd9Sstevel@tonic-gate 			break;
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 		case 'e':
2717c478bd9Sstevel@tonic-gate 			eof_pause = 0;
2727c478bd9Sstevel@tonic-gate 			break;
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 		case 'f':
2757c478bd9Sstevel@tonic-gate 			fflag = 1;
2767c478bd9Sstevel@tonic-gate 			break;
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 		case 'n':
2797c478bd9Sstevel@tonic-gate 			nflag = 1;
2807c478bd9Sstevel@tonic-gate 			break;
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 		case 'r':
2837c478bd9Sstevel@tonic-gate 			rmode = 1;	/* restricted mode */
2847c478bd9Sstevel@tonic-gate 			break;
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 		case 's':
2877c478bd9Sstevel@tonic-gate 			soflag = 1;	/* standout mode */
2887c478bd9Sstevel@tonic-gate 			break;
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 		case 'p':
2917c478bd9Sstevel@tonic-gate 			promptstr = setprompt(optarg);
2927c478bd9Sstevel@tonic-gate 			break;
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 		default:
2957c478bd9Sstevel@tonic-gate 			usage();
2967c478bd9Sstevel@tonic-gate 		}
2977c478bd9Sstevel@tonic-gate 	}
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	nfiles = argc - optind;
3007c478bd9Sstevel@tonic-gate 	fnames = &argv[optind];
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 	(void) signal(SIGQUIT, end_it);
3037c478bd9Sstevel@tonic-gate 	(void) signal(SIGINT, end_it);
3047c478bd9Sstevel@tonic-gate 	out_is_tty = isatty(1);
3057c478bd9Sstevel@tonic-gate 	my_pgid = getpgrp();
3067c478bd9Sstevel@tonic-gate 	if (out_is_tty) {
3077c478bd9Sstevel@tonic-gate 		terminit();
3087c478bd9Sstevel@tonic-gate 		(void) signal(SIGQUIT, on_brk);
3097c478bd9Sstevel@tonic-gate 		(void) signal(SIGINT, on_brk);
3107c478bd9Sstevel@tonic-gate 		if (signal(SIGTSTP, SIG_IGN) == SIG_DFL) {
3117c478bd9Sstevel@tonic-gate 			(void) signal(SIGTSTP, onsusp);
3127c478bd9Sstevel@tonic-gate 			catch_susp++;
3137c478bd9Sstevel@tonic-gate 		}
3147c478bd9Sstevel@tonic-gate 	}
3157c478bd9Sstevel@tonic-gate 	if (window == 0)
3167c478bd9Sstevel@tonic-gate 		window = lines - 1;
3177c478bd9Sstevel@tonic-gate 	if (window <= 1)
3187c478bd9Sstevel@tonic-gate 		window = 2;
3197c478bd9Sstevel@tonic-gate 	if (initline <= 0)
3207c478bd9Sstevel@tonic-gate 		initline = 1;
3217c478bd9Sstevel@tonic-gate 	if (nfiles > 1)
3227c478bd9Sstevel@tonic-gate 		prnames++;
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 	if (nfiles == 0) {
3257c478bd9Sstevel@tonic-gate 		fnames[0] = "-";
3267c478bd9Sstevel@tonic-gate 		nfiles++;
3277c478bd9Sstevel@tonic-gate 	}
3287c478bd9Sstevel@tonic-gate 	while (fnum < nfiles) {
3297c478bd9Sstevel@tonic-gate 		if (strcmp(fnames[fnum], "") == 0)
3307c478bd9Sstevel@tonic-gate 			fnames[fnum] = "-";
3314fce32e1Smuffin 		if ((in_file = checkf(fnames[fnum])) == NULL) {
3327c478bd9Sstevel@tonic-gate 			status = 2;
3337c478bd9Sstevel@tonic-gate 			fnum++;
3347c478bd9Sstevel@tonic-gate 		} else {
3357c478bd9Sstevel@tonic-gate 			status = 0;
3367c478bd9Sstevel@tonic-gate 			if (out_is_tty)
3377c478bd9Sstevel@tonic-gate 				fnum += screen(fnames[fnum]);
3387c478bd9Sstevel@tonic-gate 			else {
3397c478bd9Sstevel@tonic-gate 				if (prnames) {
3407c478bd9Sstevel@tonic-gate 					(void) fputs("::::::::::::::\n",
3417c478bd9Sstevel@tonic-gate 					    stdout);
3427c478bd9Sstevel@tonic-gate 					(void) fputs(fnames[fnum], stdout);
3437c478bd9Sstevel@tonic-gate 					(void) fputs("\n::::::::::::::\n",
3447c478bd9Sstevel@tonic-gate 					    stdout);
3457c478bd9Sstevel@tonic-gate 				}
3467c478bd9Sstevel@tonic-gate 				copy_file(in_file, stdout);
3477c478bd9Sstevel@tonic-gate 				fnum++;
3487c478bd9Sstevel@tonic-gate 			}
3497c478bd9Sstevel@tonic-gate 			(void) fflush(stdout);
3507c478bd9Sstevel@tonic-gate 			if (pipe_in)
3517c478bd9Sstevel@tonic-gate 				save_pipe();
3527c478bd9Sstevel@tonic-gate 			else
3537c478bd9Sstevel@tonic-gate 			if (in_file != tmp_fin)
3547c478bd9Sstevel@tonic-gate 				(void) fclose(in_file);
3557c478bd9Sstevel@tonic-gate 		}
3567c478bd9Sstevel@tonic-gate 	}
3577c478bd9Sstevel@tonic-gate 	end_it();
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
3607c478bd9Sstevel@tonic-gate 	return (0);
3617c478bd9Sstevel@tonic-gate }
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate static	char *
setprompt(char * s)36450fc488bSToomas Soome setprompt(char *s)
3657c478bd9Sstevel@tonic-gate {
3667c478bd9Sstevel@tonic-gate 	int i = 0;
3677c478bd9Sstevel@tonic-gate 	int pct_d = 0;
3687c478bd9Sstevel@tonic-gate 	static char pstr[PROMPTSIZE];
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 	while (i < PROMPTSIZE - 2)
3717c478bd9Sstevel@tonic-gate 		switch (pstr[i++] = *s++) {
3727c478bd9Sstevel@tonic-gate 		case '\0':
3737c478bd9Sstevel@tonic-gate 			return (pstr);
3747c478bd9Sstevel@tonic-gate 		case '%':
3757c478bd9Sstevel@tonic-gate 			if (*s == 'd' && !pct_d) {
3767c478bd9Sstevel@tonic-gate 				pct_d++;
3777c478bd9Sstevel@tonic-gate 			} else if (*s != '%')
3787c478bd9Sstevel@tonic-gate 				pstr[i++] = '%';
3797c478bd9Sstevel@tonic-gate 			if ((pstr[i++] = *s++) == '\0')
3807c478bd9Sstevel@tonic-gate 				return (pstr);
3817c478bd9Sstevel@tonic-gate 			break;
3827c478bd9Sstevel@tonic-gate 		default:
3837c478bd9Sstevel@tonic-gate 			break;
3847c478bd9Sstevel@tonic-gate 		}
3857c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("pg: prompt too long\n"));
3867c478bd9Sstevel@tonic-gate 	exit(1);
3877c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
3887c478bd9Sstevel@tonic-gate }
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate /*
3927c478bd9Sstevel@tonic-gate  * Print out the contents of the file f, one screenful at a time.
3937c478bd9Sstevel@tonic-gate  */
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate static int
screen(char * file_name)39650fc488bSToomas Soome screen(char *file_name)
3977c478bd9Sstevel@tonic-gate {
3987c478bd9Sstevel@tonic-gate 	int cmd_ret = 0;
3997c478bd9Sstevel@tonic-gate 	off_t start;
4007c478bd9Sstevel@tonic-gate 	short hadchance = 0;
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 	old_ss.is_eof = 0;
4037c478bd9Sstevel@tonic-gate 	old_ss.first_line = 0;
4047c478bd9Sstevel@tonic-gate 	old_ss.last_line = 0;
4057c478bd9Sstevel@tonic-gate 	new_ss = old_ss;
4067c478bd9Sstevel@tonic-gate 	if (!firstf)
4077c478bd9Sstevel@tonic-gate 		cmd_ret = command(file_name);
4087c478bd9Sstevel@tonic-gate 	else {
4097c478bd9Sstevel@tonic-gate 		firstf = 0;
4107c478bd9Sstevel@tonic-gate 		if (initopt) {
4117c478bd9Sstevel@tonic-gate 			initopt = 0;
4127c478bd9Sstevel@tonic-gate 			new_ss.first_line = initline;
4137c478bd9Sstevel@tonic-gate 			new_ss.last_line = initline + (off_t)window - 1;
4147c478bd9Sstevel@tonic-gate 		} else if (srchopt) {
4157c478bd9Sstevel@tonic-gate 			srchopt = 0;
4167c478bd9Sstevel@tonic-gate 			if (!search(initbuf, (off_t)1))
4177c478bd9Sstevel@tonic-gate 				cmd_ret = command(file_name);
4187c478bd9Sstevel@tonic-gate 		} else {
4197c478bd9Sstevel@tonic-gate 			new_ss.first_line = 1;
4207c478bd9Sstevel@tonic-gate 			new_ss.last_line = (off_t)window;
4217c478bd9Sstevel@tonic-gate 		}
4227c478bd9Sstevel@tonic-gate 	}
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate 	for (;;) {
4257c478bd9Sstevel@tonic-gate 		if (cmd_ret)
4267c478bd9Sstevel@tonic-gate 			return (cmd_ret);
4277c478bd9Sstevel@tonic-gate 		if (hadchance && new_ss.last_line >= eofl_no)
4287c478bd9Sstevel@tonic-gate 			return (1);
4297c478bd9Sstevel@tonic-gate 		hadchance = 0;
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 		if (new_ss.last_line < (off_t)window)
4327c478bd9Sstevel@tonic-gate 			new_ss.last_line = (off_t)window;
4337c478bd9Sstevel@tonic-gate 		if (find(0, new_ss.last_line + 1) != EOF)
4347c478bd9Sstevel@tonic-gate 			new_ss.is_eof = 0;
4357c478bd9Sstevel@tonic-gate 		else {
4367c478bd9Sstevel@tonic-gate 			new_ss.is_eof = 1;
4377c478bd9Sstevel@tonic-gate 			new_ss.last_line = eofl_no - 1;
4387c478bd9Sstevel@tonic-gate 			new_ss.first_line = new_ss.last_line -
4397c478bd9Sstevel@tonic-gate 			    (off_t)window + 1;
4407c478bd9Sstevel@tonic-gate 		}
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 		if (new_ss.first_line < 1)
4437c478bd9Sstevel@tonic-gate 			new_ss.first_line = 1;
4447c478bd9Sstevel@tonic-gate 		if (clropt) {
4457c478bd9Sstevel@tonic-gate 			doclear();
4467c478bd9Sstevel@tonic-gate 			start = new_ss.first_line;
4477c478bd9Sstevel@tonic-gate 		} else {
4487c478bd9Sstevel@tonic-gate 			if (new_ss.first_line == old_ss.last_line)
4497c478bd9Sstevel@tonic-gate 				start = new_ss.first_line + 1;
4507c478bd9Sstevel@tonic-gate 			else
4517c478bd9Sstevel@tonic-gate 			if (new_ss.first_line > old_ss.last_line)
4527c478bd9Sstevel@tonic-gate 				start = new_ss.first_line;
4537c478bd9Sstevel@tonic-gate 			else
4547c478bd9Sstevel@tonic-gate 			if (old_ss.first_line < new_ss.first_line)
4557c478bd9Sstevel@tonic-gate 				start = old_ss.last_line + 1;
4567c478bd9Sstevel@tonic-gate 			else
4577c478bd9Sstevel@tonic-gate 				start = new_ss.first_line;
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 			if (start < old_ss.first_line)
4607c478bd9Sstevel@tonic-gate 				sopr(gettext("...skipping backward\n"), 0);
4617c478bd9Sstevel@tonic-gate 			else
4627c478bd9Sstevel@tonic-gate 			if (start > old_ss.last_line + 1)
4637c478bd9Sstevel@tonic-gate 				sopr(gettext("...skipping forward\n"), 0);
4647c478bd9Sstevel@tonic-gate 		}
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 		for (; start <= new_ss.last_line; start++) {
4677c478bd9Sstevel@tonic-gate 			(void) find(0, start);
4687c478bd9Sstevel@tonic-gate 			(void) fputs(Line, stdout);
4697c478bd9Sstevel@tonic-gate 			if (brk_hit) {
4707c478bd9Sstevel@tonic-gate 				new_ss.last_line = find(1, 0);
4717c478bd9Sstevel@tonic-gate 				new_ss.is_eof = 0;
4727c478bd9Sstevel@tonic-gate 				break;
4737c478bd9Sstevel@tonic-gate 			}
4747c478bd9Sstevel@tonic-gate 		}
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 		brk_hit = 0;
4777c478bd9Sstevel@tonic-gate 		(void) fflush(stdout);
4787c478bd9Sstevel@tonic-gate 		if (new_ss.is_eof) {
4797c478bd9Sstevel@tonic-gate 			if (!eof_pause || eofl_no == 1)
4807c478bd9Sstevel@tonic-gate 				return (1);
4817c478bd9Sstevel@tonic-gate 			hadchance++;
4827c478bd9Sstevel@tonic-gate 			error("(EOF)");
4837c478bd9Sstevel@tonic-gate 		}
4847c478bd9Sstevel@tonic-gate 		old_ss = new_ss;
4857c478bd9Sstevel@tonic-gate 		cmd_ret = command((char *)NULL);
4867c478bd9Sstevel@tonic-gate 	}
4877c478bd9Sstevel@tonic-gate }
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate static	char	cmdbuf[LINSIZ], *cmdptr;
4907c478bd9Sstevel@tonic-gate #define	BEEP()		if (bell) { (void) putp(bell); (void) fflush(stdout); }
4917c478bd9Sstevel@tonic-gate #define	BLANKS(p)	while (*p == ' ' || *p == '\t') p++
4927c478bd9Sstevel@tonic-gate #define	CHECKEND()	BLANKS(cmdptr); if (*cmdptr) { BEEP(); break; }
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate /*
4957c478bd9Sstevel@tonic-gate  * Read a command and do it. A command consists of an optional integer
4967c478bd9Sstevel@tonic-gate  * argument followed by the command character.  Return the number of files
4977c478bd9Sstevel@tonic-gate  * to skip, 0 if we're still talking about the same file.
4987c478bd9Sstevel@tonic-gate  */
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate static int
command(char * filename)50150fc488bSToomas Soome command(char *filename)
5027c478bd9Sstevel@tonic-gate {
5037c478bd9Sstevel@tonic-gate 	off_t nlines;
5047c478bd9Sstevel@tonic-gate 	FILE *sf;
5057c478bd9Sstevel@tonic-gate 	char *cmdend;
5067c478bd9Sstevel@tonic-gate 	pid_t id;
5077c478bd9Sstevel@tonic-gate 	int skip;
5087c478bd9Sstevel@tonic-gate 	int	len;
5097c478bd9Sstevel@tonic-gate 	wchar_t	wc;
5107c478bd9Sstevel@tonic-gate 	wchar_t	wc_e;
5117c478bd9Sstevel@tonic-gate 	wchar_t	wc_e1;
5127c478bd9Sstevel@tonic-gate 	char	*p;
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 	for (;;) {
5157c478bd9Sstevel@tonic-gate 		/*
5167c478bd9Sstevel@tonic-gate 		 * Wait for output to drain before going on.
5177c478bd9Sstevel@tonic-gate 		 * This is done so that the user will not hit
51848bbca81SDaniel Hoffman 		 * break and quit before they have seen the prompt.
5197c478bd9Sstevel@tonic-gate 		 */
5207c478bd9Sstevel@tonic-gate 		(void) ioctl(1, TCSBRK, 1);
5217c478bd9Sstevel@tonic-gate 		if (setjmp(restore) > 0)
5227c478bd9Sstevel@tonic-gate 			end_it();
5237c478bd9Sstevel@tonic-gate 		inwait = 1;
5247c478bd9Sstevel@tonic-gate 		brk_hit = 0;
5257c478bd9Sstevel@tonic-gate 		if (errors)
5267c478bd9Sstevel@tonic-gate 			errors = 0;
5277c478bd9Sstevel@tonic-gate 		else {
5287c478bd9Sstevel@tonic-gate 			kill_line();
5297c478bd9Sstevel@tonic-gate 			prompt(filename);
5307c478bd9Sstevel@tonic-gate 		}
5317c478bd9Sstevel@tonic-gate 		(void) fflush(stdout);
5327c478bd9Sstevel@tonic-gate 		if (ttyin())
5337c478bd9Sstevel@tonic-gate 			continue;
5347c478bd9Sstevel@tonic-gate 		cmdptr = cmdbuf;
5357c478bd9Sstevel@tonic-gate 		nlines = number();
5367c478bd9Sstevel@tonic-gate 		BLANKS(cmdptr);
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 		if ((len = mbtowc(&wc, cmdptr, MB_CUR_MAX)) <= 0) {
5397c478bd9Sstevel@tonic-gate 			wc = *cmdptr;
5407c478bd9Sstevel@tonic-gate 			len = 1;
5417c478bd9Sstevel@tonic-gate 		}
5427c478bd9Sstevel@tonic-gate 		cmdptr += len;
5437c478bd9Sstevel@tonic-gate 		switch (wc) {
5447c478bd9Sstevel@tonic-gate 		case 'h':
5457c478bd9Sstevel@tonic-gate 			CHECKEND();
5467c478bd9Sstevel@tonic-gate 			help();
5477c478bd9Sstevel@tonic-gate 			break;
5487c478bd9Sstevel@tonic-gate 		case '\014': /* ^L */
5497c478bd9Sstevel@tonic-gate 		case '.':	/* redisplay current window */
5507c478bd9Sstevel@tonic-gate 			CHECKEND();
5517c478bd9Sstevel@tonic-gate 			new_ss.first_line = old_ss.first_line;
5527c478bd9Sstevel@tonic-gate 			new_ss.last_line = old_ss.last_line;
5537c478bd9Sstevel@tonic-gate 			inwait = 0;
5547c478bd9Sstevel@tonic-gate 			return (0);
5557c478bd9Sstevel@tonic-gate 		case 'w':	/* set window size */
5567c478bd9Sstevel@tonic-gate 		case 'z':
5577c478bd9Sstevel@tonic-gate 			if (sign == -1) {
5587c478bd9Sstevel@tonic-gate 				BEEP();
5597c478bd9Sstevel@tonic-gate 				break;
5607c478bd9Sstevel@tonic-gate 			}
5617c478bd9Sstevel@tonic-gate 			CHECKEND();
5627c478bd9Sstevel@tonic-gate 			if (nlines == 0)
5637c478bd9Sstevel@tonic-gate 				nlines = (off_t)window;
5647c478bd9Sstevel@tonic-gate 			else
5657c478bd9Sstevel@tonic-gate 			if (nlines > 1)
5667c478bd9Sstevel@tonic-gate 				window = (int)nlines;
5677c478bd9Sstevel@tonic-gate 			else {
5687c478bd9Sstevel@tonic-gate 				BEEP();
5697c478bd9Sstevel@tonic-gate 				break;
5707c478bd9Sstevel@tonic-gate 			}
5717c478bd9Sstevel@tonic-gate 			new_ss.first_line = old_ss.last_line;
5727c478bd9Sstevel@tonic-gate 			new_ss.last_line = new_ss.first_line +
5737c478bd9Sstevel@tonic-gate 			    (off_t)window - 1;
5747c478bd9Sstevel@tonic-gate 			inwait = 0;
5757c478bd9Sstevel@tonic-gate 			return (0);
5767c478bd9Sstevel@tonic-gate 		case '\004': /* ^D */
5777c478bd9Sstevel@tonic-gate 		case 'd':
5787c478bd9Sstevel@tonic-gate 			CHECKEND();
5797c478bd9Sstevel@tonic-gate 			if (sign == 0)
5807c478bd9Sstevel@tonic-gate 				sign = 1;
5817c478bd9Sstevel@tonic-gate 			new_ss.last_line = old_ss.last_line +
5827c478bd9Sstevel@tonic-gate 			    (off_t)sign*window/2;
5837c478bd9Sstevel@tonic-gate 			new_ss.first_line = new_ss.last_line -
5847c478bd9Sstevel@tonic-gate 			    (off_t)window + 1;
5857c478bd9Sstevel@tonic-gate 			inwait = 0;
5867c478bd9Sstevel@tonic-gate 			return (0);
5877c478bd9Sstevel@tonic-gate 		case 's':
5887c478bd9Sstevel@tonic-gate 			/*
5897c478bd9Sstevel@tonic-gate 			 * save input in filename.
5907c478bd9Sstevel@tonic-gate 			 * Check for filename, access, etc.
5917c478bd9Sstevel@tonic-gate 			 */
5927c478bd9Sstevel@tonic-gate 			BLANKS(cmdptr);
5937c478bd9Sstevel@tonic-gate 			if (!*cmdptr) {
5947c478bd9Sstevel@tonic-gate 				BEEP();
5957c478bd9Sstevel@tonic-gate 				break;
5967c478bd9Sstevel@tonic-gate 			}
5977c478bd9Sstevel@tonic-gate 			if (setjmp(restore) > 0) {
5987c478bd9Sstevel@tonic-gate 				BEEP();
5997c478bd9Sstevel@tonic-gate 			} else {
6007c478bd9Sstevel@tonic-gate 				char outstr[PROMPTSIZE];
6017c478bd9Sstevel@tonic-gate 				if ((sf = fopen(cmdptr, "w")) == NULL) {
6027c478bd9Sstevel@tonic-gate 					error("cannot open save file");
6037c478bd9Sstevel@tonic-gate 					break;
6047c478bd9Sstevel@tonic-gate 				}
6057c478bd9Sstevel@tonic-gate 				kill_line();
6067c478bd9Sstevel@tonic-gate 				(void) sprintf(outstr, gettext(
6077c478bd9Sstevel@tonic-gate 				    "saving file %s"), cmdptr);
6087c478bd9Sstevel@tonic-gate 				sopr(outstr, 1);
6097c478bd9Sstevel@tonic-gate 				(void) fflush(stdout);
6107c478bd9Sstevel@tonic-gate 				save_input(sf);
6117c478bd9Sstevel@tonic-gate 				error("saved");
6127c478bd9Sstevel@tonic-gate 			}
6137c478bd9Sstevel@tonic-gate 			(void) fclose(sf);
6147c478bd9Sstevel@tonic-gate 			break;
6157c478bd9Sstevel@tonic-gate 		case 'q':
6167c478bd9Sstevel@tonic-gate 		case 'Q':
6177c478bd9Sstevel@tonic-gate 			CHECKEND();
6187c478bd9Sstevel@tonic-gate 			inwait = 0;
6197c478bd9Sstevel@tonic-gate 			end_it();
6207c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate 		case 'f':	/* skip forward screenfuls */
6237c478bd9Sstevel@tonic-gate 			CHECKEND();
6247c478bd9Sstevel@tonic-gate 			if (sign == 0)
6257c478bd9Sstevel@tonic-gate 				sign++;	/* skips are always relative */
6267c478bd9Sstevel@tonic-gate 			if (nlines == 0)
6277c478bd9Sstevel@tonic-gate 				nlines++;
6287c478bd9Sstevel@tonic-gate 			nlines = nlines * (window - 1);
6297c478bd9Sstevel@tonic-gate 			if (sign == 1)
6307c478bd9Sstevel@tonic-gate 				new_ss.first_line = old_ss.last_line + nlines;
6317c478bd9Sstevel@tonic-gate 			else
6327c478bd9Sstevel@tonic-gate 				new_ss.first_line = old_ss.first_line - nlines;
6337c478bd9Sstevel@tonic-gate 			new_ss.last_line = new_ss.first_line +
6347c478bd9Sstevel@tonic-gate 			    (off_t)window - 1;
6357c478bd9Sstevel@tonic-gate 			inwait = 0;
6367c478bd9Sstevel@tonic-gate 			return (0);
6377c478bd9Sstevel@tonic-gate 		case 'l':	/* get a line */
6387c478bd9Sstevel@tonic-gate 			CHECKEND();
6397c478bd9Sstevel@tonic-gate 			if (nlines == 0) {
6407c478bd9Sstevel@tonic-gate 				nlines++;
6417c478bd9Sstevel@tonic-gate 				if (sign == 0)
6427c478bd9Sstevel@tonic-gate 					sign = 1;
6437c478bd9Sstevel@tonic-gate 			}
6447c478bd9Sstevel@tonic-gate 			switch (sign) {
6457c478bd9Sstevel@tonic-gate 			case 1:
6467c478bd9Sstevel@tonic-gate 				new_ss.last_line = old_ss.last_line + nlines;
6477c478bd9Sstevel@tonic-gate 				new_ss.first_line =
6487c478bd9Sstevel@tonic-gate 				    new_ss.last_line - (off_t)window + 1;
6497c478bd9Sstevel@tonic-gate 				break;
6507c478bd9Sstevel@tonic-gate 			case 0:  /* leave addressed line at top */
6517c478bd9Sstevel@tonic-gate 				new_ss.first_line = nlines;
6527c478bd9Sstevel@tonic-gate 				new_ss.last_line = nlines + (off_t)window - 1;
6537c478bd9Sstevel@tonic-gate 				break;
6547c478bd9Sstevel@tonic-gate 			case -1:
6557c478bd9Sstevel@tonic-gate 				new_ss.first_line = old_ss.first_line - nlines;
6567c478bd9Sstevel@tonic-gate 				new_ss.last_line =
6577c478bd9Sstevel@tonic-gate 				    new_ss.first_line + (off_t)window - 1;
6587c478bd9Sstevel@tonic-gate 				break;
6597c478bd9Sstevel@tonic-gate 			}
6607c478bd9Sstevel@tonic-gate 			inwait = 0;
6617c478bd9Sstevel@tonic-gate 			return (0);
6627c478bd9Sstevel@tonic-gate 		case '\0': /* \n or blank */
6637c478bd9Sstevel@tonic-gate 			if (nlines == 0) {
6647c478bd9Sstevel@tonic-gate 				nlines++;
6657c478bd9Sstevel@tonic-gate 				if (sign == 0)
6667c478bd9Sstevel@tonic-gate 					sign = 1;
6677c478bd9Sstevel@tonic-gate 			}
6687c478bd9Sstevel@tonic-gate 			nlines = (nlines - 1) * (window - 1);
6697c478bd9Sstevel@tonic-gate 			switch (sign) {
6707c478bd9Sstevel@tonic-gate 			case 1:
6717c478bd9Sstevel@tonic-gate 				new_ss.first_line = old_ss.last_line + nlines;
6727c478bd9Sstevel@tonic-gate 				new_ss.last_line =
6737c478bd9Sstevel@tonic-gate 				    new_ss.first_line + (off_t)window - 1;
6747c478bd9Sstevel@tonic-gate 				break;
6757c478bd9Sstevel@tonic-gate 			case 0:
6767c478bd9Sstevel@tonic-gate 				new_ss.first_line = nlines + 1;
6777c478bd9Sstevel@tonic-gate 				new_ss.last_line = nlines + (off_t)window;
6787c478bd9Sstevel@tonic-gate 				/*
6797c478bd9Sstevel@tonic-gate 				 * This if statement is to fix the obscure bug
6807c478bd9Sstevel@tonic-gate 				 * where you have a file that has less lines
6817c478bd9Sstevel@tonic-gate 				 * than a screen holds, and the user types '1',
6827c478bd9Sstevel@tonic-gate 				 * expecting to have the 1st page (re)displayed.
6837c478bd9Sstevel@tonic-gate 				 * If we didn't set the new last_line to
6847c478bd9Sstevel@tonic-gate 				 * eofl_no-1, the screen() routine
6857c478bd9Sstevel@tonic-gate 				 * would cause pg to exit.
6867c478bd9Sstevel@tonic-gate 				 */
6877c478bd9Sstevel@tonic-gate 				if (new_ss.first_line == 1 &&
6887c478bd9Sstevel@tonic-gate 				    new_ss.last_line >= eofl_no)
6897c478bd9Sstevel@tonic-gate 					new_ss.last_line = eofl_no - 1;
6907c478bd9Sstevel@tonic-gate 				break;
6917c478bd9Sstevel@tonic-gate 			case -1:
6927c478bd9Sstevel@tonic-gate 				new_ss.last_line = old_ss.first_line - nlines;
6937c478bd9Sstevel@tonic-gate 				new_ss.first_line =
6947c478bd9Sstevel@tonic-gate 				    new_ss.last_line - (off_t)window + 1;
6957c478bd9Sstevel@tonic-gate 				break;
6967c478bd9Sstevel@tonic-gate 			}
6977c478bd9Sstevel@tonic-gate 			inwait = 0;
6987c478bd9Sstevel@tonic-gate 			return (0);
6997c478bd9Sstevel@tonic-gate 		case 'n':	/* switch to next file in arglist */
7007c478bd9Sstevel@tonic-gate 			CHECKEND();
7017c478bd9Sstevel@tonic-gate 			if (sign == 0)
7027c478bd9Sstevel@tonic-gate 				sign = 1;
7037c478bd9Sstevel@tonic-gate 			if (nlines == 0)
7047c478bd9Sstevel@tonic-gate 				nlines++;
7057c478bd9Sstevel@tonic-gate 			if ((skip = skipf(sign *nlines)) == 0) {
7067c478bd9Sstevel@tonic-gate 				BEEP();
7077c478bd9Sstevel@tonic-gate 				break;
7087c478bd9Sstevel@tonic-gate 			}
7097c478bd9Sstevel@tonic-gate 			inwait = 0;
7107c478bd9Sstevel@tonic-gate 			return (skip);
7117c478bd9Sstevel@tonic-gate 		case 'p':	/* switch to previous file in arglist */
7127c478bd9Sstevel@tonic-gate 			CHECKEND();
7137c478bd9Sstevel@tonic-gate 			if (sign == 0)
7147c478bd9Sstevel@tonic-gate 				sign = 1;
7157c478bd9Sstevel@tonic-gate 			if (nlines == 0)
7167c478bd9Sstevel@tonic-gate 				nlines++;
7177c478bd9Sstevel@tonic-gate 			if ((skip = skipf(-sign * nlines)) == 0) {
7187c478bd9Sstevel@tonic-gate 				BEEP();
7197c478bd9Sstevel@tonic-gate 				break;
7207c478bd9Sstevel@tonic-gate 			}
7217c478bd9Sstevel@tonic-gate 			inwait = 0;
7227c478bd9Sstevel@tonic-gate 			return (skip);
7237c478bd9Sstevel@tonic-gate 		case '$':	/* go to end of file */
7247c478bd9Sstevel@tonic-gate 			CHECKEND();
7257c478bd9Sstevel@tonic-gate 			sign = 1;
7267c478bd9Sstevel@tonic-gate 			while (find(1, (off_t)10000) != EOF)
7277c478bd9Sstevel@tonic-gate 				/* any large number will do */;
7287c478bd9Sstevel@tonic-gate 			new_ss.last_line = eofl_no - 1;
7297c478bd9Sstevel@tonic-gate 			new_ss.first_line = eofl_no - (off_t)window;
7307c478bd9Sstevel@tonic-gate 			inwait = 0;
7317c478bd9Sstevel@tonic-gate 			return (0);
7327c478bd9Sstevel@tonic-gate 		case '/':	/* search forward for r.e. */
7337c478bd9Sstevel@tonic-gate 		case '?':	/*   "  backwards */
7347c478bd9Sstevel@tonic-gate 		case '^':	/* this ones a ? for regent100s */
7357c478bd9Sstevel@tonic-gate 			if (sign < 0) {
7367c478bd9Sstevel@tonic-gate 				BEEP();
7377c478bd9Sstevel@tonic-gate 				break;
7387c478bd9Sstevel@tonic-gate 			}
7397c478bd9Sstevel@tonic-gate 			if (nlines == 0)
7407c478bd9Sstevel@tonic-gate 				nlines++;
7417c478bd9Sstevel@tonic-gate 			cmdptr--;
7427c478bd9Sstevel@tonic-gate 			cmdend = cmdptr + (strlen(cmdptr) - 1);
7437c478bd9Sstevel@tonic-gate 			wc_e1 = -1;
7447c478bd9Sstevel@tonic-gate 			wc_e = -1;
7457c478bd9Sstevel@tonic-gate 			for (p = cmdptr; p <= cmdend; p += len) {
7467c478bd9Sstevel@tonic-gate 				wc_e1 = wc_e;
7477c478bd9Sstevel@tonic-gate 				if ((len = mbtowc(&wc_e, p, MB_CUR_MAX)) <= 0) {
7487c478bd9Sstevel@tonic-gate 					wc_e = *p;
7497c478bd9Sstevel@tonic-gate 					len = 1;
7507c478bd9Sstevel@tonic-gate 				}
7517c478bd9Sstevel@tonic-gate 			}
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate 			if (cmdend > cmdptr + 1) {
7547c478bd9Sstevel@tonic-gate 				if ((wc_e1 == *cmdptr) &&
7557c478bd9Sstevel@tonic-gate 				    ((wc_e == L't') ||
75650fc488bSToomas Soome 				    (wc_e == L'm') || (wc_e == L'b'))) {
7577c478bd9Sstevel@tonic-gate 					leave_search = wc_e;
7587c478bd9Sstevel@tonic-gate 					wc_e = wc_e1;
7597c478bd9Sstevel@tonic-gate 					cmdend--;
7607c478bd9Sstevel@tonic-gate 				}
7617c478bd9Sstevel@tonic-gate 			}
7627c478bd9Sstevel@tonic-gate 			if ((cmdptr < cmdend) && (wc_e == *cmdptr))
7637c478bd9Sstevel@tonic-gate 				*cmdend = '\0';
7647c478bd9Sstevel@tonic-gate 			if (*cmdptr != '/')  /* signify back search by - */
7657c478bd9Sstevel@tonic-gate 				nlines = -nlines;
7667c478bd9Sstevel@tonic-gate 			if (!search(++cmdptr, (off_t)nlines))
7677c478bd9Sstevel@tonic-gate 				break;
7687c478bd9Sstevel@tonic-gate 			else {
7697c478bd9Sstevel@tonic-gate 				inwait = 0;
7707c478bd9Sstevel@tonic-gate 				return (0);
7717c478bd9Sstevel@tonic-gate 			}
7727c478bd9Sstevel@tonic-gate 		case '!':	/* shell escape */
7737c478bd9Sstevel@tonic-gate 			if (rmode) {	/* restricted mode */
7747c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
7757c478bd9Sstevel@tonic-gate 				"!command not allowed in restricted mode.\n"));
7767c478bd9Sstevel@tonic-gate 				break;
7777c478bd9Sstevel@tonic-gate 			}
7787c478bd9Sstevel@tonic-gate 			if (!hard_copy) { /* redisplay the command */
7797c478bd9Sstevel@tonic-gate 				(void) fputs(cmdbuf, stdout);
7807c478bd9Sstevel@tonic-gate 				(void) fputs("\n", stdout);
7817c478bd9Sstevel@tonic-gate 			}
7827c478bd9Sstevel@tonic-gate 			if ((id = fork()) < 0) {
7837c478bd9Sstevel@tonic-gate 				error("cannot fork, try again later");
7847c478bd9Sstevel@tonic-gate 				break;
7857c478bd9Sstevel@tonic-gate 			}
7867c478bd9Sstevel@tonic-gate 			if (id == (pid_t)0) {
7877c478bd9Sstevel@tonic-gate 				/*
7887c478bd9Sstevel@tonic-gate 				 * if stdin is a pipe, need to close it so
7897c478bd9Sstevel@tonic-gate 				 * that the terminal is really stdin for
7907c478bd9Sstevel@tonic-gate 				 * the command
7917c478bd9Sstevel@tonic-gate 				 */
7927c478bd9Sstevel@tonic-gate 				(void) fclose(stdin);
7937c478bd9Sstevel@tonic-gate 				(void) fclose(pg_stdin);
7947c478bd9Sstevel@tonic-gate 				(void) dup(fileno(stdout));
7957c478bd9Sstevel@tonic-gate 				(void) execl(shell, shell, "-c", cmdptr, 0);
7967c478bd9Sstevel@tonic-gate 				(void) perror("exec");
7977c478bd9Sstevel@tonic-gate 				exit(1);
7987c478bd9Sstevel@tonic-gate 			}
7997c478bd9Sstevel@tonic-gate 			(void) signal(SIGINT, SIG_IGN);
8007c478bd9Sstevel@tonic-gate 			(void) signal(SIGQUIT, SIG_IGN);
8017c478bd9Sstevel@tonic-gate 			if (catch_susp)
8027c478bd9Sstevel@tonic-gate 				(void) signal(SIGTSTP, SIG_DFL);
80350fc488bSToomas Soome 			while (wait(NULL) != id) {
8047c478bd9Sstevel@tonic-gate 				if (errno == ECHILD)
8057c478bd9Sstevel@tonic-gate 					break;
8067c478bd9Sstevel@tonic-gate 				else
8077c478bd9Sstevel@tonic-gate 					errno = 0;
8087c478bd9Sstevel@tonic-gate 			}
8097c478bd9Sstevel@tonic-gate 			(void) fputs("!\n", stdout);
8107c478bd9Sstevel@tonic-gate 			(void) fflush(stdout);
8117c478bd9Sstevel@tonic-gate 			(void) signal(SIGINT, on_brk);
8127c478bd9Sstevel@tonic-gate 			(void) signal(SIGQUIT, on_brk);
8137c478bd9Sstevel@tonic-gate 			if (catch_susp)
8147c478bd9Sstevel@tonic-gate 				(void) signal(SIGTSTP, onsusp);
8157c478bd9Sstevel@tonic-gate 			break;
8167c478bd9Sstevel@tonic-gate 		default:
8177c478bd9Sstevel@tonic-gate 			BEEP();
8187c478bd9Sstevel@tonic-gate 			break;
8197c478bd9Sstevel@tonic-gate 		}
8207c478bd9Sstevel@tonic-gate 	}
8217c478bd9Sstevel@tonic-gate }
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate static int
number()8247c478bd9Sstevel@tonic-gate number()
8257c478bd9Sstevel@tonic-gate {
8267c478bd9Sstevel@tonic-gate 	int i;
8277c478bd9Sstevel@tonic-gate 	char *p;
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate 	i = 0;
8307c478bd9Sstevel@tonic-gate 	sign = 0;
8317c478bd9Sstevel@tonic-gate 	p = cmdptr;
8327c478bd9Sstevel@tonic-gate 	BLANKS(p);
8337c478bd9Sstevel@tonic-gate 	if (*p == '+') {
8347c478bd9Sstevel@tonic-gate 		p++;
8357c478bd9Sstevel@tonic-gate 		sign = 1;
8367c478bd9Sstevel@tonic-gate 	}
8377c478bd9Sstevel@tonic-gate 	else
8387c478bd9Sstevel@tonic-gate 	if (*p == '-') {
8397c478bd9Sstevel@tonic-gate 		p++;
8407c478bd9Sstevel@tonic-gate 		sign = -1;
8417c478bd9Sstevel@tonic-gate 	}
8427c478bd9Sstevel@tonic-gate 	while (isdigit(*p))
8437c478bd9Sstevel@tonic-gate 		i = i * 10 + *p++ - '0';
8447c478bd9Sstevel@tonic-gate 	cmdptr = p;
8457c478bd9Sstevel@tonic-gate 	return (i);
8467c478bd9Sstevel@tonic-gate }
8477c478bd9Sstevel@tonic-gate 
8487c478bd9Sstevel@tonic-gate static int
ttyin(void)84950fc488bSToomas Soome ttyin(void)
8507c478bd9Sstevel@tonic-gate {
8517c478bd9Sstevel@tonic-gate 	char *sptr, *p;
8527c478bd9Sstevel@tonic-gate 	wchar_t ch;
8537c478bd9Sstevel@tonic-gate 	int slash = 0;
8547c478bd9Sstevel@tonic-gate 	int state = 0;
8557c478bd9Sstevel@tonic-gate 	int width, length;
8567c478bd9Sstevel@tonic-gate 	char multic[MB_LEN_MAX];
8577c478bd9Sstevel@tonic-gate 	int 	len;
8587c478bd9Sstevel@tonic-gate 
8597c478bd9Sstevel@tonic-gate 	(void) fixterm();
8607c478bd9Sstevel@tonic-gate 	/* initialize state processing */
8617c478bd9Sstevel@tonic-gate 	(void) set_state(&state, ' ', (char *)0);
8627c478bd9Sstevel@tonic-gate 	sptr = cmdbuf;
8637c478bd9Sstevel@tonic-gate 	while (state != 10) {
8647c478bd9Sstevel@tonic-gate 		if ((ch = readch()) < 0 || !iswascii(ch) && !iswprint(ch)) {
8657c478bd9Sstevel@tonic-gate 			BEEP();
8667c478bd9Sstevel@tonic-gate 			continue;
8677c478bd9Sstevel@tonic-gate 		}
8687c478bd9Sstevel@tonic-gate 
8697c478bd9Sstevel@tonic-gate 		if ((length = wctomb(multic, ch)) < 0)
8707c478bd9Sstevel@tonic-gate 			length = 0;
8717c478bd9Sstevel@tonic-gate 		multic[length] = 0;
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate 		if (ch == '\n' && !slash)
8747c478bd9Sstevel@tonic-gate 			break;
8757c478bd9Sstevel@tonic-gate 		if (ch == erasechar() && !slash) {
8767c478bd9Sstevel@tonic-gate 			if (sptr > cmdbuf) {
8777c478bd9Sstevel@tonic-gate 				char *oldp = cmdbuf;
8787c478bd9Sstevel@tonic-gate 				wchar_t wchar;
8797c478bd9Sstevel@tonic-gate 				p = cmdbuf;
8807c478bd9Sstevel@tonic-gate 				while (p  < sptr) {
8817c478bd9Sstevel@tonic-gate 					oldp = p;
8827c478bd9Sstevel@tonic-gate 					len = mbtowc(&wchar, p, MB_CUR_MAX);
8837c478bd9Sstevel@tonic-gate 					if (len <= 0) {
8847c478bd9Sstevel@tonic-gate 						wchar = (unsigned char)*p;
8857c478bd9Sstevel@tonic-gate 						len = 1;
8867c478bd9Sstevel@tonic-gate 					}
8877c478bd9Sstevel@tonic-gate 					p += len;
8887c478bd9Sstevel@tonic-gate 				}
8897c478bd9Sstevel@tonic-gate 				if ((width = wcwidth(wchar)) <= 0)
8907c478bd9Sstevel@tonic-gate 					/* ascii control character */
8917c478bd9Sstevel@tonic-gate 					width = 2;
8927c478bd9Sstevel@tonic-gate 				promptlen -= width;
8937c478bd9Sstevel@tonic-gate 				while (width--)
8947c478bd9Sstevel@tonic-gate 					(void) fputs("\b \b", stdout);
8957c478bd9Sstevel@tonic-gate 				sptr = oldp;
8967c478bd9Sstevel@tonic-gate 			}
8977c478bd9Sstevel@tonic-gate 			(void) set_state(&state, ch, sptr);
8987c478bd9Sstevel@tonic-gate 			(void) fflush(stdout);
8997c478bd9Sstevel@tonic-gate 			continue;
9007c478bd9Sstevel@tonic-gate 		}
9017c478bd9Sstevel@tonic-gate 		else
9027c478bd9Sstevel@tonic-gate 		if (ch == killchar() && !slash) {
9037c478bd9Sstevel@tonic-gate 			if (hard_copy)
9047c478bd9Sstevel@tonic-gate 				(void) putwchar(ch);
9057c478bd9Sstevel@tonic-gate 			(void) resetterm();
9067c478bd9Sstevel@tonic-gate 			return (1);
9077c478bd9Sstevel@tonic-gate 		}
9087c478bd9Sstevel@tonic-gate 		if (ch < ' ')
9097c478bd9Sstevel@tonic-gate 			width = 2;
9107c478bd9Sstevel@tonic-gate 		else
9117c478bd9Sstevel@tonic-gate 			if ((width = wcwidth(ch)) <= 0)
9127c478bd9Sstevel@tonic-gate 				width = 0;
9137c478bd9Sstevel@tonic-gate 		if (slash) {
9147c478bd9Sstevel@tonic-gate 			slash = 0;
9157c478bd9Sstevel@tonic-gate 			(void) fputs("\b \b", stdout);
9167c478bd9Sstevel@tonic-gate 			sptr--;
9177c478bd9Sstevel@tonic-gate 			promptlen--;
9187c478bd9Sstevel@tonic-gate 		} else /* is there room to keep this character? */
9197c478bd9Sstevel@tonic-gate 		if (sptr >= cmdbuf + sizeof (cmdbuf) ||
9207c478bd9Sstevel@tonic-gate 		    promptlen + width >= columns) {
9217c478bd9Sstevel@tonic-gate 			BEEP();
9227c478bd9Sstevel@tonic-gate 			continue;
9237c478bd9Sstevel@tonic-gate 		}
9247c478bd9Sstevel@tonic-gate 		else
9257c478bd9Sstevel@tonic-gate 		if (ch == '\\')
9267c478bd9Sstevel@tonic-gate 			slash++;
9277c478bd9Sstevel@tonic-gate 		if (set_state(&state, ch, sptr) == 0) {
9287c478bd9Sstevel@tonic-gate 			BEEP();
9297c478bd9Sstevel@tonic-gate 			continue;
9307c478bd9Sstevel@tonic-gate 		}
9317c478bd9Sstevel@tonic-gate 		(void) strncpy(sptr, multic, (size_t)length);
9327c478bd9Sstevel@tonic-gate 		sptr += length;
9337c478bd9Sstevel@tonic-gate 		if (ch < ' ') {
9347c478bd9Sstevel@tonic-gate 			ch += 0100;
9357c478bd9Sstevel@tonic-gate 			multic[0] = '^';
9367c478bd9Sstevel@tonic-gate 			multic[1] = ch;
9377c478bd9Sstevel@tonic-gate 			length = 2;
9387c478bd9Sstevel@tonic-gate 		}
9397c478bd9Sstevel@tonic-gate 		p = multic;
9407c478bd9Sstevel@tonic-gate 		while (length--)
9417c478bd9Sstevel@tonic-gate 			(void) putchar(*p++);
9427c478bd9Sstevel@tonic-gate 		promptlen += width;
9437c478bd9Sstevel@tonic-gate 		(void) fflush(stdout);
9447c478bd9Sstevel@tonic-gate 	}
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate 	*sptr = '\0';
9477c478bd9Sstevel@tonic-gate 	kill_line();
9487c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
9497c478bd9Sstevel@tonic-gate 	(void) resetterm();
9507c478bd9Sstevel@tonic-gate 	return (0);
9517c478bd9Sstevel@tonic-gate }
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate static	int
set_state(int * pstate,wchar_t c,char * pc)95450fc488bSToomas Soome set_state(int *pstate, wchar_t c, char *pc)
9557c478bd9Sstevel@tonic-gate {
9567c478bd9Sstevel@tonic-gate 	static char *psign;
9577c478bd9Sstevel@tonic-gate 	static char *pnumber;
9587c478bd9Sstevel@tonic-gate 	static char *pcommand;
9597c478bd9Sstevel@tonic-gate 	static int slash;
9607c478bd9Sstevel@tonic-gate 
9617c478bd9Sstevel@tonic-gate 	if (*pstate == 0) {
9627c478bd9Sstevel@tonic-gate 		psign = (char *)NULL;
9637c478bd9Sstevel@tonic-gate 		pnumber = (char *)NULL;
9647c478bd9Sstevel@tonic-gate 		pcommand = (char *)NULL;
9657c478bd9Sstevel@tonic-gate 		*pstate = 1;
9667c478bd9Sstevel@tonic-gate 		slash = 0;
9677c478bd9Sstevel@tonic-gate 		return (1);
9687c478bd9Sstevel@tonic-gate 	}
9697c478bd9Sstevel@tonic-gate 	if (c == '\\' && !slash) {
9707c478bd9Sstevel@tonic-gate 		slash++;
9717c478bd9Sstevel@tonic-gate 		return (1);
9727c478bd9Sstevel@tonic-gate 	}
9737c478bd9Sstevel@tonic-gate 	if (c == erasechar() && !slash)
9747c478bd9Sstevel@tonic-gate 		switch (*pstate) {
9757c478bd9Sstevel@tonic-gate 		case 4:
9767c478bd9Sstevel@tonic-gate 			if (pc > pcommand)
9777c478bd9Sstevel@tonic-gate 				return (1);
9787c478bd9Sstevel@tonic-gate 			pcommand = (char *)NULL;
9797c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
9807c478bd9Sstevel@tonic-gate 
9817c478bd9Sstevel@tonic-gate 		case 3:
9827c478bd9Sstevel@tonic-gate 			if (pnumber && pc > pnumber) {
9837c478bd9Sstevel@tonic-gate 				*pstate = 3;
9847c478bd9Sstevel@tonic-gate 				return (1);
9857c478bd9Sstevel@tonic-gate 			}
9867c478bd9Sstevel@tonic-gate 			pnumber = (char *)NULL;
9877c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
9887c478bd9Sstevel@tonic-gate 
9897c478bd9Sstevel@tonic-gate 		case 2:
9907c478bd9Sstevel@tonic-gate 			if (psign && pc > psign) {
9917c478bd9Sstevel@tonic-gate 				*pstate = 2;
9927c478bd9Sstevel@tonic-gate 				return (1);
9937c478bd9Sstevel@tonic-gate 			}
9947c478bd9Sstevel@tonic-gate 			psign = (char *)NULL;
9957c478bd9Sstevel@tonic-gate 			/*FALLTHROUGH*/
9967c478bd9Sstevel@tonic-gate 
9977c478bd9Sstevel@tonic-gate 		case 1:
9987c478bd9Sstevel@tonic-gate 			*pstate = 1;
9997c478bd9Sstevel@tonic-gate 			return (1);
10007c478bd9Sstevel@tonic-gate 		}
10017c478bd9Sstevel@tonic-gate 
10027c478bd9Sstevel@tonic-gate 	slash = 0;
10037c478bd9Sstevel@tonic-gate 	switch (*pstate) {
10047c478bd9Sstevel@tonic-gate 	case 1: /* before recieving anything interesting */
10057c478bd9Sstevel@tonic-gate 		if (c == '\t' || (!nflag && c == ' '))
10067c478bd9Sstevel@tonic-gate 			return (1);
10077c478bd9Sstevel@tonic-gate 		if (c == '+' || c == '-') {
10087c478bd9Sstevel@tonic-gate 			psign = pc;
10097c478bd9Sstevel@tonic-gate 			*pstate = 2;
10107c478bd9Sstevel@tonic-gate 			return (1);
10117c478bd9Sstevel@tonic-gate 		}
10127c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
10137c478bd9Sstevel@tonic-gate 
10147c478bd9Sstevel@tonic-gate 	case 2: /* recieved sign, waiting for digit */
10157c478bd9Sstevel@tonic-gate 		if (iswascii(c) && isdigit(c)) {
10167c478bd9Sstevel@tonic-gate 			pnumber = pc;
10177c478bd9Sstevel@tonic-gate 			*pstate = 3;
10187c478bd9Sstevel@tonic-gate 			return (1);
10197c478bd9Sstevel@tonic-gate 		}
10207c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
10217c478bd9Sstevel@tonic-gate 
10227c478bd9Sstevel@tonic-gate 	case 3: /* recieved digit, waiting for the rest of the number */
10237c478bd9Sstevel@tonic-gate 		if (iswascii(c) && isdigit(c))
10247c478bd9Sstevel@tonic-gate 			return (1);
10257c478bd9Sstevel@tonic-gate 		if (iswascii(c) && pg_strchr("h\014.wz\004dqQfl np$", c)) {
10267c478bd9Sstevel@tonic-gate 			pcommand = pc;
10277c478bd9Sstevel@tonic-gate 			if (nflag)
10287c478bd9Sstevel@tonic-gate 				*pstate = 10;
10297c478bd9Sstevel@tonic-gate 			else
10307c478bd9Sstevel@tonic-gate 				*pstate = 4;
10317c478bd9Sstevel@tonic-gate 			return (1);
10327c478bd9Sstevel@tonic-gate 		}
10337c478bd9Sstevel@tonic-gate 		if (iswascii(c) && pg_strchr("s/^?!", c)) {
10347c478bd9Sstevel@tonic-gate 			pcommand = pc;
10357c478bd9Sstevel@tonic-gate 			*pstate = 4;
10367c478bd9Sstevel@tonic-gate 			return (1);
10377c478bd9Sstevel@tonic-gate 		}
10387c478bd9Sstevel@tonic-gate 		return (0);
10397c478bd9Sstevel@tonic-gate 	case 4:
10407c478bd9Sstevel@tonic-gate 		return (1);
10417c478bd9Sstevel@tonic-gate 	}
10427c478bd9Sstevel@tonic-gate 	return (0);
10437c478bd9Sstevel@tonic-gate }
10447c478bd9Sstevel@tonic-gate 
104550fc488bSToomas Soome static int
readch(void)104650fc488bSToomas Soome readch(void)
10477c478bd9Sstevel@tonic-gate {
10487c478bd9Sstevel@tonic-gate 	return (fgetwc(pg_stdin));
10497c478bd9Sstevel@tonic-gate }
10507c478bd9Sstevel@tonic-gate 
10517c478bd9Sstevel@tonic-gate static void
help(void)105250fc488bSToomas Soome help(void)
10537c478bd9Sstevel@tonic-gate {
10547c478bd9Sstevel@tonic-gate 	if (clropt)
10557c478bd9Sstevel@tonic-gate 		doclear();
10567c478bd9Sstevel@tonic-gate 
10577c478bd9Sstevel@tonic-gate 	(void) fputs(gettext(
10587c478bd9Sstevel@tonic-gate "-------------------------------------------------------\n"
10597c478bd9Sstevel@tonic-gate "  h                     help\n"
10607c478bd9Sstevel@tonic-gate "  q or Q                quit\n"
10617c478bd9Sstevel@tonic-gate "  <blank> or <newline>  next page\n"
10627c478bd9Sstevel@tonic-gate "  l                     next line\n"
10637c478bd9Sstevel@tonic-gate "  d or <^D>             display half a page more\n"
10647c478bd9Sstevel@tonic-gate "  . or <^L>             redisplay current page\n"
10657c478bd9Sstevel@tonic-gate "  f                     skip the next page forward\n"
10667c478bd9Sstevel@tonic-gate "  n                     next file\n"
10677c478bd9Sstevel@tonic-gate "  p                     previous file\n"
10687c478bd9Sstevel@tonic-gate "  $                     last page\n"
10697c478bd9Sstevel@tonic-gate "  w or z                set window size and display next page\n"
10707c478bd9Sstevel@tonic-gate "  s savefile            save current file in savefile\n"
10717c478bd9Sstevel@tonic-gate "  /pattern/             search forward for pattern\n"
10727c478bd9Sstevel@tonic-gate "  ?pattern? or\n"
10737c478bd9Sstevel@tonic-gate "  ^pattern^             search backward for pattern\n"
10747c478bd9Sstevel@tonic-gate "  !command              execute command\n"
10757c478bd9Sstevel@tonic-gate "\n"
10767c478bd9Sstevel@tonic-gate "Most commands can be preceeded by a number, as in:\n"
10777c478bd9Sstevel@tonic-gate "+1<newline> (next page); -1<newline> (previous page); 1<newline> (page 1).\n"
10787c478bd9Sstevel@tonic-gate "\n"
10797c478bd9Sstevel@tonic-gate "See the manual page for more detail.\n"
10807c478bd9Sstevel@tonic-gate "-------------------------------------------------------\n"),
10814fce32e1Smuffin 	    stdout);
10827c478bd9Sstevel@tonic-gate }
10837c478bd9Sstevel@tonic-gate 
10847c478bd9Sstevel@tonic-gate /*
10857c478bd9Sstevel@tonic-gate  * Skip nskip files in the file list (from the command line). Nskip may be
10867c478bd9Sstevel@tonic-gate  * negative.
10877c478bd9Sstevel@tonic-gate  */
10887c478bd9Sstevel@tonic-gate 
10897c478bd9Sstevel@tonic-gate static int
skipf(int nskip)109050fc488bSToomas Soome skipf(int nskip)
10917c478bd9Sstevel@tonic-gate {
10927c478bd9Sstevel@tonic-gate 	if (fnum + nskip < 0) {
10937c478bd9Sstevel@tonic-gate 		nskip = -fnum;
10947c478bd9Sstevel@tonic-gate 		if (nskip == 0)
10957c478bd9Sstevel@tonic-gate 			error("No previous file");
10967c478bd9Sstevel@tonic-gate 	}
10977c478bd9Sstevel@tonic-gate 	else
10987c478bd9Sstevel@tonic-gate 	if (fnum + nskip > nfiles - 1) {
10997c478bd9Sstevel@tonic-gate 		nskip = (nfiles - 1) - fnum;
11007c478bd9Sstevel@tonic-gate 		if (nskip == 0)
11017c478bd9Sstevel@tonic-gate 			error("No next file");
11027c478bd9Sstevel@tonic-gate 	}
11037c478bd9Sstevel@tonic-gate 	return (nskip);
11047c478bd9Sstevel@tonic-gate }
11057c478bd9Sstevel@tonic-gate 
11067c478bd9Sstevel@tonic-gate /*
11077c478bd9Sstevel@tonic-gate  * Check whether the file named by fs is a file which the user may
11087c478bd9Sstevel@tonic-gate  * access.  If it is, return the opened file. Otherwise return NULL.
11097c478bd9Sstevel@tonic-gate  */
11107c478bd9Sstevel@tonic-gate 
11117c478bd9Sstevel@tonic-gate static FILE *
checkf(char * fs)111250fc488bSToomas Soome checkf(char *fs)
11137c478bd9Sstevel@tonic-gate {
11147c478bd9Sstevel@tonic-gate 	struct stat stbuf;
11157c478bd9Sstevel@tonic-gate 	FILE *f;
11167c478bd9Sstevel@tonic-gate 	int fd;
11177c478bd9Sstevel@tonic-gate 	int f_was_opened;
11187c478bd9Sstevel@tonic-gate 
11197c478bd9Sstevel@tonic-gate 	pipe_in = 0;
11207c478bd9Sstevel@tonic-gate 	if (strcmp(fs, "-") == 0) {
11217c478bd9Sstevel@tonic-gate 		if (tmp_fin == NULL)
11227c478bd9Sstevel@tonic-gate 			f = stdin;
11237c478bd9Sstevel@tonic-gate 		else {
11247c478bd9Sstevel@tonic-gate 			rewind(tmp_fin);
11257c478bd9Sstevel@tonic-gate 			f = tmp_fin;
11267c478bd9Sstevel@tonic-gate 		}
11277c478bd9Sstevel@tonic-gate 		f_was_opened = 0;
11287c478bd9Sstevel@tonic-gate 	} else {
11297c478bd9Sstevel@tonic-gate 		if ((f = fopen(fs, "r")) == (FILE *)NULL) {
11307c478bd9Sstevel@tonic-gate 			(void) fflush(stdout);
11317c478bd9Sstevel@tonic-gate 			perror(fs);
11327c478bd9Sstevel@tonic-gate 			return ((FILE *)NULL);
11337c478bd9Sstevel@tonic-gate 		}
11347c478bd9Sstevel@tonic-gate 		f_was_opened = 1;
11357c478bd9Sstevel@tonic-gate 	}
11367c478bd9Sstevel@tonic-gate 	if (fstat(fileno(f), &stbuf) == -1) {
11377c478bd9Sstevel@tonic-gate 		if (f_was_opened)
11387c478bd9Sstevel@tonic-gate 			(void) fclose(f);
11397c478bd9Sstevel@tonic-gate 		(void) fflush(stdout);
11407c478bd9Sstevel@tonic-gate 		perror(fs);
11417c478bd9Sstevel@tonic-gate 		return ((FILE *)NULL);
11427c478bd9Sstevel@tonic-gate 	}
11437c478bd9Sstevel@tonic-gate 	if ((stbuf.st_mode & S_IFMT) == S_IFDIR) {
11447c478bd9Sstevel@tonic-gate 		if (f_was_opened)
11457c478bd9Sstevel@tonic-gate 			(void) fclose(f);
11467c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "pg: ");
11477c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s is a directory\n"), fs);
11487c478bd9Sstevel@tonic-gate 		return ((FILE *)NULL);
11497c478bd9Sstevel@tonic-gate 	}
11507c478bd9Sstevel@tonic-gate 	if ((stbuf.st_mode & S_IFMT) == S_IFREG) {
11517c478bd9Sstevel@tonic-gate 		if (f == stdin)		/* It may have been read from */
11527c478bd9Sstevel@tonic-gate 			rewind(f);	/* already, and not reopened  */
11537c478bd9Sstevel@tonic-gate 	} else {
11547c478bd9Sstevel@tonic-gate 		if (f != stdin) {
11557c478bd9Sstevel@tonic-gate 			if (f_was_opened)
11567c478bd9Sstevel@tonic-gate 				(void) fclose(f);
11577c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "pg: ");
11587c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
11597c478bd9Sstevel@tonic-gate 			"special files only handled as standard input\n"));
11607c478bd9Sstevel@tonic-gate 			return ((FILE *)NULL);
11617c478bd9Sstevel@tonic-gate 		} else {
11627c478bd9Sstevel@tonic-gate 			if ((fd = mkstemp(tmp_name)) < 0) {
116350fc488bSToomas Soome 				(void) perror(tmp_name);
116450fc488bSToomas Soome 				return ((FILE *)NULL);
11657c478bd9Sstevel@tonic-gate 			}
11667c478bd9Sstevel@tonic-gate 			(void) close(fd);
11677c478bd9Sstevel@tonic-gate 			if ((tmp_fou = fopen(tmp_name, "w")) == NULL) {
11687c478bd9Sstevel@tonic-gate 				(void) perror(tmp_name);
11697c478bd9Sstevel@tonic-gate 				return ((FILE *)NULL);
11707c478bd9Sstevel@tonic-gate 			}
11717c478bd9Sstevel@tonic-gate 			if ((tmp_fin = fopen(tmp_name, "r")) == NULL) {
11727c478bd9Sstevel@tonic-gate 				(void) perror(tmp_name);
11737c478bd9Sstevel@tonic-gate 				return ((FILE *)NULL);
11747c478bd9Sstevel@tonic-gate 			}
11757c478bd9Sstevel@tonic-gate 			pipe_in = 1;
11767c478bd9Sstevel@tonic-gate 		}
11777c478bd9Sstevel@tonic-gate 	}
11787c478bd9Sstevel@tonic-gate 	lineset(BOF);
11797c478bd9Sstevel@tonic-gate 	return (f);
11807c478bd9Sstevel@tonic-gate }
11817c478bd9Sstevel@tonic-gate 
11827c478bd9Sstevel@tonic-gate static void
copy_file(FILE * f,FILE * out)118350fc488bSToomas Soome copy_file(FILE *f, FILE *out)
11847c478bd9Sstevel@tonic-gate {
11857c478bd9Sstevel@tonic-gate 	int c;
11867c478bd9Sstevel@tonic-gate 
11877c478bd9Sstevel@tonic-gate 	while ((c = getc(f)) != EOF)
11887c478bd9Sstevel@tonic-gate 		(void) putc(c, out);
11897c478bd9Sstevel@tonic-gate 
11907c478bd9Sstevel@tonic-gate }
11917c478bd9Sstevel@tonic-gate 
11927c478bd9Sstevel@tonic-gate static void
re_error(int i)119350fc488bSToomas Soome re_error(int i)
11947c478bd9Sstevel@tonic-gate {
11957c478bd9Sstevel@tonic-gate 	int j;
11967c478bd9Sstevel@tonic-gate 	static struct messages {
11977c478bd9Sstevel@tonic-gate 		char *message;
11987c478bd9Sstevel@tonic-gate 		int number;
11997c478bd9Sstevel@tonic-gate 		} re_errmsg[] = {
12007c478bd9Sstevel@tonic-gate 		"Pattern not found",				1,
12017c478bd9Sstevel@tonic-gate 		"Range endpoint too large",			11,
12027c478bd9Sstevel@tonic-gate 		"Bad number",					16,
12037c478bd9Sstevel@tonic-gate 		"`\\digit' out of range",			25,
12047c478bd9Sstevel@tonic-gate 		"No remembered search string",  		41,
12057c478bd9Sstevel@tonic-gate 		"\\( \\) imbalance",				42,
12067c478bd9Sstevel@tonic-gate 		"Too many \\(",					43,
12077c478bd9Sstevel@tonic-gate 		"More than two numbers given in \\{ \\}",  	44,
12087c478bd9Sstevel@tonic-gate 		"} expected after \\",				45,
12097c478bd9Sstevel@tonic-gate 		"First number exceeds second in \\{ \\}",  	46,
12107c478bd9Sstevel@tonic-gate 		"[] imbalance",					49,
12117c478bd9Sstevel@tonic-gate 		"Regular expression overflow",			50,
12127c478bd9Sstevel@tonic-gate 		"Illegal byte sequence",			67,
12137c478bd9Sstevel@tonic-gate 		"Bad regular expression",			0
12147c478bd9Sstevel@tonic-gate 		};
12157c478bd9Sstevel@tonic-gate 
12167c478bd9Sstevel@tonic-gate 	for (j = 0; re_errmsg[j].number != 0; j++)
12177c478bd9Sstevel@tonic-gate 		if (re_errmsg[j].number == i)
12187c478bd9Sstevel@tonic-gate 			break;
12197c478bd9Sstevel@tonic-gate 	error(re_errmsg[j].message);
12207c478bd9Sstevel@tonic-gate 	longjmp(restore, 1);  /* restore to search() */
12217c478bd9Sstevel@tonic-gate }
12227c478bd9Sstevel@tonic-gate 
12237c478bd9Sstevel@tonic-gate /*
12247c478bd9Sstevel@tonic-gate  * Search for nth ocurrence of regular expression contained in buf in the file
12257c478bd9Sstevel@tonic-gate  *	negative n implies backward search
12267c478bd9Sstevel@tonic-gate  *	n 'guaranteed' non-zero
12277c478bd9Sstevel@tonic-gate  */
12287c478bd9Sstevel@tonic-gate static int
search(char * buf,off_t n)122950fc488bSToomas Soome search(char *buf, off_t n)
12307c478bd9Sstevel@tonic-gate {
12317c478bd9Sstevel@tonic-gate 	int direction;
12327c478bd9Sstevel@tonic-gate 	static char *expbuf;
12337c478bd9Sstevel@tonic-gate 	char *nexpbuf;
12347c478bd9Sstevel@tonic-gate 	int END_COND;
12357c478bd9Sstevel@tonic-gate 
12367c478bd9Sstevel@tonic-gate 	if (setjmp(restore) <= 0) {
12377c478bd9Sstevel@tonic-gate 		nexpbuf = compile(buf, (char *)0, (char *)0);
12387c478bd9Sstevel@tonic-gate 		if (regerrno) {
12397c478bd9Sstevel@tonic-gate 			if (regerrno != 41 || expbuf == NULL)
12407c478bd9Sstevel@tonic-gate 				re_error(regerrno);
12417c478bd9Sstevel@tonic-gate 		} else {
12427c478bd9Sstevel@tonic-gate 			if (expbuf)
12437c478bd9Sstevel@tonic-gate 				free(expbuf);
12447c478bd9Sstevel@tonic-gate 			expbuf = nexpbuf;
12457c478bd9Sstevel@tonic-gate 		}
12467c478bd9Sstevel@tonic-gate 
12477c478bd9Sstevel@tonic-gate 		if (n < 0) {	/* search back */
12487c478bd9Sstevel@tonic-gate 			direction = -1;
12497c478bd9Sstevel@tonic-gate 			(void) find(0, old_ss.first_line);
12507c478bd9Sstevel@tonic-gate 			END_COND = BOF;
12517c478bd9Sstevel@tonic-gate 		} else {
12527c478bd9Sstevel@tonic-gate 			direction = 1;
12537c478bd9Sstevel@tonic-gate 			(void) find(0, old_ss.last_line);
12547c478bd9Sstevel@tonic-gate 			END_COND = EOF;
12557c478bd9Sstevel@tonic-gate 		}
12567c478bd9Sstevel@tonic-gate 
12577c478bd9Sstevel@tonic-gate 		while (find(1, direction) != END_COND) {
12587c478bd9Sstevel@tonic-gate 			if (brk_hit)
12597c478bd9Sstevel@tonic-gate 				break;
12607c478bd9Sstevel@tonic-gate 			if (step(Line, expbuf))
12617c478bd9Sstevel@tonic-gate 				if ((n -= direction) == 0) {
12627c478bd9Sstevel@tonic-gate 					switch (leave_search) {
12637c478bd9Sstevel@tonic-gate 					case 't':
12647c478bd9Sstevel@tonic-gate 						new_ss.first_line =
12657c478bd9Sstevel@tonic-gate 						    find(1, (off_t)0);
12667c478bd9Sstevel@tonic-gate 						new_ss.last_line =
12677c478bd9Sstevel@tonic-gate 						    new_ss.first_line +
12687c478bd9Sstevel@tonic-gate 						    (off_t)window
12697c478bd9Sstevel@tonic-gate 						    - 1;
12707c478bd9Sstevel@tonic-gate 						break;
12717c478bd9Sstevel@tonic-gate 					case 'b':
12727c478bd9Sstevel@tonic-gate 						new_ss.last_line =
12737c478bd9Sstevel@tonic-gate 						    find(1, (off_t)0);
12747c478bd9Sstevel@tonic-gate 						new_ss.first_line =
12757c478bd9Sstevel@tonic-gate 						    new_ss.last_line -
12767c478bd9Sstevel@tonic-gate 						    (off_t)window
12777c478bd9Sstevel@tonic-gate 						    + 1;
12787c478bd9Sstevel@tonic-gate 						break;
12797c478bd9Sstevel@tonic-gate 					case 'm':
12807c478bd9Sstevel@tonic-gate 						new_ss.first_line =
12817c478bd9Sstevel@tonic-gate 						    find(1, (off_t)0) -
12827c478bd9Sstevel@tonic-gate 						    ((off_t)window - 1)/2;
12837c478bd9Sstevel@tonic-gate 						new_ss.last_line =
12847c478bd9Sstevel@tonic-gate 						    new_ss.first_line +
12857c478bd9Sstevel@tonic-gate 						    (off_t)window
12867c478bd9Sstevel@tonic-gate 						    - 1;
12877c478bd9Sstevel@tonic-gate 						break;
12887c478bd9Sstevel@tonic-gate 					}
12897c478bd9Sstevel@tonic-gate 					return (1);
12907c478bd9Sstevel@tonic-gate 				}
12917c478bd9Sstevel@tonic-gate 		}
12927c478bd9Sstevel@tonic-gate 		re_error(1); /* Pattern not found */
12937c478bd9Sstevel@tonic-gate 	}
12947c478bd9Sstevel@tonic-gate 	BEEP();
12957c478bd9Sstevel@tonic-gate 	return (0);
12967c478bd9Sstevel@tonic-gate }
12977c478bd9Sstevel@tonic-gate 
12987c478bd9Sstevel@tonic-gate /*
12997c478bd9Sstevel@tonic-gate  *	find -- find line in file f, subject to certain constraints.
13007c478bd9Sstevel@tonic-gate  *
13017c478bd9Sstevel@tonic-gate  *	This is the reason for all the funny stuff with sign and nlines.
13027c478bd9Sstevel@tonic-gate  *	We need to be able to differentiate between relative and abosolute
13037c478bd9Sstevel@tonic-gate  *	address specifications.
13047c478bd9Sstevel@tonic-gate  *
13057c478bd9Sstevel@tonic-gate  *	So...there are basically three cases that this routine
13067c478bd9Sstevel@tonic-gate  *	handles. Either line is zero, which  means there is to be
13077c478bd9Sstevel@tonic-gate  *	no motion (because line numbers start at one), or
13087c478bd9Sstevel@tonic-gate  *	how and line specify a number, or line itself is negative,
13097c478bd9Sstevel@tonic-gate  *	which is the same as having how == -1 and line == abs(line).
13107c478bd9Sstevel@tonic-gate  *
13117c478bd9Sstevel@tonic-gate  *	Then, figure where exactly it is that we are going (an absolute
13127c478bd9Sstevel@tonic-gate  *	line number). Find out if it is within what we have read,
13137c478bd9Sstevel@tonic-gate  *	if so, go there without further ado. Otherwise, do some
13147c478bd9Sstevel@tonic-gate  *	magic to get there, saving all the intervening lines,
13157c478bd9Sstevel@tonic-gate  *	in case the user wants to see them some time later.
13167c478bd9Sstevel@tonic-gate  *
13177c478bd9Sstevel@tonic-gate  *	In any case, return the line number that we end up at.
13187c478bd9Sstevel@tonic-gate  *	(This is used by search() and screen()). If we go past EOF,
13197c478bd9Sstevel@tonic-gate  *	return EOF.
13207c478bd9Sstevel@tonic-gate  *	This EOF will go away eventually, as pg is expanded to
13217c478bd9Sstevel@tonic-gate  *	handle multiple files as one huge one. Then EOF will
13227c478bd9Sstevel@tonic-gate  *	mean we have run off the file list.
13237c478bd9Sstevel@tonic-gate  *	If the requested line number is too far back, return BOF.
13247c478bd9Sstevel@tonic-gate  */
13257c478bd9Sstevel@tonic-gate 
13267c478bd9Sstevel@tonic-gate static off_t
find(int how,off_t line)132750fc488bSToomas Soome find(int how, off_t line)
13287c478bd9Sstevel@tonic-gate {
13297c478bd9Sstevel@tonic-gate 	/* no compacted memory yet */
13307c478bd9Sstevel@tonic-gate 	FILE *f = in_file;
13317c478bd9Sstevel@tonic-gate 	off_t where;
13327c478bd9Sstevel@tonic-gate 
13337c478bd9Sstevel@tonic-gate 	if (how == 0)
13347c478bd9Sstevel@tonic-gate 		where = line;
13357c478bd9Sstevel@tonic-gate 	else
13367c478bd9Sstevel@tonic-gate 		if (dot == zero - 1)
13377c478bd9Sstevel@tonic-gate 			where = how * line;
13387c478bd9Sstevel@tonic-gate 		else
13397c478bd9Sstevel@tonic-gate 			where = how * line + dot->l_no;
13407c478bd9Sstevel@tonic-gate 
13417c478bd9Sstevel@tonic-gate 	/* now, where is either at, before, or after dol */
13427c478bd9Sstevel@tonic-gate 	/* most likely case is after, so do it first */
13437c478bd9Sstevel@tonic-gate 
13447c478bd9Sstevel@tonic-gate 	eoflag = 0;
13457c478bd9Sstevel@tonic-gate 	if (where >= dol->l_no) {
13467c478bd9Sstevel@tonic-gate 		if (doliseof) {
13477c478bd9Sstevel@tonic-gate 			dot = dol;
13487c478bd9Sstevel@tonic-gate 			eoflag++;
13497c478bd9Sstevel@tonic-gate 			return (EOF);
13507c478bd9Sstevel@tonic-gate 		}
13517c478bd9Sstevel@tonic-gate 		if (pipe_in)
13527c478bd9Sstevel@tonic-gate 			in_file = f = stdin;
13537c478bd9Sstevel@tonic-gate 		else
13547c478bd9Sstevel@tonic-gate 			(void) fseeko(f, (off_t)dol->l_addr, SEEK_SET);
13557c478bd9Sstevel@tonic-gate 		dot = dol - 1;
135623a1cceaSRoger A. Faulkner 		while ((nchars = getaline(f)) != EOF) {
13577c478bd9Sstevel@tonic-gate 			dot++;
13587c478bd9Sstevel@tonic-gate 			newdol(f);
13597c478bd9Sstevel@tonic-gate 			if (where == dot->l_no || brk_hit)
13607c478bd9Sstevel@tonic-gate 				break;
13617c478bd9Sstevel@tonic-gate 		}
13627c478bd9Sstevel@tonic-gate 		if (nchars != EOF)
13637c478bd9Sstevel@tonic-gate 			return (dot->l_no);
13647c478bd9Sstevel@tonic-gate 		else { /* EOF */
13657c478bd9Sstevel@tonic-gate 			dot = dol;
13667c478bd9Sstevel@tonic-gate 			eoflag++;
13677c478bd9Sstevel@tonic-gate 			doliseof++;
13687c478bd9Sstevel@tonic-gate 			eofl_no = dol->l_no;
13697c478bd9Sstevel@tonic-gate 			return (EOF);
13707c478bd9Sstevel@tonic-gate 		}
13717c478bd9Sstevel@tonic-gate 	} else { /* where < dol->l_no */
13727c478bd9Sstevel@tonic-gate 		if (pipe_in) {
13737c478bd9Sstevel@tonic-gate 			(void) fflush(tmp_fou);
13747c478bd9Sstevel@tonic-gate 			in_file = f = tmp_fin;
13757c478bd9Sstevel@tonic-gate 		}
13767c478bd9Sstevel@tonic-gate 		if (where < zero->l_no) {
13777c478bd9Sstevel@tonic-gate 			(void) fseeko(f, (off_t)zero->l_addr, SEEK_SET);
13787c478bd9Sstevel@tonic-gate 			dot = zero - 1;
13797c478bd9Sstevel@tonic-gate 			return (BOF);
13807c478bd9Sstevel@tonic-gate 		} else {
13817c478bd9Sstevel@tonic-gate 			dot = zero + where - 1;
13827c478bd9Sstevel@tonic-gate 			(void) fseeko(f, (off_t)dot->l_addr, SEEK_SET);
138323a1cceaSRoger A. Faulkner 			nchars = getaline(f);
13847c478bd9Sstevel@tonic-gate 			return (dot->l_no);
13857c478bd9Sstevel@tonic-gate 		}
13867c478bd9Sstevel@tonic-gate 	}
13877c478bd9Sstevel@tonic-gate }
13887c478bd9Sstevel@tonic-gate 
13897c478bd9Sstevel@tonic-gate static FILE *fileptr;
13907c478bd9Sstevel@tonic-gate static int (*rdchar)();
13917c478bd9Sstevel@tonic-gate 
13927c478bd9Sstevel@tonic-gate static int
mrdchar()13937c478bd9Sstevel@tonic-gate mrdchar()
13947c478bd9Sstevel@tonic-gate {
13957c478bd9Sstevel@tonic-gate 	return (rdchar(fileptr));
13967c478bd9Sstevel@tonic-gate }
13977c478bd9Sstevel@tonic-gate 
13987c478bd9Sstevel@tonic-gate /*
13997c478bd9Sstevel@tonic-gate  * Get a logical line
14007c478bd9Sstevel@tonic-gate  */
14017c478bd9Sstevel@tonic-gate static off_t
getaline(FILE * f)140250fc488bSToomas Soome getaline(FILE *f)
14037c478bd9Sstevel@tonic-gate {
14047c478bd9Sstevel@tonic-gate 	char	*p;
14057c478bd9Sstevel@tonic-gate 	int	column;
14067c478bd9Sstevel@tonic-gate 	static char multic[MB_LEN_MAX];
14077c478bd9Sstevel@tonic-gate 	static int savlength;
14087c478bd9Sstevel@tonic-gate 	wchar_t c;
14097c478bd9Sstevel@tonic-gate 	int length, width;
14107c478bd9Sstevel@tonic-gate 
14117c478bd9Sstevel@tonic-gate 	if (pipe_in && f == stdin)
14127c478bd9Sstevel@tonic-gate 		rdchar = fgetputc;
14137c478bd9Sstevel@tonic-gate 	else
14147c478bd9Sstevel@tonic-gate 		rdchar = (int (*)())fgetwc;
14157c478bd9Sstevel@tonic-gate 
14167c478bd9Sstevel@tonic-gate 	fileptr = f;
141723a1cceaSRoger A. Faulkner 	/* copy overlap from previous call to getaline */
14187c478bd9Sstevel@tonic-gate 	if (savlength)
14197c478bd9Sstevel@tonic-gate 		(void) strncpy(Line, multic, (size_t)savlength);
14207c478bd9Sstevel@tonic-gate 	for (column = 0, p = Line + savlength; ; ) {
14217c478bd9Sstevel@tonic-gate 		if ((c = mrdchar()) <= 0) {
14227c478bd9Sstevel@tonic-gate 			clearerr(f);
14237c478bd9Sstevel@tonic-gate 			if (p > Line) {	/* last line doesn't have '\n', */
14247c478bd9Sstevel@tonic-gate 				*p++ = '\n';
14257c478bd9Sstevel@tonic-gate 				*p = '\0';	/* print it any way */
14267c478bd9Sstevel@tonic-gate 				return (column);
14277c478bd9Sstevel@tonic-gate 			}
14287c478bd9Sstevel@tonic-gate 			return (EOF);
14297c478bd9Sstevel@tonic-gate 		}
14307c478bd9Sstevel@tonic-gate 		length = wctomb(multic, c);
14317c478bd9Sstevel@tonic-gate 		if (length < 0) {
14327c478bd9Sstevel@tonic-gate 			length = -length;
14337c478bd9Sstevel@tonic-gate 			c = 0;
14347c478bd9Sstevel@tonic-gate 		}
14357c478bd9Sstevel@tonic-gate 		if ((width = wcwidth(c)) < 0)
14367c478bd9Sstevel@tonic-gate 			width = 0;
14377c478bd9Sstevel@tonic-gate 		if (column + width > columns && !fflag)
14387c478bd9Sstevel@tonic-gate 			break;
14397c478bd9Sstevel@tonic-gate 
14407c478bd9Sstevel@tonic-gate 		if (p + length > &Line[LINSIZ - 2] && c != '\n')
14417c478bd9Sstevel@tonic-gate 			break;
14427c478bd9Sstevel@tonic-gate 		(void) strncpy(p, multic, (size_t)length);
14437c478bd9Sstevel@tonic-gate 		p += length;
14447c478bd9Sstevel@tonic-gate 		column += width;
14457c478bd9Sstevel@tonic-gate 		/* don't have any overlap here */
14467c478bd9Sstevel@tonic-gate 		length = 0;
14477c478bd9Sstevel@tonic-gate 		switch (c) {
14487c478bd9Sstevel@tonic-gate 		case '\t': /* just a guess */
14497c478bd9Sstevel@tonic-gate 			column = 1 + (column | 7);
14507c478bd9Sstevel@tonic-gate 			break;
14517c478bd9Sstevel@tonic-gate 		case '\b':
14527c478bd9Sstevel@tonic-gate 			if (column > 0)
14537c478bd9Sstevel@tonic-gate 				column--;
14547c478bd9Sstevel@tonic-gate 			break;
14557c478bd9Sstevel@tonic-gate 		case '\r':
14567c478bd9Sstevel@tonic-gate 			column = 0;
14577c478bd9Sstevel@tonic-gate 			break;
14587c478bd9Sstevel@tonic-gate 		}
14597c478bd9Sstevel@tonic-gate 		if (c == '\n')
14607c478bd9Sstevel@tonic-gate 			break;
14617c478bd9Sstevel@tonic-gate 		if (column >= columns && !fflag)
14627c478bd9Sstevel@tonic-gate 			break;
14637c478bd9Sstevel@tonic-gate 	}
14647c478bd9Sstevel@tonic-gate 	if (c != '\n') { /* We're stopping in the middle of the line */
14657c478bd9Sstevel@tonic-gate 		if (column != columns || !auto_right_margin)
14667c478bd9Sstevel@tonic-gate 			*p++ = '\n';	/* for the display */
146723a1cceaSRoger A. Faulkner 		/* save overlap for next call to getaline */
14687c478bd9Sstevel@tonic-gate 		savlength = length;
14697c478bd9Sstevel@tonic-gate 		if (savlength == 0) {
14707c478bd9Sstevel@tonic-gate 			/*
14717c478bd9Sstevel@tonic-gate 			 * check if following byte is newline and get
14727c478bd9Sstevel@tonic-gate 			 * it if it is
14737c478bd9Sstevel@tonic-gate 			 */
14747c478bd9Sstevel@tonic-gate 			c = fgetwc(f);
14757c478bd9Sstevel@tonic-gate 			if (c == '\n') {
14767c478bd9Sstevel@tonic-gate 				/* gobble and copy (if necessary) newline */
14777c478bd9Sstevel@tonic-gate 				(void) ungetwc(c, f);
14787c478bd9Sstevel@tonic-gate 				(void) (*rdchar)(f);
14797c478bd9Sstevel@tonic-gate 			} else if (c == EOF)
14807c478bd9Sstevel@tonic-gate 				clearerr(f);
14817c478bd9Sstevel@tonic-gate 			else
14827c478bd9Sstevel@tonic-gate 				(void) ungetwc(c, f);
14837c478bd9Sstevel@tonic-gate 		}
14847c478bd9Sstevel@tonic-gate 	} else
14857c478bd9Sstevel@tonic-gate 		savlength = 0;
14867c478bd9Sstevel@tonic-gate 	*p = 0;
14877c478bd9Sstevel@tonic-gate 	return (column);
14887c478bd9Sstevel@tonic-gate }
14897c478bd9Sstevel@tonic-gate 
14907c478bd9Sstevel@tonic-gate static void
save_input(FILE * f)149150fc488bSToomas Soome save_input(FILE *f)
14927c478bd9Sstevel@tonic-gate {
14937c478bd9Sstevel@tonic-gate 	if (pipe_in) {
14947c478bd9Sstevel@tonic-gate 		save_pipe();
14957c478bd9Sstevel@tonic-gate 		in_file = tmp_fin;
14967c478bd9Sstevel@tonic-gate 		pipe_in = 0;
14977c478bd9Sstevel@tonic-gate 	}
14987c478bd9Sstevel@tonic-gate 	(void) fseeko(in_file, (off_t)0, SEEK_SET);
14997c478bd9Sstevel@tonic-gate 	copy_file(in_file, f);
15007c478bd9Sstevel@tonic-gate }
15017c478bd9Sstevel@tonic-gate 
15027c478bd9Sstevel@tonic-gate static void
save_pipe(void)150350fc488bSToomas Soome save_pipe(void)
15047c478bd9Sstevel@tonic-gate {
15057c478bd9Sstevel@tonic-gate 	if (!doliseof)
15067c478bd9Sstevel@tonic-gate 		while (fgetputc(stdin) != EOF)
15077c478bd9Sstevel@tonic-gate 			if (brk_hit) {
15087c478bd9Sstevel@tonic-gate 				brk_hit = 0;
15097c478bd9Sstevel@tonic-gate 				error("Piped input only partially saved");
15107c478bd9Sstevel@tonic-gate 				break;
15117c478bd9Sstevel@tonic-gate 			}
15127c478bd9Sstevel@tonic-gate 	(void) fclose(tmp_fou);
15137c478bd9Sstevel@tonic-gate }
15147c478bd9Sstevel@tonic-gate 
151550fc488bSToomas Soome /*
151650fc488bSToomas Soome  * copy anything read from a pipe to tmp_fou
151750fc488bSToomas Soome  */
15187c478bd9Sstevel@tonic-gate static int
fgetputc(FILE * f)151950fc488bSToomas Soome fgetputc(FILE *f)
15207c478bd9Sstevel@tonic-gate {
15217c478bd9Sstevel@tonic-gate 	int c;
15227c478bd9Sstevel@tonic-gate 
15237c478bd9Sstevel@tonic-gate 	if ((c = fgetwc(f)) != EOF)
15247c478bd9Sstevel@tonic-gate 		(void) fputwc(c, tmp_fou);
15257c478bd9Sstevel@tonic-gate 	return (c);
15267c478bd9Sstevel@tonic-gate }
15277c478bd9Sstevel@tonic-gate 
152850fc488bSToomas Soome /*
152950fc488bSToomas Soome  * initialize line memory
153050fc488bSToomas Soome  */
153150fc488bSToomas Soome static void
lineset(int how)153250fc488bSToomas Soome lineset(int how)
15337c478bd9Sstevel@tonic-gate {
15347c478bd9Sstevel@tonic-gate 	if (zero == NULL) {
15357c478bd9Sstevel@tonic-gate 		nlall = 128;
15367c478bd9Sstevel@tonic-gate 		zero = (LINE *) malloc(nlall * sizeof (LINE));
15377c478bd9Sstevel@tonic-gate 	}
15387c478bd9Sstevel@tonic-gate 	dol = contig = zero;
15397c478bd9Sstevel@tonic-gate 	zero->l_no = 1;
15407c478bd9Sstevel@tonic-gate 	zero->l_addr = 0l;
15417c478bd9Sstevel@tonic-gate 	if (how == BOF) {
15427c478bd9Sstevel@tonic-gate 		dot = zero - 1;
15437c478bd9Sstevel@tonic-gate 		eoflag = 0;
15447c478bd9Sstevel@tonic-gate 		doliseof = 0;
15457c478bd9Sstevel@tonic-gate 		eofl_no = -1;
15467c478bd9Sstevel@tonic-gate 	} else {
15477c478bd9Sstevel@tonic-gate 		dot = dol;
15487c478bd9Sstevel@tonic-gate 		eoflag = 1;
15497c478bd9Sstevel@tonic-gate 		doliseof = 1;
15507c478bd9Sstevel@tonic-gate 		eofl_no = 1;
15517c478bd9Sstevel@tonic-gate 	}
15527c478bd9Sstevel@tonic-gate }
15537c478bd9Sstevel@tonic-gate 
155450fc488bSToomas Soome /*
155550fc488bSToomas Soome  *  add address of new 'dol'
155650fc488bSToomas Soome  * assumes that f is currently at beginning of said line
155750fc488bSToomas Soome  * updates dol
155850fc488bSToomas Soome  */
15597c478bd9Sstevel@tonic-gate static void
newdol(FILE * f)156050fc488bSToomas Soome newdol(FILE *f)
15617c478bd9Sstevel@tonic-gate {
15627c478bd9Sstevel@tonic-gate 	int diff;
15637c478bd9Sstevel@tonic-gate 
15647c478bd9Sstevel@tonic-gate 	if ((dol - zero) + 1 >= nlall) {
15657c478bd9Sstevel@tonic-gate 		LINE *ozero = zero;
15667c478bd9Sstevel@tonic-gate 
15677c478bd9Sstevel@tonic-gate 		nlall += 512;
15687c478bd9Sstevel@tonic-gate 		if ((zero = (LINE *)realloc((char *)zero,
15697c478bd9Sstevel@tonic-gate 		    (unsigned)(nlall * sizeof (LINE)))) == NULL) {
15707c478bd9Sstevel@tonic-gate 			zero = ozero;
15717c478bd9Sstevel@tonic-gate 			compact();
15727c478bd9Sstevel@tonic-gate 		}
15737c478bd9Sstevel@tonic-gate 		diff = (int)((int)zero - (int)ozero);
15747c478bd9Sstevel@tonic-gate 		dot = (LINE *)((int)dot + diff);
15757c478bd9Sstevel@tonic-gate 		dol = (LINE *)((int)dol + diff);
15767c478bd9Sstevel@tonic-gate 		contig = (LINE *)((int)contig + diff);
15777c478bd9Sstevel@tonic-gate 	}
15787c478bd9Sstevel@tonic-gate 	dol++;
15797c478bd9Sstevel@tonic-gate 	if (!pipe_in)
15807c478bd9Sstevel@tonic-gate 		dol->l_addr = (off_t)ftello(f);
15817c478bd9Sstevel@tonic-gate 	else {
15827c478bd9Sstevel@tonic-gate 		(void) fflush(tmp_fou);
15837c478bd9Sstevel@tonic-gate 		dol->l_addr = (off_t)ftello(tmp_fou);
15847c478bd9Sstevel@tonic-gate 	}
15857c478bd9Sstevel@tonic-gate 	dol->l_no = (dol-1)->l_no + 1;
15867c478bd9Sstevel@tonic-gate }
15877c478bd9Sstevel@tonic-gate 
15887c478bd9Sstevel@tonic-gate static void
compact(void)158950fc488bSToomas Soome compact(void)
15907c478bd9Sstevel@tonic-gate {
15917c478bd9Sstevel@tonic-gate 	(void) perror("realloc");
15927c478bd9Sstevel@tonic-gate 	end_it();
15937c478bd9Sstevel@tonic-gate 
15947c478bd9Sstevel@tonic-gate }
15957c478bd9Sstevel@tonic-gate 
15967c478bd9Sstevel@tonic-gate static void
terminit(void)159750fc488bSToomas Soome terminit(void)	/* set up terminal dependencies from termlib */
15987c478bd9Sstevel@tonic-gate {
15997c478bd9Sstevel@tonic-gate 	int err_ret;
16007c478bd9Sstevel@tonic-gate 	struct termio ntty;
16017c478bd9Sstevel@tonic-gate 
16027c478bd9Sstevel@tonic-gate 	for (;;) {
1603f48205beScasper 		pid_t my_tgid;
1604f48205beScasper 		my_tgid = tcgetpgrp(1);
1605f48205beScasper 		if (my_tgid == -1 || my_tgid == my_pgid)
16067c478bd9Sstevel@tonic-gate 			break;
16077c478bd9Sstevel@tonic-gate 		(void) kill(-my_pgid, SIGTTOU);
16087c478bd9Sstevel@tonic-gate 	}
16097c478bd9Sstevel@tonic-gate 
16107c478bd9Sstevel@tonic-gate 	if ((freopen("/dev/tty", "r+", stdout)) == NULL) {
16117c478bd9Sstevel@tonic-gate 		(void) perror("open");
16127c478bd9Sstevel@tonic-gate 		exit(1);
16137c478bd9Sstevel@tonic-gate 	}
16147c478bd9Sstevel@tonic-gate 	(void) ioctl(fileno(stdout), TCGETA, &otty);
16157c478bd9Sstevel@tonic-gate 	termflg = 1;
16167c478bd9Sstevel@tonic-gate 
16177c478bd9Sstevel@tonic-gate 	(void) setupterm(0, fileno(stdout), &err_ret);
16187c478bd9Sstevel@tonic-gate 	(void) ioctl(fileno(stdout), TCGETA, &ntty);
16197c478bd9Sstevel@tonic-gate 	ntty.c_lflag &= ~(ECHONL | ECHO | ICANON);
16207c478bd9Sstevel@tonic-gate 	ntty.c_cc[VMIN] = 1;
16217c478bd9Sstevel@tonic-gate 	ntty.c_cc[VTIME] = 1;
16227c478bd9Sstevel@tonic-gate 	(void) ioctl(fileno(stdout), TCSETAW, &ntty);
16237c478bd9Sstevel@tonic-gate 	pg_stdin = fdopen(dup(fileno(stdout)), "r");
16247c478bd9Sstevel@tonic-gate 	(void) saveterm();
16257c478bd9Sstevel@tonic-gate 	(void) resetterm();
16267c478bd9Sstevel@tonic-gate 	if (lines <= 0 || hard_copy) {
16277c478bd9Sstevel@tonic-gate 		hard_copy = 1;
16287c478bd9Sstevel@tonic-gate 		lines = 24;
16297c478bd9Sstevel@tonic-gate 	}
16307c478bd9Sstevel@tonic-gate 	if (columns <= 0)
16317c478bd9Sstevel@tonic-gate 		columns = 80;
16327c478bd9Sstevel@tonic-gate 	if (clropt && !clear_screen)
16337c478bd9Sstevel@tonic-gate 		clropt = 0;
16347c478bd9Sstevel@tonic-gate 	if ((shell = getenv("SHELL")) == (char *)NULL)
16357c478bd9Sstevel@tonic-gate 			shell = "/usr/bin/sh";
16367c478bd9Sstevel@tonic-gate }
16377c478bd9Sstevel@tonic-gate 
16387c478bd9Sstevel@tonic-gate static void
error(char * mess)163950fc488bSToomas Soome error(char *mess)
16407c478bd9Sstevel@tonic-gate {
16417c478bd9Sstevel@tonic-gate 	kill_line();
16427c478bd9Sstevel@tonic-gate 	sopr(gettext(mess), 1);
16437c478bd9Sstevel@tonic-gate 	prompt((char *)NULL);
16447c478bd9Sstevel@tonic-gate 	errors++;
16457c478bd9Sstevel@tonic-gate }
16467c478bd9Sstevel@tonic-gate 
16477c478bd9Sstevel@tonic-gate static void
prompt(char * filename)164850fc488bSToomas Soome prompt(char *filename)
16497c478bd9Sstevel@tonic-gate {
16507c478bd9Sstevel@tonic-gate 	char outstr[PROMPTSIZE+6];
16517c478bd9Sstevel@tonic-gate 	int pagenum;
16527c478bd9Sstevel@tonic-gate 	if (filename != NULL) {
16537c478bd9Sstevel@tonic-gate 		/*
16547c478bd9Sstevel@tonic-gate 		 * TRANSLATION_NOTE
16557c478bd9Sstevel@tonic-gate 		 * 	%s is a filename.
16567c478bd9Sstevel@tonic-gate 		 */
16577c478bd9Sstevel@tonic-gate 		(void) sprintf(outstr, gettext("(Next file: %s)"), filename);
16587c478bd9Sstevel@tonic-gate 	} else {
165950fc488bSToomas Soome 		if ((pagenum = (int)((new_ss.last_line-2)/(window-1)+1)) >
166050fc488bSToomas Soome 		    999999) {
16617c478bd9Sstevel@tonic-gate 			pagenum = 999999;
166250fc488bSToomas Soome 		}
16637c478bd9Sstevel@tonic-gate 		(void) sprintf(outstr, promptstr, pagenum);
16647c478bd9Sstevel@tonic-gate 	}
16657c478bd9Sstevel@tonic-gate 	sopr(outstr, 1);
16667c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
16677c478bd9Sstevel@tonic-gate }
16687c478bd9Sstevel@tonic-gate 
16697c478bd9Sstevel@tonic-gate /*
16707c478bd9Sstevel@tonic-gate  *  sopr puts out the message (please no \n's) surrounded by standout
16717c478bd9Sstevel@tonic-gate  *  begins and ends
16727c478bd9Sstevel@tonic-gate  */
16737c478bd9Sstevel@tonic-gate 
16747c478bd9Sstevel@tonic-gate static void
sopr(char * m,int count)167550fc488bSToomas Soome sopr(char *m, int count)
16767c478bd9Sstevel@tonic-gate {
16777c478bd9Sstevel@tonic-gate 	wchar_t	wc;
16787c478bd9Sstevel@tonic-gate 	int	len, n;
16797c478bd9Sstevel@tonic-gate 	char	*p;
16807c478bd9Sstevel@tonic-gate 
16817c478bd9Sstevel@tonic-gate 	if (count) {
16827c478bd9Sstevel@tonic-gate 		p = m;
16837c478bd9Sstevel@tonic-gate 		for (; *p; p += len) {
16847c478bd9Sstevel@tonic-gate 			if ((len = mbtowc(&wc, p, MB_CUR_MAX)) <= 0) {
16857c478bd9Sstevel@tonic-gate 				len = 1;
16867c478bd9Sstevel@tonic-gate 				continue;
16877c478bd9Sstevel@tonic-gate 			}
16887c478bd9Sstevel@tonic-gate 			if ((n = wcwidth(wc)) > 0)
16897c478bd9Sstevel@tonic-gate 				promptlen += n;
16907c478bd9Sstevel@tonic-gate 		}
16917c478bd9Sstevel@tonic-gate 	}
16927c478bd9Sstevel@tonic-gate 	if (soflag && enter_standout_mode && exit_standout_mode) {
16937c478bd9Sstevel@tonic-gate 		(void) putp(enter_standout_mode);
16947c478bd9Sstevel@tonic-gate 		(void) fputs(m, stdout);
16957c478bd9Sstevel@tonic-gate 		(void) putp(exit_standout_mode);
16967c478bd9Sstevel@tonic-gate 	}
16977c478bd9Sstevel@tonic-gate 	else
16987c478bd9Sstevel@tonic-gate 		(void) fputs(m, stdout);
16997c478bd9Sstevel@tonic-gate }
17007c478bd9Sstevel@tonic-gate 
17017c478bd9Sstevel@tonic-gate static void
doclear(void)170250fc488bSToomas Soome doclear(void)
17037c478bd9Sstevel@tonic-gate {
17047c478bd9Sstevel@tonic-gate 	if (clear_screen)
17057c478bd9Sstevel@tonic-gate 		(void) putp(clear_screen);
17067c478bd9Sstevel@tonic-gate 	(void) putchar('\r');  /* this resets the terminal drivers character */
17077c478bd9Sstevel@tonic-gate 			/* count in case it is trying to expand tabs  */
17087c478bd9Sstevel@tonic-gate 
17097c478bd9Sstevel@tonic-gate }
17107c478bd9Sstevel@tonic-gate 
17117c478bd9Sstevel@tonic-gate static void
kill_line(void)171250fc488bSToomas Soome kill_line(void)
17137c478bd9Sstevel@tonic-gate {
17147c478bd9Sstevel@tonic-gate 	erase_line(0);
17157c478bd9Sstevel@tonic-gate 	if (!clr_eol) (void) putchar('\r');
17167c478bd9Sstevel@tonic-gate 
17177c478bd9Sstevel@tonic-gate }
17187c478bd9Sstevel@tonic-gate 
17197c478bd9Sstevel@tonic-gate /* erase from after col to end of prompt */
17207c478bd9Sstevel@tonic-gate static void
erase_line(int col)172150fc488bSToomas Soome erase_line(int col)
17227c478bd9Sstevel@tonic-gate {
17237c478bd9Sstevel@tonic-gate 
17247c478bd9Sstevel@tonic-gate 	if (promptlen == 0)
17257c478bd9Sstevel@tonic-gate 		return;
17267c478bd9Sstevel@tonic-gate 	if (hard_copy)
17277c478bd9Sstevel@tonic-gate 		(void) putchar('\n');
17287c478bd9Sstevel@tonic-gate 	else {
17297c478bd9Sstevel@tonic-gate 		if (col == 0)
17307c478bd9Sstevel@tonic-gate 			(void) putchar('\r');
17317c478bd9Sstevel@tonic-gate 		if (clr_eol) {
17327c478bd9Sstevel@tonic-gate 			(void) putp(clr_eol);
17337c478bd9Sstevel@tonic-gate 			/* for the terminal driver again */
17347c478bd9Sstevel@tonic-gate 			(void) putchar('\r');
17357c478bd9Sstevel@tonic-gate 		}
17367c478bd9Sstevel@tonic-gate 		else
17377c478bd9Sstevel@tonic-gate 			for (col = promptlen - col; col > 0; col--)
17387c478bd9Sstevel@tonic-gate 				(void) putchar(' ');
17397c478bd9Sstevel@tonic-gate 	}
17407c478bd9Sstevel@tonic-gate 	promptlen = 0;
17417c478bd9Sstevel@tonic-gate }
17427c478bd9Sstevel@tonic-gate 
17437c478bd9Sstevel@tonic-gate /*
17447c478bd9Sstevel@tonic-gate  * Come here if a quit or interrupt signal is received
17457c478bd9Sstevel@tonic-gate  */
17467c478bd9Sstevel@tonic-gate 
17477c478bd9Sstevel@tonic-gate static void
on_brk(int sno)174850fc488bSToomas Soome on_brk(int sno)
17497c478bd9Sstevel@tonic-gate {
17507c478bd9Sstevel@tonic-gate 	(void) signal(sno, on_brk);
17517c478bd9Sstevel@tonic-gate 	if (!inwait) {
17527c478bd9Sstevel@tonic-gate 		BEEP();
17537c478bd9Sstevel@tonic-gate 		brk_hit = 1;
17547c478bd9Sstevel@tonic-gate 	} else {
17557c478bd9Sstevel@tonic-gate 		brk_hit = 0;
17567c478bd9Sstevel@tonic-gate 		longjmp(restore, 1);
17577c478bd9Sstevel@tonic-gate 	}
17587c478bd9Sstevel@tonic-gate }
17597c478bd9Sstevel@tonic-gate 
17607c478bd9Sstevel@tonic-gate /*
17617c478bd9Sstevel@tonic-gate  * Clean up terminal state and exit.
17627c478bd9Sstevel@tonic-gate  */
17637c478bd9Sstevel@tonic-gate 
17647c478bd9Sstevel@tonic-gate void
end_it(void)176550fc488bSToomas Soome end_it(void)
17667c478bd9Sstevel@tonic-gate {
17677c478bd9Sstevel@tonic-gate 
17687c478bd9Sstevel@tonic-gate 	if (out_is_tty) {
17697c478bd9Sstevel@tonic-gate 		kill_line();
17707c478bd9Sstevel@tonic-gate 		(void) resetterm();
17717c478bd9Sstevel@tonic-gate 		if (termflg)
17727c478bd9Sstevel@tonic-gate 			(void) ioctl(fileno(stdout), TCSETAW, &otty);
17737c478bd9Sstevel@tonic-gate 	}
17747c478bd9Sstevel@tonic-gate 	if (tmp_fin)
17757c478bd9Sstevel@tonic-gate 		(void) fclose(tmp_fin);
17767c478bd9Sstevel@tonic-gate 	if (tmp_fou)
17777c478bd9Sstevel@tonic-gate 		(void) fclose(tmp_fou);
17787c478bd9Sstevel@tonic-gate 	if (tmp_fou || tmp_fin)
17797c478bd9Sstevel@tonic-gate 		(void) unlink(tmp_name);
17807c478bd9Sstevel@tonic-gate 	exit(status);
17817c478bd9Sstevel@tonic-gate }
17827c478bd9Sstevel@tonic-gate 
17837c478bd9Sstevel@tonic-gate void
onsusp(void)178450fc488bSToomas Soome onsusp(void)
17857c478bd9Sstevel@tonic-gate {
17867c478bd9Sstevel@tonic-gate 	int ttou_is_dfl;
17877c478bd9Sstevel@tonic-gate 
17887c478bd9Sstevel@tonic-gate 	/* ignore SIGTTOU so following resetterm and flush works */
17897c478bd9Sstevel@tonic-gate 	ttou_is_dfl = (signal(SIGTTOU, SIG_IGN) == SIG_DFL);
17907c478bd9Sstevel@tonic-gate 	(void) resetterm();
17917c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
17927c478bd9Sstevel@tonic-gate 	if (ttou_is_dfl)
17937c478bd9Sstevel@tonic-gate 		(void) signal(SIGTTOU, SIG_DFL);
17947c478bd9Sstevel@tonic-gate 
17957c478bd9Sstevel@tonic-gate 	/* send SIGTSTP to stop this process group */
17967c478bd9Sstevel@tonic-gate 	(void) signal(SIGTSTP, SIG_DFL);
17977c478bd9Sstevel@tonic-gate 	(void) kill(-my_pgid, SIGTSTP);
17987c478bd9Sstevel@tonic-gate 
17997c478bd9Sstevel@tonic-gate 	/* continued - reset the terminal */
18007c478bd9Sstevel@tonic-gate #ifdef __STDC__
18017c478bd9Sstevel@tonic-gate 	(void) signal(SIGTSTP, (void (*)(int))onsusp);
18027c478bd9Sstevel@tonic-gate #else
18037c478bd9Sstevel@tonic-gate 	(void) signal(SIGTSTP, (void (*))onsusp);
18047c478bd9Sstevel@tonic-gate #endif
18057c478bd9Sstevel@tonic-gate 	(void) resetterm();
18067c478bd9Sstevel@tonic-gate 	if (inwait)
18077c478bd9Sstevel@tonic-gate 		longjmp(restore, -1);
18087c478bd9Sstevel@tonic-gate 
18097c478bd9Sstevel@tonic-gate }
18107c478bd9Sstevel@tonic-gate 
18117c478bd9Sstevel@tonic-gate static char *
pg_strchr(char * str,wchar_t c)181250fc488bSToomas Soome pg_strchr(char *str, wchar_t c)
18137c478bd9Sstevel@tonic-gate {
18147c478bd9Sstevel@tonic-gate 	while (*str) {
18157c478bd9Sstevel@tonic-gate 		if (c == *str)
18167c478bd9Sstevel@tonic-gate 			return (str);
18177c478bd9Sstevel@tonic-gate 		str++;
18187c478bd9Sstevel@tonic-gate 	}
18197c478bd9Sstevel@tonic-gate 	return (0);
18207c478bd9Sstevel@tonic-gate }
18217c478bd9Sstevel@tonic-gate 
18227c478bd9Sstevel@tonic-gate void
usage(void)182350fc488bSToomas Soome usage(void)
18247c478bd9Sstevel@tonic-gate {
18257c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
18267c478bd9Sstevel@tonic-gate "Usage: pg [-number] [-p string] [-cefnrs] [+line] [+/pattern/] files\n"));
18277c478bd9Sstevel@tonic-gate 	exit(1);
18287c478bd9Sstevel@tonic-gate }
1829