Blame src/internal/argv/argv.h

9ca8c4
/****************************************************************************/
9ca8c4
/*  argv.h: a thread-safe argument vector parser and usage screen generator */
6803d8
/*  Copyright (C) 2015--2018  Z. Gilboa                                     */
0520e3
/*  Released under the Standard MIT License; see COPYING.SLIBTOOL.          */
9ca8c4
/****************************************************************************/
9ca8c4
9ca8c4
#ifndef ARGV_H
9ca8c4
#define ARGV_H
9ca8c4
9ca8c4
#include <stdbool.h>
9ca8c4
#include <stdint.h>
9ca8c4
#include <stddef.h>
9ca8c4
#include <string.h>
9ca8c4
#include <stdlib.h>
9ca8c4
#include <stdio.h>
f1dd4b
#include <unistd.h>
9ca8c4
9ca8c4
#define ARGV_VERBOSITY_NONE		0x00
9ca8c4
#define ARGV_VERBOSITY_ERRORS		0x01
9ca8c4
#define ARGV_VERBOSITY_STATUS		0x02
9ca8c4
#define ARGV_CLONE_VECTOR		0x80
9ca8c4
9ca8c4
#ifndef ARGV_TAB_WIDTH
9ca8c4
#define ARGV_TAB_WIDTH			8
9ca8c4
#endif
9ca8c4
9ca8c4
/*******************************************/
9ca8c4
/*                                         */
9ca8c4
/* support of hybrid options               */
9ca8c4
/* -------------------------               */
9ca8c4
/* hybrid options are very similar to      */
9ca8c4
/* long options, yet are prefixed by       */
9ca8c4
/* a single dash rather than two           */
9ca8c4
/* (i.e. -std, -isystem).                  */
9ca8c4
/* hybrid options are supported by this    */
9ca8c4
/* driver for compatibility with legacy    */
9ca8c4
/* tools; note, however, that the use      */
9ca8c4
/* of hybrid options should be strongly    */
9ca8c4
/* discouraged due to the limitations      */
9ca8c4
/* they impose on short options (for       */
9ca8c4
/* example, a driver implementing -std     */
9ca8c4
/* may not provide -s as a short option    */
9ca8c4
/* that takes an arbitrary value).         */
9ca8c4
/*                                         */
9ca8c4
/* SPACE: -hybrid VALUE (i.e. -MF file)    */
9ca8c4
/* EQUAL: -hybrid=VALUE (i.e. -std=c99)    */
9ca8c4
/* COMMA: -hybrid,VALUE (i.e. -Wl,<arg>)   */
9ca8c4
/* ONLY:  -opt accepted, --opt rejected    */
9ca8c4
/* JOINED: -optVALUE                       */
9ca8c4
/*                                         */
9ca8c4
/*******************************************/
9ca8c4
9ca8c4
9ca8c4
#define ARGV_OPTION_HYBRID_NONE		0x00
9ca8c4
#define ARGV_OPTION_HYBRID_ONLY		0x01
9ca8c4
#define ARGV_OPTION_HYBRID_SPACE	0x02
9ca8c4
#define ARGV_OPTION_HYBRID_EQUAL	0x04
9ca8c4
#define ARGV_OPTION_HYBRID_COMMA	0x08
9ca8c4
#define ARGV_OPTION_HYBRID_JOINED	0x10
9ca8c4
#define ARGV_OPTION_HYBRID_CIRCUS	(ARGV_OPTION_HYBRID_SPACE \
9ca8c4
					| ARGV_OPTION_HYBRID_JOINED)
9ca8c4
#define ARGV_OPTION_HYBRID_DUAL		(ARGV_OPTION_HYBRID_SPACE \
9ca8c4
					| ARGV_OPTION_HYBRID_EQUAL)
519dd2
#define ARGV_OPTION_HYBRID_SWITCH	(ARGV_OPTION_HYBRID_ONLY \
519dd2
					| ARGV_OPTION_HYBRID_SPACE \
9ca8c4
					| ARGV_OPTION_HYBRID_EQUAL \
9ca8c4
					| ARGV_OPTION_HYBRID_COMMA \
9ca8c4
					| ARGV_OPTION_HYBRID_JOINED)
