Module Name:    src
Committed By:   ginsbach
Date:           Tue Jul 24 02:13:05 UTC 2012

Modified Files:
        src/usr.bin/pr: pr.1 pr.c pr.h

Log Message:
- Fix PR 19468 by adding -f and -p options to pr(1).
  Changes adapted from patch by Ryan Younce and FreeBSD.  The provided
  patch was not used directly as the changes didn't mimic the System V
  origins of these options.  System V pr(1) does not pause for empty files.
  These changes follow that precedent.

- Improve man page description of the -i option.


To generate a diff of this commit:
cvs rdiff -u -r1.18 -r1.19 src/usr.bin/pr/pr.1
cvs rdiff -u -r1.22 -r1.23 src/usr.bin/pr/pr.c
cvs rdiff -u -r1.4 -r1.5 src/usr.bin/pr/pr.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/pr/pr.1
diff -u src/usr.bin/pr/pr.1:1.18 src/usr.bin/pr/pr.1:1.19
--- src/usr.bin/pr/pr.1:1.18	Sun Apr  8 22:00:39 2012
+++ src/usr.bin/pr/pr.1	Tue Jul 24 02:13:04 2012
@@ -1,8 +1,10 @@
-.\"	$NetBSD: pr.1,v 1.18 2012/04/08 22:00:39 wiz Exp $
+.\"	$NetBSD: pr.1,v 1.19 2012/07/24 02:13:04 ginsbach Exp $
 .\"
 .\" Copyright (c) 1991 Keith Muller.
 .\" Copyright (c) 1993
 .\"	The Regents of the University of California.  All rights reserved.
+.\" Copyright (c) 1994-1995, 1997, 1999-2003, 2009, 2012
+.\"	The NetBSD Foundation, Inc.
 .\"
 .\" This code is derived from software contributed to Berkeley by
 .\" Keith Muller of the University of California, San Diego.
@@ -32,9 +34,9 @@
 .\" SUCH DAMAGE.
 .\"
 .\"     from: @(#)pr.1	8.1 (Berkeley) 6/6/93
-.\"	$NetBSD: pr.1,v 1.18 2012/04/08 22:00:39 wiz Exp $
+.\"	$NetBSD: pr.1,v 1.19 2012/07/24 02:13:04 ginsbach Exp $
 .\"
-.Dd June 6, 1993
+.Dd May 4, 2012
 .Dt PR 1
 .Os
 .Sh NAME
@@ -44,7 +46,7 @@
 .Nm
 .Op Ar \&+page
 .Op Fl Ar column
-.Op Fl adFmrt
+.Op Fl adFfmprt
 .Oo
 .Op Fl e
 .Op Ar char
@@ -161,6 +163,11 @@ instead of the default behavior that use
 sequence of
 .Em \*[Lt]newline\*[Gt]
 characters.
+.It Fl f
+Same as
+.Fl F .
+Additionally pause before beginning the first page
+if the standard output is associated with a terminal.
 .It Fl h Ar header
 Use the string
 .Ar header
@@ -184,6 +191,12 @@ If any nondigit character,
 is specified, it is used as the output
 .Em \*[Lt]tab\*[Gt]
 character.
+If the first character of
+.Ar char
+is a digit then
+.Ar char
+is treated as
+.Ar gap .
 .It Fl l Ar lines
 Override the 66 line default and reset the page length to
 .Ar lines .
@@ -239,6 +252,16 @@ If the
 .Fl o
 option is not specified, the default is zero.
 The space taken is in addition to the output line width.
+.It Fl p
+Pause before beginning each page if the
+standard output is associated with a terminal.
+.Nm
+will write an
+.Em \*[Lt]alert\*[Gt]
+to standard error and wait for a
+.Em \*[Lt]carriage-return\*[Gt]
+to be read on
+.Pa /dev/tty .
 .It Fl r
 Write no diagnostic reports on failure to open a file.
 .It Fl s Ar char
