29937d322db055334d4a7be00b00c1842023e28b
[platform/upstream/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 (sl == 0)
71     return (sl = alloc_stringlist(n));
72
73   if (n > sl->list_size)
74     {
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;
78       sl->list_size = n;
79     }
80   return sl;
81 }
82   
83 void
84 free_stringlist (sl)
85      STRINGLIST *sl;
86 {
87   if (sl == 0)
88     return;
89   if (sl->list)
90     free_array (sl->list);
91   free (sl);
92 }
93
94 STRINGLIST *
95 copy_stringlist (sl)
96      STRINGLIST *sl;
97 {
98   STRINGLIST *new;
99   register int i;
100
101   if (sl == 0)
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. */
105   if (sl->list)
106     {
107       for (i = 0; i < sl->list_size; i++)
108         new->list[i] = STRDUP (sl->list[i]);
109     }
110   new->list_size = sl->list_size;
111   new->list_len = sl->list_len;
112   /* just being careful */
113   if (new->list)
114     new->list[new->list_len] = (char *)NULL;
115   return new;
116 }
117
118 /* Return a new STRINGLIST with everything from M1 and M2. */
119
120 STRINGLIST *
121 merge_stringlists (m1, m2)
122      STRINGLIST *m1, *m2;
123 {
124   STRINGLIST *sl;
125   int i, n, l1, l2;
126
127   l1 = m1 ? m1->list_len : 0;
128   l2 = m2 ? m2->list_len : 0;
129
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]);
135   sl->list_len = n;
136   sl->list[n] = (char *)NULL;
137   return (sl);
138 }
139
140 /* Make STRINGLIST M1 contain everything in M1 and M2. */
141 STRINGLIST *
142 append_stringlist (m1, m2)
143      STRINGLIST *m1, *m2;
144 {
145   register int i, n, len1, len2;
146
147   if (m1 == 0)
148     return (m2 ? copy_stringlist (m2) : (STRINGLIST *)0);
149
150   len1 = m1->list_len;
151   len2 = m2 ? m2->list_len : 0;
152
153   if (len2)
154     {
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;
159       m1->list_len = n;
160     }
161
162   return m1;
163 }
164
165 STRINGLIST *
166 prefix_suffix_stringlist (sl, prefix, suffix)
167      STRINGLIST *sl;
168      char *prefix, *suffix;
169 {
170   int plen, slen, tlen, llen, i;
171   char *t;
172
173   if (sl == 0 || sl->list == 0 || sl->list_len == 0)
174     return sl;
175
176   plen = STRLEN (prefix);
177   slen = STRLEN (suffix);
178
179   if (plen == 0 && slen == 0)
180     return (sl);
181
182   for (i = 0; i < sl->list_len; i++)
183     {
184       llen = STRLEN (sl->list[i]);
185       tlen = plen + llen + slen + 1;
186       t = (char *)xmalloc (tlen + 1);
187       if (plen)
188         strcpy (t, prefix);
189       strcpy (t + plen, sl->list[i]);
190       if (slen)
191         strcpy (t + plen + llen, suffix);
192       free (sl->list[i]);
193       sl->list[i] = t;
194     }
195
196   return (sl);   
197 }
198    
199 void
200 print_stringlist (sl, prefix)
201      STRINGLIST *sl;
202      char *prefix;
203 {
204   register int i;
205
206   if (sl == 0)
207     return;
208   for (i = 0; i < sl->list_len; i++)
209     printf ("%s%s\n", prefix ? prefix : "", sl->list[i]);
210 }
211
212 void
213 sort_stringlist (sl)
214      STRINGLIST *sl;
215 {
216   if (sl == 0 || sl->list_len == 0 || sl->list == 0)
217     return;
218   sort_char_array (sl->list);
219 }
220
221 STRINGLIST *
222 word_list_to_stringlist (list, copy, starting_index, ip)
223      WORD_LIST *list;
224      int copy, starting_index, *ip;
225 {
226   STRINGLIST *ret;
227   int slen, len;
228
229   if (list == 0)
230     {
231       if (ip)
232         *ip = 0;
233       return ((STRINGLIST *)0);
234     }
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;
239   ret->list_len = len;
240   if (ip)
241     *ip = len;
242   return ret;
243 }
244
245 WORD_LIST *
246 stringlist_to_word_list (sl, copy, starting_index)
247      STRINGLIST *sl;
248      int copy, starting_index;
249 {
250   WORD_LIST *list;
251
252   if (sl == 0 || sl->list == 0)
253     return ((WORD_LIST *)NULL);
254
255   list = argv_to_word_list (sl->list, copy, starting_index);
256   return list;
257 }