Module Name:    src
Committed By:   rillig
Date:           Sat Aug 29 09:30:10 UTC 2020

Modified Files:
        src/usr.bin/make: arch.c cond.c dir.c lst.c lst.h main.c

Log Message:
make(1): start replacing Lst_Find with Lst_FindB

Lst_Find is called with a "comparison" function that returns the integer
0 if the desired node is found.  This leads to confusion since there are
so many different return value conventions for int, such as 0/1 for
mimicking false/true, -1/0 as in close(2), and the sign as in strcmp(3).
This API is much easier to understand if the "comparison" function is
not called a comparison function (since that is too close to strcmp),
but a "match" function that just returns a boolean.

In Lst_FindFromB, the node argument may be null.  This deviates from the
other Lst functions, which require Lst and LstNode to generally be
non-null.  In this case it is useful though to make the calling code
simpler.

In arch.c, this makes a lot of the previous documentation redundant.

In cond.c, the documentation is reduced a little bit since it had
already been cleaned up before.  It also removes the strange negation
from CondFindStrMatch.

In dir.c, the documentation collapses as well.

In main.c, separating the ReadMakefile function from the callbacks for
Lst_FindB allows the former to get back its natural function signature,
with proper types and no unused parameters.

To catch any accidental mistakes during the migration from Lst_Find to
Lst_FindB, the code can be compiled with -DUSE_DOUBLE_BOOLEAN, which
will complain about incompatible function pointer types.


To generate a diff of this commit:
cvs rdiff -u -r1.102 -r1.103 src/usr.bin/make/arch.c
cvs rdiff -u -r1.103 -r1.104 src/usr.bin/make/cond.c
cvs rdiff -u -r1.120 -r1.121 src/usr.bin/make/dir.c
cvs rdiff -u -r1.52 -r1.53 src/usr.bin/make/lst.c
cvs rdiff -u -r1.53 -r1.54 src/usr.bin/make/lst.h
cvs rdiff -u -r1.324 -r1.325 src/usr.bin/make/main.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/arch.c
diff -u src/usr.bin/make/arch.c:1.102 src/usr.bin/make/arch.c:1.103
--- src/usr.bin/make/arch.c:1.102	Fri Aug 28 18:34:45 2020
+++ src/usr.bin/make/arch.c	Sat Aug 29 09:30:10 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: arch.c,v 1.102 2020/08/28 18:34:45 rillig Exp $	*/
+/*	$NetBSD: arch.c,v 1.103 2020/08/29 09:30:10 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -69,14 +69,14 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: arch.c,v 1.102 2020/08/28 18:34:45 rillig Exp $";
+static char rcsid[] = "$NetBSD: arch.c,v 1.103 2020/08/29 09:30:10 rillig Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
 #if 0
 static char sccsid[] = "@(#)arch.c	8.2 (Berkeley) 1/2/94";
 #else
-__RCSID("$NetBSD: arch.c,v 1.102 2020/08/28 18:34:45 rillig Exp $");
+__RCSID("$NetBSD: arch.c,v 1.103 2020/08/29 09:30:10 rillig Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -164,7 +164,6 @@ typedef struct Arch {
     size_t	  fnamesize;  /* Size of the string table */
 } Arch;
 
-static int ArchFindArchive(const void *, const void *);
 static struct ar_hdr *ArchStatMember(const char *, const char *, Boolean);
 static FILE *ArchFindMember(const char *, const char *,
 			    struct ar_hdr *, const char *);
@@ -455,24 +454,12 @@ Arch_ParseArchive(char **linePtr, Lst no
     return TRUE;
 }
 
