NEW
  AUTO 3,1
				.LIST OFF
				.OP	65C02
				.OR	$2000
				.TF bin/cut
*--------------------------------------
				.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
ZPBuf			.BS 2
ZPBufPtr		.BS 2
ArgIndex		.BS 1
ArgLine			.BS 1
OptionIdx		.BS 1
hBuf			.BS 1
FieldNum		.BS 1
StartChar		.BS 1
EndChar			.BS 1
Separator		.BS 1
hToClose		.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 #32					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
				.DA 0
*--------------------------------------
CS.INIT			clc
				rts
*--------------------------------------
CS.RUN			lda #C.SPACE
				sta Separator

.10				jsr CS.RUN.NextArg
				bcs .8

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

				lda ArgIndex
				sta ArgLine
				bra .10

.1				ldy #1
				lda (ZPPtr1),y

				ldx #OptionList.Cnt-1

.2				cmp OptionList,x
				beq .3

				dex
				bpl .2

.97				lda #E.SYN

.98				pha
				>LDYA L.MSG.USAGE
				>SYSCALL PutS
				pla
				sec
.9				rts

.3				txa
				lsr
				beq .98					-H

				cmp #4					-S
				bne .4

				jsr CS.RUN.NextArg
				bcs .97

				lda (ZPPtr1)
				sta Separator
				bra .10

.4				sta OptionIdx
				jsr CS.RUN.NextArg
				bcs .97

				>SYSCALL AToI
				bcs .97

				tax
				bne .97

				tya
				ldx OptionIdx
				sta FieldNum-1,x
				bra .10					scan for any other args

.8				>LDYAI 256
				>SYSCALL GetMem
				bcs .9

				>STYA ZPBuf
				>STYA ZPBufPtr
				stx hBuf

				lda ArgLine
				beq .80

				>PUSHW ZPBufPtr
				lda ArgLine
				>SYSCALL ArgV
				>PUSHYA
				>SYSCALL StrCpy

				jsr CS.RUN.OUT
				bcs .9

				lda #0					Exit with no Error
				sec
				rts

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

				lda (ZPPtr1)
				cmp #S.FD.T.PIPE
				bne CS.RUN.LOOP

				ldy #S.PS.hStdIn
				lda (pPS),y
				sta hToClose
*--------------------------------------
CS.RUN.LOOP		>SLEEP

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

				>LDYA ZPBuf
				>STYA ZPBufPtr
				>PUSHYA

				>PUSHWI	255
				>SYSCALL FGetS
				bcs	.8

				lda (ZPBufPtr)
				beq CS.RUN.LOOP

				jsr CS.RUN.OUT
				bcc CS.RUN.LOOP

.8				cmp #MLI.E.EOF
				bne .90

				lda #0
.90				sec
.9				rts
*--------------------------------------
CS.RUN.OUT		ldx FieldNum
				beq CS.RUN.OUT.MN

				ldy #$ff

.1				iny						skip spaces
				lda (ZPBufPtr),y
				beq CS.RUN.PRINTCR

				cmp Separator
				beq .1

				dex
				beq .4

.2				iny						skip token
				lda (ZPBufPtr),y
				beq CS.RUN.PRINTCR

				eor Separator
				bne .2
				bra .1

.4				jsr CS.RUN.AddYToBufPtr

				ldy #$ff

.6				iny
				lda (ZPBufPtr),y
				beq .8

				eor Separator
				bne .6

				sta (ZPBufPtr),y

.8				>LDYA ZPBufPtr
				>SYSCALL PutS

				rts
*--------------------------------------
CS.RUN.PRINTCR	>PUSHW L.MSG.CRLF
				>PUSHBI 0
				>SYSCALL PrintF

				rts
*--------------------------------------
CS.RUN.OUT.MN	ldy #0

				ldx StartChar
				beq .2

.1				dex
				beq .2

				lda (ZPBufPtr),y
				beq CS.RUN.PRINTCR

				iny
				bra .1

.2				lda EndChar
				beq .8

.3				lda (ZPBufPtr),y
				beq .8

				cpy EndChar
				beq .4

				iny
				bra .3

.4				lda #0
				sta (ZPBufPtr),y

.8				ldy StartChar
				beq .80
				dey
				beq .80
				jsr CS.RUN.AddYToBufPtr

.80				>LDYA ZPBufPtr
				>SYSCALL PutS

				rts
*--------------------------------------
CS.RUN.NextArg	inc ArgIndex
				lda ArgIndex
				>SYSCALL ArgV
				bcs .9

				>STYA ZPPtr1

.9				rts
*--------------------------------------
CS.RUN.AddYToBufPtr
				tya
				clc
				adc	ZPBufPtr
				sta ZPBufPtr
				bcc .8

				inc ZPBufPtr+1
.8				rts
*--------------------------------------
CS.QUIT			lda hToClose
				beq .1

				>SYSCALL FClose			Close the pipe

.1				lda hBuf
				beq .8

				>SYSCALL FreeMem

.8				clc
				rts
*--------------------------------------
CS.DOEVENT		sec
				rts
*--------------------------------------
CS.END
*--------------------------------------
OptionList		.AS "HhFfMmNnSs"
OptionList.Cnt	.EQ *-OptionList
*--------------------------------------
MSG.USAGE		.CS 'Usage : CUT "line of text"\r\n'
				.CS "   -H : This help screen\r\n"
				.CS "   -F nn : Output field nn\r\n"
				.CS "   -M nn : Output starting at nn\r\n"
				.CS "   -N nn : Output Ending at nn\r\n"
				.CS "   -S ch : Change default SPACE separator to 'ch'"
MSG.CRLF		.CZ "\r\n"
*--------------------------------------
				.DUMMY
				.OR 0
DS.START
DS.END			.ED
*--------------------------------------
MAN
SAVE usr/src/bin/cut.s
ASM
