From 58c3b6b9c5e2335abc80c33f9dd9ccb95a90a9f9 Mon Sep 17 00:00:00 2001 From: "Kyle J. McKay" Date: Fri, 29 Jan 2021 05:34:10 -0700 Subject: [PATCH] make-apache-conf.sh: enhance and tidy up Improve the apache.conf.in processor in myriad tiny ways. * Correct the line number in the #line directive to match the script * Make the IfDefine substitutions understand 0 and non-0 literal numbers * Make @@var_online_cpus@@ available for substitution * Simplify the @@@@ replacement pattern * Introduce a new @@if()@@...@@endif@@ conditional substitution Up until now, conditional parts of the configuration have been handled by [ab]using the Apache mechanism and inserting a variable name that's somewhat descriptive yet unlikely to actually exist in the environment. While this works just fine, it's a bit of a kludge and can be a bit perplexing to immediately understand the semantics when looking at the processed output. However, it does have the advantage of being usable to any nesting depth since Apache itself handles the nested parsing. With the new @@if()@@...@@endif@@ substitution, if the test fails (the test may optionally start with a '!' to negate it and then it's either a Girocco::Config variable name or a 0 or non-0 literal number), the contained lines are simply commented out in the processed output. In either case, the test itself always passes through to the output but as commented-out lines. The @@if()@@...@@endif@@ substitution works somewhat like the Apache ... directive in that it's only recognized if the "@@if()@@" part appears at the beginning of the line (optionally preceeded by white space). The matching "@@endif@@" part must also appear at the beginning of a line (optionally preceeded by white space) to be recognized. Furthermore, the only thing that may appear on the same line following the "@@()@@" or "@@endif@@" is optional white space and an optional '#'-introduced comment. While the new @@if()@@...@@endif@@ substitution does not depend on any Apache-specific processing, it's not currently possible to nest multiple levels. Since the new @@if()@@...@@endif@@ gets processed before the old but still supported @@> mechanism, it can be used to disable those lines. Signed-off-by: Kyle J. McKay --- make-apache-conf.sh | 39 ++++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/make-apache-conf.sh b/make-apache-conf.sh index d606fb9..72aa600 100755 --- a/make-apache-conf.sh +++ b/make-apache-conf.sh @@ -20,10 +20,18 @@ trap 'exit 143' TERM __girocco_conf="$GIROCCO_CONF" [ -n "$__girocco_conf" ] || __girocco_conf="Girocco::Config" perl -I"$PWD" -M"$__girocco_conf" -- - apache.conf.in >apache.conf.$$ <<'EOT' -#line 21 "make-apache-conf.sh" +#line 24 "make-apache-conf.sh" use strict; use warnings; +BEGIN { # hack to make var_online_cpus available now + $INC{'Girocco/Config.pm'} = 1; + require Girocco::Util; + my $online_cpus = Girocco::Util::online_cpus(); + defined($online_cpus) && $online_cpus >= 1 or $online_cpus = 1; + eval '$Girocco::Config::var_online_cpus = '.$online_cpus.';'; +} + my $frombegin = '# ---- BEGIN LINES TO DUPLICATE ----'; my $fromend = '# ---- END LINES TO DUPLICATE ----'; my $tobegin = '# ---- BEGIN DUPLICATE LINES ----'; @@ -37,20 +45,33 @@ my $input; } close IN; $input =~ s/^##.*(?:\n|$)//gm; -my $repl = sub { +my $replifat = sub { + my ($first, $not, $var, $lines, $last) = @_; + no warnings; + my $test = $var =~ /^\d+$/ ? 0+$var : eval('$Girocco::Config::'.$var); + $not and $test = !$test; + !$test and $lines =~ s/^/#/gm; + return '#'.$first.$lines.'#'.$last; +}; +my $replifdef = sub { + my ($not, $var) = @_; no warnings; - if (eval '$Girocco::Config::'.$_[1]) { - $_[0] ? "Girocco_Config_$_[1]_Disabled" : "!Girocco_Config_$_[1]_Disabled"; + my $test = $var =~ /^\d+$/ ? 0+$var : eval('$Girocco::Config::'.$var); + if ($test) { + $not ? "Girocco_Config_${var}_Disabled" : "!Girocco_Config_${var}_Disabled"; } else { - $_[0] ? "!Girocco_Config_$_[1]_Enabled" : "Girocco_Config_$_[1]_Enabled"; + $not ? "!Girocco_Config_${var}_Enabled" : "Girocco_Config_${var}_Enabled"; } }; -$input =~ s/^(\s*/ - $1 . &$repl($2, $3) . '>'/gmexi; +my $cmteol = qr/[ \t]*(?:[#][^\n]*)?\n/; +$input =~ s/^([ \t]*\@\@if\((!?)([a-zA-Z][a-zA-Z0-9_]*|\d+)\)\@\@(?>$cmteol)?)(.*?) + ((?<=\n)[ \t]*\@\@endif\@\@(?>$cmteol)?)/&$replifat($1, $2, $3, $4, $5)/gsmex; +$input =~ s/^(\s*/ + $1 . &$replifdef($2, $3) . '>'/gmexi; { no warnings; - $input =~ s/\@\@([a-zA-Z][a-zA-Z0-9_]*)\@\@/eval - "\$Girocco::Config::$1 ne ''?\$Girocco::Config::$1:'\@\@'.\"$1\".'\@\@'"/gsex; + $input =~ s/(\@\@([a-zA-Z][a-zA-Z0-9_]*)\@\@)/eval + "\$Girocco::Config::$2 ne ''?\$Girocco::Config::$2:\$1"/gsex; } if ($input =~ /(?:^|\n)$frombegin[^\n]*\n(.*)(?<=\n)$fromend/s) { my $dupelines = $1; -- 2.11.4.GIT