summaryrefslogtreecommitdiff
path: root/usr/src/cmd/pr/pr.c
blob: c16620edb83219e472efb5e6ad3e02682ede9b75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 *	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T
 *	All Rights Reserved
 */

/*
 * University Copyright- Copyright (c) 1982, 1986, 1988
 * The Regents of the University of California
 * All Rights Reserved
 *
 * University Acknowledgment- Portions of this document are derived from
 * software developed by the University of California, Berkeley, and its
 * contributors.
 */

/*
 *	PR command (print files in pages and columns, with headings)
 *	2+head+2+page[56]+5
 */

#include <stdio.h>
#include <signal.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <locale.h>
#include <string.h>
#include <limits.h>
#include <wchar.h>
#include <errno.h>

#define	ESC		'\033'
#define	LENGTH		66
#define	LINEW		72
#define	NUMW		5
#define	MARGIN		10
#define	DEFTAB		8
#define	NFILES		10
#define	STDINNAME()	nulls
#define	PROMPT()	(void) putc('\7', stderr) /* BEL */
#define	NOFILE		nulls
#define	ETABS		(Inpos % Etabn)
#define	NSEPC		'\t'
#define	HEAD		gettext("%s  %s Page %d\n\n\n"), date, head, Page
#define	cerror(S)	(void) fprintf(stderr, "pr: %s", gettext(S))
#define	done()		if (Ttyout) (void) chmod(Ttyout, Mode)
#define	ALL_NUMS(s)	(strspn(s, "0123456789") == strlen(s))
#define	REMOVE_ARG(argc, argp)					\
			{					\
				char	**p = argp;		\
				while (*p != NULL)		\
				{				\
					*p = *(p + 1);		\
					p++;			\
				}				\
				argc--;				\
			}
#define	SQUEEZE_ARG(argp, ind, n)					\
			{					\
				int	i;			\
				for (i = ind; argp[i]; i++)	\
					argp[i] = argp[i + n];	\
			}

/*
 *   ---date time format---
 *   b -- abbreviated month name
 *   e -- day of month
 *   H -- Hour (24 hour version)
 *   M -- Minute
 *   Y -- Year in the form ccyy
 */
#define	FORMAT		"%b %e %H:%M %Y"

typedef	int	ANY;
typedef	unsigned	int	UNS;
typedef	struct	{ FILE *f_f; char *f_name; wchar_t f_nextc; } FILS;
typedef	struct	{int fold; int skip; int eof; } foldinf;
typedef	struct	{ wchar_t *c_ptr, *c_ptr0; long c_lno; int c_skip; } *COLP;
typedef struct	err { struct err *e_nextp; char *e_mess; } ERR;

/*
 * Global data.
 */
static	FILS	*Files;
static	mode_t	Mode;
static	int	Multi = 0;
static	int	Nfiles = 0;
static	int	Error = 0;
static	char	nulls[] = "";
static	char	*Ttyout;
static	char	obuf[BUFSIZ];
static	char	time_buf[50];	/* array to hold the time and date */
static	long	Lnumb = 0;
static	FILE	*Ttyin = stdin;
static	int	Dblspace = 1;
static	int	Fpage = 1;
static	int	Formfeed = 0;
static	int	Length = LENGTH;
static	int	Linew = 0;
static	int	Offset = 0;
static	int	Ncols = 0;
static	int	Pause = 0;
static	wchar_t	Sepc = 0;
static	int	Colw;
static	int	Plength;
static	int	Margin = MARGIN;
static	int	Numw;
static	int	Nsepc = NSEPC;
static	int	Report = 1;
static	int	Etabn = 0;
static	wchar_t	Etabc = '\t';
static	int	Itabn = 0;
static	wchar_t	Itabc = '\t';
static	int	fold = 0;
static	int	foldcol = 0;
static	int	alleof = 0;
static	char	*Head = NULL;
static	wchar_t *Buffer = NULL, *Bufend, *Bufptr;
static	UNS	Buflen;
static	COLP	Colpts;
static	foldinf	*Fcol;
static	int	Page;
static	wchar_t	C = '\0';
static	int	Nspace;
static	int	Inpos;
static	int	Outpos;
static	int	Lcolpos;
static	int	Pcolpos;
static	int	Line;
static	ERR	*Err = NULL;
static	ERR	*Lasterr = (ERR *)&Err;
static	int	mbcurmax = 1;

/*
 * Function prototypes.
 */
static	void	onintr();
static	ANY	*getspace();
static	int	findopt(int, char **);
static	void	fixtty();
static	char 	*GETDATE();
static	char	*ffiler(char *);
static	int	print(char *);
static	void	putpage();
static	void	foldpage();
static	void	nexbuf();
static	void	foldbuf();
static	void	balance(int);
static	int	readbuf(wchar_t **, int, COLP);
static	wint_t	get(int);
static	int	put(wchar_t);
static	void	putspace();
static	void	unget(int);
static	FILE	*mustopen(char *, FILS *);
static	void	die(char *);
static	void	errprint();
static	void	usage(int);
static wint_t	_fgetwc_pr(FILE *, int *);
static size_t	freadw(wchar_t *, size_t, FILE *);


