Blame src/internal/argv/argv.h

0efa8c
/****************************************************************************/
0efa8c
/*  argv.h: a thread-safe argument vector parser and usage screen generator */
abfa47
/*  Copyright (C) 2015--2024  SysDeer Technologies, LLC                     */
0efa8c
/*  Released under GPLv2 and GPLv3; see COPYING.PTYCON.                     */
0efa8c
/****************************************************************************/
0efa8c
0efa8c
#ifndef ARGV_H
0efa8c
#define ARGV_H
0efa8c
0efa8c
#include <stdbool.h>
0efa8c
#include <stdint.h>
0efa8c
#include <stddef.h>
0efa8c
#include <string.h>
0efa8c
#include <stdlib.h>
0efa8c
#include <stdio.h>
97f548
#include <ctype.h>
0efa8c
#include <unistd.h>
0efa8c
0efa8c
#define ARGV_VERBOSITY_NONE		0x00
0efa8c
#define ARGV_VERBOSITY_ERRORS		0x01
0efa8c
#define ARGV_VERBOSITY_STATUS		0x02
0efa8c
#define ARGV_CLONE_VECTOR		0x80
0efa8c
0efa8c
#ifndef ARGV_TAB_WIDTH
0efa8c
#define ARGV_TAB_WIDTH			8
0efa8c
#endif
0efa8c
0efa8c
/*******************************************/
0efa8c
/*                                         */
0efa8c
/* support of hybrid options               */
0efa8c
/* -------------------------               */
0efa8c
/* hybrid options are very similar to      */
0efa8c
/* long options, yet are prefixed by       */
0efa8c
/* a single dash rather than two           */
0efa8c
/* (i.e. -std, -isystem).                  */
0efa8c
/* hybrid options are supported by this    */
0efa8c
/* driver for compatibility with legacy    */
0efa8c
/* tools; note, however, that the use      */
0efa8c
/* of hybrid options should be strongly    */
0efa8c
/* discouraged due to the limitations      */
0efa8c
/* they impose on short options (for       */
0efa8c
/* example, a driver implementing -std     */
0efa8c
/* may not provide -s as a short option    */
0efa8c
/* that takes an arbitrary value).         */
0efa8c
/*                                         */
0efa8c
/* SPACE: -hybrid VALUE (i.e. -MF file)    */
0efa8c
/* EQUAL: -hybrid=VALUE (i.e. -std=c99)    */
0efa8c
/* COMMA: -hybrid,VALUE (i.e. -Wl,<arg>)   */
0efa8c
/* ONLY:  -opt accepted, --opt rejected    */
0efa8c
/* JOINED: -optVALUE                       */
0efa8c
/*                                         */
0efa8c
/*******************************************/
0efa8c
0efa8c
0efa8c
#define ARGV_OPTION_HYBRID_NONE		0x00
0efa8c
#define ARGV_OPTION_HYBRID_ONLY		0x01
0efa8c
#define ARGV_OPTION_HYBRID_SPACE	0x02
0efa8c
#define ARGV_OPTION_HYBRID_EQUAL	0x04
0efa8c
#define ARGV_OPTION_HYBRID_COMMA	0x08
0efa8c
#define ARGV_OPTION_HYBRID_JOINED	0x10
97f548
97f548
#define ARGV_OPTION_KEYVAL_PAIR         0X20
97f548
#define ARGV_OPTION_KEYVAL_ARRAY        0X40
97f548
#define ARGV_OPTION_KEYVAL_MASK         (ARGV_OPTION_KEYVAL_PAIR \
97f548
					| ARGV_OPTION_KEYVAL_ARRAY)
97f548
0efa8c
#define ARGV_OPTION_HYBRID_CIRCUS	(ARGV_OPTION_HYBRID_SPACE \
0efa8c
					| ARGV_OPTION_HYBRID_JOINED)
97f548
0efa8c
#define ARGV_OPTION_HYBRID_DUAL		(ARGV_OPTION_HYBRID_SPACE \
0efa8c
					| ARGV_OPTION_HYBRID_EQUAL)
97f548
0efa8c
#define ARGV_OPTION_HYBRID_SWITCH	(ARGV_OPTION_HYBRID_ONLY \
0efa8c
					| ARGV_OPTION_HYBRID_SPACE \
0efa8c
					| ARGV_OPTION_HYBRID_EQUAL \
0efa8c
					| ARGV_OPTION_HYBRID_COMMA \
0efa8c
					| ARGV_OPTION_HYBRID_JOINED)
0efa8c
97f548
#define ARGV_KEYVAL_ASSIGN              0x01
97f548
#define ARGV_KEYVAL_OVERRIDE            0x02
97f548
0efa8c
enum argv_optarg {
0efa8c
	ARGV_OPTARG_NONE,
0efa8c
	ARGV_OPTARG_REQUIRED,
0efa8c
	ARGV_OPTARG_OPTIONAL,
0efa8c
};
0efa8c
0efa8c
enum argv_mode {
0efa8c
	ARGV_MODE_SCAN,
0efa8c
	ARGV_MODE_COPY,
0efa8c
};
0efa8c
0efa8c
enum argv_error {
0efa8c
	ARGV_ERROR_OK,
0efa8c
	ARGV_ERROR_INTERNAL,
0efa8c
	ARGV_ERROR_SHORT_OPTION,
0efa8c
	ARGV_ERROR_LONG_OPTION,
abfa47
	ARGV_ERROR_VENDOR_OPTION,
0efa8c
	ARGV_ERROR_OPTARG_NONE,
0efa8c
	ARGV_ERROR_OPTARG_REQUIRED,
0efa8c
	ARGV_ERROR_OPTARG_PARADIGM,
0efa8c
	ARGV_ERROR_HYBRID_NONE,
0efa8c
	ARGV_ERROR_HYBRID_ONLY,
0efa8c
	ARGV_ERROR_HYBRID_SPACE,
0efa8c
	ARGV_ERROR_HYBRID_EQUAL,
0efa8c
	ARGV_ERROR_HYBRID_COMMA,
97f548
	ARGV_ERROR_KEYVAL_KEY,
97f548
	ARGV_ERROR_KEYVAL_VALUE,
97f548
	ARGV_ERROR_KEYVAL_ALLOC,
0efa8c
};
0efa8c
0efa8c
struct argv_option {
0efa8c
	const char *		long_name;
0efa8c
	const char		short_name;
0efa8c
	int			tag;
0efa8c
	enum argv_optarg	optarg;
0efa8c
	int			flags;
0efa8c
	const char *		paradigm;
0efa8c
	const char *		argname;
0efa8c
	const char *		description;
0efa8c
};
0efa8c
97f548
struct argv_keyval {
97f548
	const char *           keyword;
97f548
	const char *           value;
97f548
	int                    flags;
97f548
};
97f548
0efa8c
struct argv_entry {
24dc3a
	const char *            arg;
97f548
	struct argv_keyval *    keyv;
24dc3a
	int                     tag;
24dc3a
	bool                    fopt;
24dc3a
	bool                    fval;
24dc3a
	bool                    fnoscan;
24dc3a
	enum argv_error         errcode;
0efa8c
};
0efa8c
0efa8c
struct argv_meta {
0efa8c
	char **			argv;
0efa8c
	struct argv_entry *	entries;
0efa8c
};
0efa8c
0efa8c
struct argv_ctx {
0efa8c
	int				flags;
0efa8c
	int				mode;
0efa8c
	int				nentries;
a64bed
	intptr_t			unitidx;
a64bed
	intptr_t			erridx;
0efa8c
	enum argv_error 		errcode;
0efa8c
	const char *			errch;
0efa8c
	const struct argv_option *	erropt;
0efa8c
	const char *			program;
97f548
	size_t				keyvlen;
0efa8c
};
0efa8c
0efa8c
#ifdef ARGV_DRIVER
0efa8c
a06fe2
struct argv_meta_impl {
a06fe2
	char **			argv;
a06fe2
	char *			strbuf;
97f548
	char *                  keyvbuf;
97f548
	char *                  keyvmark;
a06fe2
	struct argv_meta	meta;
a06fe2
};
a06fe2
97d3f3
static int argv_optv_init(
97d3f3
	const struct argv_option[],
97d3f3
	const struct argv_option **);
