Blame src/internal/argv/argv.h

f46039
/****************************************************************************/
f46039
/*  argv.h: a thread-safe argument vector parser and usage screen generator */
e2ba3e
/*  Copyright (C) 2015--2016  Z. Gilboa                                     */
f46039
/*  Released under the Standard MIT License; see COPYING.SOFORT.            */
f46039
/*  This file is (also) part of sofort: portable software project template. */
f46039
/****************************************************************************/
f46039
f46039
#ifndef ARGV_H
f46039
#define ARGV_H
f46039
f46039
#include <stdbool.h>
f46039
#include <stdint.h>
f46039
#include <stddef.h>
f46039
#include <errno.h>
f46039
#include <string.h>
f46039
#include <stdlib.h>
f46039
#include <stdio.h>
f46039
f46039
#define ARGV_VERBOSITY_NONE		0x00
f46039
#define ARGV_VERBOSITY_ERRORS		0x01
f46039
#define ARGV_VERBOSITY_STATUS		0x02
f46039
#define ARGV_CLONE_VECTOR		0x80
f46039
fe1e15
#ifndef ARGV_TAB_WIDTH
fe1e15
#define ARGV_TAB_WIDTH			8
8d9552
#endif
8d9552
3bc17b
/*******************************************/
3bc17b
/*                                         */
3bc17b
/* support of hybrid options               */
3bc17b
/* -------------------------               */
3bc17b
/* hybrid options are very similar to      */
3bc17b
/* long options, yet are prefixed by       */
3bc17b
/* a single dash rather than two           */
3bc17b
/* (i.e. -std, -isystem).                  */
3bc17b
/* hybrid options are supported by this    */
3bc17b
/* driver for compatibility with legacy    */
3bc17b
/* tools; note, however, that the use      */
3bc17b
/* of hybrid options should be strongly    */
3bc17b
/* discouraged due to the limitations      */
3bc17b
/* they impose on short options (for       */
3bc17b
/* example, a driver implementing -std     */
3bc17b
/* may not provide -s as a short option    */
3bc17b
/* that takes an arbitrary value).         */
3bc17b
/*                                         */
3bc17b
/* SPACE: -hybrid VALUE (i.e. -MF file)    */
3bc17b
/* EQUAL: -hybrid=VALUE (i.e. -std=c99)    */
35b2ff
/* COMMA: -hybrid,VALUE (i.e. -Wl,<arg>)   */
3bc17b
/* ONLY:  -opt accepted, --opt rejected    */
ae0e7a
/* JOINED: -optVALUE                       */
3bc17b
/*                                         */
3bc17b
/*******************************************/
3bc17b
3bc17b
3bc17b
#define ARGV_OPTION_HYBRID_NONE		0x00
3bc17b
#define ARGV_OPTION_HYBRID_ONLY		0x01
3bc17b
#define ARGV_OPTION_HYBRID_SPACE	0x02
3bc17b
#define ARGV_OPTION_HYBRID_EQUAL	0x04
35b2ff
#define ARGV_OPTION_HYBRID_COMMA	0x08
ae0e7a
#define ARGV_OPTION_HYBRID_JOINED	0x10
ae0e7a
#define ARGV_OPTION_HYBRID_CIRCUS	(ARGV_OPTION_HYBRID_SPACE \
ae0e7a
					| ARGV_OPTION_HYBRID_JOINED)
35b2ff
#define ARGV_OPTION_HYBRID_DUAL		(ARGV_OPTION_HYBRID_SPACE \
3bc17b
					| ARGV_OPTION_HYBRID_EQUAL)
35b2ff
#define ARGV_OPTION_HYBRID_SWITCH	(ARGV_OPTION_HYBRID_SPACE \
35b2ff
					| ARGV_OPTION_HYBRID_EQUAL \
ae0e7a
					| ARGV_OPTION_HYBRID_COMMA \
ae0e7a
					| ARGV_OPTION_HYBRID_JOINED)
3bc17b
f46039
enum argv_optarg {
f46039
	ARGV_OPTARG_NONE,
f46039
	ARGV_OPTARG_REQUIRED,
f46039
	ARGV_OPTARG_OPTIONAL,
f46039
};
f46039
f46039
enum argv_mode {
f46039
	ARGV_MODE_SCAN,
f46039
	ARGV_MODE_COPY,
f46039
};
f46039
f46039
enum argv_error {
f46039
	ARGV_ERROR_OK,
f46039
	ARGV_ERROR_INTERNAL,
f46039
	ARGV_ERROR_SHORT_OPTION,
f46039
	ARGV_ERROR_LONG_OPTION,
f46039
	ARGV_ERROR_OPTARG_NONE,
f46039
	ARGV_ERROR_OPTARG_REQUIRED,
f46039
	ARGV_ERROR_OPTARG_PARADIGM,
3bc17b
	ARGV_ERROR_HYBRID_NONE,
3bc17b
	ARGV_ERROR_HYBRID_ONLY,
3bc17b
	ARGV_ERROR_HYBRID_SPACE,
3bc17b
	ARGV_ERROR_HYBRID_EQUAL,
35b2ff
	ARGV_ERROR_HYBRID_COMMA,
f46039
};
f46039
f46039
struct argv_option {
f46039
	const char *		long_name;
f46039
	const char		short_name;
f46039
	int			tag;
f46039
	enum argv_optarg	optarg;
3bc17b
	int			flags;
f46039
	const char *		paradigm;
f46039
	const char *		argname;
f46039
	const char *		description;
f46039
};
f46039
f46039
struct argv_entry {
f46039
	const char *	arg;
f46039
	int		tag;
f46039
	bool		fopt;
f46039
	bool		fval;
f46039
	bool		fnoscan;
f46039
	enum argv_error errcode;
f46039
};
f46039
f46039
struct argv_meta {
4a749a
	char **			argv;
f46039
	struct argv_entry *	entries;
f46039
};
f46039
f46039
struct argv_meta_impl {
4a749a
	char **			argv;
f46039
	char *			strbuf;
f46039
	struct argv_meta	meta;
f46039
};
f46039
f46039
struct argv_ctx {
f46039
	int				flags;
f46039
	int				mode;
f46039
	int				nentries;
f46039
	enum argv_error 		errcode;
f46039
	const char *			errch;
f46039
	const struct argv_option *	erropt;
f46039
	const char *			program;
f46039
};
f46039
fc0fa2
#ifdef ARGV_DRIVER
fc0fa2
f46039
static const char * argv_program_name(const char *);
f46039
f46039
static void argv_usage(
f46039
	FILE *,
f46039
	const char *	header,
f46039
	const struct	argv_option[],
f46039
	const char *	mode);