int
main(int argc, char **argv)
{
	FILS	fstr[NFILES];
	int	nfdone = 0;


	/* Get locale variables for environment */
	(void) setlocale(LC_ALL, "");

#if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
#define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
#endif
	(void) textdomain(TEXT_DOMAIN);

	mbcurmax = MB_CUR_MAX;
	Files = fstr;
	for (argc = findopt(argc, argv); argc > 0; --argc, ++argv) {
		if (Multi == 'm') {
			if (Nfiles >= NFILES - 1) die("too many files");
			if (mustopen(*argv, &Files[Nfiles++]) == NULL)
				++nfdone;	/* suppress printing */
		} else {
			if (print(*argv))
				(void) fclose(Files->f_f);
			++nfdone;
		}
	}
	if (!nfdone)	/* no files named, use stdin */
		(void) print(NOFILE);	/* on GCOS, use current file, if any */

	if (Report) {
		errprint();	/* print accumulated error reports */
		exit(Error);
	}

	return (Error);
}


/*
 * findopt() returns argc modified to be the number of explicitly supplied
 * filenames, including '-', the explicit request to use stdin.
 * argc == 0 implies that no filenames were supplied and stdin should be used.
 * Options are striped from argv and only file names are returned.
 */

static	int
findopt(int argc, char **argv)
{
	int	eargc = 0;
	int	c;
	int	mflg = 0;
	int	aflg = 0;
	int	optnum;
	int	argv_ind;
	int	end_opt;
	int	i;
	int	isarg = 0;

	fixtty();

	/* Handle page number option */
	for (optnum = 1, end_opt = 0; optnum < argc && !end_opt; optnum++) {
		switch (*argv[optnum]) {
		case '+':
			/* check for all digits */
			if (strlen(&argv[optnum][1]) !=
			    strspn(&argv[optnum][1], "0123456789")) {
				(void) fprintf(stderr, gettext(
				    "pr: Badly formed number\n"));
				exit(1);
			}

			if ((Fpage = (int)strtol(&argv[optnum][1],
			    (char **)NULL, 10)) < 0) {
				(void) fprintf(stderr, gettext(
				    "pr: Badly formed number\n"));
				exit(1);
			}
			REMOVE_ARG(argc, &argv[optnum]);
			optnum--;
			break;

		case '-':
			/* Check for end of options */
			if (argv[optnum][1] == '-') {
				end_opt++;
				break;
			}

			if (argv[optnum][1] == 'h' || argv[optnum][1] == 'l' ||
			    argv[optnum][1] == 'o' || argv[optnum][1] == 'w')
				isarg = 1;
			else
				isarg = 0;

			break;

		default:
			if (isarg == 0)
				end_opt++;
			else
				isarg = 0;
			break;
		}
	}

	/*
	 * Handle options with optional arguments.
	 * If optional arguments are present they may not be separated
	 * from the option letter.
	 */

	for (optnum = 1; optnum < argc; optnum++) {
		if (argv[optnum][0] == '-' && argv[optnum][1] == '-')
			/* End of options */
			break;

		if (argv[optnum][0] == '-' && argv[optnum][1] == '\0')
			/* stdin file name */
			continue;

		if (argv[optnum][0] != '-')
			/* not option */
			continue;

		for (argv_ind = 1; argv[optnum][argv_ind] != '\0'; argv_ind++) {
			switch (argv[optnum][argv_ind]) {
			case 'e':
				SQUEEZE_ARG(argv[optnum], argv_ind, 1);
				if ((c = argv[optnum][argv_ind]) != '\0' &&
				    !isdigit(c)) {
					int	r;
					wchar_t	wc;
					r = mbtowc(&wc, &argv[optnum][argv_ind],
						mbcurmax);
					if (r == -1) {
						(void) fprintf(stderr, gettext(
"pr: Illegal character in -e option\n"));
						exit(1);
					}
					Etabc = wc;
					SQUEEZE_ARG(argv[optnum], argv_ind, r);
				}
				if (isdigit(argv[optnum][argv_ind])) {
					Etabn = (int)strtol(&argv[optnum]
					    [argv_ind], (char **)NULL, 10);
					while (isdigit(argv[optnum][argv_ind]))
					    SQUEEZE_ARG(argv[optnum],
							argv_ind, 1);
				}
				if (Etabn <= 0)
					Etabn = DEFTAB;
				argv_ind--;
				break;

			case 'i':
				SQUEEZE_ARG(argv[optnum], argv_ind, 1);
				if ((c = argv[optnum][argv_ind]) != '\0' &&
				    !isdigit(c)) {
					int	r;
					wchar_t	wc;
					r = mbtowc(&wc, &argv[optnum][argv_ind],
						mbcurmax);
					if (r == -1) {
						(void) fprintf(stderr, gettext(
"pr: Illegal character in -i option\n"));
						exit(1);
					}
					Itabc = wc;
					SQUEEZE_ARG(argv[optnum], argv_ind, r);
				}
				if (isdigit(argv[optnum][argv_ind])) {
					Itabn = (int)strtol(&argv[optnum]
					    [argv_ind], (char **)NULL, 10);
					while (isdigit(argv[optnum][argv_ind]))
					    SQUEEZE_ARG(argv[optnum],
							argv_ind, 1);
				}
				if (Itabn <= 0)
					Itabn = DEFTAB;
				argv_ind--;
				break;


			case 'n':
				++Lnumb;
				SQUEEZE_ARG(argv[optnum], argv_ind, 1);
				if ((c = argv[optnum][argv_ind]) != '\0' &&
				    !isdigit(c)) {
					int	r;
					wchar_t	wc;
					r = mbtowc(&wc, &argv[optnum][argv_ind],
						mbcurmax);
					if (r == -1) {
						(void) fprintf(stderr, gettext(
"pr: Illegal character in -n option\n"));
						exit(1);
					}
					Nsepc = wc;
					SQUEEZE_ARG(argv[optnum], argv_ind, r);
				}
				if (isdigit(argv[optnum][argv_ind])) {
					Numw = (int)strtol(&argv[optnum]
					    [argv_ind], (char **)NULL, 10);
					while (isdigit(argv[optnum][argv_ind]))
					    SQUEEZE_ARG(argv[optnum],
							argv_ind, 1);
				}
				argv_ind--;
				if (!Numw)
					Numw = NUMW;
				break;

			case 's':
				SQUEEZE_ARG(argv[optnum], argv_ind, 1);
				if ((Sepc = argv[optnum][argv_ind]) == '\0')
					Sepc = '\t';
				else {
					int	r;
					wchar_t	wc;
					r = mbtowc(&wc, &argv[optnum][argv_ind],
						mbcurmax);
					if (r == -1) {
						(void) fprintf(stderr, gettext(
"pr: Illegal character in -s option\n"));
						exit(1);
					}
					Sepc = wc;
					SQUEEZE_ARG(argv[optnum], argv_ind, r);
				}
				argv_ind--;
				break;

			default:
				break;
			}
		}
		if (argv[optnum][0] == '-' && argv[optnum][1] == '\0') {
			REMOVE_ARG(argc, &argv[optnum]);
			optnum--;
		}
	}

	/* Now get the other options */
	while ((c = getopt(argc, argv, "0123456789adfFh:l:mo:prtw:"))
	    != EOF) {
		switch (c) {
		case '0':
		case '1':
		case '2':
		case '3':
		case '4':
		case '5':
		case '6':
		case '7':
		case '8':
		case '9':
			Ncols *= 10;
			Ncols += c - '0';
			break;

		case 'a':
			aflg++;
			if (!Multi)
				Multi = c;
			break;

		case 'd':
			Dblspace = 2;
			break;

		case 'f':
			++Formfeed;
			++Pause;
			break;

		case 'h':
			Head = optarg;
			break;

		case 'l':
			if (strlen(optarg) != strspn(optarg, "0123456789"))
				usage(1);
			Length = (int)strtol(optarg, (char **)NULL, 10);
			break;

		case 'm':
			mflg++;
			Multi = c;
			break;

		case 'o':
			if (strlen(optarg) != strspn(optarg, "0123456789"))
				usage(1);
			Offset = (int)strtol(optarg, (char **)NULL, 10);
			break;

		case 'p':
			++Pause;
			break;

		case 'r':
			Report = 0;
			break;

		case 't':
			Margin = 0;
			break;

		case 'w':
			if (strlen(optarg) != strspn(optarg, "0123456789"))
				usage(1);
			Linew = (int)strtol(optarg, (char **)NULL, 10);
			break;

		case 'F':
#ifdef	XPG4
			++Formfeed;
#else
			fold++;
#endif
			break;

		case '?':
			usage(2);
			break;

		default :
			usage(2);
		}
	}

	/* Count the file names and strip options */
	for (i = 1; i < argc; i++) {
		/* Check for explicit stdin */
		if ((argv[i][0] == '-') && (argv[i][1] == '\0')) {
			argv[eargc++][0] = '\0';
			REMOVE_ARG(argc, &argv[i]);
			if (i < optind)
				optind--;
		}
	}
	for (i = eargc; optind < argc; i++, optind++) {
		argv[i] = argv[optind];
		eargc++;
	}

	/* Check options */
	if (Ncols == 0)
		Ncols = 1;

	if (mflg && (Ncols > 1)) {
		(void) fprintf(stderr,
		    gettext("pr: only one of either -m or -column allowed\n"));
		usage(1);
	}

	if (Ncols == 1 && fold)
		Multi = 'm';

	if (Length <= 0)
		Length = LENGTH;

	if (Length <= Margin)
		Margin = 0;

	Plength = Length - Margin/2;

	if (Multi == 'm')
		Ncols = eargc;

	switch (Ncols) {
	case 0:
		Ncols = 1;
		break;

	case 1:
		break;

	default:
		if (Etabn == 0)	/* respect explicit tab specification */
			Etabn = DEFTAB;
		if (Itabn == 0)
			Itabn = DEFTAB;
	}

	if ((Fcol = (foldinf *) malloc(sizeof (foldinf) * Ncols)) == NULL) {
		(void) fprintf(stderr, gettext("pr: malloc failed\n"));
		exit(1);
	}
	for (i = 0; i < Ncols; i++)
		Fcol[i].fold = Fcol[i].skip = 0;

	if (Linew == 0)
		Linew = Ncols != 1 && Sepc == 0 ? LINEW : 512;

	if (Lnumb) {
		int numw;

		if (Nsepc == '\t') {
			if (Itabn == 0)
				numw = Numw + DEFTAB - (Numw % DEFTAB);
			else
				numw = Numw + Itabn - (Numw % Itabn);
		} else {
			numw = Numw + ((iswprint(Nsepc)) ? 1 : 0);
		}
		Linew -= (Multi == 'm') ? numw : numw * Ncols;
	}

	if ((Colw = (Linew - Ncols + 1)/Ncols) < 1)
		die("width too small");

	if (Ncols != 1 && Multi == 0) {
		/* Buflen should take the number of wide characters */
		/* Not the size for Buffer */
		Buflen = ((UNS) (Plength / Dblspace + 1)) *
		    2 * (Linew + 1);
		/* Should allocate Buflen * sizeof (wchar_t) */
		Buffer = (wchar_t *)getspace(Buflen * sizeof (wchar_t));
		Bufptr = Bufend = &Buffer[Buflen];
		Colpts = (COLP) getspace((UNS) ((Ncols + 1) *
		    sizeof (*Colpts)));
		Colpts[0].c_lno = 0;
	}

	/* is stdin not a tty? */
	if (Ttyout && (Pause || Formfeed) && !ttyname(fileno(stdin)))
		Ttyin = fopen("/dev/tty", "r");

	return (eargc);
}


