#!@prefix@/bin/php -Cq | // | Tomas V.V.Cox | // +----------------------------------------------------------------------+ // ini_set('allow_url_fopen', true); set_time_limit(0); ob_implicit_flush(true); ini_set('track_errors', true); ini_set('html_errors', false); require_once 'PEAR.php'; require_once 'PEAR/Config.php'; require_once 'PEAR/Command.php'; require_once 'Console/Getopt.php'; PEAR_Command::setFrontendType('CLI'); $all_commands = PEAR_Command::getCommands(); $argv = Console_Getopt::readPHPArgv(); $progname = basename(array_shift($argv)); $options = Console_Getopt::getopt($argv, "c:C:d:D:Gh?sSqu:v"); if (PEAR::isError($options)) { usage($options); } $opts = $options[0]; $fetype = 'CLI'; if ($progname == 'gpear' || $progname == 'pear-gtk') { $fetype = 'Gtk'; } else { foreach ($opts as $opt) { if ($opt[0] == 'G') { $fetype = 'Gtk'; } } } PEAR_Command::setFrontendType($fetype); $ui = &PEAR_Command::getFrontendObject(); PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($ui, "displayFatalError")); $pear_user_config = ''; $pear_system_config = ''; $store_user_config = false; $store_system_config = false; $verbose = 1; foreach ($opts as $opt) { switch ($opt[0]) { case 'c': $pear_user_config = $opt[1]; break; case 'C': $pear_system_config = $opt[1]; break; } } $config = &PEAR_Config::singleton($pear_user_config, $pear_system_config); $verbose = $config->get("verbose"); $cmdopts = array(); foreach ($opts as $opt) { $param = !empty($opt[1]) ? $opt[1] : true; switch ($opt[0]) { case 'd': list($key, $value) = explode('=', $param); $config->set($key, $value, 'user'); break; case 'D': list($key, $value) = explode('=', $param); $config->set($key, $value, 'system'); break; case 's': $store_user_config = true; break; case 'S': $store_system_config = true; break; case 'u': $config->remove($param, 'user'); break; case 'v': $config->set('verbose', $verbose + 1); break; case 'q': $config->set('verbose', $verbose - 1); break; default: // all non pear params goes to the command $cmdopts[$opt[0]] = $param; break; } } if ($store_system_config) { $config->store('system'); } if ($store_user_config) { $config->store('user'); } $command = (isset($options[1][0])) ? array_shift($options[1]) : null; if (empty($command) && ($store_user_config || $store_system_config)) { exit; } if ($fetype == 'Gtk') { Gtk::main(); } else do { if (empty($all_commands[$command]) || $command == 'help') { usage(null, @$options[1][2]); } $cmd = PEAR_Command::factory($command, $config); if (PEAR::isError($cmd)) { die($cmd->getMessage()); } $short_args = $long_args = null; PEAR_Command::getGetoptArgs($command, $short_args, $long_args); if (PEAR::isError($tmp = Console_Getopt::getopt($options[1], $short_args, $long_args))) { break; } list($tmpopt, $params) = $tmp; $opts = array(); foreach ($tmpopt as $foo => $tmp2) { list($opt, $value) = $tmp2; if ($value === null) { $value = true; // options without args } if (strlen($opt) == 1) { $cmdoptions = $cmd->getOptions($command); foreach ($cmdoptions as $o => $d) { if (@$d['shortopt'] == $opt) { $opts[$o] = $value; } } } else { if (substr($opt, 0, 2) == '--') { $opts[substr($opt, 2)] = $value; } } } $ok = $cmd->run($command, $opts, $params); if ($ok === false) { PEAR::raiseError("unknown command `$command'"); } } while (false); // {{{ usage() function usage($error = null, $helpsubject = null) { global $progname, $all_commands; $stderr = fopen('php://stderr', 'w'); fputs($stderr, "\n"); if (PEAR::isError($error)) { fputs($stderr, $error->getMessage() . "\n"); } elseif ($error !== null) { fputs($stderr, "$error\n"); } if ($helpsubject != null) { $put = cmdHelp($helpsubject); } else { $put = "Usage: $progname [options] command [command-options] \n". "Type \"$progname help options\" to list all options.\n". "Type \"$progname help \" to get the help for the specified command.\n". "Commands:\n"; $maxlen = max(array_map("strlen", $all_commands)); $formatstr = "%-{$maxlen}s %s\n"; foreach ($all_commands as $cmd => $class) { $put .= sprintf($formatstr, $cmd, PEAR_Command::getDescription($cmd)); } } fputs($stderr, "$put\n"); fclose($stderr); exit; } function cmdHelp($command) { global $progname, $all_commands, $config; if ($command == "options") { return "Options:\n". " -v increase verbosity level (default 1)\n". " -q be quiet, decrease verbosity level\n". " -c file find user configuration in `file'\n". " -C file find system configuration in `file'\n". " -d foo=bar set user config variable `foo' to `bar'\n". " -D foo=bar set system config variable `foo' to `bar'\n". " -G start in graphical (Gtk) mode\n". " -s store user configuration\n". " -S store system configuration\n". " -u foo unset `foo' in the user configuration\n". " -h, -? display help/usage (this message)\n"; } elseif ($help = PEAR_Command::getHelp($command)) { if (is_string($help)) { return "Usage : $help"; } return "Usage : $progname $command {$help[0]}\n{$help[1]}"; } return "No such command"; } // }}} /* * Local variables: * tab-width: 4 * c-basic-offset: 4 * indent-tabs-mode: nil * End: */ // vim600:syn=php ?>