From a5523ff7e70f3386b4814c2157230c2a0034f2cb Mon Sep 17 00:00:00 2001 From: b Date: Thu, 21 Sep 2023 20:55:17 +0000 Subject: [PATCH] Conditional statements. --- configure.1.pl | 109 +++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 93 insertions(+), 16 deletions(-) diff --git a/configure.1.pl b/configure.1.pl index c452067..806f3cc 100755 --- a/configure.1.pl +++ b/configure.1.pl @@ -296,6 +296,31 @@ # $0 - the name of the setting # More functions and values can be added in future versions. # +# The settings can be conditional thanks to using statements like: +# if +# else +# endif +# +# Anything after the "if" keyword is the codition to check. +# Condition is considered true if its value is not an empty string +# Condition is parsed the same way as a dynamic setting definition +# +# Conditional statements must start at from first column in the line. +# Conditional statements can be nested +# +# Example: +# s1: 1 +# if $s1 +# s3 = $s1 +# if $s2 +# s3 = $s2 +# endif +# else +# s3: 0 +# endif +# Here the value of s3 is '1'. +# +# # Inserting settings to source files: # # After all settings are read, the actual source is processed line by line. @@ -980,6 +1005,8 @@ sub parse_file { my $parse_mode = 0; my $name = ''; my $value = ''; + my $if_depth = 0; + my $if_block = 0; while (defined(my $line = <$file>)) { $line =~ s/[\r\n]//g; @@ -989,40 +1016,90 @@ sub parse_file { # new definition name: value if ($line =~ /^([A-Za-z0-9_\-\.]+):[ \t](.*)$/) { $parse_mode = 0; - $name = $1; - $value = $2; - $cfg{$name} = ''; - print_debug($depth, "STATIC DEFINE $name=$value") + unless ($if_block) { + $name = $1; + $value = $2; + $cfg{$name} = ''; + print_debug($depth, "STATIC DEFINE $name=$value") + } } # new definition name = value elsif ($line =~ /^([A-Za-z0-9_\-\.]+)[ \t]*=(.*)$/) { $parse_mode = 1; - $name = $1; - $value = $2; - $cfg{$name} = ''; - print_debug($depth, "DYNAMIC DEFINE $name=$value") + unless ($if_block) { + $name = $1; + $value = $2; + $cfg{$name} = ''; + print_debug($depth, "DYNAMIC DEFINE $name=$value") + } } # continued definition elsif ($line =~ /^[ \t](.*)$/) { - $value = "\n".$1; - print_debug($depth, "CONTINUE $name $value") + unless ($if_block) { + $value = "\n".$1; + print_debug($depth, "CONTINUE $name $value") + } } # include file - elsif ($line =~ /^include[ \t]+([^ \t](.*[^ \t])?)[ \t]*$/) - { - my $path = parse_value($1, $depth+1, %cfg); - print_debug($depth, "INCLUDE $path"); - %cfg = parse_file($path, $encoding, $depth+1, %cfg); + elsif ($line =~ /^include[ \t]+([^ \t](.*[^ \t])?)[ \t]*$/) { + unless ($if_block) { + my $path = parse_value($1, $depth+1, %cfg); + print_debug($depth, "INCLUDE $path"); + %cfg = parse_file($path, $encoding, $depth+1, %cfg); + $name = ''; + $value = ''; + } + } + # if + elsif ($line =~ /^if[ \t]+([^ \t](.*[^ \t])?)[ \t]*$/) { + $if_depth += 1; + unless ($if_block) { + my $cond = parse_value($1, $depth+1, %cfg); + print_debug($depth, "IF.$if_depth $cond"); + unless ($cond ne '') { + $if_block = $if_depth; + } + } + $name = ''; + $value = ''; + } + # endif + elsif ($line =~ /^endif([ \t].*)?$/) { + my $_if_depth = $if_depth; + if ($if_depth > 0) { + $if_depth -= 1; + } + if ($if_depth < $if_block) { + $if_block = 0; + } + unless ($if_block) { + print_debug($depth, "ENDIF.$_if_depth"); + } $name = ''; $value = ''; } + # else + elsif ($line =~ /^else([ \t].*)?$/) { + unless ($if_block) { + $if_block = $if_depth; + print_debug($depth, "ELSE.$if_depth"); + } + elsif ($if_depth == $if_block) { + $if_block = 0; + print_debug($depth, "ELSE.$if_depth"); + } + $name = ''; + $value = ''; + } + # no el(s)if at this point + # other line, not understood else { $name = ''; $value = ''; } - if ($name ne '') { + if (($name ne '') and (not $if_block)) { if ($parse_mode != 0) { $value = parse_value($value, $depth+1, %cfg); } -- 2.30.2