static	int
print(char *name)
{
	static	int	notfirst = 0;
	char	*date = NULL;
	char	*head = NULL;
	int	c;

	if (Multi != 'm' && mustopen(name, &Files[0]) == NULL)
		return (0);
	if (Multi == 'm' && Nfiles == 0 && mustopen(name, &Files[0]) == NULL)
		die("cannot open stdin");
	if (Buffer)
		(void) ungetwc(Files->f_nextc, Files->f_f);
	if (Lnumb)
		Lnumb = 1;
	for (Page = 0; ; putpage()) {
		if (C == WEOF && !(fold && Buffer))
			break;
		if (Buffer)
			nexbuf();
		Inpos = 0;
		if (get(0) == WEOF)
			break;
		(void) fflush(stdout);
		if (++Page >= Fpage) {
			/* Pause if -p and not first page */
			if (Ttyout && Pause && !notfirst++) {
				PROMPT();	/* prompt with bell and pause */
				while ((c = getc(Ttyin)) != EOF && c != '\n')
					;
			}
			if (Margin == 0)
				continue;
			if (date == NULL)
				date = GETDATE();
			if (head == NULL)
				head = Head != NULL ? Head :
				    Nfiles < 2 ? Files->f_name : nulls;
			(void) printf("\n\n");
			Nspace = Offset;
			putspace();
			(void) printf(HEAD);
		}
	}
	C = '\0';
	return (1);
}


