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
eed801
static ssize_t pe_paragraph_break(const struct pe_driver_ctx * dctx, int * fpara)
3b884b
{
3b884b
	if (*fpara) {
3b884b
		*fpara = 0;
3b884b
eed801
		if (dctx->cctx->ioctx->fdout >= 0)
eed801
			return write(dctx->cctx->ioctx->fdout,"\n",1);
3b884b
		else
3b884b
			return fputc('\n',stdout);
3b884b
	} else
3b884b
		return 0;
3b884b
}
3b884b
251206
static void pe_perform_unit_actions(
251206
	const struct pe_driver_ctx *	dctx,
251206
	struct pe_unit_ctx *		uctx)
3b884b
{
3b884b
	int      fpara = 0;
eed801
	uint64_t flags = dctx->cctx->fmtflags;
3b884b
3b884b
	if (flags & PERK_OUTPUT_EXPORT_SYMS) {
a25e4b
		pe_output_export_symbols(dctx,uctx->meta,0);
3b884b
		fpara += uctx->meta->summary.nexpsyms;
3b884b
	}
3b884b
3b884b
	if ((flags & PERK_OUTPUT_IMPORT_LIBS) || (flags & PERK_OUTPUT_IMPORT_SYMS)) {
eed801
		pe_paragraph_break(dctx,&fpara;;
a25e4b
		pe_output_import_libraries(dctx,uctx->meta,0);
3b884b
		fpara += (uctx->meta->summary.nimplibs > 0);
3b884b
	}
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
e2d4c0
	for (unit=dctx->units; *unit && !dctx->errv[0]; 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
}