|
|
02e59c |
/**************************************************************/
|
|
|
02e59c |
/* treebnf: a tree oriented bnf library */
|
|
|
02e59c |
/* Copyright (C) 2024 SysDeer Technologies, LLC */
|
|
|
02e59c |
/* Released under GPLv2 and GPLv3; see COPYING.TREEBNF. */
|
|
|
02e59c |
/**************************************************************/
|
|
|
02e59c |
|
|
|
02e59c |
#include <stdio.h>
|
|
|
02e59c |
#include <string.h>
|
|
|
02e59c |
#include <errno.h>
|
|
|
02e59c |
#include <unistd.h>
|
|
|
02e59c |
#include <treebnf/treebnf.h>
|
|
|
02e59c |
|
|
|
02e59c |
#include "treebnf_driver_impl.h"
|
|
|
02e59c |
#include "treebnf_dprintf_impl.h"
|
|
|
02e59c |
|
|
|
02e59c |
static const char aclr_reset[] = "\x1b[0m";
|
|
|
02e59c |
static const char aclr_bold[] = "\x1b[1m";
|
|
|
02e59c |
|
|
|
02e59c |
static const char aclr_green[] = "\x1b[32m";
|
|
|
02e59c |
static const char aclr_blue[] = "\x1b[34m";
|
|
|
02e59c |
static const char aclr_magenta[] = "\x1b[35m";
|
|
|
02e59c |
|
|
|
02e59c |
static const char * tbnf_output_error_header(const struct tbnf_error_info * erri)
|
|
|
02e59c |
{
|
|
|
02e59c |
if (erri->eflags & TBNF_ERROR_CHILD)
|
|
|
02e59c |
return "exec error upon";
|
|
|
02e59c |
|
|
|
02e59c |
else if (erri->eflags & TBNF_ERROR_TOP_LEVEL)
|
|
|
02e59c |
return "error logged in";
|
|
|
02e59c |
|
|
|
02e59c |
else if (erri->eflags & TBNF_ERROR_NESTED)
|
|
|
02e59c |
return "< returned to >";
|
|
|
02e59c |
|
|
|
02e59c |
else
|
|
|
02e59c |
return "distorted state";
|
|
|
02e59c |
}
|
|
|
02e59c |
|
|
|
02e59c |
static const char * tbnf_output_strerror(
|
|
|
02e59c |
const struct tbnf_error_info * erri,
|
|
|
02e59c |
char (*errbuf)[256])
|
|
|
02e59c |
{
|
|
|
02e59c |
if (erri->eflags & TBNF_ERROR_CUSTOM)
|
|
|
02e59c |
return "flow error: unexpected condition or other";
|
|
|
02e59c |
|
|
|
02e59c |
else if (erri->eflags & TBNF_ERROR_NESTED)
|
|
|
02e59c |
return "";
|
|
|
02e59c |
|
|
|
02e59c |
else if (erri->eflags & TBNF_ERROR_CHILD)
|
|
|
02e59c |
return "(see child process error messages)";
|
|
|
02e59c |
|
|
|
02e59c |
else if (erri->esyscode == ENOBUFS)
|
|
|
02e59c |
return "input error: string length exceeds buffer size";
|
|
|
02e59c |
|
|
|
02e59c |
else if ((erri->esyscode == ENOENT) && erri->eany)
|
|
|
02e59c |
return "path not found: ";
|
|
|
02e59c |
|
|
|
02e59c |
else
|
|
|
02e59c |
return strerror_r(erri->esyscode,*errbuf,sizeof(*errbuf))
|
|
|
02e59c |
? "internal error: strerror_r(3) call failed"
|
|
|
02e59c |
: *errbuf;
|
|
|
02e59c |
}
|
|
|
02e59c |
|
|
|
02e59c |
static int tbnf_output_error_record_plain(
|
|
|
02e59c |
const struct tbnf_driver_ctx * dctx,
|
|
|
02e59c |
const struct tbnf_error_info * erri)
|
|
|
02e59c |
{
|
|
|
02e59c |
char errbuf[256];
|
|
|
02e59c |
|
|
|
02e59c |
int fderr = tbnf_driver_fderr(dctx);
|
|
|
02e59c |
const char * errdesc = tbnf_output_strerror(erri,&errbuf);
|
|
|
02e59c |
const char * path;
|
|
|
02e59c |
|
|
|
02e59c |
path = ((erri->esyscode == ENOENT) && erri->eany)
|
|
|
02e59c |
? erri->eany : "";
|
|
|
02e59c |
|
|
|
02e59c |
if (tbnf_dprintf(
|
|
|
02e59c |
fderr,
|
|
|
02e59c |
"%s: %s %s(), line %d%s%s%s.\n",
|
|
|
02e59c |
dctx->program,
|
|
|
02e59c |
tbnf_output_error_header(erri),
|
|
|
02e59c |
erri->efunction,
|
|
|
02e59c |
erri->eline,
|
|
|
02e59c |
strlen(errdesc) ? ": " : "",
|
|
|
02e59c |
errdesc,path) < 0)
|
|
|
02e59c |
return -1;
|
|
|
02e59c |
|
|
|
02e59c |
return 0;
|
|
|
02e59c |
}
|
|
|
02e59c |
|
|
|
02e59c |
static int tbnf_output_error_record_annotated(
|
|
|
02e59c |
const struct tbnf_driver_ctx * dctx,
|
|
|
02e59c |
const struct tbnf_error_info * erri)
|
|
|
02e59c |
{
|
|
|
02e59c |
char errbuf[256];
|
|
|
02e59c |
|
|
|
02e59c |
int fderr = tbnf_driver_fderr(dctx);
|
|
|
02e59c |
const char * errdesc = tbnf_output_strerror(erri,&errbuf);
|
|
|
02e59c |
const char * path;
|
|
|
02e59c |
|
|
|
02e59c |
path = ((erri->esyscode == ENOENT) && erri->eany)
|
|
|
02e59c |
? erri->eany : "";
|
|
|
02e59c |
|
|
|
02e59c |
if (tbnf_dprintf(
|
|
|
02e59c |
fderr,
|
|
|
02e59c |
"%s%s%s:%s %s%s%s %s%s%s()%s, %s%sline %d%s%s%s%s%s%s%s%s%s.\n",
|
|
|
02e59c |
|
|
|
02e59c |
aclr_bold,aclr_magenta,
|
|
|
02e59c |
dctx->program,
|
|
|
02e59c |
aclr_reset,
|
|
|
02e59c |
|
|
|
02e59c |
aclr_bold,
|
|
|
02e59c |
tbnf_output_error_header(erri),
|
|
|
02e59c |
aclr_reset,
|
|
|
02e59c |
|
|
|
02e59c |
aclr_bold,aclr_blue,
|
|
|
02e59c |
erri->efunction,
|
|
|
02e59c |
aclr_reset,
|
|
|
02e59c |
|
|
|
02e59c |
aclr_bold,aclr_green,
|
|
|
02e59c |
erri->eline,
|
|
|
02e59c |
aclr_reset,
|
|
|
02e59c |
strlen(errdesc) ? ": " : "",
|
|
|
02e59c |
|
|
|
02e59c |
aclr_bold,
|
|
|
02e59c |
errdesc,
|
|
|
02e59c |
aclr_reset,
|
|
|
02e59c |
|
|
|
02e59c |
aclr_bold,aclr_blue,
|
|
|
02e59c |
path,
|
|
|
02e59c |
aclr_reset) < 0)
|
|
|
02e59c |
return -1;
|
|
|
02e59c |
|
|
|
02e59c |
return 0;
|
|
|
02e59c |
}
|
|
|
02e59c |
|
|
|
02e59c |
int tbnf_output_error_record(
|
|
|
02e59c |
const struct tbnf_driver_ctx * dctx,
|
|
|
02e59c |
const struct tbnf_error_info * erri)
|
|
|
02e59c |
{
|
|
|
02e59c |
int fderr = tbnf_driver_fderr(dctx);
|
|
|
02e59c |
|
|
|
02e59c |
if (dctx->cctx->drvflags & TBNF_DRIVER_ANNOTATE_NEVER)
|
|
|
02e59c |
return tbnf_output_error_record_plain(dctx,erri);
|
|
|
02e59c |
|
|
|
02e59c |
else if (dctx->cctx->drvflags & TBNF_DRIVER_ANNOTATE_ALWAYS)
|
|
|
02e59c |
return tbnf_output_error_record_annotated(dctx,erri);
|
|
|
02e59c |
|
|
|
02e59c |
else if (isatty(fderr))
|
|
|
02e59c |
return tbnf_output_error_record_annotated(dctx,erri);
|
|
|
02e59c |
|
|
|
02e59c |
else
|
|
|
02e59c |
return tbnf_output_error_record_plain(dctx,erri);
|
|
|
02e59c |
}
|
|
|
02e59c |
|
|
|
02e59c |
static int tbnf_output_error_vector_plain(const struct tbnf_driver_ctx * dctx)
|
|
|
02e59c |
{
|
|
|
02e59c |
struct tbnf_error_info ** perr;
|
|
|
02e59c |
|
|
|
02e59c |
for (perr=dctx->errv; *perr; perr++)
|
|
|
02e59c |
if (tbnf_output_error_record_plain(dctx,*perr))
|
|
|
02e59c |
return -1;
|
|
|
02e59c |
|
|
|
02e59c |
return 0;
|
|
|
02e59c |
}
|
|
|
02e59c |
|
|
|
02e59c |
static int tbnf_output_error_vector_annotated(const struct tbnf_driver_ctx * dctx)
|
|
|
02e59c |
{
|
|
|
02e59c |
struct tbnf_error_info ** perr;
|
|
|
02e59c |
|
|
|
02e59c |
for (perr=dctx->errv; *perr; perr++)
|
|
|
02e59c |
if (tbnf_output_error_record_annotated(dctx,*perr))
|
|
|
02e59c |
return -1;
|
|
|
02e59c |
|
|
|
02e59c |
return 0;
|
|
|
02e59c |
}
|
|
|
02e59c |
|
|
|
02e59c |
int tbnf_output_error_vector(const struct tbnf_driver_ctx * dctx)
|
|
|
02e59c |
{
|
|
|
02e59c |
int fderr = tbnf_driver_fderr(dctx);
|
|
|
02e59c |
|
|
|
02e59c |
if (dctx->cctx->drvflags & TBNF_DRIVER_ANNOTATE_NEVER)
|
|
|
02e59c |
return tbnf_output_error_vector_plain(dctx);
|
|
|
02e59c |
|
|
|
02e59c |
else if (dctx->cctx->drvflags & TBNF_DRIVER_ANNOTATE_ALWAYS)
|
|
|
02e59c |
return tbnf_output_error_vector_annotated(dctx);
|
|
|
02e59c |
|
|
|
02e59c |
else if (isatty(fderr))
|
|
|
02e59c |
return tbnf_output_error_vector_annotated(dctx);
|
|
|
02e59c |
|
|
|
02e59c |
else
|
|
|
02e59c |
return tbnf_output_error_vector_plain(dctx);
|
|
|
02e59c |
}
|