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]
22 # Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 # Use is subject to license terms.
28 # This program manages the "active" print service selection.
29 # If called as 'print-service', it takes one of four options.
31 # [-s[et] service [-m]] Select the "active" print service, optionally
32 # migrating basic print queue configuration.
33 # [-q[uery]] Display the "active" print service.
34 # [-e[xport] file] Export basic print queue configuration to
36 # [-i[mport] file] Import basic print queue configuration from
39 # If called by any other name, it will look for a corresponding command
40 # under /usr/lib/{active-service}/bin/{command} and execute that program
41 # with the original arguments.
47 use File
::Temp qw
/ :POSIX /;
49 my $cmd = basename
($0);
51 my $LPSTAT = '/usr/bin/lpstat';
52 my $LPADMIN = '/usr/sbin/lpadmin';
53 my $ENABLE = '/usr/bin/enable';
54 my $ACCEPT = '/usr/sbin/accept';
55 my $SVCADM = '/usr/sbin/svcadm';
56 my $SVCPROP = '/usr/bin/svcprop';
57 my $SVCCFG = '/usr/sbin/svccfg';
58 my $SVC_LP_SCHEDULER = 'print/server';
59 my $SVC_LP_LPD = 'print/rfc1179';
60 my $SVC_LP_IPP = 'print/ipp-listener';
61 my $SVC_LP_PPD = 'print/ppd-cache-update';
62 my $SVC_CUPS_SCHEDULER = 'cups/scheduler';
63 my $SVC_CUPS_LPD = 'cups/in-lpd';
66 ($ENV{"DESKTOP_LAUNCHED"}) &&
67 exec("/bin/zenity", "--error", "--text=@_");
75 $cmd [-s[et] service [-m]] Select the \"active\" print service,
76 optionally migrating basic print queue
78 $cmd [-q[uery]] Display the "active" print service.
79 $cmd [-e[xport] file] Export basic print queue configuration
81 $cmd [-i[mport] file] Import basic print queue configuration
88 local ($fmri, $property) = @_;
91 open($FH, "$SVCPROP -C -p $property $fmri 2>/dev/null |");
99 local ($fmri, $operation) = @_;
102 open($FH, "$SVCCFG -s $fmri \"$operation\" 2>/dev/null |");
110 local ($operation, @fmris) = @_;
112 system("$SVCADM $operation -s @fmris");
119 $service = svcprop
("$SVC_CUPS_SCHEDULER:default", "general/active");
120 ($service =~ /true/) && ($service = 'cups') || ($service = 'lp');
126 local($command, @av) = @_;
127 my $service = print_service
();
129 if (!defined($service)) {
130 fatal
("failed to detect active print service: $!\n");
133 if (! -d
"/usr/lib/$service/bin") {
134 fatal
("print service: $service is not installed\n");
137 my $executable = "/usr/lib/$service/bin/$command";
138 # CUPS has it's own names for enable and disable
139 ($command =~ /(en|dis)able/) && ($service eq 'cups') &&
140 (! -x
$executable) &&
141 ($executable = "/usr/lib/$service/bin/$service$command");
143 if (! -x
$executable) {
144 fatal
("$command is not available from $service print service\n");
147 exec($executable, @ARGV);
150 sub export_print_queues
{
152 my $service = print_service
();
154 if ($service eq 'lp') {
158 open($DFH, "$LPSTAT -v|");
160 if (/device for (.+): (.+)/) {
163 print $FH "<Printer $1>\nDeviceURI $2\n";
164 open($EFH, "$LPSTAT -p $1 -l |");
166 (/Description: (.+)/) &&
167 print $FH "Info $1\n";
170 print $FH "</Printer>\n";
176 copy
('/etc/cups/printers.conf', $path);
185 sub import_print_queues
{
187 my $service = print_service
();
188 my $FH, %printer, @options;
190 # store queue info in the 'active' print service
193 if (/<Printer (.+)>/) {
194 $printer{'Printer'} = $1;
196 push(@options, "-p", $1);
197 } elsif (/([^\s]+)\s(.+)/) {
200 ($1 eq 'DeviceURI') &&
201 push(@options, "-v", $value);
203 push(@options, "-D", $value);
204 } elsif (/<\/Printer
>/) {
205 ($service eq 'lp') &&
206 push(@options, "-m", "uri");
207 print "importing $printer{'Printer'}...\n";
209 psystem
($LPADMIN, @options);
210 psystem
($ENABLE, $printer{'Printer'});
211 ($printer{'Accepting'} eq 'Yes') &&
212 psystem
($ACCEPT, $printer{'Printer'});
220 my ($service, $migrate) = @_;
223 if (! -d
"/usr/lib/$service/bin") {
224 fatal
("print service: $service is not installed\n");
228 # export old print queue configuration (if migrating)
230 export_print_queues
($queues);
233 # enable/disable the services
234 if ($service eq 'cups') {
235 (-f
'/etc/printers.conf') && (! -f
'/etc/lp/printers.conf') &&
236 rename('/etc/printers.conf', '/etc/lp/printers.conf');
237 print("disabling LP services...\n");
238 svcadm
("disable", $SVC_LP_SCHEDULER, $SVC_LP_IPP, $SVC_LP_LPD,
240 print("enabling CUPS services...\n");
241 svcadm
("enable", $SVC_CUPS_SCHEDULER, $SVC_CUPS_LPD);
242 svccfg
("cups/scheduler:default",
243 "setprop general/active = boolean: true");
244 system("pkill -USR1 -f '/desktop-print-management-applet'");
246 print("disabling CUPS services...\n");
247 svcadm
("disable", $SVC_CUPS_SCHEDULER, $SVC_CUPS_LPD);
248 print("enabling LP services...\n");
249 svcadm
("enable", $SVC_LP_SCHEDULER, $SVC_LP_IPP, $SVC_LP_LPD,
251 (-f
'/etc/lp/printers.conf') &&
252 rename('/etc/lp/printers.conf', '/etc/printers.conf');
253 svccfg
("cups/scheduler:default", "delprop general/active");
254 system("pkill -USR1 -f '/desktop-print-management-applet'");
257 # import the new print queue configuration (if migrating)
258 defined($queues) && import_print_queues
($queues);
262 my $service = print_service
();
264 if (!defined($service)) {
265 fatal
("failed to detect active print service: $!\n");
267 print "active print service: $service\n";
270 if ($cmd eq 'print-service') {
271 my ($import_path, $export_path, $svc_name, $query, $migrate) = ();
273 my $res = GetOptions
('q|query' => \
$query, 's|set=s' => \
$service,
274 'm|migrate' => \
$migrate, 'e|export=s' => \
$export_path,
275 'i|import=s' => \
$import_path);
279 if (defined($import_path) && !defined($export_path) &&
280 !defined($query) && !defined($service) && !defined($migrate)) {
281 import_print_queues
($import_path);
282 } elsif (!defined($import_path) && defined($export_path) &&
283 !defined($query) && !defined($service) && !defined($migrate)) {
284 export_print_queues
($export_path);
285 } elsif (!defined($import_path) && !defined($export_path) &&
286 defined($query) && !defined($service) && !defined($migrate)) {
288 } elsif (!defined($import_path) && !defined($export_path) &&
289 !defined($query) && defined($service)) {
290 select_service
($service, $migrate);
295 print_command
($cmd, @ARGV);