Process.hx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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> ) : Void
  35. {
  36. this.native = createNativeProcess(cmd, args);
  37. native.Start();
  38. this.stdout = new cs.io.NativeInput(native.StandardOutput.BaseStream);
  39. this.stderr = new cs.io.NativeInput(native.StandardError.BaseStream);
  40. this.stdin = new cs.io.NativeOutput(native.StandardInput.BaseStream);
  41. }
  42. @:allow(Sys)
  43. private static function createNativeProcess( cmd : String, ?args : Array<String> ) : NativeProcess {
  44. var native = new NativeProcess();
  45. native.StartInfo.CreateNoWindow = true;
  46. native.StartInfo.RedirectStandardError = native.StartInfo.RedirectStandardInput = native.StartInfo.RedirectStandardOutput = true;
  47. if (args != null) {
  48. // mono 4.2.1 on Windows doesn't support relative path correctly
  49. if (cmd.indexOf("/") != -1 || cmd.indexOf("\\") != -1)
  50. cmd = sys.FileSystem.fullPath(cmd);
  51. native.StartInfo.FileName = cmd;
  52. native.StartInfo.UseShellExecute = false;
  53. native.StartInfo.Arguments = buildArgumentsString(args);
  54. } else {
  55. switch (Sys.systemName()) {
  56. case "Windows":
  57. native.StartInfo.FileName = switch (Sys.getEnv("COMSPEC")) {
  58. case null: "cmd.exe";
  59. case comspec: comspec;
  60. }
  61. native.StartInfo.Arguments = '/C "$cmd"';
  62. case _:
  63. native.StartInfo.FileName = "/bin/sh";
  64. native.StartInfo.Arguments = buildArgumentsString(["-c", cmd]);
  65. }
  66. native.StartInfo.UseShellExecute = false;
  67. }
  68. return native;
  69. }
  70. private static function buildArgumentsString(args:Array<String>):String {
  71. return switch (Sys.systemName()) {
  72. case "Windows":
  73. [
  74. for (a in args)
  75. StringTools.quoteWinArg(a, false)
  76. ].join(" ");
  77. case _:
  78. // mono uses a slightly different quoting/escaping rule...
  79. // https://bugzilla.xamarin.com/show_bug.cgi?id=19296
  80. [
  81. for (arg in args) {
  82. var b = new StringBuf();
  83. b.add('"');
  84. for (i in 0...arg.length) {
  85. var c = arg.charCodeAt(i);
  86. switch (c) {
  87. case '"'.code | '\\'.code:
  88. b.addChar('\\'.code);
  89. case _: //pass
  90. }
  91. b.addChar(c);
  92. }
  93. b.add('"');
  94. b.toString();
  95. }
  96. ].join(" ");
  97. }
  98. }
  99. public function getPid() : Int
  100. {
  101. return native.Id;
  102. }
  103. public function exitCode( block : Bool = true ) : Null<Int>
  104. {
  105. if( block == false && !native.HasExited )
  106. return null;
  107. native.WaitForExit();
  108. return native.ExitCode;
  109. }
  110. public function close() : Void
  111. {
  112. native.Close();
  113. }
  114. public function kill() : Void
  115. {
  116. native.Kill();
  117. }
  118. }