5 # The contents of this file are subject to the terms of the
6 # Common Development and Distribution License (the "License").
7 # You may not use this file except in compliance with the License.
9 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 # or http://www.opensolaris.org/os/licensing.
11 # See the License for the specific language governing permissions
12 # and limitations under the License.
14 # When distributing Covered Code, include this CDDL HEADER in each
15 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 # If applicable, add the following below this CDDL HEADER, with the
17 # fields enclosed by brackets "[]" replaced with your own identifying
18 # information: Portions Copyright [yyyy] [name of copyright owner]
23 # Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 # Use is subject to license terms.
28 PATH
=/bin
:/usr
/bin
:/usr
/sbin
export PATH
30 TEXTDOMAIN
="SUNW_OST_OSCMD"
33 PFEXEC
=/usr
/bin
/pfexec
36 LPSTAT
=/usr
/bin
/lpstat
37 LPADMIN
=/usr
/lib
/lp
/local
/lpadmin
38 LPFILTER
=/usr
/sbin
/lpfilter
40 PPDMGR
=/usr
/sbin
/ppdmgr
41 MKTEMP
="/usr/bin/mktemp -t"
47 gettext "Usage:\n" 1>&2
48 gettext " lpadmin -p (printer) (options)\n" 1>&2
49 gettext " lpadmin -x (dest)\n" 1>&2
50 gettext " lpadmin -d (dest)\n" 1>&2
51 gettext " lpadmin -S print-wheel -A alert-type [ -W minutes ]\n" 1>&2
52 gettext " [ -Q requests ]\n" 1>&2
53 gettext " lpadmin -M -f form-name [ -a [ -o filebreak ]\n" 1>&2
54 gettext " [ -t tray-number ]]\n" 1>&2
58 # create a filter table for LP service
60 if [[ ! -f /etc
/lp
/filter.table
]] ; then
61 cd /etc
/lp
/fd
; for filter
in *.fd
; do
62 ${PFEXEC} ${LPFILTER} \
63 -f $
(/usr
/bin
/basename $filter .fd
) \
69 # enable/disable LP related service(s)
70 lp_config_service
() { # (enable | disable)
71 svcadm
${1} -s svc
:/application
/print
/server
:default
72 # svcadm ${1} -s svc:/application/print/rfc1179:default
75 # synchronize printers.conf with LP configuration changes
76 lp_config_sync_pconf
() { # (pre) (post)
77 ADDED
=$
(${COMM} -13 ${1} ${2})
78 REMOVED
=$
(${COMM} -23 ${1} ${2})
80 lp_server
=${server:-${HOST}}
81 for DEST
in ${ADDED} ; do
82 lp_uri
="ipp://${lp_server}/printers/${DEST}"
83 lp_bsdaddr
="${lp_server},${DEST},Solaris"
85 -a "printer-uri-supported=${lp_uri}" \
86 -a "bsdaddr=${lp_bsdaddr}" \
90 for DEST
in ${REMOVED} ; do
91 ${LPSET} -n system
-x ${DEST} 2>/dev
/null
95 # Delete all destinations in printers.conf
97 for DEST
in $
(lpget
-n system list |
egrep -e '.+:$' |
sed -e 's/://')
99 ${LPSET} -n system
-x ${DEST}
104 # Call the ppdmgr utility to add a new PPD file to the system.
106 # $1 - path to PPD file
107 # $2 - label name (optional)
109 # Add new ppd file and echo full path it was actually saved to
110 ppdmgrcmd
="${PFEXEC} ${PPDMGR} -a ${1} -w"
112 ppderrfile
=$
(${MKTEMP} lpadminerror.XXXXXX
)
113 if [[ -z "${ppderrfile}" ]] ; then
114 gettext "lpadmin: System error; cannot create temporary file\n" 1>&2
117 ppd_file
=$
(${ppdmgrcmd} 2>${ppderrfile})
119 if [[ -s "${ppderrfile}" ]] ; then
120 print
-n "lpadmin: " 1>&2
121 cat ${ppderrfile} 1>&2
122 rm -f ${ppderrfile} >/dev
/null
2>&1
123 if [[ ${ppdmgrrc} -ne 0 ]] ; then
127 rm -f ${ppderrfile} >/dev
/null
2>&1
131 # Execution begins here
134 # be sure that we can run lpset and lpget
135 if [[ ! -x ${LPSET} ||
! -x ${LPGET} ]] ; then
136 gettext "lpadmin: System error; cannot set default printer\n" 1>&2
140 if [[ $# -lt 1 ]] ; then
145 # Deal with the -d option independently since getopts does not handle
146 # options that may or may not have arguments
148 if [[ ${1} = "-d" ]] ; then
149 if [[ $# -eq 1 ]] ; then # remove the "default"
150 ${LPGET} -n system _default
>/dev
/null
2>&1
153 if [[ ${exit_code} -eq 0 ]] ; then
154 ${LPSET} -n system
-x _default
156 else # no default, nothing to do
159 elif [[ $# -eq 2 ]] ; then # add/change the "default"
160 ${LPGET} -k bsdaddr
${2} >/dev
/null
2>&1
163 if [[ $exit_code -eq 0 ]] ; then
164 ${LPSET} -n system
-a "use=${2}" _default
166 else # can't set default to an unconfigured printer
167 gettext "${2}: undefined printer\n" 1>&1
177 # Strip off legal options
178 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
182 description
="${OPTARG}"
188 if [[ -n "${delete}" ]] ; then
198 if [[ ! -n "${server}" ]] ; then
204 if [[ -n "${printer}" ||
-n "${server}" || \
205 -n "${device}" ||
-n "${description}" ]] ; then
210 if [[ ${printer} = "all" ]] ; then
220 if [[ ! -f ${LPGET} ]] ; then
221 gettext "lpadmin: System error; cannot set class\n " 1>&2
225 ${LPGET} "${class}" > /dev
/null
2>&1
227 if [[ ${lpget_class} -eq 0 && ! -r /etc
/lp
/classes
/"${class}" ]] ; then
228 gettext "lpadmin: ERROR: Can't create class ${class}.\n" 1>&2
229 gettext " TO FIX: This is an existing printer name;\n" 1>&2
230 gettext " choose another name.\n" 1>&2
241 # We don't have anything to do; let user know and bail
243 if [[ ! -n "${printer}" && ! -n "${delete}" && ! -n "${local}" ]] ; then
244 gettext "lpadmin: ERROR: Nothing to do.\n" 1>&2
245 gettext " TO FIX: You must give one of these options:\n" 1>&2
246 gettext " -p, -d, -x -S\n" 1>&2
251 # Printer does not exist
252 # To be consistent with 2.5, assume adding local printer
254 if [[ ! -n "${device}" && ! -n "${server}" && ! -n "${delete}" && \
255 ! -n "${local}" ]] ; then
256 ${LPGET} "${printer}" > /dev
/null
2>&1
258 if [[ ${lpget_stat} -ne 0 ]] ; then
259 gettext "lpadmin: ERROR: Missing -U or -v option.\n" 1>&2
260 gettext " TO FIX: Local printers must have\n" 1>&2
261 gettext " a port defined (-v option) or\n" 1>&2
262 gettext " have dial-out instructions (-U option).\n" 1>&2
267 # process the "server" value
268 # It can be a hostname, UUCP form (server!queue), RCMD form(queue@server),
269 # or in URI form ({scheme}://{endpoint})
274 rem_printer
=$
(expr "${server}" : ".*://.*/\([^/]*\)")
275 server
=$
(expr "${server}" : ".*://\([^/]*\)/.*")
278 rem_printer
=$
(expr "${server}" : "\(.*\)@.*")
279 server
=$
(expr "${server}" : ".*@\(.*\)")
282 rem_printer
=$
(expr "${server}" : ".*!\(.*\)")
283 server
=$
(expr "${server}" : "\(.*\)!.*")
286 rem_printer
=${printer}
290 # if there is a "device" or LP configuration, it's local
291 if [[ -n "${device}" ||
-f /etc
/lp
/printers
/${printer}/configuration || \
292 -f /etc
/lp
/classes
/${printer} ]] ; then
296 # Do the LP configuration for a local printer served by lpsched
297 if [[ -x ${LPADMIN} && -n "${local}" ]] ; then
298 # enumerate LP configured printers before modification
299 PRE
=$
(${MKTEMP} lpadmin-pre.XXXXXX
)
300 if [[ -z "${PRE}" ]] ; then
301 gettext "lpadmin: System error; cannot create temporary file\n" 1>&2
305 (/bin
/ls /etc
/lp
/printers
2>/dev
/null
; /bin
/ls /etc
/lp
/classes \
308 # if there are no printers configured, enable LP service(s)
309 [[ ! -s "${PRE}" ]] && lp_config_service
enable
311 # add filters to LP service
314 # add new ppd file to PPD file repositories
315 if [[ -n "${ppd_file}" && -x ${PPDMGR} ]] ; then
316 add_new_ppd_file
"${ppd_file}"
319 # modify LP destination(s)
320 CMD
="${PFEXEC} ${LPADMIN}"
321 while [[ -n "$*" ]] ; do # to deal with multi-word arguments
323 # replace the ppd_file originally specified with the -n option
324 # with the one returned from call to ppdmgr
325 if [[ "${1}" = "-n" ]] ; then
326 CMD
="$CMD \"${ppd_file}\""
337 # execute the LP lpadmin command
341 # enumerate LP configured printers after modification
342 POST
=$
(${MKTEMP} lpadmin-post.XXXXXX
)
343 if [[ -z "${POST}" ]] ; then
344 gettext "lpadmin: System error; cannot create temporary file\n" 1>&2
345 /bin
/rm -f ${PRE} >/dev
/null
2>&1
349 (/bin
/ls /etc
/lp
/printers
2>/dev
/null
; /bin
/ls /etc
/lp
/classes \
350 2>/dev
/null
) >${POST}
352 # if there are no destinations, disable the service(s)
353 [[ ! -s "${POST}" ]] && lp_config_service disable
355 # sync printers.conf with LP configuration
356 lp_config_sync_pconf
"${PRE}" "${POST}"
358 /bin
/rm -f ${PRE} ${POST}
361 # Do any printers.conf configuration that is required
362 if [[ -n "${delete}" ]] ; then
363 if [[ "${delete}" = "all" ]] ; then
364 [[ $exit_code -eq 0 ]] && delete_all
365 elif [[ -z "${local}" ]] ; then
366 ${LPSET} -n system
-x ${delete}
370 if [[ -z "${local}" ]] ; then
371 # if we need a uri, find the "best" one.
372 if [[ -z "${uri}" ]] ; then
373 uri
="ipp://${server}/printers/${rem_printer}"
374 ${LPSTAT} -p ${uri} >/dev
/null
2>&1
375 if [[ $?
-ne 0 ]] ; then
376 uri
="lpd://${server}/printers/${rem_printer}#Solaris"
380 bsdaddr
="${server},${rem_printer},Solaris"
382 if [[ -n "${printer}" && -n "${server}" ]] ; then
384 -a "printer-uri-supported=${uri}" \
385 -a "bsdaddr=${bsdaddr}" ${printer}
391 if [[ -n "${printer}" && -n "${description}" ]] ; then
393 -a "description=${description}" ${printer}
398 # if the "default" doesn't resolve a "bsdaddr", the printer is gone, remove it
399 ${LPGET} -n system
-k bsdaddr _default
>/dev
/null
2>&1 ||
400 ${LPSET} -n system
-x _default
>/dev
/null
2>&1