In   -
Out  WrchV
Type Module
Ver  1.02f

Define Module
 Name      WrchVCapture
 Author    Justin Fletcher
 Commands
  Name     WCCapture
  Max      1
  Min      0
  Code     wccapture
  Help     ...
           *WCCapture is used to start (giving a filename), or end a capture
           session. Fun, init ?
  Syntax   Syntax: *WCCapture [<filename>]
 End Commands
 Workspace *4
 Final     final
End Module

.final
   STMFD   (sp)!,{r4,link}               ; Stack registers
   LDR     r4,[r12]                      ; get the file handle
   CMP     r4,#0                         ; is it open ?
   BLNE    doclose                       ; if so, close and restore output
   LDMFD   (sp)!,{r4,pc}                 ; Return from call

.wccapture
   STMFD   (sp)!,{r0-r5,link}            ; Stack registers
   LDR     r12,[r12]                     ; read workspace from private word
   CMP     r1,#1                         ; is this a start ?
   BNE     $closecapture                 ; if not, jump to other code

   LDR     r4,[r12]                      ; read the current handle
   CMP     r4,#0                         ; are we already diverting output ?
   BNE     $exit                         ; if so, exit (no error)

   MOV     r1,r0                         ; r1 -> string
   XSWI    "XOS_Find",&8C                ; create a file with errors
   BVS     $exit                         ; if errors exit gracefully
   STR     r0,[r12]                      ; store the handle in workspace ;-)
   ADR     r1,intercept                  ; the code to use
   XSWI    "OS_Claim",3,,r12             ; claim the WRCH vector
$exit
   XLDMFD  (sp)!,{r0-r5,pc}              ; Return from call

$closecapture
   LDR     r4,[r12]                      ; read file handle
   CMP     r4,#0                         ; are we actually diverting output ?
   BEQ     $exit                         ; if not, exit (no error)
   BL      doclose                       ; do the close and release
   B       $exit                         ; exit nicely

.doclose
   STMFD   (sp)!,{r0-r2,link}            ; Stack registers
   ADR     r1,intercept                  ; the code to release
   XSWI    "OS_Release",3,,r12           ; claim the WRCH vector
   XSWI    "XOS_Find",&0,r4              ; close the file (no errors)
   MOV     r1,#0                         ; mark as closed
   STR     r1,[r12]                      ; store in the workspace
   LDMFD   (sp)!,{r0-r2,pc}              ; Return from call

.intercept
   STMFD   (sp)!,{r0-r1,link}            ; Stack registers
   LDR     r1,[r12]                      ; read the file handle from WS
   SWI     "XOS_BPut"                    ; put the byte
   LDMFD   (sp)!,{r0-r1,pc}              ; Return from call

;    LDMFD   (sp)!,{r0-r1,link}            ; restore registers
;                                          ; intercept the vector
;    LDMFD   (sp)!,{pc}^                   ; Return from call with flags

#Post
SYS "XOS_Module",4,"WrchVCapture"
#Run <CODE>
