Sys.hx 6.1 KB

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