Sys.hx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. /** Environment variables set by `Sys.putEnv()` */
  28. static var customEnvVars = new NativeAssocArray<String>();
  29. // we need to keep track of capitalization for windows
  30. static final isWindows = systemName() == "Windows";
  31. static var envCapitalization(get, null):Map<String, String>;
  32. static function get_envCapitalization():Map<String, String> {
  33. if (envCapitalization == null)
  34. return envCapitalization = [for (k => _ in SuperGlobal._SERVER) k.toUpperCase() => k];
  35. return envCapitalization;
  36. }
  37. static inline function addCustom(name:String, value:String):Void {
  38. customEnvVars[name] = value;
  39. if (!isWindows)
  40. return;
  41. final upperCase = name.toUpperCase();
  42. if (envCapitalization.exists(upperCase))
  43. return;
  44. envCapitalization[upperCase] = name;
  45. }
  46. static inline function removeCustom(name:String):Void {
  47. Global.unset(customEnvVars[name]);
  48. if (!isWindows)
  49. return;
  50. envCapitalization.remove(name.toUpperCase());
  51. }
  52. static inline function getCapitalization(name:String):String {
  53. if (!isWindows)
  54. return name;
  55. final existing = envCapitalization[name.toUpperCase()];
  56. if (existing != null)
  57. return existing;
  58. return name;
  59. }
  60. public static inline function print(v:Dynamic):Void {
  61. Global.echo(Std.string(v));
  62. }
  63. public static inline function println(v:Dynamic):Void {
  64. Global.echo(Std.string(v) + Const.PHP_EOL);
  65. }
  66. public static function args():Array<String> {
  67. if (Global.array_key_exists('argv', SuperGlobal._SERVER)) {
  68. return @:privateAccess Array.wrap(Global.array_slice(SuperGlobal._SERVER['argv'], 1));
  69. } else {
  70. return [];
  71. }
  72. }
  73. public static function getEnv(s:String):String {
  74. var value = Global.getenv(s);
  75. return value == false ? null : value;
  76. }
  77. public static function putEnv(s:String, v:Null<String>):Void {
  78. if (v == null) {
  79. removeCustom(s);
  80. Global.putenv('$s');
  81. } else {
  82. addCustom(s, v);
  83. Global.putenv('$s=$v');
  84. }
  85. }
  86. public static inline function sleep(seconds:Float):Void {
  87. return Global.usleep(Std.int(seconds * 1000000));
  88. }
  89. public static inline function setTimeLocale(loc:String):Bool {
  90. return Global.setlocale(Const.LC_TIME, loc) != false;
  91. }
  92. public static function getCwd():String {
  93. var cwd = Global.getcwd();
  94. if (cwd == false)
  95. return null;
  96. var l = (cwd : String).substr(-1);
  97. return (cwd : String) + (l == '/' || l == '\\' ? '' : '/');
  98. }
  99. public static inline function setCwd(s:String):Void {
  100. Global.chdir(s);
  101. }
  102. public static function systemName():String {
  103. var s = Global.php_uname('s');
  104. var p = s.indexOf(" ");
  105. return (p >= 0 ? s.substr(0, p) : s);
  106. }
  107. public static function command(cmd:String, ?args:Array<String>):Int {
  108. if (args != null) {
  109. switch (systemName()) {
  110. case "Windows":
  111. cmd = [
  112. for (a in [StringTools.replace(cmd, "/", "\\")].concat(args))
  113. SysTools.quoteWinArg(a, true)
  114. ].join(" ");
  115. case _:
  116. cmd = [cmd].concat(args).map(SysTools.quoteUnixArg).join(" ");
  117. }
  118. }
  119. var result = Boot.deref(0);
  120. Global.system(cmd, result);
  121. return result;
  122. }
  123. public static inline function exit(code:Int):Void {
  124. Global.exit(code);
  125. }
  126. public static inline function time():Float {
  127. return Global.microtime(true);
  128. }
  129. public static function cpuTime():Float {
  130. return time() - SuperGlobal._SERVER['REQUEST_TIME'];
  131. }
  132. @:deprecated("Use programPath instead") public static inline function executablePath():String {
  133. return SuperGlobal._SERVER['SCRIPT_FILENAME'];
  134. }
  135. // It has to be initialized before any call to Sys.setCwd()...
  136. static var _programPath = sys.FileSystem.fullPath(SuperGlobal._SERVER['SCRIPT_FILENAME']);
  137. public static function programPath():String {
  138. return _programPath;
  139. }
  140. public static function environment():Map<String, String> {
  141. var env = SuperGlobal._SERVER;
  142. Syntax.foreach(customEnvVars, function(name:String, value:String) {
  143. final actualName = getCapitalization(name);
  144. env[actualName] = value;
  145. });
  146. return php.Lib.hashOfAssociativeArray(env);
  147. }
  148. public static function stdin():haxe.io.Input {
  149. var p = Global.defined('STDIN') ? Const.STDIN : Global.fopen('php://stdin', 'r');
  150. return @:privateAccess new FileInput(p);
  151. }
  152. public static function stdout():haxe.io.Output {
  153. var p = Global.defined('STDOUT') ? Const.STDOUT : Global.fopen('php://stdout', 'w');
  154. return @:privateAccess new FileOutput(p);
  155. }
  156. public static function stderr():haxe.io.Output {
  157. var p = Global.defined('STDERR') ? Const.STDERR : Global.fopen('php://stderr', 'w');
  158. return @:privateAccess new FileOutput(p);
  159. }
  160. public static function getChar(echo:Bool):Int {
  161. var c = Global.fgetc(Const.STDIN);
  162. if (c == false) {
  163. return 0;
  164. } else {
  165. if (echo)
  166. Global.echo(c);
  167. return Global.ord(c);
  168. }
  169. }
  170. }