Process.hx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (C)2005-2015 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. package sys.io;
  23. private class Stdin extends haxe.io.Output {
  24. var p : Dynamic;
  25. var buf : haxe.io.Bytes;
  26. public function new(p) {
  27. this.p = p;
  28. buf = haxe.io.Bytes.alloc(1);
  29. }
  30. public override function close() {
  31. super.close();
  32. _stdin_close(p);
  33. }
  34. public override function writeByte(c) {
  35. buf.set(0,c);
  36. writeBytes(buf,0,1);
  37. }
  38. public override function writeBytes( buf : haxe.io.Bytes, pos : Int, len : Int ) : Int {
  39. try {
  40. return _stdin_write(p,buf.getData(),pos,len);
  41. } catch( e : Dynamic ) {
  42. throw new haxe.io.Eof();
  43. }
  44. return 0;
  45. }
  46. static var _stdin_write = cpp.Lib.load("std","process_stdin_write",4);
  47. static var _stdin_close = cpp.Lib.load("std","process_stdin_close",1);
  48. }
  49. private class Stdout extends haxe.io.Input {
  50. var p : Dynamic;
  51. var out : Bool;
  52. var buf : haxe.io.Bytes;
  53. public function new(p,out) {
  54. this.p = p;
  55. this.out = out;
  56. buf = haxe.io.Bytes.alloc(1);
  57. }
  58. public override function readByte() {
  59. if( readBytes(buf,0,1) == 0 )
  60. throw haxe.io.Error.Blocked;
  61. return buf.get(0);
  62. }
  63. public override function readBytes( str : haxe.io.Bytes, pos : Int, len : Int ) : Int {
  64. var result:Int;
  65. try {
  66. result = (out?_stdout_read:_stderr_read)(p,str.getData(),pos,len);
  67. } catch( e : Dynamic ) {
  68. throw new haxe.io.Eof();
  69. }
  70. if (result==0)throw new haxe.io.Eof();
  71. return result;
  72. }
  73. static var _stdout_read = cpp.Lib.load("std","process_stdout_read",4);
  74. static var _stderr_read = cpp.Lib.load("std","process_stderr_read",4);
  75. }
  76. @:coreApi
  77. class Process {
  78. var p : Dynamic;
  79. public var stdout(default,null) : haxe.io.Input;
  80. public var stderr(default,null) : haxe.io.Input;
  81. public var stdin(default,null) : haxe.io.Output;
  82. public function new( cmd : String, args : Array<String> ) : Void {
  83. p = try _run(cmd,args) catch( e : Dynamic ) throw "Process creation failure : "+cmd;
  84. stdin = new Stdin(p);
  85. stdout = new Stdout(p,true);
  86. stderr = new Stdout(p,false);
  87. }
  88. public function getPid() : Int {
  89. return _pid(p);
  90. }
  91. public function exitCode() : Int {
  92. return _exit(p);
  93. }
  94. public function close() : Void {
  95. _close(p);
  96. }
  97. public function kill() : Void {
  98. throw "Not implemented";
  99. }
  100. static var _run = cpp.Lib.load("std","process_run",2);
  101. static var _exit = cpp.Lib.load("std","process_exit",1);
  102. static var _pid = cpp.Lib.load("std","process_pid",1);
  103. static var _close = cpp.Lib.loadLazy("std","process_close",1);
  104. }