#include <fcntl.h>

#define WORD      unsigned int
#define BYTE      unsigned char
#define HANDLE    int
#define BOOL      int
#define FALSE     0
#define TRUE      1
#define LPSTR     char far *

#define OF_READWRITE O_RDWR
#define OF_WRITE     O_WRONLY
#define OF_READ      O_RDONLY
#define OF_CREATE    O_CREAT | O_TRUNC

#define OPENERROR (HANDLE)-1
#define IOERROR   (WORD)-1

#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Square(n) ((n)*(n))

#define Maxbuff 1024

#define M_inname  (char)0
#define M_in2name (char)1
#define M_outname (char)2
#define M_tmpname (char)3

#define O_readb   (char)0
#define O_writeb  (char)1
#define O_appendb (char)2
#define O_readt   (char)3
#define O_writet  (char)4
#define O_appendt (char)5

#define R_Data  (char)0
#define TR_Data (char)1
#define X_Data  (char)2
#define TX_Data (char)3

#define CGA_HiResImage (char)20 
#define CGA_LoResImage (char)21
#define HERC_MonoImage (char)22
#define EGA_Image      (char)23
#define PGA_Image      (char)24
#define EPSN_Image     (char)30
#define HPLJ_Image     (char)31

#define MONO_scrn (char)0
#define CGA_scrn  (char)1
#define DUAL_scrn (char)2
#define HERC_scrn (char)3
#define EGA_scrn  (char)4
#define PGA_scrn  (char)5

#define NO_pntr   (char)0
#define EPSN_pntr (char)1
#define HPLJ_pntr (char)2

/*
** Define Structures
*/
struct RData {double y;
              short  f;};

struct TRData {double t;
               double y;};

struct XData {struct complex z;
              short f;}  XData;

struct TXData {double t;
               struct complex z;};

struct FILEHDR {char type;
                double m;
                double b;};

struct DEVICES {char scrn;
                char pntr;
                char pltr;};

struct CATSTRUCT {int N;
                  char *pList;};

struct OFSTRUCT {BYTE    szPathName[128];};

struct FILESTRUCT {char *pCurrent, *pStart;
                   struct OFSTRUCT OFstruct;
                   HANDLE hFile;
                   long lBufferFilePos;
                   WORD wMemorySize, wBytesInBuffer;
                   BOOL bChanged, bReadOnly, bWriteOnly;
                   int iEoF;
                   };
/*
** Define Functions
*/
BOOL zFileOpen(struct FILESTRUCT *, char *, int, WORD);
BOOL zFileClose(struct FILESTRUCT *, BOOL);
WORD zFileGetChar(struct FILESTRUCT *);
long zFileRead(struct FILESTRUCT *, LPSTR, long);
BOOL zFilePutChar(struct FILESTRUCT *, BYTE);
long zFileWrite(struct FILESTRUCT *, LPSTR, long);
long zFileTell(struct FILESTRUCT *);
long zFileSeek(struct FILESTRUCT *, long, int);
WORD zFileGetLine(struct FILESTRUCT *, LPSTR, WORD, BYTE);
long   zFileSize(struct FILESTRUCT *);

struct FILEHDR *zGetHeader(struct FILESTRUCT *, struct FILEHDR *);
BOOL   zPutHeader(struct FILESTRUCT *, struct FILEHDR *);
BOOL   zGetAdverbs(char *);
BOOL   zPutAdverbs(char *);
BOOL   zTaskInit(char *, char *);
char  *zBuildFileName(short,char *);
short  zEncode(short,char *,...);
short  zTypeSize(char);
BOOL   zIndexedRead(struct FILESTRUCT *, long, char *, char);
BOOL   zIndexedWrite(struct FILESTRUCT *, long, char *, char);

