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