Blame view

GUI/SW2/SRC/src/base/PrcBase.cpp 14.1 KB
886c558b   Steve Greedy   SACAMOS Public Re...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
//**************************************************************************************************
//                                           PrcBase.cpp                                           *
//                                          -------------                                          *
// Started     : 2004-01-29                                                                        *
// Last Update : 2016-09-30                                                                        *
// Copyright   : (C) 2004-2016 MSWaters                                                            *
//**************************************************************************************************

//**************************************************************************************************
//                                                                                                 *
//      This program is free software; you can redistribute it and/or modify it under the          *
//      terms of the GNU General Public License as published by the Free Software Foundation;      *
//      either version 3 of the License, or (at your option) any later version.                    *
//                                                                                                 *
//**************************************************************************************************

#include "PrcBase.hpp"

//**************************************************************************************************
// Constructor.

PrcBase::PrcBase( int iFlags ) : wxProcess( iFlags )
{
  m_oNameLog  = DEF_LOG_FILE;  // Set the default log file path and name
  m_osErrMsg  = wxT("");       // Clear the error message
  m_iPid      = -1;            // Clear the process ID number
  m_iExitCode = 0;             // Clear the process exit code
}

//**************************************************************************************************
// Destructor.

PrcBase::~PrcBase( )
{
  // Delete the process log file
  bDelLogFile( );
}

//**************************************************************************************************
// Find a binary using the user's PATH environment variable given the binary name.
//
// Argument List :
//   ofnBin - The binary name without the path
//
// Return Values :
//   true  - Success (path prepended to oFnBinary)
//   false - Failure

bool  PrcBase::bFindBinary( wxFileName & roFileBin )
{
  wxPathList  opl1;
  wxFileName  ofn1;
  wxString    os1, os2, os3;

  m_osErrMsg = wxT("");

  // Initial checks
  if( ! roFileBin.IsOk( ) ) return( false );

  // Search environment variable PATH for the first occurrence of the binary
  opl1.AddEnvList( wxT("PATH") );
  ofn1 = opl1.FindAbsoluteValidPath( roFileBin.GetFullName( ) );

  // Check whether the binary was successfully found
  if( !ofn1.IsOk( ) || !ofn1.FileExists( ) )
  {
    os1 << wxT("Can't find the binary : ") << m_oNameBin.GetFullName( );
    wxGetEnv( wxT("PATH"), &os2 );
    os2.Prepend( wxT("$PATH = \n") );

    // Set the object error message
    os3 = os1 + wxT("\n\n") + os2;
    SetErrMsg( os3 );

    // If debug mode is enabled send the error message to the console
    if( g_bDebug )
    {
      os3 = os1 + wxT(" (") + os2 + wxT(").");
      std::cerr << "DEBUG : " << rosStrToLine( os3 ).mb_str( ) << "\n\n";
    }

    return( false );
  }

  m_oNameBin = ofn1;

  return( true );
}

//**************************************************************************************************
// Get the console output after a process has been initiated and record it in the log file.
//
// Return Values :
//   true  - Success
//   false - Failure

bool  PrcBase::bLogOutput( void )
{
  wxOutputStream * poStdOut;
  wxInputStream  * poStdIn;
  wxInputStream  * poStdErr;
  wxString         os1, os2;
  wxChar           oc1;

  // Check that the log file name is valid
  if( ! m_oNameLog.IsOk( ) )        return( false );

  // Open the file
  wxTextFile  oFileLog( m_oNameLog.GetFullPath( ) );
  if( oFileLog.Exists( ) )
       { if( ! oFileLog.Open( )   ) return( false ); }
  else { if( ! oFileLog.Create( ) ) return( false ); }

  // Clear the file if it contains lines
  oFileLog.Clear( );

  // Get pointers to the input streams
  poStdOut = GetOutputStream( );
  poStdIn  = GetInputStream( );
  poStdErr = GetErrorStream( );

  // Read the console input
  while( bIsExec( ) || poStdIn->IsOk( ) || poStdErr->IsOk( ) )
  {
    // Get a line of data from stdout
    while( poStdIn->CanRead( ) )
    {
      oc1 = poStdIn->GetC( );
      if( oc1==wxT('\t') || (oc1>31 && oc1!=127) ) os1 << oc1;
      if( oc1 == wxT('\n') ) { oFileLog.AddLine( os1 ); os1 = wxT(""); }
    }

    // Get a line of data from stderr
    while( poStdErr->CanRead( ) )
    {
      oc1 = poStdErr->GetC( );
      if( oc1==wxT('\t') || (oc1>31 && oc1!=127) ) os2 << oc1;
      if( oc1 == wxT('\n') ) { oFileLog.AddLine( os2 ); os2 = wxT(""); }
    }

    wxTheApp->Yield( true );  // Yield CPU to other processes
  }

  // Delete the stream objects
  delete poStdOut;
  delete poStdIn;
  delete poStdErr;
  SetPipeStreams( NULL, NULL, NULL );

  if( ! os1.IsEmpty( ) ) oFileLog.AddLine( os1 );
  if( ! os2.IsEmpty( ) ) oFileLog.AddLine( os2 );

  oFileLog.Write( ); // Save the changes to disk
  oFileLog.Close( ); // Close the log file

  return( true );
}

