home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / pearcmd.php < prev    next >
Encoding:
PHP Script  |  2004-03-24  |  9.2 KB  |  301 lines

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 5                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2004 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 3.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available through the world-wide-web at the following url:           |
  11. // | http://www.php.net/license/3_0.txt.                                  |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Stig Bakken <ssb@php.net>                                   |
  17. // |          Tomas V.V.Cox <cox@idecnet.com>                             |
  18. // |                                                                      |
  19. // +----------------------------------------------------------------------+
  20. //
  21. // $Id: pearcmd.php,v 1.9 2004/01/26 01:59:14 pajoye Exp $
  22.  
  23. ob_end_clean();
  24. /**
  25.  * @nodep Gtk
  26.  */
  27. if ('C:\xampp\php\pear' != '@'.'include_path'.'@') {
  28.     ini_set('include_path', 'C:\xampp\php\pear');
  29. }
  30. ini_set('allow_url_fopen', true);
  31. set_time_limit(0);
  32. ob_implicit_flush(true);
  33. ini_set('track_errors', true);
  34. ini_set('html_errors', false);
  35. ini_set('magic_quotes_runtime', false);
  36. set_error_handler('error_handler');
  37.  
  38. $pear_package_version = "1.3";
  39.  
  40. require_once 'PEAR.php';
  41. require_once 'PEAR/Config.php';
  42. require_once 'PEAR/Command.php';
  43. require_once 'Console/Getopt.php';
  44.  
  45. PEAR_Command::setFrontendType('CLI');
  46. $all_commands = PEAR_Command::getCommands();
  47.  
  48. $argv = Console_Getopt::readPHPArgv();
  49. /* $progname = basename($argv[0]); */
  50. $progname = 'pear';
  51. array_shift($argv);
  52. $options = Console_Getopt::getopt2($argv, "c:C:d:D:Gh?sSqu:vV");
  53. if (PEAR::isError($options)) {
  54.     usage($options);
  55. }
  56.  
  57. $opts = $options[0];
  58.  
  59. $fetype = 'CLI';
  60. if ($progname == 'gpear' || $progname == 'pear-gtk') {
  61.     $fetype = 'Gtk';
  62. } else {
  63.     foreach ($opts as $opt) {
  64.         if ($opt[0] == 'G') {
  65.             $fetype = 'Gtk';
  66.         }
  67.     }
  68. }
  69. PEAR_Command::setFrontendType($fetype);
  70. $ui = &PEAR_Command::getFrontendObject();
  71. PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($ui, "displayFatalError"));
  72.  
  73. $pear_user_config = '';
  74. $pear_system_config = '';
  75. $store_user_config = false;
  76. $store_system_config = false;
  77. $verbose = 1;
  78.  
  79. foreach ($opts as $opt) {
  80.     switch ($opt[0]) {
  81.         case 'c':
  82.             $pear_user_config = $opt[1];
  83.             break;
  84.         case 'C':
  85.             $pear_system_config = $opt[1];
  86.             break;
  87.     }
  88. }
  89.  
  90. $config = &PEAR_Config::singleton($pear_user_config, $pear_system_config);
  91. $verbose = $config->get("verbose");
  92. $cmdopts = array();
  93.  
  94. foreach ($opts as $opt) {
  95.     $param = !empty($opt[1]) ? $opt[1] : true;
  96.     switch ($opt[0]) {
  97.         case 'd':
  98.             list($key, $value) = explode('=', $param);
  99.             $config->set($key, $value, 'user');
  100.             break;
  101.         case 'D':
  102.             list($key, $value) = explode('=', $param);
  103.             $config->set($key, $value, 'system');
  104.             break;
  105.         case 's':
  106.             $store_user_config = true;
  107.             break;
  108.         case 'S':
  109.             $store_system_config = true;
  110.             break;
  111.         case 'u':
  112.             $config->remove($param, 'user');
  113.             break;
  114.         case 'v':
  115.             $config->set('verbose', $config->get('verbose') + 1);
  116.             break;
  117.         case 'q':
  118.             $config->set('verbose', $config->get('verbose') - 1);
  119.             break;
  120.         case 'V':
  121.             usage(null, 'version');
  122.         default:
  123.             // all non pear params goes to the command
  124.             $cmdopts[$opt[0]] = $param;
  125.             break;
  126.     }
  127. }
  128.  
  129. if ($store_system_config) {
  130.     $config->store('system');
  131. }
  132.  
  133. if ($store_user_config) {
  134.     $config->store('user');
  135. }
  136.  
  137. $command = (isset($options[1][0])) ? $options[1][0] : null;
  138.  
  139. if (empty($command) && ($store_user_config || $store_system_config)) {
  140.     exit;
  141. }
  142.  
  143. if ($fetype == 'Gtk') {
  144.     Gtk::main();
  145. } else do {
  146.     if ($command == 'help') {
  147.         usage(null, @$options[1][1]);
  148.     }
  149.  
  150.     PEAR::pushErrorHandling(PEAR_ERROR_RETURN);
  151.     $cmd = PEAR_Command::factory($command, $config);
  152.     PEAR::popErrorHandling();
  153.     if (PEAR::isError($cmd)) {
  154.         usage(null, @$options[1][1]);
  155.     }
  156.  
  157.     $short_args = $long_args = null;
  158.     PEAR_Command::getGetoptArgs($command, $short_args, $long_args);
  159.     array_shift($options[1]);
  160.     if (PEAR::isError($tmp = Console_Getopt::getopt2($options[1], $short_args, $long_args))) {
  161.         break;
  162.     }
  163.     list($tmpopt, $params) = $tmp;
  164.     $opts = array();
  165.     foreach ($tmpopt as $foo => $tmp2) {
  166.         list($opt, $value) = $tmp2;
  167.         if ($value === null) {
  168.             $value = true; // options without args
  169.         }
  170.         if (strlen($opt) == 1) {
  171.             $cmdoptions = $cmd->getOptions($command);
  172.             foreach ($cmdoptions as $o => $d) {
  173.                 if (@$d['shortopt'] == $opt) {
  174.                     $opts[$o] = $value;
  175.                 }
  176.             }
  177.         } else {
  178.             if (substr($opt, 0, 2) == '--') {
  179.                 $opts[substr($opt, 2)] = $value;
  180.             }
  181.         }
  182.     }
  183.     $ok = $cmd->run($command, $opts, $params);
  184.     if ($ok === false) {
  185.         PEAR::raiseError("unknown command `$command'");
  186.     }
  187. } while (false);
  188.  
  189. // {{{ usage()
  190.  
  191. function usage($error = null, $helpsubject = null)
  192. {
  193.     global $progname, $all_commands;
  194.     $stderr = fopen('php://stderr', 'w');
  195.     if (PEAR::isError($error)) {
  196.         fputs($stderr, $error->getMessage() . "\n");
  197.     } elseif ($error !== null) {
  198.         fputs($stderr, "$error\n");
  199.     }
  200.     if ($helpsubject != null) {
  201.         $put = cmdHelp($helpsubject);
  202.     } else {
  203.         $put =
  204.             "Usage: $progname [options] command [command-options] <parameters>\n".
  205.             "Type \"$progname help options\" to list all options.\n".
  206.             "Type \"$progname help <command>\" to get the help for the specified command.\n".
  207.             "Commands:\n";
  208.         $maxlen = max(array_map("strlen", $all_commands));
  209.         $formatstr = "%-{$maxlen}s  %s\n";
  210.         ksort($all_commands);
  211.         foreach ($all_commands as $cmd => $class) {
  212.             $put .= sprintf($formatstr, $cmd, PEAR_Command::getDescription($cmd));
  213.         }
  214.     }
  215.     fputs($stderr, "$put\n");
  216.     fclose($stderr);
  217.     exit;
  218. }
  219.  
  220. function cmdHelp($command)
  221. {
  222.     global $progname, $all_commands, $config;
  223.     if ($command == "options") {
  224.         return
  225.         "Options:\n".
  226.         "     -v         increase verbosity level (default 1)\n".
  227.         "     -q         be quiet, decrease verbosity level\n".
  228.         "     -c file    find user configuration in `file'\n".
  229.         "     -C file    find system configuration in `file'\n".
  230.         "     -d foo=bar set user config variable `foo' to `bar'\n".
  231.         "     -D foo=bar set system config variable `foo' to `bar'\n".
  232.         "     -G         start in graphical (Gtk) mode\n".
  233.         "     -s         store user configuration\n".
  234.         "     -S         store system configuration\n".
  235.         "     -u foo     unset `foo' in the user configuration\n".
  236.         "     -h, -?     display help/usage (this message)\n".
  237.         "     -V         version information\n";
  238.     } elseif ($command == "shortcuts") {
  239.         $sc = PEAR_Command::getShortcuts();
  240.         $ret = "Shortcuts:\n";
  241.         foreach ($sc as $s => $c) {
  242.             $ret .= sprintf("     %-8s %s\n", $s, $c);
  243.         }
  244.         return $ret;
  245.  
  246.     } elseif ($command == "version") {
  247.         return "PEAR Version: ".$GLOBALS['pear_package_version'].
  248.                "\nPHP Version: ".phpversion().
  249.                "\nZend Engine Version: ".zend_version().
  250.                "\nRunning on: ".php_uname();
  251.  
  252.     } elseif ($help = PEAR_Command::getHelp($command)) {
  253.         if (is_string($help)) {
  254.             return "$progname $command [options] $help\n";
  255.         }
  256.         if ($help[1] === null) {
  257.             return "$progname $command $help[0]";
  258.         } else {
  259.             return "$progname $command [options] $help[0]\n$help[1]";
  260.         }
  261.     }
  262.     return "No such command";
  263. }
  264.  
  265. // }}}
  266.  
  267. function error_handler($errno, $errmsg, $file, $line, $vars) {
  268.     if ((defined('E_STRICT') && $errno & E_STRICT) || !error_reporting()) {
  269.         return; // @silenced error
  270.     }
  271.     $errortype = array (
  272.         E_ERROR   =>  "Error",
  273.         E_WARNING   =>  "Warning",
  274.         E_PARSE   =>  "Parsing Error",
  275.         E_NOTICE   =>  "Notice",
  276.         E_CORE_ERROR  =>  "Core Error",
  277.         E_CORE_WARNING  =>  "Core Warning",
  278.         E_COMPILE_ERROR  =>  "Compile Error",
  279.         E_COMPILE_WARNING =>  "Compile Warning",
  280.         E_USER_ERROR =>  "User Error",
  281.         E_USER_WARNING =>  "User Warning",
  282.         E_USER_NOTICE =>  "User Notice"
  283.     );
  284.     $prefix = $errortype[$errno];
  285.     $file = basename($file);
  286.     print "\n$prefix: $errmsg in $file on line $line\n";
  287. }
  288.  
  289.  
  290. /*
  291.  * Local variables:
  292.  * tab-width: 4
  293.  * c-basic-offset: 4
  294.  * indent-tabs-mode: nil
  295.  * mode: php
  296.  * End:
  297.  */
  298. // vim600:syn=php
  299.  
  300. ?>
  301.