Process.hx 5.0 KB

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