NEW
  AUTO 3,1
				.LIST OFF
				.OP 65C02
				.OR $2000
				.TF bin/ls
*--------------------------------------
				.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 1
X.COPY.TO.DEST  .EQ 0
X.DELETE.SOURCE .EQ 0
X.RESET.SRC.DIR	.EQ 1
*--------------------------------------
MAX.COLW		.EQ 20
MAX.COLC		.EQ 4
COLOR.ENTER		.EQ 32
COLOR.REG		.EQ 93
COLOR.DIR		.EQ 92
COLOR.BDEV		.EQ 96
*--------------------------------------
				.DUMMY
				.OR ZPBIN
ZS.START
ZPPtr1			.BS 2
ZPPtr2			.BS 2
ZPFileName		.BS 2
ZPFileStat		.BS 2

ZPPWBuf			.BS 2
ArgCount		.BS 1
ColCount		.BS 1
hLineBuf        .BS 1
hPWBuf          .BS 1

bPass2			.BS 1
bPause			.BS 1
bAllmostAll		.BS 1
bColumn			.BS 1
bFullPath		.BS 1
bLong			.BS 1
bRecurse		.BS 1
bToFile			.BS 1

ZPLineBuf		.BS 2
ZPLineBufPtr	.BS 2

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 #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.ENTER		.DA MSG.ENTER
L.MSG.REGEXT	.DA MSG.REGEXT
L.MSG.DIREXT	.DA MSG.DIREXT
L.MSG.BDEVEXT	.DA MSG.BDEVEXT
L.MSG.SETCOLOR	.DA MSG.SETCOLOR
L.PRODOS.FT.TXT	.DA PRODOS.FT.TXT
L.MSG.NODATE	.DA MSG.NODATE
L.FMT.Date		.DA FMT.Date
L.FMT.Time		.DA FMT.Time
L.FMT.int16		.DA FMT.int16
J.CS.RUN.PRINT	.DA CS.RUN.PRINT.REG
				.DA CS.RUN.PRINT.DIR
				.DA CS.RUN.PRINT.CDEV
				.DA CS.RUN.PRINT.BDEV
				.DA CS.RUN.PRINT.LNK
				.DA CS.RUN.PRINT.DSOCK
				.DA CS.RUN.PRINT.SSOCK
				.DA CS.RUN.PRINT.PIPE
				.DA 0
*--------------------------------------
CS.INIT			clc
				rts
*--------------------------------------
CS.RUN			ldy #S.PS.hStdOut
				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.REG
				beq .10

				cmp #S.FD.T.PIPE
				bne .1

.10				dec bToFile

.1				inc ArgCount

				lda ArgCount
				>SYSCALL ArgV
				bcs .6

				>STYA ZPPtr1
				lda (ZPPtr1)
				cmp #'-'
				bne .4

				ldy #1
				lda (ZPPtr1),y
				beq .99

.20				ldx #OptionVars-OptionList-1

.2				cmp OptionList,x
				beq .3

				dex
				bpl .2

.99				>PUSHW L.MSG.USAGE
				>PUSHBI 0
				>SYSCALL PrintF

				lda #E.SYN
				sec
				rts

.3				lda OptionVars,x
				tax
*				sec
				ror $0,x
				
				iny
				lda (ZPPtr1),y
				bne .20
				
				bra .1

.4				>LDYA ZPPtr1
				jsr X.InitSrcDirYA
				bcc .1					scan for any other args

.9				rts

.6				>LDA.G index            do we have a Source dir ?
				bne .8

				ldy #S.PS.hCWD
				lda (pPS),y
				>SYSCALL GetMemPtr
				jsr X.InitSrcDirYA
				bcs .9

.8				>PUSHEA.G TIME.SysTime
				>SYSCALL Time

				>LDYAI S.PW
				>SYSCALL GetMem
				bcs .9

				>STYA ZPPWBuf
				stx hPWBuf

				>LDYAI 256
				>SYSCALL GetMem
				bcs .9

				>STYA ZPLineBuf
				>STYA ZPLineBufPtr
				stx hLineBuf
*--------------------------------------
CS.RUN.LOOP		stz bPass2

.1				>SLEEP

				ldy #S.PS.hStdIn
				lda (pPS),y
				>SYSCALL FEOF
				bcs .99					I/O error

				tay
				bne .2					no char

				>SYSCALL GetChar
				cmp #$03				Ctrl-C
				beq .99					Abort....

				cmp #$13				Ctrl-S
				bne .2

				lda bPause
				eor #$ff
				sta bPause
				bne .1