//**************************************************************************************************
// Does the binary exist?
//
// Return Values :
//   true  - The binary does    exist
//   false - The binary doesn't exist

bool  PrcBase::bBinExists( void )
{
  if( !m_oNameBin.IsOk( ) || !m_oNameBin.FileExists( ) )
    return( false );

  return( true );
}

//**************************************************************************************************
// Set the file name of the binary to be executed.
//
// Argument List :
//   rosFileName - A string containing the binary file name
//
// Return Values :
//   true  - Success
//   false - Failure

bool  PrcBase::bSetBinFile( const wxString & rosFileName )
{
  if( rosFileName.IsEmpty( ) )      return( false );

  m_oNameBin = rosFileName;

  if( ! bFindBinary( m_oNameBin ) ) return( false );

  return( true );
}

//**************************************************************************************************
// Set the process log file name.
// (If set all process output can be captured to this file.)
//
// Argument List :
//   rosFileName - A string containing the full path and file name
//
// Return Values :
//   true  - Success
//   false - Failure

bool  PrcBase::bSetLogFile( const wxString & rosFileName )
{
  wxFileName  ofn1;

  ofn1 = rosFileName;

  if( ! ofn1.IsOk( ) ) return( false );

  if( ofn1.GetPath( ).IsEmpty( ) ) ofn1.SetPath( wxT(".") );

  m_oNameLog = ofn1;

  return( true );
}

//**************************************************************************************************
// Set the argument list to be appended to the binary name.
//
// Argument List :
//   rosArgLst - A string containing the argument list
//
// Return Values :
//   true  - Success
//   false - Failure

bool  PrcBase::bSetArgLst( const wxString & rosArgLst )
{
  if( rosArgLst.IsEmpty( ) ) return( false );

  m_osArgLst = rosArgLst;

  return( true );
}

//**************************************************************************************************
// Execute a process asynchronously ie. return as soon as the process has been launched.
//
// Return Values :
//   true  - Success
//   false - Failure

bool  PrcBase::bExecAsync( void )
{
  wxString  os1, os2;

  // Only execute simulation if it isn't already running
  if( bIsExec( ) ) return( false );

  // Clear error attributes
  m_osErrMsg = wxT("");

  // Check that the binary exists
  if( ! bBinExists( ) )
    if( ! bFindBinary( m_oNameBin ) )
      return( false );

  // Construct the command line to be executed
  os1 = roGetBinFile( ).GetFullPath( );
  if( ! rosGetArgLst( ).IsEmpty( ) )
    os1 << wxT(' ') << rosGetArgLst( );

  // Attempt to execute the simulation
  m_iPid = (int) wxExecute( os1, wxEXEC_ASYNC, this );
  if( m_iPid <= 0 )
  {
    os2.Empty( );
    os2 << wxT("Couldn't start process : ") << os1;
    SetErrMsg( os2 );
    return( false );
  }

  return( true );
}

//**************************************************************************************************
// Execute a process synchronously ie. wait for the process to terminate before returning.
//
// Return Values :
//   true  - Success
//   false - Failure

bool  PrcBase::bExecSync( void )
{
  // Not yet implemented ??? 12/03/2010

  return( false );

//  long  liRtn;

//  wxEnableTopLevelWindows( false );
//  liRtn = wxExecute( rosCmd, wxEXEC_SYNC );
//  wxEnableTopLevelWindows( true );
//  if( liRtn != 0 ) return( false );

//  return( true );

// This is how I'd like to do it but it doesn't work 20/11/2003 ???
//  wxEnableTopLevelWindows( false );
//  int iRtn = (int) wxExecute( osCmd, wxEXEC_SYNC );
//  wxEnableTopLevelWindows( true );

//  wxProcess  oProcess( wxPROCESS_DEFAULT );
//  iPid = (int) wxExecute( osCmd, wxEXEC_SYNC );

//  if( iPid == 0 )
//  { // Error gnetlist could not be executed
//    cerr << "The command:\n   " << osCmd << "\ncould not be executed.";
//    return( false );
//  }

//  for( ui1=0; ui1<300; ui1++ )
//  { // Wait up to 30 seconds for the process to complete
//cerr << iPid << '\n';
//    wxUsleep( 100 ); // Sleep for 100msec
//    if( ! oProcess.Exists( iPid ) ) break;
//  }
//  if( oProcess.Exists( iPid ) )
//  { // Error gnetlist had to be terminated prematurely
//    oProcess.Kill( iPid );
//    cerr << "The command:\n   " << osCmd << "\ntook more than 30 sec. to "
//         << "execute and so was terminated prematurely.";
//    return( false );
//  }
}

