Module Name:    src
Committed By:   kre
Date:           Mon May 29 22:54:07 UTC 2017

Modified Files:
        src/bin/sh: arith_token.c arithmetic.c

Log Message:
Add DEBUG tracing to arithmetic $(( )) parsing & evaluation.
NFC for non-DEBUG shells.


To generate a diff of this commit:
cvs rdiff -u -r1.3 -r1.4 src/bin/sh/arith_token.c
cvs rdiff -u -r1.1 -r1.2 src/bin/sh/arithmetic.c

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

Modified files:

Index: src/bin/sh/arith_token.c
diff -u src/bin/sh/arith_token.c:1.3 src/bin/sh/arith_token.c:1.4
--- src/bin/sh/arith_token.c:1.3	Mon Mar 20 13:12:35 2017
+++ src/bin/sh/arith_token.c	Mon May 29 22:54:07 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: arith_token.c,v 1.3 2017/03/20 13:12:35 kre Exp $	*/
+/*	$NetBSD: arith_token.c,v 1.4 2017/05/29 22:54:07 kre Exp $	*/
 
 /*-
  * Copyright (c) 2002
@@ -39,7 +39,7 @@
 #include <sys/cdefs.h>
 
 #ifndef lint
-__RCSID("$NetBSD: arith_token.c,v 1.3 2017/03/20 13:12:35 kre Exp $");
+__RCSID("$NetBSD: arith_token.c,v 1.4 2017/05/29 22:54:07 kre Exp $");
 #endif /* not lint */
 
 #include <inttypes.h>
@@ -54,6 +54,7 @@ __RCSID("$NetBSD: arith_token.c,v 1.3 20
 #include "memalloc.h"
 #include "parser.h"
 #include "syntax.h"
+#include "show.h"
 
 #if ARITH_BOR + ARITH_ASS_GAP != ARITH_BORASS || \
 	ARITH_ASS + ARITH_ASS_GAP != ARITH_EQ
@@ -87,6 +88,8 @@ arith_token(void)
 			 */
 			a_t_val.val = strtoimax(buf, &end, 0);
 			arith_buf = end;
+			VTRACE(DBG_ARITH, ("Arith token ARITH_NUM=%jd\n",
+			    a_t_val.val));
 			return ARITH_NUM;
 
 		} else if (is_name(token)) {
@@ -102,6 +105,8 @@ arith_token(void)
 			memcpy(a_t_val.name, p, buf - p);
 			a_t_val.name[buf - p] = '\0';
 			arith_buf = buf;
+			VTRACE(DBG_ARITH, ("Arith token ARITH_VAR=\"%s\"\n",
+			    a_t_val.name));
 			return ARITH_VAR;
 
 		} else switch (token) {
@@ -109,9 +114,11 @@ arith_token(void)
 			 * everything else must be some kind of
 			 * operator, white space, or an error.
 			 */
+		case '\n':
+			VTRACE(DBG_ARITH, ("Arith: newline\n"));
+			/* FALLTHROUGH */
 		case ' ':
 		case '\t':
-		case '\n':
 			buf++;
 			continue;
 
@@ -231,5 +238,6 @@ arith_token(void)
 	buf++;
  out:
 	arith_buf = buf;
+	VTRACE(DBG_ARITH, ("Arith token: %d\n", token));
 	return token;
 }

Index: src/bin/sh/arithmetic.c
diff -u src/bin/sh/arithmetic.c:1.1 src/bin/sh/arithmetic.c:1.2
--- src/bin/sh/arithmetic.c:1.1	Mon Mar 20 11:26:07 2017
+++ src/bin/sh/arithmetic.c	Mon May 29 22:54:07 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: arithmetic.c,v 1.1 2017/03/20 11:26:07 kre Exp $	*/
+/*	$NetBSD: arithmetic.c,v 1.2 2017/05/29 22:54:07 kre Exp $	*/
 
 /*-
  * Copyright (c) 1993
@@ -39,7 +39,7 @@
 #include <sys/cdefs.h>
 
 #ifndef lint
-__RCSID("$NetBSD: arithmetic.c,v 1.1 2017/03/20 11:26:07 kre Exp $");
+__RCSID("$NetBSD: arithmetic.c,v 1.2 2017/05/29 22:54:07 kre Exp $");
 #endif /* not lint */
 
 #include <limits.h>
