Sys.hx 4.7 KB

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