NEW
  AUTO 3,1
				.LIST OFF
				.OP	65C02
				.OR	$2000
				.TF bin/wc
*--------------------------------------
				.INB inc/macros.i
				.INB inc/a2osx.i
				.INB inc/kernel.i
				.INB inc/mli.i
				.INB inc/mli.e.i
*--------------------------------------
X.ENTER.SUBDIR	.EQ 0
X.COPY.TO.DEST	.EQ 0
X.DELETE.SOURCE	.EQ 0
X.RESET.SRC.DIR	.EQ 0
*--------------------------------------
				.DUMMY
				.OR ZPBIN
ZS.START
ZPPtr1			.BS 2
ZPPtr2			.BS 2
ZPFileName		.BS 2
ZPFileStat		.BS 2

ZPFullPath		.BS 2
ZPRelPath		.BS 2
ZPLineBuf		.BS 2

ArgIndex		.BS 1
hSrcFullPath	.BS 1
hFile			.BS 1
hMem			.BS 1

bTabbed			.BS 1

bPause			.BS 1
bContinue		.BS 1
bRecurse		.BS 1
bChar			.BS 1
bLine			.BS 1
bWord			.BS 1
bInWord			.BS 1
bTotal			.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
				.DA CS.END-CS.START		Code Size (without Constants)
				.DA DS.END-DS.START		Data SegmentSize
				.DA #64					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.ERR		.DA MSG.ERR
L.MSG.LONGTAB	.DA MSG.LONGTAB
L.MSG.LONG		.DA MSG.LONG
L.MSG.TOTAL		.DA MSG.TOTAL
				.DA 0
*--------------------------------------
CS.INIT			clc
				rts
*--------------------------------------
CS.RUN			inc ArgIndex
				lda ArgIndex
				>SYSCALL ArgV
				bcs .5
				
				>STYA ZPPtr1
				lda (ZPPtr1)
				cmp #'-'
				bne .4
				
				jsr CS.RUN.CheckOpt
				bcc CS.RUN

.9				>LDYA L.MSG.USAGE
				>SYSCALL PutS
				lda #E.SYN
				sec
				rts

.4				>LDA.G hSrcBasePath
				bne .9

				>LDYA ZPPtr1
				jsr X.InitSrcDirYA
				bcc CS.RUN

.99				rts
*--------------------------------------
.5				lda bChar
				ora bLine
				ora bWord
				bne .51
				
				lda #$ff
				sta bChar
				sta bLine
				sta bWord

.51				>LDA.G hSrcBasePath
				bne .6
				
				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 .9
				
				jmp CS.RUN.PIPE

.6				>LDYAI 256
				>SYSCALL GetMem
				bcs .99

				>STYA ZPFullPath
				stx hSrcFullPath

				>LDA.G hSrcBasePath
				>SYSCALL GetMemPtr
				>STYA ZPPtr1

				ldy #$ff

.80				iny
				lda (ZPPtr1),y
				bne .80

				tya
				clc
				adc ZPFullPath
				sta ZPRelPath
				lda ZPFullPath+1
				adc #0
				sta ZPRelPath+1

				>LDYAI 256
				>SYSCALL GetMem
				bcs .99

				>STYA ZPLineBuf
				stx hMem
*--------------------------------------
CS.RUN.LOOP		ldy #S.PS.hStdIn
				lda (pPS),y
				>SYSCALL FEOF
				bcs .99

				tay
				bne .10

				>SYSCALL GetChar
				bcs .99

				cmp #$03				Ctrl-C
				beq .99					Abort....

				cmp #$13				Ctrl-S
				bne .10

				lda bPause
				eor	#$ff
				sta bPause
				bne CS.RUN.LOOP

.10				lda bPause
				bne CS.RUN.LOOP			Pause...
	
				jsr X.GetEntry
				bcs .9

				ldy #S.STAT.P.DRIVE
				lda (ZPFileStat),y		ProDOS Device ?
				beq .5

				jsr CS.RUN.DEV
				bcc CS.RUN.LOOP

				rts
				
.5				ldy #S.STAT.MODE+1
				lda (ZPFileStat),y
				and #$F0
				cmp /S.STAT.MODE.DIR
				bne .6

				jsr CS.RUN.DIR
				bcs .99

				bra .8			

.6				jsr CS.RUN.FILE
				bcs .99

				bra .8

.9				jsr X.LeaveSubDir
				bcs .90

				jsr X.BasePath..
.8				jsr X.GetNextEntry
				jmp CS.RUN.LOOP

.90				jsr CS.RUN.PrintTstat
				lda #0
				sec
