Blame src/perk.c

e2e2c2
/***************************************************************/
e2e2c2
/*  perk: PE Resource Kit                                      */
e2e2c2
/*  Copyright (C) 2015--2016  Z. Gilboa                        */
e2e2c2
/*  Released under GPLv2 and GPLv3; see COPYING.PERK.          */
e2e2c2
/***************************************************************/
e2e2c2
846f96
#include <stdio.h>
9c5807
#include <unistd.h>
bbf9e9
#include <perk/perk.h>
949bb3
#include <perk/perk_output.h>
b18054
#include "perk_driver_impl.h"
bbf9e9
846f96
#ifndef PERK_DRIVER_FLAGS
390d59
#define PERK_DRIVER_FLAGS	PERK_DRIVER_VERBOSITY_ERRORS \
390d59
				| PERK_DRIVER_VERBOSITY_USAGE
846f96
#endif
846f96
8435a6
static const char vermsg[] = "%s%s%s (git://midipix.org/perk): "
8435a6
			     "version %s%d.%d.%d%s.\n"
8435a6
			     "[commit reference: %s%s%s]\n";
8435a6
8435a6
static const char * const pe_ver_color[6] = {
452738
		"\x1b[1m\x1b[35m","\x1b[0m",
452738
		"\x1b[1m\x1b[32m","\x1b[0m",
452738
		"\x1b[1m\x1b[34m","\x1b[0m"
8435a6
};
8435a6
8435a6
static const char * const pe_ver_plain[6] = {
8435a6
		"","",
8435a6
		"","",
8435a6
		"",""
8435a6
};
e85100
6025fb
static ssize_t perk_version(struct pe_driver_ctx * dctx)
e85100
{
017e12
	const struct pe_source_version * verinfo;
8435a6
	const char * const * verclr;
e85100
017e12
	verinfo = pe_source_version();
8435a6
	verclr  = isatty(STDOUT_FILENO) ? pe_ver_color : pe_ver_plain;
017e12
8435a6
	return fprintf(stdout,vermsg,
8435a6
			verclr[0],dctx->program,verclr[1],
8435a6
			verclr[2],verinfo->major,verinfo->minor,
8435a6
			verinfo->revision,verclr[3],
8435a6
			verclr[4],verinfo->commit,verclr[5]);
e85100
}
e85100
6025fb
static ssize_t perk_paragraph_break(struct pe_unit_ctx * uctx, int * fpara)
9c5807
{
9c5807
	if (*fpara) {
6025fb
		*fpara = 0;
6025fb
76aabf
		if (uctx->cctx->ioctx->fdout >= 0)
76aabf
			return write(uctx->cctx->ioctx->fdout,"\n",1);
9c5807
		else
6025fb
			return fputc('\n',stdout);
6025fb
	} else
6025fb
		return 0;
9c5807
}
9c5807
846f96
static void perk_perform_unit_actions(struct pe_unit_ctx * uctx)
846f96
{
9c5807
	int      fpara = 0;
d7ed3e
	uint64_t flags = uctx->cctx->fmtflags;
846f96
9c5807
	if (flags & PERK_OUTPUT_EXPORT_SYMS) {
d7ed3e
		uctx->status = pe_output_export_symbols(uctx->meta,uctx->cctx,0);
d7ed3e
		uctx->nerrors += !!uctx->status;
b5c6e6
		fpara += uctx->meta->summary.nexpsyms;
9c5807
	}
9c5807
9c5807
	if ((flags & PERK_OUTPUT_IMPORT_LIBS) || (flags & PERK_OUTPUT_IMPORT_SYMS)) {
9c5807
		perk_paragraph_break(uctx,&fpara;;
d7ed3e
		uctx->status = pe_output_import_libraries(uctx->meta,uctx->cctx,0);
d7ed3e
		uctx->nerrors += !!uctx->status;
b5c6e6
		fpara += (uctx->meta->summary.nimplibs > 0);
9c5807
	}
846f96
}
846f96
d7ed3e
static int perk_exit(struct pe_driver_ctx * dctx, int nerrors)
bbf9e9
{
846f96
	pe_free_driver_ctx(dctx);
d7ed3e
	return nerrors ? 2 : 0;
846f96
}
846f96
7d4640
int perk_main(int argc, char ** argv, char ** envp)
846f96
{
846f96
	int			ret;
846f96
	struct pe_driver_ctx *	dctx;
846f96
	struct pe_unit_ctx *	uctx;
846f96
	const char **		unit;
846f96
846f96
	if ((ret = pe_get_driver_ctx(argv,envp,PERK_DRIVER_FLAGS,&dctx)))
dd97ea
		return (ret == PERK_USAGE) ? !--argc : 2;
846f96
c61328
	if (dctx->cctx->drvflags & PERK_DRIVER_VERSION)
6025fb
		if ((perk_version(dctx)) < 0)
6025fb
			return perk_exit(dctx,2);
e85100
846f96
	for (unit=dctx->units; *unit; unit++) {
846f96
		if (!(pe_get_unit_ctx(dctx,*unit,&uctx))) {
846f96
			perk_perform_unit_actions(uctx);
d7ed3e
			ret += uctx->nerrors;
846f96
			pe_free_unit_ctx(uctx);
846f96
		}
846f96
	}
846f96
846f96
	return perk_exit(dctx,ret);
bbf9e9
}
bbf9e9
467be1
#ifndef PERK_IN_A_BOX
467be1
7d4640
int main(int argc, char ** argv, char ** envp)
bbf9e9
{
bbf9e9
	return perk_main(argc,argv,envp);
bbf9e9
}
467be1
467be1
#endif