9ca8c4
9ca8c4
enum argv_optarg {
9ca8c4
	ARGV_OPTARG_NONE,
9ca8c4
	ARGV_OPTARG_REQUIRED,
9ca8c4
	ARGV_OPTARG_OPTIONAL,
9ca8c4
};
9ca8c4
9ca8c4
enum argv_mode {
9ca8c4
	ARGV_MODE_SCAN,
9ca8c4
	ARGV_MODE_COPY,
9ca8c4
};
9ca8c4
9ca8c4
enum argv_error {
9ca8c4
	ARGV_ERROR_OK,
9ca8c4
	ARGV_ERROR_INTERNAL,
9ca8c4
	ARGV_ERROR_SHORT_OPTION,
9ca8c4
	ARGV_ERROR_LONG_OPTION,
9ca8c4
	ARGV_ERROR_OPTARG_NONE,
9ca8c4
	ARGV_ERROR_OPTARG_REQUIRED,
9ca8c4
	ARGV_ERROR_OPTARG_PARADIGM,
9ca8c4
	ARGV_ERROR_HYBRID_NONE,
9ca8c4
	ARGV_ERROR_HYBRID_ONLY,
9ca8c4
	ARGV_ERROR_HYBRID_SPACE,
9ca8c4
	ARGV_ERROR_HYBRID_EQUAL,
9ca8c4
	ARGV_ERROR_HYBRID_COMMA,
9ca8c4
};
9ca8c4
9ca8c4
struct argv_option {
9ca8c4
	const char *		long_name;
9ca8c4
	const char		short_name;
9ca8c4
	int			tag;
9ca8c4
	enum argv_optarg	optarg;
9ca8c4
	int			flags;
9ca8c4
	const char *		paradigm;
9ca8c4
	const char *		argname;
9ca8c4
	const char *		description;
9ca8c4
};
9ca8c4
9ca8c4
struct argv_entry {
9ca8c4
	const char *	arg;
9ca8c4
	int		tag;
9ca8c4
	bool		fopt;
9ca8c4
	bool		fval;
9ca8c4
	bool		fnoscan;
9ca8c4
	enum argv_error errcode;
9ca8c4
};
9ca8c4
9ca8c4
struct argv_meta {
9ca8c4
	char **			argv;
9ca8c4
	struct argv_entry *	entries;
9ca8c4
};
9ca8c4
9ca8c4
struct argv_ctx {
9ca8c4
	int				flags;
9ca8c4
	int				mode;
9ca8c4
	int				nentries;
7869b1
	intptr_t			unitidx;
7869b1
	intptr_t			erridx;
9ca8c4
	enum argv_error 		errcode;
9ca8c4
	const char *			errch;
9ca8c4
	const struct argv_option *	erropt;
9ca8c4
	const char *			program;
9ca8c4
};
9ca8c4
9ca8c4
#ifdef ARGV_DRIVER
9ca8c4
23e9d1
struct argv_meta_impl {
23e9d1
	char **			argv;
23e9d1
	char *			strbuf;
23e9d1
	struct argv_meta	meta;
23e9d1
};
23e9d1
d58d2f
static int argv_optv_init(
d58d2f
	const struct argv_option[],
d58d2f
	const struct argv_option **);
d58d2f
9ca8c4
static const char * argv_program_name(const char *);
9ca8c4
9ca8c4
static void argv_usage(
93f9e4
	int		fd,
9ca8c4
	const char *	header,
d58d2f
	const struct	argv_option **,
9ca8c4
	const char *	mode);
9ca8c4
9ca8c4
static struct argv_meta * argv_get(
9ca8c4
	char **,
d58d2f
	const struct argv_option **,
93f9e4
	int flags,
93f9e4
	int fd);
9ca8c4
9ca8c4
static void argv_free(struct argv_meta *);
9ca8c4
93f9e4
#ifndef argv_dprintf
93f9e4
#define argv_dprintf dprintf
93f9e4
#endif
9ca8c4
9ca8c4
9ca8c4
9ca8c4
/*------------------------------------*/
9ca8c4
/* implementation of static functions */
9ca8c4
/*------------------------------------*/
9ca8c4
d58d2f
static int argv_optv_init(
d58d2f
	const struct argv_option	options[],
d58d2f
	const struct argv_option **	optv)
d58d2f
{
d58d2f
	const struct argv_option *	option;
d58d2f
	int				i;
d58d2f
d58d2f
	for (option=options,i=0; option->long_name || option->short_name; option++)
d58d2f
		optv[i++] = option;
d58d2f
d58d2f
	optv[i] = 0;
d58d2f
	return i;
d58d2f
}
d58d2f
9ca8c4
static const struct argv_option * argv_short_option(
9ca8c4
	const char *			ch,
d58d2f
	const struct argv_option **	optv,
9ca8c4
	struct argv_entry *		entry)
9ca8c4
{
9ca8c4
	const struct argv_option *	option;
9ca8c4
d58d2f
	for (; *optv; optv++) {
d58d2f
		option = *optv;
d58d2f
9ca8c4
		if (option->short_name == *ch) {
9ca8c4
			entry->tag	= option->tag;
9ca8c4
			entry->fopt	= true;
9ca8c4
			return option;
9ca8c4
		}
9ca8c4
	}
9ca8c4
9ca8c4
	return 0;
9ca8c4
}
9ca8c4
9ca8c4
static const struct argv_option * argv_long_option(
9ca8c4
	const char *			ch,
d58d2f
	const struct argv_option **	optv,
9ca8c4
	struct argv_entry *		entry)
9ca8c4
{
9ca8c4
	const struct argv_option *	option;
9ca8c4
	const char *			arg;
9ca8c4
	size_t				len;
9ca8c4
d58d2f
	for (; *optv; optv++) {
d58d2f
		option = *optv;
d58d2f
		len    = option->long_name ? strlen(option->long_name) : 0;
9ca8c4
9ca8c4
		if (len && !(strncmp(option->long_name,ch,len))) {
9ca8c4
			arg = ch + len;
9ca8c4
9ca8c4
			if (!*arg
9ca8c4
				|| (*arg == '=')
9ca8c4
				|| (option->flags & ARGV_OPTION_HYBRID_JOINED)
9ca8c4
				|| ((option->flags & ARGV_OPTION_HYBRID_COMMA)
9ca8c4
					&& (*arg == ','))) {
9ca8c4
				entry->tag	= option->tag;
9ca8c4
				entry->fopt	= true;
9ca8c4
				return option;
9ca8c4
			}
9ca8c4
		}
9ca8c4
	}
9ca8c4
9ca8c4
	return 0;
9ca8c4
}
9ca8c4
9ca8c4
static inline bool is_short_option(const char * arg)
9ca8c4
{
9ca8c4
	return (arg[0]=='-') && arg[1] && (arg[1]!='-');
9ca8c4
}
9ca8c4
9ca8c4
static inline bool is_long_option(const char * arg)
9ca8c4
{
9ca8c4
	return (arg[0]=='-') && (arg[1]=='-') && arg[2];
9ca8c4
}
9ca8c4
9ca8c4
static inline bool is_last_option(const char * arg)
9ca8c4
{
9ca8c4
	return (arg[0]=='-') && (arg[1]=='-') && !arg[2];
9ca8c4
}
9ca8c4
9ca8c4
static inline bool is_hybrid_option(
9ca8c4
	const char *			arg,
d58d2f
	const struct argv_option **	optv)
