camprocess.cpp

Go to the documentation of this file.
00001 // $Id: camprocess.cpp 1521 2006-07-25 11:03:29Z gerry $
00002 /* @@tag:xara-cn@@ DO NOT MODIFY THIS LINE
00003 ================================XARAHEADERSTART===========================
00004  
00005                Xara LX, a vector drawing and manipulation program.
00006                     Copyright (C) 1993-2006 Xara Group Ltd.
00007        Copyright on certain contributions may be held in joint with their
00008               respective authors. See AUTHORS file for details.
00009 
00010 LICENSE TO USE AND MODIFY SOFTWARE
00011 ----------------------------------
00012 
00013 This file is part of Xara LX.
00014 
00015 Xara LX is free software; you can redistribute it and/or modify it
00016 under the terms of the GNU General Public License version 2 as published
00017 by the Free Software Foundation.
00018 
00019 Xara LX and its component source files are distributed in the hope
00020 that it will be useful, but WITHOUT ANY WARRANTY; without even the
00021 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00022 See the GNU General Public License for more details.
00023 
00024 You should have received a copy of the GNU General Public License along
00025 with Xara LX (see the file GPL in the root directory of the
00026 distribution); if not, write to the Free Software Foundation, Inc., 51
00027 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
00028 
00029 
00030 ADDITIONAL RIGHTS
00031 -----------------
00032 
00033 Conditional upon your continuing compliance with the GNU General Public
00034 License described above, Xara Group Ltd grants to you certain additional
00035 rights. 
00036 
00037 The additional rights are to use, modify, and distribute the software
00038 together with the wxWidgets library, the wxXtra library, and the "CDraw"
00039 library and any other such library that any version of Xara LX relased
00040 by Xara Group Ltd requires in order to compile and execute, including
00041 the static linking of that library to XaraLX. In the case of the
00042 "CDraw" library, you may satisfy obligation under the GNU General Public
00043 License to provide source code by providing a binary copy of the library
00044 concerned and a copy of the license accompanying it.
00045 
00046 Nothing in this section restricts any of the rights you have under
00047 the GNU General Public License.
00048 
00049 
00050 SCOPE OF LICENSE
00051 ----------------
00052 
00053 This license applies to this program (XaraLX) and its constituent source
00054 files only, and does not necessarily apply to other Xara products which may
00055 in part share the same code base, and are subject to their own licensing
00056 terms.
00057 
00058 This license does not apply to files in the wxXtra directory, which
00059 are built into a separate library, and are subject to the wxWindows
00060 license contained within that directory in the file "WXXTRA-LICENSE".
00061 
00062 This license does not apply to the binary libraries (if any) within
00063 the "libs" directory, which are subject to a separate license contained
00064 within that directory in the file "LIBS-LICENSE".
00065 
00066 
00067 ARRANGEMENTS FOR CONTRIBUTION OF MODIFICATIONS
00068 ----------------------------------------------
00069 
00070 Subject to the terms of the GNU Public License (see above), you are
00071 free to do whatever you like with your modifications. However, you may
00072 (at your option) wish contribute them to Xara's source tree. You can
00073 find details of how to do this at:
00074   http://www.xaraxtreme.org/developers/
00075 
00076 Prior to contributing your modifications, you will need to complete our
00077 contributor agreement. This can be found at:
00078   http://www.xaraxtreme.org/developers/contribute/
00079 
00080 Please note that Xara will not accept modifications which modify any of
00081 the text between the start and end of this header (marked
00082 XARAHEADERSTART and XARAHEADEREND).
00083 
00084 
00085 MARKS
00086 -----
00087 
00088 Xara, Xara LX, Xara X, Xara X/Xtreme, Xara Xtreme, the Xtreme and Xara
00089 designs are registered or unregistered trademarks, design-marks, and/or
00090 service marks of Xara Group Ltd. All rights in these marks are reserved.
00091 
00092 
00093       Xara Group Ltd, Gaddesden Place, Hemel Hempstead, HP2 6EX, UK.
00094                         http://www.xara.com/
00095 
00096 =================================XARAHEADEREND============================
00097  */
00098 
00099 // The module that controls the OIL side of import/export filters.
00100 
00101 
00102 #include "camtypes.h"
00103 
00104 #include "camprocess.h"
00105 
00106 
00107 
00108 CamProcess::CamProcess(CCLexFile* pInFile, CCLexFile* pOutFile)
00109 {
00110     m_bDead = true;
00111     m_ReturnCode = -1;
00112     m_pInFile = pInFile;
00113     m_pOutFile = pOutFile;
00114     m_BytesIn = 0;
00115     m_BytesOut = 0;
00116 }
00117 
00118 CamProcess::~CamProcess()
00119 {
00120     if (!m_bDead)
00121     {
00122         TRACEUSER("Gerry", _T("Process not dead in ~CamProcess"));
00123     }
00124 }
00125 
00126 
00127 INT32 CamProcess::Execute(const wxString& cmd)
00128 {
00129     // We're now running
00130     m_bDead = false;
00131 
00132     // Make sure redirection happens
00133     Redirect();
00134 
00135     long pid = wxExecute(cmd, wxEXEC_ASYNC, this);
00136     if (pid == 0)
00137     {
00138         // Report problem
00139         m_bDead = true;
00140         return 123;
00141     }
00142 
00143     BYTE ReadBuffer[4096];
00144     size_t ReadBytes = 0;
00145     BYTE* ReadPtr = NULL;
00146     bool bMoreInput = true;
00147     size_t InFileLeft = 0;
00148     if (m_pInFile)
00149     {
00150         InFileLeft = m_pInFile->Size();
00151 //      TRACEUSER("Gerry", _T("InFileSize = %d"), InFileLeft);
00152     }
00153 
00154     // Loop until m_bDead is true
00155 
00156     while (!m_bDead)
00157     {
00158         // Call the virtual function to process any output on stderr
00159         ProcessStdErr();
00160 
00161         wxYield();
00162 
00163         // If we have an output file
00164         if (m_pOutFile)
00165         {
00166             // If there is output from the process
00167             if (!m_bDead && IsInputAvailable())
00168             {
00169                 // Copy the data to the file
00170 
00171                 size_t NumRead = 4096;
00172                 BYTE Buffer[4096];
00173 
00174                 // Read a buffer full
00175                 GetInputStream()->Read(Buffer, 4096);
00176 
00177                 NumRead = GetInputStream()->LastRead();
00178 
00179                 // Write the buffer to the file
00180                 if (NumRead > 0)
00181                 {
00182 //                  TRACEUSER("Gerry", _T("Writing %d bytes of stdout"), NumRead);
00183                     m_pOutFile->write(Buffer, NumRead);
00184                 }
00185             }
00186         }
00187         else
00188         {
00189             // Call the virtual function to process the output
00190             if (!m_bDead)
00191                 ProcessStdOut();
00192         }
00193 
00194         wxYield();
00195 
00196         // If we have an input file
00197         if (m_pInFile)
00198         {
00199             // Copy some data to the process
00200             // This was a while loop
00201             if (!m_bDead && bMoreInput)
00202             {
00203                 // If there is nothing in the buffer
00204                 if (ReadBytes == 0)
00205                 {
00206                     ReadBytes = 4096;
00207                     if (ReadBytes > InFileLeft)
00208                         ReadBytes = InFileLeft;
00209 
00210                     if (ReadBytes > 0)
00211                     {
00212                         // Read a buffer full
00213 //                      TRACEUSER("Gerry", _T("Reading %d"), ReadBytes);
00214                         m_pInFile->read(ReadBuffer, ReadBytes);
00215 
00216                         InFileLeft -= ReadBytes;
00217                         ReadPtr = ReadBuffer;
00218                     }
00219                 }
00220 
00221                 // If there is something in the buffer
00222                 if (ReadBytes > 0 && GetOutputStream()->IsOk())
00223                 {
00224 //                  TRACEUSER("Gerry", _T("Buffer contains %d"), ReadBytes);
00225                     // Try to write it to the process
00226                     GetOutputStream()->Write(ReadPtr, ReadBytes);
00227 
00228                     size_t Done = GetOutputStream()->LastWrite();
00229 //                  TRACEUSER("Gerry", _T("Written %d"), Done);
00230                     // If we couldn't write it all
00231                     if (Done < ReadBytes)
00232                     {
00233                         // Update the buffer pointer
00234                         ReadPtr += Done;
00235                     }
00236                     // This is correct for all, part or none written
00237                     ReadBytes -= Done;
00238                 }
00239                 else
00240                 {
00241                     // Indicate there is no more stdin
00242 //                  TRACEUSER("Gerry", _T("Buffer is empty - closing"));
00243                     CloseOutput();
00244                     bMoreInput = false;
00245                 }
00246             }
00247         }
00248         else
00249         {
00250             // Call the virtual function to process the input
00251             if (!m_bDead)
00252                 ProcessStdIn();
00253         }
00254 
00255         wxYield();
00256     }
00257 
00258 //  TRACEUSER("Gerry", _T("Exiting with %d"), m_ReturnCode);
00259     return m_ReturnCode;
00260 }
00261 
00262 void CamProcess::ProcessStdIn()
00263 {
00264     // Do nothing in here
00265 }
00266 
00267 
00268 void CamProcess::ProcessStdOut()
00269 {
00270     if (IsInputAvailable())
00271     {
00272         wxTextInputStream tis(*GetInputStream());
00273 
00274         // This assumes that the output is always line buffered
00275         while (IsInputAvailable())
00276         {
00277             wxString line;
00278             line << tis.ReadLine();
00279 //          TRACEUSER("Gerry", _T("(stdout):%s"), line.c_str());
00280         }
00281     }
00282 
00283 }
00284 
00285 
00286 void CamProcess::ProcessStdErr()
00287 {
00288     if (IsErrorAvailable())
00289     {
00290         wxTextInputStream tis(*GetErrorStream());
00291 
00292         // This assumes that the output is always line buffered
00293         while (IsErrorAvailable())
00294         {
00295             wxString line;
00296             line << tis.ReadLine();
00297 //          TRACEUSER("Gerry", _T("(stderr):%s"), line.c_str());
00298         }
00299     }
00300 }
00301 
00302 
00303 void CamProcess::OnTerminate(int /*TYPENOTE: Correct*/ pid, int /*TYPENOTE: Correct*/ status)
00304 {
00305 //  TRACEUSER("Gerry", _T("CamProcess::OnTerminate pid = %d  status = %d"), pid, status);
00306     m_bDead = true;
00307     m_ReturnCode = status;
00308 
00309     // Process anything remaining on stderr and stdout
00310     // If we have an output file
00311     if (m_pOutFile)
00312     {
00313         // If there is output from the process
00314         if (IsInputAvailable())
00315         {
00316             // Copy the data to the file
00317             size_t NumRead = 4096;
00318             BYTE Buffer[4096];
00319 
00320             while (NumRead > 0)
00321             {
00322                 // Read a buffer full
00323                 GetInputStream()->Read(Buffer, 4096);
00324 
00325                 NumRead = GetInputStream()->LastRead();
00326 
00327                 // Write the buffer to the file
00328                 if (NumRead > 0)
00329                 {
00330                     m_pOutFile->write(Buffer, NumRead);
00331                 }
00332             }
00333         }
00334     }
00335     else
00336     {
00337         // Call the virtual function to process the output
00338         ProcessStdOut();
00339     }
00340 
00341     ProcessStdErr();
00342 }
00343 
00344 
00345 
00346 
00347 /*****************************************************************************************
00348 
00349     class CamLaunchProcess implementation
00350 
00351 *****************************************************************************************/
00352 
00353 /********************************************************************************************
00354 
00355 >   CamLaunchProcess::CamLaunchProcess()
00356 
00357     Author:     Phil_Martin (Xara Group Ltd) <camelotdev@xara.com>
00358     Created:    19/May/2006
00359     Inputs:     -
00360     Outputs:    -
00361     Returns:    -
00362     Purpose:    CamLaunchProcess constructor
00363 
00364 ********************************************************************************************/
00365 CamLaunchProcess::CamLaunchProcess()
00366 {
00367     m_pid = 0;
00368     m_bDead = true;
00369     m_ReturnCode = -1;
00370     m_bConnected = false;
00371 }
00372 
00373 
00374 /********************************************************************************************
00375 
00376 >   CamLaunchProcess::~CamLaunchProcess()
00377 
00378     Author:     Phil_Martin (Xara Group Ltd) <camelotdev@xara.com>
00379     Created:    19/May/2006
00380     Inputs:     -
00381     Outputs:    -
00382     Returns:    -
00383     Purpose:    CamLaunchProcess destructor
00384 
00385 ********************************************************************************************/
00386 CamLaunchProcess::~CamLaunchProcess()
00387 {
00388     if (m_bConnected)
00389     {
00390         TRACEUSER("Phil", _T("Process still connected in ~CamLaunchProcess"));
00391         Disconnect();
00392     }
00393 }
00394 
00395 
00396 /********************************************************************************************
00397 
00398 >   virtual BOOL CamLaunchProcess::Execute(const wxString& cmd)
00399 
00400     Author:     Phil_Martin (Xara Group Ltd) <camelotdev@xara.com>
00401     Created:    19/May/2006
00402     Inputs:     cmd - The command string including parameters
00403     Outputs:    -
00404     Returns:    TRUE if the command was launched successfully
00405                 FALSE otherwise
00406     Purpose:    Execute a command which should launch a long-running process
00407 
00408 ********************************************************************************************/
00409 BOOL CamLaunchProcess::Execute(const wxString& cmd)
00410 {
00411     m_ReturnCode = 0;       // Assume success until we find otherwise
00412 
00413     // Make sure redirection happens
00414     Redirect();
00415 
00416     TRACEUSER("Phil", _T("Executing %s\n"), (LPCTSTR) cmd);
00417     m_pid = wxExecute(cmd, wxEXEC_ASYNC, this);
00418     if (m_pid==0)
00419     {
00420         // Couldn't even create a process for the command!
00421         m_bDead = true;
00422         return FALSE;
00423     }
00424 
00425     // We're now running
00426     m_bDead = false;
00427     m_bConnected = true;
00428 
00429     // Give the command 100 milliseconds to return an error condition or die...
00430     // After that we will either use the return code it passed back to OnTerminate
00431     // or assume it is running successfully...
00432     MonotonicTime graceperiod;
00433     while (!m_bDead && m_ReturnCode==0 && !graceperiod.Elapsed(100))
00434     {
00435         ProcessStdErr();        // Process any output on stderr
00436         wxMilliSleep(1);
00437         wxYield();
00438     }
00439 
00440     TRACEUSER("Phil", _T("Exiting with %d\n"), m_ReturnCode);
00441     return (m_ReturnCode==0);
00442 }
00443 
00444 
00445 /********************************************************************************************
00446 
00447 >   UINT32 CamLaunchProcess::Disconnect()
00448 
00449     Author:     Phil_Martin (Xara Group Ltd) <camelotdev@xara.com>
00450     Created:    19/May/2006
00451     Inputs:     -
00452     Outputs:    -
00453     Returns:    ProcessID of the process we have just allowed to live
00454     Purpose:    Allow a process to continue to run after this object is destroyed
00455 
00456 ********************************************************************************************/
00457 INT32 CamLaunchProcess::Disconnect()
00458 {
00459     INT32 pid = m_pid;
00460 
00461     m_pid = 0;
00462     m_bConnected = false;
00463     Detach();
00464 
00465     return pid;
00466 }
00467 
00468 
00469 /********************************************************************************************
00470 
00471 >   wxKillError CamLaunchProcess::Terminate()
00472 
00473     Author:     Phil_Martin (Xara Group Ltd) <camelotdev@xara.com>
00474     Created:    19/May/2006
00475     Inputs:     -
00476     Outputs:    -
00477     Returns:    wxKillError enum giving result of termination
00478     Purpose:    Kill the process
00479 
00480 ********************************************************************************************/
00481 wxKillError CamLaunchProcess::Terminate()
00482 {
00483     if (!m_bDead)
00484         return Kill(m_pid, wxSIGTERM);
00485 
00486     return wxKILL_OK;
00487 }
00488 
00489 
00490 /********************************************************************************************
00491 
00492 >   void CamLaunchProcess::ProcessStdErr()
00493 
00494     Author:     Phil_Martin (Xara Group Ltd) <camelotdev@xara.com>
00495                 Gerry_Iles (Xara Group Ltd) <camelotdev@xara.com>
00496     Created:    19/May/2006
00497     Inputs:     -
00498     Outputs:    -
00499     Returns:    -
00500     Purpose:    Process anything the process writes to the error stream
00501 
00502 ********************************************************************************************/
00503 void CamLaunchProcess::ProcessStdErr()
00504 {
00505     if (IsErrorAvailable())
00506     {
00507         wxTextInputStream tis(*GetErrorStream());
00508 
00509         // This assumes that the output is always line buffered
00510         while (!GetErrorStream()->Eof())
00511         {
00512             wxString line;
00513             line << tis.ReadLine();
00514             TRACEUSER("Phil", _T("(stderr):%s\n"), line.c_str());
00515         }
00516 
00517         m_ReturnCode = 42;      // Signal failure
00518     }
00519 }
00520 
00521 
00522 /********************************************************************************************
00523 
00524 >   void CamLaunchProcess::OnTerminate(int TYPENOTE: Correct pid, int TYPENOTE: Correct status)
00525 
00526     Author:     Phil_Martin (Xara Group Ltd) <camelotdev@xara.com>
00527     Created:    19/May/2006
00528     Inputs:     -
00529     Outputs:    -
00530     Returns:    -
00531     Purpose:    Process anything the process writes to the error stream
00532 
00533 ********************************************************************************************/
00534 void CamLaunchProcess::OnTerminate(int /*TYPENOTE: Correct*/ pid, int /*TYPENOTE: Correct*/ status)
00535 {
00536     ProcessStdErr();
00537 
00538     TRACEUSER("Phil", _T("CamProcess::OnTerminate pid = %d  status = %d\n"), pid, status);
00539     m_bDead = true;
00540     m_ReturnCode = status;
00541     m_bConnected = false;
00542 }
00543 
00544 
00545 
00546 

Generated on Sat Nov 10 03:48:10 2007 for Camelot by  doxygen 1.4.4