54 lines
826 B
C
54 lines
826 B
C
/*
|
|
* Plan 9 style: short, no levels beyond fatal/error, no colour.
|
|
*/
|
|
#include "ww.h"
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
Pos noPos = { "<none>", 0, 0 };
|
|
FILE *errout; /* set by main; defaults to stderr */
|
|
|
|
static FILE *
|
|
out(void)
|
|
{
|
|
return errout ? errout : stderr;
|
|
}
|
|
|
|
static void
|
|
prefix(Pos p)
|
|
{
|
|
FILE *f = out();
|
|
if (p.file == NULL)
|
|
p = noPos;
|
|
if (p.line > 0)
|
|
fprintf(f, "%s:%d:%d: ", p.file, p.line, p.col);
|
|
else
|
|
fprintf(f, "%s: ", p.file);
|
|
}
|
|
|
|
void
|
|
fatal(const char *fmt, ...)
|
|
{
|
|
FILE *f = out();
|
|
va_list ap;
|
|
fprintf(f, "ww: ");
|
|
va_start(ap, fmt);
|
|
vfprintf(f, fmt, ap);
|
|
va_end(ap);
|
|
fprintf(f, "\n");
|
|
exit(1);
|
|
}
|
|
|
|
void
|
|
errorf(Pos p, const char *fmt, ...)
|
|
{
|
|
FILE *f = out();
|
|
va_list ap;
|
|
prefix(p);
|
|
fprintf(f, "error: ");
|
|
va_start(ap, fmt);
|
|
vfprintf(f, fmt, ap);
|
|
va_end(ap);
|
|
fprintf(f, "\n");
|
|
}
|