| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | /* $NetBSD: var.c,v 1.531 2020/09/22 18:07:58 rillig Exp $ */ | | 1 | /* $NetBSD: var.c,v 1.532 2020/09/22 19:08:47 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. |
| @@ -111,27 +111,27 @@ | | | @@ -111,27 +111,27 @@ |
111 | #include <sys/types.h> | | 111 | #include <sys/types.h> |
112 | #include <regex.h> | | 112 | #include <regex.h> |
113 | #endif | | 113 | #endif |
114 | #include <inttypes.h> | | 114 | #include <inttypes.h> |
115 | #include <limits.h> | | 115 | #include <limits.h> |
116 | #include <time.h> | | 116 | #include <time.h> |
117 | | | 117 | |
118 | #include "make.h" | | 118 | #include "make.h" |
119 | #include "dir.h" | | 119 | #include "dir.h" |
120 | #include "job.h" | | 120 | #include "job.h" |
121 | #include "metachar.h" | | 121 | #include "metachar.h" |
122 | | | 122 | |
123 | /* "@(#)var.c 8.3 (Berkeley) 3/19/94" */ | | 123 | /* "@(#)var.c 8.3 (Berkeley) 3/19/94" */ |
124 | MAKE_RCSID("$NetBSD: var.c,v 1.531 2020/09/22 18:07:58 rillig Exp $"); | | 124 | MAKE_RCSID("$NetBSD: var.c,v 1.532 2020/09/22 19:08:47 rillig Exp $"); |
125 | | | 125 | |
126 | #define VAR_DEBUG_IF(cond, fmt, ...) \ | | 126 | #define VAR_DEBUG_IF(cond, fmt, ...) \ |
127 | if (!(DEBUG(VAR) && (cond))) \ | | 127 | if (!(DEBUG(VAR) && (cond))) \ |
128 | (void) 0; \ | | 128 | (void) 0; \ |
129 | else \ | | 129 | else \ |
130 | fprintf(debug_file, fmt, __VA_ARGS__) | | 130 | fprintf(debug_file, fmt, __VA_ARGS__) |
131 | | | 131 | |
132 | #define VAR_DEBUG(fmt, ...) VAR_DEBUG_IF(TRUE, fmt, __VA_ARGS__) | | 132 | #define VAR_DEBUG(fmt, ...) VAR_DEBUG_IF(TRUE, fmt, __VA_ARGS__) |
133 | | | 133 | |
134 | ENUM_FLAGS_RTTI_3(VarEvalFlags, | | 134 | ENUM_FLAGS_RTTI_3(VarEvalFlags, |
135 | VARE_UNDEFERR, VARE_WANTRES, VARE_ASSIGN); | | 135 | VARE_UNDEFERR, VARE_WANTRES, VARE_ASSIGN); |
136 | | | 136 | |
137 | /* | | 137 | /* |
| @@ -3699,40 +3699,35 @@ Var_Parse(const char **pp, GNode *ctxt, | | | @@ -3699,40 +3699,35 @@ Var_Parse(const char **pp, GNode *ctxt, |
3699 | * str the string which to substitute | | 3699 | * str the string which to substitute |
3700 | * ctxt the context wherein to find variables | | 3700 | * ctxt the context wherein to find variables |
3701 | * eflags VARE_UNDEFERR if undefineds are an error | | 3701 | * eflags VARE_UNDEFERR if undefineds are an error |
3702 | * VARE_WANTRES if we actually want the result | | 3702 | * VARE_WANTRES if we actually want the result |
3703 | * VARE_ASSIGN if we are in a := assignment | | 3703 | * VARE_ASSIGN if we are in a := assignment |
3704 | * | | 3704 | * |
3705 | * Results: | | 3705 | * Results: |
3706 | * The resulting string. | | 3706 | * The resulting string. |
3707 | */ | | 3707 | */ |
3708 | char * | | 3708 | char * |
3709 | Var_Subst(const char *str, GNode *ctxt, VarEvalFlags eflags) | | 3709 | Var_Subst(const char *str, GNode *ctxt, VarEvalFlags eflags) |
3710 | { | | 3710 | { |
3711 | Buffer buf; /* Buffer for forming things */ | | 3711 | Buffer buf; /* Buffer for forming things */ |
3712 | Boolean trailingBackslash; | | | |
3713 | | | 3712 | |
3714 | /* Set true if an error has already been reported, | | 3713 | /* Set true if an error has already been reported, |
3715 | * to prevent a plethora of messages when recursing */ | | 3714 | * to prevent a plethora of messages when recursing */ |
3716 | static Boolean errorReported; | | 3715 | static Boolean errorReported; |
3717 | | | 3716 | |
3718 | Buf_Init(&buf, 0); | | 3717 | Buf_Init(&buf, 0); |
3719 | errorReported = FALSE; | | 3718 | errorReported = FALSE; |
3720 | trailingBackslash = FALSE; /* variable ends in \ */ | | | |
3721 | | | 3719 | |
3722 | while (*str) { | | 3720 | while (*str) { |
3723 | if (*str == '\n' && trailingBackslash) | | | |
3724 | Buf_AddByte(&buf, ' '); | | | |
3725 | | | | |
3726 | if (*str == '$' && str[1] == '$') { | | 3721 | if (*str == '$' && str[1] == '$') { |
3727 | /* | | 3722 | /* |
3728 | * A dollar sign may be escaped with another dollar sign. | | 3723 | * A dollar sign may be escaped with another dollar sign. |
3729 | * In such a case, we skip over the escape character and store the | | 3724 | * In such a case, we skip over the escape character and store the |
3730 | * dollar sign into the buffer directly. | | 3725 | * dollar sign into the buffer directly. |
3731 | */ | | 3726 | */ |
3732 | if (save_dollars && (eflags & VARE_ASSIGN)) | | 3727 | if (save_dollars && (eflags & VARE_ASSIGN)) |
3733 | Buf_AddByte(&buf, '$'); | | 3728 | Buf_AddByte(&buf, '$'); |
3734 | Buf_AddByte(&buf, '$'); | | 3729 | Buf_AddByte(&buf, '$'); |
3735 | str += 2; | | 3730 | str += 2; |
3736 | } else if (*str != '$') { | | 3731 | } else if (*str != '$') { |
3737 | /* | | 3732 | /* |
3738 | * Skip as many characters as possible -- either to the end of | | 3733 | * Skip as many characters as possible -- either to the end of |
| @@ -3772,27 +3767,26 @@ Var_Subst(const char *str, GNode *ctxt, | | | @@ -3772,27 +3767,26 @@ Var_Subst(const char *str, GNode *ctxt, |
3772 | str = nested_str; | | 3767 | str = nested_str; |
3773 | errorReported = TRUE; | | 3768 | errorReported = TRUE; |
3774 | } else { | | 3769 | } else { |
3775 | Buf_AddByte(&buf, *str); | | 3770 | Buf_AddByte(&buf, *str); |
3776 | str++; | | 3771 | str++; |
3777 | } | | 3772 | } |
3778 | } else { | | 3773 | } else { |
3779 | size_t val_len; | | 3774 | size_t val_len; |
3780 | | | 3775 | |
3781 | str = nested_str; | | 3776 | str = nested_str; |
3782 | | | 3777 | |
3783 | val_len = strlen(val); | | 3778 | val_len = strlen(val); |
3784 | Buf_AddBytes(&buf, val, val_len); | | 3779 | Buf_AddBytes(&buf, val, val_len); |
3785 | trailingBackslash = val_len > 0 && val[val_len - 1] == '\\'; | | | |
3786 | } | | 3780 | } |
3787 | free(freeIt); | | 3781 | free(freeIt); |
3788 | freeIt = NULL; | | 3782 | freeIt = NULL; |
3789 | } | | 3783 | } |
3790 | } | | 3784 | } |
3791 | | | 3785 | |
3792 | return Buf_DestroyCompact(&buf); | | 3786 | return Buf_DestroyCompact(&buf); |
3793 | } | | 3787 | } |
3794 | | | 3788 | |
3795 | /* Initialize the module. */ | | 3789 | /* Initialize the module. */ |
3796 | void | | 3790 | void |
3797 | Var_Init(void) | | 3791 | Var_Init(void) |
3798 | { | | 3792 | { |