home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / npmeta.zip / PPR_SRV.CPP < prev    next >
C/C++ Source or Header  |  1997-04-09  |  2KB  |  78 lines

  1. #define INCL_BASE
  2. #define INCL_DOSPROCESS
  3. #define INCL_DOSERRORS
  4. #define INCL_DOSNMPIPES
  5. #include <os2.h>
  6. #include <signal.h>
  7. #include <setjmp.h>
  8.  
  9. #define HF_STDOUT 1
  10.  
  11. static jmp_buf jbuf;
  12.  
  13. extern "C" {
  14.   void my_sig( int );
  15. }
  16.  
  17. // Ctrl-Break indicator
  18. static volatile int bBreak=0;
  19. void my_sig( int ) { bBreak=1; longjmp( jbuf, 1 ); }
  20.  
  21. int main( int argc, char* argv[] ) {
  22.   const cbBuf=10240;
  23.   PSZ szPipeName="\\PIPE\\PRINTF";
  24.   if ( argc>1 ) szPipeName=argv[1];
  25.   HPIPE hPipe=NULLHANDLE;             /* Pipe handle */
  26.   APIRET rc=NO_ERROR;               /* Return code */
  27.  
  28.   rc=DosCreateNPipe( szPipeName,
  29.                      &hPipe,
  30.                      NP_ACCESS_DUPLEX,
  31.                      NP_RMESG | NP_WMESG |
  32.                      NP_WAIT |
  33.                      0x01,          /* Unique instance of pipe */
  34.                      0,             /* Output buffer size */
  35.                      cbBuf,         /* Input buffer size */
  36.                      0L );          /* Use default time-out */
  37.   if ( rc==NO_ERROR ) {
  38.     _SigFunc old_int=signal(SIGINT, my_sig);
  39.     _SigFunc old_brk=signal(SIGBREAK, my_sig);
  40.     PSZ szBuf=new char[cbBuf];
  41.     for(;;) {
  42.       setjmp( jbuf );
  43.       if ( bBreak ) {
  44.         DosBeep( 400, 150 );
  45.         break;
  46.       }
  47.       rc=DosConnectNPipe( hPipe );
  48.       if ( rc==NO_ERROR ) {
  49.         ULONG ulBytes=0;                      /* Bytes read or written */
  50.         rc=DosRead( hPipe,            /* Handle of pipe */
  51.                     szBuf,               /* Buffer for message read */
  52.                     cbBuf,       /* Buffer size */
  53.                     &ulBytes);             /* Number of bytes actually read */
  54.         if ( rc==NO_ERROR ) {
  55.           ULONG ulDummy=1, ulDummy1=0;
  56.           // for DosCallNPipe
  57.           DosWrite( hPipe, &ulDummy, sizeof( ulDummy ), &ulDummy1 );
  58.           DosWrite( HF_STDOUT, szBuf, ulBytes, &ulBytes );
  59.         } else break;
  60.         DosResetBuffer( hPipe );
  61.         DosDisConnectNPipe( hPipe );
  62.       } else {
  63.         DosBeep( 400, 150 );
  64.       }
  65.     }
  66.     delete[] szBuf;
  67.     DosClose( hPipe );
  68.     signal(SIGBREAK, old_brk);
  69.     signal(SIGINT, old_int);
  70.   } else {
  71.     DosBeep( 400, 150 );
  72.   }
  73.   return rc;
  74. }
  75.  
  76.  
  77.  
  78.