PageRenderTime 67ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/bin/std/php/io/Process.hx

http://github.com/Yoomee/clippy
Haxe | 140 lines | 102 code | 14 blank | 24 comment | 14 complexity | f2bb8d685f9117366b0a64fe7eb71745 MD5 | raw file
Possible License(s): MIT
  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 = untyped __call__("array");
  78. var descriptorspec = untyped __php__("array(
  79. array('pipe', 'r'),
  80. array('pipe', 'w'),
  81. array('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. var r = untyped __call__('proc_get_status', p);
  101. return r[untyped 'pid'];
  102. }
  103. function replaceStream(input : haxe.io.Input) {
  104. var fp = untyped __call__("fopen", "php://memory", "r+");
  105. while(true) {
  106. var s = untyped __call__("fread", untyped input.p, 8192);
  107. if(untyped __physeq__(s, false) || s == null || s == '') break;
  108. untyped __call__("fwrite", fp, s);
  109. }
  110. untyped __call__("rewind", fp);
  111. untyped input.p = fp;
  112. }
  113. public function exitCode() : Int {
  114. var status = untyped __call__('proc_get_status', p);
  115. while(status[untyped 'running']) {
  116. php.Sys.sleep(0.01);
  117. status = untyped __call__('proc_get_status', p);
  118. }
  119. replaceStream(stderr);
  120. replaceStream(stdout);
  121. var cl : Int = untyped __call__('proc_close', p);
  122. return (cast status[untyped 'exitcode']) < 0 ? cl : cast status[untyped 'exitcode'];
  123. }
  124. }