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