Home | History | Annotate | Line # | Download | only in make
lst.c revision 1.38
      1  1.38  rillig /* $NetBSD: lst.c,v 1.38 2020/08/23 11:13:08 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.38  rillig static char rcsid[] = "$NetBSD: lst.c,v 1.38 2020/08/23 11:13:08 rillig Exp $";
     41   1.1  rillig #else
     42   1.1  rillig #include <sys/cdefs.h>
     43   1.1  rillig #ifndef lint
     44  1.38  rillig __RCSID("$NetBSD: lst.c,v 1.38 2020/08/23 11:13:08 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.4  rillig     void *datum;		/* datum associated with this element */
     56  1.13  rillig };
     57   1.1  rillig 
     58   1.1  rillig typedef enum {
     59   1.1  rillig     Head, Middle, Tail, Unknown
     60   1.1  rillig } Where;
     61   1.1  rillig 
     62  1.13  rillig struct List {
     63  1.15  rillig     LstNode first;		/* first node in list */
     64  1.15  rillig     LstNode last;		/* last node in list */
     65  1.20  rillig 
     66  1.20  rillig     /* fields for sequential access */
     67  1.21  rillig     Boolean isOpen;		/* true if list has been Lst_Open'ed */
     68  1.15  rillig     Where lastAccess;		/* Where in the list the last access was */
     69  1.15  rillig     LstNode curr;		/* current node, if open. NULL if
     70   1.4  rillig 				 * *just* opened */
     71  1.20  rillig     LstNode prev;		/* Previous node, if open. Used by Lst_Remove */
     72  1.13  rillig };
     73   1.1  rillig 
     74   1.2  rillig static Boolean
     75  1.20  rillig LstIsValid(Lst list)
     76   1.2  rillig {
     77  1.16  rillig     return list != NULL;
     78   1.2  rillig }
     79   1.1  rillig 
     80   1.2  rillig static Boolean
     81  1.20  rillig LstNodeIsValid(LstNode node)
     82   1.2  rillig {
     83  1.16  rillig     return node != NULL;
     84   1.2  rillig }
     85   1.1  rillig 
     86  1.22  rillig /* Allocate and initialize a list node.
     87  1.22  rillig  *
     88  1.22  rillig  * The fields 'prev' and 'next' must be initialized by the caller.
     89  1.22  rillig  */
     90  1.12  rillig static LstNode
     91  1.12  rillig LstNodeNew(void *datum)
     92  1.12  rillig {
     93  1.16  rillig     LstNode node = bmake_malloc(sizeof *node);
     94  1.16  rillig     node->useCount = 0;
     95  1.16  rillig     node->deleted = FALSE;
     96  1.16  rillig     node->datum = datum;
     97  1.16  rillig     return node;
     98  1.12  rillig }
     99  1.12  rillig 
    100   1.2  rillig static Boolean
    101  1.16  rillig LstIsEmpty(Lst list)
    102   1.2  rillig {
    103  1.16  rillig     return list->first == NULL;
    104   1.2  rillig }
    105   1.1  rillig 
    106   1.5  rillig /* Create and initialize a new, empty list. */
    107   1.1  rillig Lst
    108   1.5  rillig Lst_Init(void)
    109   1.1  rillig {
    110  1.16  rillig     Lst list = bmake_malloc(sizeof *list);
    111   1.1  rillig 
    112  1.16  rillig     list->first = NULL;
    113  1.16  rillig     list->last = NULL;
    114  1.16  rillig     list->isOpen = FALSE;
    115  1.16  rillig     list->lastAccess = Unknown;
    116   1.1  rillig 
    117  1.16  rillig     return list;
    118   1.1  rillig }
    119   1.1  rillig 
    120  1.14  rillig /* Duplicate an entire list, usually by copying the datum pointers.
    121  1.14  rillig  * If copyProc is given, that function is used to create the new datum from the
    122  1.35  rillig  * old datum, usually by creating a copy of it. */
    123   1.1  rillig Lst
    124  1.35  rillig Lst_CopyS(Lst list, DuplicateProc *copyProc)
    125   1.1  rillig {
    126  1.16  rillig     Lst newList;
    127  1.16  rillig     LstNode node;
    128   1.1  rillig 
    129  1.35  rillig     assert(LstIsValid(list));
    130   1.1  rillig 
    131  1.16  rillig     newList = Lst_Init();
    132   1.1  rillig 
    133  1.35  rillig     for (node = list->first; node != NULL; node = node->next) {
    134  1.35  rillig 	void *datum = copyProc != NULL ? copyProc(node->datum) : node->datum;
    135  1.35  rillig 	Lst_AppendS(newList, datum);
    136   1.1  rillig     }
    137   1.1  rillig 
    138  1.16  rillig     return newList;
    139   1.1  rillig }
    140   1.1  rillig 
    141  1.14  rillig /* Destroy a list and free all its resources. If the freeProc is given, it is
    142  1.14  rillig  * called with the datum from each node in turn before the node is freed. */
    143   1.1  rillig void
    144   1.1  rillig Lst_Destroy(Lst list, FreeProc *freeProc)
    145   1.1  rillig {
    146  1.16  rillig     LstNode node;
    147  1.16  rillig     LstNode next = NULL;
    148   1.1  rillig 
    149   1.1  rillig     if (list == NULL)
    150   1.1  rillig 	return;
    151   1.1  rillig 
    152   1.1  rillig     /* To ease scanning */
    153  1.15  rillig     if (list->last != NULL)
    154  1.15  rillig 	list->last->next = NULL;
    155   1.1  rillig     else {
    156   1.1  rillig 	free(list);
    157   1.1  rillig 	return;
    158   1.1  rillig     }
    159   1.1  rillig 
    160   1.1  rillig     if (freeProc) {
    161  1.16  rillig 	for (node = list->first; node != NULL; node = next) {
    162  1.16  rillig 	    next = node->next;
    163  1.16  rillig 	    freeProc(node->datum);
    164  1.16  rillig 	    free(node);
    165   1.1  rillig 	}
    166   1.1  rillig     } else {
    167  1.16  rillig 	for (node = list->first; node != NULL; node = next) {
    168  1.16  rillig 	    next = node->next;
    169  1.16  rillig 	    free(node);
    170   1.1  rillig 	}
    171   1.1  rillig     }
    172   1.1  rillig 
    173   1.1  rillig     free(list);
    174   1.1  rillig }
    175   1.1  rillig 
    176   1.1  rillig /*
    177   1.1  rillig  * Functions to modify a list
    178   1.1  rillig  */
    179   1.1  rillig 
    180  1.14  rillig /* Insert a new node with the given piece of data before the given node in the
    181  1.14  rillig  * given list. */
    182  1.26  rillig void
    183  1.26  rillig Lst_InsertBeforeS(Lst list, LstNode node, void *datum)
    184  1.26  rillig {
    185  1.26  rillig     LstNode newNode;
    186  1.26  rillig 
    187  1.26  rillig     assert(LstIsValid(list));
    188  1.26  rillig     assert(!LstIsEmpty(list));
    189  1.26  rillig     assert(LstNodeIsValid(node));
    190  1.26  rillig     assert(datum != NULL);
    191  1.26  rillig 
    192  1.26  rillig     newNode = LstNodeNew(datum);
    193  1.26  rillig     newNode->prev = node->prev;
    194  1.26  rillig     newNode->next = node;
    195  1.26  rillig 
    196  1.26  rillig     if (node->prev != NULL) {
    197  1.26  rillig 	node->prev->next = newNode;
    198  1.26  rillig     }
    199  1.26  rillig     node->prev = newNode;
    200  1.26  rillig 
    201  1.26  rillig     if (node == list->first) {
    202  1.26  rillig 	list->first = newNode;
    203  1.26  rillig     }
    204  1.26  rillig }
    205  1.26  rillig 
    206  1.22  rillig /* Add a piece of data at the start of the given list. */
    207  1.22  rillig void
    208  1.22  rillig Lst_PrependS(Lst list, void *datum)
    209  1.22  rillig {
    210  1.22  rillig     LstNode node;
    211  1.22  rillig 
    212  1.22  rillig     assert(LstIsValid(list));
    213  1.22  rillig     assert(datum != NULL);
    214  1.22  rillig 
    215  1.22  rillig     node = LstNodeNew(datum);
    216  1.22  rillig     node->prev = NULL;
    217  1.22  rillig     node->next = list->first;
    218  1.22  rillig 
    219  1.22  rillig     if (list->first == NULL) {
    220  1.22  rillig 	list->first = node;
    221  1.22  rillig 	list->last = node;
    222  1.22  rillig     } else {
    223  1.22  rillig 	list->first->prev = node;
    224  1.22  rillig 	list->first = node;
    225  1.22  rillig     }
    226  1.22  rillig }
    227  1.22  rillig 
    228  1.21  rillig /* Add a piece of data at the end of the given list. */
    229  1.21  rillig void
    230  1.21  rillig Lst_AppendS(Lst list, void *datum)
    231  1.21  rillig {
    232  1.21  rillig     LstNode node;
    233  1.21  rillig 
    234  1.21  rillig     assert(LstIsValid(list));
    235  1.21  rillig     assert(datum != NULL);
    236  1.21  rillig 
    237  1.21  rillig     node = LstNodeNew(datum);
    238  1.21  rillig     node->prev = list->last;
    239  1.21  rillig     node->next = NULL;
    240  1.21  rillig 
    241  1.21  rillig     if (list->last == NULL) {
    242  1.21  rillig 	list->first = node;
    243  1.21  rillig 	list->last = node;
    244  1.21  rillig     } else {
    245  1.21  rillig 	list->last->next = node;
    246  1.21  rillig 	list->last = node;
    247  1.21  rillig     }
    248  1.21  rillig }
    249  1.21  rillig 
    250   1.8  rillig /* Remove the given node from the given list.
    251   1.8  rillig  * The datum stored in the node must be freed by the caller, if necessary. */
    252   1.8  rillig void
    253  1.16  rillig Lst_RemoveS(Lst list, LstNode node)
    254   1.1  rillig {
    255  1.20  rillig     assert(LstIsValid(list));
    256  1.20  rillig     assert(LstNodeIsValid(node));
    257   1.1  rillig 
    258   1.1  rillig     /*
    259   1.1  rillig      * unlink it from the list
    260   1.1  rillig      */
    261  1.16  rillig     if (node->next != NULL) {
    262  1.16  rillig 	node->next->prev = node->prev;
    263   1.1  rillig     }
    264  1.16  rillig     if (node->prev != NULL) {
    265  1.16  rillig 	node->prev->next = node->next;
    266   1.1  rillig     }
    267   1.1  rillig 
    268   1.1  rillig     /*
    269  1.15  rillig      * if either the first or last of the list point to this node,
    270   1.1  rillig      * adjust them accordingly
    271   1.1  rillig      */
    272  1.16  rillig     if (list->first == node) {
    273  1.16  rillig 	list->first = node->next;
    274   1.1  rillig     }
    275  1.16  rillig     if (list->last == node) {
    276  1.16  rillig 	list->last = node->prev;
    277   1.1  rillig     }
    278   1.1  rillig 
    279   1.1  rillig     /*
    280   1.1  rillig      * Sequential access stuff. If the node we're removing is the current
    281   1.1  rillig      * node in the list, reset the current node to the previous one. If the
    282  1.15  rillig      * previous one was non-existent (prev == NULL), we set the
    283   1.1  rillig      * end to be Unknown, since it is.
    284   1.1  rillig      */
    285  1.16  rillig     if (list->isOpen && list->curr == node) {
    286  1.15  rillig 	list->curr = list->prev;
    287  1.15  rillig 	if (list->curr == NULL) {
    288  1.15  rillig 	    list->lastAccess = Unknown;
    289   1.1  rillig 	}
    290   1.1  rillig     }
    291   1.1  rillig 
    292   1.1  rillig     /*
    293   1.1  rillig      * note that the datum is unmolested. The caller must free it as
    294   1.1  rillig      * necessary and as expected.
    295   1.1  rillig      */
    296  1.16  rillig     if (node->useCount == 0) {
    297  1.16  rillig 	free(node);
    298   1.1  rillig     } else {
    299  1.16  rillig 	node->deleted = TRUE;
    300   1.1  rillig     }
    301   1.1  rillig }
    302   1.1  rillig 
    303   1.8  rillig /* Replace the datum in the given node with the new datum. */
    304   1.8  rillig void
    305  1.37  rillig LstNode_SetS(LstNode node, void *datum)
    306   1.1  rillig {
    307  1.37  rillig     assert(LstNodeIsValid(node));
    308  1.37  rillig     assert(datum != NULL);
    309  1.37  rillig 
    310  1.16  rillig     node->datum = datum;
    311   1.1  rillig }
    312   1.1  rillig 
    313  1.37  rillig /* Replace the datum in the given node to NULL. */
    314  1.37  rillig void
    315  1.37  rillig LstNode_SetNullS(LstNode node)
    316  1.37  rillig {
    317  1.37  rillig     assert(LstNodeIsValid(node));
    318  1.37  rillig 
    319  1.37  rillig     node->datum = NULL;
    320  1.37  rillig }
    321  1.37  rillig 
    322   1.1  rillig 
    323   1.1  rillig /*
    324   1.1  rillig  * Node-specific functions
    325   1.1  rillig  */
    326   1.1  rillig 
    327  1.14  rillig /* Return the first node from the given list, or NULL if the list is empty or
    328  1.14  rillig  * invalid. */
    329   1.1  rillig LstNode
    330  1.16  rillig Lst_First(Lst list)
    331   1.1  rillig {
    332  1.20  rillig     if (!LstIsValid(list) || LstIsEmpty(list)) {
    333   1.1  rillig 	return NULL;
    334   1.1  rillig     } else {
    335  1.16  rillig 	return list->first;
    336   1.1  rillig     }
    337   1.1  rillig }
    338   1.1  rillig 
    339  1.14  rillig /* Return the last node from the given list, or NULL if the list is empty or
    340  1.14  rillig  * invalid. */
    341   1.1  rillig LstNode
    342  1.16  rillig Lst_Last(Lst list)
    343   1.1  rillig {
    344  1.20  rillig     if (!LstIsValid(list) || LstIsEmpty(list)) {
    345   1.1  rillig 	return NULL;
    346   1.1  rillig     } else {
    347  1.16  rillig 	return list->last;
    348   1.1  rillig     }
    349   1.1  rillig }
    350   1.1  rillig 
    351   1.6  rillig /* Return the successor to the given node on its list, or NULL. */
    352   1.1  rillig LstNode
    353  1.16  rillig Lst_Succ(LstNode node)
    354   1.1  rillig {
    355  1.16  rillig     if (node == NULL) {
    356   1.1  rillig 	return NULL;
    357   1.1  rillig     } else {
    358  1.16  rillig 	return node->next;
    359   1.1  rillig     }
    360   1.1  rillig }
    361   1.1  rillig 
    362   1.6  rillig /* Return the predecessor to the given node on its list, or NULL. */
    363   1.1  rillig LstNode
    364  1.33  rillig Lst_PrevS(LstNode node)
    365   1.1  rillig {
    366  1.33  rillig     assert(LstNodeIsValid(node));
    367  1.33  rillig     return node->prev;
    368   1.1  rillig }
    369   1.1  rillig 
    370  1.28  rillig /* Return the datum stored in the given node. */
    371   1.1  rillig void *
    372  1.28  rillig Lst_DatumS(LstNode node)
    373   1.1  rillig {
    374  1.28  rillig     assert(LstNodeIsValid(node));
    375  1.28  rillig     return node->datum;
    376   1.1  rillig }
    377   1.1  rillig 
    378   1.1  rillig 
    379   1.1  rillig /*
    380   1.1  rillig  * Functions for entire lists
    381   1.1  rillig  */
    382   1.1  rillig 
    383  1.14  rillig /* Return TRUE if the given list is empty or invalid. */
    384   1.1  rillig Boolean
    385  1.16  rillig Lst_IsEmpty(Lst list)
    386   1.1  rillig {
    387  1.20  rillig     return !LstIsValid(list) || LstIsEmpty(list);
    388   1.1  rillig }
    389   1.1  rillig 
    390  1.14  rillig /* Return the first node from the given list for which the given comparison
    391  1.14  rillig  * function returns 0, or NULL if none of the nodes matches. */
    392   1.1  rillig LstNode
    393  1.16  rillig Lst_Find(Lst list, const void *cmpData, int (*cmp)(const void *, const void *))
    394   1.1  rillig {
    395  1.16  rillig     return Lst_FindFrom(list, Lst_First(list), cmpData, cmp);
    396   1.1  rillig }
    397   1.1  rillig 
    398  1.14  rillig /* Return the first node from the given list, starting at the given node, for
    399  1.14  rillig  * which the given comparison function returns 0, or NULL if none of the nodes
    400  1.14  rillig  * matches. */
    401   1.1  rillig LstNode
    402  1.16  rillig Lst_FindFrom(Lst list, LstNode node, const void *cmpData,
    403  1.16  rillig 	     int (*cmp)(const void *, const void *))
    404   1.1  rillig {
    405  1.13  rillig     LstNode tln;
    406   1.1  rillig 
    407  1.20  rillig     if (!LstIsValid(list) || LstIsEmpty(list) || !LstNodeIsValid(node)) {
    408   1.1  rillig 	return NULL;
    409   1.1  rillig     }
    410   1.1  rillig 
    411  1.16  rillig     tln = node;
    412   1.1  rillig 
    413   1.1  rillig     do {
    414  1.16  rillig 	if ((*cmp)(tln->datum, cmpData) == 0)
    415   1.1  rillig 	    return tln;
    416  1.15  rillig 	tln = tln->next;
    417  1.16  rillig     } while (tln != node && tln != NULL);
    418   1.1  rillig 
    419   1.1  rillig     return NULL;
    420   1.1  rillig }
    421   1.1  rillig 
    422  1.14  rillig /* Return the first node that contains the given datum, or NULL. */
    423   1.1  rillig LstNode
    424  1.29  rillig Lst_MemberS(Lst list, void *datum)
    425   1.1  rillig {
    426  1.16  rillig     LstNode node;
    427   1.1  rillig 
    428  1.29  rillig     assert(LstIsValid(list));
    429  1.29  rillig     assert(datum != NULL);
    430   1.1  rillig 
    431  1.29  rillig     for (node = list->first; node != NULL; node = node->next) {
    432  1.16  rillig 	if (node->datum == datum) {
    433  1.16  rillig 	    return node;
    434   1.1  rillig 	}
    435  1.29  rillig     }
    436   1.1  rillig 
    437   1.1  rillig     return NULL;
    438   1.1  rillig }
    439   1.1  rillig 
    440  1.14  rillig /* Apply the given function to each element of the given list. The function
    441  1.14  rillig  * should return 0 if traversal should continue and non-zero if it should
    442  1.14  rillig  * abort. */
    443   1.1  rillig int
    444  1.16  rillig Lst_ForEach(Lst list, int (*proc)(void *, void *), void *procData)
    445   1.1  rillig {
    446  1.16  rillig     return Lst_ForEachFrom(list, Lst_First(list), proc, procData);
    447   1.1  rillig }
    448   1.1  rillig 
    449  1.14  rillig /* Apply the given function to each element of the given list, starting from
    450  1.14  rillig  * the given node. The function should return 0 if traversal should continue,
    451  1.14  rillig  * and non-zero if it should abort. */
    452   1.1  rillig int
    453  1.16  rillig Lst_ForEachFrom(Lst list, LstNode node,
    454  1.16  rillig 		int (*proc)(void *, void *), void *procData)
    455   1.1  rillig {
    456  1.16  rillig     LstNode tln = node;
    457  1.13  rillig     LstNode next;
    458   1.4  rillig     Boolean done;
    459   1.4  rillig     int result;
    460   1.1  rillig 
    461  1.20  rillig     if (!LstIsValid(list) || LstIsEmpty(list)) {
    462   1.1  rillig 	return 0;
    463   1.1  rillig     }
    464   1.1  rillig 
    465   1.1  rillig     do {
    466   1.1  rillig 	/*
    467   1.1  rillig 	 * Take care of having the current element deleted out from under
    468   1.1  rillig 	 * us.
    469   1.1  rillig 	 */
    470   1.1  rillig 
    471  1.15  rillig 	next = tln->next;
    472   1.1  rillig 
    473   1.1  rillig 	/*
    474   1.1  rillig 	 * We're done with the traversal if
    475  1.38  rillig 	 *  - the next node to examine doesn't exist and
    476   1.1  rillig 	 *  - nothing's been added after the current node (check this
    477   1.1  rillig 	 *    after proc() has been called).
    478   1.1  rillig 	 */
    479  1.38  rillig 	done = next == NULL;
    480   1.1  rillig 
    481  1.17  rillig 	tln->useCount++;
    482  1.16  rillig 	result = (*proc)(tln->datum, procData);
    483  1.17  rillig 	tln->useCount--;
    484   1.1  rillig 
    485   1.1  rillig 	/*
    486   1.1  rillig 	 * Now check whether a node has been added.
    487   1.1  rillig 	 * Note: this doesn't work if this node was deleted before
    488   1.1  rillig 	 *       the new node was added.
    489   1.1  rillig 	 */
    490  1.15  rillig 	if (next != tln->next) {
    491  1.15  rillig 	    next = tln->next;
    492   1.4  rillig 	    done = 0;
    493   1.1  rillig 	}
    494   1.1  rillig 
    495   1.7  rillig 	if (tln->deleted) {
    496   1.1  rillig 	    free((char *)tln);
    497   1.1  rillig 	}
    498   1.1  rillig 	tln = next;
    499   1.1  rillig     } while (!result && !LstIsEmpty(list) && !done);
    500   1.1  rillig 
    501   1.1  rillig     return result;
    502   1.1  rillig }
    503   1.1  rillig 
    504  1.34  rillig /* Move all nodes from list2 to the end of list1.
    505  1.34  rillig  * List2 is destroyed and freed. */
    506  1.34  rillig void
    507  1.34  rillig Lst_MoveAllS(Lst list1, Lst list2)
    508   1.1  rillig {
    509  1.34  rillig     assert(LstIsValid(list1));
    510  1.34  rillig     assert(LstIsValid(list2));
    511   1.1  rillig 
    512  1.34  rillig     if (list2->first != NULL) {
    513  1.34  rillig 	list2->first->prev = list1->last;
    514  1.34  rillig 	if (list1->last != NULL) {
    515  1.34  rillig 	    list1->last->next = list2->first;
    516  1.34  rillig 	} else {
    517  1.34  rillig 	    list1->first = list2->first;
    518   1.1  rillig 	}
    519  1.34  rillig 	list1->last = list2->last;
    520   1.1  rillig     }
    521  1.34  rillig     free(list2);
    522   1.1  rillig }
    523   1.1  rillig 
    524  1.22  rillig /* Copy the element data from src to the start of dst. */
    525  1.22  rillig void
    526  1.22  rillig Lst_PrependAllS(Lst dst, Lst src)
    527  1.22  rillig {
    528  1.22  rillig     LstNode node;
    529  1.22  rillig     for (node = src->last; node != NULL; node = node->prev)
    530  1.31  rillig 	Lst_PrependS(dst, node->datum);
    531  1.22  rillig }
    532  1.22  rillig 
    533  1.22  rillig /* Copy the element data from src to the end of dst. */
    534  1.22  rillig void
    535  1.22  rillig Lst_AppendAllS(Lst dst, Lst src)
    536  1.22  rillig {
    537  1.22  rillig     LstNode node;
    538  1.22  rillig     for (node = src->first; node != NULL; node = node->next)
    539  1.31  rillig 	Lst_AppendS(dst, node->datum);
    540  1.22  rillig }
    541   1.1  rillig 
    542   1.1  rillig /*
    543   1.1  rillig  * these functions are for dealing with a list as a table, of sorts.
    544   1.1  rillig  * An idea of the "current element" is kept and used by all the functions
    545   1.1  rillig  * between Lst_Open() and Lst_Close().
    546   1.1  rillig  *
    547   1.1  rillig  * The sequential functions access the list in a slightly different way.
    548   1.1  rillig  * CurPtr points to their idea of the current node in the list and they
    549   1.1  rillig  * access the list based on it.
    550   1.1  rillig  */
    551   1.1  rillig 
    552  1.14  rillig /* Open a list for sequential access. A list can still be searched, etc.,
    553  1.14  rillig  * without confusing these functions. */
    554   1.1  rillig ReturnStatus
    555  1.16  rillig Lst_Open(Lst list)
    556   1.1  rillig {
    557  1.20  rillig     if (!LstIsValid(list)) {
    558   1.4  rillig 	return FAILURE;
    559   1.4  rillig     }
    560  1.19  rillig     Lst_OpenS(list);
    561   1.4  rillig     return SUCCESS;
    562   1.1  rillig }
    563   1.1  rillig 
    564  1.10  rillig /* Open a list for sequential access. A list can still be searched, etc.,
    565  1.10  rillig  * without confusing these functions. */
    566  1.10  rillig void
    567  1.16  rillig Lst_OpenS(Lst list)
    568  1.10  rillig {
    569  1.20  rillig     assert(LstIsValid(list));
    570  1.24  rillig 
    571  1.19  rillig     /* XXX: This assertion fails for NetBSD's "build.sh -j1 tools", somewhere
    572  1.19  rillig      * between "dependall ===> compat" and "dependall ===> binstall".
    573  1.19  rillig      * Building without the "-j1" succeeds though. */
    574  1.24  rillig     if (DEBUG(LINT) && list->isOpen)
    575  1.19  rillig 	Parse_Error(PARSE_WARNING, "Internal inconsistency: list opened twice");
    576  1.10  rillig 
    577  1.16  rillig     list->isOpen = TRUE;
    578  1.16  rillig     list->lastAccess = LstIsEmpty(list) ? Head : Unknown;
    579  1.16  rillig     list->curr = NULL;
    580  1.10  rillig }
    581  1.10  rillig 
    582  1.10  rillig /* Return the next node for the given list, or NULL if the end has been
    583  1.10  rillig  * reached. */
    584   1.1  rillig LstNode
    585  1.16  rillig Lst_NextS(Lst list)
    586   1.1  rillig {
    587  1.16  rillig     LstNode node;
    588   1.1  rillig 
    589  1.20  rillig     assert(LstIsValid(list));
    590   1.9  rillig     assert(list->isOpen);
    591   1.1  rillig 
    592  1.15  rillig     list->prev = list->curr;
    593   1.1  rillig 
    594  1.15  rillig     if (list->curr == NULL) {
    595  1.15  rillig 	if (list->lastAccess == Unknown) {
    596   1.1  rillig 	    /*
    597  1.15  rillig 	     * If we're just starting out, lastAccess will be Unknown.
    598   1.1  rillig 	     * Then we want to start this thing off in the right
    599  1.15  rillig 	     * direction -- at the start with lastAccess being Middle.
    600   1.1  rillig 	     */
    601  1.16  rillig 	    list->curr = node = list->first;
    602  1.15  rillig 	    list->lastAccess = Middle;
    603   1.1  rillig 	} else {
    604  1.16  rillig 	    node = NULL;
    605  1.15  rillig 	    list->lastAccess = Tail;
    606   1.1  rillig 	}
    607   1.1  rillig     } else {
    608  1.16  rillig 	node = list->curr->next;
    609  1.16  rillig 	list->curr = node;
    610   1.1  rillig 
    611  1.16  rillig 	if (node == list->first || node == NULL) {
    612   1.1  rillig 	    /*
    613   1.1  rillig 	     * If back at the front, then we've hit the end...
    614   1.1  rillig 	     */
    615  1.15  rillig 	    list->lastAccess = Tail;
    616   1.1  rillig 	} else {
    617   1.1  rillig 	    /*
    618   1.1  rillig 	     * Reset to Middle if gone past first.
    619   1.1  rillig 	     */
    620  1.15  rillig 	    list->lastAccess = Middle;
    621   1.1  rillig 	}
    622   1.1  rillig     }
    623   1.1  rillig 
    624  1.16  rillig     return node;
    625   1.1  rillig }
    626   1.1  rillig 
    627  1.10  rillig /* Close a list which was opened for sequential access. */
    628   1.1  rillig void
    629  1.16  rillig Lst_CloseS(Lst list)
    630   1.1  rillig {
    631  1.20  rillig     assert(LstIsValid(list));
    632  1.16  rillig     assert(list->isOpen);
    633   1.1  rillig 
    634  1.10  rillig     list->isOpen = FALSE;
    635  1.15  rillig     list->lastAccess = Unknown;
    636   1.1  rillig }
    637   1.1  rillig 
    638   1.1  rillig 
    639   1.1  rillig /*
    640   1.1  rillig  * for using the list as a queue
    641   1.1  rillig  */
    642   1.1  rillig 
    643  1.14  rillig /* Add the datum to the tail of the given list. */
    644  1.25  rillig void
    645  1.25  rillig Lst_EnqueueS(Lst list, void *datum)
    646   1.1  rillig {
    647  1.25  rillig     Lst_AppendS(list, datum);
    648   1.1  rillig }
    649   1.1  rillig 
    650  1.25  rillig /* Remove and return the datum at the head of the given list. */
    651   1.1  rillig void *
    652  1.25  rillig Lst_DequeueS(Lst list)
    653   1.1  rillig {
    654  1.16  rillig     void *datum;
    655   1.1  rillig 
    656  1.25  rillig     assert(LstIsValid(list));
    657  1.25  rillig     assert(!LstIsEmpty(list));
    658   1.1  rillig 
    659  1.25  rillig     datum = list->first->datum;
    660  1.25  rillig     Lst_RemoveS(list, list->first);
    661  1.25  rillig     assert(datum != NULL);
    662  1.16  rillig     return datum;
    663   1.1  rillig }
    664