Blob Blame History Raw
#include <stdio.h>
#include <unistd.h>
#include <sofort/sofort.h>
#include "sofort_driver_impl.h"

#ifndef SFRT_DRIVER_FLAGS
#define SFRT_DRIVER_FLAGS	SFRT_DRIVER_VERBOSITY_ERRORS \
				| SFRT_DRIVER_VERBOSITY_USAGE
#endif

static const char vermsg[] = "%s%s%s (git://midipix.org/sofort): "
			     "version %s%d.%d.%d%s.\n"
			     "[commit reference: %s%s%s]\n";

static const char * const sfrt_ver_color[6] = {
		"\x1b[1m\x1b[35m","\x1b[0m",
		"\x1b[1m\x1b[32m","\x1b[0m",
		"\x1b[1m\x1b[34m","\x1b[0m"
};

static const char * const sfrt_ver_plain[6] = {
		"","",
		"","",
		"",""
};

static ssize_t sfrt_version(struct sfrt_driver_ctx * dctx)
{
	const struct sfrt_source_version * verinfo;
	const char * const * verclr;

	verinfo = sfrt_source_version();
	verclr  = isatty(STDOUT_FILENO) ? sfrt_ver_color : sfrt_ver_plain;

	return fprintf(stdout,vermsg,
			verclr[0],dctx->program,verclr[1],
			verclr[2],verinfo->major,verinfo->minor,
			verinfo->revision,verclr[3],
			verclr[4],verinfo->commit,verclr[5]);
}

static void sfrt_perform_unit_actions(
	const struct sfrt_driver_ctx *	dctx,
	struct sfrt_unit_ctx *		uctx)
{
	uint64_t flags = dctx->cctx->actflags;			/* dummy */
								/* dummy */
	if (flags & SFRT_OUTPUT_NAME)				/* dummy */
		sfrt_output_name(uctx,stdout);			/* dummy */
								/* dummy */
	if (flags & SFRT_OUTPUT_ADDRESS)			/* dummy */
		sfrt_output_address(uctx,stdout);		/* dummy */
}

static int sfrt_exit(struct sfrt_driver_ctx * dctx, int ret)
{
	sfrt_output_error_vector(dctx);
	sfrt_free_driver_ctx(dctx);
	return ret;
}

int sfrt_main(int argc, char ** argv, char ** envp)
{
	int				ret;
	struct sfrt_driver_ctx *	dctx;
	struct sfrt_unit_ctx *		uctx;
	const char **			unit;

	if ((ret = sfrt_get_driver_ctx(argv,envp,SFRT_DRIVER_FLAGS,&dctx)))
		return (ret == SFRT_USAGE)
			? !--argc
			: SFRT_ERROR;

	if (dctx->cctx->drvflags & SFRT_DRIVER_VERSION)
		if ((sfrt_version(dctx)) < 0)
			return sfrt_exit(dctx,SFRT_ERROR);

	if (dctx->cctx->anystring)				/* dummy */
		if ((sfrt_output_dummy(dctx->cctx,stdout)) < 0)	/* dummy */
			return sfrt_exit(dctx,2);		/* dummy */
								/* dummy */
	for (unit=dctx->units; *unit && !dctx->errv[0]; unit++) {
		if (!(sfrt_get_unit_ctx(dctx,*unit,&uctx))) {
			sfrt_perform_unit_actions(dctx,uctx);
			sfrt_free_unit_ctx(uctx);
		}
	}

	return sfrt_exit(dctx,dctx->errv[0] ? SFRT_ERROR : SFRT_OK);
}