/* ---------------------------- Ue2.c ------------------------- */

#include "uedit.h"

static UCHAR helpName[22];

static short minMax[120] = {5,255, 		/* pmESC  0,0 means ANY VAL LEGAL */
					0,0, 0,0, 0,1, 0,1, 0,1,				/* userGlobalA */
					0,1, 0,3, 0,1, 0,1, 0,1,				/* markGadgets */
					1,100, 0,1, 0,1, 5,255, 5,255,  	/* maxFiles */
					5,255, 5,255, 0,2, 0,2, 0,100,  	/* multiWild */
					0,0, 0,0, 0,1, 0,1, 0,1,			/* RESERVED (l) */
					0,1, 0,1, 0,1, 0,3, 1,32767,	/* smartTabs */
					0,1, 0,1, 0,1, 640,1064, 200,1064,  /* hide display */
					-128,127, 0,0, 0,0, 0,0, 0,0,		/* reserved */
					0,1, 0,1, 0,1, 0,1, 0,0,			/* trueTabs */
					0,1, 0,1, 0,1, 0,0, 0,1,				/* wordWrap */
					0,1, 0,4, 1,9999, 2,100, 0,255, 	/* userLocalB */
					0,255, 0,255, 1,255, 10,13};		/* topMargin */


UCHAR	*fileName(char	*x,
				  char  *y)
{
register UCHAR *z;
static  UCHAR	temp[128];

	z = temp;
	strcpy(z,x);
	strcat(z,y);
	return(z);
}

void info(char *x)
{
register	UCHAR	*temp;

	if (!icons) return;
	temp=fileName(x,".info");
	if (findFile(temp)) return;
	copyFile(fileName(sss,"NoName.info"),temp);
}

void showKeyHelp(register short  macroNum)
{
register UCHAR  *x;
register BPTT   fdKeyHelp;
UCHAR			z[MAXLINECHARS];
USHORT  	    pos;

	if (macroNum<1 || primitive) return;
	fdKeyHelp = (BPTT)NULL;
	if (cmdTable[macroNum]==NULL) goto NOTUSED;
	x = rv;
	fdKeyHelp = (BPTT)openOld(x);
	if (fdKeyHelp==NULL) {
		if (!findFile(helpName)) goto OUT;
		copyFile(helpName,x);
		fdKeyHelp = (BPTT)openOld(x);
		if (fdKeyHelp==NULL) {
OUT:
			x = (UCHAR *)"No key-help file";
			teachKeys = 0;
			goto OUT2;
		}
	}
	Seek((BPTR)fdKeyHelp,((long)macroNum << 1L),(long)OFFSET_BEGINNING);
	if (Read((BPTR)fdKeyHelp,&pos,2L)==2L && pos>0) {
		x = z;
		Seek((BPTR)fdKeyHelp,(long)pos,(long)OFFSET_BEGINNING);
		Read((BPTR)fdKeyHelp,x,80L);
		x[80] = 0x00;
	} else {
NOTUSED:
		x = (UCHAR *)"No help or unused.";
	}
OUT2:
	oneMsg(x);
	if (fdKeyHelp!=NULL) Close((BPTR)fdKeyHelp);
}

void closeFdKeyHelp()
{
register	UCHAR	*x;

	Close((BPTR)fdKeyHelp);
	x = rv;
	copyFile(x,helpName);
	DeleteFile(x);
	fdKeyHelp = (BPTT)NULL;
}

void readKeyHelp(register char	*x)
{
register short i;
register UCHAR *fp;

	*x=0x00;
	if (!getDelimiter('<')) return;
	incFast();
	sLineFast();
	sLineFast();
	fp = fastP;
	i=0;
	while (*fp!=eol && *fp!=12 && i++<80) { *(x++) = *fp; fp = incFast(); }
	*x=0x00;
}

void storeKeyHelp(register char	*x,
				  register short restarting)
{
register	USHORT  i;
register	UCHAR	*hname;
USHORT  			l;

	if (*x==0x00 || macNum >= (MAXRAW * MAXMODIFIER)) return;
	if (fdKeyHelp==NULL) {
		hname = rv;
		DeleteFile(hname);
		if ( (fdKeyHelp=openNew(hname))==NULL) {
BADOUT:
			makeKeyHelp=0L;
			return;
		}
		l = 0;
		for (i=0; i<(MAXRAW * MAXMODIFIER); i++) Write((BPTR)fdKeyHelp,&l,2L);
		keyHelpPos = MAXRAW * (MAXMODIFIER << 1);
	}
HITME:
	Seek((BPTR)fdKeyHelp,(long)(macNum << 1L),(long)OFFSET_BEGINNING);
/* ====== new new new new new new ============= */
	if (restarting) {
		Read((BPTR)fdKeyHelp,&l,2L);
		if (l>0) keyHelpPos = l;
		restarting = 0;
		goto HITME;
	}
	Write((BPTR)fdKeyHelp,&keyHelpPos,2L);

	Seek((BPTR)fdKeyHelp,(long)keyHelpPos,(long)OFFSET_BEGINNING);
	i = strlen(x) + 1;
	Write((BPTR)fdKeyHelp,x,(long)i);
	keyHelpPos += i;
}

findFile(register UCHAR *x)
{
register BPTT fd;

	if (x==NULL || *x<=' ') goto BRTN;
	fd = (BPTT)openOld(x);
	if (fd==NULL) goto BRTN;
	Close((BPTR)fd);
	return(TRUE);
BRTN:
	return(FALSE);
}

void versionMsg(UCHAR *x)
{
	passiveMsg("Wrong REV,",x,0L);
}

