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
2226ad
static const char vermsg[] = "%s (git://midipix.org/apimagic): "
2226ad
			     "version %d.%d.%d.\n"
2226ad
			     "[commit reference: %s]\n";
383aa6
383aa6
static ssize_t apimagic_version(struct amgc_driver_ctx * dctx)
383aa6
{
2226ad
	const struct amgc_source_version * verinfo;
2226ad
2226ad
	verinfo = amgc_source_version();
2226ad
2226ad
	return fprintf(stdout,vermsg,dctx->program,
2226ad
			verinfo->major,verinfo->minor,verinfo->revision,
2226ad
			verinfo->commit);
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