Sys.hx 4.7 KB

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