9ca8c4
{
9ca8c4
	const struct argv_option *	option;
9ca8c4
	struct argv_entry		entry;
9ca8c4
9ca8c4
	if (!is_short_option(arg))
9ca8c4
		return false;
9ca8c4
d58d2f
	if (!(option = argv_long_option(++arg,optv,&entry)))
9ca8c4
		return false;
9ca8c4
9ca8c4
	if (!(option->flags & ARGV_OPTION_HYBRID_SWITCH))
d58d2f
		if (argv_short_option(arg,optv,&entry))
9ca8c4
			return false;
9ca8c4
9ca8c4
	return true;
9ca8c4
}
9ca8c4
9ca8c4
static inline bool is_arg_in_paradigm(const char * arg, const char * paradigm)
9ca8c4
{
9ca8c4
	size_t		len;
9ca8c4
	const char *	ch;
9ca8c4
9ca8c4
	for (ch=paradigm,len=strlen(arg); ch; ) {
9ca8c4
		if (!strncmp(arg,ch,len)) {
9ca8c4
			if (!*(ch += len))
9ca8c4
				return true;
9ca8c4
			else if (*ch == '|')
9ca8c4
				return true;
9ca8c4
		}
9ca8c4
9ca8c4
		if ((ch = strchr(ch,'|')))
9ca8c4
			ch++;
9ca8c4
	}
9ca8c4
9ca8c4
	return false;
9ca8c4
}
9ca8c4
9ca8c4
static inline const struct argv_option * option_from_tag(
d58d2f
	const struct argv_option **	optv,
9ca8c4
	int				tag)
9ca8c4
{
d58d2f
	for (; *optv; optv++)
d58d2f
		if (optv[0]->tag == tag)
d58d2f
			return optv[0];
9ca8c4
	return 0;
9ca8c4
}
9ca8c4
9ca8c4
static void argv_scan(
9ca8c4
	char **				argv,
d58d2f
	const struct argv_option **	optv,
9ca8c4
	struct argv_ctx *		ctx,
9ca8c4
	struct argv_meta *		meta)
