Blame src/driver/sfrt_amain.c

9e67a4
#include <stdio.h>
9e67a4
#include <unistd.h>
9e67a4
#include <sofort/sofort.h>
9e67a4
#include "sofort_driver_impl.h"
9e67a4
9e67a4
#ifndef SFRT_DRIVER_FLAGS
9e67a4
#define SFRT_DRIVER_FLAGS	SFRT_DRIVER_VERBOSITY_ERRORS \
9e67a4
				| SFRT_DRIVER_VERBOSITY_USAGE
9e67a4
#endif
9e67a4
9e67a4
static const char vermsg[] = "%s%s%s (git://midipix.org/sofort): "
9e67a4
			     "version %s%d.%d.%d%s.\n"
9e67a4
			     "[commit reference: %s%s%s]\n";
9e67a4
9e67a4
static const char * const sfrt_ver_color[6] = {
9e67a4
		"\x1b[1m\x1b[35m","\x1b[0m",
9e67a4
		"\x1b[1m\x1b[32m","\x1b[0m",
9e67a4
		"\x1b[1m\x1b[34m","\x1b[0m"
9e67a4
};
9e67a4
9e67a4
static const char * const sfrt_ver_plain[6] = {
9e67a4
		"","",
9e67a4
		"","",
9e67a4
		"",""
9e67a4
};
9e67a4
9e67a4
static ssize_t sfrt_version(struct sfrt_driver_ctx * dctx)
9e67a4
{
9e67a4
	const struct sfrt_source_version * verinfo;
9e67a4
	const char * const * verclr;
9e67a4
9e67a4
	verinfo = sfrt_source_version();
9e67a4
	verclr  = isatty(STDOUT_FILENO) ? sfrt_ver_color : sfrt_ver_plain;
9e67a4
9e67a4
	return fprintf(stdout,vermsg,
9e67a4
			verclr[0],dctx->program,verclr[1],
9e67a4
			verclr[2],verinfo->major,verinfo->minor,
9e67a4
			verinfo->revision,verclr[3],
9e67a4
			verclr[4],verinfo->commit,verclr[5]);
9e67a4
}
9e67a4
3be86d
static void sfrt_perform_unit_actions(
3be86d
	const struct sfrt_driver_ctx *	dctx,
3be86d
	struct sfrt_unit_ctx *		uctx)
9e67a4
{
3be86d
	uint64_t flags = dctx->cctx->actflags;			/* dummy */
9e67a4
								/* dummy */
f530d3
	if (flags & SFRT_OUTPUT_NAME)				/* dummy */
f530d3
		sfrt_output_name(uctx,stdout);			/* dummy */
9e67a4
								/* dummy */
f530d3
	if (flags & SFRT_OUTPUT_ADDRESS)			/* dummy */
f530d3
		sfrt_output_address(uctx,stdout);		/* dummy */
9e67a4
}
9e67a4
f530d3
static int sfrt_exit(struct sfrt_driver_ctx * dctx, int ret)
9e67a4
{
f530d3
	sfrt_output_error_vector(dctx);
9e67a4
	sfrt_free_driver_ctx(dctx);
f530d3
	return ret;
9e67a4
}
9e67a4
9e67a4
int sfrt_main(int argc, char ** argv, char ** envp)
9e67a4
{
9e67a4
	int				ret;
9e67a4
	struct sfrt_driver_ctx *	dctx;
9e67a4
	struct sfrt_unit_ctx *		uctx;
9e67a4
	const char **			unit;
9e67a4
9e67a4
	if ((ret = sfrt_get_driver_ctx(argv,envp,SFRT_DRIVER_FLAGS,&dctx)))
9e67a4
		return (ret == SFRT_USAGE) ? !--argc : 2;
9e67a4
9e67a4
	if (dctx->cctx->drvflags & SFRT_DRIVER_VERSION)
9e67a4
		if ((sfrt_version(dctx)) < 0)
9e67a4
			return sfrt_exit(dctx,2);
9e67a4
9e67a4
	if (dctx->cctx->anystring)				/* dummy */
9e67a4
		if ((sfrt_output_dummy(dctx->cctx,stdout)) < 0)	/* dummy */
9e67a4
			return sfrt_exit(dctx,2);		/* dummy */
9e67a4
								/* dummy */
9e67a4
	for (unit=dctx->units; *unit; unit++) {
9e67a4
		if (!(sfrt_get_unit_ctx(dctx,*unit,&uctx))) {
3be86d
			sfrt_perform_unit_actions(dctx,uctx);
9e67a4
			sfrt_free_unit_ctx(uctx);
9e67a4
		}
9e67a4
	}
9e67a4
f530d3
	return sfrt_exit(dctx,dctx->errv[0] ? 2 : 0);
9e67a4
}