#include "all.h"
#include <math.h>
#ifdef __TURBOC__
#include "conio.h"
#include "bios.h"
#endif
#if ( defined DJ || defined EMXOS2 )      /* a.r. */
        #define unix
#endif
#ifdef EMXOS2
        int flagSURFOS2 = 0;
        FILE *ErrFileSURFOS2;
        int pmFlag;
        extern unsigned long stopPMSession(unsigned long SessID);
        extern unsigned long oldSession;
#endif

int gle_debug;
int dev_eps=0;
int control_d = !0;
int MAX_VECTOR=500;
int dev_fill,dev_font; /* do real filling for d_dvi.c, and use texcmr font */
extern int trace_on;
int init_memory(void);
int text_expand(int n);
int g_get_type(char *s);
int gle_redraw(void);
int scr_init(void);
int scr_end(void);
#define true (!false)
#define false 0
int noscreenio=true;
int BLACKANDWHITE=(false);
#define dbg if (gle_debug>0)
#ifdef __TURBOC__
        extern unsigned _stklen=45000;
#endif

typedef char (*TEXT)[];
int int_edt(char *fname);
int scrinsert(int y);
int scrdeleteline(int y);
int text_eol(void);
int text_delete(void);
int text_undelete(void);
int text_deleteline(void);
int text_left(void);
int text_right(void);
int text_up(void);
int text_down(void);
int text_return(void);
int text_refresh();
int text_putstr(char *s);
int text_setwindow();
int text_move(int x,int y);
int scr_tab(char *s, int x);
int scr_negtab(char *s, int x, int *fpos, int *scrx);
int fner(char *s);
int fner_clear(void);
int text_setwindow(void);
int text_refresh(void);
int text_main(void);
int text_inkey(void);
int text_scroll(void);
int text_scroll_up(void);
int text_scroll_down(void);
int text_select(void);
int text_cut(void);
int text_paste(void);
int restofline(int y,int x);
int normal_key(int c);
int setoutfile(char *s);
int lineset(int i,char *ss);
int lineinsert(int y, char *ss);
int textinsert(int y);
char *sline(int i);
int text_load(char *f);
int print_out(void);
char *line(int i);
int ncpy(char *d, char *s, int n);
/*------------ GLOBAL VARIABLES --------------*/
char input_file[50];
char *(*gtxt)[];   /* gtxt is a pointer to an array of poiter to char */
int ngtxt=0;
char gleroot[60];
char glearg0[66];
int gotfile;
int hpgl_size;