97d3f3
0efa8c
static const char * argv_program_name(const char *);
0efa8c
0efa8c
static void argv_usage(
e754e3
	int		fd,
0efa8c
	const char *	header,
97d3f3
	const struct	argv_option **,
0efa8c
	const char *	mode);
0efa8c
8828d8
static void argv_usage_plain(
8828d8
	int		fd,
8828d8
	const char *	header,
8828d8
	const struct	argv_option **,
8828d8
	const char *	mode);
8828d8
0efa8c
static struct argv_meta * argv_get(
0efa8c
	char **,
97d3f3
	const struct argv_option **,
e754e3
	int flags,
e754e3
	int fd);
0efa8c
0efa8c
static void argv_free(struct argv_meta *);
0efa8c
e754e3
#ifndef argv_dprintf
e754e3
#define argv_dprintf dprintf
e754e3
#endif
0efa8c
0efa8c
0efa8c
0efa8c
/*------------------------------------*/
0efa8c
/* implementation of static functions */
0efa8c
/*------------------------------------*/
0efa8c
97d3f3
static int argv_optv_init(
97d3f3
	const struct argv_option	options[],
97d3f3
	const struct argv_option **	optv)
97d3f3
{
97d3f3
	const struct argv_option *	option;
97d3f3
	int				i;
97d3f3
97d3f3
	for (option=options,i=0; option->long_name || option->short_name; option++)
97d3f3
		optv[i++] = option;
97d3f3
97d3f3
	optv[i] = 0;
97d3f3
	return i;
97d3f3
}
97d3f3
0efa8c
static const struct argv_option * argv_short_option(
0efa8c
	const char *			ch,
97d3f3
	const struct argv_option **	optv,
0efa8c
	struct argv_entry *		entry)
0efa8c
{
0efa8c
	const struct argv_option *	option;
0efa8c
97d3f3
	for (; *optv; optv++) {
97d3f3
		option = *optv;
97d3f3
0efa8c
		if (option->short_name == *ch) {
0efa8c
			entry->tag	= option->tag;
0efa8c
			entry->fopt	= true;
0efa8c
			return option;
0efa8c
		}
0efa8c
	}
0efa8c
0efa8c
	return 0;
0efa8c
}
0efa8c
0efa8c
static const struct argv_option * argv_long_option(
0efa8c
	const char *			ch,
97d3f3
	const struct argv_option **	optv,
0efa8c
	struct argv_entry *		entry)
0efa8c
{
0efa8c
	const struct argv_option *	option;
0efa8c
	const char *			arg;
0efa8c
	size_t				len;
0efa8c
97d3f3
	for (; *optv; optv++) {
97d3f3
		option = *optv;
97d3f3
		len    = option->long_name ? strlen(option->long_name) : 0;
0efa8c
0efa8c
		if (len && !(strncmp(option->long_name,ch,len))) {
0efa8c
			arg = ch + len;
0efa8c
0efa8c
			if (!*arg
0efa8c
				|| (*arg == '=')
0efa8c
				|| (option->flags & ARGV_OPTION_HYBRID_JOINED)
0efa8c
				|| ((option->flags & ARGV_OPTION_HYBRID_COMMA)
0efa8c
					&& (*arg == ','))) {
0efa8c
				entry->tag	= option->tag;
0efa8c
				entry->fopt	= true;
0efa8c
				return option;
0efa8c
			}
0efa8c
		}
0efa8c
	}
0efa8c
0efa8c
	return 0;
0efa8c
}
0efa8c
0efa8c
static inline bool is_short_option(const char * arg)
0efa8c
{
0efa8c
	return (arg[0]=='-') && arg[1] && (arg[1]!='-');
0efa8c
}
0efa8c
0efa8c
static inline bool is_long_option(const char * arg)
0efa8c
{
0efa8c
	return (arg[0]=='-') && (arg[1]=='-') && arg[2];
0efa8c
}
0efa8c
0efa8c
static inline bool is_last_option(const char * arg)
0efa8c
{
0efa8c
	return (arg[0]=='-') && (arg[1]=='-') && !arg[2];
0efa8c
}
0efa8c
0efa8c
static inline bool is_hybrid_option(
0efa8c
	const char *			arg,
97d3f3
	const struct argv_option **	optv)
0efa8c
{
0efa8c
	const struct argv_option *	option;
0efa8c
	struct argv_entry		entry;
0efa8c
0efa8c
	if (!is_short_option(arg))
0efa8c
		return false;
0efa8c
97d3f3
	if (!(option = argv_long_option(++arg,optv,&entry)))
0efa8c
		return false;
0efa8c
0efa8c
	if (!(option->flags & ARGV_OPTION_HYBRID_SWITCH))
97d3f3
		if (argv_short_option(arg,optv,&entry))
0efa8c
			return false;
0efa8c
0efa8c
	return true;
0efa8c
}
0efa8c
0efa8c
static inline bool is_arg_in_paradigm(const char * arg, const char * paradigm)
0efa8c
{
0efa8c
	size_t		len;
0efa8c
	const char *	ch;
0efa8c
0efa8c
	for (ch=paradigm,len=strlen(arg); ch; ) {
0efa8c
		if (!strncmp(arg,ch,len)) {
0efa8c
			if (!*(ch += len))
0efa8c
				return true;
0efa8c
			else if (*ch == '|')
0efa8c
				return true;
0efa8c
		}
0efa8c
0efa8c
		if ((ch = strchr(ch,'|')))
0efa8c
			ch++;
0efa8c
	}
0efa8c
0efa8c
	return false;
0efa8c
}
0efa8c
0efa8c
static inline const struct argv_option * option_from_tag(
97d3f3
	const struct argv_option **	optv,
0efa8c
	int				tag)
