Process.hx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. private class Stdin extends haxe.io.Output {
  24. var p:Dynamic;
  25. var buf:haxe.io.Bytes;
  26. public function new(p:Dynamic) {
  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: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. 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>, ?detached:Bool):Void {
  78. if (detached)
  79. throw "Detached process is not supported on this platform";
  80. p = try _run(untyped cmd.__s, neko.Lib.haxeToNeko(args)) catch (e:Dynamic) throw "Process creation failure : " + cmd;
  81. stdin = new Stdin(p);
  82. stdout = new Stdout(p, true);
  83. stderr = new Stdout(p, false);
  84. }
  85. public function getPid():Int {
  86. return _pid(p);
  87. }
  88. public function exitCode(block:Bool = true):Null<Int> {
  89. if (block == false)
  90. throw "Non blocking exitCode() not supported on this platform";
  91. return _exit(p);
  92. }
  93. public function close():Void {
  94. _close(p);
  95. }
  96. public function kill():Void {
  97. _kill(p);
  98. }
  99. static var _run = neko.Lib.load("std", "process_run", 2);
  100. static var _exit = neko.Lib.load("std", "process_exit", 1);
  101. static var _pid = neko.Lib.load("std", "process_pid", 1);
  102. static var _close = neko.Lib.loadLazy("std", "process_close", 1);
  103. static var _kill = neko.Lib.loadLazy("std", "process_kill", 1);
  104. }