Process.hx 3.7 KB

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