Blame src/driver/amgc_amain.c

c4360e
/**********************************************************/
c4360e
/*  apimagic: cparser-based API normalization utility     */
84312f
/*  Copyright (C) 2015--2021  SysDeer Technologies, LLC   */
c4360e
/*  Released under GPLv2 and GPLv3; see COPYING.APIMAGIC. */
c4360e
/**********************************************************/
c4360e
c4360e
#include <stdio.h>
c4360e
#include <unistd.h>
c4360e
#include <apimagic/apimagic.h>
c4360e
#include "apimagic_driver_impl.h"
abf923
#include "apimagic_dprintf_impl.h"
c4360e
c4360e
#ifndef AMGC_DRIVER_FLAGS
c4360e
#define AMGC_DRIVER_FLAGS	AMGC_DRIVER_VERBOSITY_ERRORS \
c4360e
				| AMGC_DRIVER_VERBOSITY_USAGE
c4360e
#endif
c4360e
c4360e
static const char vermsg[] = "%s%s%s (git://midipix.org/apimagic): "
c4360e
			     "version %s%d.%d.%d%s.\n"
c4360e
			     "[commit reference: %s%s%s]\n";
c4360e
c4360e
static const char * const amgc_ver_color[6] = {
c4360e
		"\x1b[1m\x1b[35m","\x1b[0m",
c4360e
		"\x1b[1m\x1b[32m","\x1b[0m",
c4360e
		"\x1b[1m\x1b[34m","\x1b[0m"
c4360e
};
c4360e
c4360e
static const char * const amgc_ver_plain[6] = {
c4360e
		"","",
c4360e
		"","",
c4360e
		"",""
c4360e
};
c4360e
abf923
static ssize_t amgc_version(struct amgc_driver_ctx * dctx, int fdout)
c4360e
{
c4360e
	const struct amgc_source_version * verinfo;
c4360e
	const char * const * verclr;
c4360e
c4360e
	verinfo = amgc_source_version();
abf923
	verclr  = isatty(fdout) ? amgc_ver_color : amgc_ver_plain;
c4360e
abf923
	return amgc_dprintf(
abf923
			fdout,vermsg,
c4360e
			verclr[0],dctx->program,verclr[1],
c4360e
			verclr[2],verinfo->major,verinfo->minor,
c4360e
			verinfo->revision,verclr[3],
c4360e
			verclr[4],verinfo->commit,verclr[5]);
c4360e
}
c4360e
b8225b
static void amgc_perform_unit_actions(
b8225b
	const struct amgc_driver_ctx *	dctx,
b8225b
	struct amgc_unit_ctx *		uctx)
c4360e
{
c4360e
	const struct amgc_action * action;
c4360e
c4360e
	for (action=uctx->cctx->actions; action->type; action++)
b8225b
		amgc_perform_unit_action(dctx,uctx,action,0);
c4360e
}
c4360e
884fc4
static int amgc_exit(struct amgc_driver_ctx * dctx, int ret)
c4360e
{
884fc4
	amgc_output_error_vector(dctx);
c4360e
	amgc_free_driver_ctx(dctx);
884fc4
	return ret;
c4360e
}
c4360e
c44aca
int amgc_main(char ** argv, char ** envp, const struct amgc_fd_ctx * fdctx)
c4360e
{
c4360e
	int				ret;
abf923
	int				fdout;
abf923
	uint64_t			flags;
c4360e
	struct amgc_driver_ctx *	dctx;
c4360e
	struct amgc_unit_ctx *		uctx;
c4360e
	const char **			unit;
c4360e
abf923
	flags = AMGC_DRIVER_FLAGS;
abf923
	fdout = fdctx ? fdctx->fdout : STDOUT_FILENO;
abf923
abf923
	if ((ret = amgc_get_driver_ctx(argv,envp,flags,fdctx,&dctx)))
8fcd20
		return (ret == AMGC_USAGE)
c44aca
			? !argv || !argv[0] || !argv[1]
8fcd20
			: AMGC_ERROR;
c4360e
c4360e
	if (dctx->cctx->drvflags & AMGC_DRIVER_VERSION)
abf923
		if ((amgc_version(dctx,fdout)) < 0)
8fcd20
			return amgc_exit(dctx,AMGC_ERROR);
c4360e
4aebb4
	for (unit=dctx->units; *unit && !dctx->errv[0]; unit++) {
c4360e
		if (!(amgc_get_unit_ctx(dctx,*unit,&uctx))) {
b8225b
			amgc_perform_unit_actions(dctx,uctx);
c4360e
			amgc_free_unit_ctx(uctx);
884fc4
		}
c4360e
	}
c4360e
8fcd20
	return amgc_exit(dctx,dctx->errv[0] ? AMGC_ERROR : AMGC_OK);
c4360e
}