2
0

Sys.hx 5.9 KB

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