NEW
  AUTO 3,1
                                .LIST OFF
                                .OP     65C02
                                .OR     $2000
                                .TF bin/nl
*--------------------------------------
                                .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
ArgIndex                .BS 1
ArgPattern              .BS 1
ZPPtr1                  .BS 2
ZPBufPtr                .BS 2
hFile                   .BS 1
hBuf                    .BS 1
char                    .BS 1
LineCount               .BS 2
bPause                  .BS 1
bPipe                   .BS 1
bIsTTY                  .BS 1
bNewLine                .BS 1
bDummy                  .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 Segment Size
                        .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.LINENUM           .DA MSG.LINENUM
                        .DA 0
*--------------------------------------
CS.INIT                 clc
                        rts
*--------------------------------------
CS.RUN                  stz bPipe
                        stz bIsTTY
                        stz bNewLine
                        inc bNewLine
                        jsr CS.RUN.ISATTY
                        jsr CS.RUN.CheckArgs
                        bcs CS.RUN.LOOP.RTS

                        stz LineCount
                        stz LineCount+1
                        inc LineCount

CS.RUN.LOOP             lda bPipe                If reading from pipe
                        bne .2                   No ^C/^S handling

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

                        tay
                        bne .1                   No char

                        >SYSCALL GetChar
                        bcs .9                   I/O error
                        cmp #$03                 Ctrl-C
                        beq .9
                        cmp #$13                 Ctrl-S
                        bne .1
                        
                        lda bPause
                        eor #$ff
                        sta bPause
                        bne CS.RUN.LOOP                        

.1                      lda bPause
                        bne CS.RUN.LOOP

.2                      >SLEEP

                        lda hFile
                        >SYSCALL GetC
                        bcs .7

                        jsr CS.RUN.PRINTBYTE
                        bra CS.RUN.LOOP

.7                      cmp #MLI.E.EOF
                        bne .9

.8                      lda #0                     Exit with no Error

.9                      sec
CS.RUN.LOOP.RTS         rts
*--------------------------------------
CS.RUN.CheckArgs        jsr CS.RUN.NextArg
                        bcs .4

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

.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

                        ldx #OptionList.Cnt-1

.2                      cmp OptionList,x
                        beq .3

                        dex
                        bpl .2

                        bra .97

.3                      txa
                        lsr
                        beq .98

                        tax
                        lda #$80
                        sta bDummy-1,x 
                        bra CS.RUN.CheckArgs
                                
.4                      lda hFile
                        bne .80

                        ldy #S.PS.hStdIn
                        lda (pPS),y
                        tax
                                
                        lda hFDs-1,x
                        >SYSCALL GetMemPtr
                        >STYA ZPPtr1
                        lda (ZPPtr1)
                        cmp #S.FD.T.PIPE
                        bne .97
                                
                        ldy #S.PS.hStdIn
                        lda (pPS),y
                        sta hFile
                        inc bPipe                        

.80                     >LDYAI 256
                        >SYSCALL GetMem
                        bcs .9

                        >STYA ZPBufPtr
                        stx hBuf

*                       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.ISATTY           ldy #S.PS.hStdOut
                        lda (pPS),y
                        tax
                        lda hFDs-1,x
                        >SYSCALL GetMemPtr
                        >STYA ZPPtr1
                        lda (ZPPtr1)
                        beq .9
                        inc bIsTTY
.9                      rts
*--------------------------------------
CS.QUIT                 lda hFile
                        beq .1
                        >SYSCALL FClose
.1                      lda hBuf
                        beq .8
                        >SYSCALL FreeMem
.8                      clc
                        rts
*--------------------------------------
CS.RUN.PRINTBYTE        pha
                        lda bNewLine
                        beq .2
                        stz bNewLine
                        >PUSHW L.MSG.LINENUM
                        >PUSHW LineCount
                        >PUSHBI 2
                        >SYSCALL PrintF
.2                      pla
                        pha
                        >SYSCALL PutChar
                        pla
                        cmp #C.CR
                        bne .9
                        lda bIsTTY
                        beq .4
                        lda #C.LF
                        >SYSCALL PutChar
.4                      inc bNewLine
                        inc LineCount
                        bne .9
                        inc LineCount+1
.9                      rts
*--------------------------------------
CS.DOEVENT              sec
                        rts
*--------------------------------------
CS.END
*--------------------------------------
OptionList              .AS "x"
OptionList.Cnt          .EQ *-OptionList
*--------------------------------------
MSG.USAGE               .AS "Usage : NL <File> or CMD|NL"
MSG.CRLF                .AZ "\r\n"
MSG.LINENUM             .AZ "%5D "
*--------------------------------------
                                .DUMMY
                                .OR 0
DS.START
DS.END                  .ED
*--------------------------------------
MAN
SAVE usr/src/bin/nl.s
ASM
