SYSI	equ	1
	incdir	Include:
	include	ssmac.h
	include	exec/types.i


;DEBUG	equ	1


LINESIZE	equ	$1000


	bra.s	TotalStart
	dc.b	'  Amiga BASIC++ Compiler v2.34 [04-01-92] (C) 1988-1992 DreamSoft',$d,$a,$1a
	dc.b	$a,$a,'$VER: EditCEDFile 1.2 (2.9.99)',$d,$a,0
	even

TotalStart	clistart
	clr.l	d2
	clr.l	d3
	clr.l	d4
	clr.l	d5
	clr.l	d6
	clr.l	d7
	sub.l	a2,a2
	sub.l	a3,a3
	sub.l	a4,a4



GOOO

;
;	Create public port
;
INIPORT	dv.l	RXPORT
	call	ss,TrackPort
	movev.l	d0,RXPORT
	move.l	d0,a1
	tlea	PORTNAME,a0
	move.l	a0,LN_NAME(a1)
	move.l	a1,a2
	tlea	PORTNAME,a1
	call	exec,Forbid
	call	FindPort
	tst.l	d0
	bne.s	PUBPORTERR
	move.l	a2,a1
	call	AddPort
	call	Permit
	vmove.l	RXPORT,a1
	lea	XPORT(pc),a0
	call	ss,TrackRoutine
	bra.s	PUBPORTOK
PUBPORTERR	call	Permit
	dtl	<EditCedFile already running>,a0
	jump	ss,ExitError



PUBPORTOK
;
STRIPFN	vmove.l	P_FILE,a0
	move.l	a0,d1
	call	dos,FilePart
	dv.l	FILEPARTLEN
	dv.l	FILEPART
	move.l	d0,a0
	move.l	d0,a1
	bsr	COUNTCHAR
	movemv.l	d0/a1,FILEPARTLEN
;
TRYFILE	vmove.l	P_FILE,d1
	move.l	#MODE_READWRITE,d2
	call	dos,Open
	tst.l	d0
	bne.s	FILEOK
	dtl	<Can''t open "%s">,a0
	vlea	P_FILE,a1
	jump	DosError
FILEOK	move.l	d0,d1
	call	dos,Close
;


CHKLOCAL	tstv.l	P_FORCELOCAL
	bne	LOCAL
	dtl	<CONSOLE>,a0		;Check if the console is a 'vtxx'
	move.l	a0,d1
	vlea	FNAME,a0
	move.l	a0,d2
	moveq	#$7e,d3
	move.l	#GVF_LOCAL_ONLY,d4
	call	dos,GetVar
	tst.l	d0
	bls	REMOTE
	cmpv.w	#'vt',FNAME
	bne	REMOTE

	

	vmove.l	sv_errsw,d0
	bclr	#err_library,d0
	movev.l	d0,sv_errsw
	dv.l	multiuserbase
	dtl	<multiuser.library>,a0
	moveq	#1,d0
	call	ss,TrackLibrary
	movev.l	d0,multiuserbase
	beq	REMOTE
	clr.l	d0			;Check if the user is globally logged on
	call	multiuser,muGetTaskOwner
	swap	d0
	move.w	d0,d5
	dtl	<GLOBTASK>,a0
	move.l	a0,d1
	vlea	FNAME,a0
	move.l	a0,d2
	moveq	#$7e,d3
	clr.l	d4
	call	dos,GetVar
	tst.l	d0
	bmi	REMOTE
	vlea	FNAME,a1
	call	exec,FindTask
	tst.l	d0
	beq	REMOTE
GETGLOBUID	call	multiuser,muGetTaskOwner
	swap	d0
	cmp.w	d0,d5
	bne	REMOTE
	clrv.l	FNAME

LOCAL	;Using CED

INIMSG	dv.l	RXMSG
	tlea	PORTNAME,a0
	move.l	a0,d0
	vmove.l	RXPORT,a0
	sub.l	a1,a1
	call	rexxsyslib,CreateRexxMsg
	movev.l	d0,RXMSG
	errc.ne	<Can''t create ARexx port>
	move.l	d0,a1
	lea	XREXXMSG(pc),a0
	call	ss,TrackRoutine



	STRUCTURE	PORTRECORD,0
	APTR	PREC_PORT
	APTR	PREC_NAME
	APTR	PREC_PREDPTR
	APTR	PREC_SUCCPTR
	LABEL	PREC_SIZEOF


