Process.hx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright (C)2005-2019 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. import cpp.NativeProcess;
  24. private class Stdin extends haxe.io.Output {
  25. var p:Dynamic;
  26. var buf:haxe.io.Bytes;
  27. public function new(p:Dynamic) {
  28. this.p = p;
  29. buf = haxe.io.Bytes.alloc(1);
  30. }
  31. public override function close() {
  32. super.close();
  33. NativeProcess.process_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. try {
  41. return NativeProcess.process_stdin_write(p, buf.getData(), pos, len);
  42. } catch (e:Dynamic) {
  43. throw new haxe.io.Eof();
  44. }
  45. return 0;
  46. }
  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:Dynamic, 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. var result:Int;
  64. try {
  65. result = out ? NativeProcess.process_stdout_read(p, str.getData(), pos, len) : NativeProcess.process_stderr_read(p, str.getData(), pos, len);
  66. } catch (e:Dynamic) {
  67. throw new haxe.io.Eof();
  68. }
  69. if (result == 0)
  70. throw new haxe.io.Eof();
  71. return result;
  72. }
  73. }
  74. @:coreApi
  75. class Process {
  76. var p:Dynamic;
  77. public var stdout(default, null):haxe.io.Input;
  78. public var stderr(default, null):haxe.io.Input;
  79. public var stdin(default, null):haxe.io.Output;
  80. public function new(cmd:String, ?args:Array<String>, ?detached:Bool):Void {
  81. if (detached)
  82. throw "Detached process is not supported on this platform";
  83. p = try NativeProcess.process_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 NativeProcess.process_pid(p);
  90. }
  91. public function exitCode(block:Bool = true):Null<Int> {
  92. if (block == false)
  93. throw "Non blocking exitCode() not supported on this platform";
  94. return NativeProcess.process_exit(p);
  95. }
  96. public function close():Void {
  97. NativeProcess.process_close(p);
  98. }
  99. public function kill():Void {
  100. NativeProcess.process_kill(p);
  101. }
  102. }