home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ocl150a.zip / OCL / Source / OPipeTool.cpp < prev    next >
C/C++ Source or Header  |  1996-08-12  |  4KB  |  151 lines

  1. // OCL - OS/2 Class Library
  2. // (c) Cubus 1995
  3. // All Rights Reserved
  4. // OPipeTool.cpp
  5.  
  6. /*
  7.  * Redistribution and use in source and binary forms, with or without
  8.  * modification, are permitted provided that the following conditions
  9.  * are met:
  10.  * 1. Redistributions of source code must retain the above copyright
  11.  *    notice, this list of conditions and the following disclaimer.
  12.  * 2. Neither the name Cubus nor the name Team OCL may be used to
  13.  *    endorse or promote products derived from this software
  14.  *    without specific prior written permission.
  15.  * 3. See OCL.INF for a detailed copyright notice.
  16.  *
  17.  *              THIS SOFTWARE IS PROVIDED ``AS IS'' AND
  18.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  21.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27.  * SUCH DAMAGE.
  28.  */
  29.  
  30.  
  31. // $Header: W:/Projects/OCL/Source/rcs/OPipeTool.cpp 1.50 1996/08/11 23:49:27 B.STEIN Release $
  32.  
  33. #define __OCL_SOURCE__
  34.  
  35. #define OINCL_OSTRING
  36. #define OINCL_BASE
  37.  
  38. #include <ocl.hpp>
  39. #include <OPipeTool.hpp>
  40.  
  41. #if defined(__EMX__)
  42.   template class OThread<OPipeTool>;
  43. #endif
  44.  
  45.  
  46. OPipeTool::OPipeTool(PCSZ exename, PCSZ parms, 
  47.                      const ULONG sessionType,
  48.                      const ULONG pipeBuffer)
  49.   : printer(this, &OPipeTool::printout, 8192, FALSE),
  50.     bufferSize(pipeBuffer),
  51.     pipe(pipeBuffer),
  52.     readBuffer(pipeBuffer),
  53.     sType(sessionType),
  54.     childName(exename),
  55.     childParms(parms),
  56.     childPID(0),
  57.     childSID(0)  
  58.  {}
  59.  
  60.  
  61. OPipeTool::~OPipeTool()
  62.  {}
  63.  
  64.  
  65. PSZ OPipeTool::isOfType() const
  66.  return("OPipeTool"); 
  67. }
  68.  
  69.  
  70. void OPipeTool::detachChild()
  71. {
  72.  APIRET    rc;
  73.  STARTDATA StartData;
  74.  HFILE     hfNewStdOut = -1, 
  75.            hfNewStdErr = -1,
  76.            hfStdOut = 1, 
  77.            hfStdErr = 2;
  78.  OString   Title("Spawned: ");
  79.  
  80. // Duplicate handles
  81.  
  82.  DosDupHandle(hfStdOut, &hfNewStdOut);
  83.  DosDupHandle(hfStdErr, &hfNewStdErr);
  84.  
  85. // Close existing handles for current process
  86.  
  87.  DosClose(hfStdOut);
  88.  DosClose(hfStdErr);
  89.  
  90. // Redirect existing handles to new file
  91.  DosDupHandle(pipe.ofh, &hfStdOut);
  92.  DosDupHandle(pipe.ofh, &hfStdErr);
  93.  
  94. // start printout thread
  95.  if (!printer.isRunning())
  96.    printer.run(); 
  97.  
  98. // Start new session
  99.  
  100.  Title + childName;
  101.  
  102.  memset(&StartData, 0, sizeof(STARTDATA));
  103.  StartData.Length      = sizeof(STARTDATA);
  104.  StartData.Related     = SSF_RELATED_CHILD;
  105.  StartData.FgBg        = SSF_FGBG_BACK;
  106.  StartData.TraceOpt    = SSF_TRACEOPT_NONE;
  107.  StartData.PgmTitle    = Title;
  108.  StartData.PgmName     = childName;
  109.  StartData.PgmInputs   = (PBYTE) childParms.getText();
  110.  StartData.InheritOpt  = SSF_INHERTOPT_PARENT;
  111.  StartData.SessionType = sType;
  112.  StartData.PgmControl  = SSF_CONTROL_INVISIBLE;
  113.  
  114.  rc = DosStartSession(&StartData, &childSID, &childPID);
  115.  
  116. // Get back original handles
  117.  DosDupHandle(hfNewStdOut, &hfStdOut);
  118.  DosDupHandle(hfNewStdErr, &hfStdErr);
  119.  DosClose(hfNewStdOut);
  120.  DosClose(hfNewStdErr);
  121.  
  122.  if (rc != 0)
  123.    throw OVioException("Spawn failed.", rc);
  124. }
  125.  
  126.  
  127. void OPipeTool::stopChild()
  128. {
  129.  APIRET rc;
  130.  
  131.  if (!childPID)
  132.    return;
  133.  
  134.  if ((rc = DosKillProcess(DKP_PROCESS, childPID)) != 0)
  135.    throw OVioException("Killing spawnded process failed.", rc);
  136.  
  137.  childSID = 0;
  138.  childPID = 0;
  139. }
  140.  
  141. void OPipeTool::printout()
  142. {
  143.  while(pipe) {
  144.    if (pipe.getline(readBuffer, bufferSize))
  145.       cout << (PSZ)readBuffer << endl; }
  146. }
  147.  
  148.  
  149. // end of source
  150.