	.title	k11ini	initialization and rarely used routines
	.ident	/2.18/


;	03-Jul-84  09:34:32  Brian Nelson
;
;	Copyright (C) 1984 Change Software, Inc.
;
;
;	Remove Kermit init code and routines like SPAR and RPAR that
;	are only called once per file transfer. Placed into  overlay
;	with K11ATR.  This was  done to reduce the  task size a bit.
;	Really, the only system the size is a problem is on RT11.



	.include	/K11MAC.MAC/
	.iif ndf, k11inc, .error	; missing .INCLUDE for K11MAC.MAC
	.enabl	gbl
	.psect	$code





	.sbttl	initialize ourselves

kerini::call	init0
	call	init1
	return

init0:	mov	#rwdata	,r0		; first of all, clear all read/write
	mov	#rwsize/2,r1		; data out please
10$:	clr	(r0)+			; for i := 1 to rwdata_size
	sob	r1	,10$		;  do data[i] := 0 ;
	mov	sp	,remote		; assume remote
	mov	#-1	,setrpt		; assume we will do repeat counts
	call	rparini			; default remotes sinit parameters
	call	sparini			; initialize my sinit parameters
	mov	#mx$try	,maxtry		; initialize retry limit
	mov	#defchk	,chktyp		; set the default checksum type
	movb	#defchk	,setsen+p.chkt	; here also please
	movb	#defchk	,setrec+p.chkt	; here also please
	mov	#defdly	,sendly		; init the delay for send command
	mov	#1	,chksiz		; and the default checksum size
	mov	#par$no	,parity		; default the parity type please
	mov	sp	,sendat		; default to sending attr packets
	mov	sp	,doauto		; default to cecking file attributes
	copyz	#defprm	,#prompt	; set the prompt up
	call	xinit			; system specific init here
	tst	vttype			; did xinit set this up?
	bne	20$			; yes
	mov	#tty	,vttype		; default to hardcopy console
	tst	proflg			; if pro/350 then vt100
	beq	20$			; not
	mov	#vt100	,vttype		; yes
20$:	mov	#1	,blip		; assume logging all packets to tty
	mov	sp	,con8bit	; assume 8 bits are ok for console
	return				; end

	.enabl	lc
defprm:	.asciz	/Kermit-11>/
	.even


	.enabl	lsb

init1:	nop				; can always patch this with a 207
	mov	#200$	,r3		; try to open an INIT file somewhere
10$:	tst	@r3			; any more to open up ?
	beq	100$			; no
	calls	open	,<(r3)+,#lun.ta,#text>
	tst	r0			; did the open work ?
	bne	10$			; no, just ignore it
20$:	mov	#lun.ta	,cmdlun		; yes, setup for reading from INIT
	mov	sp	,sy.ini		; a flag to use later
100$:	return

	.if ndf	iasv31
	.save
	.endc	;iasv31

	.psect	$pdata
200$:	.word	210$,220$,230$,240$,0
210$:	.asciz	/SY:KERMIT.INI/
220$:	.asciz	/LB:[1,2]KERMIT.INI/
230$:	.asciz	/SY:[1,2]KERMIT.INI/
240$:	.asciz	/KERMIT:KERMIT.INI/
	.even

	.if df	iasv31
	.psect	$code
	.iff	;iasv31
	.restore
	.endc	;iasv31

	.dsabl	lsb

	global	<xinit	,cmdlun	,lun.ta	,defchk	,defdly	,chktyp	,sendly>
	global	<chksiz	,sy.ini	,parity	,setrec	,setsen	,sendat	,vttype>
	global	<setrpt	,con8bit,proflg	,blip>


	.sbttl	spar	fill will my send-init parameters


;	S P A R
;
;	spar( %loc data )
;
;	input:	@r5	address of array [0..9] of char

spar::	save	<r0,r1,r2>		; save registers we may use
	mov	#.sparsz,sparsz		; copy the send init packet size
	tst	sendat			; attribute support disabled today?
	bne	10$			; no
	mov	#11	,sparsz		; yes, shorten the packet up
10$:	mov	@r5	,r2		; point to the destiniation
	mov	#senpar	,r1		; and our local parameters
	clr	snd8bit			; assume we don't need 8bit prefixing
	movb	#'Y&137	,p.qbin(r1)	; assume 'if we must do it, ok' for 8bit
	movb	conpar+p.qbin,r0	; get senders 8bit quote character
	cmpb	r0	,#'N&137	; can the other kermit ever do 8bit ?
	bne	15$			; no, don't bother setting the mode
	clr	do8bit			; don't ever try to do it
	br	30$			;
