Sat Oct 31 08:40:54 2020 UTC ()
make(1): merge duplicate code in ModifyWord_Subst


(rillig)
diff -r1.610 -r1.611 src/usr.bin/make/var.c

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

--- src/usr.bin/make/var.c 2020/10/30 22:55:34 1.610
+++ src/usr.bin/make/var.c 2020/10/31 08:40:54 1.611
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: var.c,v 1.610 2020/10/30 22:55:34 rillig Exp $ */ 1/* $NetBSD: var.c,v 1.611 2020/10/31 08:40:54 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.
@@ -119,27 +119,27 @@ @@ -119,27 +119,27 @@
119#include <sys/types.h> 119#include <sys/types.h>
120#include <regex.h> 120#include <regex.h>
121#endif 121#endif
122#include <inttypes.h> 122#include <inttypes.h>
123#include <limits.h> 123#include <limits.h>
124#include <time.h> 124#include <time.h>
125 125
126#include "make.h" 126#include "make.h"
127#include "dir.h" 127#include "dir.h"
128#include "job.h" 128#include "job.h"
129#include "metachar.h" 129#include "metachar.h"
130 130
131/* "@(#)var.c 8.3 (Berkeley) 3/19/94" */ 131/* "@(#)var.c 8.3 (Berkeley) 3/19/94" */
132MAKE_RCSID("$NetBSD: var.c,v 1.610 2020/10/30 22:55:34 rillig Exp $"); 132MAKE_RCSID("$NetBSD: var.c,v 1.611 2020/10/31 08:40:54 rillig Exp $");
133 133
134#define VAR_DEBUG1(fmt, arg1) DEBUG1(VAR, fmt, arg1) 134#define VAR_DEBUG1(fmt, arg1) DEBUG1(VAR, fmt, arg1)
135#define VAR_DEBUG2(fmt, arg1, arg2) DEBUG2(VAR, fmt, arg1, arg2) 135#define VAR_DEBUG2(fmt, arg1, arg2) DEBUG2(VAR, fmt, arg1, arg2)
136#define VAR_DEBUG3(fmt, arg1, arg2, arg3) DEBUG3(VAR, fmt, arg1, arg2, arg3) 136#define VAR_DEBUG3(fmt, arg1, arg2, arg3) DEBUG3(VAR, fmt, arg1, arg2, arg3)
137#define VAR_DEBUG4(fmt, arg1, arg2, arg3, arg4) DEBUG4(VAR, fmt, arg1, arg2, arg3, arg4) 137#define VAR_DEBUG4(fmt, arg1, arg2, arg3, arg4) DEBUG4(VAR, fmt, arg1, arg2, arg3, arg4)
138 138
139ENUM_FLAGS_RTTI_3(VarEvalFlags, 139ENUM_FLAGS_RTTI_3(VarEvalFlags,
140 VARE_UNDEFERR, VARE_WANTRES, VARE_ASSIGN); 140 VARE_UNDEFERR, VARE_WANTRES, VARE_ASSIGN);
141 141
142/* 142/*
143 * This lets us tell if we have replaced the original environ 143 * This lets us tell if we have replaced the original environ
144 * (which we cannot free). 144 * (which we cannot free).
145 */ 145 */
@@ -1284,39 +1284,33 @@ ModifyWord_Subst(const char *word, SepBu @@ -1284,39 +1284,33 @@ ModifyWord_Subst(const char *word, SepBu
1284{ 1284{
1285 size_t wordLen = strlen(word); 1285 size_t wordLen = strlen(word);
1286 struct ModifyWord_SubstArgs *args = data; 1286 struct ModifyWord_SubstArgs *args = data;
1287 const char *match; 1287 const char *match;
1288 1288
1289 if ((args->pflags & VARP_SUB_ONE) && args->matched) 1289 if ((args->pflags & VARP_SUB_ONE) && args->matched)
1290 goto nosub; 1290 goto nosub;
1291 1291
1292 if (args->pflags & VARP_ANCHOR_START) { 1292 if (args->pflags & VARP_ANCHOR_START) {
1293 if (wordLen < args->lhsLen || 1293 if (wordLen < args->lhsLen ||
1294 memcmp(word, args->lhs, args->lhsLen) != 0) 1294 memcmp(word, args->lhs, args->lhsLen) != 0)
1295 goto nosub; 1295 goto nosub;
1296 1296
1297 if (args->pflags & VARP_ANCHOR_END) { 1297 if ((args->pflags & VARP_ANCHOR_END) && wordLen != args->lhsLen)
1298 if (wordLen != args->lhsLen) 1298 goto nosub;
1299 goto nosub; 1299
1300 1300 /* :S,^prefix,replacement, or :S,^whole$,replacement, */
1301 /* :S,^whole$,replacement, */ 1301 SepBuf_AddBytes(buf, args->rhs, args->rhsLen);
1302 SepBuf_AddBytes(buf, args->rhs, args->rhsLen); 1302 SepBuf_AddBytes(buf, word + args->lhsLen, wordLen - args->lhsLen);
1303 args->matched = TRUE; 1303 args->matched = TRUE;
1304 } else { 
1305 /* :S,^prefix,replacement, */ 
1306 SepBuf_AddBytes(buf, args->rhs, args->rhsLen); 
1307 SepBuf_AddBytes(buf, word + args->lhsLen, wordLen - args->lhsLen); 
1308 args->matched = TRUE; 
1309 } 
1310 return; 1304 return;
1311 } 1305 }
1312 1306
1313 if (args->pflags & VARP_ANCHOR_END) { 1307 if (args->pflags & VARP_ANCHOR_END) {
1314 const char *start; 1308 const char *start;
1315 1309
1316 if (wordLen < args->lhsLen) 1310 if (wordLen < args->lhsLen)
1317 goto nosub; 1311 goto nosub;
1318 1312
1319 start = word + (wordLen - args->lhsLen); 1313 start = word + (wordLen - args->lhsLen);
1320 if (memcmp(start, args->lhs, args->lhsLen) != 0) 1314 if (memcmp(start, args->lhs, args->lhsLen) != 0)
1321 goto nosub; 1315 goto nosub;
1322 1316