cmProcess.cxx 8.11 KB
Newer Older
Bill Hoffman's avatar
Bill Hoffman committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*=========================================================================

  Program:   CMake - Cross-Platform Makefile Generator
  Module:    $RCSfile$
  Language:  C++
  Date:      $Date$
  Version:   $Revision$

  Copyright (c) 2002 Kitware, Inc., Insight Consortium.  All rights reserved.
  See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/

#include <cmProcess.h>
19
#include <cmSystemTools.h>
Bill Hoffman's avatar
Bill Hoffman committed
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

cmProcess::cmProcess()
{
  this->Process = 0;
  this->Timeout = 0;
}

cmProcess::~cmProcess()
{
  cmsysProcess_Delete(this->Process);
}
void cmProcess::SetCommand(const char* command)
{
  this->Command = command;
}

void cmProcess::SetCommandArguments(std::vector<std::string> const& args)
{
  this->Arguments = args;
}

bool cmProcess::StartProcess()
{
  if(this->Command.size() == 0)
    {
    return false;
    }
47
  this->StartTime = cmSystemTools::GetTime();
Bill Hoffman's avatar
Bill Hoffman committed
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
  this->ProcessArgs.clear();
  // put the command as arg0
  this->ProcessArgs.push_back(this->Command.c_str());
  // now put the command arguments in
  for(std::vector<std::string>::iterator i = this->Arguments.begin();
      i != this->Arguments.end(); ++i)
    {
    this->ProcessArgs.push_back(i->c_str());
    }
  this->ProcessArgs.push_back(0); // null terminate the list
  this->Process = cmsysProcess_New();
  cmsysProcess_SetCommand(this->Process, &*this->ProcessArgs.begin());
  if(this->WorkingDirectory.size())
    {
    cmsysProcess_SetWorkingDirectory(this->Process,
                                     this->WorkingDirectory.c_str());
    }
  cmsysProcess_SetOption(this->Process, cmsysProcess_Option_HideWindow, 1);
  cmsysProcess_SetTimeout(this->Process, this->Timeout);
  cmsysProcess_Execute(this->Process);
  return (cmsysProcess_GetState(this->Process)
          == cmsysProcess_State_Executing);
}
71
72

bool cmProcess::GetNextOutputLine(std::string& stdOutLine,
Bill Hoffman's avatar
Bill Hoffman committed
73
74
                            std::string& stdErrLine)
{
75
76
77
78
  if(this->StdErrorBuffer.empty() && this->StdOutBuffer.empty())
    {
    return false;
    }
Bill Hoffman's avatar
Bill Hoffman committed
79
80
81
82
83
84
  stdOutLine = "";
  stdErrLine = "";
  std::vector<char>::iterator outiter = 
    this->StdOutBuffer.begin();
  std::vector<char>::iterator erriter = 
    this->StdErrorBuffer.begin();
85
86
  // Check for a newline in stdout.
  for(;outiter != this->StdOutBuffer.end(); ++outiter)
Bill Hoffman's avatar
Bill Hoffman committed
87
    {
88
    if((*outiter == '\r') && ((outiter+1) == this->StdOutBuffer.end()))
Bill Hoffman's avatar
Bill Hoffman committed
89
      {
90
91
92
93
94
95
      break;
      }
    else if(*outiter == '\n' || *outiter == '\0')
      {
      int length = outiter-this->StdOutBuffer.begin();
      if(length > 1 && *(outiter-1) == '\r')
Bill Hoffman's avatar
Bill Hoffman committed
96
        {
97
        --length;
Bill Hoffman's avatar
Bill Hoffman committed
98
        }
99
      if(length > 0)
Bill Hoffman's avatar
Bill Hoffman committed
100
        {
101
        stdOutLine.append(&this->StdOutBuffer[0], length);
Bill Hoffman's avatar
Bill Hoffman committed
102
        }
103
104
105
      this->StdOutBuffer.erase(this->StdOutBuffer.begin(), outiter+1);
      this->LastOutputPipe = cmsysProcess_Pipe_STDOUT;
      return true;
Bill Hoffman's avatar
Bill Hoffman committed
106
      }
107
    }
Bill Hoffman's avatar
Bill Hoffman committed
108

109
110
111
112
  // Check for a newline in stderr.
  for(;erriter != this->StdErrorBuffer.end(); ++erriter)
    {
    if((*erriter == '\r') && ((erriter+1) == this->StdErrorBuffer.end()))
Bill Hoffman's avatar
Bill Hoffman committed
113
      {
114
115
116
117
118
119
      break;
      }
    else if(*erriter == '\n' || *erriter == '\0')
      {
      int length = erriter-this->StdErrorBuffer.begin();
      if(length > 1 && *(erriter-1) == '\r')
Bill Hoffman's avatar
Bill Hoffman committed
120
        {
121
        --length;
Bill Hoffman's avatar
Bill Hoffman committed
122
        }
123
      if(length > 0)
Bill Hoffman's avatar
Bill Hoffman committed
124
        {
125
        stdErrLine.append(&this->StdErrorBuffer[0], length);
Bill Hoffman's avatar
Bill Hoffman committed
126
        }
127
128
129
      this->StdErrorBuffer.erase(this->StdErrorBuffer.begin(), erriter+1);
      this->LastOutputPipe = cmsysProcess_Pipe_STDERR;
      return true;
Bill Hoffman's avatar
Bill Hoffman committed
130
      }
131
132
133
134
135
136
137
138
139
140
141
    }
  //If we get here, we have stuff waiting in the buffers, but no newline
  return false;
}
// return true if there is a new line of data
// return false if there is no new data
int cmProcess::CheckOutput(double timeout)
{
  // Wait for data from the process.
  int length;
  char* data;
Bill Hoffman's avatar
Bill Hoffman committed
142

143
144
  while(1)
    {
Bill Hoffman's avatar
Bill Hoffman committed
145
146
147
148
149
150
151
152
153
154
    int pipe = cmsysProcess_WaitForData(this->Process, &data, 
                                        &length, &timeout);
    if(pipe == cmsysProcess_Pipe_Timeout)
      {
      // Timeout has been exceeded.
      this->LastOutputPipe = pipe;
      return pipe;
      }
    else if(pipe == cmsysProcess_Pipe_STDOUT)
      {
Zach's avatar
Zach committed
155
        // Append to the stdout buffer.
Bill Hoffman's avatar
Bill Hoffman committed
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
      this->StdOutBuffer.insert(this->StdOutBuffer.end(), data, data+length);
      }
    else if(pipe == cmsysProcess_Pipe_STDERR)
      {
      // Append to the stderr buffer.
      this->StdErrorBuffer.insert(this->StdErrorBuffer.end(),
                                  data, data+length);
      }
    else if(pipe == cmsysProcess_Pipe_None)
      {
      // Both stdout and stderr pipes have broken.  Return leftover data.
      if(!this->StdOutBuffer.empty())
        {
        this->LastOutputPipe = cmsysProcess_Pipe_STDOUT;
        return this->LastOutputPipe;
        }
      else if(!this->StdErrorBuffer.empty())
        {
        this->LastOutputPipe = cmsysProcess_Pipe_STDERR;
        return this->LastOutputPipe;
        }
      else
        {
        this->LastOutputPipe = cmsysProcess_Pipe_None;
        return this->LastOutputPipe;
        }
      }
    }
}

