summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatt Spinler <spinler@us.ibm.com>2018-03-08 16:35:29 -0600
committerMatt Spinler <spinler@us.ibm.com>2018-03-14 21:05:48 +0000
commit64e084d809cf4409147df6b7391ef94b2f37715c (patch)
tree42b04e7fe1cde27642df9a07f239d24ccfc59eb5
parentab2b9c3f6b0f82c5465a4c6ca66c8b3feef2d5cb (diff)
downloadphosphor-mrw-tools-64e084d809cf4409147df6b7391ef94b2f37715c.tar.gz
phosphor-mrw-tools-64e084d809cf4409147df6b7391ef94b2f37715c.zip
gen_settings.pl: Evaluate expressions
If the script finds a '[[ ]]' expression in the input file, it will evaluate that expression and write out the result in place of it. It will resolve both the MRW attributes and any variables passed in from the command line first. For example, if the attribute FOO has a value of 50 in the MRW, and the program was started with: -v "MY_VAR1=200 MY_VAR2=400" then the line [[(MRW_FOO * MY_VAR1) + 5]]..[[MRW_FOO * MY_VAR2]] would get written out as: 10005..20000 Change-Id: Ia7aa9ab61f99b5d9620d77fa1329bd6d7b0221bc Signed-off-by: Matt Spinler <spinler@us.ibm.com>
-rwxr-xr-xgen_settings.pl40
1 files changed, 40 insertions, 0 deletions
diff --git a/gen_settings.pl b/gen_settings.pl
index 2f94e49..2c97586 100755
--- a/gen_settings.pl
+++ b/gen_settings.pl
@@ -84,6 +84,23 @@ foreach my $target (sort keys %{$targetObj->getAllTargets()})
my $settingValue = $targetObj->getAttribute($target, $setting);
$row =~ s/MRW_${setting}/$settingValue/g;
}
+
+ #If there are [[ ]] expressions, evaluate them and replace the
+ #[[expression]] with the value
+ while ($row =~ /\[\[(.*?)\]\]/)
+ {
+ my $expr = $1;
+ my $value = evaluate($expr);
+
+ #Break the row apart, remove the [[ ]]s, and put the
+ #value in the middle when putting back together.
+ my $exprStart = index($row, $expr);
+ my $front = substr($row, 0, $exprStart - 2);
+ my $back = substr($row, $exprStart + length($expr) + 2);
+
+ $row = $front . $value . $back;
+ }
+
print $outFh $row;
}
last;
@@ -91,6 +108,29 @@ foreach my $target (sort keys %{$targetObj->getAllTargets()})
close $outFh;
}
+#Evaluate the expression passed in. Substitute any variables with
+#their values passed in on the command line.
+sub evaluate
+{
+ my $expr = shift;
+
+ #Put in the value for the variable.
+ for my $var (keys %exprVars)
+ {
+ $expr =~ s/$var/$exprVars{$var}/;
+ }
+
+ my $value = eval($expr);
+ if (not defined $value)
+ {
+ die "Invalid expression found: $expr\n";
+ }
+
+ #round it to an integer
+ $value = sprintf("%.0f", $value);
+ return $value;
+}
+
#Parse the variable=value string passed in from the
#command line and load it into %exprVars.
sub loadVars
OpenPOWER on IntegriCloud