Fri Oct 30 16:48:58 2020 UTC ()
make(1): make iterating over HashTable simpler


(rillig)
diff -r1.147 -r1.148 src/usr.bin/make/arch.c
diff -r1.602 -r1.603 src/usr.bin/make/var.c

cvs diff -r1.147 -r1.148 src/usr.bin/make/arch.c (expand / switch to unified diff)

--- src/usr.bin/make/arch.c 2020/10/25 19:19:07 1.147
+++ src/usr.bin/make/arch.c 2020/10/30 16:48:58 1.148
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: arch.c,v 1.147 2020/10/25 19:19:07 rillig Exp $ */ 1/* $NetBSD: arch.c,v 1.148 2020/10/30 16:48:58 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.
@@ -120,27 +120,27 @@ @@ -120,27 +120,27 @@
120#include <sys/types.h> 120#include <sys/types.h>
121#include <sys/stat.h> 121#include <sys/stat.h>
122#include <sys/time.h> 122#include <sys/time.h>
123#include <sys/param.h> 123#include <sys/param.h>
124 124
125#include <ar.h> 125#include <ar.h>
126#include <utime.h> 126#include <utime.h>
127 127
128#include "make.h" 128#include "make.h"
129#include "dir.h" 129#include "dir.h"
130#include "config.h" 130#include "config.h"
131 131
132/* "@(#)arch.c 8.2 (Berkeley) 1/2/94" */ 132/* "@(#)arch.c 8.2 (Berkeley) 1/2/94" */
133MAKE_RCSID("$NetBSD: arch.c,v 1.147 2020/10/25 19:19:07 rillig Exp $"); 133MAKE_RCSID("$NetBSD: arch.c,v 1.148 2020/10/30 16:48:58 rillig Exp $");
134 134
135#ifdef TARGET_MACHINE 135#ifdef TARGET_MACHINE
136#undef MAKE_MACHINE 136#undef MAKE_MACHINE
137#define MAKE_MACHINE TARGET_MACHINE 137#define MAKE_MACHINE TARGET_MACHINE
138#endif 138#endif
139#ifdef TARGET_MACHINE_ARCH 139#ifdef TARGET_MACHINE_ARCH
140#undef MAKE_MACHINE_ARCH 140#undef MAKE_MACHINE_ARCH
141#define MAKE_MACHINE_ARCH TARGET_MACHINE_ARCH 141#define MAKE_MACHINE_ARCH TARGET_MACHINE_ARCH
142#endif 142#endif
143 143
144typedef struct List ArchList; 144typedef struct List ArchList;
145typedef struct ListNode ArchListNode; 145typedef struct ListNode ArchListNode;
146 146
@@ -157,32 +157,31 @@ typedef struct Arch { @@ -157,32 +157,31 @@ typedef struct Arch {
157static FILE *ArchFindMember(const char *, const char *, 157static FILE *ArchFindMember(const char *, const char *,
158 struct ar_hdr *, const char *); 158 struct ar_hdr *, const char *);
159#if defined(__svr4__) || defined(__SVR4) || defined(__ELF__) 159#if defined(__svr4__) || defined(__SVR4) || defined(__ELF__)
160#define SVR4ARCHIVES 160#define SVR4ARCHIVES
161static int ArchSVR4Entry(Arch *, char *, size_t, FILE *); 161static int ArchSVR4Entry(Arch *, char *, size_t, FILE *);
162#endif 162#endif
163 163
164#ifdef CLEANUP 164#ifdef CLEANUP
165static void 165static void
166ArchFree(void *ap) 166ArchFree(void *ap)
167{ 167{
168 Arch *a = ap; 168 Arch *a = ap;
169 HashIter hi; 169 HashIter hi;
170 HashEntry *he; 
171 170
172 /* Free memory from hash entries */ 171 /* Free memory from hash entries */
173 HashIter_Init(&hi, &a->members); 172 HashIter_Init(&hi, &a->members);
174 while ((he = HashIter_Next(&hi)) != NULL) 173 while (HashIter_Next(&hi) != NULL)
175 free(HashEntry_Get(he)); 174 free(hi.entry->value);
176 175
177 free(a->name); 176 free(a->name);
178 free(a->fnametab); 177 free(a->fnametab);
179 HashTable_Done(&a->members); 178 HashTable_Done(&a->members);
180 free(a); 179 free(a);
181} 180}
182#endif 181#endif
183 182
184 183
185/*- 184/*-
186 *----------------------------------------------------------------------- 185 *-----------------------------------------------------------------------
187 * Arch_ParseArchive -- 186 * Arch_ParseArchive --
188 * Parse the archive specification in the given line and find/create 187 * Parse the archive specification in the given line and find/create

cvs diff -r1.602 -r1.603 src/usr.bin/make/var.c (expand / switch to unified diff)

--- src/usr.bin/make/var.c 2020/10/30 16:45:37 1.602
+++ src/usr.bin/make/var.c 2020/10/30 16:48:58 1.603
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: var.c,v 1.602 2020/10/30 16:45:37 rillig Exp $ */ 1/* $NetBSD: var.c,v 1.603 2020/10/30 16:48:58 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.
@@ -119,27 +119,27 @@ @@ -119,27 +119,27 @@
119#include <sys/types.h> 119#include <sys/types.h>
120#include <regex.h> 120#include <regex.h>
121#endif 121#endif
122#include <inttypes.h> 122#include <inttypes.h>
123#include <limits.h> 123#include <limits.h>
124#include <time.h> 124#include <time.h>
125 125
126#include "make.h" 126#include "make.h"
127#include "dir.h" 127#include "dir.h"
128#include "job.h" 128#include "job.h"
129#include "metachar.h" 129#include "metachar.h"
130 130
131/* "@(#)var.c 8.3 (Berkeley) 3/19/94" */ 131/* "@(#)var.c 8.3 (Berkeley) 3/19/94" */
132MAKE_RCSID("$NetBSD: var.c,v 1.602 2020/10/30 16:45:37 rillig Exp $"); 132MAKE_RCSID("$NetBSD: var.c,v 1.603 2020/10/30 16:48:58 rillig Exp $");
133 133
134#define VAR_DEBUG1(fmt, arg1) DEBUG1(VAR, fmt, arg1) 134#define VAR_DEBUG1(fmt, arg1) DEBUG1(VAR, fmt, arg1)
135#define VAR_DEBUG2(fmt, arg1, arg2) DEBUG2(VAR, fmt, arg1, arg2) 135#define VAR_DEBUG2(fmt, arg1, arg2) DEBUG2(VAR, fmt, arg1, arg2)
136#define VAR_DEBUG3(fmt, arg1, arg2, arg3) DEBUG3(VAR, fmt, arg1, arg2, arg3) 136#define VAR_DEBUG3(fmt, arg1, arg2, arg3) DEBUG3(VAR, fmt, arg1, arg2, arg3)
137#define VAR_DEBUG4(fmt, arg1, arg2, arg3, arg4) DEBUG4(VAR, fmt, arg1, arg2, arg3, arg4) 137#define VAR_DEBUG4(fmt, arg1, arg2, arg3, arg4) DEBUG4(VAR, fmt, arg1, arg2, arg3, arg4)
138 138
139ENUM_FLAGS_RTTI_3(VarEvalFlags, 139ENUM_FLAGS_RTTI_3(VarEvalFlags,
140 VARE_UNDEFERR, VARE_WANTRES, VARE_ASSIGN); 140 VARE_UNDEFERR, VARE_WANTRES, VARE_ASSIGN);
141 141
142/* 142/*
143 * This lets us tell if we have replaced the original environ 143 * This lets us tell if we have replaced the original environ
144 * (which we cannot free). 144 * (which we cannot free).
145 */ 145 */
@@ -592,32 +592,31 @@ Var_ExportVars(void) @@ -592,32 +592,31 @@ Var_ExportVars(void)
592 * recursion - but each uses a different name. 592 * recursion - but each uses a different name.
593 * We allow the makefiles to update MAKELEVEL and ensure 593 * We allow the makefiles to update MAKELEVEL and ensure
594 * children see a correctly incremented value. 594 * children see a correctly incremented value.
595 */ 595 */
596 char tmp[BUFSIZ]; 596 char tmp[BUFSIZ];
597 snprintf(tmp, sizeof(tmp), "%d", makelevel + 1); 597 snprintf(tmp, sizeof(tmp), "%d", makelevel + 1);
598 setenv(MAKE_LEVEL_ENV, tmp, 1); 598 setenv(MAKE_LEVEL_ENV, tmp, 1);
599 599
600 if (var_exportedVars == VAR_EXPORTED_NONE) 600 if (var_exportedVars == VAR_EXPORTED_NONE)
601 return; 601 return;
602 602
603 if (var_exportedVars == VAR_EXPORTED_ALL) { 603 if (var_exportedVars == VAR_EXPORTED_ALL) {
604 HashIter hi; 604 HashIter hi;
605 HashEntry *he; 
606 605
607 /* Ouch! Exporting all variables at once is crazy... */ 606 /* Ouch! Exporting all variables at once is crazy... */
608 HashIter_Init(&hi, &VAR_GLOBAL->context); 607 HashIter_Init(&hi, &VAR_GLOBAL->context);
609 while ((he = HashIter_Next(&hi)) != NULL) { 608 while (HashIter_Next(&hi) != NULL) {
610 Var *var = HashEntry_Get(he); 609 Var *var = hi.entry->value;
611 Var_Export1(var->name, 0); 610 Var_Export1(var->name, 0);
612 } 611 }
613 return; 612 return;
614 } 613 }
615 614
616 (void)Var_Subst("${" MAKE_EXPORTED ":O:u}", VAR_GLOBAL, VARE_WANTRES, &val); 615 (void)Var_Subst("${" MAKE_EXPORTED ":O:u}", VAR_GLOBAL, VARE_WANTRES, &val);
617 /* TODO: handle errors */ 616 /* TODO: handle errors */
618 if (*val) { 617 if (*val) {
619 Words words = Str_Words(val, FALSE); 618 Words words = Str_Words(val, FALSE);
620 size_t i; 619 size_t i;
621 620
622 for (i = 0; i < words.len; i++) 621 for (i = 0; i < words.len; i++)
623 Var_Export1(words.words[i], 0); 622 Var_Export1(words.words[i], 0);
@@ -3883,34 +3882,33 @@ Var_End(void) @@ -3883,34 +3882,33 @@ Var_End(void)
3883 3882
3884void 3883void
3885Var_Stats(void) 3884Var_Stats(void)
3886{ 3885{
3887 HashTable_DebugStats(&VAR_GLOBAL->context, "VAR_GLOBAL"); 3886 HashTable_DebugStats(&VAR_GLOBAL->context, "VAR_GLOBAL");
3888} 3887}
3889 3888
3890/* Print all variables in a context, sorted by name. */ 3889/* Print all variables in a context, sorted by name. */
3891void 3890void
3892Var_Dump(GNode *ctxt) 3891Var_Dump(GNode *ctxt)
3893{ 3892{
3894 Vector /* of const char * */ vec; 3893 Vector /* of const char * */ vec;
3895 HashIter hi; 3894 HashIter hi;
3896 HashEntry *he; 
3897 size_t i; 3895 size_t i;
3898 const char **varnames; 3896 const char **varnames;
3899 3897
3900 Vector_Init(&vec, sizeof(const char *)); 3898 Vector_Init(&vec, sizeof(const char *));
3901 3899
3902 HashIter_Init(&hi, &ctxt->context); 3900 HashIter_Init(&hi, &ctxt->context);
3903 while ((he = HashIter_Next(&hi)) != NULL) 3901 while (HashIter_Next(&hi) != NULL)
3904 *(const char **)Vector_Push(&vec) = he->key; 3902 *(const char **)Vector_Push(&vec) = hi.entry->key;
3905 varnames = vec.items; 3903 varnames = vec.items;
3906 3904
3907 qsort(varnames, vec.len, sizeof varnames[0], str_cmp_asc); 3905 qsort(varnames, vec.len, sizeof varnames[0], str_cmp_asc);
3908 3906
3909 for (i = 0; i < vec.len; i++) { 3907 for (i = 0; i < vec.len; i++) {
3910 const char *varname = varnames[i]; 3908 const char *varname = varnames[i];
3911 Var *var = HashTable_FindValue(&ctxt->context, varname); 3909 Var *var = HashTable_FindValue(&ctxt->context, varname);
3912 debug_printf("%-16s = %s\n", varname, Buf_GetAll(&var->val, NULL)); 3910 debug_printf("%-16s = %s\n", varname, Buf_GetAll(&var->val, NULL));
3913 } 3911 }
3914 3912
3915 Vector_Done(&vec); 3913 Vector_Done(&vec);
3916} 3914}