Sys.hx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. using lua.NativeStringTools;
  23. import lua.Package;
  24. import lua.Lua;
  25. import lua.Table;
  26. import lua.TableTools;
  27. import lua.Os;
  28. import lua.lib.lfs.Lfs;
  29. import lua.FileHandle;
  30. import lua.Io;
  31. import lua.Boot;
  32. import sys.io.FileInput;
  33. import sys.io.FileOutput;
  34. @:coreApi
  35. class Sys {
  36. public static inline function print( v : Dynamic ) : Void {
  37. return lua.Lib.print(v);
  38. }
  39. public static inline function println( v : Dynamic ) : Void {
  40. return lua.Lib.println(v);
  41. }
  42. public inline static function args() : Array<String> {
  43. var args = lua.Lib.tableToArray(lua.Lua.arg).copy();
  44. args.shift();
  45. return args;
  46. }
  47. public static function command( cmd : String, ?args : Array<String> ) : Int {
  48. cmd = Boot.shellEscapeCmd(cmd, args);
  49. #if (lua_ver < 5.2)
  50. return Os.execute(cmd);
  51. #elseif (lua_ver >= 5.2)
  52. return Os.execute(cmd).status;
  53. #else
  54. var ret = TableTools.pack(untyped os.execute(cmd));
  55. if (ret[3] != null) return ret[3]
  56. else return ret[1];
  57. #end
  58. }
  59. public inline static function cpuTime() : Float {
  60. return lua.Os.clock();
  61. }
  62. public inline static function exit(code : Int) : Void {
  63. lua.Os.exit(code);
  64. }
  65. public inline static function getChar(echo : Bool) : Int {
  66. return lua.Io.read(1).byte();
  67. }
  68. public static function systemName() : String {
  69. switch(Package.config.sub(1,1).match){
  70. case "/" : {
  71. var f = Lua.assert(lua.Io.popen("uname"));
  72. var s = Lua.assert(f.read(All));
  73. f.close();
  74. s = s.gsub('^%s+', '');
  75. s = s.gsub('%s+$', '');
  76. s = s.gsub('[\n\r]+', ' ');
  77. if (s == "Darwin") return "Mac";
  78. else if (s.lower().find("bsd") != null) return "BSD";
  79. else return "Linux";
  80. }
  81. case "\\" : return "Windows";
  82. default : return null;
  83. }
  84. }
  85. public inline static function environment() : Map<String,String> {
  86. throw "not supported";
  87. return new Map();
  88. }
  89. public inline static function executablePath() : String {
  90. throw "not supported";
  91. return null;
  92. }
  93. public inline static function programPath() : String {
  94. return haxe.io.Path.join([getCwd(), Lua.arg[0]]);
  95. }
  96. public inline static function getCwd() : String {
  97. return lua.lib.lfs.Lfs.currentdir();
  98. }
  99. public inline static function setCwd(s : String) : Void {
  100. lua.lib.lfs.Lfs.chdir(s);
  101. }
  102. public inline static function getEnv(s : String) : String {
  103. return lua.Os.getenv(s);
  104. }
  105. public inline static function putEnv(s : String, v : String ) : Void {
  106. throw "not supported";
  107. }
  108. public inline static function setTimeLocale(loc : String) : Bool {
  109. // TODO Verify
  110. return lua.Os.setlocale(loc) != null;
  111. }
  112. public static function sleep(seconds : Float) : Void {
  113. if (seconds <= 0) return;
  114. if (Sys.systemName() == "Windows") {
  115. Sys.command("ping -n " + (seconds+1) + " localhost > NUL");
  116. } else {
  117. Sys.command('sleep $seconds');
  118. }
  119. }
  120. public inline static function stderr() : haxe.io.Output return new FileOutput(Io.stderr);
  121. public inline static function stdin() : haxe.io.Input return new FileInput(Io.stdin);
  122. public inline static function stdout() : haxe.io.Output return new FileOutput(Io.stdout);
  123. public static function time() : Float return lua.Os.time();
  124. }