Blame src/driver/pe_amain.c

3b884b
/***************************************************************/
3b884b
/*  perk: PE Resource Kit                                      */
3b884b
/*  Copyright (C) 2015--2016  Z. Gilboa                        */
3b884b
/*  Released under GPLv2 and GPLv3; see COPYING.PERK.          */
3b884b
/***************************************************************/
3b884b
3b884b
#include <stdio.h>
3b884b
#include <unistd.h>
3b884b
#include <perk/perk.h>
3b884b
#include <perk/perk_output.h>
3b884b
#include "perk_driver_impl.h"
3b884b
3b884b
#ifndef PERK_DRIVER_FLAGS
3b884b
#define PERK_DRIVER_FLAGS	PERK_DRIVER_VERBOSITY_ERRORS \
3b884b
				| PERK_DRIVER_VERBOSITY_USAGE
3b884b
#endif
3b884b
3b884b
static const char vermsg[] = "%s%s%s (git://midipix.org/perk): "
3b884b
			     "version %s%d.%d.%d%s.\n"
3b884b
			     "[commit reference: %s%s%s]\n";
3b884b
3b884b
static const char * const pe_ver_color[6] = {
3b884b
		"\x1b[1m\x1b[35m","\x1b[0m",
3b884b
		"\x1b[1m\x1b[32m","\x1b[0m",
3b884b
		"\x1b[1m\x1b[34m","\x1b[0m"
3b884b
};
3b884b
3b884b
static const char * const pe_ver_plain[6] = {
3b884b
		"","",
3b884b
		"","",
3b884b
		"",""
3b884b
};
3b884b
3b884b
static ssize_t pe_version(struct pe_driver_ctx * dctx)
3b884b
{
3b884b
	const struct pe_source_version * verinfo;
3b884b
	const char * const * verclr;
3b884b
3b884b
	verinfo = pe_source_version();
3b884b
	verclr  = isatty(STDOUT_FILENO) ? pe_ver_color : pe_ver_plain;
3b884b
3b884b
	return fprintf(stdout,vermsg,
3b884b
			verclr[0],dctx->program,verclr[1],
3b884b
			verclr[2],verinfo->major,verinfo->minor,
3b884b
			verinfo->revision,verclr[3],
3b884b
			verclr[4],verinfo->commit,verclr[5]);
3b884b
}
3b884b
251206
static void pe_perform_unit_actions(
251206
	const struct pe_driver_ctx *	dctx,
251206
	struct pe_unit_ctx *		uctx)
3b884b
{
eed801
	uint64_t flags = dctx->cctx->fmtflags;
3b884b
cd4ace
	if (flags & PERK_OUTPUT_IMAGE_CATEGORY)
204e71
		pe_output_image_category(dctx,uctx->meta,0);
007c7f
437706
	if (flags & PERK_OUTPUT_IMAGE_SECTIONS)
437706
		pe_output_image_sections(dctx,uctx->meta,0);
437706
e34e5c
	if (flags & PERK_OUTPUT_EXPORT_SYMS)
a25e4b
		pe_output_export_symbols(dctx,uctx->meta,0);
3b884b
e34e5c
	if ((flags & PERK_OUTPUT_IMPORT_LIBS) || (flags & PERK_OUTPUT_IMPORT_SYMS))
a25e4b
		pe_output_import_libraries(dctx,uctx->meta,0);
3b884b
}
3b884b
a25e4b
static int pe_exit(struct pe_driver_ctx * dctx, int ret)
3b884b
{
a25e4b
	pe_output_error_vector(dctx);
3b884b
	pe_free_driver_ctx(dctx);
a25e4b
	return ret;
3b884b
}
3b884b
3b884b
int pe_main(int argc, char ** argv, char ** envp)
3b884b
{
3b884b
	int			ret;
3b884b
	struct pe_driver_ctx *	dctx;
3b884b
	struct pe_unit_ctx *	uctx;
3b884b
	const char **		unit;
3b884b
3b884b
	if ((ret = pe_get_driver_ctx(argv,envp,PERK_DRIVER_FLAGS,&dctx)))
b0d024
		return (ret == PERK_USAGE)
b0d024
			? !--argc
b0d024
			: PERK_ERROR;
3b884b
3b884b
	if (dctx->cctx->drvflags & PERK_DRIVER_VERSION)
3b884b
		if ((pe_version(dctx)) < 0)
b0d024
			return pe_exit(dctx,PERK_ERROR);
3b884b
902a22
	for (unit=dctx->units; *unit; unit++) {
3b884b
		if (!(pe_get_unit_ctx(dctx,*unit,&uctx))) {
251206
			pe_perform_unit_actions(dctx,uctx);
3b884b
			pe_free_unit_ctx(uctx);
3b884b
		}
3b884b
	}
3b884b
b0d024
	return pe_exit(dctx,dctx->errv[0] ? PERK_ERROR : PERK_OK);
3b884b
}