Working version of SED! Except it can't handle spaces in pattern.

This commit is contained in:
Bobbi Webber-Manners 2021-06-19 23:05:46 -04:00
parent 5bdd831d51
commit e6fba6a648

View File

@ -21,6 +21,8 @@ hFile .BS 1
hBuf .BS 1 hBuf .BS 1
LineNum .BS 2 LineNum .BS 2
char .BS 1 char .BS 1
delimiter .BS 1
replaceidx .BS 1
bIgnoreCase .BS 1 bIgnoreCase .BS 1
@ -100,6 +102,8 @@ CS.RUN.CheckArgs
sta ArgPattern sta ArgPattern
>LDYA ZPPtr1 >LDYA ZPPtr1
>STYA ZPPatternPtr >STYA ZPPatternPtr
jsr CS.RUN.SEDParser
bcs .97
bra CS.RUN.CheckArgs bra CS.RUN.CheckArgs
.11 lda hFile .11 lda hFile
@ -185,6 +189,54 @@ CS.RUN.NextArg inc ArgIndex
.9 rts .9 rts
*-------------------------------------- *--------------------------------------
CS.RUN.SEDParser
* On entry, the expression is in ZPPatternPtr
* We are looking for "s/search string/replace string/"
* The '/' delimiter can be any char
* On exit. ZPPatternPtr points to delimiter-terminated search string,
* replaceidx contains the offset to the start of replacement str
stz LineNum Reusing this as cntr
lda (ZPPatternPtr)
cmp #'s' Substitute cmd
bne .8 If not, error
ldy #$01 Delimiter char
lda (ZPPatternPtr),y
sta delimiter Stash for later
.2 iny
lda (ZPPatternPtr),y
beq .4 End of string
cmp delimiter Is it delimiter?
bne .2
inc LineNum Keep count
lda LineNum
cmp #$01 Second delim
bne .2
sty replaceidx
bra .2
.4 lda LineNum Check # of delims
cmp #$02
bne .8
inc ZPPatternPtr Eat 's/'
bne .5
inc ZPPatternPtr+1
.5 inc ZPPatternPtr
bne .7
inc ZPPatternPtr+1
.7 clc
rts No error return
.8 sec
rts
*--------------------------------------
CS.RUN.OPEN >PUSHYA CS.RUN.OPEN >PUSHYA
>PUSHBI O.RDONLY+O.TEXT >PUSHBI O.RDONLY+O.TEXT
>PUSHBI S.FI.T.TXT >PUSHBI S.FI.T.TXT
@ -209,10 +261,12 @@ CS.RUN.PRINT >LDYA ZPBufPtr
bne .3 bne .3
lda (ZPPatternPtr),y EOL lda (ZPPatternPtr),y EOL
cmp delimiter
bne .7 Not end of pattern bne .7 Not end of pattern
* No match. * No match.
.3 lda (ZPPatternPtr),y .3 lda (ZPPatternPtr),y
cmp delimiter
beq .5 Match beq .5 Match
jsr CS.RUN.toUpper jsr CS.RUN.toUpper
@ -241,10 +295,19 @@ CS.RUN.PRINT >LDYA ZPBufPtr
clc clc
rts rts
*-------------------------------------- *--------------------------------------
CS.RUN.GotMatch lda #'*' TODO ... CS.RUN.GotMatch phy
ldy replaceidx
dey
.1 lda (ZPPatternPtr),y
cmp delimiter
beq .5
phy phy
>SYSCALL PutChar >SYSCALL PutChar
ply ply
iny
bra .1
.5 ply
tya Advance ZPPtr1 by Y tya Advance ZPPtr1 by Y
clc clc
adc ZPPtr1 adc ZPPtr1
@ -302,7 +365,8 @@ CS.END
OptionList .AS "HhIi" OptionList .AS "HhIi"
OptionList.Cnt .EQ *-OptionList OptionList.Cnt .EQ *-OptionList
*-------------------------------------- *--------------------------------------
MSG.USAGE .AS "Usage : SED <pattern> <File> or CMD|SED <pattern>\r\n" MSG.USAGE .AS "Usage : SED [-I] s/pattern/replacement/ <File>\r\n"
.AS " or : CMD|SED [-I] s/pattern/replacement/\r\n"
.AS " -I : Ignore Case" .AS " -I : Ignore Case"
MSG.CRLF .AZ "\r\n" MSG.CRLF .AZ "\r\n"
*-------------------------------------- *--------------------------------------