2011-09-13 23:57:57 +02:00
|
|
|
#ifndef ARGV_ARRAY_H
|
|
|
|
#define ARGV_ARRAY_H
|
|
|
|
|
2019-11-17 22:04:52 +01: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 23:08:49 +02:00
|
|
|
extern const char *empty_argv[];
|
2011-09-13 23:57:57 +02:00
|
|
|
|
2019-11-17 22:04:52 +01: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 23:57:57 +02:00
|
|
|
struct argv_array {
|
|
|
|
const char **argv;
|
|
|
|
int argc;
|
|
|
|
int alloc;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define ARGV_ARRAY_INIT { empty_argv, 0, 0 }
|
|
|
|
|
2019-11-17 22:04:52 +01:00
|
|
|
/**
|
|
|
|
* Initialize an array. This is no different than assigning from
|
|
|
|
* `ARGV_ARRAY_INIT`.
|
|
|
|
*/
|
2011-09-13 23:57:57 +02:00
|
|
|
void argv_array_init(struct argv_array *);
|
2019-11-17 22:04:52 +01:00
|
|
|
|
|
|
|
/* Push a copy of a string onto the end of the array. */
|
2018-05-21 16:54:27 +02:00
|
|
|
const char *argv_array_push(struct argv_array *, const char *);
|
2019-11-17 22:04:52 +01: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 23:57:57 +02:00
|
|
|
__attribute__((format (printf,2,3)))
|
2018-05-21 16:54:27 +02:00
|
|
|
const char *argv_array_pushf(struct argv_array *, const char *fmt, ...);
|
2019-11-17 22:04:52 +01: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 22:02:12 +02:00
|
|
|
LAST_ARG_MUST_BE_NULL
|
2012-04-18 23:10:05 +02:00
|
|
|
void argv_array_pushl(struct argv_array *, ...);
|
2019-11-17 22:04:52 +01:00
|
|
|
|
|
|
|
/* Push a null-terminated array of strings onto the end of the array. */
|
2015-06-14 10:41:50 +02:00
|
|
|
void argv_array_pushv(struct argv_array *, const char **);
|
2019-11-17 22:04:52 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the final element from the array. If there are no
|
|
|
|
* elements in the array, do nothing.
|
|
|
|
*/
|
2012-09-01 13:25:27 +02:00
|
|
|
void argv_array_pop(struct argv_array *);
|
2019-11-17 22:04:52 +01:00
|
|
|
|
2018-04-25 11:53:57 +02:00
|
|
|
/* Splits by whitespace; does not handle quoted arguments! */
|
|
|
|
void argv_array_split(struct argv_array *, const char *);
|
2019-11-17 22:04:52 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Free all memory associated with the array and return it to the
|
|
|
|
* initial, empty state.
|
|
|
|
*/
|
2011-09-13 23:57:57 +02:00
|
|
|
void argv_array_clear(struct argv_array *);
|
2019-11-17 22:04:52 +01: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 23:44:15 +01:00
|
|
|
const char **argv_array_detach(struct argv_array *);
|
2011-09-13 23:57:57 +02:00
|
|
|
|
|
|
|
#endif /* ARGV_ARRAY_H */
|