// GPC.SR		getscrwd and putscrwd

get "BRAVO.DF";
get "CHAR.DF";
// Incoming Procedures

external [
	errhlt;
	ult;
	ugt;
	errck;
	getvp;
	hpalloca;
	insertlf;
	setlf;
	mult;
	stsize;
	stnum;
	stget;
	gets;
	puts;
	umin;
	];

// Incoming Statics

external [
	vbp;
	ckproc;
	ckperr;
	pctb;
	rgpctb;
	rgcp;
	vpc;
	rgpcd;
	ppcd;
	rgmaccp;
	rgbs;
	vcp;
	cblind;
	deltacp;
	vcpput;
	vchremainput;
	vlbput;
	vpwput;
	mpfnof;
	];

// Outgoing Procedures

external [
	binsearcha;
	ckbinsearcha;
	mapcp;
	mapcpb;
	prevscrvch;
	getvch
	putvch
	getint;
	putint;
	cprealch;
	getscrwd // **
	putscrwd // **
	];

// Outgoing Statics

external
	[
	vpw;
	vlb;
	vchremain;
	vpa;
	pfwpg;
	vdoc;
	rcnew;
	vchremainb;
	]

// Local Statics

static
	[
	vpw;
	vlb;
	vchremain;
	vpa;
	pfwpg;
	vdoc;
	rcnew;
	vchremainb;
	]


// P C
//B I N S E A R C H A
//threshold search for k in tbl
// catalogue no. = SPE-59

let binsearcha(tbl,maxi,k) = valof
[ let ttbl = tbl-1;
let u = maxi;
let l = 1;
let i = nil;
if ult(k,tbl ! maxi) do
	[ i = (u+l) rshift 1;
	test ugt((ttbl ! i),k) ifso u = i-1
	ifnot
		[ test ( not ugt( (ttbl ! (i+1)),k)) ifso l = i+1
		ifnot resultis i-1
		]
	] repeat
resultis maxi;
]
//C K B I N S E A R C H A 
//checks to see if tbl!i <= k < tbl!(i+1)
// catalogue no. = SPE-63

and ckbinsearcha(tbl,i,k) be
[ ckproc = "ckbinsearch";
ckperr = i;
if ( ugt(k, (tbl ! i))) % (not ugt( (tbl ! (i+1)),k)) do
	errck("bs");
]
// M A P C P
// returns the number of characters which can be 
// accessed without another mapcp call, as well as the location
// of cp in vpw and vlb
// catalogue no. = SPE-76
and mapcp(doc,cp) be
[ pctb = rgpctb ! doc;
let proc = nil;
 rgcp = lv(pctb >> PCTB.rgcp);
vpc = binsearcha(rgcp,(pctb >> PCTB.macpc),cp);
if vpc eq pctb >> PCTB.macpc then errhlt("mpc");
let cppcrel = cp-rgcp ! vpc;
rgpcd = (rgcp+(pctb >> PCTB.maxpc)+1);
ppcd = rgpcd+(vpc lshift 1);
livetest:
test ppcd >> PCD.live ifnot
	[ rcnew = cppcrel+(ppcd >> PCD.rc);
	vpa = (ppcd >> PCD.vpaddr)+rcnew << PCD.p;
	pfwpg = getvp(vpa);
	vpw = pfwpg+(rcnew << PCD.rc  rshift  1);
	test (rcnew << odd) ifso
		vlb = false;
	ifnot	vlb = true;
	let pgremain = (#177777 << PCD.rc)+1-(rcnew << PCD.rc);
	let pcremain = (rgcp ! (vpc+1))-cp;
	vchremain = umin(pgremain,pcremain);
	] 
ifso	[ let plf = ppcd >> PCD.esc;
//**proc = lfyield ! (plf >> LF.lf);
//**proc(plf);
//**proc = lftouch ! (plf >> LF.lf);
//**proc(plf);
	rcnew = cp-(rgcp ! vpc);
	vpw = (plf >> LF.sl)+1+(rcnew rshift 1);
	test rcnew << odd ifso
		vlb = false
	ifnot vlb = true;
	vchremain = (rgcp ! (vpc+1))-(cp);
	] 
] 

// M A P C P B
// returns the number of characters which can be 
// reverse accessed without another mapcpb call, as well as the location
// of cp in vpw and vlb
// catalogue no. = SPE-76
and mapcpb(doc,cp) be
[ mapcp(doc,cp);
test ppcd >> PCD.live ifso
	vchremainb = rcnew+1
ifnot	[ let pgremainb = rcnew << PCD.rc+1;
	let pcremainb = cp-(rgcp ! vpc)+1;
	vchremainb = umin(pcremainb,pgremainb);
	] 
] 

 
// C R E A T E D O C E 
// catalogue no. = SPE-99
and createdoce(doc,maxpc) be
[ 
rgpctb ! doc = hpalloca(maxpc*3+1+pctbl);
rgmaccp ! doc = 0;
(rgpctb ! doc) >> PCTB.maxpc = maxpc;
(rgpctb ! doc) >> PCTB.macpc = 0;
(rgpctb ! doc) >> PCTB.rgcp = 0;
] 

// C R E A T E D O C M
//
and createdocm(doc) be
[
createdoce(doc,1);
insertlf(doc,0,lfedoc,doc,4);
setlf(lfedoc,doc," ~");
]

// P R E V S C R V C H 
// catalogue no. = 138
and prevscrvch( ) = valof 
[ let char = nil;
vcpput = vcpput-1;
(mpfnof ! fnscr) >> OF.pos = vcpput;
gets(fnscr);
(mpfnof ! fnscr) >> OF.pos = vcpput;
puts(fnscr,chblind);
(mpfnof ! fnscr) >> OF.pos = vcpput;
cblind = cblind+1;
deltacp = deltacp-1;
resultis char;
] 

// G E T V C H
// catalogue no. = 135
and getvch( ) = valof
[ let char = nil;
if vchremain eq 0 then mapcp(vdoc,vcp) ;
test vlb ifso
	[ char = vpw >> lh;
	vlb = false;
	] 
ifnot	[ char = vpw >> rh;
	vlb = true;
	vpw = vpw+1;
	] 
vchremain = vchremain-1;
vcp = vcp+1;
resultis char;
] 

// ** GYPSY DELETED GETINT

// P U T V C H 
// catalogue no. = SPE-107
and putvch(char) be
[ (mpfnof ! fnscr) >> OF.pos = vcpput;
puts(fnscr,char);
vcpput = vcpput+1;
] 

// ** GYPSY DELETED PUTINT AND CPREALCH

// ** GYPSY ADDED:

// G E T S C R W D
and getscrwd() = valof
[
(mpfnof ! fnscr) >> OF.pos = vcp;
(mpfnof ! fnscr) >> OF.wmode = true;
let wd = gets(fnscr);
vcp = vcp+2;
(mpfnof ! fnscr) >> OF.wmode = false;
resultis wd;
] 

// P U T S C R W D 
and putscrwd(wd) be
[
(mpfnof ! fnscr) >> OF.pos = vcpput;
(mpfnof ! fnscr) >> OF.wmode = true;
puts(fnscr,wd);
vcpput = vcpput+2;
(mpfnof ! fnscr) >> OF.wmode = false;
]