source: trunk/gsdl/bin/script/pluginfo.pl@ 10825

Last change on this file since 10825 was 10825, checked in by jrm21, 18 years ago

1) print usage if given -h or --help
2) set language before printing usage for bad options
3) be more careful when using 'eval' statement so we don't execute

any old code.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 6.3 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/plugins");
39}
40
41use plugin;
42use util;
43use gsprintf;
44use printusage;
45
46use parse2;
47
48my $arguments =
49 [ { 'name' => "collect",
50 'desc' => "{pluginfo.collect}",
51 'type' => "string",
52 'reqd' => "no" },
53 { 'name' => "xml",
54 'desc' => "{scripts.xml}",
55 'type' => "flag",
56 'reqd' => "no" },
57 { 'name' => "listall",
58 'desc' => "{scripts.listall}",
59 'type' => "flag",
60 'reqd' => "no" },
61 { 'name' => "language",
62 'desc' => "{scripts.language}",
63 'type' => "string",
64 'reqd' => "no" } ];
65
66my $options = { 'name' => "pluginfo.pl",
67 'desc' => "{pluginfo.desc}",
68 'args' => $arguments };
69
70sub gsprintf
71{
72 return &gsprintf::gsprintf(@_);
73}
74
75
76sub main {
77 my $collect = "";
78 my $xml = 0;
79 my $listall = 0;
80
81 my ($language, $encoding);
82
83 my $hashParsingResult = {};
84 my $blnParseFailed = "false";
85 # general options available to all plugins
86 my $unparsed_args = parse2::parse(\@ARGV,$arguments,$hashParsingResult,"allow_extra_options");
87
88 foreach my $strVariable (keys %$hashParsingResult)
89 {
90 my $value = $hashParsingResult->{$strVariable};
91 # test to make sure the variable name is 'safe'
92 if ($strVariable !~ /^\w+$/) {
93 die "variable name '$strVariable' isn't safe!";
94 }
95 eval "\$$strVariable = \$value";
96 }
97
98 # if language wasn't specified, see if it is set in the environment
99 # (LC_ALL or LANG)
100 if (!$language && ($_=$ENV{'LC_ALL'} or $_=$ENV{'LANG'})) {
101 m/^([^\.]+)\.?(.*)/;
102 $language=$1;
103 $encoding=$2; # might be undef...
104# gsprintf::load_language* thinks "fr" is completely different to "fr_FR"...
105 $language =~ s/_.*$//;
106 }
107
108 # If $language has been set, load the appropriate resource bundle
109 # (Otherwise, the default resource bundle will be loaded automatically)
110 if ($language) {
111 gsprintf::load_language_specific_resource_bundle($language);
112 if ($encoding) {
113 $encoding =~ tr/-/_/;
114 $encoding = lc($encoding);
115 $encoding =~ s/utf_8/utf8/; # special
116 $gsprintf::specialoutputencoding=$encoding;
117 }
118 }
119
120 # If there are more than one argument left after parsing, it mean user input too many arguments.
121 # Error occoured will return 0
122 if( $unparsed_args > 1 or (@ARGV && $ARGV[0] =~ /^\-+h/) )
123 {
124 PrintUsage::print_txt_usage($options, "{pluginfo.params}");
125 die "\n";
126 }
127
128 my $plugin = shift (@ARGV);
129 if (defined $plugin) {
130 $plugin =~ s/\.pm$//; # allow xxxPlug.pm as the argument
131 }
132 if (($listall == 0) && (!defined $plugin || $plugin eq "")) {
133 gsprintf(STDERR, "{pluginfo.no_plugin_name}\n\n");
134 PrintUsage::print_txt_usage($options, "{pluginfo.params}", 1);
135 die "\n";
136 }
137
138 if ($collect ne "") {
139 $ENV{'GSDLCOLLECTDIR'} = &util::filename_cat ($ENV{'GSDLHOME'}, "collect", $collect);
140 } else {
141 $ENV{'GSDLCOLLECTDIR'} = $ENV{'GSDLHOME'};
142 }
143
144 if ($listall) {
145 my $plugin_dir = &util::filename_cat($ENV{'GSDLCOLLECTDIR'},"perllib","plugins");
146
147 if (!opendir (INDIR, $plugin_dir)) {
148 print STDERR "pluginfo.pl: could not open directory $plugin_dir\n";
149 } else {
150 my @plugin_list = grep (/Plug\.pm$/, readdir (INDIR));
151 closedir (INDIR);
152
153
154 if ($xml) {
155 my $num_plugins = scalar(@plugin_list);
156
157 print STDERR "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
158 print STDERR "<!DOCTYPE PluginList [\n";
159 print STDERR " <!ELEMENT PluginList (PluginName*)>\n";
160 print STDERR " <!ELEMENT PlugName (#PCDATA)>\n";
161 print STDERR " <!ATTLIST PluginList\n";
162 print STDERR " length CDATA #REQUIRED>\n";
163 print STDERR "]>\n";
164
165 print STDERR "<PluginList length=\"$num_plugins\">\n";
166 map { print STDERR " <PluginName>$_</PluginName>\n"; } @plugin_list;
167 print STDERR "</PluginList>\n";
168
169 }
170 else {
171 print STDERR join(" ",@plugin_list), "\n";
172 }
173 }
174
175 }
176 else {
177
178 #my $pluginfo = &plugin::load_plugins ([[$plugin]]);
179 #my $plugobj = shift @$pluginfo;
180 my $plugobj = &plugin::load_plugin_for_info ($plugin);
181 if ($xml) {
182 $plugobj->print_xml_usage();
183 }
184 else {
185 gsprintf(STDERR, "\n{pluginfo.passing_options}\n\n");
186 gsprintf(STDERR, "{pluginfo.option_types}:\n\n");
187 gsprintf(STDERR, "{pluginfo.specific_options}\n\n");
188 gsprintf(STDERR, "{pluginfo.general_options}\n\n");
189 gsprintf(STDERR, "$plugin {pluginfo.info}:\n\n");
190
191 $plugobj->print_txt_usage();
192 }
193 }
194}
195
196
197# this causes us to automatically send output to a pager, if one is
198# set, AND our output is going to a terminal
199# active state perl on windows doesn't do open(handle, "-|");
200if ($ENV{'GSDLOS'} !~ /windows/ && -t STDOUT) {
201 my $pager = $ENV{"PAGER"};
202 if (! $pager) {$pager="(less || more)"}
203 my $pid = open(STDIN, "-|"); # this does a fork... see man perlipc(1)
204 if (!defined $pid) {
205 gsprintf(STDERR, "pluginfo.pl - can't fork: $!");
206 } else {
207 if ($pid != 0) { # parent (ie forking) process. child gets 0
208 exec ($pager);
209 }
210 }
211 open(STDERR,">&STDOUT"); # so it's easier to pipe output
212}
213
214
215&main ();
Note: See TracBrowser for help on using the repository browser.