118 lines
1.9 KiB
C
118 lines
1.9 KiB
C
#include <assert.h>
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
|
|
#include "err_handling.h"
|
|
|
|
#define SLEN 64
|
|
#define MLEN 64
|
|
|
|
static char *txt[SLEN];
|
|
static int sp=0;
|
|
|
|
int ErrCode;
|
|
char *ErrMessage=NULL;
|
|
void (*outrtn)()=NULL;
|
|
void *outarg;
|
|
|
|
void *my_malloc(size_t size, const char *text) {
|
|
void *ptr;
|
|
ptr=calloc(1,size);
|
|
/* printf("new %s %X %d\n", text, ptr, size); */
|
|
return(ptr);
|
|
}
|
|
|
|
void my_free(void *ptr) {
|
|
/* printf("my_free %X\n", ptr); */
|
|
free(ptr);
|
|
}
|
|
|
|
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 ErrOutFil(void *arg, char *text) {
|
|
fprintf((FILE *)arg, "%s\n", text);
|
|
}
|
|
|
|
void ErrWrite(char *text)
|
|
{
|
|
int i, l;
|
|
char buf[256];
|
|
|
|
if (outrtn==NULL) {
|
|
outrtn=ErrOutFil;
|
|
outarg=stdout;
|
|
}
|
|
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(outarg, buf);
|
|
l=strlen(txt[i]);
|
|
assert(l<256);
|
|
strcpy(buf, txt[i]);
|
|
}
|
|
}
|
|
outrtn(outarg, buf);
|
|
outrtn(outarg, "");
|
|
}
|
|
|
|
void ErrSetOutRtn(void (*rtn)(), void *arg) {
|
|
outrtn=rtn;
|
|
outarg=arg;
|
|
}
|
|
|
|
void ErrSetOutFile(FILE *arg) {
|
|
outrtn=ErrOutFil;
|
|
outarg=arg;
|
|
}
|
|
|
|
void ERR_EXIT(char *text) {
|
|
ErrWrite(text); exit(1);
|
|
}
|
|
|
|
void err_show_(char *text, int length) {
|
|
char buf[256];
|
|
|
|
if (length>=256) length=255;
|
|
strncpy(buf, text, length);
|
|
buf[length]='\0';
|
|
ErrWrite(buf);
|
|
}
|
|
|
|
#ifdef __VMS
|
|
|
|
typedef struct { short size, dummy; char *text; } Desc;
|
|
|
|
void err_show(Desc *desc) {
|
|
err_show_(desc->text, desc->size);
|
|
}
|
|
|
|
#endif
|
|
|
|
void errsetoutrtn_(void (*rtn)(), void *arg) {
|
|
ErrSetOutRtn(rtn, arg);
|
|
}
|