@@ -324,5 +347,5 @@ file printing is complete (when printing
 The
 .Nm
 utility is
-.St -p1003.2
+.St -p1003.1-2008
 compatible.

Index: src/usr.bin/pr/pr.c
diff -u src/usr.bin/pr/pr.c:1.22 src/usr.bin/pr/pr.c:1.23
--- src/usr.bin/pr/pr.c:1.22	Mon Mar 12 18:06:24 2012
+++ src/usr.bin/pr/pr.c	Tue Jul 24 02:13:04 2012
@@ -1,9 +1,11 @@
-/*	$NetBSD: pr.c,v 1.22 2012/03/12 18:06:24 christos Exp $	*/
+/*	$NetBSD: pr.c,v 1.23 2012/07/24 02:13:04 ginsbach Exp $	*/
 
 /*-
  * Copyright (c) 1991 Keith Muller.
  * Copyright (c) 1993
  *	The Regents of the University of California.  All rights reserved.
+ * Copyright (c) 2012
+ *	The NetBSD Foundation, Inc.
  *
  * This code is derived from software contributed to Berkeley by
  * Keith Muller of the University of California, San Diego.
@@ -43,7 +45,7 @@ __COPYRIGHT("@(#) Copyright (c) 1993\
 #if 0
 from: static char sccsid[] = "@(#)pr.c	8.1 (Berkeley) 6/6/93";
 #else
-__RCSID("$NetBSD: pr.c,v 1.22 2012/03/12 18:06:24 christos Exp $");
+__RCSID("$NetBSD: pr.c,v 1.23 2012/07/24 02:13:04 ginsbach Exp $");
 #endif
 #endif /* not lint */
 
@@ -96,9 +98,12 @@ static int	offst;			/* number of page of
 static int	nodiag;			/* do not report file open errors */
 static char	schar;			/* text column separation character */
 static int	sflag;			/* -s option for multiple columns */
+static int	ttyout;			/* output is a tty */
 static int	nohead;			/* do not write head and trailer */
+static int	pgpause;		/* pause before each page */
 static int	pgwd;			/* page width with multiple col output */
 static const char *timefrmt = TIMEFMT;	/* time conversion string */
+static FILE	*ttyinf;		/* input terminal for page pauses */
 
 /*
  * misc globals
@@ -120,6 +125,7 @@ static int	 onecol(int, char **);
 static int	 otln(char *, int, int *, int *, int);
 static void	 pfail(void);
 static int	 prhead(char *, const char *, int);
+static void	 prpause(int);
 static int	 prtail(int, int);
 static int	 setup(int, char **);
 __dead static void	 terminate(int);
@@ -242,9 +248,14 @@ onecol(int argc, char *argv[])
 				 */
 				if ((cnt = inln(inf,lbuf,LBUF,&cps,0,&mor)) < 0)
 					break;
-				if (!linecnt && !nohead &&
-					prhead(hbuf, fname, pagecnt))
-					goto out;
+				if (!linecnt) {
+					if (pgpause)
+						prpause(pagecnt);
+
+				        if (!nohead &&
+					    prhead(hbuf, fname, pagecnt))
+						goto out;
+				}
 
 				/*
 				 * start of new line.
@@ -509,6 +520,9 @@ vertcol(int argc, char *argv[])
 				if (cvc % clcnt)
 					++pln;
 
+				if (pgpause)
+					prpause(pagecnt);
+
 				/*
 				 * print header
 				 */
@@ -578,8 +592,13 @@ vertcol(int argc, char *argv[])
 			/*
 			 * print header
 			 */
-			if (pln && !nohead && prhead(hbuf, fname, pagecnt))
-				goto out;
+			if (pln) {
+				if (pgpause)
+					prpause(pagecnt);
+
+			        if (!nohead && prhead(hbuf, fname, pagecnt))
+					goto out;
+			}
 
 			/*
 			 * output each line
@@ -731,9 +750,14 @@ horzcol(int argc, char *argv[])
 				 */
 				if ((j = lstdat - buf) <= offst)
 					break;
-				if (!i && !nohead &&
-					prhead(hbuf, fname, pagecnt))
-					goto out;
+				if (!i) {
+					if (pgpause)
+						prpause(pagecnt);
+
+				        if (!nohead &&
+					    prhead(hbuf, fname, pagecnt))
+						goto out;
+				}
 				/*
 				 * output line
 				 */
@@ -941,8 +965,13 @@ mulfile(int argc, char *argv[])
 			if ((j = lstdat - buf) <= offst)
 				break;
 
-			if (!i && !nohead && prhead(hbuf, fname, pagecnt))
-				goto out;
+			if (!i) {
+				if (pgpause)
+					prpause(pagecnt);
+
+			        if (!nohead && prhead(hbuf, fname, pagecnt))
+					goto out;
+			}
 
 			/*
 			 * output line
@@ -1419,6 +1448,32 @@ addnum(char *buf, int wdth, int line)
 }
 
 /*
+ * prpause():	pause before printing each page
+ *
+ *	pagcnt	page number
+ */
+static void
+prpause(int pagcnt)
+{
+
+	if (ttyout) {
+		int c;
+
+		(void)putc('\a', stderr);
+		(void)fflush(stderr);
+
+		while ((c = getc(ttyinf)) != '\n' && c != EOF)
+			;
+
+		/*
+		 * pause ONLY before first page of first file
+		 */
+		if (pgpause == FIRSTPAGE && pagcnt == 1)
+			pgpause = NO_PAUSE;
+	}
+}
+
+/*
  * prhead():	prints the top of page header
  *
  *	buf	buffer with time field (and offset)
@@ -1561,7 +1616,7 @@ usage(void)
 	(void)fputs(
 	 "          [-i[ch][gap]] [-l line] [-n[ch][width]] [-o offset]\n",errf);
 	(void)fputs(
-	 "          [-s[ch]] [-w width] [-] [file ...]\n", errf);
+	 "          [-s[ch]] [-w width] [-fp] [-] [file ...]\n", errf);
 }
 
 /*
@@ -1577,6 +1632,8 @@ setup(int argc, char **argv)
 	int wflag = 0;
 	int cflag = 0;
 
+	ttyinf = stdin;
+
 	if (isatty(fileno(stdout))) {
 		/*
 		 * defer diagnostics until processing is done
@@ -1585,9 +1642,10 @@ setup(int argc, char **argv)
 		       (void)fputs("Cannot defer diagnostic messages\n",stderr);
 		       return(1);
 		}
+		ttyout = 1;
 	} else
 		errf = stderr;
-	while ((c = egetopt(argc, argv, "#adFmrte?h:i?l:n?o:s?T:w:")) != -1) {
+	while ((c = egetopt(argc, argv, "#adFfmrte?h:i?l:n?o:ps?T:w:")) != -1) {
 		switch (c) {
 		case '+':
 			if ((pgnm = atoi(eoptarg)) < 1) {
@@ -1633,6 +1691,9 @@ setup(int argc, char **argv)
 			} else
 				ingap = INGAP;
 			break;
+		case 'f':
+			pgpause |= FIRSTPAGE;
+			/*FALLTHROUGH*/
 		case 'F':
 			++formfeed;
 			break;
@@ -1701,6 +1762,9 @@ setup(int argc, char **argv)
 				return(1);
 			}
 			break;
