|
0 |
#!/usr/bin/perl
|
|
1 |
# $Id$
|
|
2 |
#
|
|
3 |
# File: ppd-doc-extractor
|
|
4 |
# Date: 27 Jan 2022 16:28:17
|
|
5 |
# Author: Alexander Zangerl <az@snafu.priv.at>
|
|
6 |
#
|
|
7 |
# Abstract:
|
|
8 |
# very minimal recreation of the PPD documentation extractor
|
|
9 |
# of foomatic-rip 3.x (which you got with foomatic-rip -o docs), which
|
|
10 |
# is not part of the rewritten 4.x versions despite the documentation's
|
|
11 |
# claims and the broken option code that doesn't reject what it cannot
|
|
12 |
# handle.
|
|
13 |
#
|
|
14 |
# copyright (c) 2022 Alexander Zangerl <az@snafu.priv.at>
|
|
15 |
#
|
|
16 |
# This program is free software; you can redistribute it and/or modify
|
|
17 |
# it under the terms of the GNU General Public License version 2
|
|
18 |
# as published by the Free Software Foundation.
|
|
19 |
#
|
|
20 |
# This program is distributed in the hope that it will be useful,
|
|
21 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
22 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
23 |
# GNU General Public License for more details.
|
|
24 |
#
|
|
25 |
# You should have received a copy of the GNU General Public License
|
|
26 |
# along with this program; if not, write to the Free Software
|
|
27 |
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
28 |
use strict;
|
|
29 |
use File::Slurp;
|
|
30 |
use Data::Dumper;
|
|
31 |
|
|
32 |
my ($ppdfile,@a2ps) = @ARGV;
|
|
33 |
die "usage: $0 <ppdfile> [a2ps args]\n
|
|
34 |
no a2ps args: print to stdout
|
|
35 |
automatically added: --center-title, --footer\n\n" if (!-f $ppdfile);
|
|
36 |
|
|
37 |
my @stuff = read_file($ppdfile);
|
|
38 |
|
|
39 |
# parse everything but massage and handle just the relevant parts
|
|
40 |
my (%x, $curopengroup);
|
|
41 |
for (my $i = 0; $i <= $#stuff; ++$i)
|
|
42 |
{
|
|
43 |
my $line = $stuff[$i];
|
|
44 |
$line =~ s/\r\n$//; # de-dos
|
|
45 |
chomp $line;
|
|
46 |
|
|
47 |
my ($key, $value, $option, $xlatoption);
|
|
48 |
|
|
49 |
next if ($line =~ /^(\*%.*)?$/ or $line eq "*End");
|
|
50 |
if ($line =~ /^\*([^:]+):\s*(.+)\s*$/)
|
|
51 |
{
|
|
52 |
($key, $value) = ($1,$2);
|
|
53 |
if ($key =~ /^(\S+)\s+(.+)$/)
|
|
54 |
{
|
|
55 |
$key = $1;
|
|
56 |
$option = $2;
|
|
57 |
|
|
58 |
if ($option =~ m!^([^/]+)/(.+)$!)
|
|
59 |
{
|
|
60 |
($option,$xlatoption) = ($1,$2);
|
|
61 |
undef $xlatoption if ($xlatoption eq $option); # unnecessary ones
|
|
62 |
}
|
|
63 |
}
|
|
64 |
# invocationvalue and quotedvalue allow continuation
|
|
65 |
if ($value =~ /^"([^"]+)"\s*$/)
|
|
66 |
{
|
|
67 |
$value = $1;
|
|
68 |
}
|
|
69 |
elsif ($value =~ /^"([^"]*)*$/)
|
|
70 |
{
|
|
71 |
$value = $1;
|
|
72 |
while ((my $nextline = $stuff[++$i]) !~ /"/)
|
|
73 |
{
|
|
74 |
$value .= $nextline;
|
|
75 |
}
|
|
76 |
if ($stuff[$i] =~ /^([^"]+)"\s*$/)
|
|
77 |
{
|
|
78 |
$value .= $1;
|
|
79 |
}
|
|
80 |
}
|
|
81 |
|
|
82 |
# orderdependency is laid out extraspecially stupidly
|
|
83 |
if ($key =~ /^(NonUI)?OrderDependency$/)
|
|
84 |
{
|
|
85 |
$key = "OrderDependency"; # we lump these together
|
|
86 |
my ($num,$dontcare,$appliesto) = split(/\s+/,$value);
|
|
87 |
($option = $appliesto) =~ s/^\*//;
|
|
88 |
$value = $num;
|
|
89 |
}
|
|
90 |
# want the options under openui w/o fluff
|
|
91 |
elsif ($key eq "OpenUI")
|
|
92 |
{
|
|
93 |
$option =~ s/^\*//;
|
|
94 |
}
|
|
95 |
# another instance of shitty structural layout
|
|
96 |
elsif ($key eq "OpenGroup")
|
|
97 |
{
|
|
98 |
if ($value =~ m!^\s*(\S+)/(.+)$!)
|
|
99 |
{
|
|
100 |
($option,$xlatoption) = ($1,$2);
|
|
101 |
}
|
|
102 |
else
|
|
103 |
{
|
|
104 |
$xlatoption = $option = $value;
|
|
105 |
}
|
|
106 |
$curopengroup = $value = $option;
|
|
107 |
}
|
|
108 |
elsif ($key eq "CloseGroup")
|
|
109 |
{
|
|
110 |
undef $curopengroup;
|
|
111 |
}
|
|
112 |
|
|
113 |
if (defined $option)
|
|
114 |
{
|
|
115 |
# for option entries add a sequence number for sorting - simply use the line number
|
|
116 |
$x{$key}->{$option} = { xlat => $xlatoption,
|
|
117 |
value => $value,
|
|
118 |
sequence => $i,
|
|
119 |
ingroup => $curopengroup, };
|
|
120 |
|
|
121 |
}
|
|
122 |
else
|
|
123 |
{
|
|
124 |
$x{$key} = $value;
|
|
125 |
}
|
|
126 |
}
|
|
127 |
else
|
|
128 |
{
|
|
129 |
die "unrecognized line nr. $i\n";
|
|
130 |
}
|
|
131 |
}
|
|
132 |
|
|
133 |
# print Dumper(\%x);
|
|
134 |
|
|
135 |
if (@a2ps)
|
|
136 |
{
|
|
137 |
push @a2ps, ("--center-title=Documentation for $x{ModelName}","--footer=");
|
|
138 |
|
|
139 |
# lazy me: just duping the pipe fd...
|
|
140 |
open(P, "|-", "a2ps", @a2ps) or die "cannot pipe to a2ps: $!\n";
|
|
141 |
open(STDOUT, ">&", \*P) or die "cannot dup: $!\n";
|
|
142 |
}
|
|
143 |
|
|
144 |
print qq|
|
|
145 |
Invocation summary for $x{ModelName}:
|
|
146 |
Command line: lpr [-Z opt=value, opt=value...] [lpr options] <file>
|
|
147 |
|
|
148 |
List of available options:\n\n|;
|
|
149 |
|
|
150 |
# meh...orderdependency is not in all ppds or all sections :-(
|
|
151 |
my $odep = $x{OrderDependency};
|
|
152 |
my @onames = sort { $odep->{$a}->{ingroup} cmp $odep->{$a}->{ingroup}
|
|
153 |
|| $odep->{$a}->{sequence} <=> $odep->{$b}->{sequence}
|
|
154 |
|| $odep->{$a}->{value} <=> $odep->{$b}->{value}
|
|
155 |
|| $x{OpenUI}->{$a}->{sequence} <=> $x{OpenUI}->{$b}->{sequence} } keys %{$x{OpenUI}};
|
|
156 |
|
|
157 |
for my $oname (@onames)
|
|
158 |
{
|
|
159 |
my $label = $x{OpenUI}->{$oname}->{xlat} // $oname;
|
|
160 |
my $type = $x{OpenUI}->{$oname}->{value} // "Unknown";
|
|
161 |
|
|
162 |
# one choice is no choice
|
|
163 |
next if ($type eq "PickOne" && keys %{$x{$oname}} == 1);
|
|
164 |
|
|
165 |
my $this = $x{$oname};
|
|
166 |
my $example = (keys %$this)[0];
|
|
167 |
my $sectionname = $x{OpenGroup}->{$this->{$example}->{ingroup}}->{xlat};
|
|
168 |
my $sectionlabel = $sectionname ? " Section: $sectionname\n":"";
|
|
169 |
|
|
170 |
print "Option '$oname': $label\n$sectionlabel Type: $type\n Choices:\n";
|
|
171 |
for my $choice (sort { $this->{$a}->{sequence} <=> $this->{$b}->{sequence} } keys %$this)
|
|
172 |
{
|
|
173 |
my $choicelabel = $x{$oname}->{$choice}->{xlat};
|
|
174 |
print " o '$choice'".($choicelabel? ": $choicelabel":"")."\n";
|
|
175 |
}
|
|
176 |
print " Default: ".$x{"Default$oname"}."\n Example: -Z $oname=$example\n\n";
|
|
177 |
}
|
|
178 |
|
|
179 |
if (@a2ps)
|
|
180 |
{
|
|
181 |
close(STDOUT);
|
|
182 |
close(P);
|
|
183 |
}
|