Files
sics/tecs/errhdl.c
2000-03-31 07:51:28 +00:00

87 lines
1.5 KiB
C

#include <assert.h>
#include <string.h>
#include "errhdl.h"
#define SLEN 64
#define MLEN 64
static char *txt[SLEN];
static int sp=0;
int ErrCode;
char *ErrMessage=NULL;
void ErrTxt(char *text, int systemError)
{
if (systemError) { sp=0; ErrCode=errno; ErrMessage=strerror(errno); }
if (sp<SLEN) {
txt[sp++]=text;
}
}
void ErrMsg(char *msg)
{
ErrCode=-1;
ErrMessage=msg; sp=0;
}
void ErrCod(int code)
{
ErrCode=code;
ErrMessage=strerror(code); sp=0;
}
void ErrWriteGeneral(char *text, void (*outrtn)(), void *arg)
{
int i, l;
char buf[256];
l=strlen(text)+strlen(ErrMessage)+6;
assert(l<256);
sprintf(buf, "--- %s: %s", text, ErrMessage);
for (i=0;i<sp;i++) {
if (txt[i][0]==':') {
l+=strlen(txt[i]);
assert(l<256);
strcat(buf, &(txt[i][1]));
} else {
outrtn(arg, buf);
l=strlen(txt[i]);
assert(l<256);
strcpy(buf, txt[i]);
}
}
outrtn(arg, buf);
outrtn(arg, "");
}
void ErrOutFil(void *arg, char *text) {
fprintf((FILE *)arg, "%s\n", text);
}
void ErrWrite(FILE *fil, char *text)
{
ErrWriteGeneral(text, ErrOutFil, fil);
}
void ErrShow(char *text) {
ErrWriteGeneral(text, ErrOutFil, stdout);
}
#ifdef __VMS
void ErrShowVms(char **text, void (*outrtn)(), void *arg) {
char buf[256];
int l;
l=*(short *)text;
if (l>=256) l=255;
strncpy(buf, text[1], l);
buf[l]='\0';
ErrWriteGeneral(buf, outrtn, arg);
}
#endif
void ERR_EXIT(char *text) {
ErrShow(text); exit(1);
}