diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2017-12-06 17:10:28 +0200 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2017-12-06 22:06:01 +0200 |
commit | 7cb9b8ae0c32cedad537042b969d2762d3cafbac (patch) | |
tree | 4ec6e6d8adbaf31c70098d259df5f0b878278b06 | |
parent | a1fa6c3258ef57056410e247c374ece583158515 (diff) | |
download | acpp-7cb9b8ae0c32cedad537042b969d2762d3cafbac.tar.gz acpp-7cb9b8ae0c32cedad537042b969d2762d3cafbac.tar.bz2 |
Rewrite in a modular fashion
* lib/Apache/Admin/Config/Expand.pm: Rewrite.
* lib/Apache/Admin/Config/Expand/compact.pm: New file.
* lib/Apache/Admin/Config/Expand/include.pm: New file.
-rw-r--r-- | MANIFEST.SKIP | 5 | ||||
-rw-r--r-- | lib/Apache/Admin/Config/Expand.pm | 118 | ||||
-rw-r--r-- | lib/Apache/Admin/Config/Expand/compact.pm | 17 | ||||
-rw-r--r-- | lib/Apache/Admin/Config/Expand/include.pm | 54 |
4 files changed, 126 insertions, 68 deletions
diff --git a/MANIFEST.SKIP b/MANIFEST.SKIP index 859ca13..58696be 100644 --- a/MANIFEST.SKIP +++ b/MANIFEST.SKIP @@ -57,4 +57,7 @@ ^debug.sh ^tmp ^buildreq -^\.emacs\.*
\ No newline at end of file +^\.emacs\.* + +\.tar$ +\.tar\.gz$ diff --git a/lib/Apache/Admin/Config/Expand.pm b/lib/Apache/Admin/Config/Expand.pm index 52cd929..ae57be0 100644 --- a/lib/Apache/Admin/Config/Expand.pm +++ b/lib/Apache/Admin/Config/Expand.pm @@ -3,7 +3,6 @@ use parent 'Apache::Admin::Config'; use strict; use warnings; use Carp; -use File::Spec; our $VERSION = '1.0'; @@ -11,14 +10,25 @@ sub new { my $class = shift; my $server_root = Apache::Admin::Config::Tree::_get_arg(\@_, '-server_root'); - my $compact = Apache::Admin::Config::Tree::_get_arg(\@_, '-compact!'); - my $conf = $class->SUPER::new(@_); - return unless $conf; - my $self = bless $conf, $class; + my $explist = Apache::Admin::Config::Tree::_get_arg(\@_, '-expand') + || [ qw(include) ]; + if (Apache::Admin::Config::Tree::_get_arg(\@_, '-compact!')) { + push @$explist, 'compact' + } + + my $self = $class->SUPER::new(@_) or return; + bless $self, $class; $self->{_options} = \@_; $self->{_server_root} = $server_root; - return unless $self->_preproc; - $self->_compact if $compact; + + eval { + return unless $self->_preproc($explist); + }; + if ($@) { + $Apache::Admin::Config::ERROR = $@; + return; + } + return $self; } @@ -26,86 +36,60 @@ sub server_root { my $self = shift; unless ($self->{_server_root}) { if (my $d = $self->directive('ServerRoot')) { - $self->{_server_root} = _dequote($d->value); + $self->{_server_root} = $self->dequote($d->value); } } return $self->{_server_root}; } -sub _preproc { - my $self = shift; - - for (my $i = 0; my $d = $self->select(-which => $i); ) { - if ($d->type eq 'directive' && $d->name =~ /^include(optional)?$/i) { - return unless $self->_include($d, $1); - $d->unlink; - } else { - $i++; - } - } - delete $self->{_included}; - return 1; -} - -sub _dequote { - my ($str) = @_; +sub dequote { + my ($self, $str) = @_; if ($str =~ s/^"(.*)"$/$1/) { $str =~ s/\\"/"/g; } return $str; } -sub _check_included { - my ($self, $file) = @_; - my ($dev,$ino) = stat($file) or return 0; - return 1 if $self->{_included}{$dev}{$ino}; - $self->{_included}{$dev}{$ino} = 1; - return 0; +sub options { shift->{_options} } + +sub _preproc { + my ($self, $explist) = @_; + + return 1 unless @$explist; + + return $self->_preproc_section($self, + [ map { + my $mod = 'Apache::Admin::Config::Expand::'.$_; + (my $file = $mod) =~ s|::|/|g; + require $file . '.pm'; + $mod->new + } @$explist ]); } -sub _include { - my ($self, $d, $opt) = @_; - my $pat = _dequote($d->value); - unless (File::Spec->file_name_is_absolute($pat)) { - if (my $d = $self->server_root) { - $pat = File::Spec->catfile($d, $pat); - } - } - my @filelist = glob $pat; - if (@filelist) { - foreach my $file (@filelist) { - if ($self->_check_included($file)) { - return $self->_set_error("file $file already included"); - } - if (my $inc = new Apache::Admin::Config($file, - @{$self->{_options}})) { - while (my $obj = $inc->select(-which => 0)) { - $obj->move($self, -after => $d); - $d = $obj; +sub _preproc_section { + my ($self, $section, $modlist) = @_; + + OUTER: + for (my $i = 0; defined(my $d = $section->select(-which => $i)); ) { + foreach my $mod (@$modlist) { + my @repl; + if ($mod->expand($self, $d, \@repl)) { + my $prev = $d; + foreach my $r (@repl) { + $prev = $section->add($r, -after => $prev); } - } else { - return 0; + $d->unlink; + next OUTER; + } + if ($d->type eq 'section') { + $self->_preproc_section($d, $modlist); } } - } elsif (!$opt) { - return $self->_set_error("no files matching $pat"); + $i++; } return 1; } -sub _compact { - my $self = shift; - for (my $i = 0; my $d = $self->select(-which => $i); ) { - if ($d->type eq 'blank' || $d->type eq 'comment') { - $d->delete; - } else { - if ($d->type eq 'section') { - _compact($d); - } - $i++; - } - } -} 1; __END__ diff --git a/lib/Apache/Admin/Config/Expand/compact.pm b/lib/Apache/Admin/Config/Expand/compact.pm new file mode 100644 index 0000000..fd648d0 --- /dev/null +++ b/lib/Apache/Admin/Config/Expand/compact.pm @@ -0,0 +1,17 @@ +package Apache::Admin::Config::Expand::compact; +use strict; +use warnings; + +sub new { + bless {}, shift +} + +sub expand { + my ($self, $tree, $d, $repl) = @_; + return $d->type eq 'blank' || $d->type eq 'comment'; +} + +1; + + + diff --git a/lib/Apache/Admin/Config/Expand/include.pm b/lib/Apache/Admin/Config/Expand/include.pm new file mode 100644 index 0000000..a3d638c --- /dev/null +++ b/lib/Apache/Admin/Config/Expand/include.pm @@ -0,0 +1,54 @@ +package Apache::Admin::Config::Expand::include; +use strict; +use warnings; +use Apache::Admin::Config; +use Apache::Admin::Config::Expand; +use File::Spec; +use Carp; + +sub new { + bless { included => {} }, shift +} + +sub expand { + my ($self, $tree, $d, $repl) = @_; + + if ($d->type eq 'directive' && $d->name =~ /^include(optional)?$/i) { + my $optional = $1; + + my $pat = $tree->dequote($d->value); + unless (File::Spec->file_name_is_absolute($pat)) { + if (my $d = $tree->server_root) { + $pat = File::Spec->catfile($d, $pat); + } + } + + my @filelist = glob $pat; + if (@filelist) { + foreach my $file (@filelist) { + if ($self->check_included($file)) { + croak "file $file already included"; + } + if (my $inc = new Apache::Admin::Config($file, + @{$tree->options})) { + push @$repl, $inc->select; + } else { + croak $Apache::Admin::Config::ERROR; + } + } + } + return 1; + } + + return 0; +} + +sub check_included { + my ($self, $file) = @_; + my ($dev,$ino) = stat($file) or return 0; + return 1 if $self->{included}{$dev}{$ino}; + $self->{included}{$dev}{$ino} = 1; + return 0; +} + +1; |