Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/bin/sh Implement the -X option - an apparent variant of -x w...



details:   https://anonhg.NetBSD.org/src/rev/b3b73ab3ce77
branches:  trunk
changeset: 357633:b3b73ab3ce77
user:      kre <kre%NetBSD.org@localhost>
date:      Sun Nov 19 03:23:01 2017 +0000

description:
Implement the -X option - an apparent variant of -x which sends all trace
output to the stderr which existed when the -X option was (last) enabled.
It also enables tracing by enabling -x (and when reset, +X, also resets
the 'x' flag (+x)).  Note that it is still -x/+x which actually
enables/disables the trace output.   Hence "apparent variant" - what -X
actually does (aside from setting -x) is just to lock the trace output,
rather than having it follow wherever stderr is later redirected.

diffstat:

 bin/sh/eval.c      |   73 +++++++++++-----------
 bin/sh/option.list |    3 +-
 bin/sh/options.c   |   14 +++-
 bin/sh/output.c    |  164 ++++++++++++++++++++++++++++++++++++++++++++++++++--
 bin/sh/output.h    |   13 +++-
 bin/sh/sh.1        |   40 ++++++++++++-
 bin/sh/var.c       |    6 +-
 7 files changed, 258 insertions(+), 55 deletions(-)

diffs (truncated from 617 to 300 lines):

diff -r 464378fd0df4 -r b3b73ab3ce77 bin/sh/eval.c
--- a/bin/sh/eval.c     Sun Nov 19 03:22:55 2017 +0000
+++ b/bin/sh/eval.c     Sun Nov 19 03:23:01 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: eval.c,v 1.152 2017/09/29 17:53:57 kre Exp $   */
+/*     $NetBSD: eval.c,v 1.153 2017/11/19 03:23:01 kre 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.152 2017/09/29 17:53:57 kre Exp $");
+__RCSID("$NetBSD: eval.c,v 1.153 2017/11/19 03:23:01 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -279,20 +279,20 @@
                if (xflag && n->nredir.redirect) {
                        union node *rn;
 
-                       out2str(expandstr(ps4val(), line_number));
-                       out2str("using redirections:");
+                       outxstr(expandstr(ps4val(), line_number));
+                       outxstr("using redirections:");
                        for (rn = n->nredir.redirect; rn; rn = rn->nfile.next)
-                               (void) outredir(&errout, rn, ' ');
-                       out2str(" do\n");
-                       flushout(&errout);
+                               (void) outredir(outx, rn, ' ');
+                       outxstr(" do\n");
+                       flushout(outx);
                }
                redirect(n->nredir.redirect, REDIR_PUSH | REDIR_KEEP);
                evaltree(n->nredir.n, flags);
                popredir();
                if (xflag && n->nredir.redirect) {
-                       out2str(expandstr(ps4val(), line_number));
-                       out2str("done\n");
-                       flushout(&errout);
+                       outxstr(expandstr(ps4val(), line_number));
+                       outxstr("done\n");
+                       flushout(outx);
                }
                break;
        case NSUBSHELL:
@@ -437,13 +437,13 @@
                        f |= EV_MORE;
 
                if (xflag) {
-                       out2str(expandstr(ps4val(), line_number));
-                       out2str("for ");
-                       out2str(n->nfor.var);
-                       out2c('=');
-                       out2shstr(sp->text);
-                       out2c('\n');
-                       flushout(&errout);
+                       outxstr(expandstr(ps4val(), line_number));
+                       outxstr("for ");
+                       outxstr(n->nfor.var);
+                       outxc('=');
+                       outxshstr(sp->text);
+                       outxc('\n');
+                       flushout(outx);
                }
 
                setvar(n->nfor.var, sp->text, 0);
@@ -523,12 +523,12 @@
        if (xflag && n->nredir.redirect) {
                union node *rn;
 
-               out2str(expandstr(ps4val(), line_number));
-               out2str("using redirections:");
+               outxstr(expandstr(ps4val(), line_number));
+               outxstr("using redirections:");
                for (rn = n->nredir.redirect; rn; rn = rn->nfile.next)
-                       (void) outredir(&errout, rn, ' ');
-               out2str(" do subshell\n");
-               flushout(&errout);
+                       (void) outredir(outx, rn, ' ');
+               outxstr(" do subshell\n");
+               flushout(outx);
        }
        INTOFF;
        jp = makejob(n, 1);
@@ -543,9 +543,9 @@
        exitstatus = backgnd ? 0 : waitforjob(jp);
        INTON;
        if (!backgnd && xflag && n->nredir.redirect) {
-               out2str(expandstr(ps4val(), line_number));
-               out2str("done subshell\n");
-               flushout(&errout);
+               outxstr(expandstr(ps4val(), line_number));
+               outxstr("done subshell\n");
+               flushout(outx);
        }
 }
 
@@ -797,7 +797,8 @@
 
        vforked = 0;
        /* First expand the arguments. */
