2
0

Process.hx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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.Bytes;
  24. import haxe.io.BytesInput;
  25. import haxe.io.Eof;
  26. import java.io.IOException;
  27. import java.io.EOFException;
  28. import java.NativeArray;
  29. @:coreApi
  30. class Process {
  31. public var stdout(default,null) : haxe.io.Input;
  32. public var stderr(default,null) : haxe.io.Input;
  33. public var stdin(default, null) : haxe.io.Output;
  34. private var proc:java.lang.Process;
  35. @:allow(Sys)
  36. private static function createProcessBuilder(cmd:String, ?args:Array<String>):java.lang.ProcessBuilder {
  37. var sysName = Sys.systemName();
  38. var pargs;
  39. if (args == null) {
  40. var cmdStr = cmd;
  41. switch (sysName) {
  42. case "Windows":
  43. pargs = new NativeArray(3);
  44. pargs[0] = cmd = switch (Sys.getEnv("COMSPEC")) {
  45. case null: "cmd.exe";
  46. case comspec: comspec;
  47. }
  48. pargs[1] = '/C';
  49. pargs[2] = '"$cmdStr"';
  50. case _:
  51. pargs = new NativeArray(3);
  52. pargs[0] = cmd = "/bin/sh";
  53. pargs[1] = "-c";
  54. pargs[2] = cmdStr;
  55. }
  56. } else {
  57. pargs = new NativeArray(args.length + 1);
  58. switch (sysName) {
  59. case "Windows":
  60. pargs[0] = StringTools.quoteWinArg(cmd, false);
  61. for (i in 0...args.length)
  62. {
  63. pargs[i + 1] = StringTools.quoteWinArg(args[i], false);
  64. }
  65. case _:
  66. pargs[0] = cmd;
  67. for (i in 0...args.length)
  68. {
  69. pargs[i + 1] = args[i];
  70. }
  71. }
  72. }
  73. return new java.lang.ProcessBuilder(pargs);
  74. }
  75. public function new( cmd : String, ?args : Array<String>, ?detached : Bool ) : Void
  76. {
  77. if( detached ) throw "Detached process is not supported on this platform";
  78. var p = proc = createProcessBuilder(cmd, args).start();
  79. stderr = new ProcessInput(p.getErrorStream());
  80. stdout = new ProcessInput(p.getInputStream());
  81. stdin = new java.io.NativeOutput(p.getOutputStream());
  82. }
  83. public function getPid() : Int
  84. {
  85. if (Reflect.hasField(proc, "pid"))
  86. return Reflect.field(proc, "pid");
  87. return -1;
  88. }
  89. public function exitCode( block : Bool = true ) : Null<Int>
  90. {
  91. if( block == false ) {
  92. try {
  93. return proc.exitValue();
  94. } catch( e : Dynamic ) {
  95. return null;
  96. }
  97. }
  98. cast(stdout, ProcessInput).bufferContents();
  99. cast(stderr, ProcessInput).bufferContents();
  100. try
  101. {
  102. proc.waitFor();
  103. }
  104. catch (e:Dynamic) { throw e; }
  105. return proc.exitValue();
  106. }
  107. public function close() : Void
  108. {
  109. proc.destroy();
  110. }
  111. public function kill() : Void
  112. {
  113. proc.destroy();
  114. }
  115. }
  116. private class ProcessInput extends java.io.NativeInput
  117. {
  118. private var chained:BytesInput;
  119. public function bufferContents():Void
  120. {
  121. if (chained != null) return;
  122. var b = this.readAll();
  123. chained = new BytesInput(b);
  124. }
  125. override public function readByte():Int
  126. {
  127. if (chained != null)
  128. return chained.readByte();
  129. var ret = 0;
  130. try
  131. {
  132. ret = stream.read();
  133. }
  134. catch (e:IOException) {
  135. throw haxe.io.Error.Custom(e);
  136. }
  137. if ( ret == -1 )
  138. throw new Eof();
  139. return ret;
  140. }
  141. override public function readBytes(s:Bytes, pos:Int, len:Int):Int
  142. {
  143. if (chained != null)
  144. return chained.readBytes(s, pos, len);
  145. var ret = -1;
  146. try
  147. {
  148. ret = stream.read(s.getData(), pos, len);
  149. }
  150. catch (e:EOFException) {
  151. throw new Eof();
  152. }
  153. catch (e:IOException) {
  154. throw haxe.io.Error.Custom(e);
  155. }
  156. if (ret == -1)
  157. throw new Eof();
  158. return ret;
  159. }
  160. override public function close():Void
  161. {
  162. if (chained != null)
  163. chained.close();
  164. try
  165. {
  166. stream.close();
  167. }
  168. catch (e:IOException) {
  169. throw haxe.io.Error.Custom(e);
  170. }
  171. }
  172. }