Sys.hx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. class SysError {
  24. public var msg : String;
  25. public function new(msg) {
  26. this.msg = msg;
  27. }
  28. @:keep public function toString() {
  29. return "SysError("+msg+")";
  30. }
  31. }
  32. @:coreApi
  33. @:keepInit
  34. @:access(String)
  35. class Sys {
  36. static var utf8Path : Bool;
  37. static function __init__() : Void {
  38. utf8Path = sys_utf8_path();
  39. }
  40. static function getPath( s : String ) : hl.Bytes {
  41. return utf8Path ? s.bytes.utf16ToUtf8(0, null) : s.bytes;
  42. }
  43. static function makePath( b : hl.Bytes ) : String {
  44. if( b == null ) return null;
  45. return utf8Path ? String.fromUTF8(b) : String.fromUCS2(b);
  46. }
  47. public static function print( v : Dynamic ) : Void {
  48. sys_print(Std.string(v).bytes);
  49. }
  50. public static function println( v : Dynamic ) : Void {
  51. sys_print(Std.string(v).bytes);
  52. sys_print("\n".bytes);
  53. }
  54. public static function args() : Array<String> {
  55. return [for( a in sys_args() ) makePath(a)];
  56. }
  57. public static function stdin() : haxe.io.Input {
  58. return @:privateAccess new sys.io.FileInput(file_stdin());
  59. }
  60. public static function stdout() : haxe.io.Output {
  61. return @:privateAccess new sys.io.FileOutput(file_stdout());
  62. }
  63. public static function stderr() : haxe.io.Output {
  64. return @:privateAccess new sys.io.FileOutput(file_stderr());
  65. }
  66. public static function getEnv( s : String ) : String {
  67. var v = get_env(getPath(s));
  68. if( v == null )
  69. return null;
  70. return makePath(v);
  71. }
  72. public static function putEnv( s : String, v : String ) : Void {
  73. if( !put_env(getPath(s), if( v == null ) null else getPath(v)) ) throw "putEnv() failure";
  74. }
  75. public static function environment() : Map<String,String> {
  76. var env = sys_env();
  77. var h = new haxe.ds.StringMap();
  78. for( i in 0...env.length >> 1 ) {
  79. var p = i << 1;
  80. h.set(makePath(env[p]), makePath(env[p + 1]));
  81. }
  82. return h;
  83. }
  84. @:hlNative("std","sys_sleep")
  85. public static function sleep( seconds : Float ) : Void {
  86. }
  87. public static function setTimeLocale( loc : String ) : Bool {
  88. return set_time_locale(loc.bytes.utf16ToUtf8(0,null));
  89. }
  90. public static function getCwd() : String {
  91. return makePath(get_cwd());
  92. }
  93. public static function setCwd( s : String ) : Void {
  94. if( !set_cwd(getPath(s)) ) throw new SysError("Failed to set path to " + s);
  95. }
  96. public static function systemName() : String {
  97. return String.fromUCS2(sys_string());
  98. }
  99. public static function command( cmd : String, ?args : Array<String> ) : Int {
  100. var code = 0;
  101. if (args == null) {
  102. code = sys_command(getPath(cmd));
  103. } else {
  104. switch (systemName()) {
  105. case "Windows":
  106. cmd = [
  107. for (a in [StringTools.replace(cmd, "/", "\\")].concat(args))
  108. SysTools.quoteWinArg(a, true)
  109. ].join(" ");
  110. code = sys_command(getPath(cmd));
  111. case _:
  112. cmd = [cmd].concat(args).map(SysTools.quoteUnixArg).join(" ");
  113. code = sys_command(getPath(cmd));
  114. }
  115. }
  116. return code;
  117. }
  118. @:deprecated("Use programPath instead") public static function executablePath() : String {
  119. return makePath(sys_exe_path());
  120. }
  121. public static function programPath() : String {
  122. return sys_program_path;
  123. }
  124. private static var sys_program_path = {
  125. var hlFile = sys_hl_file();
  126. if( hlFile == null )
  127. makePath( sys_exe_path() );
  128. else
  129. sys.FileSystem.fullPath( makePath(hlFile) );
  130. }
  131. @:hlNative("std", "sys_utf8_path") static function sys_utf8_path() : Bool { return false; }
  132. @:hlNative("std","sys_time") public static function time() : Float { return 0.; };
  133. @:hlNative("std","sys_exit") public static function exit( code : Int ) : Void {};
  134. @:hlNative("std", "sys_cpu_time") public static function cpuTime() : Float { return 0.; };
  135. @:hlNative("std", "sys_get_char") public static function getChar( echo : Bool ) : Int { return 0; }
  136. @:hlNative("std","sys_print") static function sys_print( v : hl.Bytes ) : Void {};
  137. @:hlNative("std", "file_stdin") static function file_stdin() : sys.io.File.FileHandle { return null; }
  138. @:hlNative("std", "file_stdout") static function file_stdout() : sys.io.File.FileHandle { return null; }
  139. @:hlNative("std", "file_stderr") static function file_stderr() : sys.io.File.FileHandle { return null; }
  140. @:hlNative("std", "sys_args") static function sys_args() : hl.NativeArray<hl.Bytes> { return null; }
  141. @:hlNative("std", "sys_get_env") static function get_env( key : hl.Bytes ) : hl.Bytes { return null; }
  142. @:hlNative("std", "sys_put_env") static function put_env( key : hl.Bytes, val : hl.Bytes ) : Bool { return false; }
  143. @:hlNative("std", "sys_env") static function sys_env() : hl.NativeArray<hl.Bytes> { return null; }
  144. @:hlNative("std", "sys_set_time_locale") static function set_time_locale( loc : hl.Bytes ) : Bool { return true; }
  145. @:hlNative("std", "sys_get_cwd") static function get_cwd() : hl.Bytes { return null; }
  146. @:hlNative("std", "sys_set_cwd") static function set_cwd( path : hl.Bytes ) : Bool { return true; }
  147. @:hlNative("std", "sys_command") static function sys_command( cmd : hl.Bytes ) : Int { return 0; }
  148. @:hlNative("std", "sys_exe_path") static function sys_exe_path() : hl.Bytes { return null; }
  149. @:hlNative("std", "sys_hl_file") static function sys_hl_file() : hl.Bytes { return null; }
  150. @:hlNative("std", "sys_string") static function sys_string() : hl.Bytes { return null; }
  151. }