From 011b6cea3fc95470c8caaf9a960d5ef3bc9f3698 Mon Sep 17 00:00:00 2001 From: mhx Date: Mon, 2 Jan 2017 18:17:14 +0100 Subject: [PATCH] Enable use of a numeric .param as an integer: .param xxx = 3.1459 echo $%xxx --- src/frontend/variable.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/frontend/variable.c b/src/frontend/variable.c index ef68809be..44aa39bf6 100644 --- a/src/frontend/variable.c +++ b/src/frontend/variable.c @@ -617,7 +617,8 @@ char cp_dol = '$'; * special... */ -#define VALIDCHARS "$-_<#?@.()[]&" +/* mhx added %. */ +#define VALIDCHARS "$-_<#?@.()[]&%" char * span_var_expr(char *t) @@ -719,7 +720,8 @@ vareval(char *string) char buf[BSIZE_SP], *s; char *oldstring = copy(string); char *range = NULL; - int i, up, low, found; +#define INTEGER 0 + int i, up, low, found, fmt = !INTEGER; char *text, *snew = NULL; double val; @@ -786,6 +788,7 @@ vareval(char *string) free_struct_variable(vfree); /* bug: free still used struct variable */ return (wl); + case '%': fmt = INTEGER; /* fall through */ case '.': /* mhx: evaluate a numeric/string .parameter */ string++; val = nupa_get_param(string, &found, &text); @@ -795,7 +798,9 @@ vareval(char *string) return NULL; } if (found == 'R') - wl = wl_cons(tprintf("%20.16e", val), NULL); + wl = (fmt==INTEGER) ? + wl_cons(tprintf("%lld", (size_t)val), NULL) : + wl_cons(tprintf("%20.16e", val), NULL) ; else if (text && found == 'S') { snew = copy(text + 1); snew[MAX(0, strlen(snew) - 1)] = '\0'; /* strip leading and trailing quote */ @@ -809,6 +814,7 @@ vareval(char *string) tfree(oldstring); return (wl); } +#undef INTEGER /* The notation var[stuff] has two meanings... If this is a real * variable, then the [] denotes range, but if this is a strange