1 | ###########################################################################
|
---|
2 | #
|
---|
3 | # printusage.pm --
|
---|
4 | # A component of the Greenstone digital library software
|
---|
5 | # from the New Zealand Digital Library Project at the
|
---|
6 | # University of Waikato, New Zealand.
|
---|
7 | #
|
---|
8 | # Copyright (C) 1999 New Zealand Digital Library Project
|
---|
9 | #
|
---|
10 | # This program is free software; you can redistribute it and/or modify
|
---|
11 | # it under the terms of the GNU General Public License as published by
|
---|
12 | # the Free Software Foundation; either version 2 of the License, or
|
---|
13 | # (at your option) any later version.
|
---|
14 | #
|
---|
15 | # This program is distributed in the hope that it will be useful,
|
---|
16 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
17 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
18 | # GNU General Public License for more details.
|
---|
19 | #
|
---|
20 | # You should have received a copy of the GNU General Public License
|
---|
21 | # along with this program; if not, write to the Free Software
|
---|
22 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
23 | #
|
---|
24 | ###########################################################################
|
---|
25 |
|
---|
26 |
|
---|
27 | package PrintUsage;
|
---|
28 |
|
---|
29 |
|
---|
30 | use gsprintf;
|
---|
31 | use strict;
|
---|
32 | no strict 'subs'; # allow barewords (eg STDERR) as function arguments
|
---|
33 |
|
---|
34 |
|
---|
35 | sub gsprintf
|
---|
36 | {
|
---|
37 | return &gsprintf::gsprintf(@_);
|
---|
38 | }
|
---|
39 |
|
---|
40 | # this is not called by plugins or classifiers, just by scripts
|
---|
41 | sub print_xml_usage
|
---|
42 | {
|
---|
43 | my $options = shift(@_);
|
---|
44 |
|
---|
45 | # XML output is always in UTF-8
|
---|
46 | &gsprintf::output_strings_in_UTF8;
|
---|
47 |
|
---|
48 | &print_xml_header("script");
|
---|
49 |
|
---|
50 | &gsprintf(STDERR, "<Info>\n");
|
---|
51 | &gsprintf(STDERR, " <Name>$options->{'name'}</Name>\n");
|
---|
52 | &gsprintf(STDERR, " <Desc>$options->{'desc'}</Desc>\n");
|
---|
53 | &gsprintf(STDERR, " <Arguments>\n");
|
---|
54 | if (defined($options->{'args'})) {
|
---|
55 | &print_options_xml($options->{'args'});
|
---|
56 | }
|
---|
57 | &gsprintf(STDERR, " </Arguments>\n");
|
---|
58 | &gsprintf(STDERR, "</Info>\n");
|
---|
59 | }
|
---|
60 |
|
---|
61 |
|
---|
62 | sub print_xml_header
|
---|
63 | {
|
---|
64 | my $type = shift(@_);
|
---|
65 | my $all = shift(@_);
|
---|
66 | # $all = 0 unless defined $all;
|
---|
67 | if ($type ne "classify" && $type ne "plugin" && $type ne "script" && $type ne "download") {
|
---|
68 | return;
|
---|
69 | }
|
---|
70 | &gsprintf(STDERR, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
|
---|
71 | if ($type eq "plugin") {
|
---|
72 | if ($all) {
|
---|
73 | &gsprintf(STDERR, "<!DOCTYPE PlugInfoList [\n");
|
---|
74 | &gsprintf(STDERR, " <!ELEMENT PlugInfoList (PlugInfo*)>\n");
|
---|
75 | &gsprintf(STDERR, " <!ATTLIST PlugInfoList\n");
|
---|
76 | &gsprintf(STDERR, " length CDATA #REQUIRED>\n");
|
---|
77 |
|
---|
78 | } else {
|
---|
79 | &gsprintf(STDERR, "<!DOCTYPE PlugInfo [\n");
|
---|
80 | }
|
---|
81 | &gsprintf(STDERR, " <!ELEMENT PlugInfo (Name, Desc, Abstract, Inherits, Explodes, Arguments, PlugInfo?)>\n");
|
---|
82 | &gsprintf(STDERR, " <!ELEMENT Abstract (#PCDATA)>\n");
|
---|
83 | &gsprintf(STDERR, " <!ELEMENT Inherits (#PCDATA)>\n");
|
---|
84 | &gsprintf(STDERR, " <!ELEMENT Explodes (#PCDATA)>\n");
|
---|
85 |
|
---|
86 | } elsif ($type eq "classify") {
|
---|
87 | if ($all) {
|
---|
88 | &gsprintf(STDERR, "<!DOCTYPE ClassInfoList [\n");
|
---|
89 | &gsprintf(STDERR, " <!ELEMENT ClassInfoList (ClassInfo*)>\n");
|
---|
90 | &gsprintf(STDERR, " <!ATTLIST ClassInfoList\n");
|
---|
91 | &gsprintf(STDERR, " length CDATA #REQUIRED>\n");
|
---|
92 | } else {
|
---|
93 | &gsprintf(STDERR, "<!DOCTYPE ClassInfo [\n");
|
---|
94 | }
|
---|
95 | &gsprintf(STDERR, " <!ELEMENT ClassInfo (Name, Desc, Abstract, Inherits, Arguments, ClassInfo?)>\n");
|
---|
96 | &gsprintf(STDERR, " <!ELEMENT Abstract (#PCDATA)>\n");
|
---|
97 | &gsprintf(STDERR, " <!ELEMENT Inherits (#PCDATA)>\n");
|
---|
98 | } elsif ($type eq "download") {
|
---|
99 | if ($all) {
|
---|
100 | &gsprintf(STDERR, "<!DOCTYPE DownloadInfoList [\n");
|
---|
101 | &gsprintf(STDERR, " <!ELEMENT DownloadInfoList (DownloadInfo*)>\n");
|
---|
102 | &gsprintf(STDERR, " <!ATTLIST DownloadInfoList\n");
|
---|
103 | &gsprintf(STDERR, " length CDATA #REQUIRED>\n");
|
---|
104 | } else {
|
---|
105 | &gsprintf(STDERR, "<!DOCTYPE DownloadInfo [\n");
|
---|
106 | }
|
---|
107 | &gsprintf(STDERR, " <!ELEMENT DownloadInfo (Name, Desc, Abstract, Inherits, Arguments, DownloadInfo?)>\n");
|
---|
108 | &gsprintf(STDERR, " <!ELEMENT Abstract (#PCDATA)>\n");
|
---|
109 | &gsprintf(STDERR, " <!ELEMENT Inherits (#PCDATA)>\n");
|
---|
110 |
|
---|
111 | } else {
|
---|
112 | &gsprintf(STDERR, "<!DOCTYPE Info [\n");
|
---|
113 | &gsprintf(STDERR, " <!ELEMENT Info (Name, Desc, Arguments)>\n");
|
---|
114 | }
|
---|
115 | &gsprintf(STDERR, " <!ELEMENT Arguments (Option*)>\n");
|
---|
116 | &gsprintf(STDERR, " <!ELEMENT Option (Name, DisplayName?, Desc, Type, Required?, CharactorLength?, Range?, List?, Default?, HiddenGLI?, ModeGLI?)>\n");
|
---|
117 | &gsprintf(STDERR, " <!ELEMENT Name (#PCDATA)>\n");
|
---|
118 | &gsprintf(STDERR, " <!ELEMENT DisplayName (#PCDATA)>\n");
|
---|
119 | &gsprintf(STDERR, " <!ELEMENT Desc (#PCDATA)>\n");
|
---|
120 | &gsprintf(STDERR, " <!ELEMENT Type (#PCDATA)>\n");
|
---|
121 | &gsprintf(STDERR, " <!ELEMENT Required (#PCDATA)>\n");
|
---|
122 | &gsprintf(STDERR, " <!ELEMENT CharactorLength (#PCDATA)>\n");
|
---|
123 | &gsprintf(STDERR, " <!ELEMENT Range (#PCDATA)>\n");
|
---|
124 | &gsprintf(STDERR, " <!ELEMENT Default (#PCDATA)>\n");
|
---|
125 | &gsprintf(STDERR, " <!ELEMENT List (Value*)>\n");
|
---|
126 | &gsprintf(STDERR, " <!ELEMENT Value (Name, Desc?)>\n");
|
---|
127 | &gsprintf(STDERR, " <!ELEMENT HiddenGLI (#PCDATA)>\n");
|
---|
128 | &gsprintf(STDERR, " <!ELEMENT ModeGLI (#PCDATA)>\n");
|
---|
129 | &gsprintf(STDERR, "]>\n\n");
|
---|
130 | }
|
---|
131 |
|
---|
132 |
|
---|
133 | sub print_options_xml
|
---|
134 | {
|
---|
135 | my $options = shift(@_);
|
---|
136 |
|
---|
137 | foreach my $option (@$options) {
|
---|
138 | my $optionname = $option->{'name'};
|
---|
139 | my $displayname = $option->{'disp'};
|
---|
140 |
|
---|
141 | my $optiondesc = &gsprintf::lookup_string($option->{'desc'});
|
---|
142 |
|
---|
143 | # Escape '<' and '>' characters
|
---|
144 | $optiondesc =~ s/</&lt;/g; # doubly escaped
|
---|
145 | $optiondesc =~ s/>/&gt;/g;
|
---|
146 |
|
---|
147 | # Display option name, description and type
|
---|
148 | &gsprintf(STDERR, " <Option>\n");
|
---|
149 | &gsprintf(STDERR, " <Name>$optionname</Name>\n");
|
---|
150 | if (defined($option->{'disp'})) {
|
---|
151 | my $displayname = &gsprintf::lookup_string($option->{'disp'});
|
---|
152 | # Escape '<' and '>' characters
|
---|
153 | $displayname =~ s/</&lt;/g; # doubly escaped
|
---|
154 | $displayname =~ s/>/&gt;/g;
|
---|
155 | &gsprintf(STDERR, " <DisplayName>$displayname</DisplayName>\n");
|
---|
156 | }
|
---|
157 | &gsprintf(STDERR, " <Desc>$optiondesc</Desc>\n");
|
---|
158 | &gsprintf(STDERR, " <Type>$option->{'type'}</Type>\n");
|
---|
159 |
|
---|
160 | # If the option has a required field, display this
|
---|
161 | if (defined($option->{'reqd'})) {
|
---|
162 | &gsprintf(STDERR, " <Required>$option->{'reqd'}</Required>\n");
|
---|
163 | }
|
---|
164 |
|
---|
165 | # If the option has a charactor length field, display this
|
---|
166 | if (defined($option->{'char_length'})) {
|
---|
167 | &gsprintf(STDERR, " <CharactorLength>$option->{'char_length'}</CharactorLength>\n");
|
---|
168 | }
|
---|
169 |
|
---|
170 | # If the option has a range field, display this
|
---|
171 | if (defined($option->{'range'})) {
|
---|
172 | &gsprintf(STDERR, " <Range>$option->{'range'}</Range>\n");
|
---|
173 | }
|
---|
174 |
|
---|
175 | # If the option has a list of possible values, display these
|
---|
176 | if (defined $option->{'list'}) {
|
---|
177 | &gsprintf(STDERR, " <List>\n");
|
---|
178 | my $optionvalueslist = $option->{'list'};
|
---|
179 | foreach my $optionvalue (@$optionvalueslist) {
|
---|
180 | &gsprintf(STDERR, " <Value>\n");
|
---|
181 | &gsprintf(STDERR, " <Name>$optionvalue->{'name'}</Name>\n");
|
---|
182 | if (defined $optionvalue->{'desc'}) {
|
---|
183 | my $optionvaluedesc = &gsprintf::lookup_string($optionvalue->{'desc'});
|
---|
184 |
|
---|
185 | # Escape '<' and '>' characters
|
---|
186 | $optionvaluedesc =~ s/</&lt;/g; #doubly escaped
|
---|
187 | $optionvaluedesc =~ s/>/&gt;/g;
|
---|
188 |
|
---|
189 | &gsprintf(STDERR, " <Desc>$optionvaluedesc</Desc>\n");
|
---|
190 | }
|
---|
191 | &gsprintf(STDERR, " </Value>\n");
|
---|
192 | }
|
---|
193 |
|
---|
194 | # # Special case for 'input_encoding'
|
---|
195 | # if ($optionname =~ m/^input_encoding$/i) {
|
---|
196 | # my $e = $encodings::encodings;
|
---|
197 | # foreach my $enc (sort {$e->{$a}->{'name'} cmp $e->{$b}->{'name'}} keys (%$e)) {
|
---|
198 | # &gsprintf(STDERR, " <Value>\n");
|
---|
199 | # &gsprintf(STDERR, " <Name>$enc</Name>\n");
|
---|
200 | # &gsprintf(STDERR, " <Desc>$e->{$enc}->{'name'}</Desc>\n");
|
---|
201 | # &gsprintf(STDERR, " </Value>\n");
|
---|
202 | # }
|
---|
203 | # }
|
---|
204 |
|
---|
205 | &gsprintf(STDERR, " </List>\n");
|
---|
206 | }
|
---|
207 |
|
---|
208 | # Show the default value for the option, if there is one
|
---|
209 | if (defined $option->{'deft'}) {
|
---|
210 | my $optiondeftvalue = $option->{'deft'};
|
---|
211 |
|
---|
212 | # Escape '<' and '>' characters
|
---|
213 | $optiondeftvalue =~ s/</</g;
|
---|
214 | $optiondeftvalue =~ s/>/>/g;
|
---|
215 |
|
---|
216 | &gsprintf(STDERR, " <Default>$optiondeftvalue</Default>\n");
|
---|
217 | }
|
---|
218 |
|
---|
219 | # If the option is noted as being hidden in GLI, add that to the printout
|
---|
220 | if (defined($option->{'hiddengli'})) {
|
---|
221 | &gsprintf(STDERR, " <HiddenGLI>$option->{'hiddengli'}</HiddenGLI>\n");
|
---|
222 | }
|
---|
223 | # If the argument is not hidden then print out the lowest detail mode it is visible in
|
---|
224 | if (defined($option->{'modegli'})) {
|
---|
225 | &gsprintf(STDERR, " <ModeGLI>$option->{'modegli'}</ModeGLI>\n");
|
---|
226 | }
|
---|
227 |
|
---|
228 | &gsprintf(STDERR, " </Option>\n");
|
---|
229 | }
|
---|
230 | }
|
---|
231 |
|
---|
232 |
|
---|
233 | sub print_txt_usage
|
---|
234 | {
|
---|
235 | my $options = shift(@_);
|
---|
236 | my $params = shift(@_);
|
---|
237 | my $programdesc = shift(@_);
|
---|
238 |
|
---|
239 | # this causes us to automatically send output to a pager, if one is
|
---|
240 | # set, AND our output is going to a terminal
|
---|
241 | # active state perl on windows doesn't do open(handle, "-|");
|
---|
242 | if ($ENV{'GSDLOS'} !~ /windows/ && -t STDOUT) {
|
---|
243 | my $pager = $ENV{"PAGER"};
|
---|
244 | if (! $pager) {$pager="(less || more)"}
|
---|
245 | my $pid = open(STDIN, "-|"); # this does a fork... see man perlipc(1)
|
---|
246 | if (!defined $pid) {
|
---|
247 | gsprintf(STDERR, "pluginfo.pl - can't fork: $!");
|
---|
248 | } else {
|
---|
249 | if ($pid != 0) { # parent (ie forking) process. child gets 0
|
---|
250 | exec ($pager);
|
---|
251 | }
|
---|
252 | }
|
---|
253 | open(STDERR,">&STDOUT"); # so it's easier to pipe output
|
---|
254 | }
|
---|
255 |
|
---|
256 |
|
---|
257 |
|
---|
258 | my $programname = $options->{'name'};
|
---|
259 | my $programargs = $options->{'args'};
|
---|
260 |
|
---|
261 | # Find the length of the longest option string
|
---|
262 | my $descoffset = 0;
|
---|
263 | if (defined($programargs)) {
|
---|
264 | $descoffset = &find_longest_option_string($programargs);
|
---|
265 | }
|
---|
266 |
|
---|
267 | # Produce the usage information using the data structure above
|
---|
268 | if ($programdesc) {
|
---|
269 | &gsprintf(STDERR, $programname . ": $options->{'desc'}\n\n");
|
---|
270 | }
|
---|
271 | &gsprintf(STDERR, " {common.usage}: $programname $params\n\n");
|
---|
272 |
|
---|
273 | # Display the program options, if there are some
|
---|
274 | if (defined($programargs)) {
|
---|
275 | # Calculate the column offset of the option descriptions
|
---|
276 | my $optiondescoffset = $descoffset + 2; # 2 spaces between options & descriptions
|
---|
277 |
|
---|
278 | &gsprintf(STDERR, " {common.options}:\n");
|
---|
279 |
|
---|
280 | # Display the program options
|
---|
281 | &print_options_txt($programargs, $optiondescoffset);
|
---|
282 | }
|
---|
283 | }
|
---|
284 |
|
---|
285 |
|
---|
286 | sub print_options_txt
|
---|
287 | {
|
---|
288 | my $options = shift(@_);
|
---|
289 | my $optiondescoffset = shift(@_);
|
---|
290 |
|
---|
291 | foreach my $option (@$options) {
|
---|
292 | # Display option name
|
---|
293 | my $optionname = $option->{'name'};
|
---|
294 | &gsprintf(STDERR, " -$optionname");
|
---|
295 | my $optionstringlength = length(" -$optionname");
|
---|
296 |
|
---|
297 | # Display option type, if the option is not a flag
|
---|
298 | my $optiontype = $option->{'type'};
|
---|
299 | if ($optiontype ne "flag") {
|
---|
300 | &gsprintf(STDERR, " <$optiontype>");
|
---|
301 | $optionstringlength = $optionstringlength + length(" <$optiontype>");
|
---|
302 | }
|
---|
303 |
|
---|
304 | # Display the option description
|
---|
305 | if (defined($option->{'disp'})) {
|
---|
306 | my $optiondisp = &gsprintf::lookup_string($option->{'disp'});
|
---|
307 | &display_text_in_column($optiondisp, $optiondescoffset, $optionstringlength, 80);
|
---|
308 | &gsprintf(STDERR, " " x $optionstringlength);
|
---|
309 | }
|
---|
310 | my $optiondesc = &gsprintf::lookup_string($option->{'desc'});
|
---|
311 | my $optionreqd = $option->{'reqd'};
|
---|
312 | if (defined($optionreqd) && $optionreqd eq "yes") {
|
---|
313 | $optiondesc = "(" . &gsprintf::lookup_string("{PrintUsage.required}") . ") " . $optiondesc;
|
---|
314 | }
|
---|
315 | &display_text_in_column($optiondesc, $optiondescoffset, $optionstringlength, 80);
|
---|
316 |
|
---|
317 | # Show the default value for the option, if there is one
|
---|
318 | my $optiondefault = $option->{'deft'};
|
---|
319 | if (defined($optiondefault)) {
|
---|
320 | &gsprintf(STDERR, " " x $optiondescoffset);
|
---|
321 | &gsprintf(STDERR, "{PrintUsage.default}: $optiondefault\n");
|
---|
322 | }
|
---|
323 |
|
---|
324 | # If the option has a list of possible values, display these
|
---|
325 | my $optionvalueslist = $option->{'list'};
|
---|
326 | if (defined($optionvalueslist)) {
|
---|
327 | &gsprintf(STDERR, "\n");
|
---|
328 | foreach my $optionvalue (@$optionvalueslist) {
|
---|
329 | my $optionvaluename = $optionvalue->{'name'};
|
---|
330 | &gsprintf(STDERR, " " x $optiondescoffset);
|
---|
331 | &gsprintf(STDERR, "$optionvaluename:");
|
---|
332 |
|
---|
333 | my $optionvaluedesc = &gsprintf::lookup_string($optionvalue->{'desc'});
|
---|
334 | &display_text_in_column($optionvaluedesc, $optiondescoffset + 2,
|
---|
335 | $optiondescoffset + length($optionvaluename), 80);
|
---|
336 | }
|
---|
337 | }
|
---|
338 |
|
---|
339 | # # Special case for 'input_encoding'
|
---|
340 | # if ($optionname =~ m/^input_encoding$/i) {
|
---|
341 | # my $e = $encodings::encodings;
|
---|
342 | # foreach my $enc (sort {$e->{$a}->{'name'} cmp $e->{$b}->{'name'}} keys (%$e)) {
|
---|
343 | # &gsprintf(STDERR, " " x $optiondescoffset);
|
---|
344 | # &gsprintf(STDERR, "$enc:");
|
---|
345 | #
|
---|
346 | # my $encodingdesc = $e->{$enc}->{'name'};
|
---|
347 | # &display_text_in_column($encodingdesc, $optiondescoffset + 2,
|
---|
348 | # $optiondescoffset + length("$enc:"), 80);
|
---|
349 | # }
|
---|
350 | # }
|
---|
351 |
|
---|
352 | # Add a blank line to separate options
|
---|
353 | &gsprintf(STDERR, "\n");
|
---|
354 | }
|
---|
355 | }
|
---|
356 |
|
---|
357 |
|
---|
358 | sub display_text_in_column
|
---|
359 | {
|
---|
360 | my ($text, $columnbeg, $firstlineoffset, $columnend) = @_;
|
---|
361 |
|
---|
362 | # Spaces are put *before* words, so treat the column beginning as 1 smaller than it is
|
---|
363 | $columnbeg = $columnbeg - 1;
|
---|
364 |
|
---|
365 | # Add some padding (if needed) for the first line
|
---|
366 | my $linelength = $columnbeg;
|
---|
367 | if ($firstlineoffset < $columnbeg) {
|
---|
368 | &gsprintf(STDERR, " " x ($columnbeg - $firstlineoffset));
|
---|
369 | }
|
---|
370 | else {
|
---|
371 | $linelength = $firstlineoffset;
|
---|
372 | }
|
---|
373 |
|
---|
374 | # Break the text into words, and display one at a time
|
---|
375 | my @words = split(/ /, $text);
|
---|
376 |
|
---|
377 | foreach my $word (@words) {
|
---|
378 | # If printing this word would exceed the column end, start a new line
|
---|
379 | if (($linelength + length($word)) >= $columnend) {
|
---|
380 | &gsprintf(STDERR, "\n");
|
---|
381 | &gsprintf(STDERR, " " x $columnbeg);
|
---|
382 | $linelength = $columnbeg;
|
---|
383 | }
|
---|
384 |
|
---|
385 | # Write the word
|
---|
386 | &gsprintf(STDERR, " $word");
|
---|
387 | $linelength = $linelength + length(" $word");
|
---|
388 | }
|
---|
389 |
|
---|
390 | &gsprintf(STDERR, "\n");
|
---|
391 | }
|
---|
392 |
|
---|
393 |
|
---|
394 | sub find_longest_option_string
|
---|
395 | {
|
---|
396 | my $options = shift(@_);
|
---|
397 |
|
---|
398 | my $maxlength = 0;
|
---|
399 | foreach my $option (@$options) {
|
---|
400 | my $optionname = $option->{'name'};
|
---|
401 | my $optiontype = $option->{'type'};
|
---|
402 |
|
---|
403 | my $optionlength = length(" -$optionname");
|
---|
404 | if ($optiontype ne "flag") {
|
---|
405 | $optionlength = $optionlength + length(" <$optiontype>");
|
---|
406 | }
|
---|
407 |
|
---|
408 | # Remember the longest
|
---|
409 | if ($optionlength > $maxlength) {
|
---|
410 | $maxlength = $optionlength;
|
---|
411 | }
|
---|
412 | }
|
---|
413 | return $maxlength;
|
---|
414 | }
|
---|
415 |
|
---|
416 |
|
---|
417 | 1;
|
---|