2
0

Sys.hx 4.4 KB

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