]> git.ipfire.org Git - thirdparty/git.git/blob - argv-array.c
tests: use shorter labels in chainlint.sed for AIX sed
[thirdparty/git.git] / argv-array.c
1 #include "cache.h"
2 #include "argv-array.h"
3 #include "strbuf.h"
4
5 const char *empty_argv[] = { NULL };
6
7 void argv_array_init(struct argv_array *array)
8 {
9 array->argv = empty_argv;
10 array->argc = 0;
11 array->alloc = 0;
12 }
13
14 static void argv_array_push_nodup(struct argv_array *array, const char *value)
15 {
16 if (array->argv == empty_argv)
17 array->argv = NULL;
18
19 ALLOC_GROW(array->argv, array->argc + 2, array->alloc);
20 array->argv[array->argc++] = value;
21 array->argv[array->argc] = NULL;
22 }
23
24 const char *argv_array_push(struct argv_array *array, const char *value)
25 {
26 argv_array_push_nodup(array, xstrdup(value));
27 return array->argv[array->argc - 1];
28 }
29
30 const char *argv_array_pushf(struct argv_array *array, const char *fmt, ...)
31 {
32 va_list ap;
33 struct strbuf v = STRBUF_INIT;
34
35 va_start(ap, fmt);
36 strbuf_vaddf(&v, fmt, ap);
37 va_end(ap);
38
39 argv_array_push_nodup(array, strbuf_detach(&v, NULL));
40 return array->argv[array->argc - 1];
41 }
42
43 void argv_array_pushl(struct argv_array *array, ...)
44 {
45 va_list ap;
46 const char *arg;
47
48 va_start(ap, array);
49 while((arg = va_arg(ap, const char *)))
50 argv_array_push(array, arg);
51 va_end(ap);
52 }
53
54 void argv_array_pushv(struct argv_array *array, const char **argv)
55 {
56 for (; *argv; argv++)
57 argv_array_push(array, *argv);
58 }
59
60 void argv_array_pop(struct argv_array *array)
61 {
62 if (!array->argc)
63 return;
64 free((char *)array->argv[array->argc - 1]);
65 array->argv[array->argc - 1] = NULL;
66 array->argc--;
67 }
68
69 void argv_array_split(struct argv_array *array, const char *to_split)
70 {
71 while (isspace(*to_split))
72 to_split++;
73 for (;;) {
74 const char *p = to_split;
75
76 if (!*p)
77 break;
78
79 while (*p && !isspace(*p))
80 p++;
81 argv_array_push_nodup(array, xstrndup(to_split, p - to_split));
82
83 while (isspace(*p))
84 p++;
85 to_split = p;
86 }
87 }
88
89 void argv_array_clear(struct argv_array *array)
90 {
91 if (array->argv != empty_argv) {
92 int i;
93 for (i = 0; i < array->argc; i++)
94 free((char *)array->argv[i]);
95 free(array->argv);
96 }
97 argv_array_init(array);
98 }
99
100 const char **argv_array_detach(struct argv_array *array)
101 {
102 if (array->argv == empty_argv)
103 return xcalloc(1, sizeof(const char *));
104 else {
105 const char **ret = array->argv;
106 argv_array_init(array);
107 return ret;
108 }
109 }