Skip to content

Commit

Permalink
Deal with backreferences
Browse files Browse the repository at this point in the history
  • Loading branch information
jcameron committed Apr 28, 2012
1 parent 985edd1 commit 6df133f
Showing 1 changed file with 34 additions and 5 deletions.
39 changes: 34 additions & 5 deletions dovecot/dovecot-lib.pl
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ sub get_config
return \@get_config_cache;
}

# read_config_file(filename)
# read_config_file(filename, [&include-parent-rv])
# Convert a file into a list od directives
sub read_config_file
{
local ($file) = @_;
local ($file, $incrv) = @_;
local $filedir = $file;
$filedir =~ s/\/[^\/]+$//;
local $lnum = 0;
Expand Down Expand Up @@ -92,6 +92,32 @@ sub read_config_file
push(@{$section->{'members'}}, $dir);
$section->{'eline'} = $lnum;
}

# Fix up references to other variables
my @w = split(/\s+/, $dir->{'value'});
my $changed;
foreach my $w (@w) {
if ($w =~ /^\$(\S+)/) {
my $var = $1;
my ($prev) = grep { $_->{'name'} eq $var } @rv;
if (!$prev && $incrv) {
($prev) = grep { $_->{'name'} eq $var }
@$incrv;
}
if ($prev) {
$w = $prev->{'value'};
$changed = 1;
}
else {
$w = undef;
$changed = 1;
}
}
}
if ($changed) {
@w = grep { defined($_) } @w;
$dir->{'value'} = join(" ", @w);
}
push(@rv, $dir);
}
elsif (/^\s*!(include|include_try)\s+(\S+)/) {
Expand All @@ -101,7 +127,7 @@ sub read_config_file
$glob = $filedir."/".$glob;
}
foreach my $i (glob($glob)) {
push(@rv, &read_config_file($i));
push(@rv, &read_config_file($i, \@rv));
}
}
$lnum++;
Expand All @@ -123,7 +149,7 @@ sub find
@rv = grep { $_->{'sectionname'} eq $sname &&
$_->{'sectionvalue'} eq $svalue } @rv;
}
return wantarray ? @rv : $rv[0];
return wantarray ? @rv : $rv[$#rv];
}

# find_value(name, &config, [disabled-mode], [sectionname], [sectionvalue])
Expand All @@ -134,8 +160,11 @@ sub find_value
if (wantarray) {
return map { $_->{'value'} } @rv;
}
elsif (!@rv) {
return undef;
}
else {
return $rv[0]->{'value'};
return $rv[$#rv]->{'value'};
}
}

Expand Down

0 comments on commit 6df133f

Please sign in to comment.