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