enable informing arithmetic parser of implicit units

This is so that "runtime=100" can be interpreted as 100 seconds
rather than 100 microseconds, but runtime=(100ms + 100s) can also
get the right answer (in microseconds).

Signed-off-by: Stephen M. Cameron <stephenmcameron@gmail.com>
Signed-off-by: Jens Axboe <axboe@fb.com>
diff --git a/exp/expression-parser.l b/exp/expression-parser.l
index c084db6..6ec0866 100644
--- a/exp/expression-parser.l
+++ b/exp/expression-parser.l
@@ -31,7 +31,7 @@
 		lexer_input((buffer), &(bytes_read), (bytes_requested))
 
 extern int yyerror(long long *result, double *dresult,
-		int *has_error, int *bye, const char *msg);
+		int *has_error, int *units_specified, int *bye, const char *msg);
 
 static void __attribute__((unused)) yyunput(int c,char *buf_ptr);
 static int __attribute__((unused)) input(void);
@@ -122,7 +122,7 @@
 				yylval.v.has_error = 0;
 				return NUMBER;
 			} else {
-				yyerror(0, 0, 0, 0, "bad number\n");
+				yyerror(0, 0, 0, 0, 0, "bad number\n");
 				yylval.v.has_error = 1;
 				return NUMBER;
 			}
@@ -137,7 +137,7 @@
 			yylval.v.has_error = 0;
 			return NUMBER;
 		} else {
-			yyerror(0, 0, 0, 0, "bad number\n");
+			yyerror(0, 0, 0, 0, 0, "bad number\n");
 			yylval.v.has_error = 1;
 			return NUMBER;
 		}
@@ -152,7 +152,7 @@
 			yylval.v.has_error = 0;
 			return NUMBER;
 		} else {
-			yyerror(0, 0, 0, 0, "bad number\n");
+			yyerror(0, 0, 0, 0, 0, "bad number\n");
 			yylval.v.has_error = 1;
 			return NUMBER;
 		}
diff --git a/exp/expression-parser.y b/exp/expression-parser.y
index 87ead8a..1cc0791 100644
--- a/exp/expression-parser.y
+++ b/exp/expression-parser.y
@@ -37,6 +37,7 @@
 int yyerror(__attribute__((unused)) long long *result,
 		__attribute__((unused)) double *dresult,
 		__attribute__((unused)) int *has_error,
+		__attribute__((unused)) int *units_specified,
 		__attribute__((unused)) int *bye, const char *msg);
 
 extern int yylex(void);
@@ -65,6 +66,7 @@
 %parse-param { long long *result }
 %parse-param { double *dresult }
 %parse-param { int *has_error }
+%parse-param { int *units_specified }
 %parse-param { int *bye }
 
 %type <v> expression
