include	clib.inc
include	io.inc
include	errno.inc

ER_ACCESS_DENIED = 5
ER_BROKEN_PIPE	 = 109

PPROC	read, h:word, b:dword, l:word
	push	si
	push	di
	xor	si,si
	les	di,b
	mov	bx,h
	mov	al,[_osfile+bx]
	test	al,FH_EOF
	jnz	read_nul
	cmp	l,si
	je	read_nul
	test	al,FH_PIPE or FH_DEVICE
	jz	read_start
	mov	al,[pipech+bx]
	cmp	al,10
	je	read_start
	mov	es:[di],al
	mov	[pipech+bx],10
	inc	di
	inc	si
	dec	l
    read_start:
	push	bx
	push	es
	push	di
	push	l
	call	osread
	test	ax,ax
	jnz	read_03
	cmp	doserrno,ER_BROKEN_PIPE
	je	read_nul
	dec	ax
	cmp	doserrno,ER_ACCESS_DENIED
	jne	read_toend
	mov	errno,EBADF
    read_toend:
	jmp	read_end
    read_nul:
	xor	ax,ax
	jmp	read_end
    read_03:
	add	si,ax
	les	di,b
	mov	bx,h
	mov	al,[_osfile+bx]
	test	al,FH_TEXT
	jz	read_16
	and	al,not FH_CRLF
	cmp	byte ptr es:[di],10
	jne	read_04
	or	al,FH_CRLF
    read_04:
	mov	[_osfile+bx],al
	mov	dx,di
    read_05:
	mov	ax,word ptr b
	add	ax,si
	cmp	di,ax
	jnb	read_06
	cmp	byte ptr es:[di],26
	jne	read_07
	mov	bx,h
	test	byte ptr [_osfile+bx],FH_DEVICE
	jnz	read_06
	or	byte ptr [_osfile+bx],FH_EOF
    read_06:
	jmp	read_15
    read_07:
	cmp	byte ptr es:[di],13
	je	read_09
    read_08:
	mov	al,es:[di]
	mov	bx,dx
	mov	es:[bx],al
	inc	di
	inc	dx
	jmp	read_05
    read_09:
	mov	ax,word ptr b
	add	ax,si
	dec	ax
	cmp	di,ax
	jnb	read_10
	cmp	byte ptr es:[di+1],10
	jne	read_08
	add	di,2
	mov	al,10
	jmp	read_11
    read_10:
	inc	di
	push	es
	push	dx
	push	h
	push	ds
	push	offset peekchr
	push	1
	call	osread
	pop	dx
	pop	es
	test	ax,ax
	jnz	read_12
	mov	al,13
    read_11:
	mov	bx,dx
	mov	es:[bx],al
	inc	dx
	jmp	read_05
    read_12:
	mov	bx,h
	test	byte ptr [_osfile+bx],FH_DEVICE or FH_PIPE
	jz	read_13
	mov	al,10
	cmp	peekchr,al
	je      read_11
	mov	al,peekchr
	mov	[bx+pipech],al
	mov	al,13
	jmp	read_11
    read_13:
	cmp	dx,word ptr b
	jne	read_14
	mov	al,10
	cmp	peekchr,al
	je      read_11
    read_14:
	push	es
	push	dx
	push	bx
	push	-1
	push	-1
	push	SEEK_CUR
	call	lseek
	pop	dx
	pop	es
	mov	al,13
	cmp	peekchr,10
	jne	read_11
	jmp	read_05
    read_15:
	mov	si,dx
	sub	si,word ptr b
    read_16:
	mov	ax,si
    read_end:
	pop	di
	pop	si
	ret
PEND	read

_DATA	SEGMENT

pipech	db	_NFILE_ dup(10)
peekchr	db	0

_DATA	ENDS

	END
