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}};
67 my $end_of_options = 0;
68 while(defined($_ = shift @ARGV)) {
72 } elsif($end_of_options) {
74 } elsif(/^--(all|none)$/) {
75 $self->options_set("$1");
77 } elsif(/^-([^=]*)(=(.*))?$/) {
87 if($name =~ /^([^-].*)$/) {
88 $name = $$options_short{$1};
90 $name =~ s/^-(.*)$/$1/;
94 if(defined($name) && $name =~ /^no-(.*)$/) {
98 $output->write("options with prefix 'no' can't take parameters\n");
106 $option = $$options_long{$name};
109 if(defined($option)) {
110 my $key = $$option{key};
111 my $parser = $$option{parser};
112 my $refvalue = \${$self->{$key}};
115 if(defined($$option{parent})) {
116 if(ref($$option{parent}) eq "ARRAY") {
117 @parents = @{$$option{parent}};
119 @parents = $$option{parent};
123 if(defined($parser)) {
124 $$refvalue = &$parser($prefix,$value);
126 if(defined($value)) {
128 } elsif(!defined($prefix)) {
135 if((ref($$refvalue) eq "HASH" && $$refvalue->{active}) || $$refvalue) {
136 while($#parents >= 0) {
137 my @old_parents = @parents;
139 foreach my $parent (@old_parents) {
140 my $parentkey = $$options_long{$parent}{key};
141 my $refparentvalue = \${$self->{$parentkey}};
143 $$refparentvalue = 1;
145 if(defined($$options_long{$parent}{parent})) {
146 if(ref($$options_long{$parent}{parent}) eq "ARRAY") {
147 push @parents, @{$$options_long{$parent}{parent}};
149 push @parents, $$options_long{$parent}{parent};
159 if(!$end_of_options && /^-(.*)$/) {
160 $output->write("unknown option: $_\n");
161 $output->write($$options_usage);
164 push @$arguments, $_;
169 $output->write($$options_usage);
183 my $arguments = \@{$self->{_ARGUMENTS}};
184 my $directories = \@{$self->{_DIRECTORIES}};
185 my $c_files = \@{$self->{_C_FILES}};
186 my $h_files = \@{$self->{_H_FILES}};
190 foreach (@$arguments) {
192 $output->write("$_: no such file or directory\n");
205 foreach my $file (@files) {
206 if($file =~ /\.c$/) {
207 push @c_files, $file;
208 } elsif($file =~ /\.h$/) {
209 push @h_files, $file;
215 if($#c_files == -1 && $#h_files == -1 && $#paths == -1)
220 if($#paths != -1 || $#c_files != -1) {
221 my $c_command = "find " . join(" ", @paths, @c_files) . " -name \\*.c";
223 @$c_files = sort(map {
225 if(defined($found{$_})) {
231 } split(/\n/, `$c_command`));
234 if($#paths != -1 || $#h_files != -1) {
235 my $h_command = "find " . join(" ", @paths, @h_files) . " -name \\*.h";
238 @$h_files = sort(map {
240 if(defined($found{$_})) {
246 } split(/\n/, `$h_command`));
250 foreach my $file (@$c_files, @$h_files) {
252 $dir =~ s%/?[^/]+$%%;
253 if(!$dir) { $dir = "."; }
257 @$directories = sort(keys(%dirs));
263 my $options_long = \%{$self->{_OPTIONS_LONG}};
264 my $options_short = \%{$self->{_OPTIONS_SHORT}};
267 for my $name (sort(keys(%$options_long))) {
268 my $option = $$options_long{$name};
271 $$option{key} = $key;
272 my $refvalue = \${$self->{$key}};
275 $$refvalue = $$option{default};
277 if($name !~ /^help|debug|verbose|module$/) {
278 if(ref($$refvalue) ne "HASH") {
281 $$refvalue = { active => 1, filter => 0, hash => {} };
285 if($name !~ /^help|debug|verbose|module$/) {
286 if(ref($$refvalue) ne "HASH") {
289 $$refvalue = { active => 0, filter => 0, hash => {} };
299 my $options_long = \%{$self->{_OPTIONS_LONG}};
300 my $options_short = \%{$self->{_OPTIONS_SHORT}};
303 for my $name (sort(keys(%$options_long))) {
304 if(length($name) > $maxname) {
305 $maxname = length($name);
309 for my $name (sort(keys(%$options_long))) {
310 my $option = $$options_long{$name};
311 my $description = $$option{description};
312 my $default = $$option{default};
313 my $current = ${$self->{$$option{key}}};
315 my $value = $current;
318 if(ref($value) ne "HASH") {
320 $command = "--no-$name";
322 $command = "--$name";
325 if($value->{active}) {
326 $command = "--[no-]$name\[=<value>]";
328 $command = "--$name\[=<value>]";
332 $output->write($command);
333 for (0..(($maxname - length($name) + 17) - (length($command) - length($name) + 1))) { $output->write(" "); }
334 if(ref($value) ne "HASH") {
336 $output->write("Disable ");
338 $output->write("Enable ");
341 if($value->{active}) {
342 $output->write("(Disable) ");
344 $output->write("Enable ");
347 if($default == $current) {
348 $output->write("$description (default)\n");
350 $output->write("$description\n");
358 my $name = $_options::AUTOLOAD;
359 $name =~ s/^.*::(.[^:]*)$/\U$1/;
361 my $refvalue = $self->{$name};
362 if(!defined($refvalue)) {
363 die "<internal>: options.pm: member $name does not exists\n";
366 if(ref($$refvalue) ne "HASH") {
369 return $$refvalue->{active};
376 my $arguments = \@{$self->{_ARGUMENTS}};
384 my $c_files = \@{$self->{_C_FILES}};
386 if(!defined(@$c_files)) {
396 my $h_files = \@{$self->{_H_FILES}};
398 if(!defined(@$h_files)) {
408 my $directories = \@{$self->{_DIRECTORIES}};
410 if(!defined(@$directories)) {
414 return @$directories;