Blame src/internal/argv/argv.h

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