	.title	k11hlp	search sequential help file for topics
	.ident	/1.0.01/

;	24-Jan-84  12:22:19  Brian Nelson
;	Copyright (C) 1984 Change Software, Inc.
;
;	 This is the help routine for KERMIT-11. It reads a sequential
;	file for finding help topics. Upon a match it simply dumps the
;	entire  help for that topic out.  As of version  1 it does not
;	understand  subtopics of help.  It would have been nice to use
;	the syslib  routine 'HLPSUB' which comes with RSTS and M+, but
;	that routine uses FCS for i/o and not RMSv2 for i/o.  While it
;	would not be hard to modify Digital's HLPSUB to use RMS11 this
;	will not be done since we have to send Kermit source out.
;	 The result of all this is that Kermit's HELP is quit slow, as
;	it  must read the entire file up to and including  the desired
;	help topic.
;
;	 The help file must be one of the following filenames.  Kermit
;	will try to open them in order.
;
;	LB:[1,2]K11HLP.HLP
;	SY:[1,2]K11HLP.HLP
;	KERMIT:K11HLP.HLP
;	HELP:K11HLP.HLP


	.include	/in:k11mac.mac/
	.psect	$code
	.enabl	gbl

	.enabl	lsb

c$help::call	hlpopn			; open the help file up?
	tst	r0			; did it work ?
	beq	5$			; no

	message	<Can't open the Kermit-11 HELP file - >
	direrr	r0			; print the actual error out now
	message	<Printing default command list text>,cr
	call	defhlp			; print out simple help and exit
	br	120$			; bye

5$:	call	hget			; get something to get help on
110$:	call	hlpclo			; close the help file up now
120$:	return


	.dsabl	lsb


	.sbttl	hget	find out what to get help on
	.enabl	lsb

	cvtarg	=	4 ! 10 ! 20 ! 40 ! 200

hget:
10$:	mov	#argbuf	,r2		; a buffer for it
	tstb	@r2			; anything there in the buffer
	bne	20$			; yes
	calls	hindex	,<#'1,#0>	; no, print the index of topics
	print	#200$			; and a prompt
	calls	kbread	,<r2>		; get some input from the user
	tst	r0			; did it work ?
	bne	110$			; no
	calls	cvt$$	,<r2,r1,#cvtarg>; squish the line up please
	add	r2	,r0		; point to the and and make .asciz
	clrb	@r0			; simple
	tstb	@r2			; anything there now
	beq	110$			; no, exit help please

20$:	mov	#'0	,r1		; start with help level '1'

30$:	tstb	@r2			; end of the command line yet ?
	beq	80$			; yes, print out what we have found

	mov	#76	,r4		; copy the next help topic to stack
	sub	#100	,sp		; allocate the space and point to
	mov	sp	,r5		; it, also keep track of amount left
50$:	dec	r4			; freespace := pred(freespace)
	ble	60$			; exit if freespace < 0
	movb	@r2	,(r5)+		; dest[i] := src[i]; i := i+1
	beq	70$			; exit if src[i-1] = null
	cmpb	(r2)+	,#40		; exit if src[i-1] = space
	bne	50$			; next
60$:	clrb	-(r5)			; dest[i] := null

70$:	mov	sp	,r5		; point back to the local buffer
	inc	r1			; help_level := succ( help_level)
	calls	hfind	,<r5,r1>	; find the correct level of help
	add	#100	,sp		; pop the local buffer now
	tst	r0			; but did the search work ?
	beq	30$			; no, start over please

	clrb	argbuf			; can't find the topic, go back
	message	<HELP topic not found>,cr; to level 1 help index
	br	10$			; next please

80$:	calls	hprint	,<r1>		; found it, please print the help
	clr	r0			; normal return
	return				; and exit

110$:	mov	sp	,r0		; exit help please
	return

200$:	.asciz	<cr><lf>/Topic ? /
	.even
	.dsabl	lsb





	.sbttl	find a topic matching topic in argbuf

;	H F I N D
;
;	input:	@r5	the topic to look for, assumed to be .asciz
;		2(r5)	the level to search for
;	output:	r0	= 1 --> topic not found
;			= 0 --> topic found
;			< 0	RMS11 error code


