A2osX/BIN/GREP.S.txt

355 lines
5.8 KiB
Plaintext
Raw Normal View History

2019-03-13 11:37:05 +00:00
NEW
2020-03-03 16:44:52 +00:00
AUTO 3,1
2019-03-13 11:37:05 +00:00
.LIST OFF
.OP 65C02
.OR $2000
2020-03-03 16:44:52 +00:00
.TF bin/grep
2022-10-10 11:58:50 +00:00
*/-------------------------------------
* # GREP
* Print lines that match patterns
* ## Arguments
* **-H**
* Help screen
* **-I**
* Ignoce case
* **-N**
* Print line number
* **-Q**
* Quiet mode
* **-S**
* Silent mode
* **-V**
* Invert match
* ## Return Value
* if **-Q** specified, always 0
* otherwise, 0 if a match found, E.NOKEY if not
*\-------------------------------------
2020-05-25 21:03:11 +00:00
.INB inc/macros.i
.INB inc/a2osx.i
.INB inc/kernel.i
.INB inc/mli.i
.INB inc/mli.e.i
2019-03-13 11:37:05 +00:00
*--------------------------------------
.DUMMY
.OR ZPBIN
ZS.START
2020-08-17 20:47:31 +00:00
ZPPtr1 .BS 2
2019-03-13 11:37:05 +00:00
ZPPatternPtr .BS 2
ZPBufPtr .BS 2
2022-10-10 11:58:50 +00:00
ArgIndex .BS 1
ArgPattern .BS 1
2019-03-13 11:37:05 +00:00
hFile .BS 1
hBuf .BS 1
2022-10-10 11:58:50 +00:00
2019-03-13 11:37:05 +00:00
LineNum .BS 2
char .BS 1
2021-04-16 19:17:18 +00:00
2019-03-13 11:37:05 +00:00
bIgnoreCase .BS 1
bLineNum .BS 1
2022-10-10 11:58:50 +00:00
bQuiet .BS 1
bSilent .BS 1
2021-04-15 20:52:08 +00:00
bInvertMatch .BS 1
2021-04-16 19:17:18 +00:00
2022-10-10 11:58:50 +00:00
bFound .BS 1
2020-08-17 20:47:31 +00:00
ZS.END .ED
2019-03-13 11:37:05 +00:00
*--------------------------------------
* File Header (16 Bytes)
*--------------------------------------
CS.START cld
jmp (.1,x)
.DA #$61 6502,Level 1 (65c02)
.DA #1 BIN Layout Version 1
.DA #0 S.PS.F.EVENT
.DA #0
.DA CS.END-CS.START Code Size (without Constants)
.DA DS.END-DS.START Data SegmentSize
.DA #16 Stack Size
.DA #ZS.END-ZS.START Zero Page Size
.DA 0
*--------------------------------------
* Relocation Table
2019-10-03 06:25:27 +00:00
*--------------------------------------
2019-03-13 11:37:05 +00:00
.1 .DA CS.INIT
.DA CS.RUN
.DA CS.DOEVENT
.DA CS.QUIT
L.MSG.USAGE .DA MSG.USAGE
L.MSG.CRLF .DA MSG.CRLF
L.MSG.NUMLINE .DA MSG.NUMLINE
.DA 0
*--------------------------------------
CS.INIT clc
rts
*--------------------------------------
2021-04-15 20:52:08 +00:00
CS.RUN jsr CS.RUN.CheckArgs
2022-10-10 11:58:50 +00:00
bcs .99
2021-04-15 20:52:08 +00:00
2022-10-10 11:58:50 +00:00
>LDYAI 256
>SYSCALL GetMem
bcs .99
2021-04-15 20:52:08 +00:00
2022-10-10 11:58:50 +00:00
>STYA ZPBufPtr
stx hBuf
.1 >SLEEP
2021-04-15 20:52:08 +00:00
>PUSHB hFile
>PUSHW ZPBufPtr
>PUSHWI 256
>SYSCALL FGetS
2022-10-10 11:58:50 +00:00
bcs .7
2021-04-15 20:52:08 +00:00
inc LineNum
bne .2
inc LineNum+1
.2 jsr CS.RUN.PRINT
2022-10-10 11:58:50 +00:00
bcc .1
2021-04-15 20:52:08 +00:00
2022-10-10 11:58:50 +00:00
rts
2021-04-15 20:52:08 +00:00
2022-10-10 11:58:50 +00:00
.7 cmp #MLI.E.EOF
bne .98
lda bQuiet
bmi .8
bit bFound
bmi .8
lda #E.NOKEY
sec
rts
2021-04-15 20:52:08 +00:00
2022-10-10 11:58:50 +00:00
.8 lda #0 Exit with no Error
.98 sec
.99 rts
2021-04-15 20:52:08 +00:00
*--------------------------------------
CS.RUN.CheckArgs
jsr CS.RUN.NextArg
2022-10-10 11:58:50 +00:00
bcs .7
2021-04-15 20:52:08 +00:00
2019-03-13 11:37:05 +00:00
lda (ZPPtr1)
cmp #'-'
beq .1
2021-04-15 20:52:08 +00:00
2019-03-13 11:37:05 +00:00
lda ArgPattern
bne .11
2021-04-15 20:52:08 +00:00
2019-03-13 11:37:05 +00:00
lda ArgIndex
sta ArgPattern
>LDYA ZPPtr1
>STYA ZPPatternPtr
2021-04-15 20:52:08 +00:00
bra CS.RUN.CheckArgs
2019-03-13 11:37:05 +00:00
.11 lda hFile
bne .97
>LDYA ZPPtr1
jsr CS.RUN.OPEN
bcs .9
2021-04-15 20:52:08 +00:00
2019-03-13 11:37:05 +00:00
sta hFile
2021-04-15 20:52:08 +00:00
bra CS.RUN.CheckArgs
2022-10-10 11:58:50 +00:00
*--------------------------------------
2019-03-13 11:37:05 +00:00
.1 ldy #1
lda (ZPPtr1),y
2022-10-10 11:58:50 +00:00
beq .97
2019-03-13 11:37:05 +00:00
2022-10-10 11:58:50 +00:00
.2 ldx #OptionList.Cnt-1
2021-04-16 19:17:18 +00:00
2022-10-10 11:58:50 +00:00
.3 cmp OptionList,x
beq .4
2021-04-15 20:52:08 +00:00
2019-03-13 11:37:05 +00:00
dex
2022-10-10 11:58:50 +00:00
bpl .3
2021-04-15 20:52:08 +00:00
2019-03-13 11:37:05 +00:00
bra .97
2021-04-15 20:52:08 +00:00
2022-10-10 11:58:50 +00:00
.4 txa
2021-04-16 19:17:18 +00:00
lsr
2022-10-10 11:58:50 +00:00
beq .98 -H or -h
2021-04-15 20:52:08 +00:00
2019-03-13 11:37:05 +00:00
tax
2022-10-10 11:58:50 +00:00
sec
ror bIgnoreCase-1,x
iny
lda (ZPPtr1),y
bne .2
2021-04-15 20:52:08 +00:00
2022-10-10 11:58:50 +00:00
bra CS.RUN.CheckArgs
*--------------------------------------
.7 lda hFile
bne .8
2019-10-26 14:33:37 +00:00
2019-03-13 11:37:05 +00:00
ldy #S.PS.hStdIn
lda (pPS),y
tax
2020-12-15 13:23:22 +00:00
2021-07-24 20:42:23 +00:00
lda hFDs-1,x
2019-03-13 11:37:05 +00:00
>SYSCALL GetMemPtr
>STYA ZPPtr1
lda (ZPPtr1)
cmp #S.FD.T.PIPE
bne .97
2019-10-26 14:33:37 +00:00
ldy #S.PS.hStdIn
lda (pPS),y
sta hFile
2019-03-13 11:37:05 +00:00
2022-10-10 11:58:50 +00:00
.8 clc
2020-12-23 14:54:57 +00:00
2021-04-15 20:52:08 +00:00
.9 rts
2022-10-10 11:58:50 +00:00
*--------------------------------------
2021-04-15 20:52:08 +00:00
.97 lda #E.SYN
2021-04-16 19:17:18 +00:00
2021-04-15 20:52:08 +00:00
.98 pha
>PUSHW L.MSG.USAGE
>PUSHBI 0
>SYSCALL PrintF
pla
sec
2019-03-13 11:37:05 +00:00
rts
*--------------------------------------
2021-04-15 20:52:08 +00:00
CS.RUN.NextArg inc ArgIndex
lda ArgIndex
>SYSCALL ArgV
bcs .9
2021-04-16 19:17:18 +00:00
2021-04-15 20:52:08 +00:00
>STYA ZPPtr1
.9 rts
*--------------------------------------
2020-03-03 16:44:52 +00:00
CS.RUN.OPEN >PUSHYA
2019-03-13 11:37:05 +00:00
>PUSHBI O.RDONLY+O.TEXT
2020-03-03 16:44:52 +00:00
>PUSHBI S.FI.T.TXT
>PUSHWZ Aux type
2019-03-13 11:37:05 +00:00
>SYSCALL FOpen
bcs .9
2021-04-15 20:52:08 +00:00
2019-03-13 11:37:05 +00:00
sta hFile
.9 rts
*--------------------------------------
CS.RUN.PRINT >LDYA ZPBufPtr
>STYA ZPPtr1
2021-04-15 20:52:08 +00:00
2019-03-13 11:37:05 +00:00
.1 lda (ZPPtr1)
2021-04-16 19:17:18 +00:00
beq .4
2021-04-15 20:52:08 +00:00
2019-03-13 11:37:05 +00:00
ldy #$ff
.2 iny
lda (ZPPtr1),y
bne .3
lda (ZPPatternPtr),y
2021-04-15 20:52:08 +00:00
bne .4
2019-03-13 11:37:05 +00:00
.3 lda (ZPPatternPtr),y
beq .6
2021-04-15 20:52:08 +00:00
2019-03-13 11:37:05 +00:00
jsr CS.RUN.toUpper
sta char
lda (ZPPtr1),y
jsr CS.RUN.toUpper
cmp char
beq .2
2021-04-15 20:52:08 +00:00
2019-03-13 11:37:05 +00:00
inc ZPPtr1
bne .1
2021-04-15 20:52:08 +00:00
2019-03-13 11:37:05 +00:00
inc ZPPtr1+1
bra .1
2021-04-15 20:52:08 +00:00
.4 clc not matching
2019-03-13 11:37:05 +00:00
2021-04-15 20:52:08 +00:00
.HS B0 BCS
.6 sec matching
ror
eor bInvertMatch
bpl .8
2022-10-10 11:58:50 +00:00
sec
ror bFound
lda bQuiet
ora bSilent
bmi .8
bit bLineNum
2019-03-13 11:37:05 +00:00
bpl .7
2020-01-29 21:45:06 +00:00
>PUSHW L.MSG.NUMLINE
2019-03-13 11:37:05 +00:00
>PUSHW LineNum
>PUSHBI 2
2020-02-28 07:21:46 +00:00
>SYSCALL PrintF
2019-03-13 11:37:05 +00:00
bcs .9
2021-04-15 20:52:08 +00:00
2019-03-13 11:37:05 +00:00
.7 >LDYA ZPBufPtr
2020-02-28 07:21:46 +00:00
>SYSCALL PutS
2019-03-13 11:37:05 +00:00
rts
.8 clc
.9 rts
*--------------------------------------
CS.RUN.toUpper bit bIgnoreCase
bpl .9
2021-04-16 19:17:18 +00:00
2019-03-13 11:37:05 +00:00
cmp #'a'
bcc .9
2021-04-16 19:17:18 +00:00
2019-03-13 11:37:05 +00:00
cmp #'z'+1
bcs .9
2021-04-16 19:17:18 +00:00
2019-03-13 11:37:05 +00:00
eor #$20
.9 rts
*--------------------------------------
2020-08-17 20:47:31 +00:00
CS.QUIT lda hFile
2019-03-13 11:37:05 +00:00
beq .1
2019-10-26 14:33:37 +00:00
>SYSCALL FClose
2019-03-13 11:37:05 +00:00
2019-10-26 14:33:37 +00:00
.1 lda hBuf
beq .8
>SYSCALL FreeMem
2019-03-13 11:37:05 +00:00
.8 clc
rts
*--------------------------------------
CS.DOEVENT sec
rts
*--------------------------------------
CS.END
*--------------------------------------
2022-10-10 11:58:50 +00:00
OptionList .AS "HhIiNnQqSsVv"
2019-03-13 11:37:05 +00:00
OptionList.Cnt .EQ *-OptionList
*--------------------------------------
2022-10-10 11:58:50 +00:00
MSG.USAGE .CS "Usage : GREP <pattern> <File> or CMD|GREP <pattern>\r\n"
.CS " -H : This help screen\r\n"
.CS " -I : Ignore Case\r\n"
.CS " -N : Print line Number\r\n"
.CS " -Q : Quiet Mode\r\n"
.CS " -S : Silent Mode\r\n"
.CS " -V : Invert Match"
MSG.CRLF .CZ "\r\n"
MSG.NUMLINE .CZ "%5D:"
2019-03-13 11:37:05 +00:00
*--------------------------------------
.DUMMY
.OR 0
DS.START
2019-10-26 14:33:37 +00:00
DS.END .ED
2019-03-13 11:37:05 +00:00
*--------------------------------------
MAN
2020-05-25 21:03:11 +00:00
SAVE usr/src/bin/grep.s
2019-03-13 11:37:05 +00:00
ASM