Process.hx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright (C)2005-2016 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 typedef ProcessHandle = hl.types.NativeAbstract<"hl_process">;
  24. private class Stdin extends haxe.io.Output {
  25. var p : Dynamic;
  26. var buf : haxe.io.Bytes;
  27. public function new(p) {
  28. this.p = p;
  29. buf = haxe.io.Bytes.alloc(1);
  30. }
  31. public override function close() {
  32. super.close();
  33. _stdin_close(p);
  34. }
  35. public override function writeByte(c) {
  36. buf.set(0,c);
  37. writeBytes(buf,0,1);
  38. }
  39. public override function writeBytes( buf : haxe.io.Bytes, pos : Int, len : Int ) : Int {
  40. var v = _stdin_write(p, buf.getData().b, pos, len);
  41. if( v < 0 ) throw new haxe.io.Eof();
  42. return v;
  43. }
  44. @:hlNative("std","process_stdin_write") static function _stdin_write( p : ProcessHandle, bytes : hl.types.Bytes, pos : Int, len : Int ) : Int { return 0; }
  45. @:hlNative("std","process_stdin_close") static function _stdin_close( p : ProcessHandle ) : Void { }
  46. }
  47. private class Stdout extends haxe.io.Input {
  48. var p : ProcessHandle;
  49. var out : Bool;
  50. var buf : haxe.io.Bytes;
  51. public function new(p,out) {
  52. this.p = p;
  53. this.out = out;
  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, len : Int ) : Int {
  62. var v = out ? _stdout_read(p,str.getData().b,pos,len) : _stderr_read(p,str.getData().b,pos,len);
  63. if( v < 0 ) throw new haxe.io.Eof();
  64. return v;
  65. }
  66. @:hlNative("std","process_stdout_read") static function _stdout_read( p : ProcessHandle, bytes : hl.types.Bytes, pos : Int, len : Int ) : Int { return 0; }
  67. @:hlNative("std","process_stderr_read") static function _stderr_read( p : ProcessHandle, bytes : hl.types.Bytes, pos : Int, len : Int ) : Int { return 0; }
  68. }
  69. @:access(Sys)
  70. @:coreApi class Process {
  71. var p : ProcessHandle;
  72. public var stdout(default,null) : haxe.io.Input;
  73. public var stderr(default,null) : haxe.io.Input;
  74. public var stdin(default,null) : haxe.io.Output;
  75. public function new( cmd : String, ?args : Array<String> ) : Void {
  76. @:privateAccess {
  77. var aargs = null;
  78. if( args != null ) {
  79. aargs = new hl.types.NativeArray<hl.types.Bytes>(args.length);
  80. for( i in 0...args.length )
  81. aargs[i] = Sys.getPath(args[i]);
  82. }
  83. p = _run(Sys.getPath(cmd), aargs);
  84. }
  85. if( p == null )
  86. throw new Sys.SysError("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. _kill(p);
  102. }
  103. @:hlNative("std","process_run") static function _run( cmd : hl.types.Bytes, args : hl.types.NativeArray<hl.types.Bytes> ) : ProcessHandle { return null; }
  104. @:hlNative("std", "process_exit") static function _exit( p : ProcessHandle ) : Int { return 0; }
  105. @:hlNative("std", "process_pid") static function _pid( p : ProcessHandle ) : Int { return 0; }
  106. @:hlNative("std","process_close") static function _close( p : ProcessHandle ) : Void { }
  107. @:hlNative("std","process_kill") static function _kill( p : ProcessHandle ) : Void { }
  108. }