;	SCCSID = @(#)demnmp2.asm	6.3 91/07/25

SCCSID	equ "@(#)demnmp2.asm	6.3 91/07/25"

PAGE,132
;***EM	demnmp2.asm
;
;	SUMMARY
;	    DOS Emulation Named Pipe support
;
;	NOTES
;
;	REVISIONS
;
;Local	DCR/
;Tag	PTR	Who	When
;		sudeepb 03-Sept-90
;		    Created for MVDM
;
;Bx001	B723450 Bill Madden 05-29-91
;		    Fixed problems with DemCallNMP, demRawWriteNMP, demRawReadNMP
;       B724423 Tom Edler 07-25-91
;                   Fixed problem with DemQNmPInfo


INCL_NONE	equ	1			; don't include everything
INCL_TYPES	equ	1			; include basic types
INCL_DEF	equ	1			; include typedef macros
INCL_VDHALL	equ	1			; include VDH services

.xlist
	include fnomvdm.inc
	include dosseg.inc
	include basemaca.inc
	include osmaca.inc
	include osconst.inc
	include dossymc.inc
	include infoseg.inc
	include sigdef.inc
	include except.inc
	include pgexport.inc
	include vmexport.inc
	include selxport.inc
	include tkexport.inc
	include dirent.inc
	include curdir.inc
	include find.inc
	include screen.inc
	include sft.inc
	include sem.inc
	include seldesc.inc
	include protmode.inc
	include chardd.inc
	include dos3dev.inc
	include filesys.inc
	include inbuf.inc
	include dbcs.inc
	include argframe.inc
	include opncreat.inc
	include dem.inc
	include error.inc
	include extattr.inc
	include kmode.inc
	include mult.inc
	include pdb.inc
	include mi.inc
	include vpb.inc
	include fsc.inc
	include mvdm.inc
	include sysentry.inc
.cref
.list

	ModName  demnmp2, FNO_DEMNMP2_ASM

	CpuMode 386

	EXTRN16     w_QNmPipeInfo
	EXTRN16     w_QNmPHandState
	EXTRN16     w_SetNmPHandState
	EXTRN16     w_PeekNmPipe
	EXTRN16     w_TransactNmPipe
	EXTRN16     w_CallNmPipe
	EXTRN16     w_WaitNmPipe
	EXTRN16     w_RawReadNmPipe
	EXTRN16     w_RawWriteNmPipe

DefCode IMPORT, SWAP
DefFn	demMapDS
DefFn	demMapESDI
DefFn	demMapSeg
EndData
	FARCODE HIGH32
DefCode PRIVATE, SWAP, PASCAL

;***LP	demQNMPInfo - Get info on a named pipe
;
;	This routine
;
;	ENTRY
;	    VSF(BX)    is handle of pipe
;	    VSF(CX)    is size of buffer for info
;	    VSF(DX)    is Level of info to get
;	    VSF(DS:SI) is address of buffer
;
;	EXIT
;	    Carry set
;		AX = error
;	    Carry Clear
;		OK (DS:SI filled in)
;
;	CONTEXT
;	    Called from Virtual Mode
;	    Executes in Kernel
;
;	STEPS
;
;	NOTES
;	    None
;
;	WARNINGS
;	    None
;
;	CALLS
;
;	USES	All
;
	ASSUME	DS:FLAT,ES:FLAT,SS:NOTHING,FS:NOTHING,GS:NOTHING
	align 4

Procedure demQNMPInfo,near

LocalVar  qnmp_ArgFrame,<size DosQNmPipeInfoStruc>
	EnterProc
	movzx	ecx,[ebx].vsf_cx			; Buffer Size
	movzx	edx,[ebx].vsf_bx			; Pipe Handle
	movzx	eax,[ebx].vsf_dx			; Info Level - b724423
	movzx	esi,[ebx].vsf_si
	call	demMapDS				; DS:SI = buffer
	mov	qnmp_ArgFrame.qnpi_InfoBuf.Segmt,ds	 ; Buffer Segment
	mov	qnmp_ArgFrame.qnpi_InfoBuf.Offst,si	 ; Buffer Offset
	lea	bx,[qnmp_ArgFrame]			 ; ArgFrame in SS:[bx]
	CALL16	w_QNmPipeInfo
	LeaveProc
	ret
EndProc   demQNMPInfo

;***LP	demQNMPHandState - Get pipe mode information
;
;	This routine
;
;	ENTRY
;	    VSF(BX)    is handle of pipe
;
;	EXIT
;	    Carry clear
;		AX = Pipe mode word
;	    Carry set
;		AX = error
;
;	CONTEXT
;	    Called from Virtual Mode
;	    Executes in Kernel
;
;	STEPS
;
;	NOTES
;	    None
;
;	WARNINGS
;	    None
;
;	CALLS
;
;	USES	All
;
	ASSUME	DS:FLAT,ES:FLAT,SS:NOTHING,FS:NOTHING,GS:NOTHING
	align 4

Procedure demQNMPHandState,near
	movzx	edx,[ebx].vsf_bx	; dx = Pipe Handle
	CALL16	w_QNmPHandState
	ret
EndProc   demQNMPHandState

;***LP	demSetNMPHandState - Set pipe mode
;
;	This routine
;
;	ENTRY
;	    VSF(BX) is handle of pipe
;	    VSF(CX) is pipe mode word to set
;
;	EXIT
;	    Carry Clear
;		Indicated mode set
;	    Carry set
;		AX = error
;
;	CONTEXT
;	    Called from Virtual Mode
;	    Executes in Kernel
;
;	STEPS
;
;	NOTES
;	    None
;
;	WARNINGS
;	    None
;
;	CALLS
;
;	USES	All
;
	ASSUME	DS:FLAT,ES:FLAT,SS:NOTHING,FS:NOTHING,GS:NOTHING
	align 4

Procedure demSetNMPHandState,near
	movzx	edx,[ebx].vsf_bx	    ; Pipe Handle
	movzx	ebx,[ebx].vsf_cx	    ; New Mode Word
	CALL16	w_SetNmPHandState
	ret
EndProc   demSetNMPHandState

;***LP	demPeekNMP - Peek on a named pipe
;
;	This routine
;
;	ENTRY
;	    VSF(BX)    is handle of pipe
;	    VSF(CX)    is size of buffer for peek
;	    VSF(DS:SI) is address of peek buffer
;
;	EXIT
;
;	Carry set
;	    AX = error code. IF ERROR IS ERROR_MORE_DATA Carry
;		 clear returns are also set.
;	Carry Clear
;	    CX = bytes peeked from pipe
;	    User DX:SI is available data
;	    AX is pipe status
;
;	CONTEXT
;	    Called from Virtual Mode
;	    Executes in Kernel
;
;	STEPS
;
;	NOTES
;	    None
;
;	WARNINGS
;	    None
;
;	CALLS
;
;	USES
;
	ASSUME	DS:FLAT,ES:FLAT,SS:NOTHING,FS:NOTHING,GS:NOTHING
	align 4

Procedure demPeekNMP,near

LocalVar  pnmp_ArgFrame,<size DosPeekNmPipeStruc>
LocalVar  pnmp_AvailBuf,DWORD

	EnterProc
	push	ebx				    ; Save VSF add
	movzx	edx,[ebx].vsf_bx		    ; Pipe Handle
	movzx	ecx,[ebx].vsf_cx		    ; Buffer Size
	movzx	esi,[ebx].vsf_si
	call	demMapDS			    ; DS:SI = buffer
	mov	pnmp_ArgFrame.pnmp_Buffer.Segmt,ds
	mov	pnmp_ArgFrame.pnmp_Buffer.Offst,si
	lea	bx,[pnmp_AvailBuf]		    ; Get ptr to avail byte buf
	mov	pnmp_ArgFrame.pnmp_BytesAvail.Segmt,ss
	mov	pnmp_ArgFrame.pnmp_BytesAvail.Offst,bx
	lea	bx,[pnmp_ArgFrame]		    ; ArgFrame in SS:[bx]
	CALL16	w_PeekNmPipe
	FlatContext <ds>,ebx
	pop	ebx
	pushf
	jnc	short dpn7
	cmp	ax,ERROR_MORE_DATA		    ; this error still
	je	short dpn5			    ; returns all info
	jmp	short dpn10
dpn5:
	mov	[ebx].vsf_cx,0			    ; no bytes peeked
	jmp	short dpn8
dpn7:
	mov	[ebx].vsf_cx,ax 		    ; bytes peeked
	mov	ax,dx				    ; Pipe Status
dpn8:
	mov	esi,[pnmp_AvailBuf]
	mov	[ebx].vsf_si,si 		    ; user dx:si=avail. bytes
	shr	esi,16
	mov	[ebx].vsf_dx,si
dpn10:
	popf
	LeaveProc
	ret
EndProc   demPeekNMP

;***LP	demTransactNMP - Do a transact named pipe
;
;	This routine
;
;	ENTRY
;	    VSF(BX)    is handle of pipe
;	    VSF(CX)    is source (write) buffer length
;	    VSF(DX)    is destination (read) buffer length
;	    VSF(DS:SI) is address of source (write) buffer
;	    VSF(ES:DI) is address of destination (read) buffer
;
;	EXIT
;	    Carry set
;		AX = error code. IF ERROR IS ERROR_MORE_DATA Carry
;		     clear returns are also set.
;	    Carry Clear
;		CX = bytes read into destination (read) buffer address
;
;	CONTEXT
;	    Called from Virtual Mode
;	    Executes in Kernel
;
;	STEPS
;
;	NOTES
;	    None
;
;	WARNINGS
;	    None
;
;	CALLS
;
;	USES
;
	ASSUME	DS:FLAT,ES:FLAT,SS:NOTHING,FS:NOTHING,GS:NOTHING
	align 4

Procedure demTransactNMP,near
LocalVar  tnmp_ArgFrame, <size DosTransactNmPipeStruc>
	EnterProc
	push	ebx
	movzx	edx,[ebx].vsf_bx		; dx = handle
	movzx	ecx,[ebx].vsf_cx		; cx = InBuf size
	movzx	eax,[ebx].vsf_dx		; ax = OutBuf Size
	call	demMapESDI
	mov	tnmp_ArgFrame.tnmp_OutBuffer.Segmt,es
	mov	tnmp_ArgFrame.tnmp_OutBuffer.Offst,di
	movzx	esi,[ebx].vsf_si
	call	demMapDS
	mov	tnmp_ArgFrame.tnmp_InBuffer.Segmt,ds
	mov	tnmp_ArgFrame.tnmp_InBuffer.Offst,si
	lea	bx,[tnmp_ArgFrame]		    ; ArgFrame in SS:[bx]
	CALL16	w_TransactNmPipe
	FlatContext <ds>,ebx
	pop	ebx
	pushf
	jnc	short dtp5
	cmp	ax,ERROR_MORE_DATA
	je	short dtp5
	jmp	short dtp10

dtp5:
	mov	[ebx].vsf_cx,cx 	    ; cx = Bytes read

dtp10:
	popf
	LeaveProc
	ret
EndProc   demTransactNMP

;***LP	demCallNMP - Do a call named pipe
;
;	This routine
;
;	ENTRY
;
;	EXIT
;
;	CONTEXT
;	    Called from Virtual Mode
;	    Executes in Kernel
;
;	STEPS
;
;	NOTES
;	    None
;
;	WARNINGS
;	    None
;
;	CALLS
;
;	USES
;
	ASSUME	DS:FLAT,ES:FLAT,SS:NOTHING,FS:NOTHING,GS:NOTHING
	align 4

Procedure demCallNMP,near
LocalVar  cnmp_ArgFrame,<size DosCallNmPipeStruc>
LocalVar  dcn_FileName,DWORD
LocalVar  dcn_InBuf,DWORD
LocalVar  dcn_TimeOut,DWORD

	EnterProc
	push	ebx
;BD001	push	cx
	movzx	edi,[ebx].vsf_si
	call	demMapDS
	push	ds
	pop	es
	FlatContext <ds>,eax
	ASSUME	ES:NOTHING, DS:FLAT
	mov	cx,es:[di].cnmp_InBufferLen	; CX = InBuf Len.	  BC001
	mov	eax,es:[di].cnmp_InBuffer
	mov	[dcn_InBuf],eax
	mov	eax,es:[di].cnmp_FileName
	mov	[dcn_FileName],eax
	mov	eax,es:[di].cnmp_TimeOut
	mov	[dcn_TimeOut],eax
	mov	ax,es:[di].cnmp_OutBuffer.Segmt
	call	demMapSeg
	mov	esi,[_pTCBCur]			; (ESI) = pointer to TCB
	mov	[esi].TCB_TmpNameBuf.Segmt,ax
	mov	ax,es:[di].cnmp_OutBuffer.Offst
	mov	[esi].TCB_TmpNameBuf.Offst,ax
	mov	ax,es:[di].cnmp_OutBufferLen
	mov	[esi].TCB_TmpNameBufL,ax
	mov	ax,[dcn_InBuf].hiword
	mov	edx,ISELDEMES
	call	demMapSeg
	mov	cnmp_ArgFrame.cnmp_InBuffer.Segmt,ax
	mov	ax,[dcn_InBuf].loword
	mov	cnmp_ArgFrame.cnmp_InBuffer.Offst,ax
	mov	ax,[dcn_FileName].hiword
	mov	edx,ISELDEMDS
	call	demMapSeg
	mov	ds,ax
	mov	dx,[dcn_FileName].loword
;	pop	cx
	mov	di,[dcn_TimeOut].hiword
	mov	ax,[dcn_TimeOut].loword
	lea	bx,cnmp_ArgFrame
	CALL16	w_CallNmPipe
	FlatContext <ds>,ebx
	ASSUME DS:FLAT
	pop	ebx
	jc	short dcp5
	mov	[ebx].vsf_cx,ax
dcp5:
	LeaveProc
	ret
EndProc   demCallNMP

;***LP	demWaitNMP - Do a wait named pipe
;
;	This routine
;
;	ENTRY
;
;	EXIT
;
;	CONTEXT
;	    Called from Virtual Mode
;	    Executes in Kernel
;
;	STEPS
;
;	NOTES
;	    None
;
;	WARNINGS
;	    None
;
;	CALLS
;
;	USES
;
	ASSUME	DS:FLAT,ES:FLAT,SS:NOTHING,FS:NOTHING,GS:NOTHING
	align 4

Procedure demWaitNMP,near
	movzx	edi,[ebx].vsf_bx
	movzx	esi,[ebx].vsf_cx		; di:si = Timeout
	movzx	edx,[ebx].vsf_dx
	call	demMapDS			; DS:DX file name
	CALL16	w_WaitNmPipe
	ret
EndProc   demWaitNMP

;***LP	demRawReadNMP  - Do raw read on a pipe
;
;	This routine
;
;	ENTRY
;	    VSF(BX)    is handle of pipe
;	    VSF(CX)    is size to read
;	    VSF(DS:DX) is address of buffer
;
;	EXIT
;	    Carry set
;		AX = error code. IF ERROR IS ERROR_MORE_DATA Carry
;			 clear returns are also set.
;	    Carry Clear
;		CX = bytes read into transfer address
;
;	CONTEXT
;	    Called from Virtual Mode
;	    Executes in Kernel
;
;	STEPS
;
;	NOTES
;	    None
;
;	WARNINGS
;	    None
;
;	CALLS
;
;	USES
;
	ASSUME	DS:FLAT,ES:FLAT,SS:NOTHING,FS:NOTHING,GS:NOTHING
	align 4

Procedure demRawReadNMP,near
LocalVar  rrnp_ArgFrame, <size	DosRawReadNmPipeStruc>
	EnterProc
	push	ebx
	mov	dx,[ebx].vsf_bx 		    ; dx = pipe handle
	mov	cx,[ebx].vsf_cx 		    ; cx = size to read
	mov	ax,[ebx].vsf_dx 		    ; ax = buffer offset
	call	demMapDS			    ; ds:ax = buffer addr.
	mov	rrnp_ArgFrame.rrnp_Buffer.Segmt,ds
	mov	rrnp_ArgFrame.rrnp_Buffer.Offst,ax
	lea	bx,[rrnp_ArgFrame]		    ; ArgFrame in SS:[bx] BN001
	CALL16	w_RawReadNmPipe 		    ; Call worker
	FlatContext <ds>,ebx
	pop	ebx
	pushf
	jnc	short drr5
	cmp	ax,ERROR_MORE_DATA		    ; ERROR_MORE_DATA is special
	je	short drr5
	jmp	short drr10

drr5:
	mov	[ebx].vsf_cx,cx 		    ; Save bytes read
drr10:
	popf
	LeaveProc
	ret
EndProc   demRawReadNMP

;***LP	demRawWriteNMP - Do raw Write on a pipe
;
;	This routine
;
;	ENTRY
;	    VSF(BX)    is handle of pipe
;	    VSF(CX)    is size to read
;	    VSF(DS:DX) is address of buffer
;
;
;	EXIT
;	    Carry set
;		AX = error code.
;	    Carry Clear
;		CX = bytes writte from transfer address
;
;	CONTEXT
;	    Called from Virtual Mode
;	    Executes in Kernel
;
;	STEPS
;
;	NOTES
;	    None
;
;	WARNINGS
;	    None
;
;	CALLS
;
;	USES
;
	ASSUME	DS:FLAT,ES:FLAT,SS:NOTHING,FS:NOTHING,GS:NOTHING
	align 4

Procedure demRawWriteNMP,near
LocalVar  rwnp_ArgFrame, <size	DosRawWriteNmPipeStruc>
	EnterProc
	push	ebx
	mov	dx,[ebx].vsf_bx 		    ; dx = pipe handle
	mov	cx,[ebx].vsf_cx 		    ; cx = size to write
	mov	ax,[ebx].vsf_dx 		    ; ax = buffer offset
	call	demMapDS			    ; ds:ax = buffer addr.
	mov	rrnp_ArgFrame.rrnp_Buffer.Segmt,ds
	mov	rrnp_ArgFrame.rrnp_Buffer.Offst,ax
	lea	bx,[rrnp_ArgFrame]		    ; ArgFrame in SS:[bx]  BN001
	CALL16	w_RawWriteNmPipe		    ; Call worker
	FlatContext <ds>,ebx
	pop	ebx
	jc	short drw5
	mov	[ebx].vsf_cx,cx 		    ; Save bytes written
drw5:
	LeaveProc
	ret
EndProc   demRawWriteNMP
EndCode

	END
