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
[])
197 * allocate line buffer
199 if ((obuf
= malloc((unsigned)(LBUF
+ off
)*sizeof(char))) == NULL
)
202 * allocate header buffer
204 if ((hbuf
= malloc((unsigned)(HDBUF
+ offst
)*sizeof(char))) == NULL
)
207 ohbuf
= hbuf
+ offst
;
211 nbuf
[--num
] = nmchar
;
213 (void)memset(obuf
, (int)' ', offst
);
214 (void)memset(hbuf
, (int)' ', offst
);
220 while ((inf
= nxtfile(argc
, argv
, &fname
, ohbuf
, 0)) != NULL
) {
223 * skip to specified page
225 if (inskip(inf
, pgnm
, lines
))
245 while (linecnt
< lines
) {
249 if ((cnt
= inln(inf
,lbuf
,LBUF
,&cps
,0,&mor
)) < 0)
256 prhead(hbuf
, fname
, pagecnt
))
265 addnum(nbuf
, num
, ++lncnt
);
266 if (otln(obuf
,cnt
+off
, &ips
, &ops
, mor
))
268 } else if (otln(lbuf
, cnt
, &ips
, &ops
, mor
))
272 * if line bigger than buffer, get more
280 * whole line rcvd. reset tab proc. state
289 * fill to end of page
291 if (linecnt
&& prtail(lines
-linecnt
-lrgln
, lrgln
))
295 * On EOF go to next file
313 if (inf
!= NULL
&& inf
!= stdin
)
319 * vertcol: print files with more than one column of output down a page
322 vertcol(int argc
, char *argv
[])
325 char **lstdat
= NULL
;
337 int mxlen
= pgwd
+ offst
+ 1;
338 int mclcnt
= clcnt
- 1;
339 struct vcol
*vc
= NULL
;
356 * allocate page buffer
358 if ((buf
= malloc((unsigned)lines
*mxlen
*sizeof(char))) == NULL
)
362 * allocate page header
364 if ((hbuf
= malloc((unsigned)(HDBUF
+ offst
)*sizeof(char))) == NULL
)
366 ohbuf
= hbuf
+ offst
;
368 (void)memset(hbuf
, (int)' ', offst
);
371 * col pointers when no headers
374 if ((vc
= malloc((unsigned)mvc
*sizeof(struct vcol
))) == NULL
)
378 * pointer into page where last data per line is located
380 if ((lstdat
= malloc((unsigned)lines
*sizeof(char *))) == NULL
)
384 * fast index lookups to locate start of lines
386 if ((indy
= malloc((unsigned)lines
*sizeof(int))) == NULL
)
388 if ((lindy
= malloc((unsigned)lines
*sizeof(int))) == NULL
)
397 * initialize buffer lookup indexes and offset area
399 for (j
= 0; j
< lines
; ++j
) {
400 lindy
[j
] = j
* mxlen
;
401 indy
[j
] = lindy
[j
] + offst
;
403 ptbf
= buf
+ lindy
[j
];
404 (void)memset(ptbf
, (int)' ', offst
);
407 ptbf
= buf
+ indy
[j
];
414 while ((inf
= nxtfile(argc
, argv
, &fname
, ohbuf
, 0)) != NULL
) {
417 * skip to requested page
419 if (inskip(inf
, pgnm
, lines
))
434 for (i
= 0; i
< clcnt
; ++i
) {
437 * if last column, do not pad
448 * is this first column
451 ptbf
= buf
+ indy
[j
];
461 addnum(ptbf
, nmwd
, ++lncnt
);
469 cnt
= inln(inf
,ptbf
,colwd
,&cps
,1,&mor
);
476 * pad all but last column on page
480 * pad to end of column
484 else if ((pln
= col
-cnt
) > 0) {
491 * remember last char in line
502 * when -t (no header) is specified the spec requires
503 * the min number of lines. The last page may not have
504 * balanced length columns. To fix this we must reorder
505 * the columns. This is a very slow technique so it is
506 * only used under limited conditions. Without -t, the
507 * balancing of text columns is unspecified. To NOT
508 * balance the last page, add the global variable
509 * nohead to the if statement below e.g.
511 * if ((cnt < 0) && nohead && cvc ......
516 * check to see if last page needs to be reordered
518 if ((cnt
< 0) && cvc
&& ((mvc
-cvc
) >= clcnt
)){
529 if (!nohead
&& prhead(hbuf
, fname
, pagecnt
))
531 for (i
= 0; i
< pln
; ++i
) {
534 if (offst
&& otln(buf
,offst
,&ips
,&ops
,1)) {
540 for (j
= 0; j
< clcnt
; ++j
) {
542 * determine column length
555 cnt
= vc
[tvc
].cnt
+ 1;
560 if (otln(vc
[tvc
].pt
, cnt
, &ips
,
570 if (otln(buf
, 0, &ips
, &ops
, 0))
576 if (prtail((lines
- pln
), 0))
579 * done with output, go to next file
585 * determine how many lines to output
599 if (!nohead
&& prhead(hbuf
, fname
, pagecnt
))
606 for (i
= 0; i
< pln
; ++i
) {
607 ptbf
= buf
+ lindy
[i
];
608 if ((j
= lstdat
[i
] - ptbf
) <= offst
)
610 if (otln(ptbf
, j
, &ips
, &ops
, 0))
617 if (pln
&& prtail((lines
- pln
), 0))
621 * if EOF go to next file
642 if (inf
!= NULL
&& inf
!= stdin
)
648 * horzcol: print files with more than one column of output across a page
651 horzcol(int argc
, char *argv
[])
673 if ((buf
= malloc((unsigned)(pgwd
+offst
+1)*sizeof(char))) == NULL
)
679 if ((hbuf
= malloc((unsigned)(HDBUF
+ offst
)*sizeof(char))) == NULL
)
681 ohbuf
= hbuf
+ offst
;
683 (void)memset(buf
, (int)' ', offst
);
684 (void)memset(hbuf
, (int)' ', offst
);
690 while ((inf
= nxtfile(argc
, argv
, &fname
, ohbuf
, 0)) != NULL
) {
692 if (inskip(inf
, pgnm
, lines
))
706 for (i
= 0; i
< lines
; ++i
) {
716 * add number to column
718 addnum(ptbf
, nmwd
, ++lncnt
);
725 if ((cnt
= inln(inf
,ptbf
,colwd
,&cps
,1,
732 * if last line skip padding
738 * pad to end of column
742 else if ((pln
= col
- cnt
) > 0) {
743 (void)memset(ptbf
,(int)' ',pln
);
749 * determine line length
751 if ((j
= lstdat
- buf
) <= offst
)
758 prhead(hbuf
, fname
, pagecnt
))
764 if (otln(buf
, j
, &ips
, &ops
, 0))
771 if (i
&& prtail(lines
-i
, 0))
775 * if EOF go to next file
793 if (inf
!= NULL
&& inf
!= stdin
)
799 * mulfile: print files with more than one column of output and
800 * more than one file concurrently
803 mulfile(int argc
, char *argv
[])
828 * array of FILE *, one for each operand
830 if ((fbuf
= calloc(clcnt
, sizeof(FILE *))) == NULL
)
836 if ((hbuf
= malloc((unsigned)(HDBUF
+ offst
)*sizeof(char))) == NULL
)
838 ohbuf
= hbuf
+ offst
;
841 * do not know how many columns yet. The number of operands provide an
842 * upper bound on the number of columns. We use the number of files
843 * we can open successfully to set the number of columns. The operation
844 * of the merge operation (-m) in relation to unsuccesful file opens
845 * is unspecified by posix.
849 if ((fbuf
[j
] = nxtfile(argc
, argv
, &fname
, ohbuf
, 1)) == NULL
)
851 if (pgnm
&& (inskip(fbuf
[j
], pgnm
, lines
)))
863 * calculate page boundries based on open file count
867 colwd
= (pgwd
- clcnt
- nmwd
)/clcnt
;
868 pgwd
= ((colwd
+ 1) * clcnt
) - nmwd
- 2;
870 colwd
= (pgwd
+ 1 - clcnt
)/clcnt
;
871 pgwd
= ((colwd
+ 1) * clcnt
) - 1;
875 "pr: page width too small for %d columns\n", clcnt
);
884 if ((buf
= malloc((unsigned)(pgwd
+offst
+1)*sizeof(char))) == NULL
)
887 (void)memset(buf
, (int)' ', offst
);
888 (void)memset(hbuf
, (int)' ', offst
);
897 * continue to loop while any file still has data
903 for (i
= 0; i
< lines
; ++i
) {
908 * add line number to line
910 addnum(ptbf
, nmwd
, ++lncnt
);
920 for (j
= 0; j
< clcnt
; ++j
) {
921 if (fbuf
[j
] == NULL
) {
926 } else if ((cnt
= inln(fbuf
[j
], ptbf
, colwd
,
927 &cps
, 1, &mor
)) < 0) {
931 if (fbuf
[j
] != stdin
)
932 (void)fclose(fbuf
[j
]);
946 * if last ACTIVE column, done with line
952 * pad to end of column
956 } else if ((pln
= col
- cnt
) > 0) {
957 (void)memset(ptbf
, (int)' ', pln
);
963 * calculate data in line
965 if ((j
= lstdat
- buf
) <= offst
)
972 if (!nohead
&& prhead(hbuf
, fname
, pagecnt
))
979 if (otln(buf
, j
, &ips
, &ops
, 0))
983 * if no more active files, done
994 if (i
&& prtail(lines
-i
, 0))
1006 for (j
= 0; j
< clcnt
; j
++)
1007 if (fbuf
[j
] && fbuf
[j
] != stdin
)
1008 (void)fclose(fbuf
[j
]);
1017 * inln(): input a line of data (unlimited length lines supported)
1018 * Input is optionally expanded to spaces
1022 * lim: buffer length
1023 * cps: column positon 1st char in buffer (large line support)
1024 * trnc: throw away data more than lim up to \n
1025 * mor: set if more data in line (not truncated)
1028 inln(FILE *inf
, char *buf
, int lim
, int *cps
, int trnc
, int *mor
)
1034 int chk
= (int)inchar
;
1040 * expanding input option
1042 while ((--lim
>= 0) && ((ch
= getc(inf
)) != EOF
)) {
1044 * is this the input "tab" char
1048 * expand to number of spaces
1050 col
= (ptbuf
- buf
) + *cps
;
1051 col
= gap
- (col
% gap
);
1054 * if more than this line, push back
1056 if ((col
> lim
) && (ungetc(ch
, inf
) == EOF
))
1062 while ((--col
>= 0) && (--lim
>= 0))
1074 while ((--lim
>= 0) && ((ch
= getc(inf
)) != EOF
)) {
1090 * entire line processed
1098 * line was larger than limit
1102 * throw away rest of line
1104 while ((ch
= getc(inf
)) != EOF
) {
1112 * save column offset if not truncated
1122 * otln(): output a line of data. (Supports unlimited length lines)
1123 * output is optionally contracted to tabs
1125 * buf: output buffer with data
1126 * cnt: number of chars of valid data in buf
1127 * svips: buffer input column position (for large lines)
1128 * svops: buffer output column position (for large lines)
1129 * mor: output line not complete in this buf; more data to come.
1130 * 1 is more, 0 is complete, -1 is no \n's
1133 otln(char *buf
, int cnt
, int *svips
, int *svops
, int mor
)
1135 int ops
; /* last col output */
1136 int ips
; /* last col in buf examined */
1143 * contracting on output
1148 while (buf
< endbuf
) {
1150 * count number of spaces and ochar in buffer
1159 * simulate ochar processing
1161 if (*buf
== ochar
) {
1162 ips
+= gap
- (ips
% gap
);
1168 * got a non space char; contract out spaces
1170 while (ips
- ops
> 1) {
1172 * use as many ochar as will fit
1174 if ((tbps
= ops
+ gap
- (ops
% gap
)) > ips
)
1176 if (putchar(ochar
) == EOF
) {
1185 * finish off with spaces
1187 if (putchar(' ') == EOF
) {
1195 * output non space char
1197 if (putchar(*buf
++) == EOF
) {
1207 * if incomplete line, save position counts
1215 while (ips
- ops
> 1) {
1217 * use as many ochar as will fit
1219 if ((tbps
= ops
+ gap
- (ops
% gap
)) > ips
)
1221 if (putchar(ochar
) == EOF
) {
1229 * finish off with spaces
1231 if (putchar(' ') == EOF
) {
1241 * output is not contracted
1243 if (cnt
&& (fwrite(buf
, sizeof(char), cnt
, stdout
) <= 0)) {
1252 * process line end and double space as required
1254 if ((putchar('\n') == EOF
) || (dspace
&& (putchar('\n') == EOF
))) {
1262 * inskip(): skip over pgcnt pages with lncnt lines per page
1263 * file is closed at EOF (if not stdin).
1265 * inf FILE * to read from
1266 * pgcnt number of pages to skip
1267 * lncnt number of lines per page
1270 inskip(FILE *inf
, int pgcnt
, int lncnt
)
1275 while(--pgcnt
> 0) {
1277 while ((c
= getc(inf
)) != EOF
) {
1278 if ((c
== '\n') && (--cnt
== 0))
1291 * nxtfile: returns a FILE * to next file in arg list and sets the
1292 * time field for this file (or current date).
1294 * buf array to store proper date for the header.
1295 * dt if set skips the date processing (used with -m)
1298 nxtfile(int argc
, char **argv
, const char **fname
, char *buf
, int dt
)
1303 struct tm
*timeptr
= NULL
;
1304 struct stat statbuf
;
1306 static int twice
= -1;
1309 if (eoptind
>= argc
) {
1311 * no file listed; default, use standard input
1323 if (gettimeofday(&tv
, &tz
) < 0) {
1325 (void)fprintf(errf
, "pr: cannot get time of day, %s\n",
1330 curtime
= tv
.tv_sec
;
1331 timeptr
= localtime(&curtime
);
1333 for (; eoptind
< argc
; ++eoptind
) {
1334 if (strcmp(argv
[eoptind
], "-") == 0) {
1336 * process a "-" for filename
1345 if (nohead
|| (dt
&& twice
))
1347 if (gettimeofday(&tv
, &tz
) < 0) {
1350 "pr: cannot get time of day, %s\n",
1354 curtime
= tv
.tv_sec
;
1355 timeptr
= localtime(&curtime
);
1358 * normal file processing
1360 if ((inf
= fopen(argv
[eoptind
], "r")) == NULL
) {
1364 (void)fprintf(errf
, "pr: Cannot open %s, %s\n",
1365 argv
[eoptind
], strerror(errno
));
1373 *fname
= argv
[eoptind
];
1375 if (nohead
|| (dt
&& twice
))
1379 if (gettimeofday(&tv
, &tz
) < 0) {
1382 "pr: cannot get time of day, %s\n",
1386 curtime
= tv
.tv_sec
;
1387 timeptr
= localtime(&curtime
);
1389 if (fstat(fileno(inf
), &statbuf
) < 0) {
1393 "pr: Cannot stat %s, %s\n",
1394 argv
[eoptind
], strerror(errno
));
1397 timeptr
= localtime(&(statbuf
.st_mtime
));
1406 * set up time field used in header
1408 if (strftime(buf
, HDBUF
, timefrmt
, timeptr
) <= 0) {
1412 (void)fputs("pr: time conversion failed\n", errf
);
1419 * addnum(): adds the line number to the column
1420 * Truncates from the front or pads with spaces as required.
1421 * Numbers are right justified.
1423 * buf buffer to store the number
1424 * wdth width of buffer to fill
1427 * NOTE: numbers occupy part of the column. The posix
1428 * spec does not specify if -i processing should or should not
1429 * occur on number padding. The spec does say it occupies
1430 * part of the column. The usage of addnum currently treats
1431 * numbers as part of the column so spaces may be replaced.
1434 addnum(char *buf
, int wdth
, int line
)
1436 char *pt
= buf
+ wdth
;
1439 *--pt
= digs
[line
% 10];
1441 } while (line
&& (pt
> buf
));
1444 * pad with space as required
1451 * prpause(): pause before printing each page
1453 * pagcnt page number
1462 (void)putc('\a', stderr
);
1463 (void)fflush(stderr
);
1465 while ((c
= getc(ttyinf
)) != '\n' && c
!= EOF
)
1469 * pause ONLY before first page of first file
1471 if (pgpause
== FIRSTPAGE
&& pagcnt
== 1)
1477 * prhead(): prints the top of page header
1479 * buf buffer with time field (and offset)
1480 * cnt number of chars in buf
1481 * fname fname field for header
1482 * pagcnt page number
1485 prhead(char *buf
, const char *fname
, int pagcnt
)
1490 if ((putchar('\n') == EOF
) || (putchar('\n') == EOF
)) {
1495 * posix is not clear if the header is subject to line length
1496 * restrictions. The specification for header line format
1497 * in the spec clearly does not limit length. No pr currently
1498 * restricts header length. However if we need to truncate in
1499 * an reasonable way, adjust the length of the printf by
1500 * changing HDFMT to allow a length max as an argument printf.
1501 * buf (which contains the offset spaces and time field could
1504 * note only the offset (if any) is processed for tab expansion
1506 if (offst
&& otln(buf
, offst
, &ips
, &ops
, -1))
1508 (void)printf(HDFMT
,buf
+offst
, fname
, pagcnt
);
1513 * prtail(): pad page with empty lines (if required) and print page trailer
1516 * cnt number of lines of padding needed
1517 * incomp was a '\n' missing from last line output
1520 prtail(int cnt
, int incomp
)
1524 * only pad with no headers when incomplete last line
1528 if ((dspace
&& (putchar('\n') == EOF
)) ||
1529 (putchar('\n') == EOF
)) {
1537 * if double space output two \n
1543 * if an odd number of lines per page, add an extra \n
1552 if ((incomp
&& (putchar('\n') == EOF
)) ||
1553 (putchar('\f') == EOF
)) {
1560 while (--cnt
>= 0) {
1561 if (putchar('\n') == EOF
) {
1570 * terminate(): when a SIGINT is recvd
1573 terminate(int which_sig
)
1576 (void)raise_default_signal(which_sig
);
1582 * flsh_errs(): output saved up diagnostic messages after all normal
1583 * processing has completed
1590 (void)fflush(stdout
);
1595 while (fgets(buf
, BUFSIZ
, errf
) != NULL
)
1596 (void)fputs(buf
, stderr
);
1602 (void)fputs("pr: memory allocation failed\n", errf
);
1608 (void)fprintf(errf
, "pr: write failure, %s\n", strerror(errno
));
1615 "usage: pr [+page] [-col] [-adFfmprt] [-e[ch][gap]] [-h header]\n",
1618 " [-i[ch][gap]] [-l line] [-n[ch][width]] [-o offset]\n",
1621 " [-s[ch]] [-T timefmt] [-w width] [-] [file ...]\n",
1626 * setup: Validate command args, initialize and perform sanity
1630 setup(int argc
, char **argv
)
1640 if (isatty(fileno(stdout
))) {
1642 * defer diagnostics until processing is done
1644 if ((errf
= tmpfile()) == NULL
) {
1645 (void)fputs("Cannot defer diagnostic messages\n",stderr
);
1651 while ((c
= egetopt(argc
, argv
, "#adFfmrte?h:i?l:n?o:ps?T:w:")) != -1) {
1654 if ((pgnm
= atoi(eoptarg
)) < 1) {
1655 (void)fputs("pr: +page number must be 1 or more\n",
1661 if ((clcnt
= atoi(eoptarg
)) < 1) {
1662 (void)fputs("pr: -columns must be 1 or more\n",errf
);
1676 if ((eoptarg
!= NULL
) &&
1677 !isdigit((unsigned char)*eoptarg
))
1678 inchar
= *eoptarg
++;
1681 if ((eoptarg
!= NULL
) &&
1682 isdigit((unsigned char)*eoptarg
)) {
1683 if ((ingap
= atoi(eoptarg
)) < 0) {
1685 "pr: -e gap must be 0 or more\n", errf
);
1690 } else if ((eoptarg
!= NULL
) && (*eoptarg
!= '\0')) {
1692 "pr: invalid value for -e %s\n", eoptarg
);
1698 pgpause
|= FIRSTPAGE
;
1708 if ((eoptarg
!= NULL
) &&
1709 !isdigit((unsigned char)*eoptarg
))
1713 if ((eoptarg
!= NULL
) &&
1714 isdigit((unsigned char)*eoptarg
)) {
1715 if ((ogap
= atoi(eoptarg
)) < 0) {
1717 "pr: -i gap must be 0 or more\n", errf
);
1722 } else if ((eoptarg
!= NULL
) && (*eoptarg
!= '\0')) {
1724 "pr: invalid value for -i %s\n", eoptarg
);
1730 if (!isdigit((unsigned char)*eoptarg
) ||
1731 ((lines
=atoi(eoptarg
)) < 1)) {
1733 "pr: Number of lines must be 1 or more\n",errf
);
1741 if ((eoptarg
!= NULL
) &&
1742 !isdigit((unsigned char)*eoptarg
))
1743 nmchar
= *eoptarg
++;
1746 if ((eoptarg
!= NULL
) &&
1747 isdigit((unsigned char)*eoptarg
)) {
1748 if ((nmwd
= atoi(eoptarg
)) < 1) {
1750 "pr: -n width must be 1 or more\n",errf
);
1753 } else if ((eoptarg
!= NULL
) && (*eoptarg
!= '\0')) {
1755 "pr: invalid value for -n %s\n", eoptarg
);
1761 if (!isdigit((unsigned char)*eoptarg
) ||
1762 ((offst
= atoi(eoptarg
))< 1)){
1763 (void)fputs("pr: -o offset must be 1 or more\n",
1769 pgpause
|= EACHPAGE
;
1776 if (eoptarg
== NULL
)
1780 if (eoptarg
&& *eoptarg
!= '\0') {
1782 "pr: invalid value for -s %s\n", eoptarg
);
1794 if (!isdigit((unsigned char)*eoptarg
) ||
1795 ((pgwd
= atoi(eoptarg
)) < 1)){
1797 "pr: -w width must be 1 or more \n",errf
);
1808 * default and sanity checks
1812 if ((clcnt
= argc
- eoptind
) <= 1) {
1821 (void)fputs("pr: -a flag requires multiple columns\n",
1826 (void)fputs("pr: -m cannot be used with -a\n", errf
);
1836 if (cflag
|| merge
) {
1849 "pr: -m cannot be used with multiple columns\n", errf
);
1853 colwd
= (pgwd
+ 1 - (clcnt
* (nmwd
+ 2)))/clcnt
;
1854 pgwd
= ((colwd
+ nmwd
+ 2) * clcnt
) - 1;
1856 colwd
= (pgwd
+ 1 - clcnt
)/clcnt
;
1857 pgwd
= ((colwd
+ 1) * clcnt
) - 1;
1861 "pr: page width is too small for %d columns\n",clcnt
);
1869 * make sure long enough for headers. if not disable
1871 if (lines
<= HEADLEN
+ TAILLEN
)
1874 lines
-= HEADLEN
+ TAILLEN
;
1877 * adjust for double space on odd length pages
1890 * open /dev/tty if we are to pause before each page
1891 * but only if stdout is a terminal and stdin is not a terminal
1893 if (ttyout
&& pgpause
&& !isatty(fileno(stdin
))) {
1894 if ((ttyinf
= fopen("/dev/tty", "r")) == NULL
) {
1895 (void)fprintf(errf
, "pr: cannot open terminal\n");