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
6025fb
static ssize_t perk_version(struct pe_driver_ctx * dctx)
e85100
{
e85100
	char	buf[512];
e85100
	size_t	len;
e85100
76aabf
	if (dctx->cctx->ioctx->fdout >= 0) {
e85100
		len = sprintf(buf,vermsg,dctx->program,PERK_GIT_VERSION);
76aabf
		return write(dctx->cctx->ioctx->fdout,buf,len);
e85100
	} else
6025fb
		return fprintf(stdout,vermsg,dctx->program,PERK_GIT_VERSION);
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
467be1
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
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
846f96
int main(int argc, const char ** argv, const char ** envp)
bbf9e9
{
bbf9e9
	return perk_main(argc,argv,envp);
bbf9e9
}
467be1
467be1
#endif