Blame src/internal/argv/argv.h

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