NEW
PREFIX
AUTO 4,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
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 #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
				.DA 0
*--------------------------------------
CS.INIT			clc
				rts
*--------------------------------------
CS.RUN			stz ArgCount
				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
				
.2				cmp OptionList,x
				beq .3
				dex
				bne .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				
				>PUSHBI 0
				>LDYA L.MSG.USAGE
				>SYSCALL printf
				pla
				sec
.9				rts

.8				>LDYAI 256
				>SYSCALL GetMem
				bcs .9
				
				>STYA ZPBufPtr
				stx hBuf
				
				lda ArgIndex
				beq .80
				
				>SYSCALL ArgV
				>PUSHYA
				>LDYA ZPBufPtr
				>SYSCALL strcpy
				
				bra CS.RUN.OUT
				
.80				ldy #S.PS.hStdIn
				lda (pPS),y
				tax
				
				lda OF.Table.hFD-1,x
				>SYSCALL GetMemPtr
				>STYA ZPPtr1
				lda (ZPPtr1)
				cmp #S.FD.T.PIPE
				bne .97
				
				>PUSHWI	256
				>PUSHW ZPBufPtr
				
				ldy #S.PS.hStdIn
				lda (pPS),y

				>SYSCALL fgets
				bcc CS.RUN.OUT
				cmp #MLI.E.EOF
				bne .9
				
CS.RUN.OUT		lda FieldNum
				beq CS.RUN.OUT.MN
				
				ldy #$ff
				
.1				iny						skip spaces
				lda (ZPBufPtr),y
				beq CS.RUN.PRINTCR
				
				cmp Separator
				beq .1
				
				dec FieldNum
				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
				
				lda #0					Exit with no Error
				sec
				rts
*--------------------------------------
CS.RUN.PRINTCR	>PUSHBI 0
				>LDYA L.MSG.CRLF
				>SYSCALL printf
				bcs .9
				
				lda #0
				sec
.9				rts
*--------------------------------------
CS.RUN.OUT.MN	ldy #$ff

				ldx StartChar
				beq .2
				
.1				iny
				lda (ZPBufPtr),y
				beq CS.RUN.PRINTCR
				dex
				bne .1
				
.2				ldx EndChar
				beq .8
				
.3				iny
				lda (ZPBufPtr),y
				beq .8

				cpy EndChar
				bne .3
				
				lda #0
				sta (ZPBufPtr),y			
				
.8				ldy StartChar
				jsr CS.RUN.AddYToBufPtr

				>LDYA ZPBufPtr
				>SYSCALL puts
				
				lda #0					Exit with no Error
				sec
				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
				lda ZPBufPtr+1
				
.8				rts
*--------------------------------------
CS.QUIT			lda hBuf
				beq .8

				>SYSCALL FreeMem

.8				clc
				rts
*--------------------------------------
CS.DOEVENT		sec
				rts
*--------------------------------------
CS.END
*--------------------------------------
OptionList		>PSTR "HhFfMmNnSs"
*--------------------------------------
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
