Blame src/internal/argv/argv.h

cde03b
/****************************************************************************/
cde03b
/*  argv.h: a thread-safe argument vector parser and usage screen generator */
3e816c
/*  Copyright (C) 2015--2016  Z. Gilboa                                     */
cde03b
/*  Released under GPLv2 and GPLv3; see COPYING.MDSO.                       */
cde03b
/*  This file is (also) part of sofort: portable software project template. */
cde03b
/****************************************************************************/
cde03b
cde03b
#ifndef ARGV_H
cde03b
#define ARGV_H
cde03b
cde03b
#include <stdbool.h>
cde03b
#include <stdint.h>
cde03b
#include <stddef.h>
cde03b
#include <errno.h>
cde03b
#include <string.h>
cde03b
#include <stdlib.h>
cde03b
#include <stdio.h>
cde03b
cde03b
#define ARGV_VERBOSITY_NONE		0x00
cde03b
#define ARGV_VERBOSITY_ERRORS		0x01
cde03b
#define ARGV_VERBOSITY_STATUS		0x02
cde03b
#define ARGV_CLONE_VECTOR		0x80
cde03b
253914
#ifndef ARGV_TAB_WIDTH
253914
#define ARGV_TAB_WIDTH			8
8e35a7
#endif
8e35a7
ffd5c6
/*******************************************/
ffd5c6
/*                                         */
ffd5c6
/* support of hybrid options               */
ffd5c6
/* -------------------------               */
ffd5c6
/* hybrid options are very similar to      */
ffd5c6
/* long options, yet are prefixed by       */
ffd5c6
/* a single dash rather than two           */
ffd5c6
/* (i.e. -std, -isystem).                  */
ffd5c6
/* hybrid options are supported by this    */
ffd5c6
/* driver for compatibility with legacy    */
ffd5c6
/* tools; note, however, that the use      */
ffd5c6
/* of hybrid options should be strongly    */
ffd5c6
/* discouraged due to the limitations      */
ffd5c6
/* they impose on short options (for       */
ffd5c6
/* example, a driver implementing -std     */
ffd5c6
/* may not provide -s as a short option    */
ffd5c6
/* that takes an arbitrary value).         */
ffd5c6
/*                                         */
ffd5c6
/* SPACE: -hybrid VALUE (i.e. -MF file)    */
ffd5c6
/* EQUAL: -hybrid=VALUE (i.e. -std=c99)    */
cef2fd
/* COMMA: -hybrid,VALUE (i.e. -Wl,<arg>)   */
ffd5c6
/* ONLY:  -opt accepted, --opt rejected    */
5ae6fc
/* JOINED: -optVALUE                       */
ffd5c6
/*                                         */
ffd5c6
/*******************************************/
ffd5c6
ffd5c6
ffd5c6
#define ARGV_OPTION_HYBRID_NONE		0x00
ffd5c6
#define ARGV_OPTION_HYBRID_ONLY		0x01
ffd5c6
#define ARGV_OPTION_HYBRID_SPACE	0x02
ffd5c6
#define ARGV_OPTION_HYBRID_EQUAL	0x04
cef2fd
#define ARGV_OPTION_HYBRID_COMMA	0x08
5ae6fc
#define ARGV_OPTION_HYBRID_JOINED	0x10
5ae6fc
#define ARGV_OPTION_HYBRID_CIRCUS	(ARGV_OPTION_HYBRID_SPACE \
5ae6fc
					| ARGV_OPTION_HYBRID_JOINED)
cef2fd
#define ARGV_OPTION_HYBRID_DUAL		(ARGV_OPTION_HYBRID_SPACE \
ffd5c6
					| ARGV_OPTION_HYBRID_EQUAL)
cef2fd
#define ARGV_OPTION_HYBRID_SWITCH	(ARGV_OPTION_HYBRID_SPACE \
cef2fd
					| ARGV_OPTION_HYBRID_EQUAL \
5ae6fc
					| ARGV_OPTION_HYBRID_COMMA \
5ae6fc
					| ARGV_OPTION_HYBRID_JOINED)
ffd5c6
cde03b
enum argv_optarg {
cde03b
	ARGV_OPTARG_NONE,
cde03b
	ARGV_OPTARG_REQUIRED,
cde03b
	ARGV_OPTARG_OPTIONAL,
cde03b
};
cde03b
cde03b
enum argv_mode {
cde03b
	ARGV_MODE_SCAN,
cde03b
	ARGV_MODE_COPY,
cde03b
};
cde03b
cde03b
enum argv_error {
cde03b
	ARGV_ERROR_OK,
cde03b
	ARGV_ERROR_INTERNAL,
cde03b
	ARGV_ERROR_SHORT_OPTION,
cde03b
	ARGV_ERROR_LONG_OPTION,
cde03b
	ARGV_ERROR_OPTARG_NONE,
cde03b
	ARGV_ERROR_OPTARG_REQUIRED,
cde03b
	ARGV_ERROR_OPTARG_PARADIGM,
ffd5c6
	ARGV_ERROR_HYBRID_NONE,
ffd5c6
	ARGV_ERROR_HYBRID_ONLY,
ffd5c6
	ARGV_ERROR_HYBRID_SPACE,
ffd5c6
	ARGV_ERROR_HYBRID_EQUAL,
cef2fd
	ARGV_ERROR_HYBRID_COMMA,
cde03b
};
cde03b
cde03b
struct argv_option {
cde03b
	const char *		long_name;
cde03b
	const char		short_name;
cde03b
	int			tag;
cde03b
	enum argv_optarg	optarg;
ffd5c6
	int			flags;
cde03b
	const char *		paradigm;
cde03b
	const char *		argname;
cde03b
	const char *		description;
cde03b
};
cde03b
cde03b
struct argv_entry {
cde03b
	const char *	arg;
cde03b
	int		tag;
cde03b
	bool		fopt;
cde03b
	bool		fval;
cde03b
	bool		fnoscan;
cde03b
	enum argv_error errcode;
cde03b
};
cde03b
cde03b
struct argv_meta {
13aa9b
	char **			argv;
cde03b
	struct argv_entry *	entries;
cde03b
};
cde03b
cde03b
struct argv_meta_impl {
13aa9b
	char **			argv;
cde03b
	char *			strbuf;
cde03b
	struct argv_meta	meta;
cde03b
};
cde03b
cde03b
struct argv_ctx {
cde03b
	int				flags;
cde03b
	int				mode;
cde03b
	int				nentries;
cde03b
	enum argv_error 		errcode;
cde03b
	const char *			errch;
cde03b
	const struct argv_option *	erropt;
cde03b
	const char *			program;
cde03b
};
cde03b
cde03b
#ifdef ARGV_DRIVER
cde03b
cde03b
static const char * argv_program_name(const char *);
cde03b
cde03b
static void argv_usage(
cde03b
	FILE *,
cde03b
	const char *	header,
cde03b
	const struct	argv_option[],
cde03b
	const char *	mode);
