Sys.hx 4.7 KB

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