@@ -106,11 +108,11 @@
 		}
 	|	expression '/' expression {
 			if ($3.ival == 0)
-				yyerror(0, 0, 0, 0, "divide by zero");
+				yyerror(0, 0, 0, 0, 0, "divide by zero");
 			else
 				$$.ival = $1.ival / $3.ival;
 			if ($3.dval < 1e-20 && $3.dval > -1e-20)
-				yyerror(0, 0, 0, 0, "divide by zero");
+				yyerror(0, 0, 0, 0, 0, "divide by zero");
 			else
 				$$.dval = $1.dval / $3.dval;
 			if ($3.has_dval || $1.has_dval)
@@ -133,12 +135,13 @@
 			else
 				$$.dval = $1.ival * $2.ival;
 			$$.has_error = $1.has_error || $2.has_error;
+			*units_specified = 1;
 		}
 	|	expression '%' expression {
 			if ($1.has_dval || $3.has_dval)
-				yyerror(0, 0, 0, 0, "modulo on floats");
+				yyerror(0, 0, 0, 0, 0, "modulo on floats");
 			if ($3.ival == 0)
-				yyerror(0, 0, 0, 0, "divide by zero");
+				yyerror(0, 0, 0, 0, 0, "divide by zero");
 			else {
 				$$.ival = $1.ival % $3.ival;
 				$$.dval = $$.ival;
@@ -211,24 +214,30 @@
 	lexer_read_offset = 0;
 }
 
-int evaluate_arithmetic_expression(const char *buffer, long long *ival, double *dval)
+int evaluate_arithmetic_expression(const char *buffer, long long *ival, double *dval,
+					double implied_units)
 {
-	int rc, bye = 0, has_error = 0;
+	int rc, units_specified = 0, bye = 0, has_error = 0;
 
 	setup_to_parse_string(buffer);
-	rc = yyparse(ival, dval, &has_error, &bye);
+	rc = yyparse(ival, dval, &has_error, &units_specified, &bye);
 	yyrestart(NULL);
 	if (rc || bye || has_error) {
 		*ival = 0;
 		*dval = 0;
 		has_error = 1;
 	}
+	if (!units_specified) {
+		*ival = (int) ((double) *ival * implied_units);
+		*dval = *dval * implied_units;
+	}
 	return has_error;
 }
 
 int yyerror(__attribute__((unused)) long long *result,
 		__attribute__((unused)) double *dresult,
 		__attribute__((unused)) int *has_error,
+		__attribute__((unused)) int *units_specified,
 		__attribute__((unused)) int *bye,
 		__attribute__((unused)) const char *msg)
 {
diff --git a/exp/test-expression-parser.c b/exp/test-expression-parser.c
index 4515144..93c766a 100644
--- a/exp/test-expression-parser.c
+++ b/exp/test-expression-parser.c
@@ -25,7 +25,7 @@
 #include "../y.tab.h"
 
 extern int evaluate_arithmetic_expression(const char *buffer, long long *ival,
-					  double *dval);
+					  double *dval, double implied_units);
  
 int main(int argc, char *argv[])
 {
@@ -40,7 +40,7 @@
 		rc = strlen(buffer);
 		if (rc > 0 && buffer[rc - 1] == '\n')
 			buffer[rc - 1] = '\0';
-		rc = evaluate_arithmetic_expression(buffer, &result, &dresult);
+		rc = evaluate_arithmetic_expression(buffer, &result, &dresult, 1.0);
 		if (!rc) {
 			printf("%lld (%20.20lf)\n", result, dresult);
 		} else {
diff --git a/parse.c b/parse.c
index 3a5fbcc..9e31908 100644
--- a/parse.c
+++ b/parse.c
@@ -269,7 +269,7 @@
 }
 
 extern int evaluate_arithmetic_expression(const char *buffer, long long *ival,
-					  double *dval);
+					  double *dval, double implied_units);
 
 #ifdef CONFIG_ARITHMETIC
 /*
@@ -278,7 +278,7 @@
  * original number parsing code.  Once sufficiently sure that the arithmetic
  * code is always getting the right answers, these can be removed.
  */
-static void verify_exp_parser_float(const char *str)
+static void verify_exp_parser_float(const char *str, double implied_units)
 {
 	long long ival;
 	double dval, tmpval;
@@ -286,7 +286,7 @@
 	if (sscanf(str, "%lf", &tmpval) != 1)
 		return;
 
-	if (evaluate_arithmetic_expression(str, &ival, &dval) != 0) {
+	if (evaluate_arithmetic_expression(str, &ival, &dval, implied_units) != 0) {
 		log_info("Arithmetic failed on '%s'\n", str);
 		return;
 	}
@@ -301,8 +301,12 @@
 	int rc;
 	long long ival;
 	double dval;
+	double implied_units = 1.0;
 
-	rc = evaluate_arithmetic_expression(str, &ival, &dval);
+	if (is_seconds)
+		implied_units = 1000000.0;
+
+	rc = evaluate_arithmetic_expression(str, &ival, &dval, implied_units);
 	if (!rc) {
 		if (ival != val)
 			log_info("Arithmetic failed on '%s', expected %lld, got %lld\n",
@@ -324,13 +328,13 @@
 	double dval;
 
 	if (str[0] == '(') {
-		rc = evaluate_arithmetic_expression(str, &ival, &dval);
+		rc = evaluate_arithmetic_expression(str, &ival, &dval, 1.0);
 		if (!rc) {
 			*val = dval;
 			return 1;
 		}
 	} else {
-		verify_exp_parser_float(str);
+		verify_exp_parser_float(str, 1.0);
 	}
 #endif
 	return 1 == sscanf(str, "%lf", val);
@@ -347,6 +351,7 @@
 #ifdef CONFIG_ARITHMETIC
 	long long ival;
 	double dval;
+	double implied_units = 1.0;
 #endif
 
 	len = strlen(str);
@@ -354,8 +359,10 @@
 		return 1;
 
 #ifdef CONFIG_ARITHMETIC
+	if (is_seconds)
+		implied_units = 1000000.0;
 	if (str[0] == '(')
-		rc = evaluate_arithmetic_expression(str, &ival, &dval);
+		rc = evaluate_arithmetic_expression(str, &ival, &dval, implied_units);
 	if (str[0] == '(' && !rc) {
 		if (!kilo && is_seconds)
 			*val = ival / 1000000LL;