Blame src/driver/pe_driver_ctx.c

81e8b3
#include <stdint.h>
6f882b
#include <unistd.h>
81e8b3
#include <fcntl.h>
c61328
249590
#define ARGV_DRIVER
249590
81e8b3
#include <perk/perk.h>
949bb3
#include <perk/perk_output.h>
249590
#include "perk_driver_impl.h"
c61328
#include "perk_impl.h"
81e8b3
#include "argv/argv.h"
81e8b3
249590
extern const struct argv_option pe_default_options[];
81e8b3
1711ab
struct pe_driver_ctx_alloc {
44d7e6
	struct argv_meta *		meta;
c61328
	struct pe_driver_ctx_impl	ctx;
44d7e6
	uint64_t			guard;
44d7e6
	const char *			units[];
81e8b3
};
81e8b3
81e8b3
static uint32_t pe_argv_flags(uint32_t flags)
81e8b3
{
81e8b3
	uint32_t ret = 0;
81e8b3
81e8b3
	if (flags & PERK_DRIVER_VERBOSITY_NONE)
81e8b3
		ret |= ARGV_VERBOSITY_NONE;
81e8b3
81e8b3
	if (flags & PERK_DRIVER_VERBOSITY_ERRORS)
81e8b3
		ret |= ARGV_VERBOSITY_ERRORS;
81e8b3
81e8b3
	if (flags & PERK_DRIVER_VERBOSITY_STATUS)
81e8b3
		ret |= ARGV_VERBOSITY_STATUS;
81e8b3
81e8b3
	return ret;
81e8b3
}
81e8b3
dd97ea
static int pe_driver_usage(
6f882b
	const char *		program,
6f882b
	const char *		arg,
249590
	const struct argv_option *	options,
dd97ea
	struct argv_meta *	meta)
dd97ea
{
dd97ea
	char header[512];
dd97ea
dd97ea
	snprintf(header,sizeof(header),
dd97ea
		"Usage: %s [options] <file>...\n" "Options:\n",
dd97ea
		program);
dd97ea
dd97ea
	argv_usage(stdout,header,options,arg);
dd97ea
	argv_free(meta);
dd97ea
dd97ea
	return PERK_USAGE;
dd97ea
}
dd97ea
c61328
static struct pe_driver_ctx_impl * pe_driver_ctx_alloc(struct argv_meta * meta, size_t nunits)
81e8b3
{
1711ab
	struct pe_driver_ctx_alloc *	ictx;
81e8b3
	size_t				size;
81e8b3
	struct argv_entry *		entry;
81e8b3
	const char **			units;
81e8b3
1711ab
	size =  sizeof(struct pe_driver_ctx_alloc);
81e8b3
	size += (nunits+1)*sizeof(const char *);
81e8b3
81e8b3
	if (!(ictx = calloc(size,1)))
81e8b3
		return 0;
81e8b3
81e8b3
	ictx->meta		= meta;
1d9a6e
	ictx->ctx.cctx.symctx	= &ictx->ctx.symctx;
1d9a6e
	ictx->ctx.cctx.outctx	= &ictx->ctx.outctx;
1d9a6e
	ictx->ctx.cctx.lnkctx	= &ictx->ctx.lnkctx;
1d9a6e
	ictx->ctx.cctx.srvctx	= &ictx->ctx.srvctx;
76aabf
	ictx->ctx.cctx.ioctx	= &ictx->ctx.ioctx;
81e8b3
81e8b3
	for (entry=meta->entries,units=ictx->units; entry->fopt || entry->arg; entry++)
81e8b3
		if (!entry->fopt)
81e8b3
			*units++ = entry->arg;
81e8b3
c61328
	ictx->ctx.ctx.units = ictx->units;
81e8b3
	return &ictx->ctx;
81e8b3
}
81e8b3
81e8b3
int pe_get_driver_ctx_fail(struct argv_meta * meta)
81e8b3
{
81e8b3
	argv_free(meta);
81e8b3
	return -1;
81e8b3
}
81e8b3
81e8b3
int pe_get_driver_ctx(
81e8b3
	const char **		argv,
81e8b3
	const char **		envp,
81e8b3
	uint32_t		flags,
81e8b3
	struct pe_driver_ctx ** pctx)
