Module Name:    src
Committed By:   christos
Date:           Sat Feb 27 18:34:12 UTC 2016

Modified Files:
        src/bin/sh: Makefile eval.c main.c mknodenames.sh show.c

Log Message:
Improve debugging, from kre (I hooked it to the build).


To generate a diff of this commit:
cvs rdiff -u -r1.101 -r1.102 src/bin/sh/Makefile
cvs rdiff -u -r1.113 -r1.114 src/bin/sh/eval.c
cvs rdiff -u -r1.59 -r1.60 src/bin/sh/main.c
cvs rdiff -u -r1.1 -r1.2 src/bin/sh/mknodenames.sh
cvs rdiff -u -r1.28 -r1.29 src/bin/sh/show.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/Makefile
diff -u src/bin/sh/Makefile:1.101 src/bin/sh/Makefile:1.102
--- src/bin/sh/Makefile:1.101	Sun May 10 16:30:54 2015
+++ src/bin/sh/Makefile	Sat Feb 27 13:34:12 2016
@@ -1,4 +1,4 @@
-#	$NetBSD: Makefile,v 1.101 2015/05/10 20:30:54 joerg Exp $
+#	$NetBSD: Makefile,v 1.102 2016/02/27 18:34:12 christos Exp $
 #	@(#)Makefile	8.4 (Berkeley) 5/5/95
 
 .include <bsd.own.mk>
@@ -10,7 +10,7 @@ SHSRCS=	alias.c cd.c echo.c error.c eval
 	mystring.c options.c parser.c redir.c show.c trap.c output.c var.c \
 	test.c kill.c syntax.c
 GENSRCS=arith.c arith_lex.c builtins.c init.c nodes.c
-GENHDRS=arith.h builtins.h nodes.h token.h
+GENHDRS=arith.h builtins.h nodes.h token.h nodenames.h
 SRCS=	${SHSRCS} ${GENSRCS}
 
 DPSRCS+=${GENHDRS}
@@ -76,6 +76,10 @@ nodes.c nodes.h: mknodes.sh nodetypes no
 	${SCRIPT_ENV} ${HOST_SH} ${.ALLSRC} ${.OBJDIR}
 	[ -f nodes.h ]
 
+nodenames.h: mknodenames.sh nodes.h
+	${_MKTARGET_CREATE}
+	${SCRIPT_ENV} ${HOST_SH} ${.ALLSRC} > ${.TARGET}
+
 .if ${USETOOLS} == "yes"
 NBCOMPATLIB=   -L${TOOLDIR}/lib -lnbcompat
 .endif

Index: src/bin/sh/eval.c
diff -u src/bin/sh/eval.c:1.113 src/bin/sh/eval.c:1.114
--- src/bin/sh/eval.c:1.113	Wed Feb 24 09:57:12 2016
+++ src/bin/sh/eval.c	Sat Feb 27 13:34:12 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: eval.c,v 1.113 2016/02/24 14:57:12 christos Exp $	*/
+/*	$NetBSD: eval.c,v 1.114 2016/02/27 18:34:12 christos Exp $	*/
 
 /*-
  * Copyright (c) 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)eval.c	8.9 (Berkeley) 6/8/95";
 #else
-__RCSID("$NetBSD: eval.c,v 1.113 2016/02/24 14:57:12 christos Exp $");
+__RCSID("$NetBSD: eval.c,v 1.114 2016/02/27 18:34:12 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -80,6 +80,7 @@ __RCSID("$NetBSD: eval.c,v 1.113 2016/02
 #include "mystring.h"
 #include "main.h"
 #ifndef SMALL
+#include "nodenames.h"
 #include "myhistedit.h"
 #endif
 
@@ -220,6 +221,7 @@ evalstring(char *s, int flag)
 	setinputstring(s, 1);
 
 	while ((n = parsecmd(0)) != NEOF) {
+		TRACE(("evalstring: "); showtree(n));
 		if (nflag == 0)
 			evaltree(n, flag);
 		popstackmark(&smark);
@@ -249,8 +251,13 @@ evaltree(union node *n, int flags)
 #ifndef SMALL
 	displayhist = 1;	/* show history substitutions done with fc */
 #endif