struct complex cmplx(double, double);  /* R + iI */
struct complex c_add(struct complex,struct complex);  /* z1 + z2    */
struct complex c_sub(struct complex,struct complex);  /* z1 - z2    */
struct complex c_mul(struct complex,struct complex);  /* z1 * z2    */
struct complex c_div(struct complex,struct complex);  /* z1 / z2    */
struct complex c_sqrt(struct complex);                /* sqrt(z)    */
struct complex c_ln(struct complex);                  /* ln(z)      */
struct complex c_exp(struct complex);                 /* exp(z)     */
struct complex c_sin(struct complex);                 /* sin(z)     */
struct complex c_cos(struct complex);                 /* cos(z)     */
struct complex c_tan(struct complex);                 /* tan(z)     */
struct complex c_sec(struct complex);                 /* sec(z)     */
struct complex c_csc(struct complex);                 /* csc(z)     */
struct complex c_cot(struct complex);                 /* cot(z)     */
struct complex c_asin(struct complex);                /* asin(z)    */
struct complex c_acos(struct complex);                /* acos(z)    */
struct complex c_atan(struct complex);                /* atan(z)    */
struct complex c_acot(struct complex);                /* acot(z)    */
struct complex c_acsc(struct complex);                /* acsc(z)    */
struct complex c_asec(struct complex);                /* asec(z)    */
struct complex c_sinh(struct complex);                /* sinh(z)    */
struct complex c_cosh(struct complex);                /* cosh(z)    */
struct complex c_tanh(struct complex);                /* tanh(z)    */
struct complex c_sech(struct complex);                /* sech(z)    */
struct complex c_csch(struct complex);                /* csch(z)    */
struct complex c_coth(struct complex);                /* coth(z)    */
struct complex c_asinh(struct complex);               /* asinh(z)   */
struct complex c_acosh(struct complex);               /* acosh(z)   */
struct complex c_atanh(struct complex);               /* atanh(z)   */
struct complex c_acsch(struct complex);               /* acsch(z)   */
struct complex c_asech(struct complex);               /* asech(z)   */
struct complex c_acoth(struct complex);               /* acoth(z)   */
struct complex c_log10(struct complex);               /* log10(z)   */
struct complex c_pow(struct complex,struct complex);  /* pow(z1,z2) */

double asinh(double);
double acosh(double);
double atanh(double);

struct FILEHDR *Zgethead(FILE *,struct FILEHDR *);
short           Zputhead(FILE *,struct FILEHDR *);
FILE           *Zopen(char *,short);
short           Zclose(FILE *);
char           *ZBuildFileName(short,char *);
short           Zgetdat(FILE *,char *,char);
short           Zgetadv(char *);
short           Zputadv(char *);
short           Ztskinit(char *,char *);
short           Zputdat(short,FILE *,char *,char);
short           Znameout(char *,char *,short);
void            Zerror(void);
short           Zencode(short,char *,...);
char           *Zread(FILE *,char *,char);
short           Zwrite(FILE *,char *,char);
short           Zsize(char);
struct CATSTRUCT *ZCatFiles(char *);

void  BEEP(void);

short BREAKREQ(void);

/*
** Define Adverbs
*/
double TRANGE[2], YRANGE[2], ZRANGE[2];
double TMAJOR[2], YMAJOR[2], ZMAJOR[2];
double PARMS[10], POINT[2], ZFACTOR[2];
short WINDOW[4];
char TASKNAME[9];
char INNAME[_MAX_FNAME],  INCLASS[_MAX_EXT-1], INPATH[_MAX_DRIVE+_MAX_DIR-1];
char IN2NAME[_MAX_FNAME], IN2CLASS[_MAX_EXT-1], IN2PATH[_MAX_DRIVE+_MAX_DIR-1];
char IN3NAME[_MAX_FNAME], IN3CLASS[_MAX_EXT-1], IN3PATH[_MAX_DRIVE+_MAX_DIR-1];
char OUTNAME[_MAX_FNAME], OUTCLASS[_MAX_EXT-1], OUTPATH[_MAX_DRIVE+_MAX_DIR-1];
char TFORMAT[24], YFORMAT[24], ZFORMAT[24];
char PARITY[6], DEVICE[5];
char TLABEL[81], YLABEL[81], ZLABEL[81], TITLE[81];
double FACTOR;
short CODE, BAUD, STOPBITS, DATABITS, ITYPE, ITYPE;
short TMINOR, YMINOR, ZMINOR, FRAME, BORDER, QUIET;
long COLOR;

struct DEVICES HARDWARE;
short INTERRUPT;
short LogFlag;
FILE *LogStream;

/******************************************
** Arrays for the Full Path for the TISAN
** Program on startup so wew know where to
** find the INPUTS and RUN files.
*/
char TisanDrive[_MAX_DRIVE] = "";
char TisanDir[_MAX_DIR] = "";

/*
** Character strings used throughout the program
*/
char *NullString = "";
char *Space = " ";
char *CrLf = "\n";

/*********************************************************************
*
*  matherr capture routine
*
*/
int matherr(x)
struct exception *x;
   {
   extern char TASKNAME[];
   printf("%-8s: ",TASKNAME);
   x->retval = 0;
   switch (x->type)
        {
        case DOMAIN:
             printf("Domain Error");
             break;
        case SING:
             printf("Singularity");
             break;
        case OVERFLOW:
             printf("Overflow");
             break;
        case UNDERFLOW:
             printf("Underflow");
             break;
        case TLOSS:
             printf("Total Significance Loss");
             break;
        case PLOSS:
             printf("Partial Significance Loss");
             break;
        default:
             printf("Undefined Error");
        }
   printf(" in Function '%s'\n",x->name);
   return(1);
   }