Sun Mar 14 17:34:50 2021 UTC ()
make: merge duplicate code in ApplyModifier_Remember

This way, parsing and evaluating the modifier is only written once in
the code.  The downside is that the variable name is allocated even if
VARE_WANTRES is not set, but since this modifier is so obscure and
seldom used this doesn't matter in practice.


(rillig)
diff -r1.867 -r1.868 src/usr.bin/make/var.c

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

--- src/usr.bin/make/var.c 2021/03/14 17:27:27 1.867
+++ src/usr.bin/make/var.c 2021/03/14 17:34:50 1.868
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: var.c,v 1.867 2021/03/14 17:27:27 rillig Exp $ */ 1/* $NetBSD: var.c,v 1.868 2021/03/14 17:34:50 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.
@@ -130,27 +130,27 @@ @@ -130,27 +130,27 @@
130#include <regex.h> 130#include <regex.h>
131#endif 131#endif
132#include <errno.h> 132#include <errno.h>
133#include <inttypes.h> 133#include <inttypes.h>
134#include <limits.h> 134#include <limits.h>
135#include <time.h> 135#include <time.h>
136 136
137#include "make.h" 137#include "make.h"
138#include "dir.h" 138#include "dir.h"
139#include "job.h" 139#include "job.h"
140#include "metachar.h" 140#include "metachar.h"
141 141
142/* "@(#)var.c 8.3 (Berkeley) 3/19/94" */ 142/* "@(#)var.c 8.3 (Berkeley) 3/19/94" */
143MAKE_RCSID("$NetBSD: var.c,v 1.867 2021/03/14 17:27:27 rillig Exp $"); 143MAKE_RCSID("$NetBSD: var.c,v 1.868 2021/03/14 17:34:50 rillig Exp $");
144 144
145typedef enum VarFlags { 145typedef enum VarFlags {
146 VFL_NONE = 0, 146 VFL_NONE = 0,
147 147
148 /* 148 /*
149 * The variable's value is currently being used by Var_Parse or 149 * The variable's value is currently being used by Var_Parse or
150 * Var_Subst. This marker is used to avoid endless recursion. 150 * Var_Subst. This marker is used to avoid endless recursion.
151 */ 151 */
152 VFL_IN_USE = 1 << 0, 152 VFL_IN_USE = 1 << 0,
153 153
154 /* 154 /*
155 * The variable comes from the environment. 155 * The variable comes from the environment.
156 * These variables are not registered in any GNode, therefore they 156 * These variables are not registered in any GNode, therefore they
@@ -3408,50 +3408,48 @@ ok: @@ -3408,50 +3408,48 @@ ok:
3408 Expr_SetValueRefer(expr, ""); 3408 Expr_SetValueRefer(expr, "");
3409 return AMR_OK; 3409 return AMR_OK;
3410} 3410}
3411 3411
3412/* 3412/*
3413 * :_=... 3413 * :_=...
3414 * remember current value 3414 * remember current value
3415 */ 3415 */
3416static ApplyModifierResult 3416static ApplyModifierResult
3417ApplyModifier_Remember(const char **pp, ApplyModifiersState *st) 3417ApplyModifier_Remember(const char **pp, ApplyModifiersState *st)
3418{ 3418{
3419 Expr *expr = st->expr; 3419 Expr *expr = st->expr;
3420 const char *mod = *pp; 3420 const char *mod = *pp;
 3421 FStr name;
3421 3422
3422 if (!ModMatchEq(mod, "_", st)) 3423 if (!ModMatchEq(mod, "_", st))
3423 return AMR_UNKNOWN; 3424 return AMR_UNKNOWN;
3424 3425
 3426 name = FStr_InitRefer("_");
3425 if (mod[1] == '=') { 3427 if (mod[1] == '=') {
3426 /* 3428 /*
3427 * XXX: This ad-hoc call to strcspn deviates from the usual 3429 * XXX: This ad-hoc call to strcspn deviates from the usual
3428 * behavior defined in ParseModifierPart. This creates an 3430 * behavior defined in ParseModifierPart. This creates an
3429 * unnecessary, undocumented inconsistency in make. 3431 * unnecessary, undocumented inconsistency in make.
3430 */ 3432 */
3431 size_t n = strcspn(mod + 2, ":)}"); 3433 size_t n = strcspn(mod + 2, ":)}");
3432 *pp = mod + 2 + n; 3434 *pp = mod + 2 + n;
3433 3435 name = FStr_InitOwn(bmake_strldup(mod + 2, n));
3434 if (expr->eflags & VARE_WANTRES) { 3436 } else
3435 char *name = bmake_strldup(mod + 2, n); 
3436 Var_Set(expr->scope, name, expr->value.str); 
3437 free(name); 
3438 } 
3439 } else { 
3440 *pp = mod + 1; 3437 *pp = mod + 1;
3441 3438
3442 if (expr->eflags & VARE_WANTRES) 3439 if (expr->eflags & VARE_WANTRES)
3443 Var_Set(expr->scope, "_", expr->value.str); 3440 Var_Set(expr->scope, name.str, expr->value.str);
3444 } 3441 FStr_Done(&name);
 3442
3445 return AMR_OK; 3443 return AMR_OK;
3446} 3444}
3447 3445
3448/* 3446/*
3449 * Apply the given function to each word of the variable value, 3447 * Apply the given function to each word of the variable value,
3450 * for a single-letter modifier such as :H, :T. 3448 * for a single-letter modifier such as :H, :T.
3451 */ 3449 */
3452static ApplyModifierResult 3450static ApplyModifierResult
3453ApplyModifier_WordFunc(const char **pp, ApplyModifiersState *st, 3451ApplyModifier_WordFunc(const char **pp, ApplyModifiersState *st,
3454 ModifyWordProc modifyWord) 3452 ModifyWordProc modifyWord)
3455{ 3453{
3456 if (!IsDelimiter((*pp)[1], st)) 3454 if (!IsDelimiter((*pp)[1], st))
3457 return AMR_UNKNOWN; 3455 return AMR_UNKNOWN;