-       CTRACE(DBG_EVAL, ("evalcommand(%p, %d) called\n", cmd, flags));
+       CTRACE(DBG_EVAL, ("evalcommand(%p, %d) called [%s]\n", cmd, flags,
+           cmd->ncmd.args ? cmd->ncmd.args->narg.text : ""));
        setstackmark(&smark);
        back_exitstatus = 0;
 
@@ -861,12 +862,12 @@
                char sep = 0;
                union node *rn;
 
-               out2str(expandstr(ps4val(), line_number));
+               outxstr(expandstr(ps4val(), line_number));
                for (sp = varlist.list ; sp ; sp = sp->next) {
                        char *p;
 
                        if (sep != 0)
-                               outc(sep, &errout);
+                               outxc(sep);
 
                        /*
                         * The "var=" part should not be quoted, regardless
@@ -876,25 +877,25 @@
                        p = strchr(sp->text, '=');
                        if (p != NULL) {
                                *p = '\0';      /*XXX*/
-                               out2shstr(sp->text);
-                               out2c('=');
+                               outxshstr(sp->text);
+                               outxc('=');
                                *p++ = '=';     /*XXX*/
                        } else
                                p = sp->text;
-                       out2shstr(p);
+                       outxshstr(p);
                        sep = ' ';
                }
                for (sp = arglist.list ; sp ; sp = sp->next) {
                        if (sep != 0)
-                               outc(sep, &errout);
-                       out2shstr(sp->text);
+                               outxc(sep);
+                       outxshstr(sp->text);
                        sep = ' ';
                }
                for (rn = cmd->ncmd.redirect; rn; rn = rn->nfile.next)
-                       if (outredir(&errout, rn, sep))
+                       if (outredir(outx, rn, sep))
                                sep = ' ';
-               outc('\n', &errout);
-               flushout(&errout);
+               outxc('\n');
+               flushout(outx);
        }
 
        /* Now locate the command. */
diff -r 464378fd0df4 -r b3b73ab3ce77 bin/sh/option.list
--- a/bin/sh/option.list        Sun Nov 19 03:22:55 2017 +0000
+++ b/bin/sh/option.list        Sun Nov 19 03:23:01 2017 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: option.list,v 1.6 2017/07/24 14:17:11 kre Exp $ */
+/* $NetBSD: option.list,v 1.7 2017/11/19 03:23:01 kre Exp $ */
 
 /*
  * define the shell's settable options
@@ -66,6 +66,7 @@
 fnline1        local_lineno    L on            # number lines in funcs starting at 1
 promptcmds promptcmds                  # allow $( ) in PS1 (et al).
 pipefail pipefail                      # pipe exit status
+Xflag  Xtrace          X               # sticky stderr for -x (implies -x)
 
 // editline/history related options ("vi" is standard, 'V' and others are not)
 // only one of vi/emacs can be set, hence the "set" definition, value
diff -r 464378fd0df4 -r b3b73ab3ce77 bin/sh/options.c
--- a/bin/sh/options.c  Sun Nov 19 03:22:55 2017 +0000
+++ b/bin/sh/options.c  Sun Nov 19 03:23:01 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: options.c,v 1.50 2017/07/24 12:35:37 kre Exp $ */
+/*     $NetBSD: options.c,v 1.51 2017/11/19 03:23:01 kre Exp $ */
 
 /*-
  * Copyright (c) 1991, 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)options.c  8.2 (Berkeley) 5/4/95";
 #else
-__RCSID("$NetBSD: options.c,v 1.50 2017/07/24 12:35:37 kre Exp $");
+__RCSID("$NetBSD: options.c,v 1.51 2017/11/19 03:23:01 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -252,10 +252,12 @@
                    if (optlist[j].opt_set == flag)
                        optlist[j].val = 0;
        }
+       if (i == _SH_OPT_Xflag)
+               xtracefdsetup(val);
        optlist[i].val = val;
 #ifdef DEBUG
        if (&optlist[i].val == &debug)
-               opentrace();
+               opentrace();    /* different "trace" than the -x one... */
 #endif
 }
 
