Process.hx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 sys.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. @:core_api
  73. class Process {
  74. var p : Void;
  75. var st : NativeArray;
  76. var cl : Int;
  77. public var stdout(default,null) : haxe.io.Input;
  78. public var stderr(default,null) : haxe.io.Input;
  79. public var stdin(default,null) : haxe.io.Output;
  80. public function new( cmd : String, args : Array<String> ) : Void {
  81. var pipes = untyped __call__("array");
  82. var descriptorspec = untyped __php__("array(
  83. array('pipe', 'r'),
  84. array('pipe', 'w'),
  85. array('pipe', 'w')
  86. )");
  87. p = untyped __call__('proc_open', cmd+sargs(args), descriptorspec, pipes);
  88. if(untyped __physeq__(p, false)) throw "Process creation failure : "+cmd;
  89. stdin = new Stdin( pipes[0]);
  90. stdout = new Stdout(pipes[1]);
  91. stderr = new Stdout(pipes[2]);
  92. }
  93. public function close() : Void {
  94. if(null == st)
  95. st = untyped __call__('proc_get_status', p);
  96. replaceStream(stderr);
  97. replaceStream(stdout);
  98. cl = untyped __call__('proc_close', p);
  99. }
  100. function sargs(args : Array<String>) : String {
  101. var b = '';
  102. for(arg in args) {
  103. arg = arg.split('"').join('\"');
  104. if(arg.indexOf(' ') >= 0)
  105. arg = '"'+arg+'"';
  106. b += ' '+arg;
  107. }
  108. return b;
  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() : Int {
  128. if (null == cl)
  129. {
  130. st = untyped __call__('proc_get_status', p);
  131. while(st[untyped 'running']) {
  132. Sys.sleep(0.01);
  133. st = untyped __call__('proc_get_status', p);
  134. }
  135. close();
  136. }
  137. return (cast st[untyped 'exitcode']) < 0 ? cl : cast st[untyped 'exitcode'];
  138. }
  139. }