]>
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) | |
25 | # include <unistd.h> | |
26 | #endif | |
27 | ||
28 | #include "shell.h" | |
29 | ||
30 | /* A global variable which acts as a sentinel for an `error' list return. */ | |
31 | GENERIC_LIST global_error_list; | |
32 | ||
33 | /* Call FUNCTION on every member of LIST, a generic list. */ | |
34 | void | |
35 | map_over_list (list, function) | |
36 | GENERIC_LIST *list; | |
37 | Function *function; | |
38 | { | |
39 | for ( ; list; list = list->next) | |
40 | (*function) (list); | |
41 | } | |
42 | ||
43 | /* Call FUNCTION on every string in WORDS. */ | |
44 | void | |
45 | map_over_words (words, function) | |
46 | WORD_LIST *words; | |
47 | Function *function; | |
48 | { | |
49 | for ( ; words; words = words->next) | |
50 | (*function) (words->word->word); | |
51 | } | |
52 | ||
53 | /* Reverse the chain of structures in LIST. Output the new head | |
54 | of the chain. You should always assign the output value of this | |
55 | function to something, or you will lose the chain. */ | |
56 | GENERIC_LIST * | |
57 | reverse_list (list) | |
58 | GENERIC_LIST *list; | |
59 | { | |
60 | register GENERIC_LIST *next, *prev; | |
61 | ||
62 | for (prev = (GENERIC_LIST *)NULL; list; ) | |
63 | { | |
64 | next = list->next; | |
65 | list->next = prev; | |
66 | prev = list; | |
67 | list = next; | |
68 | } | |
69 | return (prev); | |
70 | } | |
71 | ||
72 | /* Return the number of elements in LIST, a generic list. */ | |
73 | int | |
74 | list_length (list) | |
75 | GENERIC_LIST *list; | |
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 * | |
85 | list_append (head, tail) | |
86 | GENERIC_LIST *head, *tail; | |
87 | { | |
88 | register GENERIC_LIST *t_head; | |
89 | ||
90 | if (head == 0) | |
91 | return (tail); | |
92 | ||
93 | for (t_head = head; t_head->next; t_head = t_head->next) | |
94 | ; | |
95 | t_head->next = tail; | |
96 | return (head); | |
97 | } | |
98 | ||
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 | ||
105 | SHELL_VAR *elt = delete_element (&variable_list, check_var_has_name, "foo"); | |
106 | dispose_variable (elt); | |
107 | */ | |
108 | GENERIC_LIST * | |
109 | delete_element (list, comparer, arg) | |
110 | GENERIC_LIST **list; | |
111 | Function *comparer; | |
112 | char *arg; | |
113 | { | |
114 | register GENERIC_LIST *prev, *temp; | |
115 | ||
116 | for (prev = (GENERIC_LIST *)NULL, temp = *list; temp; prev = temp, temp = temp->next) | |
117 | { | |
118 | if ((*comparer) (temp, arg)) | |
119 | { | |
120 | if (prev) | |
121 | prev->next = temp->next; | |
122 | else | |
123 | *list = temp->next; | |
124 | return (temp); | |
125 | } | |
126 | } | |
127 | return ((GENERIC_LIST *)&global_error_list); | |
128 | } |