Subprocess.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. //
  2. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <Atomic/IO/Log.h>
  23. #include <Atomic/Core/Timer.h>
  24. #include "Subprocess.h"
  25. #include "SubprocessSystem.h"
  26. namespace ToolCore
  27. {
  28. Subprocess::Subprocess(Context* context) :
  29. Object(context),
  30. processHandle_(NULL),
  31. inStream_(pipeOut_),
  32. errorStream_(pipeError_),
  33. returnCode_(-1)
  34. {
  35. }
  36. Subprocess::~Subprocess()
  37. {
  38. if (processHandle_)
  39. delete processHandle_;
  40. }
  41. void Subprocess::ThreadFunction()
  42. {
  43. while (shouldRun_)
  44. {
  45. int read;
  46. char data[129];
  47. if (!Poco::Process::isRunning(*processHandle_))
  48. {
  49. shouldRun_ = false;
  50. break;
  51. }
  52. inStream_.read(data, 128);
  53. read = inStream_.gcount();
  54. data[read] = 0;
  55. if (read)
  56. {
  57. mutex_.Acquire();
  58. output_ += (const char*) data;
  59. mutex_.Release();
  60. }
  61. if (inStream_.eof())
  62. {
  63. returnCode_ = processHandle_->wait();
  64. if (returnCode_)
  65. {
  66. while (true)
  67. {
  68. errorStream_.read(data, 128);
  69. read = errorStream_.gcount();
  70. data[read] = 0;
  71. if (read)
  72. {
  73. mutex_.Acquire();
  74. errorOutput_ += (const char*) data;
  75. mutex_.Release();
  76. }
  77. else
  78. {
  79. break;
  80. }
  81. }
  82. }
  83. shouldRun_ = false;
  84. break;
  85. }
  86. }
  87. }
  88. void Subprocess::ProcessOutput(SubprocessSystem* system)
  89. {
  90. String output, errorOutput;
  91. GetOutput(output, errorOutput);
  92. if (output.Length())
  93. {
  94. VariantMap eventData;
  95. eventData[SubprocessOutput::P_TEXT] = output;
  96. SendEvent(E_SUBPROCESSOUTPUT, eventData);
  97. }
  98. if (errorOutput.Length())
  99. {
  100. VariantMap eventData;
  101. eventData[SubprocessOutput::P_TEXT] = errorOutput;
  102. SendEvent(E_SUBPROCESSOUTPUT, eventData);
  103. }
  104. }
  105. bool Subprocess::Update(SubprocessSystem* system)
  106. {
  107. ProcessOutput(system);
  108. if (!shouldRun_) {
  109. ProcessOutput(system);
  110. VariantMap eventData;
  111. eventData[SubprocessComplete::P_RETCODE] = returnCode_;
  112. SendEvent(E_SUBPROCESSCOMPLETE, eventData);
  113. return false;
  114. }
  115. return true;
  116. }
  117. bool Subprocess::Launch(const String& command, const Vector<String>& args, const String& initialDirectory)
  118. {
  119. Poco::Process::Env env;
  120. return Launch(command, args, initialDirectory, env);
  121. }
  122. bool Subprocess::Launch(const String& command, const Vector<String>& args, const String& initialDirectory, const Poco::Process::Env& env)
  123. {
  124. Poco::Process::Args pargs;
  125. for (unsigned i = 0; i < args.Size(); i++)
  126. pargs.push_back(args[i].CString());
  127. std::string pcommand = command.CString();
  128. std::string pinitialDirectory = initialDirectory.CString();
  129. // this can take an ENV as well, may come in useful
  130. processHandle_ = new Poco::ProcessHandle(Poco::Process::launch(pcommand, pargs, pinitialDirectory, &pipeIn_, &pipeOut_, &pipeError_, env));
  131. if (!Poco::Process::isRunning(*processHandle_))
  132. return false;
  133. return Run();
  134. }
  135. }