source: main/trunk/greenstone2/bin/script/pluginfo.pl@ 25499

Last change on this file since 25499 was 25499, checked in by ak19, 12 years ago

Dr Bainbridge modified gsprintf code to print text containing ampersand, less than and greater then with their entity values instead so that printing to STDERR from BEGIN statements (so far used only in PDFBoxConverter of the PDFBox extension) will play nicely with the XML generated for Pluginfo.pl. Pluginfo.pl has also been modified to use the correct gsprintf printing methods.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 9.2 KB
Line 
1#!/usr/bin/perl -w
2
3###########################################################################
4#
5# pluginfo.pl --
6# A component of the Greenstone digital library software
7# from the New Zealand Digital Library Project at the
8# University of Waikato, New Zealand.
9#
10# Copyright (C) 1999 New Zealand Digital Library Project
11#
12# This program is free software; you can redistribute it and/or modify
13# it under the terms of the GNU General Public License as published by
14# the Free Software Foundation; either version 2 of the License, or
15# (at your option) any later version.
16#
17# This program is distributed in the hope that it will be useful,
18# but WITHOUT ANY WARRANTY; without even the implied warranty of
19# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20# GNU General Public License for more details.
21#
22# You should have received a copy of the GNU General Public License
23# along with this program; if not, write to the Free Software
24# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25#
26###########################################################################
27
28# This program will print info about a plugin
29
30use strict;
31no strict 'subs'; # allow barewords (eg STDERR) as function arguments
32
33BEGIN {
34 die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
35 die "GSDLOS not set\n" unless defined $ENV{'GSDLOS'};
36 unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
37 unshift (@INC, "$ENV{'GSDLHOME'}/perllib/cpan");
38# unshift (@INC, "$ENV{'GSDLHOME'}/perllib/cpan/perl-5.8");
39 unshift (@INC, "$ENV{'GSDLHOME'}/perllib/plugins");
40
41 if (defined $ENV{'GSDLEXTS'}) {
42 my @extensions = split(/:/,$ENV{'GSDLEXTS'});
43 foreach my $e (@extensions) {
44 my $ext_prefix = "$ENV{'GSDLHOME'}/ext/$e";
45
46 unshift (@INC, "$ext_prefix/perllib");
47 unshift (@INC, "$ext_prefix/perllib/cpan");
48 unshift (@INC, "$ext_prefix/perllib/plugins");
49 }
50 }
51 if (defined $ENV{'GSDL3EXTS'}) {
52 my @extensions = split(/:/,$ENV{'GSDL3EXTS'});
53 foreach my $e (@extensions) {
54 my $ext_prefix = "$ENV{'GSDL3SRCHOME'}/ext/$e";
55
56 unshift (@INC, "$ext_prefix/perllib");
57 unshift (@INC, "$ext_prefix/perllib/cpan");
58 unshift (@INC, "$ext_prefix/perllib/plugins");
59 }
60 }
61
62}
63
64use plugin;
65use util;
66use gsprintf;
67use printusage;
68use parse2;
69
70my $arguments =
71 [ { 'name' => "site",
72 'desc' => "{pluginfo.site}",
73 'type' => "string",
74 'reqd' => "no" },
75 { 'name' => "collection",
76 'desc' => "{pluginfo.collection}",
77 'type' => "string",
78 'reqd' => "no" },
79 { 'name' => "xml",
80 'desc' => "{scripts.xml}",
81 'type' => "flag",
82 'reqd' => "no" },
83 { 'name' => "listall",
84 'desc' => "{scripts.listall}",
85 'type' => "flag",
86 'reqd' => "no" },
87 { 'name' => "describeall",
88 'desc' => "{scripts.describeall}",
89 'type' => "flag",
90 'reqd' => "no" },
91 { 'name' => "language",
92 'desc' => "{scripts.language}",
93 'type' => "string",
94 'reqd' => "no" } ];
95
96my $options = { 'name' => "pluginfo.pl",
97 'desc' => "{pluginfo.desc}",
98 'args' => $arguments };
99
100sub gsprintf
101{
102 return &gsprintf::gsprintf(@_);
103}
104
105
106sub main {
107 my $site = "";
108 my $collection = "";
109 my $xml = 0;
110 my $listall = 0;
111 my $describeall = 0;
112 my ($language, $encoding);
113
114 my $hashParsingResult = {};
115 # general options available to all plugins
116 my $unparsed_args = parse2::parse(\@ARGV,$arguments,$hashParsingResult,"allow_extra_options");
117 # parse returns -1 if an error occurred
118 if ($unparsed_args == -1) {
119
120 PrintUsage::print_txt_usage($options, "{pluginfo.params}");
121 die "\n";
122 }
123
124 foreach my $strVariable (keys %$hashParsingResult)
125 {
126 my $value = $hashParsingResult->{$strVariable};
127 # test to make sure the variable name is 'safe'
128 if ($strVariable !~ /^\w+$/) {
129 die "variable name '$strVariable' isn't safe!";
130 }
131 eval "\$$strVariable = \$value";
132 }
133
134 if ($xml) {
135 &gsprintf::set_print_freetext_for_xml();
136 }
137
138 # if language wasn't specified, see if it is set in the environment
139 # (LC_ALL or LANG)
140 if (!$language && ($_=$ENV{'LC_ALL'} or $_=$ENV{'LANG'})) {
141 m/^([^\.]+)\.?(.*)/;
142 $language=$1;
143 $encoding=$2; # might be undef...
144# gsprintf::load_language* thinks "fr" is completely different to "fr_FR"...
145 $language =~ s/_.*$//;
146 }
147
148 # If $language has been set, load the appropriate resource bundle
149 # (Otherwise, the default resource bundle will be loaded automatically)
150 if ($language) {
151 gsprintf::load_language_specific_resource_bundle($language);
152 if ($encoding) {
153 $encoding =~ tr/-/_/;
154 $encoding = lc($encoding);
155 $encoding =~ s/utf_8/utf8/; # special
156 $gsprintf::specialoutputencoding=$encoding;
157 }
158 }
159
160 # If there is not exactly 1 argument left (plugin name), then the arguments were wrong
161 # If the user specified -h, then we output the usage also
162 if((@ARGV && $ARGV[0] =~ /^\-+h/) )
163 {
164 PrintUsage::print_txt_usage($options, "{pluginfo.params}");
165 die "\n";
166 }
167
168 # If there is not exactly 1 argument left (plugin name), then the arguments were wrong (apart from if we had listall or describeall set)
169 if ($listall == 0 && $describeall ==0 && $unparsed_args == 0) {
170 gsprintf(STDERR, "{pluginfo.no_plugin_name}\n\n");
171 PrintUsage::print_txt_usage($options, "{pluginfo.params}", 1);
172 die "\n";
173 }
174
175 # we had some arguments that we weren't expecting
176 if ($unparsed_args > 1) {
177 pop(@ARGV); # assume that the last arg is the plugin name
178 gsprintf(STDERR, "{common.invalid_options}\n\n", join (',', @ARGV));
179 PrintUsage::print_txt_usage($options, "{pluginfo.params}", 1);
180 die "\n";
181 }
182
183 my $plugin = shift (@ARGV);
184 if (defined $plugin) {
185 $plugin =~ s/\.pm$//; # allow xxxPlugin.pm as the argument
186 }
187
188 if ($site ne "") {
189 # assume Greenstone 3
190 if ($collection ne "") {
191 $ENV{'GSDLCOLLECTDIR'} = &util::filename_cat ($ENV{'GSDL3HOME'}, "sites", $site, "collect", $collection);
192 } else {
193 # Probably more useful to default to GS2 area for plugins
194 $ENV{'GSDLCOLLECTDIR'} = $ENV{'GSDLHOME'};
195 }
196 }
197 else {
198 if ($collection ne "") {
199 $ENV{'GSDLCOLLECTDIR'} = &util::filename_cat ($ENV{'GSDLHOME'}, "collect", $collection);
200 } else {
201 $ENV{'GSDLCOLLECTDIR'} = $ENV{'GSDLHOME'};
202 }
203 }
204
205 if ($listall || $describeall) {
206 my $plugins_dir = &util::filename_cat($ENV{'GSDLCOLLECTDIR'}, "perllib", "plugins");
207 my @plugin_list = ();
208 if (opendir (INDIR, $plugins_dir)) {
209 @plugin_list = grep (/Plugin\.pm$/, readdir (INDIR));
210 closedir (INDIR);
211 }
212
213 if ((defined $ENV{'GSDLEXTS'}) && ($collection eq "")) {
214 my @extensions = split(/:/,$ENV{'GSDLEXTS'});
215 foreach my $e (@extensions) {
216 my $ext_prefix = &util::filename_cat($ENV{'GSDLHOME'},"ext",$e);
217 my $ext_plugins_dir = &util::filename_cat($ext_prefix, "perllib", "plugins");
218
219 if (opendir (INDIR, $ext_plugins_dir)) {
220 my @ext_plugin_list = grep (/Plugin\.pm$/, readdir (INDIR));
221 closedir (INDIR);
222
223 push(@plugin_list,@ext_plugin_list);
224 }
225
226 }
227 }
228 if ((defined $ENV{'GSDL3EXTS'}) && ($collection eq "")) {
229 my @extensions = split(/:/,$ENV{'GSDL3EXTS'});
230 foreach my $e (@extensions) {
231 my $ext_prefix = &util::filename_cat($ENV{'GSDL3SRCHOME'},"ext",$e);
232 my $ext_plugins_dir = &util::filename_cat($ext_prefix, "perllib", "plugins");
233
234 if (opendir (INDIR, $ext_plugins_dir)) {
235 my @ext_plugin_list = grep (/Plugin\.pm$/, readdir (INDIR));
236 closedir (INDIR);
237
238 push(@plugin_list,@ext_plugin_list);
239 }
240
241 }
242 }
243
244 # load up the plugins before writing out the xml so that any error
245 # messages are not inside the XML output (can cause parsing to fail)
246 my @plugobj_list;
247 foreach my $plugin (@plugin_list) {
248 $plugin =~ s/\.pm$//;
249 my $plugobj = &plugin::load_plugin_for_info ($plugin);
250 push (@plugobj_list, $plugobj);
251 }
252
253 &gsprintf::set_print_xml_tags();
254 print STDERR "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
255 print STDERR "<PluginList length=\"" . scalar(@plugin_list) . "\">\n";
256 foreach my $plugobj (@plugobj_list) {
257 if ($describeall) {
258 $plugobj->print_xml_usage(0);
259 }
260 else {
261 $plugobj->print_xml_usage(0, 1);
262 }
263 }
264 print STDERR "</PluginList>\n";
265
266
267 }
268 else {
269 &print_single_plugin($plugin, $xml, 1);
270 }
271}
272
273
274sub print_single_plugin {
275 my ($plugin, $xml, $header) = @_;
276 my $plugobj = &plugin::load_plugin_for_info ($plugin);
277 if ($xml) {
278 &gsprintf::set_print_xml_tags();
279 $plugobj->print_xml_usage($header);
280 }
281 else {
282
283 # this causes us to automatically send output to a pager, if one is
284 # set, AND our output is going to a terminal
285 # active state perl on windows doesn't do open(handle, "-|");
286 if ($ENV{'GSDLOS'} !~ /windows/ && -t STDOUT) {
287 my $pager = $ENV{"PAGER"};
288 if (! $pager) {$pager="(less || more)"}
289 my $pid = open(STDIN, "-|"); # this does a fork... see man perlipc(1)
290 if (!defined $pid) {
291 gsprintf(STDERR, "pluginfo.pl - can't fork: $!");
292 } else {
293 if ($pid != 0) { # parent (ie forking) process. child gets 0
294 exec ($pager);
295 }
296 }
297 open(STDERR,">&STDOUT"); # so it's easier to pipe output
298 }
299
300 gsprintf(STDERR, "\n{pluginfo.passing_options}\n\n");
301 gsprintf(STDERR, "{pluginfo.option_types}:\n\n");
302 gsprintf(STDERR, "{pluginfo.specific_options}\n\n");
303 gsprintf(STDERR, "{pluginfo.general_options}\n\n");
304 gsprintf(STDERR, "$plugin {pluginfo.info}:\n\n");
305
306 $plugobj->print_txt_usage();
307 }
308
309}
310
311&main ();
Note: See TracBrowser for help on using the repository browser.