Process.hx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. pargs[i + 1] = SysTools.quoteWinArg(args[i], false);
  64. }
  65. case _:
  66. pargs[0] = cmd;
  67. for (i in 0...args.length) {
  68. pargs[i + 1] = args[i];
  69. }
  70. }
  71. }
  72. return new java.lang.ProcessBuilder(...pargs);
  73. }
  74. public function new(cmd:String, ?args:Array<String>, ?detached:Bool):Void {
  75. if (detached)
  76. throw "Detached process is not supported on this platform";
  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. if (Reflect.hasField(proc, "pid"))
  84. return Reflect.field(proc, "pid");
  85. return -1;
  86. }
  87. public function exitCode(block:Bool = true):Null<Int> {
  88. if (block == false) {
  89. try {
  90. return proc.exitValue();
  91. } catch (e:Dynamic) {
  92. return null;
  93. }
  94. }
  95. cast(stdout, ProcessInput).bufferContents();
  96. cast(stderr, ProcessInput).bufferContents();
  97. try {
  98. proc.waitFor();
  99. } catch (e:Dynamic) {
  100. throw e;
  101. }
  102. return proc.exitValue();
  103. }
  104. public function close():Void {
  105. proc.destroy();
  106. }
  107. public function kill():Void {
  108. proc.destroy();
  109. }
  110. }
  111. private class ProcessInput extends java.io.NativeInput {
  112. private var chained:BytesInput;
  113. public function bufferContents():Void {
  114. if (chained != null)
  115. return;
  116. var b = this.readAll();
  117. chained = new BytesInput(b);
  118. }
  119. override public function readByte():Int {
  120. if (chained != null)
  121. return chained.readByte();
  122. var ret = 0;
  123. try {
  124. ret = stream.read();
  125. } catch (e:IOException) {
  126. throw haxe.io.Error.Custom(e);
  127. }
  128. if (ret == -1)
  129. throw new Eof();
  130. return ret;
  131. }
  132. override public function readBytes(s:Bytes, pos:Int, len:Int):Int {
  133. if (chained != null)
  134. return chained.readBytes(s, pos, len);
  135. var ret = -1;
  136. try {
  137. ret = stream.read(s.getData(), pos, len);
  138. } catch (e:EOFException) {
  139. throw new Eof();
  140. } catch (e:IOException) {
  141. throw haxe.io.Error.Custom(e);
  142. }
  143. if (ret == -1)
  144. throw new Eof();
  145. return ret;
  146. }
  147. override public function close():Void {
  148. if (chained != null)
  149. chained.close();
  150. try {
  151. stream.close();
  152. } catch (e:IOException) {
  153. throw haxe.io.Error.Custom(e);
  154. }
  155. }
  156. }