Apache-ConfigParser
view release on metacpan or search on metacpan
lib/Apache/ConfigParser.pm view on Meta::CPAN
This method takes a filename and adds it to the already loaded
configuration file inside the object. If a previous Apache
configuration file was loaded either with new or parse_file and the
configuration file did not close all of its contexts, such as
<VirtualHost>, then the new configuration directives and contexts in
C<$filename> will be added to the existing context.
If there is a failure in parsing any portion of the configuration
file, then this method returns undef and C<< $c->errstr >> will contain a
string explaining the error.
=cut
sub parse_file {
unless (@_ == 2) {
confess "$0: Apache::ConfigParser::parse_file $INCORRECT_NUMBER_OF_ARGS";
}
my ($self, $file_or_dir_name) = @_;
my @stat = stat($file_or_dir_name);
unless (@stat) {
$self->{errstr} = "cannot stat '$file_or_dir_name': $!";
return;
}
# If this is a real directory, than descend into it now.
if (-d _) {
unless (opendir(DIR, $file_or_dir_name)) {
$self->{errstr} = "cannot opendir '$file_or_dir_name': $!";
return;
}
my @entries = sort grep { $_ !~ /^\.{1,2}$/ } readdir(DIR);
unless (closedir(DIR)) {
$self->{errstr} = "closedir '$file_or_dir_name' failed: $!";
return;
}
my $ok = 1;
foreach my $entry (@entries) {
$ok = $self->parse_file("$file_or_dir_name/$entry") && $ok;
next;
}
if ($ok) {
return $self;
} else {
return;
}
}
# Create a new file handle to open this file and open it.
my $fd = gensym;
unless (open($fd, $file_or_dir_name)) {
$self->{errstr} = "cannot open '$file_or_dir_name' for reading: $!";
return;
}
# Change the mode to binary to mode to handle the line continuation
# match [^\\]\\[\r]\n. Since binary files may be copied from
# Windows to Unix, look for this exact match instead of relying upon
# the operating system to convert \r\n to \n.
binmode($fd);
# This holds the contents of any previous lines that are continued
# using \ at the end of the line. Also keep track of the line
# number starting a continued line for warnings.
my $continued_line = '';
my $line_number = undef;
# Scan the configuration file. Use the file format specified at
#
# http://httpd.apache.org/docs/configuring.html#syntax
#
# In addition, use the semantics from the function ap_cfg_getline
# in util.c
# 1) Leading whitespace is first skipped.
# 2) Configuration files are then parsed for line continuation. The
# line continuation is [^\\]\\[\r]\n.
# 3) If a line continues onto the next line then the line is not
# scanned for comments, the comment becomes part of the
# continuation.
# 4) Leading and trailing whitespace is compressed to a single
# space, but internal space is preserved.
while (<$fd>) {
# Apache is not consistent in removing leading whitespace
# depending upon the particular method in getting characters from
# the configuration file. Remove all leading whitespace.
s/^\s+//;
next unless length $_;
# Handle line continuation. In the case where there is only one \
# character followed by the end of line character(s), then the \
# needs to be removed. In the case where there are two \
# characters followed by the end of line character(s), then the
# two \'s need to be replaced by one.
if (s#(\\)?\\\r?\n$##) {
if ($1) {
$_ .= $1;
} else {
# The line is being continued. If this is the first line to
# be continued, then note the starting line number.
unless (length $continued_line) {
$line_number = $.;
}
$continued_line .= $_;
next;
}
} else {
# Remove the end of line characters.
s#\r?\n$##;
}
# Concatenate the continuation lines with this line. Only update
# the line number if the lines are not continued.
if (length $continued_line) {
$_ = "$continued_line $_";
$continued_line = '';
} else {
$line_number = $.;
( run in 2.231 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )