summaryrefslogtreecommitdiffstats
path: root/lib/util/util_strlist.c
blob: d542e6f74f5eba2d3cdb07e94bf227fbd1363c3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
/* 
   Unix SMB/CIFS implementation.
   
   Copyright (C) Andrew Tridgell 2005
   Copyright (C) Jelmer Vernooij 2005
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "includes.h"
#include "system/locale.h"
#include "lib/util/tsort.h"

#undef strcasecmp

/**
 * @file
 * @brief String list manipulation
 */

/**
  build an empty (only NULL terminated) list of strings (for expansion with str_list_add() etc)
*/
_PUBLIC_ char **str_list_make_empty(TALLOC_CTX *mem_ctx)
{
	char **ret = NULL;

	ret = talloc_array(mem_ctx, char *, 1);
	if (ret == NULL) {
		return NULL;
	}

	ret[0] = NULL;

	return ret;
}

/**
  place the only element 'entry' into a new, NULL terminated string list
*/
_PUBLIC_ char **str_list_make_single(TALLOC_CTX *mem_ctx, const char *entry)
{
	char **ret = NULL;

	ret = talloc_array(mem_ctx, char *, 2);
	if (ret == NULL) {
		return NULL;
	}

	ret[0] = talloc_strdup(ret, entry);
	if (!ret[0]) {
		talloc_free(ret);
		return NULL;
	}
	ret[1] = NULL;

	return ret;
}

/**
  build a null terminated list of strings from a input string and a
  separator list. The separator list must contain characters less than
  or equal to 0x2f for this to work correctly on multi-byte strings
*/
_PUBLIC_ char **str_list_make(TALLOC_CTX *mem_ctx, const char *string, const char *sep)
{
	int num_elements = 0;
	char **ret = NULL;

	if (sep == NULL) {
		sep = LIST_SEP;
	}

	ret = talloc_array(mem_ctx, char *, 1);
	if (ret == NULL) {
		return NULL;
	}

	while (string && *string) {
		size_t len = strcspn(string, sep);
		char **ret2;
		
		if (len == 0) {
			string += strspn(string, sep);
			continue;
		}

		ret2 = talloc_realloc(mem_ctx, ret, char *,
			num_elements+2);
		if (ret2 == NULL) {
			talloc_free(ret);
			return NULL;
		}
		ret = ret2;

		ret[num_elements] = talloc_strndup(ret, string, len);
		if (ret[num_elements] == NULL) {
			talloc_free(ret);
			return NULL;
		}

		num_elements++;
		string += len;
	}

	ret[num_elements] = NULL;

	return ret;
}

/**
 * build a null terminated list of strings from an argv-like input string 
 * Entries are separated by spaces and can be enclosed by quotes.
 * Does NOT support escaping
 */
_PUBLIC_ char **str_list_make_shell(TALLOC_CTX *mem_ctx, const char *string, const char *sep)
{
	int num_elements = 0;
	char **ret = NULL;

	ret = talloc_array(mem_ctx, char *, 1);
	if (ret == NULL) {
		return NULL;
	}

	if (sep == NULL)
		sep = " \t\n\r";

	while (string && *string) {
		size_t len = strcspn(string, sep);
		char *element;
		char **ret2;
		
		if (len == 0) {
			string += strspn(string, sep);
			continue;
		}

		if (*string == '\"') {
			string++;
			len = strcspn(string, "\"");
			element = talloc_strndup(ret, string, len);
			string += len + 1;
		} else {
			element = talloc_strndup(ret, string, len);
			string += len;
		}

		if (element == NULL) {
			talloc_free(ret);
			return NULL;
		}

		ret2 = talloc_realloc(mem_ctx, ret, char *, num_elements+2);
		if (ret2 == NULL) {
			talloc_free(ret);
			return NULL;
		}
		ret = ret2;

		ret[num_elements] = element;	

		num_elements++;
	}

	ret[num_elements] = NULL;

	return ret;

}

/**
 * join a list back to one string 
 */
_PUBLIC_ char *str_list_join(TALLOC_CTX *mem_ctx, const char **list, char separator)
{
	char *ret = NULL;
	int i;
	
	if (list[0] == NULL)
		return talloc_strdup(mem_ctx, "");

	ret = talloc_strdup(mem_ctx, list[0]);

	for (i = 1; list[i]; i++) {
		ret = talloc_asprintf_append_buffer(ret, "%c%s", separator, list[i]);
	}

	return ret;
}

/** join a list back to one (shell-like) string; entries 
 * separated by spaces, using quotes where necessary */
