Sys.hx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright (c) 2005-2012, The haXe Project Contributors
  3. * All rights reserved.
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * - Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * - Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
  17. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  20. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  23. * DAMAGE.
  24. */
  25. @:core_api class Sys {
  26. public static function print( v : Dynamic ) : Void {
  27. untyped __call__("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. return untyped __call__('array_key_exists', 'argv', __var__('_SERVER')) ? __call__('new _hx_array', __call__('array_slice', __var__('_SERVER', 'argv'), 1)) : [];
  35. }
  36. public static function getEnv( s : String ) : String {
  37. return untyped __call__("getenv", s);
  38. }
  39. public static function putEnv( s : String, v : String ) : Void {
  40. return untyped __call__("putenv", s + "=" + v);
  41. }
  42. public static function sleep( seconds : Float ) : Void {
  43. return untyped __call__("usleep", seconds*1000000);
  44. }
  45. public static function setTimeLocale( loc : String ) : Bool {
  46. return untyped __call__("setlocale", __php__("LC_TIME"), loc) != false;
  47. }
  48. public static function getCwd() : String {
  49. var cwd : String = untyped __call__("getcwd");
  50. var l = cwd.substr(-1);
  51. return cwd + (l == '/' || l == '\\' ? '' : '/');
  52. }
  53. public static function setCwd( s : String ) : Void {
  54. untyped __call__("chdir", s);
  55. }
  56. public static function systemName() : String {
  57. var s : String = untyped __call__("php_uname", "s");
  58. var p : Int;
  59. if((p = s.indexOf(" ")) >= 0)
  60. return s.substr(0, p);
  61. else
  62. return s;
  63. }
  64. static function escapeArgument( arg : String ) : String {
  65. var ok = true;
  66. for( i in 0...arg.length )
  67. switch( arg.charCodeAt(i) ) {
  68. case 32, 34: // [space] "
  69. ok = false;
  70. case 0, 13, 10: // [eof] [cr] [lf]
  71. arg = arg.substr(0,i);
  72. }
  73. if( ok )
  74. return arg;
  75. return '"'+arg.split('"').join('\\"')+'"';
  76. }
  77. public static function command( cmd : String, ?args : Array<String> ) : Int {
  78. if( args != null ) {
  79. cmd = escapeArgument(cmd);
  80. for( a in args )
  81. cmd += " "+escapeArgument(a);
  82. }
  83. var result = 0;
  84. untyped __call__("system", cmd, result);
  85. return result;
  86. }
  87. public static function exit( code : Int ) : Void {
  88. untyped __call__("exit", code);
  89. }
  90. public static function time() : Float {
  91. return untyped __call__("microtime", true);
  92. }
  93. public static function cpuTime() : Float {
  94. return untyped __call__("microtime", true) - __php__("$_SERVER['REQUEST_TIME']");
  95. }
  96. public static function executablePath() : String {
  97. return untyped __php__("$_SERVER['SCRIPT_FILENAME']");
  98. }
  99. public static function environment() : Hash<String> {
  100. return php.Lib.hashOfAssociativeArray(untyped __php__("$_SERVER"));
  101. }
  102. public static function stdin() : haxe.io.Input {
  103. return untyped new sys.io.FileInput(__call__('fopen', 'php://stdin', "r"));
  104. }
  105. public static function stdout() : haxe.io.Output {
  106. return untyped new sys.io.FileOutput(__call__('fopen', 'php://stdout', "w"));
  107. }
  108. public static function stderr() : haxe.io.Output {
  109. return untyped new sys.io.FileOutput(__call__('fopen', 'php://stderr', "w"));
  110. }
  111. public static function getChar( echo : Bool ) : Int {
  112. var v : Int = untyped __call__("fgetc", __php__("STDIN"));
  113. if(echo)
  114. untyped __call__('echo', v);
  115. return v;
  116. }
  117. }