0efa8c
{
97d3f3
	for (; *optv; optv++)
97d3f3
		if (optv[0]->tag == tag)
97d3f3
			return optv[0];
0efa8c
	return 0;
0efa8c
}
0efa8c
97f548
static inline int argv_scan_keyval_array(struct argv_meta_impl * meta, struct argv_entry * entry)
97f548
{
97f548
	const char *            ch;
97f548
	char *                  dst;
97f548
	int                     ncomma;
97f548
	int                     cint;
97f548
	struct argv_keyval *    keyv;
97f548
97f548
	/* count key[val] elements, assume no comma after last element */
97f548
	for (ch=entry->arg,ncomma=1; *ch; ch++) {
97f548
		if ((ch[0]=='\\') && (ch[1]==',')) {
97f548
			ch++;
97f548
97f548
		} else if ((ch[0]==',')) {
97f548
			ncomma++;
97f548
		}
97f548
	}
97f548
97f548
	/* keyval string buffer */
97f548
	dst = meta->keyvmark;
97f548
97f548
	/* allocate keyval array */
97f548
	if (!(entry->keyv = calloc(ncomma+1,sizeof(*entry->keyv))))
97f548
		return ARGV_ERROR_KEYVAL_ALLOC;
97f548
97f548
	/* create keyval array */
97f548
	entry->keyv->keyword = dst;
97f548
97f548
	for (ch=entry->arg,keyv=entry->keyv; *ch; ch++) {
97f548
		if ((ch[0]=='\\') && (ch[1]==',')) {
97f548
			*dst++ = ',';
97f548
			ch++;
97f548
97f548
		} else if ((ch[0]==':') && (ch[1]=='=')) {
97f548
			if (!keyv->keyword[0])
97f548
				return ARGV_ERROR_KEYVAL_KEY;
97f548
97f548
			keyv->flags = ARGV_KEYVAL_OVERRIDE;
97f548
			keyv->value = ++dst;
97f548
			ch++;
97f548
97f548
		} else if ((ch[0]=='=')) {
97f548
			if (!keyv->keyword[0])
97f548
				return ARGV_ERROR_KEYVAL_KEY;
97f548
97f548
			keyv->flags = ARGV_KEYVAL_ASSIGN;
97f548
			keyv->value = ++dst;
97f548
97f548
		} else if ((ch[0]==',')) {
97f548
			for (; isblank(cint = ch[1]); )
97f548
				ch++;
97f548
97f548
			if (ch[1]) {
97f548
				keyv++;
97f548
				keyv->keyword = ++dst;
97f548
			}
97f548
		} else {
97f548
			*dst++ = *ch;
97f548
		}
97f548
	}
97f548
97f548
	/* keyval string buffer */
97f548
	meta->keyvmark = ++dst;
97f548
97f548
	return ARGV_ERROR_OK;
97f548
}
97f548
97f548
static inline int argv_scan_keyval_pair(struct argv_meta_impl * meta, struct argv_entry * entry)
97f548
{
97f548
	const char *            ch;
97f548
	char *                  dst;
97f548
	struct argv_keyval *    keyv;
97f548
97f548
	/* keyval string buffer */
97f548
	dst = meta->keyvmark;
97f548
97f548
	/* allocate keyval array */
97f548
	if (!(entry->keyv = calloc(2,sizeof(*entry->keyv))))
97f548
		return ARGV_ERROR_KEYVAL_ALLOC;
97f548
97f548
	/* create keyval array */
97f548
	entry->keyv->keyword = dst;
97f548
97f548
	for (ch=entry->arg,keyv=entry->keyv; *ch && !keyv->flags; ch++) {
97f548
		if ((ch[0]=='\\') && (ch[1]==',')) {
97f548
			*dst++ = ',';
97f548
			ch++;
97f548
97f548
		} else if ((ch[0]==':') && (ch[1]=='=')) {
97f548
			if (!keyv->keyword[0])
97f548
				return ARGV_ERROR_KEYVAL_KEY;
97f548
97f548
			keyv->flags = ARGV_KEYVAL_OVERRIDE;
97f548
			keyv->value = ++dst;
97f548
			ch++;
97f548
97f548
		} else if ((ch[0]=='=')) {
97f548
			if (!keyv->keyword[0])
97f548
				return ARGV_ERROR_KEYVAL_KEY;
97f548
97f548
			keyv->flags = ARGV_KEYVAL_ASSIGN;
97f548
			keyv->value = ++dst;
97f548
97f548
		} else {
97f548
			*dst++ = *ch;
97f548
		}
97f548
	}
97f548
97f548
	for (; *ch; )
97f548
		*dst++ = *ch++;
97f548
97f548
	/* keyval string buffer */
97f548
	meta->keyvmark = ++dst;
97f548
97f548
	return ARGV_ERROR_OK;
97f548
}
97f548
0efa8c
static void argv_scan(
0efa8c
	char **				argv,
97d3f3
	const struct argv_option **	optv,
0efa8c
	struct argv_ctx *		ctx,
0efa8c
	struct argv_meta *		meta)