static	void
putpage()
{
	int	colno;

	if (fold) {
		foldpage();
		return;
	}
	for (Line = Margin / 2; ; (void) get(0)) {
		for (Nspace = Offset, colno = 0, Outpos = 0; C != '\f'; ) {
			if (Lnumb && (C != WEOF) &&
			    (((colno == 0) && (Multi == 'm')) ||
			    (Multi != 'm'))) {
				if (Page >= Fpage) {
					putspace();
					(void) printf("%*ld%wc", Numw, Buffer ?
					    Colpts[colno].c_lno++ :
					    Lnumb, Nsepc);

					/* Move Outpos for number field */
					Outpos += Numw;
					if (Nsepc == '\t')
						Outpos +=
						    DEFTAB - (Outpos % DEFTAB);
					else
						Outpos++;
				}
				++Lnumb;
			}
			for (Lcolpos = 0, Pcolpos = 0;
			    C != '\n' && C != '\f' && C != WEOF;
			    (void) get(colno))
				(void) put(C);

			if ((C == WEOF) || (++colno == Ncols) ||
			    ((C == '\n') && (get(colno) == WEOF)))
				break;

			if (Sepc)
				(void) put(Sepc);
			else if ((Nspace += Colw - Lcolpos + 1) < 1)
				Nspace = 1;
		}

		if (C == WEOF) {
			if (Margin != 0)
				break;
			if (colno != 0)
				(void) put('\n');
			return;
		}
		if (C == '\f')
			break;
		(void) put('\n');
		if (Dblspace == 2 && Line < Plength)
			(void) put('\n');
		if (Line >= Plength)
			break;
	}
	if (Formfeed)
		(void) put('\f');
	else
		while (Line < Length)
			(void) put('\n');
}


