Process.hx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright (C)2005-2012 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. }
  45. static var _stdin_write = neko.Lib.load("std","process_stdin_write",4);
  46. static var _stdin_close = neko.Lib.load("std","process_stdin_close",1);
  47. }
  48. private class Stdout extends haxe.io.Input {
  49. var p : Dynamic;
  50. var out : Bool;
  51. var buf : haxe.io.Bytes;
  52. public function new(p,out) {
  53. this.p = p;
  54. this.out = out;
  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, len : Int ) : Int {
  63. try {
  64. return (out?_stdout_read:_stderr_read)(p,str.getData(),pos,len);
  65. } catch( e : Dynamic ) {
  66. throw new haxe.io.Eof();
  67. }
  68. }
  69. static var _stdout_read = neko.Lib.load("std","process_stdout_read",4);
  70. static var _stderr_read = neko.Lib.load("std","process_stderr_read",4);
  71. }
  72. @:coreApi class Process {
  73. var p : Dynamic;
  74. public var stdout(default,null) : haxe.io.Input;
  75. public var stderr(default,null) : haxe.io.Input;
  76. public var stdin(default,null) : haxe.io.Output;
  77. public function new( cmd : String, args : Array<String> ) : Void {
  78. p = try _run(untyped cmd.__s,neko.Lib.haxeToNeko(args)) catch( e : Dynamic ) throw "Process creation failure : "+cmd;
  79. stdin = new Stdin(p);
  80. stdout = new Stdout(p,true);
  81. stderr = new Stdout(p,false);
  82. }
  83. public function getPid() : Int {
  84. return _pid(p);
  85. }
  86. public function exitCode() : Int {
  87. return _exit(p);
  88. }
  89. public function close() : Void {
  90. _close(p);
  91. }
  92. public function kill() : Void {
  93. _kill(p);
  94. }
  95. static var _run = neko.Lib.load("std","process_run",2);
  96. static var _exit = neko.Lib.load("std","process_exit",1);
  97. static var _pid = neko.Lib.load("std","process_pid",1);
  98. static var _close = neko.Lib.loadLazy("std","process_close",1);
  99. static var _kill = neko.Lib.loadLazy("std","process_kill",1);
  100. }