Blame src/driver/ntux_amain.c

26e14f
/***********************************************************/
26e14f
/*  ntux: native translation und extension                 */
47596e
/*  Copyright (C) 2016--2021  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>
ec0005
#include "ntux_dprintf_impl.h"
26e14f
#include "ntux_driver_impl.h"
26e14f
#include "ntux_nolibc_impl.h"
26e14f
74fb0c
#include <psxxfi/xfi_exit.h>
74fb0c
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
ec0005
static ssize_t ntux_version(struct ntux_driver_ctx * dctx, int fdout)
26e14f
{
26e14f
	const struct ntux_source_version * verinfo;
26e14f
	const char * const * verclr;
26e14f
26e14f
	verinfo = ntux_source_version();
ec0005
	verclr  = isatty(fdout) ? ntux_ver_color : ntux_ver_plain;
26e14f
2b5e80
	return ntux_dprintf(
ec0005
			fdout,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);
80f43a
80f43a
	else if (dctx->cctx->cmd == NTUX_CMD_CHMOD)
80f43a
		ntux_cmd_chmod(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
1cf502
int ntux_main(char ** argv, char ** envp, const struct ntux_fd_ctx * fdctx)
26e14f
{
26e14f
	int				ret;
ec0005
	int				fdout;
ec0005
	uint64_t			flags;
26e14f
	struct ntux_driver_ctx *	dctx;
c59ae6
	const char **			unit;
26e14f
ec0005
	flags = NTUX_DRIVER_FLAGS;
ec0005
	fdout = fdctx ? fdctx->fdout : STDOUT_FILENO;
ec0005
ec0005
	if ((ret = ntux_get_driver_ctx(argv,envp,flags,fdctx,&dctx)))
26e14f
		return (ret == NTUX_USAGE)
1cf502
			? !argv || !argv[0] || !argv[1]
26e14f
			: NTUX_ERROR;
26e14f
26e14f
	if (dctx->cctx->drvflags & NTUX_DRIVER_VERSION)
ec0005
		if ((ntux_version(dctx,fdout)) < 0)
26e14f
			return ntux_exit(dctx,NTUX_ERROR);
26e14f
74fb0c
	if (dctx->cctx->cmd == NTUX_CMD_SPAWN) {
5ec9b6
		ntux_cmd_spawn(dctx);
74fb0c
		__xfi_exit(dctx->cctx->status);
74fb0c
	}
5ec9b6
74fb0c
	if (dctx->cctx->cmd == NTUX_CMD_STRACE) {
8ce751
		ntux_cmd_strace(dctx);
74fb0c
		__xfi_exit(dctx->cctx->status);
74fb0c
	}
8ce751
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
}