home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / gio-201.zip / giovionp.c < prev    next >
C/C++ Source or Header  |  1994-03-16  |  4KB  |  175 lines

  1. ///////////////////////////////////////////////////////////////////////////
  2. // GIOVIONP
  3. //
  4.  
  5.  
  6. // For a VERY simple named pipe demonstration, load two copies of GIOVIONP
  7. // at the same time.
  8. // One copy with the command:  GIOVIONP \PIPE\GIOPIPE 1
  9. // The other with the command: GIOVIONP \PIPE\GIOPIPE 0
  10. //
  11. // You can then type on one copy of the program, and have it echo on the
  12. // other, and vice versa. Once again, this is a VERY simple example. You
  13. // might want to modify the program so that when operating in server mode
  14. // it not only shows what is typed, but if it receives characters from the
  15. // client it echos the characters back to the client (much like a BBS does)
  16. // Also some processing of controls characters is needed (like backspaces,
  17. // and carriage returns)
  18.  
  19. #define INCL_BASE
  20. #include <os2.h>
  21.  
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <stdlib.h>
  25. #include <ctype.h>
  26. #include <io.h>
  27.  
  28. // GIO
  29. #include "gio2.h"
  30.  
  31. #define EXITKEY 0x2D
  32. #define CLRSCRN 0x2E
  33.  
  34. LONG CommIn(HCOMINFO);
  35. LONG CommOut(HCOMINFO);
  36.  
  37. CHAR TempString[300];
  38.  
  39. void main(INT ArgCount, CHAR **ArgVars)
  40. {
  41.     BOOL    KeepLooping = TRUE;
  42.     CHAR    ComPort[256];
  43.     KBDINFO KBStatus;
  44.     HCOMINFO hComInfo;
  45.     APIRET  RetCode;
  46.     BOOL    GotConnect = FALSE;
  47.     BOOL    ServerFlag;
  48.  
  49.     if (ArgCount < 3)
  50.     {
  51.         printf("\nUsage:  GIOVIONP comport serverflag\n");
  52.  
  53.         printf("          comport = \PIPE\filename\n");
  54.  
  55.         printf("          serverflag = 1=server 0=client\n");
  56.  
  57.         printf("\n  Ex.  GIOVIONP \PIPE\MyPipeName 1\n\n");
  58.  
  59.         exit(1);
  60.     }
  61.  
  62.     strcpy(TempString, "GIOVIONP... A VIO test pgm for Gern's Named Pipe IO routines...\n\r");
  63.     VioWrtTTY(TempString, strlen(TempString), 0);
  64.  
  65.     // Set up keyboard
  66.  
  67.     KBStatus.cb = sizeof(KBStatus);
  68.     KbdGetStatus(&KBStatus, 0);
  69.  
  70.     KBStatus.fsMask &= 0xFFF0;
  71.     KBStatus.fsMask |= KEYBOARD_ECHO_OFF | KEYBOARD_BINARY_MODE;
  72.     KbdSetStatus(&KBStatus, 0);
  73.  
  74.     strcpy(ComPort, ArgVars[1]);
  75.  
  76.     ServerFlag = atol(ArgVars[2]);
  77.  
  78.     if ((RetCode = ComOpen(ComPort, 0, 0, 0, 0, 0, ServerFlag ? ISPIPESERVER : ISPIPECLIENT, &hComInfo)))
  79.     {
  80.         printf("\nError (%lu) - Can't Open '%s' (%ld)!\n", RetCode, ComPort, hComInfo);
  81.  
  82.         exit(1);
  83.     }
  84.  
  85.     do
  86.     {
  87.         if (ComGetDCD(hComInfo))
  88.         {
  89.             if (! GotConnect)
  90.             {
  91.                 GotConnect = TRUE;
  92.  
  93.                 VioWrtTTY("Named Pipe Connected!\n", 22, 0);
  94.             }
  95.         }
  96.         else
  97.         {
  98.             if (GotConnect)
  99.             {
  100.                 VioWrtTTY("Named Pipe DisConnected!\n", 25, 0);
  101.  
  102.                 ComDisConnect(hComInfo);  // nuke pipe connection
  103.  
  104.                 ComConnect(hComInfo);     // listen for new connection
  105.  
  106.                 GotConnect = FALSE;
  107.             }
  108.         }
  109.  
  110.         CommIn(hComInfo);
  111.  
  112.         KeepLooping = (BOOL) CommOut(hComInfo);
  113.  
  114.     } while (KeepLooping);
  115.  
  116.     ComClose(hComInfo);
  117.  
  118.     strcpy(TempString, "\r\nTerminating...\r\n\n");
  119.     VioWrtTTY(TempString, strlen(TempString), 0);
  120.  
  121.     DosExit(EXIT_PROCESS, 0);
  122. }
  123.  
  124. LONG CommOut(HCOMINFO hComInfo)
  125. {
  126.     KBDKEYINFO KBInfo;
  127.  
  128.     KbdCharIn(&KBInfo, IO_NOWAIT, 0);
  129.  
  130.     if (! KBInfo.fbStatus)
  131.         return(1);
  132.  
  133.     if ((KBInfo.chChar == 0) || (KBInfo.chChar == 0xE0))
  134.     {
  135.         switch (KBInfo.chScan)
  136.         {
  137.             case CLRSCRN:
  138.                 VioWrtTTY("\x1B[2J", 4, 0);
  139.                 break;
  140.             case EXITKEY:
  141.                 return (0);
  142.  
  143.         }
  144.     }
  145.  
  146.     ComTXChar(hComInfo, KBInfo.chChar, FALSE);
  147.  
  148.     return (1);
  149. }
  150.  
  151. LONG CommIn(HCOMINFO hComInfo)
  152. {
  153.     BYTE     TrueChar;
  154.     SHORT    InChar;
  155.  
  156.     if (! ComRXEmpty(hComInfo))
  157.     {
  158.         if (! ComRXChar(hComInfo, &InChar, 0))
  159.         {
  160.             if (InChar == 12)
  161.                 VioWrtTTY("\x1B[2J", 4, 0);
  162.             else
  163.             {
  164.                 TrueChar = (BYTE) InChar;
  165.                 VioWrtTTY(&TrueChar, 1, 0);
  166.             }
  167.         }
  168.     }
  169.     else
  170.         DosSleep(5);
  171.  
  172.     return(0);
  173. }
  174.  
  175.