2 # Functions for hpux-style printer management
4 $hpux_iface_path = "/usr/spool/lp/interface";
7 # Returns an array of known printer names
11 open(STAT, "lpstat -p |");
13 if (/^printer\s+(\S+)/) { push(@rv, $1); }
17 # make models executable
18 &system_logged("chmod 755 $config{'model_path'}/* >/dev/null 2>&1");
23 # get_printer(name, [nostatus])
24 # Returns a reference to an associative array of printer details
27 local($stat, @rv, $body1, $body2, $avl, $con, $sys, %prn, $_, $out);
28 local $esc = quotemeta($_[0]);
29 $out = &backquote_command("lpstat -p$esc", 1);
30 if ($out =~ /^printer\s+(\S+)\s+(.*)\n(.*)\n(.*)$/) {
33 if ($2 =~ /enabled/) {$prn{'enabled'} = "enabled"};
43 $prn{'iface'} = "$hpux_iface_path/$prn{'name'}";
46 local $wdrv = &is_windows_driver($prn{'iface'});
47 local $hdrv = &is_hpnp_driver($prn{'iface'});
48 $prn{'desc'} = $wdrv ? &grep_interface($wdrv->{'program'}) :
49 $hdrv ? &grep_interface($hdrv->{'program'}) :
50 &grep_interface($prn{'iface'});
53 if (!$prn{'enabled'} && $body1 =~ /^\s+(.*)/) {
54 $prn{'enabled_why'} = $1 eq "reason unknown" ? "" : $1;
58 # request availability
59 $avl = &backquote_command("lpstat -a$esc 2>&1", 1);
60 if ($avl =~ /^\S+\s+not accepting.*\n\s+(.*)/) {
61 $prn{'accepting'} = 0;
62 $prn{'accepting_why'} = $1;
63 if ($prn{'accepting_why'} eq "reason unknown") {
64 $prn{'accepting_why'} = "";
67 else { $prn{'accepting'} = 1; }
71 $con = &backquote_command("lpstat -v$esc 2>&1", 1);
72 if ($con =~ /^device for \S+:\s+(\S+)\n\s+(remote to:)\s+(\S+)\s+(on)\s+(\S+)/) {
76 elsif ($con =~ /^device for \S+:\s+(\S+)/) { $prn{'dev'} = $1; }
78 # Check if this is the default printer
79 if (&backquote_command("lpstat -d 2>&1", 1) =~ /: (\S+)/ &&
89 local $esc = quotemeta($_[0]);
90 open(STAT, "lpstat -o$esc |");
91 local($id, $user, $prio, $when, $printing);
93 if (/^(\S+-\d+)\s+(\S+)\s+priority\s+(\S+)\s+(\S+.*\d+ \d+:\d+)\s+(.*)/) {
98 $printing = ($5 =~ /^on /);
100 elsif (/^(\S+-\d+)\s+(\S+)\s+priority\s+(\S+)\s+from\s+(\S+)\s+(.*)/) {
105 $printing = ($5 =~ /^on /);
107 elsif (/^(.*)\s+(\d+) bytes/) {
110 $job{'user'} = $user;
111 $job{'prio'} = $prio;
112 $job{'when'} = $when;
113 $job{'printing'} = $printing;
123 # grep_interface($iface)
124 # try to find a description in the interface/model
127 local($iface, $desc, $out);
130 local $drv = &is_driver($iface);
131 if ($drv->{'mode'} == 1) {
132 $desc = $drv->{'desc'};
134 elsif ($drv->{'mode'} == 2) {
135 $out = &backquote_command("head $drv->{'prog'} | grep -e interface -e Printer -e /model/", 1);
136 if ($out =~ /interface for\s+(.*)/) { $desc = $1; }
137 elsif ($out =~ /\s+(\S.*)interface/) { $desc = $1; }
138 elsif ($out =~ /Printer Command Language level\s+(\S+)/) { $desc = "PCL$1"; }
139 elsif ($out =~ /\/model\/(\S+).*/) { $desc = $1; }
140 elsif ($out =~ /^#\s+(.*)/) { $desc = $1; }
141 else { $desc = "None"; }
149 # printer_support(option)
152 return $_[0] !~ /^(allow|alias|ctype|banner|desc|editdest|msize|direct|rnoqueue|ipp)$/;
156 # Returns an associative array of print classes
160 $stat = &backquote_command("lpstat -c 2>&1", 1);
161 while($stat =~ /^members of class (\S+):\n((\s+\S+\n)+)([\000-\377]*)$/) {
163 $rv{$1} = [ grep { $_ ne "" } split(/\s+/, $2) ];
168 # create_printer(&details)
169 # Create a new printer
172 local(%prn, $cmd, $out, $model, $dummy, $scheduler);
174 local $wdrv = &is_windows_driver($prn{'iface'});
175 local $hdrv = &is_hpnp_driver($prn{'iface'});
176 $scheduler = &sched_running();
177 $dummy = "webmin.tmp";
178 &system_logged("touch $config{'model_path'}/$dummy");
180 # create lpadmin command
181 local $esc = quotemeta($prn{'name'});
182 $cmd = "lpadmin -p$esc";
184 ## remote unix printer
187 &error("lpadmin failed : <pre>No model allowed for remote unix printer.</pre>");
189 $cmd .= " -orm".quotemeta($prn{'rhost'});
190 $cmd .= " -orp".quotemeta($prn{'rqueue'});
192 $cmd .= " -ocmrcmodel";
193 $cmd .= " -osmrsmodel";
194 $cmd .= " -v/dev/null";
196 if ($prn{'rtype'} =~ /^BSD$/) {
201 ## remote windows printer
204 $cmd .= " -v/dev/null";
208 ## remote HPNP printer
211 $cmd .= " -v/dev/null";
215 ## local printer with webmin driver
216 elsif ($prn{'iface'} eq "$drivers_directory/$prn{'name'}") {
218 $cmd .= " -v".quotemeta($prn{'dev'});
222 ## local printer with HP-UX model
223 elsif ($prn{'iface'} =~ $config{'model_path'}) {
224 $model = substr($prn{'iface'}, length($config{'model_path'}) + 1);
226 $cmd .= " -v".quotemeta($prn{'dev'});
230 &error("lpadmin failed : <pre>Action not supported.</pre>");
234 $out = &backquote_logged("lpshut 2>&1");
237 $out = &backquote_logged("$cmd 2>&1");
238 if ($?) { &error("lpsched failed : <pre>$out</pre>"); }
240 ## Link to windows webmin driver
241 &lock_file("$hpux_iface_path/$prn{'name'}");
243 &unlink_file("$hpux_iface_path/$prn{'name'}");
244 &symlink_file("$drivers_directory/$prn{'name'}.smb",
245 "$hpux_iface_path/$prn{'name'}");
248 ## Link to webmin hpnp driver
250 &unlink_file("$hpux_iface_path/$prn{'name'}");
251 &symlink_file("$drivers_directory/$prn{'name'}.hpnp",
252 "$hpux_iface_path/$prn{'name'}");
255 ## Link to webmin driver
256 if ($prn{'iface'} eq "$drivers_directory/$prn{'name'}" && !$wdrv) {
257 &unlink_file("$hpux_iface_path/$prn{'name'}");
258 &symlink_file("$drivers_directory/$prn{'name'}",
259 "$hpux_iface_path/$prn{'name'}");
261 &unlock_file("$hpux_iface_path/$prn{'name'}");
263 &lock_file("$config{'model_path'}/$dummy");
264 &unlink_file("$config{'model_path'}/$dummy");
265 &unlock_file("$config{'model_path'}/$dummy");
269 $out = &backquote_logged("lpsched 2>&1");
270 if ($?) { &error("lpsched failed : <pre>$out</pre>"); }
273 &modify_printer($_[0]);
276 # modify_printer(&details)
277 # Change an existing printer
280 local(%prn, $cmd, $out);
284 local $esc = quotemeta($prn{'name'});
285 if ($prn{'default'}) {
286 $out = &backquote_logged("lpadmin -d$esc 2>&1");
287 if ($?) { &error("Failed to set default : <pre>$out</pre>"); }
291 if ($prn{'enabled'}) { $cmd = "enable $esc"; }
292 elsif ($prn{'enabled_why'}) {
293 local $wesc = quotemeta($prn{'enabled_why'});
294 $cmd = "enable $esc ; disable -r$wesc $esc";
296 else { $cmd = "enable $esc ; disable $esc"; }
297 $out = &backquote_logged("$cmd 2>&1");
299 # accepting or rejecting requests
300 if ($prn{'accepting'}) { $cmd = "accept $esc"; }
301 elsif ($prn{'accepting_why'}) {
302 local $wesc = quotemeta($prn{'accepting_why'});
303 $cmd = "accept $esc ; reject -r$wesc $esc";
305 else { $cmd = "accept $esc ; reject $esc"; }
306 $out = &backquote_logged("$cmd 2>&1");
309 # delete_printer(name)
310 # Deletes some existing printer
313 local($out, $scheduler);
314 $scheduler = &sched_running();
317 local $esc = quotemeta($_[0]);
318 $out = &backquote_logged("cancel $esc -a 2>1");
319 if ($?) { &error("cancel failed : <pre>$out</pre>"); }
322 $out = &backquote_logged("lpshut 2>&1");
325 $out = &backquote_logged("lpadmin -x$esc 2>&1");
326 if ($?) { &error("lpadmin failed : <pre>$out</pre>"); }
330 $out = &backquote_logged("lpsched 2>&1");
331 if ($?) { &error("lpsched failed : <pre>$out</pre>"); }
335 # cancel_job(printer, id)
336 # Cancels some print job
340 local $esc = quotemeta($_[1]);
341 $out = &backquote_logged("cancel $esc 2>&1");
342 if ($?) { &error("cancel failed : <pre>$out</pre>"); }
347 # Returns 1 if running and 0 if not running
350 local $out = &backquote_command("lpstat -r 2>&1", 1);
351 if ($out =~ /not/) { return 0; }
359 local $out = &backquote_logged("lpsched 2>&1");
360 if ($?) { &error("failed to start lpsched : <tt>$out</tt>"); }
364 # Stop the running lpsched process
367 local $out = &backquote_logged("lpshut 2>&1");
368 if ($?) { &error("lpshut failed : <tt>$out</tt>"); }
371 # print_command(printer, file)
372 # Returns the command to print some file on some printer
375 local $esc = quotemeta($_[0]);
376 local $iesc = quotemeta($_[1]);
377 return "lp -d $esc $iesc";
380 # check_print_system()
381 sub check_print_system
383 &has_command("lpstat") || return &text('hpux_ecmd', "<tt>lpstat</tt>");
384 -d $hpux_iface_path || return &text('hpux_eiface',"<tt>$hpux_iface_path</tt>");
388 @device_files = ("/dev/c1t0d0_lp", "/dev/c2t0d0_lp",
389 "/dev/c0p0_lp", "/dev/c0p1_lp",
390 "/dev/c0p2_lp", "/dev/c0p3_lp",
391 "/dev/c0p4_lp", "/dev/c0p4_lp",
392 "/dev/c0p5_lp", "/dev/c0p5_lp",
395 @device_names = (&text('hpux_paralel', "c1t0d0"), &text('hpux_paralel', "c2t0d0"),
396 &text('hpux_serial', "c0p0"), &text('hpux_serial', "c0p1"),
397 &text('hpux_serial', "c0p2"), &text('hpux_serial', "c0p3"),
398 &text('hpux_serial', "c0p4"), &text('hpux_serial', "c0p5"),
399 &text('hpux_serial', "c0p6"), &text('hpux_serial', "c0p7"),
400 &text('hpux_serial', "c1p0"), $text{'hpux_null'});