Sys.hx 5.9 KB

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