Sys.hx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. class SysError {
  2. public var msg : String;
  3. public function new(msg) {
  4. this.msg = msg;
  5. }
  6. @:keep public function toString() {
  7. return "SysError("+msg+")";
  8. }
  9. }
  10. @:coreApi
  11. @:keepInit
  12. @:access(String)
  13. class Sys {
  14. static var utf8Path : Bool;
  15. static function __init__() : Void {
  16. utf8Path = sys_utf8_path();
  17. }
  18. static function getPath( s : String ) : hl.types.Bytes {
  19. var size = 0;
  20. return utf8Path ? s.bytes.utf16ToUtf8(0, size) : s.bytes;
  21. }
  22. static function makePath( b : hl.types.Bytes ) : String {
  23. return utf8Path ? String.fromUTF8(b) : String.fromUCS2(b);
  24. }
  25. public static function print( v : Dynamic ) : Void {
  26. sys_print(Std.string(v).bytes);
  27. }
  28. public static function println( v : Dynamic ) : Void {
  29. sys_print(Std.string(v).bytes);
  30. sys_print("\n".bytes);
  31. }
  32. public static function args() : Array<String> {
  33. return [for( a in sys_args() ) makePath(a)];
  34. }
  35. public static function stdin() : haxe.io.Input {
  36. return @:privateAccess new sys.io.FileInput(file_stdin());
  37. }
  38. public static function stdout() : haxe.io.Output {
  39. return @:privateAccess new sys.io.FileOutput(file_stdout());
  40. }
  41. public static function stderr() : haxe.io.Output {
  42. return @:privateAccess new sys.io.FileOutput(file_stderr());
  43. }
  44. public static function getEnv( s : String ) : String {
  45. var v = get_env(s.bytes);
  46. if( v == null )
  47. return null;
  48. return makePath(v);
  49. }
  50. public static function putEnv( s : String, v : String ) : Void {
  51. put_env(getPath(s),if( v == null ) null else getPath(v));
  52. }
  53. public static function environment() : Map<String,String> {
  54. var env = sys_env();
  55. var h = new haxe.ds.StringMap();
  56. for( i in 0...env.length >> 1 ) {
  57. var p = i << 1;
  58. h.set(makePath(env[p]), makePath(env[p + 1]));
  59. }
  60. return h;
  61. }
  62. @:hlNative("std","sys_sleep")
  63. public static function sleep( seconds : Float ) : Void {
  64. }
  65. public static function setTimeLocale( loc : String ) : Bool {
  66. var size = 0;
  67. return set_time_locale(loc.bytes.utf16ToUtf8(0,size));
  68. }
  69. public static function getCwd() : String {
  70. return makePath(get_cwd());
  71. }
  72. public static function setCwd( s : String ) : Void {
  73. if( !set_cwd(getPath(s)) ) throw new SysError("Failed to set path to " + s);
  74. }
  75. public static function systemName() : String {
  76. return String.fromUCS2(sys_string());
  77. }
  78. public static function command( cmd : String, ?args : Array<String> ) : Int {
  79. var code = 0;
  80. var ok;
  81. if (args == null) {
  82. ok = sys_command(getPath(cmd), code);
  83. } else {
  84. switch (systemName()) {
  85. case "Windows":
  86. cmd = [
  87. for (a in [StringTools.replace(cmd, "/", "\\")].concat(args))
  88. StringTools.quoteWinArg(a, true)
  89. ].join(" ");
  90. ok = sys_command(getPath(cmd), code);
  91. case _:
  92. cmd = [cmd].concat(args).map(StringTools.quoteUnixArg).join(" ");
  93. ok = sys_command(getPath(cmd), code);
  94. }
  95. }
  96. if( !ok ) throw new SysError("Failed to run command " + cmd);
  97. return code;
  98. }
  99. public static function executablePath() : String {
  100. return makePath(sys_exe_path());
  101. }
  102. @:hlNative("std", "sys_utf8_path") static function sys_utf8_path() : Bool { return false; }
  103. @:hlNative("std","sys_time") public static function time() : Float { return 0.; };
  104. @:hlNative("std","sys_exit") public static function exit( code : Int ) : Void {};
  105. @:hlNative("std", "sys_cpu_time") public static function cpuTime() : Float { return 0.; };
  106. @:hlNative("std", "sys_get_char") public static function getChar( echo : Bool ) : Int { return 0; }
  107. @:hlNative("std","sys_print") static function sys_print( v : hl.types.Bytes ) : Void {};
  108. @:hlNative("std", "file_stdin") static function file_stdin() : sys.io.File.FileHandle { return null; }
  109. @:hlNative("std", "file_stdout") static function file_stdout() : sys.io.File.FileHandle { return null; }
  110. @:hlNative("std", "file_stderr") static function file_stderr() : sys.io.File.FileHandle { return null; }
  111. @:hlNative("std", "sys_args") static function sys_args() : hl.types.NativeArray<hl.types.Bytes> { return null; }
  112. @:hlNative("std", "sys_get_env") static function get_env( key : hl.types.Bytes ) : hl.types.Bytes { return null; }
  113. @:hlNative("std", "sys_put_env") static function put_env( key : hl.types.Bytes, val : hl.types.Bytes ) : Void {}
  114. @:hlNative("std", "sys_env") static function sys_env() : hl.types.NativeArray<hl.types.Bytes> { return null; }
  115. @:hlNative("std", "sys_set_time_locale") static function set_time_locale( loc : hl.types.Bytes ) : Bool { return true; }
  116. @:hlNative("std", "sys_get_cwd") static function get_cwd() : hl.types.Bytes { return null; }
  117. @:hlNative("std", "sys_set_cwd") static function set_cwd( path : hl.types.Bytes ) : Bool { return true; }
  118. @:hlNative("std", "sys_command") static function sys_command( cmd : hl.types.Bytes, code : hl.types.Ref<Int> ) : Bool { return false; }
  119. @:hlNative("std", "sys_exe_path") static function sys_exe_path() : hl.types.Bytes { return null; }
  120. @:hlNative("std", "sys_string") static function sys_string() : hl.types.Bytes { return null; }
  121. }