loadKeyData()
{
register BPTT       fd;
register short  	i,defaults,makeHelp,restarting;
short				rtn;
long				saveLearn,dummy;
char				keyHelpText[82];

	rtn = FALSE;
	if (configPtr!=NULL) goto NEWRELEASE;			 /* -c flag was used */

	if (!findFile(dataName)) strcpy(dataName,fileName(sss,"Data!"));
	fd = (BPTT)openOld(dataName);
	if (fd==NULL && !startingUp) goto RTN;   /* don't read config */
	if (fd!=NULL) {
		Read((BPTR)fd,&dummy,4L);
		if (dummy!=VERSION) {
			Close((BPTR)fd);
			if (startingUp) goto NEWRELEASE;
			versionMsg(dataName);
			goto RTN;
		}

/* new new new for font */

		Read((BPTR)fd,fontName,20L);
		Read((BPTR)fd,&fontHeight,2L);
		
		Read((BPTR)fd,helpName,20L);
		saveLearn = learnMode;
		for (i=0; i<MAXSUCH; i++) Read((BPTR)fd,such[i],4L);
		eol = eoline; learnMode = saveLearn;

		TinyWindow.TopEdge = tinyCoords >> 16L;
		TinyWindow.LeftEdge = tinyCoords & 0x0000ffff;
		Read((BPTR)fd,tabTables[0],(long)sizeof(tabTables));

		Read((BPTR)fd,charMappings,(long)sizeof(charMappings));
		Read((BPTR)fd,bufferFlags,(long)sizeof(bufferFlags));
		Read((BPTR)fd,printKeys,(long)sizeof(printKeys));
		Read((BPTR)fd,altKeys,(long)sizeof(altKeys));
		Read((BPTR)fd,coloring,(long)sizeof(coloring));
		Read((BPTR)fd,gadgetName[0],88L);
		Read((BPTR)fd,rv,20L);
		Read((BPTR)fd,&foldTests,(long)sizeof(foldTests));
		Read((BPTR)fd,&maxTest,2L);
		killMenus();
		wipeCmds();
		Read((BPTR)fd,cmdLens,(long)sizeof(cmdLens));
		for (i=0; i<MAXMACROS; i++) {
			if (cmdLens[i]==0) continue;
			if ( (cmdTable[i] = (UCHAR *)safeMem((long)cmdLens[i]))==NULL) break;
			Read((BPTR)fd,cmdTable[i],(long)cmdLens[i]);
		}
		Close((BPTR)fd);
		passiveMsg("Loaded",dataName,0L);
		goto RETURNTRUE;
	}
NEWRELEASE:
	upWindow();
	clearMsg();
	soakInputs();

	fixDataName();
	defaults = 0;
	restarting = FALSE;
	if (!getConfig(0)) goto RTN;

STARTOVER:
	FfullRefresh();

longPtrArgs[0] = &dummy;
dummy = curBuf->BufNum;

	if (!FcompileDefaults()) {
JUMP:
		takeFast();
		if (tiny && !closeTiny()) badQuit();
		FbeepDisplay();
		stopPause = 0;
		cursorMid(TRUE);
		oneMsg("shft-Space/-BS=cursor, DEL/BS=delete, ESC=cont, Amiga-ESC=quit");
		primitiveEdit(1);
		popFast();  /* untakeFast(); */
		killMenus();
		if (!defaults) { sFileFast(); takeFast(); goto STARTOVER; }
		restarting = TRUE;
	} else { display(); defaults = 'A' - 1; }
	takeFast();
	i = loud;
	makeHelp = makeKeyHelp;

longPtrArgs[0] = &dummy;
dummy = curBuf->BufNum;

	while(1) {
   		if (makeHelp) readKeyHelp(keyHelpText);
   		if (!Fcompile()) {
			if (*fastP==EOFILE) goto NEXTCMD;
			goto JUMP;
   		}
   		if (i && !tiny) display();
   		if (makeHelp) {
			storeKeyHelp(keyHelpText,restarting);
			restarting = FALSE;
   		}
NEXTCMD:
        if (msgList->lh_Head->ln_Succ) {
            nextEvent();
            if (stopPause) goto JUMP;
            doTiny();
        }
   		if (*fastP==EOFILE) {
DOMORE:
   			if (getConfig(++defaults)) continue;
   			if (defaults<'Z') goto DOMORE;
   			break;
   		}
   	}
	if (fdKeyHelp!=NULL) closeFdKeyHelp();

	stopPause = 0;
	if (yesNo("Save new ",dataName) ) saveUeditDat();
	stopPause = 0;
	configPtr = NULL;

RETURNTRUE:
	setStuff();
	rtn = TRUE;
RTN:
	return(rtn);
}

getConfig(register short flag)
{
register UCHAR  	*cp;
register struct CB  *curB;
register short  	rtn,i;
static UCHAR		configName[NAMELEN+2];

	rtn = FALSE;

	cp = configPtr;
	if (cp==NULL) cp = DFALT_CFG;

	curB = curBuf;
	if (curB!=NULL) {
		if ((curB->Flags & CHANGED) && yesNo("Save ",curB->FileName))
			saveFile(curB);
		freeBuf(curB->BufNum);
		curBuf = NULL;
	}

	if (flag) { 				/* when flag>0, this points to configName */
		i = (short)strlen(cp);
		if (flag=='A') cp[++i] = 0;
		cp[i-1] = flag;
	}

	if (!findFile(cp)) {
		if (cp==DFALT_CFG) cp = fileName(sss,"Config!");
		else goto RTN;
		if (!findFile(cp)) goto RTN;
	}

	if (startFile(cp) < 0 || *fastP==EOFILE) goto RTN;

	if (flag==0) {
		strncpy(configName,cp,(size_t)NAMELEN);
		configPtr = configName;
	}
	popFast();
	FfullRefresh();
	rtn = TRUE;
RTN:
	stopPause = 0;
	return(rtn);
}

