Blame src/driver/tpax_amain.c

88751e
/******************************************************/
88751e
/*  tpax: a topological pax implementation            */
88751e
/*  Copyright (C) 2020  Z. Gilboa                     */
88751e
/*  Released under GPLv2 and GPLv3; see COPYING.TPAX. */
88751e
/******************************************************/
88751e
88751e
#include <stdio.h>
88751e
#include <unistd.h>
88751e
#include <tpax/tpax.h>
88751e
#include "tpax_driver_impl.h"
88751e
#include "tpax_dprintf_impl.h"
88751e
88751e
#ifndef TPAX_DRIVER_FLAGS
88751e
#define TPAX_DRIVER_FLAGS	TPAX_DRIVER_VERBOSITY_ERRORS \
23fa88
				| TPAX_DRIVER_VERBOSITY_USAGE \
23fa88
				| TPAX_DRIVER_DIR_MEMBER_RECURSE
88751e
#endif
88751e
88751e
static const char vermsg[] = "%s%s%s (https://git.foss21.org/tpax): "
88751e
			     "version %s%d.%d.%d%s.\n"
88751e
			     "[commit reference: %s%s%s]\n";
88751e
88751e
static const char * const tpax_ver_color[6] = {
88751e
		"\x1b[1m\x1b[35m","\x1b[0m",
88751e
		"\x1b[1m\x1b[32m","\x1b[0m",
88751e
		"\x1b[1m\x1b[34m","\x1b[0m"
88751e
};
88751e
88751e
static const char * const tpax_ver_plain[6] = {
88751e
		"","",
88751e
		"","",
88751e
		"",""
88751e
};
88751e
88751e
static ssize_t tpax_version(struct tpax_driver_ctx * dctx, int fdout)
88751e
{
88751e
	const struct tpax_source_version * verinfo;
88751e
	const char * const * verclr;
88751e
88751e
	verinfo = tpax_source_version();
88751e
	verclr  = isatty(fdout) ? tpax_ver_color : tpax_ver_plain;
88751e
88751e
	return tpax_dprintf(
88751e
			fdout,vermsg,
88751e
			verclr[0],dctx->program,verclr[1],
88751e
			verclr[2],verinfo->major,verinfo->minor,
88751e
			verinfo->revision,verclr[3],
88751e
			verclr[4],verinfo->commit,verclr[5]);
88751e
}
88751e
88751e
static void tpax_perform_unit_actions(
88751e
	const struct tpax_driver_ctx *	dctx,
88751e
	struct tpax_unit_ctx *		uctx)
88751e
{
88751e
	(void)dctx;
88751e
	(void)uctx;
88751e
}
88751e
88751e
static int tpax_exit(struct tpax_driver_ctx * dctx, int ret)
88751e
{
88751e
	tpax_output_error_vector(dctx);
88751e
	tpax_free_driver_ctx(dctx);
88751e
	return ret;
88751e
}
88751e
88751e
int tpax_main(char ** argv, char ** envp, const struct tpax_fd_ctx * fdctx)
88751e
{
88751e
	int			ret;
88751e
	int			fdout;
88751e
	uint64_t		flags;
88751e
	struct tpax_driver_ctx *	dctx;
88751e
	struct tpax_unit_ctx *	uctx;
88751e
	const char **		unit;
88751e
88751e
	flags = TPAX_DRIVER_FLAGS;
88751e
	fdout = fdctx ? fdctx->fdout : STDOUT_FILENO;
88751e
88751e
	if ((ret = tpax_get_driver_ctx(argv,envp,flags,fdctx,&dctx)))
88751e
		return (ret == TPAX_USAGE)
88751e
			? !argv || !argv[0] || !argv[1]
88751e
			: TPAX_ERROR;
88751e
88751e
	if (dctx->cctx->drvflags & TPAX_DRIVER_VERSION)
88751e
		if ((tpax_version(dctx,fdout)) < 0)
88751e
			return tpax_exit(dctx,TPAX_ERROR);
88751e
88751e
	for (unit=dctx->units; *unit; unit++) {
88751e
		if (!(tpax_get_unit_ctx(dctx,*unit,&uctx))) {
88751e
			tpax_perform_unit_actions(dctx,uctx);
88751e
			tpax_free_unit_ctx(uctx);
88751e
		}
88751e
	}
88751e
88751e
	return tpax_exit(dctx,dctx->errv[0] ? TPAX_ERROR : TPAX_OK);
88751e
}