xref: /csrg-svn/usr.bin/make/lst.lib/lstNext.c (revision 40411)
1*40411Sbostic /*-
2*40411Sbostic  * LstNext.c --
3*40411Sbostic  *	Return the next node for a list.
4*40411Sbostic  *	The sequential functions access the list in a slightly different way.
5*40411Sbostic  *	CurPtr points to their idea of the current node in the list and they
6*40411Sbostic  *	access the list based on it. Because the list is circular, Lst_Next
7*40411Sbostic  *	and Lst_Prev will go around the list forever. Lst_IsAtEnd must be
8*40411Sbostic  *	used to determine when to stop.
9*40411Sbostic  *
10*40411Sbostic  * Copyright (c) 1988 by University of California Regents
11*40411Sbostic  *
12*40411Sbostic  * Permission to use, copy, modify, and distribute this
13*40411Sbostic  * software and its documentation for any purpose and without
14*40411Sbostic  * fee is hereby granted, provided that the above copyright
15*40411Sbostic  * notice appears in all copies.  Neither the University of California nor
16*40411Sbostic  * Adam de Boor makes any representations about the suitability of this
17*40411Sbostic  * software for any purpose.  It is provided "as is" without
18*40411Sbostic  * express or implied warranty.
19*40411Sbostic  */
20*40411Sbostic #ifndef lint
21*40411Sbostic static char *rcsid =
22*40411Sbostic "$Id: lstNext.c,v 1.8 88/11/17 20:53:40 adam Exp $ SPRITE (Berkeley)";
23*40411Sbostic #endif lint
24*40411Sbostic 
25*40411Sbostic #include	"lstInt.h"
26*40411Sbostic 
27*40411Sbostic /*-
28*40411Sbostic  *-----------------------------------------------------------------------
29*40411Sbostic  * Lst_Next --
30*40411Sbostic  *	Return the next node for the given list.
31*40411Sbostic  *
32*40411Sbostic  * Results:
33*40411Sbostic  *	The next node or NILLNODE if the list has yet to be opened. Also
34*40411Sbostic  *	if the list is non-circular and the end has been reached, NILLNODE
35*40411Sbostic  *	is returned.
36*40411Sbostic  *
37*40411Sbostic  * Side Effects:
38*40411Sbostic  *	the curPtr field is updated.
39*40411Sbostic  *
40*40411Sbostic  *-----------------------------------------------------------------------
41*40411Sbostic  */
42*40411Sbostic LstNode
43*40411Sbostic Lst_Next (l)
44*40411Sbostic     Lst	    	  l;
45*40411Sbostic {
46*40411Sbostic     register ListNode	tln;
47*40411Sbostic     register List 	list = (List)l;
48*40411Sbostic 
49*40411Sbostic     if ((LstValid (l) == FALSE) ||
50*40411Sbostic 	(list->isOpen == FALSE)) {
51*40411Sbostic 	    return (NILLNODE);
52*40411Sbostic     }
53*40411Sbostic 
54*40411Sbostic     list->prevPtr = list->curPtr;
55*40411Sbostic 
56*40411Sbostic     if (list->curPtr == NilListNode) {
57*40411Sbostic 	if (list->atEnd == Unknown) {
58*40411Sbostic 	    /*
59*40411Sbostic 	     * If we're just starting out, atEnd will be Unknown.
60*40411Sbostic 	     * Then we want to start this thing off in the right
61*40411Sbostic 	     * direction -- at the start with atEnd being Middle.
62*40411Sbostic 	     */
63*40411Sbostic 	    list->curPtr = tln = list->firstPtr;
64*40411Sbostic 	    list->atEnd = Middle;
65*40411Sbostic 	} else {
66*40411Sbostic 	    tln = NilListNode;
67*40411Sbostic 	    list->atEnd = Tail;
68*40411Sbostic 	}
69*40411Sbostic     } else {
70*40411Sbostic 	tln = list->curPtr->nextPtr;
71*40411Sbostic 	list->curPtr = tln;
72*40411Sbostic 
73*40411Sbostic 	if (tln == list->firstPtr || tln == NilListNode) {
74*40411Sbostic 	    /*
75*40411Sbostic 	     * If back at the front, then we've hit the end...
76*40411Sbostic 	     */
77*40411Sbostic 	    list->atEnd = Tail;
78*40411Sbostic 	} else {
79*40411Sbostic 	    /*
80*40411Sbostic 	     * Reset to Middle if gone past first.
81*40411Sbostic 	     */
82*40411Sbostic 	    list->atEnd = Middle;
83*40411Sbostic 	}
84*40411Sbostic     }
85*40411Sbostic 
86*40411Sbostic     return ((LstNode)tln);
87*40411Sbostic }
88*40411Sbostic 
89