.2				bit bPause
				bmi .1

				jsr X.GetEntry
				bcs .6

				jsr X.IncludeMatch
				bcs .5					no match, skip....

				bit bAllmostAll
				bmi .4

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

.4				ldy #S.STAT.MODE+1
				lda (ZPFileStat),y

				and #$70
				lsr
				lsr
				lsr
				tax
				jsr CS.RUN.PRINT.JMP
				bcs .99

.5				jsr X.GetNextEntry
				bcc .1

				bit bPass2
				bmi .6

				jsr CS.RUN.CheckRoot
				beq .6

				jsr X.ResetSrcDir
				bcs .99

				dec bPass2
				bra .1

.6				bit bLong
				bmi .7

				jsr CS.RUN.LineBufFlush
				bcs .99

.7				jsr X.LeaveSubDir
				bcs .8

				jsr X.BasePath..

				jsr CS.RUN.ENTER.MSG
				bcs .99

				jsr X.GetNextEntry
				jmp CS.RUN.LOOP

.8				lda #0
				sec
.99				rts
*--------------------------------------
CS.RUN.CheckRoot
				>LDA.G hSrcBasePath
				>SYSCALL GetMemPtr
				>STYA ZPPtr1
				ldy #1
				lda (ZPPtr1),y
				rts
*--------------------------------------
CS.RUN.PRINT.JMP
				jmp (J.CS.RUN.PRINT,x)
*--------------------------------------
CS.RUN.PRINT.REG
				bit bPass2
				bpl .8

				bit bLong
				bmi CS.RUN.PRINT.REGEXT

				ldy #COLOR.REG
				jsr CS.RUN.SetColorY

				bit bColumn
				bmi CS.RUN.PRINT.REG1

				bit bFullPath
				bpl .2

				jsr CS.RUN.SrcPathOut

				bra CS.RUN.PRINT.REG1

.2				ldx #MAX.COLW
				clc
				jsr CS.RUN.FN2LineBuf
				jmp CS.RUN.IncColCnt

.8				clc
.9				rts
*--------------------------------------
CS.RUN.PRINT.REGEXT
				jsr CS.RUN.PrintMod
				jsr CS.RUN.PrintUidGid

				>PUSHW ZPLineBufPtr
				>PUSHW L.MSG.REGEXT

				ldy #S.STAT.SIZE+3
				ldx #4

.2				lda (ZPFileStat),y
				>PUSHA
				dey
				dex
				bne .2

				>PUSHBI 4
				jsr CS.RUN.ToLineBuf

				jsr CS.RUN.PrintTypeAux
				jsr CS.RUN.PrintDates

				ldy #COLOR.REG
				jsr CS.RUN.SetColorY

CS.RUN.PRINT.REG1
				ldx #0
				clc
				jsr CS.RUN.FN2LineBuf
				jmp CS.RUN.LineBufOut
*--------------------------------------
CS.RUN.PRINT.DIR
				bit bPass2
				bmi .8

				bit bLong
				bmi CS.RUN.PRINT.DIREXT

				ldy #COLOR.DIR
				jsr CS.RUN.SetColorY

				bit bColumn
				bmi CS.RUN.PRINT.DIR1

				bit bFullPath
				bpl .2

				jsr CS.RUN.SrcPathOut
				bra CS.RUN.PRINT.DIR1

.2				ldx #MAX.COLW-1
				sec
				jsr CS.RUN.FN2LineBuf
				jsr CS.RUN.IncColCnt
				bcs .9

				jmp CS.RUN.PRINT.DIREND

.8				clc
.9				rts
*--------------------------------------
CS.RUN.PRINT.DIREXT
				jsr CS.RUN.PrintMod
				jsr CS.RUN.PrintUidGid

				>LDYA L.MSG.DIREXT
				jsr CS.RUN.LineBufYAOut

				jsr CS.RUN.PrintDates

				ldy #COLOR.DIR
				jsr CS.RUN.SetColorY

CS.RUN.PRINT.DIR1
				ldx #0
				sec
				jsr CS.RUN.FN2LineBuf
				jsr CS.RUN.LineBufOut
				bcs .9

				jmp CS.RUN.PRINT.DIREND

.9				rts
*--------------------------------------
CS.RUN.PRINT.CDEV
				lda #MLI.E.UNSUPST
				sec
				rts
