	.title	K1180S	Server things unique to the exec we are using
	.ident	/2.0.06/
	.include	/SY:[1,2]COMMON.MAC/
	.include	/IN:K11MAC.MAC/
	.iif ndf, xrb	, .error ; INCULDE for [1,2]COMMON.MAC failed
	.iif ndf, k11inc, .error ; INCLUDE for IN:K11MAC.MAC failed
	.title	K1180S

	.psect	$code
	.enabl	lc
	.enabl	gbl


;	do a SYSTAT
;
;	01-Feb-84  11:11:34  Brian Nelson
;
;	We have several options for doing the remote WHO command.
;	First of all, we could spawn a job on a PK (or VT for M+)
;	and get  the output sent to a disk file and then send the
;	disk file over to the  reqesting Kermit.  This would have
;	the  advantage of keeping the command consistant with the
;	system managers desires.
;	The other option,  of course, is to do it ourself via the
;	appropiate  monitor directives  to get  that information.
;	That  option is really  only available for RSTS since RSX
;	does not have the directives needed to get that info from
;	the exec.  For now, I will  use the second option.  To be
;	used,  Kermit must run with  temporary privileges (RSTS).
;	To  patch out,  add (at task build time) the following to
;	the tkb command file.
;
;	GBLPAT=K11WHO:SYSTAT+0:240
;
;	It would, of course, be fairly straight forward to do the
;	SYSTAT  on a PK as I already  have the code to log output
;	to disk.
;
;
;	input:	@r5	value of channel to do i/o on, zero --> terminal
;	output:	r0	rsts error code if any.


	.macro	dosys	jobnum	,type
	mov	type	,-(sp)
	mov	jobnum	,-(sp)
	call	.dosys
	cmp	(sp)+	,(sp)+
	.endm	dosys




	.sbttl	the real work of systat

systat::br	5$			; skip the error exit
	mov	#10.	,r0		; return protection violation
	return				; bye

5$:	save	<r1,r2,r3,r4,r5>	; save all registers please
	sub	#120	,sp		; allocate a text buffer
	call	getprv			; we need temp privs
	mov	#512.	,xrb+0		; try a peek to see if we have
	.peek				; priv's to run with
	movb	firqb	,r0		; well ?
	bne	100$			; none, so exit please

	clr	r4			; index # of job to do


10$:	mov	sp	,r3		; point to our local buffer now
	inc	r4			; jobnumber := jobnumber+1
	dosys	r4	,#0		; do the uu.sys part 0 please
	tstb	r0			; did it work ?
	beq	20$			; yes
	cmpb	r0	,#10.		; no job of such number present?
	beq	80$			; yes, just do the next job then
	br	100$			; no, exit then as we are all done
20$:	tstb	firqb+5			; is the job we found attached ?
	bmi	80$			; yes, skip it then
	deccvt	r4,r3,#3		; convert job number to ascii
	add	#3	,r3		; and point to end of the string
	movb	#40	,(r3)+		; stuff a space in
	movb	#40	,(r3)+		; stuff a space in
	call	cvtppn			; convert the ppn next
	movb	#40	,(r3)+		; again a space please
	call	cvtkb			; get the kb number next
	mov	#firqb+22,r1		; get the program name next
	call	rad			; convert to ascii and fix pointer
	dosys	r4	,#1		; get the current size now
	movb	firqb+16,r0		; where it returned the size
	deccvt	r0,r3,#5		; convert with at least 3 spaces
	add	#5	,r3		; move the pointer right along
	movb	#'K	,(r3)+		; the size please
	movb	#40	,(r3)+		; spaces again
	movb	#40	,(r3)+		; spaces again
	movb	#40	,(r3)+		; spaces again
	dosys	r4	,#0		; get the info part zero back
	mov	#firqb+34,r1		; and the current RTS name now
	call	rad			; convert to ascii and fix pointer
	clrb	@r3			; all done at last
	mov	sp	,r3		; point back to the buffer
	call	doio			; do the i/o at last
80$:	br	10$			; next please


100$:	cmpb	r0	,#18.		; end of the table ?
	bne	110$			; no
	clr	r0			; yes
