1 #!/usr/local/plan9/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.
5 # rfork en # so that environment and name space are not polluted
7 # put 'fn sigexit { rm /tmp/lpcrap; exit interrupted }' into processes that create /tmp/lpcrap.
10 ' # set ifs in case it is munged in user's environment
12 LPLIB=$PLAN9/lp # lp scripts directories and configuration file are here
13 LPBIN=$PLAN9/bin/lpbin # lp specific binaries are here
14 LPSPOOL=$LPLIB/queue # lp queues
15 LPLOGDIR=$LPLIB/log # lp logs
17 # $LPLIB/bin/lpscratch || exit $status
20 path=($PLAN9/bin /usr/local/bin /usr/bin /bin $LPLIB/bin $LPBIN)
22 USAGE='usage: lp [-d printer] [-p process] [options] [files]
24 lp [-d printer] -k jobnos
30 -f<font.size> specify font and size
31 -i<src> take media from <src> input bin
32 -l<n> print <n> lines per logical page
33 -m<n> magnify <n> times
34 -n<n> print <n> logical pages per physical page
35 -o<i-j,k> print only pages i-j and k
37 -x<n> x page offset in inches
38 -y<n> y page offset in inches'
40 umask 000 # this doesn't work in plan 9
43 THIS_HOST=`{hostname | sed 's/\..*//'}
49 # Run a program from a /sys/lib/lp subdirectory.
55 @{path=($LPLIB/$_LPSUB $path); $LPLIB/$_LPSUB/$_LPCMD $*}
58 # Run a command with standard input from file $1.
59 # If $1 is '', use the current standard input.
72 # Set default printer to be output device
73 if (~ $#LPDEST 0 && test -f $LPLIB/default)
74 LPDEST=`{cat $LPLIB/default}
76 # Parse option parameters
92 # remove FLAGS from environment
93 FLAGD=();FLAGH=();FLAGL=();FLAGM=();FLAGQ=();FLAGc=();FLAGd=();FLAGf=()
94 FLAGi=();FLAGk=();FLAGl=();FLAGm=();FLAGn=();FLAGo=();FLAGp=();FLAGq=()
95 FLAGr=();FLAGt=();FLAGu=();FLAGx=();FLAGy=()
97 eval `{getflags DHLM:1QRc:1d:1f:1i:1kl:1m:1n:1o:1p:1qrt:1u:1x:1y:1 $*}
98 if (! ~ $status '') exit $status
99 if (! ~ $#FLAGD 0) { DEBUG=1; flag x + }; if not { DEBUG=''; flag x - }
100 if (! ~ $#FLAGH 0) NOHEAD=1; if not NOHEAD=''
101 if (! ~ $#FLAGL 0) LAND=1; if not LAND=''
102 # originating machine id (for information only)
103 if (! ~ $#FLAGM 0 && ~ $LPUSERID daemon) LPMACHID=$FLAGM
104 if (! ~ $#FLAGQ 0) QONLY=1; if not QONLY=''
105 if (! ~ $#FLAGR 0) RESET=1; if not RESET=''
106 if (! ~ $#FLAGc 0) COPIES=$FLAGc; if not COPIES=1
109 case '?'; exec awk 'BEGIN{printf "device location host class\n"}
110 /^[^#]/ {printf "%-12s %-9s %-20s %s\n", $1, $2, $3, $6 }' $LPLIB/devices
111 case *; LPDEST=$FLAGd
114 if (! ~ $#FLAGf 0) eval `{echo $FLAGf | sed -e 's/([^.]*)\.([0-9.]*)/FONT=\1;POINT=\2;/'}
115 if (! ~ $#FLAGi 0) IBIN=$FLAGi
116 if (! ~ $#FLAGk 0) KILLFLAG=1; if not KILLFLAG=0
117 if (! ~ $#FLAGl 0) LINES=$FLAGl
118 if (! ~ $#FLAGm 0) MAG=$FLAGm
119 if (! ~ $#FLAGn 0) NPAG=$FLAGn
120 if (! ~ $#FLAGo 0) OLIST=-o$FLAGo
123 case '?';exec ls $LPLIB/process
124 case *; LPPROC=$FLAGp
127 if (! ~ $#FLAGq 0) LPQ=1; if not LPQ=0
134 if (! ~ $#FLAGt 0) TRAY=$FLAGt
135 # originating user id
136 if (! ~ $#FLAGu 0) LPUSERID=$FLAGu
137 if (! ~ $#FLAGx 0) XOFF=$FLAGx
138 if (! ~ $#FLAGy 0) YOFF=$FLAGy
141 echo 'Set environment variable LPDEST or use the
142 ''-d printer'' option to set the destination.' >[1=2]
143 exit 'LPDEST not set'
145 if (~ $LPDEST */*) { # handles MHCC destinations like mh/lino
146 LPLOC=`{echo $LPDEST|sed 's/^(.*)\/(.*)/\1/'}
147 LPDEST=`{echo $LPDEST|sed 's/^(.*)\/(.*)/\2/'}
150 # Fetch device info from devices file.
152 LPDLINE=`{grep '^'$LPDEST'[ ]' $LPLIB/devices}
153 if (! ~ $status '') {
154 echo 'device '$LPDEST' is not in '$LPLIB'/devices' >[1=2]
158 DEST_HOST=$LPDLINE(3)
162 if (~ $#LPPROC 0) LPPROC=$LPDLINE(7)
169 # On to the actual command-line processing.
175 echo kill option not available on $LPDEST >[1=2]
186 echo queue status option not available on $LPDEST >[1=2]
187 exit 'stat option not available'
189 lpsub stat $STAT $* </dev/null
195 LPLOG=$LPLOGDIR/$LPDEST
196 if (! test -e $LPLOG) {
198 chmod +rwa $LPLOG >[2]/dev/null
201 if (~ $RESET '') { # lp file
203 case -; echo spooler does not exist for $LPDEST >[1=2]
205 case *; path=($LPLIB/spooler $path)
206 if (~ $#* 0) $SPOOLER
212 echo restarting daemon for printer $LPDEST >[1=2]
213 echo `{date} restarting daemon >>$LPLOG
214 UNLOCK $LPSPOOL/$LPDEST
219 if (~ $QONLY '') { # not lp -Q
221 lpsub daemon $DAEMON $* >>$LPLOG >[2=1] &