]> mj.ucw.cz Git - libucw.git/commitdiff
Added a Perl module for parsing options and configuration.
authorMartin Mares <mj@ucw.cz>
Fri, 23 Aug 2002 08:29:59 +0000 (08:29 +0000)
committerMartin Mares <mj@ucw.cz>
Fri, 23 Aug 2002 08:29:59 +0000 (08:29 +0000)
lib/perl/Config.pm [new file with mode: 0644]
lib/perl/Makefile [new file with mode: 0644]

diff --git a/lib/perl/Config.pm b/lib/perl/Config.pm
new file mode 100644 (file)
index 0000000..bf013b7
--- /dev/null
@@ -0,0 +1,39 @@
+# Perl module for parsing Sherlock configuration files (using the config utility)
+# (c) 2002 Martin Mares <mj@ucw.cz>
+
+package Sherlock::Config;
+
+use strict;
+use warnings;
+use Getopt::Long;
+
+our %Sections = ();
+
+sub Parse(@) {
+       my @options = @_;
+       my $defargs = "";
+       push @options, "config|C=s" => sub { my ($o,$a)=@_; $defargs .= " -C'$a'"; };
+       push @options, "set|S=s" => sub { my ($o,$a)=@_; $defargs .= " -S'$a'"; };
+       Getopt::Long::GetOptions(@options) or return 0;
+       foreach my $section (keys %Sections) {
+               my $opts = $Sections{$section};
+               my $optlist = join(" ", keys %$opts);
+               my @l = `bin/config $defargs $section $optlist`;
+               $? && exit 1;
+               foreach my $o (@l) {
+                       $o =~ /^CF_([^=]+)="(.*)"\n$/ or die "Cannot parse bin/config output: $_";
+                       my $var = $$opts{$1};
+                       my $val = $2;
+                       if (ref $var eq "SCALAR") {
+                               $$var = $val;
+                       } elsif (ref $var eq "ARRAY") {
+                               push @$var, $val;
+                       } elsif (ref $var) {
+                               die ("Sherlock::Config::Parse: don't know how to set $o");
+                       }
+               }
+       }
+       1;
+}
+
+1;  # OK
diff --git a/lib/perl/Makefile b/lib/perl/Makefile
new file mode 100644 (file)
index 0000000..580319a
--- /dev/null
@@ -0,0 +1,4 @@
+# Perl modules
+
+DIRS+=lib/perl
+PROGS+=obj/lib/perl/Config.pm