Module Name:    src
Committed By:   christos
Date:           Mon Aug 31 23:37:55 UTC 2020

Modified Files:
        src/external/historical/nawk/dist: lib.c proto.h run.c

Log Message:
Add a check_number function that does what is repeated in many places in
the code, but better.


To generate a diff of this commit:
cvs rdiff -u -r1.12 -r1.13 src/external/historical/nawk/dist/lib.c \
    src/external/historical/nawk/dist/run.c
cvs rdiff -u -r1.11 -r1.12 src/external/historical/nawk/dist/proto.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/external/historical/nawk/dist/lib.c
diff -u src/external/historical/nawk/dist/lib.c:1.12 src/external/historical/nawk/dist/lib.c:1.13
--- src/external/historical/nawk/dist/lib.c:1.12	Fri Apr 17 18:35:18 2020
+++ src/external/historical/nawk/dist/lib.c	Mon Aug 31 19:37:55 2020
@@ -191,10 +191,7 @@ int getrec(char **pbuf, int *pbufsize, b
 					xfree(fldtab[0]->sval);
 				fldtab[0]->sval = buf;	/* buf == record */
 				fldtab[0]->tval = REC | STR | DONTFREE;
-				if (is_number(fldtab[0]->sval)) {
-					fldtab[0]->fval = atof(fldtab[0]->sval);
-					fldtab[0]->tval |= NUM;
-				}
+				check_number(fldtab[0]);
 			}
 			setfval(nrloc, nrloc->fval+1);
 			setfval(fnrloc, fnrloc->fval+1);
@@ -306,10 +303,7 @@ void setclvar(char *s)	/* set var=value 
 	p = qstring(p, '\0');
 	q = setsymtab(s, p, 0.0, STR, symtab);
 	setsval(q, p);
-	if (is_number(q->sval)) {
-		q->fval = atof(q->sval);
-		q->tval |= NUM;
-	}
+	check_number(q);
 	   dprintf( ("command line set %s to |%s|\n", s, p) );
 }
 
@@ -407,10 +401,7 @@ void fldbld(void)	/* create fields from 
 	donefld = true;
 	for (j = 1; j <= lastfld; j++) {
 		p = fldtab[j];
-		if(is_number(p->sval)) {
-			p->fval = atof(p->sval);
-			p->tval |= NUM;
-		}
+		check_number(p);
 	}
 	setfval(nfloc, (Awkfloat) lastfld);
 	donerec = true; /* restore */
@@ -763,7 +754,8 @@ int isclvar(const char *s)	/* is s of fo
 /* wrong: violates 4.10.1.4 of ansi C standard */
 
 #include <math.h>
-int is_number(const char *s)
+
+static int get_number(const char *s, double *res)
 {
 	double r;
 	char *ep;
@@ -773,8 +765,20 @@ int is_number(const char *s)
 		return 0;
 	while (*ep == ' ' || *ep == '\t' || *ep == '\n')
 		ep++;
-	if (*ep == '\0')
-		return 1;
-	else
+	if (*ep != '\0')
 		return 0;
+	if (res)		
+		*res = r;
+	return 1;
+}
+
+void check_number(Cell *x)
+{
+	if (get_number(x->sval, &x->fval))
+		x->tval |= NUM;
+}
+
+int is_number(const char *s)
+{
+	return get_number(s, NULL);
 }
Index: src/external/historical/nawk/dist/run.c
diff -u src/external/historical/nawk/dist/run.c:1.12 src/external/historical/nawk/dist/run.c:1.13
--- src/external/historical/nawk/dist/run.c:1.12	Thu Feb 20 14:59:12 2020
+++ src/external/historical/nawk/dist/run.c	Mon Aug 31 19:37:55 2020
@@ -432,17 +432,11 @@ Cell *awkgetline(Node **a, int n)	/* get
 		} else if (a[0] != NULL) {	/* getline var <file */
 			x = execute(a[0]);
 			setsval(x, buf);
-			if (is_number(x->sval)) {
-				x->fval = atof(x->sval);
-				x->tval |= NUM;
-			}
+			check_number(x);
 			tempfree(x);
 		} else {			/* getline <file */
 			setsval(fldtab[0], buf);
-			if (is_number(fldtab[0]->sval)) {
-				fldtab[0]->fval = atof(fldtab[0]->sval);
-				fldtab[0]->tval |= NUM;
-			}
+			check_number(fldtab[0]);
 		}
 	} else {			/* bare getline; use current input */
 		if (a[0] == NULL)	/* getline */
@@ -451,10 +445,7 @@ Cell *awkgetline(Node **a, int n)	/* get
 			n = getrec(&buf, &bufsize, false);
 			x = execute(a[0]);
 			setsval(x, buf);
-			if (is_number(x->sval)) {
-				x->fval = atof(x->sval);
-				x->tval |= NUM;
-			}
+			check_number(x);
 			tempfree(x);
 		}
 	}

Index: src/external/historical/nawk/dist/proto.h
diff -u src/external/historical/nawk/dist/proto.h:1.11 src/external/historical/nawk/dist/proto.h:1.12
--- src/external/historical/nawk/dist/proto.h:1.11	Thu Feb 20 14:59:12 2020
+++ src/external/historical/nawk/dist/proto.h	Mon Aug 31 19:37:55 2020
@@ -149,6 +149,7 @@ extern	void	bclass(int);
 extern	double	errcheck(double, const char *);
 extern	int	isclvar(const char *);
 extern	int	is_number(const char *);
+extern	void	check_number(Cell *);
 
 extern	int	adjbuf(char **, int *, int, int, char **, const char *);
 extern	void	run(Node *);

Reply via email to