110$:	add	#120	,sp		; pop the local buffer
	unsave	<r5,r4,r3,r2,r1>	; pop temps and exit
	call	drpprv			; please do this
	return





	.sbttl	utilities and the actual i/o

doio:	tst	@r5			; channel zero today ?
	bne	10$			; no, assume disk then
	print	r3
	print	#200$
	br	100$
10$:	strlen	r3			; try to a disk file now
	calls	putrec	,<r3,r0,@r5>	; write to the passed LUN
100$:	return

200$:	.byte	cr,lf,0
	.even


rad:	calls	rdtoa	,<r3,(r1)+>	; common code to to rad50 cvt
	add	#3	,r3		; pointer := pointer + 3
	calls	rdtoa	,<r3,(r1)+>	; common code to to rad50 cvt
	add	#3	,r3		; pointer := pointer + 3
	return




	.sbttl	misc utilities

cvtppn:	save	<r0,r1>			; save temps please
	sub	#20	,sp		; convert ppn to ascii next
	mov	sp	,r0		; a pointer to the ppn
	clr	r1			; get the project number now
	bisb	firqb+27,r1		; ok
	bne	10$			; if <> 0 then a real account
	mov	#200$	,r1		; if eq 0 then not logged it yet
	br	20$			; copy over *,*
10$:	deccvt	r1,r0,#3		; convert it to decimal
	add	#3	,r0
	movb	#',	,(r0)+		; stuff a comma in please
	clr	r1			; get the programmer number now
	bisb	firqb+26,r1		; ok
	deccvt	r1,r0,#3		; convert it to decimal
	mov	sp	,r1		; point to the buffer now
20$:	movb	#40	,(r3)+		; stuff a space into our result
	mov	#7	,r0		; seven characters to copy
30$:	movb	(r1)+	,(r3)+		; and copy the rest of it
	sob	r0	,30$		; simple
	movb	#40	,(r3)+		; stuff a space into our result
	movb	#40	,(r3)+		; stuff a space into our result
	movb	#40	,(r3)+		; stuff a space into our result
	add	#20	,sp		; pop the local buffer
	unsave	<r1,r0>
	return
	
200$:	.asciz	/  *,  */
	.even

cvtkb:	save	<r0,r1>			; convert kb number to ascii
	sub	#10	,sp		; allocate a local buffer
	mov	sp	,r1		; point to it
	movb	firqb+5	,r0		; KB number to convert
	movb	#'K	,(r3)+		; insert a header
	movb	#'B	,(r3)+		; simple
	deccvt	r0,r1,#3		; can't ever be more than 127
	mov	r3	,-(sp)		; saev the output pointer now
	mov	#3	,r0		; three at most to copy over
10$:	cmpb	@r1	,#40		; a space present ?
	beq	20$			; yes, please ignore it
	movb	@r1	,(r3)+		; copy it over at last
20$:	inc	r1			; next ch please
	sob	r0	,10$		; simple
	movb	#40	,(r3)+		; insert some spaces now
	movb	#40	,(r3)+		; insert some spaces now
	movb	#40	,(r3)+		; insert some spaces now
	movb	#40	,(r3)+		; insert some spaces now
	mov	(sp)+	,r3		; restore the old pointer
	add	#5	,r3		; say we copied 5+2 characters over
	add	#10	,sp		; pop the local buffer and exit
	unsave	<r1,r0>			; pop registers
	return
	

clrfqb:	mov	r0	,-(sp)		; simple clear out the firqb
	mov	#40	,r0		; number of bytes to do
10$:	clrb	<firqb-1>(r0)		; simple to do
	sob	r0	,10$		; for all the bytes but first
	mov	(sp)+	,r0
	return


.dosys:	call	clrfqb			; clear the firqb out first
	movb	#uu.sys	,firqb+fqfun	; do a systat call to RSTS
	movb	2(sp)	,firqb+4	; job number to do it for
	movb	4(sp)	,firqb+5	; which type (0 or 1)
	.uuo				; simple
	movb	firqb	,r0		; return with error code in r0
	return				; bye

	.end
