[comp.sources.misc] v17i072: pl - Write to printer attached to user's Terminal or PC, Part01/01

malc@equinox.unr.edu (Malcolm Carlock) (03/28/91)

Submitted-by: Malcolm Carlock <malc@equinox.unr.edu>
Posting-number: Volume 17, Issue 72
Archive-name: pl/part01

The following small set of shell scripts, of which "pl" (Print Local) is
the main part, comprise a utility for printing files and streams of data
to a printer attached to a user's terminal or PC.  PL uses the Unix host's
terminfo or termcap database to determine what codes to send to the terminal
to enable/disable the printer port.

Included is a set of printer control codes for the ten or so (most widely
used) terminal types in use at our site.  Adding support for new ones is
basically a matter of finding the proper control codes from a terminal's
programming guide and having someone add them to the host's terminfo/termcap
database.

The key to accessing the printer control codes easily is the SYSV command
"tput", which given a terminal capability name interprets the capability
string and sends it to the terminal (e.g., "tput so" turns on standout mode
on the terminal.)  For systems without this command, which uses the terminfo
database, source for a termcap-using semi-replacement called "tcput" is
supplied.  Tcput does the work required by PL, but doesn't support some other
options used by tput.

The main use of PL at our site at the moment is to allow people to print
mail messages directly to their terminal/PC printers (we mainly use Elm and
Mush, which easily allow calls to external programs such as PL).  However,
pl is usable from the command line, or from any program which can be made
to pipe output through an external program.

Enjoy,

Malcolm L. Carlock                      Internet:  malc@unr.edu
                                        UUCP:      unr!malc
                                        BITNET:    malc@equinox
---
# This is a shell archive.  Remove anything before this line,
# then unpack it by saving it in a file and typing "sh file".
#
# Wrapped by equinox!malc on Mon Mar 25 00:19:30 PST 1991
# Contents:  TERMS/ Makefile README pl pl-info pl.man prtoff.proto prton.proto
#	standend.proto standout.proto tcput.c tcput.man TERMS/README TERMS/f100
#	TERMS/heath-19 TERMS/qvt101 TERMS/tvi910+ TERMS/tvi920 TERMS/tvi925
#	TERMS/tvi950 TERMS/vt100 TERMS/vt102 TERMS/wy50
 
echo x - Makefile
sed 's/^@//' > "Makefile" <<'@//E*O*F Makefile//'
#CC = gcc
CC = cc
CFLAGS = -O

#
# if on a SYSV type system, leave these uncommented, comment out the next block
#
TPUT =	/usr/5bin/tput
PON =	mc5
POFF =	mc4
SO =	smso
SE =	rmso
TLIB =	termlib

#
# if on a BSD type system, leave these uncommented, comment out previous block
#
#TPUT =	/usr/local/tcput
#PON =	po
#POFF =	pf
#SO =	so
#SE =	se
#TLIB =	termcap

scripts:
	(echo $(TPUT) $(PON) '2>/dev/null' ; cat prton.proto) > prton
	(echo $(TPUT) $(POFF) '2>/dev/null' ; cat prtoff.proto) > prtoff
	(echo $(TPUT) $(SO) '2>/dev/null' ; cat standout.proto) > standout
	(echo $(TPUT) $(SE) '2>/dev/null' ; cat standend.proto) > standend
	chmod a+x prt*[nf] stan*[td]

tcput:	tcput.c
	$(CC) $(CFLAGS) -o tcput tcput.c -lcurses -l$(TLIB)

clean:
	rm -f prt*[nf] stan*[td] tcput
@//E*O*F Makefile//
chmod u=rw,g=r,o=r Makefile
 
echo x - README
sed 's/^@//' > "README" <<'@//E*O*F README//'
-----------------------------------------------------------------------------
  PL -- A utility for writing files and data streams to a printer attached
	to a Unix user's terminal or PC.
-----------------------------------------------------------------------------