CEDON	dbuf.b	CEDP,PREC_SIZEOF
	tlea	CEDPORTNAME,a1
	vlea	CEDP,a0
	move.l	a1,PREC_NAME(a0)
	bsr	FINDPORT
	errc.ne	<Start CED first>






ccmd	macro	*comand
	dtl	<\1>,a0
	bsr	CMDTOCEDC
	endm

ccmdn	macro	*comand
	dtl	<\1>,a0
	bsr	CMDTOCED
	endm


	

	alignlong
	dbuf.b	FIB,fib_SIZEOF
	dbuf.b	FNAME,160


	dv.l	SCREEN

	vmove.l	intuitionbase,a0
	call	exec,Forbid
	movev.l	ib_FirstScreen(a0),SCREEN
	call	Permit

	

CMDCED	ccmd	<CEDTOFRONT>



	dtl	<SAVE DOS/AREXX COMMANDS >,a0
	bsr	PUTARG
	tlea	CEDTMPNAME,a0
	bsr	ADDARG
	bsr	SENDCED

	dtl	<INSTALL DOS/AREXX COMMAND 1 ""address ''>,a0
	bsr	PUTARG
	tlea	PORTNAME,a0
	bsr	ADDARG
	dtl	<'' QUIT"">,a0
	bsr	ADDARG
	bsr	SENDCED

TRACKRXC	lea	XRXCMD(pc),a0
	move.l	a0,a1
	call	ss,TrackRoutine
	dv.l	RXCMDTRK
	movev.l	d0,RXCMDTRK




CHKCEDFILE
	ccmd	<STATUS 16>,1	;# size of file
	bsr	GETNUMR
	tst.l	d0
	bne.s	OPENNEW
	ccmd	<STATUS 18>,1	;# of changes
	bsr	GETNUMR
	tst.l	d0
	beq.s	NOCEDFILE

OPENNEW
	ccmd	<OPEN NEW>

NOCEDFILE


	

	



OPENCEDF1	dtl	<OPEN >,a0
	bsr	PUTARG
	vmove.l	P_FILE,a0
OPENCEDF3	bsr	ADDARG
	bsr	SENDCED






WEDEND	vmove.l	RXPORT,a0
	call	exec,WaitPort
	vmove.l	RXPORT,a0
	call	GetMsg
	tst.l	d0
	beq.s	WEDEND
	move.l	d0,a1
	call	ReplyMsg
	ccmd	<STATUS 66>,1
	bsr	GETNUMR
	tst.l	d0
	beq.s	WEDEND
	move.w	d0,d3
	subq.b	#1,d3
	bmi.s	WEDEND

EDEND	

	ccmd	<STATUS 19>,1
	vlea	RESSTR,a0
	move.l	a0,d1
	call	dos,FilePart
	move.l	d0,a0
	move.l	d0,a1
	bsr	COUNTCHAR
	move.l	a1,a0
	vmovem.l	FILEPARTLEN,d1/a1
	cmp.l	d1,d0
	bne.s	NEXTWINDOW
	add.w	d0,a0
	add.w	d1,a1
	subq.w	#1,d1
	bmi.s	NEXTWINDOW
CMPFN	move.b	-(a0),d0
	cmp.b	-(a1),d0
	dbne	d1,CMPFN
	beq.s	TERMINATE

NEXTWINDOW	ccmd	<NEXT VIEW>
	dbra	d3,EDEND
	dtl	<OKAY2 Do you want to abandon file ">,a0
	bsr 	PUTARG
	vmove.l	P_FILE,a0
	bsr	ADDARG
	dtl	<" ?>,a0
	bsr	ADDARG
	bsr	SENDCED
	bsr	GETNUMR
	tst.l	d0
	beq	CHKCEDFILE
	clr.l	d2
	bra.s	QUITCED

	
TERMINATE	ccmd	<STATUS 18>,1
	bsr	GETNUMR
	tst.l	d0
	beq.s	ABANDON
	ccmd	<SAVE>

ABANDON	moveq	#1,d2
QUITCED	vmove.l	RXCMDTRK,a0
	call	ss,FreeObject
	ccmd	<STATUS 66>,1
	bsr	GETNUMR
	tst.l	d2
	beq.s	SCREENBACK
	move.l	d0,d2
	ccmd	<QUIT>
	subq.w	#1,d2
	bls.s	RETURN
	
SCREENBACK	vmove.l	SCREEN,a1
	vmove.l	intuitionbase,a0
	lea	ib_FirstScreen(a0),a0
	call	exec,Forbid
	cmp.l	(a0),a1
	beq.s	SCROK
