1 /* stringvec.c - functions for managing arrays of strings. */
3 /* Copyright (C) 2000-2002 Free Software Foundation, Inc.
5 This file is part of GNU Bash, the Bourne Again SHell.
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.
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.
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/>.
23 #include <bashtypes.h>
25 #if defined (HAVE_UNISTD_H)
31 #include <chartypes.h>
35 /* Allocate an array of strings with room for N members. */
40 return ((char **)xmalloc ((n) * sizeof (char *)));
44 strvec_resize (array, nsize)
48 return ((char **)xrealloc (array, nsize * sizeof (char *)));
51 /* Return the length of ARRAY, a NULL terminated array of char *. */
58 for (i = 0; array[i]; i++);
62 /* Free the contents of ARRAY, a NULL terminated array of char *. */
72 for (i = 0; array[i]; i++)
77 strvec_dispose (array)
88 strvec_remove (array, name)
97 for (i = 0; array[i]; i++)
98 if (STREQ (name, array[i]))
101 for (j = i; array[j]; j++)
102 array[j] = array[j + 1];
109 #ifdef INCLUDE_UNUSED
110 /* Find NAME in ARRAY. Return the index of NAME, or -1 if not present.
111 ARRAY should be NULL terminated. */
113 strvec_search (array, name)
118 for (i = 0; array[i]; i++)
119 if (STREQ (name, array[i]))
126 /* Allocate and return a new copy of ARRAY and its contents. */
135 len = strvec_len (array);
137 ret = (char **)xmalloc ((len + 1) * sizeof (char *));
138 for (i = 0; array[i]; i++)
139 ret[i] = savestring (array[i]);
140 ret[i] = (char *)NULL;
145 /* Comparison routine for use with qsort() on arrays of strings. Uses
146 strcoll(3) if available, otherwise it uses strcmp(3). */
148 strvec_strcmp (s1, s2)
149 register char **s1, **s2;
151 #if defined (HAVE_STRCOLL)
152 return (strcoll (*s1, *s2));
153 #else /* !HAVE_STRCOLL */
156 if ((result = **s1 - **s2) == 0)
157 result = strcmp (*s1, *s2);
160 #endif /* !HAVE_STRCOLL */
163 /* Sort ARRAY, a null terminated array of pointers to strings. */
168 qsort (array, strvec_len (array), sizeof (char *), (QSFUNC *)strvec_strcmp);
171 /* Cons up a new array of words. The words are taken from LIST,
172 which is a WORD_LIST *. If ALLOC is true, everything is malloc'ed,
173 so you should free everything in this array when you are done.
174 The array is NULL terminated. If IP is non-null, it gets the
175 number of words in the returned array. STARTING_INDEX says where
176 to start filling in the returned array; it can be used to reserve
177 space at the beginning of the array. */
180 strvec_from_word_list (list, alloc, starting_index, ip)
182 int alloc, starting_index, *ip;
187 count = list_length (list);
188 array = (char **)xmalloc ((1 + count + starting_index) * sizeof (char *));
190 for (count = 0; count < starting_index; count++)
191 array[count] = (char *)NULL;
192 for (count = starting_index; list; count++, list = list->next)
193 array[count] = alloc ? savestring (list->word->word) : list->word->word;
194 array[count] = (char *)NULL;
201 /* Convert an array of strings into the form used internally by the shell.
202 ALLOC means to allocate new storage for each WORD_DESC in the returned
203 list rather than copy the values in ARRAY. STARTING_INDEX says where
204 in ARRAY to begin. */
207 strvec_to_word_list (array, alloc, starting_index)
209 int alloc, starting_index;
215 if (array == 0 || array[0] == 0)
216 return (WORD_LIST *)NULL;
218 for (count = 0; array[count]; count++)
221 for (i = starting_index, list = (WORD_LIST *)NULL; i < count; i++)
223 w = make_bare_word (alloc ? array[i] : "");
229 list = make_word_list (w, list);
231 return (REVERSE_LIST (list, WORD_LIST *));