Blame src/output/sfrt_output_error.c

f0da18
#include <stdio.h>
f0da18
#include <string.h>
f0da18
#include <errno.h>
f0da18
#include <unistd.h>
f0da18
#include <sofort/sofort.h>
f0da18
f0da18
static const char aclr_reset[]   = "\x1b[0m";
f0da18
static const char aclr_bold[]    = "\x1b[1m";
f0da18
f0da18
static const char aclr_red[]     = "\x1b[31m";
f0da18
static const char aclr_green[]   = "\x1b[32m";
f0da18
static const char aclr_blue[]    = "\x1b[34m";
f0da18
static const char aclr_magenta[] = "\x1b[35m";
f0da18
f0da18
static const char const * const sfrt_error_strings[SFRT_ERR_CAP] = {
f0da18
	[SFRT_ERR_FLOW_ERROR]       = "flow error: unexpected condition or other",
f0da18
	[SFRT_ERR_FLEE_ERROR]       = "flees and bugs and cats and mice",
f0da18
	[SFRT_ERR_NULL_CONTEXT]     = "null driver or unit context",
f0da18
	[SFRT_ERR_NULL_SOURCE]      = "source file does not define any data",
f0da18
	[SFRT_ERR_INVALID_CONTEXT]  = "invalid driver or unit context",
f0da18
	[SFRT_ERR_INVALID_SOURCE]   = "invalid source file",
f0da18
	[SFRT_ERR_SOURCE_SIZE_ZERO] = "cannot map an empty source file",
f0da18
};
f0da18
f0da18
static const char * sfrt_output_error_header(const struct sfrt_error_info * erri)
f0da18
{
f0da18
	if (erri->eflags & SFRT_ERROR_CHILD)
f0da18
		return "exec error upon";
f0da18
f0da18
	else if (erri->eflags & SFRT_ERROR_TOP_LEVEL)
f0da18
		return "error logged in";
f0da18
f0da18
	else if (erri->eflags & SFRT_ERROR_NESTED)
f0da18
		return "< returned to >";
f0da18
f0da18
	else
f0da18
		return "distorted state";
f0da18
}
f0da18
f0da18
static const char * sfrt_output_unit_header(const struct sfrt_error_info * erri)
f0da18
{
f0da18
	if (!(erri->eflags & SFRT_ERROR_CUSTOM))
f0da18
		return "while opening";
f0da18
f0da18
	else if (erri->elibcode == SFRT_ERR_SOURCE_SIZE_ZERO)
f0da18
		return "while mapping";
f0da18
f0da18
	else
f0da18
		return "while parsing";
f0da18
}
f0da18
f0da18
static const char * sfrt_output_strerror(const struct sfrt_error_info * erri)
f0da18
{
f0da18
	if (erri->eflags & SFRT_ERROR_CUSTOM)
f0da18
		return ((erri->elibcode < 0) || (erri->elibcode >= SFRT_ERR_CAP))
f0da18
			? "internal error: please report to the maintainer"
f0da18
			: sfrt_error_strings[erri->elibcode];
f0da18
f0da18
	else if (erri->eflags & SFRT_ERROR_NESTED)
f0da18
		return "";
f0da18
f0da18
	else if (erri->eflags & SFRT_ERROR_CHILD)
f0da18
		return "(see child process error messages)";
f0da18
f0da18
	else if (erri->esyscode == ENOBUFS)
f0da18
		return "input error: string length exceeds buffer size.";
f0da18
f0da18
	else
f0da18
		return strerror(erri->esyscode);
f0da18
}
f0da18
f0da18
static int sfrt_output_error_record_plain(
f0da18
	const struct sfrt_driver_ctx *	dctx,
f0da18
	const struct sfrt_error_info *	erri)
f0da18
{
f0da18
	const char * epath;
f0da18
	const char * errdesc = sfrt_output_strerror(erri);
f0da18
f0da18
	epath = erri->euctx
f0da18
		? *erri->euctx->path
f0da18
		: erri->eunit;
f0da18
f0da18
	if (epath && !(erri->eflags & SFRT_ERROR_NESTED))
f0da18
		if (fprintf(stderr,"%s: [%s] '%s':\n",
f0da18
				dctx->program,
f0da18
				sfrt_output_unit_header(erri),
f0da18
				epath) < 0)
f0da18
			return -1;
f0da18
f0da18
	if (fprintf(stderr,"%s: %s %s(), line %d%s%s.\n",
f0da18
			dctx->program,
f0da18
			sfrt_output_error_header(erri),
f0da18
			erri->efunction,
f0da18
			erri->eline,
f0da18
			strlen(errdesc) ? ": " : "",
f0da18
			errdesc) < 0)
f0da18
		return -1;
f0da18
f0da18
	return fflush(stderr);
f0da18
}
f0da18
f0da18
static int sfrt_output_error_record_annotated(
f0da18
	const struct sfrt_driver_ctx *	dctx,
f0da18
	const struct sfrt_error_info *	erri)
