Blame src/internal/argv/argv.h

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