コード例 #1
0
ファイル: cli.php プロジェクト: ligro/php-utils
 protected static function _getData()
 {
     // todo detect if we are reading from stdin or pipe
     $data = readline("data > ");
     readline_add_history($data);
     return $data;
 }
コード例 #2
0
ファイル: readline.php プロジェクト: mdkarch/mal
function mal_readline($prompt)
{
    global $HISTORY_FILE;
    static $history_loaded = false;
    // Load the history file
    if (!$history_loaded) {
        $history_loaded = true;
        if (is_readable($HISTORY_FILE)) {
            if ($file = fopen($HISTORY_FILE, "r")) {
                while (!feof($file)) {
                    $line = fgets($file);
                    if ($line) {
                        readline_add_history($line);
                    }
                }
                fclose($file);
            }
        }
    }
    $line = readline($prompt);
    if ($line === false) {
        return NULL;
    }
    readline_add_history($line);
    // Append to the history file
    if (is_writable($HISTORY_FILE)) {
        if ($file = fopen($HISTORY_FILE, "a")) {
            fputs($file, $line . "\n");
            fclose($file);
        }
    }
    return $line;
}
コード例 #3
0
ファイル: GNUReadline.php プロジェクト: fulore/psysh
 /**
  * {@inheritDoc}
  */
 public function addHistory($line)
 {
     if ($res = readline_add_history($line)) {
         $this->writeHistory();
     }
     return $res;
 }
コード例 #4
0
ファイル: CommandReader.php プロジェクト: iTXTech/Genisys
 private function readline_callback($line)
 {
     if ($line !== "") {
         $this->buffer[] = $line;
         readline_add_history($line);
     }
 }
コード例 #5
0
ファイル: Mageshell.php プロジェクト: stolksdorf/mageshell
 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;
 }
コード例 #6
0
ファイル: repl.php プロジェクト: TestArmada/admiral
 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;
 }
コード例 #7
0
 /**
  * {@inheritDoc}
  */
 public function writeHistory()
 {
     // We have to write history first, since it is used
     // by Libedit to list history
     $res = readline_write_history($this->historyFile);
     if (!$res || !$this->eraseDups && !$this->historySize > 0) {
         return $res;
     }
     $hist = $this->listHistory();
     if (!$hist) {
         return true;
     }
     if ($this->eraseDups) {
         // flip-flip technique: removes duplicates, latest entries win.
         $hist = array_flip(array_flip($hist));
         // sort on keys to get the order back
         ksort($hist);
     }
     if ($this->historySize > 0) {
         $histsize = count($hist);
         if ($histsize > $this->historySize) {
             $hist = array_slice($hist, $histsize - $this->historySize);
         }
     }
     readline_clear_history();
     foreach ($hist as $line) {
         readline_add_history($line);
     }
     return readline_write_history($this->historyFile);
 }
コード例 #8
0
ファイル: OAuthRESTClient.php プロジェクト: henrikau/confusa
 public function getAuthorization()
 {
     /* try to get cached access token first */
     if (file_exists(".acc_tok_cache")) {
         while ($reuseAccTok != 'y' && $reuseAccTok != 'n') {
             $reuseAccTok = readline("Reuse cached access token (y/n)? ");
             readline_add_history($reuseAccTok);
         }
         if ($reuseAccTok == 'y') {
             $accTokenString = file_get_contents(".acc_tok_cache");
             $accessToken = unserialize($accTokenString);
             echo "Using access token: ";
             print_r($accessToken);
         }
     }
     /* no cached access token, get a new one */
     if (empty($accessToken)) {
         try {
             $reqToken = $this->oauth->getRequestToken($this->reqTokenURL);
         } catch (OAuthException $oae) {
             echo "The following exception occured when trying to get a request token: " . $oae->getMessage() . "\n";
         }
         print_r($reqToken);
         echo "Now you have to authorize the following token: " . $this->authorizeURL . "?oauth_token=" . $reqToken['oauth_token'] . "\n";
         $this->ssp_readline("Press any key to continue...\n");
         $accessToken = $reqToken;
         $this->oauth->setToken($reqToken['oauth_token'], $reqToken['oauth_token_secret']);
         $accessToken = $this->oauth->getAccessToken($this->accTokenURL);
         $accessTokenString = serialize($accessToken);
         file_put_contents(".acc_tok_cache", $accessTokenString);
     }
     $this->oauth->setToken($accessToken['oauth_token'], $accessToken['oauth_token_secret']);
 }
コード例 #9
0
ファイル: Shell.php プロジェクト: g4z/poop
 /**
  * Read a line from the command prompt
  * @return string
  */
 public function readInput()
 {
     $line = readline($this->prompt);
     if (!empty($line)) {
         readline_add_history($line);
     }
     return $line;
 }
コード例 #10
0
ファイル: ReadlineInput.php プロジェクト: codespot/sprinter
 protected function _read($hint = null)
 {
     if (!extension_loaded('readline')) {
         throw new \Exception('`readline` extension is not loaded.');
     }
     $value = readline($hint);
     readline_add_history($value);
     return (string) $value;
 }
