Index: /trunk/src/kash/shfile.c
===================================================================
--- /trunk/src/kash/shfile.c	(revision 2296)
+++ /trunk/src/kash/shfile.c	(revision 2297)
@@ -41,8 +41,4 @@
 # include <fcntl.h>
 # include <dirent.h>
-#endif
-
-#if defined(DEBUG) && defined(TRACE_VIA_STDIO)
-extern FILE *tracefile;
 #endif
 
@@ -611,8 +607,5 @@
 #endif
 
-#if defined(DEBUG) && defined(TRACE_VIA_STDIO)
-    if (tracefile)
-#endif
-        TRACE2((NULL, "shfile_open(%p:{%s}, %#x, 0%o) -> %d [%d]\n", name, name, flags, mode, fd, errno));
+    TRACE2((NULL, "shfile_open(%p:{%s}, %#x, 0%o) -> %d [%d]\n", name, name, flags, mode, fd, errno));
     return fd;
 }
@@ -999,24 +992,11 @@
 #endif
 
-#ifdef DEBUG
-# ifdef TRACE_VIA_STDIO
-    if (tracefile)
-# endif
-        switch (cmd)
-        {
-            case F_GETFL:
-                TRACE2((NULL, "shfile_fcntl(%d,F_GETFL,ignored=%d) -> %d [%d]\n", fd, arg, rc, errno));
-                break;
-            case F_SETFL:
-                TRACE2((NULL, "shfile_fcntl(%d,F_SETFL,newflags=%#x) -> %d [%d]\n", fd, arg, rc, errno));
-                break;
-            case F_DUPFD:
-                TRACE2((NULL, "shfile_fcntl(%d,F_DUPFS,minfd=%d) -> %d [%d]\n", fd, arg, rc, errno));
-                break;
-            default:
-                TRACE2((NULL, "shfile_fcntl(%d,%d,%d) -> %d [%d]\n", fd, cmd, arg, rc, errno));
-                break;
-        }
-#endif
+    switch (cmd)
+    {
+        case F_GETFL: TRACE2((NULL, "shfile_fcntl(%d,F_GETFL,ignored=%d) -> %d [%d]\n", fd, arg, rc, errno));  break;
+        case F_SETFL: TRACE2((NULL, "shfile_fcntl(%d,F_SETFL,newflags=%#x) -> %d [%d]\n", fd, arg, rc, errno)); break;
+        case F_DUPFD: TRACE2((NULL, "shfile_fcntl(%d,F_DUPFS,minfd=%d) -> %d [%d]\n", fd, arg, rc, errno)); break;
+        default:  TRACE2((NULL, "shfile_fcntl(%d,%d,%d) -> %d [%d]\n", fd, cmd, arg, rc, errno)); break;
+    }
     return rc;
 }
Index: /trunk/src/kash/show.c
===================================================================
--- /trunk/src/kash/show.c	(revision 2296)
+++ /trunk/src/kash/show.c	(revision 2297)
@@ -263,12 +263,8 @@
 
 
+#ifdef DEBUG
 /*
  * Debugging stuff.
  */
-
-#ifdef DEBUG
-#ifdef TRACE_VIA_STDIO
-FILE *tracefile;
-#endif
 
 /** @def TRY_GET_PSH_OR_RETURN
@@ -284,27 +280,9 @@
 /** @def RETURN_IF_NOT_TRACING
  * Return if we're not tracing. */
-# ifdef TRACE_VIA_STDIO
-#  define RETURN_IF_NOT_TRACING(psh) \
-	if (debug(psh) != 1 || !tracefile)
-		return; \
-	else do	{} while (0)
-# else
-#  define RETURN_IF_NOT_TRACING(psh) \
-	if (debug(psh) != 1 || psh->tracefd == -1) \
-		return; \
-	else do	{} while (0)
-# endif
-
-/** @def TRACE_PUTC
- * putc/trace_char wrapper. The @a psh / @a tracefile
- * is taken from the context and not as a paramenter. */
-# ifdef TRACE_VIA_STDIO
-#  define TRACE_PUTC(c) 	fputc(c, tracefile)
-# else
-#  define TRACE_PUTC(c) 	trace_char(psh, c)
-# endif
-
-
-# ifndef TRACE_VIA_STDIO
+# define RETURN_IF_NOT_TRACING(psh) \
+   if (debug(psh) != 1 || psh->tracefd == -1) \
+   	return; \
+   else do	{} while (0)
+
 /* Flushes the tracebuf. */
 static void
@@ -321,4 +299,5 @@
 
 	if (pos) {
+        int     s = errno;
 		char 	prefix[40];
 		size_t 	len;
@@ -330,4 +309,6 @@
 		psh->tracepos = 0;
 		psh->tracebuf[0] = '\0';
+
+        errno = s;
 	}
 }
