Source-Changes-HG archive

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

[src/trunk]: src/bin/sh Cosmetic changes to variable flags - make their value...



details:   https://anonhg.NetBSD.org/src/rev/c613e18a3d9e
branches:  trunk
changeset: 824758:c613e18a3d9e
user:      kre <kre%NetBSD.org@localhost>
date:      Sat Jun 17 10:46:34 2017 +0000

description:
Cosmetic changes to variable flags - make their values more suited
to my delicate sensibilities...  (NFC).

Arrange not to barf (ever) if some turkey makes _ readonly.  Do this
by adding a VNOERROR flag that causes errors in var setting to be
ignored (intended use is only for internal shell var setting, like of "_").
(nb: invalid var name errors ignore this flag, but those should never
occur on a var set by the shell itself.)

>From FreeBSD: don't simply discard memory if a variable is not set for
any reason (including because it is readonly) if the var's value had
been malloc'd.  Free it instead...

diffstat:

 bin/sh/eval.c |   6 +++---
 bin/sh/var.c  |  21 ++++++++++++++++-----
 bin/sh/var.h  |  23 +++++++++++++----------
 3 files changed, 32 insertions(+), 18 deletions(-)

diffs (118 lines):

diff -r 66b0c8c1d82f -r c613e18a3d9e bin/sh/eval.c
--- a/bin/sh/eval.c     Sat Jun 17 09:32:53 2017 +0000
+++ b/bin/sh/eval.c     Sat Jun 17 10:46:34 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: eval.c,v 1.146 2017/06/08 13:12:17 kre Exp $   */
+/*     $NetBSD: eval.c,v 1.147 2017/06/17 10:46:34 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.146 2017/06/08 13:12:17 kre Exp $");
+__RCSID("$NetBSD: eval.c,v 1.147 2017/06/17 10:46:34 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -1195,7 +1195,7 @@
 out:
        if (lastarg)
                /* implement $_ for whatever use that really is */
-               setvar("_", lastarg, 0);
+               (void) setvarsafe("_", lastarg, VNOERROR);
        popstackmark(&smark);
 }
 
diff -r 66b0c8c1d82f -r c613e18a3d9e bin/sh/var.c
--- a/bin/sh/var.c      Sat Jun 17 09:32:53 2017 +0000
+++ b/bin/sh/var.c      Sat Jun 17 10:46:34 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: var.c,v 1.59 2017/06/17 04:12:06 kre Exp $     */
+/*     $NetBSD: var.c,v 1.60 2017/06/17 10:46:34 kre Exp $     */
 
 /*-
  * Copyright (c) 1991, 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)var.c      8.3 (Berkeley) 5/4/95";
 #else
-__RCSID("$NetBSD: var.c,v 1.59 2017/06/17 04:12:06 kre Exp $");
+__RCSID("$NetBSD: var.c,v 1.60 2017/06/17 10:46:34 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -353,10 +353,18 @@
                flags |= VEXPORT;
        vp = find_var(s, &vpp, &nlen);
        if (vp != NULL) {
-               if (vp->flags & VREADONLY)
+               if (vp->flags & VREADONLY) {
+                       if ((flags & (VTEXTFIXED|VSTACK)) == 0)
+                               ckfree(s);
+                       if (flags & VNOERROR)
+                               return;
                        error("%.*s: is read only", vp->name_len, s);
-               if (flags & VNOSET)
+               }
+               if (flags & VNOSET) {
+                       if ((flags & (VTEXTFIXED|VSTACK)) == 0)
+                               ckfree(s);
                        return;
+               }
 
                INTOFF;
 
@@ -385,8 +393,11 @@
                return;
        }
        /* not found */
-       if (flags & VNOSET)
+       if (flags & VNOSET) {
+               if ((flags & (VTEXTFIXED|VSTACK)) == 0)
+                       ckfree(s);
                return;
+       }
        vp = ckmalloc(sizeof (*vp));
        vp->flags = flags & ~(VNOFUNC|VFUNCREF);
        vp->text = s;
diff -r 66b0c8c1d82f -r c613e18a3d9e bin/sh/var.h
--- a/bin/sh/var.h      Sat Jun 17 09:32:53 2017 +0000
+++ b/bin/sh/var.h      Sat Jun 17 10:46:34 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: var.h,v 1.30 2017/06/07 05:08:32 kre Exp $     */
+/*     $NetBSD: var.h,v 1.31 2017/06/17 10:46:34 kre Exp $     */
 
 /*-
  * Copyright (c) 1991, 1993
@@ -39,17 +39,20 @@
  */
 
 /* flags */
-#define VEXPORT                0x0001  /* variable is exported */
-#define VREADONLY      0x0002  /* variable cannot be modified */
-#define VSTRFIXED      0x0004  /* variable struct is statically allocated */
-#define VTEXTFIXED     0x0008  /* text is statically allocated */
-#define VSTACK         0x0010  /* text is allocated on the stack */
-#define VUNSET         0x0020  /* the variable is not set */
-#define VNOFUNC                0x0040  /* don't call the callback function */
-#define VNOSET         0x0080  /* do not set variable - just readonly test */
-#define VNOEXPORT      0x0100  /* variable may not be exported */
+#define VUNSET         0x0001  /* the variable is not set */
+#define VEXPORT                0x0002  /* variable is exported */
+#define VREADONLY      0x0004  /* variable cannot be modified */
+#define VNOEXPORT      0x0008  /* variable may not be exported */
+
+#define VSTRFIXED      0x0010  /* variable struct is statically allocated */
+#define VTEXTFIXED     0x0020  /* text is statically allocated */
+#define VSTACK         0x0040  /* text is allocated on the stack */
+#define VNOFUNC                0x0100  /* don't call the callback function */
 #define VFUNCREF       0x0200  /* the function is called on ref, not set */
 
+#define VNOSET         0x4000  /* do not set variable - just readonly test */
+#define VNOERROR       0x8000  /* be quiet if set fails (no error msg) */
+
 struct var;
 
 union var_func_union {         /* function to be called when:  */



Home | Main Index | Thread Index | Old Index