Sys.hx 4.8 KB

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