cmProcess.cxx 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include <cmProcess.h>
  11. #include <cmSystemTools.h>
  12. cmProcess::cmProcess()
  13. {
  14. this->Process = 0;
  15. this->Timeout = 0;
  16. this->TotalTime = 0;
  17. this->ExitValue = 0;
  18. this->Id = 0;
  19. this->StartTime = 0;
  20. }
  21. cmProcess::~cmProcess()
  22. {
  23. cmsysProcess_Delete(this->Process);
  24. }
  25. void cmProcess::SetCommand(const char* command)
  26. {
  27. this->Command = command;
  28. }
  29. void cmProcess::SetCommandArguments(std::vector<std::string> const& args)
  30. {
  31. this->Arguments = args;
  32. }
  33. bool cmProcess::StartProcess()
  34. {
  35. if(this->Command.size() == 0)
  36. {
  37. return false;
  38. }
  39. this->StartTime = cmSystemTools::GetTime();
  40. this->ProcessArgs.clear();
  41. // put the command as arg0
  42. this->ProcessArgs.push_back(this->Command.c_str());
  43. // now put the command arguments in
  44. for(std::vector<std::string>::iterator i = this->Arguments.begin();
  45. i != this->Arguments.end(); ++i)
  46. {
  47. this->ProcessArgs.push_back(i->c_str());
  48. }
  49. this->ProcessArgs.push_back(0); // null terminate the list
  50. this->Process = cmsysProcess_New();
  51. cmsysProcess_SetCommand(this->Process, &*this->ProcessArgs.begin());
  52. if(this->WorkingDirectory.size())
  53. {
  54. cmsysProcess_SetWorkingDirectory(this->Process,
  55. this->WorkingDirectory.c_str());
  56. }
  57. cmsysProcess_SetOption(this->Process, cmsysProcess_Option_HideWindow, 1);
  58. cmsysProcess_SetTimeout(this->Process, this->Timeout);
  59. cmsysProcess_Execute(this->Process);
  60. return (cmsysProcess_GetState(this->Process)
  61. == cmsysProcess_State_Executing);
  62. }
  63. //----------------------------------------------------------------------------
  64. bool cmProcess::Buffer::GetLine(std::string& line)
  65. {
  66. // Scan for the next newline.
  67. for(size_type sz = this->size(); this->Last != sz; ++this->Last)
  68. {
  69. if((*this)[this->Last] == '\n' || (*this)[this->Last] == '\0')
  70. {
  71. // Extract the range first..last as a line.
  72. const char* text = &*this->begin() + this->First;
  73. size_type length = this->Last - this->First;
  74. length -= (length && text[length-1] == '\r')? 1:0;
  75. line.assign(text, length);
  76. // Start a new range for the next line.
  77. ++this->Last;
  78. this->First = Last;
  79. // Return the line extracted.
  80. return true;
  81. }
  82. }
  83. // Available data have been exhausted without a newline.
  84. if(this->First != 0)
  85. {
  86. // Move the partial line to the beginning of the buffer.
  87. this->erase(this->begin(), this->begin() + this->First);
  88. this->First = 0;
  89. this->Last = this->size();
  90. }
  91. return false;
  92. }
  93. //----------------------------------------------------------------------------
  94. bool cmProcess::Buffer::GetLast(std::string& line)
  95. {
  96. // Return the partial last line, if any.
  97. if(!this->empty())
  98. {
  99. line.assign(&*this->begin(), this->size());
  100. this->First = this->Last = 0;
  101. this->clear();
  102. return true;
  103. }
  104. return false;
  105. }
  106. //----------------------------------------------------------------------------
  107. int cmProcess::GetNextOutputLine(std::string& line, double timeout)
  108. {
  109. for(;;)
  110. {
  111. // Look for lines already buffered.
  112. if(this->StdOut.GetLine(line))
  113. {
  114. return cmsysProcess_Pipe_STDOUT;
  115. }
  116. else if(this->StdErr.GetLine(line))
  117. {
  118. return cmsysProcess_Pipe_STDERR;
  119. }
  120. // Check for more data from the process.
  121. char* data;
  122. int length;
  123. int p = cmsysProcess_WaitForData(this->Process, &data, &length, &timeout);
  124. if(p == cmsysProcess_Pipe_Timeout)
  125. {
  126. return cmsysProcess_Pipe_Timeout;
  127. }
  128. else if(p == cmsysProcess_Pipe_STDOUT)
  129. {
  130. this->StdOut.insert(this->StdOut.end(), data, data+length);
  131. }
  132. else if(p == cmsysProcess_Pipe_STDERR)
  133. {
  134. this->StdErr.insert(this->StdErr.end(), data, data+length);
  135. }
  136. else // p == cmsysProcess_Pipe_None
  137. {
  138. // The process will provide no more data.
  139. break;
  140. }
  141. }
  142. // Look for partial last lines.
  143. if(this->StdOut.GetLast(line))
  144. {
  145. return cmsysProcess_Pipe_STDOUT;
  146. }
  147. else if(this->StdErr.GetLast(line))
  148. {
  149. return cmsysProcess_Pipe_STDERR;
  150. }
  151. // No more data. Wait for process exit.
  152. if(!cmsysProcess_WaitForExit(this->Process, &timeout))
  153. {
  154. return cmsysProcess_Pipe_Timeout;
  155. }
  156. // Record exit information.
  157. this->ExitValue = cmsysProcess_GetExitValue(this->Process);
  158. this->TotalTime = cmSystemTools::GetTime() - this->StartTime;
  159. // std::cerr << "Time to run: " << this->TotalTime << "\n";
  160. return cmsysProcess_Pipe_None;
  161. }
  162. // return the process status
  163. int cmProcess::GetProcessStatus()
  164. {
  165. if(!this->Process)
  166. {
  167. return cmsysProcess_State_Exited;
  168. }
  169. return cmsysProcess_GetState(this->Process);
  170. }
  171. int cmProcess::ReportStatus()
  172. {
  173. int result = 1;
  174. switch(cmsysProcess_GetState(this->Process))
  175. {
  176. case cmsysProcess_State_Starting:
  177. {
  178. std::cerr << "cmProcess: Never started "
  179. << this->Command << " process.\n";
  180. } break;
  181. case cmsysProcess_State_Error:
  182. {
  183. std::cerr << "cmProcess: Error executing " << this->Command
  184. << " process: "
  185. << cmsysProcess_GetErrorString(this->Process)
  186. << "\n";
  187. } break;
  188. case cmsysProcess_State_Exception:
  189. {
  190. std::cerr << "cmProcess: " << this->Command
  191. << " process exited with an exception: ";
  192. switch(cmsysProcess_GetExitException(this->Process))
  193. {
  194. case cmsysProcess_Exception_None:
  195. {
  196. std::cerr << "None";
  197. } break;
  198. case cmsysProcess_Exception_Fault:
  199. {
  200. std::cerr << "Segmentation fault";
  201. } break;
  202. case cmsysProcess_Exception_Illegal:
  203. {
  204. std::cerr << "Illegal instruction";
  205. } break;
  206. case cmsysProcess_Exception_Interrupt:
  207. {
  208. std::cerr << "Interrupted by user";
  209. } break;
  210. case cmsysProcess_Exception_Numerical:
  211. {
  212. std::cerr << "Numerical exception";
  213. } break;
  214. case cmsysProcess_Exception_Other:
  215. {
  216. std::cerr << "Unknown";
  217. } break;
  218. }
  219. std::cerr << "\n";
  220. } break;
  221. case cmsysProcess_State_Executing:
  222. {
  223. std::cerr << "cmProcess: Never terminated " <<
  224. this->Command << " process.\n";
  225. } break;
  226. case cmsysProcess_State_Exited:
  227. {
  228. result = cmsysProcess_GetExitValue(this->Process);
  229. std::cerr << "cmProcess: " << this->Command
  230. << " process exited with code "
  231. << result << "\n";
  232. } break;
  233. case cmsysProcess_State_Expired:
  234. {
  235. std::cerr << "cmProcess: killed " << this->Command
  236. << " process due to timeout.\n";
  237. } break;
  238. case cmsysProcess_State_Killed:
  239. {
  240. std::cerr << "cmProcess: killed " << this->Command << " process.\n";
  241. } break;
  242. }
  243. return result;
  244. }