;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]

 LDR     R1, [R12, #recording_addr - VARS]
 TEQ     R1, #0				;are we already recording?
 LDMNEFD R13!, {R1-R11, PC}		;YES, exit

 MOV     R11, R0			;preserve command line
 ADD     R10, R12, #record_filename - VARS	;filename of recording
 MOV     R9, #255			;allow 255 char command line
 MOV     R1, R10

 ._contains_path_L1
   LDRB    R2, [R0], #1
   TEQ     R2, #ASC(".")                ;is there a path?
   TEQNE   R2, #ASC("$")
   TEQNE   R2, #ASC("^")
   TEQNE   R2, #ASC("&")
   BEQ     _L1                          ;YES, dont use ADFFS$RecordingPath
   TEQ     R2, #0
   TEQNE   R2, #13
 BNE     _contains_path_L1

 ADR     R0, Read_RecordingPath		;ADFFS$RecordingPath
 MOV     R2, #255
 MOV     R3, #0				;read default recording path
 MOV     R4, #0
 SWI     XOS_ReadVarVal			;PRM1-314
 TEQ     R2, #0				;did we get a result?
 BEQ     _L1				;NO
 TEQ     R4, #0				;is it a string?
 BNE     _L1				;NO

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

 ._L1
   LDRB    R0, [R11], #1		;copy commandline
   TEQ     R0, #13
   MOVEQ   R0, #0
   STRB    R0, [R1], #1

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

 TEQ     R9, #0				;is the filename too long?
 BEQ     _filename_too_long		;YES

 #if Recording_immediate == 0
   MOV     R1, R10			;filename
   MOV     R0, #&83			;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}

 ._filename_too_long
 MOV     R0, #1<<31
 ADDS    R0, R0, R0			;set V
 ADR     R0, error_filename_too_long
 B       _exit



;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}
