Process.hx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright (C)2005-2016 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. public function new( cmd : String, ?args : Array<String> ) : Void
  36. {
  37. var pargs = new NativeArray(args.length + 1);
  38. switch (Sys.systemName()) {
  39. case "Windows":
  40. pargs[0] = StringTools.quoteWinArg(cmd, false);
  41. for (i in 0...args.length)
  42. {
  43. pargs[i + 1] = StringTools.quoteWinArg(args[i], false);
  44. }
  45. case _:
  46. pargs[0] = cmd;
  47. for (i in 0...args.length)
  48. {
  49. pargs[i + 1] = args[i];
  50. }
  51. }
  52. try
  53. {
  54. proc = new java.lang.ProcessBuilder(pargs).start();
  55. }
  56. catch (e:Dynamic) { throw e; } //wrapping typed exceptions
  57. var p = proc;
  58. stderr = new ProcessInput(p.getErrorStream());
  59. stdout = new ProcessInput(p.getInputStream());
  60. stdin = new java.io.NativeOutput(p.getOutputStream());
  61. }
  62. public function getPid() : Int
  63. {
  64. if (Reflect.hasField(proc, "pid"))
  65. return Reflect.field(proc, "pid");
  66. return -1;
  67. }
  68. public function exitCode() : Int
  69. {
  70. cast(stdout, ProcessInput).bufferContents();
  71. cast(stderr, ProcessInput).bufferContents();
  72. try
  73. {
  74. proc.waitFor();
  75. }
  76. catch (e:Dynamic) { throw e; }
  77. return proc.exitValue();
  78. }
  79. public function close() : Void
  80. {
  81. proc.destroy();
  82. }
  83. public function kill() : Void
  84. {
  85. proc.destroy();
  86. }
  87. }
  88. private class ProcessInput extends java.io.NativeInput
  89. {
  90. private var chained:BytesInput;
  91. public function bufferContents():Void
  92. {
  93. if (chained != null) return;
  94. var b = this.readAll();
  95. chained = new BytesInput(b);
  96. }
  97. override public function readByte():Int
  98. {
  99. if (chained != null)
  100. return chained.readByte();
  101. var ret = 0;
  102. try
  103. {
  104. ret = stream.read();
  105. }
  106. catch (e:IOException) {
  107. throw haxe.io.Error.Custom(e);
  108. }
  109. if ( ret == -1 )
  110. throw new Eof();
  111. return ret;
  112. }
  113. override public function readBytes(s:Bytes, pos:Int, len:Int):Int
  114. {
  115. if (chained != null)
  116. return chained.readBytes(s, pos, len);
  117. var ret = -1;
  118. try
  119. {
  120. ret = stream.read(s.getData(), pos, len);
  121. }
  122. catch (e:EOFException) {
  123. throw new Eof();
  124. }
  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 close():Void
  133. {
  134. if (chained != null)
  135. chained.close();
  136. try
  137. {
  138. stream.close();
  139. }
  140. catch (e:IOException) {
  141. throw haxe.io.Error.Custom(e);
  142. }
  143. }
  144. }