_PUBLIC_ char *str_list_join_shell(TALLOC_CTX *mem_ctx, const char **list, char sep)
{
	char *ret = NULL;
	int i;
	
	if (list[0] == NULL)
		return talloc_strdup(mem_ctx, "");

	if (strchr(list[0], ' ') || strlen(list[0]) == 0) 
		ret = talloc_asprintf(mem_ctx, "\"%s\"", list[0]);
	else 
		ret = talloc_strdup(mem_ctx, list[0]);

	for (i = 1; list[i]; i++) {
		if (strchr(list[i], ' ') || strlen(list[i]) == 0) 
			ret = talloc_asprintf_append_buffer(ret, "%c\"%s\"", sep, list[i]);
		else 
			ret = talloc_asprintf_append_buffer(ret, "%c%s", sep, list[i]);
	}

	return ret;
}

/**
  return the number of elements in a string list
*/
_PUBLIC_ size_t str_list_length(const char * const *list)
{
	size_t ret;
	for (ret=0;list && list[ret];ret++) /* noop */ ;
	return ret;
}


/**
  copy a string list
*/
_PUBLIC_ char **str_list_copy(TALLOC_CTX *mem_ctx, const char **list)
{
	int i;
	char **ret;

	if (list == NULL)
		return NULL;
	
	ret = talloc_array(mem_ctx, char *, str_list_length(list)+1);
	if (ret == NULL) 
		return NULL;

	for (i=0;list && list[i];i++) {
		ret[i] = talloc_strdup(ret, list[i]);
		if (ret[i] == NULL) {
			talloc_free(ret);
			return NULL;
		}
	}
	ret[i] = NULL;
	return ret;
}

/**
   Return true if all the elements of the list match exactly.
 */
_PUBLIC_ bool str_list_equal(const char * const *list1,
			     const char * const *list2)
{
	int i;
	
	if (list1 == NULL || list2 == NULL) {
		return (list1 == list2); 
	}
	
	for (i=0;list1[i] && list2[i];i++) {
		if (strcmp(list1[i], list2[i]) != 0) {
			return false;
		}
	}
	if (list1[i] || list2[i]) {
		return false;
	}
	return true;
}


/**
  add an entry to a string list
*/
_PUBLIC_ const char **str_list_add(const char **list, const char *s)
{
	size_t len = str_list_length(list);
	const char **ret;

	ret = talloc_realloc(NULL, list, const char *, len+2);
	if (ret == NULL) return NULL;

	ret[len] = talloc_strdup(ret, s);
	if (ret[len] == NULL) return NULL;

	ret[len+1] = NULL;

	return ret;
}

/**
  remove an entry from a string list
*/
_PUBLIC_ void str_list_remove(const char **list, const char *s)
{
	int i;

	for (i=0;list[i];i++) {
		if (strcmp(list[i], s) == 0) break;
	}
	if (!list[i]) return;

	for (;list[i];i++) {
		list[i] = list[i+1];
	}
}


/**
  return true if a string is in a list
*/
_PUBLIC_ bool str_list_check(const char **list, const char *s)
{
	int i;

	for (i=0;list[i];i++) {
		if (strcmp(list[i], s) == 0) return true;
	}
	return false;
}

/**
  return true if a string is in a list, case insensitively
*/
_PUBLIC_ bool str_list_check_ci(const char **list, const char *s)
{
	int i;

	for (i=0;list[i];i++) {
		if (strcasecmp(list[i], s) == 0) return true;
	}
	return false;
}


/**
  append one list to another - expanding list1
*/
_PUBLIC_ const char **str_list_append(const char **list1,
	const char * const *list2)
{
	size_t len1 = str_list_length(list1);
	size_t len2 = str_list_length(list2);
	const char **ret;
	int i;

	ret = talloc_realloc(NULL, list1, const char *, len1+len2+1);
	if (ret == NULL) return NULL;

	for (i=len1;i<len1+len2;i++) {
		ret[i] = talloc_strdup(ret, list2[i-len1]);
		if (ret[i] == NULL) {
			return NULL;
		}
	}
	ret[i] = NULL;

	return ret;
}

static int list_cmp(const char **el1, const char **el2)
{
	return strcmp(*el1, *el2);
}

/*
  return a list that only contains the unique elements of a list,
  removing any duplicates
 */
_PUBLIC_ const char **str_list_unique(const char **list)
{
	size_t len = str_list_length(list);
	const char **list2;
	int i, j;
	if (len < 2) {
		return list;
	}
	list2 = (const char **)talloc_memdup(list, list,
					     sizeof(list[0])*(len+1));
	TYPESAFE_QSORT(list2, len, list_cmp);
	list[0] = list2[0];
	for (i=j=1;i<len;i++) {
		if (strcmp(list2[i], list[j-1]) != 0) {
			list[j] = list2[i];
			j++;
		}
	}
	list[j] = NULL;
	list = talloc_realloc(NULL, list, const char *, j + 1);
	talloc_free(list2);
	return list;
}

