Blame src/driver/sfrt_driver_ctx.c

f46039
#include <stdint.h>
f46039
#include <unistd.h>
f46039
#include <fcntl.h>
f46039
f46039
#define ARGV_DRIVER
f46039
f46039
#include <sofort/sofort.h>
f46039
#include "sofort_driver_impl.h"
f46039
#include "argv/argv.h"
f46039
f46039
extern const struct argv_option sfrt_default_options[];
f46039
f46039
struct sfrt_driver_ctx_alloc {
f46039
	struct argv_meta *		meta;
f46039
	struct sfrt_driver_ctx_impl	ctx;
f46039
	uint64_t			guard;
f46039
	const char *			units[];
f46039
};
f46039
f46039
static uint32_t sfrt_argv_flags(uint32_t flags)
f46039
{
f46039
	uint32_t ret = 0;
f46039
f46039
	if (flags & SFRT_DRIVER_VERBOSITY_NONE)
f46039
		ret |= ARGV_VERBOSITY_NONE;
f46039
f46039
	if (flags & SFRT_DRIVER_VERBOSITY_ERRORS)
f46039
		ret |= ARGV_VERBOSITY_ERRORS;
f46039
f46039
	if (flags & SFRT_DRIVER_VERBOSITY_STATUS)
f46039
		ret |= ARGV_VERBOSITY_STATUS;
f46039
f46039
	return ret;
f46039
}
f46039
f46039
static int sfrt_driver_usage(
f46039
	const char *			program,
f46039
	const char *			arg,
f46039
	const struct argv_option *	options,
f46039
	struct argv_meta *		meta)
f46039
{
f46039
	char header[512];
f46039
f46039
	snprintf(header,sizeof(header),
f46039
		"Usage: %s [options] <file>...\n" "Options:\n",
f46039
		program);
f46039
f46039
	argv_usage(stdout,header,options,arg);
f46039
	argv_free(meta);
f46039
f46039
	return SFRT_USAGE;
f46039
}
f46039
f46039
static struct sfrt_driver_ctx_impl * sfrt_driver_ctx_alloc(struct argv_meta * meta, size_t nunits)
f46039
{
f46039
	struct sfrt_driver_ctx_alloc *	ictx;
f46039
	size_t				size;
f46039
	struct argv_entry *		entry;
f46039
	const char **			units;
f46039
f46039
	size =  sizeof(struct sfrt_driver_ctx_alloc);
f46039
	size += (nunits+1)*sizeof(const char *);
f46039
f46039
	if (!(ictx = calloc(size,1)))
f46039
		return 0;
f46039
f46039
	for (entry=meta->entries,units=ictx->units; entry->fopt || entry->arg; entry++)
f46039
		if (!entry->fopt)
f46039
			*units++ = entry->arg;
f46039
f46039
	ictx->ctx.ctx.units = ictx->units;
f46039
	return &ictx->ctx;
f46039
}
f46039
f46039
int sfrt_get_driver_ctx_fail(struct argv_meta * meta)
f46039
{
f46039
	argv_free(meta);
f46039
	return -1;
f46039
}
f46039
f46039
int sfrt_get_driver_ctx(
f46039
	const char **		argv,
f46039
	const char **		envp,
f46039
	uint32_t		flags,
f46039
	struct sfrt_driver_ctx ** pctx)
f46039
{
f46039
	struct sfrt_driver_ctx_impl *	ctx;
f46039
	const struct argv_option *	options;
f46039
	struct argv_meta *		meta;
f46039
	struct argv_entry *		entry;
f46039
	size_t				nunits;
f46039
	uint64_t			dflags;
f46039
	uint64_t			aflags;
f46039
	const char *			program;
bbc20f
	const char *			astring; /* dummy */
f46039
f46039
	options = sfrt_default_options;
f46039
f46039
	if (!(meta = argv_get(argv,options,sfrt_argv_flags(flags))))
f46039
		return -1;
f46039
f46039
	dflags	= 0;
f46039
	aflags	= 0;
f46039
	nunits	= 0;
bbc20f
	astring = 0; /* dummy */
f46039
	program = argv_program_name(argv[0]);
f46039
f46039
	if (!argv[1] && (flags & SFRT_DRIVER_VERBOSITY_USAGE))
f46039
		return sfrt_driver_usage(program,0,options,meta);
f46039
f46039
	/* get options, count units */
f46039
	for (entry=meta->entries; entry->fopt || entry->arg; entry++) {
f46039
		if (entry->fopt) {
f46039
			switch (entry->tag) {
f46039
				case TAG_HELP:
f46039
					if (flags & SFRT_DRIVER_VERBOSITY_USAGE)
f46039
						return sfrt_driver_usage(program,entry->arg,options,meta);
f46039
f46039
				case TAG_VERSION:
f46039
					dflags |= SFRT_DRIVER_VERSION;
f46039
					break;
bbc20f
/* dummy */
bbc20f
/* dummy */			case TAG_OUTPUT_DUMMY:
bbc20f
/* dummy */				astring = entry->arg;
bbc20f
/* dummy */				break;
bbc20f
/* dummy */
bbc20f
/* dummy */			case TAG_OUTPUT_PROPERTY:
bbc20f
/* dummy */				if (!(strcmp(entry->arg,"name")))
bbc20f
/* dummy */					aflags |= SFRT_OUTPUT_NAME;
bbc20f
/* dummy */				else if (!(strcmp(entry->arg,"address")))
bbc20f
/* dummy */					aflags |= SFRT_OUTPUT_ADDRESS;
bbc20f
/* dummy */				break;
f46039
			}
f46039
		} else
f46039
			nunits++;
f46039
	}
f46039
f46039
	if (!(ctx = sfrt_driver_ctx_alloc(meta,nunits)))
f46039
		return sfrt_get_driver_ctx_fail(meta);
f46039
f46039
	ctx->ctx.program	= program;
f46039
	ctx->cctx.drvflags	= dflags;
f46039
	ctx->cctx.actflags	= aflags;
bbc20f
	ctx->cctx.anystring	= astring; /* dummy */
f46039
f46039
	ctx->ctx.cctx		= &ctx->cctx;
f46039
f46039
	*pctx = &ctx->ctx;
f46039
	return SFRT_OK;
f46039
}
f46039
f46039
static void sfrt_free_driver_ctx_impl(struct sfrt_driver_ctx_alloc * ictx)
f46039
{
f46039
	argv_free(ictx->meta);
f46039
	free(ictx);
f46039
}
f46039
f46039
void sfrt_free_driver_ctx(struct sfrt_driver_ctx * ctx)
f46039
{
f46039
	struct sfrt_driver_ctx_alloc *	ictx;
f46039
	uintptr_t			addr;
f46039
f46039
	if (ctx) {
f46039
		addr = (uintptr_t)ctx - offsetof(struct sfrt_driver_ctx_alloc,ctx);
f46039
		addr = addr - offsetof(struct sfrt_driver_ctx_impl,ctx);
f46039
		ictx = (struct sfrt_driver_ctx_alloc *)addr;
f46039
		sfrt_free_driver_ctx_impl(ictx);
f46039
	}
f46039
}