Blame src/apimagic.c

383aa6
/**********************************************************/
383aa6
/*  apimagic: cparser-based API normalization utility     */
383aa6
/*  Copyright (C) 2015--2016  Z. Gilboa                   */
383aa6
/*  Released under GPLv2 and GPLv3; see COPYING.APIMAGIC. */
383aa6
/**********************************************************/
383aa6
383aa6
#include <stdio.h>
383aa6
#include <unistd.h>
383aa6
#include <apimagic/apimagic.h>
97d208
#include "apimagic_driver_impl.h"
383aa6
383aa6
#ifndef AMGC_DRIVER_FLAGS
383aa6
#define AMGC_DRIVER_FLAGS	AMGC_DRIVER_VERBOSITY_ERRORS \
383aa6
				| AMGC_DRIVER_VERBOSITY_USAGE
383aa6
#endif
383aa6
e0b135
static const char vermsg[] = "%s%s%s (git://midipix.org/apimagic): "
e0b135
			     "version %s%d.%d.%d%s.\n"
e0b135
			     "[commit reference: %s%s%s]\n";
e0b135
e0b135
static const char * const amgc_ver_color[6] = {
59a248
		"\x1b[1m\x1b[35m","\x1b[0m",
59a248
		"\x1b[1m\x1b[32m","\x1b[0m",
59a248
		"\x1b[1m\x1b[34m","\x1b[0m"
e0b135
};
e0b135
e0b135
static const char * const amgc_ver_plain[6] = {
e0b135
		"","",
e0b135
		"","",
e0b135
		"",""
e0b135
};
383aa6
383aa6
static ssize_t apimagic_version(struct amgc_driver_ctx * dctx)
383aa6
{
2226ad
	const struct amgc_source_version * verinfo;
e0b135
	const char * const * verclr;
2226ad
2226ad
	verinfo = amgc_source_version();
e0b135
	verclr  = isatty(STDOUT_FILENO) ? amgc_ver_color : amgc_ver_plain;
2226ad
e0b135
	return fprintf(stdout,vermsg,
e0b135
			verclr[0],dctx->program,verclr[1],
e0b135
			verclr[2],verinfo->major,verinfo->minor,
e0b135
			verinfo->revision,verclr[3],
e0b135
			verclr[4],verinfo->commit,verclr[5]);
383aa6
}
383aa6
383aa6
static void apimagic_perform_unit_actions(struct amgc_unit_ctx * uctx)
383aa6
{
6adb90
	const struct amgc_action * action;
6adb90
6adb90
	for (action=uctx->cctx->actions; action->type; action++)
bbff09
		amgc_perform_unit_action(uctx,action,0,stdout);
383aa6
}
383aa6
383aa6
static int apimagic_exit(struct amgc_driver_ctx * dctx, int nerrors)
383aa6
{
383aa6
	amgc_free_driver_ctx(dctx);
383aa6
	return nerrors ? 2 : 0;
383aa6
}
383aa6
10a518
int apimagic_main(int argc, char ** argv, char ** envp)
383aa6
{
383aa6
	int				ret;
383aa6
	struct amgc_driver_ctx *	dctx;
383aa6
	struct amgc_unit_ctx *		uctx;
383aa6
	const char **			unit;
383aa6
383aa6
	if ((ret = amgc_get_driver_ctx(argv,envp,AMGC_DRIVER_FLAGS,&dctx)))
383aa6
		return (ret == AMGC_USAGE) ? !--argc : 2;
383aa6
383aa6
	if (dctx->cctx->drvflags & AMGC_DRIVER_VERSION)
383aa6
		if ((apimagic_version(dctx)) < 0)
383aa6
			return apimagic_exit(dctx,2);
383aa6
383aa6
	for (unit=dctx->units; *unit; unit++) {
383aa6
		if (!(amgc_get_unit_ctx(dctx,*unit,&uctx))) {
383aa6
			apimagic_perform_unit_actions(uctx);
383aa6
			ret += uctx->nerrors;
383aa6
			amgc_free_unit_ctx(uctx);
fdf8e8
		} else
fdf8e8
			ret++;
383aa6
	}
383aa6
383aa6
	return apimagic_exit(dctx,ret);
383aa6
}
383aa6
383aa6
#ifndef APIMAGIC_IN_A_BOX
383aa6
10a518
int main(int argc, char ** argv, char ** envp)
383aa6
{
383aa6
	return apimagic_main(argc,argv,envp);
383aa6
}
383aa6
383aa6
#endif