Blame src/perk.c

846f96
#include <stdio.h>
9c5807
#include <unistd.h>
bbf9e9
#include <perk/perk.h>
949bb3
#include <perk/perk_output.h>
e85100
#include "perk_version.h"
bbf9e9
846f96
#ifndef PERK_DRIVER_FLAGS
846f96
#define PERK_DRIVER_FLAGS	PERK_DRIVER_VERBOSITY_ERRORS|PERK_DRIVER_VERBOSITY_USAGE
846f96
#endif
846f96
e85100
static const char vermsg[] = "%s (git://midipix.org/perk): commit %s.\n";
e85100
e85100
static void perk_version(struct pe_driver_ctx * dctx)
e85100
{
e85100
	char	buf[512];
e85100
	size_t	len;
e85100
e85100
	if (dctx->cctx.fdout >= 0) {
e85100
		len = sprintf(buf,vermsg,dctx->program,PERK_GIT_VERSION);
e85100
		write(dctx->cctx.fdout,buf,len);
e85100
	} else
e85100
		fprintf(stdout,vermsg,dctx->program,PERK_GIT_VERSION);
e85100
}
e85100
9c5807
static void perk_paragraph_break(struct pe_unit_ctx * uctx, int * fpara)
9c5807
{
9c5807
	if (*fpara) {
9c5807
		if (uctx->cctx.fdout >= 0)
9c5807
			write(uctx->cctx.fdout,"\n",1);
9c5807
		else
9c5807
			fputc('\n',stdout);
9c5807
		*fpara = 0;
9c5807
	}
9c5807
}
9c5807
846f96
static void perk_perform_unit_actions(struct pe_unit_ctx * uctx)
846f96
{
9c5807
	int      fpara = 0;
846f96
	uint64_t flags = uctx->cctx.fmtflags;
846f96
9c5807
	if (flags & PERK_OUTPUT_EXPORT_SYMS) {
846f96
		uctx->cctx.status = pe_output_export_symbols(uctx->meta,&uctx->cctx,0);
9c5807
		fpara += uctx->meta->summary.num_of_export_syms;
9c5807
	}
9c5807
9c5807
	if ((flags & PERK_OUTPUT_IMPORT_LIBS) || (flags & PERK_OUTPUT_IMPORT_SYMS)) {
9c5807
		perk_paragraph_break(uctx,&fpara;;
9c5807
		uctx->cctx.status = pe_output_import_libraries(uctx->meta,&uctx->cctx,0);
9c5807
		fpara += (uctx->meta->summary.num_of_implibs > 0);
9c5807
	}
846f96
}
846f96
846f96
static int perk_exit(struct pe_driver_ctx * dctx, int status)
bbf9e9
{
846f96
	pe_free_driver_ctx(dctx);
846f96
	return (status < 0) ? 2 : status;
846f96
}
846f96
846f96
static int perk_main(int argc, const char ** argv, const 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
e85100
	if (dctx->cctx.drvflags & PERK_DRIVER_VERSION)
e85100
		perk_version(dctx);
e85100
846f96
	for (unit=dctx->units; *unit; unit++) {
846f96
		if (!(pe_get_unit_ctx(dctx,*unit,&uctx))) {
846f96
			perk_perform_unit_actions(uctx);
846f96
			ret = uctx->cctx.status ? uctx->cctx.status : ret;
846f96
			pe_free_unit_ctx(uctx);
846f96
		}
846f96
	}
846f96
846f96
	return perk_exit(dctx,ret);
bbf9e9
}
bbf9e9
846f96
int main(int argc, const char ** argv, const char ** envp)
bbf9e9
{
bbf9e9
	return perk_main(argc,argv,envp);
bbf9e9
}