home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / IMHAPI10.ZIP / SAMPLIPE.C < prev    next >
Text File  |  1991-12-07  |  9KB  |  275 lines

  1. /*****************************************************************************
  2.  *
  3.  *  SAMPLIPE will create try to create a named pipe from argv[1],  start 
  4.  *          IMHAPI.EXE and send commands to and receive responses from 
  5.  *          IMHAPI.  Enter "closepipe" to stop both SAMPLIPE and IMHAPI.
  6.  *          SAMPLIPE, it is hoped, will demonstrate how IMHAPI works
  7.  *          with a named pipe.
  8.  *
  9.  *          IMHAPI is a program which allows a user to issue EHLLAPI
  10.  *          commands either interactively (from command line prompt or
  11.  *          via a pipe) or from a file of commands.
  12.  *             
  13.  *          IMHAPI.DOC should be included with this file set.  Please
  14.  *          consult that for more information on IMHAPI.
  15.  *
  16.  *          PLEASE NOTE:  This program is offered with NO warranties
  17.  *          or guarrantees of ANY kind, either expressed or implied.
  18.  *          Use of this program and any accompanying programs or materials 
  19.  *          is entirely at the risk of the user of those programs and 
  20.  *          materials.  The set of archived files and programs, of which 
  21.  *          this is a member, is offered as an means of sharing information 
  22.  *          and techniques about aspects of OS/2 programming and usage.
  23.  *          If there are any questions about this program or materials,
  24.  *          please contact the author (address below).
  25.  *             
  26.  *          I really would appreciate your comments on this program and 
  27.  *          its general level of usefulness.  I can be reached at:
  28.  *             
  29.  *                          Paul Firgens
  30.  *                          461 Glenwood Heights
  31.  *                          Wisconsin Rapids, WI 54494-6264
  32.  *                          CompuServ Member ID: 73577,1234
  33.  *             
  34.  *          Thanks, in advance!
  35.  *             
  36.  *              Copyright (c) 1991, Paul Firgens, All Rights Reserved
  37.  *
  38.  *
  39.  *****************************************************************************
  40.  */
  41.  
  42. #define INCL_BASE
  43.  
  44. #define INCL_DOSNMPIPES
  45. #define INCL_DOSFILEMGR
  46. #define INCL_ERRORS
  47. #define INCL_DOSPROCESS
  48.  
  49. #include <os2.h>
  50. #include <dos.h>
  51. #include <stdio.h>
  52. #include <string.h>
  53. #include <stdlib.h>
  54. #include <doscalls.h>
  55. #include <ctype.h>
  56.  
  57. #define COUNT_OUTBUF    4096
  58. #define COUNT_INBUF     4096
  59. #define DEF_TIMEOUT     5000L
  60. #define READ_BUFFER     4096
  61.  
  62. #define PIPE_ACCESS_DUPLEX  0x0002
  63. #define PIPE_ACCESS_IN      0x0000
  64. #define PIPE_ACCESS_OUT     0x0001
  65. #define PIPE_WRITEBEHIND    0x0000
  66. #define PIPE_NOWRITEBEHIND  0x4000
  67. #define PIPE_INHERIT        0x0000
  68. #define PIPE_NOINHERIT      0x0080
  69. #define PIPE_WAIT           0x0000
  70. #define PIPE_NOWAIT         0x8000
  71. #define PIPE_BYTE_MODE      0x0000
  72. #define PIPE_STREAM_MODE    0x0400
  73. #define PIPE_BYTE_READ      0x0000
  74. #define PIPE_STREAM_READ    0x0100
  75.  
  76.  
  77. extern VOID ShowMyError(VOID);
  78. extern int PipeErrorMessage(USHORT);
  79.  
  80. main(int argc, char *argv[])
  81. {
  82.  
  83.     unsigned    rc, arglen, c; 
  84.     PSZ         pszReadBuf, pszGotten, pszReceivedBuf;
  85.     HPIPE       hp;
  86.     SHORT       sBytesRead, sBytesReceived; 
  87.     USHORT      cbBytesWritten;
  88.     CHAR        chFailName[100];
  89.     RESULTCODES rescResults;
  90.     char        argbuf[256];
  91.  
  92.     rc = c = 0; 
  93.     
  94.     /* check to see that a pipe name parm is passed on the command line */
  95.     if(argc != 2) 
  96.     {
  97.         puts("SAMPLIPE: Enter SAMPLIPE 'pipename', to start IMHAPI.EXE, connect\n\
  98.         it to that named pipe and prompt for commands.  The form of a\n\
  99.         named pipe 'pipename' is '\\pipe\\user_supplied_name'.");
  100.         DosExit(EXIT_PROCESS,2);
  101.     }
  102.  
  103.     /* Explain what's happening and then create a named pipe... */
  104.     printf("SAMPLIPE creates the named pipe %s, starts IMHAPI.EXE and\n", argv[1]);
  105.     puts(" sends commands and receives responses from IMHAPI.  Enter \"closepipe\" to");
  106.     puts(" stop both SAMPLIPE and IMHAPI.  See IMHAPI.DOC for more information on");
  107.     puts(" IMHAPI/EHLLAPI commands.");
  108.  
  109.     if((rc = DosMakeNmPipe(argv[1],
  110.             &hp,
  111.             PIPE_NOWRITEBEHIND | PIPE_NOINHERIT | PIPE_ACCESS_DUPLEX, 
  112.             PIPE_NOWAIT | PIPE_STREAM_MODE | PIPE_STREAM_READ | 1,
  113.             COUNT_OUTBUF, COUNT_INBUF, DEF_TIMEOUT)))
  114.     {
  115.         PipeErrorMessage(rc);
  116.         free(argv[1]);
  117.         DosExit(EXIT_PROCESS,2);
  118.     }
  119.     
  120.     /* create the buffer that IMHAPI would expect to obtain if started from the 
  121.      * the command line: argv[0] = 'IMHAPI' and argv[1] = pipename (which is
  122.      * argv[1] from the start of SAMPLIPE) and then start IMHAPI...
  123.      */
  124.     arglen = strlen(argv[1]);
  125.     memmove(argbuf, "IMHAPI", 6);
  126.     memmove((char *)&(argbuf[7]), argv[1], arglen);
  127.    
  128.     rc = DosExecPgm(chFailName, 
  129.         sizeof(chFailName), 
  130.         2, 
  131.         argbuf, 
  132.         0, 
  133.         &rescResults, 
  134.         "imhapi.exe"); 
  135.  
  136.      if(rc)
  137.      {
  138.          printf("DosExecPgm failed with return code: %u\n", rc);
  139.          DosClose(hp);
  140.          DosExit(EXIT_PROCESS, 8);
  141.      }
  142.     
  143.     /* keep trying to connect until IMHAPI is going, 
  144.      * but only give it 20 chances to start up
  145.      */
  146.     do
  147.     {
  148.         rc = DosConnectNmPipe(hp);
  149.         if (rc > 0)
  150.         {
  151.             PipeErrorMessage(rc);
  152.             DosSleep(0500l); 
  153.             c++;
  154.         }
  155.     }
  156.     while ( (c<20) && rc ); 
  157.     
  158.     /* couldn't connect within the limit set.  We better quit */
  159.     if(c >= 20)
  160.     {
  161.         fprintf(stderr, 
  162.             "SAMPLIPE: Unable to Connect to Named Pipe %s.  Program aborted.\n",
  163.             argv[1]);
  164.         DosExit(EXIT_PROCESS,0);
  165.     }
  166.  
  167.     /* let us know we made contact */
  168.     puts("Connection made...");
  169.     
  170.     /* alloc space for the input and output buffers */    
  171.     if(!(pszReadBuf = (PSZ)calloc(READ_BUFFER, 1)))
  172.     {
  173.         printf("Cannot allocate Read Buffer for STDIN");
  174.         DosExit(EXIT_PROCESS,2);
  175.     }
  176.     if(!(pszReceivedBuf = (PSZ)calloc(READ_BUFFER, 1)))
  177.     {
  178.         printf("Cannot allocate Received Buffer for STDOUT");
  179.         DosExit(EXIT_PROCESS,2);
  180.     }
  181.     
  182.     /* issue prompt */
  183.     VioWrtTTY("ToPipe >>", 9, 0);
  184.     
  185.     /* loop...getting commands, sending them the pipe, receiving the response,
  186.      *        displaying the response until 'closepipe' command issued
  187.      */
  188.     for(pszGotten = gets(pszReadBuf); 
  189.         pszGotten && (memcmp(pszReadBuf, "closepipe", 9) != 0);
  190.         pszGotten = gets(pszReadBuf))
  191.     { 
  192.         sBytesRead = strlen(pszGotten);
  193.         rc = DosTransactNmPipe(hp, 
  194.             pszReadBuf, 
  195.             sBytesRead, 
  196.             pszReceivedBuf,
  197.             4096,
  198.             &sBytesReceived);
  199.         if (rc)
  200.             PipeErrorMessage(rc);
  201.         VioWrtTTY("FromPipe << ", 12, 0);
  202.         rc = DosWrite(1, pszReceivedBuf, sBytesReceived, &cbBytesWritten);
  203.         VioWrtTTY("\r\nToPipe >>", 11, 0);
  204.         
  205.     }
  206.  
  207.     /* when we're done, release the buffers we've used, 
  208.      * close the Named Pipe and exit the pgm
  209.      */
  210.     free(pszReadBuf);
  211.     free(pszReceivedBuf);
  212.     DosClose(hp);
  213.     DosExit(EXIT_PROCESS,0);
  214.  
  215. }
  216.  
  217.  
  218. int PipeErrorMessage(USHORT rc)
  219. {
  220.     switch(rc) 
  221.      {
  222.     case (ERROR_INVALID_PARAMETER):
  223.         printf("*** Error *** Invalid Pipe Parameter!\n");
  224.         break;
  225.     case (ERROR_NOT_ENOUGH_MEMORY):
  226.         printf("*** Error *** Insufficient Memory to Open Pipe!\n");
  227.         break;
  228.     case (ERROR_OUT_OF_STRUCTURES):
  229.         printf("*** Error *** Pipe Not Opened - Out of Structures!\n");
  230.         break;
  231.     case (ERROR_PATH_NOT_FOUND):
  232.         printf("*** Error *** Pipe Not Opened - Path Not Found!\n");
  233.         break;
  234.     case (ERROR_PIPE_BUSY):
  235.         printf("*** Error *** Pipe Busy!\n");
  236.         break;
  237.     case(ERROR_ACCESS_DENIED):
  238.         printf("*** Error *** Access Denied!\n");
  239.         break;
  240.     case(ERROR_BROKEN_PIPE):
  241.         printf("*** Error *** Broken Pipe!\n");
  242.         break;
  243.     case(ERROR_INVALID_HANDLE):
  244.         printf("*** Error *** Invalid Handle!\n");
  245.         break;
  246.     case(ERROR_LOCK_VIOLATION):
  247.         printf("*** Error *** Lock Violation!\n");
  248.         break;
  249.     case(ERROR_NOT_DOS_DISK):
  250.         printf("*** Error *** Not DOS Disk!\n");
  251.         break;
  252.     case(ERROR_BAD_PIPE):
  253.         printf("*** Error *** Bad Pipe!\n");
  254.         return(2);
  255.     case(ERROR_INTERRUPT):
  256.         printf("*** Error *** Interrupt Waiting for Pipe!\n");
  257.         return(2);
  258.     case(ERROR_SEM_TIMEOUT):
  259.         printf("*** Error *** Time Out Waiting for Pipe!\n");
  260.         return(2);
  261.     case(ERROR_INVALID_FUNCTION):
  262.         printf("*** Error *** Invalid Function!\n");
  263.         break;
  264.     case(ERROR_PIPE_NOT_CONNECTED):
  265.         printf("*** Error *** Not Connected!\n");
  266.         break;
  267.     default:
  268.         printf("*** Unknown Pipe Error ***!\n");
  269.         printf("Error code: %u.\n", rc);
  270.         break;
  271.     }
  272.     return(0);
  273. }
  274. 
  275.