]> git.ipfire.org Git - thirdparty/git.git/blame - argv-array.h
Sync with maint
[thirdparty/git.git] / argv-array.h
CommitLineData
c1189cae
JK
1#ifndef ARGV_ARRAY_H
2#define ARGV_ARRAY_H
3
971b1f24
HW
4/**
5 * The argv-array API allows one to dynamically build and store
6 * NULL-terminated lists. An argv-array maintains the invariant that the
7 * `argv` member always points to a non-NULL array, and that the array is
8 * always NULL-terminated at the element pointed to by `argv[argc]`. This
9 * makes the result suitable for passing to functions expecting to receive
10 * argv from main().
11 *
12 * The string-list API (documented in string-list.h) is similar, but cannot be
13 * used for these purposes; instead of storing a straight string pointer,
14 * it contains an item structure with a `util` field that is not compatible
15 * with the traditional argv interface.
16 *
17 * Each `argv_array` manages its own memory. Any strings pushed into the
18 * array are duplicated, and all memory is freed by argv_array_clear().
19 */
20
fd93d2e6 21extern const char *empty_argv[];
c1189cae 22
971b1f24
HW
23/**
24 * A single array. This should be initialized by assignment from
25 * `ARGV_ARRAY_INIT`, or by calling `argv_array_init`. The `argv`
26 * member contains the actual array; the `argc` member contains the
27 * number of elements in the array, not including the terminating
28 * NULL.
29 */
c1189cae
JK
30struct argv_array {
31 const char **argv;
32 int argc;
33 int alloc;
34};
35
36#define ARGV_ARRAY_INIT { empty_argv, 0, 0 }
37
971b1f24
HW
38/**
39 * Initialize an array. This is no different than assigning from
40 * `ARGV_ARRAY_INIT`.
41 */
c1189cae 42void argv_array_init(struct argv_array *);
971b1f24
HW
43
44/* Push a copy of a string onto the end of the array. */
342c513a 45const char *argv_array_push(struct argv_array *, const char *);
971b1f24
HW
46
47/**
48 * Format a string and push it onto the end of the array. This is a
49 * convenience wrapper combining `strbuf_addf` and `argv_array_push`.
50 */
c1189cae 51__attribute__((format (printf,2,3)))
342c513a 52const char *argv_array_pushf(struct argv_array *, const char *fmt, ...);
971b1f24
HW
53
54/**
55 * Push a list of strings onto the end of the array. The arguments
56 * should be a list of `const char *` strings, terminated by a NULL
57 * argument.
58 */
9fe3edc4 59LAST_ARG_MUST_BE_NULL
d15bbe13 60void argv_array_pushl(struct argv_array *, ...);
971b1f24
HW
61
62/* Push a null-terminated array of strings onto the end of the array. */
85b34324 63void argv_array_pushv(struct argv_array *, const char **);
971b1f24
HW
64
65/**
66 * Remove the final element from the array. If there are no
67 * elements in the array, do nothing.
68 */
fe4a0a28 69void argv_array_pop(struct argv_array *);
971b1f24 70
c5aa6db6
JS
71/* Splits by whitespace; does not handle quoted arguments! */
72void argv_array_split(struct argv_array *, const char *);
971b1f24
HW
73
74/**
75 * Free all memory associated with the array and return it to the
76 * initial, empty state.
77 */
c1189cae 78void argv_array_clear(struct argv_array *);
971b1f24
HW
79
80/**
81 * Disconnect the `argv` member from the `argv_array` struct and
82 * return it. The caller is responsible for freeing the memory used
83 * by the array, and by the strings it references. After detaching,
84 * the `argv_array` is in a reinitialized state and can be pushed
85 * into again.
86 */
b992657e 87const char **argv_array_detach(struct argv_array *);
c1189cae
JK
88
89#endif /* ARGV_ARRAY_H */