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