Sys.hx 4.7 KB

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