cde03b
cde03b
static struct argv_meta * argv_get(
13aa9b
	char **,
cde03b
	const struct argv_option[],
cde03b
	int flags);
cde03b
cde03b
static void argv_free(struct argv_meta *);
cde03b
cde03b
cde03b
cde03b
cde03b
/*------------------------------------*/
cde03b
/* implementation of static functions */
cde03b
/*------------------------------------*/
cde03b
cde03b
static const struct argv_option * argv_short_option(
cde03b
	const char *			ch,
cde03b
	const struct argv_option	options[],
cde03b
	struct argv_entry *		entry)
cde03b
{
cde03b
	const struct argv_option *	option;
cde03b
cde03b
	for (option=options; option->long_name || option->short_name; option++) {
cde03b
		if (option->short_name == *ch) {
cde03b
			entry->tag	= option->tag;
cde03b
			entry->fopt	= true;
cde03b
			return option;
cde03b
		}
cde03b
	}
cde03b
cde03b
	return 0;
cde03b
}
cde03b
cde03b
static const struct argv_option * argv_long_option(
cde03b
	const char *			ch,
cde03b
	const struct argv_option	options[],
cde03b
	struct argv_entry *		entry)
cde03b
{
cde03b
	const struct argv_option *	option;
cde03b
	const char *			arg;
cde03b
	size_t				len;
cde03b
cde03b
	for (option=options; option->long_name || option->short_name; option++) {
cde03b
		len = option->long_name ? strlen(option->long_name) : 0;
cde03b
cde03b
		if (len && !(strncmp(option->long_name,ch,len))) {
cde03b
			arg = ch + len;
cde03b
5ae6fc
			if (!*arg
5ae6fc
				|| (*arg == '=')
5ae6fc
				|| (option->flags & ARGV_OPTION_HYBRID_JOINED)
5ae6fc
				|| ((option->flags & ARGV_OPTION_HYBRID_COMMA)
5ae6fc
					&& (*arg == ','))) {
cef2fd
				entry->tag	= option->tag;
cef2fd
				entry->fopt	= true;
cef2fd
				return option;
cde03b
			}
cde03b
		}
cde03b
	}
cde03b
cde03b
	return 0;
cde03b
}
cde03b
cde03b
static inline bool is_short_option(const char * arg)
cde03b
{
cde03b
	return (arg[0]=='-') && arg[1] && (arg[1]!='-');
cde03b
}
cde03b
cde03b
static inline bool is_long_option(const char * arg)
cde03b
{
cde03b
	return (arg[0]=='-') && (arg[1]=='-') && arg[2];
cde03b
}
cde03b
cde03b
static inline bool is_last_option(const char * arg)
cde03b
{
cde03b
	return (arg[0]=='-') && (arg[1]=='-') && !arg[2];
cde03b
}
cde03b
ffd5c6
static inline bool is_hybrid_option(
ffd5c6
	const char *			arg,
ffd5c6
	const struct argv_option	options[])
ffd5c6
{
ffd5c6
	const struct argv_option *	option;
ffd5c6
	struct argv_entry		entry;
ffd5c6
ffd5c6
	if (!is_short_option(arg))
ffd5c6
		return false;
ffd5c6
ffd5c6
	if (!(option = argv_long_option(++arg,options,&entry)))
ffd5c6
		return false;
ffd5c6
ffd5c6
	if (!(option->flags & ARGV_OPTION_HYBRID_SWITCH))
ffd5c6
		if (argv_short_option(arg,options,&entry))
ffd5c6
			return false;
ffd5c6
ffd5c6
	return true;
ffd5c6
}
ffd5c6
cde03b
static inline bool is_arg_in_paradigm(const char * arg, const char * paradigm)
cde03b
{
cde03b
	size_t		len;
cde03b
	const char *	ch;
cde03b
cde03b
	for (ch=paradigm,len=strlen(arg); ch; ) {
cde03b
		if (!strncmp(arg,ch,len)) {
cde03b
			if (!*(ch += len))
cde03b
				return true;
cde03b
			else if (*ch == '|')
cde03b
				return true;
cde03b
		}
cde03b
cde03b
		if ((ch = strchr(ch,'|')))
cde03b
			ch++;
cde03b
	}
cde03b
cde03b
	return false;
cde03b
}
cde03b
cde03b
static inline const struct argv_option * option_from_tag(
cde03b
	const struct argv_option	options[],
cde03b
	int				tag)
