;Entry_ADFRecord
;---------------
;Records DiscOp information to file
;
;Entry:
; R0 - command tail
; R12 - pointer to module private word

.Entry_ADFRecord
 STMFD   R13!, {R1-R11, R14}
 LDR     R12, [R12, #0]

 MOV     R11, R0			;preserve command line
 ADR     R10, record_filename		;filename of recording
 MOV     R9, #255			;allow 255 char command line

 ._contains_path_L1
   LDRB    R1, [R0], #1
   TEQ     R1, #ASC(".")                ;is there a path?
   TEQNE   R1, #ASC("$")
   TEQNE   R1, #ASC("^")
   BEQ     _L1                          ;YES, dont use 
   TEQ     R1, #0
 BNE     _contains_path_L1
 LDR     R0, [R12, #recording_addr - VARS]
 TEQ     R0, #0				;are we already recording?
 BNE     _exit				;YES, exit

 MOV     R8, R10			;preserve R10
 ADR     R0, Read_RecordingPath		;ADFFS$RecordingPath
 MOV     R1, R10
 MOV     R2, #255
 MOV     R3, #0				;read default recording path
 SWI     XOS_ReadVarVal			;PRM1-314
 TEQ     R2, #0				;did we get a result?
 BEQ     _L1				;NO, skip add it then

 TEQ     R4, #0				;is it a string?
 BNE     _L1

 ADD     R10, R10, R2			;YES, increase ptr
 RSB     R9, R2, #255			;|  restrict command line length
 LDRB    R0, [R10, #-1]			;|
 TEQ     R0, #ASC(".")			;|  is the last char is a .?
 MOVNE   R0, #ASC(".")			;|  NO
 STRNEB  R0, [R10], #1			;|  |  add a .
 SUBNE   R9, R9, #1			;|+ |+ an reduce the max length

 ._L1
   LDRB    R0, [R11], #1
   STRB    R0, [R10], #1

   SUBS    R9, R9, #1			;ensure we dont go over 255 char limit
   TEQNE   R0, #0			;stop when we hit terminator
 BNE     _L1

 #if Recording_immediate == 0
   MOV     R1, R8			;filename
   MOV     R0, #&8F			;create new file
   SWI     XOS_Find			;PRM2-78
   BVS     _exit			;exit on error
  
   MOV     R1, R0			;file handle
   MOV     R0, #0			;close file
   SWI     XOS_Find			;PRM2-77
   BVS     _exit			;exit on error

   MOV     R0, #Module_Claim		;request memory
   MOV     R3, #Max_Recording_Size
   SWI     XOS_Module			;PRM1-237
   STRVC   R2, [R12, #recording_addr - VARS]	;notify code we're recording
   STRVC   R2, [R12, #recording_ptr - VARS]
 #endif
 #if Recording_immediate == 1
   STR     R12, [R12, #recording_addr - VARS]	;notify code we're recording
   MOV     R1, #0
   STR     R1, [R12, #recording_ptr - VARS]
 #endif

 ._exit
LDMFD   R13!, {R1-R11, PC}




;Entry_ADFStop
;-------------
;Saves a DiscOp recording
;
;Entry:
; R0 - command tail
; R12 - pointer to module private word

.Entry_ADFStop
 STMFD   R13!, {R0-R11, R14}
 LDR     R12, [R12, #0]

#if Recording_immediate == 0
 LDR     R10, [R12, #recording_addr - VARS]
 TEQ     R10, #0			;are we recording?
 BEQ     _exit				;NO, exit

 BL      resetVectors			;reset key vectors
 ADR     R1, record_filename		;filename
 MOV     R0, #&8F			;create new file
 SWI     XOS_Find			;PRM2-78
 BVS     _exit				;exit on error
 MOV     R1, R0				;file handle

 LDR     R3, [R12, #recording_ptr - VARS]	;load current pointer
 MOV     R2, R10			;..and start of buffer
 SUB     R3, R3, R2			;length to save
 MOV     R0, #2				;current file pointer
 SWI     XOS_GBPB			;PRM2-68
 BVS     _exit				;exit on error

 MOV     R0, #0				;close file
 STR     R0, [R12, #recording_addr - VARS]	;notify DiscOp to stop
 SWI     XOS_Find			;PRM2-77

 MOV     R0, #Module_Free
 MOV     R2, R10
 SWI     XOS_Module			;PRM1-238

 ._exit
 BL      resetVectors			;reset key vectors
 SUBS    R0, R0, R0                     ;clear V
#endif
#if Recording_immediate == 1
 MOV     R0, #0
 STR     R0, [R12, #recording_addr - VARS]
#endif
LDMFD   R13!, {R0-R11, PC}
