aboutsummaryrefslogtreecommitdiff
path: root/lib/Config/Parser/Ini.pm
blob: 0ea632c984e099cebecf376a8eec6fe8beb8ab86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package Config::Parser::Ini;
use strict;
use warnings;
use parent 'Config::Parser';
use Carp;
use Text::ParseWords;
    
sub parse {
    my $self = shift;
    $self->{_filename} = shift // confess "No filename given";
    local %_ = @_;
    $self->debug(1, "parsing $self->{_filename}");
    $self->_readconfig($self->{_filename}, %_);
    return $self;
}

sub filename { shift->{_filename} }

# _readconfig(FILE)
sub _readconfig {
    my $self = shift;
    my $file = shift;
    local %_ = @_;
    my $fh = delete $_{fh};
    my $need_close;

    $self->debug(1, "reading file $file");
    unless ($fh) {
	open($fh, "<", $file)
	    or do {
		$self->error("can't open configuration file $file: $!");
		$self->{_error_count}++;
		return 0;
            };
	$need_close = 1;
    }
    
    my $line = delete $_{line} // 0;
    my @path;
    my $include;
    
    while (<$fh>) {
	++$line;
	chomp;
	if (/\\$/) {
	    chop;
	    $_ .= <$fh>;
	    redo;
	}

	s/^\s+//;
	s/\s+$//;
	s/#.*//;
	next if ($_ eq "");
	
	my $locus = new Text::Locus($file, $line);
	
	if (/^\[(.+?)\]$/) {
	    @path = parse_line('\s+', 0, $1);
	    if (@path == 1 && $path[0] eq 'include') {
		$include = 1;
	    } else {
		$include = 0;
		$self->add_node(\@path,
			new Config::AST::Node::Section(locus => $locus)); 
	    }
	} elsif (/([\w_-]+)\s*=\s*(.*)/) {
	    my ($k, $v) = ($1, $2);
	    $k = lc($k) if $self->{_ci}; #FIXME:private member
	    
	    if ($include) {
		if ($k eq 'path') {
		    $self->_readconfig($v);
		} elsif ($k eq 'pathopt') {
		    $self->_readconfig($v) if -f $v;
		} elsif ($k eq 'glob') {
		    foreach my $file (bsd_glob($v, 0)) {
			$self->_readconfig($file);
		    }
		} else {
		    $self->error("keyword \"$k\" is unknown", locus => $locus);
		    $self->{_error_count}++;
		}
	    } else {
		$self->add_value([@path, $k], $v, $locus);
	    }
	} else {
    	    $self->error("malformed line", locus => $locus);
	    $self->{_error_count}++;
	}
    }
    close $fh if $need_close;
}

1;

Return to:

Send suggestions and report system problems to the System administrator.