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