Unified symbol table for shell variables.

Signed-off-by: Tom Eastep <teastep@shorewall.net>
This commit is contained in:
Tom Eastep 2012-12-03 17:18:21 -08:00
parent 409f5f2aab
commit 314632f1f4

View File

@ -122,6 +122,7 @@ our %EXPORT_TAGS = ( internal => [ qw( create_temp_script
qt
ensure_config_path
add_param
add_symbol
export_params
get_configuration
report_capabilities
@ -560,7 +561,10 @@ use constant { PLAIN_READ => 0, # No read_a_line options
NORMAL_READ => -1 # All options
};
my %symbols; # Symbol table for expanding shell variables
sub process_shorewallrc($$);
sub add_symbols( \% );
#
# Rather than initializing globals in an INIT block or during declaration,
# we initialize them in a function. This is done for two reasons:
@ -952,6 +956,8 @@ sub initialize( $;$$) {
SHAREDIR => '/usr/share/',
CONFDIR => '/etc/',
);
%symbols = %ENV;
#
# If we are compiling for export, process the shorewallrc from the remote system
#
@ -992,6 +998,8 @@ sub initialize( $;$$) {
}
%shorewallrc1 = %shorewallrc unless $shorewallrc1;
add_symbols %shorewallrc1;
}
my @abbr = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec );
@ -1927,12 +1935,7 @@ sub evaluate_expression( $$$ ) {
while ( $expression =~ m( ^(.*?) \$({)? (\d+|[a-zA-Z]\w*) (?(2)}) (.*)$ )x ) {
my ( $first, $var, $rest ) = ( $1, $3, $4);
$val = ( exists $ENV{$var} ? $ENV{$var} :
exists $params{$var} ? $params{$var} :
exists $config{$var} ? $config{$var} :
exists $renamed{$var} ? $config{$renamed{$var}} :
exists $shorewallrc1{$var} ? $shorewallrc1{$var} :
exists $shorewallrc{$var} ? $shorewallrc{$var} :
$val = ( exists $symbols{$var} ? $symbols{$var} :
exists $actparms{$var} ? ( $var ? $actparms{$var} : $actparms{0}->{name} ) :
exists $capdesc{$var} ? have_capability( $var ) : 0 );
$val = 0 unless defined $val;
@ -2554,17 +2557,31 @@ sub expand_variables( \$ ) {
( length( $var ) == 1 || $var !~ /^0/ ) ) );
fatal_error "Undefined parameter (\$$var)" if ( ! defined $actparms{$var} ) || ( length( $var ) > 1 && $var =~ /^0/ );
$val = $var ? $actparms{$var} : $actparms{0}->{name};
} elsif ( exists $params{$var} ) {
$val = $params{$var};
} elsif ( exists $shorewallrc1{$var} ) {
$val = $shorewallrc1{$var}
} elsif ( exists $shorewallrc{$var} ) {
$val = $shorewallrc{$var}
} elsif ( exists $symbols{$var} ) {
$val = $symbols{$var};
} elsif ( exists $actparms{$var} ) {
$val = $actparms{$var};
} else {
fatal_error "Undefined shell variable (\$$var)" unless exists $config{$var};
$val = $config{$var};
}
$val = '' unless defined $val;
$$lineref = join( '', $first , $val , $rest );
fatal_error "Variable Expansion Loop" if ++$count > 100;
}
}
sub expand_shorewallrc_variables( \$ ) {
my ( $lineref, $count ) = ( $_[0], 0 );
# $1 $2 $3 - $4
while ( $$lineref =~ m( ^(.*?) \$({)? (\d+|[a-zA-Z]\w*) (?(2)}) (.*)$ )x ) {
my ( $first, $var, $rest ) = ( $1, $3, $4);
my $val;
if ( exists $shorewallrc{$var} ) {
$val = $shorewallrc{$var}
}
$val = '' unless defined $val;
@ -2716,7 +2733,7 @@ sub process_shorewallrc( $$ ) {
if ( $currentline =~ /^([a-zA-Z]\w*)=(.*)$/ ) {
my ($var, $val) = ($1, $2);
$val = $1 if $val =~ /^\"([^\"]*)\"$/;
expand_variables($val) if supplied $val;
expand_shorewallrc_variables($val) if supplied $val;
$shorewallrc{$var} = $val;
} else {
fatal_error "Unrecognized shorewallrc entry";
@ -4329,18 +4346,38 @@ sub get_params() {
}
}
}
add_symbols %params;
}
#
# Add an entry to %params and to %compiler_params
# Add an entry to %param, %symbols and to %compiler_params
#
sub add_param( $$ ) {
my ( $param, $value ) = @_;
$params{$param} = $value;
$params{$param} = $value;
$symbols{$param} = $value;
$compiler_params{$param} = 1;
}
#
# Add an entry to %symbols
#
sub add_symbol( $$ ) {
$symbols{$_[0]} = $_[1];
}
#
# Add symbols from a hash
#
sub add_symbols( \% ) {
while ( my ( $var, $val ) = each %{$_[0]} ) {
$symbols{$var} = $val;
}
}
#
# emit param=value for each param set in the params file
#
@ -4914,8 +4951,13 @@ sub get_configuration( $$$ ) {
require_capability( 'RECENT_MATCH' , 'MACLIST_TTL' , 's' ) if $config{MACLIST_TTL};
require_capability( 'XCONNMARK' , 'HIGH_ROUTE_MARKS=Yes' , 's' ) if $config{PROVIDER_OFFSET} > 0;
require_capability( 'MANGLE_ENABLED' , 'Traffic Shaping' , 's' ) if $config{TC_ENABLED};
}
add_symbols %config;
while ( my ($var, $val ) = each %renamed ) {
$symbols{$var} = $config{$val};
}
}
#
# The values of the options in @propagateconfig are copied to the script file in OPTION=<value> format.
#