Home | History | Annotate | Line # | Download | only in make
lst.c revision 1.6
      1 /* $NetBSD: lst.c,v 1.6 2020/08/21 02:56:25 rillig Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1988, 1989, 1990, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Adam de Boor.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. Neither the name of the University nor the names of its contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  */
     34 
     35 #include "lst.h"
     36 #include "make_malloc.h"
     37 
     38 #ifndef MAKE_NATIVE
     39 static char rcsid[] = "$NetBSD: lst.c,v 1.6 2020/08/21 02:56:25 rillig Exp $";
     40 #else
     41 #include <sys/cdefs.h>
     42 #ifndef lint
     43 __RCSID("$NetBSD: lst.c,v 1.6 2020/08/21 02:56:25 rillig Exp $");
     44 #endif /* not lint */
     45 #endif
     46 
     47 typedef struct ListNode {
     48     struct ListNode *prevPtr;	/* previous element in list */
     49     struct ListNode *nextPtr;	/* next in list */
     50     unsigned int useCount: 8,	/* Count of functions using the node.
     51 				 * node may not be deleted until count
     52 				 * goes to 0 */
     53     		 flags: 8;	/* Node status flags */
     54     void *datum;		/* datum associated with this element */
     55 } *ListNode;
     56 /*
     57  * Flags required for synchronization
     58  */
     59 #define LN_DELETED	0x0001	/* List node should be removed when done */
     60 
     61 typedef enum {
     62     Head, Middle, Tail, Unknown
     63 } Where;
     64 
     65 typedef struct List {
     66     ListNode firstPtr;		/* first node in list */
     67     ListNode lastPtr;		/* last node in list */
     68 /*
     69  * fields for sequential access
     70  */
     71     Where atEnd;		/* Where in the list the last access was */
     72     Boolean isOpen;		/* true if list has been Lst_Open'ed */
     73     ListNode curPtr;		/* current node, if open. NULL if
     74 				 * *just* opened */
     75     ListNode prevPtr;		/* Previous node, if open. Used by
     76 				 * Lst_Remove */
     77 } *List;
     78 
     79 /*
     80  * PAlloc (var, ptype) --
     81  *	Allocate a pointer-typedef structure 'ptype' into the variable 'var'
     82  */
     83 #define PAlloc(var, ptype) \
     84     var = (ptype) bmake_malloc(sizeof *(var))
     85 
     86 /*
     87  * LstValid --
     88  *	Return TRUE if the list is valid
     89  */
     90 static Boolean
     91 LstValid(Lst l)
     92 {
     93     return l != NULL;
     94 }
     95 
     96 /*
     97  * LstNodeValid --
     98  *	Return TRUE if the list node is valid
     99  */
    100 static Boolean
    101 LstNodeValid(LstNode ln)
    102 {
    103     return ln != NULL;
    104 }
    105 
    106 /*
    107  * LstIsEmpty (l) --
    108  *	TRUE if the list l is empty.
    109  */
    110 static Boolean
    111 LstIsEmpty(Lst l)
    112 {
    113     return l->firstPtr == NULL;
    114 }
    115 
    116 /* Create and initialize a new, empty list. */
    117 Lst
    118 Lst_Init(void)
    119 {
    120     List nList;
    121 
    122     PAlloc (nList, List);
    123 
    124     nList->firstPtr = NULL;
    125     nList->lastPtr = NULL;
    126     nList->isOpen = FALSE;
    127     nList->atEnd = Unknown;
    128 
    129     return nList;
    130 }
    131 
    132 /*-
    133  *-----------------------------------------------------------------------
    134  * Lst_Duplicate --
    135  *	Duplicate an entire list. If a function to copy a void *is
    136  *	given, the individual client elements will be duplicated as well.
    137  *
    138  * Input:
    139  *	l		the list to duplicate
    140  *	copyProc	A function to duplicate each void *
    141  *
    142  * Results:
    143  *	The new Lst structure or NULL if failure.
    144  *
    145  * Side Effects:
    146  *	A new list is created.
    147  *-----------------------------------------------------------------------
    148  */
    149 Lst
    150 Lst_Duplicate(Lst l, DuplicateProc *copyProc)
    151 {
    152     Lst nl;
    153     ListNode ln;
    154     List list = l;
    155 
    156     if (!LstValid(l)) {
    157 	return NULL;
    158     }
    159 
    160     nl = Lst_Init();
    161     if (nl == NULL) {
    162 	return NULL;
    163     }
    164 
    165     ln = list->firstPtr;
    166     while (ln != NULL) {
    167 	if (copyProc != NULL) {
    168 	    if (Lst_AtEnd(nl, copyProc(ln->datum)) == FAILURE) {
    169 		return NULL;
    170 	    }
    171 	} else if (Lst_AtEnd(nl, ln->datum) == FAILURE) {
    172 	    return NULL;
    173 	}
    174 
    175 	ln = ln->nextPtr;
    176     }
    177 
    178     return nl;
    179 }
    180 
    181 /*-
    182  *-----------------------------------------------------------------------
    183  * Lst_Destroy --
    184  *	Destroy a list and free all its resources. If the freeProc is
    185  *	given, it is called with the datum from each node in turn before
    186  *	the node is freed.
    187  *
    188  * Results:
    189  *	None.
    190  *
    191  * Side Effects:
    192  *	The given list is freed in its entirety.
    193  *
    194  *-----------------------------------------------------------------------
    195  */
    196 void
    197 Lst_Destroy(Lst list, FreeProc *freeProc)
    198 {
    199     ListNode ln;
    200     ListNode tln = NULL;
    201 
    202     if (list == NULL)
    203 	return;
    204 
    205     /* To ease scanning */
    206     if (list->lastPtr != NULL)
    207 	list->lastPtr->nextPtr = NULL;
    208     else {
    209 	free(list);
    210 	return;
    211     }
    212 
    213     if (freeProc) {
    214 	for (ln = list->firstPtr; ln != NULL; ln = tln) {
    215 	    tln = ln->nextPtr;
    216 	    freeProc(ln->datum);
    217 	    free(ln);
    218 	}
    219     } else {
    220 	for (ln = list->firstPtr; ln != NULL; ln = tln) {
    221 	    tln = ln->nextPtr;
    222 	    free(ln);
    223 	}
    224     }
    225 
    226     free(list);
    227 }
    228 
    229 /*
    230  * Functions to modify a list
    231  */
    232 
    233 /*-
    234  *-----------------------------------------------------------------------
    235  * Lst_InsertBefore --
    236  *	Insert a new node with the given piece of data before the given
    237  *	node in the given list.
    238  *
    239  * Input:
    240  *	l		list to manipulate
    241  *	ln		node before which to insert d
    242  *	d		datum to be inserted
    243  *
    244  * Results:
    245  *	SUCCESS or FAILURE.
    246  *
    247  * Side Effects:
    248  *	the firstPtr field will be changed if ln is the first node in the
    249  *	list.
    250  *
    251  *-----------------------------------------------------------------------
    252  */
    253 ReturnStatus
    254 Lst_InsertBefore(Lst l, LstNode ln, void *d)
    255 {
    256     ListNode nLNode;		/* new lnode for d */
    257     ListNode lNode = ln;
    258     List list = l;
    259 
    260 
    261     /*
    262      * check validity of arguments
    263      */
    264     if (LstValid(l) && (LstIsEmpty(l) && ln == NULL))
    265 	goto ok;
    266 
    267     if (!LstValid(l) || LstIsEmpty(l) || !LstNodeValid(ln)) {
    268 	return FAILURE;
    269     }
    270 
    271     ok:
    272     PAlloc (nLNode, ListNode);
    273 
    274     nLNode->datum = d;
    275     nLNode->useCount = nLNode->flags = 0;
    276 
    277     if (ln == NULL) {
    278 	nLNode->prevPtr = nLNode->nextPtr = NULL;
    279 	list->firstPtr = list->lastPtr = nLNode;
    280     } else {
    281 	nLNode->prevPtr = lNode->prevPtr;
    282 	nLNode->nextPtr = lNode;
    283 
    284 	if (nLNode->prevPtr != NULL) {
    285 	    nLNode->prevPtr->nextPtr = nLNode;
    286 	}
    287 	lNode->prevPtr = nLNode;
    288 
    289 	if (lNode == list->firstPtr) {
    290 	    list->firstPtr = nLNode;
    291 	}
    292     }
    293 
    294     return SUCCESS;
    295 }
    296 
    297 /*-
    298  *-----------------------------------------------------------------------
    299  * Lst_InsertAfter --
    300  *	Create a new node and add it to the given list after the given node.
    301  *
    302  * Input:
    303  *	l		affected list
    304  *	ln		node after which to append the datum
    305  *	d		said datum
    306  *
    307  * Results:
    308  *	SUCCESS if all went well.
    309  *
    310  * Side Effects:
    311  *	A new ListNode is created and linked in to the List. The lastPtr
    312  *	field of the List will be altered if ln is the last node in the
    313  *	list. lastPtr and firstPtr will alter if the list was empty and
    314  *	ln was NULL.
    315  *
    316  *-----------------------------------------------------------------------
    317  */
    318 ReturnStatus
    319 Lst_InsertAfter(Lst l, LstNode ln, void *d)
    320 {
    321     List list;
    322     ListNode lNode;
    323     ListNode nLNode;
    324 
    325     if (LstValid(l) && (ln == NULL && LstIsEmpty(l))) {
    326 	goto ok;
    327     }
    328 
    329     if (!LstValid(l) || LstIsEmpty(l) || !LstNodeValid(ln)) {
    330 	return FAILURE;
    331     }
    332     ok:
    333 
    334     list = l;
    335     lNode = ln;
    336 
    337     PAlloc (nLNode, ListNode);
    338     nLNode->datum = d;
    339     nLNode->useCount = nLNode->flags = 0;
    340 
    341     if (lNode == NULL) {
    342 	nLNode->nextPtr = nLNode->prevPtr = NULL;
    343 	list->firstPtr = list->lastPtr = nLNode;
    344     } else {
    345 	nLNode->prevPtr = lNode;
    346 	nLNode->nextPtr = lNode->nextPtr;
    347 
    348 	lNode->nextPtr = nLNode;
    349 	if (nLNode->nextPtr != NULL) {
    350 	    nLNode->nextPtr->prevPtr = nLNode;
    351 	}
    352 
    353 	if (lNode == list->lastPtr) {
    354 	    list->lastPtr = nLNode;
    355 	}
    356     }
    357 
    358     return SUCCESS;
    359 }
    360 
    361 /*-
    362  *-----------------------------------------------------------------------
    363  * Lst_AtFront --
    364  *	Place a piece of data at the front of a list
    365  *
    366  * Results:
    367  *	SUCCESS or FAILURE
    368  *
    369  * Side Effects:
    370  *	A new ListNode is created and stuck at the front of the list.
    371  *	hence, firstPtr (and possible lastPtr) in the list are altered.
    372  *
    373  *-----------------------------------------------------------------------
    374  */
    375 ReturnStatus
    376 Lst_AtFront(Lst l, void *d)
    377 {
    378     LstNode front;
    379 
    380     front = Lst_First(l);
    381     return Lst_InsertBefore(l, front, d);
    382 }
    383 
    384 /*-
    385  *-----------------------------------------------------------------------
    386  * Lst_AtEnd --
    387  *	Add a node to the end of the given list
    388  *
    389  * Input:
    390  *	l		List to which to add the datum
    391  *	d		Datum to add
    392  *
    393  * Results:
    394  *	SUCCESS if life is good.
    395  *
    396  * Side Effects:
    397  *	A new ListNode is created and added to the list.
    398  *
    399  *-----------------------------------------------------------------------
    400  */
    401 ReturnStatus
    402 Lst_AtEnd(Lst l, void *d)
    403 {
    404     LstNode end;
    405 
    406     end = Lst_Last(l);
    407     return Lst_InsertAfter(l, end, d);
    408 }
    409 
    410 /*-
    411  *-----------------------------------------------------------------------
    412  * Lst_Remove --
    413  *	Remove the given node from the given list.
    414  *
    415  * Results:
    416  *	SUCCESS or FAILURE.
    417  *
    418  * Side Effects:
    419  *	The list's firstPtr will be set to NULL if ln is the last
    420  *	node on the list. firsPtr and lastPtr will be altered if ln is
    421  *	either the first or last node, respectively, on the list.
    422  *
    423  *-----------------------------------------------------------------------
    424  */
    425 ReturnStatus
    426 Lst_Remove(Lst l, LstNode ln)
    427 {
    428     List list = l;
    429     ListNode lNode = ln;
    430 
    431     if (!LstValid(l) || !LstNodeValid(ln)) {
    432 	return FAILURE;
    433     }
    434 
    435     /*
    436      * unlink it from the list
    437      */
    438     if (lNode->nextPtr != NULL) {
    439 	lNode->nextPtr->prevPtr = lNode->prevPtr;
    440     }
    441     if (lNode->prevPtr != NULL) {
    442 	lNode->prevPtr->nextPtr = lNode->nextPtr;
    443     }
    444 
    445     /*
    446      * if either the firstPtr or lastPtr of the list point to this node,
    447      * adjust them accordingly
    448      */
    449     if (list->firstPtr == lNode) {
    450 	list->firstPtr = lNode->nextPtr;
    451     }
    452     if (list->lastPtr == lNode) {
    453 	list->lastPtr = lNode->prevPtr;
    454     }
    455 
    456     /*
    457      * Sequential access stuff. If the node we're removing is the current
    458      * node in the list, reset the current node to the previous one. If the
    459      * previous one was non-existent (prevPtr == NULL), we set the
    460      * end to be Unknown, since it is.
    461      */
    462     if (list->isOpen && (list->curPtr == lNode)) {
    463 	list->curPtr = list->prevPtr;
    464 	if (list->curPtr == NULL) {
    465 	    list->atEnd = Unknown;
    466 	}
    467     }
    468 
    469     /*
    470      * note that the datum is unmolested. The caller must free it as
    471      * necessary and as expected.
    472      */
    473     if (lNode->useCount == 0) {
    474 	free(ln);
    475     } else {
    476 	lNode->flags |= LN_DELETED;
    477     }
    478 
    479     return SUCCESS;
    480 }
    481 
    482 /*-
    483  *-----------------------------------------------------------------------
    484  * Lst_Replace --
    485  *	Replace the datum in the given node with the new datum
    486  *
    487  * Results:
    488  *	SUCCESS or FAILURE.
    489  *
    490  * Side Effects:
    491  *	The datum field fo the node is altered.
    492  *
    493  *-----------------------------------------------------------------------
    494  */
    495 ReturnStatus
    496 Lst_Replace(LstNode ln, void *d)
    497 {
    498     if (ln == NULL) {
    499 	return FAILURE;
    500     } else {
    501 	(ln)->datum = d;
    502 	return SUCCESS;
    503     }
    504 }
    505 
    506 
    507 /*
    508  * Node-specific functions
    509  */
    510 
    511 /*-
    512  *-----------------------------------------------------------------------
    513  * Lst_First --
    514  *	Return the first node on the given list.
    515  *
    516  * Results:
    517  *	The first node or NULL if the list is empty.
    518  *
    519  * Side Effects:
    520  *	None.
    521  *
    522  *-----------------------------------------------------------------------
    523  */
    524 LstNode
    525 Lst_First(Lst l)
    526 {
    527     if (!LstValid(l) || LstIsEmpty(l)) {
    528 	return NULL;
    529     } else {
    530 	return l->firstPtr;
    531     }
    532 }
    533 
    534 /*-
    535  *-----------------------------------------------------------------------
    536  * Lst_Last --
    537  *	Return the last node on the list l.
    538  *
    539  * Results:
    540  *	The requested node or NULL if the list is empty.
    541  *
    542  * Side Effects:
    543  *	None.
    544  *
    545  *-----------------------------------------------------------------------
    546  */
    547 LstNode
    548 Lst_Last(Lst l)
    549 {
    550     if (!LstValid(l) || LstIsEmpty(l)) {
    551 	return NULL;
    552     } else {
    553 	return l->lastPtr;
    554     }
    555 }
    556 
    557 /* Return the successor to the given node on its list, or NULL. */
    558 LstNode
    559 Lst_Succ(LstNode ln)
    560 {
    561     if (ln == NULL) {
    562 	return NULL;
    563     } else {
    564 	return ln->nextPtr;
    565     }
    566 }
    567 
    568 /* Return the predecessor to the given node on its list, or NULL. */
    569 LstNode
    570 Lst_Prev(LstNode ln)
    571 {
    572     if (ln == NULL) {
    573 	return NULL;
    574     } else {
    575 	return ln->prevPtr;
    576     }
    577 }
    578 
    579 /*-
    580  *-----------------------------------------------------------------------
    581  * Lst_Datum --
    582  *	Return the datum stored in the given node.
    583  *
    584  * Results:
    585  *	The datum or NULL if the node is invalid.
    586  *
    587  * Side Effects:
    588  *	None.
    589  *
    590  *-----------------------------------------------------------------------
    591  */
    592 void *
    593 Lst_Datum(LstNode ln)
    594 {
    595     if (ln != NULL) {
    596 	return ln->datum;
    597     } else {
    598 	return NULL;
    599     }
    600 }
    601 
    602 
    603 /*
    604  * Functions for entire lists
    605  */
    606 
    607 /*-
    608  *-----------------------------------------------------------------------
    609  * Lst_IsEmpty --
    610  *	Return TRUE if the given list is empty.
    611  *
    612  * Results:
    613  *	TRUE if the list is empty, FALSE otherwise.
    614  *
    615  * Side Effects:
    616  *	None.
    617  *
    618  *	A list is considered empty if its firstPtr == NULL (or if
    619  *	the list itself is NULL).
    620  *-----------------------------------------------------------------------
    621  */
    622 Boolean
    623 Lst_IsEmpty(Lst l)
    624 {
    625     return !LstValid(l) || LstIsEmpty(l);
    626 }
    627 
    628 /*-
    629  *-----------------------------------------------------------------------
    630  * Lst_Find --
    631  *	Find a node on the given list using the given comparison function
    632  *	and the given datum.
    633  *
    634  * Results:
    635  *	The found node or NULL if none matches.
    636  *
    637  * Side Effects:
    638  *	None.
    639  *
    640  *-----------------------------------------------------------------------
    641  */
    642 LstNode
    643 Lst_Find(Lst l, const void *d, int (*cProc)(const void *, const void *))
    644 {
    645     return Lst_FindFrom(l, Lst_First(l), d, cProc);
    646 }
    647 
    648 /*-
    649  *-----------------------------------------------------------------------
    650  * Lst_FindFrom --
    651  *	Search for a node starting and ending with the given one on the
    652  *	given list using the passed datum and comparison function to
    653  *	determine when it has been found.
    654  *
    655  * Results:
    656  *	The found node or NULL
    657  *
    658  * Side Effects:
    659  *	None.
    660  *
    661  *-----------------------------------------------------------------------
    662  */
    663 LstNode
    664 Lst_FindFrom(Lst l, LstNode ln, const void *d,
    665 	     int (*cProc)(const void *, const void *))
    666 {
    667     ListNode tln;
    668 
    669     if (!LstValid(l) || LstIsEmpty(l) || !LstNodeValid(ln)) {
    670 	return NULL;
    671     }
    672 
    673     tln = ln;
    674 
    675     do {
    676 	if ((*cProc)(tln->datum, d) == 0)
    677 	    return tln;
    678 	tln = tln->nextPtr;
    679     } while (tln != ln && tln != NULL);
    680 
    681     return NULL;
    682 }
    683 
    684 /*-
    685  * See if a given datum is on a given list.
    686  */
    687 LstNode
    688 Lst_Member(Lst l, void *d)
    689 {
    690     List list = l;
    691     ListNode lNode;
    692 
    693     if (list == NULL) {
    694 	return NULL;
    695     }
    696     lNode = list->firstPtr;
    697     if (lNode == NULL) {
    698 	return NULL;
    699     }
    700 
    701     do {
    702 	if (lNode->datum == d) {
    703 	    return lNode;
    704 	}
    705 	lNode = lNode->nextPtr;
    706     } while (lNode != NULL && lNode != list->firstPtr);
    707 
    708     return NULL;
    709 }
    710 
    711 /*-
    712  *-----------------------------------------------------------------------
    713  * Lst_ForEach --
    714  *	Apply the given function to each element of the given list. The
    715  *	function should return 0 if Lst_ForEach should continue and non-
    716  *	zero if it should abort.
    717  *
    718  * Results:
    719  *	None.
    720  *
    721  * Side Effects:
    722  *	Only those created by the passed-in function.
    723  *
    724  *-----------------------------------------------------------------------
    725  */
    726 /*VARARGS2*/
    727 int
    728 Lst_ForEach(Lst l, int (*proc)(void *, void *), void *d)
    729 {
    730     return Lst_ForEachFrom(l, Lst_First(l), proc, d);
    731 }
    732 
    733 /*-
    734  *-----------------------------------------------------------------------
    735  * Lst_ForEachFrom --
    736  *	Apply the given function to each element of the given list,
    737  *	starting from a given point.
    738  *
    739  *	The function should return 0 if traversal should continue, and
    740  *	non-zero if it should abort.
    741  *
    742  * Results:
    743  *	None.
    744  *
    745  * Side Effects:
    746  *	Only those created by the passed-in function.
    747  *
    748  *-----------------------------------------------------------------------
    749  */
    750 /*VARARGS2*/
    751 int
    752 Lst_ForEachFrom(Lst l, LstNode ln, int (*proc)(void *, void *),
    753 		void *d)
    754 {
    755     ListNode tln = ln;
    756     List list = l;
    757     ListNode next;
    758     Boolean done;
    759     int result;
    760 
    761     if (!LstValid(list) || LstIsEmpty(list)) {
    762 	return 0;
    763     }
    764 
    765     do {
    766 	/*
    767 	 * Take care of having the current element deleted out from under
    768 	 * us.
    769 	 */
    770 
    771 	next = tln->nextPtr;
    772 
    773 	/*
    774 	 * We're done with the traversal if
    775 	 *  - the next node to examine is the first in the queue or
    776 	 *    doesn't exist and
    777 	 *  - nothing's been added after the current node (check this
    778 	 *    after proc() has been called).
    779 	 */
    780 	done = (next == NULL || next == list->firstPtr);
    781 
    782 	(void)tln->useCount++;
    783 	result = (*proc)(tln->datum, d);
    784 	(void)tln->useCount--;
    785 
    786 	/*
    787 	 * Now check whether a node has been added.
    788 	 * Note: this doesn't work if this node was deleted before
    789 	 *       the new node was added.
    790 	 */
    791 	if (next != tln->nextPtr) {
    792 	    next = tln->nextPtr;
    793 	    done = 0;
    794 	}
    795 
    796 	if (tln->flags & LN_DELETED) {
    797 	    free((char *)tln);
    798 	}
    799 	tln = next;
    800     } while (!result && !LstIsEmpty(list) && !done);
    801 
    802     return result;
    803 }
    804 
    805 /*-
    806  *-----------------------------------------------------------------------
    807  * Lst_Concat --
    808  *	Concatenate two lists. New elements are created to hold the data
    809  *	elements, if specified, but the elements themselves are not copied.
    810  *	If the elements should be duplicated to avoid confusion with another
    811  *	list, the Lst_Duplicate function should be called first.
    812  *	If LST_CONCLINK is specified, the second list is destroyed since
    813  *	its pointers have been corrupted and the list is no longer useable.
    814  *
    815  * Input:
    816  *	l1		The list to which l2 is to be appended
    817  *	l2		The list to append to l1
    818  *	flags		LST_CONCNEW if LstNode's should be duplicated
    819  *			LST_CONCLINK if should just be relinked
    820  *
    821  * Results:
    822  *	SUCCESS if all went well. FAILURE otherwise.
    823  *
    824  * Side Effects:
    825  *	New elements are created and appended the first list.
    826  *-----------------------------------------------------------------------
    827  */
    828 ReturnStatus
    829 Lst_Concat(Lst l1, Lst l2, int flags)
    830 {
    831     ListNode ln;     /* original LstNode */
    832     ListNode nln;    /* new LstNode */
    833     ListNode last;   /* the last element in the list. Keeps
    834 				 * bookkeeping until the end */
    835     List list1 = l1;
    836     List list2 = l2;
    837 
    838     if (!LstValid(l1) || !LstValid(l2)) {
    839 	return FAILURE;
    840     }
    841 
    842     if (flags == LST_CONCLINK) {
    843 	if (list2->firstPtr != NULL) {
    844 	    /*
    845 	     * So long as the second list isn't empty, we just link the
    846 	     * first element of the second list to the last element of the
    847 	     * first list. If the first list isn't empty, we then link the
    848 	     * last element of the list to the first element of the second list
    849 	     * The last element of the second list, if it exists, then becomes
    850 	     * the last element of the first list.
    851 	     */
    852 	    list2->firstPtr->prevPtr = list1->lastPtr;
    853 	    if (list1->lastPtr != NULL) {
    854 		list1->lastPtr->nextPtr = list2->firstPtr;
    855 	    } else {
    856 		list1->firstPtr = list2->firstPtr;
    857 	    }
    858 	    list1->lastPtr = list2->lastPtr;
    859 	}
    860 	free(l2);
    861     } else if (list2->firstPtr != NULL) {
    862 	/*
    863 	 * We set the nextPtr of the last element of list 2 to be nil to make
    864 	 * the loop less difficult. The loop simply goes through the entire
    865 	 * second list creating new LstNodes and filling in the nextPtr, and
    866 	 * prevPtr to fit into l1 and its datum field from the
    867 	 * datum field of the corresponding element in l2. The 'last' node
    868 	 * follows the last of the new nodes along until the entire l2 has
    869 	 * been appended. Only then does the bookkeeping catch up with the
    870 	 * changes. During the first iteration of the loop, if 'last' is nil,
    871 	 * the first list must have been empty so the newly-created node is
    872 	 * made the first node of the list.
    873 	 */
    874 	list2->lastPtr->nextPtr = NULL;
    875 	for (last = list1->lastPtr, ln = list2->firstPtr;
    876 	     ln != NULL;
    877 	     ln = ln->nextPtr)
    878 	{
    879 	    PAlloc (nln, ListNode);
    880 	    nln->datum = ln->datum;
    881 	    if (last != NULL) {
    882 		last->nextPtr = nln;
    883 	    } else {
    884 		list1->firstPtr = nln;
    885 	    }
    886 	    nln->prevPtr = last;
    887 	    nln->flags = nln->useCount = 0;
    888 	    last = nln;
    889 	}
    890 
    891 	/*
    892 	 * Finish bookkeeping. The last new element becomes the last element
    893 	 * of list one.
    894 	 */
    895 	list1->lastPtr = last;
    896 	last->nextPtr = NULL;
    897     }
    898 
    899     return SUCCESS;
    900 }
    901 
    902 
    903 /*
    904  * these functions are for dealing with a list as a table, of sorts.
    905  * An idea of the "current element" is kept and used by all the functions
    906  * between Lst_Open() and Lst_Close().
    907  *
    908  * The sequential functions access the list in a slightly different way.
    909  * CurPtr points to their idea of the current node in the list and they
    910  * access the list based on it.
    911  */
    912 
    913 /*-
    914  *-----------------------------------------------------------------------
    915  * Lst_Open --
    916  *	Open a list for sequential access. A list can still be searched,
    917  *	etc., without confusing these functions.
    918  *
    919  * Results:
    920  *	SUCCESS or FAILURE.
    921  *
    922  * Side Effects:
    923  *	isOpen is set TRUE and curPtr is set to NULL so the
    924  *	other sequential functions know it was just opened and can choose
    925  *	the first element accessed based on this.
    926  *
    927  *-----------------------------------------------------------------------
    928  */
    929 ReturnStatus
    930 Lst_Open(Lst l)
    931 {
    932     if (LstValid(l) == FALSE) {
    933 	return FAILURE;
    934     }
    935     l->isOpen = TRUE;
    936     l->atEnd = LstIsEmpty(l) ? Head : Unknown;
    937     l->curPtr = NULL;
    938 
    939     return SUCCESS;
    940 }
    941 
    942 /*-
    943  *-----------------------------------------------------------------------
    944  * Lst_Next --
    945  *	Return the next node for the given list.
    946  *
    947  * Results:
    948  *	The next node or NULL if the list has yet to be opened. Also
    949  *	if the end has been reached, NULL is returned.
    950  *
    951  * Side Effects:
    952  *	the curPtr field is updated.
    953  *
    954  *-----------------------------------------------------------------------
    955  */
    956 LstNode
    957 Lst_Next(Lst l)
    958 {
    959     ListNode tln;
    960     List list = l;
    961 
    962     if ((LstValid(l) == FALSE) ||
    963 	(list->isOpen == FALSE)) {
    964 	return NULL;
    965     }
    966 
    967     list->prevPtr = list->curPtr;
    968 
    969     if (list->curPtr == NULL) {
    970 	if (list->atEnd == Unknown) {
    971 	    /*
    972 	     * If we're just starting out, atEnd will be Unknown.
    973 	     * Then we want to start this thing off in the right
    974 	     * direction -- at the start with atEnd being Middle.
    975 	     */
    976 	    list->curPtr = tln = list->firstPtr;
    977 	    list->atEnd = Middle;
    978 	} else {
    979 	    tln = NULL;
    980 	    list->atEnd = Tail;
    981 	}
    982     } else {
    983 	tln = list->curPtr->nextPtr;
    984 	list->curPtr = tln;
    985 
    986 	if (tln == list->firstPtr || tln == NULL) {
    987 	    /*
    988 	     * If back at the front, then we've hit the end...
    989 	     */
    990 	    list->atEnd = Tail;
    991 	} else {
    992 	    /*
    993 	     * Reset to Middle if gone past first.
    994 	     */
    995 	    list->atEnd = Middle;
    996 	}
    997     }
    998 
    999     return tln;
   1000 }
   1001 
   1002 /*-
   1003  *-----------------------------------------------------------------------
   1004  * Lst_Close --
   1005  *	Close a list which was opened for sequential access.
   1006  *
   1007  * Input:
   1008  *	l		The list to close
   1009  *
   1010  * Results:
   1011  *	None.
   1012  *
   1013  * Side Effects:
   1014  *	The list is closed.
   1015  *
   1016  *-----------------------------------------------------------------------
   1017  */
   1018 void
   1019 Lst_Close(Lst l)
   1020 {
   1021     List list = l;
   1022 
   1023     if (LstValid(l) == TRUE) {
   1024 	list->isOpen = FALSE;
   1025 	list->atEnd = Unknown;
   1026     }
   1027 }
   1028 
   1029 
   1030 /*
   1031  * for using the list as a queue
   1032  */
   1033 
   1034 /*-
   1035  *-----------------------------------------------------------------------
   1036  * Lst_EnQueue --
   1037  *	Add the datum to the tail of the given list.
   1038  *
   1039  * Results:
   1040  *	SUCCESS or FAILURE as returned by Lst_InsertAfter.
   1041  *
   1042  * Side Effects:
   1043  *	the lastPtr field is altered all the time and the firstPtr field
   1044  *	will be altered if the list used to be empty.
   1045  *
   1046  *-----------------------------------------------------------------------
   1047  */
   1048 ReturnStatus
   1049 Lst_EnQueue(Lst l, void *d)
   1050 {
   1051     if (LstValid(l) == FALSE) {
   1052 	return FAILURE;
   1053     }
   1054 
   1055     return Lst_InsertAfter(l, Lst_Last(l), d);
   1056 }
   1057 
   1058 /*-
   1059  *-----------------------------------------------------------------------
   1060  * Lst_DeQueue --
   1061  *	Remove and return the datum at the head of the given list.
   1062  *
   1063  * Results:
   1064  *	The datum in the node at the head or NULL if the list
   1065  *	is empty.
   1066  *
   1067  * Side Effects:
   1068  *	The head node is removed from the list.
   1069  *
   1070  *-----------------------------------------------------------------------
   1071  */
   1072 void *
   1073 Lst_DeQueue(Lst l)
   1074 {
   1075     void *rd;
   1076     ListNode tln;
   1077 
   1078     tln = Lst_First(l);
   1079     if (tln == NULL) {
   1080 	return NULL;
   1081     }
   1082 
   1083     rd = tln->datum;
   1084     if (Lst_Remove(l, tln) == FAILURE) {
   1085 	return NULL;
   1086     } else {
   1087 	return rd;
   1088     }
   1089 }
   1090