make(1): add Hash_FindValue, for direct access to hash table datadiff -r1.121 -r1.122 src/usr.bin/make/arch.c
(rillig)
--- src/usr.bin/make/arch.c 2020/09/25 14:49:51 1.121
+++ src/usr.bin/make/arch.c 2020/09/26 14:48:31 1.122
@@ -1,14 +1,14 @@ | @@ -1,14 +1,14 @@ | |||
1 | /* $NetBSD: arch.c,v 1.121 2020/09/25 14:49:51 rillig Exp $ */ | 1 | /* $NetBSD: arch.c,v 1.122 2020/09/26 14:48:31 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. | |
@@ -123,27 +123,27 @@ | @@ -123,27 +123,27 @@ | |||
123 | 123 | |||
124 | #include <ar.h> | 124 | #include <ar.h> | |
125 | #include <ctype.h> | 125 | #include <ctype.h> | |
126 | #include <stdio.h> | 126 | #include <stdio.h> | |
127 | #include <stdlib.h> | 127 | #include <stdlib.h> | |
128 | #include <utime.h> | 128 | #include <utime.h> | |
129 | 129 | |||
130 | #include "make.h" | 130 | #include "make.h" | |
131 | #include "hash.h" | 131 | #include "hash.h" | |
132 | #include "dir.h" | 132 | #include "dir.h" | |
133 | #include "config.h" | 133 | #include "config.h" | |
134 | 134 | |||
135 | /* "@(#)arch.c 8.2 (Berkeley) 1/2/94" */ | 135 | /* "@(#)arch.c 8.2 (Berkeley) 1/2/94" */ | |
136 | MAKE_RCSID("$NetBSD: arch.c,v 1.121 2020/09/25 14:49:51 rillig Exp $"); | 136 | MAKE_RCSID("$NetBSD: arch.c,v 1.122 2020/09/26 14:48:31 rillig Exp $"); | |
137 | 137 | |||
138 | #ifdef TARGET_MACHINE | 138 | #ifdef TARGET_MACHINE | |
139 | #undef MAKE_MACHINE | 139 | #undef MAKE_MACHINE | |
140 | #define MAKE_MACHINE TARGET_MACHINE | 140 | #define MAKE_MACHINE TARGET_MACHINE | |
141 | #endif | 141 | #endif | |
142 | #ifdef TARGET_MACHINE_ARCH | 142 | #ifdef TARGET_MACHINE_ARCH | |
143 | #undef MAKE_MACHINE_ARCH | 143 | #undef MAKE_MACHINE_ARCH | |
144 | #define MAKE_MACHINE_ARCH TARGET_MACHINE_ARCH | 144 | #define MAKE_MACHINE_ARCH TARGET_MACHINE_ARCH | |
145 | #endif | 145 | #endif | |
146 | 146 | |||
147 | typedef struct List ArchList; | 147 | typedef struct List ArchList; | |
148 | typedef struct ListNode ArchListNode; | 148 | typedef struct ListNode ArchListNode; | |
149 | 149 | |||
@@ -456,67 +456,66 @@ Arch_ParseArchive(char **linePtr, GNodeL | @@ -456,67 +456,66 @@ Arch_ParseArchive(char **linePtr, GNodeL | |||
456 | * The archive will remain constant after we read all the headers, so | 456 | * The archive will remain constant after we read all the headers, so | |
457 | * there's not much point in remembering the position... | 457 | * there's not much point in remembering the position... | |
458 | *----------------------------------------------------------------------- | 458 | *----------------------------------------------------------------------- | |
459 | */ | 459 | */ | |
460 | static struct ar_hdr * | 460 | static struct ar_hdr * | |
461 | ArchStatMember(const char *archive, const char *member, Boolean hash) | 461 | ArchStatMember(const char *archive, const char *member, Boolean hash) | |
462 | { | 462 | { | |
463 | #define AR_MAX_NAME_LEN (sizeof(arh.ar_name)-1) | 463 | #define AR_MAX_NAME_LEN (sizeof(arh.ar_name)-1) | |
464 | FILE * arch; /* Stream to archive */ | 464 | FILE * arch; /* Stream to archive */ | |
465 | size_t size; /* Size of archive member */ | 465 | size_t size; /* Size of archive member */ | |
466 | char magic[SARMAG]; | 466 | char magic[SARMAG]; | |
467 | ArchListNode *ln; | 467 | ArchListNode *ln; | |
468 | Arch *ar; /* Archive descriptor */ | 468 | Arch *ar; /* Archive descriptor */ | |
469 | Hash_Entry *he; /* Entry containing member's description */ | |||
470 | struct ar_hdr arh; /* archive-member header for reading archive */ | 469 | struct ar_hdr arh; /* archive-member header for reading archive */ | |
471 | char memName[MAXPATHLEN+1]; | 470 | char memName[MAXPATHLEN+1]; | |
472 | /* Current member name while hashing. */ | 471 | /* Current member name while hashing. */ | |
473 | 472 | |||
474 | /* | 473 | /* | |
475 | * Because of space constraints and similar things, files are archived | 474 | * Because of space constraints and similar things, files are archived | |
476 | * using their final path components, not the entire thing, so we need | 475 | * using their final path components, not the entire thing, so we need | |
477 | * to point 'member' to the final component, if there is one, to make | 476 | * to point 'member' to the final component, if there is one, to make | |
478 | * the comparisons easier... | 477 | * the comparisons easier... | |
479 | */ | 478 | */ | |
480 | const char *base = strrchr(member, '/'); | 479 | const char *base = strrchr(member, '/'); | |
481 | if (base != NULL) { | 480 | if (base != NULL) { | |
482 | member = base + 1; | 481 | member = base + 1; | |
483 | } | 482 | } | |
484 | 483 | |||
485 | for (ln = archives->first; ln != NULL; ln = ln->next) { | 484 | for (ln = archives->first; ln != NULL; ln = ln->next) { | |
486 | const Arch *archPtr = ln->datum; | 485 | const Arch *archPtr = ln->datum; | |
487 | if (strcmp(archPtr->name, archive) == 0) | 486 | if (strcmp(archPtr->name, archive) == 0) | |
488 | break; | 487 | break; | |
489 | } | 488 | } | |
490 | 489 | |||
491 | if (ln != NULL) { | 490 | if (ln != NULL) { | |
492 | ar = LstNode_Datum(ln); | 491 | struct ar_hdr *hdr; | |
493 | 492 | |||
494 | he = Hash_FindEntry(&ar->members, member); | 493 | ar = LstNode_Datum(ln); | |
494 | hdr = Hash_FindValue(&ar->members, member); | |||
495 | if (hdr != NULL) | |||
496 | return hdr; | |||
495 | 497 | |||
496 | if (he != NULL) { | 498 | { | |
497 | return (struct ar_hdr *)Hash_GetValue(he); | |||
498 | } else { | |||
499 | /* Try truncated name */ | 499 | /* Try truncated name */ | |
500 | char copy[AR_MAX_NAME_LEN+1]; | 500 | char copy[AR_MAX_NAME_LEN+1]; | |
501 | size_t len = strlen(member); | 501 | size_t len = strlen(member); | |
502 | 502 | |||
503 | if (len > AR_MAX_NAME_LEN) { | 503 | if (len > AR_MAX_NAME_LEN) { | |
504 | len = AR_MAX_NAME_LEN; | 504 | len = AR_MAX_NAME_LEN; | |
505 | snprintf(copy, sizeof copy, "%s", member); | 505 | snprintf(copy, sizeof copy, "%s", member); | |
506 | } | 506 | } | |
507 | if ((he = Hash_FindEntry(&ar->members, copy)) != NULL) | 507 | hdr = Hash_FindValue(&ar->members, copy); | |
508 | return (struct ar_hdr *)Hash_GetValue(he); | 508 | return hdr; | |
509 | return NULL; | |||
510 | } | 509 | } | |
511 | } | 510 | } | |
512 | 511 | |||
513 | if (!hash) { | 512 | if (!hash) { | |
514 | /* | 513 | /* | |
515 | * Caller doesn't want the thing hashed, just use ArchFindMember | 514 | * Caller doesn't want the thing hashed, just use ArchFindMember | |
516 | * to read the header for the member out and close down the stream | 515 | * to read the header for the member out and close down the stream | |
517 | * again. Since the archive is not to be hashed, we assume there's | 516 | * again. Since the archive is not to be hashed, we assume there's | |
518 | * no need to allocate extra room for the header we're returning, | 517 | * no need to allocate extra room for the header we're returning, | |
519 | * so just declare it static. | 518 | * so just declare it static. | |
520 | */ | 519 | */ | |
521 | static struct ar_hdr sarh; | 520 | static struct ar_hdr sarh; | |
522 | 521 | |||
@@ -618,49 +617,46 @@ ArchStatMember(const char *archive, cons | @@ -618,49 +617,46 @@ ArchStatMember(const char *archive, cons | |||
618 | if ((unsigned int)elen > MAXPATHLEN) | 617 | if ((unsigned int)elen > MAXPATHLEN) | |
619 | goto badarch; | 618 | goto badarch; | |
620 | if (fread(memName, (size_t)elen, 1, arch) != 1) | 619 | if (fread(memName, (size_t)elen, 1, arch) != 1) | |
621 | goto badarch; | 620 | goto badarch; | |
622 | memName[elen] = '\0'; | 621 | memName[elen] = '\0'; | |
623 | if (fseek(arch, -elen, SEEK_CUR) != 0) | 622 | if (fseek(arch, -elen, SEEK_CUR) != 0) | |
624 | goto badarch; | 623 | goto badarch; | |
625 | if (DEBUG(ARCH) || DEBUG(MAKE)) { | 624 | if (DEBUG(ARCH) || DEBUG(MAKE)) { | |
626 | fprintf(debug_file, "ArchStat: Extended format entry for %s\n", memName); | 625 | fprintf(debug_file, "ArchStat: Extended format entry for %s\n", memName); | |
627 | } | 626 | } | |
628 | } | 627 | } | |
629 | #endif | 628 | #endif | |
630 | 629 | |||
631 | he = Hash_CreateEntry(&ar->members, memName, NULL); | 630 | { | |
632 | Hash_SetValue(he, bmake_malloc(sizeof(struct ar_hdr))); | 631 | Hash_Entry *he; | |
633 | memcpy(Hash_GetValue(he), &arh, sizeof(struct ar_hdr)); | 632 | he = Hash_CreateEntry(&ar->members, memName, NULL); | |
633 | Hash_SetValue(he, bmake_malloc(sizeof(struct ar_hdr))); | |||
634 | memcpy(Hash_GetValue(he), &arh, sizeof(struct ar_hdr)); | |||
635 | } | |||
634 | } | 636 | } | |
635 | if (fseek(arch, ((long)size + 1) & ~1, SEEK_CUR) != 0) | 637 | if (fseek(arch, ((long)size + 1) & ~1, SEEK_CUR) != 0) | |
636 | goto badarch; | 638 | goto badarch; | |
637 | } | 639 | } | |
638 | 640 | |||
639 | fclose(arch); | 641 | fclose(arch); | |
640 | 642 | |||
641 | Lst_Append(archives, ar); | 643 | Lst_Append(archives, ar); | |
642 | 644 | |||
643 | /* | 645 | /* | |
644 | * Now that the archive has been read and cached, we can look into | 646 | * Now that the archive has been read and cached, we can look into | |
645 | * the hash table to find the desired member's header. | 647 | * the hash table to find the desired member's header. | |
646 | */ | 648 | */ | |
647 | he = Hash_FindEntry(&ar->members, member); | 649 | return Hash_FindValue(&ar->members, member); | |
648 | ||||
649 | if (he != NULL) { | |||
650 | return (struct ar_hdr *)Hash_GetValue(he); | |||
651 | } else { | |||
652 | return NULL; | |||
653 | } | |||
654 | 650 | |||
655 | badarch: | 651 | badarch: | |
656 | fclose(arch); | 652 | fclose(arch); | |
657 | Hash_DeleteTable(&ar->members); | 653 | Hash_DeleteTable(&ar->members); | |
658 | free(ar->fnametab); | 654 | free(ar->fnametab); | |
659 | free(ar); | 655 | free(ar); | |
660 | return NULL; | 656 | return NULL; | |
661 | } | 657 | } | |
662 | 658 | |||
663 | #ifdef SVR4ARCHIVES | 659 | #ifdef SVR4ARCHIVES | |
664 | /*- | 660 | /*- | |
665 | *----------------------------------------------------------------------- | 661 | *----------------------------------------------------------------------- | |
666 | * ArchSVR4Entry -- | 662 | * ArchSVR4Entry -- |
--- src/usr.bin/make/hash.c 2020/09/13 15:15:51 1.32
+++ src/usr.bin/make/hash.c 2020/09/26 14:48:31 1.33
@@ -1,14 +1,14 @@ | @@ -1,14 +1,14 @@ | |||
1 | /* $NetBSD: hash.c,v 1.32 2020/09/13 15:15:51 rillig Exp $ */ | 1 | /* $NetBSD: hash.c,v 1.33 2020/09/26 14:48:31 rillig Exp $ */ | |
2 | 2 | |||
3 | /* | 3 | /* | |
4 | * Copyright (c) 1988, 1989, 1990 The Regents of the University of California. | 4 | * Copyright (c) 1988, 1989, 1990 The Regents of the University of California. | |
5 | * All rights reserved. | 5 | * 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. | |
@@ -69,27 +69,27 @@ | @@ -69,27 +69,27 @@ | |||
69 | * SUCH DAMAGE. | 69 | * SUCH DAMAGE. | |
70 | */ | 70 | */ | |
71 | 71 | |||
72 | /* hash.c -- | 72 | /* hash.c -- | |
73 | * | 73 | * | |
74 | * This module contains routines to manipulate a hash table. | 74 | * This module contains routines to manipulate a hash table. | |
75 | * See hash.h for a definition of the structure of the hash | 75 | * See hash.h for a definition of the structure of the hash | |
76 | * table. Hash tables grow automatically as the amount of | 76 | * table. Hash tables grow automatically as the amount of | |
77 | * information increases. | 77 | * information increases. | |
78 | */ | 78 | */ | |
79 | #include "make.h" | 79 | #include "make.h" | |
80 | 80 | |||
81 | /* "@(#)hash.c 8.1 (Berkeley) 6/6/93" */ | 81 | /* "@(#)hash.c 8.1 (Berkeley) 6/6/93" */ | |
82 | MAKE_RCSID("$NetBSD: hash.c,v 1.32 2020/09/13 15:15:51 rillig Exp $"); | 82 | MAKE_RCSID("$NetBSD: hash.c,v 1.33 2020/09/26 14:48:31 rillig Exp $"); | |
83 | 83 | |||
84 | /* | 84 | /* | |
85 | * Forward references to local procedures that are used before they're | 85 | * Forward references to local procedures that are used before they're | |
86 | * defined: | 86 | * defined: | |
87 | */ | 87 | */ | |
88 | 88 | |||
89 | static void RebuildTable(Hash_Table *); | 89 | static void RebuildTable(Hash_Table *); | |
90 | 90 | |||
91 | /* | 91 | /* | |
92 | * The following defines the ratio of # entries to # buckets | 92 | * The following defines the ratio of # entries to # buckets | |
93 | * at which we rebuild the table to make it larger. | 93 | * at which we rebuild the table to make it larger. | |
94 | */ | 94 | */ | |
95 | 95 | |||
@@ -178,26 +178,33 @@ Hash_FindEntry(Hash_Table *t, const char | @@ -178,26 +178,33 @@ Hash_FindEntry(Hash_Table *t, const char | |||
178 | fprintf(debug_file, "%s: %p h=%x key=%s\n", __func__, | 178 | fprintf(debug_file, "%s: %p h=%x key=%s\n", __func__, | |
179 | t, h, key); | 179 | t, h, key); | |
180 | #endif | 180 | #endif | |
181 | for (e = t->buckets[h & t->bucketsMask]; e != NULL; e = e->next) { | 181 | for (e = t->buckets[h & t->bucketsMask]; e != NULL; e = e->next) { | |
182 | chainlen++; | 182 | chainlen++; | |
183 | if (e->namehash == h && strcmp(e->name, p) == 0) | 183 | if (e->namehash == h && strcmp(e->name, p) == 0) | |
184 | break; | 184 | break; | |
185 | } | 185 | } | |
186 | if (chainlen > t->maxchain) | 186 | if (chainlen > t->maxchain) | |
187 | t->maxchain = chainlen; | 187 | t->maxchain = chainlen; | |
188 | return e; | 188 | return e; | |
189 | } | 189 | } | |
190 | 190 | |||
191 | void * | |||
192 | Hash_FindValue(Hash_Table *t, const char *key) | |||
193 | { | |||
194 | Hash_Entry *he = Hash_FindEntry(t, key); | |||
195 | return he != NULL ? he->value : NULL; | |||
196 | } | |||
197 | ||||
191 | /* Searches the hash table for an entry corresponding to the key. | 198 | /* Searches the hash table for an entry corresponding to the key. | |
192 | * If no entry is found, then one is created. | 199 | * If no entry is found, then one is created. | |
193 | * | 200 | * | |
194 | * Input: | 201 | * Input: | |
195 | * t Hash table to search. | 202 | * t Hash table to search. | |
196 | * key A hash key. | 203 | * key A hash key. | |
197 | * newPtr Filled with TRUE if new entry created, | 204 | * newPtr Filled with TRUE if new entry created, | |
198 | * FALSE otherwise. | 205 | * FALSE otherwise. | |
199 | */ | 206 | */ | |
200 | Hash_Entry * | 207 | Hash_Entry * | |
201 | Hash_CreateEntry(Hash_Table *t, const char *key, Boolean *newPtr) | 208 | Hash_CreateEntry(Hash_Table *t, const char *key, Boolean *newPtr) | |
202 | { | 209 | { | |
203 | Hash_Entry *e; | 210 | Hash_Entry *e; |
--- src/usr.bin/make/hash.h 2020/09/13 15:27:25 1.23
+++ src/usr.bin/make/hash.h 2020/09/26 14:48:31 1.24
@@ -1,14 +1,14 @@ | @@ -1,14 +1,14 @@ | |||
1 | /* $NetBSD: hash.h,v 1.23 2020/09/13 15:27:25 rillig Exp $ */ | 1 | /* $NetBSD: hash.h,v 1.24 2020/09/26 14:48:31 rillig Exp $ */ | |
2 | 2 | |||
3 | /* | 3 | /* | |
4 | * Copyright (c) 1988, 1989, 1990 The Regents of the University of California. | 4 | * Copyright (c) 1988, 1989, 1990 The Regents of the University of California. | |
5 | * | 5 | * | |
6 | * This code is derived from software contributed to Berkeley by | 6 | * This code is derived from software contributed to Berkeley by | |
7 | * Adam de Boor. | 7 | * Adam de Boor. | |
8 | * | 8 | * | |
9 | * Redistribution and use in source and binary forms, with or without | 9 | * Redistribution and use in source and binary forms, with or without | |
10 | * modification, are permitted provided that the following conditions | 10 | * modification, are permitted provided that the following conditions | |
11 | * are met: | 11 | * are met: | |
12 | * 1. Redistributions of source code must retain the above copyright | 12 | * 1. Redistributions of source code must retain the above copyright | |
13 | * notice, this list of conditions and the following disclaimer. | 13 | * notice, this list of conditions and the following disclaimer. | |
14 | * 2. Redistributions in binary form must reproduce the above copyright | 14 | * 2. Redistributions in binary form must reproduce the above copyright | |
@@ -111,21 +111,22 @@ Hash_GetValue(Hash_Entry *h) | @@ -111,21 +111,22 @@ Hash_GetValue(Hash_Entry *h) | |||
111 | { | 111 | { | |
112 | return h->value; | 112 | return h->value; | |
113 | } | 113 | } | |
114 | 114 | |||
115 | static inline MAKE_ATTR_UNUSED void | 115 | static inline MAKE_ATTR_UNUSED void | |
116 | Hash_SetValue(Hash_Entry *h, void *datum) | 116 | Hash_SetValue(Hash_Entry *h, void *datum) | |
117 | { | 117 | { | |
118 | h->value = datum; | 118 | h->value = datum; | |
119 | } | 119 | } | |
120 | 120 | |||
121 | void Hash_InitTable(Hash_Table *); | 121 | void Hash_InitTable(Hash_Table *); | |
122 | void Hash_DeleteTable(Hash_Table *); | 122 | void Hash_DeleteTable(Hash_Table *); | |
123 | Hash_Entry *Hash_FindEntry(Hash_Table *, const char *); | 123 | Hash_Entry *Hash_FindEntry(Hash_Table *, const char *); | |
124 | void *Hash_FindValue(Hash_Table *, const char *); | |||
124 | Hash_Entry *Hash_CreateEntry(Hash_Table *, const char *, Boolean *); | 125 | Hash_Entry *Hash_CreateEntry(Hash_Table *, const char *, Boolean *); | |
125 | void Hash_DeleteEntry(Hash_Table *, Hash_Entry *); | 126 | void Hash_DeleteEntry(Hash_Table *, Hash_Entry *); | |
126 | Hash_Entry *Hash_EnumFirst(Hash_Table *, Hash_Search *); | 127 | Hash_Entry *Hash_EnumFirst(Hash_Table *, Hash_Search *); | |
127 | Hash_Entry *Hash_EnumNext(Hash_Search *); | 128 | Hash_Entry *Hash_EnumNext(Hash_Search *); | |
128 | void Hash_ForEach(Hash_Table *, void (*)(void *, void *), void *); | 129 | void Hash_ForEach(Hash_Table *, void (*)(void *, void *), void *); | |
129 | void Hash_DebugStats(Hash_Table *, const char *); | 130 | void Hash_DebugStats(Hash_Table *, const char *); | |
130 | 131 | |||
131 | #endif /* MAKE_HASH_H */ | 132 | #endif /* MAKE_HASH_H */ |
--- src/usr.bin/make/var.c 2020/09/25 15:54:51 1.541
+++ src/usr.bin/make/var.c 2020/09/26 14:48:31 1.542
@@ -1,14 +1,14 @@ | @@ -1,14 +1,14 @@ | |||
1 | /* $NetBSD: var.c,v 1.541 2020/09/25 15:54:51 rillig Exp $ */ | 1 | /* $NetBSD: var.c,v 1.542 2020/09/26 14:48:31 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. | |
@@ -111,27 +111,27 @@ | @@ -111,27 +111,27 @@ | |||
111 | #include <sys/types.h> | 111 | #include <sys/types.h> | |
112 | #include <regex.h> | 112 | #include <regex.h> | |
113 | #endif | 113 | #endif | |
114 | #include <inttypes.h> | 114 | #include <inttypes.h> | |
115 | #include <limits.h> | 115 | #include <limits.h> | |
116 | #include <time.h> | 116 | #include <time.h> | |
117 | 117 | |||
118 | #include "make.h" | 118 | #include "make.h" | |
119 | #include "dir.h" | 119 | #include "dir.h" | |
120 | #include "job.h" | 120 | #include "job.h" | |
121 | #include "metachar.h" | 121 | #include "metachar.h" | |
122 | 122 | |||
123 | /* "@(#)var.c 8.3 (Berkeley) 3/19/94" */ | 123 | /* "@(#)var.c 8.3 (Berkeley) 3/19/94" */ | |
124 | MAKE_RCSID("$NetBSD: var.c,v 1.541 2020/09/25 15:54:51 rillig Exp $"); | 124 | MAKE_RCSID("$NetBSD: var.c,v 1.542 2020/09/26 14:48:31 rillig Exp $"); | |
125 | 125 | |||
126 | #define VAR_DEBUG_IF(cond, fmt, ...) \ | 126 | #define VAR_DEBUG_IF(cond, fmt, ...) \ | |
127 | if (!(DEBUG(VAR) && (cond))) \ | 127 | if (!(DEBUG(VAR) && (cond))) \ | |
128 | (void) 0; \ | 128 | (void) 0; \ | |
129 | else \ | 129 | else \ | |
130 | fprintf(debug_file, fmt, __VA_ARGS__) | 130 | fprintf(debug_file, fmt, __VA_ARGS__) | |
131 | 131 | |||
132 | #define VAR_DEBUG(fmt, ...) VAR_DEBUG_IF(TRUE, fmt, __VA_ARGS__) | 132 | #define VAR_DEBUG(fmt, ...) VAR_DEBUG_IF(TRUE, fmt, __VA_ARGS__) | |
133 | 133 | |||
134 | ENUM_FLAGS_RTTI_3(VarEvalFlags, | 134 | ENUM_FLAGS_RTTI_3(VarEvalFlags, | |
135 | VARE_UNDEFERR, VARE_WANTRES, VARE_ASSIGN); | 135 | VARE_UNDEFERR, VARE_WANTRES, VARE_ASSIGN); | |
136 | 136 | |||
137 | /* | 137 | /* | |
@@ -267,27 +267,27 @@ typedef enum { | @@ -267,27 +267,27 @@ typedef enum { | |||
267 | * ctxt context in which to find it | 267 | * ctxt context in which to find it | |
268 | * flags FIND_GLOBAL look in VAR_GLOBAL as well | 268 | * flags FIND_GLOBAL look in VAR_GLOBAL as well | |
269 | * FIND_CMD look in VAR_CMD as well | 269 | * FIND_CMD look in VAR_CMD as well | |
270 | * FIND_ENV look in the environment as well | 270 | * FIND_ENV look in the environment as well | |
271 | * | 271 | * | |
272 | * Results: | 272 | * Results: | |
273 | * A pointer to the structure describing the desired variable or | 273 | * A pointer to the structure describing the desired variable or | |
274 | * NULL if the variable does not exist. | 274 | * NULL if the variable does not exist. | |
275 | *----------------------------------------------------------------------- | 275 | *----------------------------------------------------------------------- | |
276 | */ | 276 | */ | |
277 | static Var * | 277 | static Var * | |
278 | VarFind(const char *name, GNode *ctxt, VarFindFlags flags) | 278 | VarFind(const char *name, GNode *ctxt, VarFindFlags flags) | |
279 | { | 279 | { | |
280 | Hash_Entry *var; | 280 | Var *var; | |
281 | 281 | |||
282 | /* | 282 | /* | |
283 | * If the variable name begins with a '.', it could very well be one of | 283 | * If the variable name begins with a '.', it could very well be one of | |
284 | * the local ones. We check the name against all the local variables | 284 | * the local ones. We check the name against all the local variables | |
285 | * and substitute the short version in for 'name' if it matches one of | 285 | * and substitute the short version in for 'name' if it matches one of | |
286 | * them. | 286 | * them. | |
287 | */ | 287 | */ | |
288 | if (*name == '.' && ch_isupper(name[1])) { | 288 | if (*name == '.' && ch_isupper(name[1])) { | |
289 | switch (name[1]) { | 289 | switch (name[1]) { | |
290 | case 'A': | 290 | case 'A': | |
291 | if (strcmp(name, ".ALLSRC") == 0) | 291 | if (strcmp(name, ".ALLSRC") == 0) | |
292 | name = ALLSRC; | 292 | name = ALLSRC; | |
293 | if (strcmp(name, ".ARCHIVE") == 0) | 293 | if (strcmp(name, ".ARCHIVE") == 0) | |
@@ -312,85 +312,79 @@ VarFind(const char *name, GNode *ctxt, V | @@ -312,85 +312,79 @@ VarFind(const char *name, GNode *ctxt, V | |||
312 | case 'S': | 312 | case 'S': | |
313 | if (strcmp(name, ".SHELL") == 0 ) { | 313 | if (strcmp(name, ".SHELL") == 0 ) { | |
314 | if (!shellPath) | 314 | if (!shellPath) | |
315 | Shell_Init(); | 315 | Shell_Init(); | |
316 | } | 316 | } | |
317 | break; | 317 | break; | |
318 | case 'T': | 318 | case 'T': | |
319 | if (strcmp(name, ".TARGET") == 0) | 319 | if (strcmp(name, ".TARGET") == 0) | |
320 | name = TARGET; | 320 | name = TARGET; | |
321 | break; | 321 | break; | |
322 | } | 322 | } | |
323 | } | 323 | } | |
324 | 324 | |||
325 | #ifdef notyet | 325 | #if 0 | |
326 | /* for compatibility with gmake */ | 326 | /* for compatibility with gmake */ | |
327 | if (name[0] == '^' && name[1] == '\0') | 327 | if (name[0] == '^' && name[1] == '\0') | |
328 | name = ALLSRC; | 328 | name = ALLSRC; | |
329 | #endif | 329 | #endif | |
330 | 330 | |||
331 | /* | 331 | /* | |
332 | * First look for the variable in the given context. If it's not there, | 332 | * First look for the variable in the given context. If it's not there, | |
333 | * look for it in VAR_CMD, VAR_GLOBAL and the environment, in that order, | 333 | * look for it in VAR_CMD, VAR_GLOBAL and the environment, in that order, | |
334 | * depending on the FIND_* flags in 'flags' | 334 | * depending on the FIND_* flags in 'flags' | |
335 | */ | 335 | */ | |
336 | var = Hash_FindEntry(&ctxt->context, name); | 336 | var = Hash_FindValue(&ctxt->context, name); | |
337 | 337 | |||
338 | if (var == NULL && (flags & FIND_CMD) && ctxt != VAR_CMD) | 338 | if (var == NULL && (flags & FIND_CMD) && ctxt != VAR_CMD) | |
339 | var = Hash_FindEntry(&VAR_CMD->context, name); | 339 | var = Hash_FindValue(&VAR_CMD->context, name); | |
340 | 340 | |||
341 | if (!checkEnvFirst && var == NULL && (flags & FIND_GLOBAL) && | 341 | if (!checkEnvFirst && var == NULL && (flags & FIND_GLOBAL) && | |
342 | ctxt != VAR_GLOBAL) | 342 | ctxt != VAR_GLOBAL) | |
343 | { | 343 | { | |
344 | var = Hash_FindEntry(&VAR_GLOBAL->context, name); | 344 | var = Hash_FindValue(&VAR_GLOBAL->context, name); | |
345 | if (var == NULL && ctxt != VAR_INTERNAL) { | 345 | if (var == NULL && ctxt != VAR_INTERNAL) { | |
346 | /* VAR_INTERNAL is subordinate to VAR_GLOBAL */ | 346 | /* VAR_INTERNAL is subordinate to VAR_GLOBAL */ | |
347 | var = Hash_FindEntry(&VAR_INTERNAL->context, name); | 347 | var = Hash_FindValue(&VAR_INTERNAL->context, name); | |
348 | } | 348 | } | |
349 | } | 349 | } | |
350 | 350 | |||
351 | if (var == NULL && (flags & FIND_ENV)) { | 351 | if (var == NULL && (flags & FIND_ENV)) { | |
352 | char *env; | 352 | char *env; | |
353 | 353 | |||
354 | if ((env = getenv(name)) != NULL) { | 354 | if ((env = getenv(name)) != NULL) { | |
355 | Var *v = bmake_malloc(sizeof(Var)); | 355 | Var *v = bmake_malloc(sizeof(Var)); | |
356 | size_t len; | 356 | size_t len; | |
357 | v->name = bmake_strdup(name); | 357 | v->name = bmake_strdup(name); | |
358 | 358 | |||
359 | len = strlen(env); | 359 | len = strlen(env); | |
360 | Buf_Init(&v->val, len + 1); | 360 | Buf_Init(&v->val, len + 1); | |
361 | Buf_AddBytes(&v->val, env, len); | 361 | Buf_AddBytes(&v->val, env, len); | |
362 | 362 | |||
363 | v->flags = VAR_FROM_ENV; | 363 | v->flags = VAR_FROM_ENV; | |
364 | return v; | 364 | return v; | |
365 | } | 365 | } | |
366 | 366 | |||
367 | if (checkEnvFirst && (flags & FIND_GLOBAL) && ctxt != VAR_GLOBAL) { | 367 | if (checkEnvFirst && (flags & FIND_GLOBAL) && ctxt != VAR_GLOBAL) { | |
368 | var = Hash_FindEntry(&VAR_GLOBAL->context, name); | 368 | var = Hash_FindValue(&VAR_GLOBAL->context, name); | |
369 | if (var == NULL && ctxt != VAR_INTERNAL) | 369 | if (var == NULL && ctxt != VAR_INTERNAL) | |
370 | var = Hash_FindEntry(&VAR_INTERNAL->context, name); | 370 | var = Hash_FindValue(&VAR_INTERNAL->context, name); | |
371 | if (var == NULL) | 371 | return var; | |
372 | return NULL; | |||
373 | else | |||
374 | return (Var *)Hash_GetValue(var); | |||
375 | } | 372 | } | |
376 | 373 | |||
377 | return NULL; | 374 | return NULL; | |
378 | } | 375 | } | |
379 | 376 | |||
380 | if (var == NULL) | 377 | return var; | |
381 | return NULL; | |||
382 | else | |||
383 | return (Var *)Hash_GetValue(var); | |||
384 | } | 378 | } | |
385 | 379 | |||
386 | /*- | 380 | /*- | |
387 | *----------------------------------------------------------------------- | 381 | *----------------------------------------------------------------------- | |
388 | * VarFreeEnv -- | 382 | * VarFreeEnv -- | |
389 | * If the variable is an environment variable, free it | 383 | * If the variable is an environment variable, free it | |
390 | * | 384 | * | |
391 | * Input: | 385 | * Input: | |
392 | * v the variable | 386 | * v the variable | |
393 | * destroy true if the value buffer should be destroyed. | 387 | * destroy true if the value buffer should be destroyed. | |
394 | * | 388 | * | |
395 | * Results: | 389 | * Results: | |
396 | * TRUE if it is an environment variable, FALSE otherwise. | 390 | * TRUE if it is an environment variable, FALSE otherwise. |