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
c4360e
static int amgc_exit(struct amgc_driver_ctx * dctx, int nerrors)
c4360e
{
c4360e
	amgc_free_driver_ctx(dctx);
c4360e
	return nerrors ? 2 : 0;
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)))
c4360e
		return (ret == AMGC_USAGE) ? !--argc : 2;
c4360e
c4360e
	if (dctx->cctx->drvflags & AMGC_DRIVER_VERSION)
c4360e
		if ((amgc_version(dctx)) < 0)
c4360e
			return amgc_exit(dctx,2);
c4360e
c4360e
	for (unit=dctx->units; *unit; unit++) {
c4360e
		if (!(amgc_get_unit_ctx(dctx,*unit,&uctx))) {
c4360e
			amgc_perform_unit_actions(uctx);
c4360e
			ret += uctx->nerrors;
c4360e
			amgc_free_unit_ctx(uctx);
c4360e
		} else
c4360e
			ret++;
c4360e
	}
c4360e
c4360e
	return amgc_exit(dctx,ret);
c4360e
}