Source-Changes-HG archive

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

[src/trunk]: src/bin/sh Add some comments explaining accesses to the environm...



details:   https://anonhg.NetBSD.org/src/rev/d01a656f5db7
branches:  trunk
changeset: 359920:d01a656f5db7
user:      kre <kre%NetBSD.org@localhost>
date:      Mon Jan 31 16:54:28 2022 +0000

description:
Add some comments explaining accesses to the environment via
getenv()/setenv()/unsetenv() which manipulate the envornoment
the shell was passed at entry.

These are a little odd in sh as that environment is copied into
the shell's internal variable data struct at shell startup, and
normally never accessed after that - in builtin commands (test.
printf, ...) getenv() is #defined to become an internal sh lookup
function instead, so even those never use the startup environment).

NFCI

diffstat:

 bin/sh/cd.c       |  18 ++++++++++++++++--
 bin/sh/histedit.c |  21 +++++++++++++++++++--
 2 files changed, 35 insertions(+), 4 deletions(-)

diffs (81 lines):

diff -r eb6b3aed49f8 -r d01a656f5db7 bin/sh/cd.c
--- a/bin/sh/cd.c       Mon Jan 31 14:44:49 2022 +0000
+++ b/bin/sh/cd.c       Mon Jan 31 16:54:28 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: cd.c,v 1.52 2021/11/16 16:57:15 kre Exp $      */
+/*     $NetBSD: cd.c,v 1.53 2022/01/31 16:54:28 kre Exp $      */
 
 /*-
  * Copyright (c) 1991, 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)cd.c       8.2 (Berkeley) 5/4/95";
 #else
-__RCSID("$NetBSD: cd.c,v 1.52 2021/11/16 16:57:15 kre Exp $");
+__RCSID("$NetBSD: cd.c,v 1.53 2022/01/31 16:54:28 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -404,6 +404,20 @@
                return;
 
        if (first) {
+               /*
+                * Note that this happens via the call from initpwd()
+                * just above, which is called early from main() during
+                * sh startup, so fetching PWD from the entry environment
+                * (which is what getenv() does) is acceptable.   Here we
+                * could use normal sh var lookup functions instead, as
+                * the arriving environment has already been imported before
+                * we get here, but it makes little difference.
+                *
+                * XXX What would be better perhaps would be to move all of
+                * this into initpwd() instead of here, so we could get rid of
+                * this "first" static - that function is only ever called once.
+                * XXX Some other day.
+                */
                first = 0;
                pwd = getenv("PWD");
                if (is_curdir(pwd)) {
diff -r eb6b3aed49f8 -r d01a656f5db7 bin/sh/histedit.c
--- a/bin/sh/histedit.c Mon Jan 31 14:44:49 2022 +0000
+++ b/bin/sh/histedit.c Mon Jan 31 16:54:28 2022 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: histedit.c,v 1.57 2021/09/14 15:04:09 christos Exp $   */
+/*     $NetBSD: histedit.c,v 1.58 2022/01/31 16:54:28 kre Exp $        */
 
 /*-
  * Copyright (c) 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)histedit.c 8.2 (Berkeley) 5/4/95";
 #else
-__RCSID("$NetBSD: histedit.c,v 1.57 2021/09/14 15:04:09 christos Exp $");
+__RCSID("$NetBSD: histedit.c,v 1.58 2022/01/31 16:54:28 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -129,6 +129,23 @@
                        if (tracefile)
                                el_err = tracefile;
 #endif
+                       /*
+                        * This odd piece of code doesn't affect the shell
+                        * at all, the environment modified here is the
+                        * stuff accessed via "environ" (the incoming
+                        * envoironment to the shell) which is only ever
+                        * touched at sh startup time (long before we get
+                        * here) and ignored thereafter.
+                        *
+                        * But libedit calls getenv() to discover TERM
+                        * and that searches the "environ" environment,
+                        * not the shell's internal variable data struct,
+                        * so we need to make sure that TERM in there is
+                        * correct.
+                        *
+                        * This sequence copies TERM from the shell into
+                        * the old "environ" environment.
+                        */
                        term = lookupvar("TERM");
                        if (term)
                                setenv("TERM", term, 1);



Home | Main Index | Thread Index | Old Index