| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | /* $NetBSD: var.c,v 1.336 2020/07/26 23:03:54 rillig Exp $ */ | | 1 | /* $NetBSD: var.c,v 1.337 2020/07/27 17:41:09 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. |
| @@ -59,57 +59,57 @@ | | | @@ -59,57 +59,57 @@ |
59 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | | 59 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
60 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | | 60 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
61 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | | 61 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
62 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | | 62 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
63 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | | 63 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
64 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | | 64 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
65 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | | 65 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
66 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | | 66 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
67 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | | 67 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
68 | * SUCH DAMAGE. | | 68 | * SUCH DAMAGE. |
69 | */ | | 69 | */ |
70 | | | 70 | |
71 | #ifndef MAKE_NATIVE | | 71 | #ifndef MAKE_NATIVE |
72 | static char rcsid[] = "$NetBSD: var.c,v 1.336 2020/07/26 23:03:54 rillig Exp $"; | | 72 | static char rcsid[] = "$NetBSD: var.c,v 1.337 2020/07/27 17:41:09 rillig Exp $"; |
73 | #else | | 73 | #else |
74 | #include <sys/cdefs.h> | | 74 | #include <sys/cdefs.h> |
75 | #ifndef lint | | 75 | #ifndef lint |
76 | #if 0 | | 76 | #if 0 |
77 | static char sccsid[] = "@(#)var.c 8.3 (Berkeley) 3/19/94"; | | 77 | static char sccsid[] = "@(#)var.c 8.3 (Berkeley) 3/19/94"; |
78 | #else | | 78 | #else |
79 | __RCSID("$NetBSD: var.c,v 1.336 2020/07/26 23:03:54 rillig Exp $"); | | 79 | __RCSID("$NetBSD: var.c,v 1.337 2020/07/27 17:41:09 rillig Exp $"); |
80 | #endif | | 80 | #endif |
81 | #endif /* not lint */ | | 81 | #endif /* not lint */ |
82 | #endif | | 82 | #endif |
83 | | | 83 | |
84 | /*- | | 84 | /*- |
85 | * var.c -- | | 85 | * var.c -- |
86 | * Variable-handling functions | | 86 | * Variable-handling functions |
87 | * | | 87 | * |
88 | * Interface: | | 88 | * Interface: |
89 | * Var_Set Set the value of a variable in the given | | 89 | * Var_Set Set the value of a variable in the given |
90 | * context. The variable is created if it doesn't | | 90 | * context. The variable is created if it doesn't |
91 | * yet exist. | | 91 | * yet exist. |
92 | * | | 92 | * |
93 | * Var_Append Append more characters to an existing variable | | 93 | * Var_Append Append more characters to an existing variable |
94 | * in the given context. The variable needn't | | 94 | * in the given context. The variable needn't |
95 | * exist already -- it will be created if it doesn't. | | 95 | * exist already -- it will be created if it doesn't. |
96 | * A space is placed between the old value and the | | 96 | * A space is placed between the old value and the |
97 | * new one. | | 97 | * new one. |
98 | * | | 98 | * |
99 | * Var_Exists See if a variable exists. | | 99 | * Var_Exists See if a variable exists. |
100 | * | | 100 | * |
101 | * Var_Value Return the value of a variable in a context or | | 101 | * Var_Value Return the unexpanded value of a variable in a |
102 | * NULL if the variable is undefined. | | 102 | * context or NULL if the variable is undefined. |
103 | * | | 103 | * |
104 | * Var_Subst Substitute either a single variable or all | | 104 | * Var_Subst Substitute either a single variable or all |
105 | * variables in a string, using the given context. | | 105 | * variables in a string, using the given context. |
106 | * | | 106 | * |
107 | * Var_Parse Parse a variable expansion from a string and | | 107 | * Var_Parse Parse a variable expansion from a string and |
108 | * return the result and the number of characters | | 108 | * return the result and the number of characters |
109 | * consumed. | | 109 | * consumed. |
110 | * | | 110 | * |
111 | * Var_Delete Delete a variable in a context. | | 111 | * Var_Delete Delete a variable in a context. |
112 | * | | 112 | * |
113 | * Var_Init Initialize this module. | | 113 | * Var_Init Initialize this module. |
114 | * | | 114 | * |
115 | * Debugging: | | 115 | * Debugging: |
| @@ -1005,50 +1005,50 @@ Var_Exists(const char *name, GNode *ctxt | | | @@ -1005,50 +1005,50 @@ Var_Exists(const char *name, GNode *ctxt |
1005 | cp = Var_Subst(NULL, name, ctxt, VARE_WANTRES); | | 1005 | cp = Var_Subst(NULL, name, ctxt, VARE_WANTRES); |
1006 | v = VarFind(cp ? cp : name, ctxt, FIND_CMD | FIND_GLOBAL | FIND_ENV); | | 1006 | v = VarFind(cp ? cp : name, ctxt, FIND_CMD | FIND_GLOBAL | FIND_ENV); |
1007 | free(cp); | | 1007 | free(cp); |
1008 | if (v == NULL) | | 1008 | if (v == NULL) |
1009 | return FALSE; | | 1009 | return FALSE; |
1010 | | | 1010 | |
1011 | (void)VarFreeEnv(v, TRUE); | | 1011 | (void)VarFreeEnv(v, TRUE); |
1012 | return TRUE; | | 1012 | return TRUE; |
1013 | } | | 1013 | } |
1014 | | | 1014 | |
1015 | /*- | | 1015 | /*- |
1016 | *----------------------------------------------------------------------- | | 1016 | *----------------------------------------------------------------------- |
1017 | * Var_Value -- | | 1017 | * Var_Value -- |
1018 | * Return the value of the named variable in the given context | | 1018 | * Return the unexpanded value of the given variable in the given |
| | | 1019 | * context. |
1019 | * | | 1020 | * |
1020 | * Input: | | 1021 | * Input: |
1021 | * name name to find | | 1022 | * name name to find |
1022 | * ctxt context in which to search for it | | 1023 | * ctxt context in which to search for it |
1023 | * | | 1024 | * |
1024 | * Results: | | 1025 | * Results: |
1025 | * The value if the variable exists, NULL if it doesn't | | 1026 | * The value if the variable exists, NULL if it doesn't. |
1026 | * | | 1027 | * If the returned value is not NULL, the caller must free *freeIt |
1027 | * Side Effects: | | 1028 | * as soon as the returned value is no longer needed. |
1028 | * None | | | |
1029 | *----------------------------------------------------------------------- | | 1029 | *----------------------------------------------------------------------- |
1030 | */ | | 1030 | */ |
1031 | char * | | 1031 | char * |
1032 | Var_Value(const char *name, GNode *ctxt, char **frp) | | 1032 | Var_Value(const char *name, GNode *ctxt, char **freeIt) |
1033 | { | | 1033 | { |
1034 | Var *v = VarFind(name, ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD); | | 1034 | Var *v = VarFind(name, ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD); |
1035 | *frp = NULL; | | 1035 | *freeIt = NULL; |
1036 | if (v == NULL) | | 1036 | if (v == NULL) |
1037 | return NULL; | | 1037 | return NULL; |
1038 | | | 1038 | |
1039 | char *p = Buf_GetAll(&v->val, NULL); | | 1039 | char *p = Buf_GetAll(&v->val, NULL); |
1040 | if (VarFreeEnv(v, FALSE)) | | 1040 | if (VarFreeEnv(v, FALSE)) |
1041 | *frp = p; | | 1041 | *freeIt = p; |
1042 | return p; | | 1042 | return p; |
1043 | } | | 1043 | } |
1044 | | | 1044 | |
1045 | | | 1045 | |
1046 | /* SepBuf is a string being built from "words", interleaved with separators. */ | | 1046 | /* SepBuf is a string being built from "words", interleaved with separators. */ |
1047 | typedef struct { | | 1047 | typedef struct { |
1048 | Buffer buf; | | 1048 | Buffer buf; |
1049 | Boolean needSep; | | 1049 | Boolean needSep; |
1050 | char sep; | | 1050 | char sep; |
1051 | } SepBuf; | | 1051 | } SepBuf; |
1052 | | | 1052 | |
1053 | static void | | 1053 | static void |
1054 | SepBuf_Init(SepBuf *buf, char sep) | | 1054 | SepBuf_Init(SepBuf *buf, char sep) |