| [3228] | 1 | /* stringvec.c - functions for managing arrays of strings. */
|
|---|
| 2 |
|
|---|
| 3 | /* Copyright (C) 2000-2002 Free Software Foundation, Inc.
|
|---|
| 4 |
|
|---|
| 5 | This file is part of GNU Bash, the Bourne Again SHell.
|
|---|
| 6 |
|
|---|
| 7 | Bash is free software; you can redistribute it and/or modify it under
|
|---|
| 8 | the terms of the GNU General Public License as published by the Free
|
|---|
| 9 | Software Foundation; either version 2, or (at your option) any later
|
|---|
| 10 | version.
|
|---|
| 11 |
|
|---|
| 12 | Bash is distributed in the hope that it will be useful, but WITHOUT ANY
|
|---|
| 13 | WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|---|
| 14 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|---|
| 15 | for more details.
|
|---|
| 16 |
|
|---|
| 17 | You should have received a copy of the GNU General Public License along
|
|---|
| 18 | with Bash; see the file COPYING. If not, write to the Free Software
|
|---|
| 19 | Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
|
|---|
| 20 |
|
|---|
| 21 | #include <config.h>
|
|---|
| 22 |
|
|---|
| 23 | #include <bashtypes.h>
|
|---|
| 24 |
|
|---|
| 25 | #if defined (HAVE_UNISTD_H)
|
|---|
| 26 | # include <unistd.h>
|
|---|
| 27 | #endif
|
|---|
| 28 |
|
|---|
| 29 | #include <bashansi.h>
|
|---|
| 30 | #include <stdio.h>
|
|---|
| 31 | #include <chartypes.h>
|
|---|
| 32 |
|
|---|
| 33 | #include "shell.h"
|
|---|
| 34 |
|
|---|
| 35 | /* Allocate an array of strings with room for N members. */
|
|---|
| 36 | char **
|
|---|
| 37 | strvec_create (n)
|
|---|
| 38 | int n;
|
|---|
| 39 | {
|
|---|
| 40 | return ((char **)xmalloc ((n) * sizeof (char *)));
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | char **
|
|---|
| 44 | strvec_resize (array, nsize)
|
|---|
| 45 | char **array;
|
|---|
| 46 | int nsize;
|
|---|
| 47 | {
|
|---|
| 48 | return ((char **)xrealloc (array, nsize * sizeof (char *)));
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | /* Return the length of ARRAY, a NULL terminated array of char *. */
|
|---|
| 52 | int
|
|---|
| 53 | strvec_len (array)
|
|---|
| 54 | 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 *. */
|
|---|
| 63 | void
|
|---|
| 64 | strvec_flush (array)
|
|---|
| 65 | 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 |
|
|---|
| 76 | void
|
|---|
| 77 | strvec_dispose (array)
|
|---|
| 78 | char **array;
|
|---|
| 79 | {
|
|---|
| 80 | if (array == 0)
|
|---|
| 81 | return;
|
|---|
| 82 |
|
|---|
| 83 | strvec_flush (array);
|
|---|
| 84 | free (array);
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | int
|
|---|
| 88 | strvec_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. */
|
|---|
| 112 | int
|
|---|
| 113 | strvec_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 |
|
|---|
| 126 | /* Allocate and return a new copy of ARRAY and its contents. */
|
|---|
| 127 | char **
|
|---|
| 128 | strvec_copy (array)
|
|---|
| 129 | char **array;
|
|---|
| 130 | {
|
|---|
| 131 | register int i;
|
|---|
| 132 | int len;
|
|---|
| 133 | char **ret;
|
|---|
| 134 |
|
|---|
| 135 | len = strvec_len (array);
|
|---|
| 136 |
|
|---|
| 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;
|
|---|
| 141 |
|
|---|
| 142 | return (ret);
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | /* Comparison routine for use with qsort() on arrays of strings. Uses
|
|---|
| 146 | strcoll(3) if available, otherwise it uses strcmp(3). */
|
|---|
| 147 | int
|
|---|
| 148 | strvec_strcmp (s1, s2)
|
|---|
| 149 | 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. */
|
|---|
| 164 | void
|
|---|
| 165 | strvec_sort (array)
|
|---|
| 166 | char **array;
|
|---|
| 167 | {
|
|---|
| 168 | 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 |
|
|---|
| 179 | char **
|
|---|
| 180 | strvec_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 |
|
|---|
| 206 | WORD_LIST *
|
|---|
| 207 | strvec_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 *));
|
|---|
| 232 | }
|
|---|