;----------------------------------------------------------------------------
; This is a cut down version of nsm patch converted to Amiga E calling
; conventions, converted by Claude Heiland-Allen
;
; This file contains only sendrexx and freeresult, which are not part
; of the nsm "hack".
;
; Original by Kjetil S Matheussen
;
; See original distribution for details
;----------------------------------------------------------------------------

ZAVE	MACRO	; (reglist)
		movem.l	\1,-(sp)
		ENDM

UNZAVE	MACRO	; (reglist)
		movem.l	(sp)+,\1
		ENDM

POPA	MACRO	; (saved, count, arg, dest)
		move.l	4*(1+\1+\2-\3)(sp),\4
		ENDM

CALL0	MACRO	; (name)
		bsr		\1
		ENDM

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

	xdef	_nsm_freeresult
	xdef	_nsm_sendrexx__i

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

	SECTION code

_nsm_freeresult												;Releases the memory occopied when making the result-string.
	move.l	result2(pc),a1
	cmp		#0,a1
	beq		notastring
	subq.l	#8,a1
	move.l	(a1),d0
	move.l	a6,-(sp)
	move.l	4.w,a6
	jsr		-210(a6)
	lea		result2(pc),a1
	move.l	#0,8(a1)									;take away address from arexx-message.
															;(probably not necesarry)
	move.l	(sp)+,a6
notastring:
	rts



;	Used registers for _sendrexx:
;	A6 - exec.library
;	A5 - My message-port
;	A3 - Arexx-message
;	A1 - Octa-port name
;	D4 - Return-message

_nsm_sendrexx__i:
;	bsr			_freeresult							;Frees the last resultstring, if any
	CALL0	_nsm_freeresult

		ZAVE	d6

	movem.l		d4/a3/a5-a6,-(sp)

		POPA	5,1,1,d6

	moveq			#0,d4									;If something goes wrong.

	move.l		4.w,a6

	jsr			-$29a(a6)							;Creates a message-port
	tst.l			d0
	beq			exit
	move.l		d0,a5

	lea			arexxMsg_end(pc),a3				;The end of the message
	moveq			#31,d0								;Reset arexxMsg to nulls. Don't know if this is necesarry, but...
loop:
	clr.l			-(a3)									;A3 will eventullly be 'ArexxMsg'
	dbra			d0,loop

	move.l		a5,$e(a3)							;Set replyport
	move.w		#$80,$12(a3)						;Rexx-msg length
;	move.l		#0,$6c(a3)							;Extension. (f.ex .rexx, .omed, etc.)
;	move.l		#0,$70(a3)							;Host
	move.l		#$01020000,$1c(a3)				;Set the action-type. (message | return)
	move.l		d6,$28(a3)							;Put the argument into the message

	jsr			-120(a6)								;Disable interrupts

	lea			octaportname(pc),a1				;Finds the octarexx-port
	jsr			-390(a6)
	tst.l			d0
	beq			could_not_find_port

	move.l		d0,a0									;Port to send to
	move.l		a3,a1									;Message to send
	jsr			-366(a6)								;Sends the message

	jsr			-126(a6)								;Enable interrupts

	move.l		a5,a0									;Waits for reply
	jsr			-$180(a6)

	move.l		36(a3),d4							;Gets the result2


close_port:
	move.l		a5,a0
	jsr			-$2a0(a6)


exit:
	move.l		d4,d0
	movem.l		(sp)+,d4/a3/a5-a6

		UNZAVE	d6

	rts													;Finished!

could_not_find_port:
	jsr			-126(a6)								;Enable interrupts
	bra			close_port

octaportname:
	dc.b			"OCTAMED_REXX",0

	dc.b			0										;Simulated seka-function: 'Even'


arexxMsg:													;An ArexxMsg-structure
NSMMessage:													;An NSMMessage-structure.
	;Structure message
	 ;Structure Node
	  dc.l		  0			;struct node *ln_succ
	  dc.l		  0			;struct node *ln_pred
	  dc.b		  5			;UBYTE ln_type=NT_MESSAGE
	  dc.b		  0			;UBYTE priority
	  dc.l		  0			;char *ln_Name
	 dc.l			 0			;struct MsgPort *mn_replyport
	 dc.w			24			;UWORD mn_Length


	dc.l			0			;In the NSMMessage-structure: UWORD *Octa_addr
;End of NSM-message structure. Length: 24 bytes

	dc.l			0
rm_Action:
	dc.l			0
	dc.l			0				;Result1 (error-messages and such)
result2:
	dc.l			0				;Result2 (result-string)
rm_Args:
	dc.l			0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
	dc.l			0
	dc.l			0
	dc.l			0
	dc.l			0
	dc.l			0
rm_avail:
	dc.l			0
arexxMsg_end:

;End of ArexxMsg, Length: 128 bytes

	END

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