Sys.hx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright (C)2005-2015 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. return untyped __call__("getenv", s);
  35. }
  36. public static function putEnv( s : String, v : String ) : Void {
  37. return untyped __call__("putenv", s + "=" + v);
  38. }
  39. public static function sleep( seconds : Float ) : Void {
  40. return untyped __call__("usleep", seconds*1000000);
  41. }
  42. public static function setTimeLocale( loc : String ) : Bool {
  43. return untyped __call__("setlocale", __php__("LC_TIME"), loc) != false;
  44. }
  45. public static function getCwd() : String {
  46. var cwd : String = untyped __call__("getcwd");
  47. var l = cwd.substr(-1);
  48. return cwd + (l == '/' || l == '\\' ? '' : '/');
  49. }
  50. public static function setCwd( s : String ) : Void {
  51. untyped __call__("chdir", s);
  52. }
  53. public static function systemName() : String {
  54. var s : String = untyped __call__("php_uname", "s");
  55. var p : Int;
  56. if((p = s.indexOf(" ")) >= 0)
  57. return s.substr(0, p);
  58. else
  59. return s;
  60. }
  61. static function escapeArgument( arg : String ) : String {
  62. var ok = true;
  63. for( i in 0...arg.length )
  64. switch( arg.charCodeAt(i) ) {
  65. case ' '.code, '\t'.code, '"'.code, '&'.code, '|'.code, '<'.code, '>'.code, '#'.code , ';'.code, '*'.code, '?'.code, '('.code, ')'.code, '{'.code, '}'.code, '$'.code:
  66. ok = false;
  67. case 0, 13, 10: // [eof] [cr] [lf]
  68. arg = arg.substr(0,i);
  69. }
  70. if( ok )
  71. return arg;
  72. return '"'+arg.split('\\').join("\\\\").split('"').join('\\"')+'"';
  73. }
  74. public static function command( cmd : String, ?args : Array<String> ) : Int {
  75. if( args != null ) {
  76. cmd = escapeArgument(cmd);
  77. for( a in args )
  78. cmd += " "+escapeArgument(a);
  79. }
  80. if (systemName() == "Windows") cmd = '"$cmd"';
  81. var result = 0;
  82. untyped __call__("system", cmd, result);
  83. return result;
  84. }
  85. public static function exit( code : Int ) : Void {
  86. untyped __call__("exit", code);
  87. }
  88. public static function time() : Float {
  89. return untyped __call__("microtime", true);
  90. }
  91. public static function cpuTime() : Float {
  92. return untyped __call__("microtime", true) - __php__("$_SERVER['REQUEST_TIME']");
  93. }
  94. public static function executablePath() : String {
  95. return untyped __php__("$_SERVER['SCRIPT_FILENAME']");
  96. }
  97. public static function environment() : Map<String,String> {
  98. return php.Lib.hashOfAssociativeArray(untyped __php__("$_SERVER"));
  99. }
  100. public static function stdin() : haxe.io.Input {
  101. return untyped new sys.io.FileInput(__call__('fopen', 'php://stdin', "r"));
  102. }
  103. public static function stdout() : haxe.io.Output {
  104. return untyped new sys.io.FileOutput(__call__('fopen', 'php://stdout', "w"));
  105. }
  106. public static function stderr() : haxe.io.Output {
  107. return untyped new sys.io.FileOutput(__call__('fopen', 'php://stderr', "w"));
  108. }
  109. public static function getChar( echo : Bool ) : Int {
  110. var v : Int = untyped __call__("fgetc", __php__("STDIN"));
  111. if(echo)
  112. untyped __call__('echo', v);
  113. return v;
  114. }
  115. }