/*static char *SCCSID = "@(#)pipemgr.c	6.1 90/11/18";*/
/*static char *SCCSID = "@(#)pipemgr.c     1.1 90/09/07";*/
/***	PIPEMGR.C - Named Pipe Manager
 *
 *    PipeMgr - Named Pipe Manager for Utils
 *
 *    Copyright (C) 1990 Microsoft Corp.
 *
 *    Modification history:
 *
 *
 *
 */

#define  INCL_DOS
#include <os2def.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <doscall.h>
#include <stduip.h>
#include "pipemgr.h"

USHORT
usPipeMgr(USHORT    usMsgType,	    /* Message Number		*/
	  CHAR	    **ppcVTable,    /* table of vars to insert	*/
	  USHORT    usVCount,	    /* number of vars to insert */
	  USHORT    usMsgNumber,    /* message number in msgfile*/
	  PCZ	    pczMsgFileName, /* message file name	*/
	  USHORT    usPipeHndl,     /* Pipe Handle		*/
	  USHORT    usRespType,     /* type of response required*/
	  CHAR	    *pcRespBuff,    /* Response Buffer		*/
	  USHORT    usRespBuffLen)  /* Response Buffer Length	*/




{

USHORT	  rc;				   // return code
USHORT	  u, cbWritten, cbRead; 	   // locals
PNSUI_MESSAGE pmPipeMsg;
BYTE	  bBuffer[PIPEBUFSIZE]; 	   // size of Pipe Buffer
PCZ	  pczCh;

#ifdef TRACE
    printf("\npipemgr( MsgT : %d \n", usMsgType);
    printf("ivTbl : %lx \n", ppcVTable);
    printf("ivCnt : %d \n", usVCount);
    printf("MsgNo : %d \n", usMsgNumber);
    printf("MsgFN : %s \n", pczMsgFileName);
    printf("PHdl  : %d \n", usPipeHndl);
    printf("RspT  : %d \n", usRespType);
    printf("KBPt  : %lx \n", pcRespBuff);
    printf("KBLn  : %d );\n", usRespBuffLen);
#endif

    (CHAR *)pmPipeMsg = bBuffer;

    switch ( usMsgType )
    {

	case SUI_MSGTYPE_SYSTEM_MESSAGE :

	    pmPipeMsg->usMsgType = SUI_MSGTYPE_SYSTEM_MESSAGE;
					    // put pipe msg type in output buffer
	    pmPipeMsg->mtype.suiSysMsg.usMsgNo = usMsgNumber;
					    // put usMsgNumber in output buffer
	    pmPipeMsg->mtype.suiSysMsg.usVCount = usVCount;
					    // put usVCount in output buffer
	    pmPipeMsg->mtype.suiSysMsg.usRespType = usRespType;
					    // put usRespType in output buffer

	    pczCh = pmPipeMsg->mtype.suiSysMsg.szMsgFile;
					    // point to start of variable string
	    if ( pczMsgFileName != NULL ) {   // determine if we have NULL filename
		strcpy( pczCh, pczMsgFileName );
					    // put pczMsgFileName in output buffer
		pczCh += ( strlen( pczMsgFileName ) + 1 );
	    } else *pczCh++ = NULL;
					    // insert NULL string

	    for ( u = 0; u < usVCount; u++, (*ppcVTable)++ ) {
		strcpy( pczCh, *ppcVTable );
					    // copy arguments to pipe message
		pczCh += ( strlen( *ppcVTable ) + 1 );
	    }

	    rc = DOSWRITE(usPipeHndl, bBuffer, (( pczCh - (PCZ)pmPipeMsg ) + 1),
			 &cbWritten);
					    // write to pipe
	    if ( rc != 0 )		    // check error code
		return( rc );		    // return error to caller

	    rc = DOSREAD( usPipeHndl, bBuffer, PIPEBUFSIZE, &cbRead );
	    if ( rc != 0 )		    // check error code
		return( rc );		    // return error to caller

	    switch ( usRespType )
	    {				    // handle possible pipe input

		case NO_RESPONSE :
		    #ifdef TRACE
			printf("\npipemgr:No Response Required\n");
		    #endif
		    break;

		case CR_RESPONSE :
		case ANY_KEY_RESPONSE :
		    #ifdef TRACE
			printf("\npipemgr:CR/ANYKEY Response Required\n");
		    #endif
		    break;

		case ARI_RESPONSE :
		case YES_NO_RESPONSE :
		    #ifdef TRACE
			printf("\npipemgr:YN/ARI Response Required\n");
		    #endif
		    pcRespBuff[0] = bBuffer[0];
		    break;

		case STRING_RESPONSE :
		    #ifdef TRACE
			printf("\npipemgr:STRING Response Required\n");
		    #endif
		    memcpy( pcRespBuff, bBuffer, cbRead );
		    break;

		default :
		    #ifdef TRACE
			printf("\npipemgr:UNKNOWN PIPE MESSAGE\n");
			printf("\npipemgr:UNKNOWN PIPE MESSAGE\n");
		    #endif
		    return( ERROR_UNKNOWN_PIPE_CMD );
		    break;

	    };	  //endswitch
	break;

	case SUI_MSGTYPE_PERCENT_DONE :

	    pmPipeMsg->usMsgType = SUI_MSGTYPE_PERCENT_DONE;
	    pmPipeMsg->mtype.suiPercentMsg.usPercentDone = usMsgNumber;
	    rc = DOSWRITE( usPipeHndl, bBuffer,
			  ( sizeof( pmPipeMsg->mtype.suiPercentMsg ) +
			    sizeof( pmPipeMsg->usMsgType ) ),
			   &cbWritten );
	    if ( rc != 0 )		    // check error code
		return( rc );		    // return error to caller

	break;

	case SUI_MSGTYPE_END_OF_DATA :

	    pmPipeMsg->usMsgType = SUI_MSGTYPE_END_OF_DATA;
	    rc = DOSWRITE( usPipeHndl, bBuffer, sizeof( pmPipeMsg->usMsgType ),
			   &cbWritten );
	    if ( rc != 0 )		    // check error code
		return( rc );		    // return error to caller
	break;

	case SUI_MSGTYPE_INFO_STRING :

	    pmPipeMsg->usMsgType = SUI_MSGTYPE_INFO_STRING;
	    pczCh = pmPipeMsg->mtype.suiInfoMsg.szArg;
	    strcpy( pczCh, *ppcVTable );
	    rc = DOSWRITE( usPipeHndl, bBuffer,
			 ( sizeof( pmPipeMsg->usMsgType ) +
			   strlen( *ppcVTable ) + 1 ),
			   &cbWritten );
	    if ( rc != 0 )		    // check error code
		return( rc );		    // return error to caller


	break;

	default :

	    #ifdef TRACE
		printf("\npipemgr:UNKNOWN PIPE MESSAGE TYPE\n");
		printf("\npipemgr:UNKNOWN PIPE MESSAGE TYPE\n");
	    #endif
	    return( ERROR_UNKNOWN_PIPE_CMD );

	break;

    }; // endswitch

    return( PIPE_TRANS_ACT_SUCCESS );
}





