Process.hx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 cpp.io;
  26. private class Stdin extends haxe.io.Output {
  27. var p : Dynamic;
  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. _stdin_close(p);
  36. }
  37. public override function writeByte(c) {
  38. buf.set(0,c);
  39. writeBytes(buf,0,1);
  40. }
  41. public override function writeBytes( buf : haxe.io.Bytes, pos : Int, len : Int ) : Int {
  42. try {
  43. return _stdin_write(p,buf.getData(),pos,len);
  44. } catch( e : Dynamic ) {
  45. throw new haxe.io.Eof();
  46. }
  47. }
  48. static var _stdin_write = cpp.Lib.load("std","process_stdin_write",4);
  49. static var _stdin_close = cpp.Lib.load("std","process_stdin_close",1);
  50. }
  51. private class Stdout extends haxe.io.Input {
  52. var p : Dynamic;
  53. var out : Bool;
  54. var buf : haxe.io.Bytes;
  55. public function new(p,out) {
  56. this.p = p;
  57. this.out = out;
  58. buf = haxe.io.Bytes.alloc(1);
  59. }
  60. public override function readByte() {
  61. if( readBytes(buf,0,1) == 0 )
  62. throw haxe.io.Error.Blocked;
  63. return buf.get(0);
  64. }
  65. public override function readBytes( str : haxe.io.Bytes, pos : Int, len : Int ) : Int {
  66. var result:Int;
  67. try {
  68. result = (out?_stdout_read:_stderr_read)(p,str.getData(),pos,len);
  69. } catch( e : Dynamic ) {
  70. throw new haxe.io.Eof();
  71. }
  72. if (result==0)throw new haxe.io.Eof();
  73. return result;
  74. }
  75. static var _stdout_read = cpp.Lib.load("std","process_stdout_read",4);
  76. static var _stderr_read = cpp.Lib.load("std","process_stderr_read",4);
  77. }
  78. class Process {
  79. var p : Dynamic;
  80. public var stdout(default,null) : haxe.io.Input;
  81. public var stderr(default,null) : haxe.io.Input;
  82. public var stdin(default,null) : haxe.io.Output;
  83. public function new( cmd : String, args : Array<String> ) {
  84. p = try _run(cmd,args) catch( e : Dynamic ) throw "Process creation failure : "+cmd;
  85. stdin = new Stdin(p);
  86. stdout = new Stdout(p,true);
  87. stderr = new Stdout(p,false);
  88. }
  89. public function getPid() : Int {
  90. return _pid(p);
  91. }
  92. public function exitCode() : Int {
  93. return _exit(p);
  94. }
  95. public function close() {
  96. _close(p);
  97. }
  98. static var _run = cpp.Lib.load("std","process_run",2);
  99. static var _exit = cpp.Lib.load("std","process_exit",1);
  100. static var _pid = cpp.Lib.load("std","process_pid",1);
  101. static var _close = cpp.Lib.loadLazy("std","process_close",1);
  102. }