/usr.bin/make/lst.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 344 lines · 155 code · 33 blank · 156 comment · 53 complexity · 03c110306a1c073274519c22656cba75 MD5 · raw file

  1. /*-
  2. * Copyright (c) 1988, 1989, 1990, 1993
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * This code is derived from software contributed to Berkeley by
  6. * Adam de Boor.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the name of the University nor the names of its contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  21. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  24. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  26. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  29. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * $FreeBSD$
  33. */
  34. /*-
  35. * lst.c --
  36. * Routines to maintain a linked list of objects.
  37. */
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include "lst.h"
  41. #include "util.h"
  42. /**
  43. * Lst_Append
  44. * Create a new node and add it to the given list after the given node.
  45. *
  46. * Arguments:
  47. * l affected list
  48. * ln node after which to append the datum
  49. * d said datum
  50. *
  51. * Side Effects:
  52. * A new LstNode is created and linked in to the List. The lastPtr
  53. * field of the List will be altered if ln is the last node in the
  54. * list. lastPtr and firstPtr will alter if the list was empty and
  55. * ln was NULL.
  56. */
  57. void
  58. Lst_Append(Lst *list, LstNode *ln, void *d)
  59. {
  60. LstNode *nLNode;
  61. nLNode = emalloc(sizeof(*nLNode));
  62. nLNode->datum = d;
  63. if (ln == NULL) {
  64. nLNode->nextPtr = nLNode->prevPtr = NULL;
  65. list->firstPtr = list->lastPtr = nLNode;
  66. } else {
  67. nLNode->prevPtr = ln;
  68. nLNode->nextPtr = ln->nextPtr;
  69. ln->nextPtr = nLNode;
  70. if (nLNode->nextPtr != NULL) {
  71. nLNode->nextPtr->prevPtr = nLNode;
  72. }
  73. if (ln == list->lastPtr) {
  74. list->lastPtr = nLNode;
  75. }
  76. }
  77. }
  78. /**
  79. * Lst_Concat
  80. * Concatenate two lists. New elements are created to hold the data
  81. * elements, if specified, but the elements themselves are not copied.
  82. * If the elements should be duplicated to avoid confusion with another
  83. * list, the Lst_Duplicate function should be called first.
  84. *
  85. * Arguments:
  86. * list1 The list to which list2 is to be appended
  87. * list2 The list to append to list1
  88. * flags LST_CONCNEW if LstNode's should be duplicated
  89. * LST_CONCLINK if should just be relinked
  90. *
  91. * Side Effects:
  92. * New elements are created and appended the first list.
  93. */
  94. void
  95. Lst_Concat(Lst *list1, Lst *list2, int flags)
  96. {
  97. LstNode *ln; /* original LstNode */
  98. LstNode *nln; /* new LstNode */
  99. LstNode *last; /* the last element in the list. Keeps
  100. * bookkeeping until the end */
  101. if (list2->firstPtr == NULL)
  102. return;
  103. if (flags == LST_CONCLINK) {
  104. /*
  105. * Link the first element of the second list to the last
  106. * element of the first list. If the first list isn't empty,
  107. * we then link the last element of the list to the first
  108. * element of the second list. The last element of the second
  109. * list, if it exists, then becomes the last element of the
  110. * first list.
  111. */
  112. list2->firstPtr->prevPtr = list1->lastPtr;
  113. if (list1->lastPtr != NULL)
  114. list1->lastPtr->nextPtr = list2->firstPtr;
  115. else
  116. list1->firstPtr = list2->firstPtr;
  117. list1->lastPtr = list2->lastPtr;
  118. Lst_Init(list2);
  119. } else {
  120. /*
  121. * The loop simply goes through the entire second list creating
  122. * new LstNodes and filling in the nextPtr, and prevPtr to fit
  123. * into list1 and its datum field from the datum field of the
  124. * corresponding element in list2. The 'last' node follows the
  125. * last of the new nodes along until the entire list2 has been
  126. * appended. Only then does the bookkeeping catch up with the
  127. * changes. During the first iteration of the loop, if 'last'
  128. * is NULL, the first list must have been empty so the
  129. * newly-created node is made the first node of the list.
  130. */
  131. for (last = list1->lastPtr, ln = list2->firstPtr;
  132. ln != NULL;
  133. ln = ln->nextPtr) {
  134. nln = emalloc(sizeof(*nln));
  135. nln->datum = ln->datum;
  136. if (last != NULL) {
  137. last->nextPtr = nln;
  138. } else {
  139. list1->firstPtr = nln;
  140. }
  141. nln->prevPtr = last;
  142. last = nln;
  143. }
  144. /*
  145. * Finish bookkeeping. The last new element becomes the last
  146. * element of list one.
  147. */
  148. list1->lastPtr = last;
  149. last->nextPtr = NULL;
  150. }
  151. }
  152. /**
  153. * Lst_DeQueue
  154. * Remove and return the datum at the head of the given list.
  155. *
  156. * Results:
  157. * The datum in the node at the head or (ick) NULL if the list
  158. * is empty.
  159. *
  160. * Side Effects:
  161. * The head node is removed from the list.
  162. */
  163. void *
  164. Lst_DeQueue(Lst *l)
  165. {
  166. void *rd;
  167. LstNode *tln;
  168. tln = Lst_First(l);
  169. if (tln == NULL) {
  170. return (NULL);
  171. }
  172. rd = tln->datum;
  173. Lst_Remove(l, tln);
  174. return (rd);
  175. }
  176. /**
  177. * Lst_Destroy
  178. * Destroy a list and free all its resources. If the freeProc is
  179. * given, it is called with the datum from each node in turn before
  180. * the node is freed.
  181. *
  182. * Side Effects:
  183. * The given list is freed in its entirety.
  184. */
  185. void
  186. Lst_Destroy(Lst *list, FreeProc *freeProc)
  187. {
  188. LstNode *ln;
  189. if (list->firstPtr == NULL)
  190. return;
  191. if (freeProc != NOFREE) {
  192. while ((ln = list->firstPtr) != NULL) {
  193. list->firstPtr = ln->nextPtr;
  194. (*freeProc)(ln->datum);
  195. free(ln);
  196. }
  197. } else {
  198. while ((ln = list->firstPtr) != NULL) {
  199. list->firstPtr = ln->nextPtr;
  200. free(ln);
  201. }
  202. }
  203. list->lastPtr = NULL;
  204. }
  205. /**
  206. * Lst_Duplicate
  207. * Duplicate an entire list. If a function to copy a void * is
  208. * given, the individual client elements will be duplicated as well.
  209. *
  210. * Arguments:
  211. * dst the destination list (initialized)
  212. * src the list to duplicate
  213. * copyProc A function to duplicate each void
  214. */
  215. void
  216. Lst_Duplicate(Lst *dst, Lst *src, DuplicateProc *copyProc)
  217. {
  218. LstNode *ln;
  219. ln = src->firstPtr;
  220. while (ln != NULL) {
  221. if (copyProc != NOCOPY)
  222. Lst_AtEnd(dst, (*copyProc)(ln->datum));
  223. else
  224. Lst_AtEnd(dst, ln->datum);
  225. ln = ln->nextPtr;
  226. }
  227. }
  228. /**
  229. * Lst_Insert
  230. * Insert a new node with the given piece of data before the given
  231. * node in the given list.
  232. *
  233. * Parameters:
  234. * l list to manipulate
  235. * ln node before which to insert d
  236. * d datum to be inserted
  237. *
  238. * Side Effects:
  239. * the firstPtr field will be changed if ln is the first node in the
  240. * list.
  241. */
  242. void
  243. Lst_Insert(Lst *list, LstNode *ln, void *d)
  244. {
  245. LstNode *nLNode; /* new lnode for d */
  246. nLNode = emalloc(sizeof(*nLNode));
  247. nLNode->datum = d;
  248. if (ln == NULL) {
  249. nLNode->prevPtr = nLNode->nextPtr = NULL;
  250. list->firstPtr = list->lastPtr = nLNode;
  251. } else {
  252. nLNode->prevPtr = ln->prevPtr;
  253. nLNode->nextPtr = ln;
  254. if (nLNode->prevPtr != NULL) {
  255. nLNode->prevPtr->nextPtr = nLNode;
  256. }
  257. ln->prevPtr = nLNode;
  258. if (ln == list->firstPtr) {
  259. list->firstPtr = nLNode;
  260. }
  261. }
  262. }
  263. LstNode *
  264. Lst_Member(Lst *list, void *d)
  265. {
  266. LstNode *lNode;
  267. lNode = list->firstPtr;
  268. if (lNode == NULL) {
  269. return (NULL);
  270. }
  271. do {
  272. if (lNode->datum == d) {
  273. return (lNode);
  274. }
  275. lNode = lNode->nextPtr;
  276. } while (lNode != NULL && lNode != list->firstPtr);
  277. return (NULL);
  278. }
  279. /**
  280. * Lst_Remove
  281. * Remove the given node from the given list.
  282. *
  283. * Side Effects:
  284. * The list's firstPtr will be set to NULL if ln is the last
  285. * node on the list. firsPtr and lastPtr will be altered if ln is
  286. * either the first or last node, respectively, on the list.
  287. */
  288. void
  289. Lst_Remove(Lst *list, LstNode *ln)
  290. {
  291. /*
  292. * unlink it from the list
  293. */
  294. if (ln->nextPtr != NULL)
  295. /* unlink from the backward chain */
  296. ln->nextPtr->prevPtr = ln->prevPtr;
  297. else
  298. /* this was the last element */
  299. list->lastPtr = ln->prevPtr;
  300. if (ln->prevPtr != NULL)
  301. /* unlink from the forward chain */
  302. ln->prevPtr->nextPtr = ln->nextPtr;
  303. else
  304. /* this was the first element */
  305. list->firstPtr = ln->nextPtr;
  306. /*
  307. * note that the datum is unmolested. The caller must free it as
  308. * necessary and as expected.
  309. */
  310. free(ln);
  311. }