Sys.hx 6.2 KB

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