| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | /* $NetBSD: pr.c,v 1.20 2009/04/13 00:37:05 lukem Exp $ */ | | 1 | /* $NetBSD: pr.c,v 1.21 2011/09/06 18:26:06 joerg Exp $ */ |
2 | | | 2 | |
3 | /*- | | 3 | /*- |
4 | * Copyright (c) 1991 Keith Muller. | | 4 | * Copyright (c) 1991 Keith Muller. |
5 | * Copyright (c) 1993 | | 5 | * Copyright (c) 1993 |
6 | * The Regents of the University of California. All rights reserved. | | 6 | * The Regents of the University of California. All rights reserved. |
7 | * | | 7 | * |
8 | * This code is derived from software contributed to Berkeley by | | 8 | * This code is derived from software contributed to Berkeley by |
9 | * Keith Muller of the University of California, San Diego. | | 9 | * Keith Muller of the University of California, San Diego. |
10 | * | | 10 | * |
11 | * Redistribution and use in source and binary forms, with or without | | 11 | * Redistribution and use in source and binary forms, with or without |
12 | * modification, are permitted provided that the following conditions | | 12 | * modification, are permitted provided that the following conditions |
13 | * are met: | | 13 | * are met: |
14 | * 1. Redistributions of source code must retain the above copyright | | 14 | * 1. Redistributions of source code must retain the above copyright |
| @@ -33,27 +33,27 @@ | | | @@ -33,27 +33,27 @@ |
33 | * SUCH DAMAGE. | | 33 | * SUCH DAMAGE. |
34 | */ | | 34 | */ |
35 | | | 35 | |
36 | #include <sys/cdefs.h> | | 36 | #include <sys/cdefs.h> |
37 | #ifndef lint | | 37 | #ifndef lint |
38 | __COPYRIGHT("@(#) Copyright (c) 1993\ | | 38 | __COPYRIGHT("@(#) Copyright (c) 1993\ |
39 | The Regents of the University of California. All rights reserved."); | | 39 | The Regents of the University of California. All rights reserved."); |
40 | #endif /* not lint */ | | 40 | #endif /* not lint */ |
41 | | | 41 | |
42 | #ifndef lint | | 42 | #ifndef lint |
43 | #if 0 | | 43 | #if 0 |
44 | from: static char sccsid[] = "@(#)pr.c 8.1 (Berkeley) 6/6/93"; | | 44 | from: static char sccsid[] = "@(#)pr.c 8.1 (Berkeley) 6/6/93"; |
45 | #else | | 45 | #else |
46 | __RCSID("$NetBSD: pr.c,v 1.20 2009/04/13 00:37:05 lukem Exp $"); | | 46 | __RCSID("$NetBSD: pr.c,v 1.21 2011/09/06 18:26:06 joerg Exp $"); |
47 | #endif | | 47 | #endif |
48 | #endif /* not lint */ | | 48 | #endif /* not lint */ |
49 | | | 49 | |
50 | #include <sys/types.h> | | 50 | #include <sys/types.h> |
51 | #include <sys/time.h> | | 51 | #include <sys/time.h> |
52 | #include <sys/stat.h> | | 52 | #include <sys/stat.h> |
53 | | | 53 | |
54 | #include <ctype.h> | | 54 | #include <ctype.h> |
55 | #include <errno.h> | | 55 | #include <errno.h> |
56 | #include <signal.h> | | 56 | #include <signal.h> |
57 | #include <stdio.h> | | 57 | #include <stdio.h> |
58 | #include <stdlib.h> | | 58 | #include <stdlib.h> |
59 | #include <string.h> | | 59 | #include <string.h> |
| @@ -67,63 +67,77 @@ __RCSID("$NetBSD: pr.c,v 1.20 2009/04/13 | | | @@ -67,63 +67,77 @@ __RCSID("$NetBSD: pr.c,v 1.20 2009/04/13 |
67 | /* | | 67 | /* |
68 | * pr: a printing and pagination filter. If multiple input files | | 68 | * pr: a printing and pagination filter. If multiple input files |
69 | * are specified, each is read, formatted, and written to standard | | 69 | * are specified, each is read, formatted, and written to standard |
70 | * output. By default, input is separated into 66-line pages, each | | 70 | * output. By default, input is separated into 66-line pages, each |
71 | * with a header that includes the page number, date, time and the | | 71 | * with a header that includes the page number, date, time and the |
72 | * files pathname. | | 72 | * files pathname. |
73 | * | | 73 | * |
74 | * Complies with posix P1003.2/D11 | | 74 | * Complies with posix P1003.2/D11 |
75 | */ | | 75 | */ |
76 | | | 76 | |
77 | /* | | 77 | /* |
78 | * parameter variables | | 78 | * parameter variables |
79 | */ | | 79 | */ |
80 | int pgnm; /* starting page number */ | | 80 | static int pgnm; /* starting page number */ |
81 | int clcnt; /* number of columns */ | | 81 | static int clcnt; /* number of columns */ |
82 | int colwd; /* column data width - multiple columns */ | | 82 | static int colwd; /* column data width - multiple columns */ |
83 | int across; /* mult col flag; write across page */ | | 83 | static int across; /* mult col flag; write across page */ |
84 | int dspace; /* double space flag */ | | 84 | static int dspace; /* double space flag */ |
85 | char inchar; /* expand input char */ | | 85 | static char inchar; /* expand input char */ |
86 | int ingap; /* expand input gap */ | | 86 | static int ingap; /* expand input gap */ |
87 | int formfeed; /* use formfeed as trailer */ | | 87 | static int formfeed; /* use formfeed as trailer */ |
88 | char *header; /* header name instead of file name */ | | 88 | static char *header; /* header name instead of file name */ |
89 | char ochar; /* contract output char */ | | 89 | static char ochar; /* contract output char */ |
90 | int ogap; /* contract output gap */ | | 90 | static int ogap; /* contract output gap */ |
91 | int lines; /* number of lines per page */ | | 91 | static int lines; /* number of lines per page */ |
92 | int merge; /* merge multiple files in output */ | | 92 | static int merge; /* merge multiple files in output */ |
93 | char nmchar; /* line numbering append char */ | | 93 | static char nmchar; /* line numbering append char */ |
94 | int nmwd; /* width of line number field */ | | 94 | static int nmwd; /* width of line number field */ |
95 | int offst; /* number of page offset spaces */ | | 95 | static int offst; /* number of page offset spaces */ |
96 | int nodiag; /* do not report file open errors */ | | 96 | static int nodiag; /* do not report file open errors */ |
97 | char schar; /* text column separation character */ | | 97 | static char schar; /* text column separation character */ |
98 | int sflag; /* -s option for multiple columns */ | | 98 | static int sflag; /* -s option for multiple columns */ |
99 | int nohead; /* do not write head and trailer */ | | 99 | static int nohead; /* do not write head and trailer */ |
100 | int pgwd; /* page width with multiple col output */ | | 100 | static int pgwd; /* page width with multiple col output */ |
101 | const char *timefrmt = TIMEFMT; /* time conversion string */ | | 101 | static const char *timefrmt = TIMEFMT; /* time conversion string */ |
102 | | | 102 | |
103 | /* | | 103 | /* |
104 | * misc globals | | 104 | * misc globals |
105 | */ | | 105 | */ |
106 | FILE *err; /* error message file pointer */ | | 106 | static FILE *errf; /* error message file pointer */ |
107 | int addone; /* page length is odd with double space */ | | 107 | static int addone; /* page length is odd with double space */ |
108 | int errcnt; /* error count on file processing */ | | 108 | static int errcnt; /* error count on file processing */ |
109 | char digs[] = "0123456789"; /* page number translation map */ | | 109 | static const char digs[] = "0123456789"; /* page number translation map */ |
110 | | | 110 | |
111 | int main __P((int, char **)); | | 111 | static void addnum(char *, int, int); |
| | | 112 | static void flsh_errs(void); |
| | | 113 | static int horzcol(int, char **); |
| | | 114 | static int inln(FILE *, char *, int, int *, int, int *); |
| | | 115 | static int inskip(FILE *, int, int); |
| | | 116 | static void mfail(void); |
| | | 117 | static int mulfile(int, char **); |
| | | 118 | static FILE *nxtfile(int, char **, const char **, char *, int); |
| | | 119 | static int onecol(int, char **); |
| | | 120 | static int otln(char *, int, int *, int *, int); |
| | | 121 | static void pfail(void); |
| | | 122 | static int prhead(char *, const char *, int); |
| | | 123 | static int prtail(int, int); |
| | | 124 | static int setup(int, char **); |
| | | 125 | __dead static void terminate(int); |
| | | 126 | static void usage(void); |
| | | 127 | static int vertcol(int, char **); |
112 | | | 128 | |
113 | int | | 129 | int |
114 | main(argc, argv) | | 130 | main(int argc, char *argv[]) |
115 | int argc; | | | |
116 | char *argv[]; | | | |
117 | { | | 131 | { |
118 | int ret_val; | | 132 | int ret_val; |
119 | | | 133 | |
120 | if (signal(SIGINT, SIG_IGN) != SIG_IGN) | | 134 | if (signal(SIGINT, SIG_IGN) != SIG_IGN) |
121 | (void)signal(SIGINT, terminate); | | 135 | (void)signal(SIGINT, terminate); |
122 | ret_val = setup(argc, argv); | | 136 | ret_val = setup(argc, argv); |
123 | if (!ret_val) { | | 137 | if (!ret_val) { |
124 | /* | | 138 | /* |
125 | * select the output format based on options | | 139 | * select the output format based on options |
126 | */ | | 140 | */ |
127 | if (merge) | | 141 | if (merge) |
128 | ret_val = mulfile(argc, argv); | | 142 | ret_val = mulfile(argc, argv); |
129 | else if (clcnt == 1) | | 143 | else if (clcnt == 1) |
| @@ -134,30 +148,28 @@ main(argc, argv) | | | @@ -134,30 +148,28 @@ main(argc, argv) |
134 | ret_val = vertcol(argc, argv); | | 148 | ret_val = vertcol(argc, argv); |
135 | } else | | 149 | } else |
136 | usage(); | | 150 | usage(); |
137 | flsh_errs(); | | 151 | flsh_errs(); |
138 | if (errcnt || ret_val) | | 152 | if (errcnt || ret_val) |
139 | exit(1); | | 153 | exit(1); |
140 | return(0); | | 154 | return(0); |
141 | } | | 155 | } |
142 | | | 156 | |
143 | /* | | 157 | /* |
144 | * onecol: print files with only one column of output. | | 158 | * onecol: print files with only one column of output. |
145 | * Line length is unlimited. | | 159 | * Line length is unlimited. |
146 | */ | | 160 | */ |
147 | int | | 161 | static int |
148 | onecol(argc, argv) | | 162 | onecol(int argc, char *argv[]) |
149 | int argc; | | | |
150 | char *argv[]; | | | |
151 | { | | 163 | { |
152 | int cnt = -1; | | 164 | int cnt = -1; |
153 | int off; | | 165 | int off; |
154 | int lrgln; | | 166 | int lrgln; |
155 | int linecnt; | | 167 | int linecnt; |
156 | int num; | | 168 | int num; |
157 | int lncnt; | | 169 | int lncnt; |
158 | int pagecnt; | | 170 | int pagecnt; |
159 | int ips; | | 171 | int ips; |
160 | int ops; | | 172 | int ops; |
161 | int cps; | | 173 | int cps; |
162 | char *obuf = NULL; | | 174 | char *obuf = NULL; |
163 | char *lbuf; | | 175 | char *lbuf; |
| @@ -275,42 +287,38 @@ onecol(argc, argv) | | | @@ -275,42 +287,38 @@ onecol(argc, argv) |
275 | break; | | 287 | break; |
276 | ++pagecnt; | | 288 | ++pagecnt; |
277 | } | | 289 | } |
278 | if (inf != stdin) | | 290 | if (inf != stdin) |
279 | (void)fclose(inf); | | 291 | (void)fclose(inf); |
280 | } | | 292 | } |
281 | if (eoptind < argc) | | 293 | if (eoptind < argc) |
282 | goto out; | | 294 | goto out; |
283 | error = 0; | | 295 | error = 0; |
284 | goto out; | | 296 | goto out; |
285 | oomem: | | 297 | oomem: |
286 | mfail(); | | 298 | mfail(); |
287 | out: | | 299 | out: |
288 | if (obuf) | | 300 | free(obuf); |
289 | free(obuf); | | 301 | free(hbuf); |
290 | if (hbuf) | | | |
291 | free(hbuf); | | | |
292 | if (inf != NULL && inf != stdin) | | 302 | if (inf != NULL && inf != stdin) |
293 | (void)fclose(inf); | | 303 | (void)fclose(inf); |
294 | return error; | | 304 | return error; |
295 | } | | 305 | } |
296 | | | 306 | |
297 | /* | | 307 | /* |
298 | * vertcol: print files with more than one column of output down a page | | 308 | * vertcol: print files with more than one column of output down a page |
299 | */ | | 309 | */ |
300 | int | | 310 | static int |
301 | vertcol(argc, argv) | | 311 | vertcol(int argc, char *argv[]) |
302 | int argc; | | | |
303 | char *argv[]; | | | |
304 | { | | 312 | { |
305 | char *ptbf; | | 313 | char *ptbf; |
306 | char **lstdat = NULL; | | 314 | char **lstdat = NULL; |
307 | int i; | | 315 | int i; |
308 | int j; | | 316 | int j; |
309 | int cnt = -1; | | 317 | int cnt = -1; |
310 | int pln; | | 318 | int pln; |
311 | int *indy = NULL; | | 319 | int *indy = NULL; |
312 | int cvc; | | 320 | int cvc; |
313 | int *lindy = NULL; | | 321 | int *lindy = NULL; |
314 | int lncnt; | | 322 | int lncnt; |
315 | int stp; | | 323 | int stp; |
316 | int pagecnt; | | 324 | int pagecnt; |
| @@ -597,48 +605,41 @@ vertcol(argc, argv) | | | @@ -597,48 +605,41 @@ vertcol(argc, argv) |
597 | break; | | 605 | break; |
598 | ++pagecnt; | | 606 | ++pagecnt; |
599 | } | | 607 | } |
600 | if (inf != stdin) | | 608 | if (inf != stdin) |
601 | (void)fclose(inf); | | 609 | (void)fclose(inf); |
602 | } | | 610 | } |
603 | if (eoptind < argc) | | 611 | if (eoptind < argc) |
604 | goto out; | | 612 | goto out; |
605 | error = 0; | | 613 | error = 0; |
606 | goto out; | | 614 | goto out; |
607 | oomem: | | 615 | oomem: |
608 | mfail(); | | 616 | mfail(); |
609 | out: | | 617 | out: |
610 | if (buf) | | 618 | free(buf); |
611 | free(buf); | | 619 | free(hbuf); |
612 | if (hbuf) | | 620 | free(vc); |
613 | free(hbuf); | | 621 | free(lstdat); |
614 | if (vc) | | 622 | free(lindy); |
615 | free(vc); | | | |
616 | if (lstdat) | | | |
617 | free(lstdat); | | | |
618 | if (indy) | | | |
619 | free(lindy); | | | |
620 | if (inf != NULL && inf != stdin) | | 623 | if (inf != NULL && inf != stdin) |
621 | (void)fclose(inf); | | 624 | (void)fclose(inf); |
622 | return error; | | 625 | return error; |
623 | } | | 626 | } |
624 | | | 627 | |
625 | /* | | 628 | /* |
626 | * horzcol: print files with more than one column of output across a page | | 629 | * horzcol: print files with more than one column of output across a page |
627 | */ | | 630 | */ |
628 | int | | 631 | static int |
629 | horzcol(argc, argv) | | 632 | horzcol(int argc, char *argv[]) |
630 | int argc; | | | |
631 | char *argv[]; | | | |
632 | { | | 633 | { |
633 | char *ptbf; | | 634 | char *ptbf; |
634 | int pln; | | 635 | int pln; |
635 | int cnt = -1; | | 636 | int cnt = -1; |
636 | char *lstdat; | | 637 | char *lstdat; |
637 | int col = colwd + 1; | | 638 | int col = colwd + 1; |
638 | int j; | | 639 | int j; |
639 | int i; | | 640 | int i; |
640 | int lncnt; | | 641 | int lncnt; |
641 | int pagecnt; | | 642 | int pagecnt; |
642 | char *buf = NULL; | | 643 | char *buf = NULL; |
643 | char *hbuf = NULL; | | 644 | char *hbuf = NULL; |
644 | char *ohbuf; | | 645 | char *ohbuf; |
| @@ -753,43 +754,39 @@ horzcol(argc, argv) | | | @@ -753,43 +754,39 @@ horzcol(argc, argv) |
753 | break; | | 754 | break; |
754 | ++pagecnt; | | 755 | ++pagecnt; |
755 | } | | 756 | } |
756 | if (inf != stdin) | | 757 | if (inf != stdin) |
757 | (void)fclose(inf); | | 758 | (void)fclose(inf); |
758 | } | | 759 | } |
759 | if (eoptind < argc) | | 760 | if (eoptind < argc) |
760 | goto out; | | 761 | goto out; |
761 | error = 0; | | 762 | error = 0; |
762 | goto out; | | 763 | goto out; |
763 | oomem: | | 764 | oomem: |
764 | mfail(); | | 765 | mfail(); |
765 | out: | | 766 | out: |
766 | if (buf) | | 767 | free(buf); |
767 | free(buf); | | 768 | free(hbuf); |
768 | if (hbuf) | | | |
769 | free(hbuf); | | | |
770 | if (inf != NULL && inf != stdin) | | 769 | if (inf != NULL && inf != stdin) |
771 | (void)fclose(inf); | | 770 | (void)fclose(inf); |
772 | return error; | | 771 | return error; |
773 | } | | 772 | } |
774 | | | 773 | |
775 | /* | | 774 | /* |
776 | * mulfile: print files with more than one column of output and | | 775 | * mulfile: print files with more than one column of output and |
777 | * more than one file concurrently | | 776 | * more than one file concurrently |
778 | */ | | 777 | */ |
779 | int | | 778 | static int |
780 | mulfile(argc, argv) | | 779 | mulfile(int argc, char *argv[]) |
781 | int argc; | | | |
782 | char *argv[]; | | | |
783 | { | | 780 | { |
784 | char *ptbf; | | 781 | char *ptbf; |
785 | int j; | | 782 | int j; |
786 | int pln; | | 783 | int pln; |
787 | int cnt; | | 784 | int cnt; |
788 | char *lstdat; | | 785 | char *lstdat; |
789 | int i; | | 786 | int i; |
790 | FILE **fbuf = NULL; | | 787 | FILE **fbuf = NULL; |
791 | int actf; | | 788 | int actf; |
792 | int lncnt; | | 789 | int lncnt; |
793 | int col; | | 790 | int col; |
794 | int pagecnt; | | 791 | int pagecnt; |
795 | int fproc; | | 792 | int fproc; |
| @@ -840,27 +837,27 @@ mulfile(argc, argv) | | | @@ -840,27 +837,27 @@ mulfile(argc, argv) |
840 | | | 837 | |
841 | /* | | 838 | /* |
842 | * calculate page boundries based on open file count | | 839 | * calculate page boundries based on open file count |
843 | */ | | 840 | */ |
844 | clcnt = j; | | 841 | clcnt = j; |
845 | if (nmwd) { | | 842 | if (nmwd) { |
846 | colwd = (pgwd - clcnt - nmwd)/clcnt; | | 843 | colwd = (pgwd - clcnt - nmwd)/clcnt; |
847 | pgwd = ((colwd + 1) * clcnt) - nmwd - 2; | | 844 | pgwd = ((colwd + 1) * clcnt) - nmwd - 2; |
848 | } else { | | 845 | } else { |
849 | colwd = (pgwd + 1 - clcnt)/clcnt; | | 846 | colwd = (pgwd + 1 - clcnt)/clcnt; |
850 | pgwd = ((colwd + 1) * clcnt) - 1; | | 847 | pgwd = ((colwd + 1) * clcnt) - 1; |
851 | } | | 848 | } |
852 | if (colwd < 1) { | | 849 | if (colwd < 1) { |
853 | (void)fprintf(err, | | 850 | (void)fprintf(errf, |
854 | "pr: page width too small for %d columns\n", clcnt); | | 851 | "pr: page width too small for %d columns\n", clcnt); |
855 | goto out; | | 852 | goto out; |
856 | } | | 853 | } |
857 | actf = clcnt; | | 854 | actf = clcnt; |
858 | col = colwd + 1; | | 855 | col = colwd + 1; |
859 | | | 856 | |
860 | /* | | 857 | /* |
861 | * line buffer | | 858 | * line buffer |
862 | */ | | 859 | */ |
863 | if ((buf = malloc((unsigned)(pgwd+offst+1)*sizeof(char))) == NULL) | | 860 | if ((buf = malloc((unsigned)(pgwd+offst+1)*sizeof(char))) == NULL) |
864 | goto out; | | 861 | goto out; |
865 | if (offst) { | | 862 | if (offst) { |
866 | (void)memset(buf, (int)' ', offst); | | 863 | (void)memset(buf, (int)' ', offst); |
| @@ -972,52 +969,44 @@ mulfile(argc, argv) | | | @@ -972,52 +969,44 @@ mulfile(argc, argv) |
972 | if (eoptind < argc) | | 969 | if (eoptind < argc) |
973 | goto out; | | 970 | goto out; |
974 | error = 0; | | 971 | error = 0; |
975 | goto out; | | 972 | goto out; |
976 | oomem: | | 973 | oomem: |
977 | mfail(); | | 974 | mfail(); |
978 | out: | | 975 | out: |
979 | if (fbuf) { | | 976 | if (fbuf) { |
980 | for (j = 0; j < clcnt; j++) | | 977 | for (j = 0; j < clcnt; j++) |
981 | if (fbuf[j] && fbuf[j] != stdin) | | 978 | if (fbuf[j] && fbuf[j] != stdin) |
982 | (void)fclose(fbuf[j]); | | 979 | (void)fclose(fbuf[j]); |
983 | free(fbuf); | | 980 | free(fbuf); |
984 | } | | 981 | } |
985 | if (hbuf) | | 982 | free(hbuf); |
986 | free(hbuf); | | 983 | free(buf); |
987 | if (buf) | | | |
988 | free(buf); | | | |
989 | return error; | | 984 | return error; |
990 | } | | 985 | } |
991 | | | 986 | |
992 | /* | | 987 | /* |
993 | * inln(): input a line of data (unlimited length lines supported) | | 988 | * inln(): input a line of data (unlimited length lines supported) |
994 | * Input is optionally expanded to spaces | | 989 | * Input is optionally expanded to spaces |
995 | * | | 990 | * |
996 | * inf: file | | 991 | * inf: file |
997 | * buf: buffer | | 992 | * buf: buffer |
998 | * lim: buffer length | | 993 | * lim: buffer length |
999 | * cps: column positon 1st char in buffer (large line support) | | 994 | * cps: column positon 1st char in buffer (large line support) |
1000 | * trnc: throw away data more than lim up to \n | | 995 | * trnc: throw away data more than lim up to \n |
1001 | * mor: set if more data in line (not truncated) | | 996 | * mor: set if more data in line (not truncated) |
1002 | */ | | 997 | */ |
1003 | int | | 998 | static int |
1004 | inln(inf, buf, lim, cps, trnc, mor) | | 999 | inln(FILE *inf, char *buf, int lim, int *cps, int trnc, int *mor) |
1005 | FILE *inf; | | | |
1006 | char *buf; | | | |
1007 | int lim; | | | |
1008 | int *cps; | | | |
1009 | int trnc; | | | |
1010 | int *mor; | | | |
1011 | { | | 1000 | { |
1012 | int col; | | 1001 | int col; |
1013 | int gap = ingap; | | 1002 | int gap = ingap; |
1014 | int ch = EOF; | | 1003 | int ch = EOF; |
1015 | char *ptbuf; | | 1004 | char *ptbuf; |
1016 | int chk = (int)inchar; | | 1005 | int chk = (int)inchar; |
1017 | | | 1006 | |
1018 | ptbuf = buf; | | 1007 | ptbuf = buf; |
1019 | | | 1008 | |
1020 | if (gap) { | | 1009 | if (gap) { |
1021 | /* | | 1010 | /* |
1022 | * expanding input option | | 1011 | * expanding input option |
1023 | */ | | 1012 | */ |
| @@ -1101,33 +1090,28 @@ inln(inf, buf, lim, cps, trnc, mor) | | | @@ -1101,33 +1090,28 @@ inln(inf, buf, lim, cps, trnc, mor) |
1101 | } | | 1090 | } |
1102 | | | 1091 | |
1103 | /* | | 1092 | /* |
1104 | * otln(): output a line of data. (Supports unlimited length lines) | | 1093 | * otln(): output a line of data. (Supports unlimited length lines) |
1105 | * output is optionally contracted to tabs | | 1094 | * output is optionally contracted to tabs |
1106 | * | | 1095 | * |
1107 | * buf: output buffer with data | | 1096 | * buf: output buffer with data |
1108 | * cnt: number of chars of valid data in buf | | 1097 | * cnt: number of chars of valid data in buf |
1109 | * svips: buffer input column position (for large lines) | | 1098 | * svips: buffer input column position (for large lines) |
1110 | * svops: buffer output column position (for large lines) | | 1099 | * svops: buffer output column position (for large lines) |
1111 | * mor: output line not complete in this buf; more data to come. | | 1100 | * mor: output line not complete in this buf; more data to come. |
1112 | * 1 is more, 0 is complete, -1 is no \n's | | 1101 | * 1 is more, 0 is complete, -1 is no \n's |
1113 | */ | | 1102 | */ |
1114 | int | | 1103 | static int |
1115 | otln(buf, cnt, svips, svops, mor) | | 1104 | otln(char *buf, int cnt, int *svips, int *svops, int mor) |
1116 | char *buf; | | | |
1117 | int cnt; | | | |
1118 | int *svops; | | | |
1119 | int *svips; | | | |
1120 | int mor; | | | |
1121 | { | | 1105 | { |
1122 | int ops; /* last col output */ | | 1106 | int ops; /* last col output */ |
1123 | int ips; /* last col in buf examined */ | | 1107 | int ips; /* last col in buf examined */ |
1124 | int gap = ogap; | | 1108 | int gap = ogap; |
1125 | int tbps; | | 1109 | int tbps; |
1126 | char *endbuf; | | 1110 | char *endbuf; |
1127 | | | 1111 | |
1128 | if (ogap) { | | 1112 | if (ogap) { |
1129 | /* | | 1113 | /* |
1130 | * contracting on output | | 1114 | * contracting on output |
1131 | */ | | 1115 | */ |
1132 | endbuf = buf + cnt; | | 1116 | endbuf = buf + cnt; |
1133 | ops = *svops; | | 1117 | ops = *svops; |
| @@ -1257,231 +1241,217 @@ otln(buf, cnt, svips, svops, mor) | | | @@ -1257,231 +1241,217 @@ otln(buf, cnt, svips, svops, mor) |
1257 | return(1); | | 1241 | return(1); |
1258 | } | | 1242 | } |
1259 | return(0); | | 1243 | return(0); |
1260 | } | | 1244 | } |
1261 | | | 1245 | |
1262 | /* | | 1246 | /* |
1263 | * inskip(): skip over pgcnt pages with lncnt lines per page | | 1247 | * inskip(): skip over pgcnt pages with lncnt lines per page |
1264 | * file is closed at EOF (if not stdin). | | 1248 | * file is closed at EOF (if not stdin). |
1265 | * | | 1249 | * |
1266 | * inf FILE * to read from | | 1250 | * inf FILE * to read from |
1267 | * pgcnt number of pages to skip | | 1251 | * pgcnt number of pages to skip |
1268 | * lncnt number of lines per page | | 1252 | * lncnt number of lines per page |
1269 | */ | | 1253 | */ |
1270 | int | | 1254 | static int |
1271 | inskip(inf, pgcnt, lncnt) | | 1255 | inskip(FILE *inf, int pgcnt, int lncnt) |
1272 | FILE *inf; | | | |
1273 | int pgcnt; | | | |
1274 | int lncnt; | | | |
1275 | { | | 1256 | { |
1276 | int c; | | 1257 | int c; |
1277 | int cnt; | | 1258 | int cnt; |
1278 | | | 1259 | |
1279 | while(--pgcnt > 0) { | | 1260 | while(--pgcnt > 0) { |
1280 | cnt = lncnt; | | 1261 | cnt = lncnt; |
1281 | while ((c = getc(inf)) != EOF) { | | 1262 | while ((c = getc(inf)) != EOF) { |
1282 | if ((c == '\n') && (--cnt == 0)) | | 1263 | if ((c == '\n') && (--cnt == 0)) |
1283 | break; | | 1264 | break; |
1284 | } | | 1265 | } |
1285 | if (c == EOF) { | | 1266 | if (c == EOF) { |
1286 | if (inf != stdin) | | 1267 | if (inf != stdin) |
1287 | (void)fclose(inf); | | 1268 | (void)fclose(inf); |
1288 | return(1); | | 1269 | return(1); |
1289 | } | | 1270 | } |
1290 | } | | 1271 | } |
1291 | return(0); | | 1272 | return(0); |
1292 | } | | 1273 | } |
1293 | | | 1274 | |
1294 | /* | | 1275 | /* |
1295 | * nxtfile: returns a FILE * to next file in arg list and sets the | | 1276 | * nxtfile: returns a FILE * to next file in arg list and sets the |
1296 | * time field for this file (or current date). | | 1277 | * time field for this file (or current date). |
1297 | * | | 1278 | * |
1298 | * buf array to store proper date for the header. | | 1279 | * buf array to store proper date for the header. |
1299 | * dt if set skips the date processing (used with -m) | | 1280 | * dt if set skips the date processing (used with -m) |
1300 | */ | | 1281 | */ |
1301 | FILE * | | 1282 | static FILE * |
1302 | nxtfile(argc, argv, fname, buf, dt) | | 1283 | nxtfile(int argc, char **argv, const char **fname, char *buf, int dt) |
1303 | int argc; | | | |
1304 | char **argv; | | | |
1305 | const char **fname; | | | |
1306 | char *buf; | | | |
1307 | int dt; | | | |
1308 | { | | 1284 | { |
1309 | FILE *inf = NULL; | | 1285 | FILE *inf = NULL; |
1310 | struct timeval tv; | | 1286 | struct timeval tv; |
1311 | struct timezone tz; | | 1287 | struct timezone tz; |
1312 | struct tm *timeptr = NULL; | | 1288 | struct tm *timeptr = NULL; |
1313 | struct stat statbuf; | | 1289 | struct stat statbuf; |
1314 | time_t curtime; | | 1290 | time_t curtime; |
1315 | static int twice = -1; | | 1291 | static int twice = -1; |
1316 | | | 1292 | |
1317 | ++twice; | | 1293 | ++twice; |
1318 | if (eoptind >= argc) { | | 1294 | if (eoptind >= argc) { |
1319 | /* | | 1295 | /* |
1320 | * no file listed; default, use standard input | | 1296 | * no file listed; default, use standard input |
1321 | */ | | 1297 | */ |
1322 | if (twice) | | 1298 | if (twice) |
1323 | return(NULL); | | 1299 | return(NULL); |
1324 | clearerr(stdin); | | 1300 | clearerr(stdin); |
1325 | inf = stdin; | | 1301 | inf = stdin; |
1326 | if (header != NULL) | | 1302 | if (header != NULL) |
1327 | *fname = header; | | 1303 | *fname = header; |
1328 | else | | 1304 | else |
1329 | *fname = FNAME; | | 1305 | *fname = FNAME; |
1330 | if (nohead) | | 1306 | if (nohead) |
1331 | return(inf); | | 1307 | return(inf); |
1332 | if (gettimeofday(&tv, &tz) < 0) { | | 1308 | if (gettimeofday(&tv, &tz) < 0) { |
1333 | ++errcnt; | | 1309 | ++errcnt; |
1334 | (void)fprintf(err, "pr: cannot get time of day, %s\n", | | 1310 | (void)fprintf(errf, "pr: cannot get time of day, %s\n", |
1335 | strerror(errno)); | | 1311 | strerror(errno)); |
1336 | eoptind = argc - 1; | | 1312 | eoptind = argc - 1; |
1337 | return(NULL); | | 1313 | return(NULL); |
1338 | } | | 1314 | } |
1339 | curtime = tv.tv_sec; | | 1315 | curtime = tv.tv_sec; |
1340 | timeptr = localtime(&curtime); | | 1316 | timeptr = localtime(&curtime); |
1341 | } | | 1317 | } |
1342 | for (; eoptind < argc; ++eoptind) { | | 1318 | for (; eoptind < argc; ++eoptind) { |
1343 | if (strcmp(argv[eoptind], "-") == 0) { | | 1319 | if (strcmp(argv[eoptind], "-") == 0) { |
1344 | /* | | 1320 | /* |
1345 | * process a "-" for filename | | 1321 | * process a "-" for filename |
1346 | */ | | 1322 | */ |
1347 | clearerr(stdin); | | 1323 | clearerr(stdin); |
1348 | inf = stdin; | | 1324 | inf = stdin; |
1349 | if (header != NULL) | | 1325 | if (header != NULL) |
1350 | *fname = header; | | 1326 | *fname = header; |
1351 | else | | 1327 | else |
1352 | *fname = FNAME; | | 1328 | *fname = FNAME; |
1353 | ++eoptind; | | 1329 | ++eoptind; |
1354 | if (nohead || (dt && twice)) | | 1330 | if (nohead || (dt && twice)) |
1355 | return(inf); | | 1331 | return(inf); |
1356 | if (gettimeofday(&tv, &tz) < 0) { | | 1332 | if (gettimeofday(&tv, &tz) < 0) { |
1357 | ++errcnt; | | 1333 | ++errcnt; |
1358 | (void)fprintf(err, | | 1334 | (void)fprintf(errf, |
1359 | "pr: cannot get time of day, %s\n", | | 1335 | "pr: cannot get time of day, %s\n", |
1360 | strerror(errno)); | | 1336 | strerror(errno)); |
1361 | return(NULL); | | 1337 | return(NULL); |
1362 | } | | 1338 | } |
1363 | curtime = tv.tv_sec; | | 1339 | curtime = tv.tv_sec; |
1364 | timeptr = localtime(&curtime); | | 1340 | timeptr = localtime(&curtime); |
1365 | } else { | | 1341 | } else { |
1366 | /* | | 1342 | /* |
1367 | * normal file processing | | 1343 | * normal file processing |
1368 | */ | | 1344 | */ |
1369 | if ((inf = fopen(argv[eoptind], "r")) == NULL) { | | 1345 | if ((inf = fopen(argv[eoptind], "r")) == NULL) { |
1370 | ++errcnt; | | 1346 | ++errcnt; |
1371 | if (nodiag) | | 1347 | if (nodiag) |
1372 | continue; | | 1348 | continue; |
1373 | (void)fprintf(err, "pr: Cannot open %s, %s\n", | | 1349 | (void)fprintf(errf, "pr: Cannot open %s, %s\n", |
1374 | argv[eoptind], strerror(errno)); | | 1350 | argv[eoptind], strerror(errno)); |
1375 | continue; | | 1351 | continue; |
1376 | } | | 1352 | } |
1377 | if (header != NULL) | | 1353 | if (header != NULL) |
1378 | *fname = header; | | 1354 | *fname = header; |
1379 | else if (dt) | | 1355 | else if (dt) |
1380 | *fname = FNAME; | | 1356 | *fname = FNAME; |
1381 | else | | 1357 | else |
1382 | *fname = argv[eoptind]; | | 1358 | *fname = argv[eoptind]; |
1383 | ++eoptind; | | 1359 | ++eoptind; |
1384 | if (nohead || (dt && twice)) | | 1360 | if (nohead || (dt && twice)) |
1385 | return(inf); | | 1361 | return(inf); |
1386 | | | 1362 | |
1387 | if (dt) { | | 1363 | if (dt) { |
1388 | if (gettimeofday(&tv, &tz) < 0) { | | 1364 | if (gettimeofday(&tv, &tz) < 0) { |
1389 | ++errcnt; | | 1365 | ++errcnt; |
1390 | (void)fprintf(err, | | 1366 | (void)fprintf(errf, |
1391 | "pr: cannot get time of day, %s\n", | | 1367 | "pr: cannot get time of day, %s\n", |
1392 | strerror(errno)); | | 1368 | strerror(errno)); |
1393 | return(NULL); | | 1369 | return(NULL); |
1394 | } | | 1370 | } |
1395 | curtime = tv.tv_sec; | | 1371 | curtime = tv.tv_sec; |
1396 | timeptr = localtime(&curtime); | | 1372 | timeptr = localtime(&curtime); |
1397 | } else { | | 1373 | } else { |
1398 | if (fstat(fileno(inf), &statbuf) < 0) { | | 1374 | if (fstat(fileno(inf), &statbuf) < 0) { |
1399 | ++errcnt; | | 1375 | ++errcnt; |
1400 | (void)fclose(inf); | | 1376 | (void)fclose(inf); |
1401 | (void)fprintf(err, | | 1377 | (void)fprintf(errf, |
1402 | "pr: Cannot stat %s, %s\n", | | 1378 | "pr: Cannot stat %s, %s\n", |
1403 | argv[eoptind], strerror(errno)); | | 1379 | argv[eoptind], strerror(errno)); |
1404 | return(NULL); | | 1380 | return(NULL); |
1405 | } | | 1381 | } |
1406 | timeptr = localtime(&(statbuf.st_mtime)); | | 1382 | timeptr = localtime(&(statbuf.st_mtime)); |
1407 | } | | 1383 | } |
1408 | } | | 1384 | } |
1409 | break; | | 1385 | break; |
1410 | } | | 1386 | } |
1411 | if (inf == NULL) | | 1387 | if (inf == NULL) |
1412 | return(NULL); | | 1388 | return(NULL); |
1413 | | | 1389 | |
1414 | /* | | 1390 | /* |
1415 | * set up time field used in header | | 1391 | * set up time field used in header |
1416 | */ | | 1392 | */ |
1417 | if (strftime(buf, HDBUF, timefrmt, timeptr) <= 0) { | | 1393 | if (strftime(buf, HDBUF, timefrmt, timeptr) <= 0) { |
1418 | ++errcnt; | | 1394 | ++errcnt; |
1419 | if (inf != stdin) | | 1395 | if (inf != stdin) |
1420 | (void)fclose(inf); | | 1396 | (void)fclose(inf); |
1421 | (void)fputs("pr: time conversion failed\n", err); | | 1397 | (void)fputs("pr: time conversion failed\n", errf); |
1422 | return(NULL); | | 1398 | return(NULL); |
1423 | } | | 1399 | } |
1424 | return(inf); | | 1400 | return(inf); |
1425 | } | | 1401 | } |
1426 | | | 1402 | |
1427 | /* | | 1403 | /* |
1428 | * addnum(): adds the line number to the column | | 1404 | * addnum(): adds the line number to the column |
1429 | * Truncates from the front or pads with spaces as required. | | 1405 | * Truncates from the front or pads with spaces as required. |
1430 | * Numbers are right justified. | | 1406 | * Numbers are right justified. |
1431 | * | | 1407 | * |
1432 | * buf buffer to store the number | | 1408 | * buf buffer to store the number |
1433 | * wdth width of buffer to fill | | 1409 | * wdth width of buffer to fill |
1434 | * line line number | | 1410 | * line line number |
1435 | * | | 1411 | * |
1436 | * NOTE: numbers occupy part of the column. The posix | | 1412 | * NOTE: numbers occupy part of the column. The posix |
1437 | * spec does not specify if -i processing should or should not | | 1413 | * spec does not specify if -i processing should or should not |
1438 | * occur on number padding. The spec does say it occupies | | 1414 | * occur on number padding. The spec does say it occupies |
1439 | * part of the column. The usage of addnum currently treats | | 1415 | * part of the column. The usage of addnum currently treats |
1440 | * numbers as part of the column so spaces may be replaced. | | 1416 | * numbers as part of the column so spaces may be replaced. |
1441 | */ | | 1417 | */ |
1442 | void | | 1418 | void |
1443 | addnum(buf, wdth, line) | | 1419 | addnum(char *buf, int wdth, int line) |
1444 | char *buf; | | | |
1445 | int wdth; | | | |
1446 | int line; | | | |
1447 | { | | 1420 | { |
1448 | char *pt = buf + wdth; | | 1421 | char *pt = buf + wdth; |
1449 | | | 1422 | |
1450 | do { | | 1423 | do { |
1451 | *--pt = digs[line % 10]; | | 1424 | *--pt = digs[line % 10]; |
1452 | line /= 10; | | 1425 | line /= 10; |
1453 | } while (line && (pt > buf)); | | 1426 | } while (line && (pt > buf)); |
1454 | | | 1427 | |
1455 | /* | | 1428 | /* |
1456 | * pad with space as required | | 1429 | * pad with space as required |
1457 | */ | | 1430 | */ |
1458 | while (pt > buf) | | 1431 | while (pt > buf) |
1459 | *--pt = ' '; | | 1432 | *--pt = ' '; |
1460 | } | | 1433 | } |
1461 | | | 1434 | |
1462 | /* | | 1435 | /* |
1463 | * prhead(): prints the top of page header | | 1436 | * prhead(): prints the top of page header |
1464 | * | | 1437 | * |
1465 | * buf buffer with time field (and offset) | | 1438 | * buf buffer with time field (and offset) |
1466 | * cnt number of chars in buf | | 1439 | * cnt number of chars in buf |
1467 | * fname fname field for header | | 1440 | * fname fname field for header |
1468 | * pagcnt page number | | 1441 | * pagcnt page number |
1469 | */ | | 1442 | */ |
1470 | int | | 1443 | static int |
1471 | prhead(buf, fname, pagcnt) | | 1444 | prhead(char *buf, const char *fname, int pagcnt) |
1472 | char *buf; | | | |
1473 | const char *fname; | | | |
1474 | int pagcnt; | | | |
1475 | { | | 1445 | { |
1476 | int ips = 0; | | 1446 | int ips = 0; |
1477 | int ops = 0; | | 1447 | int ops = 0; |
1478 | | | 1448 | |
1479 | if ((putchar('\n') == EOF) || (putchar('\n') == EOF)) { | | 1449 | if ((putchar('\n') == EOF) || (putchar('\n') == EOF)) { |
1480 | pfail(); | | 1450 | pfail(); |
1481 | return(1); | | 1451 | return(1); |
1482 | } | | 1452 | } |
1483 | /* | | 1453 | /* |
1484 | * posix is not clear if the header is subject to line length | | 1454 | * posix is not clear if the header is subject to line length |
1485 | * restrictions. The specification for header line format | | 1455 | * restrictions. The specification for header line format |
1486 | * in the spec clearly does not limit length. No pr currently | | 1456 | * in the spec clearly does not limit length. No pr currently |
1487 | * restricts header length. However if we need to truncate in | | 1457 | * restricts header length. However if we need to truncate in |
| @@ -1495,30 +1465,28 @@ prhead(buf, fname, pagcnt) | | | @@ -1495,30 +1465,28 @@ prhead(buf, fname, pagcnt) |
1495 | if (offst && otln(buf, offst, &ips, &ops, -1)) | | 1465 | if (offst && otln(buf, offst, &ips, &ops, -1)) |
1496 | return(1); | | 1466 | return(1); |
1497 | (void)printf(HDFMT,buf+offst, fname, pagcnt); | | 1467 | (void)printf(HDFMT,buf+offst, fname, pagcnt); |
1498 | return(0); | | 1468 | return(0); |
1499 | } | | 1469 | } |
1500 | | | 1470 | |
1501 | /* | | 1471 | /* |
1502 | * prtail(): pad page with empty lines (if required) and print page trailer | | 1472 | * prtail(): pad page with empty lines (if required) and print page trailer |
1503 | * if requested | | 1473 | * if requested |
1504 | * | | 1474 | * |
1505 | * cnt number of lines of padding needed | | 1475 | * cnt number of lines of padding needed |
1506 | * incomp was a '\n' missing from last line output | | 1476 | * incomp was a '\n' missing from last line output |
1507 | */ | | 1477 | */ |
1508 | int | | 1478 | static int |
1509 | prtail(cnt, incomp) | | 1479 | prtail(int cnt, int incomp) |
1510 | int cnt; | | | |
1511 | int incomp; | | | |
1512 | { | | 1480 | { |
1513 | if (nohead) { | | 1481 | if (nohead) { |
1514 | /* | | 1482 | /* |
1515 | * only pad with no headers when incomplete last line | | 1483 | * only pad with no headers when incomplete last line |
1516 | */ | | 1484 | */ |
1517 | if (!incomp) | | 1485 | if (!incomp) |
1518 | return(0); | | 1486 | return(0); |
1519 | if ((dspace && (putchar('\n') == EOF)) || | | 1487 | if ((dspace && (putchar('\n') == EOF)) || |
1520 | (putchar('\n') == EOF)) { | | 1488 | (putchar('\n') == EOF)) { |
1521 | pfail(); | | 1489 | pfail(); |
1522 | return(1); | | 1490 | return(1); |
1523 | } | | 1491 | } |
1524 | return(0); | | 1492 | return(0); |
| @@ -1550,306 +1518,303 @@ prtail(cnt, incomp) | | | @@ -1550,306 +1518,303 @@ prtail(cnt, incomp) |
1550 | cnt += TAILLEN; | | 1518 | cnt += TAILLEN; |
1551 | while (--cnt >= 0) { | | 1519 | while (--cnt >= 0) { |
1552 | if (putchar('\n') == EOF) { | | 1520 | if (putchar('\n') == EOF) { |
1553 | pfail(); | | 1521 | pfail(); |
1554 | return(1); | | 1522 | return(1); |
1555 | } | | 1523 | } |
1556 | } | | 1524 | } |
1557 | return(0); | | 1525 | return(0); |
1558 | } | | 1526 | } |
1559 | | | 1527 | |
1560 | /* | | 1528 | /* |
1561 | * terminate(): when a SIGINT is recvd | | 1529 | * terminate(): when a SIGINT is recvd |
1562 | */ | | 1530 | */ |
1563 | void | | 1531 | static void |
1564 | terminate(which_sig) | | 1532 | terminate(int which_sig) |
1565 | int which_sig; | | | |
1566 | { | | 1533 | { |
1567 | flsh_errs(); | | 1534 | flsh_errs(); |
1568 | (void)raise_default_signal(which_sig); | | 1535 | (void)raise_default_signal(which_sig); |
1569 | exit(1); | | 1536 | exit(1); |
1570 | } | | 1537 | } |
1571 | | | 1538 | |
1572 | | | 1539 | |
1573 | /* | | 1540 | /* |
1574 | * flsh_errs(): output saved up diagnostic messages after all normal | | 1541 | * flsh_errs(): output saved up diagnostic messages after all normal |
1575 | * processing has completed | | 1542 | * processing has completed |
1576 | */ | | 1543 | */ |
1577 | void | | 1544 | static void |
1578 | flsh_errs() | | 1545 | flsh_errs(void) |
1579 | { | | 1546 | { |
1580 | char buf[BUFSIZ]; | | 1547 | char buf[BUFSIZ]; |
1581 | | | 1548 | |
1582 | (void)fflush(stdout); | | 1549 | (void)fflush(stdout); |
1583 | (void)fflush(err); | | 1550 | (void)fflush(errf); |
1584 | if (err == stderr) | | 1551 | if (errf == stderr) |
1585 | return; | | 1552 | return; |
1586 | rewind(err); | | 1553 | rewind(errf); |
1587 | while (fgets(buf, BUFSIZ, err) != NULL) | | 1554 | while (fgets(buf, BUFSIZ, errf) != NULL) |
1588 | (void)fputs(buf, stderr); | | 1555 | (void)fputs(buf, stderr); |
1589 | } | | 1556 | } |
1590 | | | 1557 | |
1591 | void | | 1558 | static void |
1592 | mfail() | | 1559 | mfail(void) |
1593 | { | | 1560 | { |
1594 | (void)fputs("pr: memory allocation failed\n", err); | | 1561 | (void)fputs("pr: memory allocation failed\n", errf); |
1595 | } | | 1562 | } |
1596 | | | 1563 | |
1597 | void | | 1564 | static void |
1598 | pfail() | | 1565 | pfail(void) |
1599 | { | | 1566 | { |
1600 | (void)fprintf(err, "pr: write failure, %s\n", strerror(errno)); | | 1567 | (void)fprintf(errf, "pr: write failure, %s\n", strerror(errno)); |
1601 | } | | 1568 | } |
1602 | | | 1569 | |
1603 | void | | 1570 | static void |
1604 | usage() | | 1571 | usage(void) |
1605 | { | | 1572 | { |
1606 | (void)fputs( | | 1573 | (void)fputs( |
1607 | "usage: pr [+page] [-col] [-adFmrt] [-e[ch][gap]] [-h header]\n",err); | | 1574 | "usage: pr [+page] [-col] [-adFmrt] [-e[ch][gap]] [-h header]\n",errf); |
1608 | (void)fputs( | | 1575 | (void)fputs( |
1609 | " [-i[ch][gap]] [-l line] [-n[ch][width]] [-o offset]\n",err); | | 1576 | " [-i[ch][gap]] [-l line] [-n[ch][width]] [-o offset]\n",errf); |
1610 | (void)fputs( | | 1577 | (void)fputs( |
1611 | " [-s[ch]] [-w width] [-] [file ...]\n", err); | | 1578 | " [-s[ch]] [-w width] [-] [file ...]\n", errf); |
1612 | } | | 1579 | } |
1613 | | | 1580 | |
1614 | /* | | 1581 | /* |
1615 | * setup: Validate command args, initialize and perform sanity | | 1582 | * setup: Validate command args, initialize and perform sanity |
1616 | * checks on options | | 1583 | * checks on options |
1617 | */ | | 1584 | */ |
1618 | int | | 1585 | static int |
1619 | setup(argc, argv) | | 1586 | setup(int argc, char **argv) |
1620 | int argc; | | | |
1621 | char **argv; | | | |
1622 | { | | 1587 | { |
1623 | int c; | | 1588 | int c; |
1624 | int eflag = 0; | | 1589 | int eflag = 0; |
1625 | int iflag = 0; | | 1590 | int iflag = 0; |
1626 | int wflag = 0; | | 1591 | int wflag = 0; |
1627 | int cflag = 0; | | 1592 | int cflag = 0; |
1628 | | | 1593 | |
1629 | if (isatty(fileno(stdout))) { | | 1594 | if (isatty(fileno(stdout))) { |
1630 | /* | | 1595 | /* |
1631 | * defer diagnostics until processing is done | | 1596 | * defer diagnostics until processing is done |
1632 | */ | | 1597 | */ |
1633 | if ((err = tmpfile()) == NULL) { | | 1598 | if ((errf = tmpfile()) == NULL) { |
1634 | (void)fputs("Cannot defer diagnostic messages\n",stderr); | | 1599 | (void)fputs("Cannot defer diagnostic messages\n",stderr); |
1635 | return(1); | | 1600 | return(1); |
1636 | } | | 1601 | } |
1637 | } else | | 1602 | } else |
1638 | err = stderr; | | 1603 | errf = stderr; |
1639 | while ((c = egetopt(argc, argv, "#adFmrte?h:i?l:n?o:s?T:w:")) != -1) { | | 1604 | while ((c = egetopt(argc, argv, "#adFmrte?h:i?l:n?o:s?T:w:")) != -1) { |
1640 | switch (c) { | | 1605 | switch (c) { |
1641 | case '+': | | 1606 | case '+': |
1642 | if ((pgnm = atoi(eoptarg)) < 1) { | | 1607 | if ((pgnm = atoi(eoptarg)) < 1) { |
1643 | (void)fputs("pr: +page number must be 1 or more\n", | | 1608 | (void)fputs("pr: +page number must be 1 or more\n", |
1644 | err); | | 1609 | errf); |
1645 | return(1); | | 1610 | return(1); |
1646 | } | | 1611 | } |
1647 | break; | | 1612 | break; |
1648 | case '-': | | 1613 | case '-': |
1649 | if ((clcnt = atoi(eoptarg)) < 1) { | | 1614 | if ((clcnt = atoi(eoptarg)) < 1) { |
1650 | (void)fputs("pr: -columns must be 1 or more\n",err); | | 1615 | (void)fputs("pr: -columns must be 1 or more\n",errf); |
1651 | return(1); | | 1616 | return(1); |
1652 | } | | 1617 | } |
1653 | if (clcnt > 1) | | 1618 | if (clcnt > 1) |
1654 | ++cflag; | | 1619 | ++cflag; |
1655 | break; | | 1620 | break; |
1656 | case 'a': | | 1621 | case 'a': |
1657 | ++across; | | 1622 | ++across; |
1658 | break; | | 1623 | break; |
1659 | case 'd': | | 1624 | case 'd': |
1660 | ++dspace; | | 1625 | ++dspace; |
1661 | break; | | 1626 | break; |
1662 | case 'e': | | 1627 | case 'e': |
1663 | ++eflag; | | 1628 | ++eflag; |
1664 | if ((eoptarg != NULL) && | | 1629 | if ((eoptarg != NULL) && |
1665 | !isdigit((unsigned char)*eoptarg)) | | 1630 | !isdigit((unsigned char)*eoptarg)) |
1666 | inchar = *eoptarg++; | | 1631 | inchar = *eoptarg++; |
1667 | else | | 1632 | else |
1668 | inchar = INCHAR; | | 1633 | inchar = INCHAR; |
1669 | if ((eoptarg != NULL) && | | 1634 | if ((eoptarg != NULL) && |
1670 | isdigit((unsigned char)*eoptarg)) { | | 1635 | isdigit((unsigned char)*eoptarg)) { |
1671 | if ((ingap = atoi(eoptarg)) < 0) { | | 1636 | if ((ingap = atoi(eoptarg)) < 0) { |
1672 | (void)fputs( | | 1637 | (void)fputs( |
1673 | "pr: -e gap must be 0 or more\n", err); | | 1638 | "pr: -e gap must be 0 or more\n", errf); |
1674 | return(1); | | 1639 | return(1); |
1675 | } | | 1640 | } |
1676 | if (ingap == 0) | | 1641 | if (ingap == 0) |
1677 | ingap = INGAP; | | 1642 | ingap = INGAP; |
1678 | } else if ((eoptarg != NULL) && (*eoptarg != '\0')) { | | 1643 | } else if ((eoptarg != NULL) && (*eoptarg != '\0')) { |
1679 | (void)fprintf(err, | | 1644 | (void)fprintf(errf, |
1680 | "pr: invalid value for -e %s\n", eoptarg); | | 1645 | "pr: invalid value for -e %s\n", eoptarg); |
1681 | return(1); | | 1646 | return(1); |
1682 | } else | | 1647 | } else |
1683 | ingap = INGAP; | | 1648 | ingap = INGAP; |
1684 | break; | | 1649 | break; |
1685 | case 'F': | | 1650 | case 'F': |
1686 | ++formfeed; | | 1651 | ++formfeed; |
1687 | break; | | 1652 | break; |
1688 | case 'h': | | 1653 | case 'h': |
1689 | header = eoptarg; | | 1654 | header = eoptarg; |
1690 | break; | | 1655 | break; |
1691 | case 'i': | | 1656 | case 'i': |
1692 | ++iflag; | | 1657 | ++iflag; |
1693 | if ((eoptarg != NULL) && | | 1658 | if ((eoptarg != NULL) && |
1694 | !isdigit((unsigned char)*eoptarg)) | | 1659 | !isdigit((unsigned char)*eoptarg)) |
1695 | ochar = *eoptarg++; | | 1660 | ochar = *eoptarg++; |
1696 | else | | 1661 | else |
1697 | ochar = OCHAR; | | 1662 | ochar = OCHAR; |
1698 | if ((eoptarg != NULL) && | | 1663 | if ((eoptarg != NULL) && |
1699 | isdigit((unsigned char)*eoptarg)) { | | 1664 | isdigit((unsigned char)*eoptarg)) { |
1700 | if ((ogap = atoi(eoptarg)) < 0) { | | 1665 | if ((ogap = atoi(eoptarg)) < 0) { |
1701 | (void)fputs( | | 1666 | (void)fputs( |
1702 | "pr: -i gap must be 0 or more\n", err); | | 1667 | "pr: -i gap must be 0 or more\n", errf); |
1703 | return(1); | | 1668 | return(1); |
1704 | } | | 1669 | } |
1705 | if (ogap == 0) | | 1670 | if (ogap == 0) |
1706 | ogap = OGAP; | | 1671 | ogap = OGAP; |
1707 | } else if ((eoptarg != NULL) && (*eoptarg != '\0')) { | | 1672 | } else if ((eoptarg != NULL) && (*eoptarg != '\0')) { |
1708 | (void)fprintf(err, | | 1673 | (void)fprintf(errf, |
1709 | "pr: invalid value for -i %s\n", eoptarg); | | 1674 | "pr: invalid value for -i %s\n", eoptarg); |
1710 | return(1); | | 1675 | return(1); |
1711 | } else | | 1676 | } else |
1712 | ogap = OGAP; | | 1677 | ogap = OGAP; |
1713 | break; | | 1678 | break; |
1714 | case 'l': | | 1679 | case 'l': |
1715 | if (!isdigit((unsigned char)*eoptarg) || | | 1680 | if (!isdigit((unsigned char)*eoptarg) || |
1716 | ((lines=atoi(eoptarg)) < 1)) { | | 1681 | ((lines=atoi(eoptarg)) < 1)) { |
1717 | (void)fputs( | | 1682 | (void)fputs( |
1718 | "pr: Number of lines must be 1 or more\n",err); | | 1683 | "pr: Number of lines must be 1 or more\n",errf); |
1719 | return(1); | | 1684 | return(1); |
1720 | } | | 1685 | } |
1721 | break; | | 1686 | break; |
1722 | case 'm': | | 1687 | case 'm': |
1723 | ++merge; | | 1688 | ++merge; |
1724 | break; | | 1689 | break; |
1725 | case 'n': | | 1690 | case 'n': |
1726 | if ((eoptarg != NULL) && | | 1691 | if ((eoptarg != NULL) && |
1727 | !isdigit((unsigned char)*eoptarg)) | | 1692 | !isdigit((unsigned char)*eoptarg)) |
1728 | nmchar = *eoptarg++; | | 1693 | nmchar = *eoptarg++; |
1729 | else | | 1694 | else |
1730 | nmchar = NMCHAR; | | 1695 | nmchar = NMCHAR; |
1731 | if ((eoptarg != NULL) && | | 1696 | if ((eoptarg != NULL) && |
1732 | isdigit((unsigned char)*eoptarg)) { | | 1697 | isdigit((unsigned char)*eoptarg)) { |
1733 | if ((nmwd = atoi(eoptarg)) < 1) { | | 1698 | if ((nmwd = atoi(eoptarg)) < 1) { |
1734 | (void)fputs( | | 1699 | (void)fputs( |
1735 | "pr: -n width must be 1 or more\n",err); | | 1700 | "pr: -n width must be 1 or more\n",errf); |
1736 | return(1); | | 1701 | return(1); |
1737 | } | | 1702 | } |
1738 | } else if ((eoptarg != NULL) && (*eoptarg != '\0')) { | | 1703 | } else if ((eoptarg != NULL) && (*eoptarg != '\0')) { |
1739 | (void)fprintf(err, | | 1704 | (void)fprintf(errf, |
1740 | "pr: invalid value for -n %s\n", eoptarg); | | 1705 | "pr: invalid value for -n %s\n", eoptarg); |
1741 | return(1); | | 1706 | return(1); |
1742 | } else | | 1707 | } else |
1743 | nmwd = NMWD; | | 1708 | nmwd = NMWD; |
1744 | break; | | 1709 | break; |
1745 | case 'o': | | 1710 | case 'o': |
1746 | if (!isdigit((unsigned char)*eoptarg) || | | 1711 | if (!isdigit((unsigned char)*eoptarg) || |
1747 | ((offst = atoi(eoptarg))< 1)){ | | 1712 | ((offst = atoi(eoptarg))< 1)){ |
1748 | (void)fputs("pr: -o offset must be 1 or more\n", | | 1713 | (void)fputs("pr: -o offset must be 1 or more\n", |
1749 | err); | | 1714 | errf); |
1750 | return(1); | | 1715 | return(1); |
1751 | } | | 1716 | } |
1752 | break; | | 1717 | break; |
1753 | case 'r': | | 1718 | case 'r': |
1754 | ++nodiag; | | 1719 | ++nodiag; |
1755 | break; | | 1720 | break; |
1756 | case 's': | | 1721 | case 's': |
1757 | ++sflag; | | 1722 | ++sflag; |
1758 | if (eoptarg == NULL) | | 1723 | if (eoptarg == NULL) |
1759 | schar = SCHAR; | | 1724 | schar = SCHAR; |
1760 | else | | 1725 | else |
1761 | schar = *eoptarg++; | | 1726 | schar = *eoptarg++; |
1762 | if (eoptarg && *eoptarg != '\0') { | | 1727 | if (eoptarg && *eoptarg != '\0') { |
1763 | (void)fprintf(err, | | 1728 | (void)fprintf(errf, |
1764 | "pr: invalid value for -s %s\n", eoptarg); | | 1729 | "pr: invalid value for -s %s\n", eoptarg); |
1765 | return(1); | | 1730 | return(1); |
1766 | } | | 1731 | } |
1767 | break; | | 1732 | break; |
1768 | case 'T': | | 1733 | case 'T': |
1769 | timefrmt = eoptarg; | | 1734 | timefrmt = eoptarg; |
1770 | break; | | 1735 | break; |
1771 | case 't': | | 1736 | case 't': |
1772 | ++nohead; | | 1737 | ++nohead; |
1773 | break; | | 1738 | break; |
1774 | case 'w': | | 1739 | case 'w': |
1775 | ++wflag; | | 1740 | ++wflag; |
1776 | if (!isdigit((unsigned char)*eoptarg) || | | 1741 | if (!isdigit((unsigned char)*eoptarg) || |
1777 | ((pgwd = atoi(eoptarg)) < 1)){ | | 1742 | ((pgwd = atoi(eoptarg)) < 1)){ |
1778 | (void)fputs( | | 1743 | (void)fputs( |
1779 | "pr: -w width must be 1 or more \n",err); | | 1744 | "pr: -w width must be 1 or more \n",errf); |
1780 | return(1); | | 1745 | return(1); |
1781 | } | | 1746 | } |
1782 | break; | | 1747 | break; |
1783 | case '?': | | 1748 | case '?': |
1784 | default: | | 1749 | default: |
1785 | return(1); | | 1750 | return(1); |
1786 | } | | 1751 | } |
1787 | } | | 1752 | } |
1788 | | | 1753 | |
1789 | /* | | 1754 | /* |
1790 | * default and sanity checks | | 1755 | * default and sanity checks |
1791 | */ | | 1756 | */ |
1792 | if (!clcnt) { | | 1757 | if (!clcnt) { |
1793 | if (merge) { | | 1758 | if (merge) { |
1794 | if ((clcnt = argc - eoptind) <= 1) { | | 1759 | if ((clcnt = argc - eoptind) <= 1) { |
1795 | clcnt = CLCNT; | | 1760 | clcnt = CLCNT; |
1796 | merge = 0; | | 1761 | merge = 0; |
1797 | } | | 1762 | } |
1798 | } else | | 1763 | } else |
1799 | clcnt = CLCNT; | | 1764 | clcnt = CLCNT; |
1800 | } | | 1765 | } |
1801 | if (across) { | | 1766 | if (across) { |
1802 | if (clcnt == 1) { | | 1767 | if (clcnt == 1) { |
1803 | (void)fputs("pr: -a flag requires multiple columns\n", | | 1768 | (void)fputs("pr: -a flag requires multiple columns\n", |
1804 | err); | | 1769 | errf); |
1805 | return(1); | | 1770 | return(1); |
1806 | } | | 1771 | } |
1807 | if (merge) { | | 1772 | if (merge) { |
1808 | (void)fputs("pr: -m cannot be used with -a\n", err); | | 1773 | (void)fputs("pr: -m cannot be used with -a\n", errf); |
1809 | return(1); | | 1774 | return(1); |
1810 | } | | 1775 | } |
1811 | } | | 1776 | } |
1812 | if (!wflag) { | | 1777 | if (!wflag) { |
1813 | if (sflag) | | 1778 | if (sflag) |
1814 | pgwd = SPGWD; | | 1779 | pgwd = SPGWD; |
1815 | else | | 1780 | else |
1816 | pgwd = PGWD; | | 1781 | pgwd = PGWD; |
1817 | } | | 1782 | } |
1818 | if (cflag || merge) { | | 1783 | if (cflag || merge) { |
1819 | if (!eflag) { | | 1784 | if (!eflag) { |
1820 | inchar = INCHAR; | | 1785 | inchar = INCHAR; |
1821 | ingap = INGAP; | | 1786 | ingap = INGAP; |
1822 | } | | 1787 | } |
1823 | if (!iflag) { | | 1788 | if (!iflag) { |
1824 | ochar = OCHAR; | | 1789 | ochar = OCHAR; |
1825 | ogap = OGAP; | | 1790 | ogap = OGAP; |
1826 | } | | 1791 | } |
1827 | } | | 1792 | } |
1828 | if (cflag) { | | 1793 | if (cflag) { |
1829 | if (merge) { | | 1794 | if (merge) { |
1830 | (void)fputs( | | 1795 | (void)fputs( |
1831 | "pr: -m cannot be used with multiple columns\n", err); | | 1796 | "pr: -m cannot be used with multiple columns\n", errf); |
1832 | return(1); | | 1797 | return(1); |
1833 | } | | 1798 | } |
1834 | if (nmwd) { | | 1799 | if (nmwd) { |
1835 | colwd = (pgwd + 1 - (clcnt * (nmwd + 2)))/clcnt; | | 1800 | colwd = (pgwd + 1 - (clcnt * (nmwd + 2)))/clcnt; |
1836 | pgwd = ((colwd + nmwd + 2) * clcnt) - 1; | | 1801 | pgwd = ((colwd + nmwd + 2) * clcnt) - 1; |
1837 | } else { | | 1802 | } else { |
1838 | colwd = (pgwd + 1 - clcnt)/clcnt; | | 1803 | colwd = (pgwd + 1 - clcnt)/clcnt; |
1839 | pgwd = ((colwd + 1) * clcnt) - 1; | | 1804 | pgwd = ((colwd + 1) * clcnt) - 1; |
1840 | } | | 1805 | } |
1841 | if (colwd < 1) { | | 1806 | if (colwd < 1) { |
1842 | (void)fprintf(err, | | 1807 | (void)fprintf(errf, |
1843 | "pr: page width is too small for %d columns\n",clcnt); | | 1808 | "pr: page width is too small for %d columns\n",clcnt); |
1844 | return(1); | | 1809 | return(1); |
1845 | } | | 1810 | } |
1846 | } | | 1811 | } |
1847 | if (!lines) | | 1812 | if (!lines) |
1848 | lines = LINES; | | 1813 | lines = LINES; |
1849 | | | 1814 | |
1850 | /* | | 1815 | /* |
1851 | * make sure long enough for headers. if not disable | | 1816 | * make sure long enough for headers. if not disable |
1852 | */ | | 1817 | */ |
1853 | if (lines <= HEADLEN + TAILLEN) | | 1818 | if (lines <= HEADLEN + TAILLEN) |
1854 | ++nohead; | | 1819 | ++nohead; |
1855 | else if (!nohead) | | 1820 | else if (!nohead) |