Module Name: src Committed By: rillig Date: Fri Jul 3 17:03:09 UTC 2020
Modified Files: src/usr.bin/make: hash.c hash.h var.c Log Message: make(1): add Hash_ForEach to avoid duplicate code To generate a diff of this commit: cvs rdiff -u -r1.21 -r1.22 src/usr.bin/make/hash.c cvs rdiff -u -r1.12 -r1.13 src/usr.bin/make/hash.h cvs rdiff -u -r1.240 -r1.241 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/hash.c diff -u src/usr.bin/make/hash.c:1.21 src/usr.bin/make/hash.c:1.22 --- src/usr.bin/make/hash.c:1.21 Fri Jul 3 08:02:55 2020 +++ src/usr.bin/make/hash.c Fri Jul 3 17:03:09 2020 @@ -1,4 +1,4 @@ -/* $NetBSD: hash.c,v 1.21 2020/07/03 08:02:55 rillig Exp $ */ +/* $NetBSD: hash.c,v 1.22 2020/07/03 17:03:09 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: hash.c,v 1.21 2020/07/03 08:02:55 rillig Exp $"; +static char rcsid[] = "$NetBSD: hash.c,v 1.22 2020/07/03 17:03:09 rillig Exp $"; #else #include <sys/cdefs.h> #ifndef lint #if 0 static char sccsid[] = "@(#)hash.c 8.1 (Berkeley) 6/6/93"; #else -__RCSID("$NetBSD: hash.c,v 1.21 2020/07/03 08:02:55 rillig Exp $"); +__RCSID("$NetBSD: hash.c,v 1.22 2020/07/03 17:03:09 rillig Exp $"); #endif #endif /* not lint */ #endif @@ -464,3 +464,14 @@ RebuildTable(Hash_Table *t) } free(oldhp); } + +void Hash_ForEach(Hash_Table *t, void (*action)(void *, void *), void *data) +{ + Hash_Search search; + Hash_Entry *e; + + for (e = Hash_EnumFirst(t, &search); + e != NULL; + e = Hash_EnumNext(&search)) + action(Hash_GetValue(e), data); +} Index: src/usr.bin/make/hash.h diff -u src/usr.bin/make/hash.h:1.12 src/usr.bin/make/hash.h:1.13 --- src/usr.bin/make/hash.h:1.12 Wed May 31 21:07:03 2017 +++ src/usr.bin/make/hash.h Fri Jul 3 17:03:09 2020 @@ -1,4 +1,4 @@ -/* $NetBSD: hash.h,v 1.12 2017/05/31 21:07:03 maya Exp $ */ +/* $NetBSD: hash.h,v 1.13 2020/07/03 17:03:09 rillig Exp $ */ /* * Copyright (c) 1988, 1989, 1990 The Regents of the University of California. @@ -145,5 +145,6 @@ Hash_Entry *Hash_CreateEntry(Hash_Table void Hash_DeleteEntry(Hash_Table *, Hash_Entry *); Hash_Entry *Hash_EnumFirst(Hash_Table *, Hash_Search *); Hash_Entry *Hash_EnumNext(Hash_Search *); +void Hash_ForEach(Hash_Table *, void (*)(void *, void *), void *); #endif /* _HASH_H */ Index: src/usr.bin/make/var.c diff -u src/usr.bin/make/var.c:1.240 src/usr.bin/make/var.c:1.241 --- src/usr.bin/make/var.c:1.240 Fri Jul 3 17:00:47 2020 +++ src/usr.bin/make/var.c Fri Jul 3 17:03:09 2020 @@ -1,4 +1,4 @@ -/* $NetBSD: var.c,v 1.240 2020/07/03 17:00:47 rillig Exp $ */ +/* $NetBSD: var.c,v 1.241 2020/07/03 17:03:09 rillig Exp $ */ /* * Copyright (c) 1988, 1989, 1990, 1993 @@ -69,14 +69,14 @@ */ #ifndef MAKE_NATIVE -static char rcsid[] = "$NetBSD: var.c,v 1.240 2020/07/03 17:00:47 rillig Exp $"; +static char rcsid[] = "$NetBSD: var.c,v 1.241 2020/07/03 17:03:09 rillig Exp $"; #else #include <sys/cdefs.h> #ifndef lint #if 0 static char sccsid[] = "@(#)var.c 8.3 (Berkeley) 3/19/94"; #else -__RCSID("$NetBSD: var.c,v 1.240 2020/07/03 17:00:47 rillig Exp $"); +__RCSID("$NetBSD: var.c,v 1.241 2020/07/03 17:03:09 rillig Exp $"); #endif #endif /* not lint */ #endif @@ -338,7 +338,6 @@ static char *VarModify(GNode *, Var_Pars static char *VarOrder(const char *, const char); static char *VarUniq(const char *); static int VarWordCompare(const void *, const void *); -static void VarPrintVar(void *); #define BROPEN '{' #define BRCLOSE '}' @@ -673,6 +672,13 @@ Var_Export1(const char *name, int flags) return 1; } +static void +Var_ExportVars_callback(void *entry, void *unused MAKE_ATTR_UNUSED) +{ + Var *var = entry; + Var_Export1(var->name, 0); +} + /* * This gets called from our children. */ @@ -680,9 +686,6 @@ void Var_ExportVars(void) { char tmp[BUFSIZ]; - Hash_Entry *var; - Hash_Search state; - Var *v; char *val; int n; @@ -699,15 +702,8 @@ Var_ExportVars(void) return; if (VAR_EXPORTED_ALL == var_exportedVars) { - /* - * Ouch! This is crazy... - */ - for (var = Hash_EnumFirst(&VAR_GLOBAL->context, &state); - var != NULL; - var = Hash_EnumNext(&state)) { - v = (Var *)Hash_GetValue(var); - Var_Export1(v->name, 0); - } + /* Ouch! This is crazy... */ + Hash_ForEach(&VAR_GLOBAL->context, Var_ExportVars_callback, NULL); return; } /* @@ -4291,7 +4287,7 @@ Var_End(void) /****************** PRINT DEBUGGING INFO *****************/ static void -VarPrintVar(void *vp) +VarPrintVar(void *vp, void *data MAKE_ATTR_UNUSED) { Var *v = (Var *)vp; fprintf(debug_file, "%-16s = %s\n", v->name, Buf_GetAll(&v->val, NULL)); @@ -4306,12 +4302,5 @@ VarPrintVar(void *vp) void Var_Dump(GNode *ctxt) { - Hash_Search search; - Hash_Entry *h; - - for (h = Hash_EnumFirst(&ctxt->context, &search); - h != NULL; - h = Hash_EnumNext(&search)) { - VarPrintVar(Hash_GetValue(h)); - } + Hash_ForEach(&ctxt->context, VarPrintVar, NULL); }