BIND-Conf_Parser
view release on metacpan or search on metacpan
lib/BIND/Conf_Parser.pm view on Meta::CPAN
next
}
if ($self->{_data} eq "controls") {
$self->parse_controls;
next
}
if ($self->{_data} eq "server") {
$self->parse_server;
next
}
if ($self->{_data} eq "trusted-keys") {
$self->parse_trusted_keys;
next
}
if ($self->{_data} eq "zone") {
$self->parse_zone;
next
}
$self->choke("Unknown configuration entry `", $self->{_data}, "' at ",
$self->where);
}
$self
}
# The external entry points
sub new {
my $class = shift;
my $self = { };
bless $self, $class;
$self
}
sub parse_file {
my $self = shift;
$self = $self->new unless ref $self;
$self->open_file(@_);
$self->{_line} = 0;
$self->parse_conf;
}
sub parse_fh {
my $self = shift;
$self = $self->new unless ref $self;
$self->{_fh} = shift;
$self->{_file} = @_ ? shift : "a file handle";
$self->{_line} = 0;
$self->parse_conf;
}
sub parse {
require IO::Scalar;
my $self = shift;
my $scalar = shift;
$self = $self->new unless ref $self;
$self->{_fh} = IO::Scalar->new(\$scalar);
$self->{_file} = @_ ? shift : "a scalar";
$self->{_line} = 0;
$self->parse_conf;
}
# The callbacks
sub handle_logging_category {}; # $name, \@names
sub handle_logging_channel {}; # $name, \%options
sub handle_key {}; # $name, $algo, $secret
sub handle_acl {}; # $name, $addrmatchlist
sub handle_option {}; # $option, $argument
sub handle_server {}; # $name, \%options
sub handle_trusted_key {}; # $domain, [ $flags, $proto, $algo, $keydata ]
sub handle_empty_zone {}; # $name, $class, \%options
sub handle_zone {}; # $name, $class, $type, \%options
sub handle_control {}; # $socket_type, \@type_specific_data
1;
__END__
=head1 NAME
BIND::Conf_Parser - Parser class for BIND configuration files
=head1 SYNOPSIS
# Should really be a subclass
use BIND::Conf_Parser;
$p = BIND::Conf_Parser->new;
$p->parse_file("/etc/named.conf");
$p->parse_fh(STDIN);
$p->parse("server 10.0.0.1 { bogus yes; };");
# For one-shot parsing
BIND::Conf_Parser->parse_file("/etc/named.conf")
BIND::Conf_Parser->parse_fh(STDIN);
BIND::Conf_Parser->parse("server 10.0.0.1 { bogus yes; };");
=head1 DESCRIPTION
C<BIND::Conf_Parser> implements a virtual base class for parsing BIND
(Berkeley Internet Name Domain) server version 8 configuration files
("named.conf"). The parsing methods shown in the synopsis perform
syntactic analysis only. As each meaningful semantic 'chunk' is
parsed, a callback method is invoked with the parsed information.
The following methods are the public entry points for the base class:
=over 4
=item $p = BIND::Conf_Parser->new
The object constructor takes no arguments.
=item $p->parse_file( $filename )
The given filename is parsed in its entirety.
=item $p->parse_fh( $fh [, $filename] )
The given filehandle is parsed in its entirety. An optional filename
may be given for inclusion in any error messages that are generated
during the parsing. If it is not included a default of "a file handle"
will be used.
=item $p->parse( $statements [, $filename] );
lib/BIND/Conf_Parser.pm view on Meta::CPAN
=item $self->handle_empty_zone( $name, $class, \%options )
=item $self->handle_zone( $name, $class, $type, \%options )
=item $self->handle_control( $socket_type, \@type_specific_data )
=back
The exact format of the data passed to the above routines is not
currently documented outside of the source to the class, but should be
found to be fairly natural.
=head1 USAGE
A typical usage would run something like:
# Define a subclass
package Parser;
use BIND::Conf_Parser;
use vars qw(@ISA);
@ISA = qw(BIND::Conf_Parser);
# implement handle_* methods for config file statements that
# we're interested in
sub handle_option {
my($self, $option, $argument) = @_;
return unless $option eq "directory";
$named_dir = $argument;
}
sub handle_zone {
my($self, $name, $class, $type, $options) = @_;
return unless $type eq "master" && $class eq "in";
$files{$name} = $options->{file};
}
# later, back at the ranch...
package main;
Parser->parse_file("/etc/named.conf");
I<WARNING:> if the subclass is defined at the end of the main program
source file, the assignment to I<@ISA> may need to be wrapped in a
C<BEGIN> block, ala
BEGIN {
@ISA = qw(BIND::Conf_Parser);
}
=head1 BUGS
C<BIND::Conf_Parser> does not perform all the syntactic checks
performed by the parser in F<named> itself. For example, port numbers
are not verified to be positive intergers in the range 0 to 65535.
The parse() method cannot be called multiple times with parts of
statements.
Comments are not passed to a callback method.
Some callbacks are invoked before the semicolon that terminates the
corresponding syntactic form is actually recognized. It is therefore
possible for a syntax error to not be detected until after a callback
is invoked for the presumably completly parsed form. No attempt is
made to delay the invocation of callbacks to the completion of toplevel
statements.
=head1 NOTE
This version of C<BIND::Conf_Parser> corresponds to BIND version 8.2.2
and understands the statements, options, and forms of that version.
Since the BIND developers have only made upward compatible changes to
the syntax, C<BIND::Conf_Parser> will correctly parse valid config files
for previous versions of BIND.
A C<BIND::Conf_Parser> object is a blessed anonymous hash. In an
attempt to prevent modules trampling on each other I propose that any
subclass that requires persistant state between calls to the callback
routines (handle_foo()) and other subclass methods should prefix its
keys names with its own name separated by _'s. For example, a
hypothetical C<BIND::Conf_Parser::Keys> module would keep data under
keys that started with 'bind_conf_parser_keys_', e.g.,
'bind_conf_parser_keys_key_count'. The 'state' key is reserved for use
by application specific one-shot parsers (this is expected to encompass
most uses of C<BIND::Conf_Parser>). C<BIND::Conf_Parser> reserves for
itself all keys beginning with an underbar.
=head1 COPYRIGHT
Copyright 1998-1999 Philip Guenther. All rights reserved.
This library is free software; you can redistribute it and/or This
program is free software; redistribution and modification in any form
is explicitly permitted provided that all versions retain this
copyright notice and the following disclaimer.
This library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
=cut
( run in 1.908 second using v1.01-cache-2.11-cpan-140bd7fdf52 )