PL uses information stored in a Unix system's terminfo or termcap database
to turn on a terminal's or a PC's printer port, send files or a data stream
to the terminal while the printer port is enabled, and when done, once again
turn off the terminal/PC's printer port.

At this site at the University of Nevada, Reno, the following terminal types
are currently supported:

	f100
	heath-19 (kermit)
	qvt101
	tvi910+, tvi920, tvi925, tvi950
	vt100 (procomm), vt102
	wy50

Any terminal whose printer port can be opened and closed from the host can
be supported by PL.  It's a matter of someone looking up the proper control
codes in the programming guide for the terminal, and having the system
administrator install the codes into the host's terminfo or termcap database.


MANIFEST:

TERMS/		Subdirectory containing terminfo-formatted printer
		  control entries for the abovementioned terminals.
		  Conversion to termcap format is discussed.
pl*		The PL command itself.  A bourne shell script.
pl-info*	A user-info blurb for PL users at our site.
pl.man		A PL man page.
prton*		Shell script that invokes TPUT to enable a printer port.
prtoff*		Shell script that invokes TPUT to disable a printer port.
README		This file.
standout*	Shell script that invokes TPUT to enable reverse video.
standend*	Shell script that invokes TPUT to disable reverse video.
tcput.c		A termcap-using semi-substitute for TPUT for non-SYSV-type
		  Unix systems.
tcput.man	A man page for TCPUT.
Makefile	Builds the prt* and stand* scripts to suit your system, and,
		if necessary, tcput.


BUILD/INSTALL:

Edit Makefile to suit your system, then run "make scripts" to build the prt*
and stand* scripts.  To build tcput, if needed, type "make tcput".

For further information, see pl.man and tcput.man.

Hope this helps (tm).

Malcolm Carlock, UNR CCS, 3/91
@//E*O*F README//
chmod u=rw,g=r,o=r README
 
echo mkdir - TERMS
mkdir TERMS
chmod u=rwx,g=rx,o=rx TERMS
 
echo x - pl
sed 's/^@//' > "pl" <<'@//E*O*F pl//'
#!/bin/sh
#
# pl		Print Local.  Print files or the standard input to a
#		printer connected to the user's terminal.  prton and
#		prtoff are used to invoke the appropriate printer-port
#		control sequences via tput or tcput.
#
#		Author:  Malcolm Carlock, UNR Campus Computing Services, 3/91
FF=yes
VB=yes
VVB=yes
DSP=cat
ECHO=/bin/echo
for i in $* ; do
	case $i in
	-p)	DSP=pr
		;;
	-f)	FF=
		;;
	-q)	VB=
		;;
	-Q)	VVB=
		VB=
		;;
	-2)	STDERR=yes
		;;
	-*)	echo usage: pl [-p] [-f] [file ... ]
		exit 1
		;;
	*)	FILES=$FILES" "$i
		;;
	esac
done
trap prtoff 2 3
if test $VB ; then
	echo -n Sending to printer attached to your terminal..." "
fi
prton
if test $? -ne 0 ; then
	exit $?
fi
if test $STDERR ; then
	$DSP $FILES 1>&2
else
	$DSP $FILES
fi
if test $DSP = cat ; then if test $FF ; then echo  ; fi ; fi
prtoff
if test $VB ; then
	echo done.
fi
if test $VVB ; then
	case $TERM in
	wy50*)	echo ""
		echo -n "$TERM: If using" ; standout ; echo -n vi ; standend
		echo -n "," ; standout ; echo -n elm ; standend
		echo " etc., redraw screen with ^L or ^R."
		echo ""
		;;
	esac
fi
@//E*O*F pl//
chmod u=rwx,g=rx,o=rx pl
 
echo x - pl-info
sed 's/^@//' > "pl-info" <<'@//E*O*F pl-info//'
ECHO="/bin/echo -s -n"
cat << EOF | more