コード例 #11
0
ファイル: Console.php プロジェクト: bartekpie3/PhpConsole
 /**
  * in
  * 
  * @param string $text
  * @param boolean $escape
  * @return string
  */
 public static function in($text, $escape = false)
 {
     $line = readline($text);
     readline_add_history($line);
     if ($escape) {
         return escapeshellcmd($line);
     }
     return $line;
 }
コード例 #12
0
ファイル: GetAllHrefs.php プロジェクト: Kurets/origin
 public function setCheckBlog()
 {
     $GA = GreenArrow::greenArrow();
     echo "\n--------------------------------------------------------";
     echo "\nShould I check all BLOG pages? \n" . "\n[ 1 ] {$GA} YES" . "\n[ ENTER ] {$GA} NO, only '/blog/'\n";
     $checkBlog = readline("Your choice: ");
     readline_add_history($checkBlog);
     echo "--------------------------------------------------------";
     self::$checkBlog = $checkBlog;
 }
コード例 #13
0
 /**
  * @param string $prompt
  */
 public function readLine($prompt)
 {
     $configuration = $this->configuration;
     $line = trim(readline($prompt));
     $tokens = explode(' ', $line);
     if (!empty($tokens)) {
         $this->executeIfPossible($tokens, $configuration);
         readline_add_history($line);
     }
 }
コード例 #14
0
ファイル: DrutoCli.php プロジェクト: druto/framework
 public function getInput($label = "Please Enter", $callback = null)
 {
     $line = readline("{$label} : ");
     readline_add_history($line);
     if (is_object($callback) && is_callable($callback)) {
         call_user_func_array($callback, array($line));
     } else {
         return $line;
     }
 }
コード例 #15
0
 private function main()
 {
     \Cli::write(sprintf('Fuel %s - PHP %s (%s) (%s) [%s]', \Fuel::VERSION, phpversion(), php_sapi_name(), self::build_date(), PHP_OS));
     // Loop until they break it
     while (TRUE) {
         if (\Cli::$readline_support) {
             readline_completion_function(array(__CLASS__, 'tab_complete'));
         }
         if (!($__line = rtrim(trim(trim(\Cli::input('>>> ')), PHP_EOL), ';'))) {
             continue;
         }
         if ($__line == 'quit') {
             break;
         }
         // Add this line to history
         //$this->history[] = array_slice($this->history, 0, -99) + array($line);
         if (\Cli::$readline_support) {
             readline_add_history($__line);
         }
         if (self::is_immediate($__line)) {
             $__line = "return ({$__line})";
         }
         ob_start();
         // Unset the previous line and execute the new one
         $random_ret = \Str::random();
         try {
             $ret = eval("unset(\$__line); {$__line};");
         } catch (\Exception $e) {
             $ret = $random_ret;
             $__line = $e->getMessage();
         }
         // Error was returned
         if ($ret === $random_ret) {
             \Cli::error('Parse Error - ' . $__line);
             \Cli::beep();
         }
         if (ob_get_length() == 0) {
             if (is_bool($ret)) {
                 echo $ret ? 'true' : 'false';
             } elseif (is_string($ret)) {
                 echo addcslashes($ret, "....ÿ");
             } elseif (!is_null($ret)) {
                 var_export($ret);
             }
         }
         unset($ret);
         $out = ob_get_contents();
         ob_end_clean();
         if (strlen($out) > 0 && substr($out, -1) != PHP_EOL) {
             $out .= PHP_EOL;
         }
         echo $out;
         unset($out);
     }
 }
コード例 #16
0
 /**
  * Read a value from the stream
  *
  * @return mixed The value of the stream
  */
 public function read()
 {
     if ($this->_canReadline) {
         $line = readline('');
         if (!empty($line)) {
             readline_add_history($line);
         }
         return $line;
     }
     return fgets($this->_input);
 }
コード例 #17
0
 public function readInput($prompt = NULL)
 {
     $input = '';
     if ($prompt === NULL) {
         $input = readline();
     } else {
         $input = readline("{$prompt} ");
     }
     readline_add_history($input);
     return trim($input);
 }
コード例 #18
0
ファイル: sql.php プロジェクト: nischayn22/mediawiki-core
 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);
         }
     }
 }
コード例 #19
0
 private function readLine()
 {
     if (!$this->readline) {
         $line = trim(fgets(fopen("php://stdin", "r")));
     } else {
         $line = trim(readline("> "));
         if ($line != "") {
             readline_add_history($line);
         }
     }
     return $line;
 }
コード例 #20
0
 private function readLine()
 {
     if (!$this->readline) {
         $line = trim(fgets($this->fp));
     } else {
         $line = trim(readline("> "));
         if ($line != "") {
             readline_add_history($line);
         }
     }
     return $line;
 }
コード例 #21
0
ファイル: Cli.php プロジェクト: robebeye/PHPCliWrapper
 function rl_callback($ret)
 {
     if ($ret != "") {
         $this->command($ret);
         readline_add_history($ret);
         array_push($this->history, $ret);
     }
     if ($ret == "exit") {
         $this->prompting = false;
         readline_callback_handler_remove();
     } else {
         $this->install_handler();
     }
 }
