NEW
  AUTO 3,1
*--------------------------------------
IO.Reset		jsr IO.Reset.In
				jsr IO.Reset.Out
				jmp IO.Reset.Err
*--------------------------------------
IO.Pipe.In		jsr IO.Pipe.OpenR		Open last created PIPE
				bcs .9

				pha

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

				pla

				ldy #S.PS.hStdIn
				sta (pPS),y

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

				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
				rts
*--------------------------------------
IO.Pipe.Out		>PUSHEA.G IO.FIFONAME
				>PUSHWZ					mode
				>SYSCALL MKFIFO
				bcs .9

				jsr IO.Pipe.OpenW		Open for OUTPUT
				bcs .9

				pha
				ldy #S.PS.hStdOut
				lda (pPS),y
				>STA.G IO.hOut
				pla
				ldy #S.PS.hStdOut
				sta (pPS),y

.9				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
				>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
				bra IO.Pipe.Open

IO.Pipe.OpenW	ldx #O.WRONLY+O.TEXT
	
IO.Pipe.Open	>LEA.G IO.FIFONAME
*--------------------------------------
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.MODE+1
				lda (ZPCLBuf),y
				and #$F0

.9				rts
*--------------------------------------
IO.Exec			>PUSHW ZPArgVBufPtr
				>PUSHBI S.PS.F.CLOSEONX
				>SYSCALL ExecL
				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	ldy #S.PS.RC
				lda (pPS),y
				>PUSHA

				>PUSHW ZPCLBuf

				>SYSCALL GetErrMsg

.8				ldy #S.PS.hStdErr
				lda (pPS),y
				>PUSHA
				>PUSHW L.MSG.ERROR

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

				>PUSHW ZPCLBuf

				>PUSHBI 3

				>SYSCALL FPrintF

				rts
*--------------------------------------
IO.EscOutYA		>STYA ZPPtr1			In
				>STYA ZPPtr2			Out
				
				ldy #0
				
.1				jsr GetPtr1NextChar
				beq .8
				
				cmp #'\'
				bne .6

				lda (ZPPtr1)
				beq .7

				ldx #OUT.EscCharsCnt-1
	
.2				cmp OUT.EscChars,x
				beq .5
				
				dex
				bpl .2
				
				stz ZPTmpW
				
				jsr ToUpperCase
				cmp #'X'
				bne .3
				
.20				jsr IncPtr1
				
				lda (ZPPtr1)
				beq .40

				jsr IO.IsHexDigit
				bcs .40

				asl ZPTmpW
				asl ZPTmpW
				asl ZPTmpW
				asl ZPTmpW
				ora ZPTmpW
				sta ZPTmpW
				bra .20

.3				jsr IO.IsOctDigit
				bcs .40

				jsr IncPtr1

				asl ZPTmpW
				asl ZPTmpW
				asl ZPTmpW
				ora ZPTmpW
				sta ZPTmpW
				lda (ZPPtr1)
				bne .3

.40				lda ZPTmpW
				bra .6

.4				tax
				
				lda #'\'
				sta (ZPPtr2),y
				iny
				
				txa
				bra .6

.5				lda OUT.EscCodes,x	
				jsr IncPtr1
				
.6				sta (ZPPtr2),y
				iny
				bra .1
				
.7				lda #'\'
				sta (ZPPtr2),y
				iny
				lda #0
				
.8				sta (ZPPtr2),y

				>LDYA ZPPtr2
*--------------------------------------
IO.OutYA		>PUSHYA
				>PUSHBI 0
				>SYSCALL PrintF

				rts
*--------------------------------------
IO.IsOctDigit	cmp #'0'
				bcc .9
				
				cmp #'7'+1
				bcs .9
				
				and #%00000111
				
				clc
				rts
				
.9				sec
				rts
*--------------------------------------
IO.IsHexDigit	cmp #'0'
				bcc .9
				
				cmp #'9'+1
				bcc .8
				
				cmp #'A'
				bcc .9
				
				cmp #'Z'+1
				bcc .7

				cmp #'a'
				bcc .9
				
				cmp #'z'+1
				bcc .9

*				sec

				sbc #$20
				
.7				sec
				sbc #'A'-10

.8				and #$0F
				
				clc
				rts
				
.9				sec
				rts
*--------------------------------------
MAN
SAVE usr/src/bin/sh.s.io
LOAD usr/src/bin/sh.s
ASM
