Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/usr.bin/make make(1): start replacing Lst_Find with Lst_FindB
details: https://anonhg.NetBSD.org/src/rev/c8b8cad6f290
branches: trunk
changeset: 937948:c8b8cad6f290
user: rillig <rillig%NetBSD.org@localhost>
date: Sat Aug 29 09:30:10 2020 +0000
description:
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.
diffstat:
usr.bin/make/arch.c | 31 +++++++++----------------------
usr.bin/make/cond.c | 15 +++++++--------
usr.bin/make/dir.c | 38 +++++++++++---------------------------
usr.bin/make/lst.c | 35 ++++++++++++++++++++++++++++++++---
usr.bin/make/lst.h | 5 ++++-
usr.bin/make/main.c | 40 ++++++++++++++++++++++++----------------
6 files changed, 87 insertions(+), 77 deletions(-)
diffs (truncated from 423 to 300 lines):
diff -r eb9b3b426a41 -r c8b8cad6f290 usr.bin/make/arch.c
--- a/usr.bin/make/arch.c Sat Aug 29 08:59:08 2020 +0000
+++ b/usr.bin/make/arch.c Sat Aug 29 09:30:10 2020 +0000
@@ -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 @@
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 @@
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 @@
member = base + 1;
}
- ln = Lst_Find(archives, ArchFindArchive, archive);
+ ln = Lst_FindB(archives, ArchFindArchive, archive);
if (ln != NULL) {
ar = Lst_Datum(ln);
diff -r eb9b3b426a41 -r c8b8cad6f290 usr.bin/make/cond.c
--- a/usr.bin/make/cond.c Sat Aug 29 08:59:08 2020 +0000
+++ b/usr.bin/make/cond.c Sat Aug 29 09:30:10 2020 +0000
@@ -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 @@
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. */
diff -r eb9b3b426a41 -r c8b8cad6f290 usr.bin/make/dir.c
--- a/usr.bin/make/dir.c Sat Aug 29 08:59:08 2020 +0000
+++ b/usr.bin/make/dir.c Sat Aug 29 09:30:10 2020 +0000
@@ -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 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 @@
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 @@
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 @@
}
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) {
diff -r eb9b3b426a41 -r c8b8cad6f290 usr.bin/make/lst.c
--- a/usr.bin/make/lst.c Sat Aug 29 08:59:08 2020 +0000
+++ b/usr.bin/make/lst.c Sat Aug 29 09:30:10 2020 +0000
@@ -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 @@
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 @@
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)
diff -r eb9b3b426a41 -r c8b8cad6f290 usr.bin/make/lst.h
--- a/usr.bin/make/lst.h Sat Aug 29 08:59:08 2020 +0000
+++ b/usr.bin/make/lst.h Sat Aug 29 09:30:10 2020 +0000
@@ -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 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 @@
*/
/* 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 *);
Home |
Main Index |
Thread Index |
Old Index