| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | /* $NetBSD: var.c,v 1.282 2020/07/20 15:48:50 rillig Exp $ */ | | 1 | /* $NetBSD: var.c,v 1.283 2020/07/20 16:12:52 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,34 +59,34 @@ | | | @@ -59,34 +59,34 @@ |
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.282 2020/07/20 15:48:50 rillig Exp $"; | | 72 | static char rcsid[] = "$NetBSD: var.c,v 1.283 2020/07/20 16:12:52 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.282 2020/07/20 15:48:50 rillig Exp $"); | | 79 | __RCSID("$NetBSD: var.c,v 1.283 2020/07/20 16:12:52 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. The value and variable name need not | | 91 | * yet exist. The value and variable name need not |
92 | * be preserved. | | 92 | * be preserved. |
| @@ -2904,27 +2904,47 @@ ApplyModifier_IfElse(ApplyModifiersState | | | @@ -2904,27 +2904,47 @@ ApplyModifier_IfElse(ApplyModifiersState |
2904 | | | 2904 | |
2905 | if (value) { | | 2905 | if (value) { |
2906 | st->newStr = then_expr; | | 2906 | st->newStr = then_expr; |
2907 | free(else_expr); | | 2907 | free(else_expr); |
2908 | } else { | | 2908 | } else { |
2909 | st->newStr = else_expr; | | 2909 | st->newStr = else_expr; |
2910 | free(then_expr); | | 2910 | free(then_expr); |
2911 | } | | 2911 | } |
2912 | if (st->v->flags & VAR_JUNK) | | 2912 | if (st->v->flags & VAR_JUNK) |
2913 | st->v->flags |= VAR_KEEP; | | 2913 | st->v->flags |= VAR_KEEP; |
2914 | return TRUE; | | 2914 | return TRUE; |
2915 | } | | 2915 | } |
2916 | | | 2916 | |
2917 | /* "::=", "::!=", "::+=", or "::?=" */ | | 2917 | /* |
| | | 2918 | * The ::= modifiers actually assign a value to the variable. |
| | | 2919 | * Their main purpose is in supporting modifiers of .for loop |
| | | 2920 | * iterators and other obscure uses. They always expand to |
| | | 2921 | * nothing. In a target rule that would otherwise expand to an |
| | | 2922 | * empty line they can be preceded with @: to keep make happy. |
| | | 2923 | * Eg. |
| | | 2924 | * |
| | | 2925 | * foo: .USE |
| | | 2926 | * .for i in ${.TARGET} ${.TARGET:R}.gz |
| | | 2927 | * @: ${t::=$i} |
| | | 2928 | * @echo blah ${t:T} |
| | | 2929 | * .endfor |
| | | 2930 | * |
| | | 2931 | * ::=<str> Assigns <str> as the new value of variable. |
| | | 2932 | * ::?=<str> Assigns <str> as value of variable if |
| | | 2933 | * it was not already set. |
| | | 2934 | * ::+=<str> Appends <str> to variable. |
| | | 2935 | * ::!=<cmd> Assigns output of <cmd> as the new value of |
| | | 2936 | * variable. |
| | | 2937 | */ |
2918 | static int | | 2938 | static int |
2919 | ApplyModifier_Assign(ApplyModifiersState *st) | | 2939 | ApplyModifier_Assign(ApplyModifiersState *st) |
2920 | { | | 2940 | { |
2921 | const char *op = st->tstr + 1; | | 2941 | const char *op = st->tstr + 1; |
2922 | if (!(op[0] == '=' || | | 2942 | if (!(op[0] == '=' || |
2923 | (op[1] == '=' && | | 2943 | (op[1] == '=' && |
2924 | (op[0] == '!' || op[0] == '+' || op[0] == '?')))) | | 2944 | (op[0] == '!' || op[0] == '+' || op[0] == '?')))) |
2925 | return 'd'; /* "::<unrecognised>" */ | | 2945 | return 'd'; /* "::<unrecognised>" */ |
2926 | | | 2946 | |
2927 | GNode *v_ctxt; /* context where v belongs */ | | 2947 | GNode *v_ctxt; /* context where v belongs */ |
2928 | VarPatternFlags pflags; | | 2948 | VarPatternFlags pflags; |
2929 | /* FIXME: Assign has nothing to do with VarPatternFlags */ | | 2949 | /* FIXME: Assign has nothing to do with VarPatternFlags */ |
2930 | | | 2950 | |
| @@ -3148,45 +3168,27 @@ ApplyModifier_SysV(ApplyModifiersState * | | | @@ -3148,45 +3168,27 @@ ApplyModifier_SysV(ApplyModifiersState * |
3148 | * and replace each word with the result of | | 3168 | * and replace each word with the result of |
3149 | * evaluating <newval> | | 3169 | * evaluating <newval> |
3150 | * :D<newval> Use <newval> as value if variable defined | | 3170 | * :D<newval> Use <newval> as value if variable defined |
3151 | * :U<newval> Use <newval> as value if variable undefined | | 3171 | * :U<newval> Use <newval> as value if variable undefined |
3152 | * :L Use the name of the variable as the value. | | 3172 | * :L Use the name of the variable as the value. |
3153 | * :P Use the path of the node that has the same | | 3173 | * :P Use the path of the node that has the same |
3154 | * name as the variable as the value. This | | 3174 | * name as the variable as the value. This |
3155 | * basically includes an implied :L so that | | 3175 | * basically includes an implied :L so that |
3156 | * the common method of refering to the path | | 3176 | * the common method of refering to the path |
3157 | * of your dependent 'x' in a rule is to use | | 3177 | * of your dependent 'x' in a rule is to use |
3158 | * the form '${x:P}'. | | 3178 | * the form '${x:P}'. |
3159 | * :!<cmd>! Run cmd much the same as :sh run's the | | 3179 | * :!<cmd>! Run cmd much the same as :sh run's the |
3160 | * current value of the variable. | | 3180 | * current value of the variable. |
3161 | * The ::= modifiers, actually assign a value to the variable. | | 3181 | * Assignment operators (see ApplyModifier_Assign). |
3162 | * Their main purpose is in supporting modifiers of .for loop | | | |
3163 | * iterators and other obscure uses. They always expand to | | | |
3164 | * nothing. In a target rule that would otherwise expand to an | | | |
3165 | * empty line they can be preceded with @: to keep make happy. | | | |
3166 | * Eg. | | | |
3167 | * | | | |
3168 | * foo: .USE | | | |
3169 | * .for i in ${.TARGET} ${.TARGET:R}.gz | | | |
3170 | * @: ${t::=$i} | | | |
3171 | * @echo blah ${t:T} | | | |
3172 | * .endfor | | | |
3173 | * | | | |
3174 | * ::=<str> Assigns <str> as the new value of variable. | | | |
3175 | * ::?=<str> Assigns <str> as value of variable if | | | |
3176 | * it was not already set. | | | |
3177 | * ::+=<str> Appends <str> to variable. | | | |
3178 | * ::!=<cmd> Assigns output of <cmd> as the new value of | | | |
3179 | * variable. | | | |
3180 | */ | | 3182 | */ |
3181 | static char * | | 3183 | static char * |
3182 | ApplyModifiers(char *nstr, const char *tstr, | | 3184 | ApplyModifiers(char *nstr, const char *tstr, |
3183 | int const startc, int const endc, | | 3185 | int const startc, int const endc, |
3184 | Var * const v, GNode * const ctxt, VarEvalFlags const eflags, | | 3186 | Var * const v, GNode * const ctxt, VarEvalFlags const eflags, |
3185 | int * const lengthPtr, void ** const freePtr) | | 3187 | int * const lengthPtr, void ** const freePtr) |
3186 | { | | 3188 | { |
3187 | ApplyModifiersState st = { | | 3189 | ApplyModifiersState st = { |
3188 | startc, endc, v, ctxt, eflags, lengthPtr, freePtr, | | 3190 | startc, endc, v, ctxt, eflags, lengthPtr, freePtr, |
3189 | nstr, tstr, tstr, tstr, | | 3191 | nstr, tstr, tstr, tstr, |
3190 | '\0', '\0', 0, {' ', FALSE}, NULL | | 3192 | '\0', '\0', 0, {' ', FALSE}, NULL |
3191 | }; | | 3193 | }; |
3192 | | | 3194 | |