Blame src/internal/argv/argv.h

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