Module Name: src Committed By: rillig Date: Thu Jun 22 08:55:33 UTC 2023
Modified Files: src/usr.bin/make: var.c Log Message: make: merge common code for handling the ':M' and ':N' modifiers No functional change. To generate a diff of this commit: cvs rdiff -u -r1.1056 -r1.1057 src/usr.bin/make/var.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/usr.bin/make/var.c diff -u src/usr.bin/make/var.c:1.1056 src/usr.bin/make/var.c:1.1057 --- src/usr.bin/make/var.c:1.1056 Fri Jun 16 22:30:35 2023 +++ src/usr.bin/make/var.c Thu Jun 22 08:55:33 2023 @@ -1,4 +1,4 @@ -/* $NetBSD: var.c,v 1.1056 2023/06/16 22:30:35 sjg Exp $ */ +/* $NetBSD: var.c,v 1.1057 2023/06/22 08:55:33 rillig Exp $ */ /* * Copyright (c) 1988, 1989, 1990, 1993 @@ -139,7 +139,7 @@ #include "metachar.h" /* "@(#)var.c 8.3 (Berkeley) 3/19/94" */ -MAKE_RCSID("$NetBSD: var.c,v 1.1056 2023/06/16 22:30:35 sjg Exp $"); +MAKE_RCSID("$NetBSD: var.c,v 1.1057 2023/06/22 08:55:33 rillig Exp $"); /* * Variables are defined using one of the VAR=value assignments. Their @@ -1408,34 +1408,6 @@ ModifyWord_Root(Substring word, SepBuf * SepBuf_AddRange(buf, word.start, end); } -/* - * Callback for ModifyWords to implement the :M modifier. - * Place the word in the buffer if it matches the given pattern. - */ -static void -ModifyWord_Match(Substring word, SepBuf *buf, void *data) -{ - const char *pattern = data; - - assert(word.end[0] == '\0'); /* assume null-terminated word */ - if (Str_Match(word.start, pattern)) - SepBuf_AddSubstring(buf, word); -} - -/* - * Callback for ModifyWords to implement the :N modifier. - * Place the word in the buffer if it doesn't match the given pattern. - */ -static void -ModifyWord_NoMatch(Substring word, SepBuf *buf, void *data) -{ - const char *pattern = data; - - assert(word.end[0] == '\0'); /* assume null-terminated word */ - if (!Str_Match(word.start, pattern)) - SepBuf_AddSubstring(buf, word); -} - #ifdef SYSVVARSUB struct ModifyWord_SysVSubstArgs { GNode *scope; @@ -2818,6 +2790,20 @@ ParseModifier_Match(const char **pp, con return pattern; } +struct ModifyWord_MatchArgs { + const char *pattern; + bool neg; +}; + +static void +ModifyWord_Match(Substring word, SepBuf *buf, void *data) +{ + struct ModifyWord_MatchArgs *args = data; + assert(word.end[0] == '\0'); /* assume null-terminated word */ + if (Str_Match(word.start, args->pattern) != args->neg) + SepBuf_AddSubstring(buf, word); +} + /* :Mpattern or :Npattern */ static ApplyModifierResult ApplyModifier_Match(const char **pp, ModChain *ch) @@ -2828,9 +2814,10 @@ ApplyModifier_Match(const char **pp, Mod pattern = ParseModifier_Match(pp, ch); if (ModChain_ShouldEval(ch)) { - ModifyWordProc modifyWord = - mod == 'M' ? ModifyWord_Match : ModifyWord_NoMatch; - ModifyWords(ch, modifyWord, pattern, ch->oneBigWord); + struct ModifyWord_MatchArgs args; + args.pattern = pattern; + args.neg = mod == 'N'; + ModifyWords(ch, ModifyWord_Match, &args, ch->oneBigWord); } free(pattern);