unix-lpr.sh 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #!/bin/sh
  2. # $Id: unix-lpr.sh,v 1.1 2000/03/09 08:40:40 lpd Exp $
  3. #
  4. # Unix lpr filter. The default setup sends output directly to a pipe,
  5. # which requires the Ghostscript process to fork, and thus may cause
  6. # small systems to run out of memory/swap space. An alternative strategy,
  7. # based on a suggestion by Andy Fyfe (andy@cs.caltech.edu), uses a named
  8. # pipe for output, which avoids the fork and can thus save a lot of memory.
  9. #
  10. # Unfortunately this approach can cause problems when a print job is aborted,
  11. # as the abort can cause one of the processes to die, leaving the process
  12. # at the other end of the pipe hanging forever.
  13. #
  14. # Because of this, the named pipe method has not been made the default,
  15. # but it may be restored by commenting out the lines referring to
  16. # 'gsoutput' and uncommenting the lines referring to 'gspipe'.
  17. #
  18. PBMPLUSPATH=/usr/local/bin
  19. PSFILTERPATH=/usr/local/lib/ghostscript
  20. LOCALPATH=/usr/local/bin
  21. X11HOME=/usr/X11R6
  22. PATH=/bin:/usr/bin:/usr/ucb:/usr/etc
  23. PATH=${PATH}\:${LOCALPATH}\:${PBMPLUSPATH}\:${PSFILTERPATH}
  24. LD_LIBRARY_PATH=${X11HOME}/lib
  25. export PATH LD_LIBRARY_PATH acctfile host user
  26. user= host= acctfile=/dev/null
  27. #
  28. # Redirect stdout to stderr (for the logfile) and open a new channel
  29. # connected to stdout for the raw data. This enables us to keep the
  30. # raw data separate from programmed postscript output and error messages.
  31. #
  32. exec 3>&1 1>&2
  33. #
  34. # Get username and hostname from filter parameters
  35. #
  36. while [ $# != 0 ]
  37. do case "$1" in
  38. -n) user=$2 ; shift ;;
  39. -h) host=$2 ; shift ;;
  40. -*) ;;
  41. *) acctfile=$1 ;;
  42. esac
  43. shift
  44. done
  45. #
  46. # Get the filter, printer device and queue type (direct/indirect)
  47. #
  48. filter=`basename $0`
  49. device=`dirname $0`
  50. type=`dirname ${device}`
  51. device=`basename ${device}`
  52. fdevname=$device
  53. type=`basename ${type}`
  54. #
  55. # Find the bpp and number of colors, if specified
  56. #
  57. colorspec="`echo ${device} | sed 's/.*\.[0-9][0-9]*\.\([0-9][0-9]*\)$/\1/'`"
  58. if test "$colorspec" = "${device}"
  59. then
  60. colorspec=""
  61. else
  62. device=`basename ${device} .$colorspec`
  63. colorspec="-dColors=$colorspec"
  64. fi
  65. bpp="`echo ${device} | sed 's/.*\.\([0-9][0-9]*\)$/\1/'`"
  66. if test "$bpp" = "${device}"
  67. then
  68. bpp=1
  69. else
  70. device=`basename ${device} .$bpp`
  71. fi
  72. #
  73. # Information for the logfile
  74. #
  75. lock=`dirname ${acctfile}`/lock
  76. cf=`tail -1 ${lock}`
  77. job=`egrep '^J' ${cf} | tail +2c`
  78. echo "gsbanner: ${host}:${user} Job: ${job} Date: `date`"
  79. echo "gsif: ${host}:${user} ${fdevname} start - `date`"
  80. #
  81. # Set the direct or indirect output destinations
  82. #
  83. #gspipe=/tmp/gspipe.$$
  84. #mknod ${gspipe} p
  85. case "${type}" in
  86. direct)
  87. gsoutput="cat 1>&3" ;;
  88. # cat ${gspipe} 1>&3 & ;;
  89. indirect)
  90. gsoutput="lpr -P${device}.raw" ;;
  91. # cat ${gspipe} | lpr -P${device}.raw & ;;
  92. esac
  93. (
  94. #
  95. # Any setup required may be done here (eg. setting gamma for colour printing)
  96. #
  97. #echo "{0.333 exp} dup dup currenttransfer setcolortransfer"
  98. #
  99. # The input data is filtered here, before being passed on to Ghostscript
  100. #
  101. case "${filter}" in
  102. gsif) cat ;;
  103. gsnf) psdit ;;
  104. gstf) pscat ;;
  105. gsgf) psplot ;;
  106. gsvf) rasttopnm | pnmtops ;;
  107. gsdf) dvi2ps -sqlw ;;
  108. gscf|gsrf) echo "${filter}: filter not available" 1>&2 ; exit 0 ;;
  109. esac
  110. #
  111. # This is the postlude which does the accounting
  112. #
  113. echo "\
  114. (acctfile) getenv
  115. { currentdevice /PageCount gsgetdeviceprop dup cvi 0 gt
  116. { exch (a) file /acctfile exch def
  117. /string 20 string def
  118. string cvs dup length dup
  119. 4 lt
  120. { 4 exch sub
  121. { acctfile ( ) writestring } repeat
  122. } { pop } ifelse
  123. acctfile exch writestring
  124. acctfile (.00 ) writestring
  125. acctfile (host) getenv
  126. { string cvs } { (NOHOST) } ifelse writestring
  127. acctfile (:) writestring
  128. acctfile (user) getenv
  129. { string cvs } { (NOUSER) } ifelse writestring
  130. acctfile (\n) writestring
  131. acctfile closefile
  132. } { pop } ifelse
  133. } if
  134. quit"
  135. ) | gs -q -dNOPAUSE -sDEVICE=${device} -dBitsPerPixel=${bpp} $colorspec \
  136. -sOutputFile=\|"${gsoutput}" -
  137. # -sOutputFile=${gspipe} -
  138. rm -f ${gspipe}
  139. #
  140. # End the logfile entry
  141. #
  142. echo "gsif: end - `date`"