Blame src/output/sfrt_output_error.c

f0da18
#include <stdio.h>
f0da18
#include <string.h>
f0da18
#include <errno.h>
f0da18
#include <unistd.h>
816f06
f0da18
#include <sofort/sofort.h>
816f06
#include "sofort_driver_impl.h"
816f06
#include "sofort_dprintf_impl.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
65a0ef
static const char * sfrt_output_strerror(
65a0ef
	const struct sfrt_error_info * erri,
65a0ef
	char (*errbuf)[256])
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
65a0ef
		return strerror_r(erri->esyscode,*errbuf,sizeof(*errbuf))
65a0ef
			? "internal error: strerror_r(3) call failed"
65a0ef
			: *errbuf;
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;
65a0ef
	char         errbuf[256];
113eb1
816f06
	int          fderr   = sfrt_driver_fderr(dctx);
113eb1
	const char * errdesc = sfrt_output_strerror(erri,&errbuf);
f0da18
f0da18
	epath = erri->euctx
f0da18
		? *erri->euctx->path
f0da18
		: erri->eunit;
f0da18
f0da18
	if (epath && !(erri->eflags & SFRT_ERROR_NESTED))
816f06
		if (sfrt_dprintf(
816f06
				fderr,
816f06
				"%s: [%s] '%s':\n",
f0da18
				dctx->program,
f0da18
				sfrt_output_unit_header(erri),
f0da18
				epath) < 0)
f0da18
			return -1;
f0da18
816f06
	if (sfrt_dprintf(
816f06
			fderr,
816f06
			"%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
816f06
	return 0;
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;
65a0ef
	char         errbuf[256];
113eb1
816f06
	int          fderr   = sfrt_driver_fderr(dctx);
113eb1
	const char * errdesc = sfrt_output_strerror(erri,&errbuf);
f0da18
f0da18
	epath = erri->euctx
f0da18
		? *erri->euctx->path
f0da18
		: erri->eunit;
f0da18
f0da18
	if (epath && !(erri->eflags & SFRT_ERROR_NESTED))
816f06
		if (sfrt_dprintf(
816f06
				fderr,
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
816f06
	if (sfrt_dprintf(
816f06
			fderr,
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,
65a0ef
			errdesc,
f0da18
			aclr_reset) < 0)
f0da18
		return -1;
f0da18
816f06
	return 0;
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
816f06
	else if (isatty(sfrt_driver_fderr(dctx)))
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
816f06
	else if (isatty(sfrt_driver_fderr(dctx)))
f0da18
		return sfrt_output_error_vector_annotated(dctx);
f0da18
f0da18
	else
f0da18
		return sfrt_output_error_vector_plain(dctx);
f0da18
}