15$:	cmpb	r0	,#'Y&137	; has the other one required this mode?
	bne	20$			; yes, set the mode up then
	cmpb	parity	,#par$no	; no, but do we need to do it?
	beq	30$			; no, don't waste the overhead
	movb	#myqbin	,r0		; yes, force this to the other side
	movb	r0	,p.qbin(r1)	; tell the other kermit we HAVE to do it
20$:	mov	sp	,snd8bit	; flag we need it for sending a file
	mov	sp	,do8bit		; force 8bit prefixing then
	movb	r0	,ebquot		; and set ours to the same please
	movb	r0	,p.qbin(r1)	; /29/ fix this please
30$:	tochar	(r1)+	,(r2)+		; senpar.spsiz
	tochar	(r1)+	,(r2)+		; senpar.time
	tochar	(r1)+	,(r2)+		; senpar.npad
	ctl	(r1)+	,(r2)+		; senpar.padc
	tochar	(r1)+	,(r2)+		; senpar.eol
	movb	(r1)+	,(r2)+		; senpar.qctl
	movb	(r1)+	,(r2)+		; senpar.qbin
	movb	(r1)+	,(r2)+		; senpar.chkt
	movb	(r1)+	,(r2)+		; senpar.rept
	tochar	(r1)+	,(r2)+		; senpar.capas
	tochar	(r1)+	,(r2)+		; senpar.capas+1
	clrb	(r2)+			; end
	unsave	<r2,r1,r0>
	return


fixchk::tstb	setsen+p.chkt		; did the user ever set block-check?
	beq	100$			; no
	cmpb	setsen+p.chkt,#'1	; insure that it's legit
	blo	100$
	cmpb	setsen+p.chkt,#'3	; insure that it's legit
	bhi	100$
	movb	setsen+p.chkt,senpar+p.chkt
100$:	return

	.sparsz	==	13		; 10 parameters to send over

sparin::save	<r1>			; save registers we may use
	mov	#.sparsz,sparsz		; copy the send init packet size
	tst	sendat			; attribute support disabled today?
	bne	10$			; no
	mov	#11	,sparsz		; yes, shorten the packet up
10$:	mov	#senpar	,r1		; where to put them
	movb	#maxpak	,(r1)+		; maximum packet size
	movb	#mytime	,(r1)+		; my desired timeout
	movb	#mypad	,(r1)+		; how much padding
	movb	#mypchar,(r1)+		; whatever i use for padding
	movb	#myeol	,(r1)+		; line terminators (don't need it)
	movb	#myquote,(r1)+		; quoting ?
	movb	#'Y&137	,(r1)+		; do quoting ?
	movb	#mychkt	,(r1)+		; checksum type
	movb	#40	,@r1		; assume no repeat processing
	tst	setrpt			; really say we do repeat crap?
	beq	20$			; no
	movb	#myrept	,@r1		; default on the rest of it
20$:	inc	r1			; fix the pointer please
	movb	#mycapa	,(r1)+		; we can read attributes
	clrb	(r1)+			; default on the rest of it
	clrb	(r1)+			; default on the rest of it
	movb	#'&	,ebquot
	unsave	<r1>
	return

	global	<setrec	,senpar	,setsen	,sparsz>

	.sbttl	rpar	read senders initialization parameters

;	R P A R
;
;	rpar( %loc msgpacket, %val size )
;
;	input:	@r5	message packet to get data from
;		2(r5)	packet length
;	output:	REMPAR[0..20] of parameters

rpar::	save	<r0,r1,r2>		; save registers we may use
	mov	@r5	,r1		; incoming packet address
	mov	2(r5)	,r0		; size
	mov	#conpar	,r2		; address of remotes parameters
	unchar	(r1)+	,(r2)+		; conpar.spsiz
	dec	r0			; exit if no more data
	beq	1$			; all done
	unchar	(r1)+	,(r2)+		; conpar.time
	dec	r0			; exit if no more data
	beq	1$			; all done
	unchar	(r1)+	,(r2)+		; conpar.npad
	dec	r0			; exit if no more data
	beq	1$			; all done
	ctl	(r1)+	,(r2)+		; conpar.padc
	dec	r0			; exit if no more data
	beq	1$			; all done
	unchar	(r1)+	,(r2)+		; conpar.eol
	dec	r0			; exit if no more data
	beq	1$			; all done
	movb	(r1)+	,(r2)+		; conpar.qctl
	dec	r0			; exit if no more data
	beq	1$			; all done
	movb	(r1)+	,(r2)+		; conpar.qbin
	dec	r0			; exit if no more data
	beq	1$			; all done
	movb	(r1)+	,(r2)+		; conpar.chkt
	dec	r0			; exit if no more data
	beq	1$			; all done
	movb	(r1)+	,(r2)+		; conpar.rept
	dec	r0			; exit if no more data
	beq	1$			; all done
	unchar	(r1)+	,(r2)+		; conpar.capas
