1 /* stringlist.c - functions to handle a generic `list of strings' structure */
3 /* Copyright (C) 2000 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 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
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
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. */
23 #if defined (HAVE_UNISTD_H)
35 #define STRDUP(x) ((x) ? savestring (x) : (char *)NULL)
37 /* Allocate a new STRINGLIST, with room for N strings. */
46 ret = (STRINGLIST *)xmalloc (sizeof (STRINGLIST));
49 ret->list = alloc_array (n+1);
51 for (i = 0; i < n; i++)
52 ret->list[i] = (char *)NULL;
56 ret->list = (char **)NULL;
64 realloc_stringlist (sl, n)
71 return (sl = alloc_stringlist(n));
73 if (n > sl->list_size)
75 sl->list = (char **)xrealloc (sl->list, (n+1) * sizeof (char *));
76 for (i = sl->list_size; i <= n; i++)
77 sl->list[i] = (char *)NULL;
90 free_array (sl->list);
102 return ((STRINGLIST *)0);
103 new = alloc_stringlist (sl->list_size);
104 /* I'd like to use copy_array, but that doesn't copy everything. */
107 for (i = 0; i < sl->list_size; i++)
108 new->list[i] = STRDUP (sl->list[i]);
110 new->list_size = sl->list_size;
111 new->list_len = sl->list_len;
112 /* just being careful */
114 new->list[new->list_len] = (char *)NULL;
118 /* Return a new STRINGLIST with everything from M1 and M2. */
121 merge_stringlists (m1, m2)
127 l1 = m1 ? m1->list_len : 0;
128 l2 = m2 ? m2->list_len : 0;
130 sl = alloc_stringlist (l1 + l2 + 1);
131 for (i = n = 0; i < l1; i++, n++)
132 sl->list[n] = STRDUP (m1->list[i]);
133 for (i = 0; i < l2; i++, n++)
134 sl->list[n] = STRDUP (m2->list[i]);
136 sl->list[n] = (char *)NULL;
140 /* Make STRINGLIST M1 contain everything in M1 and M2. */
142 append_stringlist (m1, m2)
145 register int i, n, len1, len2;
148 return (m2 ? copy_stringlist (m2) : (STRINGLIST *)0);
151 len2 = m2 ? m2->list_len : 0;
155 m1 = realloc_stringlist (m1, len1 + len2 + 1);
156 for (i = 0, n = len1; i < len2; i++, n++)
157 m1->list[n] = STRDUP (m2->list[i]);
158 m1->list[n] = (char *)NULL;
166 prefix_suffix_stringlist (sl, prefix, suffix)
168 char *prefix, *suffix;
170 int plen, slen, tlen, llen, i;
173 if (sl == 0 || sl->list == 0 || sl->list_len == 0)
176 plen = STRLEN (prefix);
177 slen = STRLEN (suffix);
179 if (plen == 0 && slen == 0)
182 for (i = 0; i < sl->list_len; i++)
184 llen = STRLEN (sl->list[i]);
185 tlen = plen + llen + slen + 1;
186 t = (char *)xmalloc (tlen + 1);
189 strcpy (t + plen, sl->list[i]);
191 strcpy (t + plen + llen, suffix);
200 print_stringlist (sl, prefix)
208 for (i = 0; i < sl->list_len; i++)
209 printf ("%s%s\n", prefix ? prefix : "", sl->list[i]);
216 if (sl == 0 || sl->list_len == 0 || sl->list == 0)
218 sort_char_array (sl->list);
222 word_list_to_stringlist (list, copy, starting_index, ip)
224 int copy, starting_index, *ip;
233 return ((STRINGLIST *)0);
235 slen = list_length (list);
236 ret = (STRINGLIST *)xmalloc (sizeof (STRINGLIST));
237 ret->list = word_list_to_argv (list, copy, starting_index, &len);
238 ret->list_size = slen + starting_index;
246 stringlist_to_word_list (sl, copy, starting_index)
248 int copy, starting_index;
252 if (sl == 0 || sl->list == 0)
253 return ((WORD_LIST *)NULL);
255 list = argv_to_word_list (sl->list, copy, starting_index);