f46039
f46039
static struct argv_meta * argv_get(
4a749a
	char **,
f46039
	const struct argv_option[],
f46039
	int flags);
f46039
f46039
static void argv_free(struct argv_meta *);
f46039
f46039
f46039
f46039
f46039
/*------------------------------------*/
f46039
/* implementation of static functions */
f46039
/*------------------------------------*/
f46039
f46039
static const struct argv_option * argv_short_option(
f46039
	const char *			ch,
f46039
	const struct argv_option	options[],
f46039
	struct argv_entry *		entry)
f46039
{
f46039
	const struct argv_option *	option;
f46039
f46039
	for (option=options; option->long_name || option->short_name; option++) {
f46039
		if (option->short_name == *ch) {
f46039
			entry->tag	= option->tag;
f46039
			entry->fopt	= true;
f46039
			return option;
f46039
		}
f46039
	}
f46039
f46039
	return 0;
f46039
}
f46039
f46039
static const struct argv_option * argv_long_option(
f46039
	const char *			ch,
f46039
	const struct argv_option	options[],
f46039
	struct argv_entry *		entry)
f46039
{
f46039
	const struct argv_option *	option;
f46039
	const char *			arg;
f46039
	size_t				len;
f46039
f46039
	for (option=options; option->long_name || option->short_name; option++) {
f46039
		len = option->long_name ? strlen(option->long_name) : 0;
f46039
f46039
		if (len && !(strncmp(option->long_name,ch,len))) {
f46039
			arg = ch + len;
f46039
ae0e7a
			if (!*arg
ae0e7a
				|| (*arg == '=')
ae0e7a
				|| (option->flags & ARGV_OPTION_HYBRID_JOINED)
ae0e7a
				|| ((option->flags & ARGV_OPTION_HYBRID_COMMA)
ae0e7a
					&& (*arg == ','))) {
35b2ff
				entry->tag	= option->tag;
35b2ff
				entry->fopt	= true;
35b2ff
				return option;
f46039
			}
f46039
		}
f46039
	}
f46039
f46039
	return 0;
f46039
}
f46039
f46039
static inline bool is_short_option(const char * arg)
f46039
{
f46039
	return (arg[0]=='-') && arg[1] && (arg[1]!='-');
f46039
}
f46039
f46039
static inline bool is_long_option(const char * arg)
f46039
{
f46039
	return (arg[0]=='-') && (arg[1]=='-') && arg[2];
f46039
}
f46039
f46039
static inline bool is_last_option(const char * arg)
f46039
{
f46039
	return (arg[0]=='-') && (arg[1]=='-') && !arg[2];
f46039
}
f46039
3bc17b
static inline bool is_hybrid_option(
3bc17b
	const char *			arg,
3bc17b
	const struct argv_option	options[])
3bc17b
{
3bc17b
	const struct argv_option *	option;
3bc17b
	struct argv_entry		entry;
3bc17b
3bc17b
	if (!is_short_option(arg))
3bc17b
		return false;
3bc17b
3bc17b
	if (!(option = argv_long_option(++arg,options,&entry)))
3bc17b
		return false;
3bc17b
3bc17b
	if (!(option->flags & ARGV_OPTION_HYBRID_SWITCH))
3bc17b
		if (argv_short_option(arg,options,&entry))
3bc17b
			return false;
3bc17b
3bc17b
	return true;
3bc17b
}
3bc17b
f46039
static inline bool is_arg_in_paradigm(const char * arg, const char * paradigm)
f46039
{
f46039
	size_t		len;
f46039
	const char *	ch;
f46039
f46039
	for (ch=paradigm,len=strlen(arg); ch; ) {
f46039
		if (!strncmp(arg,ch,len)) {
f46039
			if (!*(ch += len))
f46039
				return true;
f46039
			else if (*ch == '|')
f46039
				return true;
f46039
		}
f46039
f46039
		if ((ch = strchr(ch,'|')))
f46039
			ch++;
f46039
	}
f46039
f46039
	return false;
f46039
}
f46039
f46039
static inline const struct argv_option * option_from_tag(
f46039
	const struct argv_option	options[],
f46039
	int				tag)
