lp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #!/bin/rc
  2. # This program enqueues the file to be printed and starts the daemon, when necessary.
  3. # Make changes to /sys/src/cmd/lp/lp.rc. Changes made directly to /rc/bin/lp will be lost.
  4. rfork en # so that environment and name space are not polluted
  5. #
  6. # put 'fn sigexit { rm /tmp/lpcrap; exit interrupted }' into processes that create /tmp/lpcrap.
  7. ifs='
  8. ' # set ifs in case it is munged in user's environment
  9. LPLIB=/sys/lib/lp # lp scripts directories and configuration file are here
  10. LPBIN=/$cputype/bin/aux # lp specific binaries are here
  11. LPSPOOL=$LPLIB/queue # lp queues
  12. LPLOGDIR=$LPLIB/log # lp logs
  13. $LPLIB/bin/lpscratch
  14. x=$status
  15. if(! ~ $x '') exit $x
  16. # build /bin from the ground up
  17. bind /$cputype/bin /bin # general compiled binaries
  18. bind -a /rc/bin /bin # general rc scripts
  19. # This needs to be fixed for the real thing
  20. bind -a $LPLIB/bin /bin # lp specific rc scripts
  21. bind -a $LPBIN /bin # lp specific compiled binaries
  22. path=(/bin)
  23. if (! test -w /tmp) bind -bc $LPLIB/tmp /tmp
  24. USAGE='usage: lp [-d printer] [-p process] [options] [files]
  25. lp [-d printer] -q
  26. lp [-d printer] -k jobnos
  27. options include:
  28. -H no header
  29. -L landscape mode
  30. -c<n> make <n> copies
  31. -f<font.size> specify font and size
  32. -i<src> take media from <src> input bin
  33. -l<n> print <n> lines per logical page
  34. -m<n> magnify <n> times
  35. -n<n> print <n> logical pages per physical page
  36. -o<i-j,k> print only pages i-j and k
  37. -r reverse pages
  38. -x<n> x page offset in inches
  39. -y<n> y page offset in inches'
  40. # umask 000 # this doesn't work in plan 9
  41. if (~ $#sysname 0)
  42. THIS_HOST=plan9
  43. if not {
  44. THIS_HOST=`{ndb/query sys $sysname dom}
  45. if(~ $#THIS_HOST 0)
  46. THIS_HOST=$sysname
  47. }
  48. LPMACHID=$THIS_HOST
  49. THIS_USERID=$user
  50. LPUSERID=$THIS_USERID
  51. LPLOC=''
  52. # Set default printer to be output device
  53. if (~ $#LPDEST 0 && test -f $LPLIB/defdevice) LPDEST=`{cat $LPLIB/defdevice}
  54. # option parameters
  55. XOFF=''
  56. YOFF=''
  57. POINT=''
  58. FONT=''
  59. LINES=''
  60. LAND=''
  61. COPIES=''
  62. MAG=''
  63. NPAG=''
  64. OLIST=''
  65. IBIN=''
  66. REVERSE=''
  67. NOHEAD=''
  68. TRAY=''
  69. # remove FLAGS from environment
  70. FLAGD=();FLAGH=();FLAGL=();FLAGM=();FLAGQ=();FLAGc=();FLAGd=();FLAGf=()
  71. FLAGi=();FLAGk=();FLAGl=();FLAGm=();FLAGn=();FLAGo=();FLAGp=();FLAGq=()
  72. FLAGr=();FLAGt=();FLAGu=();FLAGx=();FLAGy=()
  73. # Process options
  74. eval `{aux/getflags DHLM:1QRc:1d:1f:1i:1kl:1m:1n:1o:1p:1qrt:1u:1x:1y:1 $*}
  75. if (! ~ $status '') exit $status
  76. if (! ~ $#FLAGD 0) { DEBUG=1; flag x + }; if not { DEBUG=''; flag x - }
  77. if (! ~ $#FLAGH 0) NOHEAD=1; if not NOHEAD=''
  78. if (! ~ $#FLAGL 0) LAND=1; if not LAND=''
  79. # originating machine id (for information only)
  80. if (! ~ $#FLAGM 0 && ~ $LPUSERID daemon) LPMACHID=$FLAGM
  81. if (! ~ $#FLAGQ 0) QONLY=1; if not QONLY=''
  82. if (! ~ $#FLAGR 0) RESET=1; if not RESET=''
  83. if (! ~ $#FLAGc 0) COPIES=$FLAGc; if not COPIES=1
  84. if (! ~ $#FLAGd 0) {
  85. switch ($FLAGd) {
  86. case '?'; exec awk 'BEGIN{printf "device location host class\n"}
  87. /^[^#]/ {printf "%-12s %-9s %-20s %s\n", $1, $2, $3, $6 }' $LPLIB/devices
  88. case *; LPDEST=$FLAGd
  89. }
  90. }
  91. if (! ~ $#FLAGf 0) eval `{echo $FLAGf | sed -e 's/([^.]*)\.([0-9.]*)/FONT=\1;POINT=\2;/'}
  92. if (! ~ $#FLAGi 0) IBIN=$FLAGi
  93. if (! ~ $#FLAGk 0) KILLFLAG=1; if not KILLFLAG=0
  94. if (! ~ $#FLAGl 0) LINES=$FLAGl
  95. if (! ~ $#FLAGm 0) MAG=$FLAGm
  96. if (! ~ $#FLAGn 0) NPAG=$FLAGn
  97. if (! ~ $#FLAGo 0) OLIST=-o$FLAGo
  98. if (! ~ $#FLAGp 0) {
  99. switch (FLAGp) {
  100. case '?';exec ls $LPLIB/process
  101. case *; LPPROC=$FLAGp
  102. }
  103. }
  104. if (! ~ $#FLAGq 0) LPQ=1; if not LPQ=0
  105. if (! ~ $#FLAGr 0) {
  106. switch ($REVERSE) {
  107. case '';REVERSE=1
  108. case 1; REVERSE=''
  109. }
  110. }
  111. if (! ~ $#FLAGt 0) TRAY=$FLAGt
  112. # originating user id
  113. if (! ~ $#FLAGu 0) LPUSERID=$FLAGu
  114. if (! ~ $#FLAGx 0) XOFF=$FLAGx
  115. if (! ~ $#FLAGy 0) YOFF=$FLAGy
  116. if (~ $#LPDEST 0) {
  117. echo 'Set environment variable LPDEST or use the
  118. ''-d printer'' option to set the destination.' >[1=2]
  119. exit 'LPDEST not set'
  120. }
  121. if (~ $LPDEST */*) { # handles MHCC destinations like mh/lino
  122. LPLOC=`{echo $LPDEST|sed 's/^(.*)\/(.*)/\1/'}
  123. LPDEST=`{echo $LPDEST|sed 's/^(.*)\/(.*)/\2/'}
  124. }
  125. LPDLINE=`{grep '^'$LPDEST'[ ]' $LPLIB/devices}
  126. if (! ~ $status '') {
  127. echo 'device '$LPDEST' is not in '$LPLIB'/devices' >[1=2]
  128. exit 'LPDEST is bad'
  129. }
  130. LOC=$LPDLINE(2)
  131. DEST_HOST=$LPDLINE(3)
  132. OUTDEV=$LPDLINE(4)
  133. SPEED=$LPDLINE(5)
  134. LPCLASS=$LPDLINE(6)
  135. if (~ $#LPPROC 0) LPPROC=$LPDLINE(7)
  136. SPOOLER=$LPDLINE(8)
  137. STAT=$LPDLINE(9)
  138. KILL=$LPDLINE(10)
  139. DAEMON=$LPDLINE(11)
  140. SCHED=$LPDLINE(12)
  141. if (~ $KILLFLAG 1)
  142. switch ($KILL) {
  143. case -; echo kill option not available on $LPDEST >[1=2]
  144. exit 'kill n/a'
  145. case *; bind -b $LPLIB/kill /bin
  146. exec $KILL $*
  147. exit 'kill command '"$KILL"' not found'
  148. }
  149. if (~ $LPQ 1)
  150. switch ($STAT) {
  151. case -; echo queue status option not available on $LPDEST >[1=2]
  152. exit 'stat option not available'
  153. case *; bind -b $LPLIB/stat /bin
  154. exec $STAT $* < /dev/null
  155. exit 'stat command '"$STAT"' not found'
  156. }
  157. DATE=`{date}
  158. LPLOG=$LPLOGDIR/$LPDEST
  159. if (! test -e $LPLOG) {
  160. >$LPLOG
  161. chmod +rwa $LPLOG >[2]/dev/null
  162. }
  163. if (~ $RESET '') {
  164. switch ($SPOOLER) {
  165. case -; echo spooler does not exist for $LPDEST >[1=2]
  166. exit 'no spooler'
  167. case *; bind -b $LPLIB/spooler /bin
  168. if (~ $#* 0) $SPOOLER
  169. if not $SPOOLER $*
  170. }
  171. }
  172. if not {
  173. echo restarting daemon for printer $LPDEST >[1=2]
  174. UNLOCK $LPSPOOL/$LPDEST
  175. sleep 5
  176. }
  177. # run daemon
  178. if (~ $QONLY '') {
  179. if (! ~ $DAEMON -) {
  180. bind -b $LPLIB/daemon /bin
  181. $DAEMON $* >>$LPLOG >[2=1] &
  182. }
  183. }
  184. exit ''