Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1 | /* list.c - Functions for manipulating linked lists of objects. */ |
| 2 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 3 | /* Copyright (C) 1996-2009 Free Software Foundation, Inc. |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4 | |
| 5 | This file is part of GNU Bash, the Bourne Again SHell. |
| 6 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 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. |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 11 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 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. |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 16 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 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 | */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 20 | |
| 21 | #include "config.h" |
| 22 | |
| 23 | #if defined (HAVE_UNISTD_H) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 24 | # ifdef _MINIX |
| 25 | # include <sys/types.h> |
| 26 | # endif |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 35 | #ifdef INCLUDE_UNUSED |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 36 | /* Call FUNCTION on every member of LIST, a generic list. */ |
| 37 | void |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 38 | list_walk (list, function) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 39 | GENERIC_LIST *list; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 40 | sh_glist_func_t *function; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 41 | { |
| 42 | for ( ; list; list = list->next) |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 43 | if ((*function) (list) < 0) |
| 44 | return; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | /* Call FUNCTION on every string in WORDS. */ |
| 48 | void |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 49 | wlist_walk (words, function) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 50 | WORD_LIST *words; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 51 | sh_icpfunc_t *function; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 52 | { |
| 53 | for ( ; words; words = words->next) |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 54 | if ((*function) (words->word->word) < 0) |
| 55 | return; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 56 | } |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 57 | #endif /* INCLUDE_UNUSED */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 58 | |
| 59 | /* Reverse the chain of structures in LIST. Output the new head |
| 60 | of the chain. You should always assign the output value of this |
| 61 | function to something, or you will lose the chain. */ |
| 62 | GENERIC_LIST * |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 63 | list_reverse (list) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 64 | GENERIC_LIST *list; |
| 65 | { |
| 66 | register GENERIC_LIST *next, *prev; |
| 67 | |
| 68 | for (prev = (GENERIC_LIST *)NULL; list; ) |
| 69 | { |
| 70 | next = list->next; |
| 71 | list->next = prev; |
| 72 | prev = list; |
| 73 | list = next; |
| 74 | } |
| 75 | return (prev); |
| 76 | } |
| 77 | |
| 78 | /* Return the number of elements in LIST, a generic list. */ |
| 79 | int |
| 80 | list_length (list) |
| 81 | GENERIC_LIST *list; |
| 82 | { |
| 83 | register int i; |
| 84 | |
| 85 | for (i = 0; list; list = list->next, i++); |
| 86 | return (i); |
| 87 | } |
| 88 | |
| 89 | /* Append TAIL to HEAD. Return the header of the list. */ |
| 90 | GENERIC_LIST * |
| 91 | list_append (head, tail) |
| 92 | GENERIC_LIST *head, *tail; |
| 93 | { |
| 94 | register GENERIC_LIST *t_head; |
| 95 | |
| 96 | if (head == 0) |
| 97 | return (tail); |
| 98 | |
| 99 | for (t_head = head; t_head->next; t_head = t_head->next) |
| 100 | ; |
| 101 | t_head->next = tail; |
| 102 | return (head); |
| 103 | } |
| 104 | |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 105 | #ifdef INCLUDE_UNUSED |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 106 | /* Delete the element of LIST which satisfies the predicate function COMPARER. |
| 107 | Returns the element that was deleted, so you can dispose of it, or -1 if |
| 108 | the element wasn't found. COMPARER is called with the list element and |
| 109 | then ARG. Note that LIST contains the address of a variable which points |
| 110 | to the list. You might call this function like this: |
| 111 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 112 | SHELL_VAR *elt = list_remove (&variable_list, check_var_has_name, "foo"); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 113 | dispose_variable (elt); |
| 114 | */ |
| 115 | GENERIC_LIST * |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 116 | list_remove (list, comparer, arg) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 117 | GENERIC_LIST **list; |
| 118 | Function *comparer; |
| 119 | char *arg; |
| 120 | { |
| 121 | register GENERIC_LIST *prev, *temp; |
| 122 | |
| 123 | for (prev = (GENERIC_LIST *)NULL, temp = *list; temp; prev = temp, temp = temp->next) |
| 124 | { |
| 125 | if ((*comparer) (temp, arg)) |
| 126 | { |
| 127 | if (prev) |
| 128 | prev->next = temp->next; |
| 129 | else |
| 130 | *list = temp->next; |
| 131 | return (temp); |
| 132 | } |
| 133 | } |
| 134 | return ((GENERIC_LIST *)&global_error_list); |
| 135 | } |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 136 | #endif |