1$	move.l	(a0),d0	;sc_NextScreen
	beq.s	SCROK
	move.l	d0,a0
	cmp.l	a0,a1
	bne.s	1$
	call	intuition,ScreenToFront
	move.l	execbase.w,a6
SCROK	call	Permit
RETURN	rts
	
EXIT	jump	ss,ExitCleanup

;-------------------------------------------------------------------------

DOSERR	sub.l	a0,a0
	sub.l	a1,a1
	jump	ss,DosError
	
	
ADDARG	vlea.l	PARG0,a1
	move.w	-4(a1),d0
	move.l	a1,d1
	add.w	d0,a1
	subq.w	#1,d0
	bra.s	ADDARG1
PUTARG	vlea.l	PARG0,a1
	move.l	a1,d1
	moveq.w	#-1,d0
ADDARG1	addq.w	#1,d0
	cmp.w	#LINESIZE-2,d0
	bcc.s	2$
	move.b	(a0)+,(a1)+
	bne.s	ADDARG1
2$	clr.b	(a1)
	move.l	d1,a1
	move.w	d0,-4(a1)
	rts




XRXCMD
	dtl	<LOAD DOS/AREXX COMMANDS ">,a0
	bsr.s	PUTARG
	tlea	CEDTMPNAME,a0
	bsr.s	ADDARG
	dtl	<">,a0
	bsr.s	ADDARG
	bsr.s	SENDCED
	rts

;-------------------------------------------------------------------------------	



COUNTCHAR	moveq	#-1,d0
1$	addq.l	#1,d0
	tst.b	(a0)+
	bne.s	1$
	rts
	





CMDTOCED	bsr.s	PUTARG
	bsr.s	SENDCED
X001	rts



	dbuf.b	RESSTR,LINESIZE
	dv.l	ARG0ASIZE
	dv.w	ARG0SIZE
	dv.w	PAD
	dbuf.b	PARG0,LINESIZE



CMDTOCEDC	bsr.s	CMDTOCED
	beq.s	X001
	push	d0
	vlea.l	PARG0,a1
	move.l	a1,-(sp)
	move.l	sp,a1
	err	<CED command "%s" failed returncode %ld>
	



SENDCED	vlea	CEDP,a0
;
;
;	Send a message to an AREXX port
;PARAMETERS:
;	A0...PORTRECORD structure
;RESULT:
;	D0...Result1
;
SENDMSG	tstv.l	P_DEBUG
	beq.s	NORXECHO
	push	a0
	vlea	PARG0,a0
	call	ss,PutsNL	
	pop	a0
NORXECHO	push	a0
	vlea	PARG0,a0
	clr.l	d0
	move.w	-4(a0),d0
	call	rexxsyslib,CreateArgstring
	errc.ne	<Can''t create ARexx string>
	vmove.l	RXMSG,a1
	move.l	d0,rm_Args(a1)
	clr.l	rm_Result1(a1)
	clr.l	rm_Result2(a1)
	move.l	#RXCOMM+RXFF_RESULT,rm_Action(a1)
	vmove.l	rexxsyslibbase,rm_LibBase(a1)

	pop	a0
	move.l	(a0),d0
	move.l	PREC_PREDPTR(a0),a1
	call	exec,Forbid
	cmp.l	(a1),d0
	bne	MSGERR
	move.l	PREC_SUCCPTR(a0),a1
	cmp.l	(a1),d0
	bne	MSGERR
PORTOK	move.l	d0,a0
	vmove.l	RXMSG,a1
	call	PutMsg
	call	Permit
WMLOOP	bsr	GETMSG
	vmove.l	RXMSG,a0
	cmp.l	a0,d0
	bne	BADMSG
	call	ss,TestBreak



	vlea	RESSTR,a1
TESTRESULT	tst.l	rm_Result1(a0)
	bne.s	NORES2
	move.l	rm_Result2(a0),d0
	beq.s	NORES2
	move.l	rm_Action(a0),d1
	btst	#RXFB_RESULT,d1
	beq.s	NORES2

GETRESULT	move.l	d0,a0
	clr.l	d0
	move.w	-4(a0),d0
	beq.s	NORES3
	move.w	#LINESIZE-1,d1
	cmp.w	d1,d0
	bls.s	1$
	move.w	d1,d0
1$	subq.w	#1,d0
2$	move.b	(a0)+,(a1)+
	dbra	d0,2$

