10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
70Sstevel@tonic-gate * with the License.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate * See the License for the specific language governing permissions
120Sstevel@tonic-gate * and limitations under the License.
130Sstevel@tonic-gate *
140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate *
200Sstevel@tonic-gate * CDDL HEADER END
210Sstevel@tonic-gate */
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate /*
280Sstevel@tonic-gate * Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T
290Sstevel@tonic-gate * All Rights Reserved
300Sstevel@tonic-gate */
310Sstevel@tonic-gate
320Sstevel@tonic-gate /*
330Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988
340Sstevel@tonic-gate * The Regents of the University of California
350Sstevel@tonic-gate * All Rights Reserved
360Sstevel@tonic-gate *
370Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from
380Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its
390Sstevel@tonic-gate * contributors.
400Sstevel@tonic-gate */
410Sstevel@tonic-gate
420Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
430Sstevel@tonic-gate
440Sstevel@tonic-gate /*
450Sstevel@tonic-gate * PR command (print files in pages and columns, with headings)
460Sstevel@tonic-gate * 2+head+2+page[56]+5
470Sstevel@tonic-gate */
480Sstevel@tonic-gate
490Sstevel@tonic-gate #include <stdio.h>
500Sstevel@tonic-gate #include <signal.h>
510Sstevel@tonic-gate #include <ctype.h>
520Sstevel@tonic-gate #include <sys/types.h>
530Sstevel@tonic-gate #include <sys/stat.h>
540Sstevel@tonic-gate #include <unistd.h>
550Sstevel@tonic-gate #include <stdlib.h>
560Sstevel@tonic-gate #include <locale.h>
570Sstevel@tonic-gate #include <string.h>
580Sstevel@tonic-gate #include <limits.h>
590Sstevel@tonic-gate #include <wchar.h>
600Sstevel@tonic-gate #include <errno.h>
610Sstevel@tonic-gate
620Sstevel@tonic-gate #define ESC '\033'
630Sstevel@tonic-gate #define LENGTH 66
640Sstevel@tonic-gate #define LINEW 72
650Sstevel@tonic-gate #define NUMW 5
660Sstevel@tonic-gate #define MARGIN 10
670Sstevel@tonic-gate #define DEFTAB 8
680Sstevel@tonic-gate #define NFILES 10
690Sstevel@tonic-gate #define STDINNAME() nulls
700Sstevel@tonic-gate #define PROMPT() (void) putc('\7', stderr) /* BEL */
710Sstevel@tonic-gate #define NOFILE nulls
720Sstevel@tonic-gate #define ETABS (Inpos % Etabn)
730Sstevel@tonic-gate #define NSEPC '\t'
740Sstevel@tonic-gate #define HEAD gettext("%s %s Page %d\n\n\n"), date, head, Page
750Sstevel@tonic-gate #define cerror(S) (void) fprintf(stderr, "pr: %s", gettext(S))
760Sstevel@tonic-gate #define done() if (Ttyout) (void) chmod(Ttyout, Mode)
770Sstevel@tonic-gate #define ALL_NUMS(s) (strspn(s, "0123456789") == strlen(s))
780Sstevel@tonic-gate #define REMOVE_ARG(argc, argp) \
790Sstevel@tonic-gate { \
800Sstevel@tonic-gate char **p = argp; \
810Sstevel@tonic-gate while (*p != NULL) \
820Sstevel@tonic-gate { \
830Sstevel@tonic-gate *p = *(p + 1); \
840Sstevel@tonic-gate p++; \
850Sstevel@tonic-gate } \
860Sstevel@tonic-gate argc--; \
870Sstevel@tonic-gate }
880Sstevel@tonic-gate #define SQUEEZE_ARG(argp, ind, n) \
890Sstevel@tonic-gate { \
900Sstevel@tonic-gate int i; \
910Sstevel@tonic-gate for (i = ind; argp[i]; i++) \
920Sstevel@tonic-gate argp[i] = argp[i + n]; \
930Sstevel@tonic-gate }
940Sstevel@tonic-gate
950Sstevel@tonic-gate /*
960Sstevel@tonic-gate * ---date time format---
970Sstevel@tonic-gate * b -- abbreviated month name
980Sstevel@tonic-gate * e -- day of month
990Sstevel@tonic-gate * H -- Hour (24 hour version)
1000Sstevel@tonic-gate * M -- Minute
1010Sstevel@tonic-gate * Y -- Year in the form ccyy
1020Sstevel@tonic-gate */
1030Sstevel@tonic-gate #define FORMAT "%b %e %H:%M %Y"
1040Sstevel@tonic-gate
1050Sstevel@tonic-gate typedef int ANY;
1060Sstevel@tonic-gate typedef unsigned int UNS;
1070Sstevel@tonic-gate typedef struct { FILE *f_f; char *f_name; wchar_t f_nextc; } FILS;
1080Sstevel@tonic-gate typedef struct {int fold; int skip; int eof; } foldinf;
1090Sstevel@tonic-gate typedef struct { wchar_t *c_ptr, *c_ptr0; long c_lno; int c_skip; } *COLP;
1100Sstevel@tonic-gate typedef struct err { struct err *e_nextp; char *e_mess; } ERR;
1110Sstevel@tonic-gate
1120Sstevel@tonic-gate /*
1130Sstevel@tonic-gate * Global data.
1140Sstevel@tonic-gate */
1150Sstevel@tonic-gate static FILS *Files;
1160Sstevel@tonic-gate static mode_t Mode;
1170Sstevel@tonic-gate static int Multi = 0;
1180Sstevel@tonic-gate static int Nfiles = 0;
1190Sstevel@tonic-gate static int Error = 0;
1200Sstevel@tonic-gate static char nulls[] = "";
1210Sstevel@tonic-gate static char *Ttyout;
1220Sstevel@tonic-gate static char obuf[BUFSIZ];
1230Sstevel@tonic-gate static char time_buf[50]; /* array to hold the time and date */
1240Sstevel@tonic-gate static long Lnumb = 0;
1250Sstevel@tonic-gate static FILE *Ttyin = stdin;
1260Sstevel@tonic-gate static int Dblspace = 1;
1270Sstevel@tonic-gate static int Fpage = 1;
1280Sstevel@tonic-gate static int Formfeed = 0;
1290Sstevel@tonic-gate static int Length = LENGTH;
1300Sstevel@tonic-gate static int Linew = 0;
1310Sstevel@tonic-gate static int Offset = 0;
1320Sstevel@tonic-gate static int Ncols = 0;
1330Sstevel@tonic-gate static int Pause = 0;
1340Sstevel@tonic-gate static wchar_t Sepc = 0;
1350Sstevel@tonic-gate static int Colw;
1360Sstevel@tonic-gate static int Plength;
1370Sstevel@tonic-gate static int Margin = MARGIN;
1380Sstevel@tonic-gate static int Numw;
1390Sstevel@tonic-gate static int Nsepc = NSEPC;
1400Sstevel@tonic-gate static int Report = 1;
1410Sstevel@tonic-gate static int Etabn = 0;
1420Sstevel@tonic-gate static wchar_t Etabc = '\t';
1430Sstevel@tonic-gate static int Itabn = 0;
1440Sstevel@tonic-gate static wchar_t Itabc = '\t';
1450Sstevel@tonic-gate static int fold = 0;
1460Sstevel@tonic-gate static int foldcol = 0;
1470Sstevel@tonic-gate static int alleof = 0;
1480Sstevel@tonic-gate static char *Head = NULL;
1490Sstevel@tonic-gate static wchar_t *Buffer = NULL, *Bufend, *Bufptr;
1500Sstevel@tonic-gate static UNS Buflen;
1510Sstevel@tonic-gate static COLP Colpts;
1520Sstevel@tonic-gate static foldinf *Fcol;
1530Sstevel@tonic-gate static int Page;
1540Sstevel@tonic-gate static wchar_t C = '\0';
1550Sstevel@tonic-gate static int Nspace;
1560Sstevel@tonic-gate static int Inpos;
1570Sstevel@tonic-gate static int Outpos;
1580Sstevel@tonic-gate static int Lcolpos;
1590Sstevel@tonic-gate static int Pcolpos;
1600Sstevel@tonic-gate static int Line;
1610Sstevel@tonic-gate static ERR *Err = NULL;
1620Sstevel@tonic-gate static ERR *Lasterr = (ERR *)&Err;
1630Sstevel@tonic-gate static int mbcurmax = 1;
1640Sstevel@tonic-gate
1650Sstevel@tonic-gate /*
1660Sstevel@tonic-gate * Function prototypes.
1670Sstevel@tonic-gate */
1680Sstevel@tonic-gate static void onintr();
1690Sstevel@tonic-gate static ANY *getspace();
1700Sstevel@tonic-gate static int findopt(int, char **);
1710Sstevel@tonic-gate static void fixtty();
1720Sstevel@tonic-gate static char *GETDATE();
1730Sstevel@tonic-gate static char *ffiler(char *);
1740Sstevel@tonic-gate static int print(char *);
1750Sstevel@tonic-gate static void putpage();
1760Sstevel@tonic-gate static void foldpage();
1770Sstevel@tonic-gate static void nexbuf();
1780Sstevel@tonic-gate static void foldbuf();
1790Sstevel@tonic-gate static void balance(int);
1800Sstevel@tonic-gate static int readbuf(wchar_t **, int, COLP);
1810Sstevel@tonic-gate static wint_t get(int);
1820Sstevel@tonic-gate static int put(wchar_t);
1830Sstevel@tonic-gate static void putspace();
1840Sstevel@tonic-gate static void unget(int);
1850Sstevel@tonic-gate static FILE *mustopen(char *, FILS *);
1860Sstevel@tonic-gate static void die(char *);
1870Sstevel@tonic-gate static void errprint();
1880Sstevel@tonic-gate static void usage(int);
1890Sstevel@tonic-gate static wint_t _fgetwc_pr(FILE *, int *);
1900Sstevel@tonic-gate static size_t freadw(wchar_t *, size_t, FILE *);
1910Sstevel@tonic-gate
1920Sstevel@tonic-gate
193212Scf46844 int
main(int argc,char ** argv)1940Sstevel@tonic-gate main(int argc, char **argv)
1950Sstevel@tonic-gate {
1960Sstevel@tonic-gate FILS fstr[NFILES];
1970Sstevel@tonic-gate int nfdone = 0;
1980Sstevel@tonic-gate
1990Sstevel@tonic-gate
2000Sstevel@tonic-gate /* Get locale variables for environment */
2010Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
2020Sstevel@tonic-gate
2030Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
2040Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */
2050Sstevel@tonic-gate #endif
2060Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
2070Sstevel@tonic-gate
2080Sstevel@tonic-gate mbcurmax = MB_CUR_MAX;
2090Sstevel@tonic-gate Files = fstr;
2100Sstevel@tonic-gate for (argc = findopt(argc, argv); argc > 0; --argc, ++argv) {
2110Sstevel@tonic-gate if (Multi == 'm') {
2120Sstevel@tonic-gate if (Nfiles >= NFILES - 1) die("too many files");
2130Sstevel@tonic-gate if (mustopen(*argv, &Files[Nfiles++]) == NULL)
2140Sstevel@tonic-gate ++nfdone; /* suppress printing */
2150Sstevel@tonic-gate } else {
2160Sstevel@tonic-gate if (print(*argv))
2170Sstevel@tonic-gate (void) fclose(Files->f_f);
2180Sstevel@tonic-gate ++nfdone;
2190Sstevel@tonic-gate }
2200Sstevel@tonic-gate }
2210Sstevel@tonic-gate if (!nfdone) /* no files named, use stdin */
2220Sstevel@tonic-gate (void) print(NOFILE); /* on GCOS, use current file, if any */
2230Sstevel@tonic-gate
2240Sstevel@tonic-gate if (Report) {
2250Sstevel@tonic-gate errprint(); /* print accumulated error reports */
2260Sstevel@tonic-gate exit(Error);
2270Sstevel@tonic-gate }
2280Sstevel@tonic-gate
2290Sstevel@tonic-gate return (Error);
2300Sstevel@tonic-gate }
2310Sstevel@tonic-gate
2320Sstevel@tonic-gate
2330Sstevel@tonic-gate /*
2340Sstevel@tonic-gate * findopt() returns argc modified to be the number of explicitly supplied
2350Sstevel@tonic-gate * filenames, including '-', the explicit request to use stdin.
2360Sstevel@tonic-gate * argc == 0 implies that no filenames were supplied and stdin should be used.
2370Sstevel@tonic-gate * Options are striped from argv and only file names are returned.
2380Sstevel@tonic-gate */
2390Sstevel@tonic-gate
2400Sstevel@tonic-gate static int
findopt(int argc,char ** argv)2410Sstevel@tonic-gate findopt(int argc, char **argv)
2420Sstevel@tonic-gate {
2430Sstevel@tonic-gate int eargc = 0;
2440Sstevel@tonic-gate int c;
2450Sstevel@tonic-gate int mflg = 0;
2460Sstevel@tonic-gate int aflg = 0;
2470Sstevel@tonic-gate int optnum;
2480Sstevel@tonic-gate int argv_ind;
2490Sstevel@tonic-gate int end_opt;
2500Sstevel@tonic-gate int i;
251*1191Sss161016 int isarg = 0;
2520Sstevel@tonic-gate
2530Sstevel@tonic-gate fixtty();
2540Sstevel@tonic-gate
2550Sstevel@tonic-gate /* Handle page number option */
2560Sstevel@tonic-gate for (optnum = 1, end_opt = 0; optnum < argc && !end_opt; optnum++) {
2570Sstevel@tonic-gate switch (*argv[optnum]) {
2580Sstevel@tonic-gate case '+':
2590Sstevel@tonic-gate /* check for all digits */
2600Sstevel@tonic-gate if (strlen(&argv[optnum][1]) !=
2610Sstevel@tonic-gate strspn(&argv[optnum][1], "0123456789")) {
2620Sstevel@tonic-gate (void) fprintf(stderr, gettext(
2630Sstevel@tonic-gate "pr: Badly formed number\n"));
2640Sstevel@tonic-gate exit(1);
2650Sstevel@tonic-gate }
2660Sstevel@tonic-gate
2670Sstevel@tonic-gate if ((Fpage = (int)strtol(&argv[optnum][1],
2680Sstevel@tonic-gate (char **)NULL, 10)) < 0) {
2690Sstevel@tonic-gate (void) fprintf(stderr, gettext(
2700Sstevel@tonic-gate "pr: Badly formed number\n"));
2710Sstevel@tonic-gate exit(1);
2720Sstevel@tonic-gate }
2730Sstevel@tonic-gate REMOVE_ARG(argc, &argv[optnum]);
2740Sstevel@tonic-gate optnum--;
2750Sstevel@tonic-gate break;
2760Sstevel@tonic-gate
2770Sstevel@tonic-gate case '-':
2780Sstevel@tonic-gate /* Check for end of options */
2790Sstevel@tonic-gate if (argv[optnum][1] == '-') {
2800Sstevel@tonic-gate end_opt++;
2810Sstevel@tonic-gate break;
2820Sstevel@tonic-gate }
283*1191Sss161016
284*1191Sss161016 if (argv[optnum][1] == 'h' || argv[optnum][1] == 'l' ||
285*1191Sss161016 argv[optnum][1] == 'o' || argv[optnum][1] == 'w')
286*1191Sss161016 isarg = 1;
287*1191Sss161016 else
288*1191Sss161016 isarg = 0;
289*1191Sss161016
2900Sstevel@tonic-gate break;
2910Sstevel@tonic-gate
2920Sstevel@tonic-gate default:
293*1191Sss161016 if (isarg == 0)
294*1191Sss161016 end_opt++;
295*1191Sss161016 else
296*1191Sss161016 isarg = 0;
2970Sstevel@tonic-gate break;
2980Sstevel@tonic-gate }
2990Sstevel@tonic-gate }
3000Sstevel@tonic-gate
3010Sstevel@tonic-gate /*
3020Sstevel@tonic-gate * Handle options with optional arguments.
3030Sstevel@tonic-gate * If optional arguments are present they may not be separated
3040Sstevel@tonic-gate * from the option letter.
3050Sstevel@tonic-gate */
3060Sstevel@tonic-gate
3070Sstevel@tonic-gate for (optnum = 1; optnum < argc; optnum++) {
3080Sstevel@tonic-gate if (argv[optnum][0] == '-' && argv[optnum][1] == '-')
3090Sstevel@tonic-gate /* End of options */
3100Sstevel@tonic-gate break;
3110Sstevel@tonic-gate
3120Sstevel@tonic-gate if (argv[optnum][0] == '-' && argv[optnum][1] == '\0')
3130Sstevel@tonic-gate /* stdin file name */
3140Sstevel@tonic-gate continue;
3150Sstevel@tonic-gate
3160Sstevel@tonic-gate if (argv[optnum][0] != '-')
3170Sstevel@tonic-gate /* not option */
3180Sstevel@tonic-gate continue;
3190Sstevel@tonic-gate
3200Sstevel@tonic-gate for (argv_ind = 1; argv[optnum][argv_ind] != '\0'; argv_ind++) {
3210Sstevel@tonic-gate switch (argv[optnum][argv_ind]) {
3220Sstevel@tonic-gate case 'e':
3230Sstevel@tonic-gate SQUEEZE_ARG(argv[optnum], argv_ind, 1);
3240Sstevel@tonic-gate if ((c = argv[optnum][argv_ind]) != '\0' &&
3250Sstevel@tonic-gate !isdigit(c)) {
3260Sstevel@tonic-gate int r;
3270Sstevel@tonic-gate wchar_t wc;
3280Sstevel@tonic-gate r = mbtowc(&wc, &argv[optnum][argv_ind],
3290Sstevel@tonic-gate mbcurmax);
3300Sstevel@tonic-gate if (r == -1) {
3310Sstevel@tonic-gate (void) fprintf(stderr, gettext(
3320Sstevel@tonic-gate "pr: Illegal character in -e option\n"));
3330Sstevel@tonic-gate exit(1);
3340Sstevel@tonic-gate }
3350Sstevel@tonic-gate Etabc = wc;
3360Sstevel@tonic-gate SQUEEZE_ARG(argv[optnum], argv_ind, r);
3370Sstevel@tonic-gate }
3380Sstevel@tonic-gate if (isdigit(argv[optnum][argv_ind])) {
3390Sstevel@tonic-gate Etabn = (int)strtol(&argv[optnum]
3400Sstevel@tonic-gate [argv_ind], (char **)NULL, 10);
3410Sstevel@tonic-gate while (isdigit(argv[optnum][argv_ind]))
3420Sstevel@tonic-gate SQUEEZE_ARG(argv[optnum],
3430Sstevel@tonic-gate argv_ind, 1);
3440Sstevel@tonic-gate }
3450Sstevel@tonic-gate if (Etabn <= 0)
3460Sstevel@tonic-gate Etabn = DEFTAB;
3470Sstevel@tonic-gate argv_ind--;
3480Sstevel@tonic-gate break;
3490Sstevel@tonic-gate
3500Sstevel@tonic-gate case 'i':
3510Sstevel@tonic-gate SQUEEZE_ARG(argv[optnum], argv_ind, 1);
3520Sstevel@tonic-gate if ((c = argv[optnum][argv_ind]) != '\0' &&
3530Sstevel@tonic-gate !isdigit(c)) {
3540Sstevel@tonic-gate int r;
3550Sstevel@tonic-gate wchar_t wc;
3560Sstevel@tonic-gate r = mbtowc(&wc, &argv[optnum][argv_ind],
3570Sstevel@tonic-gate mbcurmax);
3580Sstevel@tonic-gate if (r == -1) {
3590Sstevel@tonic-gate (void) fprintf(stderr, gettext(
3600Sstevel@tonic-gate "pr: Illegal character in -i option\n"));
3610Sstevel@tonic-gate exit(1);
3620Sstevel@tonic-gate }
3630Sstevel@tonic-gate Itabc = wc;
3640Sstevel@tonic-gate SQUEEZE_ARG(argv[optnum], argv_ind, r);
3650Sstevel@tonic-gate }
3660Sstevel@tonic-gate if (isdigit(argv[optnum][argv_ind])) {
3670Sstevel@tonic-gate Itabn = (int)strtol(&argv[optnum]
3680Sstevel@tonic-gate [argv_ind], (char **)NULL, 10);
3690Sstevel@tonic-gate while (isdigit(argv[optnum][argv_ind]))
3700Sstevel@tonic-gate SQUEEZE_ARG(argv[optnum],
3710Sstevel@tonic-gate argv_ind, 1);
3720Sstevel@tonic-gate }
3730Sstevel@tonic-gate if (Itabn <= 0)
3740Sstevel@tonic-gate Itabn = DEFTAB;
3750Sstevel@tonic-gate argv_ind--;
3760Sstevel@tonic-gate break;
3770Sstevel@tonic-gate
3780Sstevel@tonic-gate
3790Sstevel@tonic-gate case 'n':
3800Sstevel@tonic-gate ++Lnumb;
3810Sstevel@tonic-gate SQUEEZE_ARG(argv[optnum], argv_ind, 1);
3820Sstevel@tonic-gate if ((c = argv[optnum][argv_ind]) != '\0' &&
3830Sstevel@tonic-gate !isdigit(c)) {
3840Sstevel@tonic-gate int r;
3850Sstevel@tonic-gate wchar_t wc;
3860Sstevel@tonic-gate r = mbtowc(&wc, &argv[optnum][argv_ind],
3870Sstevel@tonic-gate mbcurmax);
3880Sstevel@tonic-gate if (r == -1) {
3890Sstevel@tonic-gate (void) fprintf(stderr, gettext(
3900Sstevel@tonic-gate "pr: Illegal character in -n option\n"));
3910Sstevel@tonic-gate exit(1);
3920Sstevel@tonic-gate }
3930Sstevel@tonic-gate Nsepc = wc;
3940Sstevel@tonic-gate SQUEEZE_ARG(argv[optnum], argv_ind, r);
3950Sstevel@tonic-gate }
3960Sstevel@tonic-gate if (isdigit(argv[optnum][argv_ind])) {
3970Sstevel@tonic-gate Numw = (int)strtol(&argv[optnum]
3980Sstevel@tonic-gate [argv_ind], (char **)NULL, 10);
3990Sstevel@tonic-gate while (isdigit(argv[optnum][argv_ind]))
4000Sstevel@tonic-gate SQUEEZE_ARG(argv[optnum],
4010Sstevel@tonic-gate argv_ind, 1);
4020Sstevel@tonic-gate }
4030Sstevel@tonic-gate argv_ind--;
4040Sstevel@tonic-gate if (!Numw)
4050Sstevel@tonic-gate Numw = NUMW;
4060Sstevel@tonic-gate break;
4070Sstevel@tonic-gate
4080Sstevel@tonic-gate case 's':
4090Sstevel@tonic-gate SQUEEZE_ARG(argv[optnum], argv_ind, 1);
4100Sstevel@tonic-gate if ((Sepc = argv[optnum][argv_ind]) == '\0')
4110Sstevel@tonic-gate Sepc = '\t';
4120Sstevel@tonic-gate else {
4130Sstevel@tonic-gate int r;
4140Sstevel@tonic-gate wchar_t wc;
4150Sstevel@tonic-gate r = mbtowc(&wc, &argv[optnum][argv_ind],
4160Sstevel@tonic-gate mbcurmax);
4170Sstevel@tonic-gate if (r == -1) {
4180Sstevel@tonic-gate (void) fprintf(stderr, gettext(
4190Sstevel@tonic-gate "pr: Illegal character in -s option\n"));
4200Sstevel@tonic-gate exit(1);
4210Sstevel@tonic-gate }
4220Sstevel@tonic-gate Sepc = wc;
4230Sstevel@tonic-gate SQUEEZE_ARG(argv[optnum], argv_ind, r);
4240Sstevel@tonic-gate }
4250Sstevel@tonic-gate argv_ind--;
4260Sstevel@tonic-gate break;
4270Sstevel@tonic-gate
4280Sstevel@tonic-gate default:
4290Sstevel@tonic-gate break;
4300Sstevel@tonic-gate }
4310Sstevel@tonic-gate }
4320Sstevel@tonic-gate if (argv[optnum][0] == '-' && argv[optnum][1] == '\0') {
4330Sstevel@tonic-gate REMOVE_ARG(argc, &argv[optnum]);
4340Sstevel@tonic-gate optnum--;
4350Sstevel@tonic-gate }
4360Sstevel@tonic-gate }
4370Sstevel@tonic-gate
4380Sstevel@tonic-gate /* Now get the other options */
4390Sstevel@tonic-gate while ((c = getopt(argc, argv, "0123456789adfFh:l:mo:prtw:"))
4400Sstevel@tonic-gate != EOF) {
4410Sstevel@tonic-gate switch (c) {
4420Sstevel@tonic-gate case '0':
4430Sstevel@tonic-gate case '1':
4440Sstevel@tonic-gate case '2':
4450Sstevel@tonic-gate case '3':
4460Sstevel@tonic-gate case '4':
4470Sstevel@tonic-gate case '5':
4480Sstevel@tonic-gate case '6':
4490Sstevel@tonic-gate case '7':
4500Sstevel@tonic-gate case '8':
4510Sstevel@tonic-gate case '9':
4520Sstevel@tonic-gate Ncols *= 10;
4530Sstevel@tonic-gate Ncols += c - '0';
4540Sstevel@tonic-gate break;
4550Sstevel@tonic-gate
4560Sstevel@tonic-gate case 'a':
4570Sstevel@tonic-gate aflg++;
4580Sstevel@tonic-gate if (!Multi)
4590Sstevel@tonic-gate Multi = c;
4600Sstevel@tonic-gate break;
4610Sstevel@tonic-gate
4620Sstevel@tonic-gate case 'd':
4630Sstevel@tonic-gate Dblspace = 2;
4640Sstevel@tonic-gate break;
4650Sstevel@tonic-gate
4660Sstevel@tonic-gate case 'f':
4670Sstevel@tonic-gate ++Formfeed;
4680Sstevel@tonic-gate ++Pause;
4690Sstevel@tonic-gate break;
4700Sstevel@tonic-gate
4710Sstevel@tonic-gate case 'h':
4720Sstevel@tonic-gate Head = optarg;
4730Sstevel@tonic-gate break;
4740Sstevel@tonic-gate
4750Sstevel@tonic-gate case 'l':
4760Sstevel@tonic-gate if (strlen(optarg) != strspn(optarg, "0123456789"))
4770Sstevel@tonic-gate usage(1);
4780Sstevel@tonic-gate Length = (int)strtol(optarg, (char **)NULL, 10);
4790Sstevel@tonic-gate break;
4800Sstevel@tonic-gate
4810Sstevel@tonic-gate case 'm':
4820Sstevel@tonic-gate mflg++;
4830Sstevel@tonic-gate Multi = c;
4840Sstevel@tonic-gate break;
4850Sstevel@tonic-gate
4860Sstevel@tonic-gate case 'o':
4870Sstevel@tonic-gate if (strlen(optarg) != strspn(optarg, "0123456789"))
4880Sstevel@tonic-gate usage(1);
4890Sstevel@tonic-gate Offset = (int)strtol(optarg, (char **)NULL, 10);
4900Sstevel@tonic-gate break;
4910Sstevel@tonic-gate
4920Sstevel@tonic-gate case 'p':
4930Sstevel@tonic-gate ++Pause;
4940Sstevel@tonic-gate break;
4950Sstevel@tonic-gate
4960Sstevel@tonic-gate case 'r':
4970Sstevel@tonic-gate Report = 0;
4980Sstevel@tonic-gate break;
4990Sstevel@tonic-gate
5000Sstevel@tonic-gate case 't':
5010Sstevel@tonic-gate Margin = 0;
5020Sstevel@tonic-gate break;
5030Sstevel@tonic-gate
5040Sstevel@tonic-gate case 'w':
5050Sstevel@tonic-gate if (strlen(optarg) != strspn(optarg, "0123456789"))
5060Sstevel@tonic-gate usage(1);
5070Sstevel@tonic-gate Linew = (int)strtol(optarg, (char **)NULL, 10);
5080Sstevel@tonic-gate break;
5090Sstevel@tonic-gate
5100Sstevel@tonic-gate case 'F':
5110Sstevel@tonic-gate #ifdef XPG4
5120Sstevel@tonic-gate ++Formfeed;
5130Sstevel@tonic-gate #else
5140Sstevel@tonic-gate fold++;
5150Sstevel@tonic-gate #endif
5160Sstevel@tonic-gate break;
5170Sstevel@tonic-gate
5180Sstevel@tonic-gate case '?':
5190Sstevel@tonic-gate usage(2);
5200Sstevel@tonic-gate break;
5210Sstevel@tonic-gate
5220Sstevel@tonic-gate default :
5230Sstevel@tonic-gate usage(2);
5240Sstevel@tonic-gate }
5250Sstevel@tonic-gate }
5260Sstevel@tonic-gate
5270Sstevel@tonic-gate /* Count the file names and strip options */
5280Sstevel@tonic-gate for (i = 1; i < argc; i++) {
5290Sstevel@tonic-gate /* Check for explicit stdin */
5300Sstevel@tonic-gate if ((argv[i][0] == '-') && (argv[i][1] == '\0')) {
5310Sstevel@tonic-gate argv[eargc++][0] = '\0';
5320Sstevel@tonic-gate REMOVE_ARG(argc, &argv[i]);
5330Sstevel@tonic-gate if (i < optind)
5340Sstevel@tonic-gate optind--;
5350Sstevel@tonic-gate }
5360Sstevel@tonic-gate }
5370Sstevel@tonic-gate for (i = eargc; optind < argc; i++, optind++) {
5380Sstevel@tonic-gate argv[i] = argv[optind];
5390Sstevel@tonic-gate eargc++;
5400Sstevel@tonic-gate }
5410Sstevel@tonic-gate
5420Sstevel@tonic-gate /* Check options */
5430Sstevel@tonic-gate if (Ncols == 0)
5440Sstevel@tonic-gate Ncols = 1;
5450Sstevel@tonic-gate
5460Sstevel@tonic-gate if (mflg && (Ncols > 1)) {
5470Sstevel@tonic-gate (void) fprintf(stderr,
5480Sstevel@tonic-gate gettext("pr: only one of either -m or -column allowed\n"));
5490Sstevel@tonic-gate usage(1);
5500Sstevel@tonic-gate }
5510Sstevel@tonic-gate
5520Sstevel@tonic-gate if (Ncols == 1 && fold)
5530Sstevel@tonic-gate Multi = 'm';
5540Sstevel@tonic-gate
5550Sstevel@tonic-gate if (Length <= 0)
5560Sstevel@tonic-gate Length = LENGTH;
5570Sstevel@tonic-gate
5580Sstevel@tonic-gate if (Length <= Margin)
5590Sstevel@tonic-gate Margin = 0;
5600Sstevel@tonic-gate
5610Sstevel@tonic-gate Plength = Length - Margin/2;
5620Sstevel@tonic-gate
5630Sstevel@tonic-gate if (Multi == 'm')
5640Sstevel@tonic-gate Ncols = eargc;
5650Sstevel@tonic-gate
5660Sstevel@tonic-gate switch (Ncols) {
5670Sstevel@tonic-gate case 0:
5680Sstevel@tonic-gate Ncols = 1;
5690Sstevel@tonic-gate break;
5700Sstevel@tonic-gate
5710Sstevel@tonic-gate case 1:
5720Sstevel@tonic-gate break;
5730Sstevel@tonic-gate
5740Sstevel@tonic-gate default:
5750Sstevel@tonic-gate if (Etabn == 0) /* respect explicit tab specification */
5760Sstevel@tonic-gate Etabn = DEFTAB;
5770Sstevel@tonic-gate if (Itabn == 0)
5780Sstevel@tonic-gate Itabn = DEFTAB;
5790Sstevel@tonic-gate }
5800Sstevel@tonic-gate
5810Sstevel@tonic-gate if ((Fcol = (foldinf *) malloc(sizeof (foldinf) * Ncols)) == NULL) {
5820Sstevel@tonic-gate (void) fprintf(stderr, gettext("pr: malloc failed\n"));
5830Sstevel@tonic-gate exit(1);
5840Sstevel@tonic-gate }
5850Sstevel@tonic-gate for (i = 0; i < Ncols; i++)
5860Sstevel@tonic-gate Fcol[i].fold = Fcol[i].skip = 0;
5870Sstevel@tonic-gate
5880Sstevel@tonic-gate if (Linew == 0)
5890Sstevel@tonic-gate Linew = Ncols != 1 && Sepc == 0 ? LINEW : 512;
5900Sstevel@tonic-gate
5910Sstevel@tonic-gate if (Lnumb) {
5920Sstevel@tonic-gate int numw;
5930Sstevel@tonic-gate
5940Sstevel@tonic-gate if (Nsepc == '\t') {
5950Sstevel@tonic-gate if (Itabn == 0)
5960Sstevel@tonic-gate numw = Numw + DEFTAB - (Numw % DEFTAB);
5970Sstevel@tonic-gate else
5980Sstevel@tonic-gate numw = Numw + Itabn - (Numw % Itabn);
5990Sstevel@tonic-gate } else {
6000Sstevel@tonic-gate numw = Numw + ((iswprint(Nsepc)) ? 1 : 0);
6010Sstevel@tonic-gate }
6020Sstevel@tonic-gate Linew -= (Multi == 'm') ? numw : numw * Ncols;
6030Sstevel@tonic-gate }
6040Sstevel@tonic-gate
6050Sstevel@tonic-gate if ((Colw = (Linew - Ncols + 1)/Ncols) < 1)
6060Sstevel@tonic-gate die("width too small");
6070Sstevel@tonic-gate
6080Sstevel@tonic-gate if (Ncols != 1 && Multi == 0) {
6090Sstevel@tonic-gate /* Buflen should take the number of wide characters */
6100Sstevel@tonic-gate /* Not the size for Buffer */
6110Sstevel@tonic-gate Buflen = ((UNS) (Plength / Dblspace + 1)) *
6120Sstevel@tonic-gate 2 * (Linew + 1);
6130Sstevel@tonic-gate /* Should allocate Buflen * sizeof (wchar_t) */
6140Sstevel@tonic-gate Buffer = (wchar_t *)getspace(Buflen * sizeof (wchar_t));
6150Sstevel@tonic-gate Bufptr = Bufend = &Buffer[Buflen];
6160Sstevel@tonic-gate Colpts = (COLP) getspace((UNS) ((Ncols + 1) *
6170Sstevel@tonic-gate sizeof (*Colpts)));
6180Sstevel@tonic-gate Colpts[0].c_lno = 0;
6190Sstevel@tonic-gate }
6200Sstevel@tonic-gate
6210Sstevel@tonic-gate /* is stdin not a tty? */
6220Sstevel@tonic-gate if (Ttyout && (Pause || Formfeed) && !ttyname(fileno(stdin)))
6230Sstevel@tonic-gate Ttyin = fopen("/dev/tty", "r");
6240Sstevel@tonic-gate
6250Sstevel@tonic-gate return (eargc);
6260Sstevel@tonic-gate }
6270Sstevel@tonic-gate
6280Sstevel@tonic-gate
6290Sstevel@tonic-gate static int
print(char * name)6300Sstevel@tonic-gate print(char *name)
6310Sstevel@tonic-gate {
6320Sstevel@tonic-gate static int notfirst = 0;
6330Sstevel@tonic-gate char *date = NULL;
6340Sstevel@tonic-gate char *head = NULL;
6350Sstevel@tonic-gate int c;
6360Sstevel@tonic-gate
6370Sstevel@tonic-gate if (Multi != 'm' && mustopen(name, &Files[0]) == NULL)
6380Sstevel@tonic-gate return (0);
6390Sstevel@tonic-gate if (Multi == 'm' && Nfiles == 0 && mustopen(name, &Files[0]) == NULL)
6400Sstevel@tonic-gate die("cannot open stdin");
6410Sstevel@tonic-gate if (Buffer)
6420Sstevel@tonic-gate (void) ungetwc(Files->f_nextc, Files->f_f);
6430Sstevel@tonic-gate if (Lnumb)
6440Sstevel@tonic-gate Lnumb = 1;
6450Sstevel@tonic-gate for (Page = 0; ; putpage()) {
6460Sstevel@tonic-gate if (C == WEOF && !(fold && Buffer))
6470Sstevel@tonic-gate break;
6480Sstevel@tonic-gate if (Buffer)
6490Sstevel@tonic-gate nexbuf();
6500Sstevel@tonic-gate Inpos = 0;
6510Sstevel@tonic-gate if (get(0) == WEOF)
6520Sstevel@tonic-gate break;
6530Sstevel@tonic-gate (void) fflush(stdout);
6540Sstevel@tonic-gate if (++Page >= Fpage) {
6550Sstevel@tonic-gate /* Pause if -p and not first page */
6560Sstevel@tonic-gate if (Ttyout && Pause && !notfirst++) {
6570Sstevel@tonic-gate PROMPT(); /* prompt with bell and pause */
6580Sstevel@tonic-gate while ((c = getc(Ttyin)) != EOF && c != '\n')
6590Sstevel@tonic-gate ;
6600Sstevel@tonic-gate }
6610Sstevel@tonic-gate if (Margin == 0)
6620Sstevel@tonic-gate continue;
6630Sstevel@tonic-gate if (date == NULL)
6640Sstevel@tonic-gate date = GETDATE();
6650Sstevel@tonic-gate if (head == NULL)
6660Sstevel@tonic-gate head = Head != NULL ? Head :
6670Sstevel@tonic-gate Nfiles < 2 ? Files->f_name : nulls;
6680Sstevel@tonic-gate (void) printf("\n\n");
6690Sstevel@tonic-gate Nspace = Offset;
6700Sstevel@tonic-gate putspace();
6710Sstevel@tonic-gate (void) printf(HEAD);
6720Sstevel@tonic-gate }
6730Sstevel@tonic-gate }
6740Sstevel@tonic-gate C = '\0';
6750Sstevel@tonic-gate return (1);
6760Sstevel@tonic-gate }
6770Sstevel@tonic-gate
6780Sstevel@tonic-gate
6790Sstevel@tonic-gate static void
putpage()6800Sstevel@tonic-gate putpage()
6810Sstevel@tonic-gate {
6820Sstevel@tonic-gate int colno;
6830Sstevel@tonic-gate
6840Sstevel@tonic-gate if (fold) {
6850Sstevel@tonic-gate foldpage();
6860Sstevel@tonic-gate return;
6870Sstevel@tonic-gate }
6880Sstevel@tonic-gate for (Line = Margin / 2; ; (void) get(0)) {
6890Sstevel@tonic-gate for (Nspace = Offset, colno = 0, Outpos = 0; C != '\f'; ) {
6900Sstevel@tonic-gate if (Lnumb && (C != WEOF) &&
6910Sstevel@tonic-gate (((colno == 0) && (Multi == 'm')) ||
6920Sstevel@tonic-gate (Multi != 'm'))) {
6930Sstevel@tonic-gate if (Page >= Fpage) {
6940Sstevel@tonic-gate putspace();
6950Sstevel@tonic-gate (void) printf("%*ld%wc", Numw, Buffer ?
6960Sstevel@tonic-gate Colpts[colno].c_lno++ :
6970Sstevel@tonic-gate Lnumb, Nsepc);
6980Sstevel@tonic-gate
6990Sstevel@tonic-gate /* Move Outpos for number field */
7000Sstevel@tonic-gate Outpos += Numw;
7010Sstevel@tonic-gate if (Nsepc == '\t')
7020Sstevel@tonic-gate Outpos +=
7030Sstevel@tonic-gate DEFTAB - (Outpos % DEFTAB);
7040Sstevel@tonic-gate else
7050Sstevel@tonic-gate Outpos++;
7060Sstevel@tonic-gate }
7070Sstevel@tonic-gate ++Lnumb;
7080Sstevel@tonic-gate }
7090Sstevel@tonic-gate for (Lcolpos = 0, Pcolpos = 0;
7100Sstevel@tonic-gate C != '\n' && C != '\f' && C != WEOF;
7110Sstevel@tonic-gate (void) get(colno))
7120Sstevel@tonic-gate (void) put(C);
7130Sstevel@tonic-gate
7140Sstevel@tonic-gate if ((C == WEOF) || (++colno == Ncols) ||
7150Sstevel@tonic-gate ((C == '\n') && (get(colno) == WEOF)))
7160Sstevel@tonic-gate break;
7170Sstevel@tonic-gate
7180Sstevel@tonic-gate if (Sepc)
7190Sstevel@tonic-gate (void) put(Sepc);
7200Sstevel@tonic-gate else if ((Nspace += Colw - Lcolpos + 1) < 1)
7210Sstevel@tonic-gate Nspace = 1;
7220Sstevel@tonic-gate }
7230Sstevel@tonic-gate
7240Sstevel@tonic-gate if (C == WEOF) {
7250Sstevel@tonic-gate if (Margin != 0)
7260Sstevel@tonic-gate break;
7270Sstevel@tonic-gate if (colno != 0)
7280Sstevel@tonic-gate (void) put('\n');
7290Sstevel@tonic-gate return;
7300Sstevel@tonic-gate }
7310Sstevel@tonic-gate if (C == '\f')
7320Sstevel@tonic-gate break;
7330Sstevel@tonic-gate (void) put('\n');
7340Sstevel@tonic-gate if (Dblspace == 2 && Line < Plength)
7350Sstevel@tonic-gate (void) put('\n');
7360Sstevel@tonic-gate if (Line >= Plength)
7370Sstevel@tonic-gate break;
7380Sstevel@tonic-gate }
7390Sstevel@tonic-gate if (Formfeed)
7400Sstevel@tonic-gate (void) put('\f');
7410Sstevel@tonic-gate else
7420Sstevel@tonic-gate while (Line < Length)
7430Sstevel@tonic-gate (void) put('\n');
7440Sstevel@tonic-gate }
7450Sstevel@tonic-gate
7460Sstevel@tonic-gate
7470Sstevel@tonic-gate static void
foldpage()7480Sstevel@tonic-gate foldpage()
7490Sstevel@tonic-gate {
7500Sstevel@tonic-gate int colno;
7510Sstevel@tonic-gate int keep;
7520Sstevel@tonic-gate int i;
7530Sstevel@tonic-gate int pLcolpos;
7540Sstevel@tonic-gate static int sl;
7550Sstevel@tonic-gate
7560Sstevel@tonic-gate for (Line = Margin / 2; ; (void) get(0)) {
7570Sstevel@tonic-gate for (Nspace = Offset, colno = 0, Outpos = 0; C != '\f'; ) {
7580Sstevel@tonic-gate if (Lnumb && Multi == 'm' && foldcol) {
7590Sstevel@tonic-gate if (!Fcol[colno].skip) {
7600Sstevel@tonic-gate unget(colno);
7610Sstevel@tonic-gate putspace();
7620Sstevel@tonic-gate if (!colno) {
7630Sstevel@tonic-gate for (i = 0; i <= Numw; i++)
7640Sstevel@tonic-gate (void) printf(" ");
7650Sstevel@tonic-gate (void) printf("%wc", Nsepc);
7660Sstevel@tonic-gate }
7670Sstevel@tonic-gate for (i = 0; i <= Colw; i++)
7680Sstevel@tonic-gate (void) printf(" ");
7690Sstevel@tonic-gate (void) put(Sepc);
7700Sstevel@tonic-gate if (++colno == Ncols)
7710Sstevel@tonic-gate break;
7720Sstevel@tonic-gate (void) get(colno);
7730Sstevel@tonic-gate continue;
7740Sstevel@tonic-gate } else if (!colno)
7750Sstevel@tonic-gate Lnumb = sl;
7760Sstevel@tonic-gate }
7770Sstevel@tonic-gate
7780Sstevel@tonic-gate if (Lnumb && (C != WEOF) &&
7790Sstevel@tonic-gate ((colno == 0 && Multi == 'm') || (Multi != 'm'))) {
7800Sstevel@tonic-gate if (Page >= Fpage) {
7810Sstevel@tonic-gate putspace();
7820Sstevel@tonic-gate if ((foldcol &&
7830Sstevel@tonic-gate Fcol[colno].skip && Multi != 'a') ||
7840Sstevel@tonic-gate (Fcol[0].fold && Multi == 'a') ||
7850Sstevel@tonic-gate (Buffer && Colpts[colno].c_skip)) {
7860Sstevel@tonic-gate for (i = 0; i < Numw; i++)
7870Sstevel@tonic-gate (void) printf(" ");
7880Sstevel@tonic-gate (void) printf("%wc", Nsepc);
7890Sstevel@tonic-gate if (Buffer) {
7900Sstevel@tonic-gate Colpts[colno].c_lno++;
7910Sstevel@tonic-gate Colpts[colno].c_skip =
7920Sstevel@tonic-gate 0;
7930Sstevel@tonic-gate }
7940Sstevel@tonic-gate }
7950Sstevel@tonic-gate else
7960Sstevel@tonic-gate (void) printf("%*ld%wc", Numw, Buffer ?
7970Sstevel@tonic-gate Colpts[colno].c_lno++ :
7980Sstevel@tonic-gate Lnumb, Nsepc);
7990Sstevel@tonic-gate }
8000Sstevel@tonic-gate sl = Lnumb++;
8010Sstevel@tonic-gate }
8020Sstevel@tonic-gate pLcolpos = 0;
8030Sstevel@tonic-gate for (Lcolpos = 0, Pcolpos = 0;
8040Sstevel@tonic-gate C != '\n' && C != '\f' && C != WEOF;
8050Sstevel@tonic-gate (void) get(colno)) {
8060Sstevel@tonic-gate if (put(C)) {
8070Sstevel@tonic-gate unget(colno);
8080Sstevel@tonic-gate Fcol[(Multi == 'a') ? 0 : colno].fold
8090Sstevel@tonic-gate = 1;
8100Sstevel@tonic-gate break;
8110Sstevel@tonic-gate } else if (Multi == 'a') {
8120Sstevel@tonic-gate Fcol[0].fold = 0;
8130Sstevel@tonic-gate }
8140Sstevel@tonic-gate pLcolpos = Lcolpos;
8150Sstevel@tonic-gate }
8160Sstevel@tonic-gate if (Buffer) {
8170Sstevel@tonic-gate alleof = 1;
8180Sstevel@tonic-gate for (i = 0; i < Ncols; i++)
8190Sstevel@tonic-gate if (!Fcol[i].eof)
8200Sstevel@tonic-gate alleof = 0;
8210Sstevel@tonic-gate if (alleof || ++colno == Ncols)
8220Sstevel@tonic-gate break;
8230Sstevel@tonic-gate } else if (C == EOF || ++colno == Ncols)
8240Sstevel@tonic-gate break;
8250Sstevel@tonic-gate keep = C;
8260Sstevel@tonic-gate (void) get(colno);
8270Sstevel@tonic-gate if (keep == '\n' && C == WEOF)
8280Sstevel@tonic-gate break;
8290Sstevel@tonic-gate if (Sepc)
8300Sstevel@tonic-gate (void) put(Sepc);
8310Sstevel@tonic-gate else if ((Nspace += Colw - pLcolpos + 1) < 1)
8320Sstevel@tonic-gate Nspace = 1;
8330Sstevel@tonic-gate }
8340Sstevel@tonic-gate foldcol = 0;
8350Sstevel@tonic-gate if (Lnumb && Multi != 'a') {
8360Sstevel@tonic-gate for (i = 0; i < Ncols; i++) {
8370Sstevel@tonic-gate Fcol[i].skip = Fcol[i].fold;
8380Sstevel@tonic-gate foldcol += Fcol[i].fold;
8390Sstevel@tonic-gate Fcol[i].fold = 0;
8400Sstevel@tonic-gate }
8410Sstevel@tonic-gate }
8420Sstevel@tonic-gate if (C == WEOF) {
8430Sstevel@tonic-gate if (Margin != 0)
8440Sstevel@tonic-gate break;
8450Sstevel@tonic-gate if (colno != 0)
8460Sstevel@tonic-gate (void) put('\n');
8470Sstevel@tonic-gate return;
8480Sstevel@tonic-gate }
8490Sstevel@tonic-gate if (C == '\f')
8500Sstevel@tonic-gate break;
8510Sstevel@tonic-gate (void) put('\n');
8520Sstevel@tonic-gate (void) fflush(stdout);
8530Sstevel@tonic-gate if (Dblspace == 2 && Line < Plength)
8540Sstevel@tonic-gate (void) put('\n');
8550Sstevel@tonic-gate if (Line >= Plength)
8560Sstevel@tonic-gate break;
8570Sstevel@tonic-gate }
8580Sstevel@tonic-gate if (Formfeed)
8590Sstevel@tonic-gate (void) put('\f');
8600Sstevel@tonic-gate else while (Line < Length)
8610Sstevel@tonic-gate (void) put('\n');
8620Sstevel@tonic-gate }
8630Sstevel@tonic-gate
8640Sstevel@tonic-gate
8650Sstevel@tonic-gate static void
nexbuf()8660Sstevel@tonic-gate nexbuf()
8670Sstevel@tonic-gate {
8680Sstevel@tonic-gate wchar_t *s = Buffer;
8690Sstevel@tonic-gate COLP p = Colpts;
8700Sstevel@tonic-gate int j;
8710Sstevel@tonic-gate int c;
8720Sstevel@tonic-gate int bline = 0;
8730Sstevel@tonic-gate wchar_t wc;
8740Sstevel@tonic-gate
8750Sstevel@tonic-gate if (fold) {
8760Sstevel@tonic-gate foldbuf();
8770Sstevel@tonic-gate return;
8780Sstevel@tonic-gate }
8790Sstevel@tonic-gate for (; ; ) {
8800Sstevel@tonic-gate p->c_ptr0 = p->c_ptr = s;
8810Sstevel@tonic-gate if (p == &Colpts[Ncols])
8820Sstevel@tonic-gate return;
8830Sstevel@tonic-gate (p++)->c_lno = Lnumb + bline;
8840Sstevel@tonic-gate for (j = (Length - Margin)/Dblspace; --j >= 0; ++bline) {
8850Sstevel@tonic-gate for (Inpos = 0; ; ) {
8860Sstevel@tonic-gate errno = 0;
8870Sstevel@tonic-gate wc = _fgetwc_pr(Files->f_f, &c);
8880Sstevel@tonic-gate if (wc == WEOF) {
8890Sstevel@tonic-gate /* If there is an illegal character, */
8900Sstevel@tonic-gate /* handle it as a byte sequence. */
8910Sstevel@tonic-gate if (errno == EILSEQ) {
8920Sstevel@tonic-gate if (Inpos < Colw - 1) {
8930Sstevel@tonic-gate *s = c;
8940Sstevel@tonic-gate if (++s >= Bufend)
8950Sstevel@tonic-gate die("page-buffer overflow");
8960Sstevel@tonic-gate }
8970Sstevel@tonic-gate Inpos++;
8980Sstevel@tonic-gate Error++;
8990Sstevel@tonic-gate return;
9000Sstevel@tonic-gate } else {
9010Sstevel@tonic-gate /* Real EOF */
9020Sstevel@tonic-gate for (*s = WEOF; p <= &Colpts[Ncols]; ++p)
9030Sstevel@tonic-gate p->c_ptr0 = p->c_ptr = s;
9040Sstevel@tonic-gate balance(bline);
9050Sstevel@tonic-gate return;
9060Sstevel@tonic-gate }
9070Sstevel@tonic-gate }
9080Sstevel@tonic-gate
9090Sstevel@tonic-gate if (isascii(wc)) {
9100Sstevel@tonic-gate if (isprint(wc))
9110Sstevel@tonic-gate Inpos++;
9120Sstevel@tonic-gate } else if (iswprint(wc)) {
9130Sstevel@tonic-gate Inpos += wcwidth(wc);
9140Sstevel@tonic-gate }
9150Sstevel@tonic-gate
9160Sstevel@tonic-gate if (Inpos <= Colw || wc == '\n') {
9170Sstevel@tonic-gate *s = wc;
9180Sstevel@tonic-gate if (++s >= Bufend)
9190Sstevel@tonic-gate die("page-buffer overflow");
9200Sstevel@tonic-gate }
9210Sstevel@tonic-gate if (wc == '\n')
9220Sstevel@tonic-gate break;
9230Sstevel@tonic-gate switch (wc) {
9240Sstevel@tonic-gate case '\b':
9250Sstevel@tonic-gate if (Inpos == 0)
9260Sstevel@tonic-gate --s;
9270Sstevel@tonic-gate
9280Sstevel@tonic-gate /*FALLTHROUGH*/
9290Sstevel@tonic-gate
9300Sstevel@tonic-gate case ESC:
9310Sstevel@tonic-gate if (Inpos > 0)
9320Sstevel@tonic-gate --Inpos;
9330Sstevel@tonic-gate }
9340Sstevel@tonic-gate }
9350Sstevel@tonic-gate }
9360Sstevel@tonic-gate }
9370Sstevel@tonic-gate }
9380Sstevel@tonic-gate
9390Sstevel@tonic-gate
9400Sstevel@tonic-gate static void
foldbuf()9410Sstevel@tonic-gate foldbuf()
9420Sstevel@tonic-gate {
9430Sstevel@tonic-gate int num;
9440Sstevel@tonic-gate int i;
9450Sstevel@tonic-gate int colno = 0;
9460Sstevel@tonic-gate int size = Buflen;
9470Sstevel@tonic-gate wchar_t *s;
9480Sstevel@tonic-gate wchar_t *d;
9490Sstevel@tonic-gate COLP p = Colpts;
9500Sstevel@tonic-gate
9510Sstevel@tonic-gate for (i = 0; i < Ncols; i++)
9520Sstevel@tonic-gate Fcol[i].eof = 0;
9530Sstevel@tonic-gate d = Buffer;
9540Sstevel@tonic-gate if (Bufptr != Bufend) {
9550Sstevel@tonic-gate s = Bufptr;
9560Sstevel@tonic-gate while (s < Bufend)
9570Sstevel@tonic-gate *d++ = *s++;
9580Sstevel@tonic-gate size -= (Bufend - Bufptr);
9590Sstevel@tonic-gate }
9600Sstevel@tonic-gate Bufptr = Buffer;
9610Sstevel@tonic-gate p->c_ptr0 = p->c_ptr = Buffer;
9620Sstevel@tonic-gate if (p->c_lno == 0) {
9630Sstevel@tonic-gate p->c_lno = Lnumb;
9640Sstevel@tonic-gate p->c_skip = 0;
9650Sstevel@tonic-gate } else {
9660Sstevel@tonic-gate p->c_lno = Colpts[Ncols-1].c_lno;
9670Sstevel@tonic-gate p->c_skip = Colpts[Ncols].c_skip;
9680Sstevel@tonic-gate if (p->c_skip)
9690Sstevel@tonic-gate p->c_lno--;
9700Sstevel@tonic-gate }
9710Sstevel@tonic-gate if ((num = freadw(d, size, Files->f_f)) != size) {
9720Sstevel@tonic-gate for (*(d+num) = WEOF; (++p) <= &Colpts[Ncols]; ) {
9730Sstevel@tonic-gate p->c_ptr0 = p->c_ptr = (d+num);
9740Sstevel@tonic-gate }
9750Sstevel@tonic-gate balance(0);
9760Sstevel@tonic-gate return;
9770Sstevel@tonic-gate }
9780Sstevel@tonic-gate i = (Length - Margin) / Dblspace;
9790Sstevel@tonic-gate do {
9800Sstevel@tonic-gate (void) readbuf(&Bufptr, i, p++);
9810Sstevel@tonic-gate } while (++colno < Ncols);
9820Sstevel@tonic-gate }
9830Sstevel@tonic-gate
9840Sstevel@tonic-gate
9850Sstevel@tonic-gate static void
balance(int bline)9860Sstevel@tonic-gate balance(int bline) /* line balancing for last page */
9870Sstevel@tonic-gate {
9880Sstevel@tonic-gate wchar_t *s = Buffer;
9890Sstevel@tonic-gate COLP p = Colpts;
9900Sstevel@tonic-gate int colno = 0;
9910Sstevel@tonic-gate int j;
9920Sstevel@tonic-gate int c;
9930Sstevel@tonic-gate int l;
9940Sstevel@tonic-gate int lines;
9950Sstevel@tonic-gate
9960Sstevel@tonic-gate if (!fold) {
9970Sstevel@tonic-gate c = bline % Ncols;
9980Sstevel@tonic-gate l = (bline + Ncols - 1)/Ncols;
9990Sstevel@tonic-gate bline = 0;
10000Sstevel@tonic-gate do {
10010Sstevel@tonic-gate for (j = 0; j < l; ++j)
10020Sstevel@tonic-gate while (*s++ != '\n')
10030Sstevel@tonic-gate ;
10040Sstevel@tonic-gate (++p)->c_lno = Lnumb + (bline += l);
10050Sstevel@tonic-gate p->c_ptr0 = p->c_ptr = s;
10060Sstevel@tonic-gate if (++colno == c)
10070Sstevel@tonic-gate --l;
10080Sstevel@tonic-gate } while (colno < Ncols - 1);
10090Sstevel@tonic-gate } else {
10100Sstevel@tonic-gate lines = readbuf(&s, 0, 0);
10110Sstevel@tonic-gate l = (lines + Ncols - 1)/Ncols;
10120Sstevel@tonic-gate if (l > ((Length - Margin) / Dblspace)) {
10130Sstevel@tonic-gate l = (Length - Margin) / Dblspace;
10140Sstevel@tonic-gate c = Ncols;
10150Sstevel@tonic-gate } else {
10160Sstevel@tonic-gate c = lines % Ncols;
10170Sstevel@tonic-gate }
10180Sstevel@tonic-gate s = Buffer;
10190Sstevel@tonic-gate do {
10200Sstevel@tonic-gate (void) readbuf(&s, l, p++);
10210Sstevel@tonic-gate if (++colno == c)
10220Sstevel@tonic-gate --l;
10230Sstevel@tonic-gate } while (colno < Ncols);
10240Sstevel@tonic-gate Bufptr = s;
10250Sstevel@tonic-gate }
10260Sstevel@tonic-gate }
10270Sstevel@tonic-gate
10280Sstevel@tonic-gate
10290Sstevel@tonic-gate static int
readbuf(wchar_t ** s,int lincol,COLP p)10300Sstevel@tonic-gate readbuf(wchar_t **s, int lincol, COLP p)
10310Sstevel@tonic-gate {
10320Sstevel@tonic-gate int lines = 0;
10330Sstevel@tonic-gate int chars = 0;
10340Sstevel@tonic-gate int width;
10350Sstevel@tonic-gate int nls = 0;
10360Sstevel@tonic-gate int move;
10370Sstevel@tonic-gate int skip = 0;
10380Sstevel@tonic-gate int decr = 0;
10390Sstevel@tonic-gate
10400Sstevel@tonic-gate width = (Ncols == 1) ? Linew : Colw;
10410Sstevel@tonic-gate while (**s != WEOF) {
10420Sstevel@tonic-gate switch (**s) {
10430Sstevel@tonic-gate case '\n':
10440Sstevel@tonic-gate lines++; nls++; chars = 0; skip = 0;
10450Sstevel@tonic-gate break;
10460Sstevel@tonic-gate
10470Sstevel@tonic-gate case '\b':
10480Sstevel@tonic-gate case ESC:
10490Sstevel@tonic-gate if (chars) chars--;
10500Sstevel@tonic-gate break;
10510Sstevel@tonic-gate
10520Sstevel@tonic-gate case '\t':
10530Sstevel@tonic-gate move = Itabn - ((chars + Itabn) % Itabn);
10540Sstevel@tonic-gate move = (move < width-chars) ? move :
10550Sstevel@tonic-gate width-chars;
10560Sstevel@tonic-gate chars += move;
10570Sstevel@tonic-gate
10580Sstevel@tonic-gate default:
10590Sstevel@tonic-gate if (isascii(**s)) {
10600Sstevel@tonic-gate if (isprint(**s))
10610Sstevel@tonic-gate chars++;
10620Sstevel@tonic-gate } else if (iswprint(**s)) {
10630Sstevel@tonic-gate chars += wcwidth(**s);
10640Sstevel@tonic-gate }
10650Sstevel@tonic-gate }
10660Sstevel@tonic-gate if (chars > width) {
10670Sstevel@tonic-gate lines++;
10680Sstevel@tonic-gate skip++;
10690Sstevel@tonic-gate decr++;
10700Sstevel@tonic-gate chars = 0;
10710Sstevel@tonic-gate }
10720Sstevel@tonic-gate if (lincol && lines == lincol) {
10730Sstevel@tonic-gate (p+1)->c_lno = p->c_lno + nls;
10740Sstevel@tonic-gate (++p)->c_skip = skip;
10750Sstevel@tonic-gate if (**s == '\n') (*s)++;
10760Sstevel@tonic-gate p->c_ptr0 = p->c_ptr = (wchar_t *)*s;
10770Sstevel@tonic-gate return (0);
10780Sstevel@tonic-gate }
10790Sstevel@tonic-gate if (decr)
10800Sstevel@tonic-gate decr = 0;
10810Sstevel@tonic-gate else
10820Sstevel@tonic-gate (*s)++;
10830Sstevel@tonic-gate }
10840Sstevel@tonic-gate return (lines);
10850Sstevel@tonic-gate }
10860Sstevel@tonic-gate
10870Sstevel@tonic-gate
10880Sstevel@tonic-gate static wint_t
get(int colno)10890Sstevel@tonic-gate get(int colno)
10900Sstevel@tonic-gate {
10910Sstevel@tonic-gate static int peekc = 0;
10920Sstevel@tonic-gate COLP p;
10930Sstevel@tonic-gate FILS *q;
10940Sstevel@tonic-gate int c;
10950Sstevel@tonic-gate wchar_t wc, w;
10960Sstevel@tonic-gate
10970Sstevel@tonic-gate if (peekc) {
10980Sstevel@tonic-gate peekc = 0;
10990Sstevel@tonic-gate wc = Etabc;
11000Sstevel@tonic-gate } else if (Buffer) {
11010Sstevel@tonic-gate p = &Colpts[colno];
11020Sstevel@tonic-gate if (p->c_ptr >= (p+1)->c_ptr0)
11030Sstevel@tonic-gate wc = WEOF;
11040Sstevel@tonic-gate else if ((wc = *p->c_ptr) != WEOF)
11050Sstevel@tonic-gate ++p->c_ptr;
11060Sstevel@tonic-gate if (fold && wc == WEOF)
11070Sstevel@tonic-gate Fcol[colno].eof = 1;
11080Sstevel@tonic-gate } else if ((wc =
11090Sstevel@tonic-gate (q = &Files[Multi == 'a' ? 0 : colno])->f_nextc) == WEOF) {
11100Sstevel@tonic-gate for (q = &Files[Nfiles]; --q >= Files && q->f_nextc == WEOF; )
11110Sstevel@tonic-gate ;
11120Sstevel@tonic-gate if (q >= Files)
11130Sstevel@tonic-gate wc = '\n';
11140Sstevel@tonic-gate } else {
11150Sstevel@tonic-gate errno = 0;
11160Sstevel@tonic-gate w = _fgetwc_pr(q->f_f, &c);
11170Sstevel@tonic-gate if (w == WEOF && errno == EILSEQ) {
11180Sstevel@tonic-gate q->f_nextc = (wchar_t)c;
11190Sstevel@tonic-gate } else {
11200Sstevel@tonic-gate q->f_nextc = w;
11210Sstevel@tonic-gate }
11220Sstevel@tonic-gate }
11230Sstevel@tonic-gate
11240Sstevel@tonic-gate if (Etabn != 0 && wc == Etabc) {
11250Sstevel@tonic-gate ++Inpos;
11260Sstevel@tonic-gate peekc = ETABS;
11270Sstevel@tonic-gate wc = ' ';
11280Sstevel@tonic-gate return (C = wc);
11290Sstevel@tonic-gate }
11300Sstevel@tonic-gate
11310Sstevel@tonic-gate if (wc == WEOF)
11320Sstevel@tonic-gate return (C = wc);
11330Sstevel@tonic-gate
11340Sstevel@tonic-gate if (isascii(wc)) {
11350Sstevel@tonic-gate if (isprint(wc)) {
11360Sstevel@tonic-gate Inpos++;
11370Sstevel@tonic-gate return (C = wc);
11380Sstevel@tonic-gate }
11390Sstevel@tonic-gate } else if (iswprint(wc)) {
11400Sstevel@tonic-gate Inpos += wcwidth(wc);
11410Sstevel@tonic-gate return (C = wc);
11420Sstevel@tonic-gate }
11430Sstevel@tonic-gate
11440Sstevel@tonic-gate switch (wc) {
11450Sstevel@tonic-gate case '\b':
11460Sstevel@tonic-gate case ESC:
11470Sstevel@tonic-gate if (Inpos > 0)
11480Sstevel@tonic-gate --Inpos;
11490Sstevel@tonic-gate break;
11500Sstevel@tonic-gate case '\f':
11510Sstevel@tonic-gate if (Ncols == 1)
11520Sstevel@tonic-gate break;
11530Sstevel@tonic-gate wc = '\n';
11540Sstevel@tonic-gate /* FALLTHROUGH */
11550Sstevel@tonic-gate case '\n':
11560Sstevel@tonic-gate case '\r':
11570Sstevel@tonic-gate Inpos = 0;
11580Sstevel@tonic-gate break;
11590Sstevel@tonic-gate }
11600Sstevel@tonic-gate return (C = wc);
11610Sstevel@tonic-gate }
11620Sstevel@tonic-gate
11630Sstevel@tonic-gate
11640Sstevel@tonic-gate static int
put(wchar_t wc)11650Sstevel@tonic-gate put(wchar_t wc)
11660Sstevel@tonic-gate {
11670Sstevel@tonic-gate int move = 0;
11680Sstevel@tonic-gate int width = Colw;
11690Sstevel@tonic-gate int sp = Lcolpos;
11700Sstevel@tonic-gate
11710Sstevel@tonic-gate if (fold && Ncols == 1)
11720Sstevel@tonic-gate width = Linew;
11730Sstevel@tonic-gate
11740Sstevel@tonic-gate switch (wc) {
11750Sstevel@tonic-gate case ' ':
11760Sstevel@tonic-gate /* If column not full or this is separator char */
11770Sstevel@tonic-gate if ((!fold && Ncols < 2) || (Lcolpos < width) ||
11780Sstevel@tonic-gate ((Sepc == wc) && (Lcolpos == width))) {
11790Sstevel@tonic-gate ++Nspace;
11800Sstevel@tonic-gate ++Lcolpos;
11810Sstevel@tonic-gate }
11820Sstevel@tonic-gate if (fold && sp == Lcolpos)
11830Sstevel@tonic-gate if (Lcolpos >= width)
11840Sstevel@tonic-gate return (1);
11850Sstevel@tonic-gate
11860Sstevel@tonic-gate return (0);
11870Sstevel@tonic-gate
11880Sstevel@tonic-gate case '\t':
11890Sstevel@tonic-gate if (Itabn == 0)
11900Sstevel@tonic-gate break;
11910Sstevel@tonic-gate
11920Sstevel@tonic-gate /* If column not full or this is separator char */
11930Sstevel@tonic-gate if ((Lcolpos < width) ||
11940Sstevel@tonic-gate ((Sepc == wc) && (Lcolpos == width))) {
11950Sstevel@tonic-gate move = Itabn - ((Lcolpos + Itabn) % Itabn);
11960Sstevel@tonic-gate move = (move < width-Lcolpos) ? move : width-Lcolpos;
11970Sstevel@tonic-gate Nspace += move;
11980Sstevel@tonic-gate Lcolpos += move;
11990Sstevel@tonic-gate }
12000Sstevel@tonic-gate if (fold && sp == Lcolpos)
12010Sstevel@tonic-gate if (Lcolpos >= width)
12020Sstevel@tonic-gate return (1);
12030Sstevel@tonic-gate return (0);
12040Sstevel@tonic-gate
12050Sstevel@tonic-gate case '\b':
12060Sstevel@tonic-gate if (Lcolpos == 0)
12070Sstevel@tonic-gate return (0);
12080Sstevel@tonic-gate if (Nspace > 0) {
12090Sstevel@tonic-gate --Nspace;
12100Sstevel@tonic-gate --Lcolpos;
12110Sstevel@tonic-gate return (0);
12120Sstevel@tonic-gate }
12130Sstevel@tonic-gate if (Lcolpos > Pcolpos) {
12140Sstevel@tonic-gate --Lcolpos;
12150Sstevel@tonic-gate return (0);
12160Sstevel@tonic-gate }
12170Sstevel@tonic-gate
12180Sstevel@tonic-gate /*FALLTHROUGH*/
12190Sstevel@tonic-gate
12200Sstevel@tonic-gate case ESC:
12210Sstevel@tonic-gate move = -1;
12220Sstevel@tonic-gate break;
12230Sstevel@tonic-gate
12240Sstevel@tonic-gate case '\n':
12250Sstevel@tonic-gate ++Line;
12260Sstevel@tonic-gate
12270Sstevel@tonic-gate /*FALLTHROUGH*/
12280Sstevel@tonic-gate
12290Sstevel@tonic-gate case '\r':
12300Sstevel@tonic-gate case '\f':
12310Sstevel@tonic-gate Pcolpos = 0;
12320Sstevel@tonic-gate Lcolpos = 0;
12330Sstevel@tonic-gate Nspace = 0;
12340Sstevel@tonic-gate Outpos = 0;
12350Sstevel@tonic-gate /* FALLTHROUGH */
12360Sstevel@tonic-gate default:
12370Sstevel@tonic-gate if (isascii(wc)) {
12380Sstevel@tonic-gate if (isprint(wc))
12390Sstevel@tonic-gate move = 1;
12400Sstevel@tonic-gate else
12410Sstevel@tonic-gate move = 0;
12420Sstevel@tonic-gate } else if (iswprint(wc)) {
12430Sstevel@tonic-gate move = wcwidth(wc);
12440Sstevel@tonic-gate } else {
12450Sstevel@tonic-gate move = 0;
12460Sstevel@tonic-gate }
12470Sstevel@tonic-gate break;
12480Sstevel@tonic-gate }
12490Sstevel@tonic-gate if (Page < Fpage)
12500Sstevel@tonic-gate return (0);
12510Sstevel@tonic-gate if (Lcolpos > 0 || move > 0)
12520Sstevel@tonic-gate Lcolpos += move;
12530Sstevel@tonic-gate
12540Sstevel@tonic-gate putspace();
12550Sstevel@tonic-gate
12560Sstevel@tonic-gate /* If column not full or this is separator char */
12570Sstevel@tonic-gate if ((!fold && Ncols < 2) || (Lcolpos <= width) ||
12580Sstevel@tonic-gate ((Sepc == wc) && (Lcolpos > width))) {
12590Sstevel@tonic-gate (void) fputwc(wc, stdout);
12600Sstevel@tonic-gate Outpos += move;
12610Sstevel@tonic-gate Pcolpos = Lcolpos;
12620Sstevel@tonic-gate }
12630Sstevel@tonic-gate
12640Sstevel@tonic-gate if (fold && Lcolpos > width)
12650Sstevel@tonic-gate return (1);
12660Sstevel@tonic-gate
12670Sstevel@tonic-gate return (0);
12680Sstevel@tonic-gate }
12690Sstevel@tonic-gate
12700Sstevel@tonic-gate
12710Sstevel@tonic-gate static void
putspace(void)12720Sstevel@tonic-gate putspace(void)
12730Sstevel@tonic-gate {
12740Sstevel@tonic-gate int nc = 0;
12750Sstevel@tonic-gate
12760Sstevel@tonic-gate for (; Nspace > 0; Outpos += nc, Nspace -= nc) {
12770Sstevel@tonic-gate #ifdef XPG4
12780Sstevel@tonic-gate /* XPG4: -i: replace multiple SPACE chars with tab chars */
12790Sstevel@tonic-gate if ((Nspace >= 2 && Itabn > 0 &&
12800Sstevel@tonic-gate Nspace >= (nc = Itabn - Outpos % Itabn)) && !fold) {
12810Sstevel@tonic-gate #else
12820Sstevel@tonic-gate /* Solaris: -i: replace white space with tab chars */
12830Sstevel@tonic-gate if ((Itabn > 0 && Nspace >= (nc = Itabn - Outpos % Itabn)) &&
12840Sstevel@tonic-gate !fold) {
12850Sstevel@tonic-gate #endif
12860Sstevel@tonic-gate (void) fputwc(Itabc, stdout);
12870Sstevel@tonic-gate } else {
12880Sstevel@tonic-gate nc = 1;
12890Sstevel@tonic-gate (void) putchar(' ');
12900Sstevel@tonic-gate }
12910Sstevel@tonic-gate }
12920Sstevel@tonic-gate }
12930Sstevel@tonic-gate
12940Sstevel@tonic-gate
12950Sstevel@tonic-gate static void
12960Sstevel@tonic-gate unget(int colno)
12970Sstevel@tonic-gate {
12980Sstevel@tonic-gate if (Buffer) {
12990Sstevel@tonic-gate if (*(Colpts[colno].c_ptr-1) != '\t')
13000Sstevel@tonic-gate --(Colpts[colno].c_ptr);
13010Sstevel@tonic-gate if (Colpts[colno].c_lno)
13020Sstevel@tonic-gate Colpts[colno].c_lno--;
13030Sstevel@tonic-gate } else {
13040Sstevel@tonic-gate if ((Multi == 'm' && colno == 0) || Multi != 'm')
13050Sstevel@tonic-gate if (Lnumb && !foldcol)
13060Sstevel@tonic-gate Lnumb--;
13070Sstevel@tonic-gate colno = (Multi == 'a') ? 0 : colno;
13080Sstevel@tonic-gate (void) ungetwc(Files[colno].f_nextc, Files[colno].f_f);
13090Sstevel@tonic-gate Files[colno].f_nextc = C;
13100Sstevel@tonic-gate }
13110Sstevel@tonic-gate }
13120Sstevel@tonic-gate
13130Sstevel@tonic-gate
13140Sstevel@tonic-gate /*
13150Sstevel@tonic-gate * Defer message about failure to open file to prevent messing up
13160Sstevel@tonic-gate * alignment of page with tear perforations or form markers.
13170Sstevel@tonic-gate * Treat empty file as special case and report as diagnostic.
13180Sstevel@tonic-gate */
13190Sstevel@tonic-gate
13200Sstevel@tonic-gate static FILE *
13210Sstevel@tonic-gate mustopen(char *s, FILS *f)
13220Sstevel@tonic-gate {
13230Sstevel@tonic-gate char *empty_file_msg = gettext("%s -- empty file");
13240Sstevel@tonic-gate int c;
13250Sstevel@tonic-gate
13260Sstevel@tonic-gate if (*s == '\0') {
13270Sstevel@tonic-gate f->f_name = STDINNAME();
13280Sstevel@tonic-gate f->f_f = stdin;
13290Sstevel@tonic-gate } else if ((f->f_f = fopen(f->f_name = s, "r")) == NULL) {
13300Sstevel@tonic-gate s = ffiler(f->f_name);
13310Sstevel@tonic-gate s = strcpy((char *)getspace((UNS) strlen(s) + 1), s);
13320Sstevel@tonic-gate }
13330Sstevel@tonic-gate if (f->f_f != NULL) {
13340Sstevel@tonic-gate errno = 0;
13350Sstevel@tonic-gate f->f_nextc = _fgetwc_pr(f->f_f, &c);
13360Sstevel@tonic-gate if (f->f_nextc != WEOF) {
13370Sstevel@tonic-gate return (f->f_f);
13380Sstevel@tonic-gate } else { /* WEOF */
13390Sstevel@tonic-gate if (errno == EILSEQ) {
13400Sstevel@tonic-gate f->f_nextc = (wchar_t)c;
13410Sstevel@tonic-gate return (f->f_f);
13420Sstevel@tonic-gate }
13430Sstevel@tonic-gate if (Multi == 'm')
13440Sstevel@tonic-gate return (f->f_f);
13450Sstevel@tonic-gate }
13460Sstevel@tonic-gate (void) sprintf(s = (char *)getspace((UNS) strlen(f->f_name)
13470Sstevel@tonic-gate + 1 + (UNS) strlen(empty_file_msg)),
13480Sstevel@tonic-gate empty_file_msg, f->f_name);
13490Sstevel@tonic-gate (void) fclose(f->f_f);
13500Sstevel@tonic-gate }
13510Sstevel@tonic-gate Error = 1;
13520Sstevel@tonic-gate if (Report)
13530Sstevel@tonic-gate if (Ttyout) { /* accumulate error reports */
13540Sstevel@tonic-gate Lasterr = Lasterr->e_nextp =
13550Sstevel@tonic-gate (ERR *) getspace((UNS) sizeof (ERR));
13560Sstevel@tonic-gate Lasterr->e_nextp = NULL;
13570Sstevel@tonic-gate Lasterr->e_mess = s;
13580Sstevel@tonic-gate } else { /* ok to print error report now */
13590Sstevel@tonic-gate cerror(s);
13600Sstevel@tonic-gate (void) putc('\n', stderr);
13610Sstevel@tonic-gate }
13620Sstevel@tonic-gate return ((FILE *)NULL);
13630Sstevel@tonic-gate }
13640Sstevel@tonic-gate
13650Sstevel@tonic-gate
13660Sstevel@tonic-gate static ANY *
13670Sstevel@tonic-gate getspace(UNS n)
13680Sstevel@tonic-gate {
13690Sstevel@tonic-gate ANY *t;
13700Sstevel@tonic-gate
13710Sstevel@tonic-gate if ((t = (ANY *) malloc(n)) == NULL)
13720Sstevel@tonic-gate die("out of space");
13730Sstevel@tonic-gate return (t);
13740Sstevel@tonic-gate }
13750Sstevel@tonic-gate
13760Sstevel@tonic-gate
13770Sstevel@tonic-gate static void
13780Sstevel@tonic-gate die(char *s)
13790Sstevel@tonic-gate {
13800Sstevel@tonic-gate ++Error;
13810Sstevel@tonic-gate errprint();
13820Sstevel@tonic-gate cerror(s);
13830Sstevel@tonic-gate (void) putc('\n', stderr);
13840Sstevel@tonic-gate exit(1);
13850Sstevel@tonic-gate
13860Sstevel@tonic-gate /*NOTREACHED*/
13870Sstevel@tonic-gate }
13880Sstevel@tonic-gate
13890Sstevel@tonic-gate
13900Sstevel@tonic-gate static void
13910Sstevel@tonic-gate errprint() /* print accumulated error reports */
13920Sstevel@tonic-gate {
13930Sstevel@tonic-gate (void) fflush(stdout);
13940Sstevel@tonic-gate for (; Err != NULL; Err = Err->e_nextp) {
13950Sstevel@tonic-gate cerror(Err->e_mess);
13960Sstevel@tonic-gate (void) putc('\n', stderr);
13970Sstevel@tonic-gate }
13980Sstevel@tonic-gate done();
13990Sstevel@tonic-gate }
14000Sstevel@tonic-gate
14010Sstevel@tonic-gate
14020Sstevel@tonic-gate static void
14030Sstevel@tonic-gate fixtty()
14040Sstevel@tonic-gate {
14050Sstevel@tonic-gate struct stat sbuf;
14060Sstevel@tonic-gate
14070Sstevel@tonic-gate setbuf(stdout, obuf);
14080Sstevel@tonic-gate if (signal(SIGINT, SIG_IGN) != SIG_IGN)
14090Sstevel@tonic-gate (void) signal(SIGINT, onintr);
14100Sstevel@tonic-gate if (Ttyout = ttyname(fileno(stdout))) { /* is stdout a tty? */
14110Sstevel@tonic-gate (void) stat(Ttyout, &sbuf);
14120Sstevel@tonic-gate Mode = sbuf.st_mode; /* save permissions */
14130Sstevel@tonic-gate (void) chmod(Ttyout, (S_IREAD|S_IWRITE));
14140Sstevel@tonic-gate }
14150Sstevel@tonic-gate }
14160Sstevel@tonic-gate
14170Sstevel@tonic-gate
14180Sstevel@tonic-gate static void
14190Sstevel@tonic-gate onintr()
14200Sstevel@tonic-gate {
14210Sstevel@tonic-gate ++Error;
14220Sstevel@tonic-gate errprint();
14230Sstevel@tonic-gate _exit(1);
14240Sstevel@tonic-gate }
14250Sstevel@tonic-gate
14260Sstevel@tonic-gate
14270Sstevel@tonic-gate static char *
14280Sstevel@tonic-gate GETDATE() /* return date file was last modified */
14290Sstevel@tonic-gate {
14300Sstevel@tonic-gate static char *now = NULL;
14310Sstevel@tonic-gate static struct stat sbuf;
14320Sstevel@tonic-gate static struct stat nbuf;
14330Sstevel@tonic-gate
14340Sstevel@tonic-gate if (Nfiles > 1 || Files->f_name == nulls) {
14350Sstevel@tonic-gate if (now == NULL) {
14360Sstevel@tonic-gate (void) time(&nbuf.st_mtime);
14370Sstevel@tonic-gate (void) cftime(time_buf,
14380Sstevel@tonic-gate dcgettext(NULL, FORMAT, LC_TIME),
14390Sstevel@tonic-gate &nbuf.st_mtime);
14400Sstevel@tonic-gate now = time_buf;
14410Sstevel@tonic-gate }
14420Sstevel@tonic-gate return (now);
14430Sstevel@tonic-gate } else {
14440Sstevel@tonic-gate (void) stat(Files->f_name, &sbuf);
14450Sstevel@tonic-gate (void) cftime(time_buf, dcgettext(NULL, FORMAT, LC_TIME),
14460Sstevel@tonic-gate &sbuf.st_mtime);
14470Sstevel@tonic-gate return (time_buf);
14480Sstevel@tonic-gate }
14490Sstevel@tonic-gate }
14500Sstevel@tonic-gate
14510Sstevel@tonic-gate
14520Sstevel@tonic-gate static char *
14530Sstevel@tonic-gate ffiler(char *s)
14540Sstevel@tonic-gate {
14550Sstevel@tonic-gate static char buf[100];
14560Sstevel@tonic-gate
14570Sstevel@tonic-gate (void) sprintf(buf, gettext("can't open %s"), s);
14580Sstevel@tonic-gate return (buf);
14590Sstevel@tonic-gate }
14600Sstevel@tonic-gate
14610Sstevel@tonic-gate
14620Sstevel@tonic-gate static void
14630Sstevel@tonic-gate usage(int rc)
14640Sstevel@tonic-gate {
14650Sstevel@tonic-gate (void) fprintf(stderr, gettext(
14660Sstevel@tonic-gate "usage: pr [-# [-w #] [-a]] [-e[c][#]] [-i[c][#]] [-drtfp] [-n[c][#]] \\\n"
14670Sstevel@tonic-gate " [-o #] [-l #] [-s[char]] [-h header] [-F] [+#] [file ...]\n\n"
14680Sstevel@tonic-gate " pr [-m [-w #]] [-e[c][#]] [-i[c][#]] [-drtfp] [-n[c][#]] [-0 #] \\\n"
14690Sstevel@tonic-gate " [-l #] [-s[char]] [-h header] [-F] [+#] file1 file2 ...\n"
14700Sstevel@tonic-gate ));
14710Sstevel@tonic-gate exit(rc);
14720Sstevel@tonic-gate }
14730Sstevel@tonic-gate
14740Sstevel@tonic-gate static wint_t
14750Sstevel@tonic-gate _fgetwc_pr(FILE *f, int *ic)
14760Sstevel@tonic-gate {
14770Sstevel@tonic-gate int i;
14780Sstevel@tonic-gate int len;
14790Sstevel@tonic-gate char mbuf[MB_LEN_MAX];
14800Sstevel@tonic-gate int c;
14810Sstevel@tonic-gate wchar_t wc;
14820Sstevel@tonic-gate
14830Sstevel@tonic-gate c = getc(f);
14840Sstevel@tonic-gate
14850Sstevel@tonic-gate if (c == EOF)
14860Sstevel@tonic-gate return (WEOF);
14870Sstevel@tonic-gate if (mbcurmax == 1 || isascii(c)) {
14880Sstevel@tonic-gate return ((wint_t)c);
14890Sstevel@tonic-gate }
14900Sstevel@tonic-gate mbuf[0] = (char)c;
14910Sstevel@tonic-gate for (i = 1; i < mbcurmax; i++) {
14920Sstevel@tonic-gate c = getc(f);
14930Sstevel@tonic-gate if (c == EOF) {
14940Sstevel@tonic-gate break;
14950Sstevel@tonic-gate } else {
14960Sstevel@tonic-gate mbuf[i] = (char)c;
14970Sstevel@tonic-gate }
14980Sstevel@tonic-gate }
14990Sstevel@tonic-gate mbuf[i] = 0;
15000Sstevel@tonic-gate
15010Sstevel@tonic-gate len = mbtowc(&wc, mbuf, i);
15020Sstevel@tonic-gate if (len == -1) {
15030Sstevel@tonic-gate /* Illegal character */
15040Sstevel@tonic-gate /* Set the first byte to *ic */
15050Sstevel@tonic-gate *ic = mbuf[0];
15060Sstevel@tonic-gate /* Push back remaining characters */
15070Sstevel@tonic-gate for (i--; i > 0; i--) {
15080Sstevel@tonic-gate (void) ungetc(mbuf[i], f);
15090Sstevel@tonic-gate }
15100Sstevel@tonic-gate errno = EILSEQ;
15110Sstevel@tonic-gate return (WEOF);
15120Sstevel@tonic-gate } else {
15130Sstevel@tonic-gate /* Push back over-read characters */
15140Sstevel@tonic-gate for (i--; i >= len; i--) {
15150Sstevel@tonic-gate (void) ungetc(mbuf[i], f);
15160Sstevel@tonic-gate }
15170Sstevel@tonic-gate return ((wint_t)wc);
15180Sstevel@tonic-gate }
15190Sstevel@tonic-gate }
15200Sstevel@tonic-gate
15210Sstevel@tonic-gate static size_t
15220Sstevel@tonic-gate freadw(wchar_t *ptr, size_t nitems, FILE *f)
15230Sstevel@tonic-gate {
15240Sstevel@tonic-gate size_t i;
15250Sstevel@tonic-gate size_t ret;
15260Sstevel@tonic-gate int c;
15270Sstevel@tonic-gate wchar_t *p;
15280Sstevel@tonic-gate wint_t wc;
15290Sstevel@tonic-gate
15300Sstevel@tonic-gate if (feof(f)) {
15310Sstevel@tonic-gate return (0);
15320Sstevel@tonic-gate }
15330Sstevel@tonic-gate
15340Sstevel@tonic-gate p = ptr;
15350Sstevel@tonic-gate ret = 0;
15360Sstevel@tonic-gate for (i = 0; i < nitems; i++) {
15370Sstevel@tonic-gate errno = 0;
15380Sstevel@tonic-gate wc = _fgetwc_pr(f, &c);
15390Sstevel@tonic-gate if (wc == WEOF) {
15400Sstevel@tonic-gate if (errno == EILSEQ) {
15410Sstevel@tonic-gate *p++ = (wchar_t)c;
15420Sstevel@tonic-gate ret++;
15430Sstevel@tonic-gate } else {
15440Sstevel@tonic-gate return (ret);
15450Sstevel@tonic-gate }
15460Sstevel@tonic-gate } else {
15470Sstevel@tonic-gate *p++ = (wchar_t)wc;
15480Sstevel@tonic-gate ret++;
15490Sstevel@tonic-gate }
15500Sstevel@tonic-gate }
15510Sstevel@tonic-gate return (ret);
15520Sstevel@tonic-gate }
1553