#!/bin/sh ## ## Print screen, stdin, or files to an ANSI-connected printer ## ## $Id: lpansi,v 1.2 2003/01/14 21:42:53 dgc Exp $ ## dgc@uchicago.edu ## ## Myself. A0=`basename $0` ## Under what name will I default to printing the screen? SCREEN_CMD=snapshot ## Embedding a literal ESC will cause problems when someone ## cuts & pastes. And someone always will. ESC=`echo a | tr a '\033'` ## The relevant ANSI print codes. These can be taken from "tput -T ## ansi mc0|mc4|mc5", but they're well-defined, and a terminal that ## implements ANSI printing doesn't necessarily advertise it in ## terminfo.... ANSI_PRINT_SCREEN="${ESC}[i" ANSI_PRINT_START="${ESC}[5i" ANSI_PRINT_STOP="${ESC}[4i" ## How to echo something to the terminal without a newline. case "`echo -n`" in -n) echo_n=""; echo_c="\c";; *) echo_n="-n"; echo_c="";; esac necho () { echo $echo_n "$@""$echo_c"; } ## Usage. usage () { echo "lpansi prints to an ANSI terminal's attached printer." echo "usage: $A0 -s # print visible screen" echo " or: $A0 file # print file" echo " or: $A0 file file ... # print files" echo " or: command | $A0 # print output from command" } ## This prints stdin or files. print_files () { necho "$ANSI_PRINT_START" cat "$@" necho "$ANSI_PRINT_STOP" } ## This prints the current screen. print_screen () { necho "$ANSI_PRINT_SCREEN" } ## Do smart stuff. mode=files if [ "$A0" = "$SCREENCMD" ]; then mode=screen fi case "$1" in -h) usage; exit 1;; -s) mode=screen;; "") : ;; *) mode=files;; esac ## Play safe with others if [ $mode = "screen" ]; then print_screen else trap 'necho "$ANSI_PRINT_STOP"; exit 255;' 0 1 2 3 15 print_files "$@" trap "" 0 1 2 3 15 fi exit $?