Module Name:    src
Committed By:   rillig
Date:           Sat Aug  8 13:00:07 UTC 2020

Modified Files:
        src/usr.bin/make: nonints.h var.c
        src/usr.bin/make/unit-tests: export-variants.mk export.mk lint.mk
            unexport.mk

Log Message:
make(1): clean up Var_UnExport

Mark the parameter as constant since it is not modified.

Remove tests for '\n' since these can never succeed.

newenv can never be NULL since neither of bmake_malloc or bmake_realloc
returns NULL.

Improve variable names: vlist was too unexpressive.

Add debug logging since unexporting variables is an uncommon operation
that directly affects the observable environment of the child processes.

Fix CRLF line endings in a few unit tests.


To generate a diff of this commit:
cvs rdiff -u -r1.89 -r1.90 src/usr.bin/make/nonints.h
cvs rdiff -u -r1.422 -r1.423 src/usr.bin/make/var.c
cvs rdiff -u -r1.1 -r1.2 src/usr.bin/make/unit-tests/export-variants.mk \
    src/usr.bin/make/unit-tests/lint.mk
cvs rdiff -u -r1.4 -r1.5 src/usr.bin/make/unit-tests/export.mk
cvs rdiff -u -r1.2 -r1.3 src/usr.bin/make/unit-tests/unexport.mk

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/make/nonints.h
diff -u src/usr.bin/make/nonints.h:1.89 src/usr.bin/make/nonints.h:1.90
--- src/usr.bin/make/nonints.h:1.89	Sat Aug  1 18:02:37 2020
+++ src/usr.bin/make/nonints.h	Sat Aug  8 13:00:07 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: nonints.h,v 1.89 2020/08/01 18:02:37 rillig Exp $	*/
+/*	$NetBSD: nonints.h,v 1.90 2020/08/08 13:00:07 rillig Exp $	*/
 
 /*-
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -200,8 +200,8 @@ void Var_End(void);
 void Var_Stats(void);
 void Var_Dump(GNode *);
 void Var_ExportVars(void);
-void Var_Export(char *, int);
-void Var_UnExport(char *);
+void Var_Export(const char *, int);
+void Var_UnExport(const char *);
 
 /* util.c */
 void (*bmake_signal(int, void (*)(int)))(int);

Index: src/usr.bin/make/var.c
diff -u src/usr.bin/make/var.c:1.422 src/usr.bin/make/var.c:1.423
--- src/usr.bin/make/var.c:1.422	Sat Aug  8 12:43:06 2020
+++ src/usr.bin/make/var.c	Sat Aug  8 13:00:07 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: var.c,v 1.422 2020/08/08 12:43:06 rillig Exp $	*/
+/*	$NetBSD: var.c,v 1.423 2020/08/08 13:00:07 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -69,14 +69,14 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: var.c,v 1.422 2020/08/08 12:43:06 rillig Exp $";
+static char rcsid[] = "$NetBSD: var.c,v 1.423 2020/08/08 13:00:07 rillig Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
 #if 0
 static char sccsid[] = "@(#)var.c	8.3 (Berkeley) 3/19/94";
 #else
-__RCSID("$NetBSD: var.c,v 1.422 2020/08/08 12:43:06 rillig Exp $");
+__RCSID("$NetBSD: var.c,v 1.423 2020/08/08 13:00:07 rillig Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -603,7 +603,7 @@ Var_ExportVars(void)
  * It is also called when any exported variable is modified.
  */
 void
-Var_Export(char *str, int isExport)
+Var_Export(const char *str, int isExport)
 {
     VarExportFlags flags;
     char *val;
@@ -655,19 +655,21 @@ extern char **environ;
  * str must have the form "unexport[-env] varname...".
  */
 void
-Var_UnExport(char *str)
+Var_UnExport(const char *str)
 {
     char tmp[BUFSIZ];
-    char *vlist;
-    char *cp;
+    const char *varnames;
+    char *varnames_freeIt;
     int n;
     Boolean unexport_env;
 
-    vlist = NULL;
+    varnames = NULL;
+    varnames_freeIt = NULL;
 
     str += strlen("unexport");
     unexport_env = strncmp(str, "-env", 4) == 0;
     if (unexport_env) {
+	const char *cp;
 	char **newenv;
 
 	cp = getenv(MAKE_LEVEL_ENV);	/* we should preserve this */
@@ -681,8 +683,7 @@ Var_UnExport(char *str)
 	    }
 	    newenv = bmake_malloc(2 * sizeof(char *));
 	}
-	if (!newenv)
-	    return;
+
 	/* Note: we cannot safely free() the original environ. */
 	environ = savedEnv = newenv;
 	newenv[0] = NULL;
@@ -690,45 +691,50 @@ Var_UnExport(char *str)
 	if (cp && *cp)
 	    setenv(MAKE_LEVEL_ENV, cp, 1);
     } else {
-	for (; *str != '\n' && isspace((unsigned char)*str); str++)
+	for (; isspace((unsigned char)*str); str++)
 	    continue;
-	if (str[0] && str[0] != '\n') {
-	    vlist = str;
-	}
+	if (str[0] != '\0')
+	    varnames = str;
     }
 
