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>
5cffda
#include "mdso_driver_impl.h"
cde03b
cde03b
#ifndef MDSO_DRIVER_FLAGS
cde03b
#define MDSO_DRIVER_FLAGS	MDSO_DRIVER_VERBOSITY_ERRORS \
cde03b
				| MDSO_DRIVER_VERBOSITY_USAGE
cde03b
#endif
cde03b
0bb4f5
static const char vermsg[] = "%s%s%s (git://midipix.org/mdso): "
0bb4f5
			     "version %s%d.%d.%d%s.\n"
0bb4f5
			     "[commit reference: %s%s%s]\n";
0bb4f5
0bb4f5
static const char * const mdso_ver_color[6] = {
525f80
		"\x1b[1m\x1b[35m","\x1b[0m",
525f80
		"\x1b[1m\x1b[32m","\x1b[0m",
525f80
		"\x1b[1m\x1b[34m","\x1b[0m"
0bb4f5
};
0bb4f5
0bb4f5
static const char * const mdso_ver_plain[6] = {
0bb4f5
		"","",
0bb4f5
		"","",
0bb4f5
		"",""
0bb4f5
};
cde03b
cde03b
static ssize_t mdso_version(struct mdso_driver_ctx * dctx)
cde03b
{
9540d8
	const struct mdso_source_version * verinfo;
0bb4f5
	const char * const * verclr;
9540d8
9540d8
	verinfo = mdso_source_version();
0bb4f5
	verclr  = isatty(STDOUT_FILENO) ? mdso_ver_color : mdso_ver_plain;
9540d8
0bb4f5
	return fprintf(stdout,vermsg,
0bb4f5
			verclr[0],dctx->program,verclr[1],
0bb4f5
			verclr[2],verinfo->major,verinfo->minor,
0bb4f5
			verinfo->revision,verclr[3],
0bb4f5
			verclr[4],verinfo->commit,verclr[5]);
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
13aa9b
int mdso_main(int argc, char ** argv, 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
13aa9b
int main(int argc, char ** argv, char ** envp)
cde03b
{
cde03b
	return mdso_main(argc,argv,envp);
cde03b
}
cde03b
cde03b
#endif