-/*-
- *-----------------------------------------------------------------------
- * ArchFindArchive --
- *	See if the given archive is the one we are looking for. Called
- *	From ArchStatMember and ArchFindMember via Lst_Find.
- *
- * Input:
- *	ar		Current list element
- *	archName	Name we want
- *
- * Results:
- *	0 if it is, non-zero if it isn't.
- *-----------------------------------------------------------------------
- */
-static int
-ArchFindArchive(const void *ar, const void *archName)
+/* See if the given archive is the one we are looking for.
+ * Called via Lst_FindB. */
+static Boolean
+ArchFindArchive(const void *ar, const void *desiredName)
 {
-    return strcmp(archName, ((const Arch *)ar)->name);
+    return strcmp(((const Arch *)ar)->name, desiredName) == 0;
 }
 
 /*-
@@ -520,7 +507,7 @@ ArchStatMember(const char *archive, cons
 	member = base + 1;
     }
 
-    ln = Lst_Find(archives, ArchFindArchive, archive);
+    ln = Lst_FindB(archives, ArchFindArchive, archive);
     if (ln != NULL) {
 	ar = Lst_Datum(ln);
 

Index: src/usr.bin/make/cond.c
diff -u src/usr.bin/make/cond.c:1.103 src/usr.bin/make/cond.c:1.104
--- src/usr.bin/make/cond.c:1.103	Fri Aug 28 04:48:56 2020
+++ src/usr.bin/make/cond.c	Sat Aug 29 09:30:10 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: cond.c,v 1.103 2020/08/28 04:48:56 rillig Exp $	*/
+/*	$NetBSD: cond.c,v 1.104 2020/08/29 09:30:10 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@@ -70,14 +70,14 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: cond.c,v 1.103 2020/08/28 04:48:56 rillig Exp $";
+static char rcsid[] = "$NetBSD: cond.c,v 1.104 2020/08/29 09:30:10 rillig Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
 #if 0
 static char sccsid[] = "@(#)cond.c	8.2 (Berkeley) 1/2/94";
 #else
-__RCSID("$NetBSD: cond.c,v 1.103 2020/08/28 04:48:56 rillig Exp $");
+__RCSID("$NetBSD: cond.c,v 1.104 2020/08/29 09:30:10 rillig Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -272,19 +272,18 @@ CondDoDefined(int argLen MAKE_ATTR_UNUSE
     return result;
 }
 
-/* Wrapper around Str_Match that returns 0 on match and non-zero
- * on mismatch. Callback function for CondDoMake via Lst_Find. */
-static int
+/* Wrapper around Str_Match, to be used by Lst_FindB. */
+static Boolean
 CondFindStrMatch(const void *string, const void *pattern)
 {
-    return !Str_Match(string, pattern);
+    return Str_Match(string, pattern);
 }
 
 /* See if the given target is being made. */
 static Boolean
 CondDoMake(int argLen MAKE_ATTR_UNUSED, const char *arg)
 {
-    return Lst_Find(create, CondFindStrMatch, arg) != NULL;
+    return Lst_FindB(create, CondFindStrMatch, arg) != NULL;
 }
 
 /* See if the given file exists. */

Index: src/usr.bin/make/dir.c
diff -u src/usr.bin/make/dir.c:1.120 src/usr.bin/make/dir.c:1.121
--- src/usr.bin/make/dir.c:1.120	Fri Aug 28 04:59:17 2020
+++ src/usr.bin/make/dir.c	Sat Aug 29 09:30:10 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: dir.c,v 1.120 2020/08/28 04:59:17 rillig Exp $	*/
+/*	$NetBSD: dir.c,v 1.121 2020/08/29 09:30:10 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@@ -70,14 +70,14 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: dir.c,v 1.120 2020/08/28 04:59:17 rillig Exp $";
+static char rcsid[] = "$NetBSD: dir.c,v 1.121 2020/08/29 09:30:10 rillig Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
 #if 0
 static char sccsid[] = "@(#)dir.c	8.2 (Berkeley) 1/2/94";
 #else
-__RCSID("$NetBSD: dir.c,v 1.120 2020/08/28 04:59:17 rillig Exp $");
+__RCSID("$NetBSD: dir.c,v 1.121 2020/08/29 09:30:10 rillig Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -256,7 +256,6 @@ static Hash_Table mtimes;
 
 static Hash_Table lmtimes;	/* same as mtimes but for lstat */
 
