Blame argv-array.h

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