Blame src/internal/argv/argv.h

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