Blame src/driver/ntux_amain.c

26e14f
/***********************************************************/
26e14f
/*  ntux: native translation und extension                 */
db57dd
/*  Copyright (C) 2016--2018  Z. Gilboa                    */
26e14f
/*  Released under GPLv2 and GPLv3; see COPYING.NTUX.      */
26e14f
/***********************************************************/
26e14f
26e14f
#include <stdio.h>
26e14f
#include <unistd.h>
26e14f
26e14f
#include <ntux/ntux.h>
26e14f
#include "ntux_driver_impl.h"
26e14f
#include "ntux_nolibc_impl.h"
26e14f
26e14f
#ifndef NTUX_DRIVER_FLAGS
26e14f
#define NTUX_DRIVER_FLAGS	NTUX_DRIVER_VERBOSITY_ERRORS \
26e14f
				| NTUX_DRIVER_VERBOSITY_USAGE
26e14f
#endif
26e14f
26e14f
static const char vermsg[] = "%s%s%s (git://midipix.org/ntux): "
26e14f
			     "version %s%d.%d.%d%s.\n"
26e14f
			     "[commit reference: %s%s%s]\n";
26e14f
26e14f
static const char * const ntux_ver_color[6] = {
26e14f
		"\x1b[1m\x1b[35m","\x1b[0m",
26e14f
		"\x1b[1m\x1b[32m","\x1b[0m",
26e14f
		"\x1b[1m\x1b[34m","\x1b[0m"
26e14f
};
26e14f
26e14f
static const char * const ntux_ver_plain[6] = {
26e14f
		"","",
26e14f
		"","",
26e14f
		"",""
26e14f
};
26e14f
26e14f
static ssize_t ntux_version(struct ntux_driver_ctx * dctx)
26e14f
{
26e14f
	const struct ntux_source_version * verinfo;
26e14f
	const char * const * verclr;
26e14f
26e14f
	verinfo = ntux_source_version();
26e14f
	verclr  = isatty(STDOUT_FILENO) ? ntux_ver_color : ntux_ver_plain;
26e14f
26e14f
	return fprintf(stdout,vermsg,
26e14f
			verclr[0],dctx->program,verclr[1],
26e14f
			verclr[2],verinfo->major,verinfo->minor,
26e14f
			verinfo->revision,verclr[3],
26e14f
			verclr[4],verinfo->commit,verclr[5]);
26e14f
}
26e14f
c59ae6
static void ntux_perform_unit_actions(
c59ae6
	const struct ntux_driver_ctx *	dctx,
c59ae6
	const char *			unit)
c59ae6
{
f3d5e0
	if (dctx->cctx->cmd == NTUX_CMD_STAT)
f3d5e0
		ntux_cmd_stat(dctx,unit);
c59ae6
}
c59ae6
26e14f
static int ntux_exit(struct ntux_driver_ctx * dctx, int ret)
26e14f
{
da20d8
	ntux_output_error_vector(dctx);
26e14f
	ntux_free_driver_ctx(dctx);
26e14f
	return ret;
26e14f
}
26e14f
26e14f
int ntux_main(int argc, char ** argv, char ** envp)
26e14f
{
26e14f
	int				ret;
26e14f
	struct ntux_driver_ctx *	dctx;
c59ae6
	const char **			unit;
26e14f
26e14f
	if ((ret = ntux_get_driver_ctx(argv,envp,NTUX_DRIVER_FLAGS,&dctx)))
26e14f
		return (ret == NTUX_USAGE)
26e14f
			? !--argc
26e14f
			: NTUX_ERROR;
26e14f
26e14f
	if (dctx->cctx->drvflags & NTUX_DRIVER_VERSION)
26e14f
		if ((ntux_version(dctx)) < 0)
26e14f
			return ntux_exit(dctx,NTUX_ERROR);
26e14f
c59ae6
	for (unit=dctx->units; *unit; unit++)
c59ae6
		ntux_perform_unit_actions(dctx,*unit);
c59ae6
26e14f
	return ntux_exit(dctx,dctx->errv[0] ? NTUX_ERROR : NTUX_OK);
26e14f
}