/*
  very useful when debugging complex list related code
 */
_PUBLIC_ void str_list_show(const char **list)
{
	int i;
	DEBUG(0,("{ "));
	for (i=0;list && list[i];i++) {
		DEBUG(0,("\"%s\", ", list[i]));
	}
	DEBUG(0,("}\n"));
}



/**
  append one list to another - expanding list1
  this assumes the elements of list2 are const pointers, so we can re-use them
*/
_PUBLIC_ const char **str_list_append_const(const char **list1,
					    const char **list2)
{
	size_t len1 = str_list_length(list1);
	size_t len2 = str_list_length(list2);
	const char **ret;
	int i;

	ret = talloc_realloc(NULL, list1, const char *, len1+len2+1);
	if (ret == NULL) return NULL;

	for (i=len1;i<len1+len2;i++) {
		ret[i] = list2[i-len1];
	}
	ret[i] = NULL;

	return ret;
}

/**
 * Add a string to an array of strings.
 *
 * num should be a pointer to an integer that holds the current
 * number of elements in strings. It will be updated by this function.
 */
_PUBLIC_ bool add_string_to_array(TALLOC_CTX *mem_ctx,
			 const char *str, const char ***strings, int *num)
{
	char *dup_str = talloc_strdup(mem_ctx, str);

	*strings = talloc_realloc(mem_ctx,
				    *strings,
				    const char *, ((*num)+1));

	if ((*strings == NULL) || (dup_str == NULL)) {
		*num = 0;
		return false;
	}

	(*strings)[*num] = dup_str;
	*num += 1;

	return true;
}

/**
  add an entry to a string list
  this assumes s will not change
*/
_PUBLIC_ const char **str_list_add_const(const char **list, const char *s)
{
	size_t len = str_list_length(list);
	const char **ret;

	ret = talloc_realloc(NULL, list, const char *, len+2);
	if (ret == NULL) return NULL;

	ret[len] = s;
	ret[len+1] = NULL;

	return ret;
}

/**
  copy a string list
  this assumes list will not change
*/
_PUBLIC_ const char **str_list_copy_const(TALLOC_CTX *mem_ctx,
					  const char **list)
{
	int i;
	const char **ret;

	if (list == NULL)
		return NULL;
	
	ret = talloc_array(mem_ctx, const char *, str_list_length(list)+1);
	if (ret == NULL) 
		return NULL;

	for (i=0;list && list[i];i++) {
		ret[i] = list[i];
	}
	ret[i] = NULL;
	return ret;
}

/**
 * Needed for making an "unconst" list "const"
 */
_PUBLIC_ const char **const_str_list(char **list)
{
	return discard_const_p(const char *, list);
}

/**
 * str_list_make, v3 version. The v4 version does not
 * look at quoted strings with embedded blanks, so
 * do NOT merge this function please!
 */
#define S_LIST_ABS 16 /* List Allocation Block Size */

char **str_list_make_v3(TALLOC_CTX *mem_ctx, const char *string,
	const char *sep)
{
	char **list;
	const char *str;
	char *s, *tok;
	int num, lsize;

	if (!string || !*string)
		return NULL;

	list = talloc_array(mem_ctx, char *, S_LIST_ABS+1);
	if (list == NULL) {
		return NULL;
	}
	lsize = S_LIST_ABS;

	s = talloc_strdup(list, string);
	if (s == NULL) {
		DEBUG(0,("str_list_make: Unable to allocate memory"));
		TALLOC_FREE(list);
		return NULL;
	}
	if (!sep) sep = LIST_SEP;

	num = 0;
	str = s;

	while (next_token_talloc(list, &str, &tok, sep)) {

		if (num == lsize) {
			char **tmp;

			lsize += S_LIST_ABS;

			tmp = talloc_realloc(mem_ctx, list, char *,
						   lsize + 1);
			if (tmp == NULL) {
				DEBUG(0,("str_list_make: "
					"Unable to allocate memory"));
				TALLOC_FREE(list);
				return NULL;
			}

			list = tmp;

			memset (&list[num], 0,
				((sizeof(char*)) * (S_LIST_ABS +1)));
		}

		list[num] = tok;
		num += 1;
	}

	list[num] = NULL;

	TALLOC_FREE(s);
	return list;
}