xref: /illumos-gate/usr/src/cmd/pr/pr.c (revision 3d19b30e50b972b09ded7f37b8a5fc36d10588e6)
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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  *	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T
297c478bd9Sstevel@tonic-gate  *	All Rights Reserved
307c478bd9Sstevel@tonic-gate  */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
347c478bd9Sstevel@tonic-gate  * The Regents of the University of California
357c478bd9Sstevel@tonic-gate  * All Rights Reserved
367c478bd9Sstevel@tonic-gate  *
377c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
387c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
397c478bd9Sstevel@tonic-gate  * contributors.
407c478bd9Sstevel@tonic-gate  */
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate /*
437c478bd9Sstevel@tonic-gate  *	PR command (print files in pages and columns, with headings)
447c478bd9Sstevel@tonic-gate  *	2+head+2+page[56]+5
457c478bd9Sstevel@tonic-gate  */
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate #include <stdio.h>
487c478bd9Sstevel@tonic-gate #include <signal.h>
497c478bd9Sstevel@tonic-gate #include <ctype.h>
507c478bd9Sstevel@tonic-gate #include <sys/types.h>
517c478bd9Sstevel@tonic-gate #include <sys/stat.h>
527c478bd9Sstevel@tonic-gate #include <unistd.h>
537c478bd9Sstevel@tonic-gate #include <stdlib.h>
547c478bd9Sstevel@tonic-gate #include <locale.h>
557c478bd9Sstevel@tonic-gate #include <string.h>
567c478bd9Sstevel@tonic-gate #include <limits.h>
577c478bd9Sstevel@tonic-gate #include <wchar.h>
587c478bd9Sstevel@tonic-gate #include <errno.h>
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate #define	ESC		'\033'
617c478bd9Sstevel@tonic-gate #define	LENGTH		66
627c478bd9Sstevel@tonic-gate #define	LINEW		72
637c478bd9Sstevel@tonic-gate #define	NUMW		5
647c478bd9Sstevel@tonic-gate #define	MARGIN		10
657c478bd9Sstevel@tonic-gate #define	DEFTAB		8
667c478bd9Sstevel@tonic-gate #define	NFILES		10
677c478bd9Sstevel@tonic-gate #define	STDINNAME()	nulls
687c478bd9Sstevel@tonic-gate #define	PROMPT()	(void) putc('\7', stderr) /* BEL */
697c478bd9Sstevel@tonic-gate #define	NOFILE		nulls
707c478bd9Sstevel@tonic-gate #define	ETABS		(Inpos % Etabn)
717c478bd9Sstevel@tonic-gate #define	NSEPC		'\t'
727c478bd9Sstevel@tonic-gate #define	HEAD		gettext("%s  %s Page %d\n\n\n"), date, head, Page
737c478bd9Sstevel@tonic-gate #define	cerror(S)	(void) fprintf(stderr, "pr: %s", gettext(S))
747c478bd9Sstevel@tonic-gate #define	done()		if (Ttyout) (void) chmod(Ttyout, Mode)
757c478bd9Sstevel@tonic-gate #define	ALL_NUMS(s)	(strspn(s, "0123456789") == strlen(s))
767c478bd9Sstevel@tonic-gate #define	REMOVE_ARG(argc, argp)					\
777c478bd9Sstevel@tonic-gate 			{					\
787c478bd9Sstevel@tonic-gate 				char	**p = argp;		\
797c478bd9Sstevel@tonic-gate 				while (*p != NULL)		\
807c478bd9Sstevel@tonic-gate 				{				\
817c478bd9Sstevel@tonic-gate 					*p = *(p + 1);		\
827c478bd9Sstevel@tonic-gate 					p++;			\
837c478bd9Sstevel@tonic-gate 				}				\
847c478bd9Sstevel@tonic-gate 				argc--;				\
857c478bd9Sstevel@tonic-gate 			}
867c478bd9Sstevel@tonic-gate #define	SQUEEZE_ARG(argp, ind, n)					\
877c478bd9Sstevel@tonic-gate 			{					\
887c478bd9Sstevel@tonic-gate 				int	i;			\
897c478bd9Sstevel@tonic-gate 				for (i = ind; argp[i]; i++)	\
907c478bd9Sstevel@tonic-gate 					argp[i] = argp[i + n];	\
917c478bd9Sstevel@tonic-gate 			}
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate /*
947c478bd9Sstevel@tonic-gate  *   ---date time format---
957c478bd9Sstevel@tonic-gate  *   b -- abbreviated month name
967c478bd9Sstevel@tonic-gate  *   e -- day of month
977c478bd9Sstevel@tonic-gate  *   H -- Hour (24 hour version)
987c478bd9Sstevel@tonic-gate  *   M -- Minute
997c478bd9Sstevel@tonic-gate  *   Y -- Year in the form ccyy
1007c478bd9Sstevel@tonic-gate  */
1017c478bd9Sstevel@tonic-gate #define	FORMAT		"%b %e %H:%M %Y"
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate typedef	int	ANY;
1047c478bd9Sstevel@tonic-gate typedef	unsigned	int	UNS;
1057c478bd9Sstevel@tonic-gate typedef	struct	{ FILE *f_f; char *f_name; wchar_t f_nextc; } FILS;
1067c478bd9Sstevel@tonic-gate typedef	struct	{int fold; int skip; int eof; } foldinf;
1077c478bd9Sstevel@tonic-gate typedef	struct	{ wchar_t *c_ptr, *c_ptr0; long c_lno; int c_skip; } *COLP;
1087c478bd9Sstevel@tonic-gate typedef struct	err { struct err *e_nextp; char *e_mess; } ERR;
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate /*
1117c478bd9Sstevel@tonic-gate  * Global data.
1127c478bd9Sstevel@tonic-gate  */
1137c478bd9Sstevel@tonic-gate static	FILS	*Files;
1147c478bd9Sstevel@tonic-gate static	mode_t	Mode;
1157c478bd9Sstevel@tonic-gate static	int	Multi = 0;
1167c478bd9Sstevel@tonic-gate static	int	Nfiles = 0;
1177c478bd9Sstevel@tonic-gate static	int	Error = 0;
1187c478bd9Sstevel@tonic-gate static	char	nulls[] = "";
1197c478bd9Sstevel@tonic-gate static	char	*Ttyout;
1207c478bd9Sstevel@tonic-gate static	char	obuf[BUFSIZ];
1217c478bd9Sstevel@tonic-gate static	char	time_buf[50];	/* array to hold the time and date */
1227c478bd9Sstevel@tonic-gate static	long	Lnumb = 0;
1237c478bd9Sstevel@tonic-gate static	FILE	*Ttyin = stdin;
1247c478bd9Sstevel@tonic-gate static	int	Dblspace = 1;
1257c478bd9Sstevel@tonic-gate static	int	Fpage = 1;
1267c478bd9Sstevel@tonic-gate static	int	Formfeed = 0;
1277c478bd9Sstevel@tonic-gate static	int	Length = LENGTH;
1287c478bd9Sstevel@tonic-gate static	int	Linew = 0;
1297c478bd9Sstevel@tonic-gate static	int	Offset = 0;
1307c478bd9Sstevel@tonic-gate static	int	Ncols = 0;
1317c478bd9Sstevel@tonic-gate static	int	Pause = 0;
1327c478bd9Sstevel@tonic-gate static	wchar_t	Sepc = 0;
1337c478bd9Sstevel@tonic-gate static	int	Colw;
1347c478bd9Sstevel@tonic-gate static	int	Plength;
1357c478bd9Sstevel@tonic-gate static	int	Margin = MARGIN;
1367c478bd9Sstevel@tonic-gate static	int	Numw;
1377c478bd9Sstevel@tonic-gate static	int	Nsepc = NSEPC;
1387c478bd9Sstevel@tonic-gate static	int	Report = 1;
1397c478bd9Sstevel@tonic-gate static	int	Etabn = 0;
1407c478bd9Sstevel@tonic-gate static	wchar_t	Etabc = '\t';
1417c478bd9Sstevel@tonic-gate static	int	Itabn = 0;
1427c478bd9Sstevel@tonic-gate static	wchar_t	Itabc = '\t';
1437c478bd9Sstevel@tonic-gate static	int	fold = 0;
1447c478bd9Sstevel@tonic-gate static	int	foldcol = 0;
1457c478bd9Sstevel@tonic-gate static	int	alleof = 0;
1467c478bd9Sstevel@tonic-gate static	char	*Head = NULL;
1477c478bd9Sstevel@tonic-gate static	wchar_t *Buffer = NULL, *Bufend, *Bufptr;
1487c478bd9Sstevel@tonic-gate static	UNS	Buflen;
1497c478bd9Sstevel@tonic-gate static	COLP	Colpts;
1507c478bd9Sstevel@tonic-gate static	foldinf	*Fcol;
1517c478bd9Sstevel@tonic-gate static	int	Page;
1527c478bd9Sstevel@tonic-gate static	wchar_t	C = '\0';
1537c478bd9Sstevel@tonic-gate static	int	Nspace;
1547c478bd9Sstevel@tonic-gate static	int	Inpos;
1557c478bd9Sstevel@tonic-gate static	int	Outpos;
1567c478bd9Sstevel@tonic-gate static	int	Lcolpos;
1577c478bd9Sstevel@tonic-gate static	int	Pcolpos;
1587c478bd9Sstevel@tonic-gate static	int	Line;
1597c478bd9Sstevel@tonic-gate static	ERR	*Err = NULL;
1607c478bd9Sstevel@tonic-gate static	ERR	*Lasterr = (ERR *)&Err;
1617c478bd9Sstevel@tonic-gate static	int	mbcurmax = 1;
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate /*
1647c478bd9Sstevel@tonic-gate  * Function prototypes.
1657c478bd9Sstevel@tonic-gate  */
1667c478bd9Sstevel@tonic-gate static	void	onintr();
1677c478bd9Sstevel@tonic-gate static	ANY	*getspace();
1687c478bd9Sstevel@tonic-gate static	int	findopt(int, char **);
1697c478bd9Sstevel@tonic-gate static	void	fixtty();
1707c478bd9Sstevel@tonic-gate static	char 	*GETDATE();
1717c478bd9Sstevel@tonic-gate static	char	*ffiler(char *);
1727c478bd9Sstevel@tonic-gate static	int	print(char *);
1737c478bd9Sstevel@tonic-gate static	void	putpage();
1747c478bd9Sstevel@tonic-gate static	void	foldpage();
1757c478bd9Sstevel@tonic-gate static	void	nexbuf();
1767c478bd9Sstevel@tonic-gate static	void	foldbuf();
1777c478bd9Sstevel@tonic-gate static	void	balance(int);
1787c478bd9Sstevel@tonic-gate static	int	readbuf(wchar_t **, int, COLP);
1797c478bd9Sstevel@tonic-gate static	wint_t	get(int);
1807c478bd9Sstevel@tonic-gate static	int	put(wchar_t);
1817c478bd9Sstevel@tonic-gate static	void	putspace();
1827c478bd9Sstevel@tonic-gate static	void	unget(int);
1837c478bd9Sstevel@tonic-gate static	FILE	*mustopen(char *, FILS *);
1847c478bd9Sstevel@tonic-gate static	void	die(char *);
1857c478bd9Sstevel@tonic-gate static	void	errprint();
1867c478bd9Sstevel@tonic-gate static	void	usage(int);
1877c478bd9Sstevel@tonic-gate static wint_t	_fgetwc_pr(FILE *, int *);
1887c478bd9Sstevel@tonic-gate static size_t	freadw(wchar_t *, size_t, FILE *);
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 
191a77d64afScf46844 int
main(int argc,char ** argv)1927c478bd9Sstevel@tonic-gate main(int argc, char **argv)
1937c478bd9Sstevel@tonic-gate {
1947c478bd9Sstevel@tonic-gate 	FILS	fstr[NFILES];
1957c478bd9Sstevel@tonic-gate 	int	nfdone = 0;
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	/* Get locale variables for environment */
1997c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
2027c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
2037c478bd9Sstevel@tonic-gate #endif
2047c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	mbcurmax = MB_CUR_MAX;
2077c478bd9Sstevel@tonic-gate 	Files = fstr;
2087c478bd9Sstevel@tonic-gate 	for (argc = findopt(argc, argv); argc > 0; --argc, ++argv) {
2097c478bd9Sstevel@tonic-gate 		if (Multi == 'm') {
2107c478bd9Sstevel@tonic-gate 			if (Nfiles >= NFILES - 1) die("too many files");
2117c478bd9Sstevel@tonic-gate 			if (mustopen(*argv, &Files[Nfiles++]) == NULL)
2127c478bd9Sstevel@tonic-gate 				++nfdone;	/* suppress printing */
2137c478bd9Sstevel@tonic-gate 		} else {
2147c478bd9Sstevel@tonic-gate 			if (print(*argv))
2157c478bd9Sstevel@tonic-gate 				(void) fclose(Files->f_f);
2167c478bd9Sstevel@tonic-gate 			++nfdone;
2177c478bd9Sstevel@tonic-gate 		}
2187c478bd9Sstevel@tonic-gate 	}
2197c478bd9Sstevel@tonic-gate 	if (!nfdone)	/* no files named, use stdin */
2207c478bd9Sstevel@tonic-gate 		(void) print(NOFILE);	/* on GCOS, use current file, if any */
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	if (Report) {
2237c478bd9Sstevel@tonic-gate 		errprint();	/* print accumulated error reports */
2247c478bd9Sstevel@tonic-gate 		exit(Error);
2257c478bd9Sstevel@tonic-gate 	}
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 	return (Error);
2287c478bd9Sstevel@tonic-gate }
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate /*
2327c478bd9Sstevel@tonic-gate  * findopt() returns argc modified to be the number of explicitly supplied
2337c478bd9Sstevel@tonic-gate  * filenames, including '-', the explicit request to use stdin.
2347c478bd9Sstevel@tonic-gate  * argc == 0 implies that no filenames were supplied and stdin should be used.
2357c478bd9Sstevel@tonic-gate  * Options are striped from argv and only file names are returned.
2367c478bd9Sstevel@tonic-gate  */
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate static	int
findopt(int argc,char ** argv)2397c478bd9Sstevel@tonic-gate findopt(int argc, char **argv)
2407c478bd9Sstevel@tonic-gate {
2417c478bd9Sstevel@tonic-gate 	int	eargc = 0;
2427c478bd9Sstevel@tonic-gate 	int	c;
2437c478bd9Sstevel@tonic-gate 	int	mflg = 0;
2447c478bd9Sstevel@tonic-gate 	int	aflg = 0;
2457c478bd9Sstevel@tonic-gate 	int	optnum;
2467c478bd9Sstevel@tonic-gate 	int	argv_ind;
2477c478bd9Sstevel@tonic-gate 	int	end_opt;
2487c478bd9Sstevel@tonic-gate 	int	i;
249a8ae9964Sss161016 	int	isarg = 0;
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 	fixtty();
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	/* Handle page number option */
2547c478bd9Sstevel@tonic-gate 	for (optnum = 1, end_opt = 0; optnum < argc && !end_opt; optnum++) {
2557c478bd9Sstevel@tonic-gate 		switch (*argv[optnum]) {
2567c478bd9Sstevel@tonic-gate 		case '+':
2577c478bd9Sstevel@tonic-gate 			/* check for all digits */
2587c478bd9Sstevel@tonic-gate 			if (strlen(&argv[optnum][1]) !=
2597c478bd9Sstevel@tonic-gate 			    strspn(&argv[optnum][1], "0123456789")) {
2607c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
2617c478bd9Sstevel@tonic-gate 				    "pr: Badly formed number\n"));
2627c478bd9Sstevel@tonic-gate 				exit(1);
2637c478bd9Sstevel@tonic-gate 			}
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 			if ((Fpage = (int)strtol(&argv[optnum][1],
2667c478bd9Sstevel@tonic-gate 			    (char **)NULL, 10)) < 0) {
2677c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
2687c478bd9Sstevel@tonic-gate 				    "pr: Badly formed number\n"));
2697c478bd9Sstevel@tonic-gate 				exit(1);
2707c478bd9Sstevel@tonic-gate 			}
2717c478bd9Sstevel@tonic-gate 			REMOVE_ARG(argc, &argv[optnum]);
2727c478bd9Sstevel@tonic-gate 			optnum--;
2737c478bd9Sstevel@tonic-gate 			break;
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 		case '-':
2767c478bd9Sstevel@tonic-gate 			/* Check for end of options */
2777c478bd9Sstevel@tonic-gate 			if (argv[optnum][1] == '-') {
2787c478bd9Sstevel@tonic-gate 				end_opt++;
2797c478bd9Sstevel@tonic-gate 				break;
2807c478bd9Sstevel@tonic-gate 			}
281a8ae9964Sss161016 
282a8ae9964Sss161016 			if (argv[optnum][1] == 'h' || argv[optnum][1] == 'l' ||
283a8ae9964Sss161016 			    argv[optnum][1] == 'o' || argv[optnum][1] == 'w')
284a8ae9964Sss161016 				isarg = 1;
285a8ae9964Sss161016 			else
286a8ae9964Sss161016 				isarg = 0;
287a8ae9964Sss161016 
2887c478bd9Sstevel@tonic-gate 			break;
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 		default:
291a8ae9964Sss161016 			if (isarg == 0)
2927c478bd9Sstevel@tonic-gate 				end_opt++;
293a8ae9964Sss161016 			else
294a8ae9964Sss161016 				isarg = 0;
2957c478bd9Sstevel@tonic-gate 			break;
2967c478bd9Sstevel@tonic-gate 		}
2977c478bd9Sstevel@tonic-gate 	}
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	/*
3007c478bd9Sstevel@tonic-gate 	 * Handle options with optional arguments.
3017c478bd9Sstevel@tonic-gate 	 * If optional arguments are present they may not be separated
3027c478bd9Sstevel@tonic-gate 	 * from the option letter.
3037c478bd9Sstevel@tonic-gate 	 */
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 	for (optnum = 1; optnum < argc; optnum++) {
3067c478bd9Sstevel@tonic-gate 		if (argv[optnum][0] == '-' && argv[optnum][1] == '-')
3077c478bd9Sstevel@tonic-gate 			/* End of options */
3087c478bd9Sstevel@tonic-gate 			break;
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 		if (argv[optnum][0] == '-' && argv[optnum][1] == '\0')
3117c478bd9Sstevel@tonic-gate 			/* stdin file name */
3127c478bd9Sstevel@tonic-gate 			continue;
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 		if (argv[optnum][0] != '-')
3157c478bd9Sstevel@tonic-gate 			/* not option */
3167c478bd9Sstevel@tonic-gate 			continue;
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate 		for (argv_ind = 1; argv[optnum][argv_ind] != '\0'; argv_ind++) {
3197c478bd9Sstevel@tonic-gate 			switch (argv[optnum][argv_ind]) {
3207c478bd9Sstevel@tonic-gate 			case 'e':
3217c478bd9Sstevel@tonic-gate 				SQUEEZE_ARG(argv[optnum], argv_ind, 1);
3227c478bd9Sstevel@tonic-gate 				if ((c = argv[optnum][argv_ind]) != '\0' &&
3237c478bd9Sstevel@tonic-gate 				    !isdigit(c)) {
3247c478bd9Sstevel@tonic-gate 					int	r;
3257c478bd9Sstevel@tonic-gate 					wchar_t	wc;
3267c478bd9Sstevel@tonic-gate 					r = mbtowc(&wc, &argv[optnum][argv_ind],
3277c478bd9Sstevel@tonic-gate 						mbcurmax);
3287c478bd9Sstevel@tonic-gate 					if (r == -1) {
3297c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr, gettext(
3307c478bd9Sstevel@tonic-gate "pr: Illegal character in -e option\n"));
3317c478bd9Sstevel@tonic-gate 						exit(1);
3327c478bd9Sstevel@tonic-gate 					}
3337c478bd9Sstevel@tonic-gate 					Etabc = wc;
3347c478bd9Sstevel@tonic-gate 					SQUEEZE_ARG(argv[optnum], argv_ind, r);
3357c478bd9Sstevel@tonic-gate 				}
3367c478bd9Sstevel@tonic-gate 				if (isdigit(argv[optnum][argv_ind])) {
3377c478bd9Sstevel@tonic-gate 					Etabn = (int)strtol(&argv[optnum]
3387c478bd9Sstevel@tonic-gate 					    [argv_ind], (char **)NULL, 10);
3397c478bd9Sstevel@tonic-gate 					while (isdigit(argv[optnum][argv_ind]))
3407c478bd9Sstevel@tonic-gate 					    SQUEEZE_ARG(argv[optnum],
3417c478bd9Sstevel@tonic-gate 							argv_ind, 1);
3427c478bd9Sstevel@tonic-gate 				}
3437c478bd9Sstevel@tonic-gate 				if (Etabn <= 0)
3447c478bd9Sstevel@tonic-gate 					Etabn = DEFTAB;
3457c478bd9Sstevel@tonic-gate 				argv_ind--;
3467c478bd9Sstevel@tonic-gate 				break;
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 			case 'i':
3497c478bd9Sstevel@tonic-gate 				SQUEEZE_ARG(argv[optnum], argv_ind, 1);
3507c478bd9Sstevel@tonic-gate 				if ((c = argv[optnum][argv_ind]) != '\0' &&
3517c478bd9Sstevel@tonic-gate 				    !isdigit(c)) {
3527c478bd9Sstevel@tonic-gate 					int	r;
3537c478bd9Sstevel@tonic-gate 					wchar_t	wc;
3547c478bd9Sstevel@tonic-gate 					r = mbtowc(&wc, &argv[optnum][argv_ind],
3557c478bd9Sstevel@tonic-gate 						mbcurmax);
3567c478bd9Sstevel@tonic-gate 					if (r == -1) {
3577c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr, gettext(
3587c478bd9Sstevel@tonic-gate "pr: Illegal character in -i option\n"));
3597c478bd9Sstevel@tonic-gate 						exit(1);
3607c478bd9Sstevel@tonic-gate 					}
3617c478bd9Sstevel@tonic-gate 					Itabc = wc;
3627c478bd9Sstevel@tonic-gate 					SQUEEZE_ARG(argv[optnum], argv_ind, r);
3637c478bd9Sstevel@tonic-gate 				}
3647c478bd9Sstevel@tonic-gate 				if (isdigit(argv[optnum][argv_ind])) {
3657c478bd9Sstevel@tonic-gate 					Itabn = (int)strtol(&argv[optnum]
3667c478bd9Sstevel@tonic-gate 					    [argv_ind], (char **)NULL, 10);
3677c478bd9Sstevel@tonic-gate 					while (isdigit(argv[optnum][argv_ind]))
3687c478bd9Sstevel@tonic-gate 					    SQUEEZE_ARG(argv[optnum],
3697c478bd9Sstevel@tonic-gate 							argv_ind, 1);
3707c478bd9Sstevel@tonic-gate 				}
3717c478bd9Sstevel@tonic-gate 				if (Itabn <= 0)
3727c478bd9Sstevel@tonic-gate 					Itabn = DEFTAB;
3737c478bd9Sstevel@tonic-gate 				argv_ind--;
3747c478bd9Sstevel@tonic-gate 				break;
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 			case 'n':
3787c478bd9Sstevel@tonic-gate 				++Lnumb;
3797c478bd9Sstevel@tonic-gate 				SQUEEZE_ARG(argv[optnum], argv_ind, 1);
3807c478bd9Sstevel@tonic-gate 				if ((c = argv[optnum][argv_ind]) != '\0' &&
3817c478bd9Sstevel@tonic-gate 				    !isdigit(c)) {
3827c478bd9Sstevel@tonic-gate 					int	r;
3837c478bd9Sstevel@tonic-gate 					wchar_t	wc;
3847c478bd9Sstevel@tonic-gate 					r = mbtowc(&wc, &argv[optnum][argv_ind],
3857c478bd9Sstevel@tonic-gate 						mbcurmax);
3867c478bd9Sstevel@tonic-gate 					if (r == -1) {
3877c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr, gettext(
3887c478bd9Sstevel@tonic-gate "pr: Illegal character in -n option\n"));
3897c478bd9Sstevel@tonic-gate 						exit(1);
3907c478bd9Sstevel@tonic-gate 					}
3917c478bd9Sstevel@tonic-gate 					Nsepc = wc;
3927c478bd9Sstevel@tonic-gate 					SQUEEZE_ARG(argv[optnum], argv_ind, r);
3937c478bd9Sstevel@tonic-gate 				}
3947c478bd9Sstevel@tonic-gate 				if (isdigit(argv[optnum][argv_ind])) {
3957c478bd9Sstevel@tonic-gate 					Numw = (int)strtol(&argv[optnum]
3967c478bd9Sstevel@tonic-gate 					    [argv_ind], (char **)NULL, 10);
3977c478bd9Sstevel@tonic-gate 					while (isdigit(argv[optnum][argv_ind]))
3987c478bd9Sstevel@tonic-gate 					    SQUEEZE_ARG(argv[optnum],
3997c478bd9Sstevel@tonic-gate 							argv_ind, 1);
4007c478bd9Sstevel@tonic-gate 				}
4017c478bd9Sstevel@tonic-gate 				argv_ind--;
4027c478bd9Sstevel@tonic-gate 				if (!Numw)
4037c478bd9Sstevel@tonic-gate 					Numw = NUMW;
4047c478bd9Sstevel@tonic-gate 				break;
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 			case 's':
4077c478bd9Sstevel@tonic-gate 				SQUEEZE_ARG(argv[optnum], argv_ind, 1);
4087c478bd9Sstevel@tonic-gate 				if ((Sepc = argv[optnum][argv_ind]) == '\0')
4097c478bd9Sstevel@tonic-gate 					Sepc = '\t';
4107c478bd9Sstevel@tonic-gate 				else {
4117c478bd9Sstevel@tonic-gate 					int	r;
4127c478bd9Sstevel@tonic-gate 					wchar_t	wc;
4137c478bd9Sstevel@tonic-gate 					r = mbtowc(&wc, &argv[optnum][argv_ind],
4147c478bd9Sstevel@tonic-gate 						mbcurmax);
4157c478bd9Sstevel@tonic-gate 					if (r == -1) {
4167c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr, gettext(
4177c478bd9Sstevel@tonic-gate "pr: Illegal character in -s option\n"));
4187c478bd9Sstevel@tonic-gate 						exit(1);
4197c478bd9Sstevel@tonic-gate 					}
4207c478bd9Sstevel@tonic-gate 					Sepc = wc;
4217c478bd9Sstevel@tonic-gate 					SQUEEZE_ARG(argv[optnum], argv_ind, r);
4227c478bd9Sstevel@tonic-gate 				}
4237c478bd9Sstevel@tonic-gate 				argv_ind--;
4247c478bd9Sstevel@tonic-gate 				break;
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate 			default:
4277c478bd9Sstevel@tonic-gate 				break;
4287c478bd9Sstevel@tonic-gate 			}
4297c478bd9Sstevel@tonic-gate 		}
4307c478bd9Sstevel@tonic-gate 		if (argv[optnum][0] == '-' && argv[optnum][1] == '\0') {
4317c478bd9Sstevel@tonic-gate 			REMOVE_ARG(argc, &argv[optnum]);
4327c478bd9Sstevel@tonic-gate 			optnum--;
4337c478bd9Sstevel@tonic-gate 		}
4347c478bd9Sstevel@tonic-gate 	}
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate 	/* Now get the other options */
4377c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "0123456789adfFh:l:mo:prtw:"))
4387c478bd9Sstevel@tonic-gate 	    != EOF) {
4397c478bd9Sstevel@tonic-gate 		switch (c) {
4407c478bd9Sstevel@tonic-gate 		case '0':
4417c478bd9Sstevel@tonic-gate 		case '1':
4427c478bd9Sstevel@tonic-gate 		case '2':
4437c478bd9Sstevel@tonic-gate 		case '3':
4447c478bd9Sstevel@tonic-gate 		case '4':
4457c478bd9Sstevel@tonic-gate 		case '5':
4467c478bd9Sstevel@tonic-gate 		case '6':
4477c478bd9Sstevel@tonic-gate 		case '7':
4487c478bd9Sstevel@tonic-gate 		case '8':
4497c478bd9Sstevel@tonic-gate 		case '9':
4507c478bd9Sstevel@tonic-gate 			Ncols *= 10;
4517c478bd9Sstevel@tonic-gate 			Ncols += c - '0';
4527c478bd9Sstevel@tonic-gate 			break;
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 		case 'a':
4557c478bd9Sstevel@tonic-gate 			aflg++;
4567c478bd9Sstevel@tonic-gate 			if (!Multi)
4577c478bd9Sstevel@tonic-gate 				Multi = c;
4587c478bd9Sstevel@tonic-gate 			break;
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 		case 'd':
4617c478bd9Sstevel@tonic-gate 			Dblspace = 2;
4627c478bd9Sstevel@tonic-gate 			break;
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 		case 'f':
4657c478bd9Sstevel@tonic-gate 			++Formfeed;
4667c478bd9Sstevel@tonic-gate 			++Pause;
4677c478bd9Sstevel@tonic-gate 			break;
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 		case 'h':
4707c478bd9Sstevel@tonic-gate 			Head = optarg;
4717c478bd9Sstevel@tonic-gate 			break;
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 		case 'l':
4747c478bd9Sstevel@tonic-gate 			if (strlen(optarg) != strspn(optarg, "0123456789"))
4757c478bd9Sstevel@tonic-gate 				usage(1);
4767c478bd9Sstevel@tonic-gate 			Length = (int)strtol(optarg, (char **)NULL, 10);
4777c478bd9Sstevel@tonic-gate 			break;
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 		case 'm':
4807c478bd9Sstevel@tonic-gate 			mflg++;
4817c478bd9Sstevel@tonic-gate 			Multi = c;
4827c478bd9Sstevel@tonic-gate 			break;
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 		case 'o':
4857c478bd9Sstevel@tonic-gate 			if (strlen(optarg) != strspn(optarg, "0123456789"))
4867c478bd9Sstevel@tonic-gate 				usage(1);
4877c478bd9Sstevel@tonic-gate 			Offset = (int)strtol(optarg, (char **)NULL, 10);
4887c478bd9Sstevel@tonic-gate 			break;
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate 		case 'p':
4917c478bd9Sstevel@tonic-gate 			++Pause;
4927c478bd9Sstevel@tonic-gate 			break;
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 		case 'r':
4957c478bd9Sstevel@tonic-gate 			Report = 0;
4967c478bd9Sstevel@tonic-gate 			break;
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 		case 't':
4997c478bd9Sstevel@tonic-gate 			Margin = 0;
5007c478bd9Sstevel@tonic-gate 			break;
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 		case 'w':
5037c478bd9Sstevel@tonic-gate 			if (strlen(optarg) != strspn(optarg, "0123456789"))
5047c478bd9Sstevel@tonic-gate 				usage(1);
5057c478bd9Sstevel@tonic-gate 			Linew = (int)strtol(optarg, (char **)NULL, 10);
5067c478bd9Sstevel@tonic-gate 			break;
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate 		case 'F':
5097c478bd9Sstevel@tonic-gate #ifdef	XPG4
5107c478bd9Sstevel@tonic-gate 			++Formfeed;
5117c478bd9Sstevel@tonic-gate #else
5127c478bd9Sstevel@tonic-gate 			fold++;
5137c478bd9Sstevel@tonic-gate #endif
5147c478bd9Sstevel@tonic-gate 			break;
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate 		case '?':
5177c478bd9Sstevel@tonic-gate 			usage(2);
5187c478bd9Sstevel@tonic-gate 			break;
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 		default :
5217c478bd9Sstevel@tonic-gate 			usage(2);
5227c478bd9Sstevel@tonic-gate 		}
5237c478bd9Sstevel@tonic-gate 	}
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 	/* Count the file names and strip options */
5267c478bd9Sstevel@tonic-gate 	for (i = 1; i < argc; i++) {
5277c478bd9Sstevel@tonic-gate 		/* Check for explicit stdin */
5287c478bd9Sstevel@tonic-gate 		if ((argv[i][0] == '-') && (argv[i][1] == '\0')) {
5297c478bd9Sstevel@tonic-gate 			argv[eargc++][0] = '\0';
5307c478bd9Sstevel@tonic-gate 			REMOVE_ARG(argc, &argv[i]);
5317c478bd9Sstevel@tonic-gate 			if (i < optind)
5327c478bd9Sstevel@tonic-gate 				optind--;
5337c478bd9Sstevel@tonic-gate 		}
5347c478bd9Sstevel@tonic-gate 	}
5357c478bd9Sstevel@tonic-gate 	for (i = eargc; optind < argc; i++, optind++) {
5367c478bd9Sstevel@tonic-gate 		argv[i] = argv[optind];
5377c478bd9Sstevel@tonic-gate 		eargc++;
5387c478bd9Sstevel@tonic-gate 	}
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate 	/* Check options */
5417c478bd9Sstevel@tonic-gate 	if (Ncols == 0)
5427c478bd9Sstevel@tonic-gate 		Ncols = 1;
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 	if (mflg && (Ncols > 1)) {
5457c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
5467c478bd9Sstevel@tonic-gate 		    gettext("pr: only one of either -m or -column allowed\n"));
5477c478bd9Sstevel@tonic-gate 		usage(1);
5487c478bd9Sstevel@tonic-gate 	}
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate 	if (Ncols == 1 && fold)
5517c478bd9Sstevel@tonic-gate 		Multi = 'm';
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 	if (Length <= 0)
5547c478bd9Sstevel@tonic-gate 		Length = LENGTH;
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate 	if (Length <= Margin)
5577c478bd9Sstevel@tonic-gate 		Margin = 0;
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 	Plength = Length - Margin/2;
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate 	if (Multi == 'm')
5627c478bd9Sstevel@tonic-gate 		Ncols = eargc;
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 	switch (Ncols) {
5657c478bd9Sstevel@tonic-gate 	case 0:
5667c478bd9Sstevel@tonic-gate 		Ncols = 1;
5677c478bd9Sstevel@tonic-gate 		break;
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate 	case 1:
5707c478bd9Sstevel@tonic-gate 		break;
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate 	default:
5737c478bd9Sstevel@tonic-gate 		if (Etabn == 0)	/* respect explicit tab specification */
5747c478bd9Sstevel@tonic-gate 			Etabn = DEFTAB;
5757c478bd9Sstevel@tonic-gate 		if (Itabn == 0)
5767c478bd9Sstevel@tonic-gate 			Itabn = DEFTAB;
5777c478bd9Sstevel@tonic-gate 	}
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate 	if ((Fcol = (foldinf *) malloc(sizeof (foldinf) * Ncols)) == NULL) {
5807c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("pr: malloc failed\n"));
5817c478bd9Sstevel@tonic-gate 		exit(1);
5827c478bd9Sstevel@tonic-gate 	}
5837c478bd9Sstevel@tonic-gate 	for (i = 0; i < Ncols; i++)
5847c478bd9Sstevel@tonic-gate 		Fcol[i].fold = Fcol[i].skip = 0;
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 	if (Linew == 0)
5877c478bd9Sstevel@tonic-gate 		Linew = Ncols != 1 && Sepc == 0 ? LINEW : 512;
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 	if (Lnumb) {
5907c478bd9Sstevel@tonic-gate 		int numw;
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate 		if (Nsepc == '\t') {
5937c478bd9Sstevel@tonic-gate 			if (Itabn == 0)
5947c478bd9Sstevel@tonic-gate 				numw = Numw + DEFTAB - (Numw % DEFTAB);
5957c478bd9Sstevel@tonic-gate 			else
5967c478bd9Sstevel@tonic-gate 				numw = Numw + Itabn - (Numw % Itabn);
5977c478bd9Sstevel@tonic-gate 		} else {
5987c478bd9Sstevel@tonic-gate 			numw = Numw + ((iswprint(Nsepc)) ? 1 : 0);
5997c478bd9Sstevel@tonic-gate 		}
6007c478bd9Sstevel@tonic-gate 		Linew -= (Multi == 'm') ? numw : numw * Ncols;
6017c478bd9Sstevel@tonic-gate 	}
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate 	if ((Colw = (Linew - Ncols + 1)/Ncols) < 1)
6047c478bd9Sstevel@tonic-gate 		die("width too small");
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 	if (Ncols != 1 && Multi == 0) {
6077c478bd9Sstevel@tonic-gate 		/* Buflen should take the number of wide characters */
6087c478bd9Sstevel@tonic-gate 		/* Not the size for Buffer */
6097c478bd9Sstevel@tonic-gate 		Buflen = ((UNS) (Plength / Dblspace + 1)) *
6107c478bd9Sstevel@tonic-gate 		    2 * (Linew + 1);
6117c478bd9Sstevel@tonic-gate 		/* Should allocate Buflen * sizeof (wchar_t) */
6127c478bd9Sstevel@tonic-gate 		Buffer = (wchar_t *)getspace(Buflen * sizeof (wchar_t));
6137c478bd9Sstevel@tonic-gate 		Bufptr = Bufend = &Buffer[Buflen];
6147c478bd9Sstevel@tonic-gate 		Colpts = (COLP) getspace((UNS) ((Ncols + 1) *
6157c478bd9Sstevel@tonic-gate 		    sizeof (*Colpts)));
6167c478bd9Sstevel@tonic-gate 		Colpts[0].c_lno = 0;
6177c478bd9Sstevel@tonic-gate 	}
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate 	/* is stdin not a tty? */
6207c478bd9Sstevel@tonic-gate 	if (Ttyout && (Pause || Formfeed) && !ttyname(fileno(stdin)))
6217c478bd9Sstevel@tonic-gate 		Ttyin = fopen("/dev/tty", "r");
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 	return (eargc);
6247c478bd9Sstevel@tonic-gate }
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate static	int
print(char * name)6287c478bd9Sstevel@tonic-gate print(char *name)
6297c478bd9Sstevel@tonic-gate {
6307c478bd9Sstevel@tonic-gate 	static	int	notfirst = 0;
6317c478bd9Sstevel@tonic-gate 	char	*date = NULL;
6327c478bd9Sstevel@tonic-gate 	char	*head = NULL;
6337c478bd9Sstevel@tonic-gate 	int	c;
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate 	if (Multi != 'm' && mustopen(name, &Files[0]) == NULL)
6367c478bd9Sstevel@tonic-gate 		return (0);
6377c478bd9Sstevel@tonic-gate 	if (Multi == 'm' && Nfiles == 0 && mustopen(name, &Files[0]) == NULL)
6387c478bd9Sstevel@tonic-gate 		die("cannot open stdin");
6397c478bd9Sstevel@tonic-gate 	if (Buffer)
6407c478bd9Sstevel@tonic-gate 		(void) ungetwc(Files->f_nextc, Files->f_f);
6417c478bd9Sstevel@tonic-gate 	if (Lnumb)
6427c478bd9Sstevel@tonic-gate 		Lnumb = 1;
6437c478bd9Sstevel@tonic-gate 	for (Page = 0; ; putpage()) {
6447c478bd9Sstevel@tonic-gate 		if (C == WEOF && !(fold && Buffer))
6457c478bd9Sstevel@tonic-gate 			break;
6467c478bd9Sstevel@tonic-gate 		if (Buffer)
6477c478bd9Sstevel@tonic-gate 			nexbuf();
6487c478bd9Sstevel@tonic-gate 		Inpos = 0;
6497c478bd9Sstevel@tonic-gate 		if (get(0) == WEOF)
6507c478bd9Sstevel@tonic-gate 			break;
6517c478bd9Sstevel@tonic-gate 		(void) fflush(stdout);
6527c478bd9Sstevel@tonic-gate 		if (++Page >= Fpage) {
6537c478bd9Sstevel@tonic-gate 			/* Pause if -p and not first page */
6547c478bd9Sstevel@tonic-gate 			if (Ttyout && Pause && !notfirst++) {
6557c478bd9Sstevel@tonic-gate 				PROMPT();	/* prompt with bell and pause */
6567c478bd9Sstevel@tonic-gate 				while ((c = getc(Ttyin)) != EOF && c != '\n')
6577c478bd9Sstevel@tonic-gate 					;
6587c478bd9Sstevel@tonic-gate 			}
6597c478bd9Sstevel@tonic-gate 			if (Margin == 0)
6607c478bd9Sstevel@tonic-gate 				continue;
6617c478bd9Sstevel@tonic-gate 			if (date == NULL)
6627c478bd9Sstevel@tonic-gate 				date = GETDATE();
6637c478bd9Sstevel@tonic-gate 			if (head == NULL)
6647c478bd9Sstevel@tonic-gate 				head = Head != NULL ? Head :
6657c478bd9Sstevel@tonic-gate 				    Nfiles < 2 ? Files->f_name : nulls;
6667c478bd9Sstevel@tonic-gate 			(void) printf("\n\n");
6677c478bd9Sstevel@tonic-gate 			Nspace = Offset;
6687c478bd9Sstevel@tonic-gate 			putspace();
6697c478bd9Sstevel@tonic-gate 			(void) printf(HEAD);
6707c478bd9Sstevel@tonic-gate 		}
6717c478bd9Sstevel@tonic-gate 	}
6727c478bd9Sstevel@tonic-gate 	C = '\0';
6737c478bd9Sstevel@tonic-gate 	return (1);
6747c478bd9Sstevel@tonic-gate }
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate static	void
putpage()6787c478bd9Sstevel@tonic-gate putpage()
6797c478bd9Sstevel@tonic-gate {
6807c478bd9Sstevel@tonic-gate 	int	colno;
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 	if (fold) {
6837c478bd9Sstevel@tonic-gate 		foldpage();
6847c478bd9Sstevel@tonic-gate 		return;
6857c478bd9Sstevel@tonic-gate 	}
6867c478bd9Sstevel@tonic-gate 	for (Line = Margin / 2; ; (void) get(0)) {
6877c478bd9Sstevel@tonic-gate 		for (Nspace = Offset, colno = 0, Outpos = 0; C != '\f'; ) {
6887c478bd9Sstevel@tonic-gate 			if (Lnumb && (C != WEOF) &&
6897c478bd9Sstevel@tonic-gate 			    (((colno == 0) && (Multi == 'm')) ||
6907c478bd9Sstevel@tonic-gate 			    (Multi != 'm'))) {
6917c478bd9Sstevel@tonic-gate 				if (Page >= Fpage) {
6927c478bd9Sstevel@tonic-gate 					putspace();
6937c478bd9Sstevel@tonic-gate 					(void) printf("%*ld%wc", Numw, Buffer ?
6947c478bd9Sstevel@tonic-gate 					    Colpts[colno].c_lno++ :
6957c478bd9Sstevel@tonic-gate 					    Lnumb, Nsepc);
6967c478bd9Sstevel@tonic-gate 
6977c478bd9Sstevel@tonic-gate 					/* Move Outpos for number field */
6987c478bd9Sstevel@tonic-gate 					Outpos += Numw;
6997c478bd9Sstevel@tonic-gate 					if (Nsepc == '\t')
7007c478bd9Sstevel@tonic-gate 						Outpos +=
7017c478bd9Sstevel@tonic-gate 						    DEFTAB - (Outpos % DEFTAB);
7027c478bd9Sstevel@tonic-gate 					else
7037c478bd9Sstevel@tonic-gate 						Outpos++;
7047c478bd9Sstevel@tonic-gate 				}
7057c478bd9Sstevel@tonic-gate 				++Lnumb;
7067c478bd9Sstevel@tonic-gate 			}
7077c478bd9Sstevel@tonic-gate 			for (Lcolpos = 0, Pcolpos = 0;
7087c478bd9Sstevel@tonic-gate 			    C != '\n' && C != '\f' && C != WEOF;
7097c478bd9Sstevel@tonic-gate 			    (void) get(colno))
7107c478bd9Sstevel@tonic-gate 				(void) put(C);
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate 			if ((C == WEOF) || (++colno == Ncols) ||
7137c478bd9Sstevel@tonic-gate 			    ((C == '\n') && (get(colno) == WEOF)))
7147c478bd9Sstevel@tonic-gate 				break;
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate 			if (Sepc)
7177c478bd9Sstevel@tonic-gate 				(void) put(Sepc);
7187c478bd9Sstevel@tonic-gate 			else if ((Nspace += Colw - Lcolpos + 1) < 1)
7197c478bd9Sstevel@tonic-gate 				Nspace = 1;
7207c478bd9Sstevel@tonic-gate 		}
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate 		if (C == WEOF) {
7237c478bd9Sstevel@tonic-gate 			if (Margin != 0)
7247c478bd9Sstevel@tonic-gate 				break;
7257c478bd9Sstevel@tonic-gate 			if (colno != 0)
7267c478bd9Sstevel@tonic-gate 				(void) put('\n');
7277c478bd9Sstevel@tonic-gate 			return;
7287c478bd9Sstevel@tonic-gate 		}
7297c478bd9Sstevel@tonic-gate 		if (C == '\f')
7307c478bd9Sstevel@tonic-gate 			break;
7317c478bd9Sstevel@tonic-gate 		(void) put('\n');
7327c478bd9Sstevel@tonic-gate 		if (Dblspace == 2 && Line < Plength)
7337c478bd9Sstevel@tonic-gate 			(void) put('\n');
7347c478bd9Sstevel@tonic-gate 		if (Line >= Plength)
7357c478bd9Sstevel@tonic-gate 			break;
7367c478bd9Sstevel@tonic-gate 	}
7377c478bd9Sstevel@tonic-gate 	if (Formfeed)
7387c478bd9Sstevel@tonic-gate 		(void) put('\f');
7397c478bd9Sstevel@tonic-gate 	else
7407c478bd9Sstevel@tonic-gate 		while (Line < Length)
7417c478bd9Sstevel@tonic-gate 			(void) put('\n');
7427c478bd9Sstevel@tonic-gate }
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 
7457c478bd9Sstevel@tonic-gate static	void
foldpage()7467c478bd9Sstevel@tonic-gate foldpage()
7477c478bd9Sstevel@tonic-gate {
7487c478bd9Sstevel@tonic-gate 	int	colno;
7497c478bd9Sstevel@tonic-gate 	int	keep;
7507c478bd9Sstevel@tonic-gate 	int	i;
7517c478bd9Sstevel@tonic-gate 	int	pLcolpos;
7527c478bd9Sstevel@tonic-gate 	static	int	sl;
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate 	for (Line = Margin / 2; ; (void) get(0)) {
7557c478bd9Sstevel@tonic-gate 		for (Nspace = Offset, colno = 0, Outpos = 0; C != '\f'; ) {
7567c478bd9Sstevel@tonic-gate 			if (Lnumb && Multi == 'm' && foldcol) {
7577c478bd9Sstevel@tonic-gate 				if (!Fcol[colno].skip) {
7587c478bd9Sstevel@tonic-gate 					unget(colno);
7597c478bd9Sstevel@tonic-gate 					putspace();
7607c478bd9Sstevel@tonic-gate 					if (!colno) {
7617c478bd9Sstevel@tonic-gate 						for (i = 0; i <= Numw; i++)
7627c478bd9Sstevel@tonic-gate 							(void) printf(" ");
7637c478bd9Sstevel@tonic-gate 						(void) printf("%wc", Nsepc);
7647c478bd9Sstevel@tonic-gate 					}
7657c478bd9Sstevel@tonic-gate 					for (i = 0; i <= Colw; i++)
7667c478bd9Sstevel@tonic-gate 						(void) printf(" ");
7677c478bd9Sstevel@tonic-gate 					(void) put(Sepc);
7687c478bd9Sstevel@tonic-gate 					if (++colno == Ncols)
7697c478bd9Sstevel@tonic-gate 						break;
7707c478bd9Sstevel@tonic-gate 					(void) get(colno);
7717c478bd9Sstevel@tonic-gate 					continue;
7727c478bd9Sstevel@tonic-gate 				} else if (!colno)
7737c478bd9Sstevel@tonic-gate 					Lnumb = sl;
7747c478bd9Sstevel@tonic-gate 			}
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 			if (Lnumb && (C != WEOF) &&
7777c478bd9Sstevel@tonic-gate 			    ((colno == 0 && Multi == 'm') || (Multi != 'm'))) {
7787c478bd9Sstevel@tonic-gate 				if (Page >= Fpage) {
7797c478bd9Sstevel@tonic-gate 					putspace();
7807c478bd9Sstevel@tonic-gate 					if ((foldcol &&
7817c478bd9Sstevel@tonic-gate 					    Fcol[colno].skip && Multi != 'a') ||
7827c478bd9Sstevel@tonic-gate 					    (Fcol[0].fold && Multi == 'a') ||
7837c478bd9Sstevel@tonic-gate 					    (Buffer && Colpts[colno].c_skip)) {
7847c478bd9Sstevel@tonic-gate 						for (i = 0; i < Numw; i++)
7857c478bd9Sstevel@tonic-gate 							(void) printf(" ");
7867c478bd9Sstevel@tonic-gate 						(void) printf("%wc", Nsepc);
7877c478bd9Sstevel@tonic-gate 						if (Buffer) {
7887c478bd9Sstevel@tonic-gate 							Colpts[colno].c_lno++;
7897c478bd9Sstevel@tonic-gate 							Colpts[colno].c_skip =
7907c478bd9Sstevel@tonic-gate 							    0;
7917c478bd9Sstevel@tonic-gate 						}
7927c478bd9Sstevel@tonic-gate 					}
7937c478bd9Sstevel@tonic-gate 					else
7947c478bd9Sstevel@tonic-gate 					(void) printf("%*ld%wc", Numw, Buffer ?
7957c478bd9Sstevel@tonic-gate 					    Colpts[colno].c_lno++ :
7967c478bd9Sstevel@tonic-gate 					    Lnumb, Nsepc);
7977c478bd9Sstevel@tonic-gate 				}
7987c478bd9Sstevel@tonic-gate 				sl = Lnumb++;
7997c478bd9Sstevel@tonic-gate 			}
8007c478bd9Sstevel@tonic-gate 			pLcolpos = 0;
8017c478bd9Sstevel@tonic-gate 			for (Lcolpos = 0, Pcolpos = 0;
8027c478bd9Sstevel@tonic-gate 			    C != '\n' && C != '\f' && C != WEOF;
8037c478bd9Sstevel@tonic-gate 			    (void) get(colno)) {
8047c478bd9Sstevel@tonic-gate 				if (put(C)) {
8057c478bd9Sstevel@tonic-gate 					unget(colno);
8067c478bd9Sstevel@tonic-gate 					Fcol[(Multi == 'a') ? 0 : colno].fold
8077c478bd9Sstevel@tonic-gate 					    = 1;
8087c478bd9Sstevel@tonic-gate 					break;
8097c478bd9Sstevel@tonic-gate 				} else if (Multi == 'a') {
8107c478bd9Sstevel@tonic-gate 					Fcol[0].fold = 0;
8117c478bd9Sstevel@tonic-gate 				}
8127c478bd9Sstevel@tonic-gate 				pLcolpos = Lcolpos;
8137c478bd9Sstevel@tonic-gate 			}
8147c478bd9Sstevel@tonic-gate 			if (Buffer) {
8157c478bd9Sstevel@tonic-gate 				alleof = 1;
8167c478bd9Sstevel@tonic-gate 				for (i = 0; i < Ncols; i++)
8177c478bd9Sstevel@tonic-gate 					if (!Fcol[i].eof)
8187c478bd9Sstevel@tonic-gate 						alleof = 0;
8197c478bd9Sstevel@tonic-gate 				if (alleof || ++colno == Ncols)
8207c478bd9Sstevel@tonic-gate 					break;
8217c478bd9Sstevel@tonic-gate 			} else if (C == EOF || ++colno == Ncols)
8227c478bd9Sstevel@tonic-gate 				break;
8237c478bd9Sstevel@tonic-gate 			keep = C;
8247c478bd9Sstevel@tonic-gate 			(void) get(colno);
8257c478bd9Sstevel@tonic-gate 			if (keep == '\n' && C == WEOF)
8267c478bd9Sstevel@tonic-gate 				break;
8277c478bd9Sstevel@tonic-gate 			if (Sepc)
8287c478bd9Sstevel@tonic-gate 				(void) put(Sepc);
8297c478bd9Sstevel@tonic-gate 			else if ((Nspace += Colw - pLcolpos + 1) < 1)
8307c478bd9Sstevel@tonic-gate 				Nspace = 1;
8317c478bd9Sstevel@tonic-gate 		}
8327c478bd9Sstevel@tonic-gate 		foldcol = 0;
8337c478bd9Sstevel@tonic-gate 		if (Lnumb && Multi != 'a') {
8347c478bd9Sstevel@tonic-gate 			for (i = 0; i < Ncols; i++) {
8357c478bd9Sstevel@tonic-gate 				Fcol[i].skip = Fcol[i].fold;
8367c478bd9Sstevel@tonic-gate 				foldcol +=  Fcol[i].fold;
8377c478bd9Sstevel@tonic-gate 				Fcol[i].fold = 0;
8387c478bd9Sstevel@tonic-gate 			}
8397c478bd9Sstevel@tonic-gate 		}
8407c478bd9Sstevel@tonic-gate 		if (C == WEOF) {
8417c478bd9Sstevel@tonic-gate 			if (Margin != 0)
8427c478bd9Sstevel@tonic-gate 				break;
8437c478bd9Sstevel@tonic-gate 			if (colno != 0)
8447c478bd9Sstevel@tonic-gate 				(void) put('\n');
8457c478bd9Sstevel@tonic-gate 			return;
8467c478bd9Sstevel@tonic-gate 		}
8477c478bd9Sstevel@tonic-gate 		if (C == '\f')
8487c478bd9Sstevel@tonic-gate 			break;
8497c478bd9Sstevel@tonic-gate 		(void) put('\n');
8507c478bd9Sstevel@tonic-gate 		(void) fflush(stdout);
8517c478bd9Sstevel@tonic-gate 		if (Dblspace == 2 && Line < Plength)
8527c478bd9Sstevel@tonic-gate 			(void) put('\n');
8537c478bd9Sstevel@tonic-gate 		if (Line >= Plength)
8547c478bd9Sstevel@tonic-gate 			break;
8557c478bd9Sstevel@tonic-gate 	}
8567c478bd9Sstevel@tonic-gate 	if (Formfeed)
8577c478bd9Sstevel@tonic-gate 		(void) put('\f');
8587c478bd9Sstevel@tonic-gate 	else while (Line < Length)
8597c478bd9Sstevel@tonic-gate 		(void) put('\n');
8607c478bd9Sstevel@tonic-gate }
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate 
8637c478bd9Sstevel@tonic-gate static	void
nexbuf()8647c478bd9Sstevel@tonic-gate nexbuf()
8657c478bd9Sstevel@tonic-gate {
8667c478bd9Sstevel@tonic-gate 	wchar_t	*s = Buffer;
8677c478bd9Sstevel@tonic-gate 	COLP	p = Colpts;
8687c478bd9Sstevel@tonic-gate 	int	j;
8697c478bd9Sstevel@tonic-gate 	int	c;
8707c478bd9Sstevel@tonic-gate 	int	bline = 0;
8717c478bd9Sstevel@tonic-gate 	wchar_t	wc;
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate 	if (fold) {
8747c478bd9Sstevel@tonic-gate 		foldbuf();
8757c478bd9Sstevel@tonic-gate 		return;
8767c478bd9Sstevel@tonic-gate 	}
8777c478bd9Sstevel@tonic-gate 	for (; ; ) {
8787c478bd9Sstevel@tonic-gate 		p->c_ptr0 = p->c_ptr = s;
8797c478bd9Sstevel@tonic-gate 		if (p == &Colpts[Ncols])
8807c478bd9Sstevel@tonic-gate 			return;
8817c478bd9Sstevel@tonic-gate 		(p++)->c_lno = Lnumb + bline;
8827c478bd9Sstevel@tonic-gate 		for (j = (Length - Margin)/Dblspace; --j >= 0; ++bline) {
8837c478bd9Sstevel@tonic-gate 			for (Inpos = 0; ; ) {
8847c478bd9Sstevel@tonic-gate 				errno = 0;
8857c478bd9Sstevel@tonic-gate 				wc = _fgetwc_pr(Files->f_f, &c);
8867c478bd9Sstevel@tonic-gate 				if (wc == WEOF) {
8877c478bd9Sstevel@tonic-gate 					/* If there is an illegal character, */
8887c478bd9Sstevel@tonic-gate 					/* handle it as a byte sequence. */
8897c478bd9Sstevel@tonic-gate 					if (errno == EILSEQ) {
8907c478bd9Sstevel@tonic-gate 						if (Inpos < Colw - 1) {
8917c478bd9Sstevel@tonic-gate 							*s = c;
8927c478bd9Sstevel@tonic-gate 							if (++s >= Bufend)
8937c478bd9Sstevel@tonic-gate die("page-buffer overflow");
8947c478bd9Sstevel@tonic-gate 						}
8957c478bd9Sstevel@tonic-gate 						Inpos++;
8967c478bd9Sstevel@tonic-gate 						Error++;
8977c478bd9Sstevel@tonic-gate 						return;
8987c478bd9Sstevel@tonic-gate 					} else {
8997c478bd9Sstevel@tonic-gate 						/* Real EOF */
9007c478bd9Sstevel@tonic-gate for (*s = WEOF; p <= &Colpts[Ncols]; ++p)
9017c478bd9Sstevel@tonic-gate 	p->c_ptr0 = p->c_ptr = s;
9027c478bd9Sstevel@tonic-gate 						balance(bline);
9037c478bd9Sstevel@tonic-gate 						return;
9047c478bd9Sstevel@tonic-gate 					}
9057c478bd9Sstevel@tonic-gate 				}
9067c478bd9Sstevel@tonic-gate 
9077c478bd9Sstevel@tonic-gate 				if (isascii(wc)) {
9087c478bd9Sstevel@tonic-gate 					if (isprint(wc))
9097c478bd9Sstevel@tonic-gate 						Inpos++;
9107c478bd9Sstevel@tonic-gate 				} else if (iswprint(wc)) {
9117c478bd9Sstevel@tonic-gate 					Inpos += wcwidth(wc);
9127c478bd9Sstevel@tonic-gate 				}
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate 				if (Inpos <= Colw || wc == '\n') {
9157c478bd9Sstevel@tonic-gate 					*s = wc;
9167c478bd9Sstevel@tonic-gate 					if (++s >= Bufend)
9177c478bd9Sstevel@tonic-gate 						die("page-buffer overflow");
9187c478bd9Sstevel@tonic-gate 				}
9197c478bd9Sstevel@tonic-gate 				if (wc == '\n')
9207c478bd9Sstevel@tonic-gate 					break;
9217c478bd9Sstevel@tonic-gate 				switch (wc) {
9227c478bd9Sstevel@tonic-gate 				case '\b':
9237c478bd9Sstevel@tonic-gate 					if (Inpos == 0)
9247c478bd9Sstevel@tonic-gate 						--s;
9257c478bd9Sstevel@tonic-gate 
9267c478bd9Sstevel@tonic-gate 					/*FALLTHROUGH*/
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate 				case ESC:
9297c478bd9Sstevel@tonic-gate 					if (Inpos > 0)
9307c478bd9Sstevel@tonic-gate 						--Inpos;
9317c478bd9Sstevel@tonic-gate 				}
9327c478bd9Sstevel@tonic-gate 			}
9337c478bd9Sstevel@tonic-gate 		}
9347c478bd9Sstevel@tonic-gate 	}
9357c478bd9Sstevel@tonic-gate }
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate 
9387c478bd9Sstevel@tonic-gate static	void
foldbuf()9397c478bd9Sstevel@tonic-gate foldbuf()
9407c478bd9Sstevel@tonic-gate {
9417c478bd9Sstevel@tonic-gate 	int	num;
9427c478bd9Sstevel@tonic-gate 	int	i;
9437c478bd9Sstevel@tonic-gate 	int	colno = 0;
9447c478bd9Sstevel@tonic-gate 	int	size = Buflen;
9457c478bd9Sstevel@tonic-gate 	wchar_t	*s;
9467c478bd9Sstevel@tonic-gate 	wchar_t	*d;
9477c478bd9Sstevel@tonic-gate 	COLP	p = Colpts;
9487c478bd9Sstevel@tonic-gate 
9497c478bd9Sstevel@tonic-gate 	for (i = 0; i < Ncols; i++)
9507c478bd9Sstevel@tonic-gate 		Fcol[i].eof = 0;
9517c478bd9Sstevel@tonic-gate 	d = Buffer;
9527c478bd9Sstevel@tonic-gate 	if (Bufptr != Bufend) {
9537c478bd9Sstevel@tonic-gate 		s = Bufptr;
9547c478bd9Sstevel@tonic-gate 		while (s < Bufend)
9557c478bd9Sstevel@tonic-gate 			*d++ = *s++;
9567c478bd9Sstevel@tonic-gate 		size -= (Bufend - Bufptr);
9577c478bd9Sstevel@tonic-gate 	}
9587c478bd9Sstevel@tonic-gate 	Bufptr = Buffer;
9597c478bd9Sstevel@tonic-gate 	p->c_ptr0 = p->c_ptr = Buffer;
9607c478bd9Sstevel@tonic-gate 	if (p->c_lno == 0) {
9617c478bd9Sstevel@tonic-gate 		p->c_lno = Lnumb;
9627c478bd9Sstevel@tonic-gate 		p->c_skip = 0;
9637c478bd9Sstevel@tonic-gate 	} else {
9647c478bd9Sstevel@tonic-gate 		p->c_lno = Colpts[Ncols-1].c_lno;
9657c478bd9Sstevel@tonic-gate 		p->c_skip = Colpts[Ncols].c_skip;
9667c478bd9Sstevel@tonic-gate 		if (p->c_skip)
9677c478bd9Sstevel@tonic-gate 			p->c_lno--;
9687c478bd9Sstevel@tonic-gate 	}
9697c478bd9Sstevel@tonic-gate 	if ((num = freadw(d, size, Files->f_f)) != size) {
9707c478bd9Sstevel@tonic-gate 		for (*(d+num) = WEOF; (++p) <= &Colpts[Ncols]; ) {
9717c478bd9Sstevel@tonic-gate 			p->c_ptr0 = p->c_ptr = (d+num);
9727c478bd9Sstevel@tonic-gate 		}
9737c478bd9Sstevel@tonic-gate 		balance(0);
9747c478bd9Sstevel@tonic-gate 		return;
9757c478bd9Sstevel@tonic-gate 	}
9767c478bd9Sstevel@tonic-gate 	i = (Length - Margin) / Dblspace;
9777c478bd9Sstevel@tonic-gate 	do {
9787c478bd9Sstevel@tonic-gate 		(void) readbuf(&Bufptr, i, p++);
9797c478bd9Sstevel@tonic-gate 	} while (++colno < Ncols);
9807c478bd9Sstevel@tonic-gate }
9817c478bd9Sstevel@tonic-gate 
9827c478bd9Sstevel@tonic-gate 
9837c478bd9Sstevel@tonic-gate static	void
balance(int bline)9847c478bd9Sstevel@tonic-gate balance(int bline)	/* line balancing for last page */
9857c478bd9Sstevel@tonic-gate {
9867c478bd9Sstevel@tonic-gate 	wchar_t	*s = Buffer;
9877c478bd9Sstevel@tonic-gate 	COLP	p = Colpts;
9887c478bd9Sstevel@tonic-gate 	int	colno = 0;
9897c478bd9Sstevel@tonic-gate 	int	j;
9907c478bd9Sstevel@tonic-gate 	int	c;
9917c478bd9Sstevel@tonic-gate 	int	l;
9927c478bd9Sstevel@tonic-gate 	int	lines;
9937c478bd9Sstevel@tonic-gate 
9947c478bd9Sstevel@tonic-gate 	if (!fold) {
9957c478bd9Sstevel@tonic-gate 		c = bline % Ncols;
9967c478bd9Sstevel@tonic-gate 		l = (bline + Ncols - 1)/Ncols;
9977c478bd9Sstevel@tonic-gate 		bline = 0;
9987c478bd9Sstevel@tonic-gate 		do {
9997c478bd9Sstevel@tonic-gate 			for (j = 0; j < l; ++j)
10007c478bd9Sstevel@tonic-gate 				while (*s++ != '\n')
10017c478bd9Sstevel@tonic-gate 					;
10027c478bd9Sstevel@tonic-gate 			(++p)->c_lno = Lnumb + (bline += l);
10037c478bd9Sstevel@tonic-gate 			p->c_ptr0 = p->c_ptr = s;
10047c478bd9Sstevel@tonic-gate 			if (++colno == c)
10057c478bd9Sstevel@tonic-gate 				--l;
10067c478bd9Sstevel@tonic-gate 		} while (colno < Ncols - 1);
10077c478bd9Sstevel@tonic-gate 	} else {
10087c478bd9Sstevel@tonic-gate 		lines = readbuf(&s, 0, 0);
10097c478bd9Sstevel@tonic-gate 		l = (lines + Ncols - 1)/Ncols;
10107c478bd9Sstevel@tonic-gate 		if (l > ((Length - Margin) / Dblspace)) {
10117c478bd9Sstevel@tonic-gate 			l = (Length - Margin) / Dblspace;
10127c478bd9Sstevel@tonic-gate 			c = Ncols;
10137c478bd9Sstevel@tonic-gate 		} else {
10147c478bd9Sstevel@tonic-gate 			c = lines % Ncols;
10157c478bd9Sstevel@tonic-gate 		}
10167c478bd9Sstevel@tonic-gate 		s = Buffer;
10177c478bd9Sstevel@tonic-gate 		do {
10187c478bd9Sstevel@tonic-gate 			(void) readbuf(&s, l, p++);
10197c478bd9Sstevel@tonic-gate 			if (++colno == c)
10207c478bd9Sstevel@tonic-gate 				--l;
10217c478bd9Sstevel@tonic-gate 		} while (colno < Ncols);
10227c478bd9Sstevel@tonic-gate 		Bufptr = s;
10237c478bd9Sstevel@tonic-gate 	}
10247c478bd9Sstevel@tonic-gate }
10257c478bd9Sstevel@tonic-gate 
10267c478bd9Sstevel@tonic-gate 
10277c478bd9Sstevel@tonic-gate static	int
readbuf(wchar_t ** s,int lincol,COLP p)10287c478bd9Sstevel@tonic-gate readbuf(wchar_t **s, int lincol, COLP p)
10297c478bd9Sstevel@tonic-gate {
10307c478bd9Sstevel@tonic-gate 	int	lines = 0;
10317c478bd9Sstevel@tonic-gate 	int	chars = 0;
10327c478bd9Sstevel@tonic-gate 	int	width;
10337c478bd9Sstevel@tonic-gate 	int	nls = 0;
10347c478bd9Sstevel@tonic-gate 	int	move;
10357c478bd9Sstevel@tonic-gate 	int	skip = 0;
10367c478bd9Sstevel@tonic-gate 	int	decr = 0;
10377c478bd9Sstevel@tonic-gate 
10387c478bd9Sstevel@tonic-gate 	width = (Ncols == 1) ? Linew : Colw;
10397c478bd9Sstevel@tonic-gate 	while (**s != WEOF) {
10407c478bd9Sstevel@tonic-gate 		switch (**s) {
10417c478bd9Sstevel@tonic-gate 			case '\n':
10427c478bd9Sstevel@tonic-gate 				lines++; nls++; chars = 0; skip = 0;
10437c478bd9Sstevel@tonic-gate 				break;
10447c478bd9Sstevel@tonic-gate 
10457c478bd9Sstevel@tonic-gate 			case '\b':
10467c478bd9Sstevel@tonic-gate 			case ESC:
10477c478bd9Sstevel@tonic-gate 				if (chars) chars--;
10487c478bd9Sstevel@tonic-gate 				break;
10497c478bd9Sstevel@tonic-gate 
10507c478bd9Sstevel@tonic-gate 			case '\t':
10517c478bd9Sstevel@tonic-gate 				move = Itabn - ((chars + Itabn) % Itabn);
10527c478bd9Sstevel@tonic-gate 				move = (move < width-chars) ? move :
10537c478bd9Sstevel@tonic-gate 				    width-chars;
10547c478bd9Sstevel@tonic-gate 				chars += move;
1055*3d19b30eSToomas Soome 				/* FALLTHROUGH */
10567c478bd9Sstevel@tonic-gate 
10577c478bd9Sstevel@tonic-gate 			default:
10587c478bd9Sstevel@tonic-gate 				if (isascii(**s)) {
10597c478bd9Sstevel@tonic-gate 					if (isprint(**s))
10607c478bd9Sstevel@tonic-gate 						chars++;
10617c478bd9Sstevel@tonic-gate 				} else if (iswprint(**s)) {
10627c478bd9Sstevel@tonic-gate 					chars += wcwidth(**s);
10637c478bd9Sstevel@tonic-gate 				}
10647c478bd9Sstevel@tonic-gate 		}
10657c478bd9Sstevel@tonic-gate 		if (chars > width) {
10667c478bd9Sstevel@tonic-gate 			lines++;
10677c478bd9Sstevel@tonic-gate 			skip++;
10687c478bd9Sstevel@tonic-gate 			decr++;
10697c478bd9Sstevel@tonic-gate 			chars = 0;
10707c478bd9Sstevel@tonic-gate 		}
10717c478bd9Sstevel@tonic-gate 		if (lincol && lines == lincol) {
10727c478bd9Sstevel@tonic-gate 			(p+1)->c_lno = p->c_lno + nls;
10737c478bd9Sstevel@tonic-gate 			(++p)->c_skip = skip;
10747c478bd9Sstevel@tonic-gate 			if (**s == '\n') (*s)++;
10757c478bd9Sstevel@tonic-gate 			p->c_ptr0 = p->c_ptr = (wchar_t *)*s;
10767c478bd9Sstevel@tonic-gate 			return (0);
10777c478bd9Sstevel@tonic-gate 		}
10787c478bd9Sstevel@tonic-gate 		if (decr)
10797c478bd9Sstevel@tonic-gate 			decr = 0;
10807c478bd9Sstevel@tonic-gate 		else
10817c478bd9Sstevel@tonic-gate 			(*s)++;
10827c478bd9Sstevel@tonic-gate 	}
10837c478bd9Sstevel@tonic-gate 	return (lines);
10847c478bd9Sstevel@tonic-gate }
10857c478bd9Sstevel@tonic-gate 
10867c478bd9Sstevel@tonic-gate 
10877c478bd9Sstevel@tonic-gate static	wint_t
get(int colno)10887c478bd9Sstevel@tonic-gate get(int colno)
10897c478bd9Sstevel@tonic-gate {
10907c478bd9Sstevel@tonic-gate 	static	int	peekc = 0;
10917c478bd9Sstevel@tonic-gate 	COLP	p;
10927c478bd9Sstevel@tonic-gate 	FILS	*q;
10937c478bd9Sstevel@tonic-gate 	int	c;
10947c478bd9Sstevel@tonic-gate 	wchar_t		wc, w;
10957c478bd9Sstevel@tonic-gate 
10967c478bd9Sstevel@tonic-gate 	if (peekc) {
10977c478bd9Sstevel@tonic-gate 		peekc = 0;
10987c478bd9Sstevel@tonic-gate 		wc = Etabc;
10997c478bd9Sstevel@tonic-gate 	} else if (Buffer) {
11007c478bd9Sstevel@tonic-gate 		p = &Colpts[colno];
11017c478bd9Sstevel@tonic-gate 		if (p->c_ptr >= (p+1)->c_ptr0)
11027c478bd9Sstevel@tonic-gate 			wc = WEOF;
11037c478bd9Sstevel@tonic-gate 		else if ((wc = *p->c_ptr) != WEOF)
11047c478bd9Sstevel@tonic-gate 			++p->c_ptr;
11057c478bd9Sstevel@tonic-gate 		if (fold && wc == WEOF)
11067c478bd9Sstevel@tonic-gate 			Fcol[colno].eof = 1;
11077c478bd9Sstevel@tonic-gate 	} else if ((wc =
11087c478bd9Sstevel@tonic-gate 		(q = &Files[Multi == 'a' ? 0 : colno])->f_nextc) == WEOF) {
11097c478bd9Sstevel@tonic-gate 		for (q = &Files[Nfiles]; --q >= Files && q->f_nextc == WEOF; )
11107c478bd9Sstevel@tonic-gate 			;
11117c478bd9Sstevel@tonic-gate 		if (q >= Files)
11127c478bd9Sstevel@tonic-gate 			wc = '\n';
11137c478bd9Sstevel@tonic-gate 	} else {
11147c478bd9Sstevel@tonic-gate 		errno = 0;
11157c478bd9Sstevel@tonic-gate 		w = _fgetwc_pr(q->f_f, &c);
11167c478bd9Sstevel@tonic-gate 		if (w == WEOF && errno == EILSEQ) {
11177c478bd9Sstevel@tonic-gate 			q->f_nextc = (wchar_t)c;
11187c478bd9Sstevel@tonic-gate 		} else {
11197c478bd9Sstevel@tonic-gate 			q->f_nextc = w;
11207c478bd9Sstevel@tonic-gate 		}
11217c478bd9Sstevel@tonic-gate 	}
11227c478bd9Sstevel@tonic-gate 
11237c478bd9Sstevel@tonic-gate 	if (Etabn != 0 && wc == Etabc) {
11247c478bd9Sstevel@tonic-gate 		++Inpos;
11257c478bd9Sstevel@tonic-gate 		peekc = ETABS;
11267c478bd9Sstevel@tonic-gate 		wc = ' ';
11277c478bd9Sstevel@tonic-gate 		return (C = wc);
11287c478bd9Sstevel@tonic-gate 	}
11297c478bd9Sstevel@tonic-gate 
11307c478bd9Sstevel@tonic-gate 	if (wc == WEOF)
11317c478bd9Sstevel@tonic-gate 		return (C = wc);
11327c478bd9Sstevel@tonic-gate 
11337c478bd9Sstevel@tonic-gate 	if (isascii(wc)) {
11347c478bd9Sstevel@tonic-gate 		if (isprint(wc)) {
11357c478bd9Sstevel@tonic-gate 			Inpos++;
11367c478bd9Sstevel@tonic-gate 			return (C = wc);
11377c478bd9Sstevel@tonic-gate 		}
11387c478bd9Sstevel@tonic-gate 	} else if (iswprint(wc)) {
11397c478bd9Sstevel@tonic-gate 		Inpos += wcwidth(wc);
11407c478bd9Sstevel@tonic-gate 		return (C = wc);
11417c478bd9Sstevel@tonic-gate 	}
11427c478bd9Sstevel@tonic-gate 
11437c478bd9Sstevel@tonic-gate 	switch (wc) {
11447c478bd9Sstevel@tonic-gate 	case '\b':
11457c478bd9Sstevel@tonic-gate 	case ESC:
11467c478bd9Sstevel@tonic-gate 		if (Inpos > 0)
11477c478bd9Sstevel@tonic-gate 			--Inpos;
11487c478bd9Sstevel@tonic-gate 		break;
11497c478bd9Sstevel@tonic-gate 	case '\f':
11507c478bd9Sstevel@tonic-gate 		if (Ncols == 1)
11517c478bd9Sstevel@tonic-gate 			break;
11527c478bd9Sstevel@tonic-gate 		wc = '\n';
11537c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
11547c478bd9Sstevel@tonic-gate 	case '\n':
11557c478bd9Sstevel@tonic-gate 	case '\r':
11567c478bd9Sstevel@tonic-gate 		Inpos = 0;
11577c478bd9Sstevel@tonic-gate 		break;
11587c478bd9Sstevel@tonic-gate 	}
11597c478bd9Sstevel@tonic-gate 	return (C = wc);
11607c478bd9Sstevel@tonic-gate }
11617c478bd9Sstevel@tonic-gate 
11627c478bd9Sstevel@tonic-gate 
11637c478bd9Sstevel@tonic-gate static	int
put(wchar_t wc)11647c478bd9Sstevel@tonic-gate put(wchar_t wc)
11657c478bd9Sstevel@tonic-gate {
11667c478bd9Sstevel@tonic-gate 	int	move = 0;
11677c478bd9Sstevel@tonic-gate 	int	width = Colw;
11687c478bd9Sstevel@tonic-gate 	int	sp = Lcolpos;
11697c478bd9Sstevel@tonic-gate 
11707c478bd9Sstevel@tonic-gate 	if (fold && Ncols == 1)
11717c478bd9Sstevel@tonic-gate 		width = Linew;
11727c478bd9Sstevel@tonic-gate 
11737c478bd9Sstevel@tonic-gate 	switch (wc) {
11747c478bd9Sstevel@tonic-gate 	case ' ':
11757c478bd9Sstevel@tonic-gate 		/* If column not full or this is separator char */
11767c478bd9Sstevel@tonic-gate 		if ((!fold && Ncols < 2) || (Lcolpos < width) ||
11777c478bd9Sstevel@tonic-gate 		    ((Sepc == wc) && (Lcolpos == width))) {
11787c478bd9Sstevel@tonic-gate 			++Nspace;
11797c478bd9Sstevel@tonic-gate 			++Lcolpos;
11807c478bd9Sstevel@tonic-gate 		}
11817c478bd9Sstevel@tonic-gate 		if (fold && sp == Lcolpos)
11827c478bd9Sstevel@tonic-gate 			if (Lcolpos >= width)
11837c478bd9Sstevel@tonic-gate 				return (1);
11847c478bd9Sstevel@tonic-gate 
11857c478bd9Sstevel@tonic-gate 		return (0);
11867c478bd9Sstevel@tonic-gate 
11877c478bd9Sstevel@tonic-gate 	case '\t':
11887c478bd9Sstevel@tonic-gate 		if (Itabn == 0)
11897c478bd9Sstevel@tonic-gate 			break;
11907c478bd9Sstevel@tonic-gate 
11917c478bd9Sstevel@tonic-gate 		/* If column not full or this is separator char */
11927c478bd9Sstevel@tonic-gate 		if ((Lcolpos < width) ||
11937c478bd9Sstevel@tonic-gate 		    ((Sepc == wc) && (Lcolpos == width))) {
11947c478bd9Sstevel@tonic-gate 			move = Itabn - ((Lcolpos + Itabn) % Itabn);
11957c478bd9Sstevel@tonic-gate 			move = (move < width-Lcolpos) ? move : width-Lcolpos;
11967c478bd9Sstevel@tonic-gate 			Nspace += move;
11977c478bd9Sstevel@tonic-gate 			Lcolpos += move;
11987c478bd9Sstevel@tonic-gate 		}
11997c478bd9Sstevel@tonic-gate 		if (fold && sp == Lcolpos)
12007c478bd9Sstevel@tonic-gate 			if (Lcolpos >= width)
12017c478bd9Sstevel@tonic-gate 				return (1);
12027c478bd9Sstevel@tonic-gate 		return (0);
12037c478bd9Sstevel@tonic-gate 
12047c478bd9Sstevel@tonic-gate 	case '\b':
12057c478bd9Sstevel@tonic-gate 		if (Lcolpos == 0)
12067c478bd9Sstevel@tonic-gate 			return (0);
12077c478bd9Sstevel@tonic-gate 		if (Nspace > 0) {
12087c478bd9Sstevel@tonic-gate 			--Nspace;
12097c478bd9Sstevel@tonic-gate 			--Lcolpos;
12107c478bd9Sstevel@tonic-gate 			return (0);
12117c478bd9Sstevel@tonic-gate 		}
12127c478bd9Sstevel@tonic-gate 		if (Lcolpos > Pcolpos) {
12137c478bd9Sstevel@tonic-gate 			--Lcolpos;
12147c478bd9Sstevel@tonic-gate 			return (0);
12157c478bd9Sstevel@tonic-gate 		}
12167c478bd9Sstevel@tonic-gate 
12177c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
12187c478bd9Sstevel@tonic-gate 
12197c478bd9Sstevel@tonic-gate 	case ESC:
12207c478bd9Sstevel@tonic-gate 		move = -1;
12217c478bd9Sstevel@tonic-gate 		break;
12227c478bd9Sstevel@tonic-gate 
12237c478bd9Sstevel@tonic-gate 	case '\n':
12247c478bd9Sstevel@tonic-gate 		++Line;
12257c478bd9Sstevel@tonic-gate 
12267c478bd9Sstevel@tonic-gate 		/*FALLTHROUGH*/
12277c478bd9Sstevel@tonic-gate 
12287c478bd9Sstevel@tonic-gate 	case '\r':
12297c478bd9Sstevel@tonic-gate 	case '\f':
12307c478bd9Sstevel@tonic-gate 		Pcolpos = 0;
12317c478bd9Sstevel@tonic-gate 		Lcolpos = 0;
12327c478bd9Sstevel@tonic-gate 		Nspace = 0;
12337c478bd9Sstevel@tonic-gate 		Outpos = 0;
12347c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
12357c478bd9Sstevel@tonic-gate 	default:
12367c478bd9Sstevel@tonic-gate 		if (isascii(wc)) {
12377c478bd9Sstevel@tonic-gate 			if (isprint(wc))
12387c478bd9Sstevel@tonic-gate 				move = 1;
12397c478bd9Sstevel@tonic-gate 			else
12407c478bd9Sstevel@tonic-gate 				move = 0;
12417c478bd9Sstevel@tonic-gate 		} else if (iswprint(wc)) {
12427c478bd9Sstevel@tonic-gate 			move = wcwidth(wc);
12437c478bd9Sstevel@tonic-gate 		} else {
12447c478bd9Sstevel@tonic-gate 			move = 0;
12457c478bd9Sstevel@tonic-gate 		}
12467c478bd9Sstevel@tonic-gate 		break;
12477c478bd9Sstevel@tonic-gate 	}
12487c478bd9Sstevel@tonic-gate 	if (Page < Fpage)
12497c478bd9Sstevel@tonic-gate 		return (0);
12507c478bd9Sstevel@tonic-gate 	if (Lcolpos > 0 || move > 0)
12517c478bd9Sstevel@tonic-gate 		Lcolpos += move;
12527c478bd9Sstevel@tonic-gate 
12537c478bd9Sstevel@tonic-gate 	putspace();
12547c478bd9Sstevel@tonic-gate 
12557c478bd9Sstevel@tonic-gate 	/* If column not full or this is separator char */
12567c478bd9Sstevel@tonic-gate 	if ((!fold && Ncols < 2) || (Lcolpos <= width) ||
12577c478bd9Sstevel@tonic-gate 	    ((Sepc == wc) && (Lcolpos > width))) {
12587c478bd9Sstevel@tonic-gate 		(void) fputwc(wc, stdout);
12597c478bd9Sstevel@tonic-gate 		Outpos += move;
12607c478bd9Sstevel@tonic-gate 		Pcolpos = Lcolpos;
12617c478bd9Sstevel@tonic-gate 	}
12627c478bd9Sstevel@tonic-gate 
12637c478bd9Sstevel@tonic-gate 	if (fold && Lcolpos > width)
12647c478bd9Sstevel@tonic-gate 		return (1);
12657c478bd9Sstevel@tonic-gate 
12667c478bd9Sstevel@tonic-gate 	return (0);
12677c478bd9Sstevel@tonic-gate }
12687c478bd9Sstevel@tonic-gate 
12697c478bd9Sstevel@tonic-gate 
12707c478bd9Sstevel@tonic-gate static	void
putspace(void)12717c478bd9Sstevel@tonic-gate putspace(void)
12727c478bd9Sstevel@tonic-gate {
12737c478bd9Sstevel@tonic-gate 	int nc = 0;
12747c478bd9Sstevel@tonic-gate 
12757c478bd9Sstevel@tonic-gate 	for (; Nspace > 0; Outpos += nc, Nspace -= nc) {
12767c478bd9Sstevel@tonic-gate #ifdef XPG4
12777c478bd9Sstevel@tonic-gate 		/* XPG4:  -i:  replace multiple SPACE chars with tab chars */
12787c478bd9Sstevel@tonic-gate 		if ((Nspace >= 2 && Itabn > 0 &&
12797c478bd9Sstevel@tonic-gate 			Nspace >= (nc = Itabn - Outpos % Itabn)) && !fold) {
12807c478bd9Sstevel@tonic-gate #else
12817c478bd9Sstevel@tonic-gate 		/* Solaris:  -i:  replace white space with tab chars */
12827c478bd9Sstevel@tonic-gate 		if ((Itabn > 0 && Nspace >= (nc = Itabn - Outpos % Itabn)) &&
12837c478bd9Sstevel@tonic-gate 			!fold) {
12847c478bd9Sstevel@tonic-gate #endif
12857c478bd9Sstevel@tonic-gate 			(void) fputwc(Itabc, stdout);
12867c478bd9Sstevel@tonic-gate 		} else {
12877c478bd9Sstevel@tonic-gate 			nc = 1;
12887c478bd9Sstevel@tonic-gate 			(void) putchar(' ');
12897c478bd9Sstevel@tonic-gate 		}
12907c478bd9Sstevel@tonic-gate 	}
12917c478bd9Sstevel@tonic-gate }
12927c478bd9Sstevel@tonic-gate 
12937c478bd9Sstevel@tonic-gate 
12947c478bd9Sstevel@tonic-gate static	void
12957c478bd9Sstevel@tonic-gate unget(int colno)
12967c478bd9Sstevel@tonic-gate {
12977c478bd9Sstevel@tonic-gate 	if (Buffer) {
12987c478bd9Sstevel@tonic-gate 		if (*(Colpts[colno].c_ptr-1) != '\t')
12997c478bd9Sstevel@tonic-gate 			--(Colpts[colno].c_ptr);
13007c478bd9Sstevel@tonic-gate 		if (Colpts[colno].c_lno)
13017c478bd9Sstevel@tonic-gate 			Colpts[colno].c_lno--;
13027c478bd9Sstevel@tonic-gate 	} else {
13037c478bd9Sstevel@tonic-gate 		if ((Multi == 'm' && colno == 0) || Multi != 'm')
13047c478bd9Sstevel@tonic-gate 			if (Lnumb && !foldcol)
13057c478bd9Sstevel@tonic-gate 				Lnumb--;
13067c478bd9Sstevel@tonic-gate 		colno = (Multi == 'a') ? 0 : colno;
13077c478bd9Sstevel@tonic-gate 		(void) ungetwc(Files[colno].f_nextc, Files[colno].f_f);
13087c478bd9Sstevel@tonic-gate 		Files[colno].f_nextc = C;
13097c478bd9Sstevel@tonic-gate 	}
13107c478bd9Sstevel@tonic-gate }
13117c478bd9Sstevel@tonic-gate 
13127c478bd9Sstevel@tonic-gate 
13137c478bd9Sstevel@tonic-gate /*
13147c478bd9Sstevel@tonic-gate  * Defer message about failure to open file to prevent messing up
13157c478bd9Sstevel@tonic-gate  * alignment of page with tear perforations or form markers.
13167c478bd9Sstevel@tonic-gate  * Treat empty file as special case and report as diagnostic.
13177c478bd9Sstevel@tonic-gate  */
13187c478bd9Sstevel@tonic-gate 
13197c478bd9Sstevel@tonic-gate static	FILE *
13207c478bd9Sstevel@tonic-gate mustopen(char *s, FILS *f)
13217c478bd9Sstevel@tonic-gate {
13227c478bd9Sstevel@tonic-gate 	char	*empty_file_msg = gettext("%s -- empty file");
13237c478bd9Sstevel@tonic-gate 	int	c;
13247c478bd9Sstevel@tonic-gate 
13257c478bd9Sstevel@tonic-gate 	if (*s == '\0') {
13267c478bd9Sstevel@tonic-gate 		f->f_name = STDINNAME();
13277c478bd9Sstevel@tonic-gate 		f->f_f = stdin;
13287c478bd9Sstevel@tonic-gate 	} else if ((f->f_f = fopen(f->f_name = s, "r")) == NULL) {
13297c478bd9Sstevel@tonic-gate 		s = ffiler(f->f_name);
13307c478bd9Sstevel@tonic-gate 		s = strcpy((char *)getspace((UNS) strlen(s) + 1), s);
13317c478bd9Sstevel@tonic-gate 	}
13327c478bd9Sstevel@tonic-gate 	if (f->f_f != NULL) {
13337c478bd9Sstevel@tonic-gate 		errno = 0;
13347c478bd9Sstevel@tonic-gate 		f->f_nextc = _fgetwc_pr(f->f_f, &c);
13357c478bd9Sstevel@tonic-gate 		if (f->f_nextc != WEOF) {
13367c478bd9Sstevel@tonic-gate 			return (f->f_f);
13377c478bd9Sstevel@tonic-gate 		} else {	/* WEOF */
13387c478bd9Sstevel@tonic-gate 			if (errno == EILSEQ) {
13397c478bd9Sstevel@tonic-gate 				f->f_nextc = (wchar_t)c;
13407c478bd9Sstevel@tonic-gate 				return (f->f_f);
13417c478bd9Sstevel@tonic-gate 			}
13427c478bd9Sstevel@tonic-gate 			if (Multi == 'm')
13437c478bd9Sstevel@tonic-gate 				return (f->f_f);
13447c478bd9Sstevel@tonic-gate 		}
13457c478bd9Sstevel@tonic-gate 		(void) sprintf(s = (char *)getspace((UNS) strlen(f->f_name)
13467c478bd9Sstevel@tonic-gate 		    + 1 + (UNS) strlen(empty_file_msg)),
13477c478bd9Sstevel@tonic-gate 		    empty_file_msg, f->f_name);
13487c478bd9Sstevel@tonic-gate 		(void) fclose(f->f_f);
13497c478bd9Sstevel@tonic-gate 	}
13507c478bd9Sstevel@tonic-gate 	Error = 1;
13517c478bd9Sstevel@tonic-gate 	if (Report)
13527c478bd9Sstevel@tonic-gate 		if (Ttyout) {	/* accumulate error reports */
13537c478bd9Sstevel@tonic-gate 			Lasterr = Lasterr->e_nextp =
13547c478bd9Sstevel@tonic-gate 			    (ERR *) getspace((UNS) sizeof (ERR));
13557c478bd9Sstevel@tonic-gate 			Lasterr->e_nextp = NULL;
13567c478bd9Sstevel@tonic-gate 			Lasterr->e_mess = s;
13577c478bd9Sstevel@tonic-gate 		} else {	/* ok to print error report now */
13587c478bd9Sstevel@tonic-gate 			cerror(s);
13597c478bd9Sstevel@tonic-gate 			(void) putc('\n', stderr);
13607c478bd9Sstevel@tonic-gate 		}
13617c478bd9Sstevel@tonic-gate 	return ((FILE *)NULL);
13627c478bd9Sstevel@tonic-gate }
13637c478bd9Sstevel@tonic-gate 
13647c478bd9Sstevel@tonic-gate 
13657c478bd9Sstevel@tonic-gate static	ANY *
13667c478bd9Sstevel@tonic-gate getspace(UNS n)
13677c478bd9Sstevel@tonic-gate {
13687c478bd9Sstevel@tonic-gate 	ANY *t;
13697c478bd9Sstevel@tonic-gate 
13707c478bd9Sstevel@tonic-gate 	if ((t = (ANY *) malloc(n)) == NULL)
13717c478bd9Sstevel@tonic-gate 		die("out of space");
13727c478bd9Sstevel@tonic-gate 	return (t);
13737c478bd9Sstevel@tonic-gate }
13747c478bd9Sstevel@tonic-gate 
13757c478bd9Sstevel@tonic-gate 
13767c478bd9Sstevel@tonic-gate static	void
13777c478bd9Sstevel@tonic-gate die(char *s)
13787c478bd9Sstevel@tonic-gate {
13797c478bd9Sstevel@tonic-gate 	++Error;
13807c478bd9Sstevel@tonic-gate 	errprint();
13817c478bd9Sstevel@tonic-gate 	cerror(s);
13827c478bd9Sstevel@tonic-gate 	(void) putc('\n', stderr);
13837c478bd9Sstevel@tonic-gate 	exit(1);
13847c478bd9Sstevel@tonic-gate 
13857c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
13867c478bd9Sstevel@tonic-gate }
13877c478bd9Sstevel@tonic-gate 
13887c478bd9Sstevel@tonic-gate 
13897c478bd9Sstevel@tonic-gate static	void
13907c478bd9Sstevel@tonic-gate errprint()	/* print accumulated error reports */
13917c478bd9Sstevel@tonic-gate {
13927c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
13937c478bd9Sstevel@tonic-gate 	for (; Err != NULL; Err = Err->e_nextp) {
13947c478bd9Sstevel@tonic-gate 		cerror(Err->e_mess);
13957c478bd9Sstevel@tonic-gate 		(void) putc('\n', stderr);
13967c478bd9Sstevel@tonic-gate 	}
13977c478bd9Sstevel@tonic-gate 	done();
13987c478bd9Sstevel@tonic-gate }
13997c478bd9Sstevel@tonic-gate 
14007c478bd9Sstevel@tonic-gate 
14017c478bd9Sstevel@tonic-gate static	void
14027c478bd9Sstevel@tonic-gate fixtty()
14037c478bd9Sstevel@tonic-gate {
14047c478bd9Sstevel@tonic-gate 	struct stat sbuf;
14057c478bd9Sstevel@tonic-gate 
14067c478bd9Sstevel@tonic-gate 	setbuf(stdout, obuf);
14077c478bd9Sstevel@tonic-gate 	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
14087c478bd9Sstevel@tonic-gate 		(void) signal(SIGINT, onintr);
14097c478bd9Sstevel@tonic-gate 	if (Ttyout = ttyname(fileno(stdout))) {	/* is stdout a tty? */
14107c478bd9Sstevel@tonic-gate 		(void) stat(Ttyout, &sbuf);
14117c478bd9Sstevel@tonic-gate 		Mode = sbuf.st_mode;		/* save permissions */
14127c478bd9Sstevel@tonic-gate 		(void) chmod(Ttyout, (S_IREAD|S_IWRITE));
14137c478bd9Sstevel@tonic-gate 	}
14147c478bd9Sstevel@tonic-gate }
14157c478bd9Sstevel@tonic-gate 
14167c478bd9Sstevel@tonic-gate 
14177c478bd9Sstevel@tonic-gate static	void
14187c478bd9Sstevel@tonic-gate onintr()
14197c478bd9Sstevel@tonic-gate {
14207c478bd9Sstevel@tonic-gate 	++Error;
14217c478bd9Sstevel@tonic-gate 	errprint();
14227c478bd9Sstevel@tonic-gate 	_exit(1);
14237c478bd9Sstevel@tonic-gate }
14247c478bd9Sstevel@tonic-gate 
14257c478bd9Sstevel@tonic-gate 
14267c478bd9Sstevel@tonic-gate static	char *
14277c478bd9Sstevel@tonic-gate GETDATE()	/* return date file was last modified */
14287c478bd9Sstevel@tonic-gate {
14297c478bd9Sstevel@tonic-gate 	static	char	*now = NULL;
14307c478bd9Sstevel@tonic-gate 	static	struct	stat	sbuf;
14317c478bd9Sstevel@tonic-gate 	static	struct	stat	nbuf;
14327c478bd9Sstevel@tonic-gate 
14337c478bd9Sstevel@tonic-gate 	if (Nfiles > 1 || Files->f_name == nulls) {
14347c478bd9Sstevel@tonic-gate 		if (now == NULL) {
14357c478bd9Sstevel@tonic-gate 			(void) time(&nbuf.st_mtime);
14367c478bd9Sstevel@tonic-gate 			(void) cftime(time_buf,
14377c478bd9Sstevel@tonic-gate 				dcgettext(NULL, FORMAT, LC_TIME),
14387c478bd9Sstevel@tonic-gate 			    &nbuf.st_mtime);
14397c478bd9Sstevel@tonic-gate 			now = time_buf;
14407c478bd9Sstevel@tonic-gate 		}
14417c478bd9Sstevel@tonic-gate 		return (now);
14427c478bd9Sstevel@tonic-gate 	} else {
14437c478bd9Sstevel@tonic-gate 		(void) stat(Files->f_name, &sbuf);
14447c478bd9Sstevel@tonic-gate 		(void) cftime(time_buf, dcgettext(NULL, FORMAT, LC_TIME),
14457c478bd9Sstevel@tonic-gate 			&sbuf.st_mtime);
14467c478bd9Sstevel@tonic-gate 		return (time_buf);
14477c478bd9Sstevel@tonic-gate 	}
14487c478bd9Sstevel@tonic-gate }
14497c478bd9Sstevel@tonic-gate 
14507c478bd9Sstevel@tonic-gate 
14517c478bd9Sstevel@tonic-gate static	char *
14527c478bd9Sstevel@tonic-gate ffiler(char *s)
14537c478bd9Sstevel@tonic-gate {
14547c478bd9Sstevel@tonic-gate 	static char buf[100];
14557c478bd9Sstevel@tonic-gate 
14567c478bd9Sstevel@tonic-gate 	(void) sprintf(buf, gettext("can't open %s"), s);
14577c478bd9Sstevel@tonic-gate 	return (buf);
14587c478bd9Sstevel@tonic-gate }
14597c478bd9Sstevel@tonic-gate 
14607c478bd9Sstevel@tonic-gate 
14617c478bd9Sstevel@tonic-gate static	void
14627c478bd9Sstevel@tonic-gate usage(int rc)
14637c478bd9Sstevel@tonic-gate {
14647c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
14657c478bd9Sstevel@tonic-gate "usage: pr [-# [-w #] [-a]] [-e[c][#]] [-i[c][#]] [-drtfp] [-n[c][#]]  \\\n"
14667c478bd9Sstevel@tonic-gate "          [-o #] [-l #] [-s[char]] [-h header] [-F] [+#] [file ...]\n\n"
14677c478bd9Sstevel@tonic-gate "       pr [-m [-w #]] [-e[c][#]] [-i[c][#]] [-drtfp] [-n[c][#]] [-0 #] \\\n"
14687c478bd9Sstevel@tonic-gate "          [-l #] [-s[char]] [-h header] [-F] [+#] file1 file2 ...\n"
14697c478bd9Sstevel@tonic-gate ));
14707c478bd9Sstevel@tonic-gate 	exit(rc);
14717c478bd9Sstevel@tonic-gate }
14727c478bd9Sstevel@tonic-gate 
14737c478bd9Sstevel@tonic-gate static wint_t
14747c478bd9Sstevel@tonic-gate _fgetwc_pr(FILE *f, int *ic)
14757c478bd9Sstevel@tonic-gate {
14767c478bd9Sstevel@tonic-gate 	int	i;
14777c478bd9Sstevel@tonic-gate 	int	len;
14787c478bd9Sstevel@tonic-gate 	char	mbuf[MB_LEN_MAX];
14797c478bd9Sstevel@tonic-gate 	int	c;
14807c478bd9Sstevel@tonic-gate 	wchar_t	wc;
14817c478bd9Sstevel@tonic-gate 
14827c478bd9Sstevel@tonic-gate 	c = getc(f);
14837c478bd9Sstevel@tonic-gate 
14847c478bd9Sstevel@tonic-gate 	if (c == EOF)
14857c478bd9Sstevel@tonic-gate 		return (WEOF);
14867c478bd9Sstevel@tonic-gate 	if (mbcurmax == 1 || isascii(c)) {
14877c478bd9Sstevel@tonic-gate 		return ((wint_t)c);
14887c478bd9Sstevel@tonic-gate 	}
14897c478bd9Sstevel@tonic-gate 	mbuf[0] = (char)c;
14907c478bd9Sstevel@tonic-gate 	for (i = 1; i < mbcurmax; i++) {
14917c478bd9Sstevel@tonic-gate 		c = getc(f);
14927c478bd9Sstevel@tonic-gate 		if (c == EOF) {
14937c478bd9Sstevel@tonic-gate 			break;
14947c478bd9Sstevel@tonic-gate 		} else {
14957c478bd9Sstevel@tonic-gate 			mbuf[i] = (char)c;
14967c478bd9Sstevel@tonic-gate 		}
14977c478bd9Sstevel@tonic-gate 	}
14987c478bd9Sstevel@tonic-gate 	mbuf[i] = 0;
14997c478bd9Sstevel@tonic-gate 
15007c478bd9Sstevel@tonic-gate 	len = mbtowc(&wc, mbuf, i);
15017c478bd9Sstevel@tonic-gate 	if (len == -1) {
15027c478bd9Sstevel@tonic-gate 		/* Illegal character */
15037c478bd9Sstevel@tonic-gate 		/* Set the first byte to *ic */
15047c478bd9Sstevel@tonic-gate 		*ic = mbuf[0];
15057c478bd9Sstevel@tonic-gate 		/* Push back remaining characters */
15067c478bd9Sstevel@tonic-gate 		for (i--; i > 0; i--) {
15077c478bd9Sstevel@tonic-gate 			(void) ungetc(mbuf[i], f);
15087c478bd9Sstevel@tonic-gate 		}
15097c478bd9Sstevel@tonic-gate 		errno = EILSEQ;
15107c478bd9Sstevel@tonic-gate 		return (WEOF);
15117c478bd9Sstevel@tonic-gate 	} else {
15127c478bd9Sstevel@tonic-gate 		/* Push back over-read characters */
15137c478bd9Sstevel@tonic-gate 		for (i--; i >= len; i--) {
15147c478bd9Sstevel@tonic-gate 			(void) ungetc(mbuf[i], f);
15157c478bd9Sstevel@tonic-gate 		}
15167c478bd9Sstevel@tonic-gate 		return ((wint_t)wc);
15177c478bd9Sstevel@tonic-gate 	}
15187c478bd9Sstevel@tonic-gate }
15197c478bd9Sstevel@tonic-gate 
15207c478bd9Sstevel@tonic-gate static size_t
15217c478bd9Sstevel@tonic-gate freadw(wchar_t *ptr, size_t nitems, FILE *f)
15227c478bd9Sstevel@tonic-gate {
15237c478bd9Sstevel@tonic-gate 	size_t	i;
15247c478bd9Sstevel@tonic-gate 	size_t	ret;
15257c478bd9Sstevel@tonic-gate 	int	c;
15267c478bd9Sstevel@tonic-gate 	wchar_t	*p;
15277c478bd9Sstevel@tonic-gate 	wint_t	wc;
15287c478bd9Sstevel@tonic-gate 
15297c478bd9Sstevel@tonic-gate 	if (feof(f)) {
15307c478bd9Sstevel@tonic-gate 		return (0);
15317c478bd9Sstevel@tonic-gate 	}
15327c478bd9Sstevel@tonic-gate 
15337c478bd9Sstevel@tonic-gate 	p = ptr;
15347c478bd9Sstevel@tonic-gate 	ret = 0;
15357c478bd9Sstevel@tonic-gate 	for (i = 0; i < nitems; i++) {
15367c478bd9Sstevel@tonic-gate 		errno = 0;
15377c478bd9Sstevel@tonic-gate 		wc = _fgetwc_pr(f, &c);
15387c478bd9Sstevel@tonic-gate 		if (wc == WEOF) {
15397c478bd9Sstevel@tonic-gate 			if (errno == EILSEQ) {
15407c478bd9Sstevel@tonic-gate 				*p++ = (wchar_t)c;
15417c478bd9Sstevel@tonic-gate 				ret++;
15427c478bd9Sstevel@tonic-gate 			} else {
15437c478bd9Sstevel@tonic-gate 				return (ret);
15447c478bd9Sstevel@tonic-gate 			}
15457c478bd9Sstevel@tonic-gate 		} else {
15467c478bd9Sstevel@tonic-gate 			*p++ = (wchar_t)wc;
15477c478bd9Sstevel@tonic-gate 			ret++;
15487c478bd9Sstevel@tonic-gate 		}
15497c478bd9Sstevel@tonic-gate 	}
15507c478bd9Sstevel@tonic-gate 	return (ret);
15517c478bd9Sstevel@tonic-gate }
1552