From 20bedd04d7e7fd8a9a151071a76a5ead8f098572 Mon Sep 17 00:00:00 2001 From: teastep Date: Wed, 5 Dec 2007 00:14:30 +0000 Subject: [PATCH] Named arguments to Shorewall::Compiler::compiler() git-svn-id: https://shorewall.svn.sourceforge.net/svnroot/shorewall/trunk@7826 fbd18981-670d-0410-9b5c-8dc0c1a9a2bb --- Shorewall-common/releasenotes.txt | 34 ++++++++++++++++++++-- Shorewall-perl/Shorewall/Compiler.pm | 43 ++++++++++++++++++++++++---- Shorewall-perl/Shorewall/Config.pm | 7 +++++ Shorewall-perl/compiler.pl | 30 +++++++++++-------- 4 files changed, 94 insertions(+), 20 deletions(-) diff --git a/Shorewall-common/releasenotes.txt b/Shorewall-common/releasenotes.txt index 0eaca7685..125e8ed9a 100644 --- a/Shorewall-common/releasenotes.txt +++ b/Shorewall-common/releasenotes.txt @@ -89,12 +89,42 @@ Other changes in Shorewall 4.1.2. This change implemented two new options to the Shorewall-perl compiler (/usr/share/shorewall-perl/compiler.pl). - --log= - --log_verbosity={-1|0-2} + --log= + --log_verbosity={-1|0-2} The --log option is ignored when --log_verbosity is not supplied or is supplied with value -1. + To avoid a proliferation of parameters to + Shorewall::Compiler::compile(), that function has been changed to + use named parameters. Parameter names are: + + object Object file. If omitted or '', the + configuration is syntax checked. + directory Directory. If omitted or '', configuration + files are located using + CONFIG_PATH. Otherwise, the directory named by + this parameter is searched first. + verbosity Verbosity; range -1 to 2 + timestamp 0|1 -- timestamp messages. + debug 0|1 -- include stack trace in warning/error + messages. + export 0|1 -- compile for export. + chains List of chains to be reloaded by 'refresh'. + log File to log compiler messages to. + log_verbosity Log Verbosity; range -1 to 2. + + Those parameters that are supplied must have defined values. + + Example: + + use lib '/usr/share/shorewall-perl/'; + use Shorewall::Compiler; + + compiler( object => '/root/firewall', + log => '/root/compile.log', + log_verbosity => 2 ); + 2) Previously, when HIGH_ROUTE_MARKS=Yes, Shorewall allowed non-zero mark values < 256 to be assigned in the OUTPUT chain. This has been changed so that only high mark values may be assigned diff --git a/Shorewall-perl/Shorewall/Compiler.pm b/Shorewall-perl/Shorewall/Compiler.pm index 08c89aa8d..3a1ae706d 100644 --- a/Shorewall-perl/Shorewall/Compiler.pm +++ b/Shorewall-perl/Shorewall/Compiler.pm @@ -701,12 +701,44 @@ EOF # If the first argument is non-null, it names the script file to generate. # Otherwise, this is a 'check' command and no script is produced. # -sub compiler( $$$$$$$ ) { +sub compiler { - my ( $objectfile, $directory, $verbosity, $options , $chains , $log , $log_verbosity ) = @_; + my ( $objectfile, $directory, $verbosity, $timestamp , $debug, $chains , $log , $log_verbosity ) = + ( '', '', -1, '', 0, '', '', -1 ); $export = 0; + sub edit_boolean( $ ) { + my $val = numeric_value1( shift ); + defined $val && $val >= 0 && $val < 2; + } + + sub edit_verbosity( $ ) { + my $val = numeric_value1( shift ); + defined $val && $val >= -1 && $val < 3; + } + + my %elbat = ( object => { store => \$objectfile }, + directory => { store => \$directory }, + verbosity => { store => \$verbosity , edit => \&edit_verbosity } , + timestamp => { store => \$timestamp, edit => \&edit_boolean } , + debug => { store => \$debug, edit => \&edit_boolean } , + export => { store => \$export , edit => \&edit_boolean } , + chains => { store => \$chains }, + log => { store => \$log }, + log_verbosity => { store => \$log_verbosity, edit => \&edit_verbosity } , + ); + + while ( defined ( my $name = shift ) ) { + fatal_error "Unknown parameter ($name)" unless my $ref = $elbat{$name}; + fatal_error "Undefined value supplied for parameter $name" unless defined ( my $val = shift ) ; + if ( $ref->{edit} ) { + fatal_error "Invalid value ( $val ) supplied for parameter $name" unless $ref->{edit}->($val); + } + + ${$ref->{store}} = $val; + } + reinitialize if $reused++; if ( $directory ne '' ) { @@ -714,11 +746,10 @@ sub compiler( $$$$$$$ ) { set_shorewall_dir( $directory ); } - set_verbose( $verbosity ) unless $verbosity eq ''; + set_verbose( $verbosity ); set_log($log, $log_verbosity) if $log; - $export = 1 if $options & EXPORT; - set_timestamp( 1 ) if $options & TIMESTAMP; - set_debug( 1 ) if $options & DEBUG; + set_timestamp( $timestamp ); + set_debug( $debug ); # # Get shorewall.conf and capabilities. # diff --git a/Shorewall-perl/Shorewall/Config.pm b/Shorewall-perl/Shorewall/Config.pm index f8fae3c92..6b2380fb1 100644 --- a/Shorewall-perl/Shorewall/Config.pm +++ b/Shorewall-perl/Shorewall/Config.pm @@ -55,6 +55,7 @@ our @EXPORT_OK = qw( $shorewall_dir initialize read_a_line1 set_config_path shor our %EXPORT_TAGS = ( internal => [ qw( create_temp_object finalize_object numeric_value + numeric_value1 emit emit_unindented save_progress_message @@ -510,6 +511,12 @@ sub numeric_value ( $ ) { $mark =~ /^0/ ? oct $mark : $mark; } +sub numeric_value1 ( $ ) { + my $mark = lc $_[0]; + return undef unless $mark =~ /^-?(0x[a-f0-9]+|0[0-7]*|[1-9]\d*)$/; + $mark =~ /^0/ ? oct $mark : $mark; +} + # # Write the arguments to the object file (if any) with the current indentation. # diff --git a/Shorewall-perl/compiler.pl b/Shorewall-perl/compiler.pl index d5c7763af..1e76ee577 100755 --- a/Shorewall-perl/compiler.pl +++ b/Shorewall-perl/compiler.pl @@ -42,7 +42,7 @@ use lib "$FindBin::Bin"; use Shorewall::Compiler; use Getopt::Long; -sub usage() { +sub usage( $ ) { print STDERR 'usage: compiler.pl [