Files
sics/tecs/myc_err.c
2002-06-10 12:45:24 +00:00

145 lines
2.3 KiB
C

#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include "sys_util.h"
#include "myc_str.h"
#include "myc_err.h"
#define SLEN 64
#define MLEN 64
static char *txt[SLEN];
static int sp=0;
static int stack_empty=1;
int ErrCode;
char *ErrMessage=NULL;
void (*outrtn)()=NULL;
void *outarg;
void ErrTxt(char *text, int systemError)
{
if (systemError) {
sp=0; ErrCode=errno; ErrMessage=strerror(errno);
}
if (stack_empty && sp>0) {
sp=0;
stack_empty=0;
}
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 ErrShow(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, "");
stack_empty=1;
}
void ErrShort(char *msg) {
if (outrtn==NULL) {
outrtn=ErrOutFil;
outarg=stdout;
}
outrtn(outarg, msg);
}
void ErrSetOutRtn(void (*rtn)(), void *arg) {
outrtn=rtn;
outarg=arg;
}
void ErrSetOutFile(FILE *arg) {
outrtn=ErrOutFil;
outarg=arg;
}
void ERR_EXIT(char *text) {
ErrShow(text); exit(1);
}
/* FORTRAN wrappers */
#ifdef F_CHAR
/* compile only when fortran c interface stuff is defined */
#ifdef __VMS
#define err_show_ err_show
#define err_txt_ err_txt
#define err_msg_ err_msg
#define err_set_outrtn_ err_set_outrtn
#define err_short_ err_short
#endif
void err_show_(F_CHAR(text), int text_len) {
char buf[256];
STR_TO_C(buf, text);
ErrShow(buf);
}
void err_txt_(F_CHAR(text), int text_len) {
char buf[256];
STR_TO_C(buf, text);
ErrTxt(buf,0);
}
void err_msg_(F_CHAR(text), int text_len) {
char buf[256];
STR_TO_C(buf, text);
ErrMsg(buf);
}
void err_set_outrtn_(void (*rtn)(), void *arg) {
ErrSetOutRtn(rtn, arg);
}
void err_short_(void) {
ErrShort(ErrMessage);
}
#endif