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
ZPBufPtr		.BS 2
ArgCount		.BS 1
ArgIndex		.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 ArgCount
				sta ArgIndex
				bra .10

.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					-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		

.97				lda #E.SYN
.98				pha
				>PUSHW L.MSG.USAGE
				>PUSHBI 0
				>SYSCALL PrintF
				pla
				sec
.9				rts

.8				>LDYAI 256
				>SYSCALL GetMem
				bcs .9

				>STYA ZPBufPtr
				stx hBuf
				lda ArgIndex
				beq .80

				>PUSHW ZPBufPtr
				>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
				tax
				lsr
				bcs .97
				
				lda Nod.Table.hFD-2,x
				>SYSCALL GetMemPtr
				>STYA ZPPtr1

				lda (ZPPtr1)
				cmp #S.FD.T.PIPE
				bne .97
				
				ldy #S.PS.hStdIn
				lda (pPS),y
				sta hToClose
*--------------------------------------
CS.RUN.LOOP		>SLEEP
				
				ldy #S.PS.hStdIn
				lda (pPS),y
				>PUSHA
				>PUSHW ZPBufPtr
				>PUSHWI	256
				>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 ArgCount
				lda ArgCount
				>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		.AS "Usage : CUT "line of text"\r\n"
				.AS "   -H : This help screen\r\n"
				.AS "-F nn : Output field nn\r\n"
				.AS "-M nn : Output starting at nn\r\n"
				.AS "-N nn : Output Ending at nn\r\n"
				.AS "-S ch : Change default SPACE separator to 'ch'"
MSG.CRLF		.AZ "\r\n"
*--------------------------------------
				.DUMMY
				.OR 0
DS.START
DS.END			.ED
*--------------------------------------
MAN
SAVE usr/src/bin/cut.s
ASM