0efa8c
{
97f548
	struct argv_meta_impl *         imeta;
97f548
	uintptr_t                       addr;
0efa8c
	char **				parg;
0efa8c
	const char *			ch;
0efa8c
	const char *			val;
0efa8c
	const struct argv_option *	option;
0efa8c
	struct argv_entry		entry;
0efa8c
	struct argv_entry *		mentry;
0efa8c
	enum argv_error			ferr;
0efa8c
	bool				fval;
0efa8c
	bool				fnext;
0efa8c
	bool				fshort;
0efa8c
	bool				fhybrid;
0efa8c
	bool				fnoscan;
0efa8c
97f548
	addr  = (uintptr_t)meta - offsetof(struct argv_meta_impl,meta);
97f548
	imeta = (struct argv_meta_impl *)addr;
97f548
0efa8c
	parg	= &argv[1];
0efa8c
	ch	= *parg;
0efa8c
	ferr	= ARGV_ERROR_OK;
0efa8c
	fshort	= false;
0efa8c
	fnoscan	= false;
0efa8c
	fval	= false;
0efa8c
	mentry	= meta ? meta->entries : 0;
0efa8c
0efa8c
	ctx->unitidx = 0;
0efa8c
	ctx->erridx  = 0;
0efa8c
0efa8c
	while (ch && (ferr == ARGV_ERROR_OK)) {
0efa8c
		option  = 0;
0efa8c
		fhybrid = false;
0efa8c
0efa8c
		if (fnoscan)
0efa8c
			fval = true;
0efa8c
0efa8c
		else if (is_last_option(ch))
0efa8c
			fnoscan = true;
0efa8c
97d3f3
		else if (!fshort && is_hybrid_option(ch,optv))
0efa8c
			fhybrid = true;
0efa8c
0efa8c
		if (!fnoscan && !fhybrid && (fshort || is_short_option(ch))) {
0efa8c
			if (!fshort)
0efa8c
				ch++;
0efa8c
97d3f3
			if ((option = argv_short_option(ch,optv,&entry))) {
0efa8c
				if (ch[1]) {
0efa8c
					ch++;
0efa8c
					fnext	= false;
0efa8c
					fshort	= (option->optarg == ARGV_OPTARG_NONE);
0efa8c
				} else {
0efa8c
					parg++;
0efa8c
					ch	= *parg;
0efa8c
					fnext	= true;
0efa8c
					fshort	= false;
0efa8c
				}
0efa8c
0efa8c
				if (option->optarg == ARGV_OPTARG_NONE) {
0020cd
					if (!fnext && ch && (*ch == '-')) {
0efa8c
						ferr = ARGV_ERROR_OPTARG_NONE;
0020cd
					} else {
0efa8c
						fval = false;
0020cd
					}
0020cd
0020cd
				} else if (!fnext) {
0efa8c
					fval = true;
0020cd
0020cd
				} else if (option->optarg == ARGV_OPTARG_REQUIRED) {
0efa8c
					if (ch && is_short_option(ch))
0efa8c
						ferr = ARGV_ERROR_OPTARG_REQUIRED;
0efa8c
					else if (ch && is_long_option(ch))
0efa8c
						ferr = ARGV_ERROR_OPTARG_REQUIRED;
0efa8c
					else if (ch && is_last_option(ch))
0efa8c
						ferr = ARGV_ERROR_OPTARG_REQUIRED;
0efa8c
					else if (ch)
0efa8c
						fval = true;
0efa8c
					else
0efa8c
						ferr = ARGV_ERROR_OPTARG_REQUIRED;
0efa8c
				} else {
0efa8c
					/* ARGV_OPTARG_OPTIONAL */
0efa8c
					if (ch && is_short_option(ch))
0efa8c
						fval = false;
0efa8c
					else if (ch && is_long_option(ch))
0efa8c
						fval = false;
0efa8c
					else if (ch && is_last_option(ch))
0efa8c
						fval = false;
ec0036
					else if (fnext)
ec0036
						fval = false;
0efa8c
					else
0efa8c
						fval = ch;
0efa8c
				}
0020cd
			} else {
abfa47
				if ((ch == &parg[0][1]) && (ch[0] == 'W') && ch[1]) {
abfa47
					ferr = ARGV_ERROR_VENDOR_OPTION;
abfa47
				} else {
abfa47
					ferr = ARGV_ERROR_SHORT_OPTION;
abfa47
				}
0020cd
			}
0efa8c
0efa8c
		} else if (!fnoscan && (fhybrid || is_long_option(ch))) {
0efa8c
			ch += (fhybrid ? 1 : 2);
0efa8c
97d3f3
			if ((option = argv_long_option(ch,optv,&entry))) {
0efa8c
				val = ch + strlen(option->long_name);
0efa8c
0efa8c
				/* val[0] is either '=' (or ',') or '\0' */
0efa8c
				if (!val[0]) {
0efa8c
					parg++;
0efa8c
					ch = *parg;
0efa8c
				}
0efa8c
7762d4
				/* now verify the proper setting of option values */
7762d4
				if (fhybrid && !(option->flags & ARGV_OPTION_HYBRID_SWITCH)) {
0efa8c
					ferr = ARGV_ERROR_HYBRID_NONE;
7762d4
7762d4
				} else if (!fhybrid && (option->flags & ARGV_OPTION_HYBRID_ONLY)) {
0efa8c
					ferr = ARGV_ERROR_HYBRID_ONLY;
7762d4
7762d4
				} else if (option->optarg == ARGV_OPTARG_NONE) {
0efa8c
					if (val[0]) {
0efa8c
						ferr = ARGV_ERROR_OPTARG_NONE;
0efa8c
						ctx->errch = val + 1;
7762d4
					} else {
0efa8c
						fval = false;
7762d4
					}
7762d4
0efa8c
				} else if (val[0] && (option->flags & ARGV_OPTION_HYBRID_JOINED)) {
0efa8c
					fval = true;
0efa8c
					ch   = val;
7762d4
7762d4
				} else if (fhybrid && !val[0] && !(option->flags & ARGV_OPTION_HYBRID_SPACE)) {
228001
					if (option->optarg == ARGV_OPTARG_OPTIONAL) {
228001
						fval = false;
228001
228001
					} else {
228001
						ferr = ARGV_ERROR_HYBRID_SPACE;
228001
					}
7762d4
7762d4
				} else if (fhybrid && (val[0]=='=') && !(option->flags & ARGV_OPTION_HYBRID_EQUAL)) {
0efa8c
					ferr = ARGV_ERROR_HYBRID_EQUAL;
7762d4
7762d4
				} else if (fhybrid && (val[0]==',') && !(option->flags & ARGV_OPTION_HYBRID_COMMA)) {
0efa8c
					ferr = ARGV_ERROR_HYBRID_COMMA;
7762d4
7762d4
				} else if (!fhybrid && (val[0]==',')) {
0efa8c
					ferr = ARGV_ERROR_HYBRID_COMMA;
7762d4
7762d4
				} else if (val[0] && !val[1]) {
0efa8c
					ferr = ARGV_ERROR_OPTARG_REQUIRED;
7762d4
7762d4
				} else if (val[0] && val[1]) {
0efa8c
					fval = true;
0efa8c
					ch   = ++val;
7762d4
0efa8c
				} else if (option->optarg == ARGV_OPTARG_REQUIRED) {
7762d4
					if (!val[0] && !*parg) {
0efa8c
						ferr = ARGV_ERROR_OPTARG_REQUIRED;
7762d4
7762d4
					} else if (*parg && is_short_option(*parg)) {
0efa8c
						ferr = ARGV_ERROR_OPTARG_REQUIRED;
7762d4
7762d4
					} else if (*parg && is_long_option(*parg)) {
0efa8c
						ferr = ARGV_ERROR_OPTARG_REQUIRED;
7762d4
7762d4
					} else if (*parg && is_last_option(*parg)) {
0efa8c
						ferr = ARGV_ERROR_OPTARG_REQUIRED;
7762d4
7762d4
					} else {
0efa8c
						fval = true;
7762d4
					}
0efa8c
				} else {
0efa8c
					/* ARGV_OPTARG_OPTIONAL */
0efa8c
					fval = val[0];
0efa8c
				}
7762d4
			} else {
0efa8c
				ferr = ARGV_ERROR_LONG_OPTION;
7762d4
			}
0efa8c
		}
0efa8c
0efa8c
		if (ferr == ARGV_ERROR_OK)
0efa8c
			if (option && fval && option->paradigm)
0efa8c
				if (!is_arg_in_paradigm(ch,option->paradigm))
0efa8c
					ferr = ARGV_ERROR_OPTARG_PARADIGM;
0efa8c
0efa8c
		if (ferr == ARGV_ERROR_OK)
0efa8c
			if (!option && !ctx->unitidx)
0efa8c
				ctx->unitidx = parg - argv;
0efa8c
97f548
		if (ferr == ARGV_ERROR_OK)
97f548
			if (option && (option->flags & ARGV_OPTION_KEYVAL_MASK))
97f548
				if (ctx->mode == ARGV_MODE_SCAN)
97f548
					ctx->keyvlen += strlen(ch) + 1;
97f548
0efa8c
		if (ferr != ARGV_ERROR_OK) {
0efa8c
			ctx->errcode = ferr;
0efa8c
			ctx->errch   = ctx->errch ? ctx->errch : ch;
0efa8c
			ctx->erropt  = option;
0efa8c
			ctx->erridx  = parg - argv;
0efa8c
			return;
97f548
		}
97f548
97f548
		if (ctx->mode == ARGV_MODE_SCAN) {
0efa8c
			if (!fnoscan)
0efa8c
				ctx->nentries++;
0efa8c
			else if (fval)
0efa8c
				ctx->nentries++;
0efa8c
0efa8c
			if (fval || !option) {
0efa8c
				parg++;
0efa8c
				ch = *parg;
0efa8c
			}
97f548
0efa8c
		} else if (ctx->mode == ARGV_MODE_COPY) {
0efa8c
			if (fnoscan) {
0efa8c
				if (fval) {
0efa8c
					mentry->arg	= ch;
0efa8c
					mentry->fnoscan = true;
0efa8c
				}
0efa8c
0efa8c
				parg++;
0efa8c
				ch = *parg;
0efa8c
			} else if (option) {
0efa8c
				mentry->arg	= fval ? ch : 0;
0efa8c
				mentry->tag	= option->tag;
0efa8c
				mentry->fopt	= true;
0efa8c
				mentry->fval	= fval;
0efa8c
0efa8c
				if (fval) {
0efa8c
					parg++;
0efa8c
					ch = *parg;
0efa8c
				}
0efa8c
			} else {
0efa8c
				mentry->arg = ch;
0efa8c
				parg++;
0efa8c
				ch = *parg;
0efa8c
			}
0efa8c
		}
97f548
97f548
		if (option && (option->flags & ARGV_OPTION_KEYVAL_PAIR))
97f548
			if (ctx->mode == ARGV_MODE_COPY)
97f548
				ferr = argv_scan_keyval_pair(imeta,mentry);
97f548
97f548
		if (option && (option->flags & ARGV_OPTION_KEYVAL_ARRAY))
97f548
			if (ctx->mode == ARGV_MODE_COPY)
97f548
				ferr = argv_scan_keyval_array(imeta,mentry);
97f548
97f548
		if (ferr != ARGV_ERROR_OK) {
97f548
			ctx->errcode = ferr;
97f548
			ctx->errch   = ctx->errch ? ctx->errch : ch;
97f548
			ctx->erropt  = option;
97f548
			ctx->erridx  = parg - argv;
97f548
			return;
97f548
		}
97f548
97f548
		mentry++;
0efa8c
	}
0efa8c
}
0efa8c
0efa8c
static const char * argv_program_name(const char * program_path)
0efa8c
{
0efa8c
	const char * ch;
0efa8c
0efa8c
	if (program_path) {
0efa8c
		if ((ch = strrchr(program_path,'/')))
0efa8c
			return *(++ch) ? ch : 0;
0efa8c
0efa8c
		if ((ch = strrchr(program_path,'\\')))
0efa8c
			return *(++ch) ? ch : 0;
0efa8c
	}
0efa8c
0efa8c
	return program_path;
0efa8c
}
0efa8c
e754e3
static void argv_show_error(int fd, struct argv_ctx * ctx)
0efa8c
{
ddb580
	const char * src;
ddb580
	char *       dst;
ddb580
	char *       cap;
ddb580
	char         opt_vendor_buf[256];
ddb580
	char         opt_short_name[2] = {0,0};
9b23a2
9b23a2
	if (ctx->erropt && ctx->erropt->short_name)
9b23a2
		opt_short_name[0] = ctx->erropt->short_name;
9b23a2
e754e3
	argv_dprintf(fd,"%s: error: ",ctx->program);
0efa8c
0efa8c
	switch (ctx->errcode) {
0efa8c
		case ARGV_ERROR_SHORT_OPTION:
e754e3
			argv_dprintf(fd,"'-%c' is not a valid short option\n",*ctx->errch);
0efa8c
			break;
0efa8c
0efa8c
		case ARGV_ERROR_LONG_OPTION:
e754e3
			argv_dprintf(fd,"'--%s' is not a valid long option\n",ctx->errch);
0efa8c
			break;
0efa8c
abfa47
		case ARGV_ERROR_VENDOR_OPTION:
ddb580
			src = ctx->errch;
ddb580
			dst = opt_vendor_buf;
abfa47
			cap = &opt_vendor_buf[sizeof(opt_vendor_buf)];
abfa47
ddb580
			for (; src && *src && dst
ddb580
				if ((*src == '=') || (*src == ',') || (*src == ':')) {
ddb580
					src  = 0;
ddb580
				} else {
ddb580
					*dst++ = *src++;
ddb580
				}
ddb580
			}
ddb580
ddb580
			if (dst == cap)
ddb580
				dst--;
ddb580
ddb580
			*dst = '\0';
abfa47
abfa47
			argv_dprintf(fd,"'-%s' is not a valid vendor option\n",opt_vendor_buf);
abfa47
			break;
abfa47
0efa8c
		case ARGV_ERROR_OPTARG_NONE:
e754e3
			argv_dprintf(fd,"'%s' is not a valid option value for [%s%s%s%s%s] "
0efa8c
					"(option values may not be specified)\n",
0efa8c
				ctx->errch,
9b23a2
				opt_short_name[0] ? "-" : "",
9b23a2
				opt_short_name,
9b23a2
				opt_short_name[0] ? "," : "",
0efa8c
				ctx->erropt->long_name ? "--" : "",
0efa8c
				ctx->erropt->long_name);
0efa8c
			break;
0efa8c
0efa8c
		case ARGV_ERROR_OPTARG_REQUIRED:
e754e3
			argv_dprintf(fd,"option [%s%s%s%s%s] requires %s %s%s%s\n",
9b23a2
				opt_short_name[0] ? "-" : "",
9b23a2
				opt_short_name,
9b23a2
				opt_short_name[0] ? "," : "",
4bdc58
				ctx->erropt->long_name
4bdc58
					? (ctx->erropt->flags & ARGV_OPTION_HYBRID_ONLY) ? "-" : "--"
4bdc58
					: "",
0efa8c
				ctx->erropt->long_name,
0efa8c
				ctx->erropt->paradigm ? "one of the following values:" : "a value",
0efa8c
				ctx->erropt->paradigm ? "{" : "",
0efa8c
				ctx->erropt->paradigm ? ctx->erropt->paradigm : "",
0efa8c
				ctx->erropt->paradigm ? "}" : "");
0efa8c
			break;
0efa8c
0efa8c
		case ARGV_ERROR_OPTARG_PARADIGM:
e754e3
			argv_dprintf(fd,"'%s' is not a valid option value for [%s%s%s%s%s]={%s}\n",
0efa8c
				ctx->errch,
9b23a2
				opt_short_name[0] ? "-" : "",
9b23a2
				opt_short_name,
9b23a2
				opt_short_name[0] ? "," : "",
0efa8c
				ctx->erropt->long_name ? "--" : "",
0efa8c
				ctx->erropt->long_name,
0efa8c
				ctx->erropt->paradigm);
0efa8c
			break;
0efa8c
0efa8c
		case ARGV_ERROR_HYBRID_NONE:
e754e3
			argv_dprintf(fd,"-%s is not a synonym for --%s\n",
0efa8c
				ctx->erropt->long_name,
0efa8c
				ctx->erropt->long_name);
0efa8c
			break;
0efa8c
0efa8c
		case ARGV_ERROR_HYBRID_ONLY:
e754e3
			argv_dprintf(fd,"--%s is not a synonym for -%s\n",
0efa8c
				ctx->erropt->long_name,
0efa8c
				ctx->erropt->long_name);
0efa8c
			break;
0efa8c
0efa8c
		case ARGV_ERROR_HYBRID_SPACE:
0efa8c
		case ARGV_ERROR_HYBRID_EQUAL:
0efa8c
		case ARGV_ERROR_HYBRID_COMMA:
e754e3
			argv_dprintf(fd,"-%s: illegal value assignment; valid syntax is "
0efa8c
					"-%s%sVAL\n",
0efa8c
				ctx->erropt->long_name,
0efa8c
				ctx->erropt->long_name,
0efa8c
				(ctx->erropt->flags & ARGV_OPTION_HYBRID_SPACE)
0efa8c
					? " " : (ctx->erropt->flags & ARGV_OPTION_HYBRID_EQUAL)
0efa8c
					? "=" : (ctx->erropt->flags & ARGV_OPTION_HYBRID_COMMA)
0efa8c
					? "," : "");
0efa8c
0efa8c
			break;
0efa8c
97f548
		case ARGV_ERROR_KEYVAL_KEY:
97f548
			argv_dprintf(fd,"illegal key detected in keyval argument\n");
97f548
			break;
97f548
97f548
		case ARGV_ERROR_KEYVAL_VALUE:
97f548
			argv_dprintf(fd,"illegal value detected in keyval argument\n");
97f548
			break;
97f548
0efa8c
		case ARGV_ERROR_INTERNAL:
e754e3
			argv_dprintf(fd,"internal error");
0efa8c
			break;
0efa8c
0efa8c
		default:
0efa8c
			break;
0efa8c
	}
0efa8c
}
0efa8c
0efa8c
static void argv_show_status(
e754e3
	int				fd,
97d3f3
	const struct argv_option **	optv,
0efa8c
	struct argv_ctx *		ctx,
0efa8c
	struct argv_meta *		meta)
