X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=scripts%2Fconsole.php;h=4d207c261b5a7da9f52799107dd04805e0240520;hb=78079f34e273357d03ceee13269f9a388e66c4e3;hp=2a000d39b077071965176b8f68fd53c2f044124f;hpb=8e58f241739b97bd53f78035781f16e2067a31d9;p=quix0rs-gnu-social.git diff --git a/scripts/console.php b/scripts/console.php index 2a000d39b0..4d207c261b 100755 --- a/scripts/console.php +++ b/scripts/console.php @@ -29,19 +29,15 @@ ENDOFHELP; require_once INSTALLDIR.'/scripts/commandline.inc'; -if (function_exists('posix_isatty')) { - define('CONSOLE_INTERACTIVE', posix_isatty(0)); -} else { - // Windows? Assume we're on a terminal. :P - define('CONSOLE_INTERACTIVE', false); -} -if (CONSOLE_INTERACTIVE) { - define('CONSOLE_READLINE', function_exists('readline')); -} +// Assume we're on a terminal if on Windows, otherwise posix_isatty tells us. +define('CONSOLE_INTERACTIVE', !function_exists('posix_isatty') || posix_isatty(0)); +define('CONSOLE_READLINE', CONSOLE_INTERACTIVE && function_exists('readline')); -if (CONSOLE_READLINE && CONSOLE_INTERACTIVE && file_exists(CONSOLE_HISTORY)) { - define(CONSOLE_HISTORY, getenv("HOME") . "/.statusnet_console_history"); - readline_read_history(CONSOLE_HISTORY); +if (CONSOLE_READLINE && CONSOLE_INTERACTIVE) { + define('CONSOLE_HISTORY', getenv("HOME") . "/.statusnet_console_history"); + if (file_exists(CONSOLE_HISTORY)) { + readline_read_history(CONSOLE_HISTORY); + } } function read_input_line($prompt) @@ -49,7 +45,13 @@ function read_input_line($prompt) if (CONSOLE_INTERACTIVE) { if (CONSOLE_READLINE) { $line = readline($prompt); - readline_add_history($line); + if (trim($line) != '') { + readline_add_history($line); + if (defined('CONSOLE_HISTORY')) { + // Save often; it's easy to hit fatal errors. + readline_write_history(CONSOLE_HISTORY); + } + } return $line; } else { return readline_emulation($prompt); @@ -60,9 +62,9 @@ function read_input_line($prompt) } /** - * On Unix-like systems where PHP readline extension is not present, + * On Unix-like systems where PHP readline extension isn't present, * -cough- Mac OS X -cough- we can shell out to bash to do it for us. - * This lets us at least handle things like arrow keys, but we do not + * This lets us at least handle things like arrow keys, but we don't * get any entry history. :( * * Shamelessly ripped from when I wrote the same code for MediaWiki. :) @@ -73,7 +75,7 @@ function read_input_line($prompt) */ function readline_emulation($prompt) { - if(file_exists(trim(shell_exec('which bash')))) { + if(CONSOLE_INTERACTIVE && file_exists(trim(shell_exec('which bash')))) { $encPrompt = escapeshellarg($prompt); $command = "read -er -p $encPrompt && echo \"\$REPLY\""; $encCommand = escapeshellarg($command); @@ -103,7 +105,9 @@ function readline_emulation($prompt) if (feof(STDIN)) { return false; } - print $prompt; + if (CONSOLE_INTERACTIVE) { + print $prompt; + } return fgets(STDIN); } @@ -123,13 +127,18 @@ function console_help() print "Type ctrl+D or enter 'exit' to exit.\n"; } - -print "StatusNet interactive PHP console... type ctrl+D or enter 'exit' to exit.\n"; -$prompt = common_config('site', 'name') . '> '; +if (CONSOLE_INTERACTIVE) { + print "StatusNet interactive PHP console... type ctrl+D or enter 'exit' to exit.\n"; + $prompt = common_config('site', 'name') . '> '; +} else { + $prompt = ''; +} while (!feof(STDIN)) { $line = read_input_line($prompt); if ($line === false) { - print "\n"; + if (CONSOLE_INTERACTIVE) { + print "\n"; + } break; } elseif ($line !== '') { try { @@ -154,9 +163,7 @@ while (!feof(STDIN)) { print get_class($e) . ": " . $e->getMessage() . "\n"; } } - print "\n"; -} - -if (CONSOLE_READLINE && CONSOLE_INTERACTIVE) { - @readline_write_history(CONSOLE_HISTORY); + if (CONSOLE_INTERACTIVE) { + print "\n"; + } }