firasuke / cross / slibtool

Forked from cross/slibtool 3 months ago
Clone

Blame src/internal/argv/argv.h

9ca8c4
/****************************************************************************/
9ca8c4
/*  argv.h: a thread-safe argument vector parser and usage screen generator */
9ca8c4
/*  Copyright (C) 2015--2016  Z. Gilboa                                     */
9ca8c4
/*  Released under the Standard MIT License; see COPYING.SOFORT.            */
9ca8c4
/*  This file is (also) part of sofort: portable software project template. */
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 <errno.h>
9ca8c4
#include <string.h>
9ca8c4
#include <stdlib.h>
9ca8c4
#include <stdio.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_meta_impl {
9ca8c4
	char **			argv;
9ca8c4
	char *			strbuf;
9ca8c4
	struct argv_meta	meta;
9ca8c4
};
9ca8c4
9ca8c4
struct argv_ctx {
9ca8c4
	int				flags;
9ca8c4
	int				mode;
9ca8c4
	int				nentries;
9ca8c4
	int				unitidx;
9ca8c4
	int				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
9ca8c4
static const char * argv_program_name(const char *);
9ca8c4
9ca8c4
static void argv_usage(
9ca8c4
	FILE *,
9ca8c4
	const char *	header,
9ca8c4
	const struct	argv_option[],
9ca8c4
	const char *	mode);
9ca8c4
9ca8c4
static struct argv_meta * argv_get(
9ca8c4
	char **,
9ca8c4
	const struct argv_option[],
9ca8c4
	int flags);
9ca8c4
9ca8c4
static void argv_free(struct argv_meta *);
9ca8c4
9ca8c4
9ca8c4
9ca8c4
9ca8c4
/*------------------------------------*/
9ca8c4
/* implementation of static functions */
9ca8c4
/*------------------------------------*/
9ca8c4
9ca8c4
static const struct argv_option * argv_short_option(
9ca8c4
	const char *			ch,
9ca8c4
	const struct argv_option	options[],
9ca8c4
	struct argv_entry *		entry)
9ca8c4
{
9ca8c4
	const struct argv_option *	option;
9ca8c4
9ca8c4
	for (option=options; option->long_name || option->short_name; option++) {
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,
9ca8c4
	const struct argv_option	options[],
9ca8c4
	struct argv_entry *		entry)
9ca8c4
{
9ca8c4
	const struct argv_option *	option;
9ca8c4
	const char *			arg;
9ca8c4
	size_t				len;
9ca8c4
9ca8c4
	for (option=options; option->long_name || option->short_name; option++) {
9ca8c4
		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,
9ca8c4
	const struct argv_option	options[])
9ca8c4
{
9ca8c4
	const struct argv_option *	option;
9ca8c4
	struct argv_entry		entry;
9ca8c4
9ca8c4
	if (!is_short_option(arg))
9ca8c4
		return false;
9ca8c4
9ca8c4
	if (!(option = argv_long_option(++arg,options,&entry)))
9ca8c4
		return false;
9ca8c4
9ca8c4
	if (!(option->flags & ARGV_OPTION_HYBRID_SWITCH))
9ca8c4
		if (argv_short_option(arg,options,&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(
9ca8c4
	const struct argv_option	options[],
9ca8c4
	int				tag)
9ca8c4
{
9ca8c4
	const struct argv_option *	option;
9ca8c4
9ca8c4
	for (option=options; option->short_name || option->long_name; option++)
9ca8c4
		if (option->tag == tag)
9ca8c4
			return option;
9ca8c4
	return 0;
9ca8c4
}
9ca8c4
9ca8c4
static void argv_scan(
9ca8c4
	char **				argv,
9ca8c4
	const struct argv_option	options[],
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
9ca8c4
		else if (!fshort && is_hybrid_option(ch,options))
9ca8c4
			fhybrid = true;
9ca8c4
9ca8c4
		if (!fnoscan && !fhybrid && (fshort || is_short_option(ch))) {
9ca8c4
			if (!fshort)
9ca8c4
				ch++;
9ca8c4
9ca8c4
			if ((option = argv_short_option(ch,options,&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) {
9ca8c4
					if (!fnext && ch && (*ch == '-'))
9ca8c4
						ferr = ARGV_ERROR_OPTARG_NONE;
9ca8c4
					else
9ca8c4
						fval = false;
9ca8c4
				} else if (!fnext)
9ca8c4
					fval = true;
9ca8c4
				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;
9ca8c4
					else
9ca8c4
						fval = ch;
9ca8c4
				}
9ca8c4
			} else
9ca8c4
				ferr = ARGV_ERROR_SHORT_OPTION;
9ca8c4
9ca8c4
		} else if (!fnoscan && (fhybrid || is_long_option(ch))) {
9ca8c4
			ch += (fhybrid ? 1 : 2);
9ca8c4
9ca8c4
			if ((option = argv_long_option(ch,options,&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
9ca8c4
static void argv_show_error(struct argv_ctx * ctx)
9ca8c4
{
9ca8c4
	fprintf(stderr,"%s: error: ",ctx->program);
9ca8c4
9ca8c4
	switch (ctx->errcode) {
9ca8c4
		case ARGV_ERROR_SHORT_OPTION:
9ca8c4
			fprintf(stderr,"'-%c' is not a valid short option\n",*ctx->errch);
9ca8c4
			break;
9ca8c4
9ca8c4
		case ARGV_ERROR_LONG_OPTION:
9ca8c4
			fprintf(stderr,"'--%s' is not a valid long option\n",ctx->errch);
9ca8c4
			break;
9ca8c4
9ca8c4
		case ARGV_ERROR_OPTARG_NONE:
9ca8c4
			fprintf(stderr,"'%s' is not a valid option value for [%s%c%s%s%s] "
9ca8c4
					"(option values may not be specified)\n",
9ca8c4
				ctx->errch,
9ca8c4
				ctx->erropt->short_name ? "-" : "",
9ca8c4
				ctx->erropt->short_name,
9ca8c4
				ctx->erropt->short_name ? "," : "",
9ca8c4
				ctx->erropt->long_name ? "--" : "",
9ca8c4
				ctx->erropt->long_name);
9ca8c4
			break;
9ca8c4
9ca8c4
		case ARGV_ERROR_OPTARG_REQUIRED:
9ca8c4
			fprintf(stderr,"option [%s%c%s%s%s] requires %s %s%s%s\n",
9ca8c4
				ctx->erropt->short_name ? "-" : "",
9ca8c4
				ctx->erropt->short_name,
9ca8c4
				ctx->erropt->short_name ? "," : "",
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:
9ca8c4
			fprintf(stderr,"'%s' is not a valid option value for [%s%c%s%s%s]={%s}\n",
9ca8c4
				ctx->errch,
9ca8c4
				ctx->erropt->short_name ? "-" : "",
9ca8c4
				ctx->erropt->short_name,
9ca8c4
				ctx->erropt->short_name ? "," : "",
9ca8c4
				ctx->erropt->long_name ? "--" : "",
9ca8c4
				ctx->erropt->long_name,
9ca8c4
				ctx->erropt->paradigm);
9ca8c4
			break;
9ca8c4
9ca8c4
		case ARGV_ERROR_HYBRID_NONE:
9ca8c4
			fprintf(stderr,"-%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:
9ca8c4
			fprintf(stderr,"--%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:
9ca8c4
			fprintf(stderr,"-%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:
9ca8c4
			fputs("internal error",stderr);
9ca8c4
			break;
9ca8c4
9ca8c4
		default:
9ca8c4
			break;
9ca8c4
	}
9ca8c4
}
9ca8c4
9ca8c4
static void argv_show_status(
9ca8c4
	const struct argv_option	options[],
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
9ca8c4
	fputs("\n\nconcatenated command line:\n",stderr);
9ca8c4
	for (argv=meta->argv; *argv; argv++) {
9ca8c4
		fprintf(stderr,"%s%s",space,*argv);
9ca8c4
		space = " ";
9ca8c4
	}
9ca8c4
9ca8c4
	fputs("\n\nargument vector:\n",stderr);
9ca8c4
	for (argc=0,argv=meta->argv; *argv; argc++,argv++)
9ca8c4
		fprintf(stderr,"argv[%d]: %s\n",argc,*argv);
9ca8c4
9ca8c4
	fputs("\n\nparsed entries:\n",stderr);
9ca8c4
	for (entry=meta->entries; entry->arg || entry->fopt; entry++)
9ca8c4
		if (entry->fopt) {
9ca8c4
			option = option_from_tag(options,entry->tag);
9ca8c4
			short_name[0] = option->short_name;
9ca8c4
9ca8c4
			if (entry->fval)
9ca8c4
				fprintf(stderr,"[-%s,--%s] := %s\n",
9ca8c4
					short_name,option->long_name,entry->arg);
9ca8c4
			else
9ca8c4
				fprintf(stderr,"[-%s,--%s]\n",
9ca8c4
					short_name,option->long_name);
9ca8c4
		} else
9ca8c4
			fprintf(stderr,"<program arg> := %s\n",entry->arg);
9ca8c4
9ca8c4
	fputs("\n\n",stderr);
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(
9ca8c4
	char *				argv[],
9ca8c4
	const struct argv_option	options[],
9ca8c4
	int				flags)
9ca8c4
{
9ca8c4
	struct argv_meta *	meta;
9ca8c4
	struct argv_ctx		ctx = {flags,ARGV_MODE_SCAN,0,0,0,0,0,0,0};
9ca8c4
9ca8c4
	argv_scan(argv,options,&ctx,0);
9ca8c4
9ca8c4
	if (ctx.errcode != ARGV_ERROR_OK) {
9ca8c4
		if (!ctx.program)
9ca8c4
			ctx.program = argv_program_name(argv[0]);
9ca8c4
9ca8c4
		if (ctx.flags & ARGV_VERBOSITY_ERRORS)
9ca8c4
			argv_show_error(&ctx;;
9ca8c4
9ca8c4
		return 0;
9ca8c4
	}
9ca8c4
9ca8c4
	if (!(meta = argv_alloc(argv,&ctx)))
9ca8c4
		return 0;
9ca8c4
9ca8c4
	ctx.mode = ARGV_MODE_COPY;
9ca8c4
	argv_scan(meta->argv,options,&ctx,meta);
9ca8c4
9ca8c4
	if (ctx.errcode != ARGV_ERROR_OK) {
9ca8c4
		if (!ctx.program)
9ca8c4
			ctx.program = argv[0];
9ca8c4
9ca8c4
		ctx.errcode = ARGV_ERROR_INTERNAL;
9ca8c4
		argv_show_error(&ctx;;
9ca8c4
		argv_free(meta);
9ca8c4
9ca8c4
		return 0;
9ca8c4
	}
9ca8c4
9ca8c4
	if (ctx.flags & ARGV_VERBOSITY_STATUS)
9ca8c4
		argv_show_status(options,&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(
9ca8c4
	FILE *				file,
9ca8c4
	const char *    		header,
9ca8c4
	const struct argv_option	options[],
9ca8c4
	const char *			mode)
9ca8c4
{
9ca8c4
	const struct argv_option *	option;
9ca8c4
	bool				fshort,flong;
9ca8c4
	bool				fnewline;
9ca8c4
	size_t				len,optlen;
9ca8c4
	size_t				paralen,rparalen,mparalen;
9ca8c4
	size_t				desclen,rdesclen;
9ca8c4
9ca8c4
	char *				para;
9ca8c4
	char *				next_para;
9ca8c4
	char *				desc;
9ca8c4
	char *				next_desc;
9ca8c4
	char *				paradigm;
9ca8c4
	char *				buf;
9ca8c4
	size_t				buflen;
9ca8c4
	const char *			sdescription;
9ca8c4
	const char *			sargname;
9ca8c4
9ca8c4
	const char			indent[] = "  ";
9ca8c4
	const int			rblen  = sizeof("}") - sizeof(char);
9ca8c4
	const int			rbblen = sizeof("{]") - sizeof(char);
9ca8c4
	const int			brcklen= sizeof("[]") - sizeof(char);
9ca8c4
	const int			solen  = sizeof("-") - sizeof(char);
9ca8c4
	const int			lolen  = sizeof("--") - sizeof(char);
9ca8c4
	const int			slolen = sizeof("-X,--") - sizeof(char);
9ca8c4
9ca8c4
	fshort = mode ? !strcmp(mode,"short") : 0;
9ca8c4
	flong  = fshort ? 0 : mode && !strcmp(mode,"long");
9ca8c4
9ca8c4
	if (header)
9ca8c4
		fprintf(stdout,"%s",header);
9ca8c4
9ca8c4
	optlen  = 0;
9ca8c4
	paralen = 0;
9ca8c4
9ca8c4
	for (option=options; option->short_name || option->long_name; option++) {
9ca8c4
		if (fshort)
9ca8c4
			len = option->short_name ? sizeof(char) + solen : 0;
9ca8c4
		else if (flong)
9ca8c4
			len = option->long_name ? strlen(option->long_name) + lolen : 0;
9ca8c4
		else
9ca8c4
			len = option->long_name ? strlen(option->long_name) + slolen : 0;
9ca8c4
9ca8c4
		if (len) {
9ca8c4
			if (len > optlen)
9ca8c4
				optlen = len;
9ca8c4
9ca8c4
			if (option->argname)
9ca8c4
				len = strlen(option->argname);
9ca8c4
			else if (option->paradigm)
9ca8c4
				len = strlen(option->paradigm) + strlen("{}");
9ca8c4
			else if (option->optarg != ARGV_OPTARG_NONE)
9ca8c4
				len = strlen("<val>");
9ca8c4
9ca8c4
			if (option->optarg == ARGV_OPTARG_OPTIONAL)
9ca8c4
				len += strlen("[]");
9ca8c4
9ca8c4
			if (len > paralen)
9ca8c4
				paralen = len;
9ca8c4
		}
9ca8c4
	}
9ca8c4
9ca8c4
	optlen += ARGV_TAB_WIDTH;
9ca8c4
	optlen &= (~(ARGV_TAB_WIDTH-1));
9ca8c4
9ca8c4
	paradigm = next_para = buf = 0;
9ca8c4
	fnewline = false;
9ca8c4
	rparalen = 0;
9ca8c4
	mparalen = 0;
9ca8c4
9ca8c4
	if (paralen) {
9ca8c4
		paralen += (ARGV_TAB_WIDTH);
9ca8c4
		paralen &= (~(ARGV_TAB_WIDTH-1));
9ca8c4
		mparalen = paralen + 2*rbblen;
9ca8c4
9ca8c4
		if (optlen + paralen > 64)
9ca8c4
			paralen = 32;
9ca8c4
	}
9ca8c4
9ca8c4
	/* account for '  ','\t', try to fit in 80 or 96 columns */
9ca8c4
	if (optlen+paralen+2+ARGV_TAB_WIDTH < 80-32)
9ca8c4
		desclen = 80 - (optlen+paralen+2+ARGV_TAB_WIDTH);
9ca8c4
	else if (optlen+paralen+2+ARGV_TAB_WIDTH < 96-32)
9ca8c4
		desclen = 96 - (optlen+paralen+2+ARGV_TAB_WIDTH);
9ca8c4
	else
9ca8c4
		desclen = 32;
9ca8c4
9ca8c4
	buflen   = 0;
9ca8c4
	rdesclen = 1;
9ca8c4
9ca8c4
	for (option=options; option->short_name || option->long_name; option++) {
9ca8c4
		if (fshort && !option->short_name)
9ca8c4
			continue;
9ca8c4
		else if (flong && !option->long_name)
9ca8c4
			continue;
9ca8c4
9ca8c4
		if (option->paradigm) {
9ca8c4
			if (option->optarg == ARGV_OPTARG_OPTIONAL)
9ca8c4
				rparalen = strlen(option->paradigm) - 2*rbblen;
9ca8c4
			else
9ca8c4
				rparalen = strlen(option->paradigm) - 2*rblen;
9ca8c4
		}
9ca8c4
9ca8c4
		sdescription 	= option->description ? option->description : "";
9ca8c4
		sargname	= option->argname ? option->argname : "";
9ca8c4
9ca8c4
		if (option->paradigm)
9ca8c4
			rdesclen = snprintf(buf,buflen,sdescription,option->paradigm);
9ca8c4
		else
9ca8c4
			rdesclen = snprintf(buf,buflen,sdescription,sargname);
9ca8c4
9ca8c4
		if (fnewline)
9ca8c4
			(void)0;
9ca8c4
9ca8c4
		if ((rparalen > paralen) || (rdesclen > desclen)) {
9ca8c4
			if (!fnewline) {
9ca8c4
				(void)0;
9ca8c4
				fnewline = true;
9ca8c4
			}
9ca8c4
		} else
9ca8c4
			fnewline = false;
9ca8c4
9ca8c4
		if (fshort)
9ca8c4
			fprintf(file,"%s-%-*c",indent,(int)(optlen-solen),option->short_name);
9ca8c4
		else if (flong)
9ca8c4
			fprintf(file,
9ca8c4
				(option->flags & ARGV_OPTION_HYBRID_ONLY)
9ca8c4
					? "%s -%-*s"
9ca8c4
					: "%s--%-*s",
9ca8c4
				indent,(int)(optlen-lolen),option->long_name);
9ca8c4
		else {
9ca8c4
			if (option->short_name && option->long_name)
9ca8c4
				fprintf(file,
9ca8c4
					(option->flags & ARGV_OPTION_HYBRID_ONLY)
9ca8c4
						? "%s-%c, -%-*s"
9ca8c4
						: "%s-%c,--%-*s",
9ca8c4
					indent,option->short_name,
9ca8c4
					(int)(optlen-slolen),option->long_name);
9ca8c4
			else if (option->short_name)
9ca8c4
				 fprintf(file,"%s-%-*c",
9ca8c4
					indent,(int)(optlen-solen),option->short_name);
9ca8c4
			else
9ca8c4
				fprintf(file,
9ca8c4
					(option->flags & ARGV_OPTION_HYBRID_ONLY)
9ca8c4
						? "%s%3s -%-*s"
9ca8c4
						: "%s%3s--%-*s",
9ca8c4
					indent,"",
9ca8c4
					(int)(optlen-slolen),option->long_name);
9ca8c4
		}
9ca8c4
9ca8c4
		if (rdesclen > buflen) {
9ca8c4
			if (buf) {
9ca8c4
				free(buf);
9ca8c4
				buf = 0;
9ca8c4
			}
9ca8c4
9ca8c4
			len =  rdesclen + 512;
9ca8c4
			len &= (~511);
9ca8c4
9ca8c4
			if ((buf = calloc(1,len))) {
9ca8c4
				buflen = len;
9ca8c4
9ca8c4
				if (option->paradigm)
9ca8c4
					rdesclen = snprintf(buf,buflen,
9ca8c4
							option->description,
9ca8c4
							option->paradigm);
9ca8c4
				else
9ca8c4
					rdesclen = snprintf(buf,buflen,
9ca8c4
							option->description,
9ca8c4
							option->argname);
9ca8c4
			} else {
9ca8c4
				buflen = 0;
9ca8c4
				continue;
9ca8c4
			}
9ca8c4
		}
9ca8c4
9ca8c4
		if (option->argname) {
9ca8c4
			if (option->optarg == ARGV_OPTARG_OPTIONAL)
9ca8c4
				fprintf(file,"[%s]%-*c",
9ca8c4
					option->argname,
9ca8c4
					(int)(paralen-strlen(option->argname)-brcklen),' ');
9ca8c4
			else
9ca8c4
				fprintf(file,"%s%-*c",
9ca8c4
					option->argname,
9ca8c4
					(int)(paralen-strlen(option->argname)),' ');
9ca8c4
			para = (char *)0;
9ca8c4
		} else if (option->paradigm && (rparalen <= paralen)) {
9ca8c4
			if (option->optarg == ARGV_OPTARG_OPTIONAL)
9ca8c4
				fprintf(file,"[{%s}]%-*c",
9ca8c4
					option->paradigm,
9ca8c4
					(int)(paralen-strlen(option->paradigm)-2*rbblen),' ');
9ca8c4
			else
9ca8c4
				fprintf(file,"{%s}%-*c",
9ca8c4
					option->paradigm,
9ca8c4
					(int)(paralen-strlen(option->paradigm)-rbblen),' ');
9ca8c4
			para = (char *)0;
9ca8c4
		} else if (option->paradigm) {
9ca8c4
			if (!paradigm && !(paradigm = calloc(1,mparalen))) {
9ca8c4
				fputc('\n',file);
9ca8c4
				continue;
9ca8c4
			} else
9ca8c4
				para = strcpy(paradigm,option->paradigm);
9ca8c4
9ca8c4
			if (option->optarg == ARGV_OPTARG_OPTIONAL) {
9ca8c4
				fputs("[{",file);
9ca8c4
				rparalen = paralen - rbblen;
9ca8c4
			} else {
9ca8c4
				fputc('{',file);
9ca8c4
				rparalen = paralen - rblen;
9ca8c4
			}
9ca8c4
		} else {
9ca8c4
			fprintf(file,"%-*c",(int)paralen,' ');
9ca8c4
			para = (char *)0;
9ca8c4
		}
9ca8c4
9ca8c4
9ca8c4
		if (!para && option->description && rdesclen <= desclen) {
9ca8c4
			fputc('\t',file);
9ca8c4
			fputs(buf,file);
9ca8c4
			desc = (char *)0;
9ca8c4
		} else if (option->description)
9ca8c4
			desc = buf;
9ca8c4
		else
9ca8c4
			desc = (char *)0;
9ca8c4
9ca8c4
		while (para || desc) {
9ca8c4
			if (para) {
9ca8c4
				next_para = para+rparalen-1;
9ca8c4
9ca8c4
				for (; (next_para>para) && (*next_para!='|'); )
9ca8c4
					next_para--;
9ca8c4
9ca8c4
				if (para > paradigm) {
9ca8c4
					if (option->optarg == ARGV_OPTARG_OPTIONAL)
9ca8c4
						fputs("  ",file);
9ca8c4
					else
9ca8c4
						fputc(' ',file);
9ca8c4
				}
9ca8c4
9ca8c4
				if (*next_para != '|') {
9ca8c4
					fprintf(file,"%s",para);
9ca8c4
					para = (char *)0;
9ca8c4
				} else if (next_para > para) {
9ca8c4
					*next_para = '\0';
9ca8c4
					fprintf(file,"%-*s",(int)rparalen,para);
9ca8c4
					*next_para = '|';
9ca8c4
					para = next_para;
9ca8c4
					rparalen = strlen(para);
9ca8c4
9ca8c4
					/* 2*rbblen,2*rblen, etc.: account for indentation */
9ca8c4
					if (option->optarg == ARGV_OPTARG_OPTIONAL)
9ca8c4
						rparalen = (rparalen+2*rbblen > paralen)
9ca8c4
								? paralen-rbblen
9ca8c4
								: rparalen;
9ca8c4
					else
9ca8c4
						rparalen = (rparalen+2*rblen > paralen)
9ca8c4
								? paralen-rblen
9ca8c4
								: rparalen;
9ca8c4
				} else {
9ca8c4
					if (option->optarg == ARGV_OPTARG_OPTIONAL)
9ca8c4
						fprintf(file,"%s}]%-*c",para,
9ca8c4
							(int)(paralen-strlen(para)-rbblen),' ');
9ca8c4
					else
9ca8c4
						fprintf(file,"%s}%-*c",para,
9ca8c4
							(int)(paralen-strlen(para)-rblen),' ');
9ca8c4
					para = (char *)0;
9ca8c4
				}
9ca8c4
			} else if (desc > buf)
9ca8c4
				fprintf(file,"%-*c",(int)paralen,' ');
9ca8c4
9ca8c4
			if (desc) {
9ca8c4
				if (desc > buf)
9ca8c4
					fputs("\t ",file);
9ca8c4
				else
9ca8c4
					fputc('\t',file);
9ca8c4
9ca8c4
				if ((rdesclen = strlen(desc)+(desc>buf)) <= desclen) {
9ca8c4
					fputs(desc,file);
9ca8c4
					desc = (char *)0;
9ca8c4
				} else {
9ca8c4
					next_desc = desc + desclen - 1;
9ca8c4
9ca8c4
					for (; (next_desc > desc)
9ca8c4
							&& (*next_desc != ' ')
9ca8c4
							&& (*next_desc != '\n'); )
9ca8c4
						next_desc--;
9ca8c4
9ca8c4
					if ((*next_desc != ' ') && (*next_desc!='\n')) {
9ca8c4
						fputs(desc,file);
9ca8c4
						desc = (char *)0;
9ca8c4
					} else if (next_desc > desc) {
9ca8c4
						*next_desc = '\0';
9ca8c4
						fputs(desc,file);
9ca8c4
						desc = ++next_desc;
9ca8c4
					} else {
9ca8c4
						fputs(desc,file);
9ca8c4
						desc = (char *)0;
9ca8c4
					}
9ca8c4
				}
9ca8c4
			}
9ca8c4
9ca8c4
			if (para || desc)
9ca8c4
				fprintf(file,"\n%s%-*c",indent,(int)optlen,' ');
9ca8c4
		}
9ca8c4
9ca8c4
		fputc('\n',file);
9ca8c4
	}
9ca8c4
9ca8c4
	if (paradigm)
9ca8c4
		free(paradigm);
9ca8c4
9ca8c4
	if (buf)
9ca8c4
		free(buf);
9ca8c4
}
9ca8c4
9ca8c4
#endif
9ca8c4
9ca8c4
#endif