Process.hx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright (c) 2005-2007, The haXe Project Contributors
  3. * All rights reserved.
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * - Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * - Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
  17. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  20. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  23. * DAMAGE.
  24. */
  25. package php.io;
  26. import php.NativeArray;
  27. private class Stdin extends haxe.io.Output {
  28. var p : Void;
  29. var buf : haxe.io.Bytes;
  30. public function new(p) {
  31. this.p = p;
  32. buf = haxe.io.Bytes.alloc(1);
  33. }
  34. public override function close() {
  35. super.close();
  36. untyped __call__('fclose', p);
  37. }
  38. public override function writeByte(c) {
  39. buf.set(0,c);
  40. writeBytes(buf,0,1);
  41. }
  42. public override function writeBytes( b : haxe.io.Bytes, pos : Int, l : Int ) : Int {
  43. var s = b.readString(pos, l);
  44. if(untyped __call__('feof', p)) return throw new haxe.io.Eof();
  45. var r = untyped __call__('fwrite', p, s, l);
  46. if(untyped __physeq__(r, false)) return throw haxe.io.Error.Custom('An error occurred');
  47. return r;
  48. }
  49. }
  50. private class Stdout extends haxe.io.Input {
  51. var p : Void;
  52. var buf : haxe.io.Bytes;
  53. public function new(p) {
  54. this.p = p;
  55. buf = haxe.io.Bytes.alloc(1);
  56. }
  57. public override function readByte() {
  58. if( readBytes(buf,0,1) == 0 )
  59. throw haxe.io.Error.Blocked;
  60. return buf.get(0);
  61. }
  62. public override function readBytes( str : haxe.io.Bytes, pos : Int, l : Int ) : Int {
  63. if(untyped __call__('feof', p)) return throw new haxe.io.Eof();
  64. var r : String = untyped __call__('fread', p, l);
  65. if(untyped __physeq__(r, "")) return throw new haxe.io.Eof();
  66. if(untyped __physeq__(r, false)) return throw haxe.io.Error.Custom('An error occurred');
  67. var b = haxe.io.Bytes.ofString(r);
  68. str.blit(pos, b, 0, r.length);
  69. return r.length;
  70. }
  71. }
  72. class Process {
  73. var p : Void;
  74. var st : NativeArray;
  75. var cl : Int;
  76. public var stdout(default,null) : haxe.io.Input;
  77. public var stderr(default,null) : haxe.io.Input;
  78. public var stdin(default,null) : haxe.io.Output;
  79. public function new( cmd : String, args : Array<String> ) {
  80. var pipes = untyped __call__("array");
  81. var descriptorspec = untyped __php__("array(
  82. array('pipe', 'r'),
  83. array('pipe', 'w'),
  84. array('pipe', 'w')
  85. )");
  86. p = untyped __call__('proc_open', cmd+sargs(args), descriptorspec, pipes);
  87. if(untyped __physeq__(p, false)) throw "Process creation failure : "+cmd;
  88. stdin = new Stdin( pipes[0]);
  89. stdout = new Stdout(pipes[1]);
  90. stderr = new Stdout(pipes[2]);
  91. }
  92. public function close() {
  93. if(null == st)
  94. st = untyped __call__('proc_get_status', p);
  95. replaceStream(stderr);
  96. replaceStream(stdout);
  97. cl = untyped __call__('proc_close', p);
  98. }
  99. function sargs(args : Array<String>) {
  100. var b = '';
  101. for(arg in args) {
  102. arg = arg.split('"').join('\"');
  103. if(arg.indexOf(' ') >= 0)
  104. arg = '"'+arg+'"';
  105. b += ' '+arg;
  106. }
  107. return b;
  108. }
  109. public function getPid() : Int {
  110. var r = untyped __call__('proc_get_status', p);
  111. return r[untyped 'pid'];
  112. }
  113. function replaceStream(input : haxe.io.Input) {
  114. var fp = untyped __call__("fopen", "php://memory", "r+");
  115. while(true) {
  116. var s = untyped __call__("fread", untyped input.p, 8192);
  117. if(untyped __physeq__(s, false) || s == null || s == '') break;
  118. untyped __call__("fwrite", fp, s);
  119. }
  120. untyped __call__("rewind", fp);
  121. untyped input.p = fp;
  122. }
  123. public function exitCode() : Int {
  124. if (null == cl)
  125. {
  126. st = untyped __call__('proc_get_status', p);
  127. while(st[untyped 'running']) {
  128. php.Sys.sleep(0.01);
  129. st = untyped __call__('proc_get_status', p);
  130. }
  131. close();
  132. }
  133. return (cast st[untyped 'exitcode']) < 0 ? cl : cast st[untyped 'exitcode'];
  134. }
  135. }