	.TITLE	FIXMAIL
;
;FIXMAIL - Utility to examine/clear the VAX/VMS MAIL new message count for
;	specified usernames in the file SYS$SYSTEM:VMSMAIL.DAT
; 4/14/86 - Initial version - D.Adachi
;
	.PSECT DATA,WRT,NOEXE,LONG
	.ALIGN LONG
MAIL$T_SYSMAIFAB::
	$FAB -	;RMS macro to declare File Access Blk
		FNM=<SYS$SYSTEM:VMSMAIL.DAT>,-	;file name
		FAC=<GET,PUT,DEL,UPD>, -	;file access types
		FOP=<CIF,CBT>, -		;contiguous file
		SHR=<GET,PUT,DEL,UPD>, -	;file sharing
		ORG=IDX, -			;indexed file
		RFM=VAR, -			;variable-length record
		ALQ=10, -			;allocation blocks
		DEQ=10				;alloc blks for extension
MAIL$T_SYSMAIRAB::
	$RAB -	;RMS macro to declare Record Access Blk
		FAB=MAIL$T_SYSMAIFAB,-		;addr of file access blk
		RAC=KEY,-			;rec access: random by key
		ROP=<RLK,NLK,UIF>,-		;rec locking; update if duplic key
		KRF=0,-				;primary key
		KSZ=31,-			;length of key (username)
		KBF=MAIL$L_SYSMAIBUF,-		;addr of key buffer
		UBF=MAIL$L_SYSMAIBUF,-		;user rec area addr
		USZ=833		;VMD_C_LENGTH+3*NAM$C_MAXRSS (from macro expansion listing)
		;RBF=MAIL$L_SYSMAIBUF,-		;record buffer address
MAIL$L_SYSMAIBUF::	.BLKB	4
MAIL$L_USRSYSBUF::	.BLKB	4
TTNAME:	.ASCID	/SYS$INPUT:/
TTCHAN:	.BLKW	1
TTIOSB:	.BLKW	1
TTIOLN:	.BLKW	1
	.BLKL	1
OUTLEN:	.BLKL	1
INBUF:	.BLKB	80
GREET:	.ASCII	/FIXMAIL UTILITY PROGRAM/
PRMPT:	.ASCII	/USERNAME:/
RNFMSG:	.ASCII	/USER NOT IN VMSMAIL.DAT!/
DMSG:	.ASCII	/ new mail msgs for /
ASK:	.ASCII	/Clear this count?/
CLRD:	.ASCII	/Cleared.../

	.PSECT	PROG,LONG
	.ENTRY	FIXMAIL,^M<IV>
	$ASSIGN_S	DEVNAM=TTNAME, CHAN=TTCHAN
	MOVL	#23,OUTLEN
	$OUTPUT	CHAN=TTCHAN, LENGTH=OUTLEN, BUFFER=GREET, IOSB=TTIOSB
	TSTL	MAIL$L_SYSMAIBUF	;any ptr to buffer?
	BNEQ	1$
	PUSHAB	MAIL$L_SYSMAIBUF	;addr to receive buffer addr from LIB$GET_VM
	MOVZWL	#833,4(SP)		;alloc 833 bytes for buffer
	PUSHAB	4(SP)			;pass the addr of buffer size
	CALLS	#2,G^LIB$GET_VM		;routine to alloc memory
	BLBC	R0,2$			;signal error if any
1$:	TSTL	MAIL$L_USRSYSBUF	;any ptr to buffer?
	BNEQ	3$
	PUSHAB	MAIL$L_USRSYSBUF	;addr to receive buffer addr from LIB$GET_VM
	MOVZWL	#833,4(SP)		;alloc 833 bytes for buffer
	PUSHAB	4(SP)			;pass the addr of buffer size
	CALLS	#2,G^LIB$GET_VM		;routine to alloc memory
	BLBS	R0,3$			;jmp if no error
2$:	PUSHL	R0			;error status from LIB$GET_VM
	CALLS	#1,G^LIB$SIGNAL
	$EXIT_S
3$:	$OPEN	FAB=MAIL$T_SYSMAIFAB	;open TSTMAIL.DAT
	BLBS	R0,4$			;signal error if any
	PUSHL	R0			;error status from SYS$OPEN
	CALLS	#1,G^LIB$SIGNAL
	$EXIT_S
4$:	$CONNECT -			;associate record with file
		RAB=MAIL$T_SYSMAIRAB	;RAB already contains FAB name
	BLBS	R0,USRLP			;signal error if any
	PUSHL	R0			;error status from SYS$CONNECT
	CALLS	#1,G^LIB$SIGNAL
	$EXIT_S
