2 # Copyright 1999, 2000, 2001 Patrik Stridvall
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # Lesser General Public License for more details.
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 use vars
qw($VERSION @ISA @EXPORT @EXPORT_OK);
29 @EXPORT_OK = qw($options parse_comma_list parse_value);
31 use vars qw($options);
33 use output qw($output);
35 sub parse_comma_list($$) {
39 if(defined($prefix) && $prefix eq "no") {
40 return { active => 0, filter => 0, hash => {} };
41 } elsif(defined($value)) {
43 for my $name (split /,/, $value) {
46 return { active => 1, filter => 1, hash => \%names };
48 return { active => 1, filter => 0, hash => {} };
64 use output qw($output);
70 my $class = ref($proto) || $proto;
72 bless ($self, $class);
74 my $options_long = \%{$self->{_OPTIONS_LONG}};
75 my $options_short = \%{$self->{_OPTIONS_SHORT}};
76 my $options_usage = \${$self->{_OPTIONS_USAGE}};
78 my $refoptions_long = shift;
79 my $refoptions_short = shift;
80 $$options_usage = shift;
82 %$options_long = %{$refoptions_long};
83 %$options_short = %{$refoptions_short};
85 $self->options_set("default");
87 my $arguments = \@{$self->{_ARGUMENTS}};
90 my $end_of_options = 0;
91 while(defined($_ = shift @ARGV)) {
95 } elsif($end_of_options) {
97 } elsif(/^--(all|none)$/) {
98 $self->options_set("$1");
100 } elsif(/^-([^=]*)(=(.*))?$/) {
110 if($name =~ /^([^-].*)$/) {
111 $name = $$options_short{$1};
113 $name =~ s/^-(.*)$/$1/;
117 if(defined($name) && $name =~ /^no-(.*)$/) {
120 if(defined($value)) {
121 $output->write("options with prefix 'no' can't take parameters\n");
129 $option = $$options_long{$name};
132 if(defined($option)) {
133 my $key = $$option{key};
134 my $parser = $$option{parser};
135 my $refvalue = \${$self->{$key}};
138 if(defined($$option{parent})) {
139 if(ref($$option{parent}) eq "ARRAY") {
140 @parents = @{$$option{parent}};
142 @parents = $$option{parent};
146 if(defined($parser)) {
147 if(!defined($value)) {
148 $value = shift @ARGV;
150 $$refvalue = &$parser($prefix,$value);
152 if(defined($value)) {
154 } elsif(!defined($prefix)) {
161 if((ref($$refvalue) eq "HASH" && $$refvalue->{active}) || $$refvalue) {
162 while($#parents >= 0) {
163 my @old_parents = @parents;
165 foreach my $parent (@old_parents) {
166 my $parentkey = $$options_long{$parent}{key};
167 my $refparentvalue = \${$self->{$parentkey}};
169 $$refparentvalue = 1;
171 if(defined($$options_long{$parent}{parent})) {
172 if(ref($$options_long{$parent}{parent}) eq "ARRAY") {
173 push @parents, @{$$options_long{$parent}{parent}};
175 push @parents, $$options_long{$parent}{parent};
185 if(!$end_of_options && /^-(.*)$/) {
186 $output->write("unknown option: $_\n");
187 $output->write($$options_usage);
190 push @$arguments, $_;
195 $output->write($$options_usage);
209 my $arguments = \@{$self->{_ARGUMENTS}};
210 my $directories = \@{$self->{_DIRECTORIES}};
211 my $c_files = \@{$self->{_C_FILES}};
212 my $h_files = \@{$self->{_H_FILES}};
216 foreach (@$arguments) {
218 $output->write("$_: no such file or directory\n");
231 foreach my $file (@files) {
232 if($file =~ /\.c$/) {
233 push @c_files, $file;
234 } elsif($file =~ /\.h$/) {
235 push @h_files, $file;
241 if($#c_files == -1 && $#h_files == -1 && $#paths == -1 && -d ".git")
243 @$c_files = sort split /\0/, `git ls-files -z \\*.c`;
244 @$h_files = sort split /\0/, `git ls-files -z \\*.h`;
248 if($#c_files == -1 && $#h_files == -1 && $#paths == -1)
253 if($#paths != -1 || $#c_files != -1) {
254 my $c_command = "find " . join(" ", @paths, @c_files) . " -name \\*.c";
256 @$c_files = sort(map {
258 if(defined($found{$_})) {
264 } split(/\n/, `$c_command`));
267 if($#paths != -1 || $#h_files != -1) {
268 my $h_command = "find " . join(" ", @paths, @h_files) . " -name \\*.h";
271 @$h_files = sort(map {
273 if(defined($found{$_})) {
279 } split(/\n/, `$h_command`));
284 foreach my $file (@$c_files, @$h_files) {
286 $dir =~ s%/?[^/]+$%%;
287 if(!$dir) { $dir = "."; }
291 @$directories = sort(keys(%dirs));
294 sub options_set($$) {
297 my $options_long = \%{$self->{_OPTIONS_LONG}};
298 my $options_short = \%{$self->{_OPTIONS_SHORT}};
301 for my $name (sort(keys(%$options_long))) {
302 my $option = $$options_long{$name};
305 $$option{key} = $key;
306 my $refvalue = \${$self->{$key}};
309 $$refvalue = $$option{default};
311 if($name !~ /^(?:help|debug|verbose|module)$/) {
312 if(ref($$refvalue) ne "HASH") {
315 $$refvalue = { active => 1, filter => 0, hash => {} };
319 if($name !~ /^(?:help|debug|verbose|module)$/) {
320 if(ref($$refvalue) ne "HASH") {
323 $$refvalue = { active => 0, filter => 0, hash => {} };
333 my $options_long = \%{$self->{_OPTIONS_LONG}};
334 my $options_short = \%{$self->{_OPTIONS_SHORT}};
337 for my $name (sort(keys(%$options_long))) {
338 if(length($name) > $maxname) {
339 $maxname = length($name);
343 for my $name (sort(keys(%$options_long))) {
344 my $option = $$options_long{$name};
345 my $description = $$option{description};
346 my $parser = $$option{parser};
347 my $current = ${$self->{$$option{key}}};
349 my $value = $current;
352 if(!defined $parser) {
354 $command = "--no-$name";
356 $command = "--$name";
359 if(ref($value) eq "HASH" && $value->{active}) {
360 $command = "--[no-]$name\[=<value>]";
362 $command = "--$name\[=<value>]";
366 $output->write($command);
367 $output->write(" " x (($maxname - length($name) + 17) - (length($command) - length($name) + 1)));
368 if(!defined $parser) {
370 $output->write("Disable ");
372 $output->write("Enable ");
375 if(ref($value) eq "HASH")
377 if ($value->{active}) {
378 $output->write("(Disable) ");
380 $output->write("Enable ");
384 $output->write("$description\n");
391 my $name = $_options::AUTOLOAD;
392 $name =~ s/^.*::(.[^:]*)$/\U$1/;
394 my $refvalue = $self->{$name};
395 if(!defined($refvalue)) {
396 die "<internal>: options.pm: member $name does not exist\n";
399 if(ref($$refvalue) ne "HASH") {
402 return $$refvalue->{active};
409 my $arguments = \@{$self->{_ARGUMENTS}};
417 my $c_files = \@{$self->{_C_FILES}};
429 my $h_files = \@{$self->{_H_FILES}};
441 my $directories = \@{$self->{_DIRECTORIES}};
447 return @$directories;