xref: /plan9/rc/bin/lp (revision 51e5a6e64864eca88b38d65bc2db7f84bcf218a0)
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)
160if (~ $#SCHED 0) SCHED=FIFO	# everyone uses FIFO
161if (~ $KILLFLAG 1)
162	switch ($KILL) {
163	case -;	echo kill option not available on $LPDEST >[1=2]
164		exit 'kill n/a'
165	case *;	bind -b $LPLIB/kill /bin
166		exec $KILL $*
167		exit 'kill command '"$KILL"' not found'
168	}
169if (~ $LPQ 1)
170	switch ($STAT) {
171	case -;	echo queue status option not available on $LPDEST >[1=2]
172		exit 'stat option not available'
173	case *;	bind -b $LPLIB/stat /bin
174		exec $STAT $* < /dev/null
175		exit 'stat command '"$STAT"' not found'
176	}
177DATE=`{date}
178LPLOG=$LPLOGDIR/$LPDEST
179if (! test -e $LPLOG) {
180	>$LPLOG
181	chmod +rwa $LPLOG >[2]/dev/null
182}
183
184if (~ $RESET '') {
185	switch ($SPOOLER) {
186	case -;	echo spooler does not exist for $LPDEST >[1=2]
187		exit 'no spooler'
188	case *;	bind -b $LPLIB/spooler /bin
189		if (~ $#* 0) $SPOOLER
190		if not $SPOOLER $*
191	}
192}
193if not {
194	echo restarting daemon for printer $LPDEST >[1=2]
195	UNLOCK $LPSPOOL/$LPDEST
196	sleep 5
197}
198
199# run daemon
200if (~ $QONLY '') {
201	if (! ~ $DAEMON -) {
202		bind -b $LPLIB/daemon /bin
203		$DAEMON $* >>$LPLOG >[2=1] &
204	}
205}
206exit ''
207