FcompileDefaults()
{
register struct CB *bufPtr;

register short	i,rtn,n;
register long	j;
UCHAR			keys[2*MAXPRINTABLES + 1];
long dummy;

	rtn = TRUE;

	i = *longPtrArgs[0];
	bufPtr = buf[i];
	if (bufPtr==NULL) goto FALSERTN;
	pushFast(bufPtr,&bufPtr->Block,&bufPtr->Ptr);

	if (!readVal(&makeKeyHelp,0,1)) goto FALSERTN;

	if (!readString(helpName)) goto FALSERTN;
	if (*helpName==0x00) strcpy(helpName,fileName(sss,"Key-Help"));
	if (!readVal(&loud,0,1)) goto FALSERTN;

	if (!readString(rv)) goto FALSERTN;
	if (*rv==0x00) strcpy(rv,"RAM:uejunk");

	if (!readString(fontName)) goto FALSERTN;
	if (*fontName==0x00) strcpy(fontName,"topaz");
	strncatmax(fontName,".font",20);
	if (!readVal(&dummy,8,32)) goto FALSERTN;
	fontHeight = dummy;

	for (i=0; i<(MAXSUCH - 5); i++) {
		n = i << 1;
		if (!readVal(such[i],minMax[n],minMax[n+1])) goto FALSERTN;
	}
	eol = eoline;

	for (i = 0; i<MAXTABTABLES; i++) if (!getTabTable(i)) goto FALSERTN;

	for (i=0; i<4; i++) if (!readVal(&charMappings[i],128,255)) goto FALSERTN;

	if (!getDelimiter('<')) goto FALSERTN;
	for (i=0; i<MAXPRINTABLES; i++) { keys[i] = *fastP; incFast(); }
	if (*fastP!='>') goto FALSERTN;

	if (!getDelimiter('<')) goto FALSERTN;
	for (i=MAXPRINTABLES; i<2*MAXPRINTABLES; i++){ keys[i] = *fastP; incFast(); }
	if (*fastP!='>') goto FALSERTN;

	if (!getDelimiter('<')) goto FALSERTN;
	for (i=0; i<MAXPRINTABLES; i++){ altKeys[i] = *fastP; incFast(); }
	if (*fastP!='>') goto FALSERTN;

	if (!getDelimiter('<')) goto FALSERTN;
	for (i=MAXPRINTABLES; i<2*MAXPRINTABLES; i++){ altKeys[i] = *fastP; incFast(); }
	if (*fastP!='>') goto FALSERTN;

	for (i=0, j=0L; i<16; i++) {
		if (!readVal(&dummy,0,3)) goto FALSERTN;
		j = (j << 2L) | (dummy & 3L);
	}
	localColor = j;
	
	for (i=16, j=0L; i<32; i++) {
		if (!readVal(&dummy,0,3)) goto FALSERTN;
		j = (j << 2L) | (dummy & 3L);
	}
	screenColor = j;
	
	for (j=0; j<4; j++) for (i=0; i<3; i++) {
		if (!readVal(&dummy,0,15)) goto FALSERTN;
		coloring[i][j] = dummy;
	}
	i = 0;
	while (i<4) {
		if (!readString(gadgetName[i])) {
FALSERTN:
			rtn = FALSE;
			break;
		}
		i++;
	}
	takeFast();

	if (rtn) { killMenus(); setStuff(); }
	else displayFlag |= SHOWCURSOR_; /* splitFlag((short)*longPtrArgs[0],SHOWCURSOR_); */
	if (rtn) movdat((UCHAR *)keys,(UCHAR *)printKeys,(short)(2 * MAXPRINTABLES));

popFast();

	return(rtn);
}

getTabTable(register short i)
{
register short j;
register long  k;

	for (j=0; j<MAXTABS+1; j++) tabTables[i][j] = 0;

	if (!getDelimiter('<')) goto RTN;
	j = 0;
	while (1) {
		if (!skipSpace() || *fastP=='>')	break;
		if (j>=MAXTABS || !getIntArg()) goto RTN;
		k = dummy;
		if (k<1L || k>255L)  goto RTN;
		tabTables[i][j++] = k;
	}
	if (j>0) sortTabs(i);
	return(TRUE);
RTN:
	return(FALSE);
}

fixDataName()
{
	if (*dataName<=' ') strcpy(dataName,DFALT_DAT);
}

saveUeditDat()
{
register short  i;
register BPTT   fd;
long dummy;

	fixDataName();
	if ( (fd = (BPTT)openNew(dataName))!=NULL) {
		dummy = VERSION;
		Write((BPTR)fd,&dummy,4L);

/* new new new for font */

		Write((BPTR)fd,fontName,20L);
		Write((BPTR)fd,&fontHeight,2L);
		Write((BPTR)fd,helpName,20L);
		eoline = eol;
		tinyCoords =
					((unsigned long)TinyWindow.TopEdge << 16L) |
						(TinyWindow.LeftEdge & 0x0000ffff);

		for (i=0; i<MAXSUCH; i++) Write((BPTR)fd,such[i],4L);

		Write((BPTR)fd,tabTables[0],(long)sizeof(tabTables));
		Write((BPTR)fd,charMappings,(long)sizeof(charMappings));
 		Write((BPTR)fd,bufferFlags,(long)sizeof(bufferFlags));
		Write((BPTR)fd,printKeys,(long)sizeof(printKeys));
		Write((BPTR)fd,altKeys,(long)sizeof(altKeys));
		Write((BPTR)fd,coloring,(long)sizeof(coloring));
		Write((BPTR)fd,gadgetName[0],88L);
		Write((BPTR)fd,rv,20L);
		Write((BPTR)fd,&foldTests,(long)sizeof(foldTests));
		Write((BPTR)fd,&maxTest,2L);

		Write((BPTR)fd,cmdLens,(long)sizeof(cmdLens));
		for (i=0; i<MAXMACROS; i++) {
			if (cmdTable[i]!=NULL &&
					Write((BPTR)fd,cmdTable[i],(long)cmdLens[i])!=cmdLens[i]) {
				alertMes("Aborting: Error writing",dataName);
				Close((BPTR)fd);
				DeleteFile(dataName);
				goto RTN;
			}
		}
		Close((BPTR)fd);
		passiveMsg("Saved to",dataName,0L);
		return(TRUE);
	} else alertMes("Can't save to",dataName);
RTN:
	return(FALSE);
}

readVal(long *c,
		register short i1,
		register short i2)
{
register short  ch;
register long	j;

	if (!getDelimiter('<')) goto RTN;
	ch = *fastP;
	if (ch=='-') incFast();
	if (isdigit(*fastP)) {
		if (!getIntArg())  goto RTN;
		j = dummy;
	} else {
		j = ch;
		if (ch!='-') incFast();
	}
	if (i1 || i2) {
		if (j<i1 || j>i2) goto RTN;
	}
	if (!getDelimiter('>')) goto RTN;
	*c = j;
	return(TRUE);
RTN:
	return(FALSE);
}