// return the process status
int cmProcess::GetProcessStatus()
{
  if(!this->Process)
    {
    return cmsysProcess_State_Exited;
    }
  return cmsysProcess_GetState(this->Process);
}

// return true if the process is running
bool cmProcess::IsRunning()
{
  int status = this->GetProcessStatus();
  if(status == cmsysProcess_State_Executing )
    {
    if(this->LastOutputPipe != 0)
      {
      return true;
      }
    }
  // if the process is done, then wait for it to exit
  cmsysProcess_WaitForExit(this->Process, 0);
  this->ExitValue = cmsysProcess_GetExitValue(this->Process);
210
  this->TotalTime = cmSystemTools::GetTime() - this->StartTime;
211
//  std::cerr << "Time to run: " << this->TotalTime << "\n";
Bill Hoffman's avatar
Bill Hoffman committed
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
  return false;
}


int cmProcess::ReportStatus()
{
  int result = 1;
  switch(cmsysProcess_GetState(this->Process))
    {
    case cmsysProcess_State_Starting:
      {
      std::cerr << "cmProcess: Never started " 
           << this->Command << " process.\n";
      } break;
    case cmsysProcess_State_Error:
      {
Bill Hoffman's avatar
Bill Hoffman committed
228
229
230
231
      std::cerr << "cmProcess: Error executing " << this->Command 
                << " process: "
                << cmsysProcess_GetErrorString(this->Process)
                << "\n";
Bill Hoffman's avatar
Bill Hoffman committed
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
      } break;
    case cmsysProcess_State_Exception:
      {
      std::cerr << "cmProcess: " << this->Command
                      << " process exited with an exception: ";
      switch(cmsysProcess_GetExitException(this->Process))
        {
        case cmsysProcess_Exception_None:
          {
          std::cerr << "None";
          } break;
        case cmsysProcess_Exception_Fault:
          {
          std::cerr << "Segmentation fault";
          } break;
        case cmsysProcess_Exception_Illegal:
          {
          std::cerr << "Illegal instruction";
          } break;
        case cmsysProcess_Exception_Interrupt:
          {
          std::cerr << "Interrupted by user";
          } break;
        case cmsysProcess_Exception_Numerical:
          {
          std::cerr << "Numerical exception";
          } break;
        case cmsysProcess_Exception_Other:
          {
          std::cerr << "Unknown";
          } break;
        }
      std::cerr << "\n";
      } break;
    case cmsysProcess_State_Executing:
      {
Bill Hoffman's avatar
Bill Hoffman committed
268
269
      std::cerr << "cmProcess: Never terminated " << 
        this->Command << " process.\n";
Bill Hoffman's avatar
Bill Hoffman committed
270
271
272
273
      } break;
    case cmsysProcess_State_Exited:
      {
      result = cmsysProcess_GetExitValue(this->Process);
Bill Hoffman's avatar
Bill Hoffman committed
274
275
276
      std::cerr << "cmProcess: " << this->Command 
                << " process exited with code "
                << result << "\n";
Bill Hoffman's avatar
Bill Hoffman committed
277
278
279
      } break;
    case cmsysProcess_State_Expired:
      {
Bill Hoffman's avatar
Bill Hoffman committed
280
281
      std::cerr << "cmProcess: killed " << this->Command 
                << " process due to timeout.\n";
Bill Hoffman's avatar
Bill Hoffman committed
282
283
284
285
286
287
288
289
290
      } break;
    case cmsysProcess_State_Killed:
      {
      std::cerr << "cmProcess: killed " << this->Command << " process.\n";
      } break;
    }
  return result;

}