0efa8c
{
2e8e2a
	int				i;
0efa8c
	int				argc;
0efa8c
	char **				argv;
2e8e2a
	struct argv_keyval *            keyv;
0efa8c
	struct argv_entry *		entry;
0efa8c
	const struct argv_option *	option;
0efa8c
	char				short_name[2] = {0};
0efa8c
	const char *			space = "";
0efa8c
0efa8c
	(void)ctx;
0efa8c
e754e3
	argv_dprintf(fd,"\n\nconcatenated command line:\n");
0efa8c
	for (argv=meta->argv; *argv; argv++) {
e754e3
		argv_dprintf(fd,"%s%s",space,*argv);
0efa8c
		space = " ";
0efa8c
	}
0efa8c
e754e3
	argv_dprintf(fd,"\n\nargument vector:\n");
0efa8c
	for (argc=0,argv=meta->argv; *argv; argc++,argv++)
e754e3
		argv_dprintf(fd,"argv[%d]: %s\n",argc,*argv);
0efa8c
e754e3
	argv_dprintf(fd,"\n\nparsed entries:\n");
2fa030
	for (entry=meta->entries; entry->arg || entry->fopt; entry++) {
0efa8c
		if (entry->fopt) {
97d3f3
			option = option_from_tag(optv,entry->tag);
0efa8c
			short_name[0] = option->short_name;
0efa8c
0efa8c
			if (entry->fval)
e754e3
				argv_dprintf(fd,"[-%s,--%s] := %s\n",
0efa8c
					short_name,option->long_name,entry->arg);
0efa8c
			else
e754e3
				argv_dprintf(fd,"[-%s,--%s]\n",
0efa8c
					short_name,option->long_name);
2e8e2a
2e8e2a
			if (entry->keyv) {
2e8e2a
				for (i=0,keyv=entry->keyv; keyv->keyword; i++,keyv++) {
2e8e2a
					switch (keyv->flags) {
2e8e2a
						case ARGV_KEYVAL_ASSIGN:
2e8e2a
							argv_dprintf(fd,"\tkeyval[%d]: <%s>=%s\n",
2e8e2a
								i,keyv->keyword,keyv->value);
2e8e2a
							break;
2e8e2a
2e8e2a
						case ARGV_KEYVAL_OVERRIDE:
2e8e2a
							argv_dprintf(fd,"\tkeyval[%d]: <%s>:=%s\n",
2e8e2a
								i,keyv->keyword,keyv->value);
2e8e2a
							break;
2e8e2a
2e8e2a
						default:
2e8e2a
							argv_dprintf(fd,"\tkeyval[%d]: <%s>\n",
2e8e2a
								i,keyv->keyword);
2e8e2a
						break;
2e8e2a
					}
2e8e2a
				}
2e8e2a
			}
2fa030
		} else {
e754e3
			argv_dprintf(fd,"<program arg> := %s\n",entry->arg);
2fa030
		}
2fa030
	}
0efa8c
e754e3
	argv_dprintf(fd,"\n\n");
0efa8c
}
0efa8c
0efa8c
static struct argv_meta * argv_free_impl(struct argv_meta_impl * imeta)
0efa8c
{
97f548
	struct argv_entry * entry;
97f548
	void *              addr;
97f548
97f548
	if (imeta->keyvbuf)
97f548
		for (entry=imeta->meta.entries; entry->fopt || entry->arg; entry++)
97f548
			if (entry->keyv)
97f548
				free((addr = entry->keyv));
97f548
97f548
	if (imeta->keyvbuf)
97f548
		free(imeta->keyvbuf);
97f548
0efa8c
	if (imeta->argv)
0efa8c
		free(imeta->argv);
0efa8c
0efa8c
	if (imeta->strbuf)
0efa8c
		free(imeta->strbuf);
0efa8c
0efa8c
	if (imeta->meta.entries)
0efa8c
		free(imeta->meta.entries);
0efa8c
0efa8c
	free(imeta);
0efa8c
	return 0;
0efa8c
}
0efa8c
0efa8c
static struct argv_meta * argv_alloc(char ** argv, struct argv_ctx * ctx)
0efa8c
{
0efa8c
	struct argv_meta_impl * imeta;
0efa8c
	char **			vector;
0efa8c
	char *			dst;
0efa8c
	size_t			size;
0efa8c
	int			argc;
0efa8c
	int			i;
0efa8c
0efa8c
	if (!(imeta = calloc(1,sizeof(*imeta))))
0efa8c
		return 0;
0efa8c
0efa8c
	if (ctx->flags & ARGV_CLONE_VECTOR) {
0efa8c
		for (vector=argv,argc=0,size=0; *vector; vector++) {
0efa8c
			size += strlen(*vector) + 1;
0efa8c
			argc++;
0efa8c
		}
0efa8c
0efa8c
		if (!(imeta->argv = calloc(argc+1,sizeof(char *))))
0efa8c
			return argv_free_impl(imeta);
51db54
51db54
		if (!(imeta->strbuf = calloc(1,size+1)))
0efa8c
			return argv_free_impl(imeta);
0efa8c
0efa8c
		for (i=0,dst=imeta->strbuf; i
0efa8c
			strcpy(dst,argv[i]);
0efa8c
			imeta->argv[i] = dst;
0efa8c
			dst += strlen(dst)+1;
0efa8c
		}
0efa8c
0efa8c
		imeta->meta.argv = imeta->argv;
51db54
	} else {
0efa8c
		imeta->meta.argv = argv;
51db54
	}
0efa8c
51db54
	imeta->meta.entries = calloc(
51db54
		ctx->nentries+1,
51db54
		sizeof(struct argv_entry));
51db54
51db54
	if (!imeta->meta.entries)
0efa8c
		return argv_free_impl(imeta);
51db54
97f548
	if (ctx->keyvlen) {
97f548
		imeta->keyvbuf = calloc(
97f548
			ctx->keyvlen,
97f548
			sizeof(char));
97f548
97f548
		if (!(imeta->keyvmark = imeta->keyvbuf))
97f548
			return argv_free_impl(imeta);
97f548
	}
97f548
51db54
	return &imeta->meta;
0efa8c
}
0efa8c
0efa8c
static struct argv_meta * argv_get(
2bab1e
	char **				argv,
97d3f3
	const struct argv_option **	optv,
e754e3
	int				flags,
e754e3
	int				fd)
