Blame src/internal/argv/argv.h

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