pearcmd.php 9.5 KB

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