Blame src/output/pe_output_error.c

68f5d9
/***************************************************************/
68f5d9
/*  perk: PE Resource Kit                                      */
68f5d9
/*  Copyright (C) 2015--2016  Z. Gilboa                        */
68f5d9
/*  Released under GPLv2 and GPLv3; see COPYING.PERK.          */
68f5d9
/***************************************************************/
68f5d9
68f5d9
#include <stdio.h>
68f5d9
#include <string.h>
68f5d9
#include <errno.h>
68f5d9
#include <unistd.h>
68f5d9
#include <perk/perk.h>
68f5d9
68f5d9
static const char aclr_reset[]   = "\x1b[0m";
68f5d9
static const char aclr_bold[]    = "\x1b[1m";
68f5d9
68f5d9
static const char aclr_green[]   = "\x1b[32m";
68f5d9
static const char aclr_blue[]    = "\x1b[34m";
68f5d9
static const char aclr_magenta[] = "\x1b[35m";
68f5d9
68f5d9
static const char * pe_output_error_header(const struct pe_error_info * erri)
68f5d9
{
68f5d9
	if (erri->flags & PERK_ERROR_CHILD)
68f5d9
		return "exec error upon";
68f5d9
68f5d9
	else if (erri->flags & PERK_ERROR_TOP_LEVEL)
68f5d9
		return "error logged in";
68f5d9
68f5d9
	else if (erri->flags & PERK_ERROR_NESTED)
68f5d9
		return "< returned to >";
68f5d9
68f5d9
	else
68f5d9
		return "distorted state";
68f5d9
}
68f5d9
68f5d9
static const char * pe_output_strerror(const struct pe_error_info * erri)
68f5d9
{
68f5d9
	if (erri->flags & PERK_ERROR_CUSTOM)
68f5d9
		return "flow error: unexpected condition or other";
68f5d9
68f5d9
	else if (erri->flags & PERK_ERROR_NESTED)
68f5d9
		return "";
68f5d9
68f5d9
	else if (erri->flags & PERK_ERROR_CHILD)
68f5d9
		return "(see child process error messages)";
68f5d9
68f5d9
	else if (erri->syserror == ENOBUFS)
68f5d9
		return "input error: string length exceeds buffer size.";
68f5d9
68f5d9
	else
68f5d9
		return strerror(erri->syserror);
68f5d9
}
68f5d9
68f5d9
static int pe_output_error_record_plain(
68f5d9
	const struct pe_driver_ctx *	dctx,
68f5d9
	const struct pe_error_info *	erri)
68f5d9
{
68f5d9
	const char * errdesc = pe_output_strerror(erri);
68f5d9
68f5d9
	if (fprintf(stderr,"%s: %s %s(), line %d%s%s.\n",
68f5d9
			dctx->program,
68f5d9
			pe_output_error_header(erri),
68f5d9
			erri->function,
68f5d9
			erri->line,
68f5d9
			strlen(errdesc) ? ": " : "",
68f5d9
			errdesc) < 0)
68f5d9
		return -1;
68f5d9
68f5d9
	return fflush(stderr);
68f5d9
}
68f5d9
68f5d9
static int pe_output_error_record_annotated(
68f5d9
	const struct pe_driver_ctx *	dctx,
68f5d9
	const struct pe_error_info *	erri)
68f5d9
{
68f5d9
	const char * errdesc = pe_output_strerror(erri);
68f5d9
68f5d9
	if (fprintf(
68f5d9
			stderr,
68f5d9
			"%s%s%s:%s %s%s%s %s%s%s()%s, %s%sline %d%s%s%s%s%s.\n",
68f5d9
68f5d9
			aclr_bold,aclr_magenta,
68f5d9
			dctx->program,
68f5d9
			aclr_reset,
68f5d9
68f5d9
			aclr_bold,
68f5d9
			pe_output_error_header(erri),
68f5d9
			aclr_reset,
68f5d9
68f5d9
			aclr_bold,aclr_blue,
68f5d9
			erri->function,
68f5d9
			aclr_reset,
68f5d9
68f5d9
			aclr_bold,aclr_green,
68f5d9
			erri->line,
68f5d9
			aclr_reset,
68f5d9
			strlen(errdesc) ? ": " : "",
68f5d9
68f5d9
			aclr_bold,
68f5d9
			pe_output_strerror(erri),
68f5d9
			aclr_reset) < 0)
68f5d9
		return -1;
68f5d9
68f5d9
	return fflush(stderr);
68f5d9
}
68f5d9
68f5d9
int pe_output_error_record(
68f5d9
	const struct pe_driver_ctx *	dctx,
68f5d9
	const struct pe_error_info *	erri)
68f5d9
{
68f5d9
	if (dctx->cctx->drvflags & PERK_DRIVER_ANNOTATE_NEVER)
68f5d9
		return pe_output_error_record_plain(dctx,erri);
68f5d9
68f5d9
	else if (dctx->cctx->drvflags & PERK_DRIVER_ANNOTATE_ALWAYS)
68f5d9
		return pe_output_error_record_annotated(dctx,erri);
68f5d9
68f5d9
	else if (isatty(STDERR_FILENO))
68f5d9
		return pe_output_error_record_annotated(dctx,erri);
68f5d9
68f5d9
	else
68f5d9
		return pe_output_error_record_plain(dctx,erri);
68f5d9
}
68f5d9
68f5d9
static int pe_output_error_vector_plain(const struct pe_driver_ctx * dctx)
68f5d9
{
68f5d9
	struct pe_error_info ** perr;
68f5d9
68f5d9
	for (perr=dctx->errv; *perr; perr++)
68f5d9
		if (pe_output_error_record_plain(dctx,*perr))
68f5d9
			return -1;
68f5d9
68f5d9
	return 0;
68f5d9
}
68f5d9
68f5d9
static int pe_output_error_vector_annotated(const struct pe_driver_ctx * dctx)
68f5d9
{
68f5d9
	struct pe_error_info ** perr;
68f5d9
68f5d9
	for (perr=dctx->errv; *perr; perr++)
68f5d9
		if (pe_output_error_record_annotated(dctx,*perr))
68f5d9
			return -1;
68f5d9
68f5d9
	return 0;
68f5d9
}
68f5d9
68f5d9
int pe_output_error_vector(const struct pe_driver_ctx * dctx)
68f5d9
{
68f5d9
	if (dctx->cctx->drvflags & PERK_DRIVER_ANNOTATE_NEVER)
68f5d9
		return pe_output_error_vector_plain(dctx);
68f5d9
68f5d9
	else if (dctx->cctx->drvflags & PERK_DRIVER_ANNOTATE_ALWAYS)
68f5d9
		return pe_output_error_vector_annotated(dctx);
68f5d9
68f5d9
	else if (isatty(STDERR_FILENO))
68f5d9
		return pe_output_error_vector_annotated(dctx);
68f5d9
68f5d9
	else
68f5d9
		return pe_output_error_vector_plain(dctx);
68f5d9
}