Sys.hx 6.0 KB

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