Permalink
Browse files

整理代码

  • Loading branch information...
1 parent 42d4f65 commit 0d9688fff0ca29e6eebbfccf09fa0f5eb65a56d9 LiANG committed Jun 4, 2012
Showing with 228 additions and 0 deletions.
  1. +226 −0 psh.php
  2. +2 −0 psh_1.php
View
226 psh.php
@@ -0,0 +1,226 @@
+#!/usr/bin/env php
+<?php
+declare(ticks = 1);
+
+__psh_setup();
+__psh_print_info();
+
+for (;;)
+{
+ readline_completion_function("__psh_rl_complete");
+ $__psh_line = readline(">>> ");
+ if ($__psh_line === false)
+ {
+ echo "\n";
+ break;
+ }
+ if (strlen($__psh_line) == 0)
+ {
+ continue;
+ }
+ if ((!isset($__psh_hist)) || (($__psh_line != $__psh_hist)))
+ {
+ readline_add_history($__psh_line);
+ $__psh_hist = $__psh_line;
+ }
+
+ $is_immediate = __psh_is_immediate($__psh_line);
+ echo "is_immediate: " . ($is_immediate ? "Y\n" : "N\n");
+ if ($is_immediate)
+ {
+ $__psh_line = "return ({$__psh_line})";
+ }
+
+ ob_start();
+ $ret = eval("unset(\$__psh_line); {$__psh_line};");
+
+ if (ob_get_length() == 0)
+ {
+ if (is_bool($ret))
+ {
+ echo ($ret ? "true" : "false");
+ }
+ else if (is_string($ret))
+ {
+ echo $ret;
+ }
+ else if (!is_null($ret))
+ {
+ print_r($ret);
+ }
+ }
+
+ unset($ret);
+ $out = ob_get_contents();
+ ob_end_clean();
+
+ if ((strlen($out) > 0) && (substr($out, -1) != "\n"))
+ {
+ $out .= "\n";
+ }
+
+ echo $out;
+ unset($out);
+}
+
+function __psh_rl_complete($line, $pos, $cursor)
+{
+ $const = array_keys(get_defined_constants());
+ $var = array_keys($GLOBALS);
+
+ $func = get_defined_functions();
+ $s = "__psh_";
+ foreach ($func["user"] as $i)
+ {
+ if (substr($i, 0, strlen($s)) != $s)
+ {
+ $func["internal"][] = $i;
+ }
+ }
+
+ $func = $func["internal"];
+
+ return array_merge($const, $var, $func);
+}
+
+function __psh_is_immediate($line)
+{
+ $skip = array("class", "declare", "die", "echo", "exit", "for",
+ "foreach", "function", "global", "if", "include",
+ "include_once", "print", "require", "require_once",
+ "return", "static", "switch", "unset", "while");
+ $okeq = array("===", "!==", "==", "!=", "<=", ">=");
+ $code = "";
+ $in_single_quote = false;
+ $in_double_quote = false;
+ for ($i = 0; $i < strlen($line); $i++)
+ {
+ $char = $line{$i};
+ if ($char == "'")
+ {
+ $in_single_quote = !$in_single_quote;
+ }
+ elseif ($char == '"')
+ {
+ $in_double_quote = !$in_double_quote;
+ }
+ elseif ($char == "\\" && ($in_single_quote || $in_double_quote))
+ {
+ $i++;
+ }
+ else
+ {
+ $code .= $char;
+ }
+ }
+
+ $code = str_replace($okeq, "", $code);
+ if (strcspn($code, ";{=") != strlen($code))
+ {
+ return false;
+ }
+
+ // $kw = split("[^A-Za-z0-9_]", $code);
+ $kw = preg_split("/[^A-Za-z0-9_]/", $code);
+ foreach ($kw as $i)
+ {
+ if (in_array($i, $skip))
+ {
+ return false;
+ }
+ }
+
+ return true;
+}
+
+function __psh_print_info()
+{
+ $ver = phpversion();
+ $sapi = php_sapi_name();
+ $date = __psh_build_date();
+ $os = PHP_OS;
+ echo "PHP $ver ($sapi) ($date) [$os]\n";
+}
+
+function __psh_build_date()
+{
+ ob_start();
+ phpinfo(INFO_GENERAL);
+ $php_info = ob_get_contents();
+ ob_end_clean();
+ $php_info = strip_tags($php_info);
+ $php_info = explode("\n", $php_info);
+ $s = array("Build Date => ", "Build Date ");
+
+ foreach ($php_info as $i)
+ {
+ foreach ($s as $j)
+ {
+ if (substr($i, 0, strlen($j)) == $j)
+ {
+ return trim(substr($i, strlen($j)));
+ }
+ }
+ }
+
+ return "???";
+}
+
+function __psh_setup()
+{
+ if (version_compare(phpversion(), "4.3.0", "<"))
+ {
+ echo "PHP 4.3.0 or above is required.\n";
+ exit(111);
+ }
+ if (!extension_loaded("readline"))
+ {
+ $prefix = (PHP_SHLIB_SUFFIX == "dll") ? "php_" : "";
+ if (!@dl($prefix . "readline." . PHP_SHLIB_SUFFIX))
+ {
+ echo "The 'readline' module is required.\n";
+ exit(111);
+ }
+ }
+
+ error_reporting(E_ALL | E_STRICT);
+ ini_set("error_log", NULL);
+ ini_set("log_errors", 1);
+ ini_set("html_errors", 0);
+ ini_set("display_errors", 0);
+
+ // clean the output buffer
+ while (ob_get_level())
+ {
+ ob_end_clean();
+ }
+
+ ob_implicit_flush(true);
+
+ // setup signal handlers
+ if(function_exists('pcntl_signal'))
+ {
+ pcntl_signal(SIGTERM, "__psh_sig_handler");
+ pcntl_signal(SIGINT, "__psh_sig_handler");
+ }
+
+ register_shutdown_function('__psh_shutdown_hook');
+}
+
+// signal handler function
+function __psh_sig_handler($signo)
+{
+ switch ($signo) {
+ case SIGTERM:
+ case SIGINT:
+ exit();
+ default:
+ // handle all other signals
+ }
+}
+
+function __psh_shutdown_hook()
+{
+ echo "shutting down...\n";
+ exit();
+}
View
2 psh_1.php
@@ -0,0 +1,2 @@
+psh
+===

0 comments on commit 0d9688f

Please sign in to comment.