2 # Functions for UnixWare style printer management
4 $default_interface = "/usr/lib/lp/model/standard";
7 # Returns an array of known printer names
11 open(STAT, "lpstat -p |");
13 if (/^printer\s+(\S+)/) { push(@rv, $1); }
19 # get_printer(name, [nostatus])
20 # Returns a reference to an associative array of printer details
23 local($stat, @rv, $body, $avl, $con, $sys, %prn, @jobs, $_, $out);
24 local $esc = quotemeta($_[0]);
25 $out = `lpstat -l -p $esc`;
26 if ($out =~ /^printer\s+(\S+)\s*(.*)\s+(enabled|disabled)\s+since\s+([^\.]*)\.\s+(.*)\.\n([\000-\377]*)$/) {
29 $prn{'enabled'} = $3 eq "enabled";
32 elsif ($out =~ /^printer\s+(\S+)\s+waiting for auto-retry.\s+(\S+)\.\n([\000-\377]*)$/) {
33 # printer has some problem
42 if (!$prn{'enabled'} && $body =~ /^\s+(.*)/) {
43 $prn{'enabled_why'} = $1 eq "unknown reason" ? "" : $1;
45 if ($body =~ /Description: (.*)/) { $prn{'desc'} = $1; }
46 if ($body =~ /Printer types: (.*)/) { $prn{'ptype'} = $1; }
47 if ($body =~ /Interface: (.*)/) { $prn{'iface'} = $1; }
48 if ($body =~ /Banner not required/) { $prn{'banner'} = 0; }
49 else { $prn{'banner'} = 1; }
50 if ($body =~ /Content types: (.*)/) { $prn{'ctype'} = [ split(/[ ,]+/, $1) ]; }
51 if ($body =~ /Users (allowed|denied):\n((\s+\S+\n)+)/) {
53 @l = grep { $_ } split(/\s+/, $2);
54 if ($1 eq "allowed") {
55 if ($l[0] eq "(all)") { $prn{'allow_all'} = 1; }
56 elsif ($l[0] eq "(none)") { $prn{'deny_all'} = 1; }
57 else { $prn{'allow'} = \@l; }
59 else { $prn{'deny'} = \@l; }
63 # request availability
64 $avl = `lpstat -a $esc 2>&1`;
65 if ($avl =~ /^\S+\s+not accepting.*\n\s+(.*)/) {
66 $prn{'accepting'} = 0;
67 $prn{'accepting_why'} = $1;
68 if ($prn{'accepting_why'} eq "unknown reason") {
69 $prn{'accepting_why'} = "";
72 else { $prn{'accepting'} = 1; }
76 $con = `lpstat -v $esc 2>&1`;
77 if ($con =~ /^device for \S+:\s+(\S+)/) { $prn{'dev'} = $1; }
78 elsif ($con =~ /^system for \S+:\s+(\S+)\s+\(as printer (\S+)\)/) {
81 $sys = `lpsystem -l $prn{'rhost'} 2>&1`;
82 $sys =~ /Type:\s+(\S+)/; $prn{'rtype'} = $1;
85 # Check if this is the default printer
86 `lpstat -d 2>&1` =~ /destination: (\S+)/;
87 if ($1 eq $prn{'name'}) { $prn{'default'} = 1; }
97 local $lang = $gconfig{'lang'};
99 # Added 'env LANG=$lang'
100 local $esc = quotemeta($_[0]);
101 open(STAT, "env LANG=$lang lpstat -o $esc |");
103 # original line which does not work
104 # if (/^(\S+-\d+)\s+(\S+)\s+(\d+)\s+(\S+ \d+ \d+:\d+)\s+(.*)/) {
108 if ( $lang eq 'es' ) {
109 if (/^(\S+-\d+)\s+(\S+)\s+(\d+)\s+(\S+ \d+ \S+ \S+ \d+ \d+:\d+:\d+)\s+(.*)/) {
115 $job{'printing'} = ($5 =~ /^en /);
120 elsif ( $lang eq 'us' ) {
121 if (/^(\S+-\d+)\s+(\S+)\s+(\d+)\s+(\S+ \S+ \d+ \d+:\d+:\d+ \S+ \d+)\s+(.*)/) {
127 $job{'printing'} = ($5 =~ /^on /);
131 # short string (any language)
132 elsif (/^(\S+-\d+)\s+(\S+)\s+(\d+)\s+\s+(.*)/) {
138 $job{'printing'} = ($5 =~ /^on /);
146 # printer_support(option)
149 return $_[0] !~ /^(msize|alias|riface|direct|rnoqueue|ipp)$/;
153 # Returns an associative array of print classes
157 $stat = `lpstat -c 2>&1`;
158 while($stat =~ /^members of class (\S+):\n((\s+\S+\n)+)([\000-\377]*)$/) {
160 $rv{$1} = [ grep { $_ ne "" } split(/\s+/, $2) ];
165 # create_printer(&details)
166 # Create a new printer
169 &modify_printer($_[0]);
172 # modify_printer(&details)
173 # Change an existing printer
176 local(%prn, $cmd, $out);
179 # call lpsystem if needed
180 local $tesc = quotemeta($prn{'rtype'});
181 local $resc = quotemeta($prn{'rhost'});
182 local $qesc = quotemeta($prn{'rqueue'});
184 $out = &backquote_logged("lpsystem -t $tesc $resc 2>&1");
185 if ($?) { &error("lpsystem failed : <pre>$out</pre>"); }
189 local $esc = quotemeta($prn{'name'});
190 local $desc = quotemeta($prn{'desc'}) || "''";
191 $cmd = "lpadmin -p $esc -D $desc";
192 if ($prn{'allow_all'}) { $cmd .= " -u allow:all"; }
193 elsif ($prn{'deny_all'}) { $cmd .= " -u deny:all"; }
194 elsif ($prn{'allow'}) {
195 &system_logged("lpadmin -p $esc -u deny:all >/dev/null 2>&1");
196 $cmd .= " -u allow:".join(',', map { quotemeta($_) } @{$prn{'allow'}});
198 elsif ($prn{'deny'}) {
199 &system_logged("lpadmin -p $esc -u allow:all >/dev/null 2>&1");
200 $cmd .= " -u deny:".join(',', map { quotemeta($_) } @{$prn{'deny'}});
203 $cmd .= " -v $prn{'dev'}";
204 $cmd .= " -i \"$prn{'iface'}\"";
205 if ($prn{'banner'}) { $cmd .= " -o banner"; }
206 else { $cmd .= " -o nobanner"; }
208 else { $cmd .= " -s $resc!$qesc"; }
209 @ctype = @{$prn{'ctype'}};
210 if (@ctype) { $cmd .= " -I ".join(',' , @ctype); }
211 $out = &backquote_logged("$cmd 2>&1");
212 if ($?) { &error("lpadmin failed : <pre>$out</pre>"); }
215 if ($prn{'default'}) {
216 $out = &backquote_logged("lpadmin -d $esc 2>&1");
217 if ($?) { &error("Failed to set default : <pre>$out</pre>"); }
221 if ($prn{'enabled'}) { $cmd = "enable $esc"; }
222 elsif ($prn{'enabled_why'}) {
223 local $wesc = quotemeta($prn{'enabled_why'});
224 $cmd = "enable $esc ; disable -r $wesc $esc";
226 else { $cmd = "enable $esc ; disable $esc"; }
227 $out = &backquote_logged("$cmd 2>&1");
229 # accepting or rejecting requests
230 if ($prn{'accepting'}) { $cmd = "accept $esc"; }
231 elsif ($prn{'accepting_why'}) {
232 local $wesc = quotemeta($prn{'accepting_why'});
233 $cmd = "accept $esc ; reject -r $wesc $esc";
235 else { $cmd = "accept $esc ; reject $esc"; }
236 $out = &backquote_logged("$cmd 2>&1");
239 # delete_printer(name)
240 # Deletes some existing printer
244 local $esc = quotemeta($_[0]);
245 $out = &backquote_logged("lpadmin -x $esc 2>&1");
246 if ($?) { &error("lpadmin failed : <pre>$out</pre>"); }
249 # cancel_job(printer, id)
250 # Cancels some print job
254 local $iesc = quotemeta($_[1]);
255 $out = &backquote_logged("cancel $iesc 2>&1");
256 if ($?) { &error("cancel failed : <pre>$out</pre>"); }
261 # Returns the pid if lpsched is running, 0 if not, -1 if cannot be stopped
264 #@pid = &find_byname("lpsched");
265 #if (@pid) { return $pid[0]; }
274 local $out = &backquote_logged("/usr/lib/lp/lpsched 2>&1");
275 if ($?) { &error("failed to start lpsched : <tt>$out</tt>"); }
279 # Stop the running lpsched process
282 local $out = &backquote_logged("lpshut 2>&1");
283 if ($?) { &error("lpshut failed : <tt>$out</tt>"); }
286 # print_command(printer, file)
287 # Returns the command to print some file on some printer
290 local $esc = quotemeta($_[0]);
291 local $fesc = quotemeta($_[1]);
292 return "lp -d $esc $fesc";
295 # check_print_system()
296 sub check_print_system
298 &has_command("lpstat") || return &text('unixware_ecmd', "<tt>lpstat</tt>");
299 &has_command("/usr/lib/lp/lpsched") || return &text('unixware_ecmd', "<tt>lpsched</tt>");
303 @device_files = ("/dev/lp0", "/dev/lp1", "/dev/lp2",
304 "/dev/tty1a", "/dev/tty2a", "/dev/null");
305 @device_names = (&text('unixware_paralel', "0"), &text('unixware_paralel', "1"),
306 &text('unixware_paralel', "2"), &text('unixware_serial', "A"),
307 &text('unixware_serial', "B"), $text{'unixware_null'});