Process.hx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Copyright (C)2005-2017 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is 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
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. package sys.io;
  23. import haxe.io.BytesInput;
  24. import cs.system.io.StreamReader;
  25. import cs.system.io.StreamWriter;
  26. import cs.system.diagnostics.Process as NativeProcess;
  27. import cs.system.diagnostics.ProcessStartInfo as NativeStartInfo;
  28. @:coreApi
  29. class Process {
  30. public var stdout(default,null) : haxe.io.Input;
  31. public var stderr(default,null) : haxe.io.Input;
  32. public var stdin(default, null) : haxe.io.Output;
  33. private var native:NativeProcess;
  34. public function new( cmd : String, ?args : Array<String>, ?detached : Bool ) : Void
  35. {
  36. if( detached ) throw "Detached process is not supported on this platform";
  37. this.native = createNativeProcess(cmd, args);
  38. native.Start();
  39. this.stdout = new cs.io.NativeInput(native.StandardOutput.BaseStream);
  40. this.stderr = new cs.io.NativeInput(native.StandardError.BaseStream);
  41. this.stdin = new cs.io.NativeOutput(native.StandardInput.BaseStream);
  42. }
  43. @:allow(Sys)
  44. private static function createNativeProcess( cmd : String, ?args : Array<String> ) : NativeProcess {
  45. var native = new NativeProcess();
  46. native.StartInfo.CreateNoWindow = true;
  47. native.StartInfo.RedirectStandardError = native.StartInfo.RedirectStandardInput = native.StartInfo.RedirectStandardOutput = true;
  48. if (args != null) {
  49. // mono 4.2.1 on Windows doesn't support relative path correctly
  50. if (cmd.indexOf("/") != -1 || cmd.indexOf("\\") != -1)
  51. cmd = sys.FileSystem.fullPath(cmd);
  52. native.StartInfo.FileName = cmd;
  53. native.StartInfo.UseShellExecute = false;
  54. native.StartInfo.Arguments = buildArgumentsString(args);
  55. } else {
  56. switch (Sys.systemName()) {
  57. case "Windows":
  58. native.StartInfo.FileName = switch (Sys.getEnv("COMSPEC")) {
  59. case null: "cmd.exe";
  60. case comspec: comspec;
  61. }
  62. native.StartInfo.Arguments = '/C "$cmd"';
  63. case _:
  64. native.StartInfo.FileName = "/bin/sh";
  65. native.StartInfo.Arguments = buildArgumentsString(["-c", cmd]);
  66. }
  67. native.StartInfo.UseShellExecute = false;
  68. }
  69. return native;
  70. }
  71. private static function buildArgumentsString(args:Array<String>):String {
  72. return switch (Sys.systemName()) {
  73. case "Windows":
  74. [
  75. for (a in args)
  76. StringTools.quoteWinArg(a, false)
  77. ].join(" ");
  78. case _:
  79. // mono uses a slightly different quoting/escaping rule...
  80. // https://bugzilla.xamarin.com/show_bug.cgi?id=19296
  81. [
  82. for (arg in args) {
  83. var b = new StringBuf();
  84. b.add('"');
  85. for (i in 0...arg.length) {
  86. var c = arg.charCodeAt(i);
  87. switch (c) {
  88. case '"'.code | '\\'.code:
  89. b.addChar('\\'.code);
  90. case _: //pass
  91. }
  92. b.addChar(c);
  93. }
  94. b.add('"');
  95. b.toString();
  96. }
  97. ].join(" ");
  98. }
  99. }
  100. public function getPid() : Int
  101. {
  102. return native.Id;
  103. }
  104. public function exitCode( block : Bool = true ) : Null<Int>
  105. {
  106. if( block == false && !native.HasExited )
  107. return null;
  108. native.WaitForExit();
  109. return native.ExitCode;
  110. }
  111. public function close() : Void
  112. {
  113. native.Close();
  114. }
  115. public function kill() : Void
  116. {
  117. native.Kill();
  118. }
  119. }