0efa8c
{
0efa8c
	struct argv_meta *	meta;
97f548
	struct argv_ctx		ctx = {flags,ARGV_MODE_SCAN,0,0,0,0,0,0,0,0};
0efa8c
97d3f3
	argv_scan(argv,optv,&ctx,0);
0efa8c
0efa8c
	if (ctx.errcode != ARGV_ERROR_OK) {
df4e7d
		ctx.program = argv_program_name(argv[0]);
0efa8c
0efa8c
		if (ctx.flags & ARGV_VERBOSITY_ERRORS)
e754e3
			argv_show_error(fd,&ctx;;
0efa8c
0efa8c
		return 0;
0efa8c
	}
0efa8c
0efa8c
	if (!(meta = argv_alloc(argv,&ctx)))
0efa8c
		return 0;
0efa8c
0efa8c
	ctx.mode = ARGV_MODE_COPY;
97d3f3
	argv_scan(meta->argv,optv,&ctx,meta);
0efa8c
0efa8c
	if (ctx.errcode != ARGV_ERROR_OK) {
df4e7d
		ctx.program = argv[0];
0efa8c
		ctx.errcode = ARGV_ERROR_INTERNAL;
e754e3
		argv_show_error(fd,&ctx;;
0efa8c
		argv_free(meta);
0efa8c
0efa8c
		return 0;
0efa8c
	}
0efa8c
0efa8c
	if (ctx.flags & ARGV_VERBOSITY_STATUS)
e754e3
		argv_show_status(fd,optv,&ctx,meta);
0efa8c
0efa8c
	return meta;
0efa8c
}
0efa8c
0efa8c
static void argv_free(struct argv_meta * xmeta)
0efa8c
{
0efa8c
	struct argv_meta_impl * imeta;
0efa8c
	uintptr_t		addr;
0efa8c
0efa8c
	if (xmeta) {
0efa8c
		addr  = (uintptr_t)xmeta - offsetof(struct argv_meta_impl,meta);
0efa8c
		imeta = (struct argv_meta_impl *)addr;
0efa8c
		argv_free_impl(imeta);
0efa8c
	}
0efa8c
}
0efa8c
8828d8
static void argv_usage_impl(
e754e3
	int				fd,
0efa8c
	const char *    		header,
97d3f3
	const struct argv_option **	options,
8828d8
	const char *			mode,
8828d8
	int				fcolor)
0efa8c
{
97d3f3
	const struct argv_option **	optv;
0efa8c
	const struct argv_option *	option;
f68640
	int                             nlong;
0efa8c
	bool				fshort,flong,fboth;
0efa8c
	size_t				len,optlen,desclen;
0efa8c
	char				cache;
0efa8c
	char *				prefix;
0efa8c
	char *				desc;
0efa8c
	char *				mark;
0efa8c
	char *				cap;
0efa8c
	char				description[4096];
0efa8c
	char				optstr     [72];
0efa8c
	const size_t			optcap    = 64;
0efa8c
	const size_t			width     = 80;
0efa8c
	const char			indent[]  = "  ";
0efa8c
	const char			creset[]  = "\x1b[0m";
0efa8c
	const char			cbold []  = "\x1b[1m";
0efa8c
	const char			cgreen[]  = "\x1b[32m";
0efa8c
	const char			cblue []  = "\x1b[34m";
0efa8c
	const char			ccyan []  = "\x1b[36m";
0efa8c
	const char *			color     = ccyan;
8828d8
8828d8
	(void)argv_usage;
8828d8
	(void)argv_usage_plain;
0efa8c
0efa8c
	fshort = mode ? !strcmp(mode,"short") : 0;
0efa8c
	flong  = fshort ? 0 : mode && !strcmp(mode,"long");
0efa8c
	fboth  = !fshort && !flong;
0efa8c
0efa8c
	if (fcolor)
e754e3
		argv_dprintf(fd,"%s%s",cbold,cgreen);
0efa8c
0efa8c
	if (header)
e754e3
		argv_dprintf(fd,"%s",header);
0efa8c
f68640
	for (optlen=0,nlong=0,optv=options; *optv; optv++) {
97d3f3
		option = *optv;
0efa8c
0efa8c
		/* indent + comma */
0efa8c
		len = fboth ? sizeof(indent) + 1 : sizeof(indent);
0efa8c
0efa8c
		/* -o */
0efa8c
		if (fshort || fboth)
0efa8c
			len += option->short_name
0efa8c
				? 2 : 0;
0efa8c
0efa8c
		/* --option */
0efa8c
		if (flong || fboth)
0efa8c
			len += option->long_name
0efa8c
				? 2 + strlen(option->long_name) : 0;
0efa8c
0efa8c
		/* optlen */
0efa8c
		if (len > optlen)
0efa8c
			optlen = len;
f68640
f68640
		/* long (vs. hybrid-only) option? */
f68640
		if (option->long_name)
f68640
			if (!(option->flags & ARGV_OPTION_HYBRID_ONLY))
f68640
				nlong++;
0efa8c
	}
0efa8c
0efa8c
	if (optlen >= optcap) {
e754e3
		argv_dprintf(fd,
0efa8c
			"Option strings exceed %zu characters, "
0efa8c
			"please generate the usage screen manually.\n",
0efa8c
			optcap);
0efa8c
		return;
0efa8c
	}
0efa8c
0efa8c
	optlen += ARGV_TAB_WIDTH;
0efa8c
	optlen &= (~(ARGV_TAB_WIDTH-1));
0efa8c
	desclen = (optlen < width / 2) ? width - optlen : optlen;
0efa8c
97d3f3
	for (optv=options; *optv; optv++) {
97d3f3
		option = *optv;
97d3f3
0efa8c
		/* color */
0efa8c
		if (fcolor) {
0efa8c
			color = (color == ccyan) ? cblue : ccyan;
e6f035
			argv_dprintf(fd,color,0);
0efa8c
		}
0efa8c
0efa8c
		/* description, using either paradigm or argname if applicable */
0efa8c
		snprintf(description,sizeof(description),option->description,
0efa8c
			option->paradigm
0efa8c
				? option->paradigm
0efa8c
				: option->argname ? option->argname : "");
0efa8c
		description[sizeof(description)-1] = 0;
0efa8c
0efa8c
		/* long/hybrid option prefix (-/--) */
0efa8c
		prefix = option->flags & ARGV_OPTION_HYBRID_ONLY
089c2e
				? "  -" : " --";
0efa8c
f68640
		/* avoid extra <stace> when all long opts are hybrid-only */
f68640
		if (nlong == 0)
f68640
			prefix++;
f68640
0efa8c
		/* option string */
0efa8c
		if (fboth && option->short_name && option->long_name)
0efa8c
			sprintf(optstr,"%s-%c,%s%s",
0efa8c
				indent,option->short_name,prefix,option->long_name);
0efa8c
0efa8c
		else if ((fshort || fboth) && option->short_name)
a9072c
			sprintf(optstr,"%s-%c",indent,option->short_name);
0efa8c
0efa8c
		else if (flong && option->long_name)
0efa8c
			sprintf(optstr,"%s%s%s",
0efa8c
				indent,prefix,option->long_name);
0efa8c
0efa8c
		else if (fboth && option->long_name)
0efa8c
			sprintf(optstr,"%s   %s%s",
0efa8c
				indent,prefix,option->long_name);
0efa8c
0efa8c
		else
0efa8c
			optstr[0] = 0;
0efa8c
0efa8c
		/* right-indented option buffer */
0efa8c
		if (description[0]) {
0efa8c
			len = strlen(optstr);
0efa8c
			sprintf(&optstr[len],"%-*c",(int)(optlen-len),' ');
0efa8c
		}
0efa8c
0efa8c
		/* single line? */
0efa8c
		if (optlen + strlen(description) < width) {
e754e3
			argv_dprintf(fd,"%s%s\n",optstr,description);
0efa8c
0efa8c
		} else {
0efa8c
			desc = description;
0efa8c
			cap  = desc + strlen(description);
0efa8c
0efa8c
			while (desc < cap) {
0efa8c
				mark = (desc + desclen >= cap)
0efa8c
					? cap : desc + desclen;
0efa8c
0efa8c
				while (*mark && (mark > desc)
0efa8c
						&& (*mark != ' ')
0efa8c
						&& (*mark != '|')
0efa8c
						&& (*mark != '\t')
0efa8c
						&& (*mark != '\n'))
0efa8c
					mark--;
0efa8c
0efa8c
				if (mark == desc) {
0efa8c
					mark = (desc + desclen >= cap)
0efa8c
						? cap : desc + desclen;
0efa8c
					cache = *mark;
0efa8c
					*mark = 0;
0efa8c
				} else if (*mark == '|') {
0efa8c
					cache = *mark;
0efa8c
					*mark = 0;
0efa8c
				} else {
0efa8c
					cache = 0;
0efa8c
					*mark = 0;
0efa8c
				}
0efa8c
0efa8c
				/* first line? */
0efa8c
				if (desc == description)
e754e3
					argv_dprintf(fd,"%s%s\n",optstr,desc);
0efa8c
				else
e754e3
					argv_dprintf(fd,"%-*c %s\n",
0efa8c
						(*desc == '|')
0efa8c
							? (int)(optlen+1)
0efa8c
							: (int)optlen,
0efa8c
						' ',desc);
0efa8c
0efa8c
				if (cache)
0efa8c
					*mark = cache;
0efa8c
				else
0efa8c
					mark++;
0efa8c
0efa8c
				desc = mark;
0efa8c
			}
0efa8c
		}
0efa8c
	}
0efa8c
0efa8c
	if (fcolor)
e754e3
		argv_dprintf(fd,creset);
0efa8c
}
0efa8c
8828d8
static void argv_usage(
8828d8
	int				fd,
8828d8
	const char *    		header,
8828d8
	const struct argv_option **	options,
8828d8
	const char *			mode)
8828d8
{
8828d8
	argv_usage_impl(fd,header,options,mode,isatty(fd));
8828d8
}
8828d8
8828d8
static void argv_usage_plain(
8828d8
	int				fd,
8828d8
	const char *    		header,
8828d8
	const struct argv_option **	options,
8828d8
	const char *			mode)
8828d8
{
8828d8
	argv_usage_impl(fd,header,options,mode,0);
8828d8
}
8828d8
0efa8c
#endif
0efa8c
0efa8c
#endif