// READWRITE  module

get "BRAVO1.DF";
get "CHAR.DF";
get "DISPLAY.DF";
get "SELECT.DF";
get "DOC.DF";
get "ALTOFILESYS.D";
get "ST.DF";
get "DIR.DF";
get "com.df"
get "rn1.df"

// Incoming procedures

external
	[
	SetRegionW
	ugt;
	ult;
	underline;
	selectwholedoc;
	MakeCurrentBuf
	inserttx;
	deleted;
	getvch;
	updateunderline;
	updatedisplay;
	cpvisible;
	setsel;
	confirm;
	SetRegionSys
	move;
	unsetdirty;
	enww;
	stnum;
	oreadfile; 
 	owritefile;
	puts;
	stget;
	tsmakegood;
	tsflush;
	gets;
	errhlt;
	stput;
	umin;
	resetts;
	stsize;
	FChInSb;
	selectwholeww;
	deletea
	invalidatedoc
	]


// Incoming statics

external
	[
	vrlwsys
	tscorrect;
	tsread;
	chcom;
	rgmaccp;
	vdoc;
	vcp;
	rgpctb;
	vpc;
	selaux;
	rgsbcom;
	comno;
	vmessage;
	vmapstatus;
	char;
	fnts;
	mpWwWwd;
	dcpendofdoc
	]

// Outgoing procedures

external
	[
	oreadwritecoms;
	ofnamfilter;
	ofilemessage;
	processtsesc;
	]




// O R E A D W R I T E C O M S
//
let oreadwritecoms(cf) = valof
[ 
let ab = abnil
let selmain = cf>>CF.sel;	let selarg = cf>>CF.selarg
let tcplast = nil;
let tpc = nil;
// let docselm = selmain >> SEL.doc;
// let cpfirstselm = selmain >> SEL.cpfirst;
// let cplastselm = selmain >> SEL.cplast;
if selmain >> SEL.type eq snone then 
	[ 
	ab<<AB.crid = 2
	ab<<AB.nrid = 2		//sbselemp
	resultis ab
	] 
let wwselm = selmain >> SEL.ww;
let wwdselm = mpWwWwd ! wwselm
let wwDocName = wwdselm >> WWD.wwgroup
let wwdDocName = mpWwWwd ! wwDocName
let ww = wwDocName+1
let doc = (mpWwWwd ! ww) >> WWD.doc
selmain >> SEL.ww = ww
selmain >> SEL.doc = doc
let mppccp = nil;
unless ugt(rgmaccp ! doc,dcpendofdoc) then
	if chcom eq $p then
	[
	ab<<AB.crid = 2
	ab<<AB.nrid = 6		//Document empty, PUT -
	resultis ab
	] 
let tcpendofdoc = rgmaccp ! doc-dcpendofdoc-1;
if chcom eq $p then
	setsel(selmain,0,tcpendofdoc)
underline(ulmode1,selmain);
// if rgmaccp ! doctx3 ne 0 then
// 	[ 
// 	selectwholedoc(selarg,doctx3);
// 	MakeCurrentBuf(3);
// 	] 
unless inserttx(1000,wwDocName) do
	resultis false;
if rgmaccp ! (wwdDocName >> WWD.doc) eq dcpendofdoc then
	[
	ab<<AB.crid = 2
	ab<<AB.nrid = 0		//No file name - 
	resultis ab;
	] 
vdoc = wwdDocName >> WWD.doc;
vcp = 0;
vmapstatus = statusblind;
underline(uloff,selmain);
updateunderline();
selectwholeww(selarg,wwDocName);
selarg >> SEL.cplast = selarg >> SEL.cplast - dcpendofdoc
test (chcom eq $g) % (chcom eq ctrlz)
ifso
	[ 
// 	cpfirstselm = umin(cpfirstselm,tcpendofdoc+1);
	if (mpWwWwd ! ww) >> WWD.fDirty then
		[ unless confirm("clearing window(s) before Get") then
			[
			ab<<AB.crid = 1
			ab<<AB.nrid = 1		//Command terminated
			resultis ab
			] 
		] 
	invalidatedoc(doc)
	if ugt(rgmaccp ! doc,dcpendofdoc) then
		deletea(doc,0,rgmaccp ! doc-dcpendofdoc-1)
	setsel(selmain,0,0)
// to read.sr
// 	ab = oreadfile(selarg,doc,0)
// 	test ab eq abmsg ifso
// 		[ 
// 		move(selmain,selaux,sell);
// 		tpc = vpc;
// 		cpvisible(wwselm,cpfirstselm);
// 		mppccp = lv((rgpctb ! docselm) >> PCTB.rvmppccp);
// 		cplastselm = mppccp ! (tpc+1)-1;
// 		if (cpfirstselm eq 0) & (cplastselm+1 eq rgmaccp ! docselm-dcpendofdoc) then
// 		enww(unsetdirty,doc);
// 		setsel(selaux,0,rgmaccp ! doc-dcpendofdoc-1);
// 		setsel(selmain,0,0);
// 		underline(ulmode1,selmain)
// 		] 
// 	ifnot resultis ab
	] 
ifnot	[
// 	if ugt(selmain >> SEL.cplast,tcpendofdoc) then
// 		selmain >> SEL.cplast = tcpendofdoc;
// 	test (selmain >> SEL.cpfirst eq 0) & (selmain >> SEL.cplast eq tcpendofdoc) ifso 
	selmain >> SEL.xdlast = -1;
// 	enww(unsetdirty,doc);
// 		] 
// 	ifnot	[ if ult(selmain >> SEL.cplast,selmain >> SEL.cpfirst) then
// 			[ 
// 			ab<<AB.crid = 2	//Selection empty C T
// 			ab<<AB.nrid = 2
// 			resultis ab
// 			] 
// 		unless confirm(" if you want PARTIAL OUTPUT ONLY!!!") do 
// 			[
// 			ab<<AB.crid = 2	//PUT command terminated
// 			ab<<AB.nrid = 4
// 			resultis ab
// 			];
// 		] 
	SetRegionSys(risyspast,95);
	updatedisplay( );
	] 
resultis ab
] 


