Blame src/internal/argv/argv.h

cde03b
/****************************************************************************/
cde03b
/*  argv.h: a thread-safe argument vector parser and usage screen generator */
cde03b
/*  Copyright (C) 2015  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
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,
cde03b
};
cde03b
cde03b
struct argv_option {
cde03b
	const char *		long_name;
cde03b
	const char		short_name;
cde03b
	int			tag;
cde03b
	enum argv_optarg	optarg;
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 {
cde03b
	const char **		argv;
cde03b
	struct argv_entry *	entries;
cde03b
};
cde03b
cde03b
struct argv_meta_impl {
cde03b
	const 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(
cde03b
	const 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
cde03b
			if (!*arg || (*arg == '=')) {
cde03b
				entry->tag	= option->tag;
cde03b
				entry->fopt	= true;
cde03b
				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
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(
cde03b
	const char **			argv,
cde03b
	const struct argv_option	options[],
cde03b
	struct argv_ctx *		ctx,
cde03b
	struct argv_meta *		meta)
cde03b
{
cde03b
	const 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;
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)) {
cde03b
		option = 0;
cde03b
cde03b
		if (fnoscan)
cde03b
			fval = true;
cde03b
cde03b
		else if (is_last_option(ch))
cde03b
			fnoscan = true;
cde03b
cde03b
		else if ((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
cde03b
		} else if ((is_long_option(ch))) {
cde03b
			if ((option = argv_long_option(ch+=2,options,&entry))) {
cde03b
				val = ch + strlen(option->long_name);
cde03b
cde03b
				/* val[0] is either '=' or '\0' */
cde03b
				if (!val[0]) {
cde03b
					parg++;
cde03b
					ch = *parg;
cde03b
				}
cde03b
cde03b
				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;
cde03b
				} 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 */
cde03b
					if (!val[0] && !*parg)
cde03b
						fval = false;
cde03b
					if (*parg && is_short_option(*parg))
cde03b
						fval = false;
cde03b
					else if (*parg && is_long_option(*parg))
cde03b
						fval = false;
cde03b
					else if (*parg && is_last_option(*parg))
cde03b
						fval = false;
cde03b
					else
cde03b
						fval = *parg;
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:
cde03b
			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:
cde03b
			fprintf(stderr,"'%s' is not a valid option value for [%s%c%s%s%s] (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
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;
cde03b
	const 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
cde03b
static struct argv_meta * argv_alloc(const char ** argv, struct argv_ctx * ctx)
cde03b
{
cde03b
	struct argv_meta_impl * imeta;
cde03b
	const char **		vector;
cde03b
	char *			dst;
cde03b
	size_t			size;
cde03b
	int			argc;
cde03b
	int			i;
cde03b
cde03b
	if (!(imeta = calloc(sizeof(*imeta),1)))
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
cde03b
		if (!(imeta->argv = calloc(sizeof(char *),argc+1)))
cde03b
			return argv_free_impl(imeta);
cde03b
		else if (!(imeta->strbuf = calloc(size+1,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
cde03b
	if (!(imeta->meta.entries = calloc(sizeof(struct argv_entry),ctx->nentries+1)))
cde03b
		return argv_free_impl(imeta);
cde03b
	else
cde03b
		return &imeta->meta;
cde03b
}
cde03b
cde03b
static struct argv_meta * argv_get(
cde03b
	const char *			argv[],
cde03b
	const struct argv_option	options[],
cde03b
	int				flags)
cde03b
{
cde03b
	struct argv_meta *	meta;
cde03b
	struct argv_ctx		ctx = {flags,ARGV_MODE_SCAN,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
cde03b
	for (option=options,optlen=0,paralen=0; 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
cde03b
	optlen += 8;
cde03b
	optlen &= (~7);
cde03b
cde03b
	if (paralen) {
cde03b
		paralen += (8);
cde03b
		paralen &= (~7);
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 */
cde03b
	if (optlen+paralen+2+8 < 80-32)
cde03b
		desclen = 80 - (optlen+paralen+2+8);
cde03b
	else if (optlen+paralen+2+8 < 96-32)
cde03b
		desclen = 96 - (optlen+paralen+2+8);
cde03b
	else
cde03b
		desclen = 32;
cde03b
cde03b
	paradigm = next_para = buf = 0;
cde03b
	fnewline = false;
cde03b
	rparalen = 0;
cde03b
	mparalen = 0;
cde03b
cde03b
	for (option=options,buflen=0,rdesclen=1; option->short_name || option->long_name; option++) {
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)
cde03b
				fprintf(file,"%s-%c,--%-*s",indent,option->short_name,(int)(optlen-slolen),option->long_name);
cde03b
			else if (option->short_name)
cde03b
				 fprintf(file,"%s-%-*c",indent,(int)(optlen-solen),option->short_name);
cde03b
			else
cde03b
				fprintf(file,"%s%3s--%-*s",indent,"",(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
cde03b
			if ((buf = calloc(len,1))) {
cde03b
				buflen = len;
cde03b
cde03b
				if (option->paradigm)
cde03b
					rdesclen = snprintf(buf,buflen,option->description,option->paradigm);
cde03b
				else
cde03b
					rdesclen = snprintf(buf,buflen,option->description,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)
cde03b
				fprintf(file,"[{%s}]%-*c",option->paradigm,(int)(paralen-strlen(option->paradigm)-2*rbblen),' ');
cde03b
			else
cde03b
				fprintf(file,"{%s}%-*c",option->paradigm,(int)(paralen-strlen(option->paradigm)-rbblen),' ');
cde03b
			para = (char *)0;
cde03b
		} else if (option->paradigm) {
cde03b
			if (!paradigm && !(paradigm = calloc(mparalen,1))) {
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)
cde03b
				fprintf(file,"[%s]%-*c",option->argname,(int)(paralen-strlen(option->argname)-brcklen),' ');
cde03b
			else
cde03b
				fprintf(file,"%s%-*c",option->argname,(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) {
cde03b
				for (next_para=para+rparalen-1; (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)
cde03b
						rparalen = (rparalen+2*rbblen > paralen) ? paralen-rbblen : rparalen;
cde03b
					else
cde03b
						rparalen = (rparalen+2*rblen > paralen) ? paralen-rblen : rparalen;
cde03b
				} else {
cde03b
					if (option->optarg == ARGV_OPTARG_OPTIONAL)
cde03b
						fprintf(file,"%s}]%-*c",para,(int)(paralen-strlen(para)-rbblen),' ');
cde03b
					else
cde03b
						fprintf(file,"%s}%-*c",para,(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 {
cde03b
					for (next_desc=desc+desclen-1; (next_desc>desc) && (*next_desc!=' ') && (*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