*--------------------------------------
CS.RUN.PRINT.BDEV
				clc
				bit bPass2
				bmi .9

				bit bLong
				bmi CS.RUN.PRINT.BDEVEXT

				ldy #COLOR.BDEV
				jsr CS.RUN.SetColorY

				bit bColumn
				bmi CS.RUN.PRINT.BDEV1

				bit bFullPath
				bpl .2

				jsr CS.RUN.SrcPathOut
				bra CS.RUN.PRINT.BDEV2

.2				lda #'/'
				jsr CS.RUN.LineBufCOut

				ldx #MAX.COLW-1
				sec
				jsr CS.RUN.FN2LineBuf
				jsr CS.RUN.IncColCnt
				bcc CS.RUN.PRINT.BDEVEND

.9				rts
*--------------------------------------
CS.RUN.PRINT.BDEVEXT
				jsr CS.RUN.PrintMod
				jsr CS.RUN.PrintUidGid

				>PUSHW ZPLineBufPtr
				>PUSHW L.MSG.BDEVEXT

				ldy #S.STAT.BLOCKS+1
				>PUSHB (ZPFileStat),y
				dey
				>PUSHB (ZPFileStat),y

				ldy #S.STAT.P.DEVBLOCKS+1
				>PUSHB (ZPFileStat),y
				dey
				>PUSHB (ZPFileStat),y

				ldy #S.STAT.P.SLOT
				>PUSHB (ZPFileStat),y
				iny						DRIVE
				>PUSHB (ZPFileStat),y

				>PUSHBI 6
				jsr CS.RUN.ToLineBuf

				jsr CS.RUN.PrintDates

				ldy #COLOR.BDEV
				jsr CS.RUN.SetColorY

CS.RUN.PRINT.BDEV1
				lda #'/'
				jsr CS.RUN.LineBufCOut

CS.RUN.PRINT.BDEV2
				ldx #0
				sec
				jsr CS.RUN.FN2LineBuf
				jsr CS.RUN.LineBufOut
				bcc CS.RUN.PRINT.BDEVEND

				rts
*--------------------------------------
CS.RUN.PRINT.LNK
CS.RUN.PRINT.DSOCK
CS.RUN.PRINT.SSOCK
CS.RUN.PRINT.PIPE
				lda #MLI.E.UNSUPST
				sec
				rts
*--------------------------------------
CS.RUN.PRINT.DIREND
CS.RUN.PRINT.BDEVEND
				bit bRecurse
				bpl .8

				lda (ZPFileName)
				cmp #'.'
				bne .7

				ldy #1
				lda (ZPFileName),y
				beq .8

				cmp #'.'
				bne .7

				iny
				lda (ZPFileName),y
				beq .8

				bit bLong
				bmi .7

				jsr CS.RUN.LineBufFlush
				bcs .9

.7				>LDYA ZPFileName
				jsr X.EnterSubDirYA
				bcs .9

				jmp CS.RUN.ENTER.MSG

.8				clc
.9				rts
*--------------------------------------
CS.RUN.ENTER.MSG
				ldy #COLOR.ENTER
				jsr CS.RUN.SetColorY

				>LDYA L.MSG.ENTER
				jsr CS.RUN.LineBufYAOut

				ldy #hSrcBasePath
				lda (pData),y
				>SYSCALL GetMemPtr
				jsr CS.RUN.LineBufYAOut

				ldy #0
				jsr CS.RUN.SetColorY

				jmp CS.RUN.LineBufOut
*--------------------------------------
CS.RUN.PrintMod	ldy #S.STAT.MODE+1
				lda (ZPFileStat),y
				lsr
				php						C = RU

				lsr
				lsr
				lsr
				tax
				lda TYPES,x
				jsr CS.RUN.LineBufCOut

				ldy #S.STAT.MODE
				lda (ZPFileStat),y

				ldx #8

				plp						C = RU

.1				pha
				lda #'-'
				bcc .2

				lda ACCESS,x

.2				jsr CS.RUN.LineBufCOut

				pla
				asl

				dex
				bpl .1

				rts
*--------------------------------------
CS.RUN.PrintUidGid
				ldy #S.STAT.UID
				lda (ZPFileStat),y
				>PUSHA
				>PUSHW ZPPWBuf
				>SYSCALL GetPWUID
				bcs .2

				jsr .8
				bra .3

.2				ldy #S.STAT.UID+1
				jsr .7