+#ifdef NODETYPENAME
+	TRACE(("pid %d, evaltree(%p: %s(%d), %d) called\n",
+	    getpid(), n, NODETYPENAME(n->type), n->type, flags));
+#else
 	TRACE(("pid %d, evaltree(%p: %d, %d) called\n",
 	    getpid(), n, n->type, flags));
+#endif
 	switch (n->type) {
 	case NSEMI:
 		evaltree(n->nbinary.ch1, flags & EV_TESTED);
@@ -341,6 +348,16 @@ evalloop(union node *n, int flags)
 
 	loopnest++;
 	status = 0;
+
+#ifdef NODETYPENAME
+	TRACE(("evalloop %s: ", NODETYPENAME(n->type)));
+#else
+	TRACE(("evalloop %s: ", n->type == NWHILE ? "while" : "until"));
+#endif
+	TRACE((""); showtree(n->nbinary.ch1));
+	TRACE(("evalloop    do: "); showtree(n->nbinary.ch2));
+	TRACE(("evalloop  done\n"));
+
 	for (;;) {
 		evaltree(n->nbinary.ch1, EV_TESTED);
 		if (evalskip) {

Index: src/bin/sh/main.c
diff -u src/bin/sh/main.c:1.59 src/bin/sh/main.c:1.60
--- src/bin/sh/main.c:1.59	Tue May 26 17:35:15 2015
+++ src/bin/sh/main.c	Sat Feb 27 13:34:12 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: main.c,v 1.59 2015/05/26 21:35:15 christos Exp $	*/
+/*	$NetBSD: main.c,v 1.60 2016/02/27 18:34:12 christos Exp $	*/
 
 /*-
  * Copyright (c) 1991, 1993
@@ -42,7 +42,7 @@ __COPYRIGHT("@(#) Copyright (c) 1991, 19
 #if 0
 static char sccsid[] = "@(#)main.c	8.7 (Berkeley) 7/19/95";
 #else
-__RCSID("$NetBSD: main.c,v 1.59 2015/05/26 21:35:15 christos Exp $");
+__RCSID("$NetBSD: main.c,v 1.60 2016/02/27 18:34:12 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -270,6 +270,7 @@ cmdloop(int top)
 			flushout(&errout);
 		}
 		n = parsecmd(inter);
+		TRACE(("cmdloop: "); showtree(n));
 		/* showtree(n); DEBUG */
 		if (n == NEOF) {
 			if (!top || numeof >= 50)

Index: src/bin/sh/mknodenames.sh
diff -u src/bin/sh/mknodenames.sh:1.1 src/bin/sh/mknodenames.sh:1.2
--- src/bin/sh/mknodenames.sh:1.1	Wed Feb 24 09:51:20 2016
+++ src/bin/sh/mknodenames.sh	Sat Feb 27 13:34:12 2016
@@ -1,6 +1,13 @@
 #! /bin/sh
 
-test -t 1 && test "$#" -eq 0 && exec > nodenames.h
+if [ -z "$1" ]; then
+	echo "Usage: $0 nodes.h" 1>&2
+	exit 1
+fi
+
+NODES=$1
+
+test -t 1 && exec > nodenames.h
 
 echo "#ifdef DEBUG"
 echo '
@@ -10,7 +17,7 @@ echo '
  */
 '
 
-MAX=$(awk < nodes.h '
+MAX=$(awk < "$NODES" '
 	/#define/ {
 		if ($3 > MAX) MAX = $3
 	}
@@ -18,9 +25,10 @@ MAX=$(awk < nodes.h '
 ')
 
 echo
+echo '#ifdef DEFINE_NODENAMES'
 echo "STATIC const char * const NodeNames[${MAX} + 1] = {"
 
-grep '^#define' nodes.h | sort -k2n | while read define name number opt_comment
+grep '^#define' "$NODES" | sort -k2n | while read define name number opt_comment
 do
 	: ${next:=0}
 	while [ "$number" -gt "$next" ]
@@ -33,6 +41,9 @@ do
 done
 
 echo "};"
+echo '#else'
+echo "extern const char * const NodeNames[${MAX} + 1];"
+echo '#endif'
 echo
 echo '#define NODETYPENAME(type) \'
 echo '	((unsigned)(type) <= '"${MAX}"' ? NodeNames[(type)] : "??OOR??")'

Index: src/bin/sh/show.c
diff -u src/bin/sh/show.c:1.28 src/bin/sh/show.c:1.29
--- src/bin/sh/show.c:1.28	Tue Aug 23 06:01:32 2011
+++ src/bin/sh/show.c	Sat Feb 27 13:34:12 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: show.c,v 1.28 2011/08/23 10:01:32 christos Exp $	*/
+/*	$NetBSD: show.c,v 1.29 2016/02/27 18:34:12 christos Exp $	*/
 
 /*-
  * Copyright (c) 1991, 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)show.c	8.3 (Berkeley) 5/4/95";
 #else
-__RCSID("$NetBSD: show.c,v 1.28 2011/08/23 10:01:32 christos Exp $");
+__RCSID("$NetBSD: show.c,v 1.29 2016/02/27 18:34:12 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -54,124 +54,160 @@ __RCSID("$NetBSD: show.c,v 1.28 2011/08/
 #include "options.h"
 
 
+FILE *tracefile;
+
 #ifdef DEBUG
-static void shtree(union node *, int, char *, FILE*);
-static void shcmd(union node *, FILE *);
-static void sharg(union node *, FILE *);
-static void indent(int, char *, FILE *);
+static int shtree(union node *, int, int, char *, FILE*);
+static int shcmd(union node *, FILE *);
+static int sharg(union node *, FILE *);
+static int indent(int, char *, FILE *);
 static void trstring(char *);
 
-
 void
 showtree(union node *n)
 {
-	trputs("showtree called\n");
-	shtree(n, 1, NULL, stdout);
+	FILE *fp;
+
+	fp = tracefile ? tracefile : stdout;
+
+	trputs("showtree(");
+		if (n == NULL)
+			trputs("NULL");
+		else if (n == NEOF)
+			trputs("NEOF");
+	trputs(") called\n");
+	if (n != NULL && n != NEOF)
+		shtree(n, 1, 1, NULL, fp);
 }
 
 
-static void
-shtree(union node *n, int ind, char *pfx, FILE *fp)
+static int
+shtree(union node *n, int ind, int nl, char *pfx, FILE *fp)
 {
 	struct nodelist *lp;
 	const char *s;
+	int len;
 
-	if (n == NULL)
-		return;
+	if (n == NULL) {
+		if (nl)
+			fputc('\n', fp);
+		return 0;
+	}
 
-	indent(ind, pfx, fp);
-	switch(n->type) {
+	len = indent(ind, pfx, fp);
+	switch (n->type) {
 	case NSEMI:
 		s = "; ";
+		len += 2;
 		goto binop;
 	case NAND:
 		s = " && ";
+		len += 4;
 		goto binop;
 	case NOR:
 		s = " || ";
+		len += 4;
 binop:
-		shtree(n->nbinary.ch1, ind, NULL, fp);
-	   /*    if (ind < 0) */
-			fputs(s, fp);
-		shtree(n->nbinary.ch2, ind, NULL, fp);
+		len += shtree(n->nbinary.ch1, 0, 0, NULL, fp);
+		fputs(s, fp);
+		if (len >= 60) {
+			putc('\n', fp);
+			len = indent(ind < 0 ? 2 : ind + 1, pfx, fp);
+		}
+		len += shtree(n->nbinary.ch2, 0, nl, NULL, fp);
 		break;
 	case NCMD:
-		shcmd(n, fp);
-		if (ind >= 0)
-			putc('\n', fp);
+		len += shcmd(n, fp);
+		if (nl)
+			len = 0, putc('\n', fp);
 		break;
 	case NPIPE:
 		for (lp = n->npipe.cmdlist ; lp ; lp = lp->next) {
-			shcmd(lp->n, fp);
-			if (lp->next)
-				fputs(" | ", fp);
+			len += shcmd(lp->n, fp);
+			if (lp->next) {
+				len += 3, fputs(" | ", fp);
+				if (len >= 60)  {
+					fputc('\n', fp);
+					len = indent(ind < 0 ? 2 : ind + 1,
+					    pfx, fp);
+				}
+			}
 		}
 		if (n->npipe.backgnd)
-			fputs(" &", fp);
-		if (ind >= 0)
-			putc('\n', fp);
+			len += 2, fputs(" &", fp);
+		if (nl || len >= 60)
+			len = 0, fputc('\n', fp);
 		break;
 	default:
-		fprintf(fp, "<node type %d>", n->type);
-		if (ind >= 0)
-			putc('\n', fp);
+#ifdef NODETYPENAME
+		len += fprintf(fp, "<node type %d [%s]>", n->type,
+		    NODETYPENAME(n->type));
+#else
+		len += fprintf(fp, "<node type %d>", n->type);
+#endif
+		if (nl)
+			len = 0, putc('\n', fp);
 		break;
 	}
+	return len;
 }
 
 
 
-static void
+static int
 shcmd(union node *cmd, FILE *fp)
 {
 	union node *np;
 	int first;
 	const char *s;
 	int dftfd;
+	int len = 0;
 
 	first = 1;
 	for (np = cmd->ncmd.args ; np ; np = np->narg.next) {
 		if (! first)
-			putchar(' ');
-		sharg(np, fp);
+			len++, fputc(' ', fp);
+		len += sharg(np, fp);
 		first = 0;
 	}
 	for (np = cmd->ncmd.redirect ; np ; np = np->nfile.next) {
 		if (! first)
-			putchar(' ');
+			len++, fputc(' ', fp);
 		switch (np->nfile.type) {
-			case NTO:	s = ">";  dftfd = 1; break;
-			case NCLOBBER:	s = ">|"; dftfd = 1; break;
-			case NAPPEND:	s = ">>"; dftfd = 1; break;
-			case NTOFD:	s = ">&"; dftfd = 1; break;
-			case NFROM:	s = "<";  dftfd = 0; break;
-			case NFROMFD:	s = "<&"; dftfd = 0; break;
-			case NFROMTO:	s = "<>"; dftfd = 0; break;
-			default:  	s = "*error*"; dftfd = 0; break;
+			case NTO:	s = ">";  dftfd = 1; len += 1; break;
+			case NCLOBBER:	s = ">|"; dftfd = 1; len += 2; break;
+			case NAPPEND:	s = ">>"; dftfd = 1; len += 2; break;
+			case NTOFD:	s = ">&"; dftfd = 1; len += 2; break;
+			case NFROM:	s = "<";  dftfd = 0; len += 1; break;
+			case NFROMFD:	s = "<&"; dftfd = 0; len += 2; break;
+			case NFROMTO:	s = "<>"; dftfd = 0; len += 2; break;
+			default:   s = "*error*"; dftfd = 0; len += 7; break;
 		}
 		if (np->nfile.fd != dftfd)
-			fprintf(fp, "%d", np->nfile.fd);
+			len += fprintf(fp, "%d", np->nfile.fd);
 		fputs(s, fp);
 		if (np->nfile.type == NTOFD || np->nfile.type == NFROMFD) {
-			fprintf(fp, "%d", np->ndup.dupfd);
+			len += fprintf(fp, "%d", np->ndup.dupfd);
 		} else {
-			sharg(np->nfile.fname, fp);
+			len += sharg(np->nfile.fname, fp);
 		}
 		first = 0;
 	}
+	return len;
 }
 
 
 
-static void
+static int
 sharg(union node *arg, FILE *fp)
 {
 	char *p;
 	struct nodelist *bqlist;
 	int subtype;
+	int len = 0;
 
 	if (arg->type != NARG) {
-		printf("<node type %d>\n", arg->type);
+		fprintf(fp, "<node type %d>\n", arg->type);
 		abort();
 	}
 	bqlist = arg->narg.backquote;
@@ -179,84 +215,107 @@ sharg(union node *arg, FILE *fp)
 		switch (*p) {
 		case CTLESC:
 			putc(*++p, fp);
+			len++;
 			break;
 		case CTLVAR:
 			putc('$', fp);
 			putc('{', fp);
+			len += 2;
 			subtype = *++p;
 			if (subtype == VSLENGTH)
-				putc('#', fp);
+				len++, putc('#', fp);
 
-			while (*p != '=')
-				putc(*p++, fp);
+			while (*++p != '=')
+				len++, putc(*p, fp);
 
 			if (subtype & VSNUL)
-				putc(':', fp);
+				len++, putc(':', fp);
 
 			switch (subtype & VSTYPE) {
 			case VSNORMAL:
 				putc('}', fp);
+				len++;
 				break;
 			case VSMINUS:
 				putc('-', fp);
+				len++;
 				break;
 			case VSPLUS:
 				putc('+', fp);
+				len++;
 				break;
 			case VSQUESTION:
 				putc('?', fp);
+				len++;
 				break;
 			case VSASSIGN:
 				putc('=', fp);
+				len++;
 				break;
 			case VSTRIMLEFT:
 				putc('#', fp);
+				len++;
 				break;
 			case VSTRIMLEFTMAX:
 				putc('#', fp);
 				putc('#', fp);
+				len += 2;
 				break;
 			case VSTRIMRIGHT:
 				putc('%', fp);
+				len++;
 				break;
 			case VSTRIMRIGHTMAX:
 				putc('%', fp);
 				putc('%', fp);
+				len += 2;
 				break;
 			case VSLENGTH:
 				break;
 			default:
-				printf("<subtype %d>", subtype);
+				len += fprintf(fp, "<subtype %d>", subtype);
 			}
 			break;
 		case CTLENDVAR:
 		     putc('}', fp);
+		     len++;
 		     break;
 		case CTLBACKQ:
 		case CTLBACKQ|CTLQUOTE:
 			putc('$', fp);
 			putc('(', fp);
-			shtree(bqlist->n, -1, NULL, fp);
+			len += shtree(bqlist->n, -1, 0, NULL, fp) + 3;
 			putc(')', fp);
 			break;
 		default:
 			putc(*p, fp);
+			len++;
 			break;
 		}
 	}
+	return len;
 }
 
 
-static void
+static int
 indent(int amount, char *pfx, FILE *fp)
 {
 	int i;
+	int len = 0;
 
+	/*
+	 * in practice, pfx is **always** NULL
+	 * but here, we assume if it were not, at least strlen(pfx) < 8
+	 * if that is invalid, output will look messy
+	 */
 	for (i = 0 ; i < amount ; i++) {
 		if (pfx && i == amount - 1)
 			fputs(pfx, fp);
 		putc('\t', fp);
+		len |= 7;
+		len++;
 	}
+	return len;
 }
 #endif
 
@@ -267,7 +326,6 @@ indent(int amount, char *pfx, FILE *fp)
  */
 
 
-FILE *tracefile;
 
 
 #ifdef DEBUG

Reply via email to