Blame src/internal/argv/argv.h

f46039
/****************************************************************************/
f46039
/*  argv.h: a thread-safe argument vector parser and usage screen generator */
f46039
/*  Copyright (C) 2015  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 <errno.h>
f46039
#include <string.h>
f46039
#include <stdlib.h>
f46039
#include <stdio.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
8d9552
#ifndef ARGV_TAB
8d9552
#define ARGV_TAB			8
8d9552
#endif
8d9552
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,
f46039
};
f46039
f46039
struct argv_option {
f46039
	const char *		long_name;
f46039
	const char		short_name;
f46039
	int			tag;
f46039
	enum argv_optarg	optarg;
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 {
f46039
	const char **		argv;
f46039
	struct argv_entry *	entries;
f46039
};
f46039
f46039
struct argv_meta_impl {
f46039
	const 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;
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
f46039
static const char * argv_program_name(const char *);
f46039
f46039
static void argv_usage(
f46039
	FILE *,
f46039
	const char *	header,
f46039
	const struct	argv_option[],
f46039
	const char *	mode);
f46039
f46039
static struct argv_meta * argv_get(
f46039
	const char **,
f46039
	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
f46039
static const struct argv_option * argv_short_option(
f46039
	const char *			ch,
f46039
	const struct argv_option	options[],
f46039
	struct argv_entry *		entry)
f46039
{
f46039
	const struct argv_option *	option;
f46039
f46039
	for (option=options; option->long_name || option->short_name; option++) {
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,
f46039
	const struct argv_option	options[],
f46039
	struct argv_entry *		entry)
f46039
{
f46039
	const struct argv_option *	option;
f46039
	const char *			arg;
f46039
	size_t				len;
f46039
f46039
	for (option=options; option->long_name || option->short_name; option++) {
f46039
		len = option->long_name ? strlen(option->long_name) : 0;
f46039
f46039
		if (len && !(strncmp(option->long_name,ch,len))) {
f46039
			arg = ch + len;
f46039
f46039
			if (!*arg || (*arg == '=')) {
f46039
				entry->tag	= option->tag;
f46039
				entry->fopt	= true;
f46039
				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
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(
f46039
	const struct argv_option	options[],
f46039
	int				tag)
f46039
{
f46039
	const struct argv_option *	option;
f46039
f46039
	for (option=options; option->short_name || option->long_name; option++)
f46039
		if (option->tag == tag)
f46039
			return option;
f46039
	return 0;
f46039
}
f46039
f46039
static void argv_scan(
f46039
	const char **			argv,
f46039
	const struct argv_option	options[],
f46039
	struct argv_ctx *		ctx,
f46039
	struct argv_meta *		meta)
f46039
{
f46039
	const 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;
f46039
	enum argv_error			ferror;
f46039
	bool				fval;
f46039
	bool				fnext;
f46039
	bool				fshort;
f46039
	bool				fnoscan;
f46039
f46039
	argv++;
f46039
	parg	= argv;
f46039
	ch	= *parg;
f46039
	ferror	= ARGV_ERROR_OK;
f46039
	fshort	= false;
f46039
	fnoscan	= false;
f46039
	fval	= false;
f46039
	mentry	= meta ? meta->entries : 0;
f46039
f46039
	while (ch && (ferror == ARGV_ERROR_OK)) {
f46039
		option = 0;
f46039
f46039
		if (fnoscan)
f46039
			fval = true;
f46039
f46039
		else if (is_last_option(ch))
f46039
			fnoscan = true;
f46039
f46039
		else if ((fshort || is_short_option(ch))) {
f46039
			if (!fshort)
f46039
				ch++;
f46039
f46039
			if ((option = argv_short_option(ch,options,&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) {
f46039
					if (!fnext && ch && (*ch == '-'))
f46039
						ferror = ARGV_ERROR_OPTARG_NONE;
f46039
					else
f46039
						fval = false;
f46039
				} else if (!fnext)
f46039
					fval = true;
f46039
				else if (option->optarg == ARGV_OPTARG_REQUIRED) {
f46039
					if (ch && is_short_option(ch))
f46039
						ferror = ARGV_ERROR_OPTARG_REQUIRED;
f46039
					else if (ch && is_long_option(ch))
f46039
						ferror = ARGV_ERROR_OPTARG_REQUIRED;
f46039
					else if (ch && is_last_option(ch))
f46039
						ferror = ARGV_ERROR_OPTARG_REQUIRED;
f46039
					else if (ch)
f46039
						fval = true;
f46039
					else
f46039
						ferror = 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;
f46039
					else
f46039
						fval = ch;
f46039
				}
f46039
			} else
f46039
				ferror = ARGV_ERROR_SHORT_OPTION;
f46039
f46039
		} else if ((is_long_option(ch))) {
f46039
			if ((option = argv_long_option(ch+=2,options,&entry))) {
f46039
				val = ch + strlen(option->long_name);
f46039
f46039
				/* val[0] is either '=' or '\0' */
