Blame src/mdso.c

cde03b
/****************************************************************/
cde03b
/*  mdso: midipix dso scavenger                                 */
3e816c
/*  Copyright (C) 2015--2016  Z. Gilboa                         */
cde03b
/*  Released under GPLv2 and GPLv3; see COPYING.MDSO.           */
cde03b
/****************************************************************/
cde03b
cde03b
#include <stdio.h>
cde03b
#include <unistd.h>
cde03b
#include <mdso/mdso.h>
8cfad6
#include <mdso/mdso_output.h>
cde03b
#include "mdso_version.h"
cde03b
cde03b
#ifndef MDSO_DRIVER_FLAGS
cde03b
#define MDSO_DRIVER_FLAGS	MDSO_DRIVER_VERBOSITY_ERRORS \
cde03b
				| MDSO_DRIVER_VERBOSITY_USAGE
cde03b
#endif
cde03b
cde03b
static const char vermsg[] = "%s (git://midipix.org/mdso): commit %s.\n";
cde03b
cde03b
static ssize_t mdso_version(struct mdso_driver_ctx * dctx)
cde03b
{
cde03b
	return fprintf(stdout,vermsg,dctx->program,MDSO_GIT_VERSION);
cde03b
}
cde03b
cde03b
static void mdso_perform_unit_actions(struct mdso_unit_ctx * uctx)
cde03b
{
8cfad6
        uint64_t flags = uctx->cctx->fmtflags;
8cfad6
8cfad6
        if (flags & MDSO_OUTPUT_EXPORT_SYMS) {
8cfad6
                uctx->status = mdso_output_export_symbols(uctx,uctx->cctx,stdout);
8cfad6
                uctx->nerrors += !!uctx->status;
8cfad6
        }
cde03b
}
cde03b
cde03b
static int mdso_exit(struct mdso_driver_ctx * dctx, int nerrors)
cde03b
{
cde03b
	mdso_free_driver_ctx(dctx);
cde03b
	return nerrors ? 2 : 0;
cde03b
}
cde03b
cde03b
int mdso_main(int argc, const char ** argv, const char ** envp)
cde03b
{
cde03b
	int				ret;
cde03b
	struct mdso_driver_ctx *	dctx;
cde03b
	struct mdso_unit_ctx *		uctx;
cde03b
	const char **			unit;
cde03b
cde03b
	if ((ret = mdso_get_driver_ctx(argv,envp,MDSO_DRIVER_FLAGS,&dctx)))
cde03b
		return (ret == MDSO_USAGE) ? !--argc : 2;
cde03b
cde03b
	if (dctx->cctx->drvflags & MDSO_DRIVER_VERSION)
cde03b
		if ((mdso_version(dctx)) < 0)
cde03b
			return mdso_exit(dctx,2);
cde03b
cde03b
	for (unit=dctx->units; *unit; unit++) {
cde03b
		if (!(mdso_get_unit_ctx(dctx,*unit,&uctx))) {
cde03b
			mdso_perform_unit_actions(uctx);
cde03b
			ret += uctx->nerrors;
cde03b
			mdso_free_unit_ctx(uctx);
cde03b
		}
cde03b
	}
cde03b
cfa298
	if (*dctx->units) {
cfa298
		dctx->status  =  mdso_create_implib_sources(dctx);
cfa298
		dctx->nerrors += !!dctx->status;
cfa298
		ret += dctx->nerrors;
cfa298
	}
81f462
cde03b
	return mdso_exit(dctx,ret);
cde03b
}
cde03b
cde03b
#ifndef MDSO_IN_A_BOX
cde03b
cde03b
int main(int argc, const char ** argv, const char ** envp)
cde03b
{
cde03b
	return mdso_main(argc,argv,envp);
cde03b
}
cde03b
cde03b
#endif