1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
|
/* stringvec.c - functions for managing arrays of strings. */
/* Copyright (C) 2000-2002 Free Software Foundation, Inc.
This file is part of GNU Bash, the Bourne Again SHell.
Bash is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Bash is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Bash. If not, see <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <bashtypes.h>
#if defined (HAVE_UNISTD_H)
# include <unistd.h>
#endif
#include <bashansi.h>
#include <stdio.h>
#include <chartypes.h>
#include "shell.h"
/* Allocate an array of strings with room for N members. */
char **
strvec_create (n)
int n;
{
return ((char **)xmalloc ((n) * sizeof (char *)));
}
/* Allocate an array of strings with room for N members. */
char **
strvec_mcreate (n)
int n;
{
return ((char **)malloc ((n) * sizeof (char *)));
}
char **
strvec_resize (array, nsize)
char **array;
int nsize;
{
return ((char **)xrealloc (array, nsize * sizeof (char *)));
}
char **
strvec_mresize (array, nsize)
char **array;
int nsize;
{
return ((char **)realloc (array, nsize * sizeof (char *)));
}
/* Return the length of ARRAY, a NULL terminated array of char *. */
int
strvec_len (array)
char **array;
{
register int i;
for (i = 0; array[i]; i++);
return (i);
}
/* Free the contents of ARRAY, a NULL terminated array of char *. */
void
strvec_flush (array)
char **array;
{
register int i;
if (array == 0)
return;
for (i = 0; array[i]; i++)
free (array[i]);
}
void
strvec_dispose (array)
char **array;
{
if (array == 0)
return;
strvec_flush (array);
free (array);
}
int
strvec_remove (array, name)
char **array, *name;
{
register int i, j;
char *x;
if (array == 0)
return 0;
for (i = 0; array[i]; i++)
if (STREQ (name, array[i]))
{
x = array[i];
for (j = i; array[j]; j++)
array[j] = array[j + 1];
free (x);
return 1;
}
return 0;
}
/* Find NAME in ARRAY. Return the index of NAME, or -1 if not present.
ARRAY should be NULL terminated. */
int
strvec_search (array, name)
char **array, *name;
{
int i;
for (i = 0; array[i]; i++)
if (STREQ (name, array[i]))
return (i);
return (-1);
}
/* Allocate and return a new copy of ARRAY and its contents. */
char **
strvec_copy (array)
char **array;
{
register int i;
int len;
char **ret;
len = strvec_len (array);
ret = (char **)xmalloc ((len + 1) * sizeof (char *));
for (i = 0; array[i]; i++)
ret[i] = savestring (array[i]);
ret[i] = (char *)NULL;
return (ret);
}
/* Comparison routine for use by qsort that conforms to the new Posix
requirements (http://austingroupbugs.net/view.php?id=1070).
Perform a bytewise comparison if *S1 and *S2 collate equally. */
int
strvec_posixcmp (s1, s2)
register char **s1, **s2;
{
int result;
#if defined (HAVE_STRCOLL)
result = strcoll (*s1, *s2);
if (result != 0)
return result;
#endif
if ((result = **s1 - **s2) == 0)
result = strcmp (*s1, *s2);
return (result);
}
/* Comparison routine for use with qsort() on arrays of strings. Uses
strcoll(3) if available, otherwise it uses strcmp(3). */
int
strvec_strcmp (s1, s2)
register char **s1, **s2;
{
#if defined (HAVE_STRCOLL)
return (strcoll (*s1, *s2));
#else /* !HAVE_STRCOLL */
int result;
if ((result = **s1 - **s2) == 0)
result = strcmp (*s1, *s2);
return (result);
#endif /* !HAVE_STRCOLL */
}
/* Sort ARRAY, a null terminated array of pointers to strings. */
void
strvec_sort (array, posix)
char **array;
int posix;
{
if (posix)
qsort (array, strvec_len (array), sizeof (char *), (QSFUNC *)strvec_posixcmp);
else
qsort (array, strvec_len (array), sizeof (char *), (QSFUNC *)strvec_strcmp);
}
/* Cons up a new array of words. The words are taken from LIST,
which is a WORD_LIST *. If ALLOC is true, everything is malloc'ed,
so you should free everything in this array when you are done.
The array is NULL terminated. If IP is non-null, it gets the
number of words in the returned array. STARTING_INDEX says where
to start filling in the returned array; it can be used to reserve
space at the beginning of the array. */
char **
strvec_from_word_list (list, alloc, starting_index, ip)
WORD_LIST *list;
int alloc, starting_index, *ip;
{
int count;
char **array;
count = list_length (list);
array = (char **)xmalloc ((1 + count + starting_index) * sizeof (char *));
for (count = 0; count < starting_index; count++)
array[count] = (char *)NULL;
for (count = starting_index; list; count++, list = list->next)
array[count] = alloc ? savestring (list->word->word) : list->word->word;
array[count] = (char *)NULL;
if (ip)
*ip = count;
return (array);
}
/* Convert an array of strings into the form used internally by the shell.
ALLOC means to allocate new storage for each WORD_DESC in the returned
list rather than copy the values in ARRAY. STARTING_INDEX says where
in ARRAY to begin. */
WORD_LIST *
strvec_to_word_list (array, alloc, starting_index)
char **array;
int alloc, starting_index;
{
WORD_LIST *list;
WORD_DESC *w;
int i, count;
if (array == 0 || array[0] == 0)
return (WORD_LIST *)NULL;
for (count = 0; array[count]; count++)
;
for (i = starting_index, list = (WORD_LIST *)NULL; i < count; i++)
{
w = make_bare_word (alloc ? array[i] : "");
if (alloc == 0)
{
free (w->word);
w->word = array[i];
}
list = make_word_list (w, list);
}
return (REVERSE_LIST (list, WORD_LIST *));
}
|