]> git.ipfire.org Git - thirdparty/bash.git/blob - lib/sh/stringlist.c
Imported from ../bash-2.05.tar.gz.
[thirdparty/bash.git] / lib / sh / stringlist.c
1 /* stringlist.c - functions to handle a generic `list of strings' structure */
2
3 /* Copyright (C) 2000 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 #if defined (HAVE_UNISTD_H)
24 # include <unistd.h>
25 #endif
26
27 #include <stdio.h>
28 #include "bashansi.h"
29
30 #include "shell.h"
31
32 #ifdef STRDUP
33 # undef STRDUP
34 #endif
35 #define STRDUP(x) ((x) ? savestring (x) : (char *)NULL)
36
37 /* Allocate a new STRINGLIST, with room for N strings. */
38
39 STRINGLIST *
40 alloc_stringlist (n)
41 int n;
42 {
43 STRINGLIST *ret;
44 register int i;
45
46 ret = (STRINGLIST *)xmalloc (sizeof (STRINGLIST));
47 if (n)
48 {
49 ret->list = alloc_array (n+1);
50 ret->list_size = n;
51 for (i = 0; i < n; i++)
52 ret->list[i] = (char *)NULL;
53 }
54 else
55 {
56 ret->list = (char **)NULL;
57 ret->list_size = 0;
58 }
59 ret->list_len = 0;
60 return ret;
61 }
62
63 STRINGLIST *
64 realloc_stringlist (sl, n)
65 STRINGLIST *sl;
66 int n;
67 {
68 register int i;
69
70 if (n > sl->list_size)
71 {
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;
75 sl->list_size = n;
76 }
77 return sl;
78 }
79
80 void
81 free_stringlist (sl)
82 STRINGLIST *sl;
83 {
84 if (sl == 0)
85 return;
86 if (sl->list)
87 free_array (sl->list);
88 free (sl);
89 }
90
91 STRINGLIST *
92 copy_stringlist (sl)
93 STRINGLIST *sl;
94 {
95 STRINGLIST *new;
96 register int i;
97
98 new = alloc_stringlist (sl->list_size);
99 /* I'd like to use copy_array, but that doesn't copy everything. */
100 if (sl->list)
101 {
102 for (i = 0; i < sl->list_size; i++)
103 new->list[i] = STRDUP (sl->list[i]);
104 }
105 new->list_size = sl->list_size;
106 new->list_len = sl->list_len;
107 /* just being careful */
108 if (new->list)
109 new->list[new->list_len] = (char *)NULL;
110 return new;
111 }
112
113 /* Return a new STRINGLIST with everything from M1 and M2. */
114
115 STRINGLIST *
116 merge_stringlists (m1, m2)
117 STRINGLIST *m1, *m2;
118 {
119 STRINGLIST *sl;
120 int i, n, l1, l2;
121
122 l1 = m1 ? m1->list_len : 0;
123 l2 = m2 ? m2->list_len : 0;
124
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]);
130 sl->list_len = n;
131 sl->list[n] = (char *)NULL;
132 }
133
134 /* Make STRINGLIST M1 contain everything in M1 and M2. */
135 STRINGLIST *
136 append_stringlist (m1, m2)
137 STRINGLIST *m1, *m2;
138 {
139 register int i, n, len1, len2;
140
141 if (m1 == 0)
142 {
143 m1 = copy_stringlist (m2);
144 return m1;
145 }
146
147 len1 = m1->list_len;
148 len2 = m2 ? m2->list_len : 0;
149
150 if (len2)
151 {
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;
156 m1->list_len = n;
157 }
158
159 return m1;
160 }
161
162 STRINGLIST *
163 prefix_suffix_stringlist (sl, prefix, suffix)
164 STRINGLIST *sl;
165 char *prefix, *suffix;
166 {
167 int plen, slen, tlen, llen, i;
168 char *t;
169
170 if (sl == 0 || sl->list == 0 || sl->list_len == 0)
171 return sl;
172
173 plen = STRLEN (prefix);
174 slen = STRLEN (suffix);
175
176 if (plen == 0 && slen == 0)
177 return (sl);
178
179 for (i = 0; i < sl->list_len; i++)
180 {
181 llen = STRLEN (sl->list[i]);
182 tlen = plen + llen + slen + 1;
183 t = xmalloc (tlen + 1);
184 if (plen)
185 strcpy (t, prefix);
186 strcpy (t + plen, sl->list[i]);
187 if (slen)
188 strcpy (t + plen + llen, suffix);
189 free (sl->list[i]);
190 sl->list[i] = t;
191 }
192
193 return (sl);
194 }
195
196 void
197 print_stringlist (sl, prefix)
198 STRINGLIST *sl;
199 char *prefix;
200 {
201 register int i;
202
203 if (sl == 0)
204 return;
205 for (i = 0; i < sl->list_len; i++)
206 printf ("%s%s\n", prefix ? prefix : "", sl->list[i]);
207 }
208
209 void
210 sort_stringlist (sl)
211 STRINGLIST *sl;
212 {
213 if (sl == 0 || sl->list_len == 0 || sl->list == 0)
214 return;
215 sort_char_array (sl->list);
216 }
217
218 STRINGLIST *
219 word_list_to_stringlist (list, copy, starting_index, ip)
220 WORD_LIST *list;
221 int copy, starting_index, *ip;
222 {
223 STRINGLIST *ret;
224 int slen, len;
225
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;
230 ret->list_len = len;
231 if (ip)
232 *ip = len;
233 return ret;
234 }
235
236 WORD_LIST *
237 stringlist_to_word_list (sl, copy, starting_index)
238 STRINGLIST *sl;
239 int copy, starting_index;
240 {
241 WORD_LIST *list;
242
243 if (sl == 0 || sl->list == 0)
244 return ((WORD_LIST *)NULL);
245
246 list = argv_to_word_list (sl->list, copy, starting_index);
247 return list;
248 }