1 /* stringlist.c - functions to handle a generic `list of strings' structure */
3 /* Copyright (C) 2000-2019 Free Software Foundation, Inc.
5 This file is part of GNU Bush, the Bourne Again SHell.
7 Bush is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 Bush is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Bush. If not, see <http://www.gnu.org/licenses/>.
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
= strvec_create (n
+1);
51 for (i
= 0; i
< n
; i
++)
52 ret
->list
[i
] = (char *)NULL
;
56 ret
->list
= (char **)NULL
;
64 strlist_resize (sl
, n
)
71 return (sl
= strlist_create (n
));
73 if (n
> sl
->list_size
)
75 sl
->list
= strvec_resize (sl
->list
, n
+ 1);
76 for (i
= sl
->list_size
; i
<= n
; i
++)
77 sl
->list
[i
] = (char *)NULL
;
87 if (sl
== 0 || sl
->list
== 0)
89 strvec_flush (sl
->list
);
100 strvec_dispose (sl
->list
);
105 strlist_remove (sl
, s
)
111 if (sl
== 0 || sl
->list
== 0 || sl
->list_len
== 0)
114 r
= strvec_remove (sl
->list
, s
);
128 return ((STRINGLIST
*)0);
129 new = strlist_create (sl
->list_size
);
130 /* I'd like to use strvec_copy, but that doesn't copy everything. */
133 for (i
= 0; i
< sl
->list_size
; i
++)
134 new->list
[i
] = STRDUP (sl
->list
[i
]);
136 new->list_size
= sl
->list_size
;
137 new->list_len
= sl
->list_len
;
138 /* just being careful */
140 new->list
[new->list_len
] = (char *)NULL
;
144 /* Return a new STRINGLIST with everything from M1 and M2. */
147 strlist_merge (m1
, m2
)
153 l1
= m1
? m1
->list_len
: 0;
154 l2
= m2
? m2
->list_len
: 0;
156 sl
= strlist_create (l1
+ l2
+ 1);
157 for (i
= n
= 0; i
< l1
; i
++, n
++)
158 sl
->list
[n
] = STRDUP (m1
->list
[i
]);
159 for (i
= 0; i
< l2
; i
++, n
++)
160 sl
->list
[n
] = STRDUP (m2
->list
[i
]);
162 sl
->list
[n
] = (char *)NULL
;
166 /* Make STRINGLIST M1 contain everything in M1 and M2. */
168 strlist_append (m1
, m2
)
171 register int i
, n
, len1
, len2
;
174 return (m2
? strlist_copy (m2
) : (STRINGLIST
*)0);
177 len2
= m2
? m2
->list_len
: 0;
181 m1
= strlist_resize (m1
, len1
+ len2
+ 1);
182 for (i
= 0, n
= len1
; i
< len2
; i
++, n
++)
183 m1
->list
[n
] = STRDUP (m2
->list
[i
]);
184 m1
->list
[n
] = (char *)NULL
;
192 strlist_prefix_suffix (sl
, prefix
, suffix
)
194 char *prefix
, *suffix
;
196 int plen
, slen
, tlen
, llen
, i
;
199 if (sl
== 0 || sl
->list
== 0 || sl
->list_len
== 0)
202 plen
= STRLEN (prefix
);
203 slen
= STRLEN (suffix
);
205 if (plen
== 0 && slen
== 0)
208 for (i
= 0; i
< sl
->list_len
; i
++)
210 llen
= STRLEN (sl
->list
[i
]);
211 tlen
= plen
+ llen
+ slen
+ 1;
212 t
= (char *)xmalloc (tlen
+ 1);
215 strcpy (t
+ plen
, sl
->list
[i
]);
217 strcpy (t
+ plen
+ llen
, suffix
);
226 strlist_print (sl
, prefix
)
234 for (i
= 0; i
< sl
->list_len
; i
++)
235 printf ("%s%s\n", prefix
? prefix
: "", sl
->list
[i
]);
239 strlist_walk (sl
, func
)
241 sh_strlist_map_func_t
*func
;
247 for (i
= 0; i
< sl
->list_len
; i
++)
248 if ((*func
)(sl
->list
[i
]) < 0)
256 if (sl
== 0 || sl
->list_len
== 0 || sl
->list
== 0)
258 strvec_sort (sl
->list
, 0);
262 strlist_from_word_list (list
, alloc
, starting_index
, ip
)
264 int alloc
, starting_index
, *ip
;
273 return ((STRINGLIST
*)0);
275 slen
= list_length (list
);
276 ret
= (STRINGLIST
*)xmalloc (sizeof (STRINGLIST
));
277 ret
->list
= strvec_from_word_list (list
, alloc
, starting_index
, &len
);
278 ret
->list_size
= slen
+ starting_index
;
286 strlist_to_word_list (sl
, alloc
, starting_index
)
288 int alloc
, starting_index
;
292 if (sl
== 0 || sl
->list
== 0)
293 return ((WORD_LIST
*)NULL
);
295 list
= strvec_to_word_list (sl
->list
, alloc
, starting_index
);