aboutsummaryrefslogtreecommitdiff
path: root/lib/Config/Parser/Ini.pm
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Config/Parser/Ini.pm')
-rw-r--r--lib/Config/Parser/Ini.pm96
1 files changed, 96 insertions, 0 deletions
diff --git a/lib/Config/Parser/Ini.pm b/lib/Config/Parser/Ini.pm
new file mode 100644
index 0000000..b3a1ef9
--- /dev/null
+++ b/lib/Config/Parser/Ini.pm
@@ -0,0 +1,96 @@
1package Config::Parser::Ini;
2use strict;
3use warnings;
4use parent 'Config::Parser';
5use Carp;
6use Text::ParseWords;
7
8sub parse {
9 my $self = shift;
10 $self->{_filename} = shift // confess "No filename given";
11 local %_ = @_;
12 $self->debug(1, "parsing $self->{_filename}");
13 $self->_readconfig($self->{_filename}, %_);
14 return $self;
15}
16
17sub filename { shift->{_filename} }
18
19# _readconfig(FILE)
20sub _readconfig {
21 my $self = shift;
22 my $file = shift;
23 local %_ = @_;
24 my $fh = delete $_{fh};
25 my $need_close;
26
27 $self->debug(1, "reading file $file");
28 unless ($fh) {
29 open($fh, "<", $file)
30 or do {
31 $self->error("can't open configuration file $file: $!");
32 $self->{_error_count}++;
33 return 0;
34 };
35 $need_close = 1;
36 }
37
38 my $line = delete $_{line} // 0;
39 my @path;
40 my $include;
41
42 while (<$fh>) {
43 ++$line;
44 chomp;
45 if (/\\$/) {
46 chop;
47 $_ .= <$fh>;
48 redo;
49 }
50
51 s/^\s+//;
52 s/\s+$//;
53 s/#.*//;
54 next if ($_ eq "");
55
56 my $locus = new Config::Tree::Locus($file, $line);
57
58 if (/^\[(.+?)\]$/) {
59 @path = parse_line('\s+', 0, $1);
60 if (@path == 1 && $path[0] eq 'include') {
61 $include = 1;
62 } else {
63 $include = 0;
64 $self->add_node(\@path,
65 new Config::Tree::Node::Section(locus => $locus));
66 }
67 } elsif (/([\w_-]+)\s*=\s*(.*)/) {
68 my ($k, $v) = ($1, $2);
69 $k = lc($k) if $self->{_ci}; #FIXME:private member
70
71 if ($include) {
72 if ($k eq 'path') {
73 $self->_readconfig($v);
74 } elsif ($k eq 'pathopt') {
75 $self->_readconfig($v) if -f $v;
76 } elsif ($k eq 'glob') {
77 foreach my $file (bsd_glob($v, 0)) {
78 $self->_readconfig($file);
79 }
80 } else {
81 $self->error("keyword \"$k\" is unknown", locus => $locus);
82 $self->{_error_count}++;
83 }
84 } else {
85 $self->add_value([@path, $k], $v, $locus);
86 }
87 } else {
88 $self->error("malformed line", locus => $locus);
89 $self->{_error_count}++;
90 }
91 }
92 close $fh if $need_close;
93}
94
951;
96

Return to:

Send suggestions and report system problems to the System administrator.