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
|
#!/bin/sh
#
# 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
#
#
# Copyright 2005 Sun Microsystems, Inc. All rights reserved.
# Use is subject to license terms.
#
# ident "%Z%%M% %I% %E% SMI"
#
PATH=/bin:/usr/bin:/usr/sbin export PATH
TEXTDOMAIN="SUNW_OST_OSCMD"
export TEXTDOMAIN
LPSET=/usr/bin/lpset
LPGET=/usr/bin/lpget
HOST=`/bin/uname -n`
LHOST="localhost"
PID=$$
cmd_name=lpadmin
exit_code=0
usage() {
gettext "Usage:\n" 1>&2
gettext " lpadmin -p (printer) (options)\n" 1>&2
gettext " lpadmin -x (dest)\n" 1>&2
gettext " lpadmin -d (dest)\n" 1>&2
gettext " lpadmin -S print-wheel -A alert-type [ -W minutes ]\n" 1>&2
gettext " [ -Q requests ]\n" 1>&2
gettext " lpadmin -M -f form-name [ -a [ -o filebreak ]\n" 1>&2
gettext " [ -t tray-number ]]\n" 1>&2
exit 1
}
#
# Delete entries in /etc/printers.conf for local printers/classes that no longer
# exist in the /etc/lp database
#
delete_local() {
# Get printer names for each local printer
# grep /etc/printers.conf for each bsdaddr for this server
# get printer name from that line
for LINE in `/bin/grep bsdaddr /etc/printers.conf |
/bin/egrep -e ${HOST}\|${LHOST}`
do
PRINTER=`echo ${LINE} | /bin/sed -e 's/^:bsdaddr='$LHOST',//' \
-e 's/^:bsdaddr='$HOST',//' \
-e 's/[,:].*//'`
# If there is not an entry for this printer in
# /etc/lp/printers or /etc/lp/classes
# Then delete the entry for this printer in /etc/printers.conf
if [ ! -d /etc/lp/printers/${PRINTER} -a ! -f /etc/lp/classes/${PRINTER} ] ;
then
logger -p lpr.debug -t "lpadmin[${PID}]" \
"Removing $PRINTER entry from /etc/printers.conf"
${LPSET} -x ${PRINTER}
status=$?
if [ ${status} -ne 0 ] ; then
gettext "Warning: error removing ${PRINTER} " 1>&2
gettext "entry from /etc/printers.conf\n" 1>&2
logger -p lpr.debug -t "lpadmin[${PID}]" \
"Call to lpset -x $PRINTER exits with ${status}"
exit_code=1
fi
fi
done
#
# shutdown scheduler if there are no local printers
#
CONFIGS=/etc/lp/printers/*/configuration
printers_configured=`echo $CONFIGS`
if [ "$printers_configured" = "$CONFIGS" ]; then
svcprop -q svc:/application/print/server:default &&
svcadm disable svc:/application/print/server:default
fi
}
delete_entries() {
if [ ! -f /etc/printers.conf ] ; then
logger -p lpr.debug -t "lpadmin[${PID}]" \
"System error: Cannot access /etc/printers.conf"
gettext "lpadmin: System error; Cannot access /etc/printers.conf\n" 1>&2
exit 1
fi
# remove _default
DEFAULTP=`${LPGET} _default | /bin/grep use | /bin/sed -e 's/[ ]*use=//'`
${LPGET} -k bsdaddr ${DEFAULTP} >/dev/null 2>&1
status=$?
if [ ${status} -eq 0 ] ; then
${LPSET} -x _default
status=$?
if [ ${status} -ne 0 ] ; then
gettext "Warning: error removing _default entry from /etc/printers.conf\n" 1>&2
logger -p lpr.debug -t "lpadmin[${PID}]" \
"Call to lpset -x _default exits with ${status}"
exit_code=1
fi
fi
# delete entries in /etc/printers.conf for printers/classes that have
# been deleted
delete_local
# Delete all the remote printers using bsdaddr
for LINE in `/bin/grep bsdaddr /etc/printers.conf | /bin/egrep -v -e ${HOST}\|${LHOST}`
do
PRINTER=`echo $LINE | /bin/sed -e 's/^:bsdaddr=[^,]*,//' -e 's/[,:].*//'`
${LPSET} -x $PRINTER
status=$?
if [ ${status} -ne 0 ] ; then
gettext "Warning: error removing ${PRINTER} entry from /etc/printers.conf\n" 1>&2
logger -p lpr.debug -t "lpadmin[${PID}]" \
"Call to lpset -x $PRINTER exits with ${status}"
exit_code=1
fi
done
}
if [ $# -lt 1 ] ; then
usage
exit 1
fi
# Deal with the -d option independently since getopts does not handle
# options that may or may not have arguments
#
first=$1
second=$2
third=$3
if [ ${first} = "-d" ] ; then
# check that there are no extra arguments
if [ -n "${third}" ] ; then
usage
exit 1
fi
# be sure we have lpset and lpget
if [ ! -f ${LPSET} -o ! -f ${LPGET} ] ; then
gettext "lpadmin: System error; cannot set default printer\n" 1>&2
exit 2
fi
if [ ! -n "${second}" ] ; then
${LPGET} -n system _default >/dev/null 2>&1
exit_code=$?
if [ ${exit_code} -eq 0 ] ; then
# delete _default entry in /etc/printers.conf
${LPSET} -n system -x _default
exit_code=$?
if [ ${exit_code} -ne 0 ] ; then
gettext "lpadmin: System error while trying to delete default printer\n" 1>&2
fi
else
# there was no _default, the work is done
exit_code=0
fi
else
# add/change _default entry in /etc/printers.conf
${LPGET} -k bsdaddr ${second} >/dev/null 2>&1
exit_code=$?
if [ $exit_code -eq 0 ] ; then
${LPSET} -n system -a use=${second} _default
exit_code=$?
else
echo "${second}: " 1>&2
gettext "undefined printer\n" 1>&2
fi
fi
exit ${exit_code}
fi
# Strip off legal options
while getopts "A:ac:D:e:f:F:H:hi:I:lm:Mn:o:p:Q:r:S:s:T:u:U:v:W:x:t:P:" arg
do
case $arg in
D)
description="${OPTARG}"
;;
p)
if [ -n "${delete}" ] ; then
usage
fi
printer=${OPTARG}
;;
s)
server=${OPTARG}
;;
v|U)
device=${OPTARG}
if [ ! -n "${server}" ] ; then
server=${HOST}
fi
;;
x)
if [ -n "${printer}" -o -n "${server}" -o \
-n "${device}" -o -n "${description}" ] ; then
usage
fi
delete=${OPTARG}
printer=${OPTARG}
if [ ${printer} = "all" ] ; then
local="true"
fi
;;
S|M|A)
local="true"
;;
c)
class=${OPTARG}
local="true"
if [ ! -f ${LPGET} ] ; then
gettext "lpadmin: System error; cannot set class\n " 1>&2
exit 2
fi
${LPGET} "${class}" > /dev/null 2>&1
lpget_class=$?
if [ ${lpget_class} -eq 0 -a ! -r /etc/lp/classes/"${class}" ] ; then
gettext "lpadmin: ERROR: Can't create class ${class}.\n" 1>&2
gettext " TO FIX: This is an existing printer name;\n" 1>&2
gettext " choose another name.\n" 1>&2
exit 1
fi
;;
r)
pconflocalclean="true"
local="true"
;;
esac
done
#
# We don't have anything to do; let user know and bail
#
if [ ! -n "${printer}" -a ! -n "${delete}" -a ! -n "${local}" ] ; then
gettext "lpadmin: ERROR: Nothing to do.\n" 1>&2
gettext " TO FIX: You must give one of these options:\n" 1>&2
gettext " -p, -d, -x -S\n" 1>&2
exit 1
fi
#
# Printer does not exist
# To be consistent with 2.5, assume adding local printer
#
if [ ! -n "${device}" -a ! -n "${server}" -a ! -n "${delete}" \
-a ! -n "${local}" ] ; then
${LPGET} "${printer}" > /dev/null 2>&1
lpget_stat=$?
if [ ${lpget_stat} -ne 0 ] ; then
gettext "lpadmin: ERROR: Missing -U or -v option.\n" 1>&2
gettext " TO FIX: Local printers must have\n" 1>&2
gettext " a port defined (-v option) or\n" 1>&2
gettext " have dial-out instructions (-U option).\n" 1>&2
exit 1
fi
fi
#
# Do the LP configuration for a local printer served by lpsched
#
if [ -f /usr/lib/lp/local/$cmd_name ] ; then
if [ -f /etc/lp/printers/${printer}/configuration -o -n "${device}" -o \
-f /etc/lp/classes/${printer} -o -n "${local}" ] ; then
# to deal with multi-word arguments
CMD="/usr/lib/lp/local/$cmd_name"
while [ -n "$*" ] ; do
CMD="$CMD \"$1\""
shift
done
case "$CMD" in
*\"-D\")
CMD="$CMD \"\""
;;
esac
# if adding a printer, make sure scheduler is running
if [ -n "${printer}" -a ! -n "${delete}" -a \
! -p /var/spool/lp/fifos/FIFO ]; then
svcadm enable -s svc:/application/print/server:default
fi
eval $CMD
exit_code=$?
fi
fi
if [ $exit_code != 0 ] ; then
exit $exit_code
fi
# split the "server" into printer and server
if [ -n "${server}" ] ; then
if [ `echo ${server} | /bin/grep -c !` = 1 ] ; then
rem_printer=`echo ${server} | cut -d! -f2`
fi
server=`echo ${server} | cut -d! -f1`
fi
if [ -z "${rem_printer}" ] ; then
rem_printer=${printer}
fi
#
# Do the Solstice Print Configuration in /etc
#
if [ ! -f ${LPSET} -o ! -f ${LPGET} ] ; then
exit_code=2
else
if [ -n "${delete}" ] ; then
if [ "${delete}" = "all" ] ; then
delete_entries
else
${LPSET} -n system -x ${delete}
exit_code=$?
delete_local
fi
fi
if [ -n "${printer}" -a -n "${server}" ] ; then
${LPSET} -n system \
-a "bsdaddr=${server},${rem_printer},Solaris" \
${printer}
exit_code=$?
fi
if [ -n "${printer}" -a -n "${description}" ] ; then
${LPSET} -n system -a "description=${description}" ${printer}
exit_code=$?
fi
# Add class for local printers only
if [ -n "${class}" -a -n "${printer}" \
-a -f /etc/lp/printers/${printer}/configuration ] ; then
${LPGET} "${class}" > /dev/null 2>&1
lpget_class=$?
# If the class doesn't already exist in printers.conf, add it.
if [ -n "${server}" ] ; then
CHOST=$server
else
CHOST=$HOST
fi
if [ ${lpget_class} -ne 0 ] ; then
${LPSET} -n system \
-a "bsdaddr=${CHOST},${class},Solaris" ${class}
exit_code=$?
fi
fi
fi
# /usr/lib/lp/local/lpadmin has changed the database. This cleans up cruft in the
# /etc/printers.conf file that refers to deleted objects.
if [ -n "${pconflocalclean}" ] ; then
delete_local
fi
exit $exit_code
|