Home | History | Annotate | Line # | Download | only in make
lst.c revision 1.53
      1  1.53  rillig /* $NetBSD: lst.c,v 1.53 2020/08/29 09:30:10 rillig Exp $ */
      2   1.1  rillig 
      3   1.1  rillig /*
      4   1.1  rillig  * Copyright (c) 1988, 1989, 1990, 1993
      5   1.1  rillig  *	The Regents of the University of California.  All rights reserved.
      6   1.1  rillig  *
      7   1.1  rillig  * This code is derived from software contributed to Berkeley by
      8   1.1  rillig  * Adam de Boor.
      9   1.1  rillig  *
     10   1.1  rillig  * Redistribution and use in source and binary forms, with or without
     11   1.1  rillig  * modification, are permitted provided that the following conditions
     12   1.1  rillig  * are met:
     13   1.1  rillig  * 1. Redistributions of source code must retain the above copyright
     14   1.1  rillig  *    notice, this list of conditions and the following disclaimer.
     15   1.1  rillig  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1  rillig  *    notice, this list of conditions and the following disclaimer in the
     17   1.1  rillig  *    documentation and/or other materials provided with the distribution.
     18   1.1  rillig  * 3. Neither the name of the University nor the names of its contributors
     19   1.1  rillig  *    may be used to endorse or promote products derived from this software
     20   1.1  rillig  *    without specific prior written permission.
     21   1.1  rillig  *
     22   1.1  rillig  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23   1.1  rillig  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24   1.1  rillig  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25   1.1  rillig  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26   1.1  rillig  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27   1.1  rillig  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28   1.1  rillig  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29   1.1  rillig  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30   1.1  rillig  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31   1.1  rillig  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32   1.1  rillig  * SUCH DAMAGE.
     33   1.1  rillig  */
     34   1.1  rillig 
     35  1.30   skrll #include <stdint.h>
     36   1.8  rillig 
     37  1.19  rillig #include "make.h"
     38   1.1  rillig 
     39   1.1  rillig #ifndef MAKE_NATIVE
     40  1.53  rillig static char rcsid[] = "$NetBSD: lst.c,v 1.53 2020/08/29 09:30:10 rillig Exp $";
     41   1.1  rillig #else
     42   1.1  rillig #include <sys/cdefs.h>
     43   1.1  rillig #ifndef lint
     44  1.53  rillig __RCSID("$NetBSD: lst.c,v 1.53 2020/08/29 09:30:10 rillig Exp $");
     45   1.1  rillig #endif /* not lint */
     46   1.1  rillig #endif
     47   1.1  rillig 
     48  1.13  rillig struct ListNode {
     49  1.15  rillig     struct ListNode *prev;	/* previous element in list */
     50  1.15  rillig     struct ListNode *next;	/* next in list */
     51   1.7  rillig     uint8_t useCount;		/* Count of functions using the node.
     52   1.4  rillig 				 * node may not be deleted until count
     53   1.4  rillig 				 * goes to 0 */
     54   1.7  rillig     Boolean deleted;		/* List node should be removed when done */
     55  1.39  rillig     union {
     56  1.39  rillig 	void *datum;		/* datum associated with this element */
     57  1.39  rillig 	const GNode *gnode;	/* alias, just for debugging */
     58  1.39  rillig 	const char *str;	/* alias, just for debugging */
     59  1.39  rillig     };
     60  1.13  rillig };
     61   1.1  rillig 
     62   1.1  rillig typedef enum {
     63   1.1  rillig     Head, Middle, Tail, Unknown
     64   1.1  rillig } Where;
     65   1.1  rillig 
     66  1.13  rillig struct List {
     67  1.15  rillig     LstNode first;		/* first node in list */
     68  1.15  rillig     LstNode last;		/* last node in list */
     69  1.20  rillig 
     70  1.20  rillig     /* fields for sequential access */
     71  1.21  rillig     Boolean isOpen;		/* true if list has been Lst_Open'ed */
     72  1.15  rillig     Where lastAccess;		/* Where in the list the last access was */
     73  1.15  rillig     LstNode curr;		/* current node, if open. NULL if
     74   1.4  rillig 				 * *just* opened */
     75  1.20  rillig     LstNode prev;		/* Previous node, if open. Used by Lst_Remove */
     76  1.13  rillig };
     77   1.1  rillig 
     78  1.22  rillig /* Allocate and initialize a list node.
     79  1.22  rillig  *
     80  1.22  rillig  * The fields 'prev' and 'next' must be initialized by the caller.
     81  1.22  rillig  */
     82  1.12  rillig static LstNode
     83  1.12  rillig LstNodeNew(void *datum)
     84  1.12  rillig {
     85  1.16  rillig     LstNode node = bmake_malloc(sizeof *node);
     86  1.16  rillig     node->useCount = 0;
     87  1.16  rillig     node->deleted = FALSE;
     88  1.16  rillig     node->datum = datum;
     89  1.16  rillig     return node;
     90  1.12  rillig }
     91  1.12  rillig 
     92   1.2  rillig static Boolean
     93  1.16  rillig LstIsEmpty(Lst list)
     94   1.2  rillig {
     95  1.16  rillig     return list->first == NULL;
     96   1.2  rillig }
     97   1.1  rillig 
     98   1.5  rillig /* Create and initialize a new, empty list. */
     99   1.1  rillig Lst
    100   1.5  rillig Lst_Init(void)
    101   1.1  rillig {
    102  1.16  rillig     Lst list = bmake_malloc(sizeof *list);
    103   1.1  rillig 
    104  1.16  rillig     list->first = NULL;
    105  1.16  rillig     list->last = NULL;
    106  1.16  rillig     list->isOpen = FALSE;
    107  1.16  rillig     list->lastAccess = Unknown;
    108   1.1  rillig 
    109  1.16  rillig     return list;
    110   1.1  rillig }
    111   1.1  rillig 
    112  1.14  rillig /* Duplicate an entire list, usually by copying the datum pointers.
    113  1.14  rillig  * If copyProc is given, that function is used to create the new datum from the
    114  1.35  rillig  * old datum, usually by creating a copy of it. */
    115   1.1  rillig Lst
    116  1.50  rillig Lst_Copy(Lst list, LstCopyProc copyProc)
    117   1.1  rillig {
    118  1.16  rillig     Lst newList;
    119  1.16  rillig     LstNode node;
    120   1.1  rillig 
    121  1.52  rillig     assert(list != NULL);
    122   1.1  rillig 
    123  1.16  rillig     newList = Lst_Init();
    124   1.1  rillig 
    125  1.35  rillig     for (node = list->first; node != NULL; node = node->next) {
    126  1.35  rillig 	void *datum = copyProc != NULL ? copyProc(node->datum) : node->datum;
    127  1.50  rillig 	Lst_Append(newList, datum);
    128   1.1  rillig     }
    129   1.1  rillig 
    130  1.16  rillig     return newList;
    131   1.1  rillig }
    132   1.1  rillig 
    133  1.42  rillig /* Free a list and all its nodes. The list data itself are not freed though. */
    134  1.42  rillig void
    135  1.50  rillig Lst_Free(Lst list)
    136  1.42  rillig {
    137  1.42  rillig     LstNode node;
    138  1.42  rillig     LstNode next;
    139  1.42  rillig 
    140  1.52  rillig     assert(list != NULL);
    141  1.42  rillig 
    142  1.42  rillig     for (node = list->first; node != NULL; node = next) {
    143  1.42  rillig 	next = node->next;
    144  1.42  rillig 	free(node);
    145  1.42  rillig     }
    146  1.42  rillig 
    147  1.42  rillig     free(list);
    148  1.42  rillig }
    149  1.42  rillig 
    150  1.14  rillig /* Destroy a list and free all its resources. If the freeProc is given, it is
    151  1.14  rillig  * called with the datum from each node in turn before the node is freed. */
    152   1.1  rillig void
    153  1.50  rillig Lst_Destroy(Lst list, LstFreeProc freeProc)
    154   1.1  rillig {
    155  1.16  rillig     LstNode node;
    156  1.42  rillig     LstNode next;
    157   1.1  rillig 
    158  1.52  rillig     assert(list != NULL);
    159  1.42  rillig     assert(freeProc != NULL);
    160   1.1  rillig 
    161  1.42  rillig     for (node = list->first; node != NULL; node = next) {
    162  1.42  rillig 	next = node->next;
    163  1.42  rillig 	freeProc(node->datum);
    164  1.42  rillig 	free(node);
    165   1.1  rillig     }
    166   1.1  rillig 
    167   1.1  rillig     free(list);
    168   1.1  rillig }
    169   1.1  rillig 
    170   1.1  rillig /*
    171   1.1  rillig  * Functions to modify a list
    172   1.1  rillig  */
    173   1.1  rillig 
    174  1.14  rillig /* Insert a new node with the given piece of data before the given node in the
    175  1.14  rillig  * given list. */
    176  1.26  rillig void
    177  1.50  rillig Lst_InsertBefore(Lst list, LstNode node, void *datum)
    178  1.26  rillig {
    179  1.26  rillig     LstNode newNode;
    180  1.26  rillig 
    181  1.52  rillig     assert(list != NULL);
    182  1.26  rillig     assert(!LstIsEmpty(list));
    183  1.52  rillig     assert(node != NULL);
    184  1.26  rillig     assert(datum != NULL);
    185  1.26  rillig 
    186  1.26  rillig     newNode = LstNodeNew(datum);
    187  1.26  rillig     newNode->prev = node->prev;
    188  1.26  rillig     newNode->next = node;
    189  1.26  rillig 
    190  1.26  rillig     if (node->prev != NULL) {
    191  1.26  rillig 	node->prev->next = newNode;
    192  1.26  rillig     }
    193  1.26  rillig     node->prev = newNode;
    194  1.26  rillig 
    195  1.26  rillig     if (node == list->first) {
    196  1.26  rillig 	list->first = newNode;
    197  1.26  rillig     }
    198  1.26  rillig }
    199  1.26  rillig 
    200  1.22  rillig /* Add a piece of data at the start of the given list. */
    201  1.22  rillig void
    202  1.50  rillig Lst_Prepend(Lst list, void *datum)
    203  1.22  rillig {
    204  1.22  rillig     LstNode node;
    205  1.22  rillig 
    206  1.52  rillig     assert(list != NULL);
    207  1.22  rillig     assert(datum != NULL);
    208  1.22  rillig 
    209  1.22  rillig     node = LstNodeNew(datum);
    210  1.22  rillig     node->prev = NULL;
    211  1.22  rillig     node->next = list->first;
    212  1.22  rillig 
    213  1.22  rillig     if (list->first == NULL) {
    214  1.22  rillig 	list->first = node;
    215  1.22  rillig 	list->last = node;
    216  1.22  rillig     } else {
    217  1.22  rillig 	list->first->prev = node;
    218  1.22  rillig 	list->first = node;
    219  1.22  rillig     }
    220  1.22  rillig }
    221  1.22  rillig 
    222  1.21  rillig /* Add a piece of data at the end of the given list. */
    223  1.21  rillig void
    224  1.50  rillig Lst_Append(Lst list, void *datum)
    225  1.21  rillig {
    226  1.21  rillig     LstNode node;
    227  1.21  rillig 
    228  1.52  rillig     assert(list != NULL);
    229  1.21  rillig     assert(datum != NULL);
    230  1.21  rillig 
    231  1.21  rillig     node = LstNodeNew(datum);
    232  1.21  rillig     node->prev = list->last;
    233  1.21  rillig     node->next = NULL;
    234  1.21  rillig 
    235  1.21  rillig     if (list->last == NULL) {
    236  1.21  rillig 	list->first = node;
    237  1.21  rillig 	list->last = node;
    238  1.21  rillig     } else {
    239  1.21  rillig 	list->last->next = node;
    240  1.21  rillig 	list->last = node;
    241  1.21  rillig     }
    242  1.21  rillig }
    243  1.21  rillig 
    244   1.8  rillig /* Remove the given node from the given list.
    245   1.8  rillig  * The datum stored in the node must be freed by the caller, if necessary. */
    246   1.8  rillig void
    247  1.50  rillig Lst_Remove(Lst list, LstNode node)
    248   1.1  rillig {
    249  1.52  rillig     assert(list != NULL);
    250  1.52  rillig     assert(node != NULL);
    251   1.1  rillig 
    252   1.1  rillig     /*
    253   1.1  rillig      * unlink it from the list
    254   1.1  rillig      */
    255  1.16  rillig     if (node->next != NULL) {
    256  1.16  rillig 	node->next->prev = node->prev;
    257   1.1  rillig     }
    258  1.16  rillig     if (node->prev != NULL) {
    259  1.16  rillig 	node->prev->next = node->next;
    260   1.1  rillig     }
    261   1.1  rillig 
    262   1.1  rillig     /*
    263  1.15  rillig      * if either the first or last of the list point to this node,
    264   1.1  rillig      * adjust them accordingly
    265   1.1  rillig      */
    266  1.16  rillig     if (list->first == node) {
    267  1.16  rillig 	list->first = node->next;
    268   1.1  rillig     }
    269  1.16  rillig     if (list->last == node) {
    270  1.16  rillig 	list->last = node->prev;
    271   1.1  rillig     }
    272   1.1  rillig 
    273   1.1  rillig     /*
    274   1.1  rillig      * Sequential access stuff. If the node we're removing is the current
    275   1.1  rillig      * node in the list, reset the current node to the previous one. If the
    276  1.15  rillig      * previous one was non-existent (prev == NULL), we set the
    277   1.1  rillig      * end to be Unknown, since it is.
    278   1.1  rillig      */
    279  1.16  rillig     if (list->isOpen && list->curr == node) {
    280  1.15  rillig 	list->curr = list->prev;
    281  1.15  rillig 	if (list->curr == NULL) {
    282  1.15  rillig 	    list->lastAccess = Unknown;
    283   1.1  rillig 	}
    284   1.1  rillig     }
    285   1.1  rillig 
    286   1.1  rillig     /*
    287   1.1  rillig      * note that the datum is unmolested. The caller must free it as
    288   1.1  rillig      * necessary and as expected.
    289   1.1  rillig      */
    290  1.16  rillig     if (node->useCount == 0) {
    291  1.16  rillig 	free(node);
    292   1.1  rillig     } else {
    293  1.16  rillig 	node->deleted = TRUE;
    294   1.1  rillig     }
    295   1.1  rillig }
    296   1.1  rillig 
    297   1.8  rillig /* Replace the datum in the given node with the new datum. */
    298   1.8  rillig void
    299  1.50  rillig LstNode_Set(LstNode node, void *datum)
    300   1.1  rillig {
    301  1.52  rillig     assert(node != NULL);
    302  1.37  rillig     assert(datum != NULL);
    303  1.37  rillig 
    304  1.16  rillig     node->datum = datum;
    305   1.1  rillig }
    306   1.1  rillig 
    307  1.37  rillig /* Replace the datum in the given node to NULL. */
    308  1.37  rillig void
    309  1.50  rillig LstNode_SetNull(LstNode node)
    310  1.37  rillig {
    311  1.52  rillig     assert(node != NULL);
    312  1.37  rillig 
    313  1.37  rillig     node->datum = NULL;
    314  1.37  rillig }
    315  1.37  rillig 
    316   1.1  rillig 
    317   1.1  rillig /*
    318   1.1  rillig  * Node-specific functions
    319   1.1  rillig  */
    320   1.1  rillig 
    321  1.42  rillig /* Return the first node from the given list, or NULL if the list is empty. */
    322  1.42  rillig LstNode
    323  1.50  rillig Lst_First(Lst list)
    324  1.42  rillig {
    325  1.52  rillig     assert(list != NULL);
    326  1.42  rillig 
    327  1.42  rillig     return list->first;
    328  1.42  rillig }
    329  1.42  rillig 
    330  1.42  rillig /* Return the last node from the given list, or NULL if the list is empty. */
    331  1.42  rillig LstNode
    332  1.50  rillig Lst_Last(Lst list)
    333  1.42  rillig {
    334  1.52  rillig     assert(list != NULL);
    335  1.42  rillig 
    336  1.42  rillig     return list->last;
    337  1.42  rillig }
    338  1.42  rillig 
    339   1.6  rillig /* Return the successor to the given node on its list, or NULL. */
    340   1.1  rillig LstNode
    341  1.50  rillig Lst_Succ(LstNode node)
    342  1.42  rillig {
    343  1.52  rillig     assert(node != NULL);
    344  1.42  rillig 
    345  1.42  rillig     return node->next;
    346  1.42  rillig }
    347  1.42  rillig 
    348   1.6  rillig /* Return the predecessor to the given node on its list, or NULL. */
    349   1.1  rillig LstNode
    350  1.50  rillig Lst_Prev(LstNode node)
    351   1.1  rillig {
    352  1.52  rillig     assert(node != NULL);
    353  1.33  rillig     return node->prev;
    354   1.1  rillig }
    355   1.1  rillig 
    356  1.28  rillig /* Return the datum stored in the given node. */
    357   1.1  rillig void *
    358  1.50  rillig Lst_Datum(LstNode node)
    359   1.1  rillig {
    360  1.52  rillig     assert(node != NULL);
    361  1.28  rillig     return node->datum;
    362   1.1  rillig }
    363   1.1  rillig 
    364   1.1  rillig 
    365   1.1  rillig /*
    366   1.1  rillig  * Functions for entire lists
    367   1.1  rillig  */
    368   1.1  rillig 
    369  1.42  rillig /* Return TRUE if the given list is empty. */
    370  1.42  rillig Boolean
    371  1.50  rillig Lst_IsEmpty(Lst list)
    372  1.42  rillig {
    373  1.52  rillig     assert(list != NULL);
    374  1.42  rillig 
    375  1.42  rillig     return LstIsEmpty(list);
    376  1.42  rillig }
    377  1.42  rillig 
    378  1.14  rillig /* Return the first node from the given list for which the given comparison
    379  1.14  rillig  * function returns 0, or NULL if none of the nodes matches. */
    380   1.1  rillig LstNode
    381  1.50  rillig Lst_Find(Lst list, LstFindProc cmp, const void *cmpData)
    382  1.42  rillig {
    383  1.42  rillig     if (LstIsEmpty(list))
    384  1.42  rillig 	return NULL;
    385  1.50  rillig     return Lst_FindFrom(list, Lst_First(list), cmp, cmpData);
    386  1.42  rillig }
    387  1.42  rillig 
    388  1.53  rillig /* Return the first node from the list for which the match function returns
    389  1.53  rillig  * TRUE, or NULL if none of the nodes matched. */
    390  1.53  rillig LstNode
    391  1.53  rillig Lst_FindB(Lst list, LstFindBProc match, const void *matchArgs)
    392  1.53  rillig {
    393  1.53  rillig     return Lst_FindFromB(list, Lst_First(list), match, matchArgs);
    394  1.53  rillig }
    395  1.53  rillig 
    396  1.14  rillig /* Return the first node from the given list, starting at the given node, for
    397  1.14  rillig  * which the given comparison function returns 0, or NULL if none of the nodes
    398  1.14  rillig  * matches. */
    399   1.1  rillig LstNode
    400  1.50  rillig Lst_FindFrom(Lst list, LstNode node, LstFindProc cmp, const void *cmpData)
    401  1.42  rillig {
    402  1.42  rillig     LstNode tln;
    403  1.42  rillig 
    404  1.52  rillig     assert(list != NULL);
    405  1.52  rillig     assert(node != NULL);
    406  1.42  rillig     assert(cmp != NULL);
    407  1.42  rillig 
    408  1.42  rillig     for (tln = node; tln != NULL; tln = tln->next) {
    409  1.41  rillig 	if (cmp(tln->datum, cmpData) == 0)
    410   1.1  rillig 	    return tln;
    411  1.42  rillig     }
    412   1.1  rillig 
    413   1.1  rillig     return NULL;
    414   1.1  rillig }
    415   1.1  rillig 
    416  1.53  rillig /* Return the first node from the list, starting at the given node, for which
    417  1.53  rillig  * the match function returns TRUE, or NULL if none of the nodes matches.
    418  1.53  rillig  *
    419  1.53  rillig  * The start node may be NULL, in which case nothing is found. This allows
    420  1.53  rillig  * for passing Lst_First or Lst_Succ as the start node. */
    421  1.53  rillig LstNode
    422  1.53  rillig Lst_FindFromB(Lst list, LstNode node, LstFindBProc match, const void *matchArgs)
    423  1.53  rillig {
    424  1.53  rillig     LstNode tln;
    425  1.53  rillig 
    426  1.53  rillig     assert(list != NULL);
    427  1.53  rillig     assert(match != NULL);
    428  1.53  rillig 
    429  1.53  rillig     for (tln = node; tln != NULL; tln = tln->next) {
    430  1.53  rillig 	if (match(tln->datum, matchArgs))
    431  1.53  rillig 	    return tln;
    432  1.53  rillig     }
    433  1.53  rillig 
    434  1.53  rillig     return NULL;
    435  1.53  rillig }
    436  1.53  rillig 
    437  1.14  rillig /* Return the first node that contains the given datum, or NULL. */
    438   1.1  rillig LstNode
    439  1.50  rillig Lst_Member(Lst list, void *datum)
    440   1.1  rillig {
    441  1.16  rillig     LstNode node;
    442   1.1  rillig 
    443  1.52  rillig     assert(list != NULL);
    444  1.29  rillig     assert(datum != NULL);
    445   1.1  rillig 
    446  1.29  rillig     for (node = list->first; node != NULL; node = node->next) {
    447  1.16  rillig 	if (node->datum == datum) {
    448  1.16  rillig 	    return node;
    449   1.1  rillig 	}
    450  1.29  rillig     }
    451   1.1  rillig 
    452   1.1  rillig     return NULL;
    453   1.1  rillig }
    454   1.1  rillig 
    455  1.14  rillig /* Apply the given function to each element of the given list. The function
    456  1.14  rillig  * should return 0 if traversal should continue and non-zero if it should
    457  1.14  rillig  * abort. */
    458   1.1  rillig int
    459  1.50  rillig Lst_ForEach(Lst list, LstActionProc proc, void *procData)
    460  1.42  rillig {
    461  1.42  rillig     if (LstIsEmpty(list))
    462  1.42  rillig 	return 0;		/* XXX: Document what this value means. */
    463  1.50  rillig     return Lst_ForEachFrom(list, Lst_First(list), proc, procData);
    464  1.42  rillig }
    465  1.42  rillig 
    466  1.14  rillig /* Apply the given function to each element of the given list, starting from
    467  1.14  rillig  * the given node. The function should return 0 if traversal should continue,
    468  1.14  rillig  * and non-zero if it should abort. */
    469   1.1  rillig int
    470  1.50  rillig Lst_ForEachFrom(Lst list, LstNode node,
    471  1.42  rillig 		 LstActionProc proc, void *procData)
    472   1.1  rillig {
    473  1.16  rillig     LstNode tln = node;
    474  1.13  rillig     LstNode next;
    475   1.4  rillig     Boolean done;
    476   1.4  rillig     int result;
    477   1.1  rillig 
    478  1.52  rillig     assert(list != NULL);
    479  1.52  rillig     assert(node != NULL);
    480  1.42  rillig     assert(proc != NULL);
    481   1.1  rillig 
    482   1.1  rillig     do {
    483   1.1  rillig 	/*
    484   1.1  rillig 	 * Take care of having the current element deleted out from under
    485   1.1  rillig 	 * us.
    486   1.1  rillig 	 */
    487   1.1  rillig 
    488  1.15  rillig 	next = tln->next;
    489   1.1  rillig 
    490   1.1  rillig 	/*
    491   1.1  rillig 	 * We're done with the traversal if
    492  1.38  rillig 	 *  - the next node to examine doesn't exist and
    493   1.1  rillig 	 *  - nothing's been added after the current node (check this
    494   1.1  rillig 	 *    after proc() has been called).
    495   1.1  rillig 	 */
    496  1.38  rillig 	done = next == NULL;
    497   1.1  rillig 
    498  1.17  rillig 	tln->useCount++;
    499  1.16  rillig 	result = (*proc)(tln->datum, procData);
    500  1.17  rillig 	tln->useCount--;
    501   1.1  rillig 
    502   1.1  rillig 	/*
    503   1.1  rillig 	 * Now check whether a node has been added.
    504   1.1  rillig 	 * Note: this doesn't work if this node was deleted before
    505   1.1  rillig 	 *       the new node was added.
    506   1.1  rillig 	 */
    507  1.15  rillig 	if (next != tln->next) {
    508  1.15  rillig 	    next = tln->next;
    509   1.4  rillig 	    done = 0;
    510   1.1  rillig 	}
    511   1.1  rillig 
    512   1.7  rillig 	if (tln->deleted) {
    513   1.1  rillig 	    free((char *)tln);
    514   1.1  rillig 	}
    515   1.1  rillig 	tln = next;
    516   1.1  rillig     } while (!result && !LstIsEmpty(list) && !done);
    517   1.1  rillig 
    518   1.1  rillig     return result;
    519   1.1  rillig }
    520   1.1  rillig 
    521  1.34  rillig /* Move all nodes from list2 to the end of list1.
    522  1.34  rillig  * List2 is destroyed and freed. */
    523  1.34  rillig void
    524  1.50  rillig Lst_MoveAll(Lst list1, Lst list2)
    525   1.1  rillig {
    526  1.52  rillig     assert(list1 != NULL);
    527  1.52  rillig     assert(list2 != NULL);
    528   1.1  rillig 
    529  1.34  rillig     if (list2->first != NULL) {
    530  1.34  rillig 	list2->first->prev = list1->last;
    531  1.34  rillig 	if (list1->last != NULL) {
    532  1.34  rillig 	    list1->last->next = list2->first;
    533  1.34  rillig 	} else {
    534  1.34  rillig 	    list1->first = list2->first;
    535   1.1  rillig 	}
    536  1.34  rillig 	list1->last = list2->last;
    537   1.1  rillig     }
    538  1.34  rillig     free(list2);
    539   1.1  rillig }
    540   1.1  rillig 
    541  1.22  rillig /* Copy the element data from src to the start of dst. */
    542  1.22  rillig void
    543  1.50  rillig Lst_PrependAll(Lst dst, Lst src)
    544  1.22  rillig {
    545  1.22  rillig     LstNode node;
    546  1.22  rillig     for (node = src->last; node != NULL; node = node->prev)
    547  1.50  rillig 	Lst_Prepend(dst, node->datum);
    548  1.22  rillig }
    549  1.22  rillig 
    550  1.22  rillig /* Copy the element data from src to the end of dst. */
    551  1.22  rillig void
    552  1.50  rillig Lst_AppendAll(Lst dst, Lst src)
    553  1.22  rillig {
    554  1.22  rillig     LstNode node;
    555  1.22  rillig     for (node = src->first; node != NULL; node = node->next)
    556  1.50  rillig 	Lst_Append(dst, node->datum);
    557  1.22  rillig }
    558   1.1  rillig 
    559   1.1  rillig /*
    560   1.1  rillig  * these functions are for dealing with a list as a table, of sorts.
    561   1.1  rillig  * An idea of the "current element" is kept and used by all the functions
    562   1.1  rillig  * between Lst_Open() and Lst_Close().
    563   1.1  rillig  *
    564   1.1  rillig  * The sequential functions access the list in a slightly different way.
    565   1.1  rillig  * CurPtr points to their idea of the current node in the list and they
    566   1.1  rillig  * access the list based on it.
    567   1.1  rillig  */
    568   1.1  rillig 
    569  1.14  rillig /* Open a list for sequential access. A list can still be searched, etc.,
    570  1.14  rillig  * without confusing these functions. */
    571  1.10  rillig void
    572  1.50  rillig Lst_Open(Lst list)
    573  1.10  rillig {
    574  1.52  rillig     assert(list != NULL);
    575  1.24  rillig 
    576  1.19  rillig     /* XXX: This assertion fails for NetBSD's "build.sh -j1 tools", somewhere
    577  1.19  rillig      * between "dependall ===> compat" and "dependall ===> binstall".
    578  1.19  rillig      * Building without the "-j1" succeeds though. */
    579  1.24  rillig     if (DEBUG(LINT) && list->isOpen)
    580  1.19  rillig 	Parse_Error(PARSE_WARNING, "Internal inconsistency: list opened twice");
    581  1.10  rillig 
    582  1.16  rillig     list->isOpen = TRUE;
    583  1.16  rillig     list->lastAccess = LstIsEmpty(list) ? Head : Unknown;
    584  1.16  rillig     list->curr = NULL;
    585  1.10  rillig }
    586  1.10  rillig 
    587  1.10  rillig /* Return the next node for the given list, or NULL if the end has been
    588  1.10  rillig  * reached. */
    589   1.1  rillig LstNode
    590  1.50  rillig Lst_Next(Lst list)
    591   1.1  rillig {
    592  1.16  rillig     LstNode node;
    593   1.1  rillig 
    594  1.52  rillig     assert(list != NULL);
    595   1.9  rillig     assert(list->isOpen);
    596   1.1  rillig 
    597  1.15  rillig     list->prev = list->curr;
    598   1.1  rillig 
    599  1.15  rillig     if (list->curr == NULL) {
    600  1.15  rillig 	if (list->lastAccess == Unknown) {
    601   1.1  rillig 	    /*
    602  1.15  rillig 	     * If we're just starting out, lastAccess will be Unknown.
    603   1.1  rillig 	     * Then we want to start this thing off in the right
    604  1.15  rillig 	     * direction -- at the start with lastAccess being Middle.
    605   1.1  rillig 	     */
    606  1.16  rillig 	    list->curr = node = list->first;
    607  1.15  rillig 	    list->lastAccess = Middle;
    608   1.1  rillig 	} else {
    609  1.16  rillig 	    node = NULL;
    610  1.15  rillig 	    list->lastAccess = Tail;
    611   1.1  rillig 	}
    612   1.1  rillig     } else {
    613  1.16  rillig 	node = list->curr->next;
    614  1.16  rillig 	list->curr = node;
    615   1.1  rillig 
    616  1.16  rillig 	if (node == list->first || node == NULL) {
    617   1.1  rillig 	    /*
    618   1.1  rillig 	     * If back at the front, then we've hit the end...
    619   1.1  rillig 	     */
    620  1.15  rillig 	    list->lastAccess = Tail;
    621   1.1  rillig 	} else {
    622   1.1  rillig 	    /*
    623   1.1  rillig 	     * Reset to Middle if gone past first.
    624   1.1  rillig 	     */
    625  1.15  rillig 	    list->lastAccess = Middle;
    626   1.1  rillig 	}
    627   1.1  rillig     }
    628   1.1  rillig 
    629  1.16  rillig     return node;
    630   1.1  rillig }
    631   1.1  rillig 
    632  1.10  rillig /* Close a list which was opened for sequential access. */
    633   1.1  rillig void
    634  1.50  rillig Lst_Close(Lst list)
    635   1.1  rillig {
    636  1.52  rillig     assert(list != NULL);
    637  1.16  rillig     assert(list->isOpen);
    638   1.1  rillig 
    639  1.10  rillig     list->isOpen = FALSE;
    640  1.15  rillig     list->lastAccess = Unknown;
    641   1.1  rillig }
    642   1.1  rillig 
    643   1.1  rillig 
    644   1.1  rillig /*
    645   1.1  rillig  * for using the list as a queue
    646   1.1  rillig  */
    647   1.1  rillig 
    648  1.14  rillig /* Add the datum to the tail of the given list. */
    649  1.25  rillig void
    650  1.50  rillig Lst_Enqueue(Lst list, void *datum)
    651   1.1  rillig {
    652  1.50  rillig     Lst_Append(list, datum);
    653   1.1  rillig }
    654   1.1  rillig 
    655  1.25  rillig /* Remove and return the datum at the head of the given list. */
    656   1.1  rillig void *
    657  1.50  rillig Lst_Dequeue(Lst list)
    658   1.1  rillig {
    659  1.16  rillig     void *datum;
    660   1.1  rillig 
    661  1.52  rillig     assert(list != NULL);
    662  1.25  rillig     assert(!LstIsEmpty(list));
    663   1.1  rillig 
    664  1.25  rillig     datum = list->first->datum;
    665  1.50  rillig     Lst_Remove(list, list->first);
    666  1.25  rillig     assert(datum != NULL);
    667  1.16  rillig     return datum;
    668   1.1  rillig }
    669