;
; this is the command parser for edit -- contains all known commands
;
	.TITLE		editparse -- tparse stuff
	$TPADEF

	.psect	sub,pic,usr,ovr,rel,gbl,shr,noexe,rd,wrt,novec,long

def_addr:
	.blkb	255

	.psect 	$zap,pic,usr,con,rel,lcl,noshr,noexe,rd,wrt,novec

ibeg:	.blkl	1
iend:	.blkl	1

rep_flags:
	.blkl	1

	.psect 	eflags,pic,usr,ovr,rel,gbl,shr,noexe,rd,wrt,novec,long

quit:	.long	0
exit:	.long	0
confirm:
	.long	0
edited:	.long	0
abort:	.long	0

ascii  =	1
octal  = 	2
long   =	3
word   =	4
byte   =	5
float  =	6
double =	7
hex    =	8
	
def_descr:
	.long	0
	.long	def_addr

ambmsg:	.ascid		/Ambiguous command -- reenter/

	$init_state	editsta,editkey

	$state 	begin
	$tran		'ABORT',tpa$_exit,,ascii,abort
	$tran		'DELETE',delete_state
	$tran		'EXIT',tpa$_exit,,ascii,exit
        $tran           'HELP',help_state
	$tran		'QUIT',tpa$_exit,,ascii,quit
	$tran		'REPLACE',rep_state
	$tran		'SUBSTITUTE',sub_state
	$tran		'VIEW',tpa$_exit,view

        $state  help_state
        $tran   tpa$_any,help_state,add_help
        $tran   tpa$_eos,tpa$_exit,get_help
	$state	delete_state
	$tran		'/',dela_state
	$tran		tpa$_decimal,del1_state,,,ibeg

	$state	dela_state
	$tran		'CONFIRM',dela_state,,ascii,confirm
	$tran		tpa$_decimal,del1_state,,,ibeg

	$state 	del1_state
	$tran		tpa$_eos,tpa$_exit,do_delete
	$tran		':',del1_state
	$tran		tpa$_decimal,del1_state,,,iend

	$state	sub_state
	$tran		tpa$_eos,tpa$_exit

	$state	rep_state
	$tran		'/',repl_state
	$tran		tpa$_decimal,rep1_state,,,ibeg
	
	$state	repl_state
	$tran		'ASCII',rep2_state,ch,ascii,rep_flags
	$tran		'OCTAL',rep2_state,ch,octal,rep_flags
	$tran		'LONG',rep2_state,ch,long,rep_flags
	$tran		'WORD',rep2_state,ch,word,rep_flags	
	$tran		'BYTE',rep2_state,ch,byte,rep_flags
	$tran		'FLOAT',rep2_state,ch,float,rep_flags
	$tran		'DOUBLE',rep2_state,ch,double,rep_flags
	$tran		'HEX',rep2_state,ch,hex,rep_flags

	$state	rep2_state
	$tran		tpa$_decimal,rep1_state,,,ibeg

	$state	rep1_state
	$tran		':',rep1_state
	$tran		tpa$_decimal,rep1_state,,,iend
	$tran		'WITH',with_state
	$tran		tpa$_eos,tpa$_fail

	$state	with_state
	$tran		tpa$_eos,tpa$_exit,do_replace
	$tran		tpa$_any,with_state,get_sub

	$end_state

	.psect		$code,pic,con,rel,lcl,shr,exe,rd,nowrt,long

	.entry		editscan,^m<r2,r3,r4,r5,r6,r7>
	
	clrl	abort
        clrl    help_descr
	clrl	rep_flags
	clrl	ibeg
	clrl	iend
	clrl	def_descr
	clrl	quit
	clrl	exit
	clrl	confirm
;
; Create the parameter block for LIB$TPARSE
;-
	subl2	#tpa$k_length0,sp
	moval	(sp),r2
	movl	#tpa$k_length0,tpa$l_count(r2)
	movl	#tpa$m_abbrev,tpa$l_options(r2)
	clrb	tpa$b_mcount(r2)
	movaq	@4(ap),r0
	cvtwl	(r0),tpa$l_stringcnt(r2)
	moval	@4(r0),tpa$l_stringptr(r2)
;+
;	Call LIB$TPARSE to process the data and return
;-
	pushal	editkey
	pushal	editsta
	pushal	(r2)
	calls	#3,g^lib$tparse
;
; check for ambiguity
;
	bbc	#<tpa$v_ambig>,tpa$l_options(r2),15$	; ambiguous?
	pushaq	ambmsg					; yes, tell user
	calls	#1,g^lib$put_output
15$:	movl	tpa$l_stringcnt(r2),@8(ap)
	ret

	.entry	get_sub,^m<r2,r3,r4,r5>			; get substitution
	tstl	def_descr
	bneq 	10$
	bbss	#tpa$v_blanks,tpa$l_options(ap),10$

10$:	moval	def_addr,r2
	addl2	def_descr,r2
	movb	tpa$b_char(ap),(r2)			; store character
	incl	def_descr
	ret

	.entry	ch,^m<r2,r3,r4,r5>			; check options -- we
							; can only handle one
	movl	#1,r0					; assume okay
	tstl	rep_flags				; should be zero
							; if only 1 selected
	beql	10$
	clrl	r0
10$:	ret

	.entry	do_replace,^m<r2,r3,r4,r5>
	
	tstl	rep_flags				; check options set
	bneq	10$
	movl	#float,rep_flags			; use float if none set

10$:	pushal	def_descr
	pushal	rep_flags
	pushal	iend
	pushal	ibeg

	calls	#4,replace

	movl	#1,r0
	ret
	
	.entry	do_delete,^m<r2,r3,r4,r5>		; deletes things

	tstl	iend
	bneq	10$
	movl	ibeg,iend

10$:	pushal	iend
	pushal	ibeg
	calls	#2,delete
	movl	#1,r0
	ret
	
	.end