FREERESULT	

	ifd	OLDCED
	push	a1
	vmove.l	RXMSG,a0
	lea	rm_Result2(a0),a0
	move.l	(a0),d1
	clr.l	(a0)
	move.l	d1,a0
	subq.l	#ra_Buff,a0
	call	rexxsyslib,DeleteArgstring
	pop	a1
	endc

NORES2	clr.b	(a1)
	vmove.l	RXMSG,a0
	lea	rm_Args(a0),a0
	move.l	(a0),d1
	clr.l	(a0)
	move.l	d1,a0
	call	rexxsyslib,DeleteArgstring
	vmove.l	RXMSG,a0
	move.l	rm_Result1(a0),d0
SENDEND	rts

NORES3	bsr	COPYTOBUF
	bra.s	FREERESULT

MSGERR	call	Permit
	bsr.s	FINDPORT
	bne	PORTOK
	push	a0
	vmove.l	RXMSG,a0
	lea	rm_Args(a0),a0
	move.l	(a0),d0
	clr.l	(a0)
	move.l	d0,a0
	call	rexxsyslib,DeleteArgstring
	pop	a0
	
	lea	PREC_NAME(a0),a1
	err	<Port "%s" disapeared>
	
BADMSG	move.l	d0,a0
	cmp.b	#NT_MESSAGE,LN_TYPE(a0)
	bne.s	1$
	call	exec,ReplyMsg
1$	err	<Unknown message received>

FINDPORT	push	a2
	move.l	a0,a2
	move.l	PREC_NAME(a2),a1
	call	exec,Forbid
	call	FindPort
	move.l	d0,(a2)
	beq.s	NOPORT
	move.l	d0,a0
	move.l	LN_PRED(a0),a0
	move.l	a0,PREC_PREDPTR(a2)
	move.l	d0,a0
	move.l	(a0),a0	;LN_SUCC
	addq.l	#LN_PRED,a0
	move.l	a0,PREC_SUCCPTR(a2)
NOPORT	call	Permit
	move.l	a2,a0
	pop	a2
	tst.l	d0
	rts


GETMSG	vmove.l	RXPORT,a0
	call	WaitPort
	vmove.l	RXPORT,a0
	call	GetMsg
	tst.l	d0
	beq.s	GETMSG
	move.l	d0,a0
	rts



COPYTOBUF	move.l	#LINESIZE-2,d0
ADDTOBUF	move.b	(a0)+,(a1)+
	dbeq	d0,ADDTOBUF
	clr.b	-(a1)
	rts

GETNUMR	vlea	RESSTR,a0
GETNUM	clr.l	d0
2$	move.b	(a0)+,d1
	beq.s	1$
	sub.b	#'0',d1
	bcs.s	1$
	cmp.b	#'9',d1
	bhi.s	1$
	mulu	#10,d0
	add.w	d1,d0
	bra.s	2$
1$	rts



;using a console-based editor
REMOTE	vmove.l	P_REMEDIT,d0
	move.l	d0,a0
	bne.s	LEDITOK
	dtl	<vim>,a0
LEDITOK	vlea	RESSTR,a1
	bsr.s	COPYTOBUF
	move.b	#' ',(a1)+
	vmove.l	P_FILE,a0
	bsr.s	ADDTOBUF
	vlea	RESSTR,a0
	move.l	a0,d1
	clr.l	d2
	call	dos,SystemTagList
	rts
	

INTERR
	ifd	DEBUG
	illegal
	endc
	err	<Internal error>



XPORT	push	a0
	call	exec,Forbid
XPLOOP	move.l	(sp),a0
	call	GetMsg
	tst.l	d0
	beq.s	XPOK
	move.l	d0,a1
	call	ReplyMsg
	bra.s	XPLOOP
XPOK	pop	a1
	call	RemPort
	call	Permit
	rts


XREXXMSG
	push	a0
	moveq	#$10,d0
	call	rexxsyslib,ClearRexxMsg
	pop	a0
	call	DeleteRexxMsg
	rts


	dt	PORTNAME,<EDITCEDFILE>
	dt	CEDPORTNAME,<CYGNUSED>
	dt	CEDTMPNAME,<T:RexxCommands.EDITCEDFILE>

	
	tags
	dc.w	sst_errorreq
	library	rexxsyslib,36
	template	<FILE/A,REMOTEEDITOR/K,DEBUG/S,FORCELOCAL/S>
	dv.l	P_FILE
	dv.l	P_REMEDIT
	dv.l	P_DEBUG
	dv.l	P_FORCELOCAL
	finish
	end

