Process.hx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * Copyright (C)2005-2017 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>){
  68. var _stdout = new Pipe(false);
  69. var _stderr = new Pipe(false);
  70. var _stdin = new Pipe(false);
  71. stdout = new ProcessInput(_stdout);
  72. stderr = new ProcessInput(_stderr);
  73. stdin = new ProcessOutput(_stdin);
  74. var stdio = untyped __lua_table__([_stdin,_stdout,_stderr]);
  75. var opt = { args: setArgs(cmd, args), stdio : stdio };
  76. var p : lua.lib.luv.Process.LuvSpawn;
  77. p = lua.lib.luv.Process.spawn( _shell, opt, function(code : Int, signal : Signal){
  78. _code = code;
  79. });
  80. if (p.handle == null) throw p.pid;
  81. _pid = p.pid;
  82. _handle = p.handle;
  83. }
  84. public function getPid() : Int {
  85. return _pid;
  86. }
  87. public function close() : Void {
  88. stdout.close();
  89. stdin.close();
  90. stderr.close();
  91. _handle.close();
  92. }
  93. public function exitCode( block : Bool = true ) : Null<Int> {
  94. if (!block) return _code;
  95. while (_handle.is_active()) {
  96. Loop.run(); // process io until the handle closes (emulate blocking)
  97. }
  98. return _code;
  99. }
  100. public function kill() : Void {
  101. _handle.kill("sigterm");
  102. }
  103. }
  104. class ProcessInput extends haxe.io.Input {
  105. var b : Pipe;
  106. var buf : String;
  107. var idx : Int;
  108. public function new(pipe:Pipe) {
  109. b = pipe;
  110. }
  111. override public function readByte() {
  112. var err_str = null;
  113. if (buf == null || idx >= buf.length){
  114. buf = null;
  115. idx = 0;
  116. var pending = true;
  117. b.read_start(function(err, chunk){
  118. if (chunk != null) buf = chunk;
  119. if (err != null) err_str = err;
  120. pending = false;
  121. });
  122. // process io until we read our input (emulate blocking)
  123. while (pending) Loop.run();
  124. }
  125. if (buf == null) throw new haxe.io.Eof();
  126. if (err_str != null) throw err_str;
  127. var code : Int = cast buf.charCodeAt(idx++);
  128. return code;
  129. }
  130. override public function readAll( ?bufsize : Int ) : Bytes {
  131. if( bufsize == null ) bufsize = (1 << 14); // 16 Ko
  132. var buf = Bytes.alloc(bufsize);
  133. var total = new haxe.io.BytesBuffer();
  134. try {
  135. while( true ) {
  136. var len = readBytes(buf,0,bufsize);
  137. // don't throw blocked error here
  138. if (len != 0)
  139. total.addBytes(buf,0,len);
  140. if (len < bufsize) break;
  141. }
  142. } catch( e : Eof ) {}
  143. return total.getBytes();
  144. }
  145. }
  146. class ProcessOutput extends haxe.io.Output {
  147. var b : Pipe;
  148. public function new(pipe:Pipe) {
  149. b = pipe;
  150. set_bigEndian(Boot.platformBigEndian);
  151. }
  152. override public function writeByte(c : Int ) : Void {
  153. b.write(String.fromCharCode(c));
  154. }
  155. override public function close(){
  156. b.close();
  157. }
  158. }