.99				rts
*--------------------------------------
CS.RUN.PIPE		>LDYAI 256
				>SYSCALL GetMem
				bcs .99

				>STYA ZPLineBuf
				stx hMem

				ldy #S.PS.hStdIn
				lda (pPS),y
				
				sta hFile

				jsr CS.RUN.FILE.LOOP
				jsr CS.RUN.PrintFstat
				
				lda #0
				sec
.99				rts
*--------------------------------------
CS.RUN.DIR		lda bRecurse
				bpl .8

				lda (ZPFileName)
				cmp #'.'
				beq .8

				>LDYA ZPFileName
				jsr X.EnterSubDirYA
				jmp CS.RUN.CheckErr

.8				clc
				rts
*--------------------------------------
CS.RUN.DEV
.8				jsr X.GetNextEntry

CS.RUN.DEV.CLC.RTS
				clc
				rts
*--------------------------------------
CS.RUN.FILE		jsr X.IncludeMatch
				bcs CS.RUN.DEV.CLC.RTS	no match, skip....

				jsr CS.RUN.GetFilePath

				jsr CS.RUN.OpenFile
				bcs .9

				jsr CS.RUN.FILE.LOOP
				bcs .7
				
				jsr CS.RUN.CloseFile

				jsr CS.RUN.PrintFstat
				bcs .99

				jsr CS.RUN.AddFstat2Tstat
				lda bTotal
				bmi .8

				beq .61

				lda #$fe

.61				inc
				sta bTotal

.8				clc
				rts

.7				pha
				jsr CS.RUN.CloseFile
				pla
				sec
.9				jmp CS.RUN.CheckErr

.99				rts
*--------------------------------------
CS.RUN.FILE.LOOP
.1				>PUSHB hFile
				>PUSHW ZPLineBuf
				>PUSHWI 255
				>SYSCALL FGetS
				bcs .8

				jsr CS.RUN.IncFLine
				ldy #$ff
				stz bInWord

.2				iny
				lda (ZPLineBuf),y
				bne .21
				
				lda bInWord
				beq .1

				jsr CS.RUN.IncFWord
				bra .1

.21				cmp #C.SPACE
				beq .3

				cmp #C.TAB
				bne .4

.3				lda bInWord
				beq .5 

				jsr CS.RUN.IncFWord
				stz bInWord
				bra .5

.4				inc bInWord

.5				jsr CS.RUN.IncFChar
				bra .2
				
.8				cmp #MLI.E.EOF
				bne .9

				clc
				rts
				
.9				sec
				rts
*--------------------------------------
CS.RUN.CheckErr	bcc .9

				pha
				>PUSHW L.MSG.ERR
				>PUSHA
				>PUSHBI 1
				>SYSCALL PrintF
				lda bContinue
				eor #$80
				asl
				pla

.9				rts
*--------------------------------------
CS.RUN.GetFilePath
				>PUSHW ZPFullPath
				>LDA.G hSrcBasePath
				>SYSCALL GetMemPtr
				>PUSHYA
				>SYSCALL StrCpy

				>PUSHW ZPFullPath
				>PUSHW ZPFileName
				>SYSCALL StrCat
				rts
*--------------------------------------
CS.RUN.OpenFile	ldx #12
				ldy #fCharCount

				lda #0

.1				sta (pData),y
				iny
				dex
				bne .1
				
				>PUSHW ZPFullPath
				>PUSHBI	O.RDONLY+O.TEXT
				>PUSHBI 0				Type
				>PUSHWZ					Aux type
				>SYSCALL FOpen
				bcs .9

				sta hFile

.9				rts
*--------------------------------------
CS.RUN.CloseFile
				lda hFile
				>SYSCALL FClose
				bcs .9
				
				stz hFile
				
.9				rts
*--------------------------------------
CS.RUN.IncFChar	phy
				ldy #fCharCount
				bra CS.RUN.IncF

CS.RUN.IncFLine	phy
				ldy #fLineCount
				bra CS.RUN.IncF

CS.RUN.IncFWord	phy

				ldy #fWordCount
				
CS.RUN.IncF		ldx #4

.1				lda (pData),y
				inc
				sta (pData),y
				bne .8

				iny
				dex
				bne .1

.8				ply
				rts
*--------------------------------------
CS.RUN.AddFstat2Tstat
				>PUSHL.G fWordCount
				>PUSHL.G tWordCount
				>FPU uADD
				>PULLL.G tWordCount

				>PUSHL.G fLineCount
				>PUSHL.G tLineCount
				>FPU uADD
				>PULLL.G tLineCount

				>PUSHL.G fCharCount
				>PUSHL.G tCharCount
				>FPU uADD
				>PULLL.G tCharCount
				rts
