Sys.hx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 haxe.SysTools;
  23. @:haxe.warning("-WUnboundMonomorph")
  24. @:coreApi class Sys {
  25. public static function print( v : Dynamic ) : Void {
  26. untyped __dollar__print(v);
  27. }
  28. public static function println( v : Dynamic ) : Void {
  29. untyped __dollar__print(v,"\n");
  30. }
  31. public static function getChar( echo : Bool ) : Int {
  32. return getch(echo);
  33. }
  34. public static function stdin() : haxe.io.Input {
  35. return untyped new sys.io.FileInput(file_stdin());
  36. }
  37. public static function stdout() : haxe.io.Output {
  38. return untyped new sys.io.FileOutput(file_stdout());
  39. }
  40. public static function stderr() : haxe.io.Output {
  41. return untyped new sys.io.FileOutput(file_stderr());
  42. }
  43. public static function args() : Array<String> untyped {
  44. var a = __dollar__loader.args;
  45. if( __dollar__typeof(a) != __dollar__tarray )
  46. return [];
  47. var r = new Array();
  48. var i = 0;
  49. var l = __dollar__asize(a);
  50. while( i < l ) {
  51. if( __dollar__typeof(a[i]) == __dollar__tstring )
  52. r.push(new String(a[i]));
  53. i += 1;
  54. }
  55. return r;
  56. }
  57. public static function getEnv( s : String ) : String {
  58. var v = get_env(untyped s.__s);
  59. if( v == null )
  60. return null;
  61. return new String(v);
  62. }
  63. public static function putEnv( s : String, v : Null<String> ) : Void {
  64. untyped put_env(s.__s,if( v == null ) null else v.__s);
  65. }
  66. public static function sleep( seconds : Float ) : Void {
  67. _sleep(seconds);
  68. }
  69. public static function setTimeLocale( loc : String ) : Bool {
  70. return set_time_locale(untyped loc.__s);
  71. }
  72. public static function getCwd() : String {
  73. return new String(get_cwd());
  74. }
  75. public static function setCwd( s : String ) : Void {
  76. set_cwd(untyped s.__s);
  77. }
  78. public static function systemName() : String {
  79. return new String(sys_string());
  80. }
  81. public static function command( cmd : String, ?args : Array<String> ) : Int {
  82. if (args == null) {
  83. return sys_command(untyped cmd.__s);
  84. } else {
  85. switch (systemName()) {
  86. case "Windows":
  87. cmd = [
  88. for (a in [StringTools.replace(cmd, "/", "\\")].concat(args))
  89. SysTools.quoteWinArg(a, true)
  90. ].join(" ");
  91. return sys_command(untyped cmd.__s);
  92. case _:
  93. cmd = [cmd].concat(args).map(SysTools.quoteUnixArg).join(" ");
  94. return sys_command(untyped cmd.__s);
  95. }
  96. }
  97. }
  98. public static function exit( code : Int ) : Void {
  99. sys_exit(code);
  100. }
  101. public static function time() : Float {
  102. return sys_time();
  103. }
  104. public static function cpuTime() : Float {
  105. return sys_cpu_time();
  106. }
  107. @:deprecated("Use programPath instead") public static function executablePath() : String {
  108. return new String(sys_exe_path());
  109. }
  110. public static function programPath() : String {
  111. #if macro
  112. return null;
  113. #elseif interp
  114. return new String(sys_program_path());
  115. #else
  116. return sys_program_path;
  117. #end
  118. }
  119. public static function environment() : Map<String,String> {
  120. var l : Array<Dynamic> = sys_env();
  121. var h = new haxe.ds.StringMap();
  122. while( l != null ) {
  123. h.set(new String(l[0]),new String(l[1]));
  124. l = l[2];
  125. }
  126. return h;
  127. }
  128. private static var get_env = neko.Lib.load("std","get_env",1);
  129. private static var put_env = neko.Lib.load("std","put_env",2);
  130. private static var _sleep = neko.Lib.load("std","sys_sleep",1);
  131. private static var set_time_locale = neko.Lib.load("std","set_time_locale",1);
  132. private static var get_cwd = neko.Lib.load("std","get_cwd",0);
  133. private static var set_cwd = neko.Lib.load("std","set_cwd",1);
  134. private static var sys_string = neko.Lib.load("std","sys_string",0);
  135. private static var sys_command = neko.Lib.load("std","sys_command",1);
  136. private static var sys_exit = neko.Lib.load("std","sys_exit",1);
  137. private static var sys_time = neko.Lib.load("std","sys_time",0);
  138. private static var sys_cpu_time = neko.Lib.load("std","sys_cpu_time",0);
  139. private static var sys_exe_path = neko.Lib.load("std","sys_exe_path",0);
  140. #if interp
  141. private static var sys_program_path = neko.Lib.load("std","sys_program_path",0);
  142. #elseif !macro
  143. // It has to be initialized before any call to loadModule or Sys.setCwd()...
  144. private static var sys_program_path = {
  145. var m = neko.vm.Module.local().name;
  146. if (m == "") { // it is likely neko embedded in an exe
  147. var exe = new String(sys_exe_path());
  148. try {
  149. sys.FileSystem.fullPath(exe);
  150. } catch (e:Dynamic) {
  151. exe;
  152. }
  153. } else {
  154. try {
  155. sys.FileSystem.fullPath(m);
  156. } catch (e:Dynamic) {
  157. // maybe the neko module name was supplied without .n extension...
  158. if (!StringTools.endsWith(m, ".n")) {
  159. try {
  160. sys.FileSystem.fullPath(m + ".n");
  161. } catch (e:Dynamic) {
  162. m;
  163. }
  164. } else {
  165. m;
  166. }
  167. }
  168. }
  169. }
  170. #end
  171. private static var sys_env = neko.Lib.load("std","sys_env",0);
  172. private static var file_stdin = neko.Lib.load("std","file_stdin",0);
  173. private static var file_stdout = neko.Lib.load("std","file_stdout",0);
  174. private static var file_stderr = neko.Lib.load("std","file_stderr",0);
  175. private static var getch = neko.Lib.load("std","sys_getch",1);
  176. }