Blame src/internal/argv/argv.h

cde03b
/****************************************************************************/
cde03b
/*  argv.h: a thread-safe argument vector parser and usage screen generator */
3e816c
/*  Copyright (C) 2015--2016  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 <errno.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_meta_impl {
13aa9b
	char **			argv;
cde03b
	char *			strbuf;
cde03b
	struct argv_meta	meta;
cde03b
};
cde03b
cde03b
struct argv_ctx {
cde03b
	int				flags;
cde03b
	int				mode;
cde03b
	int				nentries;
d81b54
	int				unitidx;
d81b54
	int				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
cde03b
static const char * argv_program_name(const char *);
cde03b
cde03b
static void argv_usage(
cde03b
	FILE *,
cde03b
	const char *	header,
cde03b
	const struct	argv_option[],
cde03b
	const char *	mode);
cde03b
cde03b
static struct argv_meta * argv_get(
13aa9b
	char **,
cde03b
	const struct argv_option[],
cde03b
	int flags);
cde03b
cde03b
static void argv_free(struct argv_meta *);
cde03b
cde03b
cde03b
cde03b
cde03b
/*------------------------------------*/
cde03b
/* implementation of static functions */
cde03b
/*------------------------------------*/
cde03b
cde03b
static const struct argv_option * argv_short_option(
cde03b
	const char *			ch,
cde03b
	const struct argv_option	options[],
cde03b
	struct argv_entry *		entry)