@@ -57,6 +57,7 @@ __RCSID("$NetBSD: arithmetic.c,v 1.1 201
 #include "output.h"
 #include "options.h"
 #include "var.h"
+#include "show.h"
 
 #if ARITH_BOR + ARITH_ASS_GAP != ARITH_BORASS || \
 	ARITH_ASS + ARITH_ASS_GAP != ARITH_EQ
@@ -139,6 +140,7 @@ static intmax_t
 do_binop(int op, intmax_t a, intmax_t b)
 {
 
+	VTRACE(DBG_ARITH, ("Arith do binop %d (%jd, %jd)\n", op, a, b));
 	switch (op) {
 	default:
 		arith_err("token error");
@@ -187,6 +189,9 @@ primary(int token, union a_token_val *va
 {
 	intmax_t result;
 
+	VTRACE(DBG_ARITH, ("Arith primary: token %d op %d%s\n",
+	    token, op, noeval ? " noeval" : ""));
+
 	switch (token) {
 	case ARITH_LPAREN:
 		result = assignment(op, noeval);
@@ -226,6 +231,9 @@ binop2(intmax_t a, int op, int precedenc
 	int op2;
 	int token;
 
+	VTRACE(DBG_ARITH, ("Arith: binop2 %jd op %d (P:%d)%s\n",
+	    a, op, precedence, noeval ? " noeval" : ""));
+
 	for (;;) {
 		token = arith_token();
 		val = a_t_val;
@@ -271,6 +279,8 @@ and(int token, union a_token_val *val, i
 	if (op != ARITH_AND)
 		return a;
 
+	VTRACE(DBG_ARITH, ("Arith: AND %jd%s\n", a, noeval ? " noeval" : ""));
+
 	token = arith_token();
 	*val = a_t_val;
 
@@ -289,6 +299,8 @@ or(int token, union a_token_val *val, in
 	if (op != ARITH_OR)
 		return a;
 
+	VTRACE(DBG_ARITH, ("Arith: OR %jd%s\n", a, noeval ? " noeval" : ""));
+
 	token = arith_token();
 	*val = a_t_val;
 
@@ -307,6 +319,8 @@ cond(int token, union a_token_val *val, 
 	if (last_token != ARITH_QMARK)
 		return a;
 
+	VTRACE(DBG_ARITH, ("Arith: ?: %jd%s\n", a, noeval ? " noeval" : ""));
+
 	b = assignment(arith_token(), noeval | !a);
 
 	if (last_token != ARITH_COLON)
@@ -335,6 +349,9 @@ assignment(int var, int noeval)
 	if (op != ARITH_ASS && (op < ARITH_ASS_MIN || op >= ARITH_ASS_MAX))
 		return cond(var, &val, op, noeval);
 
+	VTRACE(DBG_ARITH, ("Arith: %s ASSIGN %d%s\n", val.name, op,
+	    noeval ? " noeval" : ""));
+
 	result = assignment(arith_token(), noeval);
 	if (noeval)
 		return result;
@@ -355,6 +372,8 @@ arith(const char *s)
 
 	setstackmark(&smark);
 
+	CTRACE(DBG_ARITH, ("Arith(\"%s\")\n", s));
+
 	arith_buf = arith_startbuf = s;
 
 	result = assignment(arith_token(), 0);
@@ -364,6 +383,8 @@ arith(const char *s)
 
 	popstackmark(&smark);
 
+	CTRACE(DBG_ARITH, ("Arith result=%jd\n", result));
+
 	return result;
 }
 

Reply via email to