Sys.hx 3.9 KB

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