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)
70 if (n > sl->list_size)
72 sl->list = (char **)xrealloc (sl->list, (n+1) * sizeof (char *));
73 for (i = sl->list_size; i <= n; i++)
74 sl->list[i] = (char *)NULL;
87 free_array (sl->list);
98 new = alloc_stringlist (sl->list_size);
99 /* I'd like to use copy_array, but that doesn't copy everything. */
102 for (i = 0; i < sl->list_size; i++)
103 new->list[i] = STRDUP (sl->list[i]);
105 new->list_size = sl->list_size;
106 new->list_len = sl->list_len;
107 /* just being careful */
109 new->list[new->list_len] = (char *)NULL;
113 /* Return a new STRINGLIST with everything from M1 and M2. */
116 merge_stringlists (m1, m2)
122 l1 = m1 ? m1->list_len : 0;
123 l2 = m2 ? m2->list_len : 0;
125 sl = alloc_stringlist (l1 + l2 + 1);
126 for (i = n = 0; i < l1; i++, n++)
127 sl->list[n] = STRDUP (m1->list[i]);
128 for (i = 0; i < l2; i++, n++)
129 sl->list[n] = STRDUP (m2->list[i]);
131 sl->list[n] = (char *)NULL;
134 /* Make STRINGLIST M1 contain everything in M1 and M2. */
136 append_stringlist (m1, m2)
139 register int i, n, len1, len2;
143 m1 = copy_stringlist (m2);
148 len2 = m2 ? m2->list_len : 0;
152 m1 = realloc_stringlist (m1, len1 + len2 + 1);
153 for (i = 0, n = len1; i < len2; i++, n++)
154 m1->list[n] = STRDUP (m2->list[i]);
155 m1->list[n] = (char *)NULL;
163 prefix_suffix_stringlist (sl, prefix, suffix)
165 char *prefix, *suffix;
167 int plen, slen, tlen, llen, i;
170 if (sl == 0 || sl->list == 0 || sl->list_len == 0)
173 plen = STRLEN (prefix);
174 slen = STRLEN (suffix);
176 if (plen == 0 && slen == 0)
179 for (i = 0; i < sl->list_len; i++)
181 llen = STRLEN (sl->list[i]);
182 tlen = plen + llen + slen + 1;
183 t = xmalloc (tlen + 1);
186 strcpy (t + plen, sl->list[i]);
188 strcpy (t + plen + llen, suffix);
197 print_stringlist (sl, prefix)
205 for (i = 0; i < sl->list_len; i++)
206 printf ("%s%s\n", prefix ? prefix : "", sl->list[i]);
213 if (sl == 0 || sl->list_len == 0 || sl->list == 0)
215 sort_char_array (sl->list);
219 word_list_to_stringlist (list, copy, starting_index, ip)
221 int copy, starting_index, *ip;
226 slen = list_length (list);
227 ret = (STRINGLIST *)xmalloc (sizeof (STRINGLIST));
228 ret->list = word_list_to_argv (list, copy, starting_index, &len);
229 ret->list_size = slen + starting_index;
237 stringlist_to_word_list (sl, copy, starting_index)
239 int copy, starting_index;
243 if (sl == 0 || sl->list == 0)
244 return ((WORD_LIST *)NULL);
246 list = argv_to_word_list (sl->list, copy, starting_index);