*--------------------------------------
.3				ldy #S.STAT.GID
				lda (ZPFileStat),y
				>PUSHA
				>PUSHW ZPPWBuf
				>SYSCALL GetGRGID
				bcc .8

				ldy #S.STAT.GID+1

.7				>PUSHW ZPLineBufPtr
				>PUSHW L.FMT.int16

				lda (ZPFileStat),y
				>PUSHA
				dey
				lda (ZPFileStat),y
				>PUSHA

				>PUSHBI 2
				jmp CS.RUN.ToLineBuf

.8				>LDYA ZPPWBuf
				ldx #5
				jmp CS.RUN.LineBufYAOutX
*--------------------------------------
CS.RUN.PrintDates
				ldy #S.STAT.MTIME+S.TIME.MONTH
				lda (ZPFileStat),y
				bne .1

				jsr .8
				bra .2

.1				lda #S.STAT.MTIME
				jsr .5

.2				ldy #S.STAT.CTIME+S.TIME.MONTH
				lda (ZPFileStat),y
				beq .8

				lda #S.STAT.CTIME

.5				clc
				adc ZPFileStat
				sta ZPPtr1

				lda #0
				adc ZPFileStat+1
				sta ZPPtr1+1
				
				>PUSHW ZPLineBufPtr

				ldx #0

				ldy #S.TIME.DAY

.6				lda (ZPPtr1),y
				cmp (pData),y			TIME.SysTime
				bne .7

				dey						S.TIME.MONTH,S.TIME.YEAR,S.TIME.CENTURY
				bpl .6

				inx
				inx						Select L.FMT.Time

.7				>PUSHW L.FMT.Date,x

				>PUSHW ZPPtr1
				>SYSCALL StrFTime

				lda #10
				jmp CS.RUN.LineBufPtrAddA

.8				>LDYA L.MSG.NODATE
				jmp CS.RUN.LineBufYAOut
*--------------------------------------
CS.RUN.PrintTypeAux
				ldy #S.STAT.P.TYPE
				lda (ZPFileStat),y

				ldy #PRODOS.FT.TXT-PRODOS.FT.HEX-1

.1				cmp PRODOS.FT.HEX,y
				beq .2

				dey
				bpl .1

				pha
				lda #'$'
				jsr CS.RUN.LineBufCOut
				pla
				jsr CS.RUN.LineBufHOut
				bra .3

.2				tya
				asl
				asl				        CC
				adc L.PRODOS.FT.TXT
				tay

				lda L.PRODOS.FT.TXT+1
				adc #0
				jsr CS.RUN.LineBufYAOut

.3				lda #C.SPACE
				jsr CS.RUN.LineBufCOut

				ldy #S.STAT.P.AUXTYPE+1
				lda (ZPFileStat),y
				tax
				dey
				ora (ZPFileStat),y
				beq .4

				txa
				jsr CS.RUN.LineBufHOut

				lda (ZPFileStat),y
				jsr CS.RUN.LineBufHOut

				ldx #0
				bra .5

.4				ldx #4

.5				lda #C.SPACE

.6				jsr CS.RUN.LineBufCOut
				dex
				bpl .6

				rts
*--------------------------------------
CS.RUN.FN2LineBuf
				phx						X = MaxLen

				php

				>LDYA ZPFileName
				jsr CS.RUN.LineBufYAOutX	Y = output count

				plp
				bcc .1

				lda #'/'
				jsr CS.RUN.LineBufCOut

.1				pla						MaxLen was 0, no extra SPACE needed
				beq .3

				txa
				beq .3					all chars printed, no more SPACE

				lda #C.SPACE

.2				jsr CS.RUN.LineBufCOut
				dex
				bne .2

.3				ldy #0					Reset Color
*--------------------------------------
CS.RUN.SetColorY
				bit bToFile
				bmi CS.RUN.IncColCnt.8

				>PUSHW ZPLineBufPtr
				>PUSHW L.MSG.SETCOLOR
				tya
				>PUSHA
				>PUSHBI 1
*--------------------------------------
CS.RUN.ToLineBuf
				>SYSCALL SPrintF
*--------------------------------------
CS.RUN.LineBufPtrAddY
				tya
CS.RUN.LineBufPtrAddA
				clc
				adc ZPLineBufPtr
				sta ZPLineBufPtr
				bcc .8

				inc ZPLineBufPtr+1

.8				rts
*--------------------------------------
CS.RUN.IncColCnt
				inc ColCount
				lda ColCount
				cmp #MAX.COLC
				beq CS.RUN.LineBufFlush1