*--------------------------------------
CS.RUN.PrintFstat
				sec
				bit bTotal
				bmi .19
				
				ldx #0
				
				bit bWord
				bpl .10
				
				inx
				
.10				bit bLine
				bpl .11
				
				inx
				
.11				bit bChar
				bpl .12

				inx

.12				cpx #2

.19				ror bTabbed
				
				bit bWord
				bpl .1
				
				jsr CS.RUN.PUSHFMT
				>PUSHL.G fWordCount
				>PUSHBI 4
				>SYSCALL PrintF
				bcs .9
				
.1				bit bLine
				bpl .2

				jsr CS.RUN.PUSHFMT
				>PUSHL.G fLineCount
				>PUSHBI 4
				>SYSCALL PrintF
				bcs .9
				
.2				bit bChar
				bpl .3
				
				jsr CS.RUN.PUSHFMT
				>PUSHL.G fCharCount
				>PUSHBI 4
				>SYSCALL PrintF
				bcs .9
				
.3				bit bTabbed
				bpl .4

				>LDYA ZPRelPath
				>SYSCALL PutS
				rts
				
.4				>PUSHW L.MSG.CRLF
				>PUSHBI 0
				>SYSCALL PrintF
.9				
CS.RUN.PrintFstat.RTS
				rts
*--------------------------------------
CS.RUN.PUSHFMT	bit bTabbed
				bmi .1
				
				>PUSHW L.MSG.LONG
				rts
				
.1				>PUSHW L.MSG.LONGTAB
				rts
*--------------------------------------
CS.RUN.PrintTstat
				clc
				bit bTotal
				bpl CS.RUN.PrintFstat.RTS
				
				bit bWord
				bpl .1
				
				>PUSHW L.MSG.LONGTAB
				>PUSHL.G tWordCount
				>PUSHBI 4
				>SYSCALL PrintF
				bcs .9
				
.1				bit bLine
				bpl .2

				>PUSHW L.MSG.LONGTAB
				>PUSHL.G tLineCount
				>PUSHBI 4
				>SYSCALL PrintF
				bcs .9
				
.2				bit bChar
				bpl .3
				
				>PUSHW L.MSG.LONGTAB
				>PUSHL.G tCharCount
				>PUSHBI 4
				>SYSCALL PrintF
				bcs .9

.3				>LDYA L.MSG.TOTAL
				>SYSCALL PutS
.9				rts
*--------------------------------------
CS.DOEVENT		sec
				rts
*--------------------------------------
CS.QUIT			jsr X.LeaveSubDir
				bcc CS.QUIT

				>LDA.G hInclude
				beq .1

				>SYSCALL FreeMem
.1				lda hSrcFullPath
				beq .2

				>SYSCALL FreeMem
.2				lda hMem
				beq .8

				>SYSCALL FreeMem

.8				clc
				rts
*--------------------------------------
CS.RUN.CheckOpt	ldy #1 
				lda (ZPPtr1),y

				ldx #OptionVars-OptionList-1

.2				cmp OptionList,x
				beq .3

				dex
				bpl .2

				sec
				rts

.3				ldy OptionVars,x
				lda #$ff
				sta 0,y
				clc
				rts
*--------------------------------------
				.INB usr/src/shared/x.fileenum.s
*--------------------------------------
CS.END
*--------------------------------------
OptionList		.AS "CRMLWcrmlw"
OptionVars		.DA #bContinue,#bRecurse,#bChar,#bLine,#bWord
				.DA #bContinue,#bRecurse,#bChar,#bLine,#bWord
*--------------------------------------
MSG.USAGE		.CS "Usage : WC File(s) (*,? wildcards allowed)\r\n"
				.CS "   -C : Continue on error\r\n"
				.CS "   -R : Recurse subdirectories\r\n"
				.CS "   -M : Print the CHAR counts\r\n"
				.CS "   -L : Print the LINE counts\r\n"
				.CS "   -W : Print the WORD counts"
MSG.CRLF		.CZ "\r\n"
MSG.ERR			.CZ "[%h]\r\n"
MSG.LONG		.CZ "%L"
MSG.LONGTAB		.CZ "%10L "
MSG.TOTAL		.CZ "(total)"
*--------------------------------------
				.DUMMY
				.OR 0
DS.START
fCharCount		.BS 4
fLineCount		.BS 4
fWordCount		.BS 4

tCharCount		.BS 4
tLineCount		.BS 4
tWordCount		.BS 4

				.INB usr/src/shared/x.fileenum.g

DS.END			.ED
*--------------------------------------
MAN
SAVE usr/src/bin/wc.s
ASM
