1 /* $NetBSD: pr.c,v 1.24 2012/08/01 02:27:48 ginsbach Exp $ */
4 * Copyright (c) 1991 Keith Muller.
6 * The Regents of the University of California. All rights reserved.
8 * The NetBSD Foundation, Inc.
10 * This code is derived from software contributed to Berkeley by
11 * Keith Muller of the University of California, San Diego.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #include <sys/cdefs.h>
40 __COPYRIGHT("@(#) Copyright (c) 1993\
41 The Regents of the University of California. All rights reserved.");
46 from
: static char sccsid
[] = "@(#)pr.c 8.1 (Berkeley) 6/6/93";
48 __RCSID("$NetBSD: pr.c,v 1.24 2012/08/01 02:27:48 ginsbach Exp $");
52 #include <sys/types.h>
70 * pr: a printing and pagination filter. If multiple input files
71 * are specified, each is read, formatted, and written to standard
72 * output. By default, input is separated into 66-line pages, each
73 * with a header that includes the page number, date, time and the
76 * Complies with posix P1003.2/D11
82 static int pgnm
; /* starting page number */
83 static int clcnt
; /* number of columns */
84 static int colwd
; /* column data width - multiple columns */
85 static int across
; /* mult col flag; write across page */
86 static int dspace
; /* double space flag */
87 static char inchar
; /* expand input char */
88 static int ingap
; /* expand input gap */
89 static int formfeed
; /* use formfeed as trailer */
90 static char *header
; /* header name instead of file name */
91 static char ochar
; /* contract output char */
92 static int ogap
; /* contract output gap */
93 static int lines
; /* number of lines per page */
94 static int merge
; /* merge multiple files in output */
95 static char nmchar
; /* line numbering append char */
96 static int nmwd
; /* width of line number field */
97 static int offst
; /* number of page offset spaces */
98 static int nodiag
; /* do not report file open errors */
99 static char schar
; /* text column separation character */
100 static int sflag
; /* -s option for multiple columns */
101 static int ttyout
; /* output is a tty */
102 static int nohead
; /* do not write head and trailer */
103 static int pgpause
; /* pause before each page */
104 static int pgwd
; /* page width with multiple col output */
105 static const char *timefrmt
= TIMEFMT
; /* time conversion string */
106 static FILE *ttyinf
; /* input terminal for page pauses */
111 static FILE *errf
; /* error message file pointer */
112 static int addone
; /* page length is odd with double space */
113 static int errcnt
; /* error count on file processing */
114 static const char digs
[] = "0123456789"; /* page number translation map */
116 static void addnum(char *, int, int);
117 static void flsh_errs(void);
118 static int horzcol(int, char **);
119 static int inln(FILE *, char *, int, int *, int, int *);
120 static int inskip(FILE *, int, int);
121 static void mfail(void);
122 static int mulfile(int, char **);
123 static FILE *nxtfile(int, char **, const char **, char *, int);
124 static int onecol(int, char **);
125 static int otln(char *, int, int *, int *, int);
126 static void pfail(void);
127 static int prhead(char *, const char *, int);
128 static void prpause(int);
129 static int prtail(int, int);
130 static int setup(int, char **);
131 __dead
static void terminate(int);
132 static void usage(void);
133 static int vertcol(int, char **);
136 main(int argc
, char *argv
[])
140 if (signal(SIGINT
, SIG_IGN
) != SIG_IGN
)
141 (void)signal(SIGINT
, terminate
);
142 ret_val
= setup(argc
, argv
);
145 * select the output format based on options
148 ret_val
= mulfile(argc
, argv
);
150 ret_val
= onecol(argc
, argv
);
152 ret_val
= horzcol(argc
, argv
);
154 ret_val
= vertcol(argc
, argv
);
158 if (errcnt
|| ret_val
)
164 * onecol: print files with only one column of output.
165 * Line length is unlimited.
168 onecol(int argc
, char *argv
[])
191 /* LSC: -Werror=maybe-uninitialized, when compiling with -O3 */
193 #endif /* defined(__minix) */
201 * allocate line buffer
203 if ((obuf
= malloc((unsigned)(LBUF
+ off
)*sizeof(char))) == NULL
)
206 * allocate header buffer
208 if ((hbuf
= malloc((unsigned)(HDBUF
+ offst
)*sizeof(char))) == NULL
)
211 ohbuf
= hbuf
+ offst
;
215 nbuf
[--num
] = nmchar
;
217 (void)memset(obuf
, (int)' ', offst
);
218 (void)memset(hbuf
, (int)' ', offst
);
224 while ((inf
= nxtfile(argc
, argv
, &fname
, ohbuf
, 0)) != NULL
) {
227 * skip to specified page
229 if (inskip(inf
, pgnm
, lines
))
249 while (linecnt
< lines
) {
253 if ((cnt
= inln(inf
,lbuf
,LBUF
,&cps
,0,&mor
)) < 0)
260 prhead(hbuf
, fname
, pagecnt
))
269 addnum(nbuf
, num
, ++lncnt
);
270 if (otln(obuf
,cnt
+off
, &ips
, &ops
, mor
))
272 } else if (otln(lbuf
, cnt
, &ips
, &ops
, mor
))
276 * if line bigger than buffer, get more
284 * whole line rcvd. reset tab proc. state
293 * fill to end of page
295 if (linecnt
&& prtail(lines
-linecnt
-lrgln
, lrgln
))
299 * On EOF go to next file
317 if (inf
!= NULL
&& inf
!= stdin
)
323 * vertcol: print files with more than one column of output down a page
326 vertcol(int argc
, char *argv
[])
329 char **lstdat
= NULL
;
341 int mxlen
= pgwd
+ offst
+ 1;
342 int mclcnt
= clcnt
- 1;
343 struct vcol
*vc
= NULL
;
360 * allocate page buffer
362 if ((buf
= malloc((unsigned)lines
*mxlen
*sizeof(char))) == NULL
)
366 * allocate page header
368 if ((hbuf
= malloc((unsigned)(HDBUF
+ offst
)*sizeof(char))) == NULL
)
370 ohbuf
= hbuf
+ offst
;
372 (void)memset(hbuf
, (int)' ', offst
);
375 * col pointers when no headers
378 if ((vc
= malloc((unsigned)mvc
*sizeof(struct vcol
))) == NULL
)
382 * pointer into page where last data per line is located
384 if ((lstdat
= malloc((unsigned)lines
*sizeof(char *))) == NULL
)
388 * fast index lookups to locate start of lines
390 if ((indy
= malloc((unsigned)lines
*sizeof(int))) == NULL
)
392 if ((lindy
= malloc((unsigned)lines
*sizeof(int))) == NULL
)
401 * initialize buffer lookup indexes and offset area
403 for (j
= 0; j
< lines
; ++j
) {
404 lindy
[j
] = j
* mxlen
;
405 indy
[j
] = lindy
[j
] + offst
;
407 ptbf
= buf
+ lindy
[j
];
408 (void)memset(ptbf
, (int)' ', offst
);
411 ptbf
= buf
+ indy
[j
];
418 while ((inf
= nxtfile(argc
, argv
, &fname
, ohbuf
, 0)) != NULL
) {
421 * skip to requested page
423 if (inskip(inf
, pgnm
, lines
))
438 for (i
= 0; i
< clcnt
; ++i
) {
441 * if last column, do not pad
452 * is this first column
455 ptbf
= buf
+ indy
[j
];
465 addnum(ptbf
, nmwd
, ++lncnt
);
473 cnt
= inln(inf
,ptbf
,colwd
,&cps
,1,&mor
);
480 * pad all but last column on page
484 * pad to end of column
488 else if ((pln
= col
-cnt
) > 0) {
495 * remember last char in line
506 * when -t (no header) is specified the spec requires
507 * the min number of lines. The last page may not have
508 * balanced length columns. To fix this we must reorder
509 * the columns. This is a very slow technique so it is
510 * only used under limited conditions. Without -t, the
511 * balancing of text columns is unspecified. To NOT
512 * balance the last page, add the global variable
513 * nohead to the if statement below e.g.
515 * if ((cnt < 0) && nohead && cvc ......
520 * check to see if last page needs to be reordered
522 if ((cnt
< 0) && cvc
&& ((mvc
-cvc
) >= clcnt
)){
533 if (!nohead
&& prhead(hbuf
, fname
, pagecnt
))
535 for (i
= 0; i
< pln
; ++i
) {
538 if (offst
&& otln(buf
,offst
,&ips
,&ops
,1)) {
544 for (j
= 0; j
< clcnt
; ++j
) {
546 * determine column length
559 cnt
= vc
[tvc
].cnt
+ 1;
564 if (otln(vc
[tvc
].pt
, cnt
, &ips
,
574 if (otln(buf
, 0, &ips
, &ops
, 0))
580 if (prtail((lines
- pln
), 0))
583 * done with output, go to next file
589 * determine how many lines to output
603 if (!nohead
&& prhead(hbuf
, fname
, pagecnt
))
610 for (i
= 0; i
< pln
; ++i
) {
611 ptbf
= buf
+ lindy
[i
];
612 if ((j
= lstdat
[i
] - ptbf
) <= offst
)
614 if (otln(ptbf
, j
, &ips
, &ops
, 0))
621 if (pln
&& prtail((lines
- pln
), 0))
625 * if EOF go to next file
646 if (inf
!= NULL
&& inf
!= stdin
)
652 * horzcol: print files with more than one column of output across a page
655 horzcol(int argc
, char *argv
[])
677 if ((buf
= malloc((unsigned)(pgwd
+offst
+1)*sizeof(char))) == NULL
)
683 if ((hbuf
= malloc((unsigned)(HDBUF
+ offst
)*sizeof(char))) == NULL
)
685 ohbuf
= hbuf
+ offst
;
687 (void)memset(buf
, (int)' ', offst
);
688 (void)memset(hbuf
, (int)' ', offst
);
694 while ((inf
= nxtfile(argc
, argv
, &fname
, ohbuf
, 0)) != NULL
) {
696 if (inskip(inf
, pgnm
, lines
))
710 for (i
= 0; i
< lines
; ++i
) {
720 * add number to column
722 addnum(ptbf
, nmwd
, ++lncnt
);
729 if ((cnt
= inln(inf
,ptbf
,colwd
,&cps
,1,
736 * if last line skip padding
742 * pad to end of column
746 else if ((pln
= col
- cnt
) > 0) {
747 (void)memset(ptbf
,(int)' ',pln
);
753 * determine line length
755 if ((j
= lstdat
- buf
) <= offst
)
762 prhead(hbuf
, fname
, pagecnt
))
768 if (otln(buf
, j
, &ips
, &ops
, 0))
775 if (i
&& prtail(lines
-i
, 0))
779 * if EOF go to next file
797 if (inf
!= NULL
&& inf
!= stdin
)
803 * mulfile: print files with more than one column of output and
804 * more than one file concurrently
807 mulfile(int argc
, char *argv
[])
832 * array of FILE *, one for each operand
834 if ((fbuf
= calloc(clcnt
, sizeof(FILE *))) == NULL
)
840 if ((hbuf
= malloc((unsigned)(HDBUF
+ offst
)*sizeof(char))) == NULL
)
842 ohbuf
= hbuf
+ offst
;
845 * do not know how many columns yet. The number of operands provide an
846 * upper bound on the number of columns. We use the number of files
847 * we can open successfully to set the number of columns. The operation
848 * of the merge operation (-m) in relation to unsuccesful file opens
849 * is unspecified by posix.
853 if ((fbuf
[j
] = nxtfile(argc
, argv
, &fname
, ohbuf
, 1)) == NULL
)
855 if (pgnm
&& (inskip(fbuf
[j
], pgnm
, lines
)))
867 * calculate page boundries based on open file count
871 colwd
= (pgwd
- clcnt
- nmwd
)/clcnt
;
872 pgwd
= ((colwd
+ 1) * clcnt
) - nmwd
- 2;
874 colwd
= (pgwd
+ 1 - clcnt
)/clcnt
;
875 pgwd
= ((colwd
+ 1) * clcnt
) - 1;
879 "pr: page width too small for %d columns\n", clcnt
);
888 if ((buf
= malloc((unsigned)(pgwd
+offst
+1)*sizeof(char))) == NULL
)
891 (void)memset(buf
, (int)' ', offst
);
892 (void)memset(hbuf
, (int)' ', offst
);
901 * continue to loop while any file still has data
907 for (i
= 0; i
< lines
; ++i
) {
912 * add line number to line
914 addnum(ptbf
, nmwd
, ++lncnt
);
924 for (j
= 0; j
< clcnt
; ++j
) {
925 if (fbuf
[j
] == NULL
) {
930 } else if ((cnt
= inln(fbuf
[j
], ptbf
, colwd
,
931 &cps
, 1, &mor
)) < 0) {
935 if (fbuf
[j
] != stdin
)
936 (void)fclose(fbuf
[j
]);
950 * if last ACTIVE column, done with line
956 * pad to end of column
960 } else if ((pln
= col
- cnt
) > 0) {
961 (void)memset(ptbf
, (int)' ', pln
);
967 * calculate data in line
969 if ((j
= lstdat
- buf
) <= offst
)
976 if (!nohead
&& prhead(hbuf
, fname
, pagecnt
))
983 if (otln(buf
, j
, &ips
, &ops
, 0))
987 * if no more active files, done
998 if (i
&& prtail(lines
-i
, 0))
1010 for (j
= 0; j
< clcnt
; j
++)
1011 if (fbuf
[j
] && fbuf
[j
] != stdin
)
1012 (void)fclose(fbuf
[j
]);
1021 * inln(): input a line of data (unlimited length lines supported)
1022 * Input is optionally expanded to spaces
1026 * lim: buffer length
1027 * cps: column positon 1st char in buffer (large line support)
1028 * trnc: throw away data more than lim up to \n
1029 * mor: set if more data in line (not truncated)
1032 inln(FILE *inf
, char *buf
, int lim
, int *cps
, int trnc
, int *mor
)
1038 int chk
= (int)inchar
;
1044 * expanding input option
1046 while ((--lim
>= 0) && ((ch
= getc(inf
)) != EOF
)) {
1048 * is this the input "tab" char
1052 * expand to number of spaces
1054 col
= (ptbuf
- buf
) + *cps
;
1055 col
= gap
- (col
% gap
);
1058 * if more than this line, push back
1060 if ((col
> lim
) && (ungetc(ch
, inf
) == EOF
))
1066 while ((--col
>= 0) && (--lim
>= 0))
1078 while ((--lim
>= 0) && ((ch
= getc(inf
)) != EOF
)) {
1094 * entire line processed
1102 * line was larger than limit
1106 * throw away rest of line
1108 while ((ch
= getc(inf
)) != EOF
) {
1116 * save column offset if not truncated
1126 * otln(): output a line of data. (Supports unlimited length lines)
1127 * output is optionally contracted to tabs
1129 * buf: output buffer with data
1130 * cnt: number of chars of valid data in buf
1131 * svips: buffer input column position (for large lines)
1132 * svops: buffer output column position (for large lines)
1133 * mor: output line not complete in this buf; more data to come.
1134 * 1 is more, 0 is complete, -1 is no \n's
1137 otln(char *buf
, int cnt
, int *svips
, int *svops
, int mor
)
1139 int ops
; /* last col output */
1140 int ips
; /* last col in buf examined */
1147 * contracting on output
1152 while (buf
< endbuf
) {
1154 * count number of spaces and ochar in buffer
1163 * simulate ochar processing
1165 if (*buf
== ochar
) {
1166 ips
+= gap
- (ips
% gap
);
1172 * got a non space char; contract out spaces
1174 while (ips
- ops
> 1) {
1176 * use as many ochar as will fit
1178 if ((tbps
= ops
+ gap
- (ops
% gap
)) > ips
)
1180 if (putchar(ochar
) == EOF
) {
1189 * finish off with spaces
1191 if (putchar(' ') == EOF
) {
1199 * output non space char
1201 if (putchar(*buf
++) == EOF
) {
1211 * if incomplete line, save position counts
1219 while (ips
- ops
> 1) {
1221 * use as many ochar as will fit
1223 if ((tbps
= ops
+ gap
- (ops
% gap
)) > ips
)
1225 if (putchar(ochar
) == EOF
) {
1233 * finish off with spaces
1235 if (putchar(' ') == EOF
) {
1245 * output is not contracted
1247 if (cnt
&& (fwrite(buf
, sizeof(char), cnt
, stdout
) <= 0)) {
1256 * process line end and double space as required
1258 if ((putchar('\n') == EOF
) || (dspace
&& (putchar('\n') == EOF
))) {
1266 * inskip(): skip over pgcnt pages with lncnt lines per page
1267 * file is closed at EOF (if not stdin).
1269 * inf FILE * to read from
1270 * pgcnt number of pages to skip
1271 * lncnt number of lines per page
1274 inskip(FILE *inf
, int pgcnt
, int lncnt
)
1279 while(--pgcnt
> 0) {
1281 while ((c
= getc(inf
)) != EOF
) {
1282 if ((c
== '\n') && (--cnt
== 0))
1295 * nxtfile: returns a FILE * to next file in arg list and sets the
1296 * time field for this file (or current date).
1298 * buf array to store proper date for the header.
1299 * dt if set skips the date processing (used with -m)
1302 nxtfile(int argc
, char **argv
, const char **fname
, char *buf
, int dt
)
1307 struct tm
*timeptr
= NULL
;
1308 struct stat statbuf
;
1310 static int twice
= -1;
1313 if (eoptind
>= argc
) {
1315 * no file listed; default, use standard input
1327 if (gettimeofday(&tv
, &tz
) < 0) {
1329 (void)fprintf(errf
, "pr: cannot get time of day, %s\n",
1334 curtime
= tv
.tv_sec
;
1335 timeptr
= localtime(&curtime
);
1337 for (; eoptind
< argc
; ++eoptind
) {
1338 if (strcmp(argv
[eoptind
], "-") == 0) {
1340 * process a "-" for filename
1349 if (nohead
|| (dt
&& twice
))
1351 if (gettimeofday(&tv
, &tz
) < 0) {
1354 "pr: cannot get time of day, %s\n",
1358 curtime
= tv
.tv_sec
;
1359 timeptr
= localtime(&curtime
);
1362 * normal file processing
1364 if ((inf
= fopen(argv
[eoptind
], "r")) == NULL
) {
1368 (void)fprintf(errf
, "pr: Cannot open %s, %s\n",
1369 argv
[eoptind
], strerror(errno
));
1377 *fname
= argv
[eoptind
];
1379 if (nohead
|| (dt
&& twice
))
1383 if (gettimeofday(&tv
, &tz
) < 0) {
1386 "pr: cannot get time of day, %s\n",
1390 curtime
= tv
.tv_sec
;
1391 timeptr
= localtime(&curtime
);
1393 if (fstat(fileno(inf
), &statbuf
) < 0) {
1397 "pr: Cannot stat %s, %s\n",
1398 argv
[eoptind
], strerror(errno
));
1401 timeptr
= localtime(&(statbuf
.st_mtime
));
1410 * set up time field used in header
1412 if (strftime(buf
, HDBUF
, timefrmt
, timeptr
) <= 0) {
1416 (void)fputs("pr: time conversion failed\n", errf
);
1423 * addnum(): adds the line number to the column
1424 * Truncates from the front or pads with spaces as required.
1425 * Numbers are right justified.
1427 * buf buffer to store the number
1428 * wdth width of buffer to fill
1431 * NOTE: numbers occupy part of the column. The posix
1432 * spec does not specify if -i processing should or should not
1433 * occur on number padding. The spec does say it occupies
1434 * part of the column. The usage of addnum currently treats
1435 * numbers as part of the column so spaces may be replaced.
1438 addnum(char *buf
, int wdth
, int line
)
1440 char *pt
= buf
+ wdth
;
1443 *--pt
= digs
[line
% 10];
1445 } while (line
&& (pt
> buf
));
1448 * pad with space as required
1455 * prpause(): pause before printing each page
1457 * pagcnt page number
1466 (void)putc('\a', stderr
);
1467 (void)fflush(stderr
);
1469 while ((c
= getc(ttyinf
)) != '\n' && c
!= EOF
)
1473 * pause ONLY before first page of first file
1475 if (pgpause
== FIRSTPAGE
&& pagcnt
== 1)
1481 * prhead(): prints the top of page header
1483 * buf buffer with time field (and offset)
1484 * cnt number of chars in buf
1485 * fname fname field for header
1486 * pagcnt page number
1489 prhead(char *buf
, const char *fname
, int pagcnt
)
1494 if ((putchar('\n') == EOF
) || (putchar('\n') == EOF
)) {
1499 * posix is not clear if the header is subject to line length
1500 * restrictions. The specification for header line format
1501 * in the spec clearly does not limit length. No pr currently
1502 * restricts header length. However if we need to truncate in
1503 * an reasonable way, adjust the length of the printf by
1504 * changing HDFMT to allow a length max as an argument printf.
1505 * buf (which contains the offset spaces and time field could
1508 * note only the offset (if any) is processed for tab expansion
1510 if (offst
&& otln(buf
, offst
, &ips
, &ops
, -1))
1512 (void)printf(HDFMT
,buf
+offst
, fname
, pagcnt
);
1517 * prtail(): pad page with empty lines (if required) and print page trailer
1520 * cnt number of lines of padding needed
1521 * incomp was a '\n' missing from last line output
1524 prtail(int cnt
, int incomp
)
1528 * only pad with no headers when incomplete last line
1532 if ((dspace
&& (putchar('\n') == EOF
)) ||
1533 (putchar('\n') == EOF
)) {
1541 * if double space output two \n
1547 * if an odd number of lines per page, add an extra \n
1556 if ((incomp
&& (putchar('\n') == EOF
)) ||
1557 (putchar('\f') == EOF
)) {
1564 while (--cnt
>= 0) {
1565 if (putchar('\n') == EOF
) {
1574 * terminate(): when a SIGINT is recvd
1577 terminate(int which_sig
)
1580 (void)raise_default_signal(which_sig
);
1586 * flsh_errs(): output saved up diagnostic messages after all normal
1587 * processing has completed
1594 (void)fflush(stdout
);
1599 while (fgets(buf
, BUFSIZ
, errf
) != NULL
)
1600 (void)fputs(buf
, stderr
);
1606 (void)fputs("pr: memory allocation failed\n", errf
);
1612 (void)fprintf(errf
, "pr: write failure, %s\n", strerror(errno
));
1619 "usage: pr [+page] [-col] [-adFfmprt] [-e[ch][gap]] [-h header]\n",
1622 " [-i[ch][gap]] [-l line] [-n[ch][width]] [-o offset]\n",
1625 " [-s[ch]] [-T timefmt] [-w width] [-] [file ...]\n",
1630 * setup: Validate command args, initialize and perform sanity
1634 setup(int argc
, char **argv
)
1644 if (isatty(fileno(stdout
))) {
1646 * defer diagnostics until processing is done
1648 if ((errf
= tmpfile()) == NULL
) {
1649 (void)fputs("Cannot defer diagnostic messages\n",stderr
);
1655 while ((c
= egetopt(argc
, argv
, "#adFfmrte?h:i?l:n?o:ps?T:w:")) != -1) {
1658 if ((pgnm
= atoi(eoptarg
)) < 1) {
1659 (void)fputs("pr: +page number must be 1 or more\n",
1665 if ((clcnt
= atoi(eoptarg
)) < 1) {
1666 (void)fputs("pr: -columns must be 1 or more\n",errf
);
1680 if ((eoptarg
!= NULL
) &&
1681 !isdigit((unsigned char)*eoptarg
))
1682 inchar
= *eoptarg
++;
1685 if ((eoptarg
!= NULL
) &&
1686 isdigit((unsigned char)*eoptarg
)) {
1687 if ((ingap
= atoi(eoptarg
)) < 0) {
1689 "pr: -e gap must be 0 or more\n", errf
);
1694 } else if ((eoptarg
!= NULL
) && (*eoptarg
!= '\0')) {
1696 "pr: invalid value for -e %s\n", eoptarg
);
1702 pgpause
|= FIRSTPAGE
;
1712 if ((eoptarg
!= NULL
) &&
1713 !isdigit((unsigned char)*eoptarg
))
1717 if ((eoptarg
!= NULL
) &&
1718 isdigit((unsigned char)*eoptarg
)) {
1719 if ((ogap
= atoi(eoptarg
)) < 0) {
1721 "pr: -i gap must be 0 or more\n", errf
);
1726 } else if ((eoptarg
!= NULL
) && (*eoptarg
!= '\0')) {
1728 "pr: invalid value for -i %s\n", eoptarg
);
1734 if (!isdigit((unsigned char)*eoptarg
) ||
1735 ((lines
=atoi(eoptarg
)) < 1)) {
1737 "pr: Number of lines must be 1 or more\n",errf
);
1745 if ((eoptarg
!= NULL
) &&
1746 !isdigit((unsigned char)*eoptarg
))
1747 nmchar
= *eoptarg
++;
1750 if ((eoptarg
!= NULL
) &&
1751 isdigit((unsigned char)*eoptarg
)) {
1752 if ((nmwd
= atoi(eoptarg
)) < 1) {
1754 "pr: -n width must be 1 or more\n",errf
);
1757 } else if ((eoptarg
!= NULL
) && (*eoptarg
!= '\0')) {
1759 "pr: invalid value for -n %s\n", eoptarg
);
1765 if (!isdigit((unsigned char)*eoptarg
) ||
1766 ((offst
= atoi(eoptarg
))< 1)){
1767 (void)fputs("pr: -o offset must be 1 or more\n",
1773 pgpause
|= EACHPAGE
;
1780 if (eoptarg
== NULL
)
1784 if (eoptarg
&& *eoptarg
!= '\0') {
1786 "pr: invalid value for -s %s\n", eoptarg
);
1798 if (!isdigit((unsigned char)*eoptarg
) ||
1799 ((pgwd
= atoi(eoptarg
)) < 1)){
1801 "pr: -w width must be 1 or more \n",errf
);
1812 * default and sanity checks
1816 if ((clcnt
= argc
- eoptind
) <= 1) {
1825 (void)fputs("pr: -a flag requires multiple columns\n",
1830 (void)fputs("pr: -m cannot be used with -a\n", errf
);
1840 if (cflag
|| merge
) {
1853 "pr: -m cannot be used with multiple columns\n", errf
);
1857 colwd
= (pgwd
+ 1 - (clcnt
* (nmwd
+ 2)))/clcnt
;
1858 pgwd
= ((colwd
+ nmwd
+ 2) * clcnt
) - 1;
1860 colwd
= (pgwd
+ 1 - clcnt
)/clcnt
;
1861 pgwd
= ((colwd
+ 1) * clcnt
) - 1;
1865 "pr: page width is too small for %d columns\n",clcnt
);
1873 * make sure long enough for headers. if not disable
1875 if (lines
<= HEADLEN
+ TAILLEN
)
1878 lines
-= HEADLEN
+ TAILLEN
;
1881 * adjust for double space on odd length pages
1894 * open /dev/tty if we are to pause before each page
1895 * but only if stdout is a terminal and stdin is not a terminal
1897 if (ttyout
&& pgpause
&& !isatty(fileno(stdin
))) {
1898 if ((ttyinf
= fopen("/dev/tty", "r")) == NULL
) {
1899 (void)fprintf(errf
, "pr: cannot open terminal\n");