-static int DirFindName(const void *, const void *);
 static void DirExpandCurly(const char *, const char *, Lst, Lst);
 static void DirExpandInt(const char *, Lst, Lst);
 static int DirPrintWord(void *, void *);
@@ -516,28 +515,13 @@ Dir_SetPATH(void)
     Lst_Close(dirSearchPath);
 }
 
-/*-
- *-----------------------------------------------------------------------
- * DirFindName --
- *	See if the Path structure describes the same directory as the
- *	given one by comparing their names. Called from Dir_AddDir via
- *	Lst_Find when searching the list of open directories.
- *
- * Input:
- *	p		Current name
- *	dname		Desired name
- *
- * Results:
- *	0 if it is the same. Non-zero otherwise
- *
- * Side Effects:
- *	None
- *-----------------------------------------------------------------------
- */
-static int
-DirFindName(const void *p, const void *dname)
+/* See if the Path structure describes the same directory as the
+ * given one by comparing their names. Called from Dir_AddDir via
+ * Lst_FindB when searching the list of open directories. */
+static Boolean
+DirFindName(const void *p, const void *desiredName)
 {
-    return strcmp(((const Path *)p)->name, dname);
+    return strcmp(((const Path *)p)->name, desiredName) == 0;
 }
 
 /*-
@@ -1565,7 +1549,7 @@ Dir_AddDir(Lst path, const char *name)
     struct dirent *dp;		/* entry in directory */
 
     if (path != NULL && strcmp(name, ".DOTLAST") == 0) {
-	ln = Lst_Find(path, DirFindName, name);
+	ln = Lst_FindB(path, DirFindName, name);
 	if (ln != NULL)
 	    return Lst_Datum(ln);
 
@@ -1574,7 +1558,7 @@ Dir_AddDir(Lst path, const char *name)
     }
 
     if (path != NULL)