static	void
foldpage()
{
	int	colno;
	int	keep;
	int	i;
	int	pLcolpos;
	static	int	sl;

	for (Line = Margin / 2; ; (void) get(0)) {
		for (Nspace = Offset, colno = 0, Outpos = 0; C != '\f'; ) {
			if (Lnumb && Multi == 'm' && foldcol) {
				if (!Fcol[colno].skip) {
					unget(colno);
					putspace();
					if (!colno) {
						for (i = 0; i <= Numw; i++)
							(void) printf(" ");
						(void) printf("%wc", Nsepc);
					}
					for (i = 0; i <= Colw; i++)
						(void) printf(" ");
					(void) put(Sepc);
					if (++colno == Ncols)
						break;
					(void) get(colno);
					continue;
				} else if (!colno)
					Lnumb = sl;
			}

			if (Lnumb && (C != WEOF) &&
			    ((colno == 0 && Multi == 'm') || (Multi != 'm'))) {
				if (Page >= Fpage) {
					putspace();
					if ((foldcol &&
					    Fcol[colno].skip && Multi != 'a') ||
					    (Fcol[0].fold && Multi == 'a') ||
					    (Buffer && Colpts[colno].c_skip)) {
						for (i = 0; i < Numw; i++)
							(void) printf(" ");
						(void) printf("%wc", Nsepc);
						if (Buffer) {
							Colpts[colno].c_lno++;
							Colpts[colno].c_skip =
							    0;
						}
					}
					else
					(void) printf("%*ld%wc", Numw, Buffer ?
					    Colpts[colno].c_lno++ :
					    Lnumb, Nsepc);
				}
				sl = Lnumb++;
			}
			pLcolpos = 0;
			for (Lcolpos = 0, Pcolpos = 0;
			    C != '\n' && C != '\f' && C != WEOF;
			    (void) get(colno)) {
				if (put(C)) {
					unget(colno);
					Fcol[(Multi == 'a') ? 0 : colno].fold
					    = 1;
					break;
				} else if (Multi == 'a') {
					Fcol[0].fold = 0;
				}
				pLcolpos = Lcolpos;
			}
			if (Buffer) {
				alleof = 1;
				for (i = 0; i < Ncols; i++)
					if (!Fcol[i].eof)
						alleof = 0;
				if (alleof || ++colno == Ncols)
					break;
			} else if (C == EOF || ++colno == Ncols)
				break;
			keep = C;
			(void) get(colno);
			if (keep == '\n' && C == WEOF)
				break;
			if (Sepc)
				(void) put(Sepc);
			else if ((Nspace += Colw - pLcolpos + 1) < 1)
				Nspace = 1;
		}
		foldcol = 0;
		if (Lnumb && Multi != 'a') {
			for (i = 0; i < Ncols; i++) {
				Fcol[i].skip = Fcol[i].fold;
				foldcol +=  Fcol[i].fold;
				Fcol[i].fold = 0;
			}
		}
		if (C == WEOF) {
			if (Margin != 0)
				break;
			if (colno != 0)
				(void) put('\n');
			return;
		}
		if (C == '\f')
			break;
		(void) put('\n');
		(void) fflush(stdout);
		if (Dblspace == 2 && Line < Plength)
			(void) put('\n');
		if (Line >= Plength)
			break;
	}
	if (Formfeed)
		(void) put('\f');
	else while (Line < Length)
		(void) put('\n');
}


static	void
nexbuf()
{
	wchar_t	*s = Buffer;
	COLP	p = Colpts;
	int	j;
	int	c;
	int	bline = 0;
	wchar_t	wc;

	if (fold) {
		foldbuf();
		return;
	}
	for (; ; ) {
		p->c_ptr0 = p->c_ptr = s;
		if (p == &Colpts[Ncols])
			return;
		(p++)->c_lno = Lnumb + bline;
		for (j = (Length - Margin)/Dblspace; --j >= 0; ++bline) {
			for (Inpos = 0; ; ) {
				errno = 0;
				wc = _fgetwc_pr(Files->f_f, &c);
				if (wc == WEOF) {
					/* If there is an illegal character, */
					/* handle it as a byte sequence. */
					if (errno == EILSEQ) {
						if (Inpos < Colw - 1) {
							*s = c;
							if (++s >= Bufend)
die("page-buffer overflow");
						}
						Inpos++;
						Error++;
						return;
					} else {
						/* Real EOF */
for (*s = WEOF; p <= &Colpts[Ncols]; ++p)
	p->c_ptr0 = p->c_ptr = s;
						balance(bline);
						return;
					}
				}

				if (isascii(wc)) {
					if (isprint(wc))
						Inpos++;
				} else if (iswprint(wc)) {
					Inpos += wcwidth(wc);
				}

				if (Inpos <= Colw || wc == '\n') {
					*s = wc;
					if (++s >= Bufend)
						die("page-buffer overflow");
				}
				if (wc == '\n')
					break;
				switch (wc) {
				case '\b':
					if (Inpos == 0)
						--s;

					/*FALLTHROUGH*/

				case ESC:
					if (Inpos > 0)
						--Inpos;
				}
			}
		}
	}
}


