	.TITLE	HelSub
	.Ident	/Ver 01/

;+
;
; *** HelSub -- subroutine for "Hello" utility to log user's
;		last name in "MAILLOG.Dat",indexed by terminal number
;
;	Written By:
;
;		Michael Lynch
;		Engineering Systems and Technology Labs
;		3M Co.
;		3M Center
;		St. Paul, Mn.
;
;	Inputs:
;		"[0,0]Rsx11.sys" is open via FDB address = $ACTFL
;		R0 - pointer to user's entry in account file
;
;	Outputs:
;		C-bit cleared if O.K. to log in user
;		C-bit set if not
;
;-

	.MCALL	FDOF$L,FCSBT$S,FDBDF$,FDAT$A,FDRC$A,FDOP$A,FDBF$A
	.MCALL	CLOSE$,OPEN$W,PUT$R,OPEN$A,Open$U,Get$r,GLUN$S
	.MCall	Mrkt$S,WtSe$S

TTyLun=1
TTyEfn=1

Msg1:	.Ascii	<12><12><15>"Hel - I am unable to Open Dm1:[10,3]Maillog.dat"
Msg1Lh: . - Msg1

	.Even

Errcnt:	.word	0		;count of MAILLOG.DAT open failures
LunBuf:	.BlkW	6.

$UseSb::			;reference label expected by HELLO
	Add	#A.lnm,R0	;point to start of last name
	Mov	#LogSiz,R1	;set up length of leat name field
	Mov	#LogBuf,R2	;point to MAILLOG.DAT file I/O buffer

10$:	MovB	(R0)+,(R2)+	;copy user's last name into buffer
	Sob	R1,10$		;loop back till done

	Close$	#$ACTFL		;close account file for now

	GLUN$S	#1,#LunBuf	;get ti: info
	Clr	R1		;set up record pointer
	BisB	LunBuf+2,R1	;get unit number
	Inc	R1		;TT0: is the 1st record
	Mov	#1,Errcnt	;allow only 1 retry

20$:	Open$A	#LogFDB			;open log file for append
	Bcc	60$			;if no error, jump around
	CmpB	#Ie.Nsf,F.Err(R0)	;if error, maybe we need to create file
	Beq	40$			;if so, jump around
	Tst	Errcnt			;too many open failures ?
	Beq	30$			;if so, give up and reject log-on
	Mrkt$s	#Fillun,#30.,#1		;wait 1/2 second
	WtSe$S	#fillun			;wait here
	Dec	Errcnt			;keep track of errors
	Br	20$			;loop back and try again

30$:	Call	100$			;re-open account file
	Qiow$S	#Io.Wvb,#TTyLun,#TTyEfn,,,,<#Msg1,#Msg1Lh>
	Return				;return to "Hello"

40$:	Open$W				
	Bcs	110$			;now if error, give up
	Mov	R1,R2			;set up loop
50$:	Put$R	#LogFdb,#Blank,#LogSiz,R2,#0	;zero file up to this user
	Sob	R2,50$			;loop back till done
	Br	90$			;write in this user

60$:	Mov	#1,R2			;start with record #1
70$:	Get$R	#LogFDB,#Junk,#LogSiz,R2,#0	;read next record
	Bcs	80$
	Inc	R2			;point to next record
	Br	70$			;loop back and read it

80$:	Cmp	R2,R1			;all done?
	BHis	90$			;if so, jump around
	Put$R	#LogFDB,#Blank,#logSiz,R2,#0	;make up new record
	Inc	R2			;point to next record
	Cmp	R2,#200.		;have we somehow screwed up ?
	Bhis	100$			;if so, get out now
	Br	80$			;loop back for more

90$:	PUT$R	#LogFDB,#LogBuf,#LogSiz,R1,#0 ;write user's last name
100$:	Close$				;close the file

110$:	Open$U	#$ACTFL,,,#FD.RWM	;re-open the account file
	ClC			;accept the log-in
	Return

	.PAGE
	.SBTTL	File I/O Macros and processing

 
	FilLun = 2			;use account file lun
	FilEfn = 1			;use account file Event Flag
 
	FDOF$L				;DEFINE FDB OFFSETS
	FCSBT$S				;DEFINE FDB BIT PARAMETERS
LogFDB:	FDBDF$				;DEFINE FDB FOR Log file
	FDAT$A	R.fix,,LogSiz		;define type and size of records
	FDRC$A	FD.ran,LogBuf,LogSiz	;BUFFER LOCATION,LENGTH
					;MOVE MODE SPECIFIED BY DEFAULT
	FDOP$A	FilLun,DatDsc,,,fa.dlk
	FDBF$A	FilEfn			;FCS EVENT FLAG FOR THIS FILE

DATDSC:	.WORD	DEVSIZ,DEVNAM		;DEVICE NAME DESCRIPTOR
	.WORD	DIRSIZ,DIRNAM		;UIC DESCRIPTOR
	.WORD	FILSIZ,FILNAM		;FILE NAME DESCRIPTOR
 
DEVNAM:	.ASCII	"DM1:"
DEVSIZ=	.-DEVNAM
DIRNAM:	.ASCII	"[010,003]"
DIRSIZ=.-DIRNAM
 
FILNAM:	.ASCII	"MAILLOG.DAT"
FILSIZ=.-FILNAM
 
LogBuf:
	.BlkB	14.			;last name of logged in user
LogSiz=.-LogBuf
Blank:	.Rept	LogSiz
	.byte	0
	.EndR

Junk:	.BlkB	LogSiz			;place to put junk input records
	.even

	.End