cde03b
{
cde03b
	const struct argv_option *	option;
cde03b
cde03b
	for (option=options; option->short_name || option->long_name; option++)
cde03b
		if (option->tag == tag)
cde03b
			return option;
cde03b
	return 0;
cde03b
}
cde03b
cde03b
static void argv_scan(
13aa9b
	char **				argv,
cde03b
	const struct argv_option	options[],
cde03b
	struct argv_ctx *		ctx,
cde03b
	struct argv_meta *		meta)
cde03b
{
13aa9b
	char **				parg;
cde03b
	const char *			ch;
cde03b
	const char *			val;
cde03b
	const struct argv_option *	option;
cde03b
	struct argv_entry		entry;
cde03b
	struct argv_entry *		mentry;
cde03b
	enum argv_error			ferror;
cde03b
	bool				fval;
cde03b
	bool				fnext;
cde03b
	bool				fshort;
ffd5c6
	bool				fhybrid;
cde03b
	bool				fnoscan;
cde03b
cde03b
	argv++;
cde03b
	parg	= argv;
cde03b
	ch	= *parg;
cde03b
	ferror	= ARGV_ERROR_OK;
cde03b
	fshort	= false;
cde03b
	fnoscan	= false;
cde03b
	fval	= false;
cde03b
	mentry	= meta ? meta->entries : 0;
cde03b
cde03b
	while (ch && (ferror == ARGV_ERROR_OK)) {
ffd5c6
		option  = 0;
ffd5c6
		fhybrid = false;
cde03b
cde03b
		if (fnoscan)
cde03b
			fval = true;
cde03b
cde03b
		else if (is_last_option(ch))
cde03b
			fnoscan = true;
cde03b
ffd5c6
		else if (!fshort && is_hybrid_option(ch,options))
ffd5c6
			fhybrid = true;
ffd5c6
ffd5c6
		if (!fnoscan && !fhybrid && (fshort || is_short_option(ch))) {
cde03b
			if (!fshort)
cde03b
				ch++;
cde03b
cde03b
			if ((option = argv_short_option(ch,options,&entry))) {
cde03b
				if (ch[1]) {
cde03b
					ch++;
cde03b
					fnext	= false;
cde03b
					fshort	= (option->optarg == ARGV_OPTARG_NONE);
cde03b
				} else {
cde03b
					parg++;
cde03b
					ch	= *parg;
cde03b
					fnext	= true;
cde03b
					fshort	= false;
cde03b
				}
cde03b
cde03b
				if (option->optarg == ARGV_OPTARG_NONE) {
cde03b
					if (!fnext && ch && (*ch == '-'))
cde03b
						ferror = ARGV_ERROR_OPTARG_NONE;
cde03b
					else
cde03b
						fval = false;
cde03b
				} else if (!fnext)
cde03b
					fval = true;
cde03b
				else if (option->optarg == ARGV_OPTARG_REQUIRED) {
cde03b
					if (ch && is_short_option(ch))
cde03b
						ferror = ARGV_ERROR_OPTARG_REQUIRED;
cde03b
					else if (ch && is_long_option(ch))
cde03b
						ferror = ARGV_ERROR_OPTARG_REQUIRED;
cde03b
					else if (ch && is_last_option(ch))
cde03b
						ferror = ARGV_ERROR_OPTARG_REQUIRED;
cde03b
					else if (ch)
cde03b
						fval = true;
cde03b
					else
cde03b
						ferror = ARGV_ERROR_OPTARG_REQUIRED;
cde03b
				} else {
cde03b
					/* ARGV_OPTARG_OPTIONAL */
cde03b
					if (ch && is_short_option(ch))
cde03b
						fval = false;
cde03b
					else if (ch && is_long_option(ch))
cde03b
						fval = false;
cde03b
					else if (ch && is_last_option(ch))
cde03b
						fval = false;
cde03b
					else
cde03b
						fval = ch;
cde03b
				}
cde03b
			} else
cde03b
				ferror = ARGV_ERROR_SHORT_OPTION;
cde03b
ffd5c6
		} else if (!fnoscan && (fhybrid || is_long_option(ch))) {
ffd5c6
			ch += (fhybrid ? 1 : 2);
ffd5c6
ffd5c6
			if ((option = argv_long_option(ch,options,&entry))) {
cde03b
				val = ch + strlen(option->long_name);
cde03b
cef2fd
				/* val[0] is either '=' (or ',') or '\0' */
cde03b
				if (!val[0]) {
cde03b
					parg++;
cde03b
					ch = *parg;
cde03b
				}
cde03b
ffd5c6
				if (fhybrid && !(option->flags & ARGV_OPTION_HYBRID_SWITCH))
ffd5c6
					ferror = ARGV_ERROR_HYBRID_NONE;
ffd5c6
				else if (option->optarg == ARGV_OPTARG_NONE) {
cde03b
					if (val[0]) {
cde03b
						ferror = ARGV_ERROR_OPTARG_NONE;
cde03b
						ctx->errch = val + 1;
cde03b
					} else
cde03b
						fval = false;
ffd5c6
				} else if (!fhybrid && (option->flags & ARGV_OPTION_HYBRID_ONLY))
ffd5c6
					ferror = ARGV_ERROR_HYBRID_ONLY;
5ae6fc
				else if (val[0] && (option->flags & ARGV_OPTION_HYBRID_JOINED)) {
5ae6fc
					fval = true;
5ae6fc
					ch   = val;
5ae6fc
				} else if (fhybrid && !val[0] && !(option->flags & ARGV_OPTION_HYBRID_SPACE))
ffd5c6
					ferror = ARGV_ERROR_HYBRID_SPACE;
cef2fd
				else if (fhybrid && (val[0]=='=') && !(option->flags & ARGV_OPTION_HYBRID_EQUAL))
ffd5c6
					ferror = ARGV_ERROR_HYBRID_EQUAL;
cef2fd
				else if (fhybrid && (val[0]==',') && !(option->flags & ARGV_OPTION_HYBRID_COMMA))
cef2fd
					ferror = ARGV_ERROR_HYBRID_COMMA;
cef2fd
				else if (!fhybrid && (val[0]==','))
cef2fd
					ferror = ARGV_ERROR_HYBRID_COMMA;
ffd5c6
				else if (val[0] && !val[1])
cde03b
					ferror = ARGV_ERROR_OPTARG_REQUIRED;
cde03b
				else if (val[0] && val[1]) {
cde03b
					fval = true;
cde03b
					ch   = ++val;
cde03b
				} else if (option->optarg == ARGV_OPTARG_REQUIRED) {
cde03b
					if (!val[0] && !*parg)
cde03b
						ferror = ARGV_ERROR_OPTARG_REQUIRED;
cde03b
					else if (*parg && is_short_option(*parg))
cde03b
						ferror = ARGV_ERROR_OPTARG_REQUIRED;
cde03b
					else if (*parg && is_long_option(*parg))
cde03b
						ferror = ARGV_ERROR_OPTARG_REQUIRED;
cde03b
					else if (*parg && is_last_option(*parg))
cde03b
						ferror = ARGV_ERROR_OPTARG_REQUIRED;
cde03b
					else
cde03b
						fval = true;
cde03b
				} else {
cde03b
					/* ARGV_OPTARG_OPTIONAL */
b07525
					fval = val[0];
cde03b
				}
cde03b
			} else
cde03b
				ferror = ARGV_ERROR_LONG_OPTION;
cde03b
		}
cde03b
cde03b
		if (ferror == ARGV_ERROR_OK)
cde03b
			if (option && fval && option->paradigm)
cde03b
				if (!is_arg_in_paradigm(ch,option->paradigm))
cde03b
					ferror = ARGV_ERROR_OPTARG_PARADIGM;
cde03b
cde03b
		if (ferror != ARGV_ERROR_OK) {
cde03b
			ctx->errcode = ferror;
cde03b
			ctx->errch   = ctx->errch ? ctx->errch : ch;
cde03b
			ctx->erropt  = option;
cde03b
			return;
cde03b
		} else if (ctx->mode == ARGV_MODE_SCAN) {
cde03b
			if (!fnoscan)
cde03b
				ctx->nentries++;
cde03b
			else if (fval)
cde03b
				ctx->nentries++;
cde03b
cde03b
			if (fval || !option) {
cde03b
				parg++;
cde03b
				ch = *parg;
cde03b
			}
cde03b
		} else if (ctx->mode == ARGV_MODE_COPY) {
cde03b
			if (fnoscan) {
cde03b
				if (fval) {
cde03b
					mentry->arg	= ch;
cde03b
					mentry->fnoscan = true;
cde03b
					mentry++;
cde03b
				}
cde03b
cde03b
				parg++;
cde03b
				ch = *parg;
cde03b
			} else if (option) {
cde03b
				mentry->arg	= fval ? ch : 0;
cde03b
				mentry->tag	= option->tag;
cde03b
				mentry->fopt	= true;
cde03b
				mentry->fval	= fval;
cde03b
				mentry++;
cde03b
cde03b
				if (fval) {
cde03b
					parg++;
cde03b
					ch = *parg;
cde03b
				}
cde03b
			} else {
cde03b
				mentry->arg = ch;
cde03b
				mentry++;
cde03b
				parg++;
cde03b
				ch = *parg;
cde03b
			}
cde03b
		}
cde03b
	}
cde03b
}
cde03b
cde03b
static const char * argv_program_name(const char * program_path)
cde03b
{
cde03b
	const char * ch;
cde03b
cde03b
	if (program_path) {
cde03b
		if ((ch = strrchr(program_path,'/')))
cde03b
			return *(++ch) ? ch : 0;
cde03b
cde03b
		if ((ch = strrchr(program_path,'\\')))
cde03b
			return *(++ch) ? ch : 0;
cde03b
	}
cde03b
cde03b
	return program_path;
cde03b
}
cde03b
cde03b
static void argv_show_error(struct argv_ctx * ctx)
cde03b
{
cde03b
	fprintf(stderr,"%s: error: ",ctx->program);
cde03b
cde03b
	switch (ctx->errcode) {
cde03b
		case ARGV_ERROR_SHORT_OPTION:
55faed
			fprintf(stderr,"'-%c' is not a valid short option\n",*ctx->errch);
cde03b
			break;
cde03b
cde03b
		case ARGV_ERROR_LONG_OPTION:
cde03b
			fprintf(stderr,"'--%s' is not a valid long option\n",ctx->errch);
cde03b
			break;
cde03b
cde03b
		case ARGV_ERROR_OPTARG_NONE:
985732
			fprintf(stderr,"'%s' is not a valid option value for [%s%c%s%s%s] "
985732
					"(option values may not be specified)\n",
cde03b
				ctx->errch,
cde03b
				ctx->erropt->short_name ? "-" : "",
cde03b
				ctx->erropt->short_name,
cde03b
				ctx->erropt->short_name ? "," : "",
cde03b
				ctx->erropt->long_name ? "--" : "",
cde03b
				ctx->erropt->long_name);
cde03b
			break;
cde03b
cde03b
		case ARGV_ERROR_OPTARG_REQUIRED:
cde03b
			fprintf(stderr,"option [%s%c%s%s%s] requires %s %s%s%s\n",
cde03b
				ctx->erropt->short_name ? "-" : "",
cde03b
				ctx->erropt->short_name,
cde03b
				ctx->erropt->short_name ? "," : "",
cde03b
				ctx->erropt->long_name ? "--" : "",
cde03b
				ctx->erropt->long_name,
cde03b
				ctx->erropt->paradigm ? "one of the following values:" : "a value",
cde03b
				ctx->erropt->paradigm ? "{" : "",
cde03b
				ctx->erropt->paradigm ? ctx->erropt->paradigm : "",
cde03b
				ctx->erropt->paradigm ? "}" : "");
cde03b
			break;
cde03b
cde03b
		case ARGV_ERROR_OPTARG_PARADIGM:
cde03b
			fprintf(stderr,"'%s' is not a valid option value for [%s%c%s%s%s]={%s}\n",
cde03b
				ctx->errch,
cde03b
				ctx->erropt->short_name ? "-" : "",
cde03b
				ctx->erropt->short_name,
cde03b
				ctx->erropt->short_name ? "," : "",
cde03b
				ctx->erropt->long_name ? "--" : "",
cde03b
				ctx->erropt->long_name,
cde03b
				ctx->erropt->paradigm);
cde03b
			break;
cde03b
ffd5c6
		case ARGV_ERROR_HYBRID_NONE:
ffd5c6
			fprintf(stderr,"-%s is not a synonym for --%s\n",
ffd5c6
				ctx->erropt->long_name,
ffd5c6
				ctx->erropt->long_name);
ffd5c6
			break;
ffd5c6
ffd5c6
		case ARGV_ERROR_HYBRID_ONLY:
ffd5c6
			fprintf(stderr,"--%s is not a synonym for -%s\n",
ffd5c6
				ctx->erropt->long_name,
ffd5c6
				ctx->erropt->long_name);
ffd5c6
			break;
ffd5c6
ffd5c6
		case ARGV_ERROR_HYBRID_SPACE:
ffd5c6
		case ARGV_ERROR_HYBRID_EQUAL:
cef2fd
		case ARGV_ERROR_HYBRID_COMMA:
cef2fd
			fprintf(stderr,"-%s: illegal value assignment; valid syntax is "
cef2fd
					"-%s%sVAL\n",
ffd5c6
				ctx->erropt->long_name,
cef2fd
				ctx->erropt->long_name,
cef2fd
				(ctx->erropt->flags & ARGV_OPTION_HYBRID_SPACE)
cef2fd
					? " " : (ctx->erropt->flags & ARGV_OPTION_HYBRID_EQUAL)
5ae6fc
					? "=" : (ctx->erropt->flags & ARGV_OPTION_HYBRID_COMMA)
5ae6fc
					? "," : "");
cef2fd
ffd5c6
			break;
ffd5c6
cde03b
		case ARGV_ERROR_INTERNAL:
cde03b
			fputs("internal error",stderr);
cde03b
			break;
cde03b
cde03b
		default:
cde03b
			break;
cde03b
	}
cde03b
}
cde03b
cde03b
static void argv_show_status(
cde03b
	const struct argv_option	options[],
cde03b
	struct argv_ctx *		ctx,
cde03b
	struct argv_meta *		meta)
