Process.hx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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> ) : Void
  76. {
  77. var p = proc = createProcessBuilder(cmd, args).start();
  78. stderr = new ProcessInput(p.getErrorStream());
  79. stdout = new ProcessInput(p.getInputStream());
  80. stdin = new java.io.NativeOutput(p.getOutputStream());
  81. }
  82. public function getPid() : Int
  83. {
  84. if (Reflect.hasField(proc, "pid"))
  85. return Reflect.field(proc, "pid");
  86. return -1;
  87. }
  88. public function exitCode( block : Bool = true ) : Null<Int>
  89. {
  90. if( block == false ) {
  91. try {
  92. return proc.exitValue();
  93. } catch( e : Dynamic ) {
  94. return null;
  95. }
  96. }
  97. cast(stdout, ProcessInput).bufferContents();
  98. cast(stderr, ProcessInput).bufferContents();
  99. try
  100. {
  101. proc.waitFor();
  102. }
  103. catch (e:Dynamic) { throw e; }
  104. return proc.exitValue();
  105. }
  106. public function close() : Void
  107. {
  108. proc.destroy();
  109. }
  110. public function kill() : Void
  111. {
  112. proc.destroy();
  113. }
  114. }
  115. private class ProcessInput extends java.io.NativeInput
  116. {
  117. private var chained:BytesInput;
  118. public function bufferContents():Void
  119. {
  120. if (chained != null) return;
  121. var b = this.readAll();
  122. chained = new BytesInput(b);
  123. }
  124. override public function readByte():Int
  125. {
  126. if (chained != null)
  127. return chained.readByte();
  128. var ret = 0;
  129. try
  130. {
  131. ret = stream.read();
  132. }
  133. catch (e:IOException) {
  134. throw haxe.io.Error.Custom(e);
  135. }
  136. if ( ret == -1 )
  137. throw new Eof();
  138. return ret;
  139. }
  140. override public function readBytes(s:Bytes, pos:Int, len:Int):Int
  141. {
  142. if (chained != null)
  143. return chained.readBytes(s, pos, len);
  144. var ret = -1;
  145. try
  146. {
  147. ret = stream.read(s.getData(), pos, len);
  148. }
  149. catch (e:EOFException) {
  150. throw new Eof();
  151. }
  152. catch (e:IOException) {
  153. throw haxe.io.Error.Custom(e);
  154. }
  155. if (ret == -1)
  156. throw new Eof();
  157. return ret;
  158. }
  159. override public function close():Void
  160. {
  161. if (chained != null)
  162. chained.close();
  163. try
  164. {
  165. stream.close();
  166. }
  167. catch (e:IOException) {
  168. throw haxe.io.Error.Custom(e);
  169. }
  170. }
  171. }