Source-Changes-HG archive

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

[src/trunk]: src/bin/sh add crude $LINENO support for FreeBSD



details:   https://anonhg.NetBSD.org/src/rev/9aba049f638c
branches:  trunk
changeset: 790296:9aba049f638c
user:      christos <christos%NetBSD.org@localhost>
date:      Wed Oct 02 19:52:58 2013 +0000

description:
add crude $LINENO support for FreeBSD

diffstat:

 bin/sh/expand.c |  12 +++++++++---
 bin/sh/parser.c |  27 +++++++++++++++++++++++----
 bin/sh/parser.h |  10 ++++++----
 3 files changed, 38 insertions(+), 11 deletions(-)

diffs (164 lines):

diff -r 77010b22f28d -r 9aba049f638c bin/sh/expand.c
--- a/bin/sh/expand.c   Wed Oct 02 18:25:43 2013 +0000
+++ b/bin/sh/expand.c   Wed Oct 02 19:52:58 2013 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: expand.c,v 1.88 2012/12/22 20:15:22 dsl Exp $  */
+/*     $NetBSD: expand.c,v 1.89 2013/10/02 19:52:58 christos Exp $     */
 
 /*-
  * Copyright (c) 1991, 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)expand.c   8.5 (Berkeley) 5/15/95";
 #else
-__RCSID("$NetBSD: expand.c,v 1.88 2012/12/22 20:15:22 dsl Exp $");
+__RCSID("$NetBSD: expand.c,v 1.89 2013/10/02 19:52:58 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -647,7 +647,12 @@
        p = strchr(p, '=') + 1;
 
 again: /* jump here after setting a variable with ${var=text} */
-       if (special) {
+       if (varflags & VSLINENO) {
+               set = 1;
+               special = 0;
+               val = var;
+               p[-1] = '\0';
+       } else if (special) {
                set = varisset(var, varflags & VSNUL);
                val = NULL;
        } else {
@@ -784,6 +789,7 @@
        default:
                abort();
        }
+       p[-1] = '=';    /* recover overwritten '=' */
 
        if (apply_ifs)
                recordregion(startloc, expdest - stackblock(),
diff -r 77010b22f28d -r 9aba049f638c bin/sh/parser.c
--- a/bin/sh/parser.c   Wed Oct 02 18:25:43 2013 +0000
+++ b/bin/sh/parser.c   Wed Oct 02 19:52:58 2013 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: parser.c,v 1.83 2012/06/17 20:48:27 wiz Exp $  */
+/*     $NetBSD: parser.c,v 1.84 2013/10/02 19:52:58 christos Exp $     */
 
 /*-
  * Copyright (c) 1991, 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)parser.c   8.7 (Berkeley) 5/16/95";
 #else
-__RCSID("$NetBSD: parser.c,v 1.83 2012/06/17 20:48:27 wiz Exp $");
+__RCSID("$NetBSD: parser.c,v 1.84 2013/10/02 19:52:58 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -100,6 +100,7 @@
 struct heredoc *heredoc;
 int quoteflag;                 /* set if (part of) last token was quoted */
 int startlinno;                        /* line # where last token started */
+int funclinno;                 /* line # where the current function started */
 
 
 STATIC union node *list(int, int);
@@ -583,11 +584,13 @@
                        /* We have a function */
                        if (readtoken() != TRP)
                                synexpect(TRP);
+                       funclinno = plinno;
                        rmescapes(n->narg.text);
                        if (!goodname(n->narg.text))
                                synerror("Bad function name");
                        n->type = NDEFUN;
                        n->narg.next = command();
+                       funclinno = 0;
                        goto checkneg;
                } else {
                        tokpushback++;
@@ -1253,11 +1256,14 @@
  */
 
 parsesub: {
+       char buf[10];
        int subtype;
        int typeloc;
        int flags;
        char *p;
        static const char types[] = "}-+?=";
+       int i;
+       int linno;
 
        c = pgetc();
        if (c != '(' && c != OPENBRACE && !is_name(c) && !is_special(c)) {
@@ -1275,6 +1281,7 @@
                typeloc = out - stackblock();
                USTPUTC(VSNORMAL, out);
                subtype = VSNORMAL;
+               flags = 0;
                if (c == OPENBRACE) {
                        c = pgetc();
                        if (c == '#') {
@@ -1287,10 +1294,23 @@
                                subtype = 0;
                }
                if (is_name(c)) {
+                       p = out;
                        do {
                                STPUTC(c, out);
                                c = pgetc();
                        } while (is_in_name(c));
+                       if (out - p == 6 && strncmp(p, "LINENO", 6) == 0) {
+                               /* Replace the variable name with the
+                                * current line number. */
+                               linno = plinno;
+                               if (funclinno != 0)
+                                       linno -= funclinno - 1;
+                               snprintf(buf, sizeof(buf), "%d", linno);
+                               STADJUST(-6, out);
+                               for (i = 0; buf[i] != '\0'; i++)
+                                       STPUTC(buf[i], out);
+                               flags |= VSLINENO;
+                       }
                } else if (is_digit(c)) {
                        do {
                                USTPUTC(c, out);
@@ -1305,11 +1325,10 @@
 badsub:                        synerror("Bad substitution");
 
                STPUTC('=', out);
-               flags = 0;
                if (subtype == 0) {
                        switch (c) {
                        case ':':
-                               flags = VSNUL;
+                               flags |= VSNUL;
                                c = pgetc();
                                /*FALLTHROUGH*/
                        default:
diff -r 77010b22f28d -r 9aba049f638c bin/sh/parser.h
--- a/bin/sh/parser.h   Wed Oct 02 18:25:43 2013 +0000
+++ b/bin/sh/parser.h   Wed Oct 02 19:52:58 2013 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: parser.h,v 1.17 2004/06/26 22:09:49 dsl Exp $  */
+/*     $NetBSD: parser.h,v 1.18 2013/10/02 19:52:58 christos Exp $     */
 
 /*-
  * Copyright (c) 1991, 1993
@@ -49,9 +49,11 @@
 #define        CTL_LAST '\211'         /* last 'special' character */
 
 /* variable substitution byte (follows CTLVAR) */
-#define VSTYPE 0x0f            /* type of variable substitution */
-#define VSNUL  0x10            /* colon--treat the empty string as unset */
-#define VSQUOTE 0x80           /* inside double quotes--suppress splitting */
+#define VSTYPE         0x0f    /* type of variable substitution */
+#define VSNUL          0x10    /* colon--treat the empty string as unset */
+#define VSLINENO       0x20    /* expansion of $LINENO, the line number
+                                  follows immediately */
+#define VSQUOTE                0x80    /* inside double quotes--suppress splitting */
 
 /* values of VSTYPE field */
 #define VSNORMAL       0x1             /* normal variable:  $var or ${var} */



Home | Main Index | Thread Index | Old Index