Blame src/driver/amgc_amain.c

c4360e
/**********************************************************/
c4360e
/*  apimagic: cparser-based API normalization utility     */
c4360e
/*  Copyright (C) 2015--2016  Z. Gilboa                   */
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"
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
c4360e
static ssize_t amgc_version(struct amgc_driver_ctx * dctx)
c4360e
{
c4360e
	const struct amgc_source_version * verinfo;
c4360e
	const char * const * verclr;
c4360e
c4360e
	verinfo = amgc_source_version();
c4360e
	verclr  = isatty(STDOUT_FILENO) ? amgc_ver_color : amgc_ver_plain;
c4360e
c4360e
	return fprintf(stdout,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
c4360e
static void amgc_perform_unit_actions(struct amgc_unit_ctx * uctx)
c4360e
{
c4360e
	const struct amgc_action * action;
c4360e
c4360e
	for (action=uctx->cctx->actions; action->type; action++)
c4360e
		amgc_perform_unit_action(uctx,action,0,stdout);
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
c4360e
int amgc_main(int argc, char ** argv, char ** envp)
c4360e
{
c4360e
	int				ret;
c4360e
	struct amgc_driver_ctx *	dctx;
c4360e
	struct amgc_unit_ctx *		uctx;
c4360e
	const char **			unit;
c4360e
c4360e
	if ((ret = amgc_get_driver_ctx(argv,envp,AMGC_DRIVER_FLAGS,&dctx)))
8fcd20
		return (ret == AMGC_USAGE)
8fcd20
			? !--argc
8fcd20
			: AMGC_ERROR;
c4360e
c4360e
	if (dctx->cctx->drvflags & AMGC_DRIVER_VERSION)
c4360e
		if ((amgc_version(dctx)) < 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))) {
c4360e
			amgc_perform_unit_actions(uctx);
c4360e
			amgc_free_unit_ctx(uctx);
884fc4
		}
c4360e
	}
c4360e
8fcd20
	return amgc_exit(dctx,dctx->errv[0] ? AMGC_ERROR : AMGC_OK);
c4360e
}