Source-Changes-HG archive

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

[src/trunk]: src/bin/sh A better LINENO implementation. This version delete...



details:   https://anonhg.NetBSD.org/src/rev/138b969a1d55
branches:  trunk
changeset: 824498:138b969a1d55
user:      kre <kre%NetBSD.org@localhost>
date:      Wed Jun 07 05:08:32 2017 +0000

description:
A better LINENO implementation.   This version deletes (well, #if 0's out)
the LINENO hack, and uses the LINENO var for both ${LINENO} and $((LINENO)).
(Code to invert the LINENO hack when required, like when de-compiling the
execution tree to provide the "jobs" command strings, is still included,
that can be deleted when the LINENO hack is completely removed - look for
refs to VSLINENO throughout the code.  The var funclinno in parser.c can
also be removed, it is used only for the LINENO hack.)

This version produces accurate results: $((LINENO)) was made as accurate
as the LINENO hack made ${LINENO} which is very good.  That's why the
LINENO hack is not yet completely removed, so it can be easily re-enabled.
If you can tell the difference when it is in use, or not in use, then
something has broken (or I managed to miss a case somewhere.)

The way that LINENO works is documented in its own (new) section in the
man page, so nothing more about that, or the new options, etc, here.

This version introduces the possibility of having a "reference" function
associated with a variable, which gets called whenever the value of the
variable is required (that's what implements LINENO).  There is just
one function pointer however, so any particular variable gets at most
one of the set function (as used for PATH, etc) or the reference function.
The VFUNCREF bit in the var flags indicates which func the variable in
question uses (if any - the func ptr, as before, can be NULL).

I would not call the results of this perfect yet, but it is close.

diffstat:

 bin/sh/arith_token.c  |    6 +-
 bin/sh/arith_tokens.h |    4 +-
 bin/sh/arithmetic.c   |   39 ++++++++-
 bin/sh/arithmetic.h   |    4 +-
 bin/sh/eval.c         |   42 +++++++++-
 bin/sh/exec.c         |   14 ++-
 bin/sh/exec.h         |    8 +-
 bin/sh/expand.c       |  118 +++++++++++++++++++++++++-----
 bin/sh/expand.h       |    3 +-
 bin/sh/input.c        |    9 +-
 bin/sh/jobs.c         |   27 ++++--
 bin/sh/nodetypes      |    4 +-
 bin/sh/option.list    |    3 +-
 bin/sh/parser.c       |  155 ++++++++++++++++++++++++++++-----------
 bin/sh/parser.h       |   10 ++-
 bin/sh/sh.1           |  190 +++++++++++++++++++++++++++++++++++++++++++++----
 bin/sh/show.c         |   29 ++++++-
 bin/sh/syntax.c       |    7 +-
 bin/sh/syntax.h       |    4 +-
 bin/sh/var.c          |   94 ++++++++++++++++++------
 bin/sh/var.h          |   17 +++-
 21 files changed, 629 insertions(+), 158 deletions(-)

diffs (truncated from 2019 to 300 lines):

diff -r 5aa968c95a0d -r 138b969a1d55 bin/sh/arith_token.c
--- a/bin/sh/arith_token.c      Wed Jun 07 04:44:17 2017 +0000
+++ b/bin/sh/arith_token.c      Wed Jun 07 05:08:32 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: arith_token.c,v 1.4 2017/05/29 22:54:07 kre Exp $      */
+/*     $NetBSD: arith_token.c,v 1.5 2017/06/07 05:08:32 kre Exp $      */
 
 /*-
  * Copyright (c) 2002
@@ -39,7 +39,7 @@
 #include <sys/cdefs.h>
 
 #ifndef lint
-__RCSID("$NetBSD: arith_token.c,v 1.4 2017/05/29 22:54:07 kre Exp $");
+__RCSID("$NetBSD: arith_token.c,v 1.5 2017/06/07 05:08:32 kre Exp $");
 #endif /* not lint */
 
 #include <inttypes.h>
@@ -98,6 +98,7 @@
                         * and nothing else does.  They continue for the
                         * longest unbroken sequence of alphanumerics ( + _ )
                         */
+                       arith_var_lno = arith_lno;
                        p = buf;
                        while (buf++, is_in_name(*buf))
                                ;
@@ -115,6 +116,7 @@
                         * operator, white space, or an error.
                         */
                case '\n':
+                       arith_lno++;
                        VTRACE(DBG_ARITH, ("Arith: newline\n"));
                        /* FALLTHROUGH */
                case ' ':
diff -r 5aa968c95a0d -r 138b969a1d55 bin/sh/arith_tokens.h
--- a/bin/sh/arith_tokens.h     Wed Jun 07 04:44:17 2017 +0000
+++ b/bin/sh/arith_tokens.h     Wed Jun 07 05:08:32 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: arith_tokens.h,v 1.1 2017/03/20 11:26:07 kre Exp $     */
+/*     $NetBSD: arith_tokens.h,v 1.2 2017/06/07 05:08:32 kre Exp $     */
 
 /*-
  * Copyright (c) 1993
@@ -112,4 +112,6 @@
 
 extern union a_token_val a_t_val;
 
+extern int arith_lno, arith_var_lno;
+
 int arith_token(void);
diff -r 5aa968c95a0d -r 138b969a1d55 bin/sh/arithmetic.c
--- a/bin/sh/arithmetic.c       Wed Jun 07 04:44:17 2017 +0000
+++ b/bin/sh/arithmetic.c       Wed Jun 07 05:08:32 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: arithmetic.c,v 1.2 2017/05/29 22:54:07 kre Exp $       */
+/*     $NetBSD: arithmetic.c,v 1.3 2017/06/07 05:08:32 kre Exp $       */
 
 /*-
  * Copyright (c) 1993
@@ -39,7 +39,7 @@
 #include <sys/cdefs.h>
 
 #ifndef lint
-__RCSID("$NetBSD: arithmetic.c,v 1.2 2017/05/29 22:54:07 kre Exp $");
+__RCSID("$NetBSD: arithmetic.c,v 1.3 2017/06/07 05:08:32 kre Exp $");
 #endif /* not lint */
 
 #include <limits.h>
@@ -47,6 +47,7 @@
 #include <inttypes.h>
 #include <stdlib.h>
 #include <stdio.h>
+#include <string.h>
 
 #include "shell.h"
 #include "arithmetic.h"
@@ -71,6 +72,8 @@
 
 static int last_token;
 
+int arith_lno, arith_var_lno;
+
 #define ARITH_PRECEDENCE(op, prec) [op - ARITH_BINOP_MIN] = prec
 
 static const char prec[ARITH_BINOP_MAX - ARITH_BINOP_MIN] = {
@@ -109,8 +112,15 @@
        const char *str;
        char *p;
        intmax_t result;
+       const int oln = line_number;
 
+       VTRACE(DBG_ARITH, ("Arith var lookup(\"%s\") with lno=%d\n", varname,
+           arith_var_lno));
+
+       line_number = arith_var_lno;
        str = lookupvar(varname);
+       line_number = oln;
+
        if (uflag && str == NULL)
                arith_err("variable not set");
        if (str == NULL || *str == '\0')
@@ -365,14 +375,33 @@
 }
 
 intmax_t
-arith(const char *s)
+arith(const char *s, int lno)
 {
        struct stackmark smark;
        intmax_t result;
+       const char *p;
+       int nls = 0;
 
        setstackmark(&smark);
 
-       CTRACE(DBG_ARITH, ("Arith(\"%s\")\n", s));
+       arith_lno = lno;
+
+       CTRACE(DBG_ARITH, ("Arith(\"%s\", %d) @%d\n", s, lno, arith_lno));
+
+       /* check if it is possible we might reference LINENO */
+       p = s;
+       while ((p = strchr(p, 'L')) != NULL) {
+               if (p[1] == 'I' && p[2] == 'N') {
+                       /* if it is possible, we need to correct airth_lno */
+                       p = s;
+                       while ((p = strchr(p, '\n')) != NULL)
+                               nls++, p++;
+                       VTRACE(DBG_ARITH, ("Arith found %d newlines\n", nls));
+                       arith_lno -= nls;
+                       break;
+               }
+               p++;
+       }
 
        arith_buf = arith_startbuf = s;
 
@@ -420,7 +449,7 @@
        } else
                p = "";
 
-       i = arith(p);
+       i = arith(p, line_number);
 
        out1fmt(ARITH_FORMAT_STR "\n", i);
        return !i;
diff -r 5aa968c95a0d -r 138b969a1d55 bin/sh/arithmetic.h
--- a/bin/sh/arithmetic.h       Wed Jun 07 04:44:17 2017 +0000
+++ b/bin/sh/arithmetic.h       Wed Jun 07 05:08:32 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: arithmetic.h,v 1.1 2017/03/20 11:26:07 kre Exp $       */
+/*     $NetBSD: arithmetic.h,v 1.2 2017/06/07 05:08:32 kre Exp $       */
 
 /*-
  * Copyright (c) 1995
@@ -39,4 +39,4 @@
 
 #define        ARITH_FORMAT_STR        "%" PRIdMAX
 
-intmax_t arith(const char *);
+intmax_t arith(const char *, int);
diff -r 5aa968c95a0d -r 138b969a1d55 bin/sh/eval.c
--- a/bin/sh/eval.c     Wed Jun 07 04:44:17 2017 +0000
+++ b/bin/sh/eval.c     Wed Jun 07 05:08:32 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: eval.c,v 1.142 2017/06/07 04:44:17 kre Exp $   */
+/*     $NetBSD: eval.c,v 1.143 2017/06/07 05:08:32 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.142 2017/06/07 04:44:17 kre Exp $");
+__RCSID("$NetBSD: eval.c,v 1.143 2017/06/07 05:08:32 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -217,7 +217,7 @@
        struct stackmark smark;
 
        setstackmark(&smark);
-       setinputstring(s, 1, atoi(line_num.text + line_num.name_len + 1));
+       setinputstring(s, 1, line_number);
 
        while ((n = parsecmd(0)) != NEOF) {
                TRACE(("evalstring: "); showtree(n));
@@ -317,7 +317,9 @@
                evalcase(n, sflags);
                break;
        case NDEFUN:
-               defun(n->narg.text, n->narg.next);
+               CTRACE(DBG_EVAL, ("Defining fn %s @%d%s\n", n->narg.text,
+                   n->narg.lineno, fnline1 ? " LINENO=1" : ""));
+               defun(n->narg.text, n->narg.next, n->narg.lineno);
                exitstatus = 0;
                break;
        case NNOT:
@@ -473,9 +475,11 @@
 
        setstackmark(&smark);
        arglist.lastp = &arglist.list;
+       line_number = n->ncase.lineno;
        expandarg(n->ncase.expr, &arglist, EXP_TILDE);
        for (cp = n->ncase.cases; cp && evalskip == 0; cp = cp->nclist.next) {
                for (patp = cp->nclist.pattern; patp; patp = patp->narg.next) {
+                       line_number = patp->narg.lineno;
                        if (casematch(patp, arglist.list->text)) {
                                while (cp != NULL && evalskip == 0 &&
                                    nflag == 0) {
@@ -483,6 +487,7 @@
                                                ncp = cp->nclist.next;
                                        else
                                                ncp = NULL;
+                                       line_number = cp->nclist.lineno;
                                        evaltree(cp->nclist.body,
                                            ncp ? (flags | EV_MORE) : flags);
                                        status = exitstatus;
@@ -766,21 +771,25 @@
        char * volatile lastarg;
        const char * volatile path = pathval();
        volatile int temp_path;
+       const int savefuncline = funclinebase;
+       const int savefuncabs = funclineabs;
 
        vforked = 0;
        /* First expand the arguments. */
-       TRACE(("evalcommand(0x%lx, %d) called\n", (long)cmd, flags));
+       TRACE(("evalcommand(%p, %d) called\n", cmd, flags));
        setstackmark(&smark);
        back_exitstatus = 0;
 
        if (cmd != NULL)
-               set_lineno(cmd->ncmd.lineno);
+               line_number = cmd->ncmd.lineno;
 
        arglist.lastp = &arglist.list;
        varflag = 1;
        /* Expand arguments, ignoring the initial 'name=value' ones */
        for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) {
                char *p = argp->narg.text;
+
+               line_number = argp->narg.lineno;
                if (varflag && is_name(*p)) {
                        do {
                                p++;
@@ -799,6 +808,8 @@
        varlist.lastp = &varlist.list;
        for (argp = cmd->ncmd.args ; argp ; argp = argp->narg.next) {
                char *p = argp->narg.text;
+
+               line_number = argp->narg.lineno;
                if (!is_name(*p))
                        break;
                do
@@ -879,6 +890,7 @@
 
                do {
                        int argsused, use_syspath;
+
                        find_command(argv[0], &cmdentry, cmd_flags, path);
                        if (cmdentry.cmdtype == CMDUNKNOWN) {
                                exitstatus = 127;
@@ -1031,11 +1043,27 @@
                        }
                        poplocalvars();
                        localvars = savelocalvars;
+                       funclinebase = savefuncline;
+                       funclineabs = savefuncabs;
                        handler = savehandler;
                        longjmp(handler->loc, 1);
                }
                savehandler = handler;
                handler = &jmploc;
+               if (cmdentry.u.func) {
+                       if (cmdentry.lno_frel)
+                               funclinebase = cmdentry.lineno - 1;
+                       else
+                               funclinebase = 0;
+                       funclineabs = cmdentry.lineno;
+
+                       VTRACE(DBG_EVAL,
+                         ("function: node: %d '%s' # %d%s; funclinebase=%d\n",
+                           cmdentry.u.func->type,
+                           NODETYPENAME(cmdentry.u.func->type),
+                           cmdentry.lineno, cmdentry.lno_frel?" (=1)":"",
+                           funclinebase));
+               }
                listmklocal(varlist.list, VEXPORT);
                /* stop shell blowing its stack */
                if (++funcnest > 1000)
@@ -1045,6 +1073,8 @@
                INTOFF;
                poplocalvars();
                localvars = savelocalvars;
+               funclinebase = savefuncline;
+               funclineabs = savefuncabs;
                freeparam(&shellparam);
                shellparam = saveparam;



Home | Main Index | Thread Index | Old Index