Sys.hx 4.8 KB

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