Sys.hx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. var size = 0;
  41. return utf8Path ? s.bytes.utf16ToUtf8(0, size) : s.bytes;
  42. }
  43. static function makePath( b : hl.types.Bytes ) : String {
  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(s.bytes);
  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. var size = 0;
  88. return set_time_locale(loc.bytes.utf16ToUtf8(0,size));
  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. StringTools.quoteWinArg(a, true)
  109. ].join(" ");
  110. code = sys_command(getPath(cmd));
  111. case _:
  112. cmd = [cmd].concat(args).map(StringTools.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 makePath(sys_exe_path());
  123. }
  124. @:hlNative("std", "sys_utf8_path") static function sys_utf8_path() : Bool { return false; }
  125. @:hlNative("std","sys_time") public static function time() : Float { return 0.; };
  126. @:hlNative("std","sys_exit") public static function exit( code : Int ) : Void {};
  127. @:hlNative("std", "sys_cpu_time") public static function cpuTime() : Float { return 0.; };
  128. @:hlNative("std", "sys_get_char") public static function getChar( echo : Bool ) : Int { return 0; }
  129. @:hlNative("std","sys_print") static function sys_print( v : hl.types.Bytes ) : Void {};
  130. @:hlNative("std", "file_stdin") static function file_stdin() : sys.io.File.FileHandle { return null; }
  131. @:hlNative("std", "file_stdout") static function file_stdout() : sys.io.File.FileHandle { return null; }
  132. @:hlNative("std", "file_stderr") static function file_stderr() : sys.io.File.FileHandle { return null; }
  133. @:hlNative("std", "sys_args") static function sys_args() : hl.types.NativeArray<hl.types.Bytes> { return null; }
  134. @:hlNative("std", "sys_get_env") static function get_env( key : hl.types.Bytes ) : hl.types.Bytes { return null; }
  135. @:hlNative("std", "sys_put_env") static function put_env( key : hl.types.Bytes, val : hl.types.Bytes ) : Bool { return false; }
  136. @:hlNative("std", "sys_env") static function sys_env() : hl.types.NativeArray<hl.types.Bytes> { return null; }
  137. @:hlNative("std", "sys_set_time_locale") static function set_time_locale( loc : hl.types.Bytes ) : Bool { return true; }
  138. @:hlNative("std", "sys_get_cwd") static function get_cwd() : hl.types.Bytes { return null; }
  139. @:hlNative("std", "sys_set_cwd") static function set_cwd( path : hl.types.Bytes ) : Bool { return true; }
  140. @:hlNative("std", "sys_command") static function sys_command( cmd : hl.types.Bytes ) : Int { return 0; }
  141. @:hlNative("std", "sys_exe_path") static function sys_exe_path() : hl.types.Bytes { return null; }
  142. @:hlNative("std", "sys_string") static function sys_string() : hl.types.Bytes { return null; }
  143. }