PHPonTrax
[ class tree: PHPonTrax ] [ index: PHPonTrax ] [ all elements ]

Source for file php_shell.php

Documentation is available at php_shell.php

  1. <?php
  2. /**
  3.  *  (PHP 5)
  4.  *
  5.  *  @package PHPonTrax
  6.  *  @version $Id:$
  7.  *  @copyright (c) 2005 John Peterson
  8.  *
  9.  *   Permission is hereby granted, free of charge, to any person obtaining
  10.  *   a copy of this software and associated documentation files (the
  11.  *   "Software"), to deal in the Software without restriction, including
  12.  *   without limitation the rights to use, copy, modify, merge, publish,
  13.  *   distribute, sublicense, and/or sell copies of the Software, and to
  14.  *   permit persons to whom the Software is furnished to do so, subject to
  15.  *   the following conditions:
  16.  *
  17.  *   The above copyright notice and this permission notice shall be
  18.  *   included in all copies or substantial portions of the Software.
  19.  *
  20.  *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21.  *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22.  *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23.  *   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24.  *   LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25.  *   OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26.  *   WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27.  */
  28.  
  29.  
  30. require_once("php_shell/shell.php");
  31.     
  32. /**
  33. * default error-handler
  34. *
  35. * Instead of printing the NOTICE or WARNING from php we wan't the turn non-FATAL
  36. * messages into exceptions and handle them in our own way.
  37. *
  38. * you can set your own error-handler by createing a function named
  39. * __shell_error_handler
  40. *
  41. @param integer $errno Error-Number
  42. @param string $errstr Error-Message
  43. @param string $errfile Filename where the error was raised
  44. @param interger $errline Line-Number in the File
  45. @param mixed $errctx ...
  46. */
  47. function __shell_default_error_handler($errno$errstr$errfile$errline$errctx{
  48.     ## ... what is this errno again ?
  49.     if ($errno == 2048return;
  50.   
  51.     throw new Exception(sprintf("%s:%d\r\n%s"$errfile$errline$errstr));
  52. }
  53.  
  54. set_error_handler("__shell_default_error_handler");
  55.  
  56. $__shell new PHP_Shell();
  57.  
  58. $f = <<<EOF
  59. >> use '?' to open the inline help 
  60. EOF;
  61.  
  62. printf($f
  63.     $__shell->getVersion()
  64.     $__shell->hasReadline(', with readline() support' '');
  65. unset($f);
  66.  
  67. print $__shell->getColour("default");
  68. while($__shell->input()) {
  69.     try {
  70.         if ($__shell->parse(== 0{
  71.             ## we have a full command, execute it
  72.  
  73.             if ($__shell->isAutoloadEnabled(&& !function_exists('__autoload')) {
  74.                 /**
  75.                 * default autoloader
  76.                 *
  77.                 * If a class doesn't exist try to load it by guessing the filename
  78.                 * class PHP_Shell should be located in PHP/Shell.php.
  79.                 *
  80.                 * you can set your own autoloader by defining __autoload() before including
  81.                 * this file
  82.                 * 
  83.                 * @param string $classname name of the class
  84.                 */
  85.  
  86.                 function __autoload($classname{
  87.                     include str_replace('_''/'$classname).'.php';
  88.                 }
  89.             }
  90.  
  91.             $__shell_retval = eval($__shell->getCode());        
  92.             if (isset($__shell_retval)) {
  93.                 print $__shell->getColour("value");
  94.  
  95.                 if (function_exists("__shell_print_var")) {
  96.                     __shell_print_var($__shell_retval$__shell->getVerbose());
  97.                 else {
  98.                     var_export($__shell_retval);
  99.                 }
  100.             }
  101.             ## cleanup the variable namespace
  102.             unset($__shell_retval);
  103.             $__shell->resetCode();
  104.         }
  105.     catch(Exception $__shell_exception{
  106.         print $__shell->getColour("exception");
  107.         print $__shell_exception->getMessage();
  108.         
  109.         $__shell->resetCode();
  110.  
  111.         ## cleanup the variable namespace
  112.         unset($__shell_exception);
  113.     }
  114.     print $__shell->getColour("default");
  115. }
  116.  
  117. print $__shell->getColour("reset");
  118.  
  119. ?>

Documentation generated on Mon, 21 May 2007 22:28:45 -0600 by phpDocumentor 1.3.2