3 * PEAR, the PHP Extension and Application Repository
5 * Command line interface
11 * @author Stig Bakken <ssb@php.net>
12 * @author Tomas V.V.Cox <cox@idecnet.com>
13 * @copyright 1997-2009 The Authors
14 * @license http://opensource.org/licenses/bsd-license.php New BSD License
15 * @version CVS: $Id: pearcmd.php 313023 2011-07-06 19:17:11Z dufuz $
16 * @link http://pear.php.net/package/PEAR
20 if (!defined('PEAR_RUNTYPE')) {
21 // this is defined in peclcmd.php as 'pecl'
22 define('PEAR_RUNTYPE', 'pear');
24 define('PEAR_IGNORE_BACKTRACE', 1);
28 if ('@include_path@' != '@'.'include_path'.'@') {
29 ini_set('include_path', '@include_path@');
32 // this is a raw, uninstalled pear, either a cvs checkout, or php distro
35 @ini_set('allow_url_fopen', true);
36 if (!ini_get('safe_mode')) {
39 ob_implicit_flush(true);
40 @ini_set('track_errors', true);
41 @ini_set('html_errors', false);
42 @ini_set('magic_quotes_runtime', false);
43 $_PEAR_PHPDIR = '#$%^&*';
44 set_error_handler('error_handler');
46 $pear_package_version = "@pear_version@";
48 require_once 'PEAR.php';
49 require_once 'PEAR/Frontend.php';
50 require_once 'PEAR/Config.php';
51 require_once 'PEAR/Command.php';
52 require_once 'Console/Getopt.php';
55 PEAR_Command::setFrontendType('CLI');
56 $all_commands = PEAR_Command::getCommands();
58 // remove this next part when we stop supporting that crap-ass PHP 4.2
59 if (!isset($_SERVER['argv']) && !isset($argv) && !isset($HTTP_SERVER_VARS['argv'])) {
60 echo 'ERROR: either use the CLI php executable, or set register_argc_argv=On in php.ini';
64 $argv = Console_Getopt::readPHPArgv();
65 // fix CGI sapi oddity - the -- in pear.bat/pear is not removed
66 if (php_sapi_name() != 'cli' && isset($argv[1]) && $argv[1] == '--') {
68 $argv = array_values($argv);
70 $progname = PEAR_RUNTYPE;
72 $options = Console_Getopt::getopt2($argv, "c:C:d:D:Gh?sSqu:vV");
73 if (PEAR::isError($options)) {
80 if ($progname == 'gpear' || $progname == 'pear-gtk') {
83 foreach ($opts as $opt) {
89 //Check if Gtk and PHP >= 5.1.0
90 if ($fetype == 'Gtk' && version_compare(phpversion(), '5.1.0', '>=')) {
94 $pear_user_config = '';
95 $pear_system_config = '';
96 $store_user_config = false;
97 $store_system_config = false;
100 foreach ($opts as $opt) {
103 $pear_user_config = $opt[1];
106 $pear_system_config = $opt[1];
111 PEAR_Command::setFrontendType($fetype);
112 $ui = &PEAR_Command::getFrontendObject();
113 $config = &PEAR_Config::singleton($pear_user_config, $pear_system_config);
115 if (PEAR::isError($config)) {
117 if ($pear_user_config !== false) {
118 $_file .= $pear_user_config;
120 if ($pear_system_config !== false) {
121 $_file .= '/' . $pear_system_config;
124 $_file = 'The default config file';
126 $config->getMessage();
127 $ui->outputData("ERROR: $_file is not a valid config file or is corrupted.");
128 // We stop, we have no idea where we are :)
132 // this is used in the error handler to retrieve a relative path
133 $_PEAR_PHPDIR = $config->get('php_dir');
134 $ui->setConfig($config);
135 PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($ui, "displayFatalError"));
136 if (ini_get('safe_mode')) {
137 $ui->outputData('WARNING: running in safe mode requires that all files created ' .
138 'be the same uid as the current script. PHP reports this script is uid: ' .
139 @getmyuid() . ', and current user is: ' . @get_current_user());
142 $verbose = $config->get("verbose");
146 if (!$config->isDefinedLayer('user') && !$config->isDefinedLayer('system')) {
148 foreach ($opts as $opt) {
149 if ($opt[0] == 'd' || $opt[0] == 'D') {
150 $found = true; // the user knows what they are doing, and are setting config values
154 // no prior runs, try to install PEAR
155 if (strpos(dirname(__FILE__), 'scripts')) {
156 $packagexml = dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . 'package2.xml';
157 $pearbase = dirname(dirname(__FILE__));
159 $packagexml = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'package2.xml';
160 $pearbase = dirname(__FILE__);
162 if (file_exists($packagexml)) {
167 $config->set('php_dir', $pearbase . DIRECTORY_SEPARATOR . 'php');
168 $config->set('data_dir', $pearbase . DIRECTORY_SEPARATOR . 'data');
169 $config->set('doc_dir', $pearbase . DIRECTORY_SEPARATOR . 'docs');
170 $config->set('test_dir', $pearbase . DIRECTORY_SEPARATOR . 'tests');
171 $config->set('ext_dir', $pearbase . DIRECTORY_SEPARATOR . 'extensions');
172 $config->set('bin_dir', $pearbase);
173 $config->mergeConfigFile($pearbase . 'pear.ini', false);
175 $config->set('auto_discover', 1);
180 foreach ($opts as $opt) {
181 $param = !empty($opt[1]) ? $opt[1] : true;
184 if ($param === true) {
185 die('Invalid usage of "-d" option, expected -d config_value=value, ' .
186 'received "-d"' . "\n");
188 $possible = explode('=', $param);
189 if (count($possible) != 2) {
190 die('Invalid usage of "-d" option, expected -d config_value=value, received "' .
191 $param . '"' . "\n");
193 list($key, $value) = explode('=', $param);
194 $config->set($key, $value, 'user');
197 if ($param === true) {
198 die('Invalid usage of "-d" option, expected -d config_value=value, ' .
199 'received "-d"' . "\n");
201 $possible = explode('=', $param);
202 if (count($possible) != 2) {
203 die('Invalid usage of "-d" option, expected -d config_value=value, received "' .
204 $param . '"' . "\n");
206 list($key, $value) = explode('=', $param);
207 $config->set($key, $value, 'system');
210 $store_user_config = true;
213 $store_system_config = true;
216 $config->remove($param, 'user');
219 $config->set('verbose', $config->get('verbose') + 1);
222 $config->set('verbose', $config->get('verbose') - 1);
225 usage(null, 'version');
230 // all non pear params goes to the command
231 $cmdopts[$opt[0]] = $param;
236 if ($store_system_config) {
237 $config->store('system');
240 if ($store_user_config) {
241 $config->store('user');
244 $command = (isset($options[1][0])) ? $options[1][0] : null;
245 if (empty($command) && ($store_user_config || $store_system_config)) {
249 if ($fetype == 'Gtk' || $fetype == 'Gtk2') {
250 if (!$config->validConfiguration()) {
251 PEAR::raiseError('CRITICAL ERROR: no existing valid configuration files found in files ' .
252 "'$pear_user_config' or '$pear_system_config', please copy an existing configuration" .
253 'file to one of these locations, or use the -c and -s options to create one');
257 if ($command == 'help') {
258 usage(null, @$options[1][1]);
261 if (!$config->validConfiguration()) {
262 PEAR::raiseError('CRITICAL ERROR: no existing valid configuration files found in files ' .
263 "'$pear_user_config' or '$pear_system_config', please copy an existing configuration" .
264 'file to one of these locations, or use the -c and -s options to create one');
267 PEAR::pushErrorHandling(PEAR_ERROR_RETURN);
268 $cmd = PEAR_Command::factory($command, $config);
269 PEAR::popErrorHandling();
270 if (PEAR::isError($cmd)) {
271 usage(null, @$options[1][0]);
274 $short_args = $long_args = null;
275 PEAR_Command::getGetoptArgs($command, $short_args, $long_args);
276 array_shift($options[1]);
277 $tmp = Console_Getopt::getopt2($options[1], $short_args, $long_args);
279 if (PEAR::isError($tmp)) {
283 list($tmpopt, $params) = $tmp;
285 foreach ($tmpopt as $foo => $tmp2) {
286 list($opt, $value) = $tmp2;
287 if ($value === null) {
288 $value = true; // options without args
291 if (strlen($opt) == 1) {
292 $cmdoptions = $cmd->getOptions($command);
293 foreach ($cmdoptions as $o => $d) {
294 if (isset($d['shortopt']) && $d['shortopt'] == $opt) {
299 if (substr($opt, 0, 2) == '--') {
300 $opts[substr($opt, 2)] = $value;
305 $ok = $cmd->run($command, $opts, $params);
307 PEAR::raiseError("unknown command `$command'");
310 if (PEAR::isError($ok)) {
311 PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, array($ui, "displayFatalError"));
312 PEAR::raiseError($ok);
318 function usage($error = null, $helpsubject = null)
320 global $progname, $all_commands;
321 $stdout = fopen('php://stdout', 'w');
322 if (PEAR::isError($error)) {
323 fputs($stdout, $error->getMessage() . "\n");
324 } elseif ($error !== null) {
325 fputs($stdout, "$error\n");
328 if ($helpsubject != null) {
329 $put = cmdHelp($helpsubject);
331 $put = "Commands:\n";
332 $maxlen = max(array_map("strlen", $all_commands));
333 $formatstr = "%-{$maxlen}s %s\n";
334 ksort($all_commands);
335 foreach ($all_commands as $cmd => $class) {
336 $put .= sprintf($formatstr, $cmd, PEAR_Command::getDescription($cmd));
339 "Usage: $progname [options] command [command-options] <parameters>\n".
340 "Type \"$progname help options\" to list all options.\n".
341 "Type \"$progname help shortcuts\" to list all command shortcuts.\n".
342 "Type \"$progname help <command>\" to get the help for the specified command.";
344 fputs($stdout, "$put\n");
347 if ($error === null) {
353 function cmdHelp($command)
355 global $progname, $all_commands, $config;
356 if ($command == "options") {
359 " -v increase verbosity level (default 1)\n".
360 " -q be quiet, decrease verbosity level\n".
361 " -c file find user configuration in `file'\n".
362 " -C file find system configuration in `file'\n".
363 " -d foo=bar set user config variable `foo' to `bar'\n".
364 " -D foo=bar set system config variable `foo' to `bar'\n".
365 " -G start in graphical (Gtk) mode\n".
366 " -s store user configuration\n".
367 " -S store system configuration\n".
368 " -u foo unset `foo' in the user configuration\n".
369 " -h, -? display help/usage (this message)\n".
370 " -V version information\n";
371 } elseif ($command == "shortcuts") {
372 $sc = PEAR_Command::getShortcuts();
373 $ret = "Shortcuts:\n";
374 foreach ($sc as $s => $c) {
375 $ret .= sprintf(" %-8s %s\n", $s, $c);
379 } elseif ($command == "version") {
380 return "PEAR Version: ".$GLOBALS['pear_package_version'].
381 "\nPHP Version: ".phpversion().
382 "\nZend Engine Version: ".zend_version().
383 "\nRunning on: ".php_uname();
385 } elseif ($help = PEAR_Command::getHelp($command)) {
386 if (is_string($help)) {
387 return "$progname $command [options] $help\n";
390 if ($help[1] === null) {
391 return "$progname $command $help[0]";
394 return "$progname $command [options] $help[0]\n$help[1]";
397 return "Command '$command' is not valid, try '$progname help'";
402 function error_handler($errno, $errmsg, $file, $line, $vars) {
403 if ((defined('E_STRICT') && $errno & E_STRICT) || (defined('E_DEPRECATED') &&
404 $errno & E_DEPRECATED) || !error_reporting()) {
405 if (defined('E_STRICT') && $errno & E_STRICT) {
408 if (defined('E_DEPRECATED') && $errno & E_DEPRECATED) {
409 return; // E_DEPRECATED
411 if ($GLOBALS['config']->get('verbose') < 4) {
412 return false; // @silenced error, show all if debug is high enough
417 E_WARNING => "Warning",
418 E_PARSE => "Parsing Error",
419 E_NOTICE => "Notice",
420 E_CORE_ERROR => "Core Error",
421 E_CORE_WARNING => "Core Warning",
422 E_COMPILE_ERROR => "Compile Error",
423 E_COMPILE_WARNING => "Compile Warning",
424 E_USER_ERROR => "User Error",
425 E_USER_WARNING => "User Warning",
426 E_USER_NOTICE => "User Notice"
428 $prefix = $errortype[$errno];
429 global $_PEAR_PHPDIR;
430 if (stristr($file, $_PEAR_PHPDIR)) {
431 $file = substr($file, strlen($_PEAR_PHPDIR) + 1);
433 $file = basename($file);
435 print "\n$prefix: $errmsg in $file on line $line\n";
444 * indent-tabs-mode: nil