1 | ###########################################################################
|
---|
2 | #
|
---|
3 | # parse2.pm --
|
---|
4 | #
|
---|
5 | # A component of the Greenstone digital library software
|
---|
6 | # from the New Zealand Digital Library Project at the
|
---|
7 | # University of Waikato, New Zealand.
|
---|
8 | #
|
---|
9 | # Copyright (C) 2005-2010 New Zealand Digital Library Project
|
---|
10 | #
|
---|
11 | # This program is free software; you can redistribute it and/or modify
|
---|
12 | # it under the terms of the GNU General Public License as published by
|
---|
13 | # the Free Software Foundation; either version 2 of the License, or
|
---|
14 | # (at your option) any later version.
|
---|
15 | #
|
---|
16 | # This program is distributed in the hope that it will be useful,
|
---|
17 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
18 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
19 | # GNU General Public License for more details.
|
---|
20 | #
|
---|
21 | # You should have received a copy of the GNU General Public License
|
---|
22 | # along with this program; if not, write to the Free Software
|
---|
23 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
24 | #
|
---|
25 | ###########################################################################
|
---|
26 |
|
---|
27 |
|
---|
28 | #Last: Keeping doing the processArg for handing different type of arguments
|
---|
29 |
|
---|
30 | #parse2(\@_,$arguments,$self )
|
---|
31 |
|
---|
32 | package parse2;
|
---|
33 |
|
---|
34 | BEGIN {
|
---|
35 | die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
|
---|
36 | die "GSDLOS not set\n" unless defined $ENV{'GSDLOS'};
|
---|
37 |
|
---|
38 | my $current_library_paths = join(":",@INC);
|
---|
39 | my $gsdl_perllib_path = $ENV{'GSDLHOME'} . '/perllib';
|
---|
40 | if ($current_library_paths !~ /$gsdl_perllib_path/)
|
---|
41 | {
|
---|
42 | unshift (@INC, $gsdl_perllib_path); # [jmt12]
|
---|
43 | }
|
---|
44 | my $gsdl_cpan_path = $gsdl_perllib_path . '/cpan';
|
---|
45 | if ($current_library_paths !~ /$gsdl_cpan_path/)
|
---|
46 | {
|
---|
47 | unshift (@INC, $gsdl_cpan_path); # [jmt12]
|
---|
48 | }
|
---|
49 | }
|
---|
50 |
|
---|
51 | use strict;
|
---|
52 | use util;
|
---|
53 |
|
---|
54 |
|
---|
55 |
|
---|
56 | #--Local Util Functions----------------------------
|
---|
57 | #-----------------------------------------
|
---|
58 | # Name: transformArg
|
---|
59 | # Parameters: 1.(Array pointer of plugin pre-defined argument list)
|
---|
60 | # Pre-condition: Call this function and pass a array pointer of argument list.
|
---|
61 | # Post-condition: This function will transform the array to a hash table
|
---|
62 | # with "Argument name" as its key
|
---|
63 | # Return value: Return a hash table of plugin pre-defined argument
|
---|
64 | # list with "argument name" as the key
|
---|
65 | #-----------------------------------------
|
---|
66 | sub transformArg
|
---|
67 | {
|
---|
68 | my ($aryptSysArguList) = @_;
|
---|
69 | my %hashArg;
|
---|
70 |
|
---|
71 | foreach my $hashOneArg (@{$aryptSysArguList})
|
---|
72 | {
|
---|
73 | if(!(defined $hashArg{$hashOneArg->{"name"}}))
|
---|
74 | {
|
---|
75 | $hashArg{$hashOneArg->{"name"}} = $hashOneArg;
|
---|
76 | }
|
---|
77 | }
|
---|
78 | return %hashArg;
|
---|
79 | }
|
---|
80 |
|
---|
81 | sub checkRange
|
---|
82 | {
|
---|
83 | my ($strRange,$intInputArg,$strArgName) = @_;
|
---|
84 | my @aryRange = split(",",$strRange);
|
---|
85 | if(defined $aryRange[0])
|
---|
86 | {
|
---|
87 | if($intInputArg < $aryRange[0])
|
---|
88 | {
|
---|
89 | print STDERR " Parameter Parsing Error (Incorrect Range): when parse argument parameter for \"-$strArgName\"\n";
|
---|
90 | return 0;
|
---|
91 | }
|
---|
92 | else
|
---|
93 | {
|
---|
94 | if(scalar(@aryRange) == 2)
|
---|
95 | {
|
---|
96 | if($intInputArg > $aryRange[1])
|
---|
97 | {
|
---|
98 | print STDERR " Parameter Parsing Error (Incorrect Range): when parse argument parameter for \"-$strArgName\"\n";
|
---|
99 | return 0;
|
---|
100 | }
|
---|
101 | }
|
---|
102 | }
|
---|
103 | }
|
---|
104 | else{ die " System error: minimum range is not defined. Possible mistyping in Argument list for $strArgName\n";}
|
---|
105 | return 1;
|
---|
106 | }
|
---|
107 |
|
---|
108 | sub checkCharLength
|
---|
109 | {
|
---|
110 | my ($intCharLength,$intInputArg,$strArgName) = @_;
|
---|
111 | if($intCharLength =~ m/\d/)
|
---|
112 | {
|
---|
113 | if(length($intInputArg) != $intCharLength)
|
---|
114 | {
|
---|
115 | print STDERR " Parameter Parsing Error (Incorrect Char_Length): when parse argument parameter for \"-$strArgName\"\n";
|
---|
116 | return 0;
|
---|
117 | }
|
---|
118 | }
|
---|
119 | else
|
---|
120 | {
|
---|
121 | die " System error: incorrect char_length. Possible mistyping in Argument list for $strArgName\n";
|
---|
122 | }
|
---|
123 | return 1;
|
---|
124 | }
|
---|
125 | #-----------------------------------------
|
---|
126 | # Name: processArg
|
---|
127 | # Parameters: 1.(Hash pointer of one argument)
|
---|
128 | # 2.(Array pointer of the user given argument)
|
---|
129 | # 3.(Hash pointer of user given arguments' values)
|
---|
130 | # Pre-condition: Given a argument ($hashOneArg)
|
---|
131 | # Post-condition: System will check whether it need to get parameter
|
---|
132 | # from $aryptInputArguList or not, and also check the
|
---|
133 | # given parameter is following the argument description
|
---|
134 | # Return value: 1 is parsing successful, 0 is failed.
|
---|
135 | #-----------------------------------------
|
---|
136 | sub processArg
|
---|
137 | {
|
---|
138 | my ($hashOneArg,$aryptInputArguList,$hashInputArg) = @_;
|
---|
139 |
|
---|
140 | # Since these two variables are going to be
|
---|
141 | # used a lot, store them with some better names.
|
---|
142 | my $strArgName = $hashOneArg->{"name"};
|
---|
143 | my $strArgType = $hashOneArg->{"type"};
|
---|
144 |
|
---|
145 | # If the argument type is "flag" then
|
---|
146 | # set it to 1(which is "true")
|
---|
147 | if($strArgType eq "flag")
|
---|
148 | {
|
---|
149 | $hashInputArg->{$strArgName} = 1;
|
---|
150 | }
|
---|
151 |
|
---|
152 | # If the argument type is "int" then
|
---|
153 | # gets the next argument from $aryptInputArguList
|
---|
154 | # and check whether it is a digit
|
---|
155 | # TODO: check its "range" and "char_length"
|
---|
156 | elsif($strArgType eq "int")
|
---|
157 | {
|
---|
158 | my $intInputArg = shift(@{$aryptInputArguList});
|
---|
159 | if ($intInputArg =~ /\d+/)
|
---|
160 | {
|
---|
161 | $hashInputArg->{$strArgName} = $intInputArg;
|
---|
162 | }
|
---|
163 | else
|
---|
164 | {
|
---|
165 | print STDERR " Error: occur in parse2.pm::processArg()\n Unmatched Argument: -$strArgName with type $strArgType\n";
|
---|
166 | return 0;
|
---|
167 | }
|
---|
168 | }
|
---|
169 |
|
---|
170 | # If the argument type is "enum" then
|
---|
171 | elsif($strArgType eq "enum")
|
---|
172 | {
|
---|
173 | if(defined $hashOneArg->{"list"})
|
---|
174 | {
|
---|
175 | my $aryptList = $hashOneArg->{"list"};
|
---|
176 | my $blnCheckInList = "false";
|
---|
177 | my $strInputArg = shift(@{$aryptInputArguList});
|
---|
178 | foreach my $hashEachItem (@$aryptList)
|
---|
179 | {
|
---|
180 | if($strInputArg eq $hashEachItem->{"name"})
|
---|
181 | {
|
---|
182 | $blnCheckInList = "true";
|
---|
183 | }
|
---|
184 | last if($blnCheckInList eq "true");
|
---|
185 | }
|
---|
186 | if($blnCheckInList ne "true")
|
---|
187 | {
|
---|
188 | print STDERR " Error: occur in parse2.pm::processArg()\n Unknown Enum List Type: -$strArgName with parameter: $strInputArg\n";
|
---|
189 | return 0;
|
---|
190 | } else {
|
---|
191 | $hashInputArg->{$strArgName} = $strInputArg;
|
---|
192 | }
|
---|
193 |
|
---|
194 | }
|
---|
195 | else
|
---|
196 | {
|
---|
197 | print STDERR " Error: occur in parse2.pm::processArg(2)\n Unknown Type: -$strArgName with type $strArgType\n";
|
---|
198 | return 0;
|
---|
199 | }
|
---|
200 | }
|
---|
201 |
|
---|
202 | # If the argument type is "string" or "metadata" or "quotestr" then
|
---|
203 | # just shift the next argument from $aryptInputArguList
|
---|
204 | # TODO: make sure if there is any checking required for this two types
|
---|
205 | elsif($strArgType eq "string" || $strArgType eq "enumstring" || $strArgType eq "quotestr" || $strArgType eq "metadata" || $strArgType eq "regexp" || $strArgType eq "url")
|
---|
206 | {
|
---|
207 | $hashInputArg->{$strArgName}= shift(@{$aryptInputArguList});
|
---|
208 | }
|
---|
209 |
|
---|
210 | # Report any undefined types
|
---|
211 | else
|
---|
212 | {
|
---|
213 | print STDERR " Error: occur in parse2.pm::processArg(3)\n Unknown Type: -$strArgName with type $strArgType\n";
|
---|
214 | return 0;
|
---|
215 | }
|
---|
216 |
|
---|
217 | return 1;
|
---|
218 | }
|
---|
219 |
|
---|
220 | #--Main Parsing Function----------------------------
|
---|
221 | #-----------------------------------------
|
---|
222 | # Name: parse
|
---|
223 | # Parameters: 1.(Array pointer of the user given argument)
|
---|
224 | # 2.(Array pointer of plugin pre-defined argument list)
|
---|
225 | # 3.(Hash pointer, where we store all the argument value)
|
---|
226 | # Pre-condition: Plugin gives the parameters to parse function in parse2
|
---|
227 | # Post-condition: Store all the default or user given values to the hash->{$ArgumentName}.
|
---|
228 | # Since hash may be a plugin $self, plugin will have every values we set.
|
---|
229 | # 4. Optional "allow_extra_options" argument. If this is set, then
|
---|
230 | # its ok to have arguments that are not in the predefined list
|
---|
231 | # Return value: -1 if parsing is unsuccessful
|
---|
232 | # other value for success. This will be 0 unless "allow_extra_options" is set, in which case it will be the number of extra arguments found.
|
---|
233 | #-----------------------------------------
|
---|
234 | sub parse
|
---|
235 | {
|
---|
236 | # Get the user supplied arguments pointer "\@_"
|
---|
237 | my $aryptUserArguList = shift;
|
---|
238 |
|
---|
239 | # Check if allow extra arguments
|
---|
240 | my $blnAllowExtraOptions = "false";
|
---|
241 |
|
---|
242 | if(scalar(@_) == 3)
|
---|
243 | {
|
---|
244 | my $strAllowExtraOptions = pop @_;
|
---|
245 |
|
---|
246 | if ($strAllowExtraOptions eq "allow_extra_options")
|
---|
247 | {
|
---|
248 | $blnAllowExtraOptions = "true";
|
---|
249 | }
|
---|
250 | }
|
---|
251 |
|
---|
252 | my ($aryptSysArguList,$self) = @_;
|
---|
253 | my %hashArg;
|
---|
254 | my %hashInputArg;
|
---|
255 | my @ExtraOption;
|
---|
256 |
|
---|
257 | # Transform the system argument (predefined the code)
|
---|
258 | # from array to hash table for increasing performance
|
---|
259 | %hashArg = &transformArg($aryptSysArguList);
|
---|
260 |
|
---|
261 | # Process each User input argument and store the
|
---|
262 | # information into hashInputArg
|
---|
263 | while (my $strOneArg = shift(@{$aryptUserArguList}))
|
---|
264 | {
|
---|
265 | # Check whether it start with a "-" sign
|
---|
266 | if ($strOneArg =~ /^-+\w/)
|
---|
267 | {
|
---|
268 | # If it is start with a "-" sign then take it off
|
---|
269 | $strOneArg =~ s/^-+//;
|
---|
270 |
|
---|
271 | # If the inputed argument is defined in the argument
|
---|
272 | # list from this plugin then process
|
---|
273 |
|
---|
274 | if(defined $hashArg{$strOneArg})
|
---|
275 | {
|
---|
276 | #$%^
|
---|
277 | #print "($strOneArg) is processed\n";
|
---|
278 | # Process this argument and store the related
|
---|
279 | # information in %hashInputArg
|
---|
280 | if(processArg($hashArg{$strOneArg},$aryptUserArguList,\%hashInputArg) == 0){
|
---|
281 | print STDERR "<BadArgumentValue a=$strOneArg>\n";
|
---|
282 | return -1;}
|
---|
283 | }
|
---|
284 |
|
---|
285 | # Else check if it allows extra options, if yes
|
---|
286 | # then push it to a new array, else return fault
|
---|
287 | else
|
---|
288 | {
|
---|
289 | if($blnAllowExtraOptions eq "true")
|
---|
290 | {
|
---|
291 | push(@ExtraOption,"-$strOneArg");
|
---|
292 | }
|
---|
293 | else
|
---|
294 | {
|
---|
295 | print STDERR "<BadArgument a=$strOneArg>\n";
|
---|
296 | print STDERR " Error: occur in parse2.pm::parse()\n Extra Arguments: $strOneArg\n";
|
---|
297 | return -1;
|
---|
298 | }
|
---|
299 | }
|
---|
300 | }
|
---|
301 |
|
---|
302 | # This part follow the previous parsing system.
|
---|
303 | # It doesn't return error message even user
|
---|
304 | # gave a invalid argument.
|
---|
305 | else
|
---|
306 | {
|
---|
307 | if($blnAllowExtraOptions eq "true")
|
---|
308 | {
|
---|
309 | push(@ExtraOption,$strOneArg);
|
---|
310 | }
|
---|
311 | else
|
---|
312 | {
|
---|
313 | print STDERR " Error: occur in parse2.pm::parse()\n Invalid Argument: $strOneArg\n";
|
---|
314 | return -1;
|
---|
315 | }
|
---|
316 | }
|
---|
317 | }
|
---|
318 |
|
---|
319 | # Store the extra option back
|
---|
320 | # to the user given argument list.
|
---|
321 | @$aryptUserArguList = @ExtraOption;
|
---|
322 |
|
---|
323 | # Now we go through all the pre defined arguments,
|
---|
324 | # if the user has specified the arguments then just
|
---|
325 | # set to whatever they set. Otherwise use the default value
|
---|
326 | foreach my $hashOneArg (@{$aryptSysArguList})
|
---|
327 | {
|
---|
328 | my $strArgName = $hashOneArg->{"name"};
|
---|
329 |
|
---|
330 | # If the strArgName has defined in the %hashInputArg,
|
---|
331 | # this means users has give this argument, store the
|
---|
332 | # user given to self->{"$strArgName"}
|
---|
333 | if(defined $hashInputArg{$strArgName})
|
---|
334 | {
|
---|
335 | if(defined $hashOneArg->{"range"})
|
---|
336 | {
|
---|
337 | if(checkRange($hashOneArg->{"range"},$hashInputArg{$strArgName},$strArgName) == 0){ return -1;}
|
---|
338 | }
|
---|
339 | if(defined $hashOneArg->{"char_length"})
|
---|
340 | {
|
---|
341 | if(checkCharLength($hashOneArg->{"char_length"},$hashInputArg{$strArgName},$strArgName) == 0){ return -1;}
|
---|
342 | }
|
---|
343 | $self->{"$strArgName"} = $hashInputArg{"$strArgName"};
|
---|
344 | }
|
---|
345 | elsif (!defined $self->{$strArgName})
|
---|
346 | {
|
---|
347 | # don't want to override default with superclass value
|
---|
348 |
|
---|
349 | # Else use the default value of the arguments,
|
---|
350 | # if there is no default value, then it must be a flag,
|
---|
351 | # then set it to 0 (which is false)
|
---|
352 |
|
---|
353 | if(defined $hashOneArg->{"deft"})
|
---|
354 | {
|
---|
355 | $self->{"$strArgName"} = $hashOneArg->{"deft"};
|
---|
356 | }
|
---|
357 | else
|
---|
358 | {
|
---|
359 | if($hashOneArg->{"type"} eq "flag"){
|
---|
360 | $self->{"$strArgName"} = 0;
|
---|
361 | }
|
---|
362 | else {
|
---|
363 | # all other cases, use "" as default
|
---|
364 | $self->{"$strArgName"} = "";
|
---|
365 | }
|
---|
366 | }
|
---|
367 | }
|
---|
368 | }
|
---|
369 |
|
---|
370 | # If allow_extra_options is set, then return the number of arguments left in the argument list.
|
---|
371 | if($blnAllowExtraOptions eq "true")
|
---|
372 | {
|
---|
373 | return scalar(@$aryptUserArguList);
|
---|
374 | }
|
---|
375 | else
|
---|
376 | {
|
---|
377 | return 0;
|
---|
378 | }
|
---|
379 | }
|
---|
380 |
|
---|
381 | 1;
|
---|