@@ -378,8 +359,10 @@
 		} else {
 			/* it's too big for some reason... */
+            int s = errno;
 			trace_flush(psh);
 			shfile_write(&psh->fdtab, psh->tracefd, str, len);
 			if (!flush_it)
 				shfile_write(&psh->fdtab, psh->tracefd, "[too long]\n", sizeof( "[too long]\n") - 1);
+            errno = s;
 		}
 
@@ -388,5 +371,4 @@
 	}
 }
-# endif
 
 void
@@ -396,31 +378,16 @@
 	RETURN_IF_NOT_TRACING(psh);
 
-# ifdef TRACE_VIA_STDIO
-    putc(c, tracefile);
-# else
 	trace_char(psh, c);
-# endif
-}
-#endif
+}
 
 void
 trace(shinstance *psh, const char *fmt, ...)
 {
-#ifdef DEBUG
-	int savederrno = errno;
 	va_list va;
-# ifndef TRACE_VIA_STDIO
 	char buf[2048];
-# endif
 
 	TRY_GET_PSH_OR_RETURN(psh);
 	RETURN_IF_NOT_TRACING(psh);
 
-# ifdef TRACE_VIA_STDIO
-	fprintf(tracefile, "[%d] ", sh_getpid(psh));
-	va_start(va, fmt);
-	(void) vfprintf(tracefile, fmt, va);
-	va_end(va);
-# else
 	va_start(va, fmt);
 #  ifdef _MSC_VER
@@ -431,8 +398,4 @@
 	va_end(va);
 	trace_string(psh, buf);
-# endif
-
-	errno = savederrno;
-#endif
 }
 
@@ -440,17 +403,9 @@
 tracev(shinstance *psh, const char *fmt, va_list va)
 {
-#ifdef DEBUG
-	int savederrno = errno;
-# ifndef TRACE_VIA_STDIO
 	char buf[2048];
-# endif
 
 	TRY_GET_PSH_OR_RETURN(psh);
 	RETURN_IF_NOT_TRACING(psh);
 
-# ifdef TRACE_VIA_STDIO
-	fprintf(tracefile, "[%d] ", sh_getpid(psh));
-	(void) vfprintf(tracefile, fmt, va);
-# else
 #  ifdef _MSC_VER
 	_vsnprintf(buf, sizeof(buf), fmt, va);
@@ -459,27 +414,14 @@
 #  endif
 	trace_string(psh, buf);
-# endif
-
-	errno = savederrno;
-#endif
-}
-
-
-#ifdef DEBUG
+}
+
 void
 trputs(shinstance *psh, const char *s)
 {
-	int savederrno = errno;
-
 	TRY_GET_PSH_OR_RETURN(psh);
 	RETURN_IF_NOT_TRACING(psh);
 
-# ifdef TRACE_VIA_STDIO
-	fputs(s, tracefile);
-# else
 	trace_string(psh, s);
-# endif
-
-	errno = savederrno;
+    trace_char(psh, '\n');
 }
 
