Category : Recently Uploaded Files
Archive   : DOSUIT07.ZIP
Filename : CRITERR.CPP

 
Output of file : CRITERR.CPP contained in archive : DOSUIT07.ZIP

#include
#ifdef __ZTC__
#include
#endif
#include "event.hpp"
#include "applic.hpp"
#ifdef __TURBOC__
#include
#ifdef enable
#undef enable
#endif
#ifdef disable
#undef disable
#endif
#endif

extern "C" {

static volatile int caught, drive, ax, errcode;

#ifdef __ZTC__
int criterr(struct INT_DATA *pd)
{
caught = 1;
drive = (pd->regs.h.ah & 0x80)? '?' : pd->regs.h.al + 'A';
ax = pd->regs.x.ax;
errcode = pd->regs.x.di;
pd->regs.h.al = 3; // return error to program
return 1;
}
#endif
}

int CriticalErrorEvent::handle(VObject *o)
{
return (o ? o->criterr(*this) : application->criterr(*this));
}

const char *CriticalErrorEvent::description() const
{
static const char * descriptions[] = {
"Write protect error",
"Unknown unit",
"Drive not ready",
"Unknown command",
"Data error",
"Bad request structure length",
"Seek error",
"Unknown media type",
"Sector not found",
"Printer out of paper",
"Write fault",
"Read fault",
"General error"
};

int x = errcode();

if(x < 0 || x > CEgeneralError)
x = CEgeneralError;
return form("Hardware error : Drive %c : Code %04x\n%s",
drive(), ax(), descriptions[x]);
}

class CriticalErrorSource : public EventSource {
friend class CriticalErrorHandler;
virtual Event *v_poll();
virtual int v_enable();
virtual void v_disable();
public:
CriticalErrorSource();
~CriticalErrorSource();
};

static CriticalErrorEvent critEvent;
static CriticalErrorSource critSource;
static int handlerCount, intercepted;

CriticalErrorSource::CriticalErrorSource()
: EventSource("CriticalErrorSource")
{
}

CriticalErrorSource::~CriticalErrorSource()
{
disable();
}

Event *CriticalErrorSource::v_poll()
{
if(!caught)
return 0;
critEvent.d = drive;
critEvent.a = ax;
critEvent.e = (CEerrorCode) errcode;
caught = 0;
#ifndef NDEBUG
if(VObject::debug)
fprintf(VObject::debug,"CriticalErrorEvent %s %d %d %04x\n",
critEvent.description(), drive, errcode, ax);
#endif
return &critEvent;
}

int CriticalErrorSource::v_enable()
{
#ifdef __ZTC__
if(!intercepted)
intercepted = !int_intercept(0x24,criterr,256);
#endif
return intercepted;
}

void CriticalErrorSource::v_disable()
{
if(intercepted)
{
#ifdef __ZTC__
int_restore(0x24);
#endif
intercepted = 0;
}
}

CriticalErrorHandler::CriticalErrorHandler()
{
if(!handlerCount++)
{
if(critSource.enable() && EventQueue::constructed())
eventQueue.add(&critSource);
}
}

CriticalErrorHandler::~CriticalErrorHandler()
{
if(!--handlerCount && EventQueue::constructed())
critSource.disable();
}