Cross Reference: /illumos-gate/usr/src/cmd/lp/model/netstandard
netstandard revision 7c478bd95313f5f23a4c958a745db2134aa03244
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
#
# Copyright 2005 Sun Microsystems, Inc. All rights reserved.
# Use is subject to license terms.
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License, Version 1.0 only
# (the "License"). You may not use this file except in compliance
# with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#pragma ident "%Z%%M% %I% %E% SMI"
###########
##
## Network Standard printer interface program.
##
###########
#####
# We can't do much except exit if spooler/scheduler
# cancels us.
#####
trap 'eval exit_clean 15' 15
####
#
# Send standard error messages to /dev/null rather than to
# the spooler. Avoids "Terminated" messages that shell puts out
# when gets SIGTERM. Save standard error so it can be used
# when we need it
####
exec 5>&2 2>/dev/null 3>&1
####
# set some global variables
####
: ${LPTMPDIR:=/tmp}
: ${SPOOLDIR:=/usr/spool/lp}
: ${LOCALPATH:=${SPOOLDIR}/bin}
PATH="/bin:/usr/bin:${LOCALPATH}"
exit_code=0
# ${LPTELL} is the name of a program that will send its
# standard input to the Spooler. It is used to forward
# the description of a printer fault to the Spooler,
# which uses it in an alert to the administrator.
#####
if [ ! -x "${LPTELL:=${LOCALPATH}/lp.tell}" ]
then
fake_lptell () {
header="no"
while read line
do
if [ "no" = "${header}" ]
then
errmsg ERROR ${E_IP_UNKNOWN} \
"unknown printer/interface failure" \
"consult your system administrator;
reasons for failure (if any) follow:"
header=yes
fi
echo "${line}" >&2
done
return 1
}
LPTELL=fake_lptell
fi
#####
# Error message formatter:
#
# Invoke as
#
# errmsg severity message-number problem help
#
# where severity is "ERROR" or "WARNING", message-number is
# a unique identifier, problem is a short description of the
# problem, and help is a short suggestion for fixing the problem.
#####
LP_ERR_LABEL="UX:lp"
E_IP_ARGS=1
E_IP_OPTS=2
#E_IP_FILTER=3
E_IP_UNKNOWN=5
E_IP_BADFILE=6
E_IP_ERRORS=12 # (in slow.filter)
errmsg () {
case $1 in
ERROR )
sev=" ERROR";
;;
WARNING )
sev="WARNING";
;;
esac
echo "${LP_ERR_LABEL}:$2 ${sev}: $3
TO FIX: $4" >&5
}
###########
##
## Check arguments
###########
parse () {
echo "`expr \"$1\" : \"^[^=]*=\(.*\)\"`"
}
#####
##
## Error Cleanup and Exit
##
#####
exit_clean()
{
if [ -f "${LPTMPDIR}/pr_eexit_code.$$" ]
then
/bin/rm ${LPTMPDIR}/pr_eexit_code.$$
fi
if [ -f "${LPTMPDIR}/small_banner.$$" ]
then
/bin/rm ${LPTMPDIR}/small_banner.$$
fi
if [ -f "${tmpfile}" ]
then
/bin/rm "${tmpfile}"
fi
exit $1
}
#####
#
# This program is invoked as
#
# ${SPOOLDIR}/.../printer request-id user title copies options files...
#
# The first three arguments are simply reprinted on the banner page,
# the fourth (copies) is used to control the number of copies to print,
# the fifth (options) is a blank separated list (in a single argument)
# of user or Spooler supplied options (without the -o prefix),
# and the last arguments are the files to print.
#####
if [ $# -lt 5 ]
then
errmsg ERROR ${E_IP_ARGS} \
"wrong number of arguments to interface program" \
"consult your system administrator"
exit 1
fi
printer=`basename $0`
request_id=$1
user_name=$2
title=$3
copies=$4
option_list=$5
shift 5
files="$*"
#
# debug sent to file if defined in /etc/syslog.conf
# syslog.conf entry:
# lpr.debug /path/filename
#
logger -p lpr.debug -t "netstandard: ${request_id}" " "
logger -p lpr.debug -t "netstandard: ${request_id}" "INPUT"
logger -p lpr.debug -t "netstandard: ${request_id}" " printer : ${printer}"
logger -p lpr.debug -t "netstandard: ${request_id}" " request_id : ${request_id}"
logger -p lpr.debug -t "netstandard: ${request_id}" " user_name : ${user_name}"
logger -p lpr.debug -t "netstandard: ${request_id}" " title : ${title}"
logger -p lpr.debug -t "netstandard: ${request_id}" " copies : ${copies}"
logger -p lpr.debug -t "netstandard: ${request_id}" " option_list : ${option_list}"
logger -p lpr.debug -t "netstandard: ${request_id}" " files : ${files}"
logger -p lpr.debug -t "netstandard: ${request_id}" " spooler_key ${SPOOLER_KEY}"
####
# default: do print a banner
####
nobanner=no
nofilebreak="no"
inlist=
data_file_flag=
for i in ${option_list}
do
case "${inlist}${i}" in
nobanner )
nobanner="yes"
;;
nofilebreak )
nofilebreak="yes"
;;
#####
#
# If you want to add simple options (e.g. -o simple)
# identify them here.
#####
# simple )
# simple="yes"
# ;;
cpi=pica )
cpi=10
;;
cpi=elite )
cpi=12
;;
cpi=* )
cpi=`parse ${i}`
;;
lpi=* )
lpi=`parse ${i}`
;;
length=* )
length=`parse ${i}`
;;
width=* )
width=`parse ${i}`
;;
dest=* )
dest="-d `parse ${i}`"
;;
protocol=* )
protocol="-P `parse ${i}`"
;;
bsdctrl=* )
controlfile="-c `parse ${i}`"
;;
timeout=* )
timeout="-t `parse ${i}`"
;;
data-file-type=* )
data_file_flag="-f `parse ${i}`"
;;
#####
#
# If you want to add simple-value options (e.g. -o value=a)
# identify them here.
#####
# value=* )
# value=`parse ${i}`
# ;;
#####
#
# If you want to add options that,
# take a list (e.g. -o lopt='a b c'), identif
# them here and below (look for LOPT).
#####
# flist=* | lpd=* | options=* )
flist=* | lpd=* )
#LOPT stty=* | flist=* | lpd=* | lopt=* )
inlist=`expr "${inlist}${i}" : "^\([^=]*=\)"`
case "${i}" in
${inlist}\'*\' )
item=`expr "${i}" : "^[^=]*='*\(.*\)'\$"`
;;
${inlist}\' )
continue
;;
${inlist}\'* )
item=`expr "${i}" : "^[^=]*='*\(.*\)\$"`
;;
${inlist}* )
item=`expr "${i}" : "^[^=]*=\(.*\)\$"`
;;
*\' )
item=`expr "${i}" : "^\(.*\)'\$"`
;;
* )
item="${i}"
;;
esac
#####
#
# We don't dare use "eval" because a clever user could
# put something in an option value that we'd end up
# exec'ing.
#####
case "${inlist}" in
flist= )
flist="${flist} ${item}"
;;
lpd= )
lpd="${lpd} ${item}"
;;
#LOPT lopt= )
#LOPT lopt="${lopt} ${item}"
#LOPT ;;
# options= )
# options="${options} ${item}"
# ;;
esac
case "${i}" in
${inlist}\'*\' )
inlist=
;;
${inlist}\'* )
;;
*\' | ${inlist}* )
inlist=
;;
esac
;;
* )
errmsg WARNING ${E_IP_OPTS} \
"unrecognized \"-o ${i}\" option" \
"check the option, resubmit if necessary
printing continues"
;;
esac
done
logger -p lpr.debug -t "netstandard: ${request_id}" "term : ${TERM}"
if [ -z "${FILTER}" ]
then
#####
#
# If no filter is being used, we use netpr to push the
# file to the printer.
# (QUOTES ARE IMPORTANT!)
#####
case "$TERM" in
PS )
# make the "postscript" printers use netpr
FILTER=
;;
PSR )
# make the "reverse postscript" printers reverse the
# output and the use postio to talk to the printer
#FILTER="/usr/lib/lp/postscript/postreverse "
#FILTER=
FILTER="/usr/lib/lp/postscript/postreverse "
;;
* )
# We don't know the type, so just assume that the
# input and output are the same. Use netpr.
#FILTER=/bin/cat
FILTER=
;;
esac
fi
####
# sets default value for ordering of data and control files with
# bsd protocol. Default: data files first. Administrator
# may set to control file first with lpadmin -o bsdctrl=first
####
banner_flag=""
case "${nobanner}" in
yes )
banner_flag="-b"
;;
esac
NETPR="/usr/lib/lp/bin/netpr ${banner_flag} ${data_file_flag} \
-I ${request_id} -U ${user_name} \
-p ${printer} ${dest} -T \"${title}\" \
${timeout} ${protocol} ${controlfile} "
LPTELL_OPTS="-l" # netpr sends LaserWriter style messages back
logger -p lpr.debug -t "netstandard: ${request_id}" "NETPR= ${NETPR}"
logger -p lpr.debug -t "netstandard: ${request_id}" "filter : ${FILTER}"
node=`uname -n`
pid=$$
tmpfile=${LPTMPDIR}/${node}.${pid}
logger -p lpr.debug -t "netstandard: ${request_id}" "tmpfile : ${tmpfile}"
#####
#
# Set up filter for banner page
#
#####
banner_filter=
case "${TERM}" in
PS | PSR )
banner_filter=" | /usr/lib/lp/postscript/postprint "
LPTELL_OPTS="-l"
;;
esac
#####
#
# Build temporary file that is the banner page
#
#####
PAD="#####${NL}"
CR="\r"
NL="${CR}\n"
FF=
small_banner() {
echo "${CR}\c"
echo "${PAD}\c"
echo "##### User: ${user_name}${NL}\c"
if [ -n "${title}" ]
then
echo "##### Title: ${title}${NL}\c"
fi
echo "##### Date: `LANG=C date '+%a %H:%M %h %d, %Y'`${NL}\c"
echo "##### Job: ${request_id}${NL}\c"
echo "${PAD}\c"
if [ -n "${FF}" ]
then
echo "${CR}${FF}\c"
fi
}
#####
#
# Doing small banner as we don't know what printer is out there
#
#####
banner=small_banner
if [ "no" = "${nobanner}" ]
then
eval "${banner} ${banner_filter}" 2>&1 1>${LPTMPDIR}/small_banner.$$
fi
#####
#
# Print banner page before job unless PSR
#
#####
if [ "no" = "${nobanner}" -a "${TERM}" != "PSR" ]
then
(
eval ${NETPR} ${LPTMPDIR}/small_banner.$$ 2>&1
echo $? > ${LPTMPDIR}/pr_eexit_code.$$
) | ${LPTELL} ${LPTELL_OPTS} ${printer}
exit_code=`cat ${LPTMPDIR}/pr_eexit_code.$$`
logger -p lpr.debug -t "netstandard: ${request_id}" \
"banner page exit code : ${exit_code}"
fi
i=1
while [ $i -le $copies ]
do
for file in ${files}
do
if [ -r "${file}" ]
then
if [ ! -z "${FILTER}" ]
then
(
#####
# There is a filter, use it
#
# Put 0<${file} before the "eval" to keep
# clever users from giving a file name that
# evaluates as something to execute.
# Redirect stderr to stdout so LPTELL will
# get error messages from pipe.
#####
0<${file} eval ${FILTER} 2>&1 1>${tmpfile}
echo $? > ${LPTMPDIR}/pr_eexit_code.$$
) | ${LPTELL} ${LPTELL_OPTS} ${printer}
exit_code=`cat ${LPTMPDIR}/pr_eexit_code.$$`
logger -p lpr.debug -t "netstandard: ${request_id}" \
"filter exit_code : ${exit_code}"
if [ -n "${exit_code}" ]
then
if [ "${exit_code}" -eq 0 ]
then
printfile=${tmpfile}
else
####
# The filter did not succeed, so don't try to print
####
printfile=
fi
fi
else
printfile=${file}
fi
logger -p lpr.debug -t "netstandard: ${request_id}" \
"printfile : ${printfile}"
#####
# Print the file
#####
if [ -r "${printfile}" ]
then
(
eval ${NETPR} ${printfile} 2>&1
echo $? > ${LPTMPDIR}/pr_eexit_code.$$
) | ${LPTELL} ${LPTELL_OPTS} ${printer}
exit_code=`cat ${LPTMPDIR}/pr_eexit_code.$$`
logger -p lpr.debug -t "netstandard: ${request_id}" \
"netpr exit_code : ${exit_code}"
if [ -f "${tmpfile}" ]
then
/bin/rm "${tmpfile}"
fi
if [ -n "${exit_code}" ]
then
if [ "${exit_code}" -eq 0 ]
then
printone=yes
else
if [ "${exit_code}" -lt 128 ]
then
noprint=yes
else
retry=yes
fi
fi
fi
else
errmsg WARNING ${E_IP_BADFILE} \
"cannot read temporary file \"${printfile}\""\
"see if file still exists,
or consult your system administrator;
printing continues"
fi
else
#####
#
# Don't complain about not being able to read
# a file on second and subsequent copies, unless
# we've not complained yet. This removes repeated
# messages about the same file yet reduces the
# chance that the user can remove a file and not
# know that we had trouble finding it.
#####
if [ "${i}" -le 1 -o -z "${badfileyet}" ]
then
errmsg WARNING ${E_IP_BADFILE} \
"cannot read file \"${file}\"" \
"see if the file still exists and is readable,
or consult your system administrator;
printing continues"
badfileyet=yes
fi
fi
# for file in ${files}
done
i=`expr $i + 1`
done
#####
#
# If printing in reverse order, print the banner page now
#
#####
if [ "no" = "${nobanner}" -a "${TERM}" = "PSR" ]
then
(
eval ${NETPR} ${LPTMPDIR}/small_banner.$$ 2>&1
echo $? > ${LPTMPDIR}/pr_eexit_code.$$
) | ${LPTELL} ${LPTELL_OPTS} ${printer}
fi
exit_code=`cat ${LPTMPDIR}/pr_eexit_code.$$`
logger -p lpr.debug -t "netstandard: ${request_id}" \
"banner page exit code : ${exit_code}"
if [ -n "${printone}" -a -z "${retry}" -a -z "${noprint}" ]
then
exit_code=`expr 0`
else
if [ -n "${retry}" -a -z "${printone}" -a -z "${noprint}" ]
then
exit_code=`expr 129`
else
exit_code=`expr 1`
fi
fi
logger -p lpr.debug -t "netstandard: ${request_id}" \
"FINAL exit_code : ${exit_code}"
exit_clean ${exit_code}