lp 5.1 KB

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