source: main/trunk/greenstone2/bin/script/classinfo.pl@ 31888

Last change on this file since 31888 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: 7.5 KB
Line 
1#!/usr/bin/perl -w
2
3###########################################################################
4#
5# classinfo.pl -- provide information about classifiers
6#
7# A component of the Greenstone digital library software
8# from the New Zealand Digital Library Project at the
9# University of Waikato, New Zealand.
10#
11# Copyright (C) 1999 New Zealand Digital Library Project
12#
13# This program is free software; you can redistribute it and/or modify
14# it under the terms of the GNU General Public License as published by
15# the Free Software Foundation; either version 2 of the License, or
16# (at your option) any later version.
17#
18# This program is distributed in the hope that it will be useful,
19# but WITHOUT ANY WARRANTY; without even the implied warranty of
20# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21# GNU General Public License for more details.
22#
23# You should have received a copy of the GNU General Public License
24# along with this program; if not, write to the Free Software
25# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26#
27###########################################################################
28
29use strict;
30no strict 'subs'; # allow barewords (eg STDERR) as function arguments
31
32BEGIN {
33 die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
34 die "GSDLOS not set\n" unless defined $ENV{'GSDLOS'};
35 unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
36 unshift (@INC, "$ENV{'GSDLHOME'}/perllib/cpan");
37 unshift (@INC, "$ENV{'GSDLHOME'}/perllib/classify");
38
39 if (defined $ENV{'GSDLEXTS'}) {
40 my @extensions = split(/:/,$ENV{'GSDLEXTS'});
41 foreach my $e (@extensions) {
42 my $ext_prefix = "$ENV{'GSDLHOME'}/ext/$e";
43
44 unshift (@INC, "$ext_prefix/perllib");
45 unshift (@INC, "$ext_prefix/perllib/cpan");
46 unshift (@INC, "$ext_prefix/perllib/classify");
47 }
48 }
49 if (defined $ENV{'GSDL3EXTS'}) {
50 my @extensions = split(/:/,$ENV{'GSDL3EXTS'});
51 foreach my $e (@extensions) {
52 my $ext_prefix = "$ENV{'GSDL3SRCHOME'}/ext/$e";
53
54 unshift (@INC, "$ext_prefix/perllib");
55 unshift (@INC, "$ext_prefix/perllib/cpan");
56 unshift (@INC, "$ext_prefix/perllib/classify");
57
58 }
59 }
60
61}
62
63use classify;
64use util;
65use gsprintf;
66use printusage;
67
68use parse2;
69
70my $arguments =
71 [ { 'name' => "collection",
72 'desc' => "{classinfo.collection}",
73 'type' => "string",
74 'deft' => "",
75 'reqd' => "no" },
76 { 'name' => "xml",
77 'desc' => "{scripts.xml}",
78 'type' => "flag",
79 'reqd' => "no" },
80 { 'name' => "listall",
81 'desc' => "{scripts.listall}",
82 'type' => "flag",
83 'reqd' => "no" },
84 { 'name' => "describeall",
85 'desc' => "{scripts.describeall}",
86 'type' => "flag",
87 'reqd' => "no" },
88 { 'name' => "language",
89 'desc' => "{scripts.language}",
90 'type' => "string",
91 'reqd' => "no" } ];
92
93my $options = { 'name' => "classinfo.pl",
94 'desc' => "{classinfo.desc}",
95 'args' => $arguments };
96
97sub gsprintf
98{
99 return &gsprintf::gsprintf(@_);
100}
101
102sub main {
103 my $collection = "";
104 my $xml = 0;
105 my $listall = 0;
106 my $describeall = 0;
107 my $language;
108
109 my $hashParsingResult = {};
110 # general options available to all classifiers
111 my $intArgLeftinAfterParsing = parse2::parse(\@ARGV,$arguments,$hashParsingResult,"allow_extra_options");
112 # parse returns -1 if an error occurred
113 if($intArgLeftinAfterParsing == -1)
114 {
115 &PrintUsage::print_txt_usage($options, "{classinfo.params}");
116 die "\n";
117 }
118
119 foreach my $strVariable (keys %$hashParsingResult)
120 {
121 eval "\$$strVariable = \$hashParsingResult->{\"\$strVariable\"}";
122 }
123 # If $language has been specified, load the appropriate resource bundle
124 # (Otherwise, the default resource bundle will be loaded automatically)
125 if ($language) {
126 &gsprintf::load_language_specific_resource_bundle($language);
127 }
128
129 # If there is not exactly 1 argument left (classifier name), then the arguments were wrong
130 # If the user specified -h, then we output the usage also
131 if((@ARGV && $ARGV[0] =~ /^\-+h/) )
132 {
133 PrintUsage::print_txt_usage($options, "{classinfo.params}");
134 die "\n";
135 }
136
137 # If there is not exactly 1 argument left (classifier name), then the arguments were wrong (apart from if we had listall or describeall set)
138 if ($listall == 0 && $describeall ==0 && $intArgLeftinAfterParsing == 0) {
139 gsprintf(STDERR, "{classinfo.no_classifier_name}\n\n");
140 PrintUsage::print_txt_usage($options, "{classinfo.params}", 1);
141 die "\n";
142 }
143
144 # we had some arguments that we weren't expecting
145 if ($intArgLeftinAfterParsing > 1) {
146 pop(@ARGV); # assume that the last arg is the classifier name
147 gsprintf(STDERR, "{common.invalid_options}\n\n", join (',', @ARGV));
148 PrintUsage::print_txt_usage($options, "{classinfo.params}", 1);
149 die "\n";
150 }
151
152 # Get classifier
153 my $classifier = shift (@ARGV);
154 if (defined $classifier) {
155 $classifier =~ s/\.pm$//; # allow xxx.pm as the argument
156 }
157
158 # make sure the classifier is loaded from the correct location - a hack.
159 if ($collection ne "") {
160 $ENV{'GSDLCOLLECTDIR'} = &util::filename_cat ($ENV{'GSDLHOME'}, "collect", $collection);
161 } else {
162 $ENV{'GSDLCOLLECTDIR'} = $ENV{'GSDLHOME'};
163 }
164
165 if ($listall || $describeall) {
166 my $classify_dir = &util::filename_cat($ENV{'GSDLCOLLECTDIR'}, "perllib", "classify");
167 my @classifier_list = ();
168 if (opendir (INDIR, $classify_dir)) {
169 @classifier_list = grep (/\.pm$/, readdir (INDIR));
170 closedir (INDIR);
171 }
172
173 if ((defined $ENV{'GSDLEXTS'}) && ($collection eq "")) {
174 my @extensions = split(/:/,$ENV{'GSDLEXTS'});
175 foreach my $e (@extensions) {
176 my $ext_prefix = &util::filename_cat($ENV{'GSDLHOME'},"ext",$e);
177 my $ext_classify_dir = &util::filename_cat($ext_prefix, "perllib", "classify");
178
179 if (opendir (INDIR, $ext_classify_dir)) {
180 my @ext_classifier_list = grep (/\.pm$/, readdir (INDIR));
181 closedir (INDIR);
182
183 push(@classifier_list,@ext_classifier_list);
184 }
185
186 }
187 }
188
189 print STDERR "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
190 print STDERR "<ClassifyList length=\"" . scalar(@classifier_list) . "\">\n";
191 foreach my $classifier (@classifier_list) {
192 $classifier =~ s/\.pm$//;
193 my $classifierobj = &classify::load_classifier_for_info ($classifier);
194 if ($describeall) {
195 $classifierobj->print_xml_usage(0);
196 }
197 else {
198 $classifierobj->print_xml_usage(0, 1);
199 }
200 }
201 print STDERR "</ClassifyList>\n";
202 }
203 else {
204 &print_single_classifier($classifier, $xml, 1);
205 }
206}
207
208
209sub print_single_classifier {
210 my ($classifier, $xml, $header) = @_;
211 my $classobj = &classify::load_classifier_for_info ($classifier);
212 if ($xml) {
213 $classobj->print_xml_usage($header);
214 }
215 else {
216
217 # this causes us to automatically send output to a pager, if one is
218 # set, AND our output is going to a terminal
219 # active state perl on windows doesn't do open(handle, "-|");
220 if ($ENV{'GSDLOS'} !~ /windows/ && -t STDOUT) {
221 my $pager = $ENV{"PAGER"};
222 if (! $pager) {$pager="(less || more)"}
223 my $pid = open(STDIN, "-|"); # this does a fork... see man perlipc(1)
224 if (!defined $pid) {
225 gsprintf(STDERR, "pluginfo.pl - can't fork: $!");
226 } else {
227 if ($pid != 0) { # parent (ie forking) process. child gets 0
228 exec ($pager);
229 }
230 }
231 open(STDERR,">&STDOUT"); # so it's easier to pipe output
232 }
233
234 &gsprintf(STDERR, "\n{classinfo.passing_options}\n\n");
235 &gsprintf(STDERR, "{classinfo.option_types}:\n\n");
236 &gsprintf(STDERR, "{classinfo.specific_options}\n\n");
237 &gsprintf(STDERR, "{classinfo.general_options}\n\n");
238 &gsprintf(STDERR, "$classifier {classinfo.info}:\n\n");
239
240 $classobj->print_txt_usage();
241 }
242
243}
244
245
246&main ();
Note: See TracBrowser for help on using the repository browser.