f46039
{
f46039
	const struct argv_option *	option;
f46039
f46039
	for (option=options; option->short_name || option->long_name; option++)
f46039
		if (option->tag == tag)
f46039
			return option;
f46039
	return 0;
f46039
}
f46039
f46039
static void argv_scan(
4a749a
	char **				argv,
f46039
	const struct argv_option	options[],
f46039
	struct argv_ctx *		ctx,
f46039
	struct argv_meta *		meta)
f46039
{
4a749a
	char **				parg;
f46039
	const char *			ch;
f46039
	const char *			val;
f46039
	const struct argv_option *	option;
f46039
	struct argv_entry		entry;
f46039
	struct argv_entry *		mentry;
f46039
	enum argv_error			ferror;
f46039
	bool				fval;
f46039
	bool				fnext;
f46039
	bool				fshort;
3bc17b
	bool				fhybrid;
f46039
	bool				fnoscan;
f46039
f46039
	argv++;
f46039
	parg	= argv;
f46039
	ch	= *parg;
f46039
	ferror	= ARGV_ERROR_OK;
f46039
	fshort	= false;
f46039
	fnoscan	= false;
f46039
	fval	= false;
f46039
	mentry	= meta ? meta->entries : 0;
f46039
f46039
	while (ch && (ferror == ARGV_ERROR_OK)) {
3bc17b
		option  = 0;
3bc17b
		fhybrid = false;
f46039
f46039
		if (fnoscan)
f46039
			fval = true;
f46039
f46039
		else if (is_last_option(ch))
f46039
			fnoscan = true;
f46039
3bc17b
		else if (!fshort && is_hybrid_option(ch,options))
3bc17b
			fhybrid = true;
3bc17b
3bc17b
		if (!fnoscan && !fhybrid && (fshort || is_short_option(ch))) {
f46039
			if (!fshort)
f46039
				ch++;
f46039
f46039
			if ((option = argv_short_option(ch,options,&entry))) {
f46039
				if (ch[1]) {
f46039
					ch++;
f46039
					fnext	= false;
f46039
					fshort	= (option->optarg == ARGV_OPTARG_NONE);
f46039
				} else {
f46039
					parg++;
f46039
					ch	= *parg;
f46039
					fnext	= true;
f46039
					fshort	= false;
f46039
				}
f46039
f46039
				if (option->optarg == ARGV_OPTARG_NONE) {
f46039
					if (!fnext && ch && (*ch == '-'))
f46039
						ferror = ARGV_ERROR_OPTARG_NONE;
f46039
					else
f46039
						fval = false;
f46039
				} else if (!fnext)
f46039
					fval = true;
f46039
				else if (option->optarg == ARGV_OPTARG_REQUIRED) {
f46039
					if (ch && is_short_option(ch))
f46039
						ferror = ARGV_ERROR_OPTARG_REQUIRED;
f46039
					else if (ch && is_long_option(ch))
f46039
						ferror = ARGV_ERROR_OPTARG_REQUIRED;
f46039
					else if (ch && is_last_option(ch))
f46039
						ferror = ARGV_ERROR_OPTARG_REQUIRED;
f46039
					else if (ch)
f46039
						fval = true;
f46039
					else
f46039
						ferror = ARGV_ERROR_OPTARG_REQUIRED;
f46039
				} else {
f46039
					/* ARGV_OPTARG_OPTIONAL */
f46039
					if (ch && is_short_option(ch))
f46039
						fval = false;
f46039
					else if (ch && is_long_option(ch))
f46039
						fval = false;
f46039
					else if (ch && is_last_option(ch))
f46039
						fval = false;
f46039
					else
f46039
						fval = ch;
f46039
				}
f46039
			} else
f46039
				ferror = ARGV_ERROR_SHORT_OPTION;
f46039
3bc17b
		} else if (!fnoscan && (fhybrid || is_long_option(ch))) {
3bc17b
			ch += (fhybrid ? 1 : 2);
3bc17b
3bc17b
			if ((option = argv_long_option(ch,options,&entry))) {
f46039
				val = ch + strlen(option->long_name);
f46039
35b2ff
				/* val[0] is either '=' (or ',') or '\0' */
f46039
				if (!val[0]) {
f46039
					parg++;
f46039
					ch = *parg;
f46039
				}
f46039
3bc17b
				if (fhybrid && !(option->flags & ARGV_OPTION_HYBRID_SWITCH))
3bc17b
					ferror = ARGV_ERROR_HYBRID_NONE;
3bc17b
				else if (option->optarg == ARGV_OPTARG_NONE) {
f46039
					if (val[0]) {
f46039
						ferror = ARGV_ERROR_OPTARG_NONE;
f46039
						ctx->errch = val + 1;
f46039
					} else
f46039
						fval = false;
3bc17b
				} else if (!fhybrid && (option->flags & ARGV_OPTION_HYBRID_ONLY))
3bc17b
					ferror = ARGV_ERROR_HYBRID_ONLY;
ae0e7a
				else if (val[0] && (option->flags & ARGV_OPTION_HYBRID_JOINED)) {
ae0e7a
					fval = true;
ae0e7a
					ch   = val;
ae0e7a
				} else if (fhybrid && !val[0] && !(option->flags & ARGV_OPTION_HYBRID_SPACE))
3bc17b
					ferror = ARGV_ERROR_HYBRID_SPACE;
35b2ff
				else if (fhybrid && (val[0]=='=') && !(option->flags & ARGV_OPTION_HYBRID_EQUAL))
3bc17b
					ferror = ARGV_ERROR_HYBRID_EQUAL;
35b2ff
				else if (fhybrid && (val[0]==',') && !(option->flags & ARGV_OPTION_HYBRID_COMMA))
35b2ff
					ferror = ARGV_ERROR_HYBRID_COMMA;
35b2ff
				else if (!fhybrid && (val[0]==','))
35b2ff
					ferror = ARGV_ERROR_HYBRID_COMMA;
3bc17b
				else if (val[0] && !val[1])
f46039
					ferror = ARGV_ERROR_OPTARG_REQUIRED;
f46039
				else if (val[0] && val[1]) {
f46039
					fval = true;
f46039
					ch   = ++val;
f46039
				} else if (option->optarg == ARGV_OPTARG_REQUIRED) {
f46039
					if (!val[0] && !*parg)
f46039
						ferror = ARGV_ERROR_OPTARG_REQUIRED;
f46039
					else if (*parg && is_short_option(*parg))
f46039
						ferror = ARGV_ERROR_OPTARG_REQUIRED;
f46039
					else if (*parg && is_long_option(*parg))
f46039
						ferror = ARGV_ERROR_OPTARG_REQUIRED;
f46039
					else if (*parg && is_last_option(*parg))
f46039
						ferror = ARGV_ERROR_OPTARG_REQUIRED;
f46039
					else
f46039
						fval = true;
f46039
				} else {
f46039
					/* ARGV_OPTARG_OPTIONAL */
89e8cb
					fval = val[0];
f46039
				}
f46039
			} else
f46039
				ferror = ARGV_ERROR_LONG_OPTION;
f46039
		}
f46039
f46039
		if (ferror == ARGV_ERROR_OK)
f46039
			if (option && fval && option->paradigm)
f46039
				if (!is_arg_in_paradigm(ch,option->paradigm))
f46039
					ferror = ARGV_ERROR_OPTARG_PARADIGM;
f46039
f46039
		if (ferror != ARGV_ERROR_OK) {
f46039
			ctx->errcode = ferror;
f46039
			ctx->errch   = ctx->errch ? ctx->errch : ch;
f46039
			ctx->erropt  = option;
f46039
			return;
f46039
		} else if (ctx->mode == ARGV_MODE_SCAN) {
f46039
			if (!fnoscan)
f46039
				ctx->nentries++;
f46039
			else if (fval)
f46039
				ctx->nentries++;
f46039
f46039
			if (fval || !option) {
f46039
				parg++;
f46039
				ch = *parg;
f46039
			}
f46039
		} else if (ctx->mode == ARGV_MODE_COPY) {
f46039
			if (fnoscan) {
f46039
				if (fval) {
f46039
					mentry->arg	= ch;
f46039
					mentry->fnoscan = true;
f46039
					mentry++;
f46039
				}
f46039
f46039
				parg++;
f46039
				ch = *parg;
f46039
			} else if (option) {
f46039
				mentry->arg	= fval ? ch : 0;
f46039
				mentry->tag	= option->tag;
f46039
				mentry->fopt	= true;
f46039
				mentry->fval	= fval;
f46039
				mentry++;
f46039
f46039
				if (fval) {
f46039
					parg++;
f46039
					ch = *parg;
f46039
				}
f46039
			} else {
f46039
				mentry->arg = ch;
f46039
				mentry++;
f46039
				parg++;
f46039
				ch = *parg;
f46039
			}
f46039
		}
f46039
	}
f46039
}
f46039
f46039
static const char * argv_program_name(const char * program_path)
f46039
{
f46039
	const char * ch;
f46039
f46039
	if (program_path) {
f46039
		if ((ch = strrchr(program_path,'/')))
f46039
			return *(++ch) ? ch : 0;
f46039
f46039
		if ((ch = strrchr(program_path,'\\')))
f46039
			return *(++ch) ? ch : 0;
f46039
	}
f46039
f46039
	return program_path;
f46039
}
f46039
f46039
static void argv_show_error(struct argv_ctx * ctx)
f46039
{
f46039
	fprintf(stderr,"%s: error: ",ctx->program);
f46039
f46039
	switch (ctx->errcode) {
f46039
		case ARGV_ERROR_SHORT_OPTION:
9a57db
			fprintf(stderr,"'-%c' is not a valid short option\n",*ctx->errch);
f46039
			break;
f46039
f46039
		case ARGV_ERROR_LONG_OPTION:
f46039
			fprintf(stderr,"'--%s' is not a valid long option\n",ctx->errch);
f46039
			break;
f46039
f46039
		case ARGV_ERROR_OPTARG_NONE:
134b58
			fprintf(stderr,"'%s' is not a valid option value for [%s%c%s%s%s] "
134b58
					"(option values may not be specified)\n",
f46039
				ctx->errch,
f46039
				ctx->erropt->short_name ? "-" : "",
f46039
				ctx->erropt->short_name,
f46039
				ctx->erropt->short_name ? "," : "",
f46039
				ctx->erropt->long_name ? "--" : "",
f46039
				ctx->erropt->long_name);
f46039
			break;
f46039
f46039
		case ARGV_ERROR_OPTARG_REQUIRED:
f46039
			fprintf(stderr,"option [%s%c%s%s%s] requires %s %s%s%s\n",
f46039
				ctx->erropt->short_name ? "-" : "",
f46039
				ctx->erropt->short_name,
f46039
				ctx->erropt->short_name ? "," : "",
f46039
				ctx->erropt->long_name ? "--" : "",
f46039
				ctx->erropt->long_name,
f46039
				ctx->erropt->paradigm ? "one of the following values:" : "a value",
f46039
				ctx->erropt->paradigm ? "{" : "",
f46039
				ctx->erropt->paradigm ? ctx->erropt->paradigm : "",
f46039
				ctx->erropt->paradigm ? "}" : "");
f46039
			break;
f46039
f46039
		case ARGV_ERROR_OPTARG_PARADIGM:
f46039
			fprintf(stderr,"'%s' is not a valid option value for [%s%c%s%s%s]={%s}\n",
f46039
				ctx->errch,
f46039
				ctx->erropt->short_name ? "-" : "",
f46039
				ctx->erropt->short_name,
f46039
				ctx->erropt->short_name ? "," : "",
f46039
				ctx->erropt->long_name ? "--" : "",
f46039
				ctx->erropt->long_name,
f46039
				ctx->erropt->paradigm);
f46039
			break;
f46039
3bc17b
		case ARGV_ERROR_HYBRID_NONE:
3bc17b
			fprintf(stderr,"-%s is not a synonym for --%s\n",
3bc17b
				ctx->erropt->long_name,
3bc17b
				ctx->erropt->long_name);
3bc17b
			break;
3bc17b
3bc17b
		case ARGV_ERROR_HYBRID_ONLY:
3bc17b
			fprintf(stderr,"--%s is not a synonym for -%s\n",
3bc17b
				ctx->erropt->long_name,
3bc17b
				ctx->erropt->long_name);
3bc17b
			break;
3bc17b
3bc17b
		case ARGV_ERROR_HYBRID_SPACE:
3bc17b
		case ARGV_ERROR_HYBRID_EQUAL:
35b2ff
		case ARGV_ERROR_HYBRID_COMMA:
35b2ff
			fprintf(stderr,"-%s: illegal value assignment; valid syntax is "
35b2ff
					"-%s%sVAL\n",
3bc17b
				ctx->erropt->long_name,
35b2ff
				ctx->erropt->long_name,
35b2ff
				(ctx->erropt->flags & ARGV_OPTION_HYBRID_SPACE)
35b2ff
					? " " : (ctx->erropt->flags & ARGV_OPTION_HYBRID_EQUAL)
ae0e7a
					? "=" : (ctx->erropt->flags & ARGV_OPTION_HYBRID_COMMA)
ae0e7a
					? "," : "");
35b2ff
3bc17b
			break;
3bc17b
f46039
		case ARGV_ERROR_INTERNAL:
f46039
			fputs("internal error",stderr);
f46039
			break;
f46039
f46039
		default:
f46039
			break;
f46039
	}
f46039
}
f46039
f46039
static void argv_show_status(
f46039
	const struct argv_option	options[],
f46039
	struct argv_ctx *		ctx,
f46039
	struct argv_meta *		meta)