f46039
				if (!val[0]) {
f46039
					parg++;
f46039
					ch = *parg;
f46039
				}
f46039
f46039
				if (option->optarg == ARGV_OPTARG_NONE) {
f46039
					if (val[0]) {
f46039
						ferror = ARGV_ERROR_OPTARG_NONE;
f46039
						ctx->errch = val + 1;
f46039
					} else
f46039
						fval = false;
f46039
				} else if (val[0] && !val[1])
f46039
					ferror = 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)
f46039
						ferror = ARGV_ERROR_OPTARG_REQUIRED;
f46039
					else if (*parg && is_short_option(*parg))
f46039
						ferror = ARGV_ERROR_OPTARG_REQUIRED;
f46039
					else if (*parg && is_long_option(*parg))
f46039
						ferror = ARGV_ERROR_OPTARG_REQUIRED;
f46039
					else if (*parg && is_last_option(*parg))
f46039
						ferror = ARGV_ERROR_OPTARG_REQUIRED;
f46039
					else
f46039
						fval = true;
f46039
				} else {
f46039
					/* ARGV_OPTARG_OPTIONAL */
f46039
					if (!val[0] && !*parg)
f46039
						fval = false;
f46039
					if (*parg && is_short_option(*parg))
f46039
						fval = false;
f46039
					else if (*parg && is_long_option(*parg))
f46039
						fval = false;
f46039
					else if (*parg && is_last_option(*parg))
f46039
						fval = false;
f46039
					else
f46039
						fval = *parg;
f46039
				}
f46039
			} else
f46039
				ferror = ARGV_ERROR_LONG_OPTION;
f46039
		}
f46039
f46039
		if (ferror == ARGV_ERROR_OK)
f46039
			if (option && fval && option->paradigm)
f46039
				if (!is_arg_in_paradigm(ch,option->paradigm))
f46039
					ferror = ARGV_ERROR_OPTARG_PARADIGM;