コード例 #22
0
ファイル: ReadlineClient.php プロジェクト: TedaLIEz/AUNET
 * 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');
コード例 #23
0
ファイル: Shell.php プロジェクト: kchhainarong/chantuchP
    /**
     * 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));
            }
        }
    }
コード例 #24
0
ファイル: Chooser.php プロジェクト: arunahk/CLIFramework
 /**
  *
  *
  */
 public function choose($prompt, $choices)
 {
     echo $prompt . ": \n";
     $choicesMap = array();
     // Not an indexed array
     if (!isset($choices[0])) {
         $i = 0;
         foreach ($choices as $choice => $value) {
             $i++;
             $choicesMap[$i] = $value;
             echo "\t" . $i . "  {$choice}\n";
         }
     } else {
         foreach ($choices as $choice => $desc) {
             $choicesMap[$choice] = $choice;
             echo "\t{$choice}: {$desc}\n";
         }
     }
     if ($this->style) {
         echo $this->formatter->getStartMark($this->style);
     }
     $completionItems = array_keys($choicesMap);
     $choosePrompt = "Please Choose 1-{$i} > ";
     while (1) {
         if (extension_loaded('readline')) {
             $success = readline_completion_function(function ($string, $index) use($completionItems) {
                 return $completionItems;
             });
             $answer = readline($choosePrompt);
             readline_add_history($answer);
         } else {
             echo $choosePrompt;
             $answer = rtrim(fgets(STDIN), "\n");
         }
         $answer = (int) trim($answer);
         if (is_integer($answer)) {
             if (isset($choicesMap[$answer])) {
                 if ($this->style) {
                     echo $this->formatter->getClearMark();
                 }
                 return $choicesMap[$answer];
             } else {
                 continue;
             }
         }
         break;
     }
 }
コード例 #25
0
ファイル: CommandReader.php プロジェクト: PepbookPvP/Genisys
 private function readLine()
 {
     if (!$this->readline) {
         global $stdin;
         if (!is_resource($stdin)) {
             return "";
         }
         return trim(fgets($stdin));
     } else {
         $line = trim(readline("> "));
         if ($line != "") {
             readline_add_history($line);
         }
         return $line;
     }
 }
コード例 #26
0
ファイル: Cmd.php プロジェクト: iHunt101/phlite
 function cmdloop($intro = false)
 {
     $this->preloop();
     if ($this->completekey) {
         readline_completion_function(array($this, 'complete'));
     }
     if ($intro) {
         $this->intro = $intro;
     }
     if ($this->intro) {
         fwrite($this->stdout, $this->intro . "\n");
     }
     if (function_exists('pcntl_signal')) {
         pcntl_signal(SIGINT, function () {
             throw new Exception\KeyboardInterrupt();
         });
     }
     $stop = null;
     while (!$stop) {
         if ($this->cmdqueue) {
             $line = array_pop($this->cmdqueue);
         } elseif ($this->completekey) {
             $line = readline($this->prompt);
             if ($line) {
                 readline_add_history($line);
             } elseif ($line === false) {
                 $line = 'EOF';
             }
         } else {
             fwrite($this->stdout, $this->prompt);
             fflush($this->stdout);
             $line = $this->stdin->readline();
             if (!$line) {
                 $line = 'EOF';
             } else {
                 $line = rtrim($line, "\r\n");
             }
         }
         if (function_exists('pcntl_signal_dispatch')) {
             pcntl_signal_dispatch();
         }
         $line = $this->precmd($line);
         $stop = $this->onecmd($line);
         $stop = $this->postcmd($stop, $line);
     }
     $this->postloop();
 }
コード例 #27
0
ファイル: mwp.php プロジェクト: rustyeddy/sandbox
function do_readline($prompt = 'command > ')
{
    if (function_exists('readline')) {
        $cmd = readline($prompt);
        if ($cmd === '') {
            return null;
        }
        readline_add_history($cmd);
    } else {
        $cmd = \cli\prompt("mwp ");
        if ($cmd === '') {
            return null;
        }
    }
    $args = explode(" ", $cmd);
    return $args;
}
コード例 #28
0
ファイル: ConsoleBase.php プロジェクト: guitarpoet/clips-tool
 /**
  * The interface method, to start the console
  */
 public function console()
 {
     $line = readline($this->prompt()) . "\n";
     $this->running = true;
     while ($this->running) {
         if ($this->isComplete($line)) {
             $this->doRun($line, true);
             readline_add_history($line);
             if ($this->running) {
                 $line = readline($this->prompt()) . "\n";
             }
         } else {
             if ($this->running) {
                 $line .= readline($this->prompt(true)) . "\n";
             }
         }
     }
 }
コード例 #29
0
ファイル: Prompt.php プロジェクト: yslbc/twcompany
 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;
     }
 }
コード例 #30
0
ファイル: sql.php プロジェクト: seedbank/old-repo
 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);
         }
     }
 }