Module Name:    src
Committed By:   rillig
Date:           Tue Dec 29 03:21:09 UTC 2020

Modified Files:
        src/usr.bin/make: var.c

Log Message:
make(1): clean up code in extracted ExportVar functions


To generate a diff of this commit:
cvs rdiff -u -r1.776 -r1.777 src/usr.bin/make/var.c

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/var.c
diff -u src/usr.bin/make/var.c:1.776 src/usr.bin/make/var.c:1.777
--- src/usr.bin/make/var.c:1.776	Tue Dec 29 03:05:15 2020
+++ src/usr.bin/make/var.c	Tue Dec 29 03:21:09 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: var.c,v 1.776 2020/12/29 03:05:15 rillig Exp $	*/
+/*	$NetBSD: var.c,v 1.777 2020/12/29 03:21:09 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -131,7 +131,7 @@
 #include "metachar.h"
 
 /*	"@(#)var.c	8.3 (Berkeley) 3/19/94" */
-MAKE_RCSID("$NetBSD: var.c,v 1.776 2020/12/29 03:05:15 rillig Exp $");
+MAKE_RCSID("$NetBSD: var.c,v 1.777 2020/12/29 03:21:09 rillig Exp $");
 
 typedef enum VarFlags {
 	VAR_NONE	= 0,
@@ -579,155 +579,64 @@ MayExport(const char *name)
 static Boolean
 ExportVarEnv(Var *v)
 {
-	VarExportMode mode = VEM_ENV;
 	const char *name = v->name.str;
-	char *val;
+	char *val = v->val.data;
+	char *expr;
 
-	if (!(mode == VEM_PLAIN) && (v->flags & VAR_EXPORTED) && !(v->flags & VAR_REEXPORT))
+	if ((v->flags & VAR_EXPORTED) && !(v->flags & VAR_REEXPORT))
 		return FALSE;	/* nothing to do */
 
-	val = Buf_GetAll(&v->val, NULL);
-	if (mode != VEM_LITERAL && strchr(val, '$') != NULL) {
-		char *expr;
-
-		if (mode == VEM_PLAIN) {
-			/*
-			 * Flag the variable as something we need to re-export.
-			 * No point actually exporting it now though,
-			 * the child process can do it at the last minute.
-			 */
-			v->flags |= VAR_EXPORTED | VAR_REEXPORT;
-			return TRUE;
-		}
-		if (v->flags & VAR_IN_USE) {
-			/*
-			 * We recursed while exporting in a child.
-			 * This isn't going to end well, just skip it.
-			 */
-			return FALSE;
-		}
-
-		/* XXX: name is injected without escaping it */
-		expr = str_concat3("${", name, "}");
-		(void)Var_Subst(expr, VAR_GLOBAL, VARE_WANTRES, &val);
-		/* TODO: handle errors */
-		setenv(name, val, 1);
-		free(val);
-		free(expr);
-	} else {
-		if (mode == VEM_PLAIN)
-			v->flags &= ~(unsigned)VAR_REEXPORT; /* once will do */
-		if (mode == VEM_PLAIN || !(v->flags & VAR_EXPORTED))
+	if (strchr(val, '$') == NULL) {
+		if (!(v->flags & VAR_EXPORTED))
 			setenv(name, val, 1);
+		return TRUE;
 	}
 
-	/* This is so Var_Set knows to call Var_Export again. */
-	if (mode == VEM_PLAIN)
-		v->flags |= VAR_EXPORTED;
+	if (v->flags & VAR_IN_USE) {
+		/*
+		 * We recursed while exporting in a child.
+		 * This isn't going to end well, just skip it.
+		 */
+		return FALSE;
+	}
 
+	/* XXX: name is injected without escaping it */
+	expr = str_concat3("${", name, "}");
+	(void)Var_Subst(expr, VAR_GLOBAL, VARE_WANTRES, &val);
+	/* TODO: handle errors */
+	setenv(name, val, 1);
+	free(val);
+	free(expr);
 	return TRUE;
 }
 
 static Boolean
 ExportVarPlain(Var *v)
 {
-	VarExportMode mode = VEM_PLAIN;
-	const char *name = v->name.str;
-	char *val;
-
-	if (!(mode == VEM_PLAIN) && (v->flags & VAR_EXPORTED) && !(v->flags & VAR_REEXPORT))
-		return FALSE;	/* nothing to do */
-
-	val = Buf_GetAll(&v->val, NULL);
-	if (mode != VEM_LITERAL && strchr(val, '$') != NULL) {
-		char *expr;
-
-		if (mode == VEM_PLAIN) {
-			/*
-			 * Flag the variable as something we need to re-export.
-			 * No point actually exporting it now though,
-			 * the child process can do it at the last minute.
-			 */
-			v->flags |= VAR_EXPORTED | VAR_REEXPORT;
-			return TRUE;
-		}
-		if (v->flags & VAR_IN_USE) {
-			/*
-			 * We recursed while exporting in a child.
-			 * This isn't going to end well, just skip it.
-			 */
-			return FALSE;
-		}
-
-		/* XXX: name is injected without escaping it */
-		expr = str_concat3("${", name, "}");
-		(void)Var_Subst(expr, VAR_GLOBAL, VARE_WANTRES, &val);
-		/* TODO: handle errors */
-		setenv(name, val, 1);
-		free(val);
-		free(expr);
-	} else {
-		if (mode == VEM_PLAIN)
-			v->flags &= ~(unsigned)VAR_REEXPORT; /* once will do */
-		if (mode == VEM_PLAIN || !(v->flags & VAR_EXPORTED))
-			setenv(name, val, 1);
-	}
-
-	/* This is so Var_Set knows to call Var_Export again. */
-	if (mode == VEM_PLAIN)
+	if (strchr(v->val.data, '$') == NULL) {
+		setenv(v->name.str, v->val.data, 1);
 		v->flags |= VAR_EXPORTED;
+		v->flags &= ~(unsigned)VAR_REEXPORT;
+		return TRUE;
+	}
 
+	/*
+	 * Flag the variable as something we need to re-export.
+	 * No point actually exporting it now though,
+	 * the child process can do it at the last minute.
+	 */
+	v->flags |= VAR_EXPORTED | VAR_REEXPORT;
 	return TRUE;
 }
 
 static Boolean
 ExportVarLiteral(Var *v)
 {
-	VarExportMode mode = VEM_LITERAL;
-	const char *name = v->name.str;
-	char *val;
-
-	if (!(mode == VEM_PLAIN) && (v->flags & VAR_EXPORTED) && !(v->flags & VAR_REEXPORT))
-		return FALSE;	/* nothing to do */
-
-	val = Buf_GetAll(&v->val, NULL);
-	if (mode != VEM_LITERAL && strchr(val, '$') != NULL) {
-		char *expr;
-
-		if (mode == VEM_PLAIN) {
-			/*
-			 * Flag the variable as something we need to re-export.
-			 * No point actually exporting it now though,
-			 * the child process can do it at the last minute.
-			 */
-			v->flags |= VAR_EXPORTED | VAR_REEXPORT;
-			return TRUE;
-		}
-		if (v->flags & VAR_IN_USE) {
-			/*
-			 * We recursed while exporting in a child.
-			 * This isn't going to end well, just skip it.
-			 */
-			return FALSE;
-		}
-
-		/* XXX: name is injected without escaping it */
-		expr = str_concat3("${", name, "}");
-		(void)Var_Subst(expr, VAR_GLOBAL, VARE_WANTRES, &val);
-		/* TODO: handle errors */
-		setenv(name, val, 1);
-		free(val);
-		free(expr);
-	} else {
-		if (mode == VEM_PLAIN)
-			v->flags &= ~(unsigned)VAR_REEXPORT; /* once will do */
-		if (mode == VEM_PLAIN || !(v->flags & VAR_EXPORTED))
-			setenv(name, val, 1);
-	}
+	if ((v->flags & VAR_EXPORTED) && !(v->flags & VAR_REEXPORT))
+		return FALSE;
 
-	/* This is so Var_Set knows to call Var_Export again. */
-	if (mode == VEM_PLAIN)
-		v->flags |= VAR_EXPORTED;
+	if (!(v->flags & VAR_EXPORTED))
+		setenv(v->name.str, v->val.data, 1);
 
 	return TRUE;
 }

Reply via email to