2021-09-09 02:34:10 +00:00
|
|
|
* MAINMEM.WILD.S
|
|
|
|
* (c) Bobbi 2021 GPLv3
|
|
|
|
*
|
|
|
|
* Wildcard support
|
|
|
|
|
2021-09-09 22:51:22 +00:00
|
|
|
* Performs wildcard matching for operations that only require the
|
|
|
|
* first match. <*obj-spec*> in Acorn ADFS terminology.
|
|
|
|
WILDONE JSR WILDCARD
|
|
|
|
JSR CLSDIR
|
|
|
|
RTS
|
|
|
|
|
2021-09-09 02:34:10 +00:00
|
|
|
* Scan path in MOSFILE, break it into segments (ie: chunks delimited
|
|
|
|
* by '/'), and for each segment see if it contains wildcard chars.
|
|
|
|
* If so, pass it to SRCHDIR to expand the wildcard. If not, just
|
|
|
|
* append the segment as it is. Uses MFTEMP to build up the path.
|
|
|
|
* Returns with carry set if wildcard match fails, clear otherwise
|
2021-09-09 22:51:22 +00:00
|
|
|
WILDCARD STZ :LAST
|
2021-09-10 02:52:22 +00:00
|
|
|
LDX #$00 ; Start with first char
|
|
|
|
STX MFTEMP ; Clear MFTEMP (len=0)
|
2021-09-09 17:38:27 +00:00
|
|
|
PHX
|
|
|
|
:L1 PLX
|
2021-09-10 02:52:22 +00:00
|
|
|
JSR SEGMENT ; Extract segment of pathname
|
2021-09-10 04:12:40 +00:00
|
|
|
JSR CLSDIR ; Close open dir, if any
|
|
|
|
LDA #$FF ; WILDIDX=$FF denotes new search
|
|
|
|
STA WILDIDX
|
2021-09-09 17:38:27 +00:00
|
|
|
BCC :NOTLST
|
|
|
|
DEC :LAST
|
|
|
|
:NOTLST PHX
|
2021-09-10 02:52:22 +00:00
|
|
|
LDA SEGBUF ; Length of segment
|
|
|
|
BNE :S1 ; Check for zero length segments
|
|
|
|
LDA :LAST ; If not the last segment ...
|
|
|
|
BEQ :L1 ; ... go again
|
|
|
|
:S1 JSR HASWILD ; See if it has '*'/'#'/'?'
|
|
|
|
BCS :WILD ; It does
|
|
|
|
JSR APPSEG ; Not wild: Append SEGBUF to MFTEMP
|
2021-09-09 17:38:27 +00:00
|
|
|
BRA :NEXT
|
2021-09-10 02:52:22 +00:00
|
|
|
:WILD LDX #<MFTEMP ; Invoke SRCHDIR to look for pattern
|
|
|
|
LDY #>MFTEMP ; in the directory path MFTEMP
|
2021-09-09 17:38:27 +00:00
|
|
|
JSR SRCHDIR
|
2021-09-10 02:52:22 +00:00
|
|
|
BCS :NOMATCH ; Wildcard did not match anything
|
|
|
|
JSR APPMATCH ; Append MATCHBUF to MFTEMP
|
2021-09-09 17:38:27 +00:00
|
|
|
:NEXT LDA :LAST
|
|
|
|
BEQ :L1
|
|
|
|
PLX
|
2021-09-10 02:52:22 +00:00
|
|
|
JSR TMPtoMF ; Copy the path we built to MOSFILE
|
2021-09-09 17:38:27 +00:00
|
|
|
CLC
|
|
|
|
RTS
|
|
|
|
:NOMATCH PLX
|
|
|
|
SEC
|
|
|
|
RTS
|
2021-09-10 02:52:22 +00:00
|
|
|
:LAST DB $00 ; Flag for last segment
|
2021-09-09 02:34:10 +00:00
|
|
|
|
2021-09-09 23:55:27 +00:00
|
|
|
* Obtain subsequent wildcard matches
|
|
|
|
* WILDCARD must have been called first
|
|
|
|
* Returns with carry set if wildcard match fails, clear otherwise
|
2021-09-10 02:52:22 +00:00
|
|
|
WILDNEXT LDX MFTEMP ; Length of MFTEMP
|
|
|
|
:L1 CPX #$00 ; Find final segment (previous match)
|
|
|
|
BEQ :S1
|
|
|
|
LDA MFTEMP,X
|
|
|
|
CMP #'/'
|
|
|
|
BNE :S2
|
|
|
|
DEX
|
|
|
|
STX MFTEMP ; Trim MFTEMP
|
|
|
|
BRA :S1
|
|
|
|
:S2 DEX
|
|
|
|
BRA :L1
|
|
|
|
:S1 JSR SRCHDIR
|
|
|
|
BCS :NOMATCH
|
|
|
|
JSR APPMATCH ; Append MATCHBUF to MFTEMP
|
|
|
|
JSR TMPtoMF ; Copy back to MOSFILE
|
|
|
|
CLC
|
|
|
|
:NOMATCH RTS
|
2021-09-09 23:55:27 +00:00
|
|
|
|
2021-09-09 02:34:10 +00:00
|
|
|
* Copy a segment of the path into SEGBUF
|
|
|
|
* PREPATH makes all paths absolute, so always begins with '/'
|
|
|
|
* On entry: X contains index of first char in MOSFILE to process
|
|
|
|
* Set carry if no more segments, clear otherwise
|
2021-09-09 17:38:27 +00:00
|
|
|
SEGMENT LDY #$00
|
2021-09-10 02:52:22 +00:00
|
|
|
:L1 CPX MOSFILE ; See if we are done
|
2021-09-09 17:38:27 +00:00
|
|
|
BEQ :NOMORE
|
|
|
|
LDA MOSFILE+1,X
|
|
|
|
CMP #'/'
|
|
|
|
BEQ :DONE
|
|
|
|
STA SEGBUF+1,Y
|
|
|
|
INX
|
|
|
|
INY
|
|
|
|
BRA :L1
|
2021-09-10 02:52:22 +00:00
|
|
|
:DONE STY SEGBUF ; Record the length
|
2021-09-09 17:38:27 +00:00
|
|
|
LDA #$00
|
2021-09-10 02:52:22 +00:00
|
|
|
STA SEGBUF+1,Y ; Null terminate for MATCH
|
|
|
|
INX ; Skip the slash
|
|
|
|
CLC ; Not the last one
|
2021-09-09 17:38:27 +00:00
|
|
|
RTS
|
2021-09-10 02:52:22 +00:00
|
|
|
:NOMORE STY SEGBUF ; Record the length
|
2021-09-09 17:38:27 +00:00
|
|
|
LDA #$00
|
2021-09-10 02:52:22 +00:00
|
|
|
STA SEGBUF+1,Y ; Null terminate for MATCH
|
|
|
|
SEC ; Last segment
|
2021-09-09 17:38:27 +00:00
|
|
|
RTS
|
2021-09-09 02:34:10 +00:00
|
|
|
|
|
|
|
* See if SEGBUF contains any of '*', '#', '?'
|
|
|
|
* Set carry if wild, clear otherwise
|
2021-09-09 17:38:27 +00:00
|
|
|
HASWILD LDX #$00
|
2021-09-10 02:52:22 +00:00
|
|
|
:L1 CPX SEGBUF ; At end?
|
2021-09-09 17:38:27 +00:00
|
|
|
BEQ :NOTWILD
|
|
|
|
LDA SEGBUF+1,X
|
|
|
|
CMP #'*'
|
|
|
|
BEQ :WILD
|
|
|
|
CMP #'#'
|
|
|
|
BEQ :WILD
|
|
|
|
CMP #'?'
|
|
|
|
BEQ :WILD
|
|
|
|
INX
|
|
|
|
BRA :L1
|
|
|
|
:NOTWILD CLC
|
|
|
|
RTS
|
|
|
|
:WILD SEC
|
|
|
|
RTS
|
2021-09-09 02:34:10 +00:00
|
|
|
|
|
|
|
* Append SEGBUF to MFTEMP
|
2021-09-10 02:52:22 +00:00
|
|
|
APPSEG LDY MFTEMP ; Dest idx = length
|
|
|
|
LDA #'/' ; Add a '/' separator
|
2021-09-09 17:38:27 +00:00
|
|
|
STA MFTEMP+1,Y
|
|
|
|
INY
|
2021-09-10 02:52:22 +00:00
|
|
|
LDX #$00 ; Source idx
|
|
|
|
:L1 CPX SEGBUF ; At end?
|
2021-09-09 17:38:27 +00:00
|
|
|
BEQ :DONE
|
|
|
|
LDA SEGBUF+1,X
|
|
|
|
STA MFTEMP+1,Y
|
|
|
|
INX
|
|
|
|
INY
|
|
|
|
BRA :L1
|
2021-09-10 02:52:22 +00:00
|
|
|
:DONE STY MFTEMP ; Update length
|
|
|
|
RTS
|
|
|
|
|
|
|
|
* Append MATCHBUF to MFTEMP
|
|
|
|
APPMATCH LDY MFTEMP ; Dest idx = length
|
|
|
|
LDA #'/' ; Add a '/' separator
|
|
|
|
STA MFTEMP+1,Y
|
|
|
|
INY
|
|
|
|
LDX #$00 ; Source idx
|
|
|
|
:L1 CPX MATCHBUF ; At end?
|
|
|
|
BEQ :DONE
|
|
|
|
LDA MATCHBUF+1,X
|
|
|
|
STA MFTEMP+1,Y
|
|
|
|
INX
|
|
|
|
INY
|
|
|
|
BRA :L1
|
|
|
|
:DONE STY MFTEMP ; Update length
|
2021-09-09 17:38:27 +00:00
|
|
|
RTS
|
2021-09-09 02:34:10 +00:00
|
|
|
|
2021-09-09 22:51:22 +00:00
|
|
|
* The following is required in order to be able to resume
|
|
|
|
* a directory search
|
2021-09-10 02:52:22 +00:00
|
|
|
WILDFILE DB $00 ; File ref num for open dir
|
|
|
|
WILDIDX DB $00 ; Dirent idx in current block
|
2021-09-09 22:51:22 +00:00
|
|
|
|
2021-09-09 02:34:10 +00:00
|
|
|
* Read directory, apply wildcard match
|
|
|
|
* Inputs: directory name in XY (Pascal string)
|
|
|
|
* If there is a match, replaces SEGBUF with the first match and CLC
|
|
|
|
* If no match, or any other error, returns with carry set
|
2021-09-09 22:51:22 +00:00
|
|
|
* Leaves the directory open to allow resumption of search.
|
2021-09-10 02:52:22 +00:00
|
|
|
SRCHDIR LDA WILDIDX
|
|
|
|
CMP #$FF ; Is it a new search?
|
|
|
|
BEQ :NEW
|
|
|
|
BRA :S1 ; Continue search
|
|
|
|
:NEW STX OPENPL+1
|
2021-09-09 17:38:27 +00:00
|
|
|
STY OPENPL+2
|
|
|
|
JSR OPENFILE
|
|
|
|
BCS :NODIR
|
2021-09-10 02:52:22 +00:00
|
|
|
LDA OPENPL+5 ; File ref num
|
|
|
|
STA WILDFILE ; Stash for later
|
2021-09-09 17:38:27 +00:00
|
|
|
STA READPL+1
|
2021-09-10 02:52:22 +00:00
|
|
|
:L1 JSR RDFILE ; Read->BLKBUF
|
2021-09-09 17:38:27 +00:00
|
|
|
BCC :S1
|
2021-09-10 02:52:22 +00:00
|
|
|
CMP #$4C ; EOF
|
2021-09-09 17:38:27 +00:00
|
|
|
BEQ :EOF
|
|
|
|
BRA :BADDIR
|
2021-09-10 02:52:22 +00:00
|
|
|
:S1 JSR SRCHBLK ; Handle one block
|
2021-09-09 17:38:27 +00:00
|
|
|
BCS :MATCH
|
|
|
|
BRA :L1
|
|
|
|
:MATCH CLC
|
2021-09-09 22:51:22 +00:00
|
|
|
BRA :EXIT
|
2021-09-09 02:34:10 +00:00
|
|
|
:BADDIR
|
2021-09-09 22:51:22 +00:00
|
|
|
:EOF
|
|
|
|
:NODIR SEC
|
|
|
|
:EXIT RTS
|
|
|
|
|
|
|
|
* Close directory, if it was open
|
|
|
|
* Preserves flags
|
|
|
|
CLSDIR PHP
|
2021-09-10 02:52:22 +00:00
|
|
|
LDA WILDFILE ; File ref num for open dir
|
|
|
|
BEQ :ALREADY ; Already been closed
|
2021-09-09 17:38:27 +00:00
|
|
|
STA CLSPL+1
|
|
|
|
JSR CLSFILE
|
2021-09-10 02:52:22 +00:00
|
|
|
STZ WILDFILE ; Not strictly necessary
|
2021-09-09 22:51:22 +00:00
|
|
|
:ALREADY PLP
|
2021-09-09 17:38:27 +00:00
|
|
|
RTS
|
2021-09-09 02:34:10 +00:00
|
|
|
|
|
|
|
* Apply wildcard match to a directory block
|
|
|
|
* Directory block is in BLKBUF
|
|
|
|
* On exit: set carry if match, clear carry otherwise
|
2021-09-10 02:52:22 +00:00
|
|
|
SRCHBLK LDX WILDIDX
|
|
|
|
CPX #$FF ; Is it a new search?
|
|
|
|
BEQ :NEW
|
|
|
|
BRA :CONT
|
|
|
|
:NEW LDA BLKBUF+4 ; Obtain storage type
|
|
|
|
AND #$E0 ; Mask 3 MSBs
|
2021-09-09 17:38:27 +00:00
|
|
|
CMP #$E0
|
|
|
|
BNE :NOTKEY
|
2021-09-10 02:52:22 +00:00
|
|
|
LDX #$01 ; Skip dir name
|
2021-09-09 17:38:27 +00:00
|
|
|
BRA :L1
|
|
|
|
:NOTKEY LDX #$00
|
|
|
|
:L1 PHX
|
|
|
|
JSR MATCHENT
|
|
|
|
PLX
|
|
|
|
BCS :MATCH
|
2021-09-10 02:52:22 +00:00
|
|
|
:CONT INX
|
|
|
|
CPX #13 ; Number of dirents in block
|
2021-09-09 17:38:27 +00:00
|
|
|
BNE :L1
|
2021-09-10 02:52:22 +00:00
|
|
|
CLC ; Fell off end, no match
|
|
|
|
:MATCH STX WILDIDX ; Record dirent idx for resume
|
2021-09-09 22:51:22 +00:00
|
|
|
RTS
|
2021-09-09 02:34:10 +00:00
|
|
|
|
|
|
|
* Apply wildcard match to a directory entry
|
|
|
|
* On entry: X = dirent index in BLKBUF
|
|
|
|
* On exit: set carry if match, clear carry otherwise
|
2021-09-10 02:52:22 +00:00
|
|
|
MATCHENT LDA #<BLKBUF+4 ; Skip pointers
|
2021-09-09 17:38:27 +00:00
|
|
|
STA A1L
|
|
|
|
LDA #>BLKBUF+4
|
|
|
|
STA A1H
|
|
|
|
:L1 CPX #$00
|
|
|
|
BEQ :S1
|
|
|
|
CLC
|
2021-09-10 02:52:22 +00:00
|
|
|
LDA #$27 ; Size of dirent
|
2021-09-09 17:38:27 +00:00
|
|
|
ADC A1L
|
|
|
|
STA A1L
|
|
|
|
LDA #$00
|
|
|
|
ADC A1H
|
|
|
|
STA A1H
|
|
|
|
DEX
|
|
|
|
BRA :L1
|
|
|
|
:S1 LDY #$00
|
2021-09-10 02:52:22 +00:00
|
|
|
LDA (A1L),Y ; Length byte
|
|
|
|
BEQ :NOMATCH ; Inactive entry
|
|
|
|
INC A1L ; Inc ptr, skip length byte
|
2021-09-09 17:38:27 +00:00
|
|
|
BNE :S2
|
|
|
|
INC A1H
|
2021-09-10 02:52:22 +00:00
|
|
|
:S2 JSR MATCH ; Try wildcard match
|
2021-09-09 17:38:27 +00:00
|
|
|
BCC :NOMATCH
|
2021-09-10 02:52:22 +00:00
|
|
|
LDA A1L ; Decrement ptr again
|
2021-09-09 17:38:27 +00:00
|
|
|
BNE :S3
|
|
|
|
DEC A1H
|
|
|
|
:S3 DEC A1L
|
2021-09-10 02:52:22 +00:00
|
|
|
LDY #$00 ; If matches, copy matching filename
|
|
|
|
LDA (A1L),Y ; Length of filename
|
|
|
|
AND #$0F ; Mask out other ProDOS stuff
|
|
|
|
STA MATCHBUF
|
2021-09-09 17:38:27 +00:00
|
|
|
TAY
|
|
|
|
:L2 CPY #$00
|
|
|
|
BEQ :MATCH
|
|
|
|
LDA (A1L),Y
|
2021-09-10 02:52:22 +00:00
|
|
|
STA MATCHBUF,Y
|
2021-09-09 17:38:27 +00:00
|
|
|
DEY
|
|
|
|
BRA :L2
|
|
|
|
:MATCH SEC
|
|
|
|
RTS
|
|
|
|
:NOMATCH CLC
|
|
|
|
RTS
|
2021-09-09 02:34:10 +00:00
|
|
|
|
|
|
|
* From: http://6502.org/source/strings/patmatch.htm
|
|
|
|
* Input: A NUL-terminated, <255-length pattern at address PATTERN.
|
|
|
|
* A NUL-terminated, <255-length string pointed to by STR.
|
|
|
|
* Output: Carry bit = 1 if the string matches the pattern, = 0 if not.
|
|
|
|
* Notes: Clobbers A, X, Y. Each * in the pattern uses 4 bytes of stack.
|
|
|
|
|
2021-09-10 02:52:22 +00:00
|
|
|
MATCH1 EQU '?' ; Matches exactly 1 character
|
|
|
|
MATCHN EQU '*' ; Matches any string (including "")
|
|
|
|
STR EQU A1L ; Pointer to string to match
|
2021-09-09 02:34:10 +00:00
|
|
|
|
2021-09-10 02:52:22 +00:00
|
|
|
MATCH LDX #$00 ; X is an index in the pattern
|
|
|
|
LDY #$FF ; Y is an index in the string
|
|
|
|
:NEXT LDA SEGBUF+1,X ; Look at next pattern character
|
|
|
|
CMP #MATCHN ; Is it a star?
|
|
|
|
BEQ :STAR ; Yes, do the complicated stuff
|
|
|
|
INY ; No, let's look at the string
|
|
|
|
CMP #MATCH1 ; Is the pattern caracter a ques?
|
|
|
|
BNE :REG ; No, it's a regular character
|
|
|
|
LDA (STR),Y ; Yes, so it will match anything
|
|
|
|
BEQ :FAIL ; except the end of string
|
|
|
|
:REG CMP (STR),Y ; Are both characters the same?
|
|
|
|
BNE :FAIL ; No, so no match
|
|
|
|
INX ; Yes, keep checking
|
|
|
|
CMP #0 ; Are we at end of string?
|
|
|
|
BNE :NEXT ; Not yet, loop
|
|
|
|
:FOUND RTS ; Success, return with C=1
|
2021-09-09 02:34:10 +00:00
|
|
|
|
2021-09-10 02:52:22 +00:00
|
|
|
:STAR INX ; Skip star in pattern
|
|
|
|
CMP SEGBUF+1,X ; String of stars equals one star
|
|
|
|
BEQ :STAR ; so skip them also
|
|
|
|
:STLOOP TXA ; We first try to match with * = ""
|
|
|
|
PHA ; and grow it by 1 character every
|
|
|
|
TYA ; time we loop
|
|
|
|
PHA ; Save X and Y on stack
|
|
|
|
JSR :NEXT ; Recursive call
|
|
|
|
PLA ; Restore X and Y
|
2021-09-09 17:38:27 +00:00
|
|
|
TAY
|
|
|
|
PLA
|
|
|
|
TAX
|
2021-09-10 02:52:22 +00:00
|
|
|
BCS :FOUND ; We found a match, return with C=1
|
|
|
|
INY ; No match yet, try to grow * string
|
|
|
|
LDA (STR),Y ; Are we at the end of string?
|
|
|
|
BNE :STLOOP ; Not yet, add a character
|
|
|
|
:FAIL CLC ; Yes, no match found, return with C=0
|
2021-09-09 17:38:27 +00:00
|
|
|
RTS
|
2021-09-09 02:34:10 +00:00
|
|
|
|
2021-09-10 02:52:22 +00:00
|
|
|
SEGBUF DS 65 ; For storing path segments (Pascal str)
|
|
|
|
MATCHBUF DS 65 ; For storing match results (Pascal str)
|
|
|
|
|
|
|
|
|
2021-09-09 02:34:10 +00:00
|
|
|
|