NEW
  AUTO 3,1
				.LIST OFF
				.OP	65C02
				.OR	$2000
				.TF bin/grep
*/-------------------------------------
* # 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
*\-------------------------------------
				.INB inc/macros.i
				.INB inc/a2osx.i
				.INB inc/kernel.i
				.INB inc/mli.i
				.INB inc/mli.e.i
*--------------------------------------
				.DUMMY
				.OR ZPBIN
ZS.START
ZPPtr1			.BS 2
ZPPatternPtr	.BS 2
ZPBufPtr		.BS 2

ArgIndex		.BS 1
ArgPattern		.BS 1

hFile			.BS 1
hBuf			.BS 1

LineNum			.BS 2
char			.BS 1

bIgnoreCase		.BS 1
bLineNum		.BS 1
bQuiet			.BS 1
bSilent			.BS 1
bInvertMatch	.BS 1

bFound			.BS 1

ZS.END			.ED
*--------------------------------------
*			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
*--------------------------------------
.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
*--------------------------------------
CS.RUN			jsr CS.RUN.CheckArgs
				bcs .99

				>LDYAI 256
				>SYSCALL GetMem
				bcs .99

				>STYA ZPBufPtr
				stx hBuf

.1				>SLEEP

				>PUSHB hFile
				>PUSHW ZPBufPtr
				>PUSHWI	256
				>SYSCALL FGetS
				bcs	.7

				inc LineNum
				bne .2

				inc LineNum+1

.2				jsr CS.RUN.PRINT

				bcc .1

				rts

.7				cmp #MLI.E.EOF
				bne .98
				
				lda bQuiet
				bmi .8
				
				bit bFound
				bmi .8
				
				lda #E.NOKEY
				sec
				rts

.8				lda #0					Exit with no Error
.98				sec
.99				rts
*--------------------------------------
CS.RUN.CheckArgs
				jsr CS.RUN.NextArg
				bcs .7

				lda (ZPPtr1)
				cmp #'-'
				beq .1

				lda ArgPattern
				bne .11

				lda ArgIndex
				sta ArgPattern
				>LDYA ZPPtr1
				>STYA ZPPatternPtr
				bra CS.RUN.CheckArgs

.11				lda hFile
				bne .97

				>LDYA ZPPtr1
				jsr CS.RUN.OPEN
				bcs .9

				sta hFile
				bra CS.RUN.CheckArgs
*--------------------------------------
.1				ldy #1 
				lda (ZPPtr1),y
				beq .97

.2				ldx #OptionList.Cnt-1

.3				cmp OptionList,x
				beq .4

				dex
				bpl .3

				bra .97

.4				txa
				lsr
				beq .98					-H or -h

				tax
				sec
				ror bIgnoreCase-1,x
				iny 
				lda (ZPPtr1),y
				bne .2
				
				bra CS.RUN.CheckArgs
*--------------------------------------
.7				lda hFile
				bne .8

				ldy #S.PS.hStdIn
				lda (pPS),y
				tay
				ldx FILEs.hFD-1,y
				lda FDs.pLO-1,x
				sta ZPPtr1
				lda FDs.pHI-1,x
				sta ZPPtr1+1

				lda (ZPPtr1)
				cmp #S.FD.T.PIPE
				bne .97
				
				sty hFile
	
.8				clc

.9				rts
*--------------------------------------
.97				lda #E.SYN

.98				pha
				>PUSHW L.MSG.USAGE
				>PUSHBI 0
				>SYSCALL PrintF
				pla
				sec
				rts
*--------------------------------------
CS.RUN.NextArg	inc ArgIndex
				lda ArgIndex
				>SYSCALL ArgV
				bcs .9

				>STYA ZPPtr1

.9				rts
*--------------------------------------
CS.RUN.OPEN		>PUSHYA
				>PUSHBI	O.RDONLY+O.TEXT
				>PUSHBI S.FI.T.TXT
				>PUSHWZ				Aux type
				>SYSCALL FOpen
				bcs .9

				sta hFile

.9				rts
*--------------------------------------
CS.RUN.PRINT	>LDYA ZPBufPtr
				>STYA ZPPtr1

.1				lda (ZPPtr1)
				beq .4

				ldy #$ff

.2				iny
				lda (ZPPtr1),y
				bne .3

				lda (ZPPatternPtr),y
				bne .4

.3				lda (ZPPatternPtr),y
				beq .6

				jsr CS.RUN.toUpper
				sta char
				lda (ZPPtr1),y
				jsr CS.RUN.toUpper
				cmp char
				beq .2

				inc ZPPtr1
				bne .1

				inc ZPPtr1+1
				bra .1
				
.4				clc						not matching

				.HS B0					BCS

.6				sec						matching

				ror
				eor bInvertMatch
				bpl .8
				
				sec
				ror bFound
				
				lda bQuiet
				ora bSilent
				bmi .8
				
				bit bLineNum
				bpl .7

				>PUSHW L.MSG.NUMLINE
				>PUSHW LineNum
				>PUSHBI 2
				>SYSCALL PrintF
				bcs .9

.7				>LDYA ZPBufPtr
				>SYSCALL PutS
				rts

.8				clc
.9				rts
*--------------------------------------
CS.RUN.toUpper	bit bIgnoreCase
				bpl .9

				cmp #'a'
				bcc .9

				cmp #'z'+1
				bcs .9

				eor #$20

.9				rts	
*--------------------------------------
CS.QUIT			lda hFile
				beq .1

				>SYSCALL FClose

.1				lda hBuf
				beq .8

				>SYSCALL FreeMem

.8				clc
				rts
*--------------------------------------
CS.DOEVENT		sec
				rts
*--------------------------------------
CS.END
*--------------------------------------
OptionList		.AS "HhIiNnQqSsVv"
OptionList.Cnt	.EQ *-OptionList
*--------------------------------------
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:"
*--------------------------------------
				.DUMMY
				.OR 0
DS.START
DS.END			.ED
*--------------------------------------
MAN
SAVE usr/src/bin/grep.s
ASM