cde03b
{
cde03b
	const struct argv_option *	option;
cde03b
cde03b
	for (option=options; option->long_name || option->short_name; option++) {
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,
cde03b
	const struct argv_option	options[],
cde03b
	struct argv_entry *		entry)
cde03b
{
cde03b
	const struct argv_option *	option;
cde03b
	const char *			arg;
cde03b
	size_t				len;
cde03b
cde03b
	for (option=options; option->long_name || option->short_name; option++) {
cde03b
		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,
ffd5c6
	const struct argv_option	options[])
ffd5c6
{
ffd5c6
	const struct argv_option *	option;
ffd5c6
	struct argv_entry		entry;
ffd5c6
ffd5c6
	if (!is_short_option(arg))
ffd5c6
		return false;
ffd5c6
ffd5c6
	if (!(option = argv_long_option(++arg,options,&entry)))
ffd5c6
		return false;
ffd5c6
ffd5c6
	if (!(option->flags & ARGV_OPTION_HYBRID_SWITCH))
ffd5c6
		if (argv_short_option(arg,options,&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(
cde03b
	const struct argv_option	options[],
cde03b
	int				tag)
cde03b
{
cde03b
	const struct argv_option *	option;
cde03b
cde03b
	for (option=options; option->short_name || option->long_name; option++)
cde03b
		if (option->tag == tag)
cde03b
			return option;
cde03b
	return 0;
cde03b
}
cde03b
cde03b
static void argv_scan(
13aa9b
	char **				argv,
cde03b
	const struct argv_option	options[],
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
ffd5c6
		else if (!fshort && is_hybrid_option(ch,options))
ffd5c6
			fhybrid = true;
ffd5c6
ffd5c6
		if (!fnoscan && !fhybrid && (fshort || is_short_option(ch))) {
cde03b
			if (!fshort)
cde03b
				ch++;
cde03b
cde03b
			if ((option = argv_short_option(ch,options,&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) {
cde03b
					if (!fnext && ch && (*ch == '-'))
875f8d
						ferr = ARGV_ERROR_OPTARG_NONE;
cde03b
					else
cde03b
						fval = false;
cde03b
				} else if (!fnext)
cde03b
					fval = true;
cde03b
				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;
cde03b
					else
cde03b
						fval = ch;
cde03b
				}
cde03b
			} else
875f8d
				ferr = ARGV_ERROR_SHORT_OPTION;
cde03b
ffd5c6
		} else if (!fnoscan && (fhybrid || is_long_option(ch))) {
ffd5c6
			ch += (fhybrid ? 1 : 2);
ffd5c6
ffd5c6
			if ((option = argv_long_option(ch,options,&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
cde03b
static void argv_show_error(struct argv_ctx * ctx)
cde03b
{
cde03b
	fprintf(stderr,"%s: error: ",ctx->program);
cde03b
cde03b
	switch (ctx->errcode) {
cde03b
		case ARGV_ERROR_SHORT_OPTION:
55faed
			fprintf(stderr,"'-%c' is not a valid short option\n",*ctx->errch);
cde03b
			break;
cde03b
cde03b
		case ARGV_ERROR_LONG_OPTION:
cde03b
			fprintf(stderr,"'--%s' is not a valid long option\n",ctx->errch);
cde03b
			break;
cde03b
cde03b
		case ARGV_ERROR_OPTARG_NONE:
985732
			fprintf(stderr,"'%s' is not a valid option value for [%s%c%s%s%s] "
985732
					"(option values may not be specified)\n",
cde03b
				ctx->errch,
cde03b
				ctx->erropt->short_name ? "-" : "",
cde03b
				ctx->erropt->short_name,
cde03b
				ctx->erropt->short_name ? "," : "",
cde03b
				ctx->erropt->long_name ? "--" : "",
cde03b
				ctx->erropt->long_name);
cde03b
			break;
cde03b
cde03b
		case ARGV_ERROR_OPTARG_REQUIRED:
cde03b
			fprintf(stderr,"option [%s%c%s%s%s] requires %s %s%s%s\n",
cde03b
				ctx->erropt->short_name ? "-" : "",
cde03b
				ctx->erropt->short_name,
cde03b
				ctx->erropt->short_name ? "," : "",
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:
cde03b
			fprintf(stderr,"'%s' is not a valid option value for [%s%c%s%s%s]={%s}\n",
cde03b
				ctx->errch,
cde03b
				ctx->erropt->short_name ? "-" : "",
cde03b
				ctx->erropt->short_name,
cde03b
				ctx->erropt->short_name ? "," : "",
cde03b
				ctx->erropt->long_name ? "--" : "",
cde03b
				ctx->erropt->long_name,
cde03b
				ctx->erropt->paradigm);
cde03b
			break;
cde03b
ffd5c6
		case ARGV_ERROR_HYBRID_NONE:
ffd5c6
			fprintf(stderr,"-%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:
ffd5c6
			fprintf(stderr,"--%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:
cef2fd
			fprintf(stderr,"-%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:
cde03b
			fputs("internal error",stderr);
cde03b
			break;
cde03b
cde03b
		default:
cde03b
			break;
cde03b
	}
cde03b
}
cde03b
cde03b
static void argv_show_status(
cde03b
	const struct argv_option	options[],
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
cde03b
	fputs("\n\nconcatenated command line:\n",stderr);
cde03b
	for (argv=meta->argv; *argv; argv++) {
cde03b
		fprintf(stderr,"%s%s",space,*argv);
cde03b
		space = " ";
cde03b
	}
cde03b
cde03b
	fputs("\n\nargument vector:\n",stderr);
cde03b
	for (argc=0,argv=meta->argv; *argv; argc++,argv++)
cde03b
		fprintf(stderr,"argv[%d]: %s\n",argc,*argv);
cde03b
cde03b
	fputs("\n\nparsed entries:\n",stderr);
cde03b
	for (entry=meta->entries; entry->arg || entry->fopt; entry++)
cde03b
		if (entry->fopt) {
cde03b
			option = option_from_tag(options,entry->tag);
cde03b
			short_name[0] = option->short_name;
cde03b
cde03b
			if (entry->fval)
cde03b
				fprintf(stderr,"[-%s,--%s] := %s\n",
cde03b
					short_name,option->long_name,entry->arg);
cde03b
			else
cde03b
				fprintf(stderr,"[-%s,--%s]\n",
cde03b
					short_name,option->long_name);
cde03b
		} else
cde03b
			fprintf(stderr,"<program arg> := %s\n",entry->arg);
cde03b
cde03b
	fputs("\n\n",stderr);
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(
13aa9b
	char *				argv[],
cde03b
	const struct argv_option	options[],
cde03b
	int				flags)
cde03b
{
cde03b
	struct argv_meta *	meta;
d81b54
	struct argv_ctx		ctx = {flags,ARGV_MODE_SCAN,0,0,0,0,0,0,0};
cde03b
cde03b
	argv_scan(argv,options,&ctx,0);
cde03b
cde03b
	if (ctx.errcode != ARGV_ERROR_OK) {
cde03b
		if (!ctx.program)
cde03b
			ctx.program = argv_program_name(argv[0]);
cde03b
cde03b
		if (ctx.flags & ARGV_VERBOSITY_ERRORS)
cde03b
			argv_show_error(&ctx;;
cde03b
cde03b
		return 0;
cde03b
	}
cde03b
cde03b
	if (!(meta = argv_alloc(argv,&ctx)))
cde03b
		return 0;
cde03b
cde03b
	ctx.mode = ARGV_MODE_COPY;
cde03b
	argv_scan(meta->argv,options,&ctx,meta);
cde03b
cde03b
	if (ctx.errcode != ARGV_ERROR_OK) {
cde03b
		if (!ctx.program)
cde03b
			ctx.program = argv[0];
cde03b
cde03b
		ctx.errcode = ARGV_ERROR_INTERNAL;
cde03b
		argv_show_error(&ctx;;
cde03b
		argv_free(meta);
cde03b
cde03b
		return 0;
cde03b
	}
cde03b
cde03b
	if (ctx.flags & ARGV_VERBOSITY_STATUS)
cde03b
		argv_show_status(options,&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(
cde03b
	FILE *				file,
cde03b
	const char *    		header,
cde03b
	const struct argv_option	options[],
cde03b
	const char *			mode)
cde03b
{
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;
0d3082
	fcolor = isatty(fileno(file));
6e8c80
6e8c80
	if (fcolor)
0d3082
		fprintf(file,"%s%s",cbold,cgreen);
e86e3e
e86e3e
	if (header)
0d3082
		fprintf(file,"%s",header);
e86e3e
e86e3e
	option = options;
e86e3e
	optlen = 0;
e86e3e
e86e3e
	for (; option->short_name || option->long_name; option++) {
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) {
e86e3e
		fprintf(stderr,
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
e86e3e
	for (option=options; option->short_name || option->long_name; option++) {
6e8c80
		/* color */
6e8c80
		if (fcolor) {
6e8c80
			color = (color == ccyan) ? cblue : ccyan;
0d3082
			fputs(color,file);
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)
e86e3e
			sprintf(optstr,"%s-%d",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) {
0d3082
			fprintf(file,"%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)
0d3082
					fprintf(file,"%s%s\n",optstr,desc);
e86e3e
				else
0d3082
					fprintf(file,"%-*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)
0d3082
		fputs(creset,file);
cde03b
}
cde03b
cde03b
#endif
cde03b
cde03b
#endif