Process.hx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. private class Stdin extends haxe.io.Output {
  27. var p : Void;
  28. var buf : haxe.io.Bytes;
  29. public function new(p) {
  30. this.p = p;
  31. buf = haxe.io.Bytes.alloc(1);
  32. }
  33. public override function close() {
  34. super.close();
  35. untyped __call__('fclose', p);
  36. }
  37. public override function writeByte(c) {
  38. buf.set(0,c);
  39. writeBytes(buf,0,1);
  40. }
  41. public override function writeBytes( b : haxe.io.Bytes, pos : Int, l : Int ) : Int {
  42. var s = b.readString(pos, l);
  43. if(untyped __call__('feof', p)) return throw new haxe.io.Eof();
  44. var r = untyped __call__('fwrite', p, s, l);
  45. if(untyped __physeq__(r, false)) return throw haxe.io.Error.Custom('An error occurred');
  46. return r;
  47. }
  48. }
  49. private class Stdout extends haxe.io.Input {
  50. var p : Void;
  51. var buf : haxe.io.Bytes;
  52. public function new(p) {
  53. this.p = p;
  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, l : Int ) : Int {
  62. if(untyped __call__('feof', p)) return throw new haxe.io.Eof();
  63. var r : String = untyped __call__('fread', p, l);
  64. if(untyped __physeq__(r, "")) return throw new haxe.io.Eof();
  65. if(untyped __physeq__(r, false)) return throw haxe.io.Error.Custom('An error occurred');
  66. var b = haxe.io.Bytes.ofString(r);
  67. str.blit(pos, b, 0, r.length);
  68. return r.length;
  69. }
  70. }
  71. class Process {
  72. var p : Void;
  73. public var stdout(default,null) : haxe.io.Input;
  74. public var stderr(default,null) : haxe.io.Input;
  75. public var stdin(default,null) : haxe.io.Output;
  76. public function new( cmd : String, args : Array<String> ) {
  77. var pipes = new Array<Dynamic>();
  78. var descriptorspec = [
  79. ['pipe', 'r'],
  80. ['pipe', 'w'],
  81. ['pipe', 'w']
  82. ];
  83. p = untyped __call__('proc_open', cmd+sargs(args), descriptorspec, pipes);
  84. if(untyped __physeq__(p, false)) throw "Process creation failure : "+cmd;
  85. stdin = new Stdin(pipes[0]);
  86. stdout = new Stdout(pipes[1]);
  87. stderr = new Stdout(pipes[2]);
  88. }
  89. function sargs(args : Array<String>) {
  90. var b = '';
  91. for(arg in args) {
  92. arg = arg.split('"').join('\"');
  93. if(arg.indexOf(' ') >= 0)
  94. arg = '"'+arg+'"';
  95. b += ' '+arg;
  96. }
  97. return b;
  98. }
  99. public function getPid() : Int {
  100. return untyped __call__('proc_get_status', p)['pid'];
  101. }
  102. function replaceStream(input : haxe.io.Input) {
  103. var fp = untyped __call__("fopen", "php://memory", "r+");
  104. while(true) {
  105. var s = untyped __call__("fread", untyped input.p, 8192);
  106. if(untyped __physeq__(s, false) || s == null || s == '') break;
  107. untyped __call__("fwrite", fp, s);
  108. }
  109. untyped __call__("rewind", fp);
  110. untyped input.p = fp;
  111. }
  112. public function exitCode() : Int {
  113. var status : Array<Dynamic> = untyped __call__('proc_get_status', p);
  114. while(status[untyped 'running']) {
  115. php.Sys.sleep(0.01);
  116. status = untyped __call__('proc_get_status', p);
  117. }
  118. replaceStream(stderr);
  119. replaceStream(stdout);
  120. return untyped __call__('proc_close', p);
  121. }
  122. }