Blame src/driver/pe_amain.c

3b884b
/***************************************************************/
3b884b
/*  perk: PE Resource Kit                                      */
ced38a
/*  Copyright (C) 2015--2017  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"
a85d3e
#include "perk_dprintf_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
2b94b4
static ssize_t pe_version(struct pe_driver_ctx * dctx, int fdout)
3b884b
{
3b884b
	const struct pe_source_version * verinfo;
3b884b
	const char * const * verclr;
3b884b
3b884b
	verinfo = pe_source_version();
a85d3e
	verclr  = isatty(fdout) ? pe_ver_color : pe_ver_plain;
3b884b
a85d3e
	return pe_dprintf(
a85d3e
			fdout,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
a9d23f
static void pe_perform_hdrdump_actions(
a9d23f
	const struct pe_driver_ctx *	dctx,
a9d23f
	struct pe_unit_ctx *		uctx)
a9d23f
{
a9d23f
	if (dctx->cctx->hdrdump & PERK_HDRDUMP_IMAGE_DOS_HEADER)
a9d23f
		pe_hdrdump_image_dos_hdr(dctx,uctx->meta);
a9d23f
a9d23f
	if (dctx->cctx->hdrdump & PERK_HDRDUMP_COFF_IMAGE_HEADER)
a9d23f
		pe_hdrdump_coff_image_hdr(dctx,uctx->meta);
a9d23f
a9d23f
	if (dctx->cctx->hdrdump & PERK_HDRDUMP_COFF_OBJECT_HEADER)
a9d23f
		pe_hdrdump_coff_object_hdr(dctx,uctx->meta);
a9d23f
a9d23f
	if (dctx->cctx->hdrdump & PERK_HDRDUMP_COFF_OPT_HEADER)
7dfe4b
		pe_hdrdump_coff_opt_hdr(dctx,uctx->meta);
a9d23f
a9d23f
	if (dctx->cctx->hdrdump & PERK_HDRDUMP_SECTION_TABLE)
a9d23f
		pe_hdrdump_sec_tbl(dctx,uctx->meta);
a9d23f
a9d23f
	if (dctx->cctx->hdrdump & PERK_HDRDUMP_EXPORT_HEADER)
a9d23f
		pe_hdrdump_export_hdr(dctx,uctx->meta);
a9d23f
a9d23f
	if (dctx->cctx->hdrdump & PERK_HDRDUMP_IMPORT_TABLE)
a9d23f
		pe_hdrdump_import_tbl(dctx,uctx->meta);
a9d23f
}
a9d23f
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)
67bba7
		pe_output_image_category(dctx,uctx->meta);
007c7f
437706
	if (flags & PERK_OUTPUT_IMAGE_SECTIONS)
67bba7
		pe_output_image_sections(dctx,uctx->meta);
437706
f779e3
	if (flags & PERK_OUTPUT_IMAGE_SYMBOLS)
67bba7
		pe_output_image_symbols(dctx,uctx->meta);
f779e3
0d4d47
	if (flags & PERK_OUTPUT_IMAGE_STRINGS)
67bba7
		pe_output_image_strings(dctx,uctx->meta);
0d4d47
e34e5c
	if (flags & PERK_OUTPUT_EXPORT_SYMS)
67bba7
		pe_output_export_symbols(dctx,uctx->meta);
3b884b
e34e5c
	if ((flags & PERK_OUTPUT_IMPORT_LIBS) || (flags & PERK_OUTPUT_IMPORT_SYMS))
67bba7
		pe_output_import_libraries(dctx,uctx->meta);
c63b6d
c63b6d
	if ((flags & PERK_OUTPUT_MDSO_LIBS) || (flags & PERK_OUTPUT_MDSO_SYMS))
67bba7
		pe_output_mdso_libraries(dctx,uctx->meta);
a9d23f
a9d23f
	if (dctx->cctx->hdrdump)
a9d23f
		pe_perform_hdrdump_actions(dctx,uctx);
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
e1a772
int pe_main(char ** argv, char ** envp, const struct pe_fd_ctx * fdctx)
3b884b
{
3b884b
	int			ret;
a85d3e
	int			fdout;
a85d3e
	uint64_t		flags;
3b884b
	struct pe_driver_ctx *	dctx;
3b884b
	struct pe_unit_ctx *	uctx;
3b884b
	const char **		unit;
3b884b
a85d3e
	flags = PERK_DRIVER_FLAGS;
a85d3e
	fdout = fdctx ? fdctx->fdout : STDOUT_FILENO;
a85d3e
a85d3e
	if ((ret = pe_get_driver_ctx(argv,envp,flags,fdctx,&dctx)))
b0d024
		return (ret == PERK_USAGE)
e1a772
			? !argv || !argv[0] || !argv[1]
b0d024
			: PERK_ERROR;
3b884b
3b884b
	if (dctx->cctx->drvflags & PERK_DRIVER_VERSION)
2b94b4
		if ((pe_version(dctx,fdout)) < 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
}