#include "all.h"
#include <errno.h>
#include <math.h>
#ifdef __TURBOC__
#define HLINECHAR 205
#include "bios.h"
#include "conio.h"
#else
#define ENOFILE 999
#define HLINECHAR '-'
#include "vaxconio.h"
#endif
#include "edt.h"
int vax_edt(char *s);
int delay(int i);
extern int gle_debug;
int gle_speed;
int gle_nspeed;
int trace_on;
#define true (!false)
#define false 0
#define dbg if (gle_debug>0)
int32 mem_total(void);
int pick_file(char *r, char *w);
typedef char (*TEXT)[];
int scr_blackwhite;
int scr_left(int i);
int scr_right(int i);
int scr_norm(void);
int scr_inv(void);
int scr_grey(void);
int text_search(void);
int text_deleol(void);
int text_undeleol(void);
int scr_savexy(void);
int scr_restorexy(void);
int scr_gets(char *s);
int text_showerror(void);
int text_findnext(void);
int text_changed(void);
int read_command(char *ans, char *ques);
int read_str(char *s);
int text_load(char *fname);
int text_free(void);
int do_help(char *s);
int int_edt(char *fname);
int vinsert(int y, char *s);
int vdelete(int y);
int menu(void);
int gle_redraw(void);
int scrinsert(int y);
int scrdeleteline(int y);
int text_save(void);
int text_eol(void);
int text_delright(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_saveas(void);
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 lineset(int i,char *ss);
int lineinsert(int y, char *ss);
int textinsert(int y);
char *sline(int i);
char *line(int i);
int ncpy(char *d, char *s, int n);
/*------------ GLOBAL VARIABLES --------------*/
extern int32 gtotalmem;
int max_ngtxt;
extern int ngtxt;
extern char *(*gtxt)[];   /* gtxt is a pointer to an array of poiter to char */
char *(*ctxt)[];	/* cut buffer */
int ncut;
int iserr;
int w_top=0,w_bot=0,w_len,w_cl,w_cx;
#define MAXLINES (max_ngtxt-10)
extern int ngtxt;
int move_pos,onmove;
int ii;
char gbuff[255];
char gbuff2[255];
int selx,sely;
extern char input_file[];
int changed;
int scr_isblackwhite(void);
extern int netxt;	/* errors which occured */
extern char *(*etxt)[];
/*---------------------------------------------------------------------------*/
/* copy gtxt, ngtxt   to local paramters */
int tfx,tfy;
int_edt(char *fname)
{
}
int text_expand(int n);
text_expand(int n)
{
	char **a;
	max_ngtxt = n;
	a = myallocz(4*n);
	if (gtxt!=NULL) {
		memcpy(a,gtxt,(ngtxt+1)*sizeof(char*));
		myfrees(gtxt,"textexpand");
	}
	gtxt = a;
}
text_load(char *fname)
{
static char inbuff[200];
FILE *fptr;
int i;
char emess[80];
	changed = false;
	strcpy(input_file,fname);
	fptr = fopen(fname,"r");
	if (fptr==NULL) {
		if (errno == ENOFILE) {
		sprintf(emess,"Input file does not exist (%s) ",fname);
		fner(emess);
		delay(1000);
		} else {
			gprint("Unable to open input file {%s} \n",fname);
			perror("Reason");
			fner("Press return to continue");
			text_inkey();
		}
	} else {
	 for (;!feof(fptr);) {
		if (fgets(inbuff,200,fptr)!=NULL) {
			i = strlen(inbuff);
			if (inbuff[i-1]=='\n') inbuff[i-1] = 0;
			ngtxt++;
			if (ngtxt>(max_ngtxt-300)) {
				text_expand(2*(ngtxt+500));
			}
			(*gtxt)[ngtxt] = myalloc(i+1);
			strcpy((*gtxt)[ngtxt],inbuff);
		}
	 }
	 fclose(fptr);
	}
	if (ngtxt==0) {
		(*gtxt)[++ngtxt] = sdup("size 24 18");
		(*gtxt)[++ngtxt] = sdup("");
	}
	tfx = 1;
	tfy = 1;
}
char errgle[90];
int dont_clear;
gle_abort(char *s)
{
	d_tidyup();
	delay(1000);
	dont_clear = true;
	window(1,1,80,25);
	scr_norm();
	clrscr();
	printf("\n\n\n\n %s \nGle is aborting, Attempting to save file in abort.gle\n",s);
	strcpy(input_file,"abort.gle");
	text_save();
	printf("File is now saved (hopefully)\n");
	if (strstr(s,"alloc")!=NULL  || strstr(s,"Init")!=NULL) {
		printf("\nGLE is short of memory!!\n\n");
		printf("If you are using very large datasets you should try\n");
		printf("the BIGFILE option, otherwise you probably have too\n");
		printf("many memory resident programs on your pc and to make GLE work\n");
		printf("you should remove these from your CONFIG.SYS and AUTOEXEC.BAT\n");
		printf("\n  (make coppies of these files before altering them)\n");
	}
	exit(1);
}
char *function_bar(void);
fner_clear()
{
	if (iserr==false) return;
	if (dont_clear) return;
	window(1,1,80,25);
	gotoxy(1,25);
	scr_grey();
	clreol();
	gotoxy(2,25);
	cputs(function_bar());
	scr_norm();
	iserr = false;
	window(2,3,79,23);
}
fner(char *s)
{
	if (dont_clear) return;
	scr_savexy();
	iserr = true;
	window(1,1,80,25);
	gotoxy(1,25);
	scr_inv();
	clreol();
	gotoxy(2,25);
	cputs(s);
	scr_norm();
	scr_restorexy();
	scr_refresh();
}
text_save()
{}
char *line(int i)
{
	static char xxx[2]="";
	static char eob[7]="[EOB]";
	if (i>ngtxt) {
		if (i==ngtxt+1) return &eob[0];
		else return &xxx[0];
	}
	return (*gtxt)[i];
}

