improve of cmpl.
[bush.git] / lib / sh / stringvec.c
blob3b33b377505c5fa8dcbcbe459ebf0d9c6981ee9e
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 Bush, the Bourne Again SHell.
7 Bush 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 Bush 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 Bush. If not, see <http://www.gnu.org/licenses/>.
21 #include <config.h>
23 #include <bushtypes.h>
25 #if defined (HAVE_UNISTD_H)
26 # include <unistd.h>
27 #endif
29 #include <bushansi.h>
30 #include <stdio.h>
31 #include <chartypes.h>
33 #include "shell.h"
35 /* Allocate an array of strings with room for N members. */
36 char **
37 strvec_create (n)
38 int n;
40 return ((char **)xmalloc ((n) * sizeof (char *)));
43 /* Allocate an array of strings with room for N members. */
44 char **
45 strvec_mcreate (n)
46 int n;
48 return ((char **)malloc ((n) * sizeof (char *)));
51 char **
52 strvec_resize (array, nsize)
53 char **array;
54 int nsize;
56 return ((char **)xrealloc (array, nsize * sizeof (char *)));
59 char **
60 strvec_mresize (array, nsize)
61 char **array;
62 int nsize;
64 return ((char **)realloc (array, nsize * sizeof (char *)));
67 /* Return the length of ARRAY, a NULL terminated array of char *. */
68 int
69 strvec_len (array)
70 char **array;
72 register int i;
74 for (i = 0; array[i]; i++);
75 return (i);
78 /* Free the contents of ARRAY, a NULL terminated array of char *. */
79 void
80 strvec_flush (array)
81 char **array;
83 register int i;
85 if (array == 0)
86 return;
88 for (i = 0; array[i]; i++)
89 free (array[i]);
92 void
93 strvec_dispose (array)
94 char **array;
96 if (array == 0)
97 return;
99 strvec_flush (array);
100 free (array);
104 strvec_remove (array, name)
105 char **array, *name;
107 register int i, j;
108 char *x;
110 if (array == 0)
111 return 0;
113 for (i = 0; array[i]; i++)
114 if (STREQ (name, array[i]))
116 x = array[i];
117 for (j = i; array[j]; j++)
118 array[j] = array[j + 1];
119 free (x);
120 return 1;
122 return 0;
125 /* Find NAME in ARRAY. Return the index of NAME, or -1 if not present.
126 ARRAY should be NULL terminated. */
128 strvec_search (array, name)
129 char **array, *name;
131 int i;
133 for (i = 0; array[i]; i++)
134 if (STREQ (name, array[i]))
135 return (i);
137 return (-1);
140 /* Allocate and return a new copy of ARRAY and its contents. */
141 char **
142 strvec_copy (array)
143 char **array;
145 register int i;
146 int len;
147 char **ret;
149 len = strvec_len (array);
151 ret = (char **)xmalloc ((len + 1) * sizeof (char *));
152 for (i = 0; array[i]; i++)
153 ret[i] = savestring (array[i]);
154 ret[i] = (char *)NULL;
156 return (ret);
159 /* Comparison routine for use by qsort that conforms to the new Posix
160 requirements (http://austingroupbugs.net/view.php?id=1070).
162 Perform a bytewise comparison if *S1 and *S2 collate equally. */
164 strvec_posixcmp (s1, s2)
165 register char **s1, **s2;
167 int result;
169 #if defined (HAVE_STRCOLL)
170 result = strcoll (*s1, *s2);
171 if (result != 0)
172 return result;
173 #endif
175 if ((result = **s1 - **s2) == 0)
176 result = strcmp (*s1, *s2);
178 return (result);
181 /* Comparison routine for use with qsort() on arrays of strings. Uses
182 strcoll(3) if available, otherwise it uses strcmp(3). */
184 strvec_strcmp (s1, s2)
185 register char **s1, **s2;
187 #if defined (HAVE_STRCOLL)
188 return (strcoll (*s1, *s2));
189 #else /* !HAVE_STRCOLL */
190 int result;
192 if ((result = **s1 - **s2) == 0)
193 result = strcmp (*s1, *s2);
195 return (result);
196 #endif /* !HAVE_STRCOLL */
199 /* Sort ARRAY, a null terminated array of pointers to strings. */
200 void
201 strvec_sort (array, posix)
202 char **array;
203 int posix;
205 if (posix)
206 qsort (array, strvec_len (array), sizeof (char *), (QSFUNC *)strvec_posixcmp);
207 else
208 qsort (array, strvec_len (array), sizeof (char *), (QSFUNC *)strvec_strcmp);
211 /* Cons up a new array of words. The words are taken from LIST,
212 which is a WORD_LIST *. If ALLOC is true, everything is malloc'ed,
213 so you should free everything in this array when you are done.
214 The array is NULL terminated. If IP is non-null, it gets the
215 number of words in the returned array. STARTING_INDEX says where
216 to start filling in the returned array; it can be used to reserve
217 space at the beginning of the array. */
219 char **
220 strvec_from_word_list (list, alloc, starting_index, ip)
221 WORD_LIST *list;
222 int alloc, starting_index, *ip;
224 int count;
225 char **array;
227 count = list_length (list);
228 array = (char **)xmalloc ((1 + count + starting_index) * sizeof (char *));
230 for (count = 0; count < starting_index; count++)
231 array[count] = (char *)NULL;
232 for (count = starting_index; list; count++, list = list->next)
233 array[count] = alloc ? savestring (list->word->word) : list->word->word;
234 array[count] = (char *)NULL;
236 if (ip)
237 *ip = count;
238 return (array);
241 /* Convert an array of strings into the form used internally by the shell.
242 ALLOC means to allocate new storage for each WORD_DESC in the returned
243 list rather than copy the values in ARRAY. STARTING_INDEX says where
244 in ARRAY to begin. */
246 WORD_LIST *
247 strvec_to_word_list (array, alloc, starting_index)
248 char **array;
249 int alloc, starting_index;
251 WORD_LIST *list;
252 WORD_DESC *w;
253 int i, count;
255 if (array == 0 || array[0] == 0)
256 return (WORD_LIST *)NULL;
258 for (count = 0; array[count]; count++)
261 for (i = starting_index, list = (WORD_LIST *)NULL; i < count; i++)
263 w = make_bare_word (alloc ? array[i] : "");
264 if (alloc == 0)
266 free (w->word);
267 w->word = array[i];
269 list = make_word_list (w, list);
271 return (REVERSE_LIST (list, WORD_LIST *));