CS.RUN.IncColCnt.8
				clc
				rts
*--------------------------------------
CS.RUN.LineBufFlush
				lda ColCount
				beq CS.RUN.IncColCnt.8

CS.RUN.LineBufFlush1
				stz ColCount
*--------------------------------------
CS.RUN.LineBufOut
				lda #C.CR
				jsr CS.RUN.LineBufCOut
				bit bToFile
				bmi .1

				lda #C.LF
				jsr CS.RUN.LineBufCOut

.1				lda #0
				sta (ZPLineBufPtr)

				ldy #S.PS.hStdOut
				lda (pPS),y
				>PUSHA

				>LDYA ZPLineBuf
				>STYA ZPLineBufPtr

				>PUSHYA

				>SYSCALL FPutS
				rts
*--------------------------------------
CS.RUN.SrcPathOut
				ldy #hSrcBasePath
				lda (pData),y
				>SYSCALL GetMemPtr
*--------------------------------------
CS.RUN.LineBufYAOut
				ldx #0
*--------------------------------------
CS.RUN.LineBufYAOutX
				>STYA ZPPtr1			X = MaxLen

				ldy #$ff

.1				iny
				lda (ZPPtr1),y
				beq .8

				jsr CS.RUN.LineBufCOut
				dex
				bne .1

.8				clc
				rts
*--------------------------------------
CS.RUN.LineBufHOut
				pha
				lsr
				lsr
				lsr
				lsr
				jsr .1

				pla
				and #$0F

.1				ora #$30
				cmp #'9'+1
				bcc CS.RUN.LineBufCOut

				adc #6
*--------------------------------------
CS.RUN.LineBufCOut
				sta (ZPLineBufPtr)
				inc ZPLineBufPtr
				bne .8

				inc ZPLineBufPtr+1

.8				rts
*--------------------------------------
CS.DOEVENT		sec
				rts
*--------------------------------------
CS.QUIT			jsr X.LeaveSubDir
				bcc CS.QUIT

				>LDA.G hInclude
				jsr .7

				lda hPWBuf
				jsr .7

				lda hLineBuf

.7				beq .8

				>SYSCALL FreeMem

.8				clc
				rts
*--------------------------------------
				.INB usr/src/shared/x.fileenum.s
*--------------------------------------
CS.END
*--------------------------------------
OptionList		.AS "ACFLRacflr"
OptionVars		.DA #bAllmostAll,#bColumn,#bFullPath,#bLong,#bRecurse,#bAllmostAll,#bColumn,#bFullPath,#bLong,#bRecurse
*--------------------------------------
MSG.USAGE       .CS "Usage : LS [-A] [-C] [-F] [-L] [-R] [filespec]\r\n"
                .CS "   -A : Show all files\r\n"
                .CS "   -C : List in one column\r\n"
                .CS "   -F : Show full paths\r\n"
				.CS "   -I <*,? wildcards allowed> : Ignore matching files\r\n"
                .CS "   -L : Use long listing format\r\n"
                .CZ "   -R : Recursively list subdirectories\r\n"
*--------------------------------------
MSG.ENTER       .CZ "Directory:"
*--------------------------------------
MSG.REGEXT      .CZ "  %10u "
MSG.DIREXT      .CZ "              <dir>   "
MSG.BDEVEXT     .CZ " %5D/%5D     s%dd%d "
*--------------------------------------
MSG.SETCOLOR	.CZ "\e[1;%dm"
PRODOS.FT.HEX   .HS 0406FAFCFDCBCCCFFF
PRODOS.FT.TXT   .AZ "txt"
				.AZ "bin"
				.AZ "asm"
				.AZ "bas"
				.AZ "var"
				.AZ "pix"
				.AZ "fon"
				.AZ "pak"
				.AZ "sys"
MSG.NODATE      .AZ "<no-date> "
FMT.Date        .AZ "%d-%b-%y "
FMT.Time        .AZ " %H:%M:%S "
FMT.int16       .AZ "%6D"
TYPES           .AS "-dcblssp"
ACCESS          .AS "xwrxwrxwr"
*--------------------------------------
				.DUMMY
				.OR 0
DS.START
TIME.SysTime    .BS S.TIME

				.INB usr/src/shared/x.fileenum.g
DS.END          .ED
*--------------------------------------
MAN
SAVE usr/src/bin/ls.s
ASM