static	void
foldbuf()
{
	int	num;
	int	i;
	int	colno = 0;
	int	size = Buflen;
	wchar_t	*s;
	wchar_t	*d;
	COLP	p = Colpts;

	for (i = 0; i < Ncols; i++)
		Fcol[i].eof = 0;
	d = Buffer;
	if (Bufptr != Bufend) {
		s = Bufptr;
		while (s < Bufend)
			*d++ = *s++;
		size -= (Bufend - Bufptr);
	}
	Bufptr = Buffer;
	p->c_ptr0 = p->c_ptr = Buffer;
	if (p->c_lno == 0) {
		p->c_lno = Lnumb;
		p->c_skip = 0;
	} else {
		p->c_lno = Colpts[Ncols-1].c_lno;
		p->c_skip = Colpts[Ncols].c_skip;
		if (p->c_skip)
			p->c_lno--;
	}
	if ((num = freadw(d, size, Files->f_f)) != size) {
		for (*(d+num) = WEOF; (++p) <= &Colpts[Ncols]; ) {
			p->c_ptr0 = p->c_ptr = (d+num);
		}
		balance(0);
		return;
	}
	i = (Length - Margin) / Dblspace;
	do {
		(void) readbuf(&Bufptr, i, p++);
	} while (++colno < Ncols);
}


static	void
balance(int bline)	/* line balancing for last page */
{
	wchar_t	*s = Buffer;
	COLP	p = Colpts;
	int	colno = 0;
	int	j;
	int	c;
	int	l;
	int	lines;

	if (!fold) {
		c = bline % Ncols;
		l = (bline + Ncols - 1)/Ncols;
		bline = 0;
		do {
			for (j = 0; j < l; ++j)
				while (*s++ != '\n')
					;
			(++p)->c_lno = Lnumb + (bline += l);
			p->c_ptr0 = p->c_ptr = s;
			if (++colno == c)
				--l;
		} while (colno < Ncols - 1);
	} else {
		lines = readbuf(&s, 0, 0);
		l = (lines + Ncols - 1)/Ncols;
		if (l > ((Length - Margin) / Dblspace)) {
			l = (Length - Margin) / Dblspace;
			c = Ncols;
		} else {
			c = lines % Ncols;
		}
		s = Buffer;
		do {
			(void) readbuf(&s, l, p++);
			if (++colno == c)
				--l;
		} while (colno < Ncols);
		Bufptr = s;
	}
}


static	int
readbuf(wchar_t **s, int lincol, COLP p)
{
	int	lines = 0;
	int	chars = 0;
	int	width;
	int	nls = 0;
	int	move;
	int	skip = 0;
	int	decr = 0;

	width = (Ncols == 1) ? Linew : Colw;
	while (**s != WEOF) {
		switch (**s) {
			case '\n':
				lines++; nls++; chars = 0; skip = 0;
				break;

			case '\b':
			case ESC:
				if (chars) chars--;
				break;

			case '\t':
				move = Itabn - ((chars + Itabn) % Itabn);
				move = (move < width-chars) ? move :
				    width-chars;
				chars += move;
				/* FALLTHROUGH */

			default:
				if (isascii(**s)) {
					if (isprint(**s))
						chars++;
				} else if (iswprint(**s)) {
					chars += wcwidth(**s);
				}
		}
		if (chars > width) {
			lines++;
			skip++;
			decr++;
			chars = 0;
		}
		if (lincol && lines == lincol) {
			(p+1)->c_lno = p->c_lno + nls;
			(++p)->c_skip = skip;
			if (**s == '\n') (*s)++;
			p->c_ptr0 = p->c_ptr = (wchar_t *)*s;
			return (0);
		}
		if (decr)
			decr = 0;
		else
			(*s)++;
	}
	return (lines);
}


static	wint_t
get(int colno)
{
	static	int	peekc = 0;
	COLP	p;
	FILS	*q;
	int	c;
	wchar_t		wc, w;

	if (peekc) {
		peekc = 0;
		wc = Etabc;
	} else if (Buffer) {
		p = &Colpts[colno];
		if (p->c_ptr >= (p+1)->c_ptr0)
			wc = WEOF;
		else if ((wc = *p->c_ptr) != WEOF)
			++p->c_ptr;
		if (fold && wc == WEOF)
			Fcol[colno].eof = 1;
	} else if ((wc =
		(q = &Files[Multi == 'a' ? 0 : colno])->f_nextc) == WEOF) {
		for (q = &Files[Nfiles]; --q >= Files && q->f_nextc == WEOF; )
			;
		if (q >= Files)
			wc = '\n';
	} else {
		errno = 0;
		w = _fgetwc_pr(q->f_f, &c);
		if (w == WEOF && errno == EILSEQ) {
			q->f_nextc = (wchar_t)c;
		} else {
			q->f_nextc = w;
		}
	}

	if (Etabn != 0 && wc == Etabc) {
		++Inpos;
		peekc = ETABS;
		wc = ' ';
		return (C = wc);
	}

	if (wc == WEOF)
		return (C = wc);

	if (isascii(wc)) {
		if (isprint(wc)) {
			Inpos++;
			return (C = wc);
		}
	} else if (iswprint(wc)) {
		Inpos += wcwidth(wc);
		return (C = wc);
	}

	switch (wc) {
	case '\b':
	case ESC:
		if (Inpos > 0)
			--Inpos;
		break;
	case '\f':
		if (Ncols == 1)
			break;
		wc = '\n';
		/* FALLTHROUGH */
	case '\n':
	case '\r':
		Inpos = 0;
		break;
	}
	return (C = wc);
}


