2 #include "argv-array.h"
5 const char *empty_argv[] = { NULL };
7 void argv_array_init(struct argv_array *array)
9 array->argv = empty_argv;
14 static void argv_array_push_nodup(struct argv_array *array, const char *value)
16 if (array->argv == empty_argv)
19 ALLOC_GROW(array->argv, array->argc + 2, array->alloc);
20 array->argv[array->argc++] = value;
21 array->argv[array->argc] = NULL;
24 const char *argv_array_push(struct argv_array *array, const char *value)
26 argv_array_push_nodup(array, xstrdup(value));
27 return array->argv[array->argc - 1];
30 const char *argv_array_pushf(struct argv_array *array, const char *fmt, ...)
33 struct strbuf v = STRBUF_INIT;
36 strbuf_vaddf(&v, fmt, ap);
39 argv_array_push_nodup(array, strbuf_detach(&v, NULL));
40 return array->argv[array->argc - 1];
43 void argv_array_pushl(struct argv_array *array, ...)
49 while((arg = va_arg(ap, const char *)))
50 argv_array_push(array, arg);
54 void argv_array_pushv(struct argv_array *array, const char **argv)
57 argv_array_push(array, *argv);
60 void argv_array_pop(struct argv_array *array)
64 free((char *)array->argv[array->argc - 1]);
65 array->argv[array->argc - 1] = NULL;
69 void argv_array_split(struct argv_array *array, const char *to_split)
71 while (isspace(*to_split))
74 const char *p = to_split;
79 while (*p && !isspace(*p))
81 argv_array_push_nodup(array, xstrndup(to_split, p - to_split));
89 void argv_array_clear(struct argv_array *array)
91 if (array->argv != empty_argv) {
93 for (i = 0; i < array->argc; i++)
94 free((char *)array->argv[i]);
97 argv_array_init(array);
100 const char **argv_array_detach(struct argv_array *array)
102 if (array->argv == empty_argv)
103 return xcalloc(1, sizeof(const char *));
105 const char **ret = array->argv;
106 argv_array_init(array);