Fix for some odd problems when reading from a non-tty
authorTim Starling <tstarling@users.mediawiki.org>
Tue, 1 Nov 2005 00:57:13 +0000 (00:57 +0000)
committerTim Starling <tstarling@users.mediawiki.org>
Tue, 1 Nov 2005 00:57:13 +0000 (00:57 +0000)
install-utils.inc

index 6de9377..5764c52 100644 (file)
@@ -57,10 +57,16 @@ function copydirectory( $source, $dest ) {
 }
 
 function readconsole( $prompt = '' ) {
-       if ( function_exists( 'posix_isatty' ) && posix_isatty( STDIN ) ) {
-               $isatty = true;
-       } else {
-               $isatty = false;
+       static $isatty = null, $fp = null;
+       if ( is_null( $fp ) ) {
+               $fp = fopen( 'php://stdin', 'r' );
+       }
+       if ( is_null( $isatty ) ) {
+               if ( !function_exists( 'posix_isatty' ) || posix_isatty( $fp ) ) {
+                       $isatty = true;
+               } else {
+                       $isatty = false;
+               }
        }
 
        if ( $isatty && function_exists( 'readline' ) ) {
@@ -69,11 +75,12 @@ function readconsole( $prompt = '' ) {
                if ( $isatty ) {
                        print $prompt;
                }
-               $fp = fopen( 'php://stdin', 'r' );
+               if ( feof( $fp ) ) {
+                       return false;
+               }
                $st = fgets($fp, 1024);
                if ($st === false) return false;
                $resp = trim( $st );
-               fclose( $fp );
                return $resp;
        }
 }