Process.hx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. private typedef ProcessHandle = hl.Abstract<"hl_process">;
  24. private class Stdin extends haxe.io.Output {
  25. var p : Dynamic;
  26. var buf : haxe.io.Bytes;
  27. public function new(p) {
  28. this.p = p;
  29. buf = haxe.io.Bytes.alloc(1);
  30. }
  31. public override function close() {
  32. super.close();
  33. _stdin_close(p);
  34. }
  35. public override function writeByte(c) {
  36. buf.set(0,c);
  37. writeBytes(buf,0,1);
  38. }
  39. public override function writeBytes( buf : haxe.io.Bytes, pos : Int, len : Int ) : Int {
  40. var v = _stdin_write(p, buf.getData().bytes, pos, len);
  41. if( v < 0 ) throw new haxe.io.Eof();
  42. return v;
  43. }
  44. @:hlNative("std","process_stdin_write") static function _stdin_write( p : ProcessHandle, bytes : hl.Bytes, pos : Int, len : Int ) : Int { return 0; }
  45. @:hlNative("std", "process_stdin_close") static function _stdin_close( p : ProcessHandle ) : Bool { return false; }
  46. }
  47. private class Stdout extends haxe.io.Input {
  48. var p : ProcessHandle;
  49. var out : Bool;
  50. var buf : haxe.io.Bytes;
  51. public function new(p,out) {
  52. this.p = p;
  53. this.out = out;
  54. buf = haxe.io.Bytes.alloc(1);
  55. }
  56. public override function readByte() {
  57. if( readBytes(buf,0,1) == 0 )
  58. throw haxe.io.Error.Blocked;
  59. return buf.get(0);
  60. }
  61. public override function readBytes( str : haxe.io.Bytes, pos : Int, len : Int ) : Int {
  62. var v = out ? _stdout_read(p,str.getData().bytes,pos,len) : _stderr_read(p,str.getData().bytes,pos,len);
  63. if( v < 0 ) throw new haxe.io.Eof();
  64. return v;
  65. }
  66. @:hlNative("std","process_stdout_read") static function _stdout_read( p : ProcessHandle, bytes : hl.Bytes, pos : Int, len : Int ) : Int { return 0; }
  67. @:hlNative("std","process_stderr_read") static function _stderr_read( p : ProcessHandle, bytes : hl.Bytes, pos : Int, len : Int ) : Int { return 0; }
  68. }
  69. @:access(Sys)
  70. @:coreApi class Process {
  71. var p : ProcessHandle;
  72. public var stdout(default,null) : haxe.io.Input;
  73. public var stderr(default,null) : haxe.io.Input;
  74. public var stdin(default, null) : haxe.io.Output;
  75. static var isWin = Sys.systemName() == "Windows";
  76. public function new( cmd : String, ?args : Array<String> ) : Void {
  77. var runCmd = cmd;
  78. if( isWin ) {
  79. var b = new StringBuf();
  80. if( args == null ) {
  81. var exe = Sys.getEnv("COMSPEC");
  82. if( exe == null ) exe = "cmd.exe";
  83. b.add("\"");
  84. b.add(exe);
  85. b.add("\" /C \"");
  86. b.add(cmd);
  87. b.addChar('"'.code);
  88. } else {
  89. b.addChar('"'.code);
  90. b.add(cmd);
  91. b.addChar('"'.code);
  92. for( a in args ) {
  93. b.add(" \"");
  94. var bsCount = 0;
  95. for( i in 0...a.length ) {
  96. switch( StringTools.fastCodeAt(a, i) ) {
  97. case '"'.code:
  98. for( i in 0...bsCount * 2 )
  99. b.addChar('\\'.code);
  100. bsCount = 0;
  101. b.add("\\\"");
  102. case '\\'.code:
  103. bsCount++;
  104. case c:
  105. for( i in 0...bsCount )
  106. b.addChar('\\'.code);
  107. bsCount = 0;
  108. b.addChar(c);
  109. }
  110. }
  111. // Add remaining backslashes, if any.
  112. for( i in 0...bsCount * 2 )
  113. b.addChar('\\'.code);
  114. b.addChar('"'.code);
  115. }
  116. args = null;
  117. }
  118. runCmd = b.toString();
  119. }
  120. @:privateAccess {
  121. var aargs = null;
  122. if( args != null ) {
  123. aargs = new hl.NativeArray<hl.Bytes>(args.length);
  124. for( i in 0...args.length )
  125. aargs[i] = Sys.getPath(args[i]);
  126. }
  127. p = _run(Sys.getPath(runCmd), aargs);
  128. }
  129. if( p == null )
  130. throw new Sys.SysError("Process creation failure : "+cmd);
  131. stdin = new Stdin(p);
  132. stdout = new Stdout(p,true);
  133. stderr = new Stdout(p,false);
  134. }
  135. public function getPid() : Int {
  136. return _pid(p);
  137. }
  138. public function exitCode( block : Bool = true ) : Null<Int> {
  139. var running = false;
  140. var code = _exit(p, block == false ? new hl.Ref(running) : null);
  141. if( block == false )
  142. return running ? null : code;
  143. return code;
  144. }
  145. public function close() : Void {
  146. _close(p);
  147. }
  148. public function kill() : Void {
  149. _kill(p);
  150. }
  151. @:hlNative("std","process_run") static function _run( cmd : hl.Bytes, args : hl.NativeArray<hl.Bytes> ) : ProcessHandle { return null; }
  152. @:hlNative("std", "process_exit") static function _exit( p : ProcessHandle, running : hl.Ref<Bool> ) : Int { return 0; }
  153. @:hlNative("std", "process_pid") static function _pid( p : ProcessHandle ) : Int { return 0; }
  154. @:hlNative("std","process_close") static function _close( p : ProcessHandle ) : Void { }
  155. @:hlNative("std","process_kill") static function _kill( p : ProcessHandle ) : Void { }
  156. }