`standout`pl`standend` is a command which allows text to be routed automatically to a user's
terminal-attached printer, without other action (such as special keystrokes
to enable the terminal's printer port) being required by the user.  `standout`pl`standend` can
be used from the command line to write files and program output to a user's
local printer;  it can also be used from within `standout`elm`standend`, `standout`mail/mush`standend` and `standout`rn/rrn`standend` to print mail and news messages in the same manner, as described below:

Enabling local print from ELM:

  The following line, added to the user's `standout`.elm/elmrc`standend` file, causes ELM's 'p'
  command to write the current mail message to the user's local printer:

    print = pl -q %s 1>&2

@From Mush (the default "mail" command on this system):

  The following lines, added to the user's `standout`.mushrc`standend` file, allow a 'P'
  command to be used to write the current mail message to the user's local
  printer:

    set rpager = \$pager
    cmd P "set pager = pl ; p ; set pager = \$rpager"

@From rn:

  The following line, added to the user's `standout`.rnmac`standend` file, allows a 'P'
  command to be used to write the current news article to the user's local
  printer:

     P	s | pl -q^J

  Where "^J" is a '^' character followed by 'J'.


The abovementioned ELM, Mush, and rn initialization files are all user-
modifiable.  However, if you wish for system staff to make any of these
changes for you, send mail to 'staff'.

For more information about the `standout`pl`standend` command, including a list of
supported terminal types, type "man pl" at the system prompt.

EOF
@//E*O*F pl-info//
chmod u=rwx,g=rx,o=rx pl-info
 
echo x - pl.man
sed 's/^@//' > "pl.man" <<'@//E*O*F pl.man//'
@.TH PL LOCAL "March 22, 1991"
@.UC
@.SH NAME
pl \- print to a user's terminal-attached printer
@.SH SYNOPSIS
@.B pl
@.I [-f] [-p] [-q] [-Q] [file ...]
@.SH DESCRIPTION
@.B pl
causes the contents of the listed files to be written to a printer directly
attached to the user's terminal or PC, if the user's terminal type is known
by the system to have the capability of supporting such an operation
("transparent print" or something close to it).  Terminal types currently
supported on this system are:
@.nf

	f100
	h19/heath/kermit
	qume
	Most, if not all, Televideo (tviXXX) terminals
	vt100*/vt102
	wy50*

	* see notes below
@.fi
@.ad

Other terminal types can be added as required, assuming that the terminal
can support remote printer control of some kind.  The above terminal types
were chosen as being representative of the majority in use at the University
of Nevada, Reno, as of March 1991.  If you wish to have a terminal type added
to this list, send mail to "staff".

If no filenames are specified on
@.B pl's
command line,
@.B pl's
standard input is sent to the printer.  This allows such uses as
@.I cmd | pl,
and allows
@.B pl
to be used as a "print spooler" for things like mail and news messages,
where the text is "spooled" to the user's personal, terminal-attached
printer rather than to the system line printer.  For example, in the
Elm(1) program, adding the line

	print = pl -q \%%s 1>&2

to the user's .elm/elmrc file
causes the 'p' command in Elm to send the current mail message to the
user's terminal-attached printer.

The following lines, added to a user's .mushrc file, allow the command 'P'
to be used in mush (the "mail" command on this system) for the same purpose:

@.nf
	set rpager = $pager
	cmd P "set pager = pl ; p ; set pager = $rpager"
@.fi
@.ad

The "standard" Berkeley mail program ("Mail" on this system) does not
support calls to external programs such as
@.B pl.

OPTIONS

-f  No formfeed is sent to the printer following the printed text.

-p  The text to be printed is filtered through pr(1).

-q  Omit chatty "printing in progress" messages.

-Q  Same as -q, but also omits the Wyse50 redraw suggestion.

If a KILL or INTERRUPT signal is received by
@.B pl
during execution,
the terminal's printer port is turned off again before
@.B pl
exits.
@.SH SEE ALSO
tput(1V), terminfo(5)
@.SH DIAGNOSTICS
If
@.B pl
doesn't know how to enable/disable your terminal's printer port, it tells you
so and prints no other output.
@.SH BUGS
If the terminal has no printer attached, or if the printer is powered
off, many terminals will lock up when the printer port is opened.

* Limitations of certain terminals:

VT100:  Remote printer control, as employed by pl, is not supported by
the native VT100.  However, some VT100 emulations, such as that
provided by PROCOMM, support this operation, and can be used with pl.
@.B pl
assumes that the vt100 terminal type supports this operation.  If this
isn't the case for a particular terminal,
@.B pl's
output simply appears on the terminal's screen.

Wyse50:  "Transparent printing", where printer output is sent to the user's
printer but not to the terminal screen, it not supported by the
Wyse50 terminal in its native mode.  However, "simultaneous printing",
where printer output appears simultaneously on the terminal screen,
is supported, and is used by pl to support the Wyse50 terminal type.
If the terminal type is wy50, then after
@.B pl
is finished executing a suggestion is
printed that the screen (as in Elm(1)) be redrawn with ^L or ^R.
@.SH AUTHOR
M. L. Carlock, UNR CCS
@//E*O*F pl.man//
chmod u=rw,g=r,o=r pl.man
 
echo x - prtoff.proto
sed 's/^@//' > "prtoff.proto" <<'@//E*O*F prtoff.proto//'
if test $? -ne 0 ; then
	echo "Don't know how to disable transparent printing for a \`\`"$TERM"'' terminal."
	echo "Send mail to 'staff' if you're interested in having this capability"
	echo "added to the system."
	exit 1
fi
@//E*O*F prtoff.proto//
chmod u=rwx,g=rx,o=rx prtoff.proto
 
echo x - prton.proto
sed 's/^@//' > "prton.proto" <<'@//E*O*F prton.proto//'
if test $? -ne 0 ; then
	echo "Don't know how to enable transparent printing for a \`\`"$TERM"'' terminal."
	echo "Send mail to 'staff' if you're interested in having this capability"
	echo "added to the system."
	exit 1
fi
@//E*O*F prton.proto//
chmod u=rwx,g=rx,o=rx prton.proto
 
echo x - standend.proto
sed 's/^@//' > "standend.proto" <<'@//E*O*F standend.proto//'
if test $? -ne 0 ; then
	echo "Don't know how to disable standout mode for a \`\`"$TERM"'' terminal."
	echo "Send mail to 'staff' if you're interested in having this capability"
	echo "added to the system."
	exit 1
fi
@//E*O*F standend.proto//
chmod u=rwx,g=rx,o=rx standend.proto
 
echo x - standout.proto
sed 's/^@//' > "standout.proto" <<'@//E*O*F standout.proto//'
if test $? -ne 0 ; then
	echo "Don't know how to enable standout mode for a \`\`"$TERM"'' terminal."
	echo "Send mail to 'staff' if you're interested in having this capability"
	echo "added to the system."
	exit 1
fi
@//E*O*F standout.proto//
chmod u=rwx,g=rx,o=rx standout.proto
 
echo x - tcput.c
sed 's/^@//' > "tcput.c" <<'@//E*O*F tcput.c//'
/*
 * tcput.c	Find the given terminal capability in the termcap database,
 *		interpet it, and print it to the terminal.  Raw mode is used
 *		when printing to avoid system-level mappings.
 *
 *		Similar to tput on SYSV unix, but doesn't allow instantiation
 *		of parameter strings from the command line.
 *
 *		Author:  mlc, UNR CCS, 3/91
 */

#include	<stdio.h>
#include	<sgtty.h>
#include	<sys/ioctl.h>

#define		PROG		"tcput"

void		do_args(char **);
void		usage();
char		*getenv();

char		bp[BUFSIZ];
char		*type;
char		*cap;

int		mputc();

main(int argc, char **argv)
{
	int	val;
	char	*cp = bp;

	do_args(argv);
	switch (tgetent(bp, type)) {
	case -1:
		fprintf(stderr, "tcput: can't open termcap database\n");
		exit(1);
		break;
	case 0:
		fprintf(stderr, "tcput: unknown terminal type %s\n", type);
		exit(1);
		break;
	}
	if (tgetstr(cap, &cp) == NULL) {
		fprintf(stderr, "tcput: unknown terminal capability %s\n", cap);
		exit(1);
	}
	raw(); tputs(bp, 1, mputc); noraw();
	return 0;
}

void
do_args(char **argv)
{
	for (++argv; *argv != NULL && **argv == '-'; ++argv) switch(*++*argv) {
	case 'T':
		type = ++*argv;
		break;
	default:
		usage();
		break;
	}
	if (*argv == NULL || *(argv+1) != NULL)
		usage();
	cap = *argv;
	if (type == NULL && (type = getenv("TERM")) == NULL) {
		fprintf(stderr, "TERM not set in environment or given on command line!\n");
		exit(1);
	}
}

void
usage()
{
	fprintf(stderr, "usage: tcput [-Ttype] capability_abbr\n");
	exit(1);
}

mputc(int c)
{
	putchar(c);
	return 0;
}

raw()
{
	struct sgttyb	port;

	if (ioctl(0, TIOCGETP, &port) < 0) {
		perror("raw: stdin");
		exit(1);
	}
	port.sg_flags |= RAW;
	if (ioctl(0, TIOCSETP, &port) < 0) {
		perror("raw: stdin");
		exit(1);
	}
}

noraw()
{
	struct sgttyb	port;

	if (ioctl(0, TIOCGETP, &port) < 0) {
		perror("noraw: stdin");
		exit(1);
	}
	port.sg_flags &= ~RAW;
	if (ioctl(0, TIOCSETP, &port) < 0) {
		perror("noraw: stdin");
		exit(1);
	}
}
@//E*O*F tcput.c//
chmod u=rw,g=r,o=r tcput.c
 
echo x - tcput.man
sed 's/^@//' > "tcput.man" <<'@//E*O*F tcput.man//'
@.TH TCPUT LOCAL "March 24, 1991"
@.UC
@.SH NAME
tcput \- send a particular termcap initialization string to the terminal
@.SH SYNOPSIS
@.B tcput
@.I [-Ttype] capability_name
@.SH DESCRIPTION
@.B tcput
searches the termcap database or the user's TERMCAP environment variable
for the capability given on the command line.  If it is found, the
associated capability string is interpreted and sent to the terminal.  For
example, if the
@.I cl
(clear screen) capability is desired, the terminal's screen can be cleared
from the command line by invoking
@.B tcput
as follows:

@.I	tcput cl

By default,
@.B tcput
uses the capability information for the terminal type given in the user's
TERM environment variable.  The
@.I -Ttype
option allows a different terminal type's capabilities to be used.
@.SH DIAGNOSTICS
If the termcap database cannot be opened, or terminal type in TERM or
in
@.I -Ttype
is not known, or if the requested capability does not exist for the
terminal type being used,
@.B tcput
exits with an appropriate error message without taking further action.
@.SH BUGS
@.B tcput
is intended as a semi-substitute for the SYSV
@.I tput
command for systems which do not posess it, with the main purpose being to
provide the capability-string printing function of
@.I tput.
Unlike
@.I tput,
@.B tcput
doesn't allow string instantiation from the command line, nor are the
@.I init, longname,
or
@.I reset
options supported.
@.SH SEE ALSO
termcap(5)
@.SH AUTHOR
M. L. Carlock, UNR CCS
@//E*O*F tcput.man//
chmod u=rw,g=r,o=r tcput.man
 
echo x - TERMS/README
sed 's/^@//' > "TERMS/README" <<'@//E*O*F TERMS/README//'
The files in this directory contain, in terminfo format, printer port
disable/enable sequences for the terminals represented by the names of
the files, where 'mc4' is the disable and 'mc5' the enable sequence.

The mc4 and mc5 strings can also be used on systems using a termcap instead
of terminfo terminal capability database, by simply substituting 'pf' for
'mc4' and 'po' for 'mc5' and adding the entries to the system termcap file
in the usual format, i.e., :po=....:pf=....:

These entries seem to work OK in our environment.  If they don't work in
yours, you need to see what's going on at your end.

MLC
@//E*O*F TERMS/README//
chmod u=rw,g=r,o=r TERMS/README
 
echo x - TERMS/f100
sed 's/^@//' > "TERMS/f100" <<'@//E*O*F TERMS/f100//'
mc4=\Ea, mc5=\E`,
@//E*O*F TERMS/f100//
chmod u=rw,g=r,o=r TERMS/f100
 
echo x - TERMS/heath-19
sed 's/^@//' > "TERMS/heath-19" <<'@//E*O*F TERMS/heath-19//'
mc4=\E[4i\E[?2h, mc5=\E<\E[5i,
@//E*O*F TERMS/heath-19//
chmod u=rw,g=r,o=r TERMS/heath-19
 
echo x - TERMS/qvt101
sed 's/^@//' > "TERMS/qvt101" <<'@//E*O*F TERMS/qvt101//'
mc4=^T\EA, mc5=^R,
@//E*O*F TERMS/qvt101//
chmod u=rw,g=r,o=r TERMS/qvt101
 
echo x - TERMS/tvi910+
sed 's/^@//' > "TERMS/tvi910+" <<'@//E*O*F TERMS/tvi910+//'
mc4=\EA, mc5=\E`,
@//E*O*F TERMS/tvi910+//
chmod u=rw,g=r,o=r TERMS/tvi910+
 
echo x - TERMS/tvi920
sed 's/^@//' > "TERMS/tvi920" <<'@//E*O*F TERMS/tvi920//'
mc4=\EA, mc5=\E`,
@//E*O*F TERMS/tvi920//
chmod u=rw,g=r,o=r TERMS/tvi920
 
echo x - TERMS/tvi925
sed 's/^@//' > "TERMS/tvi925" <<'@//E*O*F TERMS/tvi925//'
mc4=\Ea, mc5=\E`,
@//E*O*F TERMS/tvi925//
chmod u=rw,g=r,o=r TERMS/tvi925
 
echo x - TERMS/tvi950
sed 's/^@//' > "TERMS/tvi950" <<'@//E*O*F TERMS/tvi950//'
mc4=\EA, mc5=\E`,
@//E*O*F TERMS/tvi950//
chmod u=rw,g=r,o=r TERMS/tvi950
 
echo x - TERMS/vt100
sed 's/^@//' > "TERMS/vt100" <<'@//E*O*F TERMS/vt100//'
mc4=\E[4i, mc5=\E[5i,
@//E*O*F TERMS/vt100//
chmod u=rw,g=r,o=r TERMS/vt100
 
echo x - TERMS/vt102
sed 's/^@//' > "TERMS/vt102" <<'@//E*O*F TERMS/vt102//'
mc4=\E[4i, mc5=\E[5i,
@//E*O*F TERMS/vt102//
chmod u=rw,g=r,o=r TERMS/vt102
 
echo x - TERMS/wy50
sed 's/^@//' > "TERMS/wy50" <<'@//E*O*F TERMS/wy50//'
mc4=^T, mc5=^R,
@//E*O*F TERMS/wy50//
chmod u=rw,g=r,o=r TERMS/wy50
 
exit 0

Malcolm L. Carlock                      Internet:  malc@unr.edu
                                        UUCP:      unr!malc
                                        BITNET:    malc@equinox


exit 0 # Just in case...
-- 
Kent Landfield                   INTERNET: kent@sparky.IMD.Sterling.COM
Sterling Software, IMD           UUCP:     uunet!sparky!kent
Phone:    (402) 291-8300         FAX:      (402) 291-4362
Please send comp.sources.misc-related mail to kent@uunet.uu.net.