@@ -307,6 +309,8 @@
                for (i = 0; i < NOPTS; i++)
                        if (optlist[i].name && equal(name, optlist[i].name)) {
                                set_opt_val(i, val);
+                               if (i == _SH_OPT_Xflag)
+                                       set_opt_val(_SH_OPT_xflag, val);
                                return;
                        }
                error("Illegal option %co %s", "+-"[val], name);
@@ -321,7 +325,9 @@
 
        for (i = 0; i < NOPTS; i++)
                if (optlist[i].letter == flag) {
-                       set_opt_val( i, val );
+                       set_opt_val(i, val);
+                       if (i == _SH_OPT_Xflag)
+                               set_opt_val(_SH_OPT_xflag, val);
                        return;
                }
        error("Illegal option %c%c", "+-"[val], flag);
diff -r 464378fd0df4 -r b3b73ab3ce77 bin/sh/output.c
--- a/bin/sh/output.c   Sun Nov 19 03:22:55 2017 +0000
+++ b/bin/sh/output.c   Sun Nov 19 03:23:01 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: output.c,v 1.38 2017/11/19 03:22:55 kre Exp $  */
+/*     $NetBSD: output.c,v 1.39 2017/11/19 03:23:01 kre Exp $  */
 
 /*-
  * Copyright (c) 1991, 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)output.c   8.2 (Berkeley) 5/4/95";
 #else
-__RCSID("$NetBSD: output.c,v 1.38 2017/11/19 03:22:55 kre Exp $");
+__RCSID("$NetBSD: output.c,v 1.39 2017/11/19 03:23:01 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -67,6 +67,9 @@
 #include "output.h"
 #include "memalloc.h"
 #include "error.h"
+#include "redir.h"
+#include "options.h"
+#include "show.h"
 
 
 #define OUTBUFSIZ BUFSIZ
@@ -74,12 +77,14 @@
 #define MEM_OUT -3             /* output to dynamically allocated memory */
 
 
-struct output output = {NULL, 0, OUTBUFSIZ, NULL, 1, 0};
-struct output errout = {NULL, 0, 100, NULL, 2, 0};
-struct output memout = {NULL, 0, 0, NULL, MEM_OUT, 0};
+               /*      nextc  nleft  bufsize  buf     fd  flags  chain */
+struct output output = {NULL,    0, OUTBUFSIZ, NULL,    1,    0,   NULL };
+struct output errout = {NULL,    0,       100, NULL,    2,    0,   NULL };
+struct output memout = {NULL,    0,         0, NULL, MEM_OUT, 0,   NULL };
 struct output *out1 = &output;
 struct output *out2 = &errout;
-
+struct output *outx = &errout;
+struct output *outxtop = NULL;
 
 
 #ifdef mkinit
@@ -128,13 +133,21 @@
        outstr(p, out2);
 }
 
+void
+outxstr(const char *p)
+{
+       outstr(p, outx);
+}
+
 
 void
 outstr(const char *p, struct output *file)
 {
+       char c = 0;
+
        while (*p)
-               outc(*p++, file);
-       if (file == out2)
+               outc((c = *p++), file);
+       if (file == out2 || (file == outx && c == '\n'))
                flushout(file);
 }
 
@@ -145,6 +158,11 @@
        outshstr(p, out2);



Home | Main Index | Thread Index | Old Index