|
|
1480f4 |
#include <stdio.h>
|
|
|
1480f4 |
#include <string.h>
|
|
|
1480f4 |
#include <errno.h>
|
|
|
1480f4 |
#include <unistd.h>
|
|
|
5eaec4 |
|
|
|
1480f4 |
#include <apimagic/apimagic.h>
|
|
|
5eaec4 |
#include "apimagic_driver_impl.h"
|
|
|
5eaec4 |
#include "apimagic_dprintf_impl.h"
|
|
|
1480f4 |
|
|
|
1480f4 |
static const char aclr_reset[] = "\x1b[0m";
|
|
|
1480f4 |
static const char aclr_bold[] = "\x1b[1m";
|
|
|
1480f4 |
|
|
|
1480f4 |
static const char aclr_red[] = "\x1b[31m";
|
|
|
1480f4 |
static const char aclr_green[] = "\x1b[32m";
|
|
|
1480f4 |
static const char aclr_blue[] = "\x1b[34m";
|
|
|
1480f4 |
static const char aclr_magenta[] = "\x1b[35m";
|
|
|
1480f4 |
|
|
|
1f9fe7 |
static char const * const amgc_error_strings[AMGC_ERR_CAP] = {
|
|
|
1480f4 |
[AMGC_ERR_FLOW_ERROR] = "flow error: unexpected condition or other",
|
|
|
1480f4 |
[AMGC_ERR_FLEE_ERROR] = "flees and bugs and cats and mice",
|
|
|
1480f4 |
[AMGC_ERR_NULL_CONTEXT] = "null driver or unit context",
|
|
|
1480f4 |
[AMGC_ERR_NULL_SOURCE] = "source file does not define any data",
|
|
|
1480f4 |
[AMGC_ERR_INVALID_CONTEXT] = "invalid driver or unit context",
|
|
|
1480f4 |
[AMGC_ERR_INVALID_SOURCE] = "invalid source file",
|
|
|
1480f4 |
[AMGC_ERR_SOURCE_SIZE_ZERO] = "cannot map an empty source file",
|
|
|
1480f4 |
};
|
|
|
1480f4 |
|
|
|
1480f4 |
static const char * amgc_output_error_header(const struct amgc_error_info * erri)
|
|
|
1480f4 |
{
|
|
|
1480f4 |
if (erri->eflags & AMGC_ERROR_CHILD)
|
|
|
1480f4 |
return "exec error upon";
|
|
|
1480f4 |
|
|
|
1480f4 |
else if (erri->eflags & AMGC_ERROR_TOP_LEVEL)
|
|
|
1480f4 |
return "error logged in";
|
|
|
1480f4 |
|
|
|
1480f4 |
else if (erri->eflags & AMGC_ERROR_NESTED)
|
|
|
1480f4 |
return "< returned to >";
|
|
|
1480f4 |
|
|
|
1480f4 |
else
|
|
|
1480f4 |
return "distorted state";
|
|
|
1480f4 |
}
|
|
|
1480f4 |
|
|
|
1480f4 |
static const char * amgc_output_unit_header(const struct amgc_error_info * erri)
|
|
|
1480f4 |
{
|
|
|
1480f4 |
if (!(erri->eflags & AMGC_ERROR_CUSTOM))
|
|
|
1480f4 |
return "while opening";
|
|
|
1480f4 |
|
|
|
1480f4 |
else if (erri->elibcode == AMGC_ERR_SOURCE_SIZE_ZERO)
|
|
|
1480f4 |
return "while mapping";
|
|
|
1480f4 |
|
|
|
1480f4 |
else
|
|
|
1480f4 |
return "while parsing";
|
|
|
1480f4 |
}
|
|
|
1480f4 |
|
|
|
a8b655 |
static const char * amgc_output_strerror(
|
|
|
a8b655 |
const struct amgc_error_info * erri,
|
|
|
a8b655 |
char (*errbuf)[256])
|
|
|
1480f4 |
{
|
|
|
1480f4 |
if (erri->eflags & AMGC_ERROR_CUSTOM)
|
|
|
1480f4 |
return ((erri->elibcode < 0) || (erri->elibcode >= AMGC_ERR_CAP))
|
|
|
1480f4 |
? "internal error: please report to the maintainer"
|
|
|
1480f4 |
: amgc_error_strings[erri->elibcode];
|
|
|
1480f4 |
|
|
|
1480f4 |
else if (erri->eflags & AMGC_ERROR_NESTED)
|
|
|
1480f4 |
return "";
|
|
|
1480f4 |
|
|
|
1480f4 |
else if (erri->eflags & AMGC_ERROR_CHILD)
|
|
|
1480f4 |
return "(see child process error messages)";
|
|
|
1480f4 |
|
|
|
1480f4 |
else if (erri->esyscode == ENOBUFS)
|
|
|
1480f4 |
return "input error: string length exceeds buffer size.";
|
|
|
1480f4 |
|
|
|
1480f4 |
else
|
|
|
a8b655 |
return strerror_r(erri->esyscode,*errbuf,sizeof(*errbuf))
|
|
|
a8b655 |
? "internal error: strerror_r(3) call failed"
|
|
|
a8b655 |
: *errbuf;
|
|
|
1480f4 |
}
|
|
|
1480f4 |
|
|
|
1480f4 |
static int amgc_output_error_record_plain(
|
|
|
1480f4 |
const struct amgc_driver_ctx * dctx,
|
|
|
1480f4 |
const struct amgc_error_info * erri)
|
|
|
1480f4 |
{
|
|
|
1480f4 |
const char * epath;
|
|
|
a8b655 |
char errbuf[256];
|
|
|
a8b655 |
|
|
|
5eaec4 |
int fderr = amgc_driver_fderr(dctx);
|
|
|
a8b655 |
const char * errdesc = amgc_output_strerror(erri,&errbuf);
|
|
|
1480f4 |
|
|
|
1480f4 |
epath = erri->euctx
|
|
|
1480f4 |
? *erri->euctx->path
|
|
|
1480f4 |
: erri->eunit;
|
|
|
1480f4 |
|
|
|
1480f4 |
if (epath && !(erri->eflags & AMGC_ERROR_NESTED))
|
|
|
5eaec4 |
if (amgc_dprintf(
|
|
|
5eaec4 |
fderr,
|
|
|
5eaec4 |
"%s: [%s] '%s':\n",
|
|
|
1480f4 |
dctx->program,
|
|
|
1480f4 |
amgc_output_unit_header(erri),
|
|
|
1480f4 |
epath) < 0)
|
|
|
1480f4 |
return -1;
|
|
|
1480f4 |
|
|
|
5eaec4 |
if (amgc_dprintf(
|
|
|
5eaec4 |
fderr,
|
|
|
5eaec4 |
"%s: %s %s(), line %d%s%s.\n",
|
|
|
1480f4 |
dctx->program,
|
|
|
1480f4 |
amgc_output_error_header(erri),
|
|
|
1480f4 |
erri->efunction,
|
|
|
1480f4 |
erri->eline,
|
|
|
1480f4 |
strlen(errdesc) ? ": " : "",
|
|
|
1480f4 |
errdesc) < 0)
|
|
|
1480f4 |
return -1;
|
|
|
1480f4 |
|
|
|
5eaec4 |
return 0;
|
|
|
1480f4 |
}
|
|
|
1480f4 |
|
|
|
1480f4 |
static int amgc_output_error_record_annotated(
|
|
|
1480f4 |
const struct amgc_driver_ctx * dctx,
|
|
|
1480f4 |
const struct amgc_error_info * erri)
|
|
|
1480f4 |
{
|
|
|
1480f4 |
const char * epath;
|
|
|
a8b655 |
char errbuf[256];
|
|
|
a8b655 |
|
|
|
5eaec4 |
int fderr = amgc_driver_fderr(dctx);
|
|
|
a8b655 |
const char * errdesc = amgc_output_strerror(erri,&errbuf);
|
|
|
1480f4 |
|
|
|
1480f4 |
epath = erri->euctx
|
|
|
1480f4 |
? *erri->euctx->path
|
|
|
1480f4 |
: erri->eunit;
|
|
|
1480f4 |
|
|
|
1480f4 |
if (epath && !(erri->eflags & AMGC_ERROR_NESTED))
|
|
|
5eaec4 |
if (amgc_dprintf(
|
|
|
5eaec4 |
fderr,
|
|
|
1480f4 |
"%s%s%s:%s %s[%s]%s %s%s'%s'%s:\n",
|
|
|
1480f4 |
|
|
|
1480f4 |
aclr_bold,aclr_magenta,
|
|
|
1480f4 |
dctx->program,
|
|
|
1480f4 |
aclr_reset,
|
|
|
1480f4 |
|
|
|
1480f4 |
aclr_bold,
|
|
|
1480f4 |
amgc_output_unit_header(erri),
|
|
|
1480f4 |
aclr_reset,
|
|
|
1480f4 |
|
|
|
1480f4 |
aclr_bold,aclr_red,
|
|
|
1480f4 |
epath,
|
|
|
1480f4 |
aclr_reset) < 0)
|
|
|
1480f4 |
return -1;
|
|
|
1480f4 |
|
|
|
5eaec4 |
if (amgc_dprintf(
|
|
|
5eaec4 |
fderr,
|
|
|
1480f4 |
"%s%s%s:%s %s%s%s %s%s%s()%s, %s%sline %d%s%s%s%s%s.\n",
|
|
|
1480f4 |
|
|
|
1480f4 |
aclr_bold,aclr_magenta,
|
|
|
1480f4 |
dctx->program,
|
|
|
1480f4 |
aclr_reset,
|
|
|
1480f4 |
|
|
|
1480f4 |
aclr_bold,
|
|
|
1480f4 |
amgc_output_error_header(erri),
|
|
|
1480f4 |
aclr_reset,
|
|
|
1480f4 |
|
|
|
1480f4 |
aclr_bold,aclr_blue,
|
|
|
1480f4 |
erri->efunction,
|
|
|
1480f4 |
aclr_reset,
|
|
|
1480f4 |
|
|
|
1480f4 |
aclr_bold,aclr_green,
|
|
|
1480f4 |
erri->eline,
|
|
|
1480f4 |
aclr_reset,
|
|
|
1480f4 |
strlen(errdesc) ? ": " : "",
|
|
|
1480f4 |
|
|
|
1480f4 |
aclr_bold,
|
|
|
a8b655 |
errdesc,
|
|
|
1480f4 |
aclr_reset) < 0)
|
|
|
1480f4 |
return -1;
|
|
|
1480f4 |
|
|
|
5eaec4 |
return 0;
|
|
|
1480f4 |
}
|
|
|
1480f4 |
|
|
|
1480f4 |
int amgc_output_error_record(
|
|
|
1480f4 |
const struct amgc_driver_ctx * dctx,
|
|
|
1480f4 |
const struct amgc_error_info * erri)
|
|
|
1480f4 |
{
|
|
|
1480f4 |
if (dctx->cctx->drvflags & AMGC_DRIVER_ANNOTATE_NEVER)
|
|
|
1480f4 |
return amgc_output_error_record_plain(dctx,erri);
|
|
|
1480f4 |
|
|
|
1480f4 |
else if (dctx->cctx->drvflags & AMGC_DRIVER_ANNOTATE_ALWAYS)
|
|
|
1480f4 |
return amgc_output_error_record_annotated(dctx,erri);
|
|
|
1480f4 |
|
|
|
5eaec4 |
else if (isatty(amgc_driver_fderr(dctx)))
|
|
|
1480f4 |
return amgc_output_error_record_annotated(dctx,erri);
|
|
|
1480f4 |
|
|
|
1480f4 |
else
|
|
|
1480f4 |
return amgc_output_error_record_plain(dctx,erri);
|
|
|
1480f4 |
}
|
|
|
1480f4 |
|
|
|
1480f4 |
static int amgc_output_error_vector_plain(const struct amgc_driver_ctx * dctx)
|
|
|
1480f4 |
{
|
|
|
1480f4 |
struct amgc_error_info ** perr;
|
|
|
1480f4 |
|
|
|
1480f4 |
for (perr=dctx->errv; *perr; perr++)
|
|
|
1480f4 |
if (amgc_output_error_record_plain(dctx,*perr))
|
|
|
1480f4 |
return -1;
|
|
|
1480f4 |
|
|
|
1480f4 |
return 0;
|
|
|
1480f4 |
}
|
|
|
1480f4 |
|
|
|
1480f4 |
static int amgc_output_error_vector_annotated(const struct amgc_driver_ctx * dctx)
|
|
|
1480f4 |
{
|
|
|
1480f4 |
struct amgc_error_info ** perr;
|
|
|
1480f4 |
|
|
|
1480f4 |
for (perr=dctx->errv; *perr; perr++)
|
|
|
1480f4 |
if (amgc_output_error_record_annotated(dctx,*perr))
|
|
|
1480f4 |
return -1;
|
|
|
1480f4 |
|
|
|
1480f4 |
return 0;
|
|
|
1480f4 |
}
|
|
|
1480f4 |
|
|
|
1480f4 |
int amgc_output_error_vector(const struct amgc_driver_ctx * dctx)
|
|
|
1480f4 |
{
|
|
|
1480f4 |
if (dctx->cctx->drvflags & AMGC_DRIVER_ANNOTATE_NEVER)
|
|
|
1480f4 |
return amgc_output_error_vector_plain(dctx);
|
|
|
1480f4 |
|
|
|
1480f4 |
else if (dctx->cctx->drvflags & AMGC_DRIVER_ANNOTATE_ALWAYS)
|
|
|
1480f4 |
return amgc_output_error_vector_annotated(dctx);
|
|
|
1480f4 |
|
|
|
5eaec4 |
else if (isatty(amgc_driver_fderr(dctx)))
|
|
|
1480f4 |
return amgc_output_error_vector_annotated(dctx);
|
|
|
1480f4 |
|
|
|
1480f4 |
else
|
|
|
1480f4 |
return amgc_output_error_vector_plain(dctx);
|
|
|
1480f4 |
}
|