Sys.hx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. import java.lang.System;
  23. import java.net.URI;
  24. import java.nio.file.Paths;
  25. import sys.io.Process;
  26. using haxe.Int64;
  27. using jvm.NativeTools;
  28. @:coreApi class Sys {
  29. private static var _args:java.NativeArray<String>;
  30. private static var _env:haxe.ds.StringMap<String>;
  31. private static var _sysName:String;
  32. public static inline function print(v:Dynamic):Void {
  33. java.lang.System.out.print(v);
  34. }
  35. public static inline function println(v:Dynamic):Void {
  36. java.lang.System.out.println(v);
  37. }
  38. public static function args():Array<String> {
  39. if (_args == null)
  40. return [];
  41. return @:privateAccess Array.ofNative(_args);
  42. }
  43. public static function getEnv(s:String):Null<String> {
  44. return java.lang.System.getenv(s);
  45. }
  46. public static function putEnv(s:String, v:Null<String>):Void {
  47. // java offers no support for it (!)
  48. throw new haxe.exceptions.NotImplementedException("Not implemented in this platform");
  49. }
  50. public static function environment():Map<String, String> {
  51. if (_env == null) {
  52. _env = new haxe.ds.StringMap();
  53. for (mv in java.lang.System.getenv().entrySet())
  54. _env.set(mv.getKey(), mv.getValue());
  55. }
  56. return _env.copy();
  57. }
  58. public static function sleep(seconds:Float):Void {
  59. try
  60. java.lang.Thread.sleep(cast seconds * 1000)
  61. catch (e:Dynamic)
  62. throw e;
  63. }
  64. public static function setTimeLocale(loc:String):Bool {
  65. return false;
  66. }
  67. public static function getCwd():String {
  68. return new java.io.File(".").getAbsolutePath().substr(0, -1);
  69. }
  70. public static function setCwd(s:String):Void {
  71. // java offers no support for it (!)
  72. throw new haxe.exceptions.NotImplementedException();
  73. }
  74. public static function systemName():String {
  75. if (_sysName != null)
  76. return _sysName;
  77. var sname = System.getProperty("os.name").toLowerCase();
  78. if (sname.indexOf("win") >= 0)
  79. return _sysName = "Windows";
  80. if (sname.indexOf("mac") >= 0)
  81. return _sysName = "Mac";
  82. if (sname.indexOf("nux") >= 0)
  83. return _sysName = "Linux";
  84. if (sname.indexOf("nix") >= 0)
  85. return _sysName = "BSD";
  86. return _sysName = System.getProperty("os.name");
  87. }
  88. public static function command(cmd:String, ?args:Array<String>):Int {
  89. var pb = Process.createProcessBuilder(cmd, args);
  90. #if java6
  91. pb.redirectErrorStream(true);
  92. #else
  93. pb.redirectOutput(java.lang.ProcessBuilder.ProcessBuilder_Redirect.INHERIT);
  94. pb.redirectError(java.lang.ProcessBuilder.ProcessBuilder_Redirect.INHERIT);
  95. #end
  96. var proc = pb.start();
  97. #if java6
  98. var reader = new java.io.NativeInput(proc.getInputStream());
  99. try {
  100. while (true) {
  101. var ln = reader.readLine();
  102. Sys.println(ln);
  103. }
  104. } catch (e:haxe.io.Eof) {}
  105. #end
  106. proc.waitFor();
  107. var exitCode = proc.exitValue();
  108. proc.destroy();
  109. return exitCode;
  110. }
  111. public static function exit(code:Int):Void {
  112. System.exit(code);
  113. }
  114. public static function time():Float {
  115. return cast(System.currentTimeMillis(), Float) / 1000;
  116. }
  117. public static function cpuTime():Float {
  118. return cast(System.nanoTime(), Float) / 1000000000;
  119. }
  120. @:deprecated("Use programPath instead") public static function executablePath():String {
  121. return getCwd();
  122. }
  123. public static function programPath():String {
  124. final uri:URI = Sys.native().getProtectionDomain().getCodeSource().getLocation().toURI();
  125. return Std.string(Paths.get(uri));
  126. }
  127. public static function getChar(echo:Bool):Int {
  128. // TODO
  129. return throw new haxe.exceptions.NotImplementedException();
  130. }
  131. public static function stdin():haxe.io.Input {
  132. var _in:java.io.InputStream = Reflect.field(System, "in");
  133. return new jvm.io.NativeInput(_in);
  134. }
  135. public static function stdout():haxe.io.Output {
  136. return new jvm.io.NativeOutput(System.out);
  137. }
  138. public static function stderr():haxe.io.Output {
  139. return new jvm.io.NativeOutput(System.err);
  140. }
  141. }