Sys.hx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. @:coreApi class Sys {
  23. public static function print( v : Dynamic ) : Void {
  24. untyped __call__("echo", Std.string(v));
  25. }
  26. public static function println( v : Dynamic ) : Void {
  27. print(v);
  28. print("\n");
  29. }
  30. public static function args() : Array<String> {
  31. return untyped __call__('array_key_exists', 'argv', __var__('_SERVER')) ? __call__('new _hx_array', __call__('array_slice', __var__('_SERVER', 'argv'), 1)) : [];
  32. }
  33. public static function getEnv( s : String ) : String {
  34. var ret:Dynamic = untyped __call__("getenv", s);
  35. return ret == false ? null : ret;
  36. }
  37. public static function putEnv( s : String, v : String ) : Void {
  38. return untyped __call__("putenv", s + "=" + v);
  39. }
  40. public static function sleep( seconds : Float ) : Void {
  41. return untyped __call__("usleep", seconds*1000000);
  42. }
  43. public static function setTimeLocale( loc : String ) : Bool {
  44. return untyped __call__("setlocale", __php__("LC_TIME"), loc) != false;
  45. }
  46. public static function getCwd() : String {
  47. var cwd : String = untyped __call__("getcwd");
  48. var l = cwd.substr(-1);
  49. return cwd + (l == '/' || l == '\\' ? '' : '/');
  50. }
  51. public static function setCwd( s : String ) : Void {
  52. untyped __call__("chdir", s);
  53. }
  54. public static function systemName() : String {
  55. var s : String = untyped __call__("php_uname", "s");
  56. var p : Int;
  57. if((p = s.indexOf(" ")) >= 0)
  58. return s.substr(0, p);
  59. else
  60. return s;
  61. }
  62. public static function command( cmd : String, ?args : Array<String> ) : Int {
  63. if (args != null) {
  64. switch (systemName()) {
  65. case "Windows":
  66. cmd = [
  67. for (a in [StringTools.replace(cmd, "/", "\\")].concat(args))
  68. StringTools.quoteWinArg(a, true)
  69. ].join(" ");
  70. case _:
  71. cmd = [cmd].concat(args).map(StringTools.quoteUnixArg).join(" ");
  72. }
  73. }
  74. var result = 0;
  75. untyped __call__("system", cmd, result);
  76. return result;
  77. }
  78. public static function exit( code : Int ) : Void {
  79. untyped __call__("exit", code);
  80. }
  81. public static function time() : Float {
  82. return untyped __call__("microtime", true);
  83. }
  84. public static function cpuTime() : Float {
  85. return untyped __call__("microtime", true) - __php__("$_SERVER['REQUEST_TIME']");
  86. }
  87. @:deprecated("Use programPath instead") public static function executablePath() : String {
  88. return untyped __php__("$_SERVER['SCRIPT_FILENAME']");
  89. }
  90. // It has to be initialized before any call to Sys.setCwd()...
  91. static var _programPath = sys.FileSystem.fullPath(untyped __php__("$_SERVER['SCRIPT_FILENAME']"));
  92. public static function programPath() : String {
  93. return _programPath;
  94. }
  95. public static function environment() : Map<String,String> {
  96. return php.Lib.hashOfAssociativeArray(untyped __php__("$_SERVER"));
  97. }
  98. public static function stdin() : haxe.io.Input {
  99. var p = untyped __php__("defined('STDIN') ? STDIN : fopen('php://stdin', 'r')");
  100. return untyped new sys.io.FileInput(p);
  101. }
  102. public static function stdout() : haxe.io.Output {
  103. var p = untyped __php__("defined('STDOUT') ? STDOUT : fopen('php://stdout', 'w')");
  104. return untyped new sys.io.FileOutput(p);
  105. }
  106. public static function stderr() : haxe.io.Output {
  107. var p = untyped __php__("defined('STDERR') ? STDERR : fopen('php://stderr', 'w')");
  108. return untyped new sys.io.FileOutput(p);
  109. }
  110. public static function getChar( echo : Bool ) : Int {
  111. var v : Int = untyped __call__("fgetc", __php__("STDIN"));
  112. if(echo)
  113. untyped __call__('echo', v);
  114. return v;
  115. }
  116. }