f46039
f46039
		if (ferror != ARGV_ERROR_OK) {
f46039
			ctx->errcode = ferror;
f46039
			ctx->errch   = ctx->errch ? ctx->errch : ch;
f46039
			ctx->erropt  = option;
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
{
f46039
	fprintf(stderr,"%s: error: ",ctx->program);
f46039
f46039
	switch (ctx->errcode) {
f46039
		case ARGV_ERROR_SHORT_OPTION:
f46039
			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:
f46039
			fprintf(stderr,"'%s' is not a valid option value for [%s%c%s%s%s] (option values may not be specified)\n",
f46039
				ctx->errch,
f46039
				ctx->erropt->short_name ? "-" : "",
f46039
				ctx->erropt->short_name,
f46039
				ctx->erropt->short_name ? "," : "",
f46039
				ctx->erropt->long_name ? "--" : "",
f46039
				ctx->erropt->long_name);
f46039
			break;
f46039
f46039
		case ARGV_ERROR_OPTARG_REQUIRED:
f46039
			fprintf(stderr,"option [%s%c%s%s%s] requires %s %s%s%s\n",
f46039
				ctx->erropt->short_name ? "-" : "",
f46039
				ctx->erropt->short_name,
f46039
				ctx->erropt->short_name ? "," : "",
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:
f46039
			fprintf(stderr,"'%s' is not a valid option value for [%s%c%s%s%s]={%s}\n",
f46039
				ctx->errch,
f46039
				ctx->erropt->short_name ? "-" : "",
f46039
				ctx->erropt->short_name,
f46039
				ctx->erropt->short_name ? "," : "",
f46039
				ctx->erropt->long_name ? "--" : "",
f46039
				ctx->erropt->long_name,
f46039
				ctx->erropt->paradigm);
f46039
			break;
f46039
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(
f46039
	const struct argv_option	options[],
f46039
	struct argv_ctx *		ctx,
f46039
	struct argv_meta *		meta)
f46039
{
f46039
	int				argc;
f46039
	const char **			argv;
f46039
	struct argv_entry *		entry;
f46039
	const struct argv_option *	option;
f46039
	char				short_name[2] = {0};
f46039
	const char *			space = "";
f46039
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) {
f46039
			option = option_from_tag(options,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
f46039
static struct argv_meta * argv_alloc(const char ** argv, struct argv_ctx * ctx)
f46039
{
f46039
	struct argv_meta_impl * imeta;
f46039
	const char **		vector;
f46039
	char *			dst;
f46039
	size_t			size;
f46039
	int			argc;
f46039
	int			i;
f46039
f46039
	if (!(imeta = calloc(sizeof(*imeta),1)))
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
f46039
		if (!(imeta->argv = calloc(sizeof(char *),argc+1)))
f46039
			return argv_free_impl(imeta);
f46039
		else if (!(imeta->strbuf = calloc(size+1,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
f46039
	if (!(imeta->meta.entries = calloc(sizeof(struct argv_entry),ctx->nentries+1)))
f46039
		return argv_free_impl(imeta);
f46039
	else
f46039
		return &imeta->meta;
f46039
}
f46039
f46039
static struct argv_meta * argv_get(
f46039
	const char *			argv[],
f46039
	const struct argv_option	options[],
f46039
	int				flags)
f46039
{
f46039
	struct argv_meta *	meta;
f46039
	struct argv_ctx		ctx = {flags,ARGV_MODE_SCAN,0,0,0,0};
f46039
f46039
	argv_scan(argv,options,&ctx,0);
f46039
f46039
	if (ctx.errcode != ARGV_ERROR_OK) {
f46039
		if (!ctx.program)
f46039
			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;
f46039
	argv_scan(meta->argv,options,&ctx,meta);
f46039
f46039
	if (ctx.errcode != ARGV_ERROR_OK) {
f46039
		if (!ctx.program)
f46039
			ctx.program = argv[0];
f46039
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)
f46039
		argv_show_status(options,&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,
f46039
	const struct argv_option	options[],
f46039
	const char *			mode)
f46039
{
f46039
	const struct argv_option *	option;
f46039
	bool				fshort,flong;
f46039
	bool				fnewline;
f46039
	size_t				len,optlen;
f46039
	size_t				paralen,rparalen,mparalen;
f46039
	size_t				desclen,rdesclen;
f46039
f46039
	char *				para;
f46039
	char *				next_para;
f46039
	char *				desc;
f46039
	char *				next_desc;
f46039
	char *				paradigm;
f46039
	char *				buf;
f46039
	size_t				buflen;
f46039
	const char *			sdescription;
f46039
	const char *			sargname;
f46039
f46039
	const char			indent[] = "  ";
f46039
	const int			rblen  = sizeof("}") - sizeof(char);
f46039
	const int			rbblen = sizeof("{]") - sizeof(char);
f46039
	const int			brcklen= sizeof("[]") - sizeof(char);
f46039
	const int			solen  = sizeof("-") - sizeof(char);
f46039
	const int			lolen  = sizeof("--") - sizeof(char);
f46039
	const int			slolen = sizeof("-X,--") - sizeof(char);
f46039
f46039
	fshort = mode ? !strcmp(mode,"short") : 0;
f46039
	flong  = fshort ? 0 : mode && !strcmp(mode,"long");
f46039
f46039
	if (header)
f46039
		fprintf(stdout,"%s",header);
f46039
f46039
	for (option=options,optlen=0,paralen=0; option->short_name || option->long_name; option++) {
f46039
		if (fshort)
f46039
			len = option->short_name ? sizeof(char) + solen : 0;
f46039
		else if (flong)
f46039
			len = option->long_name ? strlen(option->long_name) + lolen : 0;
f46039
		else
f46039
			len = option->long_name ? strlen(option->long_name) + slolen : 0;
f46039
f46039
		if (len) {
f46039
			if (len > optlen)
f46039
				optlen = len;
f46039
f46039
			if (option->paradigm)
f46039
				len = strlen(option->paradigm) + strlen("{}");
f46039
			else if (option->argname)
f46039
				len = strlen(option->argname);
f46039
			else if (option->optarg != ARGV_OPTARG_NONE)
f46039
				len = strlen("<val>");
f46039
f46039
			if (option->optarg == ARGV_OPTARG_OPTIONAL)
f46039
				len += strlen("[]");
f46039
f46039
			if (len > paralen)
f46039
				paralen = len;
f46039
		}
f46039
	}
f46039
8d9552
	optlen += ARGV_TAB;
8d9552
	optlen &= (~(ARGV_TAB-1));
f46039
f46039
	if (paralen) {
8d9552
		paralen += (ARGV_TAB);
8d9552
		paralen &= (~(ARGV_TAB-1));
f46039
		mparalen = paralen + 2*rbblen;
f46039
f46039
		if (optlen + paralen > 64)
f46039
			paralen = 32;
f46039
	}
f46039
f46039
	/* account for '  ','\t', try to fit in 80 or 96 columns */
8d9552
	if (optlen+paralen+2+ARGV_TAB < 80-32)
8d9552
		desclen = 80 - (optlen+paralen+2+ARGV_TAB);
8d9552
	else if (optlen+paralen+2+ARGV_TAB < 96-32)
8d9552
		desclen = 96 - (optlen+paralen+2+ARGV_TAB);
f46039
	else
f46039
		desclen = 32;
f46039
f46039
	paradigm = next_para = buf = 0;
f46039
	fnewline = false;
f46039
	rparalen = 0;
f46039
	mparalen = 0;
f46039
f46039
	for (option=options,buflen=0,rdesclen=1; option->short_name || option->long_name; option++) {
f46039
		if (option->paradigm) {
f46039
			if (option->optarg == ARGV_OPTARG_OPTIONAL)
f46039
				rparalen = strlen(option->paradigm) - 2*rbblen;
f46039
			else
f46039
				rparalen = strlen(option->paradigm) - 2*rblen;
f46039
		}
f46039
f46039
		sdescription 	= option->description ? option->description : "";
f46039
		sargname	= option->argname ? option->argname : "";
f46039
f46039
		if (option->paradigm)
f46039
			rdesclen = snprintf(buf,buflen,sdescription,option->paradigm);
f46039
		else
f46039
			rdesclen = snprintf(buf,buflen,sdescription,sargname);
f46039
f46039
		if (fnewline)
f46039
			(void)0;
f46039
f46039
		if ((rparalen > paralen) || (rdesclen > desclen)) {
f46039
			if (!fnewline) {
f46039
				(void)0;
f46039
				fnewline = true;
f46039
			}
f46039
		} else
f46039
			fnewline = false;
f46039
f46039
		if (fshort)
f46039
			fprintf(file,"%s-%-*c",indent,(int)(optlen-solen),option->short_name);
f46039
		else if (flong)
f46039
			fprintf(file,"%s--%-*s",indent,(int)(optlen-lolen),option->long_name);
f46039
		else {
f46039
			if (option->short_name && option->long_name)
f46039
				fprintf(file,"%s-%c,--%-*s",indent,option->short_name,(int)(optlen-slolen),option->long_name);
f46039
			else if (option->short_name)
f46039
				 fprintf(file,"%s-%-*c",indent,(int)(optlen-solen),option->short_name);
f46039
			else
f46039
				fprintf(file,"%s%3s--%-*s",indent,"",(int)(optlen-slolen),option->long_name);
f46039
		}
f46039
f46039
		if (rdesclen > buflen) {
f46039
			if (buf) {
f46039
				free(buf);
f46039
				buf = 0;
f46039
			}
f46039
f46039
			len =  rdesclen + 512;
f46039
			len &= (~511);
f46039
f46039
			if ((buf = calloc(len,1))) {
f46039
				buflen = len;
f46039
f46039
				if (option->paradigm)
f46039
					rdesclen = snprintf(buf,buflen,option->description,option->paradigm);
f46039
				else
f46039
					rdesclen = snprintf(buf,buflen,option->description,option->argname);
f46039
			} else {
f46039
				buflen = 0;
f46039
				continue;
f46039
			}
f46039
		}
f46039
f46039
		if (option->paradigm && (rparalen <= paralen)) {
f46039
			if (option->optarg == ARGV_OPTARG_OPTIONAL)
f46039
				fprintf(file,"[{%s}]%-*c",option->paradigm,(int)(paralen-strlen(option->paradigm)-2*rbblen),' ');
f46039
			else
f46039
				fprintf(file,"{%s}%-*c",option->paradigm,(int)(paralen-strlen(option->paradigm)-rbblen),' ');
f46039
			para = (char *)0;
f46039
		} else if (option->paradigm) {
f46039
			if (!paradigm && !(paradigm = calloc(mparalen,1))) {
f46039
				fputc('\n',file);
f46039
				continue;
f46039
			} else
f46039
				para = strcpy(paradigm,option->paradigm);
f46039
f46039
			if (option->optarg == ARGV_OPTARG_OPTIONAL) {
f46039
				fputs("[{",file);
f46039
				rparalen = paralen - rbblen;
f46039
			} else {
f46039
				fputc('{',file);
f46039
				rparalen = paralen - rblen;
f46039
			}
f46039
		} else if (option->argname) {
f46039
			if (option->optarg == ARGV_OPTARG_OPTIONAL)
f46039
				fprintf(file,"[%s]%-*c",option->argname,(int)(paralen-strlen(option->argname)-brcklen),' ');
f46039
			else
f46039
				fprintf(file,"%s%-*c",option->argname,(int)(paralen-strlen(option->argname)),' ');
f46039
			para = (char *)0;
f46039
		} else {
f46039
			fprintf(file,"%-*c",(int)paralen,' ');
f46039
			para = (char *)0;
f46039
		}
f46039
f46039
f46039
		if (!para && option->description && rdesclen <= desclen) {
f46039
			fputc('\t',file);
f46039
			fputs(buf,file);
f46039
			desc = (char *)0;
f46039
		} else if (option->description)
f46039
			desc = buf;
f46039
		else
f46039
			desc = (char *)0;
f46039
f46039
		while (para || desc) {
f46039
			if (para) {
f46039
				for (next_para=para+rparalen-1; (next_para>para) && (*next_para!='|'); )
f46039
					next_para--;
f46039
f46039
				if (para > paradigm) {
f46039
					if (option->optarg == ARGV_OPTARG_OPTIONAL)
f46039
						fputs("  ",file);
f46039
					else
f46039
						fputc(' ',file);
f46039
				}
f46039
f46039
				if (*next_para != '|') {
f46039
					fprintf(file,"%s",para);
f46039
					para = (char *)0;
f46039
				} else if (next_para > para) {
f46039
					*next_para = '\0';
f46039
					fprintf(file,"%-*s",(int)rparalen,para);
f46039
					*next_para = '|';
f46039
					para = next_para;
f46039
					rparalen = strlen(para);
f46039
f46039
					/* 2*rbblen,2*rblen, etc.: account for indentation */
f46039
					if (option->optarg == ARGV_OPTARG_OPTIONAL)
f46039
						rparalen = (rparalen+2*rbblen > paralen) ? paralen-rbblen : rparalen;
f46039
					else
f46039
						rparalen = (rparalen+2*rblen > paralen) ? paralen-rblen : rparalen;
f46039
				} else {
f46039
					if (option->optarg == ARGV_OPTARG_OPTIONAL)
f46039
						fprintf(file,"%s}]%-*c",para,(int)(paralen-strlen(para)-rbblen),' ');
f46039
					else
f46039
						fprintf(file,"%s}%-*c",para,(int)(paralen-strlen(para)-rblen),' ');
f46039
					para = (char *)0;
f46039
				}
f46039
			} else if (desc > buf)
f46039
				fprintf(file,"%-*c",(int)paralen,' ');
f46039
f46039
			if (desc) {
f46039
				if (desc > buf)
f46039
					fputs("\t ",file);
f46039
				else
f46039
					fputc('\t',file);
f46039
f46039
				if ((rdesclen = strlen(desc)+(desc>buf)) <= desclen) {
f46039
					fputs(desc,file);
f46039
					desc = (char *)0;
f46039
				} else {
f46039
					for (next_desc=desc+desclen-1; (next_desc>desc) && (*next_desc!=' ') && (*next_desc!='\n'); )
f46039
						next_desc--;
f46039
f46039
					if ((*next_desc != ' ') && (*next_desc!='\n')) {
f46039
						fputs(desc,file);
f46039
						desc = (char *)0;
f46039
					} else if (next_desc > desc) {
f46039
						*next_desc = '\0';
f46039
						fputs(desc,file);
f46039
						desc = ++next_desc;
f46039
					} else {
f46039
						fputs(desc,file);
f46039
						desc = (char *)0;
f46039
					}
f46039
				}
f46039
			}
f46039
f46039
			if (para || desc)
f46039
				fprintf(file,"\n%s%-*c",indent,(int)optlen,' ');
f46039
		}
f46039
f46039
		fputc('\n',file);
f46039
	}
f46039
f46039
	if (paradigm)
f46039
		free(paradigm);
f46039
f46039
	if (buf)
f46039
		free(buf);
f46039
}
f46039
f46039
#endif
f46039
f46039
#endif