NEW
  AUTO 3,1
				.LIST OFF	
*--------------------------------------
IO.Reset		jsr IO.Reset.In
				jsr IO.Reset.Out
				jsr IO.Reset.Err
IO.Reset.9		rts
*--------------------------------------
IO.Load			>PUSHYA
				>PUSHBI	O.RDONLY
				>PUSHBI S.FI.T.TXT
				>PUSHWZ					Aux type
				>SYSCALL LoadTxtFile
				rts
*--------------------------------------
IO.Pipe.In		jsr IO.Pipe.OpenR		Open last created PIPE
				bcs IO.Set.RTS

				pha

				ldy #S.PS.hStdIn
				lda (pPS),y
				>STA.G IO.hIn

				pla

				ldy #S.PS.hStdIn
				sta (pPS),y
				rts
*--------------------------------------
IO.Set.In		pha
				jsr IO.Reset.In
				ldy #S.PS.hStdIn
				lda (pPS),y
				>STA.G IO.hIn
				pla
				ldy #S.PS.hStdIn
				sta (pPS),y
IO.Set.RTS		rts
*--------------------------------------			
IO.Pop.In		clc
				.HS B0					BCS
*--------------------------------------
IO.Reset.In		sec
				>LDA.G IO.hIn
				beq .8
				bcc .1
				
				ldy #S.PS.hStdIn
				jsr IO.Close.Y
				
.1				ldy #S.PS.hStdIn
				sta (pPS),y
				>STZ.G IO.hIn

.8				clc
IO.Reset.In.RTS	rts
*--------------------------------------
IO.Pipe.Out		>PUSHWZ					mode
				>LEA.G IO.FIFO
				>SYSCALL MKFIFO
				bcs IO.Reset.In.RTS		Create a new PIPE

				jsr IO.Pipe.OpenW		Open for OUTPUT
		
				bcs IO.Reset.In.RTS
				
				pha
				ldy #S.PS.hStdOut
				lda (pPS),y
				>STA.G IO.hOut
				pla
				ldy #S.PS.hStdOut
				sta (pPS),y

				rts
*--------------------------------------
IO.Set.Out		pha
				jsr IO.Reset.Out
				ldy #S.PS.hStdOut
				lda (pPS),y
				>STA.G IO.hOut
				pla
				ldy #S.PS.hStdOut
				sta (pPS),y
				clc
				rts
*--------------------------------------			
IO.Pop.Out		clc
				.HS B0					BCS
*--------------------------------------			
IO.Reset.Out	sec
IO.Reset.OutC	>LDA.G IO.hOut
				beq .8
				
				bcc .1
				
				ldy #S.PS.hStdOut
				jsr IO.Close.Y
				
.1				ldy #S.PS.hStdOut
				sta (pPS),y
				>STZ.G IO.hOut
.8				rts
*--------------------------------------
IO.Set.Err		pha
				jsr IO.Reset.Err
				ldy #S.PS.hStdErr
				lda (pPS),y
				>STA.G IO.hErr
				pla
				ldy #S.PS.hStdErr
				sta (pPS),y
				rts
*--------------------------------------			
IO.Reset.Err	>LDA.G IO.hErr
				beq .8

				ldy #S.PS.hStdErr
				jsr IO.Close.Y
				
				ldy #S.PS.hStdErr
				sta (pPS),y
				>STZ.G IO.hErr
.8				rts
*--------------------------------------
IO.Close.Y		pha
				lda (pPS),y
				>SYSCALL fclose
				pla
				rts
*--------------------------------------
IO.Pipe.OpenR	ldx #O.RDONLY+O.TEXT
				.HS 2C					BIT ABS

IO.Pipe.OpenW	ldx #O.WRONLY+O.TEXT
				>LEA.G IO.FIFO
*--------------------------------------
IO.FOpenYAX		>PUSHYA
				txa
				>PUSHA
				>PUSHBI S.FI.T.TXT
				>PUSHWZ					Aux type
				>SYSCALL fopen
				rts
*--------------------------------------
IO.StatArgVBufPtr
				>LDYA ZPArgVBufPtr
IO.StatYA		>PUSHYA
				>PUSHW ZPCLBuf
				>SYSCALL STAT
				bcs .9
				
				ldy #S.STAT.P.TYPE
				lda (ZPCLBuf),y
				
.9				rts
*--------------------------------------
IO.PrintBatchErrMsg
				>LDYA ZPInputBuf
				>STYA ZPPtr1
				ldy #S.PS.RC
				lda (pPs),y
				cmp #E.SYN
				beq .10

				>LDYA ZPInputCmdPtr
				bra .11

.10				>LDYA ZPInputBufPtr

.11				>STYA ZPPtr2
				stz ZPTmpW				Line Count
				stz ZPTmpW+1

.1				inc ZPTmpW
				bne .2
				inc ZPTmpW+1

.2				ldy #$ff

.3				iny
				lda (ZPPtr1),y
				beq .4					EoF

				cmp #C.CR
				bne .3					Scan until EoL
.4				tya
				clc
				adc ZPPtr1
				tax
				lda ZPPtr1+1
				adc #0
				pha

				cpx ZPPtr2
				sbc ZPPtr2+1
				pla
				bcs .41
				inx
				bne .40
				inc
.40				stx ZPPtr1
				sta ZPPtr1+1
				bra .1

.41				ldy #$ff

.5				iny
				lda (ZPPtr1),y
				sta (ZPCLBuf),y
				beq .6

				eor #C.CR
				bne .5

				sta (ZPCLBuf),y
				
.6				ldy #S.PS.hStdErr
				lda (pPS),y
				>PUSHA
				
				>PUSHW L.MSG.BATCHLINE
				>PUSHW ZPCLBuf

				>PUSHBI 2
				>SYSCALL fprintf

				ldy #$ff
				ldx #'-'

.7				iny
				lda (ZPCLBuf),y
				beq .8

				txa
				sta (ZPCLBuf),y
				bra .7

.8				ldy #S.PS.hStdErr
				lda (pPS),y
				>PUSHA
				>PUSHW L.MSG.BATCHERR
				>PUSHW ZPCLBuf
				>PUSHW ZPTmpW
				>PUSHBI 4
				>SYSCALL fprintf
				rts
*--------------------------------------
IO.PrintErrMsg	ldx L.ERR.Codes
				stx ZPPTR1
				ldx L.ERR.Codes+1
				stx ZPPTR1+1

				ldy #S.PS.RC
				lda (pPs),y

				ldy #0

.1				cmp (ZPPTR1),y
				beq .2
				iny
				cpy #ERR.Count
				bne .1

.2				lda L.ERR.Messages
				sta ZPPTR1
				lda L.ERR.Messages+1
				sta ZPPTR1+1
.3				dey
				bmi .8

				lda (ZPPTR1)
				jsr AddAp1Ptr1
				bra .3

.8				ldy #S.PS.hStdErr
				lda (pPS),y
				>PUSHA
				>PUSHW L.MSG.ERROR
				
				ldy #S.PS.RC
				lda (pPs),y
				
				>PUSHA
				>PUSHW ZPPTR1
				
				>PUSHBI 3

				>SYSCALL fprintf
				rts
*--------------------------------------
MAN
SAVE USR/SRC/BIN/SH.S.IO
LOAD USR/SRC/BIN/SH.S
ASM
