7 td-lpstat - lpstat(1) wrapper to output printers status in Tabular Data format
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');
23 open $pipe, '-|', 'lpstat', '-d', '-p', '-a';
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'),
42 if($stage =~ /^now printing .*-(\d+)$/)
45 $Printer{$last_printer}->{'JOB'} = $1;
47 $Printer{$last_printer}->{'STAGE'} = $stage;
49 elsif(/^\s+(.+?)$/ and defined $last_printer)
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');
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;