cde03b
{
cde03b
	int				argc;
13aa9b
	char **				argv;
cde03b
	struct argv_entry *		entry;
cde03b
	const struct argv_option *	option;
cde03b
	char				short_name[2] = {0};
cde03b
	const char *			space = "";
cde03b
cde03b
	fputs("\n\nconcatenated command line:\n",stderr);
cde03b
	for (argv=meta->argv; *argv; argv++) {
cde03b
		fprintf(stderr,"%s%s",space,*argv);
cde03b
		space = " ";
cde03b
	}
cde03b
cde03b
	fputs("\n\nargument vector:\n",stderr);
cde03b
	for (argc=0,argv=meta->argv; *argv; argc++,argv++)
cde03b
		fprintf(stderr,"argv[%d]: %s\n",argc,*argv);
cde03b
cde03b
	fputs("\n\nparsed entries:\n",stderr);
cde03b
	for (entry=meta->entries; entry->arg || entry->fopt; entry++)
cde03b
		if (entry->fopt) {
cde03b
			option = option_from_tag(options,entry->tag);
cde03b
			short_name[0] = option->short_name;
cde03b
cde03b
			if (entry->fval)
cde03b
				fprintf(stderr,"[-%s,--%s] := %s\n",
cde03b
					short_name,option->long_name,entry->arg);
cde03b
			else
cde03b
				fprintf(stderr,"[-%s,--%s]\n",
cde03b
					short_name,option->long_name);
cde03b
		} else
cde03b
			fprintf(stderr,"<program arg> := %s\n",entry->arg);
cde03b
cde03b
	fputs("\n\n",stderr);
cde03b
}
cde03b
cde03b
static struct argv_meta * argv_free_impl(struct argv_meta_impl * imeta)
cde03b
{
cde03b
	if (imeta->argv)
cde03b
		free(imeta->argv);
cde03b
cde03b
	if (imeta->strbuf)
cde03b
		free(imeta->strbuf);
cde03b
cde03b
	if (imeta->meta.entries)
cde03b
		free(imeta->meta.entries);
cde03b
cde03b
	free(imeta);
cde03b
	return 0;
cde03b
}
cde03b
13aa9b
static struct argv_meta * argv_alloc(char ** argv, struct argv_ctx * ctx)
cde03b
{
cde03b
	struct argv_meta_impl * imeta;
13aa9b
	char **			vector;
cde03b
	char *			dst;
cde03b
	size_t			size;
cde03b
	int			argc;
cde03b
	int			i;
cde03b
0a6310
	if (!(imeta = calloc(1,sizeof(*imeta))))
cde03b
		return 0;
cde03b
cde03b
	if (ctx->flags & ARGV_CLONE_VECTOR) {
cde03b
		for (vector=argv,argc=0,size=0; *vector; vector++) {
cde03b
			size += strlen(*vector) + 1;
cde03b
			argc++;
cde03b
		}
cde03b
0a6310
		if (!(imeta->argv = calloc(argc+1,sizeof(char *))))
cde03b
			return argv_free_impl(imeta);
0a6310
		else if (!(imeta->strbuf = calloc(1,size+1)))
cde03b
			return argv_free_impl(imeta);
cde03b
cde03b
		for (i=0,dst=imeta->strbuf; i
cde03b
			strcpy(dst,argv[i]);
cde03b
			imeta->argv[i] = dst;
cde03b
			dst += strlen(dst)+1;
cde03b
		}
cde03b
cde03b
		imeta->meta.argv = imeta->argv;
cde03b
	} else
cde03b
		imeta->meta.argv = argv;
cde03b
0a6310
	if (!(imeta->meta.entries = calloc(
0a6310
				ctx->nentries+1,
0a6310
				sizeof(struct argv_entry))))
cde03b
		return argv_free_impl(imeta);
cde03b
	else
cde03b
		return &imeta->meta;
cde03b
}
cde03b
cde03b
static struct argv_meta * argv_get(
13aa9b
	char *				argv[],
cde03b
	const struct argv_option	options[],
cde03b
	int				flags)
