]> mj.ucw.cz Git - libucw.git/blob - lib/perl/Config.pm
Readding line of makefile lost in merge
[libucw.git] / lib / perl / Config.pm
1 #       Perl module for parsing Sherlock configuration files (using the config utility)
2 #
3 #       (c) 2002--2005 Martin Mares <mj@ucw.cz>
4 #
5 #       This software may be freely distributed and used according to the terms
6 #       of the GNU Lesser General Public License.
7
8 package UCW::Config;
9
10 use strict;
11 use warnings;
12 use Getopt::Long;
13
14 our %Sections = ();
15
16 our $DefaultConfigFile = "";
17 our $Usage = "-C, --config filename   Override the default configuration file
18 -S, --set sec.item=val  Manual setting of a configuration item";
19
20
21 sub Parse(@) {
22         my @options = @_;
23         my $defargs = "";
24         my $override_config = 0;
25         push @options, "config|C=s" => sub { my ($o,$a)=@_; $defargs .= " -C'$a'"; $override_config=1; };
26         push @options, "set|S=s" => sub { my ($o,$a)=@_; $defargs .= " -S'$a'"; };
27         Getopt::Long::Configure("bundling");
28         Getopt::Long::GetOptions(@options) or return 0;
29         if (!$override_config && $DefaultConfigFile) {
30                 $defargs = "-C'$DefaultConfigFile' $defargs";
31         }
32         foreach my $section (keys %Sections) {
33                 my $opts = $Sections{$section};
34                 my $optlist = join(";", keys %$opts);
35                 my %filtered_opts = map { my $t=$_; $t=~s/[#\$]+$//; $t => $$opts{$_} } keys %$opts;
36                 my @l = `bin/config $defargs "$section\{$optlist\}"`;
37                 $? && exit 1;
38                 foreach my $o (@l) {
39                         $o =~ /^CF_.*_([^=]+)='(.*)'\n$/ or die "Cannot parse bin/config output: $_";
40                         my $var = $filtered_opts{$1};
41                         my $val = $2;
42                         if (ref $var eq "SCALAR") {
43                                 $$var = $val;
44                         } elsif (ref $var eq "ARRAY") {
45                                 push @$var, $val;
46                         } elsif (ref $var) {
47                                 die ("UCW::Config::Parse: don't know how to set $o");
48                         }
49                 }
50         }
51         1;
52 }
53
54 1;  # OK