// O F N A M F I L T E R
// 
and ofnamfilter(sbfn,fsizck; numargs n) = valof
	[
	if n ls 2 then fsizck = true;
	let sbfnsiz = stsize(sbfn);
	if fsizck & (sbfnsiz ge mastx-3) then resultis mastx-3;
	let tchar = nil;
	for ich = 1 to sbfnsiz do
		[ tchar = stget(sbfn,ich);
		if ((tchar ge $A) & (tchar le $Z)) % ((tchar ge $a) & (tchar le $z)) % ((tchar ge $0) & (tchar le $9)) % (FChInSb(tchar,"+.-?!$")) then
			unless ich eq sbfnsiz do loop;
		resultis ich;
		]
	resultis -1;
	]

// O F I L E M E S S A G E
//
and ofilemessage(sbname,siz,write,fty) be
[ let tsb = vec 5;
tsb ! 0 = 0;
let trid1,trid2,trid3,trid4 = nil,nil,nil,rinil
stnum(tsb,siz,10,0,false,false,false);
SetRegionW(vrlwsys,0,tsb)
trid1<<RID.nrl = 1
trid1<<RID.ri = 0
trid2 = (write ? 76, 77)
SetRegionW(vrlwsys,1,sbname)
trid3<<RID.nrl = 1
trid3<<RID.ri = 1
if write then
	[ trid4 = valof switchon fty into
		[ 
case ftyNewFile:	resultis 78;
case ftyNewOnDollar:
default:		resultis 79;
case ftyNewOnOld:
case ftyNewVer:	resultis 101;
		] 
	SetRegionSys(risyspast,trid1,trid2,trid3,trid4)
	return
	] 
SetRegionSys(risyspast,trid1,trid2,trid3)
]