//**************************************************************************************************
// Stop the simulation currently in progress.
//
// Return Values :
//   true  - Success
//   false - Failure

bool  PrcBase::bKill( void )
{
  if( ! bIsExec( ) )                           return( true );
  if( ! Exists( m_iPid ) )                     return( true );

  if( Kill( m_iPid, wxSIGTERM ) != wxKILL_OK ) return( false );

  m_iPid      = -1;
  m_iExitCode = 0;

  return( true );
}

//**************************************************************************************************
// Remove the log file.
//
// Return Values :
//   Success - true
//   Failure - false

bool  PrcBase::bDelLogFile( void )
{
  if( m_oNameLog.GetFullPath( ).IsEmpty( ) )          return( true );

  if( ! ::wxFileExists( m_oNameLog.GetFullPath( ) ) ) return( true );

  return( ::wxRemoveFile( m_oNameLog.GetFullPath( ) ) );
}

//**************************************************************************************************
// Collect the output from the simulator and print to a text control.
//
// Argument List :
//   roTxtCtl - The text control to contain the simulator output

void  PrcBase::Print( TextCtrl & roTxtCtl )
{
  roTxtCtl.bClear( );    // Clear the text control

  PrintCmd( roTxtCtl );  // Print the process command

  PrintRsp( roTxtCtl );  // Print the process command response
}

//**************************************************************************************************
// Print the command envoking the process to a text control.
//
// Argument List :
//   roTxtCtl - The text control to contain process input

void  PrcBase::PrintCmd( TextCtrl & roTxtCtl )
{
  wxString  os1;

  // Print the process command
  os1.Empty( );
  os1 << wxT("                    *************** PROCESS COMMAND ***************");
  roTxtCtl.bAppendLine( os1 );
  os1.Empty( );
  roTxtCtl.bAppendLine( os1 );
  os1 << roGetBinFile( ).GetFullPath( );
  if( ! rosGetArgLst( ).IsEmpty( ) ) os1 << wxT(' ') << rosGetArgLst( );
  roTxtCtl.bAppendLine( os1 );
  os1.Empty( );
  roTxtCtl.bAppendLine( os1 );
}

//**************************************************************************************************
// Collect the output from the process and print it to a text control.
//
// Argument List :
//   roTxtCtl - The text control to contain the simulator output

void  PrcBase::PrintRsp( TextCtrl & roTxtCtl )
{
  wxString  os1;

  // Print the process command response
  if( ! m_oNameLog.GetFullPath( ).IsEmpty( ) )
  {
    os1.Empty( );
    os1 << wxT("                    *************** PROCESS RESPONSE **************");
    roTxtCtl.bAppendLine( os1 );
    os1.Empty( );
    roTxtCtl.bAppendLine( os1 );
    if( m_oNameLog.IsOk( ) && m_oNameLog.FileExists( ) )
      roTxtCtl.bAppendFile( m_oNameLog.GetFullPath( ) );
    else
    {
      os1 << wxT("Couldn't load the file containing the process ouput : ")
          << m_oNameLog.GetFullPath( );
      roTxtCtl.bAppendLine( os1 );
    }
  }
}

//**************************************************************************************************
// Print the object attributes.
//
// Argument List :
//   rosPrefix - A prefix to every line displayed (usually just spaces)

void  PrcBase::Print( const wxString & rosPrefix )
{
  std::cout << rosPrefix.mb_str() << "m_oNameBin  : " << m_oNameBin.GetFullPath( ).mb_str() << '\n';
  std::cout << rosPrefix.mb_str() << "m_oNameLog  : " << m_oNameLog.GetFullPath( ).mb_str() << '\n';
  std::cout << rosPrefix.mb_str() << "m_osArgLst  : " << m_osArgLst.mb_str( )               << '\n';
  std::cout << rosPrefix.mb_str() << "m_osErrMsg  : " << m_osErrMsg.mb_str( )               << '\n';
  std::cout << rosPrefix.mb_str() << "m_iPid      : " << m_iPid                             << '\n';
  std::cout << rosPrefix.mb_str() << "m_iExitCode : " << m_iExitCode                        << '\n';
}

//**************************************************************************************************
// Call-back function called when the process terminates.
// (WARNING : Be careful of this function definition, if it's incorrect the application can throw a
//            segmentation fault.)
//
// Argument List :
//   iPid    - The PID of the process which just terminated
//   iStatus - The exit code of the process

void  PrcBase::OnTerminate( int iPid, int iStatus )
{
  if( iPid == m_iPid )
  {
    m_iPid      = -1;
    m_iExitCode = iStatus;
  }
}

//**************************************************************************************************