1$:	clrb	(r2)+
	mov	#conpar	,r2		; now clear parity off please in
	mov	#12	,r0		; case an IBM system set it.
5$:	bicb	#200	,(r2)+		; simple
	sob	r0	,5$		; next please
	mov	#setrec	,r0		; check to see if we need to
	mov	#conpar	,r1		; override any of the sinit stuff
	movb	p.spsiz(r0),r2		; if user set packetsize
	beq	10$			;  then
	movb	r2	,p.spsiz(r1)	;   conpar.size := setrec.size
10$:	movb	p.eol(r0),r2		; if user set endofline
	beq	20$			;  then
	movb	r2	,p.eol(r1)	;   conpar.eol := setrec.eol
20$:	movb	p.time(r0),r2		; if user set timeout
	beq	30$			;  then
	movb	r2	,p.time(r1)	;   conpar.time := setrec.time
30$:	tstb	p.chkt(r1)		; if checksum_type = null
	bne	40$			;  then
	movb	#defchk	,p.chkt(r1)	;   checksum_type := default
40$:	movb	p.chkt(r1),senpar+p.chkt; setup for type of checksum used

	mov	snd8bit	,do8bit		; in case SPAR decided WE need 8bit
	clr	snd8bit			; prefixing to send a file over.
	cmpb	p.qbin(r1),#'Y&137	; was this a simple 'YES' ?
	bne	50$			; no
	movb	#myqbin	,ebquot		; yes, change it to the default '&'
	br	70$			; and exit
50$:	cmpb	p.qbin(r1),#'N&137	; eight bit quoting support present
	bne	60$			; yes
	clr	do8bit			; no
	br	70$
60$:	mov	sp	,do8bit		; flag for doing 8 bit prefixing then
	movb	p.qbin(r1),ebquot	; and set the quote character please
70$:
	unsave	<r2,r1,r0>
	return

rparin::save	<r1>			; save registers we may use
	mov	#conpar	,r1		; where to put them
	movb	#maxpak	,(r1)+		; maximum packet size
	movb	#mytime	,(r1)+		; my desired timeout
	movb	#mypad	,(r1)+		; how much padding
	movb	#mypchar,(r1)+		; whatever i use for padding
	movb	#myeol	,(r1)+		; line terminators (don't need it)
	movb	#myquote,(r1)+		; quoting ?
	movb	#'Y&137	,(r1)+		; do quoting ?
	movb	#mychkt	,(r1)+		; checksum type
	movb	#40	,(r1)+		; assume no repeat count processing
	clrb	(r1)+			; default on the rest of it
	clrb	(r1)+			; default on the rest of it
	clrb	(r1)+			; default on the rest of it
	unsave	<r1>
	return

	global	<conpar	,do8bit	,setrec	,ebquot>




	.sbttl	fillog	log file opens/close to disk

;	F I L L O G
;
;	input:	@r5	0	for open for read
;			1	for open for write
;		2(r5)	filename


fillog::save	<r0,r1>
	bit	#log$fi	,trace		; logging file activity to disk ?
	beq	100$			; no
	calls	putc	,<#cr,#lun.lo>	; insure buffers are flushed
	mov	#200$	,r1		; assume a header of 'writing'
	tst	@r5			; perhaps writing ?
	beq	10$			; no
	mov	#210$	,r1		; yes
10$:	movb	(r1)+	,r0		; copy the byte over
	beq	20$			; all done
	calls	putc	,<r0,#lun.lo>	; next byte pleae
	br	10$			; next
20$:	mov	2(r5)	,r1		; now for the filename
30$:	movb	(r1)+	,r0		; copy the byte over
	beq	40$			; all done
	calls	putc	,<r0,#lun.lo>	; next byte pleae
	br	30$			; next
40$:	calls	putc	,<#cr,#lun.lo>	; dump the record
100$:	unsave	<r1,r0>			; and exit
	return


200$:	.asciz	/Receiving file /
210$:	.asciz	/Sending   file /
	.even




	.sbttl	debug dump to disk

;	D S K D M P
;
;	input:	@r5	name ('rpack' or 'spack')
;		2(r5)	packet length
;		4(r5)	packet type
;		6(r5)	packet number
;		10(r5)	packet address

