blob: b0ed416456fa49162b63165bfaec7c341e6d7b87 [file] [log] [blame]
Jari Aalto7117c2d2002-07-17 14:10:11 +00001/* stringvec.c - functions for managing arrays of strings. */
Jari Aalto28ef6c32001-04-06 19:14:31 +00002
Jari Aalto7117c2d2002-07-17 14:10:11 +00003/* Copyright (C) 2000-2002 Free Software Foundation, Inc.
Jari Aalto28ef6c32001-04-06 19:14:31 +00004
5 This file is part of GNU Bash, the Bourne Again SHell.
6
Jari Aalto31859422009-01-12 13:36:28 +00007 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 Aalto28ef6c32001-04-06 19:14:31 +000011
Jari Aalto31859422009-01-12 13:36:28 +000012 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 Aalto28ef6c32001-04-06 19:14:31 +000016
Jari Aalto31859422009-01-12 13:36:28 +000017 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 Aalto28ef6c32001-04-06 19:14:31 +000020
Jari Aaltof73dda02001-11-13 17:56:06 +000021#include <config.h>
Jari Aalto28ef6c32001-04-06 19:14:31 +000022
Jari Aaltof73dda02001-11-13 17:56:06 +000023#include <bashtypes.h>
Jari Aalto28ef6c32001-04-06 19:14:31 +000024
25#if defined (HAVE_UNISTD_H)
26# include <unistd.h>
27#endif
28
Jari Aaltof73dda02001-11-13 17:56:06 +000029#include <bashansi.h>
Jari Aalto28ef6c32001-04-06 19:14:31 +000030#include <stdio.h>
Jari Aaltof73dda02001-11-13 17:56:06 +000031#include <chartypes.h>
Jari Aalto28ef6c32001-04-06 19:14:31 +000032
33#include "shell.h"
34
Jari Aalto28ef6c32001-04-06 19:14:31 +000035/* Allocate an array of strings with room for N members. */
36char **
Jari Aalto7117c2d2002-07-17 14:10:11 +000037strvec_create (n)
Jari Aalto28ef6c32001-04-06 19:14:31 +000038 int n;
39{
40 return ((char **)xmalloc ((n) * sizeof (char *)));
41}
42
Jari Aalto7117c2d2002-07-17 14:10:11 +000043char **
44strvec_resize (array, nsize)
45 char **array;
46 int nsize;
47{
48 return ((char **)xrealloc (array, nsize * sizeof (char *)));
49}
50
Jari Aalto28ef6c32001-04-06 19:14:31 +000051/* Return the length of ARRAY, a NULL terminated array of char *. */
52int
Jari Aalto7117c2d2002-07-17 14:10:11 +000053strvec_len (array)
Jari Aalto28ef6c32001-04-06 19:14:31 +000054 char **array;
55{
56 register int i;
57
58 for (i = 0; array[i]; i++);
59 return (i);
60}
61
62/* Free the contents of ARRAY, a NULL terminated array of char *. */
63void
Jari Aalto7117c2d2002-07-17 14:10:11 +000064strvec_flush (array)
Jari Aalto28ef6c32001-04-06 19:14:31 +000065 char **array;
66{
67 register int i;
68
69 if (array == 0)
70 return;
71
72 for (i = 0; array[i]; i++)
73 free (array[i]);
74}
75
76void
Jari Aalto7117c2d2002-07-17 14:10:11 +000077strvec_dispose (array)
Jari Aalto28ef6c32001-04-06 19:14:31 +000078 char **array;
79{
80 if (array == 0)
81 return;
82
Jari Aalto7117c2d2002-07-17 14:10:11 +000083 strvec_flush (array);
Jari Aalto28ef6c32001-04-06 19:14:31 +000084 free (array);
85}
86
Jari Aalto7117c2d2002-07-17 14:10:11 +000087int
88strvec_remove (array, name)
89 char **array, *name;
90{
91 register int i, j;
92 char *x;
93
94 if (array == 0)
95 return 0;
96
97 for (i = 0; array[i]; i++)
98 if (STREQ (name, array[i]))
99 {
100 x = array[i];
101 for (j = i; array[j]; j++)
102 array[j] = array[j + 1];
103 free (x);
104 return 1;
105 }
106 return 0;
107}
108
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. */
112int
113strvec_search (array, name)
114 char **array, *name;
115{
116 int i;
117
118 for (i = 0; array[i]; i++)
119 if (STREQ (name, array[i]))
120 return (i);
121
122 return (-1);
123}
124#endif
125
Jari Aalto28ef6c32001-04-06 19:14:31 +0000126/* Allocate and return a new copy of ARRAY and its contents. */
127char **
Jari Aalto7117c2d2002-07-17 14:10:11 +0000128strvec_copy (array)
Jari Aalto28ef6c32001-04-06 19:14:31 +0000129 char **array;
130{
131 register int i;
132 int len;
Jari Aaltof73dda02001-11-13 17:56:06 +0000133 char **ret;
Jari Aalto28ef6c32001-04-06 19:14:31 +0000134
Jari Aalto7117c2d2002-07-17 14:10:11 +0000135 len = strvec_len (array);
Jari Aalto28ef6c32001-04-06 19:14:31 +0000136
Jari Aaltof73dda02001-11-13 17:56:06 +0000137 ret = (char **)xmalloc ((len + 1) * sizeof (char *));
Jari Aalto28ef6c32001-04-06 19:14:31 +0000138 for (i = 0; array[i]; i++)
Jari Aaltof73dda02001-11-13 17:56:06 +0000139 ret[i] = savestring (array[i]);
140 ret[i] = (char *)NULL;
Jari Aalto28ef6c32001-04-06 19:14:31 +0000141
Jari Aaltof73dda02001-11-13 17:56:06 +0000142 return (ret);
Jari Aalto28ef6c32001-04-06 19:14:31 +0000143}
144
145/* Comparison routine for use with qsort() on arrays of strings. Uses
146 strcoll(3) if available, otherwise it uses strcmp(3). */
147int
Jari Aalto7117c2d2002-07-17 14:10:11 +0000148strvec_strcmp (s1, s2)
Jari Aalto28ef6c32001-04-06 19:14:31 +0000149 register char **s1, **s2;
150{
151#if defined (HAVE_STRCOLL)
152 return (strcoll (*s1, *s2));
153#else /* !HAVE_STRCOLL */
154 int result;
155
156 if ((result = **s1 - **s2) == 0)
157 result = strcmp (*s1, *s2);
158
159 return (result);
160#endif /* !HAVE_STRCOLL */
161}
162
163/* Sort ARRAY, a null terminated array of pointers to strings. */
164void
Jari Aalto7117c2d2002-07-17 14:10:11 +0000165strvec_sort (array)
Jari Aalto28ef6c32001-04-06 19:14:31 +0000166 char **array;
167{
Jari Aalto7117c2d2002-07-17 14:10:11 +0000168 qsort (array, strvec_len (array), sizeof (char *), (QSFUNC *)strvec_strcmp);
169}
170
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. */
178
179char **
180strvec_from_word_list (list, alloc, starting_index, ip)
181 WORD_LIST *list;
182 int alloc, starting_index, *ip;
183{
184 int count;
185 char **array;
186
187 count = list_length (list);
188 array = (char **)xmalloc ((1 + count + starting_index) * sizeof (char *));
189
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;
195
196 if (ip)
197 *ip = count;
198 return (array);
199}
200
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. */
205
206WORD_LIST *
207strvec_to_word_list (array, alloc, starting_index)
208 char **array;
209 int alloc, starting_index;
210{
211 WORD_LIST *list;
212 WORD_DESC *w;
213 int i, count;
214
215 if (array == 0 || array[0] == 0)
216 return (WORD_LIST *)NULL;
217
218 for (count = 0; array[count]; count++)
219 ;
220
221 for (i = starting_index, list = (WORD_LIST *)NULL; i < count; i++)
222 {
223 w = make_bare_word (alloc ? array[i] : "");
224 if (alloc == 0)
225 {
226 free (w->word);
227 w->word = array[i];
228 }
229 list = make_word_list (w, list);
230 }
231 return (REVERSE_LIST (list, WORD_LIST *));
Jari Aalto28ef6c32001-04-06 19:14:31 +0000232}