Printer/Unix.pm untested

filter: Statementstmt 0.0% 0 / 122 Branchbran 0.0% 0 / 60 Conditioncond 0.0% 0 / 39 MC/DCmcdc 0.0% 0 / 26 Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 256 CC 31 SCAR 47.0
CC
31
Cov
0%
list_jobs
182.0
use_default
90.0
list_printers
72.0
CRAP 109.5
1 ############################################################################
2 ############################################################################
3 ## ##
4 ## Copyright 2004 Stephen Patterson (steve@patter.mine.nu) ##
5 ## ##
6 ## A cross platform perl printer interface ##
7 ## This code is made available under the perl artistic licence ##
8 ## ##
9 ## Documentation is at the end (search for __END__) or process with ##
10 ## pod2man/pod2text/pod2html ##
11 ## ##
12 ## Debugging and code contributions from: ##
13 ## David W Phillips (ss0300@dfa.state.ny.us) ##
14 ## ##
15 ############################################################################
16 ############################################################################
17  
18 # routines specifically for unix like systems (linux/bsd etc)
19 # $self->{system} eq 'linux'
20  
21 # load environment variables which contain the default printer name (Linux)
22 # (from the lprng lpr command manpage)
23 use Env qw(PRINTER LPDEST NPRINTER NGPRINTER PATH);
24  
25 ############################################################################
26 sub list_printers {
27 # list available printers
28 my $self = shift();
29 my %printers;
30 my @prs;
31 if ( -f '/etc/printcap' ) {
32 # DWP - linux, dec_osf
33 open (PRINTCAP, '</etc/printcap') or
34 Carp::croak "Can't read /etc/printcap: $!";
35 while (<PRINTCAP>) {
36 if ($ARG =~ /^\w/) {
37 chomp $ARG;
38 $ARG =~ s!\\!!;
39 $ARG =~ s!|.*!!;
40 push @prs, $ARG;
41 }
42 }
43 } elsif ( -f '/etc/printers.conf' ) {
44 # DWP - solaris
45 open (PRINTCNF, '</etc/printers.conf') or
46 Carp::croak "Can't read /etc/printers.conf: $!";
47 while (<PRINTCNF>) {
48 if ($ARG =~ /\|/ or $ARG =~ /^[^:]+:\\/) {
49 chomp $ARG;
50 $ARG =~ s/[\|:].*//;
51 push @prs, $ARG unless $ARG =~ /^_(?:all|default)/i;
52 }
53 }
54 } elsif ( -d '/etc/lp/member' ) {
55 # DWP - hpux
56 opendir (LPMEM, '/etc/lp/member') or
57 Carp::croak "Can't readdir /etc/lp/member: $!";
58 @prs = grep { /^[^\.]/ && -f "/etc/lp/member/$_" } readdir(LPMEM);
59 } elsif (-e '/etc/printcap.cups') {
60 # cups spooler
61 open (PRINTCAP, '/etc/printcap.cups')
62 or Carp::croak "Can't read /etc/printcap.cups: $!";
63 @prs = <PRINTCAP>;
64 }
65  
66 # remove : at end of each name
67 foreach my $pr (@prs) {$pr =~ s/:$//;}
68  
69 $printers{name} = [ @prs ];
70 $printers{port} = [ @prs ];
71 return %printers;
72 }
73 #############################################################################
74 sub use_default {
75 # select the default printer
76 my $self = shift;
77 if ($Env{PRINTER}) {
78 $self->{'printer'}{$OSNAME} = $Env{PRINTER};
79 } elsif ($Env{LPDEST}) {
80 $self->{'printer'}{$OSNAME} = $Env{LPDEST};
81 } elsif ($Env{NPRINTER}) {
82 $self->{'printer'}{$OSNAME} = $Env{NPRINTER};
83 } elsif ($Env{NGPRINTER}) {
84 $self->{'printer'}{$OSNAME} = $Env{NGPRINTER};
85 } elsif ( open LPDEST, 'lpstat -d |' ) {
86 # DWP - lpstat -d
87 my @lpd = grep { /system default destination/i } <LPDEST>;
88 if ( @lpd == 0 ) {
89 Carp::cluck
90 'I can\'t determine your default printer, setting it to lp';
91 $self->{'printer'}{$OSNAME} = "lp";
92 } elsif ( $lpd[-1] =~ /no system default destination/i ) {
93 Carp::cluck 'No default printer specified, setting it to lp';
94 $self->{'printer'}{$OSNAME} = "lp";
95 } elsif ( $lpd[-1] =~ /system default destination:\s*(\S+)/i ) {
96 $self->{'printer'}{$OSNAME} = $1;
97 }
98 } else {
99 Carp::cluck 'I can\'t determine your default printer, setting it to lp';
100 $self->{'printer'}{$OSNAME} = "lp";
101 }
102 print "Linuxish default = $self->{printer}{$OSNAME}\n\n";
103 # DWP - test
104 }
105 ############################################################################
106 sub print {
107 # print
108 # $prn->print($data, -orientation => 'landscape') etc
109 my ($self) = shift;
110 my $data = join("", @_);
111  
112 # use standard print command
113 unless ($self->{print_command}) {
114 my $pr_cmd = "| lpr -P $self->{'printer'}{$OSNAME}";
115 if ($self->{orientation} eq 'landscape') {
116 $pr_cmd = '| a2ps -r' . $pr_cmd;
117 }
118 open PRINTER, $pr_cmd
119 or Carp::croak
120 "Can't open printer connection to $self->{'printer'}{$OSNAME}: $!";
121 print PRINTER $data;
122 close PRINTER;
123 } else {
124 # user has specified a custom print command
125 if ($self->{print_command}->{linux}->{type} eq 'pipe') {
126 # command accepts piped data
127 open PRINTER, "| $self->{print_command}->{linux}->{command}"
128 or Carp::croak "Can't open printer connection to $self->{print_command}->{linux}->{command}";
129 print PRINTER $data;
130 close PRINTER;
131 } else {
132 # command accepts file data, not piped
133 # write $data to a temp file
134 my $spoolfile = &get_unique_spool('linux');
135 open SPOOL, ">" . $spoolfile or Carp::croak "Can't write to required temproary file $spoolfile: $!";
136 print SPOOL $data;
137  
138 # print this file
139 my $cmd = $self->{print_command}->{linux}->{command};
140 $cmd =~ s/FILE/$spoolfile/;
141 system($cmd);
142 # or Carp::croak "Can't execute print command: $cmd, $!\n";
143 # this or is being executed when it shouldn't be.
144 unlink $spoolfile;
145 }
146  
147 }
148 }
149 ############################################################################
150 sub list_jobs {
151 # list the current print queue
152 my $self = shift;
153 my @queue;
154 # use available query program, lpq preferred
155 my $lpcmd;
156 if ( exists $self->{'program'}{'lpq'} ) {
157 $lpcmd = $self->{'program'}{'lpq'}.' -P'
158 } elsif ( exists $self->{'program'}{'lpstat'} ) {
159 $lpcmd = $self->{'program'}{'lpstat'}.' -o'
160 } else {
161 Carp::croak "Can't find lpq or lpstat prog for jobs function";
162 }
163 my @lpq = `$lpcmd$self->{'printer'}{$OSNAME}`;
164 chomp @_;
165 # lprng returns
166 # Printer: lp@localhost 'HP Laserjet 4L' (dest raw1@192.168.1.1)
167 # Queue: 1 printable job
168 # Server: pid 7145 active
169 # Status: job 'cfA986localhost.localdomain' removed at 15:34:48.157
170 # Rank Owner/ID Class Job Files Size Time
171 # 1 steve@localhost+144 A 144 (STDIN) 708 09:45:35
172 my $pr = $self->{'printer'}{$OSNAME};
173  
174 if ($lpq[0] =~ /^Printer/) {
175 # first line of lpq starts with Printer
176 # lprng spooler, skip first 5 lines
177 for (my $i = 5; $i < @lpq; ++$i) {
178 push @queue, join(' ',(split(/\s+/,$lpq[$i]))[0,1,3..5]);
179 # DWP - fix to exclude class
180 }
181 } elsif ($lpq[1] =~/^Rank/) {
182 # DWP - said queue, should be lpq
183 # second line of BSD & solaris lpq starts with Rank
184 # DWP - compressed doc, inc solaris
185 # Rank Owner Job Files Total Size
186 # active mwf 31 thesis.txt 682048 bytes
187 for (my $i = 2; $i < @lpq; ++$i) {
188 push @queue, $lpq[$i];
189 }
190 } elsif ($lpq[0] =~ /^$pr-\d+\s+/ and $lpq[1] =~ / bytes/) {
191 # hpux lpstat -o has multi-line entries
192 #NE1-9638 da0240 priority 0 Mar 14 14:53 on NE1
193 # (standard input) 661 bytes
194 #NE1-110 ss0300 priority 0 Oct 19 12:51
195 # mediafas [ 3 copies ] 69 bytes
196 # rescan [ 3 copies ] 62 bytes
197 my @job;
198 foreach my $line ( @lpq ) {
199 if ( $line =~ /^($pr-\d+)\s+(\S+)\s+priority/ ) {
200 if ( @job ) {
201 push @queue, join(' ',@job);
202 @job<5 and Carp::cluck "Short job entry: $queue[-1] ";
203 }
204 @job = ( 'active', $2, $1 ); # rank,owner,job
205 } elsif ( $line =~ /^\s*(\S+|\(.+\))\s.*\s(\d+)\s+bytes/ ) {
206 $job[3] = $job[3] ? $job[3].",$1" : $1; # add file(s)
207 my $sz = $2;
208 $line =~ /\s(\d+)\s+copies/ and ( $sz *= $1 ); # copies?
209 $job[4] = $job[4] ? $job[4].",$sz" : $sz; # add size(s)
210 $job[3] =~ s/ /_/g; # elim spaces
211 }
212 }
213 } elsif ( ($lpq[1] !~ /\S/) and ($lpq[2] =~/^Rank/) ) {
214 # third line of dec_osf lpq starts with Rank, second is blank
215 #Rank Owner Job Files Total Size
216 #active ss0300 40 lpr.doc, Printer.pm 103014 bytes
217 #active ss0300 42 (standard input) 54585 bytes
218 for (my $i = 3; $i < @lpq; ++$i) {
219 $lpq[$i] =~ s/,\s/,/g; # multi-files
220 if ( $lpq[$i] =~ /(\(.*\))/ ) { # spaces in file
221 my ($ofil,$nfil) = ($1,$1);
222 $nfil =~ s/ /_/g;
223 ($ofil,$nfil) = (quotemeta($ofil),quotemeta($nfil));
224 $lpq[$i] = s/$ofil/$nfil/;
225 }
226 push @queue, $lpq[$i];
227 }
228 }
229  
230 # make the queue into an array of hashes
231 for (my $i = 0; $i < @queue; ++$i) {
232 $queue[$i] =~ s/\s+/ /g; # remove extraneous spaces
233 my @job = split / /, $queue[$i];
234 $queue[$i] = {Rank => $job[0],
235 Owner => $job[1],
236 Job => $job[2],
237 Files => $job[3],
238 Size => $job[4]
239 };
240 }
241  
242 }
243 #############################################################################
244 1;
245