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>
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 @@
             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 @@
     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