Blame src/internal/argv/argv.h

0efa8c
/****************************************************************************/
0efa8c
/*  argv.h: a thread-safe argument vector parser and usage screen generator */
d8519c
/*  Copyright (C) 2015--2017  Z. Gilboa                                     */
0efa8c
/*  Released under GPLv2 and GPLv3; see COPYING.PTYCON.                     */
0efa8c
/*  This file is (also) part of sofort: portable software project template. */
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>
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
0efa8c
#define ARGV_OPTION_HYBRID_CIRCUS	(ARGV_OPTION_HYBRID_SPACE \
0efa8c
					| ARGV_OPTION_HYBRID_JOINED)
0efa8c
#define ARGV_OPTION_HYBRID_DUAL		(ARGV_OPTION_HYBRID_SPACE \
0efa8c
					| ARGV_OPTION_HYBRID_EQUAL)
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
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,
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,
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
0efa8c
struct argv_entry {
0efa8c
	const char *	arg;
0efa8c
	int		tag;
0efa8c
	bool		fopt;
0efa8c
	bool		fval;
0efa8c
	bool		fnoscan;
0efa8c
	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;
0efa8c
};
0efa8c
0efa8c
#ifdef ARGV_DRIVER
0efa8c
a06fe2
struct argv_meta_impl {
a06fe2
	char **			argv;
a06fe2
	char *			strbuf;
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
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
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
{
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
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 {
0efa8c
				ferr = ARGV_ERROR_SHORT_OPTION;
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
0efa8c
				if (fhybrid && !(option->flags & ARGV_OPTION_HYBRID_SWITCH))
0efa8c
					ferr = ARGV_ERROR_HYBRID_NONE;
0efa8c
				else if (!fhybrid && (option->flags & ARGV_OPTION_HYBRID_ONLY))
0efa8c
					ferr = ARGV_ERROR_HYBRID_ONLY;
0efa8c
				else if (option->optarg == ARGV_OPTARG_NONE) {
0efa8c
					if (val[0]) {
0efa8c
						ferr = ARGV_ERROR_OPTARG_NONE;
0efa8c
						ctx->errch = val + 1;
0efa8c
					} else
0efa8c
						fval = false;
0efa8c
				} else if (val[0] && (option->flags & ARGV_OPTION_HYBRID_JOINED)) {
0efa8c
					fval = true;
0efa8c
					ch   = val;
0efa8c
				} else if (fhybrid && !val[0] && !(option->flags & ARGV_OPTION_HYBRID_SPACE))
0efa8c
					ferr = ARGV_ERROR_HYBRID_SPACE;
0efa8c
				else if (fhybrid && (val[0]=='=') && !(option->flags & ARGV_OPTION_HYBRID_EQUAL))
0efa8c
					ferr = ARGV_ERROR_HYBRID_EQUAL;
0efa8c
				else if (fhybrid && (val[0]==',') && !(option->flags & ARGV_OPTION_HYBRID_COMMA))
0efa8c
					ferr = ARGV_ERROR_HYBRID_COMMA;
0efa8c
				else if (!fhybrid && (val[0]==','))
0efa8c
					ferr = ARGV_ERROR_HYBRID_COMMA;
0efa8c
				else if (val[0] && !val[1])
0efa8c
					ferr = ARGV_ERROR_OPTARG_REQUIRED;
0efa8c
				else if (val[0] && val[1]) {
0efa8c
					fval = true;
0efa8c
					ch   = ++val;
0efa8c
				} else if (option->optarg == ARGV_OPTARG_REQUIRED) {
0efa8c
					if (!val[0] && !*parg)
0efa8c
						ferr = ARGV_ERROR_OPTARG_REQUIRED;
0efa8c
					else if (*parg && is_short_option(*parg))
0efa8c
						ferr = ARGV_ERROR_OPTARG_REQUIRED;
0efa8c
					else if (*parg && is_long_option(*parg))
0efa8c
						ferr = ARGV_ERROR_OPTARG_REQUIRED;
0efa8c
					else if (*parg && is_last_option(*parg))
0efa8c
						ferr = ARGV_ERROR_OPTARG_REQUIRED;
0efa8c
					else
0efa8c
						fval = true;
0efa8c
				} else {
0efa8c
					/* ARGV_OPTARG_OPTIONAL */
0efa8c
					fval = val[0];
0efa8c
				}
0efa8c
			} else
0efa8c
				ferr = ARGV_ERROR_LONG_OPTION;
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
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;
0efa8c
		} else 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
			}
0efa8c
		} else if (ctx->mode == ARGV_MODE_COPY) {
0efa8c
			if (fnoscan) {
0efa8c
				if (fval) {
0efa8c
					mentry->arg	= ch;
0efa8c
					mentry->fnoscan = true;
0efa8c
					mentry++;
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
				mentry++;
0efa8c
0efa8c
				if (fval) {
0efa8c
					parg++;
0efa8c
					ch = *parg;
0efa8c
				}
0efa8c
			} else {
0efa8c
				mentry->arg = ch;
0efa8c
				mentry++;
0efa8c
				parg++;
0efa8c
				ch = *parg;
0efa8c
			}
0efa8c
		}
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
{
9b23a2
	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
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] ? "," : "",
0efa8c
				ctx->erropt->long_name ? "--" : "",
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
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
{
0efa8c
	int				argc;
0efa8c
	char **				argv;
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");
0efa8c
	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);
0efa8c
		} else
e754e3
			argv_dprintf(fd,"<program arg> := %s\n",entry->arg);
0efa8c
e754e3
	argv_dprintf(fd,"\n\n");
0efa8c
}
0efa8c
0efa8c
static struct argv_meta * argv_free_impl(struct argv_meta_impl * imeta)
0efa8c
{
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);
0efa8c
		else 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;
0efa8c
	} else
0efa8c
		imeta->meta.argv = argv;
0efa8c
0efa8c
	if (!(imeta->meta.entries = calloc(
0efa8c
				ctx->nentries+1,
0efa8c
				sizeof(struct argv_entry))))
0efa8c
		return argv_free_impl(imeta);
0efa8c
	else
0efa8c
		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;
0efa8c
	struct argv_ctx		ctx = {flags,ARGV_MODE_SCAN,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
0efa8c
static void argv_usage(
e754e3
	int				fd,
0efa8c
	const char *    		header,
97d3f3
	const struct argv_option **	options,
0efa8c
	const char *			mode)
0efa8c
{
97d3f3
	const struct argv_option **	optv;
0efa8c
	const struct argv_option *	option;
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;
0efa8c
	bool				fcolor;
0efa8c
0efa8c
	fshort = mode ? !strcmp(mode,"short") : 0;
0efa8c
	flong  = fshort ? 0 : mode && !strcmp(mode,"long");
0efa8c
	fboth  = !fshort && !flong;
e754e3
	fcolor = isatty(fd);
0efa8c
0efa8c
	if (fcolor)
e754e3
		argv_dprintf(fd,"%s%s",cbold,cgreen);
0efa8c
0efa8c
	if (header)
e754e3
		argv_dprintf(fd,"%s",header);
0efa8c
97d3f3
	for (optlen=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;
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;
e754e3
			argv_dprintf(fd,color);
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
0efa8c
				? " -" : "--";
0efa8c
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
0efa8c
#endif
0efa8c
0efa8c
#endif