@@ -488,5 +430,4 @@
 trstring(shinstance *psh, char *s)
 {
-	int savederrno = errno;
 	char *p;
 	char c;
@@ -495,5 +436,5 @@
 	RETURN_IF_NOT_TRACING(psh);
 
-	TRACE_PUTC('"');
+	trace_char(psh, '"');
 	for (p = s ; *p ; p++) {
 		switch (*p) {
@@ -508,32 +449,25 @@
 		case CTLBACKQ:  c = 'q';  goto backslash;
 		case CTLBACKQ+CTLQUOTE:  c = 'Q';  goto backslash;
-backslash:	  TRACE_PUTC('\\');
-			TRACE_PUTC(c);
+backslash:	  trace_char(psh, '\\');
+			trace_char(psh, c);
 			break;
 		default:
 			if (*p >= ' ' && *p <= '~')
-				TRACE_PUTC(*p);
+				trace_char(psh, *p);
 			else {
-				TRACE_PUTC('\\');
-				TRACE_PUTC(*p >> 6 & 03);
-				TRACE_PUTC(*p >> 3 & 07);
-				TRACE_PUTC(*p & 07);
+				trace_char(psh, '\\');
+				trace_char(psh, *p >> 6 & 03);
+				trace_char(psh, *p >> 3 & 07);
+				trace_char(psh, *p & 07);
 			}
 			break;
 		}
 	}
-	TRACE_PUTC('"');
-
-	errno = savederrno;
-}
-#endif
-
+	trace_char(psh, '"');
+}
 
 void
 trargs(shinstance *psh, char **ap)
 {
-#ifdef DEBUG
-	int savederrno = errno;
-
 	TRY_GET_PSH_OR_RETURN(psh);
 	RETURN_IF_NOT_TRACING(psh);
@@ -542,30 +476,18 @@
 		trstring(psh, *ap++);
 		if (*ap)
-			TRACE_PUTC(' ');
+			trace_char(psh, ' ');
 		else
-			TRACE_PUTC('\n');
-	}
-
-	errno = savederrno;
-#endif
-}
-
-
-#ifdef DEBUG
+			trace_char(psh, '\n');
+	}
+}
+
 void
 opentrace(shinstance *psh)
 {
     static const char s[] = "./trace";
-# ifdef TRACE_VIA_STDIO
-	int fd;
-# endif
 
 	TRY_GET_PSH_OR_RETURN(psh);
 	if (debug(psh) != 1) {
-# ifdef TRACE_VIA_STDIO
-		if (tracefile)
-			fflush(tracefile);
-		/* leave open because libedit might be using it */
-# else
+        /* disabled */
 		if (psh->tracefd != -1) {
 			trace_flush(psh);
@@ -573,67 +495,18 @@
 			psh->tracefd = -1;
 		}
-# endif
 		return;
 	}
-
-# ifdef TRACE_VIA_STDIO
-	if (tracefile) {
-		if (!freopen(s, "a", tracefile)) {
-			fprintf(stderr, "Can't re-open %s\n", s);
-			debug(psh) = 0;
-			return;
-		}
-	} else {
-		fd = open(s, O_APPEND | O_RDWR | O_CREAT, 0600);
-		if (fd != -1) {
-#  if K_OS == K_OS_WINDOWS
-            int fds[50];
-            int i = 0;
-            while (i < 50) {
-                fds[i] = _dup(fd);
-                if (fds[i] == -1 || fds[i] > 199)
-                    break;
-                i++;
-            }
-            if (i > 0) {
-                close(fd);
-                fd = fds[--i];
-            }
-            while (i-- > 0)
-                close(fds[i]);
-#  else
-            int fdTarget = 199;
-            while (fdTarget > 10)
-            {
-                int fd2 = shfile_fcntl(&psh->fdtab, fd, F_DUPFD, fdTarget);
-				if (fd2 != -1) {
-					close(fd);
-					fd = fd2;
-					break;
-				}
-                fdTarget = (fdTarget + 1 / 2) - 1;
-            }
-#  endif
-		}
-		if (fd == -1 || (tracefile = fdopen(fd, "a")) == NULL) {
-			fprintf(stderr, "Can't open %s\n", s);
-			debug(psh) = 0;
-			return;
-		}
-	}
-	setvbuf(tracefile, (char *)NULL, _IOLBF, 1024);
-	fputs("\nTracing started.\n", tracefile);
-
-# else  /* !TRACE_VIA_STDIO */
-	if (psh->tracefd != -1) {
-		return;
-	}
+    /* else: (re-)enabled */
+
+	if (psh->tracefd != -1)
+        return;
+
 	psh->tracefd = shfile_open(&psh->fdtab, s, O_APPEND | O_RDWR | O_CREAT, 0600);
 	if (psh->tracefd != -1) {
 		/* relocate it */
-		int fdTarget = 199;
-		while (fdTarget > 10)
+		int want_fd = 199;
+		while (want_fd > 10)
 		{
-			int fd2 = shfile_fcntl(&psh->fdtab, psh->tracefd, F_DUPFD, fdTarget);
+			int fd2 = shfile_fcntl(&psh->fdtab, psh->tracefd, F_DUPFD, want_fd);
 			if (fd2 != -1) {
 				shfile_close(&psh->fdtab, psh->tracefd);
@@ -641,5 +514,5 @@
 				break;
 			}
-			fdTarget = (fdTarget + 1 / 2) - 1;
+			want_fd = ((want_fd + 1) / 2) - 1;
 		}
 	}
@@ -650,6 +523,6 @@
 	}
 	trace_string(psh, "Tracing started.\n");
-
-# endif /* !TRACE_VIA_STDIO */
-}
+}
+
 #endif /* DEBUG */
+
Index: /trunk/src/kash/show.h
===================================================================
--- /trunk/src/kash/show.h	(revision 2296)
+++ /trunk/src/kash/show.h	(revision 2297)
@@ -39,8 +39,8 @@
 union node;
 void showtree(struct shinstance *, union node *);
+#ifdef DEBUG
 void trace(struct shinstance *, const char *, ...);
 void tracev(struct shinstance *, const char *, va_list);
 void trargs(struct shinstance *, char **);
-#ifdef DEBUG
 void trputc(struct shinstance *, int);
 void trputs(struct shinstance *, const char *);