static	int
put(wchar_t wc)
{
	int	move = 0;
	int	width = Colw;
	int	sp = Lcolpos;

	if (fold && Ncols == 1)
		width = Linew;

	switch (wc) {
	case ' ':
		/* If column not full or this is separator char */
		if ((!fold && Ncols < 2) || (Lcolpos < width) ||
		    ((Sepc == wc) && (Lcolpos == width))) {
			++Nspace;
			++Lcolpos;
		}
		if (fold && sp == Lcolpos)
			if (Lcolpos >= width)
				return (1);

		return (0);

	case '\t':
		if (Itabn == 0)
			break;

		/* If column not full or this is separator char */
		if ((Lcolpos < width) ||
		    ((Sepc == wc) && (Lcolpos == width))) {
			move = Itabn - ((Lcolpos + Itabn) % Itabn);
			move = (move < width-Lcolpos) ? move : width-Lcolpos;
			Nspace += move;
			Lcolpos += move;
		}
		if (fold && sp == Lcolpos)
			if (Lcolpos >= width)
				return (1);
		return (0);

	case '\b':
		if (Lcolpos == 0)
			return (0);
		if (Nspace > 0) {
			--Nspace;
			--Lcolpos;
			return (0);
		}
		if (Lcolpos > Pcolpos) {
			--Lcolpos;
			return (0);
		}

		/*FALLTHROUGH*/

	case ESC:
		move = -1;
		break;

	case '\n':
		++Line;

		/*FALLTHROUGH*/

	case '\r':
	case '\f':
		Pcolpos = 0;
		Lcolpos = 0;
		Nspace = 0;
		Outpos = 0;
		/* FALLTHROUGH */
	default:
		if (isascii(wc)) {
			if (isprint(wc))
				move = 1;
			else
				move = 0;
		} else if (iswprint(wc)) {
			move = wcwidth(wc);
		} else {
			move = 0;
		}
		break;
	}
	if (Page < Fpage)
		return (0);
	if (Lcolpos > 0 || move > 0)
		Lcolpos += move;

	putspace();

	/* If column not full or this is separator char */
	if ((!fold && Ncols < 2) || (Lcolpos <= width) ||
	    ((Sepc == wc) && (Lcolpos > width))) {
		(void) fputwc(wc, stdout);
		Outpos += move;
		Pcolpos = Lcolpos;
	}

	if (fold && Lcolpos > width)
		return (1);

	return (0);
}


