2 # Functions for solaris/hpux-style printer management
5 # But first define the default.
6 $config{'lpschedcmd'} = "/usr/lib/lp/lpsched";
7 if (-x "/usr/lib/lpsched") {
8 $config{'lpschedcmd'} = "/usr/lib/lpsched";
11 $interface_dir = "/usr/lib/lp/model";
12 $default_interface = "$interface_dir/standard";
13 $netstandard_interface = "$interface_dir/netstandard";
14 $foomatic_interface = "$interface_dir/standard_foomatic";
15 $foomatic_netstandard_interface = "$interface_dir/netstandard_foomatic";
18 # Returns an array of known printer names
21 return () if (!&sched_running());
23 if (open(CONF, "/etc/printers.conf")) {
24 # Printers can be read from a file
28 if (/^(\S+):/ && $1 ne '_default') { push(@rv, $1); }
33 # Must use a command to get printers
34 open(STAT, "lpstat -v |");
36 if (/\s+for\s+(\S+):/i && $1 ne '_default') { push(@rv, $1); }
43 # get_printer(name, [nostatus])
44 # Returns a reference to an associative array of printer details
47 local($stat, @rv, $body, $avl, $con, $sys, %prn, $_, $out);
48 local $esc = quotemeta($_[0]);
49 $out = &backquote_command("lpstat -l -p $esc", 1);
50 if ($out =~ /^printer\s+(\S+)\s*(.*)\s+(enabled|disabled)\s+since\s+([^\.]*)\.\s+(.*)\.\n([\000-\377]*)$/) {
53 $prn{'enabled'} = $3 eq "enabled";
56 elsif ($out =~ /^printer\s+(\S+)\s+waiting for auto-retry.\s+(\S+)\.\n([\000-\377]*)$/) {
57 # printer has some problem
66 if (!$prn{'enabled'} && $body =~ /^\s+(.*)/) {
67 $prn{'enabled_why'} = $1 eq "unknown reason" ? "" : $1;
69 if ($body =~ /Description: (.*)/) { $prn{'desc'} = $1; }
70 if ($body =~ /Printer types: (.*)/) { $prn{'ptype'} = $1; }
71 if ($body =~ /Interface: (.*)/ && $1 ne $default_interface)
72 { $prn{'iface'} = $1; }
73 if ($body =~ /Banner not required/) { $prn{'banner'} = 0; }
74 else { $prn{'banner'} = 1; }
75 if ($body =~ /Content types: (.*)/) { $prn{'ctype'} = [ split(/[ ,]+/, $1) ]; }
76 if ($body =~ /Users (allowed|denied):\n((\s+\S+\n)+)/) {
78 @l = grep { $_ } split(/\s+/, $2);
79 if ($1 eq "allowed") {
80 if ($l[0] eq "(all)") { $prn{'allow_all'} = 1; }
81 elsif ($l[0] eq "(none)") { $prn{'deny_all'} = 1; }
82 else { $prn{'allow'} = \@l; }
84 else { $prn{'deny'} = \@l; }
86 if ($body =~ /Options:\s*(.*)/) {
89 foreach $o (split(/,\s*/, $opts)) {
90 local ($on, $ov) = split(/=/, $o);
91 $prn{'options'}->{$on} = $ov;
94 if ($body =~ /PPD:\s+(\S+)/ && $1 ne "none" && $1 ne "/dev/null") {
99 # request availability
100 $avl = &backquote_command("lpstat -a $esc 2>&1", 1);
101 if ($avl =~ /^\S+\s+not accepting.*\n\s+(.*)/) {
102 $prn{'accepting'} = 0;
103 $prn{'accepting_why'} = $1;
104 if ($prn{'accepting_why'} eq "unknown reason") {
105 $prn{'accepting_why'} = "";
108 else { $prn{'accepting'} = 1; }
112 $con = &backquote_command("lpstat -v $esc 2>&1", 1);
113 if ($con =~ /^device for \S+:\s+(\S+)/) {
114 # Prints to a local file
116 if ($prn{'dev'} eq "/dev/null" &&
117 ($prn{'iface'} eq $netstandard_interface ||
118 $prn{'iface'} eq $foomatic_netstandard_interface)) {
119 # Actually a remote TCP printer
120 local ($dh, $dp) = split(/:/, $prn{'options'}->{'dest'});
125 delete($prn{'iface'});
129 elsif ($con =~ /^system for \S+:\s+(\S+)\s+\(as printer (\S+)\)/ ||
130 $con =~ /^system for \S+:\s+(\S+)/) {
131 # Prints to a remote server
133 $prn{'rqueue'} = $2 || $prn{'name'};
134 $sys = &backquote_command("lpsystem -l $prn{'rhost'} 2>&1", 1);
135 $sys =~ /Type:\s+(\S+)/; $prn{'rtype'} = $1;
138 # Check if this is the default printer
139 if (!defined($default_printer)) {
140 if (&backquote_command("lpstat -d 2>&1", 1) =~ /destination:\s+(\S+)/) {
141 $default_printer = $1;
144 if ($default_printer eq $prn{'name'}) { $prn{'default'} = 1; }
153 if ($_[0] =~ /\-/ || $config{'always_lpq'}) {
154 # Apparently lpq must be used if the printer name contains a -
155 local $esc = quotemeta($_[0]);
156 local $lpq = &has_command("lpq") || "/usr/ucb/lpq";
157 open(LPQ, "$lpq -P$esc |");
160 if (/^Rank\s+Owner\s+/i) { $doneheader++; }
161 elsif ($doneheader &&
162 /^(\S+)\s+(\S+)\s+(\d+)\s+(.*\S)\s+(\d+)\s+(\S+)$/) {
168 $job{'printing'} = ($1 eq "active");
175 # Can use the normal lpstat command
176 local $esc = quotemeta($_[0]);
177 open(STAT, "lpstat -o $esc |");
179 if (/^(\S+-(\d+))\s+(\S+)\s+(\d+)\s+(\S+ \d+ \d+:\d+)\s+(.*)/) {
180 local(%job, $d, $f, @pf);
186 $job{'printing'} = ($6 =~ /^on /);
187 if ($job{'user'} =~ /(\S+)\!/ &&
188 -d ($d="/var/spool/lp/tmp/$1")) {
190 foreach $f (readdir(DIR)) {
192 if ($f =~ /^$id-[1-9]/);
195 $job{'printfile'} = @pf ? \@pf : undef;
205 # printer_support(option)
208 return $_[0] !~ /^(msize|alias|riface|rnoqueue|ipp)$/;
212 # Returns an associative array of print classes
216 $stat = &backquote_command("lpstat -c 2>&1", 1);
217 while($stat =~ /^members of class (\S+):\n((\s+\S+\n)+)([\000-\377]*)$/) {
219 $rv{$1} = [ grep { $_ ne "" } split(/\s+/, $2) ];
224 # create_printer(&details)
225 # Create a new printer
228 &modify_printer($_[0]);
231 # modify_printer(&details)
232 # Change an existing printer
235 local(%prn, $cmd, $out);
237 local $old = &get_printer($prn{'name'});
239 # call lpsystem if needed
240 local $tesc = quotemeta($prn{'rtype'});
241 local $resc = quotemeta($prn{'rhost'});
242 local $qesc = quotemeta($prn{'rqueue'});
244 $out = &backquote_logged(
245 "lpsystem -t $tesc $resc 2>&1");
246 if ($?) { &error("lpsystem failed : <pre>$out</pre>"); }
250 local $esc = quotemeta($prn{'name'});
251 local $desc = quotemeta($prn{'desc'}) || "''";
252 $cmd = "lpadmin -p $esc -D $desc";
253 if ($prn{'allow_all'}) { $cmd .= " -u allow:all"; }
254 elsif ($prn{'deny_all'}) { $cmd .= " -u deny:all"; }
255 elsif ($prn{'allow'}) {
256 &system_logged("lpadmin -p $esc -u deny:all >/dev/null 2>&1");
257 $cmd .= " -u allow:".join(',', map { quotemeta($_) } @{$prn{'allow'}});
259 elsif ($prn{'deny'}) {
260 &system_logged("lpadmin -p $esc -u allow:all >/dev/null 2>&1");
261 $cmd .= " -u deny:".join(',', map { quotemeta($_) } @{$prn{'deny'}});
264 # Just printing to a device file
265 local $vesc = quotemeta($prn{'dev'});
268 local $iesc = quotemeta($prn{'iface'});
273 $cmd .= " -i $foomatic_interface";
276 $cmd .= " -i $default_interface";
279 if ($prn{'banner'}) { $cmd .= " -o banner"; }
280 else { $cmd .= " -o nobanner"; }
282 elsif ($prn{'dhost'}) {
283 # Printing to a remote host
284 local $hesc = quotemeta($prn{'dhost'});
285 local $pesc = quotemeta($prn{'dport'});
286 $cmd .= " -v /dev/null -o dest=$hesc:$pesc -o protocol=tcp";
288 $cmd .= " -i $foomatic_netstandard_interface";
291 $cmd .= " -i $netstandard_interface";
293 if ($prn{'banner'}) { $cmd .= " -o banner"; }
294 else { $cmd .= " -o nobanner"; }
297 # Printing to remote LPR server
298 $cmd .= " -s $resc!$qesc";
301 # Add any content types
302 local @ctype = @{$prn{'ctype'}};
304 $cmd .= " -I ".join(',' , @ctype);
308 if ($_[0]->{'ppd'}) {
309 $cmd .= " -n ".quotemeta($_[0]->{'ppd'});
311 elsif ($old->{'ppd'}) {
312 # Need to clear out PPD .. but how?
313 $cmd .= " -n /dev/null";
316 local $out = &backquote_logged("cd / ; $cmd 2>&1");
317 if ($?) { &error("lpadmin failed : <pre>$out ($cmd)</pre>"); }
320 if ($prn{'default'}) {
321 $out = &backquote_logged("cd / ; lpadmin -d $esc 2>&1");
322 if ($?) { &error("Failed to set default : <pre>$out</pre>"); }
326 &open_execute_command(STAT, "/usr/bin/ls -1 /etc/lp/fd/*.fd", 1, 1);
328 $file = substr($_, rindex($_, "/") +1, -4 );
329 &system_logged("/usr/sbin/lpfilter -f $file -F /etc/lp/fd/$file.fd");
334 if ($prn{'enabled'}) { $cmd = "enable $esc"; }
335 elsif ($prn{'enabled_why'}) {
336 local $wesc = quotemeta($prn{'enabled_why'});
337 $cmd = "enable $esc ; disable -r $wesc $esc";
339 else { $cmd = "enable $esc ; disable $esc"; }
340 $out = &backquote_logged("$cmd 2>&1");
342 # accepting or rejecting requests
343 if ($prn{'accepting'}) { $cmd = "accept $esc"; }
344 elsif ($prn{'accepting_why'}) {
345 local $wesc = quotemeta($prn{'accepting_why'});
346 $cmd = "accept $esc ; reject -r $wesc $esc";
348 else { $cmd = "accept $esc ; reject $esc"; }
349 $out = &backquote_logged("$cmd 2>&1");
352 # delete_printer(name)
353 # Deletes some existing printer
357 local $esc = quotemeta($_[0]);
358 $out = &backquote_logged("lpadmin -x $esc 2>&1");
359 if ($?) { &error("lpadmin failed : <pre>$out</pre>"); }
362 # cancel_job(printer, id)
363 # Cancels some print job
367 local $iesc = quotemeta($_[1]);
368 if ($_[0] =~ /\-/ || $config{'always_lpq'}) {
369 # lprm must be used if printer name contains a -
370 local $esc = quotemeta($_[0]);
371 local $lprm = &has_command("lprm") || "/usr/ucb/lprm";
372 $out = &backquote_logged("$lprm -P$esc $iesc 2>&1");
373 if ($?) { &error("cancel failed : <pre>$out</pre>"); }
376 $out = &backquote_logged("cancel $iesc 2>&1");
377 if ($?) { &error("cancel failed : <pre>$out</pre>"); }
383 # Returns the pid if lpsched is running, 0 if not, -1 if cannot be stopped
386 local @pid = &find_byname("lpsched");
387 if (@pid) { return $pid[0]; }
395 local $out = &backquote_logged("$config{'lpschedcmd'} 2>&1");
396 if ($?) { &error("failed to start lpsched : <tt>$out</tt>"); }
400 # Stop the running lpsched process
403 local $out = &backquote_logged("lpshut 2>&1");
404 if ($?) { &error("lpshut failed : <tt>$out</tt>"); }
407 # print_command(printer, file)
408 # Returns the command to print some file on some printer
411 local $esc = quotemeta($_[0]);
412 local $fesc = quotemeta($_[1]);
413 return "lp -d $esc $fesc";
416 # check_print_system()
417 sub check_print_system
419 local $lpschedcmd = $config{'lpschedcmd'};
420 &has_command("lpstat") || return &text('solaris_ecmd', "<tt>lpstat</tt>");
421 &has_command("$lpschedcmd") || return &text('solaris_ecmd', "<tt>$lpschedcmd</tt>");
433 # There are a string of files that could be used here, include only the
434 # ones that actually exist. Start with the parallel ports, and then the
435 # term ports, lp and printers/*.
437 $devlist = "/dev/bpp*";
438 $devlist .= " /dev/ecpp*";
439 $devlist .= " /dev/term/*";
440 $devlist .= " /dev/lp*";
441 $devlist .= " /dev/printers/*";
442 open(DEV, "/bin/find $devlist -print 2>/dev/null |");
448 # And also include /dev/null
450 push (@files, "/dev/null");
456 # get_device_names(@files)
463 # There are a string of files that could be used here, include only the
464 # ones that actually exist. Start with the parallel ports, and then the
465 # term ports, lp and printers/*.
468 if (/bpp/) { push(@names, "$text{'solaris_paralel'} - $_"); }
469 elsif (/ecpp/) { push(@names, "$text{'solaris_paralel'} - $_"); }
470 elsif (/term\/a/) { push(@names, &text('solaris_serial', 'A') . " - $_"); }
471 elsif (/term\/b/) { push(@names, &text('solaris_serial', 'B') . " - $_"); }
472 elsif (/null/) { push(@names, "$text{'solaris_null'} - $_"); }
473 else { push(@names, "$_"); }
480 @device_files = &get_device_files();
481 @device_names = &get_device_names(@device_files);