        SUBT    > <wini>arm.FileSwitch.FSShared

 [ AssemblingArthur
 ! 1,"Why are you assembling this file ???
 ]

SharedCodeStart ROUT

; Make sure these are consistent with those used in OSUtils

; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; In    r0 = open mode
;       r1 -> filename

; Out   VC: r0 = r1 = handle
;       VS: r0 -> error (FilingSystemError or 'NotFound')

OpenFileWithWinge ENTRY

        ORR     r0, r0, #open_nodir + open_mustopen
        SWI     XOS_Open
        MOVVC   r1, r0
        EXIT

; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

SetErrorEscape ; This is for utility command and should NOT go BL CopyError !

        ADR     r0, ErrorBlock_Escape
        ORRS    pc, lr, #V_bit

ErrorBlock_Escape
        DCD     ErrorNumber_Escape
        DCB     "Escape", 0
        ALIGN

; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

SkipOverNameAndSpaces ENTRY

        BL      SkipToSpace

10      LDRB    r0, [r1], #1
        CMP     r0, #space      ; Leave r1 -> ~space
        BEQ     %BT10
        SUB     r1, r1, #1
        EXITS                   ; r0 = first ~space

; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

SkipToSpace ENTRY "r0"

10      LDRB    r0, [r1], #1
        CMP     r0, #delete
        CMPNE   r0, #space      ; Leave r1 -> space or CtrlChar
        BHI     %BT10
        SUB     r1, r1, #1
        EXITS

; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; Bastid Bastid Bastid Bastid Bastid Bastid Bastid Bastid Bastid Bastid Bastid

xyzzy_WildMatch ROUT

; In : R0 is wildcard spec ptr, R4 is name ptr.
;       Wild Terminators are any ch <=" ",name terminator 0
;       Wildcards are *,#
; Out: EQ/NE for match (EQ if matches)
;       R4 points after name terminator for found
;       R0 preserved,R10,11 corrupted

 [ debugname :LOR: debugdaftgbpb
 DSTRING r4,"WildMatch: trying to match "
 DSTRING r0," against "
 ]
        Push  "R0-R3"
        MOV    R11,#0        ; this is the wild backtrack pointer
        MOV    R3,#0         ; and this is the name backtrack ptr.
01      LDRB   R1,[R0],#1    ; nextwild
        CMP    R1,#"*"
        BEQ    %FT02         ; IF nextwild = "*"
        LDRB   R2,[R4],#1    ; nextname
        CMP    R2,#0
        BEQ    %FT03
        UpperCase R1,R10
        UpperCase R2,R10
        CMP    R1,R2         ; IF nextwild=nextname
        CMPNE  R1,#"#"       ;   OR nextwild = #  (terminator checked already)
        BEQ    %BT01         ; THEN LOOP (stepped already)
        MOV    R0,R11        ; try backtrack
        MOVS   R4,R3         ; if * had at all
        BNE    %FT02
        CMP    PC,#0         ; set NE
04      Pull  "R0-R3"        ; return NE (failed)
        MOV    PC,lr

03      CMP    R1,#" "       ; name terminated: has wildcard?
        BHI    %BA04         ; note HI has NE set.
        CMP    R1,R1         ; set EQ
        Pull  "R0-R3"
        MOV    PC,lr

02      MOV    R11,R0        ; wild backtrack ptr is char after *
        LDRB   R1,[R0],#1    ; step wild
        CMP    R1,#"*"
        BEQ    %BT02         ; fujj **
        UpperCase R1,R10
05      LDRB   R2,[R4],#1    ; step name
        CMP    R2,#0         ; terminator?
        BEQ    %BT03
        UpperCase R2,R10
        CMP    R1,R2
        CMPNE  R1,#"#"       ; match if #
        BNE    %BT05
        MOV    R3,R4         ; name backtrack ptr is char after match
        B      %BT01         ; LOOP

; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; In    r0 = number to print
;       r1 = 0 -> strip spaces
;            1 -> print leading spaces for field of 4

; Number gets printed RJ in a field of 4 if possible, or more as necessary

PrintR0Decimal ENTRY "r0-r3"

        SUB     sp, sp, #16
        MOV     r3, r1                  ; Save flag
        MOV     r1, sp
        MOV     r2, #16
        SWI     XOS_BinaryToDecimal     ; Ignore any errors from this
        CMP     r3, #0                  ; Doing spaces ?
        RSBNES  r3, r2, #4              ; How many spaces do we need ?
        BLE     %FT10

05      SWI     XOS_WriteI+" "          ; But errors in here are bad
        BVS     %FT99
        SUBS    r3, r3, #1
        BNE     %BT05

10      LDRB    r0, [r1], #1
        SWI     XOS_WriteC
        BVS     %FT99
        SUBS    r2, r2, #1
        BNE     %BT10

99      ADD     sp, sp, #16
        STRVS   r0, [sp]
        EXIT

; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; Shared output routines; present in OS version

HexR0LongWord
        Push    "R0, lr"
        MOV     R0, R0, ROR #16
        BL      HexR0Word
        MOVVC   R0, R0, ROR #32-16
        BLVC    HexR0Word
        STRVS   R0, [sp]
        Pull    "R0, PC"

HexR0Word
        Push    "R0, lr"
        MOV     R0, R0, ROR #8
        BL      HexR0Byte
        MOVVC   R0, R0, ROR #32-8
        BLVC    HexR0Byte
        STRVS   R0, [sp]
        Pull    "R0, PC"

HexR0Byte
        Push    "R0, lr"
        MOV     R0, R0, ROR #4
        BL      HexR0Nibble
        MOVVC   R0, R0, ROR #32-4
        BLVC    HexR0Nibble
        STRVS   R0, [sp]
        Pull    "R0, PC"

HexR0Nibble
        Push    "R0, lr"
        AND     R0, R0, #15
        CMP     R0, #10
        ADDCC   R0, R0, #"0"
        ADDCS   R0, R0, #"A"-10
        SWI     XOS_WriteC
        STRVS   R0, [sp]
        Pull    "R0, PC"

 [ :LNOT: anyfiledebug
 ! 0, "It would cost ":CC:(:STR:(.-SharedCodeStart)):CC:" bytes to do a near-full divorce from the kernel"
 ]

FileSwitch_ModuleEnd

        END
