2011-09-13 21:57:57 +00:00
|
|
|
#ifndef ARGV_ARRAY_H
|
|
|
|
#define ARGV_ARRAY_H
|
|
|
|
|
2019-11-17 21:04:52 +00:00
|
|
|
/**
|
|
|
|
* The argv-array API allows one to dynamically build and store
|
|
|
|
* NULL-terminated lists. An argv-array maintains the invariant that the
|
|
|
|
* `argv` member always points to a non-NULL array, and that the array is
|
|
|
|
* always NULL-terminated at the element pointed to by `argv[argc]`. This
|
|
|
|
* makes the result suitable for passing to functions expecting to receive
|
|
|
|
* argv from main().
|
|
|
|
*
|
|
|
|
* The string-list API (documented in string-list.h) is similar, but cannot be
|
|
|
|
* used for these purposes; instead of storing a straight string pointer,
|
|
|
|
* it contains an item structure with a `util` field that is not compatible
|
|
|
|
* with the traditional argv interface.
|
|
|
|
*
|
|
|
|
* Each `argv_array` manages its own memory. Any strings pushed into the
|
|
|
|
* array are duplicated, and all memory is freed by argv_array_clear().
|
|
|
|
*/
|
|
|
|
|
2012-04-18 21:08:49 +00:00
|
|
|
extern const char *empty_argv[];
|
2011-09-13 21:57:57 +00:00
|
|
|
|
2019-11-17 21:04:52 +00:00
|
|
|
/**
|
|
|
|
* A single array. This should be initialized by assignment from
|
|
|
|
* `ARGV_ARRAY_INIT`, or by calling `argv_array_init`. The `argv`
|
|
|
|
* member contains the actual array; the `argc` member contains the
|
|
|
|
* number of elements in the array, not including the terminating
|
|
|
|
* NULL.
|
|
|
|
*/
|
2011-09-13 21:57:57 +00:00
|
|
|
struct argv_array {
|
|
|
|
const char **argv;
|
|
|
|
int argc;
|
|
|
|
int alloc;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define ARGV_ARRAY_INIT { empty_argv, 0, 0 }
|
|
|
|
|
2019-11-17 21:04:52 +00:00
|
|
|
/**
|
|
|
|
* Initialize an array. This is no different than assigning from
|
|
|
|
* `ARGV_ARRAY_INIT`.
|
|
|
|
*/
|
2011-09-13 21:57:57 +00:00
|
|
|
void argv_array_init(struct argv_array *);
|
2019-11-17 21:04:52 +00:00
|
|
|
|
|
|
|
/* Push a copy of a string onto the end of the array. */
|
2018-05-21 14:54:27 +00:00
|
|
|
const char *argv_array_push(struct argv_array *, const char *);
|
2019-11-17 21:04:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Format a string and push it onto the end of the array. This is a
|
|
|
|
* convenience wrapper combining `strbuf_addf` and `argv_array_push`.
|
|
|
|
*/
|
2011-09-13 21:57:57 +00:00
|
|
|
__attribute__((format (printf,2,3)))
|
2018-05-21 14:54:27 +00:00
|
|
|
const char *argv_array_pushf(struct argv_array *, const char *fmt, ...);
|
2019-11-17 21:04:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Push a list of strings onto the end of the array. The arguments
|
|
|
|
* should be a list of `const char *` strings, terminated by a NULL
|
|
|
|
* argument.
|
|
|
|
*/
|
2013-07-18 20:02:12 +00:00
|
|
|
LAST_ARG_MUST_BE_NULL
|
2012-04-18 21:10:05 +00:00
|
|
|
void argv_array_pushl(struct argv_array *, ...);
|
2019-11-17 21:04:52 +00:00
|
|
|
|
|
|
|
/* Push a null-terminated array of strings onto the end of the array. */
|
2015-06-14 08:41:50 +00:00
|
|
|
void argv_array_pushv(struct argv_array *, const char **);
|
2019-11-17 21:04:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the final element from the array. If there are no
|
|
|
|
* elements in the array, do nothing.
|
|
|
|
*/
|
2012-09-01 11:25:27 +00:00
|
|
|
void argv_array_pop(struct argv_array *);
|
2019-11-17 21:04:52 +00:00
|
|
|
|
2018-04-25 09:53:57 +00:00
|
|
|
/* Splits by whitespace; does not handle quoted arguments! */
|
|
|
|
void argv_array_split(struct argv_array *, const char *);
|
2019-11-17 21:04:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Free all memory associated with the array and return it to the
|
|
|
|
* initial, empty state.
|
|
|
|
*/
|
2011-09-13 21:57:57 +00:00
|
|
|
void argv_array_clear(struct argv_array *);
|
2019-11-17 21:04:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Disconnect the `argv` member from the `argv_array` struct and
|
|
|
|
* return it. The caller is responsible for freeing the memory used
|
|
|
|
* by the array, and by the strings it references. After detaching,
|
|
|
|
* the `argv_array` is in a reinitialized state and can be pushed
|
|
|
|
* into again.
|
|
|
|
*/
|
2016-02-22 22:44:15 +00:00
|
|
|
const char **argv_array_detach(struct argv_array *);
|
2011-09-13 21:57:57 +00:00
|
|
|
|
|
|
|
#endif /* ARGV_ARRAY_H */
|