readString(register UCHAR *x)
{
register UCHAR  *y;
register short   i;

	if (!getDelimiter('<')) goto RTN;
	y = x;
	i = 0;
	while (*fastP!='>' && i < 19) {
		*(y++) = *fastP;
		incFast();
		i++;
	}
	if (*fastP!='>') goto RTN;
	*y = 0x00;
	x[21] = i;
	return(TRUE);
RTN:
	return(FALSE);
}

void copyFile(char *from,
			  char *to)
{
register UCHAR          *fc,*fd;
register	long		i;
char					buf[512];

	if ( (fc=openOld(from))==NULL) return;
	if ( (fd=openNew(to))==NULL) goto QUIT;
	while ( (i=Read((BPTR)fc,buf,512L))>0L) Write((BPTR)fd,buf,i);
	Close((BPTR)fd);
QUIT:
	Close((BPTR)fc);
}

speakIt(register UCHAR *x)
{
register UCHAR  i;
register long	y;
register BPTT   fd;

	fd = (BPTT)openOld("SPEAK:");
	if (fd==NULL) {
		fd = (BPTT)openNew("SPEAK:");
		if (fd==NULL) return(FALSE);
	}
	while (1) {
		y = (long)x;
		while (1) {
			i = *(x++);
			if (i>=65) continue;
			if (i==0 || i=='.' || i=='?' || i=='!' || i==';' || i==':') break;
		}
		Write((BPTR)fd,(char *)y,(long)x - y);
		if (i==0 || stop()) break;
	}
	Close((BPTR)fd);
	return(TRUE);
}

request(register char *x,
		register char *y,
		short neg)	/* false is neg or abort, true is pos */
{
char				*pos;
register long		len;
register UCHAR  	i;
register short  	rtn,j;
short				savee,saveStop,saveLearn,speakalerts;
long				saveUse;
UCHAR				z[MAXLINECHARS];

	rtn = FALSE;

	if (tiny) {
		if (!closeTiny()) goto RTN;
	}

	if (edWindow==NULL) goto RTN;
	forceRefresh();
	if ( (strlen(x) + strlen(y))==0) goto RTN;
	savee = execLearn;
	execLearn = 0;
	saveLearn = learnMode;
	learnMode = 0;
	saveStop = stopPause;
	saveUse = useKeys; useKeys = 0;
	speakalerts = (short)speakAlerts;
	soakInputs();

AGAIN:
	len = (horPixels - 20)/charWidth;
	strcpy(z," ");
	strncatmax(z,x,(short)len);
	strncatmax(z,y,(short)len);
	strncatmax(z," ",(short)len);

	if (depth==1) {
		setColors(normalVid);
		RectFill(rp,5L,30L,635L,60L);
	} else {
		setColors(requesterVid);
		drawBox(5,30,635,60);
	}
	Move(rp,10L,40L);
	Text(rp,z,(long)strlen(z));
	Move(rp,10L,55L);
	pos = "  Okay  ";
	if (neg)	pos = "  Yes   ";
	Text(rp,pos,8L);
	j = 11 * charWidth;
	len = 10L + j;
	if (neg) {
		Move(rp,len,55L);
		Text(rp,"   No   ",8L);
	}
	len += j;
	Move(rp,len,55L);
	Text(rp," Cancel ",8L);
	reportMouse = mouseDown = 0;
	stopPause = 0;
	if (speakalerts) {
		*z = 0x00;
		strncatmax(z,x,127);
		if (*y) { strncatmax(z," ",127); strncatmax(z,y,127); }
		speakIt(z);
		speakalerts = 0;
	}

	while (1) {
		mouseY=0;
		if (nextEvent() || stopPause) {
			i = gChar;
			if (mouseDown || reportMouse) {
				forceRefresh();
				while (!collectRaw());
				continue;
			}
			/* if (mouseDown || reportMouse || (i==0 && macroNum==0)) continue; */
			if (stopPause || i=='c' || i=='C' || inBox(2)) {
				stopPause = TRUE;
				break;
			}
			if (neg) {
				if (i=='n' || i=='N' || inBox(1)) break;
				if (i=='y' || i=='Y' || i==eol || inBox(0)) { rtn=TRUE; break; }
			} else {
				if (i=='o' || i=='O'|| i==eol || inBox(0)) { rtn=TRUE; break; }
			}
			goto AGAIN;
		} else /* CANNOT DO BUSYWORK here because itmay call REQUEST! */ 
		waite();
	}
	forceRefresh();
	soakInputs();
	if (saveStop) stopPause = TRUE;
	if (!stopPause) execLearn = savee;
	learnMode = saveLearn;
	useKeys = saveUse;
	mouseY = 0;

RTN:
	return(rtn);
}

inBox(short i)
{
register short    minx,miny,maxx,maxy,j,k;

	if (mouseDown) return(FALSE);
	miny = 55 - charHeight;
	maxy = 55;
	j = charWidth;
	i *= 11;
	minx = 10 + (i * j);
	maxx = minx + (j << 3);
	j = mouseX; k = mouseY;
	if (j>=minx && j<=maxx && k>=miny && k<=maxy) return(TRUE);
	return(FALSE);
}

void passiveMsg(register UCHAR  *xx,
				UCHAR *y,
				register long i) /* message bottom line cur window*/
{
register UCHAR  *zz;
register short  j;
UCHAR			zzz[130];

	if (*xx || i) {
		zz = passive;
		*zz = 0x00;
		j = maxLineChars;
		if (*xx) {
			strncatmax(zz,xx,j);
			if (y) {
				strncatmax(zz," ",j);
				strncatmax(zz,y,j);
			}
		}
		if (i) { strncatmax(zz," ",j); catNumber(zz,i,j); }
	}
	restoreMsg();
	if (speakMsgs) {
		zz = zzz;
		*zz = 0x00;
		j = 127;
		if (*xx) {
			strncatmax(zz,xx,j);
			if (y) { strncatmax(zz," ",j); strncatmax(zz,y,j); }
		}
		if (i) {  strncatmax(zz," ",j); catNumber(zz,i,j); }
		if (*zz) speakIt(zz);
	}
	return;
}