f46039
{
f46039
	int				argc;
4a749a
	char **				argv;
f46039
	struct argv_entry *		entry;
f46039
	const struct argv_option *	option;
f46039
	char				short_name[2] = {0};
f46039
	const char *			space = "";
f46039
f46039
	fputs("\n\nconcatenated command line:\n",stderr);
f46039
	for (argv=meta->argv; *argv; argv++) {
f46039
		fprintf(stderr,"%s%s",space,*argv);
f46039
		space = " ";
f46039
	}
f46039
f46039
	fputs("\n\nargument vector:\n",stderr);
f46039
	for (argc=0,argv=meta->argv; *argv; argc++,argv++)
f46039
		fprintf(stderr,"argv[%d]: %s\n",argc,*argv);
f46039
f46039
	fputs("\n\nparsed entries:\n",stderr);
f46039
	for (entry=meta->entries; entry->arg || entry->fopt; entry++)
f46039
		if (entry->fopt) {
f46039
			option = option_from_tag(options,entry->tag);
f46039
			short_name[0] = option->short_name;
f46039
f46039
			if (entry->fval)
f46039
				fprintf(stderr,"[-%s,--%s] := %s\n",
f46039
					short_name,option->long_name,entry->arg);
f46039
			else
f46039
				fprintf(stderr,"[-%s,--%s]\n",
f46039
					short_name,option->long_name);
f46039
		} else
f46039
			fprintf(stderr,"<program arg> := %s\n",entry->arg);
f46039
f46039
	fputs("\n\n",stderr);
f46039
}
f46039
f46039
static struct argv_meta * argv_free_impl(struct argv_meta_impl * imeta)
f46039
{
f46039
	if (imeta->argv)
f46039
		free(imeta->argv);
f46039
f46039
	if (imeta->strbuf)
f46039
		free(imeta->strbuf);
f46039
f46039
	if (imeta->meta.entries)
f46039
		free(imeta->meta.entries);
f46039
f46039
	free(imeta);
f46039
	return 0;
f46039
}
f46039
4a749a
static struct argv_meta * argv_alloc(char ** argv, struct argv_ctx * ctx)
f46039
{
f46039
	struct argv_meta_impl * imeta;
4a749a
	char **			vector;
f46039
	char *			dst;
f46039
	size_t			size;
f46039
	int			argc;
f46039
	int			i;
f46039
cb73f2
	if (!(imeta = calloc(1,sizeof(*imeta))))
f46039
		return 0;
f46039
f46039
	if (ctx->flags & ARGV_CLONE_VECTOR) {
f46039
		for (vector=argv,argc=0,size=0; *vector; vector++) {
f46039
			size += strlen(*vector) + 1;
f46039
			argc++;
f46039
		}
f46039
cb73f2
		if (!(imeta->argv = calloc(argc+1,sizeof(char *))))
f46039
			return argv_free_impl(imeta);
cb73f2
		else if (!(imeta->strbuf = calloc(1,size+1)))
f46039
			return argv_free_impl(imeta);
f46039
f46039
		for (i=0,dst=imeta->strbuf; i
f46039
			strcpy(dst,argv[i]);
f46039
			imeta->argv[i] = dst;
f46039
			dst += strlen(dst)+1;
f46039
		}
f46039
f46039
		imeta->meta.argv = imeta->argv;
f46039
	} else
f46039
		imeta->meta.argv = argv;
f46039
cb73f2
	if (!(imeta->meta.entries = calloc(
cb73f2
				ctx->nentries+1,
cb73f2
				sizeof(struct argv_entry))))
f46039
		return argv_free_impl(imeta);
f46039
	else
f46039
		return &imeta->meta;
f46039
}
f46039
f46039
static struct argv_meta * argv_get(
4a749a
	char *				argv[],
f46039
	const struct argv_option	options[],
f46039
	int				flags)
