	.title	acreset - Reset accounting functions at midnight
	.ident	/first/
;
	.mcall	exit$s,dir$,crvt$,spwn$,wtse$,qiow$,astx$s,qiow$s,alun$,mrkt$s
	.mcall	setf$s
;
alun:	alun$	2,vt,0
create:	crvt$	inast,outast
spawn:	spwn$	mcr...,,,,,1,exast,exstat,cmd1,cmd1l,unit
wait:	wtse$	1
qio:	qiow$	io.wvb,5,5,,iosb,,<buf,bufl,0>
qiovt:	qiow$	io.rlb,2,2,,iosb,,<buf,bufl,0>
;
exstat:	.blkw	8.		; status return from mcr
unit:	.word	0		; vt unit number
iosb:	.word	0,0
buf:	.blkb	80.
bufl	= .-buf
argblk:	.blkw	10
;
cmd1:	.ascii	"hel 1/1/syst"
cmd1l	= .-cmd1
cmd2:	.ascii	"@lb:[1,2]accountup"
cmd2l	=.-cmd2
cmd3:	.ascii	"bye"
cmd3l	= .-cmd3
;
errmsg:	.asciz	/Error at %P, DSW = %P, IOSB = %2P/
inmsg:	.ascii	/Input ast/
inmsgl	= .-inmsg
attm:	.ascii	/Device attached/
attml	= .-attm
.even
;
reset::
	dir$	#create			; create virtual terminal
	bcc	10$
	mov	#1,r5
	jmp	error
;
10$:
	mov	@#$dsw,unit		; save unit number
	mov	unit,alun+a.lunu	; set vt unit number
	dir$	#alun			; assign unit to vtn:
	bcc	15$
	mov	#2,r5
	jmp	error
15$:
	mov	unit,spawn+s.pwvt	; set up unit number
	mov	#cmd1,spawn+s.pwca	; command address
	mov	#cmd1l,spawn+s.pwcl	;  and length
	dir$	#spawn			; send out first command
	bcc	20$
	mov	#3,r5			; set error ind
	jmp	error
20$:
	dir$	#wait
	mov	#cmd2,spawn+s.pwca	; command address
	mov	#cmd2l,spawn+s.pwcl	;  and length
	dir$	#spawn			; send out next one
	bcc	30$
	mov	#4,r5
	jmp	error
30$:
	dir$	#wait
	mov	#cmd3,spawn+s.pwca	; command address
	mov	#cmd3l,spawn+s.pwcl	;  and length
	dir$	#spawn			; send out next one
	bcc	40$
	mov	#5,r5
	jmp	error
40$:
	dir$	#wait
	exit$s				; end of prog
;
error:	
	mov	#argblk,r1
	mov	r5,(r1)+
	mov	@#$dsw,(r1)+
	mov	iosb,(r1)+
	mov	iosb+2,(r1)+
;
	mov	#buf,r0
	mov	#errmsg,r1
	mov	#argblk,r2
	call	$edmsg
	qiow$s	io.wvb,#5,#5,,,,<#buf,r1,#'0>
	exit$s
;
;.. input ast
inast:
	add	#6,sp
; there should be no input requests here
	qiow$s	io.wvb,#5,#5,,,,<#inmsg,#inmsgl,#'0>
	astx$s
;
;.. output ast
outast:
	tst	(sp)+			; pop off unit number
	mov	(sp)+,qiovt+q.iopl+2	; set up request length
	mov	(sp)+,qiovt+q.iopl+4	; set up carriage control
	dir$	#qiovt			; read VT's output
	bcc	10$			; error if cs
	mov	#6,r5			; position error
	call	error			; report it
10$:	mov	iosb+2,qio+q.iopl+2	; set up for echo
	mov	qiovt+q.iopl+4,qio+q.iopl+4 ; copy vfc 
	dir$	#qio			; do it
	astx$s
;
;.. attach ast
;;attast:
;;	add	#6,sp
;;	qiow$s	io.wvb,#5,#5,,,,<#attm,#attml,#'0>
;;	astx$s
;
;.. exit ast
exast:
	tst	(sp)+
	setf$s	#1			; set flag 1 on exit (for AT. ??)
	astx$s
;
	.end	reset
