bugfix
[hband-tools.git] / tabdata / td-lpstat
blob3a24fd7f5b0f11d2dfedf86837ddc8fb1b6a85bb
1 #!/usr/bin/env perl
3 =pod
5 =head1 NAME
7 td-lpstat - lpstat(1) wrapper to output printers status in Tabular Data format
9 =cut
12 %OptionDefs = (
15 use Data::Dumper;
16 use DateTime::Format::Strptime;
17 no if ($] >= 5.018), 'warnings' => 'experimental::smartmatch';
18 do '/usr/lib/tool/perl5/tabdata/common.pl' or die "$@";
20 $datetime_parser = new DateTime::Format::Strptime(pattern=>'%b %d %T %Y', on_error=>'croak', locale=>'C');
22 $ENV{'LANG'} = 'C';
23 open $pipe, '-|', 'lpstat', '-d', '-p', '-a';
24 while(<$pipe>)
26 chomp;
27 if(/^system default destination: (.+)$/)
29 $default_printer = $1;
31 elsif(/^printer (?'PRINTER'\S+) ((?'STAGE'.*?)\. (?'STATUS'enabled)|(?'STATUS'.+)) since \S+ (?'datetime'\S+ \d+ \S+ \d+)( -)?$/)
33 $last_printer = $+{'PRINTER'};
34 my $stage = $+{'STAGE'};
35 $Printer{$last_printer} = {
36 'PRINTER' => $last_printer,
37 'STATUS' => $+{'STATUS'},
38 'SINCE' => $datetime_parser->parse_datetime($+{'datetime'})->strftime('%F %T'),
41 $stage =~ s/^is //;
42 if($stage =~ /^now printing .*-(\d+)$/)
44 $stage = 'printing';
45 $Printer{$last_printer}->{'JOB'} = $1;
47 $Printer{$last_printer}->{'STAGE'} = $stage;
49 elsif(/^\s+(.+?)$/ and defined $last_printer)
51 my $reason = $1;
52 $Printer{$last_printer}->{'REASON'} .= "\n" if exists $Printer{$last_printer}->{'REASON'};
53 $Printer{$last_printer}->{'REASON'} .= $1;
55 elsif(/^(?'PRINTER'\S+) (?'ACCEPTANCE'not accepting|accepting) requests since \S+ (?'datetime'\S+ \d+ \S+ \d+)( -)?/)
57 $Printer{$+{'PRINTER'}}->{'ACCEPTANCE'} = $+{'ACCEPTANCE'};
58 $Printer{$+{'PRINTER'}}->{'ACCEPTANCE_SINCE'} = $datetime_parser->parse_datetime($+{'datetime'})->strftime('%F %T');
60 else
62 warn "$0: can not parse: $_\n";
66 #warn Dumper \%Printer;
68 @Headers = qw/PRINTER DEFAULT STATUS SINCE STAGE JOB ACCEPTANCE ACCEPTANCE_SINCE REASON/;
69 print join($FS, @Headers).$RS;
71 for my $printer (sort {$a cmp $b} keys %Printer)
73 $Printer{$printer}->{'DEFAULT'} = $printer eq $default_printer ? "yes" : "no";
74 print join($FS, map {escape_tabdata($Printer{$printer}->{$_})} @Headers).$RS;