5 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
10 @EXPORT_OK = qw($options &parse_comma_list &parse_value);
12 use vars qw($options);
14 use output qw($output);
16 sub parse_comma_list {
20 if(defined($prefix) && $prefix eq "no") {
21 return { active => 0, filter => 0, hash => {} };
22 } elsif(defined($value)) {
24 for my $name (split /,/, $value) {
27 return { active => 1, filter => 1, hash => \%names };
29 return { active => 1, filter => 0, hash => {} };
44 use output qw($output);
48 my $class = ref($proto) || $proto;
50 bless ($self, $class);
52 my $options_long = \%{$self->{_OPTIONS_LONG}};
53 my $options_short = \%{$self->{_OPTIONS_SHORT}};
54 my $options_usage = \${$self->{_OPTIONS_USAGE}};
56 my $refoptions_long = shift;
57 my $refoptions_short = shift;
58 $$options_usage = shift;
60 %$options_long = %{$refoptions_long};
61 %$options_short = %{$refoptions_short};
63 $self->options_set("default");
65 my $arguments = \@{$self->{_ARGUMENTS}};
68 my $end_of_options = 0;
69 while(defined($_ = shift @ARGV)) {
73 } elsif($end_of_options) {
75 } elsif(/^--(all|none)$/) {
76 $self->options_set("$1");
78 } elsif(/^-([^=]*)(=(.*))?$/) {
88 if($name =~ /^([^-].*)$/) {
89 $name = $$options_short{$1};
91 $name =~ s/^-(.*)$/$1/;
95 if(defined($name) && $name =~ /^no-(.*)$/) {
99 $output->write("options with prefix 'no' can't take parameters\n");
107 $option = $$options_long{$name};
110 if(defined($option)) {
111 my $key = $$option{key};
112 my $parser = $$option{parser};
113 my $refvalue = \${$self->{$key}};
116 if(defined($$option{parent})) {
117 if(ref($$option{parent}) eq "ARRAY") {
118 @parents = @{$$option{parent}};
120 @parents = $$option{parent};
124 if(defined($parser)) {
125 if(!defined($value)) {
126 $value = shift @ARGV;
128 $$refvalue = &$parser($prefix,$value);
130 if(defined($value)) {
132 } elsif(!defined($prefix)) {
139 if((ref($$refvalue) eq "HASH" && $$refvalue->{active}) || $$refvalue) {
140 while($#parents >= 0) {
141 my @old_parents = @parents;
143 foreach my $parent (@old_parents) {
144 my $parentkey = $$options_long{$parent}{key};
145 my $refparentvalue = \${$self->{$parentkey}};
147 $$refparentvalue = 1;
149 if(defined($$options_long{$parent}{parent})) {
150 if(ref($$options_long{$parent}{parent}) eq "ARRAY") {
151 push @parents, @{$$options_long{$parent}{parent}};
153 push @parents, $$options_long{$parent}{parent};
163 if(!$end_of_options && /^-(.*)$/) {
164 $output->write("unknown option: $_\n");
165 $output->write($$options_usage);
168 push @$arguments, $_;
173 $output->write($$options_usage);
187 my $arguments = \@{$self->{_ARGUMENTS}};
188 my $directories = \@{$self->{_DIRECTORIES}};
189 my $c_files = \@{$self->{_C_FILES}};
190 my $h_files = \@{$self->{_H_FILES}};
194 foreach (@$arguments) {
196 $output->write("$_: no such file or directory\n");
209 foreach my $file (@files) {
210 if($file =~ /\.c$/) {
211 push @c_files, $file;
212 } elsif($file =~ /\.h$/) {
213 push @h_files, $file;
219 if($#c_files == -1 && $#h_files == -1 && $#paths == -1)
224 if($#paths != -1 || $#c_files != -1) {
225 my $c_command = "find " . join(" ", @paths, @c_files) . " -name \\*.c";
227 @$c_files = sort(map {
229 if(defined($found{$_})) {
235 } split(/\n/, `$c_command`));
238 if($#paths != -1 || $#h_files != -1) {
239 my $h_command = "find " . join(" ", @paths, @h_files) . " -name \\*.h";
242 @$h_files = sort(map {
244 if(defined($found{$_})) {
250 } split(/\n/, `$h_command`));
254 foreach my $file (@$c_files, @$h_files) {
256 $dir =~ s%/?[^/]+$%%;
257 if(!$dir) { $dir = "."; }
261 @$directories = sort(keys(%dirs));
267 my $options_long = \%{$self->{_OPTIONS_LONG}};
268 my $options_short = \%{$self->{_OPTIONS_SHORT}};
271 for my $name (sort(keys(%$options_long))) {
272 my $option = $$options_long{$name};
275 $$option{key} = $key;
276 my $refvalue = \${$self->{$key}};
279 $$refvalue = $$option{default};
281 if($name !~ /^help|debug|verbose|module$/) {
282 if(ref($$refvalue) ne "HASH") {
285 $$refvalue = { active => 1, filter => 0, hash => {} };
289 if($name !~ /^help|debug|verbose|module$/) {
290 if(ref($$refvalue) ne "HASH") {
293 $$refvalue = { active => 0, filter => 0, hash => {} };
303 my $options_long = \%{$self->{_OPTIONS_LONG}};
304 my $options_short = \%{$self->{_OPTIONS_SHORT}};
307 for my $name (sort(keys(%$options_long))) {
308 if(length($name) > $maxname) {
309 $maxname = length($name);
313 for my $name (sort(keys(%$options_long))) {
314 my $option = $$options_long{$name};
315 my $description = $$option{description};
316 my $default = $$option{default};
317 my $current = ${$self->{$$option{key}}};
319 my $value = $current;
322 if(ref($value) ne "HASH") {
324 $command = "--no-$name";
326 $command = "--$name";
329 if($value->{active}) {
330 $command = "--[no-]$name\[=<value>]";
332 $command = "--$name\[=<value>]";
336 $output->write($command);
337 for (0..(($maxname - length($name) + 17) - (length($command) - length($name) + 1))) { $output->write(" "); }
338 if(ref($value) ne "HASH") {
340 $output->write("Disable ");
342 $output->write("Enable ");
345 if($value->{active}) {
346 $output->write("(Disable) ");
348 $output->write("Enable ");
351 if($default == $current) {
352 $output->write("$description (default)\n");
354 $output->write("$description\n");
362 my $name = $_options::AUTOLOAD;
363 $name =~ s/^.*::(.[^:]*)$/\U$1/;
365 my $refvalue = $self->{$name};
366 if(!defined($refvalue)) {
367 die "<internal>: options.pm: member $name does not exists\n";
370 if(ref($$refvalue) ne "HASH") {
373 return $$refvalue->{active};
380 my $arguments = \@{$self->{_ARGUMENTS}};
388 my $c_files = \@{$self->{_C_FILES}};
390 if(!defined(@$c_files)) {
400 my $h_files = \@{$self->{_H_FILES}};
402 if(!defined(@$h_files)) {
412 my $directories = \@{$self->{_DIRECTORIES}};
414 if(!defined(@$directories)) {
418 return @$directories;