-- File: GCMisc.mesa, HGM August 15, 1978 12:17 AM
-- Last Edit: Ly September 21, 1978 10:46 PM

DIRECTORY
DisplayDefs: FROM "DisplayDefs" USING [SetSystemDisplaySize],
GCDefs: FROM "GCDefs" USING [
GCInterface, maxInform, PrintErrorPup, PrintPupAddress],
IODefs
: FROM "<Mesa>IODefs" USING [
CR, ReadID, ReadChar, ReadNumber, Rubout, SP,
WriteChar, WriteLine, WriteString],
PupDefs: FROM "PupDefs" USING [
AppendPupAddress, GetFreePupBuffer, GetLocalPupAddress,
GetPupAddress, GetPupContentsBytes, PupBuffer, ReturnFreePupBuffer,
MsToTocks, PupAddress, PupNameTrouble,
PupSocketMake, PupSocketDestroy,
SetPupContentsWords],
PupStatsDefs: FROM "PupStatsDefs" USING [
gateControlExamine, gateControlStatsAck, gateControlDeposit],
PupTypes: FROM "PupTypes" USING [fillInSocketID];

GCMisc: PROGRAM [gc: GCDefs.GCInterface]
IMPORTS DisplayDefs, IODefs, PupDefs, GCDefs
EXPORTS GCDefs = BEGIN
OPEN gc
, IODefs, PupDefs, GCDefs;

sysDispPages: CARDINAL ← 40;
socketOn: BOOLEAN ← FALSE;

GetFailure
: PUBLIC SIGNAL = CODE;
PutFailure: PUBLIC SIGNAL = CODE;

SetDisplay: PUBLIC PROCEDURE =
BEGIN
WriteString["Display Pages? "L];
sysDispPages ← ReadNumber[sysDispPages,10 ! Rubout => GOTO Reject];
WriteLine[""];
DisplayDefs.SetSystemDisplaySize[sysDispPages,(3*sysDispPages)/2];
EXITS Reject => WriteLine["xxx"];
END;

WaitUntilKeyHit
: PUBLIC PROCEDURE =
BEGIN
stopFlag ← FALSE;
[] ← ReadChar[];
stopFlag ← TRUE;
END;

SetHost: PUBLIC PROCEDURE =
BEGIN
name: STRING ← [50];
who, address: PupAddress;
i: CARDINAL;
IF socketOn THEN PupSocketDestroy[gcSoc];
DO
address ← [myNet,myHost,[0,0]];
AppendPupAddress[name,@address];
WriteString["Host: "L];
ReadID[name ! Rubout => BEGIN WriteLine["xxx"L]; LOOP; END];
GetPupAddress[@address,name !
PupNameTrouble => BEGIN WriteLine[e]; LOOP; END];
EXIT;
ENDLOOP;
IF address.net=0 THEN address.net←myNet;
WriteString["="L];
PrintPupAddress[@address];
remHost←address;
who ← [remHost.net,remHost.host,[31415,9265]];
gcSoc ← PupSocketMake[PupTypes.fillInSocketID,who,MsToTocks[3000]];
socketOn ← TRUE;
FOR i IN [1..maxInform] DO newPartner[i] ← TRUE; ENDLOOP;
WriteLine["."L];
END;

FixHostAndNet: PUBLIC PROCEDURE =
BEGIN
me: PupAddress ←
GetLocalPupAddress[[0,0],NIL];
myHost ← me.host;
myNet ← me.net;
END;

YesOrNo: PUBLIC PROCEDURE[s: STRING] RETURNS[b: BOOLEAN] =
BEGIN OPEN IODefs;
WriteString[s];
WriteChar[SP];
SELECT ReadChar[] FROM
’Y, ’y, CR => b ← TRUE;
ENDCASE => b ← FALSE;
WriteLine[IF b THEN "Yes" ELSE "No"];
END;

Read: PUBLIC PROCEDURE [p: POINTER] RETURNS [x: UNSPECIFIED] =
BEGIN
Get[to: @x, from: p, size: 1];
END;

Get: PUBLIC PROCEDURE [to, from: POINTER, size: CARDINAL] =
BEGIN OPEN PupDefs;
b: PupBuffer;
i,j: CARDINAL ← 0;
IF size>250 THEN ERROR;
DO -- until we get the answer
IF i>2 OR j>6 THEN GetFailure[];
b ← GetFreePupBuffer[];
b.pupID ← [27182,i];
b.pupWords[0] ← LOOPHOLE[from];
b.pupWords[1] ← size;
b.pupType ← PupStatsDefs.gateControlExamine;
SetPupContentsWords[b,2];
gcSoc.put[b];
b ← gcSoc.get[];
IF b#NIL
AND b.pupID=[27182,i]
AND b.pupType=PupStatsDefs.gateControlStatsAck
AND GetPupContentsBytes[b]=2*(2+size) THEN EXIT;
IF b#NIL THEN
BEGIN
j ← j+1;
PrintErrorPup[b];
ReturnFreePupBuffer[b];
END
ELSE i ← i+1;
ENDLOOP;
FOR i IN [0..size) DO (to+i)↑←b.pupWords[2+i]; ENDLOOP;
ReturnFreePupBuffer[b];
END;

Put: PUBLIC PROCEDURE [to, from: POINTER, size: CARDINAL] =
BEGIN OPEN PupDefs;
b: PupBuffer;
i: CARDINAL ← 0;
IF size>250 THEN ERROR;
DO -- until we get the answer
i ← i+1;
b ← GetFreePupBuffer[];
b.pupWords[0] ← LOOPHOLE[to];
b.pupWords[1] ← size;
b.pupType ← PupStatsDefs.gateControlDeposit;
FOR i IN [0..size) DO b.pupWords[2+i] ← (from+i)↑; ENDLOOP;
SetPupContentsWords[b,size+2];
b.pupID ← [27182,i];
gcSoc.put[b];
b ← gcSoc.get[];
IF b#NIL
AND b.pupID=[27182,i]
AND b.pupType=PupStatsDefs.gateControlStatsAck
AND GetPupContentsBytes[b]=0 THEN EXIT;
IF b#NIL THEN ReturnFreePupBuffer[b];
IF i>4 THEN PutFailure[];
ENDLOOP;
ReturnFreePupBuffer[b];
END;

-- initialization
END.