Process.hx 5.4 KB

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