]> mj.ucw.cz Git - libucw.git/blob - lib/perl/Config.pm
Allow setting of default configuration file.
[libucw.git] / lib / perl / Config.pm
1 # Perl module for parsing Sherlock configuration files (using the config utility)
2 # (c) 2002 Martin Mares <mj@ucw.cz>
3
4 package Sherlock::Config;
5
6 use strict;
7 use warnings;
8 use Getopt::Long;
9
10 our %Sections = ();
11
12 our $DefaultConfigFile = "";
13
14 sub Parse(@) {
15         my @options = @_;
16         my $defargs = "";
17         my $override_config = 0;
18         push @options, "config|C=s" => sub { my ($o,$a)=@_; $defargs .= " -C'$a'"; $override_config=1; };
19         push @options, "set|S=s" => sub { my ($o,$a)=@_; $defargs .= " -S'$a'"; };
20         Getopt::Long::GetOptions(@options) or return 0;
21         if (!$override_config && $DefaultConfigFile) {
22                 $defargs = "-C'$DefaultConfigFile' $defargs";
23         }
24         foreach my $section (keys %Sections) {
25                 my $opts = $Sections{$section};
26                 my $optlist = join(" ", keys %$opts);
27                 my @l = `bin/config $defargs $section $optlist`;
28                 $? && exit 1;
29                 foreach my $o (@l) {
30                         $o =~ /^CF_([^=]+)="(.*)"\n$/ or die "Cannot parse bin/config output: $_";
31                         my $var = $$opts{$1};
32                         my $val = $2;
33                         if (ref $var eq "SCALAR") {
34                                 $$var = $val;
35                         } elsif (ref $var eq "ARRAY") {
36                                 push @$var, $val;
37                         } elsif (ref $var) {
38                                 die ("Sherlock::Config::Parse: don't know how to set $o");
39                         }
40                 }
41         }
42         1;
43 }
44
45 1;  # OK