*=- PROGRAM INFORMATION -=*************************************************************
* NoSoSuper-RexxSend, ©1995 Leo Davidson                                              *
*=====================================================================================*
* Purpose: This program sends messages to a program's ARexx port and returns the      *
*	   results in a requester.						      *
*          (Written to test my ARexx/rexxsyslib.library routines).		      *
***************************************************************************************

*=- ASSEMBLE-TIME SWITCHES -=**********************************************************
* Comment out to disable the effect(s) described.                                     *
***************************************************************************************
;Beta_Version		; Include Debug-Labels, Beta messages, etc.
Beta_Version_ARexx	; Include runtime-info about ARexx messages sent.
;Test_Pool_On_OS3	; Force use of non-ROM pool routines, even with OS3+.
DOpus5_Error		; Enable Error messages via DOpus5/"dopus request".
;No_Requesters		; Disable all requester-based output.
			; All error messages will be sent to shell.
			; Will also turn off "Please insert volume XXX" reqs.
;Default_Req		; Make requesters the default for error messages.

*=- VERSION INFORMATION -=*************************************************************
* Things like versions numbers, dates, and the name of the program are all defined    *
* here as macros to save having to edit the entire source code.                       *
*=====================================================================================*
* The version strings should conform to the style guide. (Even though I think it      *
* isn't the best way to do it, it's a standard :-(  ).                                *
***************************************************************************************
PROGNAM	MACRO				Program name.
	dc.b	"NoSoSuper-RexxSend"	/No spaces!/
	ENDM
VERSION	MACRO				The version of the program.
	dc.b	"1.5"			version.revision - /No leading zeros/
	ENDM
REVDATE	MACRO				Date of completion.
	dc.b	"21.8.95"		DD.MM.YY - /No leading zeros/
	ENDM
REVDAT2	MACRO				Date of completion.
	dc.b	"21-Aug-1995"		/Any format/ (Not in $VER string)
	ENDM
*=- ASSEMBLER INFORMATION/OPTIONS -=***************************************************
	Section	Super_Main,Code		Assemble to Public Memory.
	IFD	Beta_Version		-.
	Opt	D+			 |
	Opt	hcln			 |_ Debug info ON
	ELSE				 |  for Beta Versions.
	Opt	D-			 |
	ENDC				-'
***************************************************************************************
	Bra	ProgBeg			Jump to the start of the program.
***************************************************************************************
VerStr	Dc.b	"$VER: "		-.
	PROGNAM				 |
	IFD	Beta_Version		 |
	Dc.b	"_BETA"			 |
	ENDC				 |_ For 2.0+ Version Command
	Dc.b	" "			 |  (At the top to make "Version"
ActVer	VERSION				 |  find it quicker!)
	Dc.b	" ("			 |
	REVDATE				 |
	Dc.b	")",0			-'
	Even
;=====================================================================================;
;	Include	Asm:Include/Nudel_Constants.i
;
;	include	dos/dos.i
;	include	dos/var.i
;	include	exec/types.i
;	include	exec/nodes.i
;	include	exec/memory.i
;	include	rexx/storage.i
;	include	intuition/intuition.i
;	include	intuition/intuitionbase.i
;	include	libraries/iffparse.i
;
;	include	ASM:LVO3.0/dos_lib.i
;	include	ASM:LVO3.0/exec_lib.i
;	include	ASM:LVO3.0/intuition_lib.i
;	include	ASM:LVO3.0/rexxsyslib_lib.i
;	include	ASM:LVO3.0/utility_lib.i
;	include	ASM:LVO3.0/iffparse_lib.i
;	include	ASM:LVO3.0/mathieeedoubbas_lib.i
;	include	ASM:LVO3.0/graphics_lib.i

	Include	Nudel_Symbols.gs
***************************************************************************************
* The Variables (Address Equates for Space allocated at program start)                *
***************************************************************************************
	RSReset
;=====================================================================================;
N_DTStruc	Equ	__RS		;- Date-Time Structure -----------------------;
;=====================================================================================;
N_DT_Days	Rs.l	1		Days Since 1978
N_DT_Mins	Rs.l	1		Mins Since Midnight
N_DT_Tiks	Rs.l	1		Ticks Since Minute (Always Even*50)
N_DT_4Mat	Rs.b	1		Format   -.
N_DT_Flag	Rs.b	1		Flags     |
N_DT_Name	Rs.l	1		Date Adr  |- For Outputting.
N_DT_Date	Rs.l	1		Name Adr  |
N_DT_Time	Rs.l	1		Time Adr -'
;- End of DTStruc --------------------------------------------------------------------;
D8_Name		Rs.b	16		Name String	-.
D8_Date		Rs.b	16		Date String	 |- Output buffers.
D8_Time		Rs.b	16		Time String	-'

	IFND	No_Requesters
;=====================================================================================;
N_EasyStruct	Equ	__RS		;- EasyStruct for EasyRequest ----------------;
;=====================================================================================;
N_ES_Length	Rs.l	1		Length of the structure.
N_ES_Flags	Rs.l	1		Flags (not currently supported).
N_ES_Title	Rs.l	1		Ptr to NullTerm Title Text.
N_ES_Body	Rs.l	1		Ptr to NullTerm Body Text.
N_ES_Gadgets	Rs.l	1		Prt to Gadget(s) Texts.
N_EasyStruct_Len	Equ	(__RS)-N_EasyStruct
	ENDC

;=====================================================================================;
; Nudel Info-Block Stuff							      ;
;=====================================================================================;
FakeNIB		Rs.b	NIB_SizeOf	;- Fake NIB for setting pseudo-filenames -----;
FakeNIB_Adrs	Rs.l	1		Address of FakeNIB (saves a few "lea"'s).
Active_NIB	Rs.l	1		Ptr to currently "Active" NIB, or null.
Base_NIB	Rs.l	1		Ptr to base NIB in linked list, or null.
Last_NIB	Rs.l	1		Ptr to last NIB in linked list, or null.
CAction		Rs.l	1		Ptr to current "Action" error message.
;=====================================================================================;

;=====================================================================================;
; READARGS() STUFF								      ;
;=====================================================================================;
RDA_Rtn		Rs.l	1		Pointer to returned RDArgs structure
;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -;
RDA_Array	Equ	__RS		Array of longwords matching template
RDA_Template	Equ	*
RexxPort_A	Rs.l	1
		Dc.b	"RP=REXXPORT/A,"
RexxCommand_A	Rs.l	1
		Dc.b	"RC=REXXCOMMAND/A,"
Reqs_S		Rs.l	1
		Dc.b	"REQ=REQS/S"
RDA_Temp_Len	Equ	*-RDA_Template
		Dc.b	0
		Even
;=====================================================================================;
; OPEN_LIBRARIES STRUCTURES							      ;
;=====================================================================================;
LibBases_Start	Equ	__RS
LibData_Start	Equ	*
; NOTE: The open_lib_error routine requires lib-versions < 100
N_DOSBase	Rs.l	1
DOSName_Ptr	Dc.l	N_DOSName-LibNames_Start
DOSVers		Dc.l	37
N_RexBase	Rs.l	1
RexName_Ptr	Dc.l	N_RexName-LibNames_Start
RexVers		Dc.l	0
	IFND	No_Requesters
N_IntBase	Rs.l	1
IntBase_Ptr	Dc.l	N_IntName-LibNames_Start
IntVers		Dc.l	37
	ENDC
LibBases_Finish	Equ	__RS
NumLibs	Equ	(LibBases_Finish-LibBases_Start)/4
;-------------------------------------------------------------------------------------;
LibNames_Start	Equ	*
N_DOSName	DOSNAME
N_RexName	REXXSYSLIBNAME
	IFND	No_Requesters
N_IntName	INTNAME		Required by Open_Libraries_Error rtn.
	ENDC
	Even
;=====================================================================================;
; FLAG BYTES									      ;
;=====================================================================================;
STD_F_1		Rs.b	1		Standard files.
EVEN_F_1	Rs.b	1		(Here as an Even)
;=====================================================================================;
; VARIOUS DATA									      ;
;=====================================================================================;
DosRtnC		Rs.l	1		Return code for DOS. (-> d0 at end)
;=====================================================================================;
; RawDoFmt() Stuff								      ;
;=====================================================================================;
;	IFD	DATA_RAWDOFMT
RDF_Size	Rs.l	1		Size of RawDoFmt() buffer.
RDF_Adrs	Rs.l	1		Address of RawDoFmt() buffer.
RDF_Array	Equ	__RS
RDF_1_Long	Rs.l	1		-.
RDF_2_Long	Rs.l	1		 |_ DataArray for
RDF_3_Long	Rs.l	1		 |  RawDoFmt()
RDF_4_Long	Rs.l	1		-'
;	ENDC
;=====================================================================================;
; VARIOUS ADDRESS-POINTERS							      ;
;=====================================================================================;
StarStk		Rs.l	1		Original Stack Pointer.
PoolHead	Rs.l	1		Header for program's memory pool.
CLI_Hdl		Rs.l	1		Output CLI (or whatever) handle.
NudelPort	Rs.l	1		Pointer to our MsgPort.
NudelRexxMsg	Rs.l	1		Pointer to our RexxMsg.
CRexxPort	Rs.l	1		Ptr to name of current Rexx host to talk to.
;=====================================================================================;
; TEMP STORAGE									      ;
;=====================================================================================;
Buffer1Len	Equ	1024
Buffer1		Rs.b	Buffer1Len	Buffer for anything
Buffer2Len	Equ	1024
Buffer2		Rs.b	Buffer2Len	Buffer for anything
BufferALen	Equ	1024
BufferA		Rs.b	BufferALen	Buffer for anything
Temp001		Rs.l	1		-._ For temp storage
Temp002		Rs.l	1		-'  in routines.
;=====================================================================================;
NULLEND		Equ	__RS		;- Overlays Start Here -----------------------;
;=====================================================================================;
;=====================================================================================;
LENVARS		Equ	__RS		;- End of variables/overlays -----------------;
;=====================================================================================;

*=- MAIN PROGRAM -=********************************************************************
* Calls the subroutines.							      *
***************************************************************************************
ProgBeg	Bsr.s	Setup			Allocate Mem and initialize stuff.

	Move.l	RexxCommand_A(a5),a1	Point to the command.
	Bsr	FillAndSendNudelRexxMsg	Send it off.

	Bra.s	Finish			Close/Deallocate everything and exit.

*=- SETUP -=***************************************************************************
* Allocate Memory, Open Libraries, Parse Commandline, etc			      *
* This MUST be the first Subroutine run.					      *
***************************************************************************************
Setup	Move.l	#Memf_Public!Memf_Clear,d0	Public, Cleared mem.
	Move.l	#5120,d1			Puddle size = 5k.
	Move.l	#5120,d2			Thresh size = Puddle size.
	Bsr	AsmCreatePool			Create this prog's mem pool.
	Move.l	a0,-(SP)		Preserve PoolHeader
	Bne.s	SGotMm1			-.
	Addq.l	#4,a7			 |_ If Allocation failed, Quick Exit.
	Moveq	#RETURN_FAIL,d0		 |  pool and do a quick exit.
	RTS				-'
SGotMm1	Move.l	#LENVARS,d0		Size of variables' memory
;;;;;;;	Move.l	a0,a0			PoolHeader to a0 for allocation.
	Bsr	AsmAllocPooled		Allocate for the variables.
	Move.l	(SP)+,a0		Restore PoolHeader
	Move.l	d0,a5			Put variable/overlay address into A5
	Tst.l	d0			(Moves to a? don't set CCR).
	Bne.s	SGotMm2			-.
	Bsr	AsmDeletePool		 |
	Addq.l	#4,a7			 |- If Allocation failed, delete the
	Moveq	#RETURN_FAIL,d0		 |  pool and do a quick exit.
	RTS				-'
SGotMm2	Move.l	a0,PoolHead(a5)		Store header of memory pool.
	Lea	FakeNIB(a5),a0		-._ Store address of FakeNIB(a5)
	Move.l	a0,FakeNIB_Adrs(a5)	-'  for quick access later.
	Move.l	#RETURN_OK,DosRtnC(a5)	Default DOS return code: "OK".
	IFD	LENOVER
	Lea	NULLEND(a5),a0		-.
	Lea	Over01(pc),a1		 |  Copy Overlay
	Move.l	#(LENOVER/2)-1,d0	 |- Backings onto
OverPrL	Move.w	(a1)+,(a0)+		 |  Overlay Space (word moves)
	DBra	d0,OverPrL		-'
	ENDC
;=====================================================================================;
	Move.l	a7,StarStk(a5)		Store Stack Pointer
	Addq.l	#4,StarStk(a5)		Compensate for this being a subrout.
	IFND	No_Requesters
	SetReqDefault			Set default to requester/shell.
	ENDC
	Bsr	Open_Libraries		Open libraries.
	N_CallDOS	Output		-._ Get output Handle
	Move.l	d0,CLI_Hdl(a5)		-'  (CLI window or whatever)

;;;;;;;	Setup any non-zero default values for switch options here (below).
	Bsr	RArgNor			Parse the commandline with ReadArgs

	Move.l	RexxPort_A(a5),CRexxPort(a5)	Use RexxPort from CmdLine.

	Bsr.s	CreateNudelPort		Create our MsgPort.
	RTS

*=- FINISH -=**************************************************************************
* Close Everything Down, Return Memory Etc to System and End Program		      *
*=====================================================================================*
* All routines in this section MUST make sure that what they are about to free back   *
* to the system has actually been taken in the first place! (With the exception of    *
* the Main-Variable memory: if this could not be allocated the program will have      *
* quit without calling Finish.)							      *
*=====================================================================================*
* The Finish routine may be called from sub-routines at any level because the	      *
* original stack-pointer is restored so that RTS always quits the prog.		      *
***************************************************************************************
Finish
	IFD	Beta_Version
	Bsr_ErrorN	ErrAct_FinishBetaMsg(pc),#0
	ENDC

	Bsr	DeleteNudelRexxMsg	Delete our RexxMsg (also clears arguments).
	Bsr.s	DeleteNudelPort		Delete our MsgPort
;;;;;;;	Bsr	NIBs_DeleteAll		Delete all Nudel-Info-Blocks :-)
	Bsr	FArgNor			Free the commandline related memory.
	Bsr	Close_Libraries		Close all open libraries.
	Move.l	StarStk(a5),a7		Ignore all Branches.
	Move.l	DosRtnC(a5),-(SP)	Store DOS return code...
	Bsr	LeoDeletePool		Free the entire memory pool.
	Move.l	(SP)+,d0		Return code to d0 for DOS.
	RTS				End Program

	IFD	Beta_Version
ErrAct_FinishBetaMsg
	Dc.b	"Finish routine run",0
	Even
	ENDC

*=- EXTERNAL SUB-ROUTINES -=***********************************************************
* Sub-routines stored in external "library" files.				      *
***************************************************************************************
;=====================================================================================;
; OS Subroutines								      ;
;=====================================================================================;
SYS_CLIRITE	; Write text using CLI_Hdl(a5) as output handle.
SYS_READARG	; ReadArgs() routine.
SYS_FREEARG	; FreeArgs() routine.
SYS_CHKCRTC	; Check for ^C-Break and abort program if sent.
SYS_PORTS	; CreateMsgPort(), DeleteMsgPort() routines.
	Include	ASM:Source/Routines/System.s
;=====================================================================================;
; Data Subroutines. (Number/String Handling, etc etc)				      ;
;=====================================================================================;
DATA_NULLLEN	; Calculate the length of a Null-Term String.
DATA_COPYCN3	; Copy chars with Null-Term, don't copy Null, no Length
DATA_CHARCNT	; Copy x chars with Null-Term
DATA_CHARCOP	; Copy x chars with specified terminator.
DATA_CHARCOPNL
DATA_NUM2ASC	; Raw Number to ASCII conversion.
DATA_RAWDOFMT	; RawDoFmt() routine.
	Include	ASM:Source/Routines/Data.s
;=====================================================================================;
; Other routines.								      ;
;=====================================================================================;
	Include	ASM:Source/Routines/ARexx.s
	Include	ASM:Source/Routines/Pool.s
	Include	ASM:Source/Routines/OpenLibraries.s
;;;;;;;	Include	ASM:Source/Routines/NIB.s
; Note: NIB_DeleteAll has been removed from Finish.
***************************************************************************************
* THE END									      *
***************************************************************************************

