protected function init() { global $swarmInstallDir; $this->checkAtty(); $supportsReadline = function_exists('readline_add_history'); if ($supportsReadline) { $historyFile = isset($_ENV['HOME']) ? "{$_ENV['HOME']}/.testswarm_eval_history" : "{$swarmInstallDir}/config/.testswarm_eval_history"; readline_read_history($historyFile); } while (!!($line = $this->cliInput())) { if ($supportsReadline) { readline_add_history($line); readline_write_history($historyFile); } $ret = eval($line . ";"); if (is_null($ret)) { echo "\n"; } elseif (is_string($ret) || is_numeric($ret)) { echo "{$ret}\n"; } else { var_dump($ret); } } print "\n"; exit; }
public static function setHistoryFile($file) { if (file_exists($file)) { readline_read_history($file); } self::$history_file = $file; }
public function start() { $histfile = '.magesh_history'; if (PHP_OS == 'Linux') { readline_read_history($histfile); } do { $input = $this->_getInput($this->_prompt); if ($input === false) { break; } $cmd = $this->_formatInput($input); if (PHP_OS == 'Linux') { readline_add_history($input); readline_write_history($histfile); } echo "\n"; $result = null; try { $result = eval($cmd); } catch (Exception $e) { $result = $e->getMessage() . "\n\n" . $e->getTraceAsString() . "\n\n"; } $output = $this->_formatOutput($result); echo "Result: " . $output . "\n\n"; } while (true); return true; }
protected static function _initCompletion() { self::$_readlineFile = sys_get_temp_dir() . '/phputils-data-history'; // init read line readline_info('readline_name', 'data'); readline_completion_function([__CLASS__, 'readlineCompletion']); is_file(self::$_readlineFile) and readline_read_history(self::$_readlineFile); }
/** * Constructor * * @return void */ public function __construct($options = array()) { $this->input = fopen('php://stdin', 'r'); $this->rc_file = getenv('PHPREPLRC') ? getenv('PHPREPLRC') : getenv('HOME') . '/.phpreplrc'; $defaults = $this->defaultOptions(); $this->options = array_merge($defaults, $options); if ($this->options['readline'] && is_readable($this->options['readline_hist'])) { readline_read_history($this->options['readline_hist']); } }
public function execute() { $dbw = wfGetDB(DB_MASTER); if ($this->hasArg()) { $fileName = $this->getArg(); $file = fopen($fileName, 'r'); if (!$file) { $this->error("Unable to open input file", true); } $error = $dbw->sourceStream($file, false, array($this, 'sqlPrintResult')); if ($error !== true) { $this->error($error, true); } else { exit(0); } } $useReadline = function_exists('readline_add_history') && Maintenance::posix_isatty(0); if ($useReadline) { global $IP; $historyFile = isset($_ENV['HOME']) ? "{$_ENV['HOME']}/.mwsql_history" : "{$IP}/maintenance/.mwsql_history"; readline_read_history($historyFile); } $wholeLine = ''; $newPrompt = '> '; $prompt = $newPrompt; while (($line = Maintenance::readconsole($prompt)) !== false) { if (!$line) { # User simply pressed return key continue; } $done = $dbw->streamStatementEnd($wholeLine, $line); $wholeLine .= $line; if (!$done) { $wholeLine .= ' '; $prompt = ' -> '; continue; } if ($useReadline) { # Delimiter is eated by streamStatementEnd, we add it # up in the history (bug 37020) readline_add_history($wholeLine . $dbw->getDelimiter()); readline_write_history($historyFile); } try { $res = $dbw->query($wholeLine); $this->sqlPrintResult($res, $dbw); $prompt = $newPrompt; $wholeLine = ''; } catch (DBQueryError $e) { $doDie = !Maintenance::posix_isatty(0); $this->error($e, $doDie); } } }
public function setUp() { if (!Libedit::isSupported()) { $this->markTestSkipped('Libedit not enabled'); } $this->historyFile = tempnam(sys_get_temp_dir(), 'psysh_test_history'); if (false === file_put_contents($this->historyFile, "_HiStOrY_V2_\n")) { $this->fail('Unable to write history file: ' . $this->historyFile); } // Calling readline_read_history before readline_clear_history // avoids segfault with PHP 5.5.7 & libedit v3.1 readline_read_history($this->historyFile); readline_clear_history(); }
* Create a new ReadlineClient using $socket for communication. * * @param resource $socket */ public function __construct($socket) { $this->_socket = $socket; } /** * Start the client with an prompt and readline history path. * * This method never returns. * * @param string $prompt * @param string $historyFile */ public function start($prompt, $historyFile) { readline_read_history($historyFile); declare (ticks=1); pcntl_signal(SIGCHLD, SIG_IGN); pcntl_signal(SIGINT, array($this, 'clear'), true); // wait for the worker to finish executing hooks if (fread($this->_socket, 1) != EvalWorker::READY) { throw new \RuntimeException('EvalWorker failed to start'); } $parser = new ShallowParser(); $buf = ''; $lineno = 1; for (;;) { $this->_clear = false; $line = readline(sprintf('[%d] %s', $lineno, $buf == '' ? $prompt : str_pad('*> ', strlen($prompt), ' ', STR_PAD_LEFT))); if ($this->_clear) { $buf = ''; continue; } if (false === $line) { $buf = 'exit(0);'; // ctrl-d acts like exit } if (strlen($line) > 0) { readline_add_history($line); } $buf .= sprintf("%s\n", $line); if ($statements = $parser->statements($buf)) { ++$lineno; $buf = ''; foreach ($statements as $stmt) { if (false === ($written = fwrite($this->_socket, $stmt))) { throw new \RuntimeException('Socket error: failed to write data');
/** * @param ContainerInterface $container */ public function __construct(ContainerInterface $container) { $this->hasReadline = function_exists('readline'); $this->application = $container->get('ui.application'); $file = getenv('HOME') . '/.history_phpguard'; $this->historyFile = $file; $this->output = $container->get('ui.output'); $this->container = $container; // @codeCoverageIgnoreStart if ($this->hasReadline) { readline_read_history($this->historyFile); readline_completion_function(array($this, 'autocompleter')); } // @codeCoverageIgnoreEnd }
/** * Constructor * * @return void */ public function __construct($options = array()) { $this->input = fopen('php://stdin', 'r'); $this->rc_file = getenv('PHPREPLRC') ? getenv('PHPREPLRC') : getenv('HOME') . '/.phpreplrc'; $this->printers = $this->getDefaultPrinters(); $defaults = $this->getDefaultOptions(); $this->options = array_merge_recursive($defaults, $options); $this->readline_support = true; if (!function_exists('readline') || getenv('TERM') == 'dumb') { $this->readline_support = false; } if ($this->readline_support && is_readable($this->getOption('readline_hist'))) { readline_read_history($this->getOption('readline_hist')); } }
/** * Runs the shell. */ public function run() { $this->application->setAutoExit(false); $this->application->setCatchExceptions(true); if ($this->hasReadline) { readline_read_history($this->history); readline_completion_function(array($this, 'autocompleter')); } $this->output->writeln($this->getHeader()); $php = null; if ($this->processIsolation) { $finder = new PhpExecutableFinder(); $php = $finder->find(); $this->output->writeln(<<<EOF <info>Running with process isolation, you should consider this:</info> * each command is executed as separate process, * commands don't support interactivity, all params must be passed explicitly, * commands output is not colorized. EOF ); } while (true) { $command = $this->readline(); if (false === $command) { $this->output->writeln("\n"); break; } if ($this->hasReadline) { readline_add_history($command); readline_write_history($this->history); } if ($this->processIsolation) { $pb = new ProcessBuilder(); $process = $pb->add($php)->add($_SERVER['argv'][0])->add($command)->inheritEnvironmentVariables(true)->getProcess(); $output = $this->output; $process->run(function ($type, $data) use($output) { $output->writeln($data); }); $ret = $process->getExitCode(); } else { $ret = $this->application->run(new StringInput($command), $this->output); } if (0 !== $ret) { $this->output->writeln(sprintf('<error>The command terminated with an error status (%s)</error>', $ret)); } } }
public static function init($paths = null, $history_path = null) { self::$_often = get_defined_functions(); self::$_often = array_merge(self::$_often['internal'], get_declared_classes()); if (is_null($paths)) { $paths = explode(PATH_SEPARATOR, get_include_path()); } self::$_paths = $paths; if (self::_supportedReadline()) { readline_completion_function(array(__CLASS__, 'autocomplete')); self::$__last = null; if (is_null($history_path)) { if ($home = getenv('HOME')) { $history_path = $home . '/.pprompt_history'; } } if (self::$__history_path = $history_path) { readline_read_history(self::$__history_path); } } unset($paths); unset($history_path); unset($home); while (self::$__l = self::_readline(">> ")) { if (self::_supportedReadline()) { if (is_null(self::$__last) or self::$__l != self::$__last) { readline_add_history(self::$__l); } if (self::$__history_path) { readline_write_history(self::$__history_path); } } try { eval(self::$__l . ";"); echo "\n"; } catch (Exception $e) { echo $e->getMessage() . "\n"; echo $e->getTraceAsString() . "\n"; } self::$_vars = get_defined_vars(); self::$__last = self::$__l; } }
protected function __construct() { if (!empty($_SERVER['HOME'])) { $this->historyFile = $_SERVER['HOME'] . '/.imc_history'; if (!file_exists($this->historyFile)) { file_put_contents($this->historyFile, ''); } readline_read_history($this->historyFile); $this->history = explode(file_get_contents($this->historyFile), "\n"); if (isset($_ENV['HISTSIZE']) && $_ENV['HISTSIZE'] > 0) { $this->histSize = $_ENV['HISTSIZE']; } } readline_completion_function(array($this, 'completeCallback')); register_shutdown_function(array($this, 'fatalErrorShutdown')); # // Catch Ctrl+C, kill and SIGTERM pcntl_signal(SIGTERM, array($this, 'sigintShutdown')); pcntl_signal(SIGINT, array($this, 'sigintShutdown')); }
public function execute() { $dbw = wfGetDB(DB_MASTER); if ($this->hasArg()) { $fileName = $this->getArg(); $file = fopen($fileName, 'r'); if (!$file) { $this->error("Unable to open input file", true); } $error = $dbw->sourceStream($file, false, array($this, 'sqlPrintResult')); if ($error !== true) { $this->error($error, true); } else { exit(0); } } $useReadline = function_exists('readline_add_history') && Maintenance::posix_isatty(0); if ($useReadline) { global $IP; $historyFile = isset($_ENV['HOME']) ? "{$_ENV['HOME']}/.mwsql_history" : "{$IP}/maintenance/.mwsql_history"; readline_read_history($historyFile); } $wholeLine = ''; while (($line = Maintenance::readconsole()) !== false) { $done = $dbw->streamStatementEnd($wholeLine, $line); $wholeLine .= $line; if (!$done) { continue; } if ($useReadline) { readline_add_history($wholeLine); readline_write_history($historyFile); } try { $res = $dbw->query($wholeLine); $this->sqlPrintResult($res, $dbw); $wholeLine = ''; } catch (DBQueryError $e) { $this->error($e, true); } } }
/** * Runs the shell. */ public function run() { $this->application->setAutoExit(false); $this->application->setCatchExceptions(true); readline_read_history($this->history); readline_completion_function(array($this, 'autocompleter')); $this->output->writeln($this->getHeader()); while (true) { $command = readline($this->application->getName() . ' > '); if (false === $command) { $this->output->writeln("\n"); break; } readline_add_history($command); readline_write_history($this->history); if (0 !== ($ret = $this->application->run(new StringInput($command), $this->output))) { $this->output->writeln(sprintf('<error>The command terminated with an error status (%s)</error>', $ret)); } } }
/** * Runs the shell. */ public function run() { $this->application->setAutoExit(false); $this->application->setCatchExceptions(false); if ($this->hasReadline) { readline_read_history($this->history); readline_completion_function(array($this, 'autocompleter')); } $this->output->writeln($this->getHeader()); while (true) { $command = $this->readline(); if (false === $command) { $this->output->writeln("\n"); break; } if ($this->hasReadline) { readline_add_history($command); readline_write_history($this->history); } $ret = $this->application->run(new StringInput($command), $this->output); } }
* along with this program. If not, see <http://www.gnu.org/licenses/>. */ # Abort if called from a web server define('INSTALLDIR', realpath(dirname(__FILE__) . '/..')); $helptext = <<<ENDOFHELP console.php - provide an interactive PHP interpreter for testing ENDOFHELP; require_once INSTALLDIR . '/scripts/commandline.inc'; // 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) { define('CONSOLE_HISTORY', getenv("HOME") . "/.statusnet_console_history"); if (file_exists(CONSOLE_HISTORY)) { readline_read_history(CONSOLE_HISTORY); } } function read_input_line($prompt) { if (CONSOLE_INTERACTIVE) { if (CONSOLE_READLINE) { $line = readline($prompt); 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;
/** * Run the interactive Shell * * The shell command runs Flow's interactive shell. This shell allows for * entering commands like through the regular command line interface but * additionally supports autocompletion and a user-based command history. * * @return void */ public function shellCommand() { if (!function_exists('readline_read_history')) { $this->outputLine('Interactive Shell is not available on this system!'); $this->quit(1); } $subProcess = false; $pipes = array(); $historyPathAndFilename = getenv('HOME') . '/.flow_' . md5(FLOW_PATH_ROOT); readline_read_history($historyPathAndFilename); readline_completion_function(array($this, 'autocomplete')); echo "Flow Interactive Shell\n\n"; while (true) { $commandLine = readline('Flow > '); if ($commandLine == '') { echo "\n"; break; } readline_add_history($commandLine); readline_write_history($historyPathAndFilename); $request = $this->requestBuilder->build($commandLine); $response = new Response(); $command = $request->getCommand(); if ($request === false || $command->getCommandIdentifier() === false) { echo "Bad command\n"; continue; } if ($this->bootstrap->isCompiletimeCommand($command->getCommandIdentifier())) { $this->dispatcher->dispatch($request, $response); $response->send(); if (is_resource($subProcess)) { $this->quitSubProcess($subProcess, $pipes); } } else { if (is_resource($subProcess)) { $subProcessStatus = proc_get_status($subProcess); if ($subProcessStatus['running'] === false) { proc_close($subProcess); } } if (!is_resource($subProcess)) { list($subProcess, $pipes) = $this->launchSubProcess(); if ($subProcess === false || !is_array($pipes)) { echo "Failed launching the shell sub process for executing the runtime command.\n"; continue; } $this->echoSubProcessResponse($pipes); } fwrite($pipes[0], $commandLine . "\n"); fflush($pipes[0]); $this->echoSubProcessResponse($pipes); if ($command->isFlushingCaches()) { $this->quitSubProcess($subProcess, $pipes); } } } if (is_resource($subProcess)) { $this->quitSubProcess($subProcess, $pipes); } echo "Bye!\n"; }
/** * {@inheritDoc} */ public function readHistory() { return readline_read_history($this->historyFile); }
public function startup($event) { readline_read_history($event->getSubject()->getConfigDir() . '/history'); }
<?php $name = tempnam('/tmp', 'readline.tmp'); readline_add_history('foo'); readline_add_history(''); readline_add_history(1); readline_add_history(NULL); var_dump(readline_write_history($name)); var_dump(readline_read_history($name)); var_dump(file_get_contents($name)); readline_clear_history(); readline_write_history($name); var_dump(file_get_contents($name)); unlink($name);
public function execute() { $wiki = $this->getOption('wikidb') ?: false; // Get the appropriate load balancer (for this wiki) if ($this->hasOption('cluster')) { $lb = wfGetLBFactory()->getExternalLB($this->getOption('cluster'), $wiki); } else { $lb = wfGetLB($wiki); } // Figure out which server to use if ($this->hasOption('slave')) { $server = $this->getOption('slave'); if ($server === 'any') { $index = DB_SLAVE; } else { $index = null; for ($i = 0; $i < $lb->getServerCount(); ++$i) { if ($lb->getServerName($i) === $server) { $index = $i; break; } } if ($index === null) { $this->error("No slave server configured with the name '{$server}'.", 1); } } } else { $index = DB_MASTER; } // Get a DB handle (with this wiki's DB selected) from the appropriate load balancer $db = $lb->getConnection($index, array(), $wiki); if ($this->hasOption('slave') && $db->getLBInfo('master') !== null) { $this->error("The server selected ({$db->getServer()}) is not a slave.", 1); } if ($this->hasArg(0)) { $file = fopen($this->getArg(0), 'r'); if (!$file) { $this->error("Unable to open input file", true); } $error = $db->sourceStream($file, false, array($this, 'sqlPrintResult')); if ($error !== true) { $this->error($error, true); } else { exit(0); } } $useReadline = function_exists('readline_add_history') && Maintenance::posix_isatty(0); if ($useReadline) { global $IP; $historyFile = isset($_ENV['HOME']) ? "{$_ENV['HOME']}/.mwsql_history" : "{$IP}/maintenance/.mwsql_history"; readline_read_history($historyFile); } $wholeLine = ''; $newPrompt = '> '; $prompt = $newPrompt; while (($line = Maintenance::readconsole($prompt)) !== false) { if (!$line) { # User simply pressed return key continue; } $done = $db->streamStatementEnd($wholeLine, $line); $wholeLine .= $line; if (!$done) { $wholeLine .= ' '; $prompt = ' -> '; continue; } if ($useReadline) { # Delimiter is eated by streamStatementEnd, we add it # up in the history (bug 37020) readline_add_history($wholeLine . $db->getDelimiter()); readline_write_history($historyFile); } try { $res = $db->query($wholeLine); $this->sqlPrintResult($res, $db); $prompt = $newPrompt; $wholeLine = ''; } catch (DBQueryError $e) { $doDie = !Maintenance::posix_isatty(0); $this->error($e, $doDie); } } wfWaitForSlaves(); }
$lb = wfGetLB(); $serverCount = $lb->getServerCount(); for ($i = 0; $i < $serverCount; $i++) { $server = $lb->getServerInfo($i); $server['flags'] |= DBO_DEBUG; $lb->setServerInfo($i, $server); } } if ($d > 2) { $wgDebugFunctionEntry = true; } } $useReadline = function_exists('readline_add_history') && Maintenance::posix_isatty(0); if ($useReadline) { $historyFile = isset($_ENV['HOME']) ? "{$_ENV['HOME']}/.mweval_history" : "{$IP}/maintenance/.mweval_history"; readline_read_history($historyFile); } while (($line = Maintenance::readconsole()) !== false) { if ($useReadline) { readline_add_history($line); readline_write_history($historyFile); } $val = eval($line . ";"); if (wfIsHipHop() || is_null($val)) { echo "\n"; } elseif (is_string($val) || is_numeric($val)) { echo "{$val}\n"; } else { var_dump($val); } }
public function execute() { // Get a DB handle (with this wiki's DB select) from the appropriate load balancer if ($this->hasOption('cluster')) { $lb = wfGetLBFactory()->getExternalLB($this->getOption('cluster')); $dbw = $lb->getConnection(DB_MASTER); // master for external LB } else { $dbw = wfGetDB(DB_MASTER); // master for primary LB for this wiki } if ($this->hasArg(0)) { $file = fopen($this->getArg(0), 'r'); if (!$file) { $this->error("Unable to open input file", true); } $error = $dbw->sourceStream($file, false, array($this, 'sqlPrintResult')); if ($error !== true) { $this->error($error, true); } else { exit(0); } } $useReadline = function_exists('readline_add_history') && Maintenance::posix_isatty(0); if ($useReadline) { global $IP; $historyFile = isset($_ENV['HOME']) ? "{$_ENV['HOME']}/.mwsql_history" : "{$IP}/maintenance/.mwsql_history"; readline_read_history($historyFile); } $wholeLine = ''; $newPrompt = '> '; $prompt = $newPrompt; while (($line = Maintenance::readconsole($prompt)) !== false) { if (!$line) { # User simply pressed return key continue; } $done = $dbw->streamStatementEnd($wholeLine, $line); $wholeLine .= $line; if (!$done) { $wholeLine .= ' '; $prompt = ' -> '; continue; } if ($useReadline) { # Delimiter is eated by streamStatementEnd, we add it # up in the history (bug 37020) readline_add_history($wholeLine . $dbw->getDelimiter()); readline_write_history($historyFile); } try { $res = $dbw->query($wholeLine); $this->sqlPrintResult($res, $dbw); $prompt = $newPrompt; $wholeLine = ''; } catch (DBQueryError $e) { $doDie = !Maintenance::posix_isatty(0); $this->error($e, $doDie); } } wfWaitForSlaves(); }
/** * Load readline history from a file * * @param io.File file * @return bool success * @throws io.IOException */ public function readHistoryFile($file) { if (FALSE === readline_read_history($file->getURI())) { throw new IOException('Could not read history from ' . $file->getURI()); } return TRUE; }
public static function main($options = array()) { $shell = new iphp($options); print <<<END Welcome to the PHOCOA shell! The PHOCOA shell is a powerful interactive PHP shell for allowing you to experiment with your application interactively. Features include: - autocomplete (tab key) - readline support w/history - automatically wired into your project's autoload Enter a php statement at the prompt, and it will be evaluated. The variable \$_ will contain the result. Example: > new WFArray(array(1,2)) Array: 0 => 1 1 => 2 END Array > \$_[0] + 1 2 END; // readline history if (function_exists('readline_read_history')) { readline_read_history($shell->historyFile()); // doesn't seem to work, even though readline_list_history() shows the read items! } // install tab-complete if (function_exists('readline_completion_function')) { readline_completion_function(array($shell, 'readlineCompleter')); } while (true) { $shell->doCommand($shell->readline()); } }
<?php readline_read_history(); ?> ==DONE==
public function runREPL() { // install signal handlers if possible declare (ticks=1); if (function_exists('pcntl_signal')) { pcntl_signal(SIGINT, array($this, 'stop')); } print $this->options[self::OPT_PROMPT_HEADER]; // readline history if (function_exists('readline_read_history')) { readline_read_history($this->historyFile()); // doesn't seem to work, even though readline_list_history() shows the read items! } // install tab-complete if (function_exists('readline_completion_function')) { readline_completion_function(array($this, 'readlineCompleter')); } // run repl loop. if (function_exists('readline')) { // readline automatically re-prints the prompt after the callback runs, so the only way to prevent double-prompts is to do it this way until we figure out something better readline_callback_handler_install($this->inputPrompt, array($this, 'doCommand')); while (true) { $this->realReadline(); } } else { while (true) { $this->fakeReadline(); } } }
/** * {@inheritdoc} */ public function readHistory() { // Workaround PHP bug #69054 // // If open_basedir is set, readline_read_history() segfaults. This will be fixed in 5.6.7: // // https://github.com/php/php-src/blob/423a057023ef3c00d2ffc16a6b43ba01d0f71796/NEWS#L19-L21 // // projects: add a PHP version check after next point release if (!ini_get('open_basedir')) { readline_read_history(); } readline_clear_history(); return readline_read_history($this->historyFile); }
<?php var_dump(readline_read_history()); var_dump(readline_read_history('nofile'));