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

Last change on this file since 22642 was 22518, checked in by kjdon, 14 years ago

copied the pager code out of printusage and added it to print_single_{plugin,classifier} so that pluginfo and classinfo go to less

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 8.4 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' => "collection",
72 'desc' => "{pluginfo.collection}",
73 'type' => "string",
74 'reqd' => "no" },
75 { 'name' => "xml",
76 'desc' => "{scripts.xml}",
77 'type' => "flag",
78 'reqd' => "no" },
79 { 'name' => "listall",
80 'desc' => "{scripts.listall}",
81 'type' => "flag",
82 'reqd' => "no" },
83 { 'name' => "describeall",
84 'desc' => "{scripts.describeall}",
85 'type' => "flag",
86 'reqd' => "no" },
87 { 'name' => "language",
88 'desc' => "{scripts.language}",
89 'type' => "string",
90 'reqd' => "no" } ];
91
92my $options = { 'name' => "pluginfo.pl",
93 'desc' => "{pluginfo.desc}",
94 'args' => $arguments };
95
96sub gsprintf
97{
98 return &gsprintf::gsprintf(@_);
99}
100
101
102sub main {
103 my $collection = "";
104 my $xml = 0;
105 my $listall = 0;
106 my $describeall = 0;
107 my ($language, $encoding);
108
109 my $hashParsingResult = {};
110 # general options available to all plugins
111 my $unparsed_args = parse2::parse(\@ARGV,$arguments,$hashParsingResult,"allow_extra_options");
112 # parse returns -1 if an error occurred
113 if ($unparsed_args == -1) {
114
115 PrintUsage::print_txt_usage($options, "{pluginfo.params}");
116 die "\n";
117 }
118
119 foreach my $strVariable (keys %$hashParsingResult)
120 {
121 my $value = $hashParsingResult->{$strVariable};
122 # test to make sure the variable name is 'safe'
123 if ($strVariable !~ /^\w+$/) {
124 die "variable name '$strVariable' isn't safe!";
125 }
126 eval "\$$strVariable = \$value";
127 }
128
129 # if language wasn't specified, see if it is set in the environment
130 # (LC_ALL or LANG)
131 if (!$language && ($_=$ENV{'LC_ALL'} or $_=$ENV{'LANG'})) {
132 m/^([^\.]+)\.?(.*)/;
133 $language=$1;
134 $encoding=$2; # might be undef...
135# gsprintf::load_language* thinks "fr" is completely different to "fr_FR"...
136 $language =~ s/_.*$//;
137 }
138
139 # If $language has been set, load the appropriate resource bundle
140 # (Otherwise, the default resource bundle will be loaded automatically)
141 if ($language) {
142 gsprintf::load_language_specific_resource_bundle($language);
143 if ($encoding) {
144 $encoding =~ tr/-/_/;
145 $encoding = lc($encoding);
146 $encoding =~ s/utf_8/utf8/; # special
147 $gsprintf::specialoutputencoding=$encoding;
148 }
149 }
150
151 # If there is not exactly 1 argument left (plugin name), then the arguments were wrong
152 # If the user specified -h, then we output the usage also
153 if((@ARGV && $ARGV[0] =~ /^\-+h/) )
154 {
155 PrintUsage::print_txt_usage($options, "{pluginfo.params}");
156 die "\n";
157 }
158
159 # If there is not exactly 1 argument left (plugin name), then the arguments were wrong (apart from if we had listall or describeall set)
160 if ($listall == 0 && $describeall ==0 && $unparsed_args == 0) {
161 gsprintf(STDERR, "{pluginfo.no_plugin_name}\n\n");
162 PrintUsage::print_txt_usage($options, "{pluginfo.params}", 1);
163 die "\n";
164 }
165
166 # we had some arguments that we weren't expecting
167 if ($unparsed_args > 1) {
168 pop(@ARGV); # assume that the last arg is the plugin name
169 gsprintf(STDERR, "{common.invalid_options}\n\n", join (',', @ARGV));
170 PrintUsage::print_txt_usage($options, "{pluginfo.params}", 1);
171 die "\n";
172 }
173
174 my $plugin = shift (@ARGV);
175 if (defined $plugin) {
176 $plugin =~ s/\.pm$//; # allow xxxPlugin.pm as the argument
177 }
178
179 if ($collection ne "") {
180 $ENV{'GSDLCOLLECTDIR'} = &util::filename_cat ($ENV{'GSDLHOME'}, "collect", $collection);
181 } else {
182 $ENV{'GSDLCOLLECTDIR'} = $ENV{'GSDLHOME'};
183 }
184
185 if ($listall || $describeall) {
186 my $plugins_dir = &util::filename_cat($ENV{'GSDLCOLLECTDIR'}, "perllib", "plugins");
187 my @plugin_list = ();
188 if (opendir (INDIR, $plugins_dir)) {
189 @plugin_list = grep (/Plugin\.pm$/, readdir (INDIR));
190 closedir (INDIR);
191 }
192
193 if ((defined $ENV{'GSDLEXTS'}) && ($collection eq "")) {
194 my @extensions = split(/:/,$ENV{'GSDLEXTS'});
195 foreach my $e (@extensions) {
196 my $ext_prefix = &util::filename_cat($ENV{'GSDLHOME'},"ext",$e);
197 my $ext_plugins_dir = &util::filename_cat($ext_prefix, "perllib", "plugins");
198
199 if (opendir (INDIR, $ext_plugins_dir)) {
200 my @ext_plugin_list = grep (/Plugin\.pm$/, readdir (INDIR));
201 closedir (INDIR);
202
203 push(@plugin_list,@ext_plugin_list);
204 }
205
206 }
207 }
208 if ((defined $ENV{'GSDL3EXTS'}) && ($collection eq "")) {
209 my @extensions = split(/:/,$ENV{'GSDL3EXTS'});
210 foreach my $e (@extensions) {
211 my $ext_prefix = &util::filename_cat($ENV{'GSDL3SRCHOME'},"ext",$e);
212 my $ext_plugins_dir = &util::filename_cat($ext_prefix, "perllib", "plugins");
213
214 if (opendir (INDIR, $ext_plugins_dir)) {
215 my @ext_plugin_list = grep (/Plugin\.pm$/, readdir (INDIR));
216 closedir (INDIR);
217
218 push(@plugin_list,@ext_plugin_list);
219 }
220
221 }
222 }
223
224 print STDERR "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
225 print STDERR "<PluginList length=\"" . scalar(@plugin_list) . "\">\n";
226 foreach my $plugin (@plugin_list) {
227 $plugin =~ s/\.pm$//;
228 my $plugobj = &plugin::load_plugin_for_info ($plugin);
229 if ($describeall) {
230 $plugobj->print_xml_usage(0);
231 }
232 else {
233 $plugobj->print_xml_usage(0, 1);
234 }
235 }
236 print STDERR "</PluginList>\n";
237 }
238 else {
239 &print_single_plugin($plugin, $xml, 1);
240 }
241}
242
243
244sub print_single_plugin {
245 my ($plugin, $xml, $header) = @_;
246 my $plugobj = &plugin::load_plugin_for_info ($plugin);
247 if ($xml) {
248 $plugobj->print_xml_usage($header);
249 }
250 else {
251
252 # this causes us to automatically send output to a pager, if one is
253 # set, AND our output is going to a terminal
254 # active state perl on windows doesn't do open(handle, "-|");
255 if ($ENV{'GSDLOS'} !~ /windows/ && -t STDOUT) {
256 my $pager = $ENV{"PAGER"};
257 if (! $pager) {$pager="(less || more)"}
258 my $pid = open(STDIN, "-|"); # this does a fork... see man perlipc(1)
259 if (!defined $pid) {
260 gsprintf(STDERR, "pluginfo.pl - can't fork: $!");
261 } else {
262 if ($pid != 0) { # parent (ie forking) process. child gets 0
263 exec ($pager);
264 }
265 }
266 open(STDERR,">&STDOUT"); # so it's easier to pipe output
267 }
268
269 gsprintf(STDERR, "\n{pluginfo.passing_options}\n\n");
270 gsprintf(STDERR, "{pluginfo.option_types}:\n\n");
271 gsprintf(STDERR, "{pluginfo.specific_options}\n\n");
272 gsprintf(STDERR, "{pluginfo.general_options}\n\n");
273 gsprintf(STDERR, "$plugin {pluginfo.info}:\n\n");
274
275 $plugobj->print_txt_usage();
276 }
277
278}
279
280&main ();
Note: See TracBrowser for help on using the repository browser.