dskdmp::save	<r0,r1,r2,r5>
	sub	#120	,sp		; allocate a formatting buffer
	mov	sp	,r1		; point to it
	mov	#120	,r0		; and clear it out
10$:	movb	#40	,(r1)+		; simple
	sob	r0	,10$
	mov	sp	,r1		; point back to the buffer
	mov	(r5)+	,r0		; point to the routine name
	call	200$			; and copy it
	mov	#110$	,r0		; and a label ('LEN')
	call	200$			; copy it
	mov	(r5)+	,r2		; get the length saved
	deccvt	r2,r1,#3		; convert the length to decimal
	add	#6	,r1		; and skip over it
	mov	#120$	,r0		; another label ('TYP')
	call	200$			; simple
	movb	(r5)+	,(r1)+		; get the packet type
	movb	#40	,(r1)+		; and some spaces
	cmpb	@r1	,#badchk	; checksum error ?
	bne	20$			; no
	movb	#'*	,-1(r1)		; yes, flag it please
20$:	inc	r5			; point to the next arguement
	movb	#40	,(r1)+		; and some spaces
	movb	#40	,(r1)+		; and some spaces
	movb	#40	,(r1)+		; and some spaces
	mov	#130$	,r0		; and a label ('PAK')
	call	200$			; copy it
	mov	(r5)+	,r0
	deccvt	r0,r1,#3		; and convert to decimal
	add	#4	,r1		; now point to the end
	clrb	@r1			; make it .asciz
	mov	sp	,r1		; point back to the start
	calls	putrec	,<r1,#70.,#lun.lo> ; and put out to disk now
	calls	putrec	,<@r5,r2,#lun.lo>  ; also dump the data record
	tst	debug			; should we also dump to ti:?
	beq	100$			; no
	.print	r1			; yes, dump the length and type
	.newli				; and a carrriage return
	tst	r2			; anything in the packet?
	beq	100$			; no
	.print	@r5	,r2		; yes, dump it
	.newlin				; and do a <cr><lf>
100$:	add	#120	,sp		; pop the local buffer and exit
	unsave	<r5,r2,r1,r0>		; pop registers first, of course
	return				; bye


110$:	.asciz	/Length/
120$:	.asciz	/Type/
130$:	.asciz	/Paknum/
	.even


200$:	movb	(r0)+	,(r1)+		; copy .asciz string to buffer
	bne	200$			; done yet ?
	dec	r1			; yes, back up and overwrite the
	movb	#40	,(r1)+		; null with a space
	movb	#40	,(r1)+		; one more space for formatting
	return				; bye



	.sbttl	do some logging to TI: ?


senhdr::mov	#-1	,pcnt.n
	call	dovt
	bcs	100$
	print	#$sendh
	mov	sp	,logini
100$:	return

rechdr::mov	#-1	,pcnt.n
	call	dovt			; vt100 vttype type?
	bcs	100$			; no, forget it
	print	#$rech			; initial header please
	mov	sp	,logini		; save we did it already
100$:	return				; bye


reclog::save	<r1>
	call	dolog
	bcs	100$
	mov	pcnt.r	,r1		; check for modulo on screen updates
	clr	r0			; setup for the divide
	div	blip	,r0		; do it
	tst	r1			; any remainder left over
	bne	100$			; yes, simply exit
	mov	vttype	,r0		; no, dispatch to the correct routine
	asl	r0
	jsr	pc	,@recdsp(r0)
100$:	unsave	<r1>
	return


rectty:	mov	pcnt.r	,r0
	call	numout
	print	#$delim
	mov	pcnt.s+<2*<<'N&137>-100>>,r0
	cmp	r0	,pcnt.n
	beq	100$
	mov	r0	,pcnt.n
	call	numout
100$:	print	#$leftm
	return

recvt1:	call	dovt			; vt100 type?
	bcs	100$			; no
	tst	logini			; need the header?
	bne	10$			; no
	call	rechdr			; yes
10$:	print	#$pos1			; position the cursor	
	mov	pcnt.r	,r0		; received packet count
	call	numout			; dump it
	mov	pcnt.s+<2*<<'N&137>-100>>,r1 ; get the sent NAK count
	cmp	r1	,pcnt.n		; do we really need to update naks?
	beq	90$			; no
	mov	r1	,pcnt.n
	call	nakpos
	mov	r1	,r0
;	print	#$pos2			; position the cursor
	call	numout			; print the NAK count
90$:	print	#$leftm
100$:	return
	



;	for sending files, log transactions here

