Process.hx 4.9 KB

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