USRLP:	MOVL	#9,OUTLEN
	$OUTPUT	CHAN=TTCHAN, LENGTH=OUTLEN, BUFFER=PRMPT, IOSB=TTIOSB
	$INPUT	CHAN=TTCHAN, LENGTH=#80, -
		BUFFER=INBUF, IOSB=TTIOSB
	MOVZWL	TTIOLN,R6		;number of chars input
	CMPL	R6,#0
	BGTR	6$
	JMP	QUIT
6$:	MOVL	MAIL$L_SYSMAIBUF,R5	;USRSYSBUF holds address of key buffer
	MOVL	MAIL$L_SYSMAIBUF,MAIL$T_SYSMAIRAB+48	;modify KBF in RAB
	MOVL	R6,MAIL$T_SYSMAIRAB+52	;modify KSZ in RAB to be username length
	INCL	MAIL$T_SYSMAIRAB+52	;accommodate blank terminator
	MOVAL	INBUF,R4		;copy username from terminal buffer
INLP:	MOVB	(R4)+,(R5)+		; to RMS record key buffer
	SOBGTR	R6,INLP
	MOVB	#32,(R5)+		;append blank terminator
	MOVL	MAIL$L_USRSYSBUF,MAIL$T_SYSMAIRAB+36	;modify UBF addr in RAB
	CLRL	MAIL$T_SYSMAIRAB+40	;no RBF specified in RAB for $GET
	$GET	RAB=MAIL$T_SYSMAIRAB	;ERR=ERR_DISPLAY
	BLBS	R0,10$
	CMPL	R0,#RMS$_RNF		;record not found?
	BEQL	8$
	PUSHL	R0			;error status from SYS$GET
	CALLS	#1,ERR_DISPLAY
8$:	MOVL	#24,OUTLEN
	$OUTPUT	CHAN=TTCHAN, LENGTH=OUTLEN, BUFFER=RNFMSG, IOSB=TTIOSB
	JMP	USRLP
10$:	;Display new msg count for the username:
	MOVL	MAIL$L_USRSYSBUF,R5	;start of RMS rec buffer read by $GET
	MOVZWL	33(R5),R6		;newmsg count is word (MAILDEF.SDL)
	CVTLP	R6,#3,OUTLEN		;convert num to packed decimal
	CVTPS	#3,OUTLEN,#3,INBUF	;convert packed decimal to ascii string
	ADDL3	#4,R3,R4		;3 digits plus sign char
	MOVAL	DMSG,R6			;copy part of msg txt
	MOVC3	#19,(R6),(R4)
	MOVL	R3,R4			;MOVC3 returns end addr in R3
	MOVL	MAIL$L_USRSYSBUF,R5	;username is at beginning of rec buffer
	MOVL	#23,R6
OUTLP:	MOVB	(R5)+,(R4)+		;copy username to msg buffer
	INCL	R6
	CMPB	(R5),#32		;trailing blank?
	BNEQ	OUTLP
	MOVL	R6,OUTLEN
	$OUTPUT	CHAN=TTCHAN, LENGTH=OUTLEN, BUFFER=INBUF, IOSB=TTIOSB
	;Ask user if the new msg count is to be cleared:
	MOVL	#17,OUTLEN
	$OUTPUT	CHAN=TTCHAN, LENGTH=OUTLEN, BUFFER=ASK, IOSB=TTIOSB
	$INPUT	CHAN=TTCHAN, LENGTH=#80, -
		BUFFER=INBUF, IOSB=TTIOSB
	CMPB	#89,INBUF	;Y?
	BNEQ	NEXT
	;Update record with zeroed new msg count:
	MOVL	MAIL$L_USRSYSBUF,R5
	CLRW	33(R5)		;newmsg count is word (MAILDEF.SDL)
	MOVL	MAIL$L_USRSYSBUF,MAIL$T_SYSMAIRAB+RAB$L_RBF	;modify RBF addr in RAB
	MOVW	#833,MAIL$T_SYSMAIRAB+RAB$W_RSZ		;modify RSZ in RAB
	$PUT	RAB=MAIL$T_SYSMAIRAB,ERR=ERR_DISPLAY
	BLBS	R0,OKAY			;signal error if any
	PUSHL	R0			;error status from SYS$CONNECT
	CALLS	#1,G^LIB$SIGNAL
	$EXIT_S
OKAY:	MOVL	#10,OUTLEN
	$OUTPUT	CHAN=TTCHAN, LENGTH=OUTLEN, BUFFER=CLRD, IOSB=TTIOSB
NEXT:	JMP	USRLP			;prompt for next username
QUIT:	$DASSGN_S	CHAN=TTCHAN
	$EXIT_S

.ENTRY	ERR_DISPLAY,^M<>
	MOVL	4(AP), R1		;arg passed is status
	MOVL	FAB$L_STS(R1), R0	;interpret status code
	CMPL	R0, #RMS$_EOF
	BEQL	99$
	PUSHL	R0
	CALLS	#1, G^LIB$STOP
99$:	RET
.END FIXMAIL