-	ln = Lst_Find(openDirectories, DirFindName, name);
+	ln = Lst_FindB(openDirectories, DirFindName, name);
     if (ln != NULL) {
 	p = Lst_Datum(ln);
 	if (Lst_Member(path, p) == NULL) {

Index: src/usr.bin/make/lst.c
diff -u src/usr.bin/make/lst.c:1.52 src/usr.bin/make/lst.c:1.53
--- src/usr.bin/make/lst.c:1.52	Fri Aug 28 19:52:14 2020
+++ src/usr.bin/make/lst.c	Sat Aug 29 09:30:10 2020
@@ -1,4 +1,4 @@
-/* $NetBSD: lst.c,v 1.52 2020/08/28 19:52:14 rillig Exp $ */
+/* $NetBSD: lst.c,v 1.53 2020/08/29 09:30:10 rillig Exp $ */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -37,11 +37,11 @@
 #include "make.h"
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: lst.c,v 1.52 2020/08/28 19:52:14 rillig Exp $";
+static char rcsid[] = "$NetBSD: lst.c,v 1.53 2020/08/29 09:30:10 rillig Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: lst.c,v 1.52 2020/08/28 19:52:14 rillig Exp $");
+__RCSID("$NetBSD: lst.c,v 1.53 2020/08/29 09:30:10 rillig Exp $");
 #endif /* not lint */
 #endif
 
@@ -385,6 +385,14 @@ Lst_Find(Lst list, LstFindProc cmp, cons
     return Lst_FindFrom(list, Lst_First(list), cmp, cmpData);
 }
 
+/* Return the first node from the list for which the match function returns
+ * TRUE, or NULL if none of the nodes matched. */
+LstNode
+Lst_FindB(Lst list, LstFindBProc match, const void *matchArgs)
+{
+    return Lst_FindFromB(list, Lst_First(list), match, matchArgs);
+}
+
 /* Return the first node from the given list, starting at the given node, for
  * which the given comparison function returns 0, or NULL if none of the nodes
  * matches. */
@@ -405,6 +413,27 @@ Lst_FindFrom(Lst list, LstNode node, Lst
     return NULL;
 }
 
+/* Return the first node from the list, starting at the given node, for which
+ * the match function returns TRUE, or NULL if none of the nodes matches.
+ *
+ * The start node may be NULL, in which case nothing is found. This allows
+ * for passing Lst_First or Lst_Succ as the start node. */
+LstNode
+Lst_FindFromB(Lst list, LstNode node, LstFindBProc match, const void *matchArgs)
+{
+    LstNode tln;
+
+    assert(list != NULL);
+    assert(match != NULL);
+
+    for (tln = node; tln != NULL; tln = tln->next) {
+	if (match(tln->datum, matchArgs))
+	    return tln;
+    }
+
+    return NULL;
+}
+
 /* Return the first node that contains the given datum, or NULL. */
 LstNode
 Lst_Member(Lst list, void *datum)

Index: src/usr.bin/make/lst.h
diff -u src/usr.bin/make/lst.h:1.53 src/usr.bin/make/lst.h:1.54
--- src/usr.bin/make/lst.h:1.53	Fri Aug 28 04:48:57 2020
+++ src/usr.bin/make/lst.h	Sat Aug 29 09:30:10 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: lst.h,v 1.53 2020/08/28 04:48:57 rillig Exp $	*/
+/*	$NetBSD: lst.h,v 1.54 2020/08/29 09:30:10 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@@ -93,6 +93,7 @@ typedef	struct ListNode	*LstNode;
 typedef void *LstCopyProc(void *);
 typedef void LstFreeProc(void *);
 typedef int LstFindProc(const void *, const void *);
+typedef Boolean LstFindBProc(const void *, const void *);
 typedef int LstActionProc(void *, void *);
 
 /*
@@ -146,8 +147,10 @@ void		*Lst_Datum(LstNode);
  */
 /* Find an element in a list */
 LstNode		Lst_Find(Lst, LstFindProc, const void *);
+LstNode		Lst_FindB(Lst, LstFindBProc, const void *);
 /* Find an element starting from somewhere */
 LstNode		Lst_FindFrom(Lst, LstNode, LstFindProc, const void *);
+LstNode		Lst_FindFromB(Lst, LstNode, LstFindBProc, const void *);
 /*
  * See if the given datum is on the list. Returns the LstNode containing
  * the datum

Index: src/usr.bin/make/main.c
diff -u src/usr.bin/make/main.c:1.324 src/usr.bin/make/main.c:1.325
--- src/usr.bin/make/main.c:1.324	Sat Aug 29 08:59:08 2020
+++ src/usr.bin/make/main.c	Sat Aug 29 09:30:10 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: main.c,v 1.324 2020/08/29 08:59:08 rillig Exp $	*/
+/*	$NetBSD: main.c,v 1.325 2020/08/29 09:30:10 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -69,7 +69,7 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: main.c,v 1.324 2020/08/29 08:59:08 rillig Exp $";
+static char rcsid[] = "$NetBSD: main.c,v 1.325 2020/08/29 09:30:10 rillig Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
@@ -81,7 +81,7 @@ __COPYRIGHT("@(#) Copyright (c) 1988, 19
 #if 0
 static char sccsid[] = "@(#)main.c	8.3 (Berkeley) 3/19/94";
 #else
-__RCSID("$NetBSD: main.c,v 1.324 2020/08/29 08:59:08 rillig Exp $");
+__RCSID("$NetBSD: main.c,v 1.325 2020/08/29 09:30:10 rillig Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -183,7 +183,7 @@ Boolean			doing_depend;	/* Set while rea
 static Boolean		jobsRunning;	/* TRUE if the jobs might be running */
 static const char *	tracefile;
 static void		MainParseArgs(int, char **);
-static int		ReadMakefile(const void *, const void *);
+static int		ReadMakefile(const char *);
 static void		usage(void) MAKE_ATTR_DEAD;
 static void		purge_cached_realpaths(void);
 
@@ -786,11 +786,20 @@ Main_SetVarObjdir(const char *var, const
 	return TRUE;
 }
 
-/* Return 0 if reading the makefile failed, for Lst_Find. */
-static int
-ReadMakefileFailed(const void *p, const void *q)
+/* Read and parse the makefile.
+ * Return TRUE if reading the makefile succeeded, for Lst_FindB. */
+static Boolean
+ReadMakefileSucceeded(const void *fname, const void *unused)
+{
+	return ReadMakefile(fname) == 0;
+}
+
+/* Read and parse the makefile.
+ * Return TRUE if reading the makefile failed, for Lst_FindB. */
+static Boolean
+ReadMakefileFailed(const void *fname, const void *unused)
 {
-	return ReadMakefile(p, q) == 0;
+	return ReadMakefile(fname) != 0;
 }
 
 int
@@ -1313,8 +1322,8 @@ main(int argc, char **argv)
 
 	/*
 	 * Read in the built-in rules first, followed by the specified
-	 * makefile, if it was (makefile != NULL), or the default
-	 * makefile and Makefile, in that order, if it wasn't.
+	 * makefiles, or the default makefile and Makefile, in that order,
+	 * if no makefiles were given on the command line.
 	 */
 	if (!noBuiltins) {
 		LstNode ln;
@@ -1326,7 +1335,7 @@ main(int argc, char **argv)
 		if (Lst_IsEmpty(sysMkPath))
 			Fatal("%s: no system rules (%s).", progname,
 			    _PATH_DEFSYSMK);
-		ln = Lst_Find(sysMkPath, ReadMakefile, NULL);
+		ln = Lst_FindB(sysMkPath, ReadMakefileSucceeded, NULL);
 		if (ln == NULL)
 			Fatal("%s: cannot open %s.", progname,
 			    (char *)Lst_Datum(Lst_First(sysMkPath)));
@@ -1335,7 +1344,7 @@ main(int argc, char **argv)
 	if (!Lst_IsEmpty(makefiles)) {
 		LstNode ln;
 
-		ln = Lst_Find(makefiles, ReadMakefileFailed, NULL);
+		ln = Lst_FindB(makefiles, ReadMakefileFailed, NULL);
 		if (ln != NULL)
 			Fatal("%s: cannot open %s.", progname,
 			    (char *)Lst_Datum(ln));
@@ -1344,7 +1353,7 @@ main(int argc, char **argv)
 		VAR_CMD, VARE_WANTRES);
 	    if (p1) {
 		(void)str2Lst_Append(makefiles, p1, NULL);
-		(void)Lst_Find(makefiles, ReadMakefile, NULL);
+		(void)Lst_FindB(makefiles, ReadMakefileSucceeded, NULL);
 		free(p1);
 	    }
 	}
@@ -1354,7 +1363,7 @@ main(int argc, char **argv)
 	    makeDependfile = Var_Subst("${.MAKE.DEPENDFILE:T}",
 		VAR_CMD, VARE_WANTRES);
 	    doing_depend = TRUE;
-	    (void)ReadMakefile(makeDependfile, NULL);
+	    (void)ReadMakefile(makeDependfile);
 	    doing_depend = FALSE;
 	}
 
@@ -1500,9 +1509,8 @@ main(int argc, char **argv)
  *	0 if ok. -1 if couldn't open file.
  */
 static int
-ReadMakefile(const void *p, const void *q MAKE_ATTR_UNUSED)
+ReadMakefile(const char *fname)
 {
-	const char *fname = p;		/* makefile to read */
 	int fd;
 	char *name, *path = NULL;
 

Reply via email to