home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / guipip.zip / TEST.CPP < prev    next >
C/C++ Source or Header  |  1994-08-15  |  4KB  |  93 lines

  1. /* test module
  2.  
  3.    Copyright (c) 1994 Spitfire Software
  4.  
  5. */
  6.  
  7.  
  8. /* system includes */
  9. #ifdef __OS2__                          // test for os/2 or dos
  10.   #define INCL_DOSQUEUES                // include part of OS/2 header file
  11.   #define INCL_BASE                        // include part of OS/2 header file
  12.   #define INCL_NOPM                     // include part of OS/2 header file
  13.   #define INCL_DOSFILEMGR               // File manager values
  14.   #include <os2.h>                      // required for guirun.h and OS/2 functions
  15.   #include "d:\guide\sys\guirun.h"      // Guidelines OS/2 header: for String class
  16. #else
  17.   #include <windows.h>                  // Windows functions
  18.   #include "d:\guide\sys\guiwun.h"    // Guidelines Win header: for String
  19. #endif
  20. #include "test.h"
  21. #include "string.h"
  22.  
  23.  
  24. #define PIPESIZE 1
  25. #define HF_STDOUT 1
  26.  
  27. /* definition of GuideLines variables */
  28. extern LONG TerminateSw;
  29.  
  30. /* routine to start a subprocess and display its
  31.    output to stdout in a GuideLines list Box.
  32.  
  33.    Input: progname: Name of program to be executed
  34.           args:     Argument string to be passed to program
  35.  
  36.    Return: 0 no errors
  37.            1 could not start program
  38. */
  39. LONG readprog(String & progname, String & args)
  40. {
  41.  CHAR buff[1000]; //buffer for building string to send to GL list box
  42.  CHAR * ptr;      //pointer used for multiple purposes
  43.  CHAR abuff[200]; //argument string for invoke requested program
  44.  HPIPE hpR, hpW;  //pipe handles
  45.  RESULTCODES resc;  //pipe create result code
  46.  ULONG cbRead;    //number of bytes read from pipe
  47.  CHAR achBuf[PIPESIZE],        //input buffer for reading from pipe
  48.       szFailName[CCHMAXPATH];  //failure buffer for DosExecPgm
  49.  
  50.  HFILE hfSave = -1,            //buffer for saving normal stdout handle
  51.        hfNew = HF_STDOUT;      //pipe handle for writing
  52.  
  53.  // build arg string for DosExecPgm
  54.  strcpy(abuff,(char *) progname);  //get prog name
  55.  ptr = abuff + strlen(abuff) + 1;  // set up for putting second string after prog
  56.                                    // name
  57.  strcpy (ptr,(char *) args);       // add arg string after prog name
  58.  *(ptr + strlen(ptr) + 1) = 0;     // add double 0 to terminate list of strings
  59.  *buff = 0;                        // clear list box string build buffer
  60.  ptr = buff;                       // set temp pointer
  61.  DosDupHandle(HF_STDOUT,&hfSave);       // Save standard output handle
  62.  DosCreatePipe(&hpR,&hpW,PIPESIZE);     //Creates pipe
  63.  DosDupHandle(hpW,&hfNew);              //Duplicates standard output handle
  64.  if (DosExecPgm(szFailName,sizeof(szFailName),
  65.             EXEC_ASYNC, (PSZ) abuff, (PSZ) NULL,
  66.             &resc, (PSZ) progname) != 0) return (-1);
  67.  DosClose(hpW);                         //Closes write handle to ensure notification
  68.                                         // at child termination
  69.  DosDupHandle(hfSave,&hfNew);           //Brings stdout back
  70.  DosRead(hpR,achBuf,sizeof(achBuf),&cbRead);
  71.  while (cbRead && !TerminateSw)         //Read chars from pipe while open and
  72.   {                                     //  teerminate sw not set
  73.    if (*achBuf == 13)                   // if new line send string to GL
  74.     {
  75.      *ptr = 0;                          // set sting terminator
  76.      UpdateList((String) buff);         // call GL to update list box
  77.      *buff = 0;                         // reset for new string
  78.      ptr = buff;
  79.     }
  80.    else if (*achBuf != 10) *ptr++ = *achBuf;  //if not line feed add char to
  81.                                               //  GL string buffer
  82.    DosRead(hpR,achBuf,sizeof(achBuf),&cbRead);//get next char from pipe
  83.   }
  84.  if (TerminateSw) DosKillProcess((ULONG) 0,resc.codeTerminate); //Kill process if requested
  85.  DosClose(hpR);                         // close pipe
  86.  return(0L);                            // end function with all ok
  87. }
  88.  
  89.  
  90.  
  91.  
  92.  
  93.