#ifdef USEC

void restoreMsg()
{
register UCHAR  *x;
register short  i,j;

	if (edWindow==NULL || noDisplay || tiny) return;
	x = passive;
	if (*x) {
		cleared = 0;
		if (oldColors!=msgVid) setColors(msgVid);
		i = 1;
		j = maxLineChars;
		wptr = x;
DOMORE:
		x++;
		if (*x==0x00 || i==j) {
			*x=0x00;
			move(0,0);
			text(i);
			cleareol();
			return;
		}
		i++;
		goto DOMORE;
	}
	clearMsg();
}

UCHAR *openOld(UCHAR *x)
{
	if (x==NULL) return(NULL);
	return( (UCHAR *)Open(x,(long)MODE_OLDFILE) );
}

UCHAR *openNew(UCHAR *x)
{
	if (x==NULL) return(NULL);
	return( (UCHAR *)Open(x,(long)MODE_NEWFILE) );
}

void catNumber(char *name,long num,short len)
{
register UCHAR *x;
UCHAR	v[12];

	x = v;
	stci_d(x,num);
	strncatmax(name,x,len);
}

alertMes(register UCHAR  *xx,
		 register UCHAR  *y)
{
	return(request(xx,y,0));
}

yesNo(UCHAR *x,
	  UCHAR *y)
{
	return(request(x,y,1));
}

void drawBox(register short j,
			 register short k,
			 register short l,
			 register short m)
{
register short *b;
short	box[10];

	Move(rp,(long)j,(long)k);
	b = &(box[0]);
	*(b++) = j; *(b++) = k;
	*(b++) = l; *(b++) = k;
	*(b++) = l; *(b++) = m;
	*(b++) = j; *(b++) = m;
	*(b++) = j; *(b++) = k;
	PolyDraw(rp,5L,&box[0]);
}

void memoryMsg()
{
	oneMsg("Memory....");
}

void oneMsg(UCHAR *x)
{
	passiveMsg(x,NULL,0L);
}

soakInputs()
{
	rmbFlags();
	while (mouseDown || reportMouse) nextEvent();
	while (nextEvent());
}

void waite()
{
	Delay(3L);
}

void nullArrays()
{
	zero(bufferNames);
}

void oneOk(UCHAR *x)
{
	alertMes(x,"");
}

void stci_d(UCHAR out[],register long in)
{
register UCHAR  *out1,*out2;
register short  i,j,c;

	out1 = out;
	if (in<0L) { j = 1; in = -in; }
	else j=0;
	do {
		*(out1++) = (in % 10L) + '0';
	} while (in /= 10L);
	if (j) *(out1++) = '-';
	*out1 = 0x00;
	out1 = out;
	j = (short)strlen(out1)-1;
	out2 = &out[j];
	for (i=0; i<j; i++, j--) {
		c = *out1;
		*(out1++) = *out2;
		*out2 = c;
		out2--;
	}
}

void clearMsg()
{
register UCHAR *x,*y;
register short    i,k,n,j,c160;

	if (noDisplay || cleared || tiny) return;
	cleared = 1;
	if (oldColors!=msgVid) setColors(msgVid);
	x = passive;
	wptr = x;
	move(0,0);
	if (markGadgets) {
		c160 = maxLineChars >> 2;
		y = gadgetName[0];
		for (i=0; i<4; i++) {
			n = y[21];
			if (n>=c160) n = c160 - 1;
			k = (c160 - n - 1) >> 1;
			j = k;
			while (j-- > 0) *(x++) = ' ';
			*x = 0x00;
			if (i==3 && rowCol) break;
			if (n) strcpy(x,y);
			if (i==3) break;
			x += n;
			k += n;
			while (++k<c160) *(x++) = ' ';
			*(x++) = '|';
			y += 22;
		}
		text((short)strlen(wptr));
		x = passive;
	}
	cleareol();
	*x = 0x00;
	if (rowCol && stackDepth==0) rowMsg();
}

void strncatmax(register UCHAR *x,
				register UCHAR *y,
				register short max)
{
register ULONG p;

		p = (ULONG)x;
		while (*x) x++;
		max -= (short)((ULONG)x - p);
		while (max-- > 0 && *y) *(x++) = *(y++);
		*x = 0x00;
}

#endif

void setStuff()
{
	if (uga!=0L) { userGlobalA = uga; uga = 0L; }
	if (ugb!=0L) { userGlobalB = ugb; ugb = 0L; }
	setGlobals();
	if (curBuf!=NULL) curBuf->colors = localColor;
	makeWindow();
}

#ifndef USEC
#asm
	XREF  _bufferNames
	XREF  _charHeight
	XREF  _cleared
	XREF  _cleareol
	XREF  _Close
	XREF  _collectRaw
	XREF  _curBuf
	XREF  _Delay
	XREF  _depth
	XREF  _edWindow
	XREF  _eol
	XREF  _execLearn
	XREF  _findFile
	XREF  _forceRefresh
	XREF  _gChar
	XREF  _horPixels
	XREF  _icons
	XREF  _learnMode
	XREF  _makeWindow
	XREF  _markGadgets
	XREF  _maxLineChars
	XREF  _mouseDown
	XREF  _mouseX
	XREF  _mouseY
	XREF  _Move
	XREF  _move
	XREF  _nextEvent
	XREF  _noDisplay
	XREF  _menuDrawn
	XREF  _nullify
	XREF  _Open
	XREF  _passive
	XREF  _PolyDraw
	XREF  _primitive
	XREF  _Read
	XREF  _RectFill
	XREF  _reportMouse
	XREF  _oldColors
	XREF  _restoreMsg
	XREF  _rmbFlags
	XREF  _rowCol
	XREF  _rowMsg
	XREF  _rp
	XREF  _saveDis
	XREF  _setColors
	XREF  _speakAlerts
	XREF  _speakMsgs
	XREF  _stackDepth
	XREF  _stci_d
	XREF  _stopPause
	XREF  _strcat
	XREF  _strcpy
	XREF  _strncatmax
	XREF  _Text
	XREF  _text
	XREF  _tiny
	XREF  _uga
	XREF  _ugb
	XREF  _unsaveDis
	XREF  _wptr
	XREF  _Write

	xdef  _openOld
