#if ( defined DJ || defined EMXOS2 )      /* a.r. */
        #define unix
#endif
#ifdef EMXOS2
        extern int pmFlag;
#endif

#include "all.h"
#include <math.h>
#include <time.h>

#if (defined M_PI && !defined PI)
#define PI M_PI
#define pi M_PI
#elif defined PI
#define pi PI
#else
#define PI 3.141592653
#define pi 3.141592653
#endif

#define true (!false)
#define false 0
#define dbg if (gle_debug>0)
extern int done_open;
extern int gle_debug;
extern int trace_on;
extern int netxt;
extern int this_line;
int32 *(*gpcode)[];   /* gpcode is a pointer to an array of poiter to int32 */
int32 (*gplen)[];   /* gpcode is a pointer to an array of int32 */
int ngpcode=0;
extern int ngtxt;
extern char *(*gtxt)[];
int ngerror;
extern int last_line;
char *line(int i);
int pass_checkmode(void);
int expand_pcode(int i, int *j);
char *dr_nextline(int *srclin);
int dr_init(void);
int getch(void);
int fner(char *s);
int abort_flag;
static char inbuff[300];
int var_def(char *s, double x);
/*---------------------------------------------------------------------------*/
gle_redraw()
{
static char xinbuff[300];
char source[20];
static char *tk[500];
static char tkbuff[500];
int ntok,f;
int32 time1,time2;
static int32 pcode[500];
double oval;
int maxpcode=0;
int plen,cp,otyp,ii,i,j,al,endp;
FILE *fptr;
char space_str[] = " ";
char fname[80],*s;
        ngpcode = 0;
        expand_pcode(ngtxt,&maxpcode);

        token_space();
        /* Now compile the file into pcode */
        for (i=0;i<500;i++) tk[i] = space_str;
        abort_flag = false;
        ngerror = 0;
        netxt = 0;
        last_line = 0;
        fner("CGLE V3.3h,  Compiling...");

        mark_clear();
        sub_clear();
        for_init();
        f_init();
        dr_init();
/*        var_def("PI",3.14159265); */
        var_def("PI",3.14159265);
        done_open = false;
        for (;(s = dr_nextline(&i)) != NULL;) {
                strcpy(xinbuff,s);
and_again:        al = strlen(xinbuff);
                if (xinbuff[al-1]=='&') {
                        xinbuff[al-1] = 0;
                        strcat(xinbuff, dr_nextline(&i));
                        if (strlen(xinbuff)>290) printf("Input line too int32\n");
                        goto and_again;
                }
                if (xinbuff[0]==26) xinbuff[0] = 0;
                strcpy(inbuff,xinbuff);
                this_line = i;
                if (trace_on) gprint("Source | %s \n",xinbuff);
                token(xinbuff,&tk,&ntok,tkbuff);
                plen = 0;
                passt(ngpcode+1,inbuff,&tk,&ntok,pcode,&plen);
/*
                for (ii=0;ii<plen;ii++) {
                printf("%x ",pcode[ii]);
                }
                printf("\n");
*/
                if (ngpcode > maxpcode) expand_pcode(ngpcode*2,&maxpcode);
                (*gpcode)[++ngpcode] = myallocz(plen*sizeof(char *));
                (*gplen)[ngpcode] = plen;
                memcpy((*gpcode)[ngpcode],&pcode,plen*sizeof(char *));
                if (abort_key()) goto do_abort;
        }
        pass_checkmode();

        time(&time2);
        fner("CGLE V3.3h,  Running...");
        if (ngerror>0) {
                gprint("Abort (Y/N)  [Y] \n");
               #ifdef EMXOS2            /* a.r.- prevents locking OS/2 */
                if (pmFlag == 0) {
                        if (tolower(text_inkey())!='n') 
                                return;
                        }
               #else
                if (tolower(text_inkey())!='n') return;
               #endif
        }
        for (i=1;i<=ngpcode;i++) {
                if (abort_key()) goto do_abort;
                this_line = i;
                if (trace_on) gprint("Trace |%s\n",line(i));
                dbg gprint("Call do_pcode[%d] \n",i);
/*                printf("Pcode pointer %p %d \n",(*gpcode)[i],i);*/
                do_pcode(&i,(*gpcode)[i],(*gplen)[i],&endp);
        }
do_abort:
        g_close();

        for (i=1;i<=ngpcode;i++) {
                this_line = i;
                if ( (*gpcode)[i] != NULL) myfrees( (*gpcode)[i] ,"gpcode");
        }

}
/*--------------------------------------------------------------------------*/
glezzz()
{
        printf("~~~~~~~~~~~~~~~~~~Pcode pointer %p 2 \n",(*gpcode)[2]);
}
gle_include(char *s)
{
        gprint("include file (not implemented ) {%s} \n",s);
}
/*--------------------------------------------------------------------------*/
extern char gleroot[];
char *gle_top()
{
        static char s[80];
#ifdef __TURBOC__
        return gleroot;
#elif ( defined DJ || defined EMXOS2 || defined unix)
        strcpy(s,gleroot);
        strcat(s, "/");
        return s;
#else
        return "cgle_top:";
#endif
}
char *gledir(char *s);
char *bgidir()
{
#ifdef __TURBOC__
        char *s;
        s = getenv("GLE_BGI");
        if (s==NULL) {
                s = gledir("");
        }
        return s;
#endif
}
char *fontdir(char *fname)
{
        static char fbuff[80];
#if ( defined DJ || defined EMXOS2 || defined unix)
        strcpy(fbuff,gle_top());
        strcat(fbuff,"font/");
#elif __TURBOC__
        strcpy(fbuff,gle_top());
        strcat(fbuff,"font\\");
#else
        strcpy(fbuff,"cgle_top:");
#endif
        strcat(fbuff,fname);
        return &fbuff[0];
}