/*---------------------------------------------------------------------------*/
#ifdef unix
#include "glepath.h"
#endif
main(int argc, char **argv)
{
char dtype[60];
char fname[60],*ss;
static int i,ask_debug,spawn_print,call_menu,drawit;
        strcpy(fname,"test.gle");
        strcpy(glearg0,argv[0]);
        strcpy(gleroot,argv[0]);
        init_memory(); /* saves some memory for emergencies */
#ifdef VMS
        if (strcmp(strupr(getsymbol("GLE_NOCONTROLD")),"TRUE")==0) {
                control_d = false;
        }
#endif
#ifdef unix
        dtype[0] = 0;
        ss = getenv("GLE_TOP");
        if (ss==NULL) ss = GLEPATH;
        strcpy(gleroot,ss);
        control_d = false;
#endif
#ifdef __TURBOC__
        ss = strchr(gleroot,'\\');
        if (ss==NULL) gle_abort("Unable to locate files AGRV[0] wrong");
        for (;strchr(ss+1,'\\')!=NULL;) ss = strchr(ss+1,'\\');
        *(ss+1) = 0;
        if (getenv("GLE_NOCONTROLD")!=NULL) control_d = false;
#endif
#ifdef VAXC
        if (strcmp(getsymbol("GLE_NOCONTROLD"),"TRUE")==0) control_d = false;
#endif
        hpgl_size = 3;
        for (i=1;i<argc;i++) {
                strcpy(dtype,argv[i]);
                strupr( argv[i] );
                if (strcmp( argv[i] , "/TRACE")==0) trace_on = true;
                else if (strcmp( argv[i] , "/DEBUG")==0) ask_debug = true;
                else if (strcmp( argv[i] , "/PRINT")==0) spawn_print = true;
                else if (strncmp( argv[i] , "/DEV", 4)==0) spawn_print = false;
                else if (strcmp( argv[i] , "/NOD")==0) control_d = false;
                else if (strcmp( argv[i] , "/ADDD")==0) control_d = true;
                else if (strcmp( argv[i] , "/A0")==0) hpgl_size = 0;
                else if (strcmp( argv[i] , "/A1")==0) hpgl_size = 1;
                else if (strcmp( argv[i] , "/A2")==0) hpgl_size = 2;
                else if (strcmp( argv[i] , "/A3")==0) hpgl_size = 3;
                else if (strcmp( argv[i] , "/A4")==0) hpgl_size = 4;
                else if (strcmp( argv[i] , "/MENU")==0) call_menu = true;
                else if (strcmp( argv[i] , "/EPS")==0) dev_eps = true;
                else if (strcmp( argv[i] , "/FILL")==0) dev_fill = true;
                else if (strcmp( argv[i] , "/FONT")==0) dev_font = true;
                else if (strcmp( argv[i] , "/NOMAXPATH")==0) MAX_VECTOR = 999999;
                else if (strcmp( argv[i] , "/COLOR")==0) BLACKANDWHITE = false;
                else if (strcmp( argv[i] , "/NOCOLOR")==0) BLACKANDWHITE = false;
                else if (strncmp( argv[i] , "/OUT", 4)==0) setoutfile(argv[i]);
                else if (strncmp( argv[i] , "/DRA", 4)==0) drawit = true;
                else if (!gotfile && (isalnum(*argv[i]) || (*argv[i] == '['))) {strcpy(fname, dtype); gotfile=true;}
                else printf("Unrecognised qualifier {%s} \n",argv[i]);
        }
/*        if (argc>1) strcpy(fname,*(++argv)); else strcpy(fname,"test.gle");*/
        g_get_type(dtype);
        /* works here */
        
#ifdef EMXOS2 /* a.r. - prevent locking PM single Message Queue */
        if (strstr(dtype, "OS2PM")!=NULL && drawit == true)
               {
                pmFlag = 1;
                if (!gotfile) exit(1);
               }
        else
                pmFlag = 0;
#endif

        if (strstr(dtype,"HARDCOPY")!=NULL  && !call_menu) {
                if (!gotfile) {
try_again:                gprint("Enter name of GLE file ? ");
                        gets(fname);
                        ss = strchr(fname,'\n');
                        if (ss!=NULL) *ss = 0;
                        if (strcmp(fname,"")==0) goto try_again;
                }
        }

        if (strchr(fname,'.')==NULL) strcat(fname,".gle");
/* works*/
        if (strstr(dtype,"HARDCOPY")==NULL  || call_menu) {
                noscreenio = false;
                scr_init();
        }
        strcpy(input_file,fname);
        text_expand(500); /* Initially allocate 500 lines of text */

        if (strstr(dtype,"HARDCOPY")!=NULL  && !call_menu) {
                text_load(fname);
                if (ask_debug) {
                printf("Debug options 16=do_pcode, 8=pass 4=polish, 2=eval ");
                printf("Debug ");scanf("%d",&gle_debug);
                printf("Trace "); scanf("%d",&trace_on);
                }
                gle_redraw();
        } else {
                noscreenio = false;
                if (coreleft()<120000L) {
 printf("\n\nThere is not enough memory for GLE to run properly, follow these guidelines\n");
 printf("to free up more memory.\n");
 printf("        1) Don't run GLE from inside editors/file utilities.\n");
 printf("        2) Don't run GLE at the same time as disk servers like:\n");
 printf("                NOVELL, PCSA, VDISK, RAF etc...\n");
 printf("        3) Remove any extra's from CONFIG.SYS and AUTOEXEC.BAT\n");
 printf("                (after taking copies of them)\n\n");
 printf("        (GLE will attempt to continue by not using any complex fonts)\n");
 printf("Press any key to continue");
                        font_simple();
                        if (!drawit) text_inkey();
                }
                if (gotfile) text_load(fname);
                if (drawit) gle_redraw();
                else int_edt(fname);
        }
        if (!noscreenio) scr_end();
/*         if (spawn_print) {
                print_out();
        } */


#ifdef EMXOS2                                           /* a.r. */
        stopPMSession(oldSession);
        unlink("GLEOS2.TMP");
        if (drawit != true)
                unlink("GLEOS2.ERR");
#endif
#ifdef VMS
        return 1;
#else
        return 0;
#endif
}
#ifdef unix
print_out(){}
#endif
#ifdef __TURBOC__
print_out()
{}
#else
#ifdef VMS
#include <descrip.h>
/* #include <lib$routines.h> */
print_out()
{
        static $DESCRIPTOR(cmd_desc
          ,"print out.ps /que=laser/notify/noburst/noflag");

        printf("$ %s \n",cmd_desc.dsc$a_pointer);
        LIB$DO_COMMAND(&cmd_desc);
}
#endif
#endif
char output_file[80];
setoutfile(char *q)
{
        char *s;
        s = strchr(q,'=');
        if (s==NULL) return;
        strcpy(output_file,s+1);
        strlwr(output_file);
}





