Sat Oct 31 23:44:42 2020 UTC ()
make(1): clean up StrContainsWord


(rillig)
diff -r1.418 -r1.419 src/usr.bin/make/parse.c

cvs diff -r1.418 -r1.419 src/usr.bin/make/parse.c (expand / switch to unified diff)

--- src/usr.bin/make/parse.c 2020/10/31 23:39:01 1.418
+++ src/usr.bin/make/parse.c 2020/10/31 23:44:42 1.419
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: parse.c,v 1.418 2020/10/31 23:39:01 rillig Exp $ */ 1/* $NetBSD: parse.c,v 1.419 2020/10/31 23:44:42 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.
@@ -107,27 +107,27 @@ @@ -107,27 +107,27 @@
107#ifndef MAP_FILE 107#ifndef MAP_FILE
108#define MAP_FILE 0 108#define MAP_FILE 0
109#endif 109#endif
110#ifndef MAP_COPY 110#ifndef MAP_COPY
111#define MAP_COPY MAP_PRIVATE 111#define MAP_COPY MAP_PRIVATE
112#endif 112#endif
113 113
114#include "make.h" 114#include "make.h"
115#include "dir.h" 115#include "dir.h"
116#include "job.h" 116#include "job.h"
117#include "pathnames.h" 117#include "pathnames.h"
118 118
119/* "@(#)parse.c 8.3 (Berkeley) 3/19/94" */ 119/* "@(#)parse.c 8.3 (Berkeley) 3/19/94" */
120MAKE_RCSID("$NetBSD: parse.c,v 1.418 2020/10/31 23:39:01 rillig Exp $"); 120MAKE_RCSID("$NetBSD: parse.c,v 1.419 2020/10/31 23:44:42 rillig Exp $");
121 121
122/* types and constants */ 122/* types and constants */
123 123
124/* 124/*
125 * Structure for a file being read ("included file") 125 * Structure for a file being read ("included file")
126 */ 126 */
127typedef struct IFile { 127typedef struct IFile {
128 char *fname; /* name of file */ 128 char *fname; /* name of file */
129 Boolean fromForLoop; /* simulated .include by the .for loop */ 129 Boolean fromForLoop; /* simulated .include by the .for loop */
130 int lineno; /* current line number in file */ 130 int lineno; /* current line number in file */
131 int first_lineno; /* line number of start of text */ 131 int first_lineno; /* line number of start of text */
132 unsigned int cond_depth; /* 'if' nesting when file opened */ 132 unsigned int cond_depth; /* 'if' nesting when file opened */
133 Boolean depending; /* state of doing_depend on EOF */ 133 Boolean depending; /* state of doing_depend on EOF */
@@ -2344,43 +2344,42 @@ ParseSetParseFile(const char *filename) @@ -2344,43 +2344,42 @@ ParseSetParseFile(const char *filename)
2344 including = GetActuallyIncludingFile(); 2344 including = GetActuallyIncludingFile();
2345 if (including != NULL) { 2345 if (including != NULL) {
2346 SetFilenameVars(including, 2346 SetFilenameVars(including,
2347 ".INCLUDEDFROMDIR", ".INCLUDEDFROMFILE"); 2347 ".INCLUDEDFROMDIR", ".INCLUDEDFROMFILE");
2348 } else { 2348 } else {
2349 Var_Delete(".INCLUDEDFROMDIR", VAR_GLOBAL); 2349 Var_Delete(".INCLUDEDFROMDIR", VAR_GLOBAL);
2350 Var_Delete(".INCLUDEDFROMFILE", VAR_GLOBAL); 2350 Var_Delete(".INCLUDEDFROMFILE", VAR_GLOBAL);
2351 } 2351 }
2352} 2352}
2353 2353
2354static Boolean 2354static Boolean
2355StrContainsWord(const char *str, const char *word) 2355StrContainsWord(const char *str, const char *word)
2356{ 2356{
2357 const char *val = str; 2357 size_t strLen = strlen(str);
2358 size_t valLen = strlen(val); 
2359 size_t wordLen = strlen(word); 2358 size_t wordLen = strlen(word);
2360 const char *end; 2359 const char *p, *end;
2361 2360
2362 if (valLen < wordLen) 2361 if (strLen < wordLen)
2363 return FALSE; /* str is too short to contain word */ 2362 return FALSE; /* str is too short to contain word */
2364 2363
2365 end = val + valLen - wordLen; 2364 end = str + strLen - wordLen;
2366 for (; val != NULL; val = strchr(val, ' ')) { 2365 for (p = str; p != NULL; p = strchr(p, ' ')) {
2367 if (*val == ' ') 2366 if (*p == ' ')
2368 val++; 2367 p++;
2369 if (val > end) 2368 if (p > end)
2370 return FALSE; /* cannot contain word */ 2369 return FALSE; /* cannot contain word */
2371 2370
2372 if (memcmp(val, word, wordLen) == 0 && 2371 if (memcmp(p, word, wordLen) == 0 &&
2373 (val[wordLen] == '\0' || val[wordLen] == ' ')) 2372 (p[wordLen] == '\0' || p[wordLen] == ' '))
2374 return TRUE; 2373 return TRUE;
2375 } 2374 }
2376 return FALSE; 2375 return FALSE;
2377} 2376}
2378 2377
2379/* XXX: Searching through a set of words with this linear search is 2378/* XXX: Searching through a set of words with this linear search is
2380 * inefficient for variables that contain thousands of words. */ 2379 * inefficient for variables that contain thousands of words. */
2381static Boolean 2380static Boolean
2382VarContainsWord(const char *varname, const char *word) 2381VarContainsWord(const char *varname, const char *word)
2383{ 2382{
2384 void *val_freeIt; 2383 void *val_freeIt;
2385 const char *val = Var_Value(varname, VAR_GLOBAL, &val_freeIt); 2384 const char *val = Var_Value(varname, VAR_GLOBAL, &val_freeIt);
2386 Boolean found = val != NULL && StrContainsWord(val, word); 2385 Boolean found = val != NULL && StrContainsWord(val, word);