OSXScriptLauncher.cxx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #include <cmsys/SystemTools.hxx>
  2. #include <cmsys/Process.h>
  3. #include <cmsys/ios/fstream>
  4. #include <cmsys/ios/iostream>
  5. #include <Carbon/Carbon.h>
  6. #include <CoreFoundation/CoreFoundation.h>
  7. // For the PATH_MAX constant
  8. #include <sys/syslimits.h>
  9. #define DebugError(x) \
  10. ofs << x << cmsys_ios::endl; \
  11. cmsys_ios::cout << x << cmsys_ios::endl
  12. int main(int argc, char* argv[])
  13. {
  14. //if ( cmsys::SystemTools::FileExists(
  15. cmsys_stl::string cwd = cmsys::SystemTools::GetCurrentWorkingDirectory();
  16. cmsys_ios::ofstream ofs("/tmp/output.txt");
  17. CFStringRef fileName;
  18. CFBundleRef appBundle;
  19. CFURLRef scriptFileURL;
  20. UInt8 *path;
  21. //get CF URL for script
  22. if (! (appBundle = CFBundleGetMainBundle()))
  23. {
  24. DebugError("Cannot get main bundle");
  25. return 1;
  26. }
  27. fileName = CFSTR("RuntimeScript");
  28. if (! (scriptFileURL = CFBundleCopyResourceURL(appBundle, fileName, NULL,
  29. NULL)))
  30. {
  31. DebugError("CFBundleCopyResourceURL failed");
  32. return 1;
  33. }
  34. //create path string
  35. if (! (path = new UInt8[PATH_MAX]))
  36. {
  37. return 1;
  38. }
  39. //get the file system path of the url as a cstring
  40. //in an encoding suitable for posix apis
  41. if ( CFURLGetFileSystemRepresentation(scriptFileURL, true, path,
  42. PATH_MAX) == false)
  43. {
  44. DebugError("CFURLGetFileSystemRepresentation failed");
  45. return 1;
  46. }
  47. //dispose of the CF variable
  48. CFRelease(scriptFileURL);
  49. cmsys_stl::string fullScriptPath = reinterpret_cast<char*>(path);
  50. delete [] path;
  51. if (! cmsys::SystemTools::FileExists(fullScriptPath.c_str()))
  52. {
  53. return 1;
  54. }
  55. cmsys_stl::string scriptDirectory = cmsys::SystemTools::GetFilenamePath(
  56. fullScriptPath);
  57. ofs << fullScriptPath.c_str() << cmsys_ios::endl;
  58. cmsys_stl::vector<const char*> args;
  59. args.push_back(fullScriptPath.c_str());
  60. int cc;
  61. for ( cc = 1; cc < argc; ++ cc )
  62. {
  63. args.push_back(argv[cc]);
  64. }
  65. args.push_back(0);
  66. cmsysProcess* cp = cmsysProcess_New();
  67. cmsysProcess_SetCommand(cp, &*args.begin());
  68. cmsysProcess_SetWorkingDirectory(cp, scriptDirectory.c_str());
  69. cmsysProcess_SetOption(cp, cmsysProcess_Option_HideWindow, 1);
  70. cmsysProcess_SetTimeout(cp, 0);
  71. cmsysProcess_Execute(cp);
  72. std::vector<char> tempOutput;
  73. char* data;
  74. int length;
  75. while(cmsysProcess_WaitForData(cp, &data, &length, 0))
  76. {
  77. // Translate NULL characters in the output into valid text.
  78. // Visual Studio 7 puts these characters in the output of its
  79. // build process.
  80. for(int i=0; i < length; ++i)
  81. {
  82. if(data[i] == '\0')
  83. {
  84. data[i] = ' ';
  85. }
  86. }
  87. cmsys_ios::cout.write(data, length);
  88. }
  89. cmsysProcess_WaitForExit(cp, 0);
  90. bool result = true;
  91. if(cmsysProcess_GetState(cp) == cmsysProcess_State_Exited)
  92. {
  93. if ( cmsysProcess_GetExitValue(cp) != 0 )
  94. {
  95. result = false;
  96. }
  97. }
  98. else if(cmsysProcess_GetState(cp) == cmsysProcess_State_Exception)
  99. {
  100. const char* exception_str = cmsysProcess_GetExceptionString(cp);
  101. std::cerr << exception_str << std::endl;
  102. result = false;
  103. }
  104. else if(cmsysProcess_GetState(cp) == cmsysProcess_State_Error)
  105. {
  106. const char* error_str = cmsysProcess_GetErrorString(cp);
  107. std::cerr << error_str << std::endl;
  108. result = false;
  109. }
  110. else if(cmsysProcess_GetState(cp) == cmsysProcess_State_Expired)
  111. {
  112. const char* error_str = "Process terminated due to timeout\n";
  113. std::cerr << error_str << std::endl;
  114. result = false;
  115. }
  116. cmsysProcess_Delete(cp);
  117. return 0;
  118. }