hfind:	save	<r1,r2,r3,r4>		; save registers we may use
	strlen	@r5			; the length of the desired topic
	mov	r0	,r3		; save it please
	cmpb	2(r5)	,#'1		; level one help today ?
	bne	10$			; no, don't rewind the help file
	calls	rewind	,<#lun.in>	; insure position is start of file

10$:	mov	#hlpbuf	,r2		; point to the buffer we use
	calls	getrec	,<r2,#lun.in>	; read the next file record
	tst	r0			; did it work ?
	beq	20$			; no
	cmp	r0	,#ER$EOF	; make end of file into NOT FOUND
	beq	60$			; not found error
	br	110$			; else exit
20$:	tst	r1			; did we read anything at all
	beq	50$			; no, must have been a blank line
	cmpb	@r2	,#'1		; a digit ?
	blo	50$			; no
	cmpb	@r2	,#'9		; keep trying for a digit
	bhi	50$			; no, not a digit, try the next record
	cmpb	@r2	,2(r5)		; is this the correct help level ?
	blo	60$			; no, the level is < than desired
	bhi	50$			; no, the level is > than desired
	inc	r2			; point to the the next character
	dec	r1			; and drop the length by one please
	ble	50$			; nothing is in the record but a number
	calls	cvt$$	,<r2,r1,#377>	; remove all spaces and do case xlate
	mov	r0	,r1		; anything left over from trim ?
	beq	50$			; no, try the next record then
	calls	instr	,<r2,r1,@r5,r3>	; and see if there is a match here
	dec	r0			; if <> 0 then yes (should be eq 1)
	bne	50$			; no match, try again please
	add	r2	,r1		; a match, make the topic .asciz
	clrb	@r1			; please
	copyz	r2	,#hlptop	; copy the line and exit
	br	100$			; bye

50$:	br	10$			; next record please

60$:	mov	#1	,r0		; flag we did not find it
	br	110$			; and exit

100$:	clr	r0			; success return
110$:	unsave	<r4,r3,r2,r1>
	return



	.sbttl	print out help for a topic found by hfind

;	HPRINT
;
;	input:	@r5	current topic level ('1'..'9')
;	output:	r0	RMS11 error code, 0 for success


hprint:	print	#200$			; a crlf
	print	#hlptop			; the help topic name
	print	#200$			; some more cr/lf's
	print	#200$			; and so on
	mov	#hlpbuf	,r2		; use the command line buffer please
10$:	calls	getrec	,<r2,#lun.in>	; read the next sequential record
	tst	r0			; did it work ?
	bne	90$			; no, that's an error please
	tst	cccnt			; control C from user?
	bne	100$			; bye
	tst	r1			; cr/lf only in the record ?
	beq	20$			; yes, just print a cr/lf then
	movb	@r2	,r0		; get the character please
	cmpb	r0	,#'1		; a new topic or subtopic found ?
	blo	15$			; no
	cmpb	r0	,#'9		; well ?
	bhi	15$			; no
	cmpb	r0	,@r5		; found a new topic, is it a subtopic
	blos	100$			; no, it's a new topic
	add	r2	,r1		; point to the end of the line
	clrb	@r1			; to insure .asciz string to HINDEX
	calls	hindex	,<r0,#-1>	; simple to do
	br	100$			; and exit
	inc	r2			; yes, just ignore it please
	dec	r1			; one character less to print
	beq	20$			; nothing is left?

15$:	print	r2	,r1		; no, print the help text out
20$:	print	#200$			; and a cr/lf also
	br	10$			; next please

90$:	return				; error return
100$:	clr	r0			; success return
	return


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



	.sbttl	print the index of topics available


;	H I N D E X
;
;	input:	@r5	level to print index for
;		2(r5)	if <> 0 then HLPBUF has a valid record in it
;			else read first.
;
;	local data

	rflag	=	0
	items	=	2



hindex:	save	<r1,r2,r3,r4>		; save all registers
	clr	-(sp)			; also need some local r/w data	
	clr	-(sp)			; also need some local r/w data
	mov	sp	,r4		; and set up a pointer to the data
	sub	#40	,sp		; need a buffer please
	mov	2(r5)	,rflag(r4)	; need to do a read for 1st record?
	cmpb	@r5	,#'1		; if help_level = level_one
	bne	5$			;  then
	calls	rewind	,<#lun.in>	;   rewind(help_file)

