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