Home | History | Annotate | Line # | Download | only in pr
pr.c revision 1.22
      1  1.22  christos /*	$NetBSD: pr.c,v 1.22 2012/03/12 18:06:24 christos Exp $	*/
      2   1.4       tls 
      3   1.1       cgd /*-
      4  1.12       agc  * Copyright (c) 1991 Keith Muller.
      5   1.1       cgd  * Copyright (c) 1993
      6   1.1       cgd  *	The Regents of the University of California.  All rights reserved.
      7   1.1       cgd  *
      8   1.1       cgd  * This code is derived from software contributed to Berkeley by
      9   1.1       cgd  * Keith Muller of the University of California, San Diego.
     10   1.1       cgd  *
     11   1.1       cgd  * Redistribution and use in source and binary forms, with or without
     12   1.1       cgd  * modification, are permitted provided that the following conditions
     13   1.1       cgd  * are met:
     14   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     15   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     16   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     18   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     19  1.11       agc  * 3. Neither the name of the University nor the names of its contributors
     20  1.11       agc  *    may be used to endorse or promote products derived from this software
     21  1.11       agc  *    without specific prior written permission.
     22  1.11       agc  *
     23  1.11       agc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  1.11       agc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  1.11       agc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  1.11       agc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  1.11       agc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  1.11       agc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  1.11       agc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  1.11       agc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  1.11       agc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  1.11       agc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  1.11       agc  * SUCH DAMAGE.
     34  1.11       agc  */
     35  1.11       agc 
     36   1.5     lukem #include <sys/cdefs.h>
     37   1.1       cgd #ifndef lint
     38  1.19     lukem __COPYRIGHT("@(#) Copyright (c) 1993\
     39  1.19     lukem  The Regents of the University of California.  All rights reserved.");
     40   1.1       cgd #endif /* not lint */
     41   1.1       cgd 
     42   1.1       cgd #ifndef lint
     43   1.5     lukem #if 0
     44   1.5     lukem from: static char sccsid[] = "@(#)pr.c	8.1 (Berkeley) 6/6/93";
     45   1.5     lukem #else
     46  1.22  christos __RCSID("$NetBSD: pr.c,v 1.22 2012/03/12 18:06:24 christos Exp $");
     47   1.5     lukem #endif
     48   1.1       cgd #endif /* not lint */
     49   1.1       cgd 
     50   1.1       cgd #include <sys/types.h>
     51   1.1       cgd #include <sys/time.h>
     52   1.1       cgd #include <sys/stat.h>
     53   1.1       cgd 
     54   1.1       cgd #include <ctype.h>
     55   1.1       cgd #include <errno.h>
     56   1.1       cgd #include <signal.h>
     57   1.1       cgd #include <stdio.h>
     58   1.1       cgd #include <stdlib.h>
     59   1.1       cgd #include <string.h>
     60   1.6    kleink #include <time.h>
     61   1.1       cgd #include <unistd.h>
     62  1.18     lukem #include <util.h>
     63   1.1       cgd 
     64   1.1       cgd #include "pr.h"
     65   1.1       cgd #include "extern.h"
     66   1.1       cgd 
     67   1.1       cgd /*
     68   1.1       cgd  * pr:	a printing and pagination filter. If multiple input files
     69   1.1       cgd  *	are specified, each is read, formatted, and written to standard
     70  1.10       wiz  *	output. By default, input is separated into 66-line pages, each
     71   1.1       cgd  *	with a header that includes the page number, date, time and the
     72   1.1       cgd  *	files pathname.
     73   1.1       cgd  *
     74   1.1       cgd  *	Complies with posix P1003.2/D11
     75   1.1       cgd  */
     76   1.1       cgd 
     77   1.1       cgd /*
     78   1.1       cgd  * parameter variables
     79   1.1       cgd  */
     80  1.21     joerg static int	pgnm;			/* starting page number */
     81  1.21     joerg static int	clcnt;			/* number of columns */
     82  1.21     joerg static int	colwd;			/* column data width - multiple columns */
     83  1.21     joerg static int	across;			/* mult col flag; write across page */
     84  1.21     joerg static int	dspace;			/* double space flag */
     85  1.21     joerg static char	inchar;			/* expand input char */
     86  1.21     joerg static int	ingap;			/* expand input gap */
     87  1.21     joerg static int	formfeed;		/* use formfeed as trailer */
     88  1.21     joerg static char	*header;		/* header name instead of file name */
     89  1.21     joerg static char	ochar;			/* contract output char */
     90  1.21     joerg static int	ogap;			/* contract output gap */
     91  1.21     joerg static int	lines;			/* number of lines per page */
     92  1.21     joerg static int	merge;			/* merge multiple files in output */
     93  1.21     joerg static char	nmchar;			/* line numbering append char */
     94  1.21     joerg static int	nmwd;			/* width of line number field */
     95  1.21     joerg static int	offst;			/* number of page offset spaces */
     96  1.21     joerg static int	nodiag;			/* do not report file open errors */
     97  1.21     joerg static char	schar;			/* text column separation character */
     98  1.21     joerg static int	sflag;			/* -s option for multiple columns */
     99  1.21     joerg static int	nohead;			/* do not write head and trailer */
    100  1.21     joerg static int	pgwd;			/* page width with multiple col output */
    101  1.21     joerg static const char *timefrmt = TIMEFMT;	/* time conversion string */
    102   1.1       cgd 
    103   1.1       cgd /*
    104   1.1       cgd  * misc globals
    105   1.1       cgd  */
    106  1.21     joerg static FILE	*errf;			/* error message file pointer */
    107  1.21     joerg static int	addone;			/* page length is odd with double space */
    108  1.21     joerg static int	errcnt;			/* error count on file processing */
    109  1.21     joerg static const char	digs[] = "0123456789";	/* page number translation map */
    110  1.21     joerg 
    111  1.21     joerg static void	 addnum(char *, int, int);
    112  1.21     joerg static void	 flsh_errs(void);
    113  1.21     joerg static int	 horzcol(int, char **);
    114  1.21     joerg static int	 inln(FILE *, char *, int, int *, int, int *);
    115  1.21     joerg static int	 inskip(FILE *, int, int);
    116  1.21     joerg static void	 mfail(void);
    117  1.21     joerg static int	 mulfile(int, char **);
    118  1.21     joerg static FILE	*nxtfile(int, char **, const char **, char *, int);
    119  1.21     joerg static int	 onecol(int, char **);
    120  1.21     joerg static int	 otln(char *, int, int *, int *, int);
    121  1.21     joerg static void	 pfail(void);
    122  1.21     joerg static int	 prhead(char *, const char *, int);
    123  1.21     joerg static int	 prtail(int, int);
    124  1.21     joerg static int	 setup(int, char **);
    125  1.21     joerg __dead static void	 terminate(int);
    126  1.21     joerg static void	 usage(void);
    127  1.21     joerg static int	 vertcol(int, char **);
    128   1.5     lukem 
    129   1.1       cgd int
    130  1.21     joerg main(int argc, char *argv[])
    131   1.1       cgd {
    132   1.1       cgd 	int ret_val;
    133   1.1       cgd 
    134   1.1       cgd 	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
    135   1.1       cgd 		(void)signal(SIGINT, terminate);
    136   1.1       cgd 	ret_val = setup(argc, argv);
    137   1.1       cgd 	if (!ret_val) {
    138   1.1       cgd 		/*
    139   1.1       cgd 		 * select the output format based on options
    140   1.1       cgd 		 */
    141   1.1       cgd 		if (merge)
    142   1.1       cgd 			ret_val = mulfile(argc, argv);
    143   1.1       cgd 		else if (clcnt == 1)
    144   1.1       cgd 			ret_val = onecol(argc, argv);
    145   1.1       cgd 		else if (across)
    146   1.1       cgd 			ret_val = horzcol(argc, argv);
    147   1.1       cgd 		else
    148   1.1       cgd 			ret_val = vertcol(argc, argv);
    149   1.1       cgd 	} else
    150   1.1       cgd 		usage();
    151   1.1       cgd 	flsh_errs();
    152   1.1       cgd 	if (errcnt || ret_val)
    153   1.1       cgd 		exit(1);
    154   1.1       cgd 	return(0);
    155   1.1       cgd }
    156   1.1       cgd 
    157   1.1       cgd /*
    158   1.1       cgd  * onecol:	print files with only one column of output.
    159   1.1       cgd  *		Line length is unlimited.
    160   1.1       cgd  */
    161  1.21     joerg static int
    162  1.21     joerg onecol(int argc, char *argv[])
    163   1.1       cgd {
    164   1.5     lukem 	int cnt = -1;
    165   1.5     lukem 	int off;
    166   1.5     lukem 	int lrgln;
    167   1.5     lukem 	int linecnt;
    168   1.5     lukem 	int num;
    169   1.1       cgd 	int lncnt;
    170   1.1       cgd 	int pagecnt;
    171   1.1       cgd 	int ips;
    172   1.1       cgd 	int ops;
    173   1.1       cgd 	int cps;
    174  1.14  christos 	char *obuf = NULL;
    175   1.1       cgd 	char *lbuf;
    176   1.1       cgd 	char *nbuf;
    177  1.14  christos 	char *hbuf = NULL;
    178   1.1       cgd 	char *ohbuf;
    179  1.14  christos 	FILE *inf = NULL;
    180  1.20     lukem 	const char *fname;
    181   1.1       cgd 	int mor;
    182  1.14  christos 	int error = 1;
    183   1.1       cgd 
    184   1.1       cgd 	if (nmwd)
    185   1.1       cgd 		num = nmwd + 1;
    186   1.1       cgd 	else
    187   1.1       cgd 		num = 0;
    188   1.1       cgd 	off = num + offst;
    189   1.1       cgd 
    190   1.1       cgd 	/*
    191   1.1       cgd 	 * allocate line buffer
    192   1.1       cgd 	 */
    193  1.14  christos 	if ((obuf = malloc((unsigned)(LBUF + off)*sizeof(char))) == NULL)
    194  1.14  christos 		goto oomem;
    195   1.1       cgd 	/*
    196   1.1       cgd 	 * allocate header buffer
    197   1.1       cgd 	 */
    198  1.14  christos 	if ((hbuf = malloc((unsigned)(HDBUF + offst)*sizeof(char))) == NULL)
    199  1.14  christos 		goto oomem;
    200   1.1       cgd 
    201   1.1       cgd 	ohbuf = hbuf + offst;
    202   1.1       cgd 	nbuf = obuf + offst;
    203   1.1       cgd 	lbuf = nbuf + num;
    204   1.1       cgd 	if (num)
    205   1.1       cgd 		nbuf[--num] = nmchar;
    206   1.1       cgd 	if (offst) {
    207   1.1       cgd 		(void)memset(obuf, (int)' ', offst);
    208   1.1       cgd 		(void)memset(hbuf, (int)' ', offst);
    209   1.1       cgd 	}
    210   1.1       cgd 
    211   1.1       cgd 	/*
    212   1.1       cgd 	 * loop by file
    213   1.1       cgd 	 */
    214   1.1       cgd 	while ((inf = nxtfile(argc, argv, &fname, ohbuf, 0)) != NULL) {
    215   1.1       cgd 		if (pgnm) {
    216   1.1       cgd 			/*
    217   1.1       cgd 			 * skip to specified page
    218   1.1       cgd 			 */
    219   1.1       cgd 			if (inskip(inf, pgnm, lines))
    220   1.1       cgd 				continue;
    221   1.1       cgd 			pagecnt = pgnm;
    222   1.1       cgd 		} else
    223   1.1       cgd 			pagecnt = 1;
    224   1.1       cgd 		lncnt = 0;
    225   1.1       cgd 
    226   1.1       cgd 		/*
    227   1.1       cgd 		 * loop by page
    228   1.1       cgd 		 */
    229   1.1       cgd 		for(;;) {
    230   1.1       cgd 			linecnt = 0;
    231   1.1       cgd 			lrgln = 0;
    232   1.1       cgd 			ops = 0;
    233   1.1       cgd 			ips = 0;
    234   1.1       cgd 			cps = 0;
    235   1.1       cgd 
    236   1.1       cgd 			/*
    237   1.1       cgd 			 * loop by line
    238   1.1       cgd 			 */
    239   1.1       cgd 			while (linecnt < lines) {
    240   1.1       cgd 				/*
    241   1.1       cgd 				 * input next line
    242   1.1       cgd 				 */
    243   1.1       cgd 				if ((cnt = inln(inf,lbuf,LBUF,&cps,0,&mor)) < 0)
    244   1.1       cgd 					break;
    245   1.1       cgd 				if (!linecnt && !nohead &&
    246   1.1       cgd 					prhead(hbuf, fname, pagecnt))
    247  1.14  christos 					goto out;
    248   1.1       cgd 
    249   1.1       cgd 				/*
    250   1.1       cgd 				 * start of new line.
    251   1.1       cgd 				 */
    252   1.1       cgd 				if (!lrgln) {
    253   1.1       cgd 					if (num)
    254   1.1       cgd 						addnum(nbuf, num, ++lncnt);
    255   1.1       cgd 					if (otln(obuf,cnt+off, &ips, &ops, mor))
    256  1.14  christos 						goto out;
    257   1.1       cgd 				} else if (otln(lbuf, cnt, &ips, &ops, mor))
    258  1.14  christos 					goto out;
    259   1.1       cgd 
    260   1.1       cgd 				/*
    261   1.1       cgd 				 * if line bigger than buffer, get more
    262   1.1       cgd 				 */
    263   1.1       cgd 				if (mor) {
    264   1.1       cgd 					lrgln = 1;
    265   1.1       cgd 					continue;
    266   1.1       cgd 				}
    267   1.1       cgd 
    268   1.1       cgd 				/*
    269   1.1       cgd 				 * whole line rcvd. reset tab proc. state
    270   1.1       cgd 				 */
    271   1.1       cgd 				++linecnt;
    272   1.1       cgd 				lrgln = 0;
    273   1.1       cgd 				ops = 0;
    274   1.1       cgd 				ips = 0;
    275   1.1       cgd 			}
    276   1.1       cgd 
    277   1.1       cgd 			/*
    278   1.1       cgd 			 * fill to end of page
    279   1.1       cgd 			 */
    280   1.1       cgd 			if (linecnt && prtail(lines-linecnt-lrgln, lrgln))
    281  1.14  christos 				goto out;
    282   1.1       cgd 
    283   1.1       cgd 			/*
    284   1.1       cgd 			 * On EOF go to next file
    285   1.1       cgd 			 */
    286   1.1       cgd 			if (cnt < 0)
    287   1.1       cgd 				break;
    288   1.1       cgd 			++pagecnt;
    289   1.1       cgd 		}
    290   1.1       cgd 		if (inf != stdin)
    291   1.1       cgd 			(void)fclose(inf);
    292   1.1       cgd 	}
    293   1.1       cgd 	if (eoptind < argc)
    294  1.14  christos 		goto out;
    295  1.14  christos 	error = 0;
    296  1.14  christos 	goto out;
    297  1.14  christos oomem:
    298  1.14  christos 	mfail();
    299  1.14  christos out:
    300  1.21     joerg 	free(obuf);
    301  1.21     joerg 	free(hbuf);
    302  1.14  christos 	if (inf != NULL && inf != stdin)
    303  1.14  christos 		(void)fclose(inf);
    304  1.14  christos 	return error;
    305   1.1       cgd }
    306   1.1       cgd 
    307   1.1       cgd /*
    308   1.1       cgd  * vertcol:	print files with more than one column of output down a page
    309   1.1       cgd  */
    310  1.21     joerg static int
    311  1.21     joerg vertcol(int argc, char *argv[])
    312   1.1       cgd {
    313   1.5     lukem 	char *ptbf;
    314  1.14  christos 	char **lstdat = NULL;
    315   1.5     lukem 	int i;
    316   1.5     lukem 	int j;
    317   1.5     lukem 	int cnt = -1;
    318   1.5     lukem 	int pln;
    319  1.14  christos 	int *indy = NULL;
    320   1.1       cgd 	int cvc;
    321  1.14  christos 	int *lindy = NULL;
    322   1.1       cgd 	int lncnt;
    323   1.1       cgd 	int stp;
    324   1.1       cgd 	int pagecnt;
    325   1.1       cgd 	int col = colwd + 1;
    326   1.1       cgd 	int mxlen = pgwd + offst + 1;
    327   1.1       cgd 	int mclcnt = clcnt - 1;
    328  1.14  christos 	struct vcol *vc = NULL;
    329   1.1       cgd 	int mvc;
    330   1.1       cgd 	int tvc;
    331   1.1       cgd 	int cw = nmwd + 1;
    332   1.1       cgd 	int fullcol;
    333  1.14  christos 	char *buf = NULL;
    334  1.14  christos 	char *hbuf = NULL;
    335   1.1       cgd 	char *ohbuf;
    336  1.20     lukem 	const char *fname;
    337  1.14  christos 	FILE *inf = NULL;
    338   1.1       cgd 	int ips = 0;
    339   1.1       cgd 	int cps = 0;
    340   1.1       cgd 	int ops = 0;
    341   1.1       cgd 	int mor = 0;
    342  1.14  christos 	int error = 1;
    343   1.1       cgd 
    344   1.1       cgd 	/*
    345   1.1       cgd 	 * allocate page buffer
    346   1.1       cgd 	 */
    347  1.14  christos 	if ((buf = malloc((unsigned)lines*mxlen*sizeof(char))) == NULL)
    348  1.14  christos 		goto oomem;
    349   1.1       cgd 
    350   1.1       cgd 	/*
    351   1.1       cgd 	 * allocate page header
    352   1.1       cgd 	 */
    353  1.14  christos 	if ((hbuf = malloc((unsigned)(HDBUF + offst)*sizeof(char))) == NULL)
    354  1.14  christos 		goto oomem;
    355   1.1       cgd 	ohbuf = hbuf + offst;
    356   1.1       cgd 	if (offst)
    357   1.1       cgd 		(void)memset(hbuf, (int)' ', offst);
    358   1.1       cgd 
    359   1.1       cgd 	/*
    360   1.1       cgd 	 * col pointers when no headers
    361   1.1       cgd 	 */
    362   1.1       cgd 	mvc = lines * clcnt;
    363  1.14  christos 	if ((vc = malloc((unsigned)mvc*sizeof(struct vcol))) == NULL)
    364  1.14  christos 		goto oomem;
    365   1.1       cgd 
    366   1.1       cgd 	/*
    367   1.1       cgd 	 * pointer into page where last data per line is located
    368   1.1       cgd 	 */
    369  1.14  christos 	if ((lstdat = malloc((unsigned)lines*sizeof(char *))) == NULL)
    370  1.14  christos 		goto oomem;
    371   1.1       cgd 
    372   1.1       cgd 	/*
    373   1.1       cgd 	 * fast index lookups to locate start of lines
    374   1.1       cgd 	 */
    375  1.14  christos 	if ((indy = malloc((unsigned)lines*sizeof(int))) == NULL)
    376  1.14  christos 		goto oomem;
    377  1.14  christos 	if ((lindy = malloc((unsigned)lines*sizeof(int))) == NULL)
    378  1.14  christos 		goto oomem;
    379   1.1       cgd 
    380   1.1       cgd 	if (nmwd)
    381   1.1       cgd 		fullcol = col + cw;
    382   1.1       cgd 	else
    383   1.1       cgd 		fullcol = col;
    384   1.1       cgd 
    385   1.1       cgd 	/*
    386   1.1       cgd 	 * initialize buffer lookup indexes and offset area
    387   1.1       cgd 	 */
    388   1.1       cgd 	for (j = 0; j < lines; ++j) {
    389   1.1       cgd 		lindy[j] = j * mxlen;
    390   1.1       cgd 		indy[j] = lindy[j] + offst;
    391   1.1       cgd 		if (offst) {
    392   1.1       cgd 			ptbf = buf + lindy[j];
    393   1.1       cgd 			(void)memset(ptbf, (int)' ', offst);
    394   1.1       cgd 			ptbf += offst;
    395   1.1       cgd 		} else
    396   1.1       cgd 			ptbf = buf + indy[j];
    397   1.1       cgd 		lstdat[j] = ptbf;
    398   1.1       cgd 	}
    399   1.1       cgd 
    400   1.1       cgd 	/*
    401   1.1       cgd 	 * loop by file
    402   1.1       cgd 	 */
    403   1.1       cgd 	while ((inf = nxtfile(argc, argv, &fname, ohbuf, 0)) != NULL) {
    404   1.1       cgd 		if (pgnm) {
    405   1.1       cgd 			/*
    406   1.1       cgd 			 * skip to requested page
    407   1.1       cgd 			 */
    408   1.1       cgd 			if (inskip(inf, pgnm, lines))
    409   1.1       cgd 				continue;
    410   1.1       cgd 			pagecnt = pgnm;
    411   1.1       cgd 		} else
    412   1.1       cgd 			pagecnt = 1;
    413   1.1       cgd 		lncnt = 0;
    414   1.1       cgd 
    415   1.1       cgd 		/*
    416   1.1       cgd 		 * loop by page
    417   1.1       cgd 		 */
    418   1.1       cgd 		for(;;) {
    419   1.1       cgd 			/*
    420   1.1       cgd 			 * loop by column
    421   1.1       cgd 			 */
    422   1.1       cgd 			cvc = 0;
    423   1.1       cgd 			for (i = 0; i < clcnt; ++i) {
    424   1.1       cgd 				j = 0;
    425   1.1       cgd 				/*
    426   1.1       cgd 				 * if last column, do not pad
    427   1.1       cgd 				 */
    428   1.1       cgd 				if (i == mclcnt)
    429   1.1       cgd 					stp = 1;
    430   1.1       cgd 				else
    431   1.1       cgd 					stp = 0;
    432   1.1       cgd 				/*
    433   1.1       cgd 				 * loop by line
    434   1.1       cgd 				 */
    435   1.1       cgd 				for(;;) {
    436   1.1       cgd 					/*
    437   1.1       cgd 					 * is this first column
    438   1.1       cgd 					 */
    439   1.1       cgd 					if (!i) {
    440   1.1       cgd 						ptbf = buf + indy[j];
    441   1.1       cgd 						lstdat[j] = ptbf;
    442   1.1       cgd 					} else
    443   1.1       cgd 						ptbf = lstdat[j];
    444   1.1       cgd 					vc[cvc].pt = ptbf;
    445   1.1       cgd 
    446   1.1       cgd 					/*
    447   1.1       cgd 					 * add number
    448   1.1       cgd 					 */
    449   1.1       cgd 					if (nmwd) {
    450   1.1       cgd 						addnum(ptbf, nmwd, ++lncnt);
    451   1.1       cgd 						ptbf += nmwd;
    452   1.1       cgd 						*ptbf++ = nmchar;
    453   1.1       cgd 					}
    454   1.1       cgd 
    455   1.1       cgd 					/*
    456   1.1       cgd 					 * input next line
    457   1.1       cgd 					 */
    458   1.1       cgd 					cnt = inln(inf,ptbf,colwd,&cps,1,&mor);
    459   1.1       cgd 					vc[cvc++].cnt = cnt;
    460   1.1       cgd 					if (cnt < 0)
    461   1.1       cgd 						break;
    462   1.1       cgd 					ptbf += cnt;
    463   1.1       cgd 
    464   1.1       cgd 					/*
    465   1.1       cgd 					 * pad all but last column on page
    466   1.1       cgd 					 */
    467   1.1       cgd 					if (!stp) {
    468   1.1       cgd 						/*
    469   1.1       cgd 						 * pad to end of column
    470   1.1       cgd 						 */
    471   1.1       cgd 						if (sflag)
    472   1.1       cgd 							*ptbf++ = schar;
    473   1.1       cgd 						else if ((pln = col-cnt) > 0) {
    474   1.1       cgd 							(void)memset(ptbf,
    475   1.1       cgd 								(int)' ',pln);
    476   1.1       cgd 							ptbf += pln;
    477   1.1       cgd 						}
    478   1.1       cgd 					}
    479   1.1       cgd 					/*
    480   1.1       cgd 					 * remember last char in line
    481   1.1       cgd 					 */
    482   1.1       cgd 					lstdat[j] = ptbf;
    483   1.1       cgd 					if (++j >= lines)
    484   1.1       cgd 						break;
    485   1.1       cgd 				}
    486   1.1       cgd 				if (cnt < 0)
    487   1.1       cgd 					break;
    488   1.1       cgd 			}
    489   1.1       cgd 
    490   1.1       cgd 			/*
    491   1.1       cgd 			 * when -t (no header) is specified the spec requires
    492   1.1       cgd 			 * the min number of lines. The last page may not have
    493   1.1       cgd 			 * balanced length columns. To fix this we must reorder
    494   1.1       cgd 			 * the columns. This is a very slow technique so it is
    495   1.1       cgd 			 * only used under limited conditions. Without -t, the
    496   1.1       cgd 			 * balancing of text columns is unspecified. To NOT
    497   1.1       cgd 			 * balance the last page, add the global variable
    498   1.1       cgd 			 * nohead to the if statement below e.g.
    499   1.1       cgd 			 *
    500   1.1       cgd 			 * if ((cnt < 0) && nohead && cvc ......
    501   1.1       cgd 			 */
    502   1.1       cgd 			--cvc;
    503   1.1       cgd 
    504   1.1       cgd 			/*
    505   1.1       cgd 			 * check to see if last page needs to be reordered
    506   1.1       cgd 			 */
    507   1.1       cgd 			if ((cnt < 0) && cvc && ((mvc-cvc) >= clcnt)){
    508   1.1       cgd 				pln = cvc/clcnt;
    509   1.1       cgd 				if (cvc % clcnt)
    510   1.1       cgd 					++pln;
    511   1.1       cgd 
    512   1.1       cgd 				/*
    513   1.1       cgd 				 * print header
    514   1.1       cgd 				 */
    515   1.1       cgd 				if (!nohead && prhead(hbuf, fname, pagecnt))
    516  1.14  christos 					goto out;
    517   1.1       cgd 				for (i = 0; i < pln; ++i) {
    518   1.1       cgd 					ips = 0;
    519   1.1       cgd 					ops = 0;
    520  1.15  christos 					if (offst&& otln(buf,offst,&ips,&ops,1)) {
    521  1.15  christos 						error = 1;
    522  1.15  christos 						goto out;
    523  1.15  christos 					}
    524   1.1       cgd 					tvc = i;
    525   1.1       cgd 
    526   1.1       cgd 					for (j = 0; j < clcnt; ++j) {
    527   1.1       cgd 						/*
    528   1.1       cgd 						 * determine column length
    529   1.1       cgd 						 */
    530   1.1       cgd 						if (j == mclcnt) {
    531   1.1       cgd 							/*
    532   1.1       cgd 							 * last column
    533   1.1       cgd 							 */
    534   1.1       cgd 							cnt = vc[tvc].cnt;
    535   1.1       cgd 							if (nmwd)
    536   1.1       cgd 								cnt += cw;
    537   1.1       cgd 						} else if (sflag) {
    538   1.1       cgd 							/*
    539   1.1       cgd 							 * single ch between
    540   1.1       cgd 							 */
    541   1.1       cgd 							cnt = vc[tvc].cnt + 1;
    542   1.1       cgd 							if (nmwd)
    543   1.1       cgd 								cnt += cw;
    544   1.1       cgd 						} else
    545   1.1       cgd 							cnt = fullcol;
    546   1.1       cgd 						if (otln(vc[tvc].pt, cnt, &ips,
    547   1.1       cgd 								&ops, 1))
    548  1.16  christos 							goto out;
    549   1.1       cgd 						tvc += pln;
    550   1.1       cgd 						if (tvc >= cvc)
    551   1.1       cgd 							break;
    552   1.1       cgd 					}
    553   1.1       cgd 					/*
    554   1.1       cgd 					 * terminate line
    555   1.1       cgd 					 */
    556   1.1       cgd 					if (otln(buf, 0, &ips, &ops, 0))
    557  1.14  christos 						goto out;
    558   1.1       cgd 				}
    559   1.1       cgd 				/*
    560   1.1       cgd 				 * pad to end of page
    561   1.1       cgd 				 */
    562   1.1       cgd 				if (prtail((lines - pln), 0))
    563  1.14  christos 					goto out;
    564   1.1       cgd 				/*
    565   1.1       cgd 				 * done with output, go to next file
    566   1.1       cgd 				 */
    567   1.1       cgd 				break;
    568   1.1       cgd 			}
    569   1.1       cgd 
    570   1.1       cgd 			/*
    571   1.1       cgd 			 * determine how many lines to output
    572   1.1       cgd 			 */
    573   1.1       cgd 			if (i > 0)
    574   1.1       cgd 				pln = lines;
    575   1.1       cgd 			else
    576   1.1       cgd 				pln = j;
    577   1.1       cgd 
    578   1.1       cgd 			/*
    579   1.1       cgd 			 * print header
    580   1.1       cgd 			 */
    581   1.1       cgd 			if (pln && !nohead && prhead(hbuf, fname, pagecnt))
    582  1.14  christos 				goto out;
    583   1.1       cgd 
    584   1.1       cgd 			/*
    585   1.1       cgd 			 * output each line
    586   1.1       cgd 			 */
    587   1.1       cgd 			for (i = 0; i < pln; ++i) {
    588   1.1       cgd 				ptbf = buf + lindy[i];
    589   1.1       cgd 				if ((j = lstdat[i] - ptbf) <= offst)
    590   1.1       cgd 					break;
    591   1.1       cgd 				if (otln(ptbf, j, &ips, &ops, 0))
    592  1.14  christos 					goto out;
    593   1.1       cgd 			}
    594   1.1       cgd 
    595   1.1       cgd 			/*
    596   1.1       cgd 			 * pad to end of page
    597   1.1       cgd 			 */
    598   1.1       cgd 			if (pln && prtail((lines - pln), 0))
    599  1.14  christos 				goto out;
    600   1.1       cgd 
    601   1.1       cgd 			/*
    602   1.1       cgd 			 * if EOF go to next file
    603   1.1       cgd 			 */
    604   1.1       cgd 			if (cnt < 0)
    605   1.1       cgd 				break;
    606   1.1       cgd 			++pagecnt;
    607   1.1       cgd 		}
    608   1.1       cgd 		if (inf != stdin)
    609   1.1       cgd 			(void)fclose(inf);
    610   1.1       cgd 	}
    611   1.1       cgd 	if (eoptind < argc)
    612  1.14  christos 		goto out;
    613  1.14  christos 	error = 0;
    614  1.14  christos 	goto out;
    615  1.14  christos oomem:
    616  1.14  christos 	mfail();
    617  1.14  christos out:
    618  1.21     joerg 	free(buf);
    619  1.21     joerg 	free(hbuf);
    620  1.21     joerg 	free(vc);
    621  1.21     joerg 	free(lstdat);
    622  1.21     joerg 	free(lindy);
    623  1.14  christos 	if (inf != NULL && inf != stdin)
    624  1.14  christos 		(void)fclose(inf);
    625  1.14  christos 	return error;
    626   1.1       cgd }
    627   1.1       cgd 
    628   1.1       cgd /*
    629   1.1       cgd  * horzcol:	print files with more than one column of output across a page
    630   1.1       cgd  */
    631  1.21     joerg static int
    632  1.21     joerg horzcol(int argc, char *argv[])
    633   1.1       cgd {
    634   1.5     lukem 	char *ptbf;
    635   1.5     lukem 	int pln;
    636   1.5     lukem 	int cnt = -1;
    637   1.5     lukem 	char *lstdat;
    638   1.5     lukem 	int col = colwd + 1;
    639   1.5     lukem 	int j;
    640   1.5     lukem 	int i;
    641   1.1       cgd 	int lncnt;
    642   1.1       cgd 	int pagecnt;
    643  1.14  christos 	char *buf = NULL;
    644  1.14  christos 	char *hbuf = NULL;
    645   1.1       cgd 	char *ohbuf;
    646  1.20     lukem 	const char *fname;
    647  1.14  christos 	FILE *inf = NULL;
    648   1.1       cgd 	int ips = 0;
    649   1.1       cgd 	int cps = 0;
    650   1.1       cgd 	int ops = 0;
    651   1.1       cgd 	int mor = 0;
    652  1.14  christos 	int error = 1;
    653   1.1       cgd 
    654  1.14  christos 	if ((buf = malloc((unsigned)(pgwd+offst+1)*sizeof(char))) == NULL)
    655  1.14  christos 		goto oomem;
    656   1.1       cgd 
    657   1.1       cgd 	/*
    658   1.1       cgd 	 * page header
    659   1.1       cgd 	 */
    660  1.14  christos 	if ((hbuf = malloc((unsigned)(HDBUF + offst)*sizeof(char))) == NULL)
    661  1.14  christos 		goto oomem;
    662   1.1       cgd 	ohbuf = hbuf + offst;
    663   1.1       cgd 	if (offst) {
    664   1.1       cgd 		(void)memset(buf, (int)' ', offst);
    665   1.1       cgd 		(void)memset(hbuf, (int)' ', offst);
    666   1.1       cgd 	}
    667   1.1       cgd 
    668   1.1       cgd 	/*
    669   1.1       cgd 	 * loop by file
    670   1.1       cgd 	 */
    671   1.1       cgd 	while ((inf = nxtfile(argc, argv, &fname, ohbuf, 0)) != NULL) {
    672   1.1       cgd 		if (pgnm) {
    673   1.1       cgd 			if (inskip(inf, pgnm, lines))
    674   1.1       cgd 				continue;
    675   1.1       cgd 			pagecnt = pgnm;
    676   1.1       cgd 		} else
    677   1.1       cgd 			pagecnt = 1;
    678   1.1       cgd 		lncnt = 0;
    679   1.1       cgd 
    680   1.1       cgd 		/*
    681   1.1       cgd 		 * loop by page
    682   1.1       cgd 		 */
    683   1.1       cgd 		for(;;) {
    684   1.1       cgd 			/*
    685   1.1       cgd 			 * loop by line
    686   1.1       cgd 			 */
    687   1.1       cgd 			for (i = 0; i < lines; ++i) {
    688   1.1       cgd 				ptbf = buf + offst;
    689   1.1       cgd 				lstdat = ptbf;
    690   1.1       cgd 				j = 0;
    691   1.1       cgd 				/*
    692   1.1       cgd 				 * loop by col
    693   1.1       cgd 				 */
    694   1.1       cgd 				for(;;) {
    695   1.1       cgd 					if (nmwd) {
    696   1.1       cgd 						/*
    697   1.1       cgd 						 * add number to column
    698   1.1       cgd 						 */
    699   1.1       cgd 						addnum(ptbf, nmwd, ++lncnt);
    700   1.1       cgd 						ptbf += nmwd;
    701   1.1       cgd 						*ptbf++ = nmchar;
    702   1.1       cgd 					}
    703   1.1       cgd 					/*
    704   1.1       cgd 					 * input line
    705   1.1       cgd 					 */
    706   1.1       cgd 					if ((cnt = inln(inf,ptbf,colwd,&cps,1,
    707   1.1       cgd 							&mor)) < 0)
    708   1.1       cgd 						break;
    709   1.1       cgd 					ptbf += cnt;
    710   1.1       cgd 					lstdat = ptbf;
    711   1.1       cgd 
    712   1.1       cgd 					/*
    713   1.1       cgd 					 * if last line skip padding
    714   1.1       cgd 					 */
    715   1.1       cgd 					if (++j >= clcnt)
    716   1.1       cgd 						break;
    717   1.1       cgd 
    718   1.1       cgd 					/*
    719   1.1       cgd 					 * pad to end of column
    720   1.1       cgd 					 */
    721   1.1       cgd 					if (sflag)
    722   1.1       cgd 						*ptbf++ = schar;
    723   1.1       cgd 					else if ((pln = col - cnt) > 0) {
    724   1.1       cgd 						(void)memset(ptbf,(int)' ',pln);
    725   1.1       cgd 						ptbf += pln;
    726   1.1       cgd 					}
    727   1.1       cgd 				}
    728   1.1       cgd 
    729   1.1       cgd 				/*
    730   1.1       cgd 				 * determine line length
    731   1.1       cgd 				 */
    732   1.1       cgd 				if ((j = lstdat - buf) <= offst)
    733   1.1       cgd 					break;
    734   1.1       cgd 				if (!i && !nohead &&
    735   1.1       cgd 					prhead(hbuf, fname, pagecnt))
    736  1.14  christos 					goto out;
    737   1.1       cgd 				/*
    738   1.1       cgd 				 * output line
    739   1.1       cgd 				 */
    740   1.1       cgd 				if (otln(buf, j, &ips, &ops, 0))
    741  1.14  christos 					goto out;
    742   1.1       cgd 			}
    743   1.1       cgd 
    744   1.1       cgd 			/*
    745   1.1       cgd 			 * pad to end of page
    746   1.1       cgd 			 */
    747   1.1       cgd 			if (i && prtail(lines-i, 0))
    748  1.14  christos 				goto out;
    749   1.1       cgd 
    750   1.1       cgd 			/*
    751   1.1       cgd 			 * if EOF go to next file
    752   1.1       cgd 			 */
    753   1.1       cgd 			if (cnt < 0)
    754   1.1       cgd 				break;
    755   1.1       cgd 			++pagecnt;
    756   1.1       cgd 		}
    757   1.1       cgd 		if (inf != stdin)
    758   1.1       cgd 			(void)fclose(inf);
    759   1.1       cgd 	}
    760   1.1       cgd 	if (eoptind < argc)
    761  1.14  christos 		goto out;
    762  1.14  christos 	error = 0;
    763  1.14  christos 	goto out;
    764  1.14  christos oomem:
    765  1.14  christos 	mfail();
    766  1.14  christos out:
    767  1.21     joerg 	free(buf);
    768  1.21     joerg 	free(hbuf);
    769  1.14  christos 	if (inf != NULL && inf != stdin)
    770  1.14  christos 		(void)fclose(inf);
    771  1.14  christos 	return error;
    772   1.1       cgd }
    773   1.1       cgd 
    774   1.1       cgd /*
    775   1.1       cgd  * mulfile:	print files with more than one column of output and
    776   1.1       cgd  *		more than one file concurrently
    777   1.1       cgd  */
    778  1.21     joerg static int
    779  1.21     joerg mulfile(int argc, char *argv[])
    780   1.1       cgd {
    781   1.5     lukem 	char *ptbf;
    782   1.5     lukem 	int j;
    783   1.5     lukem 	int pln;
    784   1.5     lukem 	int cnt;
    785   1.5     lukem 	char *lstdat;
    786   1.5     lukem 	int i;
    787  1.14  christos 	FILE **fbuf = NULL;
    788   1.1       cgd 	int actf;
    789   1.1       cgd 	int lncnt;
    790   1.1       cgd 	int col;
    791   1.1       cgd 	int pagecnt;
    792   1.1       cgd 	int fproc;
    793  1.14  christos 	char *buf = NULL;
    794  1.14  christos 	char *hbuf = NULL;
    795   1.1       cgd 	char *ohbuf;
    796  1.20     lukem 	const char *fname;
    797   1.1       cgd 	int ips = 0;
    798   1.1       cgd 	int cps = 0;
    799   1.1       cgd 	int ops = 0;
    800   1.1       cgd 	int mor = 0;
    801  1.14  christos 	int error = 1;
    802   1.1       cgd 
    803   1.1       cgd 	/*
    804   1.1       cgd 	 * array of FILE *, one for each operand
    805   1.1       cgd 	 */
    806  1.14  christos 	if ((fbuf = calloc(clcnt, sizeof(FILE *))) == NULL)
    807  1.14  christos 		goto oomem;
    808   1.1       cgd 
    809   1.1       cgd 	/*
    810   1.1       cgd 	 * page header
    811   1.1       cgd 	 */
    812  1.14  christos 	if ((hbuf = malloc((unsigned)(HDBUF + offst)*sizeof(char))) == NULL)
    813  1.14  christos 		goto oomem;
    814   1.1       cgd 	ohbuf = hbuf + offst;
    815   1.1       cgd 
    816   1.1       cgd 	/*
    817   1.1       cgd 	 * do not know how many columns yet. The number of operands provide an
    818   1.1       cgd 	 * upper bound on the number of columns. We use the number of files
    819   1.1       cgd 	 * we can open successfully to set the number of columns. The operation
    820   1.1       cgd 	 * of the merge operation (-m) in relation to unsuccesful file opens
    821   1.1       cgd 	 * is unspecified by posix.
    822   1.1       cgd 	 */
    823   1.1       cgd 	j = 0;
    824   1.1       cgd 	while (j < clcnt) {
    825   1.1       cgd 		if ((fbuf[j] = nxtfile(argc, argv, &fname, ohbuf, 1)) == NULL)
    826   1.1       cgd 			break;
    827   1.1       cgd 		if (pgnm && (inskip(fbuf[j], pgnm, lines)))
    828   1.1       cgd 			fbuf[j] = NULL;
    829   1.1       cgd 		++j;
    830   1.1       cgd 	}
    831   1.1       cgd 
    832   1.1       cgd 	/*
    833   1.1       cgd 	 * if no files, exit
    834   1.1       cgd 	 */
    835   1.1       cgd 	if (!j)
    836  1.14  christos 		goto out;
    837   1.1       cgd 
    838   1.1       cgd 	/*
    839   1.1       cgd 	 * calculate page boundries based on open file count
    840   1.1       cgd 	 */
    841   1.1       cgd 	clcnt = j;
    842   1.1       cgd 	if (nmwd) {
    843   1.1       cgd 		colwd = (pgwd - clcnt - nmwd)/clcnt;
    844   1.1       cgd 		pgwd = ((colwd + 1) * clcnt) - nmwd - 2;
    845   1.1       cgd 	} else {
    846   1.1       cgd 		colwd = (pgwd + 1 - clcnt)/clcnt;
    847   1.1       cgd 		pgwd = ((colwd + 1) * clcnt) - 1;
    848   1.1       cgd 	}
    849   1.1       cgd 	if (colwd < 1) {
    850  1.21     joerg 		(void)fprintf(errf,
    851   1.1       cgd 		  "pr: page width too small for %d columns\n", clcnt);
    852  1.14  christos 		goto out;
    853   1.1       cgd 	}
    854   1.1       cgd 	actf = clcnt;
    855   1.1       cgd 	col = colwd + 1;
    856   1.1       cgd 
    857   1.1       cgd 	/*
    858   1.1       cgd 	 * line buffer
    859   1.1       cgd 	 */
    860  1.14  christos 	if ((buf = malloc((unsigned)(pgwd+offst+1)*sizeof(char))) == NULL)
    861  1.14  christos 		goto out;
    862   1.1       cgd 	if (offst) {
    863   1.1       cgd 		(void)memset(buf, (int)' ', offst);
    864   1.1       cgd 		(void)memset(hbuf, (int)' ', offst);
    865   1.1       cgd 	}
    866   1.1       cgd 	if (pgnm)
    867   1.1       cgd 		pagecnt = pgnm;
    868   1.1       cgd 	else
    869   1.1       cgd 		pagecnt = 1;
    870   1.1       cgd 	lncnt = 0;
    871   1.1       cgd 
    872   1.1       cgd 	/*
    873   1.1       cgd 	 * continue to loop while any file still has data
    874   1.1       cgd 	 */
    875   1.1       cgd 	while (actf > 0) {
    876   1.1       cgd 		/*
    877   1.1       cgd 		 * loop by line
    878   1.1       cgd 		 */
    879   1.1       cgd 		for (i = 0; i < lines; ++i) {
    880   1.1       cgd 			ptbf = buf + offst;
    881   1.1       cgd 			lstdat = ptbf;
    882   1.1       cgd 			if (nmwd) {
    883   1.1       cgd 				/*
    884   1.1       cgd 				 * add line number to line
    885   1.1       cgd 				 */
    886   1.1       cgd 				addnum(ptbf, nmwd, ++lncnt);
    887   1.1       cgd 				ptbf += nmwd;
    888   1.1       cgd 				*ptbf++ = nmchar;
    889   1.1       cgd 			}
    890   1.1       cgd 			j = 0;
    891   1.1       cgd 			fproc = 0;
    892   1.1       cgd 
    893   1.1       cgd 			/*
    894   1.1       cgd 			 * loop by column
    895   1.1       cgd 			 */
    896   1.1       cgd 			for (j = 0; j < clcnt; ++j) {
    897   1.1       cgd 				if (fbuf[j] == NULL) {
    898   1.1       cgd 					/*
    899   1.1       cgd 					 * empty column; EOF
    900   1.1       cgd 					 */
    901   1.1       cgd 					cnt = 0;
    902   1.1       cgd 				} else if ((cnt = inln(fbuf[j], ptbf, colwd,
    903   1.1       cgd 							&cps, 1, &mor)) < 0) {
    904   1.1       cgd 					/*
    905   1.1       cgd 					 * EOF hit; no data
    906   1.1       cgd 					 */
    907   1.1       cgd 					if (fbuf[j] != stdin)
    908   1.1       cgd 						(void)fclose(fbuf[j]);
    909   1.1       cgd 					fbuf[j] = NULL;
    910   1.1       cgd 					--actf;
    911   1.1       cgd 					cnt = 0;
    912   1.1       cgd 				} else {
    913   1.1       cgd 					/*
    914   1.1       cgd 					 * process file data
    915   1.1       cgd 					 */
    916   1.1       cgd 					ptbf += cnt;
    917   1.1       cgd 					lstdat = ptbf;
    918   1.1       cgd 					fproc++;
    919   1.1       cgd 				}
    920   1.1       cgd 
    921   1.1       cgd 				/*
    922   1.1       cgd 				 * if last ACTIVE column, done with line
    923   1.1       cgd 				 */
    924   1.1       cgd 				if (fproc >= actf)
    925   1.1       cgd 					break;
    926   1.1       cgd 
    927   1.1       cgd 				/*
    928   1.1       cgd 				 * pad to end of column
    929   1.1       cgd 				 */
    930   1.1       cgd 				if (sflag) {
    931   1.1       cgd 					*ptbf++ = schar;
    932   1.1       cgd 				} else if ((pln = col - cnt) > 0) {
    933   1.1       cgd 					(void)memset(ptbf, (int)' ', pln);
    934   1.1       cgd 					ptbf += pln;
    935   1.1       cgd 				}
    936   1.1       cgd 			}
    937   1.1       cgd 
    938   1.1       cgd 			/*
    939   1.1       cgd 			 * calculate data in line
    940   1.1       cgd 			 */
    941   1.1       cgd 			if ((j = lstdat - buf) <= offst)
    942   1.1       cgd 				break;
    943   1.1       cgd 
    944   1.1       cgd 			if (!i && !nohead && prhead(hbuf, fname, pagecnt))
    945  1.14  christos 				goto out;
    946   1.1       cgd 
    947   1.1       cgd 			/*
    948   1.1       cgd 			 * output line
    949   1.1       cgd 			 */
    950   1.1       cgd 			if (otln(buf, j, &ips, &ops, 0))
    951  1.14  christos 				goto out;
    952   1.1       cgd 
    953   1.1       cgd 			/*
    954   1.1       cgd 			 * if no more active files, done
    955   1.1       cgd 			 */
    956   1.1       cgd 			if (actf <= 0) {
    957   1.1       cgd 				++i;
    958   1.1       cgd 				break;
    959   1.1       cgd 			}
    960   1.1       cgd 		}
    961   1.1       cgd 
    962   1.1       cgd 		/*
    963   1.1       cgd 		 * pad to end of page
    964   1.1       cgd 		 */
    965   1.1       cgd 		if (i && prtail(lines-i, 0))
    966  1.14  christos 			goto out;
    967   1.1       cgd 		++pagecnt;
    968   1.1       cgd 	}
    969   1.1       cgd 	if (eoptind < argc)
    970  1.14  christos 		goto out;
    971  1.14  christos 	error = 0;
    972  1.14  christos 	goto out;
    973  1.14  christos oomem:
    974  1.14  christos 	mfail();
    975  1.14  christos out:
    976  1.14  christos 	if (fbuf) {
    977  1.14  christos 		for (j = 0; j < clcnt; j++)
    978  1.14  christos 			if (fbuf[j] && fbuf[j] != stdin)
    979  1.14  christos 				(void)fclose(fbuf[j]);
    980  1.14  christos 		free(fbuf);
    981  1.14  christos 	}
    982  1.21     joerg 	free(hbuf);
    983  1.21     joerg 	free(buf);
    984  1.14  christos 	return error;
    985   1.1       cgd }
    986   1.1       cgd 
    987   1.1       cgd /*
    988   1.1       cgd  * inln():	input a line of data (unlimited length lines supported)
    989   1.1       cgd  *		Input is optionally expanded to spaces
    990   1.1       cgd  *
    991   1.1       cgd  *	inf:	file
    992   1.1       cgd  *	buf:	buffer
    993   1.1       cgd  *	lim:	buffer length
    994   1.1       cgd  *	cps:	column positon 1st char in buffer (large line support)
    995   1.1       cgd  *	trnc:	throw away data more than lim up to \n
    996   1.1       cgd  *	mor:	set if more data in line (not truncated)
    997   1.1       cgd  */
    998  1.21     joerg static int
    999  1.21     joerg inln(FILE *inf, char *buf, int lim, int *cps, int trnc, int *mor)
   1000   1.1       cgd {
   1001   1.5     lukem 	int col;
   1002   1.5     lukem 	int gap = ingap;
   1003   1.5     lukem 	int ch = EOF;
   1004   1.5     lukem 	char *ptbuf;
   1005   1.5     lukem 	int chk = (int)inchar;
   1006   1.1       cgd 
   1007   1.1       cgd 	ptbuf = buf;
   1008   1.1       cgd 
   1009   1.1       cgd 	if (gap) {
   1010   1.1       cgd 		/*
   1011   1.1       cgd 		 * expanding input option
   1012   1.1       cgd 		 */
   1013   1.1       cgd 		while ((--lim >= 0) && ((ch = getc(inf)) != EOF)) {
   1014   1.1       cgd 			/*
   1015   1.1       cgd 			 * is this the input "tab" char
   1016   1.1       cgd 			 */
   1017   1.1       cgd 			if (ch == chk) {
   1018   1.1       cgd 				/*
   1019   1.1       cgd 				 * expand to number of spaces
   1020   1.1       cgd 				 */
   1021   1.1       cgd 				col = (ptbuf - buf) + *cps;
   1022   1.1       cgd 				col = gap - (col % gap);
   1023   1.1       cgd 
   1024   1.1       cgd 				/*
   1025   1.1       cgd 				 * if more than this line, push back
   1026   1.1       cgd 				 */
   1027   1.1       cgd 				if ((col > lim) && (ungetc(ch, inf) == EOF))
   1028   1.1       cgd 					return(1);
   1029   1.1       cgd 
   1030   1.1       cgd 				/*
   1031   1.1       cgd 				 * expand to spaces
   1032   1.1       cgd 				 */
   1033   1.1       cgd 				while ((--col >= 0) && (--lim >= 0))
   1034   1.1       cgd 					*ptbuf++ = ' ';
   1035   1.1       cgd 				continue;
   1036   1.1       cgd 			}
   1037   1.1       cgd 			if (ch == '\n')
   1038   1.1       cgd 				break;
   1039   1.1       cgd 			*ptbuf++ = ch;
   1040   1.1       cgd 		}
   1041   1.1       cgd 	} else {
   1042   1.1       cgd 		/*
   1043   1.1       cgd 		 * no expansion
   1044   1.1       cgd 		 */
   1045   1.1       cgd 		while ((--lim >= 0) && ((ch = getc(inf)) != EOF)) {
   1046   1.1       cgd 			if (ch == '\n')
   1047   1.1       cgd 				break;
   1048   1.1       cgd 			*ptbuf++ = ch;
   1049   1.1       cgd 		}
   1050   1.1       cgd 	}
   1051   1.1       cgd 	col = ptbuf - buf;
   1052   1.1       cgd 	if (ch == EOF) {
   1053   1.1       cgd 		*mor = 0;
   1054   1.1       cgd 		*cps = 0;
   1055   1.1       cgd 		if (!col)
   1056   1.1       cgd 			return(-1);
   1057   1.1       cgd 		return(col);
   1058   1.1       cgd 	}
   1059   1.1       cgd 	if (ch == '\n') {
   1060   1.1       cgd 		/*
   1061   1.1       cgd 		 * entire line processed
   1062   1.1       cgd 		 */
   1063   1.1       cgd 		*mor = 0;
   1064   1.1       cgd 		*cps = 0;
   1065   1.1       cgd 		return(col);
   1066   1.1       cgd 	}
   1067   1.1       cgd 
   1068   1.1       cgd 	/*
   1069   1.1       cgd 	 * line was larger than limit
   1070   1.1       cgd 	 */
   1071   1.1       cgd 	if (trnc) {
   1072   1.1       cgd 		/*
   1073   1.1       cgd 		 * throw away rest of line
   1074   1.1       cgd 		 */
   1075   1.1       cgd 		while ((ch = getc(inf)) != EOF) {
   1076   1.1       cgd 			if (ch == '\n')
   1077   1.1       cgd 				break;
   1078   1.1       cgd 		}
   1079   1.1       cgd 		*cps = 0;
   1080   1.1       cgd 		*mor = 0;
   1081   1.1       cgd 	} else {
   1082   1.1       cgd 		/*
   1083   1.1       cgd 		 * save column offset if not truncated
   1084   1.1       cgd 		 */
   1085   1.1       cgd 		*cps += col;
   1086   1.1       cgd 		*mor = 1;
   1087   1.1       cgd 	}
   1088   1.1       cgd 
   1089   1.1       cgd 	return(col);
   1090   1.1       cgd }
   1091   1.1       cgd 
   1092   1.1       cgd /*
   1093   1.1       cgd  * otln():	output a line of data. (Supports unlimited length lines)
   1094   1.1       cgd  *		output is optionally contracted to tabs
   1095   1.1       cgd  *
   1096   1.1       cgd  *	buf:	output buffer with data
   1097   1.1       cgd  *	cnt:	number of chars of valid data in buf
   1098   1.1       cgd  *	svips:	buffer input column position (for large lines)
   1099   1.1       cgd  *	svops:	buffer output column position (for large lines)
   1100   1.1       cgd  *	mor:	output line not complete in this buf; more data to come.
   1101   1.1       cgd  *		1 is more, 0 is complete, -1 is no \n's
   1102   1.1       cgd  */
   1103  1.21     joerg static int
   1104  1.21     joerg otln(char *buf, int cnt, int *svips, int *svops, int mor)
   1105   1.1       cgd {
   1106   1.5     lukem 	int ops;		/* last col output */
   1107   1.5     lukem 	int ips;		/* last col in buf examined */
   1108   1.5     lukem 	int gap = ogap;
   1109   1.5     lukem 	int tbps;
   1110   1.5     lukem 	char *endbuf;
   1111   1.1       cgd 
   1112   1.1       cgd 	if (ogap) {
   1113   1.1       cgd 		/*
   1114   1.1       cgd 		 * contracting on output
   1115   1.1       cgd 		 */
   1116   1.1       cgd 		endbuf = buf + cnt;
   1117   1.1       cgd 		ops = *svops;
   1118   1.1       cgd 		ips = *svips;
   1119   1.1       cgd 		while (buf < endbuf) {
   1120   1.1       cgd 			/*
   1121   1.1       cgd 			 * count number of spaces and ochar in buffer
   1122   1.1       cgd 			 */
   1123   1.1       cgd 			if (*buf == ' ') {
   1124   1.1       cgd 				++ips;
   1125   1.1       cgd 				++buf;
   1126   1.1       cgd 				continue;
   1127   1.1       cgd 			}
   1128   1.1       cgd 
   1129   1.1       cgd 			/*
   1130   1.1       cgd 			 * simulate ochar processing
   1131   1.1       cgd 			 */
   1132   1.1       cgd 			if (*buf == ochar) {
   1133   1.1       cgd 				ips += gap - (ips % gap);
   1134   1.1       cgd 				++buf;
   1135   1.1       cgd 				continue;
   1136   1.1       cgd 			}
   1137   1.1       cgd 
   1138   1.1       cgd 			/*
   1139   1.1       cgd 			 * got a non space char; contract out spaces
   1140   1.1       cgd 			 */
   1141  1.22  christos 			while (ips - ops > 1) {
   1142   1.8    simonb 				/*
   1143   1.1       cgd 				 * use as many ochar as will fit
   1144   1.1       cgd 				 */
   1145   1.1       cgd 				if ((tbps = ops + gap - (ops % gap)) > ips)
   1146   1.1       cgd 					break;
   1147   1.1       cgd 				if (putchar(ochar) == EOF) {
   1148   1.1       cgd 					pfail();
   1149   1.1       cgd 					return(1);
   1150   1.1       cgd 				}
   1151   1.1       cgd 				ops = tbps;
   1152   1.1       cgd 			}
   1153   1.1       cgd 
   1154   1.1       cgd 			while (ops < ips) {
   1155   1.1       cgd 				/*
   1156   1.1       cgd 				 * finish off with spaces
   1157   1.1       cgd 				 */
   1158   1.1       cgd 				if (putchar(' ') == EOF) {
   1159   1.1       cgd 					pfail();
   1160   1.1       cgd 					return(1);
   1161   1.1       cgd 				}
   1162   1.1       cgd 				++ops;
   1163   1.1       cgd 			}
   1164   1.1       cgd 
   1165   1.1       cgd 			/*
   1166   1.1       cgd 			 * output non space char
   1167   1.1       cgd 			 */
   1168   1.1       cgd 			if (putchar(*buf++) == EOF) {
   1169   1.1       cgd 				pfail();
   1170   1.1       cgd 				return(1);
   1171   1.1       cgd 			}
   1172   1.1       cgd 			++ips;
   1173   1.1       cgd 			++ops;
   1174   1.1       cgd 		}
   1175   1.1       cgd 
   1176   1.1       cgd 		if (mor > 0) {
   1177   1.1       cgd 			/*
   1178   1.1       cgd 			 * if incomplete line, save position counts
   1179   1.1       cgd 			 */
   1180   1.1       cgd 			*svops = ops;
   1181   1.1       cgd 			*svips = ips;
   1182   1.1       cgd 			return(0);
   1183   1.1       cgd 		}
   1184   1.1       cgd 
   1185   1.1       cgd 		if (mor < 0) {
   1186  1.22  christos 			while (ips - ops > 1) {
   1187   1.1       cgd 				/*
   1188   1.1       cgd 				 * use as many ochar as will fit
   1189   1.1       cgd 				 */
   1190   1.1       cgd 				if ((tbps = ops + gap - (ops % gap)) > ips)
   1191   1.1       cgd 					break;
   1192   1.1       cgd 				if (putchar(ochar) == EOF) {
   1193   1.1       cgd 					pfail();
   1194   1.1       cgd 					return(1);
   1195   1.1       cgd 				}
   1196   1.1       cgd 				ops = tbps;
   1197   1.1       cgd 			}
   1198   1.1       cgd 			while (ops < ips) {
   1199   1.1       cgd 				/*
   1200   1.1       cgd 				 * finish off with spaces
   1201   1.1       cgd 				 */
   1202   1.1       cgd 				if (putchar(' ') == EOF) {
   1203   1.1       cgd 					pfail();
   1204   1.1       cgd 					return(1);
   1205   1.1       cgd 				}
   1206   1.1       cgd 				++ops;
   1207   1.1       cgd 			}
   1208   1.1       cgd 			return(0);
   1209   1.1       cgd 		}
   1210   1.1       cgd 	} else {
   1211   1.1       cgd 		/*
   1212   1.1       cgd 		 * output is not contracted
   1213   1.1       cgd 		 */
   1214   1.1       cgd 		if (cnt && (fwrite(buf, sizeof(char), cnt, stdout) <= 0)) {
   1215   1.1       cgd 			pfail();
   1216   1.1       cgd 			return(1);
   1217   1.1       cgd 		}
   1218   1.1       cgd 		if (mor != 0)
   1219   1.1       cgd 			return(0);
   1220   1.1       cgd 	}
   1221   1.1       cgd 
   1222   1.1       cgd 	/*
   1223   1.1       cgd 	 * process line end and double space as required
   1224   1.1       cgd 	 */
   1225   1.1       cgd 	if ((putchar('\n') == EOF) || (dspace && (putchar('\n') == EOF))) {
   1226   1.1       cgd 		pfail();
   1227   1.1       cgd 		return(1);
   1228   1.1       cgd 	}
   1229   1.1       cgd 	return(0);
   1230   1.1       cgd }
   1231   1.1       cgd 
   1232   1.1       cgd /*
   1233   1.1       cgd  * inskip():	skip over pgcnt pages with lncnt lines per page
   1234   1.1       cgd  *		file is closed at EOF (if not stdin).
   1235   1.1       cgd  *
   1236   1.1       cgd  *	inf	FILE * to read from
   1237   1.1       cgd  *	pgcnt	number of pages to skip
   1238   1.1       cgd  *	lncnt	number of lines per page
   1239   1.1       cgd  */
   1240  1.21     joerg static int
   1241  1.21     joerg inskip(FILE *inf, int pgcnt, int lncnt)
   1242   1.1       cgd {
   1243   1.5     lukem 	int c;
   1244   1.5     lukem 	int cnt;
   1245   1.1       cgd 
   1246   1.1       cgd 	while(--pgcnt > 0) {
   1247   1.1       cgd 		cnt = lncnt;
   1248   1.1       cgd 		while ((c = getc(inf)) != EOF) {
   1249   1.1       cgd 			if ((c == '\n') && (--cnt == 0))
   1250   1.1       cgd 				break;
   1251   1.1       cgd 		}
   1252   1.1       cgd 		if (c == EOF) {
   1253   1.1       cgd 			if (inf != stdin)
   1254   1.1       cgd 				(void)fclose(inf);
   1255   1.1       cgd 			return(1);
   1256   1.1       cgd 		}
   1257   1.1       cgd 	}
   1258   1.1       cgd 	return(0);
   1259   1.1       cgd }
   1260   1.1       cgd 
   1261   1.1       cgd /*
   1262   1.1       cgd  * nxtfile:	returns a FILE * to next file in arg list and sets the
   1263   1.1       cgd  *		time field for this file (or current date).
   1264   1.1       cgd  *
   1265   1.1       cgd  *	buf	array to store proper date for the header.
   1266   1.1       cgd  *	dt	if set skips the date processing (used with -m)
   1267   1.1       cgd  */
   1268  1.21     joerg static FILE *
   1269  1.21     joerg nxtfile(int argc, char **argv, const char **fname, char *buf, int dt)
   1270   1.1       cgd {
   1271   1.1       cgd 	FILE *inf = NULL;
   1272   1.1       cgd 	struct timeval tv;
   1273   1.1       cgd 	struct timezone tz;
   1274   1.1       cgd 	struct tm *timeptr = NULL;
   1275   1.1       cgd 	struct stat statbuf;
   1276   1.3       cgd 	time_t curtime;
   1277   1.1       cgd 	static int twice = -1;
   1278   1.1       cgd 
   1279   1.1       cgd 	++twice;
   1280   1.1       cgd 	if (eoptind >= argc) {
   1281   1.1       cgd 		/*
   1282   1.1       cgd 		 * no file listed; default, use standard input
   1283   1.1       cgd 		 */
   1284   1.1       cgd 		if (twice)
   1285   1.1       cgd 			return(NULL);
   1286   1.1       cgd 		clearerr(stdin);
   1287   1.1       cgd 		inf = stdin;
   1288   1.1       cgd 		if (header != NULL)
   1289   1.1       cgd 			*fname = header;
   1290   1.1       cgd 		else
   1291   1.1       cgd 			*fname = FNAME;
   1292   1.1       cgd 		if (nohead)
   1293   1.1       cgd 			return(inf);
   1294   1.1       cgd 		if (gettimeofday(&tv, &tz) < 0) {
   1295   1.1       cgd 			++errcnt;
   1296  1.21     joerg 			(void)fprintf(errf, "pr: cannot get time of day, %s\n",
   1297   1.1       cgd 				strerror(errno));
   1298   1.1       cgd 			eoptind = argc - 1;
   1299   1.1       cgd 			return(NULL);
   1300   1.1       cgd 		}
   1301   1.3       cgd 		curtime = tv.tv_sec;
   1302   1.3       cgd 		timeptr = localtime(&curtime);
   1303   1.1       cgd 	}
   1304   1.1       cgd 	for (; eoptind < argc; ++eoptind) {
   1305   1.1       cgd 		if (strcmp(argv[eoptind], "-") == 0) {
   1306   1.1       cgd 			/*
   1307   1.1       cgd 			 * process a "-" for filename
   1308   1.1       cgd 			 */
   1309   1.1       cgd 			clearerr(stdin);
   1310   1.1       cgd 			inf = stdin;
   1311   1.1       cgd 			if (header != NULL)
   1312   1.1       cgd 				*fname = header;
   1313   1.1       cgd 			else
   1314   1.1       cgd 				*fname = FNAME;
   1315   1.1       cgd 			++eoptind;
   1316   1.1       cgd 			if (nohead || (dt && twice))
   1317   1.1       cgd 				return(inf);
   1318   1.1       cgd 			if (gettimeofday(&tv, &tz) < 0) {
   1319   1.1       cgd 				++errcnt;
   1320  1.21     joerg 				(void)fprintf(errf,
   1321   1.1       cgd 					"pr: cannot get time of day, %s\n",
   1322   1.1       cgd 					strerror(errno));
   1323   1.1       cgd 				return(NULL);
   1324   1.1       cgd 			}
   1325   1.3       cgd 			curtime = tv.tv_sec;
   1326   1.3       cgd 			timeptr = localtime(&curtime);
   1327   1.1       cgd 		} else {
   1328   1.1       cgd 			/*
   1329   1.1       cgd 			 * normal file processing
   1330   1.1       cgd 			 */
   1331   1.1       cgd 			if ((inf = fopen(argv[eoptind], "r")) == NULL) {
   1332   1.1       cgd 				++errcnt;
   1333   1.1       cgd 				if (nodiag)
   1334   1.1       cgd 					continue;
   1335  1.21     joerg 				(void)fprintf(errf, "pr: Cannot open %s, %s\n",
   1336   1.1       cgd 					argv[eoptind], strerror(errno));
   1337   1.1       cgd 				continue;
   1338   1.1       cgd 			}
   1339   1.1       cgd 			if (header != NULL)
   1340   1.1       cgd 				*fname = header;
   1341   1.1       cgd 			else if (dt)
   1342   1.1       cgd 				*fname = FNAME;
   1343   1.1       cgd 			else
   1344   1.1       cgd 				*fname = argv[eoptind];
   1345   1.1       cgd 			++eoptind;
   1346   1.1       cgd 			if (nohead || (dt && twice))
   1347   1.1       cgd 				return(inf);
   1348   1.1       cgd 
   1349   1.1       cgd 			if (dt) {
   1350   1.1       cgd 				if (gettimeofday(&tv, &tz) < 0) {
   1351   1.1       cgd 					++errcnt;
   1352  1.21     joerg 					(void)fprintf(errf,
   1353   1.1       cgd 					     "pr: cannot get time of day, %s\n",
   1354   1.1       cgd 					     strerror(errno));
   1355   1.1       cgd 					return(NULL);
   1356   1.1       cgd 				}
   1357   1.3       cgd 				curtime = tv.tv_sec;
   1358   1.3       cgd 				timeptr = localtime(&curtime);
   1359   1.1       cgd 			} else {
   1360   1.1       cgd 				if (fstat(fileno(inf), &statbuf) < 0) {
   1361   1.1       cgd 					++errcnt;
   1362   1.1       cgd 					(void)fclose(inf);
   1363  1.21     joerg 					(void)fprintf(errf,
   1364   1.1       cgd 						"pr: Cannot stat %s, %s\n",
   1365   1.1       cgd 						argv[eoptind], strerror(errno));
   1366   1.1       cgd 					return(NULL);
   1367   1.1       cgd 				}
   1368   1.1       cgd 				timeptr = localtime(&(statbuf.st_mtime));
   1369   1.1       cgd 			}
   1370   1.1       cgd 		}
   1371   1.1       cgd 		break;
   1372   1.1       cgd 	}
   1373   1.1       cgd 	if (inf == NULL)
   1374   1.1       cgd 		return(NULL);
   1375   1.1       cgd 
   1376   1.1       cgd 	/*
   1377   1.1       cgd 	 * set up time field used in header
   1378   1.1       cgd 	 */
   1379   1.1       cgd 	if (strftime(buf, HDBUF, timefrmt, timeptr) <= 0) {
   1380   1.1       cgd 		++errcnt;
   1381   1.1       cgd 		if (inf != stdin)
   1382   1.1       cgd 			(void)fclose(inf);
   1383  1.21     joerg 		(void)fputs("pr: time conversion failed\n", errf);
   1384   1.1       cgd 		return(NULL);
   1385   1.1       cgd 	}
   1386   1.1       cgd 	return(inf);
   1387   1.1       cgd }
   1388   1.1       cgd 
   1389   1.1       cgd /*
   1390   1.1       cgd  * addnum():	adds the line number to the column
   1391   1.1       cgd  *		Truncates from the front or pads with spaces as required.
   1392   1.1       cgd  *		Numbers are right justified.
   1393   1.1       cgd  *
   1394   1.1       cgd  *	buf	buffer to store the number
   1395   1.1       cgd  *	wdth	width of buffer to fill
   1396   1.1       cgd  *	line	line number
   1397   1.1       cgd  *
   1398   1.1       cgd  *		NOTE: numbers occupy part of the column. The posix
   1399   1.1       cgd  *		spec does not specify if -i processing should or should not
   1400   1.1       cgd  *		occur on number padding. The spec does say it occupies
   1401   1.1       cgd  *		part of the column. The usage of addnum	currently treats
   1402   1.1       cgd  *		numbers as part of the column so spaces may be replaced.
   1403   1.1       cgd  */
   1404   1.1       cgd void
   1405  1.21     joerg addnum(char *buf, int wdth, int line)
   1406   1.1       cgd {
   1407   1.5     lukem 	char *pt = buf + wdth;
   1408   1.1       cgd 
   1409   1.1       cgd 	do {
   1410   1.1       cgd 		*--pt = digs[line % 10];
   1411   1.1       cgd 		line /= 10;
   1412   1.1       cgd 	} while (line && (pt > buf));
   1413   1.1       cgd 
   1414   1.1       cgd 	/*
   1415   1.1       cgd 	 * pad with space as required
   1416   1.1       cgd 	 */
   1417   1.1       cgd 	while (pt > buf)
   1418   1.1       cgd 		*--pt = ' ';
   1419   1.1       cgd }
   1420   1.1       cgd 
   1421   1.1       cgd /*
   1422   1.1       cgd  * prhead():	prints the top of page header
   1423   1.1       cgd  *
   1424   1.1       cgd  *	buf	buffer with time field (and offset)
   1425   1.1       cgd  *	cnt	number of chars in buf
   1426   1.1       cgd  *	fname	fname field for header
   1427   1.1       cgd  *	pagcnt	page number
   1428   1.1       cgd  */
   1429  1.21     joerg static int
   1430  1.21     joerg prhead(char *buf, const char *fname, int pagcnt)
   1431   1.1       cgd {
   1432   1.1       cgd 	int ips = 0;
   1433   1.1       cgd 	int ops = 0;
   1434   1.1       cgd 
   1435   1.1       cgd 	if ((putchar('\n') == EOF) || (putchar('\n') == EOF)) {
   1436   1.1       cgd 		pfail();
   1437   1.1       cgd 		return(1);
   1438   1.1       cgd 	}
   1439   1.1       cgd 	/*
   1440   1.1       cgd 	 * posix is not clear if the header is subject to line length
   1441   1.1       cgd 	 * restrictions. The specification for header line format
   1442   1.1       cgd 	 * in the spec clearly does not limit length. No pr currently
   1443   1.1       cgd 	 * restricts header length. However if we need to truncate in
   1444   1.1       cgd 	 * an reasonable way, adjust the length of the printf by
   1445  1.17   msaitoh 	 * changing HDFMT to allow a length max as an argument printf.
   1446   1.1       cgd 	 * buf (which contains the offset spaces and time field could
   1447   1.1       cgd 	 * also be trimmed
   1448   1.1       cgd 	 *
   1449   1.1       cgd 	 * note only the offset (if any) is processed for tab expansion
   1450   1.1       cgd 	 */
   1451   1.1       cgd 	if (offst && otln(buf, offst, &ips, &ops, -1))
   1452   1.1       cgd 		return(1);
   1453   1.1       cgd 	(void)printf(HDFMT,buf+offst, fname, pagcnt);
   1454   1.1       cgd 	return(0);
   1455   1.1       cgd }
   1456   1.1       cgd 
   1457   1.1       cgd /*
   1458   1.1       cgd  * prtail():	pad page with empty lines (if required) and print page trailer
   1459   1.1       cgd  *		if requested
   1460   1.1       cgd  *
   1461   1.1       cgd  *	cnt	number of lines of padding needed
   1462   1.1       cgd  *	incomp	was a '\n' missing from last line output
   1463   1.1       cgd  */
   1464  1.21     joerg static int
   1465  1.21     joerg prtail(int cnt, int incomp)
   1466   1.1       cgd {
   1467   1.1       cgd 	if (nohead) {
   1468   1.1       cgd 		/*
   1469   1.1       cgd 		 * only pad with no headers when incomplete last line
   1470   1.1       cgd 		 */
   1471   1.1       cgd 		if (!incomp)
   1472   1.1       cgd 			return(0);
   1473   1.1       cgd 		if ((dspace && (putchar('\n') == EOF)) ||
   1474   1.1       cgd 		    (putchar('\n') == EOF)) {
   1475   1.1       cgd 			pfail();
   1476   1.1       cgd 			return(1);
   1477   1.1       cgd 		}
   1478   1.1       cgd 		return(0);
   1479   1.1       cgd 	}
   1480   1.1       cgd 
   1481   1.1       cgd 	/*
   1482   1.1       cgd 	 * if double space output two \n
   1483   1.1       cgd 	 */
   1484   1.1       cgd 	if (dspace)
   1485   1.1       cgd 		cnt *= 2;
   1486   1.1       cgd 
   1487   1.1       cgd 	/*
   1488   1.1       cgd 	 * if an odd number of lines per page, add an extra \n
   1489   1.1       cgd 	 */
   1490   1.1       cgd 	if (addone)
   1491   1.1       cgd 		++cnt;
   1492   1.1       cgd 
   1493   1.1       cgd 	/*
   1494   1.1       cgd 	 * pad page
   1495   1.1       cgd 	 */
   1496   1.1       cgd 	if (formfeed) {
   1497   1.1       cgd 		if ((incomp && (putchar('\n') == EOF)) ||
   1498   1.1       cgd 		    (putchar('\f') == EOF)) {
   1499   1.1       cgd 			pfail();
   1500   1.1       cgd 			return(1);
   1501   1.1       cgd 		}
   1502   1.1       cgd 		return(0);
   1503   1.1       cgd 	}
   1504   1.1       cgd 	cnt += TAILLEN;
   1505   1.1       cgd 	while (--cnt >= 0) {
   1506   1.1       cgd 		if (putchar('\n') == EOF) {
   1507   1.1       cgd 			pfail();
   1508   1.1       cgd 			return(1);
   1509   1.1       cgd 		}
   1510   1.1       cgd 	}
   1511   1.1       cgd 	return(0);
   1512   1.1       cgd }
   1513   1.1       cgd 
   1514   1.1       cgd /*
   1515   1.1       cgd  * terminate():	when a SIGINT is recvd
   1516   1.1       cgd  */
   1517  1.21     joerg static void
   1518  1.21     joerg terminate(int which_sig)
   1519   1.1       cgd {
   1520   1.1       cgd 	flsh_errs();
   1521  1.18     lukem 	(void)raise_default_signal(which_sig);
   1522   1.1       cgd 	exit(1);
   1523   1.1       cgd }
   1524   1.1       cgd 
   1525   1.1       cgd 
   1526   1.1       cgd /*
   1527   1.1       cgd  * flsh_errs():	output saved up diagnostic messages after all normal
   1528   1.1       cgd  *		processing has completed
   1529   1.1       cgd  */
   1530  1.21     joerg static void
   1531  1.21     joerg flsh_errs(void)
   1532   1.1       cgd {
   1533   1.1       cgd 	char buf[BUFSIZ];
   1534   1.1       cgd 
   1535   1.1       cgd 	(void)fflush(stdout);
   1536  1.21     joerg 	(void)fflush(errf);
   1537  1.21     joerg 	if (errf == stderr)
   1538   1.1       cgd 		return;
   1539  1.21     joerg 	rewind(errf);
   1540  1.21     joerg 	while (fgets(buf, BUFSIZ, errf) != NULL)
   1541   1.1       cgd 		(void)fputs(buf, stderr);
   1542   1.1       cgd }
   1543   1.1       cgd 
   1544  1.21     joerg static void
   1545  1.21     joerg mfail(void)
   1546   1.1       cgd {
   1547  1.21     joerg 	(void)fputs("pr: memory allocation failed\n", errf);
   1548   1.1       cgd }
   1549   1.1       cgd 
   1550  1.21     joerg static void
   1551  1.21     joerg pfail(void)
   1552   1.1       cgd {
   1553  1.21     joerg 	(void)fprintf(errf, "pr: write failure, %s\n", strerror(errno));
   1554   1.1       cgd }
   1555   1.1       cgd 
   1556  1.21     joerg static void
   1557  1.21     joerg usage(void)
   1558   1.1       cgd {
   1559   1.1       cgd 	(void)fputs(
   1560  1.21     joerg 	 "usage: pr [+page] [-col] [-adFmrt] [-e[ch][gap]] [-h header]\n",errf);
   1561   1.1       cgd 	(void)fputs(
   1562  1.21     joerg 	 "          [-i[ch][gap]] [-l line] [-n[ch][width]] [-o offset]\n",errf);
   1563   1.1       cgd 	(void)fputs(
   1564  1.21     joerg 	 "          [-s[ch]] [-w width] [-] [file ...]\n", errf);
   1565   1.1       cgd }
   1566   1.1       cgd 
   1567   1.1       cgd /*
   1568   1.1       cgd  * setup:	Validate command args, initialize and perform sanity
   1569   1.1       cgd  *		checks on options
   1570   1.1       cgd  */
   1571  1.21     joerg static int
   1572  1.21     joerg setup(int argc, char **argv)
   1573   1.1       cgd {
   1574   1.5     lukem 	int c;
   1575   1.1       cgd 	int eflag = 0;
   1576   1.1       cgd 	int iflag = 0;
   1577   1.1       cgd 	int wflag = 0;
   1578   1.1       cgd 	int cflag = 0;
   1579   1.1       cgd 
   1580   1.1       cgd 	if (isatty(fileno(stdout))) {
   1581   1.1       cgd 		/*
   1582   1.1       cgd 		 * defer diagnostics until processing is done
   1583   1.1       cgd 		 */
   1584  1.21     joerg 		if ((errf = tmpfile()) == NULL) {
   1585   1.1       cgd 		       (void)fputs("Cannot defer diagnostic messages\n",stderr);
   1586   1.1       cgd 		       return(1);
   1587   1.1       cgd 		}
   1588   1.1       cgd 	} else
   1589  1.21     joerg 		errf = stderr;
   1590   1.9    kleink 	while ((c = egetopt(argc, argv, "#adFmrte?h:i?l:n?o:s?T:w:")) != -1) {
   1591   1.1       cgd 		switch (c) {
   1592   1.1       cgd 		case '+':
   1593   1.1       cgd 			if ((pgnm = atoi(eoptarg)) < 1) {
   1594   1.1       cgd 			    (void)fputs("pr: +page number must be 1 or more\n",
   1595  1.21     joerg 				errf);
   1596   1.1       cgd 			    return(1);
   1597   1.1       cgd 			}
   1598   1.1       cgd 			break;
   1599   1.1       cgd 		case '-':
   1600   1.1       cgd 			if ((clcnt = atoi(eoptarg)) < 1) {
   1601  1.21     joerg 			    (void)fputs("pr: -columns must be 1 or more\n",errf);
   1602   1.1       cgd 			    return(1);
   1603   1.1       cgd 			}
   1604   1.1       cgd 			if (clcnt > 1)
   1605   1.1       cgd 				++cflag;
   1606   1.1       cgd 			break;
   1607   1.1       cgd 		case 'a':
   1608   1.1       cgd 			++across;
   1609   1.1       cgd 			break;
   1610   1.1       cgd 		case 'd':
   1611   1.1       cgd 			++dspace;
   1612   1.1       cgd 			break;
   1613   1.1       cgd 		case 'e':
   1614   1.1       cgd 			++eflag;
   1615   1.7  christos 			if ((eoptarg != NULL) &&
   1616   1.7  christos 			    !isdigit((unsigned char)*eoptarg))
   1617   1.1       cgd 				inchar = *eoptarg++;
   1618   1.1       cgd 			else
   1619   1.1       cgd 				inchar = INCHAR;
   1620   1.7  christos 			if ((eoptarg != NULL) &&
   1621   1.7  christos 			    isdigit((unsigned char)*eoptarg)) {
   1622   1.1       cgd 				if ((ingap = atoi(eoptarg)) < 0) {
   1623   1.1       cgd 					(void)fputs(
   1624  1.21     joerg 					"pr: -e gap must be 0 or more\n", errf);
   1625   1.1       cgd 					return(1);
   1626   1.1       cgd 				}
   1627   1.1       cgd 				if (ingap == 0)
   1628   1.1       cgd 					ingap = INGAP;
   1629   1.1       cgd 			} else if ((eoptarg != NULL) && (*eoptarg != '\0')) {
   1630  1.21     joerg 				(void)fprintf(errf,
   1631   1.1       cgd 				      "pr: invalid value for -e %s\n", eoptarg);
   1632   1.1       cgd 				return(1);
   1633   1.1       cgd 			} else
   1634   1.1       cgd 				ingap = INGAP;
   1635   1.1       cgd 			break;
   1636   1.1       cgd 		case 'F':
   1637   1.1       cgd 			++formfeed;
   1638   1.1       cgd 			break;
   1639   1.1       cgd 		case 'h':
   1640   1.1       cgd 			header = eoptarg;
   1641   1.1       cgd 			break;
   1642   1.1       cgd 		case 'i':
   1643   1.1       cgd 			++iflag;
   1644   1.7  christos 			if ((eoptarg != NULL) &&
   1645   1.7  christos 			    !isdigit((unsigned char)*eoptarg))
   1646   1.1       cgd 				ochar = *eoptarg++;
   1647   1.1       cgd 			else
   1648   1.1       cgd 				ochar = OCHAR;
   1649   1.7  christos 			if ((eoptarg != NULL) &&
   1650   1.7  christos 			    isdigit((unsigned char)*eoptarg)) {
   1651   1.1       cgd 				if ((ogap = atoi(eoptarg)) < 0) {
   1652   1.1       cgd 					(void)fputs(
   1653  1.21     joerg 					"pr: -i gap must be 0 or more\n", errf);
   1654   1.1       cgd 					return(1);
   1655   1.1       cgd 				}
   1656   1.1       cgd 				if (ogap == 0)
   1657   1.1       cgd 					ogap = OGAP;
   1658   1.1       cgd 			} else if ((eoptarg != NULL) && (*eoptarg != '\0')) {
   1659  1.21     joerg 				(void)fprintf(errf,
   1660   1.1       cgd 				      "pr: invalid value for -i %s\n", eoptarg);
   1661   1.1       cgd 				return(1);
   1662   1.1       cgd 			} else
   1663   1.1       cgd 				ogap = OGAP;
   1664   1.1       cgd 			break;
   1665   1.1       cgd 		case 'l':
   1666   1.7  christos 			if (!isdigit((unsigned char)*eoptarg) ||
   1667   1.7  christos 			    ((lines=atoi(eoptarg)) < 1)) {
   1668   1.1       cgd 				(void)fputs(
   1669  1.21     joerg 				 "pr: Number of lines must be 1 or more\n",errf);
   1670   1.1       cgd 				return(1);
   1671   1.1       cgd 			}
   1672   1.1       cgd 			break;
   1673   1.1       cgd 		case 'm':
   1674   1.1       cgd 			++merge;
   1675   1.1       cgd 			break;
   1676   1.1       cgd 		case 'n':
   1677   1.7  christos 			if ((eoptarg != NULL) &&
   1678   1.7  christos 			    !isdigit((unsigned char)*eoptarg))
   1679   1.1       cgd 				nmchar = *eoptarg++;
   1680   1.1       cgd 			else
   1681   1.1       cgd 				nmchar = NMCHAR;
   1682   1.7  christos 			if ((eoptarg != NULL) &&
   1683   1.7  christos 			    isdigit((unsigned char)*eoptarg)) {
   1684   1.1       cgd 				if ((nmwd = atoi(eoptarg)) < 1) {
   1685   1.1       cgd 					(void)fputs(
   1686  1.21     joerg 					"pr: -n width must be 1 or more\n",errf);
   1687   1.1       cgd 					return(1);
   1688   1.1       cgd 				}
   1689   1.1       cgd 			} else if ((eoptarg != NULL) && (*eoptarg != '\0')) {
   1690  1.21     joerg 				(void)fprintf(errf,
   1691   1.1       cgd 				      "pr: invalid value for -n %s\n", eoptarg);
   1692   1.1       cgd 				return(1);
   1693   1.1       cgd 			} else
   1694   1.1       cgd 				nmwd = NMWD;
   1695   1.1       cgd 			break;
   1696   1.1       cgd 		case 'o':
   1697   1.7  christos 			if (!isdigit((unsigned char)*eoptarg) ||
   1698   1.7  christos 			    ((offst = atoi(eoptarg))< 1)){
   1699   1.1       cgd 				(void)fputs("pr: -o offset must be 1 or more\n",
   1700  1.21     joerg 					errf);
   1701   1.1       cgd 				return(1);
   1702   1.1       cgd 			}
   1703   1.1       cgd 			break;
   1704   1.1       cgd 		case 'r':
   1705   1.1       cgd 			++nodiag;
   1706   1.1       cgd 			break;
   1707   1.1       cgd 		case 's':
   1708   1.1       cgd 			++sflag;
   1709   1.1       cgd 			if (eoptarg == NULL)
   1710   1.1       cgd 				schar = SCHAR;
   1711   1.1       cgd 			else
   1712   1.1       cgd 				schar = *eoptarg++;
   1713  1.13  christos 			if (eoptarg && *eoptarg != '\0') {
   1714  1.21     joerg 				(void)fprintf(errf,
   1715   1.1       cgd 				      "pr: invalid value for -s %s\n", eoptarg);
   1716   1.1       cgd 				return(1);
   1717   1.1       cgd 			}
   1718   1.1       cgd 			break;
   1719   1.9    kleink 		case 'T':
   1720   1.9    kleink 			timefrmt = eoptarg;
   1721   1.9    kleink 			break;
   1722   1.1       cgd 		case 't':
   1723   1.1       cgd 			++nohead;
   1724   1.1       cgd 			break;
   1725   1.1       cgd 		case 'w':
   1726   1.1       cgd 			++wflag;
   1727   1.7  christos 			if (!isdigit((unsigned char)*eoptarg) ||
   1728   1.7  christos 			    ((pgwd = atoi(eoptarg)) < 1)){
   1729   1.1       cgd 				(void)fputs(
   1730  1.21     joerg 				   "pr: -w width must be 1 or more \n",errf);
   1731   1.1       cgd 				return(1);
   1732   1.1       cgd 			}
   1733   1.1       cgd 			break;
   1734   1.1       cgd 		case '?':
   1735   1.1       cgd 		default:
   1736   1.1       cgd 			return(1);
   1737   1.1       cgd 		}
   1738   1.1       cgd 	}
   1739   1.1       cgd 
   1740   1.1       cgd 	/*
   1741   1.1       cgd 	 * default and sanity checks
   1742   1.1       cgd 	 */
   1743   1.1       cgd 	if (!clcnt) {
   1744   1.1       cgd 		if (merge) {
   1745   1.1       cgd 			if ((clcnt = argc - eoptind) <= 1) {
   1746   1.1       cgd 				clcnt = CLCNT;
   1747   1.1       cgd 				merge = 0;
   1748   1.1       cgd 			}
   1749   1.1       cgd 		} else
   1750   1.1       cgd 			clcnt = CLCNT;
   1751   1.1       cgd 	}
   1752   1.1       cgd 	if (across) {
   1753   1.1       cgd 		if (clcnt == 1) {
   1754   1.1       cgd 			(void)fputs("pr: -a flag requires multiple columns\n",
   1755  1.21     joerg 				errf);
   1756   1.1       cgd 			return(1);
   1757   1.1       cgd 		}
   1758   1.1       cgd 		if (merge) {
   1759  1.21     joerg 			(void)fputs("pr: -m cannot be used with -a\n", errf);
   1760   1.1       cgd 			return(1);
   1761   1.1       cgd 		}
   1762   1.1       cgd 	}
   1763   1.1       cgd 	if (!wflag) {
   1764   1.1       cgd 		if (sflag)
   1765   1.1       cgd 			pgwd = SPGWD;
   1766   1.1       cgd 		else
   1767   1.1       cgd 			pgwd = PGWD;
   1768   1.1       cgd 	}
   1769   1.1       cgd 	if (cflag || merge) {
   1770   1.1       cgd 		if (!eflag) {
   1771   1.1       cgd 			inchar = INCHAR;
   1772   1.1       cgd 			ingap = INGAP;
   1773   1.1       cgd 		}
   1774   1.1       cgd 		if (!iflag) {
   1775   1.1       cgd 			ochar = OCHAR;
   1776   1.1       cgd 			ogap = OGAP;
   1777   1.1       cgd 		}
   1778   1.1       cgd 	}
   1779   1.1       cgd 	if (cflag) {
   1780   1.1       cgd 		if (merge) {
   1781   1.1       cgd 			(void)fputs(
   1782  1.21     joerg 			  "pr: -m cannot be used with multiple columns\n", errf);
   1783   1.1       cgd 			return(1);
   1784   1.1       cgd 		}
   1785   1.1       cgd 		if (nmwd) {
   1786   1.1       cgd 			colwd = (pgwd + 1 - (clcnt * (nmwd + 2)))/clcnt;
   1787   1.1       cgd 			pgwd = ((colwd + nmwd + 2) * clcnt) - 1;
   1788   1.1       cgd 		} else {
   1789   1.1       cgd 			colwd = (pgwd + 1 - clcnt)/clcnt;
   1790   1.1       cgd 			pgwd = ((colwd + 1) * clcnt) - 1;
   1791   1.1       cgd 		}
   1792   1.1       cgd 		if (colwd < 1) {
   1793  1.21     joerg 			(void)fprintf(errf,
   1794   1.1       cgd 			  "pr: page width is too small for %d columns\n",clcnt);
   1795   1.1       cgd 			return(1);
   1796   1.1       cgd 		}
   1797   1.1       cgd 	}
   1798   1.1       cgd 	if (!lines)
   1799   1.1       cgd 		lines = LINES;
   1800   1.1       cgd 
   1801   1.1       cgd 	/*
   1802   1.1       cgd 	 * make sure long enough for headers. if not disable
   1803   1.1       cgd 	 */
   1804   1.1       cgd 	if (lines <= HEADLEN + TAILLEN)
   1805   1.1       cgd 		++nohead;
   1806   1.1       cgd 	else if (!nohead)
   1807   1.1       cgd 		lines -= HEADLEN + TAILLEN;
   1808   1.1       cgd 
   1809   1.1       cgd 	/*
   1810   1.1       cgd 	 * adjust for double space on odd length pages
   1811   1.1       cgd 	 */
   1812   1.1       cgd 	if (dspace) {
   1813   1.1       cgd 		if (lines == 1)
   1814   1.1       cgd 			dspace = 0;
   1815   1.1       cgd 		else {
   1816   1.1       cgd 			if (lines & 1)
   1817   1.1       cgd 				++addone;
   1818   1.1       cgd 			lines /= 2;
   1819   1.1       cgd 		}
   1820   1.1       cgd 	}
   1821   1.1       cgd 
   1822   1.1       cgd 	return(0);
   1823   1.1       cgd }
   1824