Process.hx 4.5 KB

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