Process.hx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Copyright (C)2005-2015 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> ) : Void {
  78. var pipes = untyped __call__("array");
  79. var descriptorspec = untyped __php__("array(
  80. array('pipe', 'r'),
  81. array('pipe', 'w'),
  82. array('pipe', 'w')
  83. )");
  84. p = untyped __call__('proc_open', cmd+sargs(args), descriptorspec, pipes);
  85. if(untyped __physeq__(p, false)) throw "Process creation failure : "+cmd;
  86. stdin = new Stdin( pipes[0]);
  87. stdout = new Stdout(pipes[1]);
  88. stderr = new Stdout(pipes[2]);
  89. }
  90. public function close() : Void {
  91. if(null == st)
  92. st = untyped __call__('proc_get_status', p);
  93. replaceStream(stderr);
  94. replaceStream(stdout);
  95. cl = untyped __call__('proc_close', p);
  96. }
  97. function sargs(args : Array<String>) : String {
  98. var b = '';
  99. for(arg in args) {
  100. arg = arg.split('"').join('\"');
  101. if(arg.indexOf(' ') >= 0)
  102. arg = '"'+arg+'"';
  103. b += ' '+arg;
  104. }
  105. return b;
  106. }
  107. public function getPid() : Int {
  108. var r = untyped __call__('proc_get_status', p);
  109. return r[untyped 'pid'];
  110. }
  111. public function kill() : Void {
  112. untyped __call__('proc_terminate',p);
  113. }
  114. function replaceStream(input : haxe.io.Input) : Void {
  115. var fp = untyped __call__("fopen", "php://memory", "r+");
  116. while(true) {
  117. var s = untyped __call__("fread", untyped input.p, 8192);
  118. if(untyped __physeq__(s, false) || s == null || s == '') break;
  119. untyped __call__("fwrite", fp, s);
  120. }
  121. untyped __call__("rewind", fp);
  122. untyped input.p = fp;
  123. }
  124. public function exitCode() : Int {
  125. if (null == cl)
  126. {
  127. st = untyped __call__('proc_get_status', p);
  128. while(st[untyped 'running']) {
  129. Sys.sleep(0.01);
  130. st = untyped __call__('proc_get_status', p);
  131. }
  132. close();
  133. }
  134. return (cast st[untyped 'exitcode']) < 0 ? cl : cast st[untyped 'exitcode'];
  135. }
  136. }