5$:	message
	message	<Additional information is available on:>,cr
	message

10$:	mov	#4	,items(r4)	; counter for # topics printed/line
	print	#210$			; a crlf

20$:	tst	cccnt			; control C ?
	bne	100$			; yes, exit
	mov	sp	,r3		; point to the buffer
	mov	#hlpbuf	,r2		; point to a buffer to use please
	tst	rflag(r4)		; does the help record buffer already
	beq	21$			; have a record sitting around there?
	strlen	r2			; yes, get the length and use it
	mov	r0	,r1		; get the old record's length please
	clr	r0			; fake no read errors please
	br	22$			; skip the read from disk for now.
21$:	calls	getrec	,<r2,#lun.in>	; get the next record from the file
22$:	clr	rflag(r4)		; always read the buffer now
	tst	r0			; did it work ?
	bne	90$			; no
	tst	r1			; was the record blank (ie, crlf?)?
	beq	30$			; yes, ignore it please

	cmpb	@r2	,#'1		; must be a digit in the range [1..9]
	blo	30$			; < '1' thus not a topic start
	cmpb	@r2	,#'9		; > '9' then not a digit please
	bhi	30$			; can't be the start of a topic

	cmpb	@r2	,@r5		; correct topic level ?
	bhi	30$			; no, level too low to print it
	beq	25$			; correct level found now
	 mov	#ER$EOF	,r0		; higher level, fake eof and exit
	 br	90$			; bye
25$:	inc	r2			; yes, skip over to topic name
	dec	r1			; one less character in the record
	bne	40$			; nothing left in the record ?
30$:	br	20$

40$:	copyz	#200$,r3,#22		; copy over 18 spaces please
	cmp	r1	,#20		; truncate topic after 16 characters
	blos	50$			; ok
	mov	#20	,r1		; not ok, set the length to 16 (10)
50$:	movb	(r2)+	,(r3)+		; copy the topic name over now
	sob	r1	,50$		; next byte please
	mov	sp	,r3		; point back to the buffer please
	print	r3	,#22		; and dump it out now
	dec	items(r4)		; room for more on the print line?
	bne	20$			; yes
	br	10$			; start a new line now

90$:	print	#210$			; cr/lf
	print	#210$			; cr/lf
	cmp	r0	,#ER$EOF	; normal end of file found ?
	bne	110$			; yes
100$:	clr	r0			; no errors, return then
110$:	add	#40	,sp		; pop the buffer we used
	cmp	(sp)+	,(sp)+		; pop the local r/w data please
	unsave	<r4,r3,r2,r1>
	return				; bye


200$:	.rept	30
	.byte	40
	.endr
	.byte	0
210$:	.byte	cr,lf,0
	.even



	.sbttl	open and close the kermit-11 help file up


hlpclo:	calls	close	,<#lun.in>
	return


hlpopn:	call	getprv			; seems to be needed
	mov	#hnames	,r1		; the list of help file names
	call	getsys			; if this is RT11 use something
	cmpb	r0	,#sy$rt		; reasonable please
	bne	10$			; not RT
	mov	#rtname	,r1		; RT11, try DK: and SY:
10$:	tst	@r1			; end of the list as of yet ?
	beq	90$			; yes, can't find the help file
	calls	fopen	,<@r1,#lun.in,#text,#2>; try hard to open the help file
	tst	r0			; did it work ?
	beq	100$			; yes
	tst	(r1)+			; no, try the next help filename
	br	10$			; next please
90$:	mov	#ER$FNF	,r0		; return file not found
100$:	call	drpprv			; please no more privs
	return				; return with it open or not found


hnames::.word	10$,20$,30$,40$,0
10$:	.asciz	/LB:[1,2]K11HLP.HLP/
20$:	.asciz	/SY:[1,2]K11HLP.HLP/
30$:	.asciz	/KERMIT:K11HLP.HLP/
40$:	.asciz	/HELP:K11HLP.HLP/
	.even

rtname:	.word	10$,20$,0
10$:	.asciz	/DK:K11HLP.HLP/
20$:	.asciz	/SY:K11HLP.HLP/
	.even


	global	<drpprv	,getprv	,getsys>

	.end
