Process.hx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 lua.Io;
  24. import lua.lib.luv.Pipe;
  25. import lua.lib.luv.Signal;
  26. import lua.lib.luv.Loop;
  27. import lua.Boot;
  28. import lua.Table;
  29. import lua.NativeStringTools;
  30. import haxe.SysTools;
  31. import haxe.io.Bytes;
  32. import haxe.io.Error;
  33. import haxe.io.Eof;
  34. @:coreApi
  35. class Process {
  36. var _pid : Int;
  37. var _handle : lua.lib.luv.Process;
  38. var _code : Int;
  39. var closef : Int->Signal->Void;
  40. public var stdout(default,null) : haxe.io.Input;
  41. public var stderr(default,null) : haxe.io.Input;
  42. public var stdin(default,null) : haxe.io.Output;
  43. static var argQuote = Sys.systemName() == "Windows" ? function(x) return SysTools.quoteWinArg(x,true) : SysTools.quoteUnixArg;
  44. static var _shell = Sys.systemName() == "Windows" ? 'cmd.exe' : '/bin/sh';
  45. /**
  46. Sets the args for the shell, which will include the cmd to be executed
  47. by the shell.
  48. **/
  49. static function setArgs(cmd : String, ?args : Array<String>) : Table<Int,String>{
  50. var pargs = lua.Table.create();
  51. var idx = 1;
  52. if (sys.FileSystem.exists(cmd)) cmd = '"$cmd"'; // escape simple paths
  53. var all = [cmd];
  54. if (args != null) {
  55. for (a in args){
  56. all.push(argQuote(a));
  57. }
  58. }
  59. if (Sys.systemName() == "Windows") {
  60. pargs[idx++] = '/s';
  61. pargs[idx++] = '/c';
  62. pargs[idx++] = all.join(" ");
  63. } else {
  64. pargs[idx++] = "-c";
  65. pargs[idx++] = all.join(" ");
  66. }
  67. return pargs;
  68. }
  69. public function new( cmd : String, ?args : Array<String>, ?detached : Bool){
  70. if( detached ) throw "Detached process is not supported on this platform";
  71. var _stdout = new Pipe(false);
  72. var _stderr = new Pipe(false);
  73. var _stdin = new Pipe(false);
  74. stdout = new ProcessInput(_stdout);
  75. stderr = new ProcessInput(_stderr);
  76. stdin = new ProcessOutput(_stdin);
  77. var stdio = untyped __lua_table__([_stdin,_stdout,_stderr]);
  78. var opt = { args: setArgs(cmd, args), stdio : stdio };
  79. var p : lua.lib.luv.Process.LuvSpawn;
  80. p = lua.lib.luv.Process.spawn( _shell, opt, function(code : Int, signal : Signal){
  81. _code = code;
  82. });
  83. if (p.handle == null) throw p.pid;
  84. _pid = p.pid;
  85. _handle = p.handle;
  86. }
  87. public function getPid() : Int {
  88. return _pid;
  89. }
  90. public function close() : Void {
  91. stdout.close();
  92. stdin.close();
  93. stderr.close();
  94. _handle.close();
  95. }
  96. public function exitCode( block : Bool = true ) : Null<Int> {
  97. if (!block) return _code;
  98. while (_handle.is_active()) {
  99. Loop.run(); // process io until the handle closes (emulate blocking)
  100. }
  101. return _code;
  102. }
  103. public function kill() : Void {
  104. _handle.kill("sigterm");
  105. }
  106. }
  107. private class ProcessInput extends haxe.io.Input {
  108. var b : Pipe;
  109. var buf : String;
  110. var idx : Int;
  111. var _eof:Bool;
  112. public function new(pipe:Pipe) {
  113. b = pipe;
  114. _eof = false;
  115. }
  116. inline public function eof() : Bool {
  117. return _eof;
  118. }
  119. override function readBytes( s : Bytes, pos : Int, len : Int ) : Int {
  120. if(eof()) throw new haxe.io.Eof();
  121. return super.readBytes(s, pos, len);
  122. }
  123. override public function readByte() {
  124. var err_str = null;
  125. if (buf == null || idx >= NativeStringTools.len(buf)){
  126. buf = null;
  127. idx = 0;
  128. var pending = true;
  129. b.read_start(function(err, chunk){
  130. if (chunk != null){
  131. if (buf != null){
  132. buf = buf + chunk;
  133. } else {
  134. buf = chunk;
  135. }
  136. }
  137. if (err != null) err_str = err;
  138. pending = false;
  139. });
  140. // process io until we read our input (emulate blocking)
  141. while (pending) Loop.run();
  142. }
  143. if (buf == null){
  144. _eof = true;
  145. throw new haxe.io.Eof();
  146. }
  147. if (err_str != null) throw err_str;
  148. var code = NativeStringTools.byte(buf, ++idx);
  149. return code;
  150. }
  151. override public function readAll( ?bufsize : Int ) : Bytes {
  152. if( bufsize == null ) bufsize = (1 << 14); // 16 Ko
  153. var buf = Bytes.alloc(bufsize);
  154. var total = new haxe.io.BytesBuffer();
  155. try {
  156. while( true ) {
  157. var len = readBytes(buf,0,bufsize);
  158. // don't throw blocked error here
  159. if (len != 0)
  160. total.addBytes(buf,0,len);
  161. if (len < bufsize) break;
  162. }
  163. } catch( e : Eof ) {
  164. _eof = true;
  165. }
  166. return total.getBytes();
  167. }
  168. override public function close() {
  169. b.close();
  170. }
  171. }
  172. private class ProcessOutput extends haxe.io.Output {
  173. var b : Pipe;
  174. public function new(pipe:Pipe) {
  175. b = pipe;
  176. set_bigEndian(Boot.platformBigEndian);
  177. }
  178. override public function writeByte(c : Int ) : Void {
  179. b.write(NativeStringTools.char(c));
  180. }
  181. override public function close(){
  182. b.close();
  183. }
  184. }