-    if (!vlist) {
+    if (varnames == NULL) {
 	/* Using .MAKE.EXPORTED */
-	vlist = Var_Subst("${" MAKE_EXPORTED ":O:u}", VAR_GLOBAL,
-			  VARE_WANTRES);
+	varnames = varnames_freeIt = Var_Subst("${" MAKE_EXPORTED ":O:u}",
+					       VAR_GLOBAL, VARE_WANTRES);
     }
-    if (vlist) {
+
+    if (TRUE) {
 	Var *v;
 	char **av;
 	char *as;
 	int ac;
 	int i;
 
-	av = brk_string(vlist, &ac, FALSE, &as);
+	av = brk_string(varnames, &ac, FALSE, &as);
 	for (i = 0; i < ac; i++) {
 	    v = VarFind(av[i], VAR_GLOBAL, 0);
-	    if (!v)
+	    if (v == NULL) {
+		VAR_DEBUG("Not unexporting \"%s\" (not found)\n", av[i]);
 		continue;
-	    if (!unexport_env &&
-		(v->flags & (VAR_EXPORTED | VAR_REEXPORT)) == VAR_EXPORTED)
+	    }
+
+	    VAR_DEBUG("Unexporting \"%s\"\n", av[i]);
+	    if (!unexport_env && (v->flags & VAR_EXPORTED) &&
+		!(v->flags & VAR_REEXPORT))
 		unsetenv(v->name);
 	    v->flags &= ~(VAR_EXPORTED | VAR_REEXPORT);
+
 	    /*
 	     * If we are unexporting a list,
 	     * remove each one from .MAKE.EXPORTED.
 	     * If we are removing them all,
 	     * just delete .MAKE.EXPORTED below.
 	     */
-	    if (vlist == str) {
+	    if (varnames == str) {
 		n = snprintf(tmp, sizeof(tmp),
 			     "${" MAKE_EXPORTED ":N%s}", v->name);
 		if (n < (int)sizeof(tmp)) {
-		    cp = Var_Subst(tmp, VAR_GLOBAL, VARE_WANTRES);
+		    char *cp = Var_Subst(tmp, VAR_GLOBAL, VARE_WANTRES);
 		    Var_Set(MAKE_EXPORTED, cp, VAR_GLOBAL);
 		    free(cp);
 		}
@@ -736,9 +742,9 @@ Var_UnExport(char *str)
 	}
 	free(as);
 	free(av);
-	if (vlist != str) {
+	if (varnames != str) {
 	    Var_Delete(MAKE_EXPORTED, VAR_GLOBAL);
-	    free(vlist);
+	    free(varnames_freeIt);
 	}
     }
 }

Index: src/usr.bin/make/unit-tests/export-variants.mk
diff -u src/usr.bin/make/unit-tests/export-variants.mk:1.1 src/usr.bin/make/unit-tests/export-variants.mk:1.2
--- src/usr.bin/make/unit-tests/export-variants.mk:1.1	Fri Aug  7 19:24:27 2020
+++ src/usr.bin/make/unit-tests/export-variants.mk	Sat Aug  8 13:00:07 2020
@@ -1,40 +1,40 @@
-# $NetBSD: export-variants.mk,v 1.1 2020/08/07 19:24:27 rillig Exp $
-#
-# Test whether exported variables apply to each variant of running
-# external commands:
-#
-# The != assignments.
-# The :!cmd! modifier.
-# The :sh modifier.
-
-SHVAR!=	env | grep ^UT_ || true
-.if ${SHVAR} != ""
-.warning At this point, no variable should be exported.
-.endif
-
-.if ${:!env | grep ^UT_ || true!} != ""
-.warning At this point, no variable should be exported.
-.endif
-
-.if ${env | grep ^UT_ || true:L:sh} != ""
-.warning At this point, no variable should be exported.
-.endif
-
-UT_VAR=		value
-.export UT_VAR
-
-SHVAR!=	env | grep ^UT_ || true
-.if ${SHVAR} != "UT_VAR=value"
-.warning At this point, no variable should be exported.
-.endif
-
-.if ${:!env | grep ^UT_ || true!} != "UT_VAR=value"
-.warning At this point, some variables should be exported.
-.endif
-
-.if ${env | grep ^UT_ || true:L:sh} != "UT_VAR=value"
-.warning At this point, some variables should be exported.
-.endif
-
-all:
-	@:;
+# $NetBSD: export-variants.mk,v 1.2 2020/08/08 13:00:07 rillig Exp $
+#
+# Test whether exported variables apply to each variant of running
+# external commands:
+#
+# The != assignments.
+# The :!cmd! modifier.
+# The :sh modifier.
+
+SHVAR!=	env | grep ^UT_ || true
+.if ${SHVAR} != ""
+.warning At this point, no variable should be exported.
+.endif
+
+.if ${:!env | grep ^UT_ || true!} != ""
+.warning At this point, no variable should be exported.
+.endif
+
+.if ${env | grep ^UT_ || true:L:sh} != ""
+.warning At this point, no variable should be exported.
+.endif
+
+UT_VAR=		value
+.export UT_VAR
+
+SHVAR!=	env | grep ^UT_ || true
+.if ${SHVAR} != "UT_VAR=value"
+.warning At this point, no variable should be exported.
+.endif
+
+.if ${:!env | grep ^UT_ || true!} != "UT_VAR=value"
+.warning At this point, some variables should be exported.
+.endif
+
+.if ${env | grep ^UT_ || true:L:sh} != "UT_VAR=value"
+.warning At this point, some variables should be exported.
+.endif
+
+all:
+	@:;
Index: src/usr.bin/make/unit-tests/lint.mk
diff -u src/usr.bin/make/unit-tests/lint.mk:1.1 src/usr.bin/make/unit-tests/lint.mk:1.2
--- src/usr.bin/make/unit-tests/lint.mk:1.1	Mon Aug  3 15:43:32 2020
+++ src/usr.bin/make/unit-tests/lint.mk	Sat Aug  8 13:00:07 2020
@@ -1,17 +1,17 @@
-# $NetBSD: lint.mk,v 1.1 2020/08/03 15:43:32 rillig Exp $
-#
-# Demonstrates stricter checks that are only enabled in the lint mode,
-# using the -dL option.
-
-# Ouch: as of 2020-08-03, make exits successfully even though the error
-# message has been issued as PARSE_FATAL.
-
-# Ouch: as of 2020-08-03, the variable is malformed and parsing stops
-# for a moment, but is continued after the wrongly-guessed end of the
-# variable, which echoes "y@:Q}".
-
-all: mod-loop-varname
-
-mod-loop-varname:
-	@echo ${VAR:Uvalue:@${:Ubar:S,b,v,}@x${var}y@:Q}
-	@echo ${VAR:Uvalue:@!@x$!y@:Q}	# surprisingly allowed
+# $NetBSD: lint.mk,v 1.2 2020/08/08 13:00:07 rillig Exp $
+#
+# Demonstrates stricter checks that are only enabled in the lint mode,
+# using the -dL option.
+
+# Ouch: as of 2020-08-03, make exits successfully even though the error
+# message has been issued as PARSE_FATAL.
+
+# Ouch: as of 2020-08-03, the variable is malformed and parsing stops
+# for a moment, but is continued after the wrongly-guessed end of the
+# variable, which echoes "y@:Q}".
+
+all: mod-loop-varname
+
+mod-loop-varname:
+	@echo ${VAR:Uvalue:@${:Ubar:S,b,v,}@x${var}y@:Q}
+	@echo ${VAR:Uvalue:@!@x$!y@:Q}	# surprisingly allowed

Index: src/usr.bin/make/unit-tests/export.mk
diff -u src/usr.bin/make/unit-tests/export.mk:1.4 src/usr.bin/make/unit-tests/export.mk:1.5
--- src/usr.bin/make/unit-tests/export.mk:1.4	Tue Jul 28 18:53:07 2020
+++ src/usr.bin/make/unit-tests/export.mk	Sat Aug  8 13:00:07 2020
@@ -1,11 +1,11 @@
-# $Id: export.mk,v 1.4 2020/07/28 18:53:07 sjg Exp $
+# $Id: export.mk,v 1.5 2020/08/08 13:00:07 rillig Exp $
 
 UT_TEST=export
 UT_FOO=foo${BAR}
 UT_FU=fubar
 UT_ZOO=hoopie
 UT_NO=all
-# belive it or not, we expect this one to come out with $UT_FU unexpanded.
+# believe it or not, we expect this one to come out with $UT_FU unexpanded.
 UT_DOLLAR= This is $$UT_FU
 
 .export UT_FU UT_FOO

Index: src/usr.bin/make/unit-tests/unexport.mk
diff -u src/usr.bin/make/unit-tests/unexport.mk:1.2 src/usr.bin/make/unit-tests/unexport.mk:1.3
--- src/usr.bin/make/unit-tests/unexport.mk:1.2	Mon Jul 27 19:45:56 2020
+++ src/usr.bin/make/unit-tests/unexport.mk	Sat Aug  8 13:00:07 2020
@@ -1,4 +1,4 @@
-# $Id: unexport.mk,v 1.2 2020/07/27 19:45:56 rillig Exp $
+# $Id: unexport.mk,v 1.3 2020/08/08 13:00:07 rillig Exp $
 
 # pick up a bunch of exported vars
 FILTER_CMD=	grep ^UT_
@@ -7,3 +7,13 @@ FILTER_CMD=	grep ^UT_
 .unexport UT_ZOO UT_FOO
 
 UT_TEST = unexport
+
+# Until 2020-08-08, Var_UnExport had special handling for '\n', that code
+# was not reachable though.  At that point, backslash-newline has already
+# been replaced with a simple space, and variables are not yet expanded.
+UT_BEFORE_NL=	before
+UT_AFTER_NL=	after
+.export UT_BEFORE_NL UT_AFTER_NL
+.unexport \
+  UT_BEFORE_NL
+.unexport ${.newline} UT_AFTER_NL

Reply via email to