senlog::save	<r1>
	call	dolog
	bcs	100$
	mov	pcnt.s	,r1		; check for modulo on screen updates
	clr	r0			; setup for the divide
	div	blip	,r0		; do it
	tst	r1			; any remainder left over
	bne	100$			; yes, simply exit
	mov	vttype	,r0
	asl	r0
	jsr	pc	,@sendsp(r0)
100$:	unsave	<r1>
	return


sentty:	mov	pcnt.s	,r0
	call	numout
	print	#$delim
	mov	pcnt.r+<2*<<'N&137>-100>>,r0 ; get the sent NAK count
	cmp	r0	,pcnt.n
	beq	100$
	mov	r0	,pcnt.n
	call	numout
100$:	print	#$leftm
	return

senvt1:	tst	logini			; need the header?
	bne	10$			; no
	call	senhdr			; yes
10$:	print	#$pos1			; position the cursor
	mov	pcnt.s	,r0
	call	numout
	mov	pcnt.r+<2*<<'N&137>-100>>,r1 ; get the sent NAK count
	cmp	r1	,pcnt.n
	beq	90$
	mov	r1	,pcnt.n
	call	nakpos
	mov	r1	,r0
;	print	#$pos2
	call	numout
90$:	print	#$leftm
100$:	return


	.sbttl	data for packet transfer logging

	.if ndf	iasv31
	.save
	.endc	;iasv31

	.psect	$vtdat	,ro,d,lcl,rel,con
$sendh:	.asciz	<cr><33>/[2KPackets sent     :            Naks: /
$rech:	.asciz	<cr><33>/[2KPackets received :            Naks: /
$pos1:	.asciz	<cr><33>/[20C/		; goto column 20
$pos2:	.asciz	<33>/[14C/		; move over 14 please
$leftm:	.byte	cr,0			; goto left margin please
$delim:	.asciz	#/#
	.even

sendsp:	.word	sentty	,senvt1
recdsp:	.word	rectty	,recvt1
	.assume	tty	eq	0
	.assume	vt100	eq	1

	.if df	iasv31
	.psect	$code
	.iff	;iasv31
	.restore
	.endc	;iasv31
	
numout:	save	<r0,r1>			; save a register please
	sub	#10	,sp		; allocate a buffer on the stack
	mov	sp	,r1		; and a pointer to it please
	deccvt	r0,r1			; convert packet count
	clrb	6(r1)			; insure .asciz
	mov	sp	,r0		; and reset the string pointers
10$:	tstb	@r0			; end of the string yet?
	beq	30$			; yes
	cmpb	@r0	,#40		; found a space
	beq	20$			; yes, skip it
	movb	@r0	,(r1)+		; no, copy the character then
20$:	inc	r0			; point to the next source character
	br	10$			; and try again please
30$:	clrb	@r1			; insure .asciz
	mov	sp	,r1		; reset pointer
	print	r1			; dump it
	add	#10	,sp		; pop buffer
	unsave	<r1,r0>			; and register
	return				; and exit


	.sbttl	decide what to do about logging
	.enabl	lsb


dovt:	cmpb	vttype,#vt100		; a vt100 today?
	bne	90$			; no
dolog:	tst	blip			;
	beq	90$			; do not do this at all
	tst	remote			; a server?
	bne	90$			; could be
	tst	xmode			; text reply?
	bne	90$			; yes
	br	100$			; debug is ok then
90$:	sec
	return
100$:	clc
	return

	global	<blip>

	.dsabl	lsb


nakpos:	save	<r0,r1,r2>		; save temporary registers please
	clr	r2			; count of the number of digits-1

10$:	inc	r2			; digitcount++
	mov	r0	,r1		; get the previous result
	clr	r0			; and setup for a divide
	div	#12	,r0		; divide by 10 please
	tst	r0			; anything left
	bne	10$			; yes

	dec	r2			; no, correct magnitude size
	sub	#10	,sp		; allocate a buffer
	mov	sp	,r0		; and a pointer to it please
	movb	#33	,(r0)+		; escape
	movb	#'[	,(r0)+		; you know
	movb	#'1	,(r0)+		; move from 10 to 16 right
	mov	#'6	,r1		; the next digit
	sub	r2	,r1		; correct for size of packet count
	movb	r1	,(r0)+		; stuff it in
	movb	#'C&137	,(r0)+		; the vt100 opcode
	clrb	@r0			; .asciz
	mov	sp	,r0		; reset pointer
	print	r0			; and dump it
	add	#10	,sp		; pop local buffer and exit
	unsave	<r2,r1,r0>		; pop all registers
	return				; bye
	

	global	<pcnt.n	,pcnt.r	,pcnt.s	,remote	,xmode	,vttype>



	.end