f46039
{
f46039
	struct argv_meta *	meta;
86378e
	struct argv_ctx		ctx = {flags,ARGV_MODE_SCAN,0,0,0,0,0};
f46039
f46039
	argv_scan(argv,options,&ctx,0);
f46039
f46039
	if (ctx.errcode != ARGV_ERROR_OK) {
f46039
		if (!ctx.program)
f46039
			ctx.program = argv_program_name(argv[0]);
f46039
f46039
		if (ctx.flags & ARGV_VERBOSITY_ERRORS)
f46039
			argv_show_error(&ctx;;
f46039
f46039
		return 0;
f46039
	}
f46039
f46039
	if (!(meta = argv_alloc(argv,&ctx)))
f46039
		return 0;
f46039
f46039
	ctx.mode = ARGV_MODE_COPY;
f46039
	argv_scan(meta->argv,options,&ctx,meta);
f46039
f46039
	if (ctx.errcode != ARGV_ERROR_OK) {
f46039
		if (!ctx.program)
f46039
			ctx.program = argv[0];
f46039
f46039
		ctx.errcode = ARGV_ERROR_INTERNAL;
f46039
		argv_show_error(&ctx;;
f46039
		argv_free(meta);
f46039
f46039
		return 0;
f46039
	}
f46039
f46039
	if (ctx.flags & ARGV_VERBOSITY_STATUS)
f46039
		argv_show_status(options,&ctx,meta);
f46039
f46039
	return meta;
f46039
}
f46039
f46039
static void argv_free(struct argv_meta * xmeta)
f46039
{
f46039
	struct argv_meta_impl * imeta;
f46039
	uintptr_t		addr;
f46039
f46039
	if (xmeta) {
f46039
		addr  = (uintptr_t)xmeta - offsetof(struct argv_meta_impl,meta);
f46039
		imeta = (struct argv_meta_impl *)addr;
f46039
		argv_free_impl(imeta);
f46039
	}
f46039
}
f46039
f46039
static void argv_usage(
f46039
	FILE *				file,
f46039
	const char *    		header,
f46039
	const struct argv_option	options[],
f46039
	const char *			mode)
f46039
{
f46039
	const struct argv_option *	option;
f46039
	bool				fshort,flong;
f46039
	bool				fnewline;
f46039
	size_t				len,optlen;
f46039
	size_t				paralen,rparalen,mparalen;
f46039
	size_t				desclen,rdesclen;
f46039
f46039
	char *				para;
f46039
	char *				next_para;
f46039
	char *				desc;
f46039
	char *				next_desc;
f46039
	char *				paradigm;
f46039
	char *				buf;
f46039
	size_t				buflen;
f46039
	const char *			sdescription;
f46039
	const char *			sargname;
f46039
f46039
	const char			indent[] = "  ";
f46039
	const int			rblen  = sizeof("}") - sizeof(char);
f46039
	const int			rbblen = sizeof("{]") - sizeof(char);
f46039
	const int			brcklen= sizeof("[]") - sizeof(char);
f46039
	const int			solen  = sizeof("-") - sizeof(char);
f46039
	const int			lolen  = sizeof("--") - sizeof(char);
f46039
	const int			slolen = sizeof("-X,--") - sizeof(char);
f46039
f46039
	fshort = mode ? !strcmp(mode,"short") : 0;
f46039
	flong  = fshort ? 0 : mode && !strcmp(mode,"long");
f46039
f46039
	if (header)
f46039
		fprintf(stdout,"%s",header);
f46039
134b58
	optlen  = 0;
134b58
	paralen = 0;
134b58
134b58
	for (option=options; option->short_name || option->long_name; option++) {
f46039
		if (fshort)
f46039
			len = option->short_name ? sizeof(char) + solen : 0;
f46039
		else if (flong)
f46039
			len = option->long_name ? strlen(option->long_name) + lolen : 0;
f46039
		else
f46039
			len = option->long_name ? strlen(option->long_name) + slolen : 0;
f46039
f46039
		if (len) {
f46039
			if (len > optlen)
f46039
				optlen = len;
f46039
f46039
			if (option->paradigm)
f46039
				len = strlen(option->paradigm) + strlen("{}");
f46039
			else if (option->argname)
f46039
				len = strlen(option->argname);
f46039
			else if (option->optarg != ARGV_OPTARG_NONE)
f46039
				len = strlen("<val>");
f46039
f46039
			if (option->optarg == ARGV_OPTARG_OPTIONAL)
f46039
				len += strlen("[]");
f46039
f46039
			if (len > paralen)
f46039
				paralen = len;
f46039
		}
f46039
	}
f46039
fe1e15
	optlen += ARGV_TAB_WIDTH;
fe1e15
	optlen &= (~(ARGV_TAB_WIDTH-1));
f46039
5c5a0d
	paradigm = next_para = buf = 0;
5c5a0d
	fnewline = false;
5c5a0d
	rparalen = 0;
5c5a0d
	mparalen = 0;
5c5a0d
f46039
	if (paralen) {
fe1e15
		paralen += (ARGV_TAB_WIDTH);
fe1e15
		paralen &= (~(ARGV_TAB_WIDTH-1));
f46039
		mparalen = paralen + 2*rbblen;
f46039
f46039
		if (optlen + paralen > 64)
f46039
			paralen = 32;
f46039
	}
f46039
f46039
	/* account for '  ','\t', try to fit in 80 or 96 columns */
fe1e15
	if (optlen+paralen+2+ARGV_TAB_WIDTH < 80-32)
fe1e15
		desclen = 80 - (optlen+paralen+2+ARGV_TAB_WIDTH);
fe1e15
	else if (optlen+paralen+2+ARGV_TAB_WIDTH < 96-32)
fe1e15
		desclen = 96 - (optlen+paralen+2+ARGV_TAB_WIDTH);
f46039
	else
f46039
		desclen = 32;
f46039
134b58
	buflen   = 0;
134b58
	rdesclen = 1;
134b58
134b58
	for (option=options; option->short_name || option->long_name; option++) {
a41165
		if (fshort && !option->short_name)
a41165
			continue;
a41165
		else if (flong && !option->long_name)
a41165
			continue;
a41165
f46039
		if (option->paradigm) {
f46039
			if (option->optarg == ARGV_OPTARG_OPTIONAL)
f46039
				rparalen = strlen(option->paradigm) - 2*rbblen;
f46039
			else
f46039
				rparalen = strlen(option->paradigm) - 2*rblen;
f46039
		}
f46039
f46039
		sdescription 	= option->description ? option->description : "";
f46039
		sargname	= option->argname ? option->argname : "";
f46039
f46039
		if (option->paradigm)
f46039
			rdesclen = snprintf(buf,buflen,sdescription,option->paradigm);
f46039
		else
f46039
			rdesclen = snprintf(buf,buflen,sdescription,sargname);
f46039
f46039
		if (fnewline)
f46039
			(void)0;
f46039
f46039
		if ((rparalen > paralen) || (rdesclen > desclen)) {
f46039
			if (!fnewline) {
f46039
				(void)0;
f46039
				fnewline = true;
f46039
			}
f46039
		} else
f46039
			fnewline = false;
f46039
f46039
		if (fshort)
f46039
			fprintf(file,"%s-%-*c",indent,(int)(optlen-solen),option->short_name);
f46039
		else if (flong)
f46039
			fprintf(file,"%s--%-*s",indent,(int)(optlen-lolen),option->long_name);
f46039
		else {
f46039
			if (option->short_name && option->long_name)
134b58
				fprintf(file,"%s-%c,--%-*s",
134b58
					indent,option->short_name,
134b58
					(int)(optlen-slolen),option->long_name);
f46039
			else if (option->short_name)
134b58
				 fprintf(file,"%s-%-*c",
134b58
					indent,(int)(optlen-solen),option->short_name);
f46039
			else
134b58
				fprintf(file,"%s%3s--%-*s",
134b58
					indent,"",
134b58
					(int)(optlen-slolen),option->long_name);
f46039
		}
f46039
f46039
		if (rdesclen > buflen) {
f46039
			if (buf) {
f46039
				free(buf);
f46039
				buf = 0;
f46039
			}
f46039
f46039
			len =  rdesclen + 512;
f46039
			len &= (~511);
f46039
cb73f2
			if ((buf = calloc(1,len))) {
f46039
				buflen = len;
f46039
f46039
				if (option->paradigm)
134b58
					rdesclen = snprintf(buf,buflen,
134b58
							option->description,
134b58
							option->paradigm);
f46039
				else
134b58
					rdesclen = snprintf(buf,buflen,
134b58
							option->description,
134b58
							option->argname);
f46039
			} else {
f46039
				buflen = 0;
f46039
				continue;
f46039
			}
f46039
		}
f46039
f46039
		if (option->paradigm && (rparalen <= paralen)) {
f46039
			if (option->optarg == ARGV_OPTARG_OPTIONAL)
134b58
				fprintf(file,"[{%s}]%-*c",
134b58
					option->paradigm,
134b58
					(int)(paralen-strlen(option->paradigm)-2*rbblen),' ');
f46039
			else
134b58
				fprintf(file,"{%s}%-*c",
134b58
					option->paradigm,
134b58
					(int)(paralen-strlen(option->paradigm)-rbblen),' ');
f46039
			para = (char *)0;
f46039
		} else if (option->paradigm) {
cb73f2
			if (!paradigm && !(paradigm = calloc(1,mparalen))) {
f46039
				fputc('\n',file);
f46039
				continue;
f46039
			} else
f46039
				para = strcpy(paradigm,option->paradigm);
f46039
f46039
			if (option->optarg == ARGV_OPTARG_OPTIONAL) {
f46039
				fputs("[{",file);
f46039
				rparalen = paralen - rbblen;
f46039
			} else {
f46039
				fputc('{',file);
f46039
				rparalen = paralen - rblen;
f46039
			}
f46039
		} else if (option->argname) {
f46039
			if (option->optarg == ARGV_OPTARG_OPTIONAL)
134b58
				fprintf(file,"[%s]%-*c",
134b58
					option->argname,
134b58
					(int)(paralen-strlen(option->argname)-brcklen),' ');
f46039
			else
134b58
				fprintf(file,"%s%-*c",
134b58
					option->argname,
134b58
					(int)(paralen-strlen(option->argname)),' ');
f46039
			para = (char *)0;
f46039
		} else {
f46039
			fprintf(file,"%-*c",(int)paralen,' ');
f46039
			para = (char *)0;
f46039
		}
f46039
f46039
f46039
		if (!para && option->description && rdesclen <= desclen) {
f46039
			fputc('\t',file);
f46039
			fputs(buf,file);
f46039
			desc = (char *)0;
f46039
		} else if (option->description)
f46039
			desc = buf;
f46039
		else
f46039
			desc = (char *)0;
f46039
f46039
		while (para || desc) {
f46039
			if (para) {
134b58
				next_para = para+rparalen-1;
134b58
134b58
				for (; (next_para>para) && (*next_para!='|'); )
f46039
					next_para--;
f46039
f46039
				if (para > paradigm) {
f46039
					if (option->optarg == ARGV_OPTARG_OPTIONAL)
f46039
						fputs("  ",file);
f46039
					else
f46039
						fputc(' ',file);
f46039
				}
f46039
f46039
				if (*next_para != '|') {
f46039
					fprintf(file,"%s",para);
f46039
					para = (char *)0;
f46039
				} else if (next_para > para) {
f46039
					*next_para = '\0';
f46039
					fprintf(file,"%-*s",(int)rparalen,para);
f46039
					*next_para = '|';
f46039
					para = next_para;
f46039
					rparalen = strlen(para);
f46039
f46039
					/* 2*rbblen,2*rblen, etc.: account for indentation */
f46039
					if (option->optarg == ARGV_OPTARG_OPTIONAL)
134b58
						rparalen = (rparalen+2*rbblen > paralen)
134b58
								? paralen-rbblen
134b58
								: rparalen;
f46039
					else
134b58
						rparalen = (rparalen+2*rblen > paralen)
134b58
								? paralen-rblen
134b58
								: rparalen;
f46039
				} else {
f46039
					if (option->optarg == ARGV_OPTARG_OPTIONAL)
134b58
						fprintf(file,"%s}]%-*c",para,
134b58
							(int)(paralen-strlen(para)-rbblen),' ');
f46039
					else
134b58
						fprintf(file,"%s}%-*c",para,
134b58
							(int)(paralen-strlen(para)-rblen),' ');
f46039
					para = (char *)0;
f46039
				}
f46039
			} else if (desc > buf)
f46039
				fprintf(file,"%-*c",(int)paralen,' ');
f46039
f46039
			if (desc) {
f46039
				if (desc > buf)
f46039
					fputs("\t ",file);
f46039
				else
f46039
					fputc('\t',file);
f46039
f46039
				if ((rdesclen = strlen(desc)+(desc>buf)) <= desclen) {
f46039
					fputs(desc,file);
f46039
					desc = (char *)0;
f46039
				} else {
134b58
					next_desc = desc + desclen - 1;
134b58
134b58
					for (; (next_desc > desc)
134b58
							&& (*next_desc != ' ')
134b58
							&& (*next_desc != '\n'); )
f46039
						next_desc--;
f46039
f46039
					if ((*next_desc != ' ') && (*next_desc!='\n')) {
f46039
						fputs(desc,file);
f46039
						desc = (char *)0;
f46039
					} else if (next_desc > desc) {
f46039
						*next_desc = '\0';
f46039
						fputs(desc,file);
f46039
						desc = ++next_desc;
f46039
					} else {
f46039
						fputs(desc,file);
f46039
						desc = (char *)0;
f46039
					}
f46039
				}
f46039
			}
f46039
f46039
			if (para || desc)
f46039
				fprintf(file,"\n%s%-*c",indent,(int)optlen,' ');
f46039
		}
f46039
f46039
		fputc('\n',file);
f46039
	}
f46039
f46039
	if (paradigm)
f46039
		free(paradigm);
f46039
f46039
	if (buf)
f46039
		free(buf);
f46039
}
f46039
f46039
#endif
f46039
f46039
#endif