9ca8c4
{
9ca8c4
	char **				parg;
9ca8c4
	const char *			ch;
9ca8c4
	const char *			val;
9ca8c4
	const struct argv_option *	option;
9ca8c4
	struct argv_entry		entry;
9ca8c4
	struct argv_entry *		mentry;
9ca8c4
	enum argv_error			ferr;
9ca8c4
	bool				fval;
9ca8c4
	bool				fnext;
9ca8c4
	bool				fshort;
9ca8c4
	bool				fhybrid;
9ca8c4
	bool				fnoscan;
9ca8c4
9ca8c4
	parg	= &argv[1];
9ca8c4
	ch	= *parg;
9ca8c4
	ferr	= ARGV_ERROR_OK;
9ca8c4
	fshort	= false;
9ca8c4
	fnoscan	= false;
9ca8c4
	fval	= false;
9ca8c4
	mentry	= meta ? meta->entries : 0;
9ca8c4
9ca8c4
	ctx->unitidx = 0;
9ca8c4
	ctx->erridx  = 0;
9ca8c4
9ca8c4
	while (ch && (ferr == ARGV_ERROR_OK)) {
9ca8c4
		option  = 0;
9ca8c4
		fhybrid = false;
9ca8c4
9ca8c4
		if (fnoscan)
9ca8c4
			fval = true;
9ca8c4
9ca8c4
		else if (is_last_option(ch))
9ca8c4
			fnoscan = true;
9ca8c4
d58d2f
		else if (!fshort && is_hybrid_option(ch,optv))
9ca8c4
			fhybrid = true;
9ca8c4
9ca8c4
		if (!fnoscan && !fhybrid && (fshort || is_short_option(ch))) {
9ca8c4
			if (!fshort)
9ca8c4
				ch++;
9ca8c4
d58d2f
			if ((option = argv_short_option(ch,optv,&entry))) {
9ca8c4
				if (ch[1]) {
9ca8c4
					ch++;
9ca8c4
					fnext	= false;
9ca8c4
					fshort	= (option->optarg == ARGV_OPTARG_NONE);
9ca8c4
				} else {
9ca8c4
					parg++;
9ca8c4
					ch	= *parg;
9ca8c4
					fnext	= true;
9ca8c4
					fshort	= false;
9ca8c4
				}
9ca8c4
9ca8c4
				if (option->optarg == ARGV_OPTARG_NONE) {
3438e6
					if (!fnext && ch && (*ch == '-')) {
9ca8c4
						ferr = ARGV_ERROR_OPTARG_NONE;
3438e6
					} else {
9ca8c4
						fval = false;
3438e6
					}
3438e6
3438e6
				} else if (!fnext) {
9ca8c4
					fval = true;
3438e6
3438e6
				} else if (option->optarg == ARGV_OPTARG_REQUIRED) {
9ca8c4
					if (ch && is_short_option(ch))
9ca8c4
						ferr = ARGV_ERROR_OPTARG_REQUIRED;
9ca8c4
					else if (ch && is_long_option(ch))
9ca8c4
						ferr = ARGV_ERROR_OPTARG_REQUIRED;
9ca8c4
					else if (ch && is_last_option(ch))
9ca8c4
						ferr = ARGV_ERROR_OPTARG_REQUIRED;
9ca8c4
					else if (ch)
9ca8c4
						fval = true;
9ca8c4
					else
9ca8c4
						ferr = ARGV_ERROR_OPTARG_REQUIRED;
9ca8c4
				} else {
9ca8c4
					/* ARGV_OPTARG_OPTIONAL */
9ca8c4
					if (ch && is_short_option(ch))
9ca8c4
						fval = false;
9ca8c4
					else if (ch && is_long_option(ch))
9ca8c4
						fval = false;
9ca8c4
					else if (ch && is_last_option(ch))
9ca8c4
						fval = false;
44416d
					else if (fnext)
44416d
						fval = false;
9ca8c4
					else
9ca8c4
						fval = ch;
9ca8c4
				}
3438e6
			} else {
9ca8c4
				ferr = ARGV_ERROR_SHORT_OPTION;
3438e6
			}
9ca8c4
9ca8c4
		} else if (!fnoscan && (fhybrid || is_long_option(ch))) {
9ca8c4
			ch += (fhybrid ? 1 : 2);
9ca8c4
d58d2f
			if ((option = argv_long_option(ch,optv,&entry))) {
9ca8c4
				val = ch + strlen(option->long_name);
9ca8c4
9ca8c4
				/* val[0] is either '=' (or ',') or '\0' */
9ca8c4
				if (!val[0]) {
9ca8c4
					parg++;
9ca8c4
					ch = *parg;
9ca8c4
				}
9ca8c4
9ca8c4
				if (fhybrid && !(option->flags & ARGV_OPTION_HYBRID_SWITCH))
9ca8c4
					ferr = ARGV_ERROR_HYBRID_NONE;
600e85
				else if (!fhybrid && (option->flags & ARGV_OPTION_HYBRID_ONLY))
600e85
					ferr = ARGV_ERROR_HYBRID_ONLY;
9ca8c4
				else if (option->optarg == ARGV_OPTARG_NONE) {
9ca8c4
					if (val[0]) {
9ca8c4
						ferr = ARGV_ERROR_OPTARG_NONE;
9ca8c4
						ctx->errch = val + 1;
9ca8c4
					} else
9ca8c4
						fval = false;
600e85
				} else if (val[0] && (option->flags & ARGV_OPTION_HYBRID_JOINED)) {
9ca8c4
					fval = true;
9ca8c4
					ch   = val;
9ca8c4
				} else if (fhybrid && !val[0] && !(option->flags & ARGV_OPTION_HYBRID_SPACE))
9ca8c4
					ferr = ARGV_ERROR_HYBRID_SPACE;
9ca8c4
				else if (fhybrid && (val[0]=='=') && !(option->flags & ARGV_OPTION_HYBRID_EQUAL))
9ca8c4
					ferr = ARGV_ERROR_HYBRID_EQUAL;
9ca8c4
				else if (fhybrid && (val[0]==',') && !(option->flags & ARGV_OPTION_HYBRID_COMMA))
9ca8c4
					ferr = ARGV_ERROR_HYBRID_COMMA;
9ca8c4
				else if (!fhybrid && (val[0]==','))
9ca8c4
					ferr = ARGV_ERROR_HYBRID_COMMA;
9ca8c4
				else if (val[0] && !val[1])
9ca8c4
					ferr = ARGV_ERROR_OPTARG_REQUIRED;
9ca8c4
				else if (val[0] && val[1]) {
9ca8c4
					fval = true;
9ca8c4
					ch   = ++val;
9ca8c4
				} else if (option->optarg == ARGV_OPTARG_REQUIRED) {
9ca8c4
					if (!val[0] && !*parg)
9ca8c4
						ferr = ARGV_ERROR_OPTARG_REQUIRED;
9ca8c4
					else if (*parg && is_short_option(*parg))
9ca8c4
						ferr = ARGV_ERROR_OPTARG_REQUIRED;
9ca8c4
					else if (*parg && is_long_option(*parg))
9ca8c4
						ferr = ARGV_ERROR_OPTARG_REQUIRED;
9ca8c4
					else if (*parg && is_last_option(*parg))
9ca8c4
						ferr = ARGV_ERROR_OPTARG_REQUIRED;
9ca8c4
					else
9ca8c4
						fval = true;
9ca8c4
				} else {
9ca8c4
					/* ARGV_OPTARG_OPTIONAL */
9ca8c4
					fval = val[0];
9ca8c4
				}
9ca8c4
			} else
9ca8c4
				ferr = ARGV_ERROR_LONG_OPTION;
9ca8c4
		}
9ca8c4
9ca8c4
		if (ferr == ARGV_ERROR_OK)
9ca8c4
			if (option && fval && option->paradigm)
9ca8c4
				if (!is_arg_in_paradigm(ch,option->paradigm))
9ca8c4
					ferr = ARGV_ERROR_OPTARG_PARADIGM;
9ca8c4
9ca8c4
		if (ferr == ARGV_ERROR_OK)
9ca8c4
			if (!option && !ctx->unitidx)
9ca8c4
				ctx->unitidx = parg - argv;
9ca8c4
9ca8c4
		if (ferr != ARGV_ERROR_OK) {
9ca8c4
			ctx->errcode = ferr;
9ca8c4
			ctx->errch   = ctx->errch ? ctx->errch : ch;
9ca8c4
			ctx->erropt  = option;
9ca8c4
			ctx->erridx  = parg - argv;
9ca8c4
			return;
9ca8c4
		} else if (ctx->mode == ARGV_MODE_SCAN) {
9ca8c4
			if (!fnoscan)
9ca8c4
				ctx->nentries++;
9ca8c4
			else if (fval)
9ca8c4
				ctx->nentries++;
9ca8c4
9ca8c4
			if (fval || !option) {
9ca8c4
				parg++;
9ca8c4
				ch = *parg;
9ca8c4
			}
9ca8c4
		} else if (ctx->mode == ARGV_MODE_COPY) {
9ca8c4
			if (fnoscan) {
9ca8c4
				if (fval) {
9ca8c4
					mentry->arg	= ch;
9ca8c4
					mentry->fnoscan = true;
9ca8c4
					mentry++;
9ca8c4
				}
9ca8c4
9ca8c4
				parg++;
9ca8c4
				ch = *parg;
9ca8c4
			} else if (option) {
9ca8c4
				mentry->arg	= fval ? ch : 0;
9ca8c4
				mentry->tag	= option->tag;
9ca8c4
				mentry->fopt	= true;
9ca8c4
				mentry->fval	= fval;
9ca8c4
				mentry++;
9ca8c4
9ca8c4
				if (fval) {
9ca8c4
					parg++;
9ca8c4
					ch = *parg;
9ca8c4
				}
9ca8c4
			} else {
9ca8c4
				mentry->arg = ch;
9ca8c4
				mentry++;
9ca8c4
				parg++;
9ca8c4
				ch = *parg;
9ca8c4
			}
9ca8c4
		}
9ca8c4
	}
9ca8c4
}
9ca8c4
9ca8c4
static const char * argv_program_name(const char * program_path)
9ca8c4
{
9ca8c4
	const char * ch;
9ca8c4
9ca8c4
	if (program_path) {
9ca8c4
		if ((ch = strrchr(program_path,'/')))
9ca8c4
			return *(++ch) ? ch : 0;
9ca8c4
9ca8c4
		if ((ch = strrchr(program_path,'\\')))
9ca8c4
			return *(++ch) ? ch : 0;
9ca8c4
	}
9ca8c4
9ca8c4
	return program_path;
9ca8c4
}
9ca8c4
93f9e4
static void argv_show_error(int fd, struct argv_ctx * ctx)
9ca8c4
{
97a847
	char opt_short_name[2] = {0,0};
97a847
97a847
	if (ctx->erropt && ctx->erropt->short_name)
97a847
		opt_short_name[0] = ctx->erropt->short_name;
97a847
93f9e4
	argv_dprintf(fd,"%s: error: ",ctx->program);
9ca8c4
9ca8c4
	switch (ctx->errcode) {
9ca8c4
		case ARGV_ERROR_SHORT_OPTION:
93f9e4
			argv_dprintf(fd,"'-%c' is not a valid short option\n",*ctx->errch);
9ca8c4
			break;
9ca8c4
9ca8c4
		case ARGV_ERROR_LONG_OPTION:
93f9e4
			argv_dprintf(fd,"'--%s' is not a valid long option\n",ctx->errch);
9ca8c4
			break;
9ca8c4
9ca8c4
		case ARGV_ERROR_OPTARG_NONE:
93f9e4
			argv_dprintf(fd,"'%s' is not a valid option value for [%s%s%s%s%s] "
9ca8c4
					"(option values may not be specified)\n",
9ca8c4
				ctx->errch,
97a847
				opt_short_name[0] ? "-" : "",
97a847
				opt_short_name,
97a847
				opt_short_name[0] ? "," : "",
9ca8c4
				ctx->erropt->long_name ? "--" : "",
9ca8c4
				ctx->erropt->long_name);
9ca8c4
			break;
9ca8c4
9ca8c4
		case ARGV_ERROR_OPTARG_REQUIRED:
93f9e4
			argv_dprintf(fd,"option [%s%s%s%s%s] requires %s %s%s%s\n",
97a847
				opt_short_name[0] ? "-" : "",
97a847
				opt_short_name,
97a847
				opt_short_name[0] ? "," : "",
9ca8c4
				ctx->erropt->long_name ? "--" : "",
9ca8c4
				ctx->erropt->long_name,
9ca8c4
				ctx->erropt->paradigm ? "one of the following values:" : "a value",
9ca8c4
				ctx->erropt->paradigm ? "{" : "",
9ca8c4
				ctx->erropt->paradigm ? ctx->erropt->paradigm : "",
9ca8c4
				ctx->erropt->paradigm ? "}" : "");
9ca8c4
			break;
9ca8c4
9ca8c4
		case ARGV_ERROR_OPTARG_PARADIGM:
93f9e4
			argv_dprintf(fd,"'%s' is not a valid option value for [%s%s%s%s%s]={%s}\n",
9ca8c4
				ctx->errch,
97a847
				opt_short_name[0] ? "-" : "",
97a847
				opt_short_name,
97a847
				opt_short_name[0] ? "," : "",
9ca8c4
				ctx->erropt->long_name ? "--" : "",
9ca8c4
				ctx->erropt->long_name,
9ca8c4
				ctx->erropt->paradigm);
9ca8c4
			break;
9ca8c4
9ca8c4
		case ARGV_ERROR_HYBRID_NONE:
93f9e4
			argv_dprintf(fd,"-%s is not a synonym for --%s\n",
9ca8c4
				ctx->erropt->long_name,
9ca8c4
				ctx->erropt->long_name);
9ca8c4
			break;
9ca8c4
9ca8c4
		case ARGV_ERROR_HYBRID_ONLY:
93f9e4
			argv_dprintf(fd,"--%s is not a synonym for -%s\n",
9ca8c4
				ctx->erropt->long_name,
9ca8c4
				ctx->erropt->long_name);
9ca8c4
			break;
9ca8c4
9ca8c4
		case ARGV_ERROR_HYBRID_SPACE:
9ca8c4
		case ARGV_ERROR_HYBRID_EQUAL:
9ca8c4
		case ARGV_ERROR_HYBRID_COMMA:
93f9e4
			argv_dprintf(fd,"-%s: illegal value assignment; valid syntax is "
9ca8c4
					"-%s%sVAL\n",
9ca8c4
				ctx->erropt->long_name,
9ca8c4
				ctx->erropt->long_name,
9ca8c4
				(ctx->erropt->flags & ARGV_OPTION_HYBRID_SPACE)
9ca8c4
					? " " : (ctx->erropt->flags & ARGV_OPTION_HYBRID_EQUAL)
9ca8c4
					? "=" : (ctx->erropt->flags & ARGV_OPTION_HYBRID_COMMA)
9ca8c4
					? "," : "");
9ca8c4
9ca8c4
			break;
9ca8c4
9ca8c4
		case ARGV_ERROR_INTERNAL:
93f9e4
			argv_dprintf(fd,"internal error");
9ca8c4
			break;
9ca8c4
9ca8c4
		default:
9ca8c4
			break;
9ca8c4
	}
9ca8c4
}
9ca8c4
9ca8c4
static void argv_show_status(
93f9e4
	int				fd,
d58d2f
	const struct argv_option **	optv,
9ca8c4
	struct argv_ctx *		ctx,
9ca8c4
	struct argv_meta *		meta)
