]>
Commit | Line | Data |
---|---|---|
ccc6cda3 JA |
1 | /* list.c - Functions for manipulating linked lists of objects. */ |
2 | ||
3 | /* Copyright (C) 1996 | |
4 | Free Software Foundation, Inc. | |
5 | ||
6 | This file is part of GNU Bash, the Bourne Again SHell. | |
7 | ||
8 | Bash is free software; you can redistribute it and/or modify it under | |
9 | the terms of the GNU General Public License as published by the Free | |
10 | Software Foundation; either version 2, or (at your option) any later | |
11 | version. | |
12 | ||
13 | Bash is distributed in the hope that it will be useful, but WITHOUT ANY | |
14 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 | for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License along | |
19 | with Bash; see the file COPYING. If not, write to the Free Software | |
20 | Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
21 | ||
22 | #include "config.h" | |
23 | ||
24 | #if defined (HAVE_UNISTD_H) | |
cce855bc JA |
25 | # ifdef _MINIX |
26 | # include <sys/types.h> | |
27 | # endif | |
ccc6cda3 JA |
28 | # include <unistd.h> |
29 | #endif | |
30 | ||
31 | #include "shell.h" | |
32 | ||
33 | /* A global variable which acts as a sentinel for an `error' list return. */ | |
34 | GENERIC_LIST global_error_list; | |
35 | ||
cce855bc | 36 | #ifdef INCLUDE_UNUSED |
ccc6cda3 JA |
37 | /* Call FUNCTION on every member of LIST, a generic list. */ |
38 | void | |
39 | map_over_list (list, function) | |
40 | GENERIC_LIST *list; | |
41 | Function *function; | |
42 | { | |
43 | for ( ; list; list = list->next) | |
44 | (*function) (list); | |
45 | } | |
46 | ||
47 | /* Call FUNCTION on every string in WORDS. */ | |
48 | void | |
49 | map_over_words (words, function) | |
50 | WORD_LIST *words; | |
51 | Function *function; | |
52 | { | |
53 | for ( ; words; words = words->next) | |
54 | (*function) (words->word->word); | |
55 | } | |
cce855bc | 56 | #endif /* INCLUDE_UNUSED */ |
ccc6cda3 JA |
57 | |
58 | /* Reverse the chain of structures in LIST. Output the new head | |
59 | of the chain. You should always assign the output value of this | |
60 | function to something, or you will lose the chain. */ | |
61 | GENERIC_LIST * | |
62 | reverse_list (list) | |
63 | GENERIC_LIST *list; | |
64 | { | |
65 | register GENERIC_LIST *next, *prev; | |
66 | ||
67 | for (prev = (GENERIC_LIST *)NULL; list; ) | |
68 | { | |
69 | next = list->next; | |
70 | list->next = prev; | |
71 | prev = list; | |
72 | list = next; | |
73 | } | |
74 | return (prev); | |
75 | } | |
76 | ||
77 | /* Return the number of elements in LIST, a generic list. */ | |
78 | int | |
79 | list_length (list) | |
80 | GENERIC_LIST *list; | |
81 | { | |
82 | register int i; | |
83 | ||
84 | for (i = 0; list; list = list->next, i++); | |
85 | return (i); | |
86 | } | |
87 | ||
88 | /* Append TAIL to HEAD. Return the header of the list. */ | |
89 | GENERIC_LIST * | |
90 | list_append (head, tail) | |
91 | GENERIC_LIST *head, *tail; | |
92 | { | |
93 | register GENERIC_LIST *t_head; | |
94 | ||
95 | if (head == 0) | |
96 | return (tail); | |
97 | ||
98 | for (t_head = head; t_head->next; t_head = t_head->next) | |
99 | ; | |
100 | t_head->next = tail; | |
101 | return (head); | |
102 | } | |
103 | ||
d166f048 | 104 | #ifdef INCLUDE_UNUSED |
ccc6cda3 JA |
105 | /* Delete the element of LIST which satisfies the predicate function COMPARER. |
106 | Returns the element that was deleted, so you can dispose of it, or -1 if | |
107 | the element wasn't found. COMPARER is called with the list element and | |
108 | then ARG. Note that LIST contains the address of a variable which points | |
109 | to the list. You might call this function like this: | |
110 | ||
111 | SHELL_VAR *elt = delete_element (&variable_list, check_var_has_name, "foo"); | |
112 | dispose_variable (elt); | |
113 | */ | |
114 | GENERIC_LIST * | |
115 | delete_element (list, comparer, arg) | |
116 | GENERIC_LIST **list; | |
117 | Function *comparer; | |
118 | char *arg; | |
119 | { | |
120 | register GENERIC_LIST *prev, *temp; | |
121 | ||
122 | for (prev = (GENERIC_LIST *)NULL, temp = *list; temp; prev = temp, temp = temp->next) | |
123 | { | |
124 | if ((*comparer) (temp, arg)) | |
125 | { | |
126 | if (prev) | |
127 | prev->next = temp->next; | |
128 | else | |
129 | *list = temp->next; | |
130 | return (temp); | |
131 | } | |
132 | } | |
133 | return ((GENERIC_LIST *)&global_error_list); | |
134 | } | |
d166f048 | 135 | #endif |