Process.hx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 php.NativeArray;
  24. private class Stdin extends haxe.io.Output {
  25. var p : Dynamic;
  26. var buf : haxe.io.Bytes;
  27. public function new(p:Dynamic) {
  28. this.p = p;
  29. buf = haxe.io.Bytes.alloc(1);
  30. }
  31. public override function close() {
  32. super.close();
  33. untyped __call__('fclose', p);
  34. }
  35. public override function writeByte(c) {
  36. buf.set(0,c);
  37. writeBytes(buf,0,1);
  38. }
  39. public override function writeBytes( b : haxe.io.Bytes, pos : Int, l : Int ) : Int {
  40. var s = b.getString(pos, l);
  41. if(untyped __call__('feof', p)) return throw new haxe.io.Eof();
  42. var r = untyped __call__('fwrite', p, s, l);
  43. if(untyped __physeq__(r, false)) return throw haxe.io.Error.Custom('An error occurred');
  44. return r;
  45. }
  46. }
  47. private class Stdout extends haxe.io.Input {
  48. var p : Dynamic;
  49. var buf : haxe.io.Bytes;
  50. public function new(p:Dynamic) {
  51. this.p = p;
  52. buf = haxe.io.Bytes.alloc(1);
  53. }
  54. public override function readByte() {
  55. if( readBytes(buf,0,1) == 0 )
  56. throw haxe.io.Error.Blocked;
  57. return buf.get(0);
  58. }
  59. public override function readBytes( str : haxe.io.Bytes, pos : Int, l : Int ) : Int {
  60. if(untyped __call__('feof', p)) return throw new haxe.io.Eof();
  61. var r : String = untyped __call__('fread', p, l);
  62. if(untyped __physeq__(r, "")) return throw new haxe.io.Eof();
  63. if(untyped __physeq__(r, false)) return throw haxe.io.Error.Custom('An error occurred');
  64. var b = haxe.io.Bytes.ofString(r);
  65. str.blit(pos, b, 0, r.length);
  66. return r.length;
  67. }
  68. }
  69. @:coreApi
  70. class Process {
  71. var p : Dynamic;
  72. var st : NativeArray;
  73. var cl : Int;
  74. public var stdout(default,null) : haxe.io.Input;
  75. public var stderr(default,null) : haxe.io.Input;
  76. public var stdin(default,null) : haxe.io.Output;
  77. public function new( cmd : String, ?args : Array<String>, ?detached : Bool ) : Void {
  78. if( detached ) throw "Detached process is not supported on this platform";
  79. var pipes = untyped __call__("array");
  80. var descriptorspec = untyped __php__("array(
  81. array('pipe', 'r'),
  82. array('pipe', 'w'),
  83. array('pipe', 'w')
  84. )");
  85. if (args != null) {
  86. switch (Sys.systemName()) {
  87. case "Windows":
  88. cmd = [
  89. for (a in [StringTools.replace(cmd, "/", "\\")].concat(args))
  90. StringTools.quoteWinArg(a, true)
  91. ].join(" ");
  92. case _:
  93. cmd = [cmd].concat(args).map(StringTools.quoteUnixArg).join(" ");
  94. }
  95. }
  96. p = untyped __call__('proc_open', cmd, descriptorspec, pipes);
  97. if(untyped __physeq__(p, false)) throw "Process creation failure : "+cmd;
  98. stdin = new Stdin( pipes[0]);
  99. stdout = new Stdout(pipes[1]);
  100. stderr = new Stdout(pipes[2]);
  101. }
  102. public function close() : Void {
  103. if(null == st)
  104. st = untyped __call__('proc_get_status', p);
  105. replaceStream(stderr);
  106. replaceStream(stdout);
  107. if(null == cl)
  108. cl = untyped __call__('proc_close', p);
  109. }
  110. public function getPid() : Int {
  111. var r = untyped __call__('proc_get_status', p);
  112. return r[untyped 'pid'];
  113. }
  114. public function kill() : Void {
  115. untyped __call__('proc_terminate',p);
  116. }
  117. function replaceStream(input : haxe.io.Input) : Void {
  118. var fp = untyped __call__("fopen", "php://memory", "r+");
  119. while(true) {
  120. var s = untyped __call__("fread", untyped input.p, 8192);
  121. if(untyped __physeq__(s, false) || s == null || s == '') break;
  122. untyped __call__("fwrite", fp, s);
  123. }
  124. untyped __call__("rewind", fp);
  125. untyped input.p = fp;
  126. }
  127. public function exitCode( block : Bool = true ) : Null<Int> {
  128. if (null == cl)
  129. {
  130. st = untyped __call__('proc_get_status', p);
  131. while(st[untyped 'running']) {
  132. if( block == false ) return null;
  133. Sys.sleep(0.01);
  134. st = untyped __call__('proc_get_status', p);
  135. }
  136. close();
  137. }
  138. return (cast st[untyped 'exitcode']) < 0 ? cl : cast st[untyped 'exitcode'];
  139. }
  140. }