Home | History | Annotate | Line # | Download | only in make
lst.c revision 1.28
      1  1.28  rillig /* $NetBSD: lst.c,v 1.28 2020/08/22 15:17:09 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.8  rillig #include <assert.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.28  rillig static char rcsid[] = "$NetBSD: lst.c,v 1.28 2020/08/22 15:17:09 rillig Exp $";
     41   1.1  rillig #else
     42   1.1  rillig #include <sys/cdefs.h>
     43   1.1  rillig #ifndef lint
     44  1.28  rillig __RCSID("$NetBSD: lst.c,v 1.28 2020/08/22 15:17:09 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.23  rillig static ReturnStatus Lst_AtEnd(Lst, void *);
     75  1.23  rillig 
     76   1.2  rillig static Boolean
     77  1.20  rillig LstIsValid(Lst list)
     78   1.2  rillig {
     79  1.16  rillig     return list != NULL;
     80   1.2  rillig }
     81   1.1  rillig 
     82   1.2  rillig static Boolean
     83  1.20  rillig LstNodeIsValid(LstNode node)
     84   1.2  rillig {
     85  1.16  rillig     return node != NULL;
     86   1.2  rillig }
     87   1.1  rillig 
     88  1.22  rillig /* Allocate and initialize a list node.
     89  1.22  rillig  *
     90  1.22  rillig  * The fields 'prev' and 'next' must be initialized by the caller.
     91  1.22  rillig  */
     92  1.12  rillig static LstNode
     93  1.12  rillig LstNodeNew(void *datum)
     94  1.12  rillig {
     95  1.16  rillig     LstNode node = bmake_malloc(sizeof *node);
     96  1.16  rillig     node->useCount = 0;
     97  1.16  rillig     node->deleted = FALSE;
     98  1.16  rillig     node->datum = datum;
     99  1.16  rillig     return node;
    100  1.12  rillig }
    101  1.12  rillig 
    102   1.2  rillig static Boolean
    103  1.16  rillig LstIsEmpty(Lst list)
    104   1.2  rillig {
    105  1.16  rillig     return list->first == NULL;
    106   1.2  rillig }
    107   1.1  rillig 
    108   1.5  rillig /* Create and initialize a new, empty list. */
    109   1.1  rillig Lst
    110   1.5  rillig Lst_Init(void)
    111   1.1  rillig {
    112  1.16  rillig     Lst list = bmake_malloc(sizeof *list);
    113   1.1  rillig 
    114  1.16  rillig     list->first = NULL;
    115  1.16  rillig     list->last = NULL;
    116  1.16  rillig     list->isOpen = FALSE;
    117  1.16  rillig     list->lastAccess = Unknown;
    118   1.1  rillig 
    119  1.16  rillig     return list;
    120   1.1  rillig }
    121   1.1  rillig 
    122  1.14  rillig /* Duplicate an entire list, usually by copying the datum pointers.
    123  1.14  rillig  * If copyProc is given, that function is used to create the new datum from the
    124  1.14  rillig  * old datum, usually by creating a copy of it.
    125  1.14  rillig  * Return the new list, or NULL on failure. */
    126   1.1  rillig Lst
    127  1.16  rillig Lst_Duplicate(Lst list, DuplicateProc *copyProc)
    128   1.1  rillig {
    129  1.16  rillig     Lst newList;
    130  1.16  rillig     LstNode node;
    131   1.1  rillig 
    132  1.20  rillig     if (!LstIsValid(list)) {
    133   1.1  rillig 	return NULL;
    134   1.1  rillig     }
    135   1.1  rillig 
    136  1.16  rillig     newList = Lst_Init();
    137   1.1  rillig 
    138  1.16  rillig     node = list->first;
    139  1.16  rillig     while (node != NULL) {
    140   1.1  rillig 	if (copyProc != NULL) {
    141  1.16  rillig 	    if (Lst_AtEnd(newList, copyProc(node->datum)) == FAILURE) {
    142   1.1  rillig 		return NULL;
    143   1.1  rillig 	    }
    144  1.16  rillig 	} else if (Lst_AtEnd(newList, node->datum) == FAILURE) {
    145   1.1  rillig 	    return NULL;
    146   1.1  rillig 	}
    147   1.1  rillig 
    148  1.16  rillig 	node = node->next;
    149   1.1  rillig     }
    150   1.1  rillig 
    151  1.16  rillig     return newList;
    152   1.1  rillig }
    153   1.1  rillig 
    154  1.14  rillig /* Destroy a list and free all its resources. If the freeProc is given, it is
    155  1.14  rillig  * called with the datum from each node in turn before the node is freed. */
    156   1.1  rillig void
    157   1.1  rillig Lst_Destroy(Lst list, FreeProc *freeProc)
    158   1.1  rillig {
    159  1.16  rillig     LstNode node;
    160  1.16  rillig     LstNode next = NULL;
    161   1.1  rillig 
    162   1.1  rillig     if (list == NULL)
    163   1.1  rillig 	return;
    164   1.1  rillig 
    165   1.1  rillig     /* To ease scanning */
    166  1.15  rillig     if (list->last != NULL)
    167  1.15  rillig 	list->last->next = NULL;
    168   1.1  rillig     else {
    169   1.1  rillig 	free(list);
    170   1.1  rillig 	return;
    171   1.1  rillig     }
    172   1.1  rillig 
    173   1.1  rillig     if (freeProc) {
    174  1.16  rillig 	for (node = list->first; node != NULL; node = next) {
    175  1.16  rillig 	    next = node->next;
    176  1.16  rillig 	    freeProc(node->datum);
    177  1.16  rillig 	    free(node);
    178   1.1  rillig 	}
    179   1.1  rillig     } else {
    180  1.16  rillig 	for (node = list->first; node != NULL; node = next) {
    181  1.16  rillig 	    next = node->next;
    182  1.16  rillig 	    free(node);
    183   1.1  rillig 	}
    184   1.1  rillig     }
    185   1.1  rillig 
    186   1.1  rillig     free(list);
    187   1.1  rillig }
    188   1.1  rillig 
    189   1.1  rillig /*
    190   1.1  rillig  * Functions to modify a list
    191   1.1  rillig  */
    192   1.1  rillig 
    193  1.14  rillig /* Insert a new node with the given piece of data before the given node in the
    194  1.14  rillig  * given list. */
    195  1.26  rillig static ReturnStatus
    196  1.27  rillig LstInsertBefore(Lst list, LstNode node, void *datum)
    197   1.1  rillig {
    198  1.16  rillig     LstNode newNode;
    199   1.1  rillig 
    200   1.1  rillig     /*
    201   1.1  rillig      * check validity of arguments
    202   1.1  rillig      */
    203  1.20  rillig     if (LstIsValid(list) && (LstIsEmpty(list) && node == NULL))
    204   1.1  rillig 	goto ok;
    205   1.1  rillig 
    206  1.20  rillig     if (!LstIsValid(list) || LstIsEmpty(list) || !LstNodeIsValid(node)) {
    207   1.1  rillig 	return FAILURE;
    208   1.1  rillig     }
    209   1.1  rillig 
    210   1.1  rillig     ok:
    211  1.16  rillig     newNode = LstNodeNew(datum);
    212   1.1  rillig 
    213  1.16  rillig     if (node == NULL) {
    214  1.16  rillig 	newNode->prev = newNode->next = NULL;
    215  1.16  rillig 	list->first = list->last = newNode;
    216   1.1  rillig     } else {
    217  1.16  rillig 	newNode->prev = node->prev;
    218  1.16  rillig 	newNode->next = node;
    219   1.1  rillig 
    220  1.16  rillig 	if (newNode->prev != NULL) {
    221  1.16  rillig 	    newNode->prev->next = newNode;
    222   1.1  rillig 	}
    223  1.16  rillig 	node->prev = newNode;
    224   1.1  rillig 
    225  1.16  rillig 	if (node == list->first) {
    226  1.16  rillig 	    list->first = newNode;
    227   1.1  rillig 	}
    228   1.1  rillig     }
    229   1.1  rillig 
    230   1.1  rillig     return SUCCESS;
    231   1.1  rillig }
    232   1.1  rillig 
    233  1.26  rillig /* Insert a new node with the given piece of data before the given node in the
    234  1.26  rillig  * given list. */
    235  1.26  rillig void
    236  1.26  rillig Lst_InsertBeforeS(Lst list, LstNode node, void *datum)
    237  1.26  rillig {
    238  1.26  rillig     LstNode newNode;
    239  1.26  rillig 
    240  1.26  rillig     assert(LstIsValid(list));
    241  1.26  rillig     assert(!LstIsEmpty(list));
    242  1.26  rillig     assert(LstNodeIsValid(node));
    243  1.26  rillig     assert(datum != NULL);
    244  1.26  rillig 
    245  1.26  rillig     newNode = LstNodeNew(datum);
    246  1.26  rillig     newNode->prev = node->prev;
    247  1.26  rillig     newNode->next = node;
    248  1.26  rillig 
    249  1.26  rillig     if (node->prev != NULL) {
    250  1.26  rillig 	node->prev->next = newNode;
    251  1.26  rillig     }
    252  1.26  rillig     node->prev = newNode;
    253  1.26  rillig 
    254  1.26  rillig     if (node == list->first) {
    255  1.26  rillig 	list->first = newNode;
    256  1.26  rillig     }
    257  1.26  rillig }
    258  1.26  rillig 
    259  1.14  rillig /* Insert a new node with the given piece of data after the given node in the
    260  1.14  rillig  * given list. */
    261  1.27  rillig static ReturnStatus
    262  1.27  rillig LstInsertAfter(Lst list, LstNode node, void *datum)
    263   1.1  rillig {
    264  1.17  rillig     LstNode newNode;
    265   1.1  rillig 
    266  1.20  rillig     if (LstIsValid(list) && (node == NULL && LstIsEmpty(list))) {
    267   1.1  rillig 	goto ok;
    268   1.1  rillig     }
    269   1.1  rillig 
    270  1.20  rillig     if (!LstIsValid(list) || LstIsEmpty(list) || !LstNodeIsValid(node)) {
    271   1.1  rillig 	return FAILURE;
    272   1.1  rillig     }
    273   1.1  rillig     ok:
    274   1.1  rillig 
    275  1.17  rillig     newNode = LstNodeNew(datum);
    276   1.1  rillig 
    277  1.16  rillig     if (node == NULL) {
    278  1.17  rillig 	newNode->next = newNode->prev = NULL;
    279  1.17  rillig 	list->first = list->last = newNode;
    280   1.1  rillig     } else {
    281  1.17  rillig 	newNode->prev = node;
    282  1.17  rillig 	newNode->next = node->next;
    283   1.1  rillig 
    284  1.17  rillig 	node->next = newNode;
    285  1.17  rillig 	if (newNode->next != NULL) {
    286  1.17  rillig 	    newNode->next->prev = newNode;
    287   1.1  rillig 	}
    288   1.1  rillig 
    289  1.16  rillig 	if (node == list->last) {
    290  1.17  rillig 	    list->last = newNode;
    291   1.1  rillig 	}
    292   1.1  rillig     }
    293   1.1  rillig 
    294   1.1  rillig     return SUCCESS;
    295   1.1  rillig }
    296   1.1  rillig 
    297  1.14  rillig /* Add a piece of data at the front of the given list. */
    298   1.1  rillig ReturnStatus
    299  1.16  rillig Lst_AtFront(Lst list, void *datum)
    300   1.1  rillig {
    301  1.16  rillig     LstNode front = Lst_First(list);
    302  1.27  rillig     return LstInsertBefore(list, front, datum);
    303   1.1  rillig }
    304   1.1  rillig 
    305  1.14  rillig /* Add a piece of data at the end of the given list. */
    306   1.1  rillig ReturnStatus
    307  1.16  rillig Lst_AtEnd(Lst list, void *datum)
    308   1.1  rillig {
    309  1.16  rillig     LstNode end = Lst_Last(list);
    310  1.27  rillig     return LstInsertAfter(list, end, datum);
    311   1.1  rillig }
    312   1.1  rillig 
    313  1.22  rillig /* Add a piece of data at the start of the given list. */
    314  1.22  rillig void
    315  1.22  rillig Lst_PrependS(Lst list, void *datum)
    316  1.22  rillig {
    317  1.22  rillig     LstNode node;
    318  1.22  rillig 
    319  1.22  rillig     assert(LstIsValid(list));
    320  1.22  rillig     assert(datum != NULL);
    321  1.22  rillig 
    322  1.22  rillig     node = LstNodeNew(datum);
    323  1.22  rillig     node->prev = NULL;
    324  1.22  rillig     node->next = list->first;
    325  1.22  rillig 
    326  1.22  rillig     if (list->first == NULL) {
    327  1.22  rillig 	list->first = node;
    328  1.22  rillig 	list->last = node;
    329  1.22  rillig     } else {
    330  1.22  rillig 	list->first->prev = node;
    331  1.22  rillig 	list->first = node;
    332  1.22  rillig     }
    333  1.22  rillig }
    334  1.22  rillig 
    335  1.21  rillig /* Add a piece of data at the end of the given list. */
    336  1.21  rillig void
    337  1.21  rillig Lst_AppendS(Lst list, void *datum)
    338  1.21  rillig {
    339  1.21  rillig     LstNode node;
    340  1.21  rillig 
    341  1.21  rillig     assert(LstIsValid(list));
    342  1.21  rillig     assert(datum != NULL);
    343  1.21  rillig 
    344  1.21  rillig     node = LstNodeNew(datum);
    345  1.21  rillig     node->prev = list->last;
    346  1.21  rillig     node->next = NULL;
    347  1.21  rillig 
    348  1.21  rillig     if (list->last == NULL) {
    349  1.21  rillig 	list->first = node;
    350  1.21  rillig 	list->last = node;
    351  1.21  rillig     } else {
    352  1.21  rillig 	list->last->next = node;
    353  1.21  rillig 	list->last = node;
    354  1.21  rillig     }
    355  1.21  rillig }
    356  1.21  rillig 
    357   1.8  rillig /* Remove the given node from the given list.
    358   1.8  rillig  * The datum stored in the node must be freed by the caller, if necessary. */
    359   1.8  rillig void
    360  1.16  rillig Lst_RemoveS(Lst list, LstNode node)
    361   1.1  rillig {
    362  1.20  rillig     assert(LstIsValid(list));
    363  1.20  rillig     assert(LstNodeIsValid(node));
    364   1.1  rillig 
    365   1.1  rillig     /*
    366   1.1  rillig      * unlink it from the list
    367   1.1  rillig      */
    368  1.16  rillig     if (node->next != NULL) {
    369  1.16  rillig 	node->next->prev = node->prev;
    370   1.1  rillig     }
    371  1.16  rillig     if (node->prev != NULL) {
    372  1.16  rillig 	node->prev->next = node->next;
    373   1.1  rillig     }
    374   1.1  rillig 
    375   1.1  rillig     /*
    376  1.15  rillig      * if either the first or last of the list point to this node,
    377   1.1  rillig      * adjust them accordingly
    378   1.1  rillig      */
    379  1.16  rillig     if (list->first == node) {
    380  1.16  rillig 	list->first = node->next;
    381   1.1  rillig     }
    382  1.16  rillig     if (list->last == node) {
    383  1.16  rillig 	list->last = node->prev;
    384   1.1  rillig     }
    385   1.1  rillig 
    386   1.1  rillig     /*
    387   1.1  rillig      * Sequential access stuff. If the node we're removing is the current
    388   1.1  rillig      * node in the list, reset the current node to the previous one. If the
    389  1.15  rillig      * previous one was non-existent (prev == NULL), we set the
    390   1.1  rillig      * end to be Unknown, since it is.
    391   1.1  rillig      */
    392  1.16  rillig     if (list->isOpen && list->curr == node) {
    393  1.15  rillig 	list->curr = list->prev;
    394  1.15  rillig 	if (list->curr == NULL) {
    395  1.15  rillig 	    list->lastAccess = Unknown;
    396   1.1  rillig 	}
    397   1.1  rillig     }
    398   1.1  rillig 
    399   1.1  rillig     /*
    400   1.1  rillig      * note that the datum is unmolested. The caller must free it as
    401   1.1  rillig      * necessary and as expected.
    402   1.1  rillig      */
    403  1.16  rillig     if (node->useCount == 0) {
    404  1.16  rillig 	free(node);
    405   1.1  rillig     } else {
    406  1.16  rillig 	node->deleted = TRUE;
    407   1.1  rillig     }
    408   1.1  rillig }
    409   1.1  rillig 
    410   1.8  rillig /* Replace the datum in the given node with the new datum. */
    411   1.8  rillig void
    412  1.16  rillig Lst_ReplaceS(LstNode node, void *datum)
    413   1.1  rillig {
    414  1.16  rillig     node->datum = datum;
    415   1.1  rillig }
    416   1.1  rillig 
    417   1.1  rillig 
    418   1.1  rillig /*
    419   1.1  rillig  * Node-specific functions
    420   1.1  rillig  */
    421   1.1  rillig 
    422  1.14  rillig /* Return the first node from the given list, or NULL if the list is empty or
    423  1.14  rillig  * invalid. */
    424   1.1  rillig LstNode
    425  1.16  rillig Lst_First(Lst list)
    426   1.1  rillig {
    427  1.20  rillig     if (!LstIsValid(list) || LstIsEmpty(list)) {
    428   1.1  rillig 	return NULL;
    429   1.1  rillig     } else {
    430  1.16  rillig 	return list->first;
    431   1.1  rillig     }
    432   1.1  rillig }
    433   1.1  rillig 
    434  1.14  rillig /* Return the last node from the given list, or NULL if the list is empty or
    435  1.14  rillig  * invalid. */
    436   1.1  rillig LstNode
    437  1.16  rillig Lst_Last(Lst list)
    438   1.1  rillig {
    439  1.20  rillig     if (!LstIsValid(list) || LstIsEmpty(list)) {
    440   1.1  rillig 	return NULL;
    441   1.1  rillig     } else {
    442  1.16  rillig 	return list->last;
    443   1.1  rillig     }
    444   1.1  rillig }
    445   1.1  rillig 
    446   1.6  rillig /* Return the successor to the given node on its list, or NULL. */
    447   1.1  rillig LstNode
    448  1.16  rillig Lst_Succ(LstNode node)
    449   1.1  rillig {
    450  1.16  rillig     if (node == NULL) {
    451   1.1  rillig 	return NULL;
    452   1.1  rillig     } else {
    453  1.16  rillig 	return node->next;
    454   1.1  rillig     }
    455   1.1  rillig }
    456   1.1  rillig 
    457   1.6  rillig /* Return the predecessor to the given node on its list, or NULL. */
    458   1.1  rillig LstNode
    459  1.16  rillig Lst_Prev(LstNode node)
    460   1.1  rillig {
    461  1.16  rillig     if (node == NULL) {
    462   1.1  rillig 	return NULL;
    463   1.1  rillig     } else {
    464  1.16  rillig 	return node->prev;
    465   1.1  rillig     }
    466   1.1  rillig }
    467   1.1  rillig 
    468  1.28  rillig /* Return the datum stored in the given node. */
    469   1.1  rillig void *
    470  1.28  rillig Lst_DatumS(LstNode node)
    471   1.1  rillig {
    472  1.28  rillig     assert(LstNodeIsValid(node));
    473  1.28  rillig     return node->datum;
    474   1.1  rillig }
    475   1.1  rillig 
    476   1.1  rillig 
    477   1.1  rillig /*
    478   1.1  rillig  * Functions for entire lists
    479   1.1  rillig  */
    480   1.1  rillig 
    481  1.14  rillig /* Return TRUE if the given list is empty or invalid. */
    482   1.1  rillig Boolean
    483  1.16  rillig Lst_IsEmpty(Lst list)
    484   1.1  rillig {
    485  1.20  rillig     return !LstIsValid(list) || LstIsEmpty(list);
    486   1.1  rillig }
    487   1.1  rillig 
    488  1.14  rillig /* Return the first node from the given list for which the given comparison
    489  1.14  rillig  * function returns 0, or NULL if none of the nodes matches. */
    490   1.1  rillig LstNode
    491  1.16  rillig Lst_Find(Lst list, const void *cmpData, int (*cmp)(const void *, const void *))
    492   1.1  rillig {
    493  1.16  rillig     return Lst_FindFrom(list, Lst_First(list), cmpData, cmp);
    494   1.1  rillig }
    495   1.1  rillig 
    496  1.14  rillig /* Return the first node from the given list, starting at the given node, for
    497  1.14  rillig  * which the given comparison function returns 0, or NULL if none of the nodes
    498  1.14  rillig  * matches. */
    499   1.1  rillig LstNode
    500  1.16  rillig Lst_FindFrom(Lst list, LstNode node, const void *cmpData,
    501  1.16  rillig 	     int (*cmp)(const void *, const void *))
    502   1.1  rillig {
    503  1.13  rillig     LstNode tln;
    504   1.1  rillig 
    505  1.20  rillig     if (!LstIsValid(list) || LstIsEmpty(list) || !LstNodeIsValid(node)) {
    506   1.1  rillig 	return NULL;
    507   1.1  rillig     }
    508   1.1  rillig 
    509  1.16  rillig     tln = node;
    510   1.1  rillig 
    511   1.1  rillig     do {
    512  1.16  rillig 	if ((*cmp)(tln->datum, cmpData) == 0)
    513   1.1  rillig 	    return tln;
    514  1.15  rillig 	tln = tln->next;
    515  1.16  rillig     } while (tln != node && tln != NULL);
    516   1.1  rillig 
    517   1.1  rillig     return NULL;
    518   1.1  rillig }
    519   1.1  rillig 
    520  1.14  rillig /* Return the first node that contains the given datum, or NULL. */
    521   1.1  rillig LstNode
    522  1.16  rillig Lst_Member(Lst list, void *datum)
    523   1.1  rillig {
    524  1.16  rillig     LstNode node;
    525   1.1  rillig 
    526   1.1  rillig     if (list == NULL) {
    527   1.1  rillig 	return NULL;
    528   1.1  rillig     }
    529  1.16  rillig     node = list->first;
    530  1.16  rillig     if (node == NULL) {
    531   1.1  rillig 	return NULL;
    532   1.1  rillig     }
    533   1.1  rillig 
    534   1.1  rillig     do {
    535  1.16  rillig 	if (node->datum == datum) {
    536  1.16  rillig 	    return node;
    537   1.1  rillig 	}
    538  1.16  rillig 	node = node->next;
    539  1.16  rillig     } while (node != NULL && node != list->first);
    540   1.1  rillig 
    541   1.1  rillig     return NULL;
    542   1.1  rillig }
    543   1.1  rillig 
    544  1.14  rillig /* Apply the given function to each element of the given list. The function
    545  1.14  rillig  * should return 0 if traversal should continue and non-zero if it should
    546  1.14  rillig  * abort. */
    547   1.1  rillig int
    548  1.16  rillig Lst_ForEach(Lst list, int (*proc)(void *, void *), void *procData)
    549   1.1  rillig {
    550  1.16  rillig     return Lst_ForEachFrom(list, Lst_First(list), proc, procData);
    551   1.1  rillig }
    552   1.1  rillig 
    553  1.14  rillig /* Apply the given function to each element of the given list, starting from
    554  1.14  rillig  * the given node. The function should return 0 if traversal should continue,
    555  1.14  rillig  * and non-zero if it should abort. */
    556   1.1  rillig int
    557  1.16  rillig Lst_ForEachFrom(Lst list, LstNode node,
    558  1.16  rillig 		int (*proc)(void *, void *), void *procData)
    559   1.1  rillig {
    560  1.16  rillig     LstNode tln = node;
    561  1.13  rillig     LstNode next;
    562   1.4  rillig     Boolean done;
    563   1.4  rillig     int result;
    564   1.1  rillig 
    565  1.20  rillig     if (!LstIsValid(list) || LstIsEmpty(list)) {
    566   1.1  rillig 	return 0;
    567   1.1  rillig     }
    568   1.1  rillig 
    569   1.1  rillig     do {
    570   1.1  rillig 	/*
    571   1.1  rillig 	 * Take care of having the current element deleted out from under
    572   1.1  rillig 	 * us.
    573   1.1  rillig 	 */
    574   1.1  rillig 
    575  1.15  rillig 	next = tln->next;
    576   1.1  rillig 
    577   1.1  rillig 	/*
    578   1.1  rillig 	 * We're done with the traversal if
    579   1.1  rillig 	 *  - the next node to examine is the first in the queue or
    580   1.1  rillig 	 *    doesn't exist and
    581   1.1  rillig 	 *  - nothing's been added after the current node (check this
    582   1.1  rillig 	 *    after proc() has been called).
    583   1.1  rillig 	 */
    584  1.15  rillig 	done = (next == NULL || next == list->first);
    585   1.1  rillig 
    586  1.17  rillig 	tln->useCount++;
    587  1.16  rillig 	result = (*proc)(tln->datum, procData);
    588  1.17  rillig 	tln->useCount--;
    589   1.1  rillig 
    590   1.1  rillig 	/*
    591   1.1  rillig 	 * Now check whether a node has been added.
    592   1.1  rillig 	 * Note: this doesn't work if this node was deleted before
    593   1.1  rillig 	 *       the new node was added.
    594   1.1  rillig 	 */
    595  1.15  rillig 	if (next != tln->next) {
    596  1.15  rillig 	    next = tln->next;
    597   1.4  rillig 	    done = 0;
    598   1.1  rillig 	}
    599   1.1  rillig 
    600   1.7  rillig 	if (tln->deleted) {
    601   1.1  rillig 	    free((char *)tln);
    602   1.1  rillig 	}
    603   1.1  rillig 	tln = next;
    604   1.1  rillig     } while (!result && !LstIsEmpty(list) && !done);
    605   1.1  rillig 
    606   1.1  rillig     return result;
    607   1.1  rillig }
    608   1.1  rillig 
    609  1.14  rillig /* Concatenate two lists. New nodes are created to hold the data elements,
    610  1.14  rillig  * if specified, but the data themselves are not copied. If the data
    611  1.14  rillig  * should be duplicated to avoid confusion with another list, the Lst_Duplicate
    612  1.14  rillig  * function should be called first. If LST_CONCLINK is specified, the second
    613  1.14  rillig  * list is destroyed since its pointers have been corrupted and the list is no
    614  1.14  rillig  * longer usable.
    615   1.1  rillig  *
    616   1.1  rillig  * Input:
    617  1.16  rillig  *	list1		The list to which list2 is to be appended
    618  1.16  rillig  *	list2		The list to append to list1
    619  1.14  rillig  *	flags		LST_CONCNEW if the list nodes should be duplicated
    620  1.14  rillig  *			LST_CONCLINK if the list nodes should just be relinked
    621   1.1  rillig  */
    622   1.1  rillig ReturnStatus
    623  1.16  rillig Lst_Concat(Lst list1, Lst list2, int flags)
    624   1.1  rillig {
    625  1.16  rillig     LstNode node;	/* original node */
    626  1.16  rillig     LstNode newNode;
    627  1.16  rillig     LstNode last;	/* the last element in the list.
    628  1.16  rillig 			 * Keeps bookkeeping until the end */
    629   1.1  rillig 
    630  1.20  rillig     if (!LstIsValid(list1) || !LstIsValid(list2)) {
    631   1.1  rillig 	return FAILURE;
    632   1.1  rillig     }
    633   1.1  rillig 
    634   1.1  rillig     if (flags == LST_CONCLINK) {
    635  1.15  rillig 	if (list2->first != NULL) {
    636   1.1  rillig 	    /*
    637   1.1  rillig 	     * So long as the second list isn't empty, we just link the
    638   1.1  rillig 	     * first element of the second list to the last element of the
    639   1.1  rillig 	     * first list. If the first list isn't empty, we then link the
    640   1.1  rillig 	     * last element of the list to the first element of the second list
    641   1.1  rillig 	     * The last element of the second list, if it exists, then becomes
    642   1.1  rillig 	     * the last element of the first list.
    643   1.1  rillig 	     */
    644  1.15  rillig 	    list2->first->prev = list1->last;
    645  1.15  rillig 	    if (list1->last != NULL) {
    646  1.15  rillig 		list1->last->next = list2->first;
    647   1.1  rillig 	    } else {
    648  1.15  rillig 		list1->first = list2->first;
    649   1.1  rillig 	    }
    650  1.15  rillig 	    list1->last = list2->last;
    651   1.1  rillig 	}
    652  1.16  rillig 	free(list2);
    653  1.15  rillig     } else if (list2->first != NULL) {
    654   1.1  rillig 	/*
    655  1.15  rillig 	 * We set the 'next' of the last element of list 2 to be nil to make
    656   1.1  rillig 	 * the loop less difficult. The loop simply goes through the entire
    657  1.15  rillig 	 * second list creating new LstNodes and filling in the 'next', and
    658  1.16  rillig 	 * 'prev' to fit into list1 and its datum field from the
    659  1.16  rillig 	 * datum field of the corresponding element in list2. The 'last' node
    660  1.16  rillig 	 * follows the last of the new nodes along until the entire list2 has
    661   1.1  rillig 	 * been appended. Only then does the bookkeeping catch up with the
    662   1.1  rillig 	 * changes. During the first iteration of the loop, if 'last' is nil,
    663   1.1  rillig 	 * the first list must have been empty so the newly-created node is
    664   1.1  rillig 	 * made the first node of the list.
    665   1.1  rillig 	 */
    666  1.15  rillig 	list2->last->next = NULL;
    667  1.16  rillig 	for (last = list1->last, node = list2->first;
    668  1.16  rillig 	     node != NULL;
    669  1.16  rillig 	     node = node->next)
    670  1.16  rillig 	{
    671  1.16  rillig 	    newNode = LstNodeNew(node->datum);
    672   1.1  rillig 	    if (last != NULL) {
    673  1.16  rillig 		last->next = newNode;
    674   1.1  rillig 	    } else {
    675  1.16  rillig 		list1->first = newNode;
    676   1.1  rillig 	    }
    677  1.16  rillig 	    newNode->prev = last;
    678  1.16  rillig 	    last = newNode;
    679   1.1  rillig 	}
    680   1.1  rillig 
    681   1.1  rillig 	/*
    682   1.1  rillig 	 * Finish bookkeeping. The last new element becomes the last element
    683   1.1  rillig 	 * of list one.
    684   1.1  rillig 	 */
    685  1.15  rillig 	list1->last = last;
    686  1.15  rillig 	last->next = NULL;
    687   1.1  rillig     }
    688   1.1  rillig 
    689   1.1  rillig     return SUCCESS;
    690   1.1  rillig }
    691   1.1  rillig 
    692  1.22  rillig /* Copy the element data from src to the start of dst. */
    693  1.22  rillig void
    694  1.22  rillig Lst_PrependAllS(Lst dst, Lst src)
    695  1.22  rillig {
    696  1.22  rillig     LstNode node;
    697  1.22  rillig     for (node = src->last; node != NULL; node = node->prev)
    698  1.22  rillig         Lst_PrependS(dst, node->datum);
    699  1.22  rillig }
    700  1.22  rillig 
    701  1.22  rillig /* Copy the element data from src to the end of dst. */
    702  1.22  rillig void
    703  1.22  rillig Lst_AppendAllS(Lst dst, Lst src)
    704  1.22  rillig {
    705  1.22  rillig     LstNode node;
    706  1.22  rillig     for (node = src->first; node != NULL; node = node->next)
    707  1.22  rillig         Lst_AppendS(dst, node->datum);
    708  1.22  rillig }
    709   1.1  rillig 
    710   1.1  rillig /*
    711   1.1  rillig  * these functions are for dealing with a list as a table, of sorts.
    712   1.1  rillig  * An idea of the "current element" is kept and used by all the functions
    713   1.1  rillig  * between Lst_Open() and Lst_Close().
    714   1.1  rillig  *
    715   1.1  rillig  * The sequential functions access the list in a slightly different way.
    716   1.1  rillig  * CurPtr points to their idea of the current node in the list and they
    717   1.1  rillig  * access the list based on it.
    718   1.1  rillig  */
    719   1.1  rillig 
    720  1.14  rillig /* Open a list for sequential access. A list can still be searched, etc.,
    721  1.14  rillig  * without confusing these functions. */
    722   1.1  rillig ReturnStatus
    723  1.16  rillig Lst_Open(Lst list)
    724   1.1  rillig {
    725  1.20  rillig     if (!LstIsValid(list)) {
    726   1.4  rillig 	return FAILURE;
    727   1.4  rillig     }
    728  1.19  rillig     Lst_OpenS(list);
    729   1.4  rillig     return SUCCESS;
    730   1.1  rillig }
    731   1.1  rillig 
    732  1.10  rillig /* Open a list for sequential access. A list can still be searched, etc.,
    733  1.10  rillig  * without confusing these functions. */
    734  1.10  rillig void
    735  1.16  rillig Lst_OpenS(Lst list)
    736  1.10  rillig {
    737  1.20  rillig     assert(LstIsValid(list));
    738  1.24  rillig 
    739  1.19  rillig     /* XXX: This assertion fails for NetBSD's "build.sh -j1 tools", somewhere
    740  1.19  rillig      * between "dependall ===> compat" and "dependall ===> binstall".
    741  1.19  rillig      * Building without the "-j1" succeeds though. */
    742  1.24  rillig     if (DEBUG(LINT) && list->isOpen)
    743  1.19  rillig 	Parse_Error(PARSE_WARNING, "Internal inconsistency: list opened twice");
    744  1.10  rillig 
    745  1.16  rillig     list->isOpen = TRUE;
    746  1.16  rillig     list->lastAccess = LstIsEmpty(list) ? Head : Unknown;
    747  1.16  rillig     list->curr = NULL;
    748  1.10  rillig }
    749  1.10  rillig 
    750  1.10  rillig /* Return the next node for the given list, or NULL if the end has been
    751  1.10  rillig  * reached. */
    752   1.1  rillig LstNode
    753  1.16  rillig Lst_NextS(Lst list)
    754   1.1  rillig {
    755  1.16  rillig     LstNode node;
    756   1.1  rillig 
    757  1.20  rillig     assert(LstIsValid(list));
    758   1.9  rillig     assert(list->isOpen);
    759   1.1  rillig 
    760  1.15  rillig     list->prev = list->curr;
    761   1.1  rillig 
    762  1.15  rillig     if (list->curr == NULL) {
    763  1.15  rillig 	if (list->lastAccess == Unknown) {
    764   1.1  rillig 	    /*
    765  1.15  rillig 	     * If we're just starting out, lastAccess will be Unknown.
    766   1.1  rillig 	     * Then we want to start this thing off in the right
    767  1.15  rillig 	     * direction -- at the start with lastAccess being Middle.
    768   1.1  rillig 	     */
    769  1.16  rillig 	    list->curr = node = list->first;
    770  1.15  rillig 	    list->lastAccess = Middle;
    771   1.1  rillig 	} else {
    772  1.16  rillig 	    node = NULL;
    773  1.15  rillig 	    list->lastAccess = Tail;
    774   1.1  rillig 	}
    775   1.1  rillig     } else {
    776  1.16  rillig 	node = list->curr->next;
    777  1.16  rillig 	list->curr = node;
    778   1.1  rillig 
    779  1.16  rillig 	if (node == list->first || node == NULL) {
    780   1.1  rillig 	    /*
    781   1.1  rillig 	     * If back at the front, then we've hit the end...
    782   1.1  rillig 	     */
    783  1.15  rillig 	    list->lastAccess = Tail;
    784   1.1  rillig 	} else {
    785   1.1  rillig 	    /*
    786   1.1  rillig 	     * Reset to Middle if gone past first.
    787   1.1  rillig 	     */
    788  1.15  rillig 	    list->lastAccess = Middle;
    789   1.1  rillig 	}
    790   1.1  rillig     }
    791   1.1  rillig 
    792  1.16  rillig     return node;
    793   1.1  rillig }
    794   1.1  rillig 
    795  1.10  rillig /* Close a list which was opened for sequential access. */
    796   1.1  rillig void
    797  1.16  rillig Lst_CloseS(Lst list)
    798   1.1  rillig {
    799  1.20  rillig     assert(LstIsValid(list));
    800  1.16  rillig     assert(list->isOpen);
    801   1.1  rillig 
    802  1.10  rillig     list->isOpen = FALSE;
    803  1.15  rillig     list->lastAccess = Unknown;
    804   1.1  rillig }
    805   1.1  rillig 
    806   1.1  rillig 
    807   1.1  rillig /*
    808   1.1  rillig  * for using the list as a queue
    809   1.1  rillig  */
    810   1.1  rillig 
    811  1.14  rillig /* Add the datum to the tail of the given list. */
    812  1.25  rillig void
    813  1.25  rillig Lst_EnqueueS(Lst list, void *datum)
    814   1.1  rillig {
    815  1.25  rillig     Lst_AppendS(list, datum);
    816   1.1  rillig }
    817   1.1  rillig 
    818  1.25  rillig /* Remove and return the datum at the head of the given list. */
    819   1.1  rillig void *
    820  1.25  rillig Lst_DequeueS(Lst list)
    821   1.1  rillig {
    822  1.16  rillig     void *datum;
    823   1.1  rillig 
    824  1.25  rillig     assert(LstIsValid(list));
    825  1.25  rillig     assert(!LstIsEmpty(list));
    826   1.1  rillig 
    827  1.25  rillig     datum = list->first->datum;
    828  1.25  rillig     Lst_RemoveS(list, list->first);
    829  1.25  rillig     assert(datum != NULL);
    830  1.16  rillig     return datum;
    831   1.1  rillig }
    832