char *libdir(char *fname)
{
        static char fbuff[80];
#if ( defined DJ || defined EMXOS2 || defined unix)
        strcpy(fbuff,gle_top());
        strcat(fbuff,"lib/");
#elif __TURBOC__
        strcpy(fbuff,gle_top());
        strcat(fbuff,"lib\\");
#else
        strcpy(fbuff,"cgle_top:");
#endif
        strcat(fbuff,fname);
        return &fbuff[0];
}

char *usrlibdir(char *fname)
{
        char *s;
        static char fbuff[80];

        s = getenv("GLE_USRLIB");
        strcpy(fbuff,s);
#if ( defined DJ || defined EMXOS2 || defined unix)
        strcat(fbuff,"/");
#elif __TURBOC__
        strcat(fbuff,"\\");
#else
        strcpy(fbuff,"cgle_top:");
#endif
        strcat(fbuff,fname);
        return &fbuff[0];
}

char *gledir(char *fname)
{
        static char fbuff[80];
#if ( defined DJ || defined EMXOS2 || defined unix)
        strcpy(fbuff,gle_top());
#elif __TURBOC__
        strcpy(fbuff,gle_top());
        strcat(fbuff,"exe\\");
#else
        strcpy(fbuff,"cgle_top:");
#endif
        strcat(fbuff,fname);
        return &fbuff[0];
}
static int dri;
static ninc=0;
static FILE *incptr[10];
include_file(char *fname)
{
        incptr[++ninc] = fopen(fname,"r");
        if (incptr[ninc]==NULL)
                   incptr[ninc] = fopen(libdir(fname),"r");
        if (incptr[ninc]==NULL)
                   incptr[ninc] = fopen(usrlibdir(fname),"r");
        if (incptr[ninc]==NULL) {
                gprint("Unable to open input file {%s} \n",fname);
                gprint("I searched in the directories: current directory %s %s \n",
                       libdir(""), usrlibdir(""));
                perror("Reason"); ninc--;
        }
}
dr_init()
{
        dri = 1;
}
char *dr_nextline(int *srclin)
{
        int i;
try_again:;
        if (ninc==0) {
                if (dri>ngtxt) return NULL;
                *srclin = dri;
                return (*gtxt)[dri++];
        } else {
                if (feof(incptr[ninc])) goto close_file;
                if (fgets(inbuff,295,incptr[ninc])==NULL) goto close_file;
                i = strlen(inbuff);
                if (inbuff[i-1]=='\n') inbuff[i-1] = 0;
                return inbuff;
        }
close_file:
        fclose(incptr[ninc--]);
        goto try_again;
}
expand_pcode(int ngtxt,int *maxpcode)
{
        int32 *a,*b;
        a = myallocz((ngtxt+10)*sizeof(char *));
        b = myallocz((ngtxt+10)*sizeof(char *));
        if (gpcode!=0) {
                memcpy(a,gpcode,(*maxpcode + 4)*sizeof(char *));
                memcpy(b,gplen,(*maxpcode + 4)*sizeof(char *));
                myfrees(gpcode,"gpcode2");
                myfrees(gplen,"gplen");
        }
        gpcode = (int32 *(*)[]) a;
        gplen = (int32 (*)[]) b;
        *maxpcode = ngtxt+1;
}
run_bigfile(char *fname)
{
        FILE *fptr;
        char inbuff[200],s[200];
        char *tk[300];
        char tkbuff[300];
        int32 pcode[300];
        int plen,ntok,i,endp;

        fptr = fopen(fname,"r");
        if (fptr==NULL) {
                gprint("Unable to open bigfile {%s} \n",fname);
                perror("Reason");
                return;
        }
        for (;!feof(fptr);) {
         if (fgets(inbuff,190,fptr)!=NULL) {
                strcpy(s,inbuff);
                i = strlen(inbuff);
                if (inbuff[i-1]=='\n') inbuff[i-1] = 0;
                if (trace_on) gprint("Source | %s \n",inbuff);
                token(inbuff,&tk,&ntok,tkbuff);
                plen = 0;
                passt(ngpcode+1,s,&tk,&ntok,pcode,&plen);
                do_pcode(&i,pcode,plen,&endp);
         }
        }
        fclose(fptr);
}

