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

Last change on this file since 31751 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
RevLine 
[1885]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
[10230]29use strict;
30no strict 'subs'; # allow barewords (eg STDERR) as function arguments
[1885]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");
[5882]36 unshift (@INC, "$ENV{'GSDLHOME'}/perllib/cpan");
[1885]37 unshift (@INC, "$ENV{'GSDLHOME'}/perllib/classify");
[14942]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");
[16787]46 unshift (@INC, "$ext_prefix/perllib/classify");
[14942]47 }
48 }
[21292]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
[1885]61}
62
63use classify;
64use util;
[5606]65use gsprintf;
66use printusage;
[1885]67
[10230]68use parse2;
[5606]69
70my $arguments =
[12639]71 [ { 'name' => "collection",
72 'desc' => "{classinfo.collection}",
[5606]73 'type' => "string",
[10230]74 'deft' => "",
[5606]75 'reqd' => "no" },
76 { 'name' => "xml",
77 'desc' => "{scripts.xml}",
78 'type' => "flag",
79 'reqd' => "no" },
[7952]80 { 'name' => "listall",
81 'desc' => "{scripts.listall}",
82 'type' => "flag",
83 'reqd' => "no" },
[11683]84 { 'name' => "describeall",
85 'desc' => "{scripts.describeall}",
86 'type' => "flag",
87 'reqd' => "no" },
[5606]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
[6921]97sub gsprintf
[5606]98{
[6921]99 return &gsprintf::gsprintf(@_);
[1885]100}
101
102sub main {
[12639]103 my $collection = "";
[4762]104 my $xml = 0;
[7952]105 my $listall = 0;
[11683]106 my $describeall = 0;
[10230]107 my $language;
[1885]108
[10230]109 my $hashParsingResult = {};
[12545]110 # general options available to all classifiers
[10230]111 my $intArgLeftinAfterParsing = parse2::parse(\@ARGV,$arguments,$hashParsingResult,"allow_extra_options");
[12545]112 # parse returns -1 if an error occurred
113 if($intArgLeftinAfterParsing == -1)
[4762]114 {
[10230]115 &PrintUsage::print_txt_usage($options, "{classinfo.params}");
[4762]116 die "\n";
[1885]117 }
118
[10230]119 foreach my $strVariable (keys %$hashParsingResult)
120 {
121 eval "\$$strVariable = \$hashParsingResult->{\"\$strVariable\"}";
122 }
[6945]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 }
[12545]128
129 # If there is not exactly 1 argument left (classifier name), then the arguments were wrong
[12613]130 # If the user specified -h, then we output the usage also
131 if((@ARGV && $ARGV[0] =~ /^\-+h/) )
[12545]132 {
133 PrintUsage::print_txt_usage($options, "{classinfo.params}");
134 die "\n";
135 }
[6926]136
[12614]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)
[12613]138 if ($listall == 0 && $describeall ==0 && $intArgLeftinAfterParsing == 0) {
[12614]139 gsprintf(STDERR, "{classinfo.no_classifier_name}\n\n");
140 PrintUsage::print_txt_usage($options, "{classinfo.params}", 1);
[12613]141 die "\n";
142 }
143
144 # we had some arguments that we weren't expecting
145 if ($intArgLeftinAfterParsing > 1) {
[12614]146 pop(@ARGV); # assume that the last arg is the classifier name
[12613]147 gsprintf(STDERR, "{common.invalid_options}\n\n", join (',', @ARGV));
[12614]148 PrintUsage::print_txt_usage($options, "{classinfo.params}", 1);
[12613]149 die "\n";
150 }
151
[1885]152 # Get classifier
153 my $classifier = shift (@ARGV);
[10350]154 if (defined $classifier) {
155 $classifier =~ s/\.pm$//; # allow xxx.pm as the argument
156 }
[1885]157
158 # make sure the classifier is loaded from the correct location - a hack.
[12639]159 if ($collection ne "") {
160 $ENV{'GSDLCOLLECTDIR'} = &util::filename_cat ($ENV{'GSDLHOME'}, "collect", $collection);
[1885]161 } else {
[4762]162 $ENV{'GSDLCOLLECTDIR'} = $ENV{'GSDLHOME'};
[1885]163 }
[4762]164
[12629]165 if ($listall || $describeall) {
[12640]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);
[12625]171 }
[7952]172
[14958]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
[12640]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);
[7952]196 }
[12640]197 else {
198 $classifierobj->print_xml_usage(0, 1);
199 }
[7952]200 }
[12640]201 print STDERR "</ClassifyList>\n";
[4762]202 }
[11683]203 else {
204 &print_single_classifier($classifier, $xml, 1);
205 }
[1885]206}
207
[12629]208
[11683]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 {
[22518]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
[11683]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}
[1885]244
[11683]245
[5606]246&main ();
Note: See TracBrowser for help on using the repository browser.