// read.sr


get "BRAVO1.DF";
get "CHAR.DF";
get "VM.DF";
get "DOC.DF";
get "COM.DF"
get "RN1.DF"
get "DISPLAY.DF"
get "SELECT.DF"


// Incoming procedures

external	[
	readsel
	ClearBuf
	deleted
	stsize
	movec
	setmag
	getfontc
	fnalloc
	processtsesc
	SetRegionW
	stcompare
	SetRegionSys
	FtyOpen
	updatedisplay
	invalidatedisplay
	insertd
	move
	enww
	unsetdirty
	setsel
	setupdate
	underline
	stget
	FChInSb
	stnum
	ReadDiskKd;
	WriteDiskKd;
	]


// Incoming statics

external	[
	vformattedfile
	chcom
	selmain
	tsread
	vrlwsys
	mpfnof
	mpfnsb
	vdlhint
	vpc
	selarg
	selaux
	rgmaccp
	mpWwWwd
	dcpendofdoc
	]


// Outgoing procedures

external	[
	oreadfile
	oreadfile1;
	GetFile
	]


// Outgoing statics

external	[ 
	ccycles;
	centries;
	dnfun;
	] 


// Local statics

static	[ 
	ccycles;
	centries;
	dnfun;
	] 


// Local manifests -- guess why?

manifest	[
	ftyNil = -1; ftyOld = 1; ftyNewFile = 2; ftyNewVer = 3
	ftyNewOnDollar = 4; ftyNewOnOld = 5
	ftyOverwrite = 6

	maxfun = 11
	]


// O R E A D F I L E
// 
let oreadfile(selfnam, doc, cp) = valof 
[ 
let ab = nil
let sbfnam = vec sbfnaml;
readsel(sbfnam, selfnam >> SEL.doc, selfnam >> SEL.cpfirst, selfnam >> SEL.cplast, (sbfnaml lshift 1)-1);
let legalsiz = ofnamfilter(sbfnam);
if legalsiz eq mastx-3 then
	[
	ab<<AB.crid = 2		// Filename too long -
	ab<<AB.nrid = 4
	ClearBuf(3);
	deleted(doctx3);
	resultis ab
	]
if legalsiz ne stsize(sbfnam) then
	[
	ab<<AB.crid = 2		//Illegal character in filename -
	ab<<AB.nrid = 6
	ClearBuf(3);
	deleted(doctx3);
	resultis ab
	] 
// A
l1:
let tdnfun = vec maxfun;
dnfun = tdnfun;
movec(dnfun, dnfun+maxfun-1, false);
vformattedfile = (chcom eq ctrlz ? false, true);
ab = oreadfile1(sbfnam, doc, cp)
test ab eq abnil ifso
	[ 
//	setmag(selmain >> SEL.ww);
//	for fun = 0 to maxfun-1 do
//		if dnfun ! fun then
//			getfontc(fun);
	resultis abmsg
	] 
ifnot	resultis ab
] 

// A
// let sbdollar = vec (sbfnaml+1);
// NormalizeSbFile(sbfnam);
// stcopy(sbdollar, sbfnam)
// sbdollar >> lh = sbdollar >> lh-1;
// stappend(sbdollar, "$.");
// for fn = minuserfn to maxfn-1 do
// 	if mpfnof ! fn ne -1 do
// 		[ if stcompare(sbdollar, mpfnsb ! fn) eq 0 then
// 			[ stcopy(sbpast, "File temporarily inaccessible - ")
// 			resultis false;
// 			] 
// 		if stcompare(sbfnam, mpfnsb ! fn) eq 0 then
// 			[ setlp(lpsyspast, "Getting the same file twice can mean trouble!!! Type CR to proceed, DEL to abort")
// 			updatedisplay()
// 				[ let tchar = bravochar()
// 				if tchar eq chdel then resultis false
// 				if tchar eq chcr then goto l1;
// 				] repeat
// 			] 
// 		] 


// O R E A D F I L E 1
//
and oreadfile1(sbFile, doc, cp) = valof
[ 
let ab, trid = nil, nil
let tfn = fnalloc();
if tfn eq -1 then 
	[
	ab<<AB.crid = 2		// Too many open files -
	ab<<AB.nrid = 8
	resultis ab
	] 
centries = 0;
ccycles = 0;
if tsread then processtsesc($G, sbFile);
SetRegionW(vrlwsys, 0, sbFile)
trid<<RID.nrl = 1;	trid<<RID.ri = 0
if stcompare(sbFile, "") eq 0 then
	[  
//  Could not open filename
	SetRegionSys(risyspast, 98, trid)
	resultis abmsg
	] 
ReadDiskKd();
let tfty = FtyOpen(tfn, sbFile, false);
WriteDiskKd();
if tfty eq ftyNil then
	[  
	unless tsread then processtsesc($G, "");
	resultis abmsg
	] 
if (mpfnof ! tfn) >> OF.macpos eq 0 then
	[  
// File filename empty - Command terminated
	SetRegionSys(risyspast, 207, trid, 209, 50)
	unless tsread then processtsesc($G, "");
	resultis abmsg
	] 
SetRegionSys(risyspast, 96)	// Reading file
updatedisplay();
unless tsread then processtsesc($G, mpfnsb ! tfn);
let l = (mpfnof ! tfn) >> OF.macpos;
invalidatedisplay(doc, cp, vdlhint);
vpc = insertd(doc, cp, l, tfn, 0, ((mpfnof ! tfn) >> OF.formatted) ? 0, -1);
ofilemessage(mpfnsb ! tfn, (mpfnof ! tfn) >> OF.macpos, false, tfty);
resultis abnil
] 


// G E T F I L E
//
and GetFile(cf) = valof
[
let doc = selmain >> SEL.doc
selmain>>SEL.type = snone
let ab = oreadfile(selarg, doc, 0)
if ab eq abmsg then
	[ 
	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);
	test selmain>>SEL.type eq snone ifso
		[
		setsel(selmain, 0, 0);
		selmain>>SEL.type = schar
		]
	ifnot	[
		let cpFirst = selmain>>SEL.cpfirstorig
		let ww = selmain>>SEL.ww
		setsel(selmain, cpFirst, selmain>>SEL.cplastorig)
		(mpWwWwd ! ww)>>WWD.cpFDispl = cpFirst
		setupdate(ww)
		SetRegionSys(risyspast, 75)
		]
	underline(ulmode1, selmain)
	] 
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)
]