Sat Dec 12 20:00:52 2020 UTC ()
make(1): extract ExportVarsExpand from Var_Export


(rillig)
diff -r1.727 -r1.728 src/usr.bin/make/var.c

cvs diff -r1.727 -r1.728 src/usr.bin/make/var.c (expand / switch to unified diff)

--- src/usr.bin/make/var.c 2020/12/12 19:39:34 1.727
+++ src/usr.bin/make/var.c 2020/12/12 20:00:51 1.728
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: var.c,v 1.727 2020/12/12 19:39:34 rillig Exp $ */ 1/* $NetBSD: var.c,v 1.728 2020/12/12 20:00:51 rillig Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 1988, 1989, 1990, 1993 4 * Copyright (c) 1988, 1989, 1990, 1993
5 * The Regents of the University of California. All rights reserved. 5 * The Regents of the University of California. All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to Berkeley by 7 * This code is derived from software contributed to Berkeley by
8 * Adam de Boor. 8 * Adam de Boor.
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
@@ -121,27 +121,27 @@ @@ -121,27 +121,27 @@
121#include <regex.h> 121#include <regex.h>
122#endif 122#endif
123#include <errno.h> 123#include <errno.h>
124#include <inttypes.h> 124#include <inttypes.h>
125#include <limits.h> 125#include <limits.h>
126#include <time.h> 126#include <time.h>
127 127
128#include "make.h" 128#include "make.h"
129#include "dir.h" 129#include "dir.h"
130#include "job.h" 130#include "job.h"
131#include "metachar.h" 131#include "metachar.h"
132 132
133/* "@(#)var.c 8.3 (Berkeley) 3/19/94" */ 133/* "@(#)var.c 8.3 (Berkeley) 3/19/94" */
134MAKE_RCSID("$NetBSD: var.c,v 1.727 2020/12/12 19:39:34 rillig Exp $"); 134MAKE_RCSID("$NetBSD: var.c,v 1.728 2020/12/12 20:00:51 rillig Exp $");
135 135
136/* A string that may need to be freed after use. */ 136/* A string that may need to be freed after use. */
137typedef struct FStr { 137typedef struct FStr {
138 const char *str; 138 const char *str;
139 void *freeIt; 139 void *freeIt;
140} FStr; 140} FStr;
141 141
142typedef enum VarFlags { 142typedef enum VarFlags {
143 VAR_NONE = 0, 143 VAR_NONE = 0,
144 144
145 /* 145 /*
146 * The variable's value is currently being used by Var_Parse or 146 * The variable's value is currently being used by Var_Parse or
147 * Var_Subst. This marker is used to avoid endless recursion. 147 * Var_Subst. This marker is used to avoid endless recursion.
@@ -684,59 +684,66 @@ ExportVars(const char *varnames, Boolean @@ -684,59 +684,66 @@ ExportVars(const char *varnames, Boolean
684 const char *varname = words.words[i]; 684 const char *varname = words.words[i];
685 if (!ExportVar(varname, flags)) 685 if (!ExportVar(varname, flags))
686 continue; 686 continue;
687 687
688 if (var_exportedVars == VAR_EXPORTED_NONE) 688 if (var_exportedVars == VAR_EXPORTED_NONE)
689 var_exportedVars = VAR_EXPORTED_SOME; 689 var_exportedVars = VAR_EXPORTED_SOME;
690 690
691 if (isExport && (flags & VAR_EXPORT_PARENT)) 691 if (isExport && (flags & VAR_EXPORT_PARENT))
692 Var_Append(MAKE_EXPORTED, varname, VAR_GLOBAL); 692 Var_Append(MAKE_EXPORTED, varname, VAR_GLOBAL);
693 } 693 }
694 Words_Free(words); 694 Words_Free(words);
695} 695}
696 696
 697static void
 698ExportVarsExpand(const char *uvarnames, Boolean isExport, VarExportFlags flags)
 699{
 700 char *xvarnames;
 701
 702 (void)Var_Subst(uvarnames, VAR_GLOBAL, VARE_WANTRES, &xvarnames);
 703 /* TODO: handle errors */
 704 ExportVars(xvarnames, isExport, flags);
 705 free(xvarnames);
 706}
 707
697/* 708/*
698 * This is called when .export is seen or .MAKE.EXPORTED is modified. 709 * This is called when .export is seen or .MAKE.EXPORTED is modified.
699 * 710 *
700 * It is also called when any exported variable is modified. 711 * It is also called when any exported variable is modified.
701 * XXX: Is it really? 712 * XXX: Is it really?
702 * 713 *
703 * str has the format "[-env|-literal] varname...". 714 * str has the format "[-env|-literal] varname...".
704 */ 715 */
705void 716void
706Var_Export(const char *str, Boolean isExport) 717Var_Export(const char *str, Boolean isExport)
707{ 718{
708 VarExportFlags flags; 719 VarExportFlags flags;
709 char *varnames; 
710 720
711 if (isExport && str[0] == '\0') { 721 if (isExport && str[0] == '\0') {
712 var_exportedVars = VAR_EXPORTED_ALL; /* use with caution! */ 722 var_exportedVars = VAR_EXPORTED_ALL; /* use with caution! */
713 return; 723 return;
714 } 724 }
715 725
716 if (isExport && strncmp(str, "-env", 4) == 0) { 726 if (isExport && strncmp(str, "-env", 4) == 0) {
717 str += 4; 727 str += 4;
718 flags = VAR_EXPORT_NORMAL; 728 flags = VAR_EXPORT_NORMAL;
719 } else if (isExport && strncmp(str, "-literal", 8) == 0) { 729 } else if (isExport && strncmp(str, "-literal", 8) == 0) {
720 str += 8; 730 str += 8;
721 flags = VAR_EXPORT_LITERAL; 731 flags = VAR_EXPORT_LITERAL;
722 } else { 732 } else {
723 flags = VAR_EXPORT_PARENT; 733 flags = VAR_EXPORT_PARENT;
724 } 734 }
725 735
726 (void)Var_Subst(str, VAR_GLOBAL, VARE_WANTRES, &varnames); 736 ExportVarsExpand(str, isExport, flags);
727 /* TODO: handle errors */ 
728 ExportVars(varnames, isExport, flags); 
729 free(varnames); 
730} 737}
731 738
732 739
733extern char **environ; 740extern char **environ;
734 741
735static void 742static void
736ClearEnv(void) 743ClearEnv(void)
737{ 744{
738 const char *cp; 745 const char *cp;
739 char **newenv; 746 char **newenv;
740 747
741 cp = getenv(MAKE_LEVEL_ENV); /* we should preserve this */ 748 cp = getenv(MAKE_LEVEL_ENV); /* we should preserve this */
742 if (environ == savedEnv) { 749 if (environ == savedEnv) {