f0da18
{
f0da18
	const char * epath;
f0da18
	const char * errdesc = sfrt_output_strerror(erri);
f0da18
f0da18
	epath = erri->euctx
f0da18
		? *erri->euctx->path
f0da18
		: erri->eunit;
f0da18
f0da18
	if (epath && !(erri->eflags & SFRT_ERROR_NESTED))
f0da18
		if (fprintf(
f0da18
				stderr,
f0da18
				"%s%s%s:%s %s[%s]%s %s%s'%s'%s:\n",
f0da18
f0da18
				aclr_bold,aclr_magenta,
f0da18
				dctx->program,
f0da18
				aclr_reset,
f0da18
f0da18
				aclr_bold,
f0da18
				sfrt_output_unit_header(erri),
f0da18
				aclr_reset,
f0da18
f0da18
				aclr_bold,aclr_red,
f0da18
				epath,
f0da18
				aclr_reset) < 0)
f0da18
			return -1;
f0da18
f0da18
	if (fprintf(
f0da18
			stderr,
f0da18
			"%s%s%s:%s %s%s%s %s%s%s()%s, %s%sline %d%s%s%s%s%s.\n",
f0da18
f0da18
			aclr_bold,aclr_magenta,
f0da18
			dctx->program,
f0da18
			aclr_reset,
f0da18
f0da18
			aclr_bold,
f0da18
			sfrt_output_error_header(erri),
f0da18
			aclr_reset,
f0da18
f0da18
			aclr_bold,aclr_blue,
f0da18
			erri->efunction,
f0da18
			aclr_reset,
f0da18
f0da18
			aclr_bold,aclr_green,
f0da18
			erri->eline,
f0da18
			aclr_reset,
f0da18
			strlen(errdesc) ? ": " : "",
f0da18
f0da18
			aclr_bold,
f0da18
			sfrt_output_strerror(erri),
f0da18
			aclr_reset) < 0)
f0da18
		return -1;
f0da18
f0da18
	return fflush(stderr);
f0da18
}
f0da18
f0da18
int sfrt_output_error_record(
f0da18
	const struct sfrt_driver_ctx *	dctx,
f0da18
	const struct sfrt_error_info *	erri)
f0da18
{
f0da18
	if (dctx->cctx->drvflags & SFRT_DRIVER_ANNOTATE_NEVER)
f0da18
		return sfrt_output_error_record_plain(dctx,erri);
f0da18
f0da18
	else if (dctx->cctx->drvflags & SFRT_DRIVER_ANNOTATE_ALWAYS)
f0da18
		return sfrt_output_error_record_annotated(dctx,erri);
f0da18
f0da18
	else if (isatty(STDERR_FILENO))
f0da18
		return sfrt_output_error_record_annotated(dctx,erri);
f0da18
f0da18
	else
f0da18
		return sfrt_output_error_record_plain(dctx,erri);
f0da18
}
f0da18
f0da18
static int sfrt_output_error_vector_plain(const struct sfrt_driver_ctx * dctx)
f0da18
{
f0da18
	struct sfrt_error_info ** perr;
f0da18
f0da18
	for (perr=dctx->errv; *perr; perr++)
f0da18
		if (sfrt_output_error_record_plain(dctx,*perr))
f0da18
			return -1;
f0da18
f0da18
	return 0;
f0da18
}
f0da18
f0da18
static int sfrt_output_error_vector_annotated(const struct sfrt_driver_ctx * dctx)
f0da18
{
f0da18
	struct sfrt_error_info ** perr;
f0da18
f0da18
	for (perr=dctx->errv; *perr; perr++)
f0da18
		if (sfrt_output_error_record_annotated(dctx,*perr))
f0da18
			return -1;
f0da18
f0da18
	return 0;
f0da18
}
f0da18
f0da18
int sfrt_output_error_vector(const struct sfrt_driver_ctx * dctx)
f0da18
{
f0da18
	if (dctx->cctx->drvflags & SFRT_DRIVER_ANNOTATE_NEVER)
f0da18
		return sfrt_output_error_vector_plain(dctx);
f0da18
f0da18
	else if (dctx->cctx->drvflags & SFRT_DRIVER_ANNOTATE_ALWAYS)
f0da18
		return sfrt_output_error_vector_annotated(dctx);
f0da18
f0da18
	else if (isatty(STDERR_FILENO))
f0da18
		return sfrt_output_error_vector_annotated(dctx);
f0da18
f0da18
	else
f0da18
		return sfrt_output_error_vector_plain(dctx);
f0da18
}