+		case 'p':
+			pgpause |= EACHPAGE;
+			break;
 		case 'r':
 			++nodiag;
 			break;
@@ -1819,5 +1883,16 @@ setup(int argc, char **argv)
 		}
 	}
 
+	/*
+	 * open /dev/tty if we are to pause before each page
+	 * but only if stdout is a terminal and stdin is not a terminal
+	 */
+	if (ttyout && pgpause && !isatty(fileno(stdin))) {
+		if ((ttyinf = fopen("/dev/tty", "r")) == NULL) {
+			(void)fprintf(errf, "pr: cannot open terminal\n");
+			return(1);
+		}
+	}
+
 	return(0);
 }

Index: src/usr.bin/pr/pr.h
diff -u src/usr.bin/pr/pr.h:1.4 src/usr.bin/pr/pr.h:1.5
--- src/usr.bin/pr/pr.h:1.4	Mon Oct 13 07:41:22 2003
+++ src/usr.bin/pr/pr.h	Tue Jul 24 02:13:04 2012
@@ -1,9 +1,11 @@
-/*	$NetBSD: pr.h,v 1.4 2003/10/13 07:41:22 agc Exp $	*/
+/*	$NetBSD: pr.h,v 1.5 2012/07/24 02:13:04 ginsbach Exp $	*/
 
 /*-
  * Copyright (c) 1991 Keith Muller.
  * Copyright (c) 1993
  *	The Regents of the University of California.  All rights reserved.
+ * Copyright (c) 2012
+ *	The NetBSD Foundation, Inc.
  *
  * This code is derived from software contributed to Berkeley by
  * Keith Muller of the University of California, San Diego.
@@ -33,7 +35,7 @@
  * SUCH DAMAGE.
  *
  *      from: @(#)pr.h	8.1 (Berkeley) 6/6/93
- *	$NetBSD: pr.h,v 1.4 2003/10/13 07:41:22 agc Exp $
+ *	$NetBSD: pr.h,v 1.5 2012/07/24 02:13:04 ginsbach Exp $
  */
 
 /*
@@ -62,6 +64,12 @@
 #define	LBUF		8192
 #define	HDBUF		512
 
+/* when to pause before (for -f and -p options) */
+#define NO_PAUSE	0
+#define FIRSTPAGE	1
+#define ENSUINGPAGES	2
+#define EACHPAGE	(FIRSTPAGE | ENSUINGPAGES)
+
 /*
  * structure for vertical columns. Used to balance cols on last page
  */

Reply via email to