static	void
putspace(void)
{
	int nc = 0;

	for (; Nspace > 0; Outpos += nc, Nspace -= nc) {
#ifdef XPG4
		/* XPG4:  -i:  replace multiple SPACE chars with tab chars */
		if ((Nspace >= 2 && Itabn > 0 &&
			Nspace >= (nc = Itabn - Outpos % Itabn)) && !fold) {
#else
		/* Solaris:  -i:  replace white space with tab chars */
		if ((Itabn > 0 && Nspace >= (nc = Itabn - Outpos % Itabn)) &&
			!fold) {
#endif
			(void) fputwc(Itabc, stdout);
		} else {
			nc = 1;
			(void) putchar(' ');
		}
	}
}


static	void
unget(int colno)
{
	if (Buffer) {
		if (*(Colpts[colno].c_ptr-1) != '\t')
			--(Colpts[colno].c_ptr);
		if (Colpts[colno].c_lno)
			Colpts[colno].c_lno--;
	} else {
		if ((Multi == 'm' && colno == 0) || Multi != 'm')
			if (Lnumb && !foldcol)
				Lnumb--;
		colno = (Multi == 'a') ? 0 : colno;
		(void) ungetwc(Files[colno].f_nextc, Files[colno].f_f);
		Files[colno].f_nextc = C;
	}
}


/*
 * Defer message about failure to open file to prevent messing up
 * alignment of page with tear perforations or form markers.
 * Treat empty file as special case and report as diagnostic.
 */

static	FILE *
mustopen(char *s, FILS *f)
{
	char	*empty_file_msg = gettext("%s -- empty file");
	int	c;

	if (*s == '\0') {
		f->f_name = STDINNAME();
		f->f_f = stdin;
	} else if ((f->f_f = fopen(f->f_name = s, "r")) == NULL) {
		s = ffiler(f->f_name);
		s = strcpy((char *)getspace((UNS) strlen(s) + 1), s);
	}
	if (f->f_f != NULL) {
		errno = 0;
		f->f_nextc = _fgetwc_pr(f->f_f, &c);
		if (f->f_nextc != WEOF) {
			return (f->f_f);
		} else {	/* WEOF */
			if (errno == EILSEQ) {
				f->f_nextc = (wchar_t)c;
				return (f->f_f);
			}
			if (Multi == 'm')
				return (f->f_f);
		}
		(void) sprintf(s = (char *)getspace((UNS) strlen(f->f_name)
		    + 1 + (UNS) strlen(empty_file_msg)),
		    empty_file_msg, f->f_name);
		(void) fclose(f->f_f);
	}
	Error = 1;
	if (Report)
		if (Ttyout) {	/* accumulate error reports */
			Lasterr = Lasterr->e_nextp =
			    (ERR *) getspace((UNS) sizeof (ERR));
			Lasterr->e_nextp = NULL;
			Lasterr->e_mess = s;
		} else {	/* ok to print error report now */
			cerror(s);
			(void) putc('\n', stderr);
		}
	return ((FILE *)NULL);
}


static	ANY *
getspace(UNS n)
{
	ANY *t;

	if ((t = (ANY *) malloc(n)) == NULL)
		die("out of space");
	return (t);
}


static	void
die(char *s)
{
	++Error;
	errprint();
	cerror(s);
	(void) putc('\n', stderr);
	exit(1);

	/*NOTREACHED*/
}


static	void
errprint()	/* print accumulated error reports */
{
	(void) fflush(stdout);
	for (; Err != NULL; Err = Err->e_nextp) {
		cerror(Err->e_mess);
		(void) putc('\n', stderr);
	}
	done();
}


static	void
fixtty()
{
	struct stat sbuf;

	setbuf(stdout, obuf);
	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
		(void) signal(SIGINT, onintr);
	if (Ttyout = ttyname(fileno(stdout))) {	/* is stdout a tty? */
		(void) stat(Ttyout, &sbuf);
		Mode = sbuf.st_mode;		/* save permissions */
		(void) chmod(Ttyout, (S_IREAD|S_IWRITE));
	}
}


static	void
onintr()
{
	++Error;
	errprint();
	_exit(1);
}


static	char *
GETDATE()	/* return date file was last modified */
{
	static	char	*now = NULL;
	static	struct	stat	sbuf;
	static	struct	stat	nbuf;

	if (Nfiles > 1 || Files->f_name == nulls) {
		if (now == NULL) {
			(void) time(&nbuf.st_mtime);
			(void) cftime(time_buf,
				dcgettext(NULL, FORMAT, LC_TIME),
			    &nbuf.st_mtime);
			now = time_buf;
		}
		return (now);
	} else {
		(void) stat(Files->f_name, &sbuf);
		(void) cftime(time_buf, dcgettext(NULL, FORMAT, LC_TIME),
			&sbuf.st_mtime);
		return (time_buf);
	}
}


static	char *
ffiler(char *s)
{
	static char buf[100];

	(void) sprintf(buf, gettext("can't open %s"), s);
	return (buf);
}


static	void
usage(int rc)
{
	(void) fprintf(stderr, gettext(
"usage: pr [-# [-w #] [-a]] [-e[c][#]] [-i[c][#]] [-drtfp] [-n[c][#]]  \\\n"
"          [-o #] [-l #] [-s[char]] [-h header] [-F] [+#] [file ...]\n\n"
"       pr [-m [-w #]] [-e[c][#]] [-i[c][#]] [-drtfp] [-n[c][#]] [-0 #] \\\n"
"          [-l #] [-s[char]] [-h header] [-F] [+#] file1 file2 ...\n"
));
	exit(rc);
}

static wint_t
_fgetwc_pr(FILE *f, int *ic)
{
	int	i;
	int	len;
	char	mbuf[MB_LEN_MAX];
	int	c;
	wchar_t	wc;

	c = getc(f);

	if (c == EOF)
		return (WEOF);
	if (mbcurmax == 1 || isascii(c)) {
		return ((wint_t)c);
	}
	mbuf[0] = (char)c;
	for (i = 1; i < mbcurmax; i++) {
		c = getc(f);
		if (c == EOF) {
			break;
		} else {
			mbuf[i] = (char)c;
		}
	}
	mbuf[i] = 0;

	len = mbtowc(&wc, mbuf, i);
	if (len == -1) {
		/* Illegal character */
		/* Set the first byte to *ic */
		*ic = mbuf[0];
		/* Push back remaining characters */
		for (i--; i > 0; i--) {
			(void) ungetc(mbuf[i], f);
		}
		errno = EILSEQ;
		return (WEOF);
	} else {
		/* Push back over-read characters */
		for (i--; i >= len; i--) {
			(void) ungetc(mbuf[i], f);
		}
		return ((wint_t)wc);
	}
}

static size_t
freadw(wchar_t *ptr, size_t nitems, FILE *f)
{
	size_t	i;
	size_t	ret;
	int	c;
	wchar_t	*p;
	wint_t	wc;

	if (feof(f)) {
		return (0);
	}

	p = ptr;
	ret = 0;
	for (i = 0; i < nitems; i++) {
		errno = 0;
		wc = _fgetwc_pr(f, &c);
		if (wc == WEOF) {
			if (errno == EILSEQ) {
				*p++ = (wchar_t)c;
				ret++;
			} else {
				return (ret);
			}
		} else {
			*p++ = (wchar_t)wc;
			ret++;
		}
	}
	return (ret);
}