9ca8c4
{
9ca8c4
	int				argc;
9ca8c4
	char **				argv;
9ca8c4
	struct argv_entry *		entry;
9ca8c4
	const struct argv_option *	option;
9ca8c4
	char				short_name[2] = {0};
9ca8c4
	const char *			space = "";
9ca8c4
d3ca02
	(void)ctx;
d3ca02
93f9e4
	argv_dprintf(fd,"\n\nconcatenated command line:\n");
9ca8c4
	for (argv=meta->argv; *argv; argv++) {
93f9e4
		argv_dprintf(fd,"%s%s",space,*argv);
9ca8c4
		space = " ";
9ca8c4
	}
9ca8c4
93f9e4
	argv_dprintf(fd,"\n\nargument vector:\n");
9ca8c4
	for (argc=0,argv=meta->argv; *argv; argc++,argv++)
93f9e4
		argv_dprintf(fd,"argv[%d]: %s\n",argc,*argv);
9ca8c4
93f9e4
	argv_dprintf(fd,"\n\nparsed entries:\n");
9ca8c4
	for (entry=meta->entries; entry->arg || entry->fopt; entry++)
9ca8c4
		if (entry->fopt) {
d58d2f
			option = option_from_tag(optv,entry->tag);
9ca8c4
			short_name[0] = option->short_name;
9ca8c4
9ca8c4
			if (entry->fval)
93f9e4
				argv_dprintf(fd,"[-%s,--%s] := %s\n",
9ca8c4
					short_name,option->long_name,entry->arg);
9ca8c4
			else
93f9e4
				argv_dprintf(fd,"[-%s,--%s]\n",
9ca8c4
					short_name,option->long_name);
9ca8c4
		} else
93f9e4
			argv_dprintf(fd,"<program arg> := %s\n",entry->arg);
9ca8c4
93f9e4
	argv_dprintf(fd,"\n\n");
9ca8c4
}
9ca8c4
9ca8c4
static struct argv_meta * argv_free_impl(struct argv_meta_impl * imeta)
9ca8c4
{
9ca8c4
	if (imeta->argv)
9ca8c4
		free(imeta->argv);
9ca8c4
9ca8c4
	if (imeta->strbuf)
9ca8c4
		free(imeta->strbuf);
9ca8c4
9ca8c4
	if (imeta->meta.entries)
9ca8c4
		free(imeta->meta.entries);
9ca8c4
9ca8c4
	free(imeta);
9ca8c4
	return 0;
9ca8c4
}
9ca8c4
9ca8c4
static struct argv_meta * argv_alloc(char ** argv, struct argv_ctx * ctx)
9ca8c4
{
9ca8c4
	struct argv_meta_impl * imeta;
9ca8c4
	char **			vector;
9ca8c4
	char *			dst;
9ca8c4
	size_t			size;
9ca8c4
	int			argc;
9ca8c4
	int			i;
9ca8c4
9ca8c4
	if (!(imeta = calloc(1,sizeof(*imeta))))
9ca8c4
		return 0;
9ca8c4
9ca8c4
	if (ctx->flags & ARGV_CLONE_VECTOR) {
9ca8c4
		for (vector=argv,argc=0,size=0; *vector; vector++) {
9ca8c4
			size += strlen(*vector) + 1;
9ca8c4
			argc++;
9ca8c4
		}
9ca8c4
9ca8c4
		if (!(imeta->argv = calloc(argc+1,sizeof(char *))))
9ca8c4
			return argv_free_impl(imeta);
9ca8c4
		else if (!(imeta->strbuf = calloc(1,size+1)))
9ca8c4
			return argv_free_impl(imeta);
9ca8c4
9ca8c4
		for (i=0,dst=imeta->strbuf; i
9ca8c4
			strcpy(dst,argv[i]);
9ca8c4
			imeta->argv[i] = dst;
9ca8c4
			dst += strlen(dst)+1;
9ca8c4
		}
9ca8c4
9ca8c4
		imeta->meta.argv = imeta->argv;
9ca8c4
	} else
9ca8c4
		imeta->meta.argv = argv;
9ca8c4
9ca8c4
	if (!(imeta->meta.entries = calloc(
9ca8c4
				ctx->nentries+1,
9ca8c4
				sizeof(struct argv_entry))))
9ca8c4
		return argv_free_impl(imeta);
9ca8c4
	else
9ca8c4
		return &imeta->meta;
9ca8c4
}
9ca8c4
9ca8c4
static struct argv_meta * argv_get(
cd7b15
	char **				argv,
d58d2f
	const struct argv_option **	optv,
93f9e4
	int				flags,
93f9e4
	int				fd)