_openOld:
	move.l  4(sp),a0
	pea 1005
	move.l  a0,-(sp)	;{return((UCHAR*)Open(x,(long)MODE_OLDFILE))
	jsr _Open
	addq.w  #8,sp
	rts

	xdef  _openNew
_openNew:
	move.l  4(sp),a0
	pea 1006
	move.l  a0,-(sp)
	jsr _Open
	addq.w  #8,sp
	rts

	xdef  _catNumber
_catNumber:
	link	a5,#U12
	move.l  a2,-(sp)	;x=v
	lea -12(a5),a2  ;stci_d(x,num)
	move.l  12(a5),-(sp)
	move.l  a2,-(sp)
	jsr _stci_d ;strncatmax(name,x,len)
	move.w  16(a5),-(sp)
	move.l  a2,-(sp)
	move.l  8(a5),-(sp)
	jsr _strncatmax
	lea 18(sp),sp
U14:
	move.l  (sp)+,a2
	unlk	a5
	rts
U12:	equ -12

	xdef  _alertMes
_alertMes:
	move.l  4(sp),a0
	move.l  8(sp),a1	;return(request(xx,y,0))
	clr.w	-(sp)
	move.l  a1,-(sp)
	move.l  a0,-(sp)
	jsr _request
	lea 10(sp),sp
	rts

	xdef  _yesNo
_yesNo:
	move.l  8(sp),a0
	move.l  4(sp),a1
	move.w  #1,-(sp)	;{return(request(x,y,1))
	move.l  a0,-(sp)
	move.l  a1,-(sp)
	jsr _request
	lea 10(sp),sp
	rts

	xdef  _drawBox
_drawBox:
	link	a5,#U43
	movem.l U44,-(sp)
	moveq	#0,d4
	moveq	#0,d5
	move.w  8(a5),d4
	move.w  10(a5),d5
	move.w  12(a5),d6
	move.w  14(a5),d7	;Move(rp,(long)j,(long)k)
	move.l  d5,-(sp)
	move.l  d4,-(sp)
	move.l  _rp,-(sp)
	jsr _Move	;b=&(box[0])
	lea -20(a5),a0
	move.w  d4,(a0)+	;*(b++)=j*(b++)=k
	move.w  d5,(a0)+	;*(b++)=l*(b++)=k
	move.w  d6,(a0)+
	move.w  d5,(a0)+	;*(b++)=l*(b++)=m
	move.w  d6,(a0)+
	move.w  d7,(a0)+	;*(b++)=j*(b++)=m
	move.w  d4,(a0)+
	move.w  d7,(a0)+	;*(b++)=j*(b++)=k
	move.w  d4,(a0)+
	move.w  d5,(a0)+	;PolyDraw(rp,5L,&box[0])
	pea -20(a5)
	pea 5
	move.l  _rp,-(sp)
	jsr _PolyDraw
	lea 24(sp),sp
U45:
	movem.l (sp)+,U44
	unlk	a5
	rts
U43:	equ -20
U44:	reg d4-d7

	xdef  _memoryMsg
_memoryMsg:
	pea U91+0	;oneMsg("Memory....")
	jsr _oneMsg
	addq.w  #4,sp
	rts
U91:
	dc.b	77,101,109,111,114,121,46,46,46,46,0
	ds  0

	xdef  _oneMsg
_oneMsg:
	move.l  4(sp),a0
	clr.l	-(sp)	;{passiveMsg(x,NULL,0L)
	clr.l   -(sp)
	move.l  a0,-(sp)
	jsr _passiveMsg
	lea 12(sp),sp
	rts

	xdef  _soakInputs
_soakInputs:
	jsr _rmbFlags
U102:	;while(mouseDown||reportMouse)nextEvent()
	tst.w	_mouseDown
	bne U104
	tst.w	_reportMouse
	beq U103
U104:
	jsr _nextEvent
	bra U102
U103:	;while(nextEvent())
	jsr _nextEvent
	tst.w	d0
	bne U103
	rts

	xdef  _waite
_waite:
	pea 3	;Delay(3L)
	jsr _Delay
	addq.w  #4,sp
	rts

	xdef  _nullArrays
_nullArrays:
	move.l  #400,-(sp)  ;zero(bufferNames)
	pea _bufferNames
	jsr _nullify
	addq.w  #8,sp
U118:
	rts

	xdef  _oneOk
_oneOk:
	move.l  4(sp),a0
	pea U99+0	;alertMes(x,"")
	move.l  a0,-(sp)
	jsr _alertMes
	addq.w  #8,sp
	rts
U99:
	dc.b	0
	ds  0

	xdef  _stci_d
_stci_d:
	link	a5,#JJ68
	movem.l d4/d5,-(sp)
	move.l  8(a5),a0		;register long  in;
							;register UCHAR *x,i;
							;register short j;
							;UCHAR  		zz[14];
	moveq  #0,d5			;	j = 0;
	move.l  12(a5),d4
;;;    tst.l   d4			;	if (in<0L) { j = 1; in = -in; }
	bge JJ70
	moveq  #1,d5
	neg.l	d4
JJ70:					;	x = zz + 13;
	lea -1(a5),a1
	clr.b	(a1)			;	*x = 0x00;
JJ71:					;	while (1) {
	moveq	#10,d1  	;		*(--x) = (in % 10L) + '0';
	move.l  d4,d0
	jsr .mods#
	add.l	#48,d0
	move.b  d0,-(a1)
	moveq	#10,d1  	;		in /= 10L;
	move.l  d4,d0
	jsr .divs#
	move.l  d0,d4
;;;    tst.l   d4			;		if (in==0L) break;
	bne JJ71
JJ72:
	tst.w	d5  		;	if (j) *(y++) = '-';
	beq JJ74
	move.b  #45,(a0)+
	moveq	#0,d0
JJ74:					;	while (1) {
	move.b  (a1)+,d0		;		i = *(x++);
;;;    tst.b   d0			;		if (i==0) break;
;;;    beq JJ75
	move.b  d0,(a0)+		;		*(y++) = i;
	bne JJ74			;	}
JJ75:
;;;    clr.b   (a0) 	;	*y = 0x00;
	movem.l (sp)+,d4/d5
	unlk	a5
	rts
JJ68:	equ -14

	xdef  _strncatmax
_strncatmax:
	move.l  4(sp),a1		;register short max;
	moveq	#0,d1
	move.w  12(sp),d1
							;register UCHAR c;
	move.l  a1,a0			;	xx = x;
JJ91:					;		while (*x) { x++; max--; }
	tst.b	(a1)+
	bne JJ91
	subq.w  #1,a1			;	x--;
	move.l  a1,d0
	sub.l	a0,d0			;	len = x - xx;
	sub.w	d0,d1			;	max -= len;
	move.l  8(sp),a0
	moveq	#0,d0
	addq.w  #1,d1			;	len++;
JJ93:					;		while (1) {
;;;    tst.w   d1			;			if (max<=0) break;
	subq.w   #1,d1  	;		max--;
	ble JJ94
	move.b  (a0)+,d0		;		c = *y++;
	tst.b	d0			;			if (c==0) break;
	beq JJ94
	move.b  d0,(a1)+		;			*(x++) = c;
	bra JJ93			;		}

JJ94:
	clr.b	(a1)			;		*x = 0x00;
	rts

	xdef  _restoreMsg
_restoreMsg:
	movem.l d4/d5/a2,-(sp)
	tst.l	_edWindow	; if (edWindow==NULL || tiny || noDisplay) return
	beq JJ23
	tst.w	_noDisplay
	bne JJ23
;;;;	move.l  _menuDrawn,a0
;;;;	move.l  (a0),d0
;;;;	and.l	#128,d0
;;;;	bne JJ23
	tst.w	_tiny
	beq JJ21
JJ23:
	movem.l (sp)+,d4/d5/a2
	rts
JJ21:					;	x = passive;
	lea _passive,a2 ;	move.l  a0,a2
	tst.b	(a2)			;	if (*x) {
	beq JJ24
	clr.w	_cleared		;			cleared = 0;
;;;	moveq	#9,d4			;				i = msgVid;
;;;	cmp.w	#1,_depth		;			if (depth==1) i = normalVid;
;;;	bne JJ25
;;;	moveq	#0,d4
;;;JJ25:					;			setColors(i);
	cmp.w	#9,_oldColors
	beq JJJ99
	move.w  #9,-(sp)
	jsr _setColors
	addq.w  #2,sp
JJJ99:
	moveq	#1,d4			;			i = 1;
	move.w  _maxLineChars,d5  ; 	j = maxLineChars;
	move.l  a2,_wptr		;			wptr = x;
JJ26:
	addq.w  #1,a2			;				x++;
	tst.b	(a2)			;				if (*x==0x00 || i==j) {
	beq JJ28
	cmp.w	d5,d4
	bne JJ27
JJ28:
	clr.b	(a2)			;					*x=0x00;
	clr.w	-(sp)			;					move(0,0);
	clr.w	-(sp)
	jsr _move
	addq.w  #4,sp
	move.w  d4,-(sp)		;					text(i);
	jsr _text
	addq.w  #2,sp
	jsr _cleareol			;					cleareol();
	bra JJ23
JJ27:					;				i++;
	addq.w  #1,d4
	bra JJ26			;				goto DOMORE;
JJ24:					;	clearMsg();
	jsr _clearMsg
	bra JJ23

	xdef  _clearMsg
_clearMsg:
	link	a5,#JJ2
	movem.l d4-d7/a2/a3,-(sp)
	tst.w	_noDisplay  ; if (noDisplay || cleared || tiny) return
	bne JJ6
	tst.w	_cleared
	bne JJ6
;;;;	move.l  _menuDrawn,a0
;;;;	move.l  (a0),d0
;;;;	and.l	#128,d0
;;;;	bne JJ6
	tst.w	_tiny
	beq JJ4
JJ6:
	movem.l (sp)+,d4-d7/a2/a3
	unlk	a5
	rts
JJ4:					;	cleared = 1;
	move.w  #1,_cleared
;;;	moveq	#9,d6			;	n = msgVid;
;;;	cmp.w	#1,_depth		;	if (depth==1) n = normalVid;
;;;	bne JJ7
;;;	moveq	#0,d6
;;;JJ7:					;	setColors(n);
	cmp.w	#9,_oldColors
	beq JJJ98
	move.w  #9,-(sp)
	jsr _setColors
	addq.w  #2,sp
JJJ98:
	lea _passive,a2 	;	x = passive;
	move.l  a2,_wptr		;	wptr = x;
	clr.w	-(sp)			;	move(0,0);
	clr.w	-(sp)
	jsr _move
	addq.w  #4,sp
	tst.l	_markGadgets	;	if (markGadgets) {
	beq JJ8
 moveq  #0,d7
	move.w  _maxLineChars,d0
	asr.w	#2,d0			;			c160 = maxLineChars >> 2;
	move.w  d0,-2(a5)
	moveq	#0,d4			;			for (i=0; i<4; i++) {
	lea _gadgetName,a3
JJ11:					;				y = gadgetName[i];
	moveq	#0,d6			;				n = y[21];
	move.b  21(a3),d6
;	move.w  d0,d6
	move.w  -2(a5),d1
	cmp.w	d1,d6	;			if (n>=c160) n = c160 - 1;
	blt JJ12
	move.w   d1,d6
	subq.w   #1,d6
JJ12:					;				k = (c160 - n - 1) >> 1;
	sub.w	d6,d1
	subq.w   #1,d1
	move.w  d1,d5
	asr.w	#1,d5
	move.w  d5,d7			;				j = k;
JJ13:					;				while (j-- > 0) *(x++) = ' ';
	tst.w	d7  		;;;
	ble JJ14
	move.b  #32,(a2)+
	subq.w  #1,d7
	bra JJ13
JJ14:
	clr.b	(a2)			;				*x = 0x00;
	cmp.w	#3,d4			;				if (i==3 && rowCol) break;
	bne JJ15
	tst.l	_rowCol
	bne JJ10
JJ15:
	tst.w	d6  		;				if (n) strcpy(x,y);
	beq JJ16
	move.l  a3,a0			;	this is y
	move.l  a2,a1			;	this is x
	moveq	#0,d0
JSTRCPY:
	move.b  (a0)+,d0		;	i = *(y++);
	move.b  d0,(a1)+		;	*(x++) = i;
;;;    tst.b   d0			;	if (i) goto JSTRCPY;
	bne JSTRCPY
JJ16:
	cmp.w	#3,d4			;				if (i==3) break;
	beq JJ10
	moveq	#0,d0
	move.w  d6,d0			;				x += n;
	add.w	d0,a2
	add.w	d6,d5			;				k += n;
	move.w  -2(a5),d1
JJ17:					;				while (++k<c160) *(x++) = ' ';
	addq.w   #1,d5
	cmp.w	d1,d5
	bge JJ18
	move.b  #32,(a2)+
	bra JJ17
JJ18:
	move.b  #124,(a2)+  ;				*(x++) = '|';
JJ9:
	add.w	#22,a3  	; bump gadgetName[++i];
	addq.w   #1,d4
	cmp.w	#4,d4
	blt JJ11
JJ10:
	move.l  _wptr,a0
	move.l  a0,a1
	moveq	#0,d1
JSTRLEN:
	move.b  (a0)+,d1
;;;    tst.b   d1
	bne   JSTRLEN
	move.l  a0,d0
	sub.l	a1,d0
	subq.w  #1,d0
	move.w  d0,-(sp)		;		text(strlen(wptr));
	jsr _text
	addq.w   #2,sp  	;	cleareol();
JJ8:
	jsr _cleareol
	lea _passive,a0 	;	x = passive;
	clr.b	(a0)			;	*x = 0x00;
	tst.l	_rowCol
	beq JJ6
	tst.w	_stackDepth
	bne JJ6
	jsr _rowMsg
	bra JJ6
JJ2:	equ -2
#endasm
#endif

#ifdef ISK

typedef  int	 (*PFA)();

extern  PFA   macroFunc[MAXFCTTOKENS];

static short *suchs[82]
= {
(short *)&eol, /* use for eol */

(short *)&mouseX,
(short *)&mouseY,
(short *)&reportMouse,
(short *)&mouseDown,
(short *)&inputChar,

(short *)&macroNum,
(short *)&gChar,
(short *)&showTitle,
(short *)&depth,
(short *)&maxLineChars,

(short *)&charHeight,
(short *)&charWidth,
(short *)&vertPixels,
(short *)&horPixels,
(short *)&maxLines,

(short *)&vscrollamt,
(short *)&hscrollamt,
(short *)&globalFlags,
(short *)&primitive,
(short *)&displayFlag,

(short *)&curSplit,
(short *)&noDisplay,
(short *)&toTiny,
(short *)&stopPause,
(short *)&menusUp,

(short *)&warpUp,
(short *)&execLearn,
(short *)&stopSave,
(short *)&saves,
(short *)&prtOpen,

(short *)&prints,
(short *)&dummy,
(short *)&menuInset,
(short *)&curLearn,
(short *)&curMsgIn,

(short *)&curMsgOut,
(short *)&curBuf,
(short *)&rp,
(short *)&uport,
(short *)&edScreen,

(short *)&edWindow,
(short *)&vp,
(short *)&ffont,
(short *)&nStack[0],
(short *)&bStack[0],

(short *)&sStack,
(short *)&cmdTable[0],
(short *)&cmdLens[0],
(short *)&msgQ[0],
(short *)&learnQ[0],

(short *)&tabTables[0][0],
(short *)&printByte[0],
(short *)&printLen[0],
(short *)&printBuf[0],
(short *)&printChoice[0],

(short *)&prtPort,
(short *)&charMappings[0],
(short *)&such[0],
(short *)&splitBufNum[0],
(short *)&startSplit,

(short *)&stopSplit[0],
(short *)&lineHeight[0],
(short *)&coloring[0][0],
(short *)&dummy,
(short *)&rc[0],

(short *)&printKeys[0],
(short *)&bufferNames[0],
(short *)&gadgetName[0][0],
(short *)&configPtr,
(short *)&helpName[0],

(short *)&dataName[0],
(short *)&buf[0],
(short *)&n[0],        /* had no ampersand and next line also! */
(short *)&macroFunc[0],
(short *)&oldCursorLine,

(short *)&oldCursorCol,
(short *)&startLine,
(short *)&stopLine,
(short *)&nSplit,
(short *)&maxSaveSize,

(short *)&altKeys[0]
};
	/* --------------NEW NEW NEW maxSaveSize -------------------- */

Fpeek()  /* addr,val,type */
{
register UCHAR *addr;
register short *ps;
long *pl;
register long val,type;

	addr = (UCHAR *)*longPtrArgs[0];
	type = *longPtrArgs[2];

	if (type==1) val = *addr;
	else if (type==2) {
		ps = (short *)addr;
		val = *ps;
	} else if (type==4) {
		pl = (long *)addr;
		val = *pl;
	}
	*longPtrArgs[1] = val;
	return(TRUE);
}

Fpoke()  /* addr,val,type */
{
register UCHAR *addr;
register short *ps;
long *pl;
register long val,type;

	addr = (UCHAR *)*longPtrArgs[0];
	val = *longPtrArgs[1];
	type = *longPtrArgs[2];

	if (type==1) *addr = (UCHAR)val;
	else if (type==2) {
		ps = (short *)addr;
		*ps = (short)val;
	} else if (type==4) {
		pl = (long *)addr;
		*pl = val;
	}
	return(TRUE);
}

FgetAddress()  /* item,addr */
{
register short item;

	item = *longPtrArgs[0];
	if (item>81 || item<0) return(FALSE);
	*longPtrArgs[1] = (long)suchs[item];
	return(TRUE);
}

#endif

