/*****************************************************************************/
/*
                                  rawLIB.h
*/
/*****************************************************************************/

#ifndef RAWLIB_H__LOADED
#define RAWLIB_H__LOADED 1

#include <in.h>
#include <socket.h>

/* mainly to allow easy use of the __unaligned directive */
#define ULONGPTR __unaligned ulong*
#define USHORTPTR __unaligned ushort*
#define INT64PTR __unaligned __int64*

#define RAWLIB_ASYNCH ((void*)-1)

/*******************/
/* data structures */
/*******************/

static struct RawLibIOsb
{
   ushort  iosb$w_status;
   ushort  iosb$w_bcnt;
   uint    iosb$l_reserved;
};

static struct RawLibIoStruct
{
   int  DataCount,
        DataSize;
   char  *DataPtr;
   struct dsc$descriptor_s  *DataDscPtr,
                            *ResultDscPtr;
   struct RawLibIOsb  IOsb;
   void (*AstFunction)();
   struct RawLibStruct *RawLibPtr;
};

struct RawLibStruct
{
   ulong  CalloutInProgress,
          InputDataCount,
          InputDataMax,
          InputDataSize,
          InputMrs,
          MsgLineNumber,
          MsgStringLength,
          MsgStringSize,
          OutputMrs,
          QueuedInput,
          QueuedOutput,
          RawSocketClosed,
          RawSocketShut,
          RawSocketVersion,
          ReadDataCount,
          ReadDataSize,
          ReadStatus,
          ShutCount,
          WatchScript,
          WatchDogCloseTime,
          WatchDogCloseSecs,
          WatchDogIdleSecs,
          WatchDogIdleTime,
          WatchDogReadSecs,
          WatchDogReadTime,
          WatchDogWakeSecs,
          WatchDogWakeTime,
          WriteDataCount,
          WriteStatus;

   ulong  InputCount [2],
          InputMsgCount [2],
          MsgBinTime [2],
          OutputCount [2],
          OutputMsgCount [2];

   ushort  InputChannel,
           OutputChannel;

   char  InputDevName [64],
         OutputDevName [64];

   char  *InBufferPtr,
         *MsgStringPtr,
         *MsgDataPtr,
         *OutBufferPtr,
         *OutputDataPtr,
         *ReadDataPtr,
         *ServerSoftwarePtr,
         *WriteDataPtr;

   FILE  *WatchLog;

   void  (*CalloutAstFunction)(),
         (*DestroyAstFunction)(),
         (*MsgCallbackFunction)(),
         (*ReadAstFunction)(),
         (*WakeCallbackFunction)();

   struct dsc$descriptor_s  CalloutDataDsc,
                            InputDevDsc,
                            MsgDsc,
                            OutputDevDsc,
                            ReadDataDsc,
                            ReadResultDsc,
                            WriteDataDsc;

   struct dsc$descriptor_s  *ReadDataDscPtr,
                            *ReadResultDscPtr,
                            *WriteDataDscPtr;

   void  *UserDataPtr;

   struct RawLibStruct  *NextPtr;
};

/***********************/
/* function prototypes */
/***********************/

struct RawLibStruct* RawLibCreate (void*, void*);
void* RawLibDestroy (struct RawLibStruct*);
struct RawLibStruct* RawLibNext (struct RawLibStruct**);

void RawLibSetUserData (struct RawLibStruct*, void*);
void* RawLibGetUserData (struct RawLibStruct*);

int RawLibOpen (struct RawLibStruct*);
int RawLibConnected (struct RawLibStruct*);
void RawLibClose (struct RawLibStruct*);
int RawLibIsClosed (struct RawLibStruct*);
static void Close (struct RawLibStruct*);
static void CloseFreeAst (struct RawLibStruct*);
static void Destroy (struct RawLibStruct*);
int Shut (struct RawLibStruct*);

int RawLibRead (struct RawLibStruct*, char*, int, void*);
int RawLibReadDsc (struct RawLibStruct*, struct dsc$descriptor_s*,
                   struct dsc$descriptor_s*, void*);
char* RawLibReadData (struct RawLibStruct*);
int RawLibReadCount (struct RawLibStruct*);
struct dsc$descriptor_s* RawLibReadDataDsc (struct RawLibStruct*);
int RawLibReadFree (struct RawLibStruct*);
int RawLibReadStatus (struct RawLibStruct*);
ulong* RawLibReadTotal (struct RawLibStruct*);
ulong* RawLibReadMsgTotal (struct RawLibStruct*);

int RawLibWrite (struct RawLibStruct*, char*, int, void*);
void RawLibWriteClose (struct RawLibStruct*, void*);
int RawLibWriteDsc (struct RawLibStruct*, struct dsc$descriptor_s*, void*);
static void WriteEofAst (struct RawLibStruct*);
ulong* RawLibWriteTotal (struct RawLibStruct*);
ulong* RawLibWriteMsgTotal (struct RawLibStruct*);
int RawLibWriteStatus (struct RawLibStruct*);

void* RawLibSetCallout (struct RawLibStruct*, void*);
void* RawLibSetMsgCallback (struct RawLibStruct*, void*);
void* RawLibSetWakeCallback (struct RawLibStruct*, void*, int);
void RawLibSetIdleSecs (struct RawLibStruct*, int);
void RawLibSetLifeSecs (int);
void RawLibSetReadSecs (struct RawLibStruct*, int);

char* RawLibCgiVar (char*);
char* RawLibCgiVarNull (char*);
void RawLibCgiPlusEof ();
void RawLibCgiPlusEot ();
void RawLibCgiPlusEsc ();
int RawLibIsCgiPlus ();
void RawLibCallout (struct RawLibStruct*, char*, ...);
void RawLibOnNextRequest (void*);
int RawLibCgiVarAvailable ();
void RawLibWatchScript (struct RawLibStruct*, char*, int, char*, ...);
char* RawLibVersion ();

void RawLibInit();
unsigned int RawLibTime();
void RawLibExit (struct RawLibStruct*, char*, int, int);
struct dsc$descriptor_s* RawLibMsgDsc (struct RawLibStruct*);
char* RawLibMsgString (struct RawLibStruct*);
int RawLibMsgLineNumber (struct RawLibStruct*);
void RawLibResetMsg (struct RawLibStruct*);

static void MsgCallback (struct RawLibStruct*, int, int, char*, ...);
static void ReadDataAst (struct RawLibIoStruct*);
static void WatchDog ();
static void WriteDataAst (struct RawLibIoStruct*);
static void WriteFreeAst (char*);
static void WriteOutputAst (struct RawLibStruct*);

#endif /* RAWLIB_H__LOADED */

/*****************************************************************************/