9ca8c4
{
9ca8c4
	struct argv_meta *	meta;
9ca8c4
	struct argv_ctx		ctx = {flags,ARGV_MODE_SCAN,0,0,0,0,0,0,0};
9ca8c4
d58d2f
	argv_scan(argv,optv,&ctx,0);
9ca8c4
9ca8c4
	if (ctx.errcode != ARGV_ERROR_OK) {
5fe98f
		ctx.program = argv_program_name(argv[0]);
9ca8c4
9ca8c4
		if (ctx.flags & ARGV_VERBOSITY_ERRORS)
93f9e4
			argv_show_error(fd,&ctx;;
9ca8c4
9ca8c4
		return 0;
9ca8c4
	}
9ca8c4
9ca8c4
	if (!(meta = argv_alloc(argv,&ctx)))
9ca8c4
		return 0;
9ca8c4
9ca8c4
	ctx.mode = ARGV_MODE_COPY;
d58d2f
	argv_scan(meta->argv,optv,&ctx,meta);
9ca8c4
9ca8c4
	if (ctx.errcode != ARGV_ERROR_OK) {
5fe98f
		ctx.program = argv[0];
9ca8c4
		ctx.errcode = ARGV_ERROR_INTERNAL;
93f9e4
		argv_show_error(fd,&ctx;;
9ca8c4
		argv_free(meta);
9ca8c4
9ca8c4
		return 0;
9ca8c4
	}
9ca8c4
9ca8c4
	if (ctx.flags & ARGV_VERBOSITY_STATUS)
93f9e4
		argv_show_status(fd,optv,&ctx,meta);
9ca8c4
9ca8c4
	return meta;
9ca8c4
}
9ca8c4
9ca8c4
static void argv_free(struct argv_meta * xmeta)
9ca8c4
{
9ca8c4
	struct argv_meta_impl * imeta;
9ca8c4
	uintptr_t		addr;
9ca8c4
9ca8c4
	if (xmeta) {
9ca8c4
		addr  = (uintptr_t)xmeta - offsetof(struct argv_meta_impl,meta);
9ca8c4
		imeta = (struct argv_meta_impl *)addr;
9ca8c4
		argv_free_impl(imeta);
9ca8c4
	}
9ca8c4
}
9ca8c4
9ca8c4
static void argv_usage(
93f9e4
	int				fd,
9ca8c4
	const char *    		header,
d58d2f
	const struct argv_option **	options,
9ca8c4
	const char *			mode)
9ca8c4
{
d58d2f
	const struct argv_option **	optv;
93f2e7
	const struct argv_option *	option;
93f2e7
	bool				fshort,flong,fboth;
93f2e7
	size_t				len,optlen,desclen;
93f2e7
	char				cache;
93f2e7
	char *				prefix;
93f2e7
	char *				desc;
93f2e7
	char *				mark;
93f2e7
	char *				cap;
93f2e7
	char				description[4096];
93f2e7
	char				optstr     [72];
93f2e7
	const size_t			optcap    = 64;
93f2e7
	const size_t			width     = 80;
93f2e7
	const char			indent[]  = "  ";
f1dd4b
	const char			creset[]  = "\x1b[0m";
f1dd4b
	const char			cbold []  = "\x1b[1m";
f1dd4b
	const char			cgreen[]  = "\x1b[32m";
f1dd4b
	const char			cblue []  = "\x1b[34m";
f1dd4b
	const char			ccyan []  = "\x1b[36m";
f1dd4b
	const char *			color     = ccyan;
f1dd4b
	bool				fcolor;
93f2e7
93f2e7
	fshort = mode ? !strcmp(mode,"short") : 0;
93f2e7
	flong  = fshort ? 0 : mode && !strcmp(mode,"long");
93f2e7
	fboth  = !fshort && !flong;
93f9e4
	fcolor = isatty(fd);
f1dd4b
f1dd4b
	if (fcolor)
93f9e4
		argv_dprintf(fd,"%s%s",cbold,cgreen);
93f2e7
93f2e7
	if (header)
93f9e4
		argv_dprintf(fd,"%s",header);
93f2e7
d58d2f
	for (optlen=0,optv=options; *optv; optv++) {
d58d2f
		option = *optv;
93f2e7
93f2e7
		/* indent + comma */
93f2e7
		len = fboth ? sizeof(indent) + 1 : sizeof(indent);
93f2e7
93f2e7
		/* -o */
93f2e7
		if (fshort || fboth)
93f2e7
			len += option->short_name
93f2e7
				? 2 : 0;
93f2e7
93f2e7
		/* --option */
93f2e7
		if (flong || fboth)
93f2e7
			len += option->long_name
93f2e7
				? 2 + strlen(option->long_name) : 0;
93f2e7
93f2e7
		/* optlen */
93f2e7
		if (len > optlen)
93f2e7
			optlen = len;
93f2e7
	}
93f2e7
93f2e7
	if (optlen >= optcap) {
93f9e4
		argv_dprintf(fd,
93f2e7
			"Option strings exceed %zu characters, "
93f2e7
			"please generate the usage screen manually.\n",
93f2e7
			optcap);
93f2e7
		return;
93f2e7
	}
93f2e7
93f2e7
	optlen += ARGV_TAB_WIDTH;
93f2e7
	optlen &= (~(ARGV_TAB_WIDTH-1));
93f2e7
	desclen = (optlen < width / 2) ? width - optlen : optlen;
93f2e7
d58d2f
	for (optv=options; *optv; optv++) {
d58d2f
		option = *optv;
d58d2f
f1dd4b
		/* color */
f1dd4b
		if (fcolor) {
f1dd4b
			color = (color == ccyan) ? cblue : ccyan;
93f9e4
			argv_dprintf(fd,color);
f1dd4b
		}
f1dd4b
93f2e7
		/* description, using either paradigm or argname if applicable */
93f2e7
		snprintf(description,sizeof(description),option->description,
93f2e7
			option->paradigm
93f2e7
				? option->paradigm
93f2e7
				: option->argname ? option->argname : "");
93f2e7
		description[sizeof(description)-1] = 0;
93f2e7
93f2e7
		/* long/hybrid option prefix (-/--) */
93f2e7
		prefix = option->flags & ARGV_OPTION_HYBRID_ONLY
93f2e7
				? " -" : "--";
93f2e7
93f2e7
		/* option string */
93f2e7
		if (fboth && option->short_name && option->long_name)
93f2e7
			sprintf(optstr,"%s-%c,%s%s",
93f2e7
				indent,option->short_name,prefix,option->long_name);
93f2e7
93f2e7
		else if ((fshort || fboth) && option->short_name)
5a0a23
			sprintf(optstr,"%s-%c",indent,option->short_name);
93f2e7
93f2e7
		else if (flong && option->long_name)
93f2e7
			sprintf(optstr,"%s%s%s",
93f2e7
				indent,prefix,option->long_name);
93f2e7
93f2e7
		else if (fboth && option->long_name)
93f2e7
			sprintf(optstr,"%s   %s%s",
93f2e7
				indent,prefix,option->long_name);
93f2e7
93f2e7
		else
93f2e7
			optstr[0] = 0;
93f2e7
93f2e7
		/* right-indented option buffer */
93f2e7
		if (description[0]) {
93f2e7
			len = strlen(optstr);
93f2e7
			sprintf(&optstr[len],"%-*c",(int)(optlen-len),' ');
93f2e7
		}
93f2e7
93f2e7
		/* single line? */
93f2e7
		if (optlen + strlen(description) < width) {
93f9e4
			argv_dprintf(fd,"%s%s\n",optstr,description);
93f2e7
93f2e7
		} else {
93f2e7
			desc = description;
93f2e7
			cap  = desc + strlen(description);
93f2e7
93f2e7
			while (desc < cap) {
93f2e7
				mark = (desc + desclen >= cap)
93f2e7
					? cap : desc + desclen;
93f2e7
93f2e7
				while (*mark && (mark > desc)
93f2e7
						&& (*mark != ' ')
7410b1
						&& (*mark != '|')
93f2e7
						&& (*mark != '\t')
93f2e7
						&& (*mark != '\n'))
93f2e7
					mark--;
93f2e7
93f2e7
				if (mark == desc) {
93f2e7
					mark = (desc + desclen >= cap)
93f2e7
						? cap : desc + desclen;
93f2e7
					cache = *mark;
93f2e7
					*mark = 0;
7410b1
				} else if (*mark == '|') {
7410b1
					cache = *mark;
7410b1
					*mark = 0;
93f2e7
				} else {
93f2e7
					cache = 0;
93f2e7
					*mark = 0;
93f2e7
				}
93f2e7
93f2e7
				/* first line? */
93f2e7
				if (desc == description)
93f9e4
					argv_dprintf(fd,"%s%s\n",optstr,desc);
93f2e7
				else
93f9e4
					argv_dprintf(fd,"%-*c %s\n",
7410b1
						(*desc == '|')
7410b1
							? (int)(optlen+1)
7410b1
							: (int)optlen,
7410b1
						' ',desc);
93f2e7
93f2e7
				if (cache)
93f2e7
					*mark = cache;
93f2e7
				else
93f2e7
					mark++;
93f2e7
93f2e7
				desc = mark;
93f2e7
			}
93f2e7
		}
93f2e7
	}
f1dd4b
f1dd4b
	if (fcolor)
93f9e4
		argv_dprintf(fd,creset);
9ca8c4
}
9ca8c4
9ca8c4
#endif
9ca8c4
9ca8c4
#endif