cde03b
{
cde03b
	struct argv_meta *	meta;
f24214
	struct argv_ctx		ctx = {flags,ARGV_MODE_SCAN,0,0,0,0,0};
cde03b
cde03b
	argv_scan(argv,options,&ctx,0);
cde03b
cde03b
	if (ctx.errcode != ARGV_ERROR_OK) {
cde03b
		if (!ctx.program)
cde03b
			ctx.program = argv_program_name(argv[0]);
cde03b
cde03b
		if (ctx.flags & ARGV_VERBOSITY_ERRORS)
cde03b
			argv_show_error(&ctx;;
cde03b
cde03b
		return 0;
cde03b
	}
cde03b
cde03b
	if (!(meta = argv_alloc(argv,&ctx)))
cde03b
		return 0;
cde03b
cde03b
	ctx.mode = ARGV_MODE_COPY;
cde03b
	argv_scan(meta->argv,options,&ctx,meta);
cde03b
cde03b
	if (ctx.errcode != ARGV_ERROR_OK) {
cde03b
		if (!ctx.program)
cde03b
			ctx.program = argv[0];
cde03b
cde03b
		ctx.errcode = ARGV_ERROR_INTERNAL;
cde03b
		argv_show_error(&ctx;;
cde03b
		argv_free(meta);
cde03b
cde03b
		return 0;
cde03b
	}
cde03b
cde03b
	if (ctx.flags & ARGV_VERBOSITY_STATUS)
cde03b
		argv_show_status(options,&ctx,meta);
cde03b
cde03b
	return meta;
cde03b
}
cde03b
cde03b
static void argv_free(struct argv_meta * xmeta)
cde03b
{
cde03b
	struct argv_meta_impl * imeta;
cde03b
	uintptr_t		addr;
cde03b
cde03b
	if (xmeta) {
cde03b
		addr  = (uintptr_t)xmeta - offsetof(struct argv_meta_impl,meta);
cde03b
		imeta = (struct argv_meta_impl *)addr;
cde03b
		argv_free_impl(imeta);
cde03b
	}
cde03b
}
cde03b
cde03b
static void argv_usage(
cde03b
	FILE *				file,
cde03b
	const char *    		header,
cde03b
	const struct argv_option	options[],
cde03b
	const char *			mode)
cde03b
{
cde03b
	const struct argv_option *	option;
cde03b
	bool				fshort,flong;
cde03b
	bool				fnewline;
cde03b
	size_t				len,optlen;
cde03b
	size_t				paralen,rparalen,mparalen;
cde03b
	size_t				desclen,rdesclen;
cde03b
cde03b
	char *				para;
cde03b
	char *				next_para;
cde03b
	char *				desc;
cde03b
	char *				next_desc;
cde03b
	char *				paradigm;
cde03b
	char *				buf;
cde03b
	size_t				buflen;
cde03b
	const char *			sdescription;
cde03b
	const char *			sargname;
cde03b
cde03b
	const char			indent[] = "  ";
cde03b
	const int			rblen  = sizeof("}") - sizeof(char);
cde03b
	const int			rbblen = sizeof("{]") - sizeof(char);
cde03b
	const int			brcklen= sizeof("[]") - sizeof(char);
cde03b
	const int			solen  = sizeof("-") - sizeof(char);
cde03b
	const int			lolen  = sizeof("--") - sizeof(char);
cde03b
	const int			slolen = sizeof("-X,--") - sizeof(char);
cde03b
cde03b
	fshort = mode ? !strcmp(mode,"short") : 0;
cde03b
	flong  = fshort ? 0 : mode && !strcmp(mode,"long");
cde03b
cde03b
	if (header)
cde03b
		fprintf(stdout,"%s",header);
cde03b
985732
	optlen  = 0;
985732
	paralen = 0;
985732
985732
	for (option=options; option->short_name || option->long_name; option++) {
cde03b
		if (fshort)
cde03b
			len = option->short_name ? sizeof(char) + solen : 0;
cde03b
		else if (flong)
cde03b
			len = option->long_name ? strlen(option->long_name) + lolen : 0;
cde03b
		else
cde03b
			len = option->long_name ? strlen(option->long_name) + slolen : 0;
cde03b
cde03b
		if (len) {
cde03b
			if (len > optlen)
cde03b
				optlen = len;
cde03b
cde03b
			if (option->paradigm)
cde03b
				len = strlen(option->paradigm) + strlen("{}");
cde03b
			else if (option->argname)
cde03b
				len = strlen(option->argname);
cde03b
			else if (option->optarg != ARGV_OPTARG_NONE)
cde03b
				len = strlen("<val>");
cde03b
cde03b
			if (option->optarg == ARGV_OPTARG_OPTIONAL)
cde03b
				len += strlen("[]");
cde03b
cde03b
			if (len > paralen)
cde03b
				paralen = len;
cde03b
		}
cde03b
	}
cde03b
253914
	optlen += ARGV_TAB_WIDTH;
253914
	optlen &= (~(ARGV_TAB_WIDTH-1));
cde03b
c71aad
	paradigm = next_para = buf = 0;
c71aad
	fnewline = false;
c71aad
	rparalen = 0;
c71aad
	mparalen = 0;
c71aad
cde03b
	if (paralen) {
253914
		paralen += (ARGV_TAB_WIDTH);
253914
		paralen &= (~(ARGV_TAB_WIDTH-1));
cde03b
		mparalen = paralen + 2*rbblen;
cde03b
cde03b
		if (optlen + paralen > 64)
cde03b
			paralen = 32;
cde03b
	}
cde03b
cde03b
	/* account for '  ','\t', try to fit in 80 or 96 columns */
253914
	if (optlen+paralen+2+ARGV_TAB_WIDTH < 80-32)
253914
		desclen = 80 - (optlen+paralen+2+ARGV_TAB_WIDTH);
253914
	else if (optlen+paralen+2+ARGV_TAB_WIDTH < 96-32)
253914
		desclen = 96 - (optlen+paralen+2+ARGV_TAB_WIDTH);
cde03b
	else
cde03b
		desclen = 32;
cde03b
985732
	buflen   = 0;
985732
	rdesclen = 1;
985732
985732
	for (option=options; option->short_name || option->long_name; option++) {
c094bd
		if (fshort && !option->short_name)
c094bd
			continue;
c094bd
		else if (flong && !option->long_name)
c094bd
			continue;
c094bd
cde03b
		if (option->paradigm) {
cde03b
			if (option->optarg == ARGV_OPTARG_OPTIONAL)
cde03b
				rparalen = strlen(option->paradigm) - 2*rbblen;
cde03b
			else
cde03b
				rparalen = strlen(option->paradigm) - 2*rblen;
cde03b
		}
cde03b
cde03b
		sdescription 	= option->description ? option->description : "";
cde03b
		sargname	= option->argname ? option->argname : "";
cde03b
cde03b
		if (option->paradigm)
cde03b
			rdesclen = snprintf(buf,buflen,sdescription,option->paradigm);
cde03b
		else
cde03b
			rdesclen = snprintf(buf,buflen,sdescription,sargname);
cde03b
cde03b
		if (fnewline)
cde03b
			(void)0;
cde03b
cde03b
		if ((rparalen > paralen) || (rdesclen > desclen)) {
cde03b
			if (!fnewline) {
cde03b
				(void)0;
cde03b
				fnewline = true;
cde03b
			}
cde03b
		} else
cde03b
			fnewline = false;
cde03b
cde03b
		if (fshort)
cde03b
			fprintf(file,"%s-%-*c",indent,(int)(optlen-solen),option->short_name);
cde03b
		else if (flong)
cde03b
			fprintf(file,"%s--%-*s",indent,(int)(optlen-lolen),option->long_name);
cde03b
		else {
cde03b
			if (option->short_name && option->long_name)
985732
				fprintf(file,"%s-%c,--%-*s",
985732
					indent,option->short_name,
985732
					(int)(optlen-slolen),option->long_name);
cde03b
			else if (option->short_name)
985732
				 fprintf(file,"%s-%-*c",
985732
					indent,(int)(optlen-solen),option->short_name);
cde03b
			else
985732
				fprintf(file,"%s%3s--%-*s",
985732
					indent,"",
985732
					(int)(optlen-slolen),option->long_name);
cde03b
		}
cde03b
cde03b
		if (rdesclen > buflen) {
cde03b
			if (buf) {
cde03b
				free(buf);
cde03b
				buf = 0;
cde03b
			}
cde03b
cde03b
			len =  rdesclen + 512;
cde03b
			len &= (~511);
cde03b
0a6310
			if ((buf = calloc(1,len))) {
cde03b
				buflen = len;
cde03b
cde03b
				if (option->paradigm)
985732
					rdesclen = snprintf(buf,buflen,
985732
							option->description,
985732
							option->paradigm);
cde03b
				else
985732
					rdesclen = snprintf(buf,buflen,
985732
							option->description,
985732
							option->argname);
cde03b
			} else {
cde03b
				buflen = 0;
cde03b
				continue;
cde03b
			}
cde03b
		}
cde03b
cde03b
		if (option->paradigm && (rparalen <= paralen)) {
cde03b
			if (option->optarg == ARGV_OPTARG_OPTIONAL)
985732
				fprintf(file,"[{%s}]%-*c",
985732
					option->paradigm,
985732
					(int)(paralen-strlen(option->paradigm)-2*rbblen),' ');
cde03b
			else
985732
				fprintf(file,"{%s}%-*c",
985732
					option->paradigm,
985732
					(int)(paralen-strlen(option->paradigm)-rbblen),' ');
cde03b
			para = (char *)0;
cde03b
		} else if (option->paradigm) {
0a6310
			if (!paradigm && !(paradigm = calloc(1,mparalen))) {
cde03b
				fputc('\n',file);
cde03b
				continue;
cde03b
			} else
cde03b
				para = strcpy(paradigm,option->paradigm);
cde03b
cde03b
			if (option->optarg == ARGV_OPTARG_OPTIONAL) {
cde03b
				fputs("[{",file);
cde03b
				rparalen = paralen - rbblen;
cde03b
			} else {
cde03b
				fputc('{',file);
cde03b
				rparalen = paralen - rblen;
cde03b
			}
cde03b
		} else if (option->argname) {
cde03b
			if (option->optarg == ARGV_OPTARG_OPTIONAL)
985732
				fprintf(file,"[%s]%-*c",
985732
					option->argname,
985732
					(int)(paralen-strlen(option->argname)-brcklen),' ');
cde03b
			else
985732
				fprintf(file,"%s%-*c",
985732
					option->argname,
985732
					(int)(paralen-strlen(option->argname)),' ');
cde03b
			para = (char *)0;
cde03b
		} else {
cde03b
			fprintf(file,"%-*c",(int)paralen,' ');
cde03b
			para = (char *)0;
cde03b
		}
cde03b
cde03b
cde03b
		if (!para && option->description && rdesclen <= desclen) {
cde03b
			fputc('\t',file);
cde03b
			fputs(buf,file);
cde03b
			desc = (char *)0;
cde03b
		} else if (option->description)
cde03b
			desc = buf;
cde03b
		else
cde03b
			desc = (char *)0;
cde03b
cde03b
		while (para || desc) {
cde03b
			if (para) {
985732
				next_para = para+rparalen-1;
985732
985732
				for (; (next_para>para) && (*next_para!='|'); )
cde03b
					next_para--;
cde03b
cde03b
				if (para > paradigm) {
cde03b
					if (option->optarg == ARGV_OPTARG_OPTIONAL)
cde03b
						fputs("  ",file);
cde03b
					else
cde03b
						fputc(' ',file);
cde03b
				}
cde03b
cde03b
				if (*next_para != '|') {
cde03b
					fprintf(file,"%s",para);
cde03b
					para = (char *)0;
cde03b
				} else if (next_para > para) {
cde03b
					*next_para = '\0';
cde03b
					fprintf(file,"%-*s",(int)rparalen,para);
cde03b
					*next_para = '|';
cde03b
					para = next_para;
cde03b
					rparalen = strlen(para);
cde03b
cde03b
					/* 2*rbblen,2*rblen, etc.: account for indentation */
cde03b
					if (option->optarg == ARGV_OPTARG_OPTIONAL)
985732
						rparalen = (rparalen+2*rbblen > paralen)
985732
								? paralen-rbblen
985732
								: rparalen;
cde03b
					else
985732
						rparalen = (rparalen+2*rblen > paralen)
985732
								? paralen-rblen
985732
								: rparalen;
cde03b
				} else {
cde03b
					if (option->optarg == ARGV_OPTARG_OPTIONAL)
985732
						fprintf(file,"%s}]%-*c",para,
985732
							(int)(paralen-strlen(para)-rbblen),' ');
cde03b
					else
985732
						fprintf(file,"%s}%-*c",para,
985732
							(int)(paralen-strlen(para)-rblen),' ');
cde03b
					para = (char *)0;
cde03b
				}
cde03b
			} else if (desc > buf)
cde03b
				fprintf(file,"%-*c",(int)paralen,' ');
cde03b
cde03b
			if (desc) {
cde03b
				if (desc > buf)
cde03b
					fputs("\t ",file);
cde03b
				else
cde03b
					fputc('\t',file);
cde03b
cde03b
				if ((rdesclen = strlen(desc)+(desc>buf)) <= desclen) {
cde03b
					fputs(desc,file);
cde03b
					desc = (char *)0;
cde03b
				} else {
985732
					next_desc = desc + desclen - 1;
985732
985732
					for (; (next_desc > desc)
985732
							&& (*next_desc != ' ')
985732
							&& (*next_desc != '\n'); )
cde03b
						next_desc--;
cde03b
cde03b
					if ((*next_desc != ' ') && (*next_desc!='\n')) {
cde03b
						fputs(desc,file);
cde03b
						desc = (char *)0;
cde03b
					} else if (next_desc > desc) {
cde03b
						*next_desc = '\0';
cde03b
						fputs(desc,file);
cde03b
						desc = ++next_desc;
cde03b
					} else {
cde03b
						fputs(desc,file);
cde03b
						desc = (char *)0;
cde03b
					}
cde03b
				}
cde03b
			}
cde03b
cde03b
			if (para || desc)
cde03b
				fprintf(file,"\n%s%-*c",indent,(int)optlen,' ');
cde03b
		}
cde03b
cde03b
		fputc('\n',file);
cde03b
	}
cde03b
cde03b
	if (paradigm)
cde03b
		free(paradigm);
cde03b
cde03b
	if (buf)
cde03b
		free(buf);
cde03b
}
cde03b
cde03b
#endif
cde03b
cde03b
#endif