81e8b3
{
c61328
	struct pe_driver_ctx_impl *	ctx;
249590
	const struct argv_option *	options;
44d7e6
	struct argv_meta *		meta;
44d7e6
	struct argv_entry *		entry;
44d7e6
	size_t				nunits;
44d7e6
	uint64_t			dflags;
44d7e6
	uint64_t			fflags;
44d7e6
	const char *			program;
44d7e6
	const char *			output;
44d7e6
	const char *			pretty;
44d7e6
	int				fdout;
81e8b3
249590
	options = pe_default_options;
249590
81e8b3
	if (!(meta = argv_get(argv,options,pe_argv_flags(flags))))
81e8b3
		return -1;
81e8b3
81e8b3
	dflags	= 0;
81e8b3
	fflags	= 0;
81e8b3
	output	= 0;
0d3dde
	pretty	= 0;
81e8b3
	nunits	= 0;
4e7a71
	fdout	= 0;
81e8b3
	program = argv_program_name(argv[0]);
81e8b3
dd97ea
	if (!argv[1] && (flags & PERK_DRIVER_VERBOSITY_USAGE))
249590
		return pe_driver_usage(program,0,options,meta);
dd97ea
81e8b3
	/* get options, count units */
81e8b3
	for (entry=meta->entries; entry->fopt || entry->arg; entry++) {
81e8b3
		if (entry->fopt) {
81e8b3
			switch (entry->tag) {
81e8b3
				case TAG_HELP:
dd97ea
					if (flags & PERK_DRIVER_VERBOSITY_USAGE)
249590
						return pe_driver_usage(program,entry->arg,options,meta);
81e8b3
81e8b3
				case TAG_VERSION:
8fd1f5
					dflags |= PERK_DRIVER_VERSION;
81e8b3
					break;
81e8b3
81e8b3
				case TAG_OUTPUT:
81e8b3
					output = entry->arg;
81e8b3
					break;
81e8b3
0d3dde
				case TAG_PRETTY:
0d3dde
					pretty = entry->arg;
0d3dde
					break;
0d3dde
81e8b3
				case TAG_EXPSYMS:
8fd1f5
					fflags |= PERK_OUTPUT_EXPORT_SYMS;
81e8b3
					break;
9c5807
9c5807
				case TAG_IMPLIBS:
9c5807
					fflags |= PERK_OUTPUT_IMPORT_LIBS;
9c5807
					break;
9c5807
9c5807
				case TAG_IMPSYMS:
9c5807
					fflags |= PERK_OUTPUT_IMPORT_SYMS;
9c5807
					break;
81e8b3
			}
81e8b3
		} else
81e8b3
			nunits++;
81e8b3
	}
81e8b3
81e8b3
	if (output && ((fdout = open(output,
81e8b3
			O_CREAT|O_TRUNC|O_WRONLY|O_NOCTTY|O_NOFOLLOW,
81e8b3
			S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0))
81e8b3
		return pe_get_driver_ctx_fail(meta);
81e8b3
2699a2
	if (!(ctx = pe_driver_ctx_alloc(meta,nunits)) && output)
2699a2
		close(fdout);
2699a2
2699a2
	if (!ctx)
81e8b3
		return pe_get_driver_ctx_fail(meta);
81e8b3
0d3dde
	if (pretty && !strcmp(pretty,"yaml"))
0d3dde
		fflags |= PERK_PRETTY_YAML;
0d3dde
c61328
	ctx->ctx.program	= program;
81e8b3
	ctx->cctx.drvflags	= dflags;
81e8b3
	ctx->cctx.fmtflags	= fflags;
81e8b3
	ctx->cctx.output	= output;
76aabf
	ctx->ioctx.fdout	= output ? fdout : -1;
81e8b3
76aabf
	ctx->ioctx.fdin		= -1;
76aabf
	ctx->ioctx.fderr	= -1;
76aabf
	ctx->ioctx.fdlog	= -1;
76aabf
	ctx->ioctx.fdsrc	= AT_FDCWD;
76aabf
	ctx->ioctx.fddst	= AT_FDCWD;
76aabf
	ctx->ioctx.fdtmp	= AT_FDCWD;
81e8b3
c61328
	ctx->ctx.cctx		= &ctx->cctx;
c61328
c61328
	*pctx = &ctx->ctx;
81e8b3
	return PERK_OK;
81e8b3
}
81e8b3
81e8b3
static void pe_driver_close_fds(struct pe_common_ctx * cctx)
81e8b3
{
76aabf
	if (cctx->ioctx->status && cctx->output)
76aabf
		unlinkat(cctx->ioctx->fddst,cctx->output,0);
81e8b3
76aabf
	if (cctx->ioctx->fdout >= 0)
76aabf
		close(cctx->ioctx->fdout);
81e8b3
}
81e8b3
1711ab
static void pe_free_driver_ctx_impl(struct pe_driver_ctx_alloc * ictx)
81e8b3
{
1d9a6e
	if (ictx->ctx.symctx.append)
1d9a6e
		free(ictx->ctx.symctx.append);
81e8b3
1d9a6e
	if (ictx->ctx.symctx.exclude)
1d9a6e
		free(ictx->ctx.symctx.exclude);
81e8b3
81e8b3
	pe_driver_close_fds(&ictx->ctx.cctx);
81e8b3
	argv_free(ictx->meta);
81e8b3
	free(ictx);
81e8b3
}
81e8b3
81e8b3
void pe_free_driver_ctx(struct pe_driver_ctx * ctx)
81e8b3
{
1711ab
	struct pe_driver_ctx_alloc *	ictx;
81e8b3
	uintptr_t			addr;
81e8b3
81e8b3
	if (ctx) {
1711ab
		addr = (uintptr_t)ctx - offsetof(struct pe_driver_ctx_alloc,ctx);
c61328
		addr = addr - offsetof(struct pe_driver_ctx_impl,ctx);
1711ab
		ictx = (struct pe_driver_ctx_alloc *)addr;
81e8b3
		pe_free_driver_ctx_impl(ictx);
81e8b3
	}
81e8b3
}