]> granicus.if.org Git - php/commitdiff
* modularize "pear" and "pear-get" commands somewhat. checking options
authorStig Bakken <ssb@php.net>
Wed, 30 Jan 2002 12:20:12 +0000 (12:20 +0000)
committerStig Bakken <ssb@php.net>
Wed, 30 Jan 2002 12:20:12 +0000 (12:20 +0000)
  etc. is done for both in pearcmd-common.php.

12 files changed:
pear/Makefile.in
pear/scripts/pear-get.in
pear/scripts/pear.in
pear/scripts/pearcmd-common.php [new file with mode: 0644]
pear/scripts/pearcmd-help.php [new file with mode: 0644]
pear/scripts/pearcmd-info.php [new file with mode: 0644]
pear/scripts/pearcmd-install.php [new file with mode: 0644]
pear/scripts/pearcmd-list.php [new file with mode: 0644]
pear/scripts/pearcmd-package.php [new file with mode: 0644]
pear/scripts/pearcmd-remote-list.php [new file with mode: 0644]
pear/scripts/pearcmd-show-config.php [new file with mode: 0644]
pear/scripts/pearcmd-uninstall.php [new file with mode: 0644]

index 80b7a0cbe8a30c13de789f534afaaecf8bf9d6c0..ffc0b2c7b7cdce59a43cad4e3ce1bff4c277e9d9 100644 (file)
@@ -83,6 +83,17 @@ PEAR_FILES = \
        System.php \
        XML/Parser.php 
 
+PEAR_COMMAND_LIBS=\
+       pearcmd-common.php \
+       pearcmd-help.php \
+       pearcmd-info.php \
+       pearcmd-install.php \
+       pearcmd-list.php \
+       pearcmd-package.php \
+       pearcmd-remote-list.php \
+       pearcmd-show-config.php \
+       pearcmd-uninstall.php
+
 install-data-local:
        @if $(mkinstalldirs) $(INSTALL_ROOT)$(peardir); then \
                for i in $(PEAR_SUBDIRS); do \
@@ -131,6 +142,10 @@ install-programs:
        for prog in phpextdist; do \
                echo "Installing program: $$prog"; \
                $(INSTALL) -m 755 $(srcdir)/scripts/$$prog $(INSTALL_ROOT)$(bindir)/$$prog; \
+       done; \
+       for lib in $(PEAR_COMMAND_LIBS); do \
+               echo "Installing program library: $$lib"; \
+               $(INSTALL) -m 755 $(srcdir)/scripts/$$lib $(INSTALL_ROOT)$(bindir)/$$lib; \
        done
 
 HEADER_DIRS = \
index 49cd2840032f4e607e613794a1575350f9e003dc..5ff308de03e1b0317c7139cb980afc75df1417ab 100644 (file)
 // +----------------------------------------------------------------------+
 //
 
-
-/*
-
-command structure:
-
-* list available packages
-* list not-installed packages
-* list upgrades
-* list packages (by author/category/state)
-
-* installation commands:
-** install latest stable
-** install specific release
-
-* package generation commands:
-
-*/
-
 require_once 'PEAR.php';
-require_once 'PEAR/Common.php';
-require_once 'PEAR/Config.php';
 require_once 'PEAR/Remote.php';
-require_once 'PEAR/Registry.php';
-require_once 'Console/Getopt.php';
 
 error_reporting(E_ALL & ~E_NOTICE);
 
-$argv = Console_Getopt::readPHPArgv();
-if (PEAR::isError($argv)) {
-    die($argv->getMessage());
-}
-$options = Console_Getopt::getopt($argv, "c:C:d:D:h?sSqu:v");
-if (PEAR::isError($options)) {
-    usage($options);
-}
-
-PEAR::setErrorHandling(PEAR_ERROR_PRINT, "pear: %s\n");
-
-if (OS_WINDOWS) {
-    $pear_default_config = PHP_SYSCONFDIR.DIRECTORY_SEPARATOR.'pearsys.ini';
-    $pear_user_config    = PHP_SYSCONFDIR.DIRECTORY_SEPARATOR.'pear.ini';
-} else {
-    $pear_default_config = PHP_SYSCONFDIR.DIRECTORY_SEPARATOR.'pear.conf';
-    $pear_user_config    = getenv('HOME').DIRECTORY_SEPARATOR.'.pearrc';
-}
-
-$opts = $options[0];
-
-foreach ($opts as $opt) {
-    switch ($opt[0]) {
-        case 'c':
-            $pear_user_config = $opt[1];
-            break;
-        case 'C':
-            $pear_default_config = $opt[1];
-            break;
-    }
-}
-
-$config = new PEAR_Config($pear_user_config, $pear_default_config);
-$verbose = 1;
-
-foreach ($opts as $opt) {
-    $param = $opt[1];
-    switch ($opt[0]) {
-        case 'd':
-            list($key, $value) = explode('=', $param);
-            $config->set($key, $value);
-            break;
-        case 'D':
-            list($key, $value) = explode('=', $param);
-            $config->set($key, $value, true);
-            break;
-        case 'u':
-            $config->toDefault($param);
-            break;
-        case 'v':
-            $verbose++;
-            break;
-        case 'q':
-            $verbose--;
-            break;
-    }
-}
-
-$fallback_config = array(
-    'master_server' => 'pear.php.net',
-    'php_dir'       => PEAR_INSTALL_DIR,
-    'ext_dir'       => PEAR_EXTENSION_DIR,
-    'doc_dir'       => '',
-    'verbose'       => true,
+$subcommands = array(
+    'help'        => 'help [command]',
+    'install'     => 'install [-r] <package file/name>',
+    'upgrade'     => 'upgrade [-r] <package file/name>',
+    'list'        => 'list',
+    'info'        => 'info <package name>',
 );
-$fallback_done = array();
-
-foreach ($fallback_config as $key => $value) {
-    if (!$config->isDefined($key)) {
-        $config->set($key, $value);
-        $fallback_done[$key] = true;
-    }
-}
-
-//$verbose    = $config->get("verbose");
-$script_dir = $config->get("php_dir");
-$ext_dir    = $config->get("ext_dir");
-$doc_dir    = $config->get("doc_dir");
 
-$command = (isset($options[1][1])) ? $options[1][1] : null;
-$rest = array_slice($options[1], 2);
+$shortcuts = array(
+    'list'     => 'remote-list',
+    'upgrade'  => 'install',
+);
 
 $command_options = array(
-    "install"        => "fr",
-    "uninstall"      => "fr",
-    "upgrade"        => "fr",
+    'install' => 'fr',
 );
 
-if (isset($command_options[$command])) {
-    $tmp = Console_Getopt::getopt($rest, $command_options[$command]);
-    if (PEAR::isError($tmp)) {
-        usage($tmp);
-    }
-    $cmdopts = $tmp[0];
-    $cmdargs = $tmp[1];
-} else {
-    $cmdopts = array();
-    $cmdargs = $rest;
-}
-
-// }}}
-
-switch ($command) {
-
-    // {{{ install, upgrade
-
-    case 'install': case 'upgrade': {
-        include_once 'PEAR/Installer.php';
-        $pkgfile = $cmdargs[0];
-        $installer =& new PEAR_Installer($script_dir, $ext_dir, $doc_dir);
-        $installer->setErrorHandling(PEAR_ERROR_DIE,
-                                     basename($pkgfile) . ": %s\n");
-        $installer->debug = $verbose;
-        $install_options = array();
-        if ($command == 'upgrade') {
-            $install_options['upgrade'] = true;
-        }
-        foreach ($cmdopts as $opt) {
-            switch ($opt[0]) {
-                case 'r':
-                    // This option is for use by rpm and other package
-                    // tools that can install files etc. by itself, but
-                    // still needs to register the package as installed in
-                    // PEAR's local registry.
-                    $install_options['register_only'] = true;
-                    break;
-                case 'f':
-                    $install_options['force'] = true;
-                    break;
-            }
-        }
-        if ($installer->install($pkgfile, $install_options, $config)) {
-            print "install ok\n";
-        } else {
-            print "install failed\n";
-        }
-        break;
-    }
-
-    // }}}
-    // {{{ uninstall
-    case 'uninstall': {
-        include_once 'PEAR/Installer.php';
-        $pkgfile = $cmdargs[0];
-        $installer =& new PEAR_Installer($script_dir, $ext_dir, $doc_dir);
-        $installer->setErrorHandling(PEAR_ERROR_DIE,
-                                     basename($pkgfile) . ": %s\n");
-        $installer->debug = $verbose;
-        $uninstall_options = array();
-        foreach ($cmdopts as $opt) {
-            switch ($opt[0]) {
-                case 'r':
-                    $uninstall_options['register_only'] = true;
-                    break;
-                case 'f':
-                    $uninstall_options['force'] = true;
-                    break;
-            }
-        }
-        if ($installer->uninstall($pkgfile, $uninstall_options)) {
-            print "uninstall ok\n";
-        } else {
-            print "uninstall failed\n";
-        }
-        break;
-    }
-    // }}}
-    // {{{ list
-
-    case 'list': {
-        $remote = new PEAR_Remote($config);
-        $result = $remote->call('package.listAll');
-        $i = $j = 0;
-        heading("Available packages");
-        foreach ($result as $package => $info) {
-            if ($i++ % 20 == 0) {
-                if ($j++ > 0) {
-                    print "\n";
-                }
-                printf("%-20s %-10s %-15s %s\n",
-                       "Package", "Stable", "Lead", "Category");
-                print str_repeat("-", 75)."\n";
-            }
-            if (empty($info['stable'])) {
-                $info['stable'] = '(none)';
-            }
-            $stable = (string)$info['stable'];
-            printf("%-20s %-10s %-15s %s\n", $package,
-                   $info['stable'], $info['lead'], $info['category']);
-        }
-        break;
-    }
+include "pearcmd-common.php";
 
-    // }}}
-    default: {
-        if (!$store_default_config && !$store_user_config) {
-            usage();
-        }
-        break;
-    }
-}
-
-// {{{ usage()
-
-function usage($error = null)
-{
-    $stderr = fopen('php://stderr', 'w');
-    if (PEAR::isError($error)) {
-        fputs($stderr, $error->getMessage());
-    } elseif ($error !== null) {
-        fputs($stderr, $error);
-    }
-    fputs($stderr,
-          "Usage: pear-get [options] command [command-options] <parameters>\n".
-          "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".
-          "     -u foo     unset `foo' in the user configuration\n".
-          "     -h, -?     display help/usage (this message)\n".
-          "Commands:\n".
-          "   help [command]\n".
-          "   install [-r] <package file>\n".
-          "   uninstall [-r] <package name>\n".
-          "   list\n".
-          "   info\n".
-          "\n");
-    fclose($stderr);
-    exit;
-}
-
-// }}}
-// {{{ present_array()
-
-function present_array(&$arr, $keys = null)
-{
-    if ($keys === null) {
-        $keys = array_keys($arr);
-    }
-    $longest_key = max(array_map("strlen", array_keys($arr))) + 2;
-    $format_string = "%{$longest_key}s : %s\n";
-    foreach ($keys as $k) {
-        if (is_array($arr[$k])) {
-            foreach ($arr[$k] as $i => $value) {
-                $x = "$k #$i";
-                $cont = array();
-                foreach(array_keys($value) as $val) {
-                    $cont[] = "$val=" . $value[$val];
-                }
-                $v = implode(", ", $cont);
-                printf($format_string, $x, $v);
-            }
-            continue;
-        } else {
-            $v = $arr[$k];
-            printf($format_string, $k, $v);
-        }
-    }
+if (isset($shortcuts[$command])) {
+    $realcommand = $shortcuts[$command];
+} else {
+    $realcommand = $command;
 }
 
-// }}}
-// {{{ heading()
-
-function heading($text)
-{
-    $l = strlen(trim($text));
-    print rtrim($text) . "\n" . str_repeat("=", $l) . "\n";
+if (isset($subcommands[$command])) {
+    include "pearcmd-$realcommand.php";
+} elseif (!$store_default_config && !$store_user_config) {
+    usage();
 }
 
-// }}}
-
-/*
- * Local variables:
- * tab-width: 4
- * c-basic-offset: 4
- * indent-tabs-mode: nil
- * End:
- */
-
 ?>
index 0791419b3b98eb372d7a3a3630f43fe278ef6aab..d7bb854b53bb2cc4e4bb2e7cc4973665ae1d25fc 100644 (file)
 // +----------------------------------------------------------------------+
 //
 
-
-/*
-
-command structure:
-
-* remote queries:
-** list available packages
-** list not-installed packages
-** list upgrades
-
-** list packages (by author/category/state)
-
-* operations on package files:
-
-* installation commands:
-
-* package generation commands:
-
-
-
- */
 require_once 'PEAR.php';
 require_once 'PEAR/Common.php';
-require_once 'PEAR/Config.php';
-require_once 'PEAR/Remote.php';
 require_once 'PEAR/Registry.php';
-require_once 'Console/Getopt.php';
 
 error_reporting(E_ALL & ~E_NOTICE);
 
-$argv = Console_Getopt::readPHPArgv();
-if (PEAR::isError($argv)) {
-    die($argv->getMessage());
-}
-$options = Console_Getopt::getopt($argv, "c:C:d:D:h?sSqu:v");
-if (PEAR::isError($options)) {
-    usage($options);
-}
-
-PEAR::setErrorHandling(PEAR_ERROR_PRINT, "pear: %s\n");
-
-if (OS_WINDOWS) {
-    $pear_default_config = PHP_SYSCONFDIR.DIRECTORY_SEPARATOR.'pearsys.ini';
-    $pear_user_config    = PHP_SYSCONFDIR.DIRECTORY_SEPARATOR.'pear.ini';
-} else {
-    $pear_default_config = PHP_SYSCONFDIR.DIRECTORY_SEPARATOR.'pear.conf';
-    $pear_user_config    = getenv('HOME').DIRECTORY_SEPARATOR.'.pearrc';
-}
-
-$opts = $options[0];
-
-foreach ($opts as $opt) {
-    switch ($opt[0]) {
-        case 'c':
-            $pear_user_config = $opt[1];
-            break;
-        case 'C':
-            $pear_default_config = $opt[1];
-            break;
-    }
-}
-
-$config = new PEAR_Config($pear_user_config, $pear_default_config);
-$store_user_config = false;
-$store_default_config = false;
-$verbose = 1;
-
-foreach ($opts as $opt) {
-    $param = $opt[1];
-    switch ($opt[0]) {
-        case 'd':
-            list($key, $value) = explode('=', $param);
-            $config->set($key, $value);
-            break;
-        case 'D':
-            list($key, $value) = explode('=', $param);
-            $config->set($key, $value, true);
-            break;
-        case 's':
-            $store_user_config = true;
-            break;
-        case 'S':
-            $store_default_config = true;
-            break;
-        case 'u':
-            $config->toDefault($param);
-            break;
-        case 'v':
-            $verbose++;
-            break;
-        case 'q':
-            $verbose--;
-            break;
-    }
-}
-
-if ($store_default_config) {
-    if (@is_writeable($pear_default_config)) {
-        $config->writeConfigFile($pear_default_config, 'default');
-    } else {
-        die("You don't have write access to $pear_default_config, exiting!\n");
-    }
-}
-
-if ($store_user_config) {
-    $config->writeConfigFile($pear_user_config, 'userdefined');
-}
-
-$fallback_config = array(
-    'master_server' => 'pear.php.net',
-    'php_dir'       => PEAR_INSTALL_DIR,
-    'ext_dir'       => PEAR_EXTENSION_DIR,
-    'doc_dir'       => PHP_DATADIR . DIRECTORY_SEPARATOR . 'pear' .
-                       DIRECTORY_SEPARATOR . 'doc',
-    'verbose'       => true,
+$subcommands = array(
+    'help'        => 'help [command]',
+    'uninstall'   => 'uninstall [-r] <package name>',
+    'package'     => 'package [package info file]',
+    'info'        => 'info',
+    'list'        => 'list',
+    'show-config' => 'show-config',
 );
-$fallback_done = array();
-
-foreach ($fallback_config as $key => $value) {
-    if (!$config->isDefined($key)) {
-        $config->set($key, $value);
-        $fallback_done[$key] = true;
-    }
-}
-
-//$verbose    = $config->get("verbose");
-$script_dir = $config->get("php_dir");
-$ext_dir    = $config->get("ext_dir");
-$doc_dir    = $config->get("doc_dir");
-
-$command = (isset($options[1][1])) ? $options[1][1] : null;
-$rest = array_slice($options[1], 2);
 
 $command_options = array(
-    "list-installed" => "v",
-    "install"        => "fr",
-    "uninstall"      => "fr",
-    "upgrade"        => "fr",
+    "list"      => "v",
+    "uninstall" => "fr",
 );
 
-if (isset($command_options[$command])) {
-    $tmp = Console_Getopt::getopt($rest, $command_options[$command]);
-    if (PEAR::isError($tmp)) {
-        usage($tmp);
-    }
-    $cmdopts = $tmp[0];
-    $cmdargs = $tmp[1];
-} else {
-    $cmdopts = array();
-    $cmdargs = $rest;
-}
-
-// }}}
-
-switch ($command) {
-
-    // {{{ install
-
-    case 'install': case 'upgrade': {
-        include_once 'PEAR/Installer.php';
-        $pkgfile = $cmdargs[0];
-        $installer =& new PEAR_Installer($script_dir, $ext_dir, $doc_dir);
-        $installer->setErrorHandling(PEAR_ERROR_DIE,
-                                     basename($pkgfile) . ": %s\n");
-        $installer->debug = $verbose;
-        $install_options = array();
-        if ($command == 'upgrade') {
-            $install_options['upgrade'] = true;
-        }
-        foreach ($cmdopts as $opt) {
-            switch ($opt[0]) {
-                case 'r':
-                    // This option is for use by rpm and other package
-                    // tools that can install files etc. by itself, but
-                    // still needs to register the package as installed in
-                    // PEAR's local registry.
-                    $install_options['register_only'] = true;
-                    break;
-                case 'f':
-                    $install_options['force'] = true;
-                    break;
-            }
-        }
-        if ($installer->install($pkgfile, $install_options)) {
-            print "install ok\n";
-        } else {
-            print "install failed\n";
-        }
-        break;
-    }
-
-    // }}}
-    // {{{ uninstall
-    case 'uninstall': {
-        include_once 'PEAR/Installer.php';
-        $pkgfile = $cmdargs[0];
-        $installer =& new PEAR_Installer($script_dir, $ext_dir, $doc_dir);
-        $installer->setErrorHandling(PEAR_ERROR_DIE,
-                                     basename($pkgfile) . ": %s\n");
-        $installer->debug = $verbose;
-        $uninstall_options = array();
-        foreach ($cmdopts as $opt) {
-            switch ($opt[0]) {
-                case 'r':
-                    $uninstall_options['register_only'] = true;
-                    break;
-                case 'f':
-                    $uninstall_options['force'] = true;
-                    break;
-            }
-        }
-        if ($installer->uninstall($pkgfile, $uninstall_options)) {
-            print "uninstall ok\n";
-        } else {
-            print "uninstall failed\n";
-        }
-        break;
-    }
-    // }}}
-    // {{{ package
-
-    case 'package': {
-        include_once 'PEAR/Packager.php';
-        $pkginfofile = isset($cmdargs[0]) ? $cmdargs[0] : null;
-        $packager =& new PEAR_Packager($script_dir, $ext_dir, $doc_dir);
-        $packager->setErrorHandling(PEAR_ERROR_DIE, "pear page: %s\n");
-        $packager->debug = $verbose;
-        if (PEAR::isError($packager->Package($pkginfofile))) {
-            print "\npackage failed\n";
-        } else {
-            print "package ok\n";
-        }
-        break;
-    }
-
-    // }}}
-    // {{{ info
-
-    case 'info': {
-        $parser = new PEAR_Common;
-        $parser->setErrorHandling(PEAR_ERROR_DIE, "pear info: %s\n");
-        $info = $parser->infoFromTgzFile($cmdargs[0]);
-        unset($info['filelist']);
-        present_array($info);
-        break;
-    }
-
-    // }}}
-    // {{{ list-installed
-
-    case 'list-installed': {
-        $reg = new PEAR_Registry;
-        $installed = $reg->packageInfo();
-        $i = $j = 0;
-        heading("Installed packages:");
-        foreach ($installed as $package) {
-            if ($i++ % 20 == 0) {
-                if ($j++ > 0) {
-                    print "\n";
-                }
-                printf("%-20s %-10s %s\n",
-                       "Package", "Version", "State");
-                print str_repeat("-", 75)."\n";
-            }
-            printf("%-20s %-10s %s\n", $package['package'],
-                   $package['version'], $package['release_state']);
-        }
-        break;
-    }
-
-    // }}}
-    // {{{ remote-list
-
-    case 'remote-list': {
-        $remote = new PEAR_Remote($config);
-        $result = $remote->call('package.listAll');
-        $i = $j = 0;
-        heading("Available packages");
-        foreach ($result as $package => $info) {
-            if ($i++ % 20 == 0) {
-                if ($j++ > 0) {
-                    print "\n";
-                }
-                printf("%-20s %-10s %-15s %s\n",
-                       "Package", "Stable", "Lead", "Category");
-                print str_repeat("-", 75)."\n";
-            }
-            if (empty($info['stable'])) {
-                $info['stable'] = '(none)';
-            }
-            $stable = (string)$info['stable'];
-            printf("%-20s %-10s %-15s %s\n", $package,
-                   $info['stable'], $info['lead'], $info['category']);
-        }
-        break;
-    }
-
-    // }}}
-    // {{{ show-config
+include "pearcmd-common.php";
 
-    case 'show-config': {
-        $keys = $config->getKeys();
-        foreach ($keys as $key) {
-            $value = $config->get($key);
-            $xi = "";
-            if ($config->isDefaulted($key)) {
-                $xi .= " (default)";
-            }
-            if (isset($fallback_done[$key])) {
-                $xi .= " (built-in)";
-            }
-            printf("%s = %s%s\n", $key, $value, $xi);
-        }
-        break;
-    }
-
-    // }}}
-    // {{{ upload-release
-
-    case 'upload-release': {
-        if (sizeof($cmdargs) < 1) {
-
-        }
-        $tarball = $cmdargs[0];
-
-    }
-
-    // }}}
-    default: {
-        if (!$store_default_config && !$store_user_config) {
-            usage();
-        }
-        break;
-    }
-}
-
-// {{{ usage()
-
-function usage($error = null)
-{
-    $stderr = fopen('php://stderr', 'w');
-    if (PEAR::isError($error)) {
-        fputs($stderr, $error->getMessage());
-    } elseif ($error !== null) {
-        fputs($stderr, $error);
-    }
-    fputs($stderr,
-          "Usage: pear [options] command [command-options] <parameters>\n".
-          "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".
-          "     -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".
-          "Commands:\n".
-          "   help [command]\n".
-          "   install [-r] <package file>\n".
-          "   uninstall [-r] <package name>\n".
-          "   package [package info file]\n".
-          "   list-installed\n".
-          "   remote-list\n".
-          "   info\n".
-          "   show-config\n".
-          "   upload-release\n".
-          "\n");
-    fclose($stderr);
-    exit;
+if (isset($subcommands[$command])) {
+    include "pearcmd-$command.php";
+} elseif (!$store_default_config && !$store_user_config) {
+    usage();
 }
 
-// }}}
-// {{{ present_array()
-
-function present_array(&$arr, $keys = null)
-{
-    if ($keys === null) {
-        $keys = array_keys($arr);
-    }
-    $longest_key = max(array_map("strlen", array_keys($arr))) + 2;
-    $format_string = "%{$longest_key}s : %s\n";
-    foreach ($keys as $k) {
-        if (is_array($arr[$k])) {
-            foreach ($arr[$k] as $i => $value) {
-                $x = "$k #$i";
-                $cont = array();
-                foreach(array_keys($value) as $val) {
-                    $cont[] = "$val=" . $value[$val];
-                }
-                $v = implode(", ", $cont);
-                printf($format_string, $x, $v);
-            }
-            continue;
-        } else {
-            $v = $arr[$k];
-            printf($format_string, $k, $v);
-        }
-    }
-}
-
-// }}}
-// {{{ heading()
-
-function heading($text)
-{
-    $l = strlen(trim($text));
-    print rtrim($text) . "\n" . str_repeat("=", $l) . "\n";
-}
-
-// }}}
-
 /*
  * Local variables:
  * tab-width: 4
diff --git a/pear/scripts/pearcmd-common.php b/pear/scripts/pearcmd-common.php
new file mode 100644 (file)
index 0000000..6293532
--- /dev/null
@@ -0,0 +1,197 @@
+<?php
+
+require_once "PEAR/Config.php";
+require_once "Console/Getopt.php";
+
+$progname = basename($argv[0]);
+
+PEAR::setErrorHandling(PEAR_ERROR_PRINT, "$progname: %s\n");
+
+$argv = Console_Getopt::readPHPArgv();
+if (PEAR::isError($argv)) {
+    die($argv->getMessage());
+}
+$options = Console_Getopt::getopt($argv, "c:C:d:D:h?sSqu:v");
+if (PEAR::isError($options)) {
+    usage($options);
+}
+
+if (OS_WINDOWS) {
+    $pear_default_config = PHP_SYSCONFDIR.DIRECTORY_SEPARATOR.'pearsys.ini';
+    $pear_user_config    = PHP_SYSCONFDIR.DIRECTORY_SEPARATOR.'pear.ini';
+} else {
+    $pear_default_config = PHP_SYSCONFDIR.DIRECTORY_SEPARATOR.'pear.conf';
+    $pear_user_config    = getenv('HOME').DIRECTORY_SEPARATOR.'.pearrc';
+}
+
+$opts = $options[0];
+
+foreach ($opts as $opt) {
+    switch ($opt[0]) {
+        case 'c':
+            $pear_user_config = $opt[1];
+            break;
+        case 'C':
+            $pear_default_config = $opt[1];
+            break;
+    }
+}
+
+$config = new PEAR_Config($pear_user_config, $pear_default_config);
+$store_user_config = false;
+$store_default_config = false;
+$verbose = 1;
+
+foreach ($opts as $opt) {
+    $param = $opt[1];
+    switch ($opt[0]) {
+        case 'd':
+            list($key, $value) = explode('=', $param);
+            $config->set($key, $value);
+            break;
+        case 'D':
+            list($key, $value) = explode('=', $param);
+            $config->set($key, $value, true);
+            break;
+        case 's':
+            $store_user_config = true;
+            break;
+        case 'S':
+            $store_default_config = true;
+            break;
+        case 'u':
+            $config->toDefault($param);
+            break;
+        case 'v':
+            $verbose++;
+            break;
+        case 'q':
+            $verbose--;
+            break;
+    }
+}
+
+if ($store_default_config) {
+    if (@is_writeable($pear_default_config)) {
+        $config->writeConfigFile($pear_default_config, 'default');
+    } else {
+        die("You don't have write access to $pear_default_config, exiting!\n");
+    }
+}
+
+if ($store_user_config) {
+    $config->writeConfigFile($pear_user_config, 'userdefined');
+}
+
+$fallback_config = array(
+    'master_server' => 'pear.php.net',
+    'php_dir'       => PEAR_INSTALL_DIR,
+    'ext_dir'       => PEAR_EXTENSION_DIR,
+    'doc_dir'       => PHP_DATADIR . DIRECTORY_SEPARATOR . 'pear' .
+                       DIRECTORY_SEPARATOR . 'doc',
+    'verbose'       => true,
+);
+$fallback_done = array();
+
+foreach ($fallback_config as $key => $value) {
+    if (!$config->isDefined($key)) {
+        $config->set($key, $value);
+        $fallback_done[$key] = true;
+    }
+}
+
+//$verbose    = $config->get("verbose");
+$script_dir = $config->get("php_dir");
+$ext_dir    = $config->get("ext_dir");
+$doc_dir    = $config->get("doc_dir");
+
+$command = (isset($options[1][1])) ? $options[1][1] : null;
+$rest = array_slice($options[1], 2);
+
+if (isset($command_options[$command])) {
+    $tmp = Console_Getopt::getopt($rest, $command_options[$command]);
+    if (PEAR::isError($tmp)) {
+        usage($tmp);
+    }
+    $cmdopts = $tmp[0];
+    $cmdargs = $tmp[1];
+} else {
+    $cmdopts = array();
+    $cmdargs = $rest;
+}
+
+// {{{ usage()
+
+function usage($error = null, $helpsubject = null)
+{
+    global $progname, $subcommands;
+    $stderr = fopen('php://stderr', 'w');
+    if (PEAR::isError($error)) {
+        fputs($stderr, $error->getMessage());
+    } elseif ($error !== null) {
+        fputs($stderr, $error);
+    }
+    fputs($stderr,
+        "Usage: $progname [options] command [command-options] <parameters>\n");
+    if ($helpsubject == "options") {
+        fputs($stderr,
+        "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".
+        "     -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");
+    } else {
+        fputs($stderr, "Type \"$progname help options\" to list all options.\n");
+    }        
+    fputs($stderr, "Commands:".implode("\n   ", $subcommands) . "\n");
+    fclose($stderr);
+    exit;
+}
+
+// }}}
+// {{{ present_array()
+
+function present_array(&$arr, $keys = null)
+{
+    if ($keys === null) {
+        $keys = array_keys($arr);
+    }
+    $longest_key = max(array_map("strlen", array_keys($arr))) + 2;
+    $format_string = "%{$longest_key}s : %s\n";
+    foreach ($keys as $k) {
+        if (is_array($arr[$k])) {
+            foreach ($arr[$k] as $i => $value) {
+                $x = "$k #$i";
+                $cont = array();
+                foreach(array_keys($value) as $val) {
+                    $cont[] = "$val=" . $value[$val];
+                }
+                $v = implode(", ", $cont);
+                printf($format_string, $x, $v);
+            }
+            continue;
+        } else {
+            $v = $arr[$k];
+            printf($format_string, $k, $v);
+        }
+    }
+}
+
+// }}}
+// {{{ heading()
+
+function heading($text)
+{
+    $l = strlen(trim($text));
+    print rtrim($text) . "\n" . str_repeat("=", $l) . "\n";
+}
+
+// }}}
+
+?>
\ No newline at end of file
diff --git a/pear/scripts/pearcmd-help.php b/pear/scripts/pearcmd-help.php
new file mode 100644 (file)
index 0000000..5bb8b7d
--- /dev/null
@@ -0,0 +1,9 @@
+<?php
+
+if (isset($cmdargs[0])) {
+    usage(null, $cmdargs[0]);
+} else {
+    usage();
+}
+
+?>
\ No newline at end of file
diff --git a/pear/scripts/pearcmd-info.php b/pear/scripts/pearcmd-info.php
new file mode 100644 (file)
index 0000000..baa6b88
--- /dev/null
@@ -0,0 +1,9 @@
+<?php
+
+$parser = new PEAR_Common;
+$parser->setErrorHandling(PEAR_ERROR_DIE, "pear info: %s\n");
+$info = $parser->infoFromTgzFile($cmdargs[0]);
+unset($info['filelist']);
+present_array($info);
+
+?>
\ No newline at end of file
diff --git a/pear/scripts/pearcmd-install.php b/pear/scripts/pearcmd-install.php
new file mode 100644 (file)
index 0000000..4a4a349
--- /dev/null
@@ -0,0 +1,33 @@
+<?php
+
+include_once 'PEAR/Installer.php';
+$pkgfile = $cmdargs[0];
+$installer =& new PEAR_Installer($script_dir, $ext_dir, $doc_dir);
+$installer->setErrorHandling(PEAR_ERROR_DIE,
+                             basename($pkgfile) . ": %s\n");
+$installer->debug = $verbose;
+$install_options = array();
+if ($command == 'upgrade') {
+    $install_options['upgrade'] = true;
+}
+foreach ($cmdopts as $opt) {
+    switch ($opt[0]) {
+        case 'r':
+            // This option is for use by rpm and other package
+            // tools that can install files etc. by itself, but
+            // still needs to register the package as installed in
+            // PEAR's local registry.
+            $install_options['register_only'] = true;
+            break;
+        case 'f':
+            $install_options['force'] = true;
+            break;
+    }
+}
+if ($installer->install($pkgfile, $install_options, $config)) {
+    print "install ok\n";
+} else {
+    print "install failed\n";
+}
+
+?>
\ No newline at end of file
diff --git a/pear/scripts/pearcmd-list.php b/pear/scripts/pearcmd-list.php
new file mode 100644 (file)
index 0000000..030c0b2
--- /dev/null
@@ -0,0 +1,20 @@
+<?php
+
+$reg = new PEAR_Registry;
+$installed = $reg->packageInfo();
+$i = $j = 0;
+heading("Installed packages:");
+foreach ($installed as $package) {
+    if ($i++ % 20 == 0) {
+        if ($j++ > 0) {
+            print "\n";
+        }
+        printf("%-20s %-10s %s\n",
+               "Package", "Version", "State");
+        print str_repeat("-", 75)."\n";
+    }
+    printf("%-20s %-10s %s\n", $package['package'],
+           $package['version'], $package['release_state']);
+}
+
+?>
diff --git a/pear/scripts/pearcmd-package.php b/pear/scripts/pearcmd-package.php
new file mode 100644 (file)
index 0000000..5cc485a
--- /dev/null
@@ -0,0 +1,15 @@
+<?php
+
+include_once 'PEAR/Packager.php';
+$pkginfofile = isset($cmdargs[0]) ? $cmdargs[0] : null;
+$packager =& new PEAR_Packager($script_dir, $ext_dir, $doc_dir);
+$packager->setErrorHandling(PEAR_ERROR_DIE, "pear page: %s\n");
+$packager->debug = $verbose;
+if (PEAR::isError($packager->Package($pkginfofile))) {
+    print "\npackage failed\n";
+} else {
+    print "package ok\n";
+}
+
+
+?>
\ No newline at end of file
diff --git a/pear/scripts/pearcmd-remote-list.php b/pear/scripts/pearcmd-remote-list.php
new file mode 100644 (file)
index 0000000..d30a467
--- /dev/null
@@ -0,0 +1,24 @@
+<?php
+
+$remote = new PEAR_Remote($config);
+$result = $remote->call('package.listAll', 1);
+$i = $j = 0;
+heading("Available packages");
+foreach ($result as $package => $info) {
+    if ($i++ % 20 == 0) {
+        if ($j++ > 0) {
+            print "\n";
+        }
+        printf("%-20s %-10s %-15s %s\n",
+               "Package", "Stable", "Lead", "Category");
+        print str_repeat("-", 75)."\n";
+    }
+    if (empty($info['stable'])) {
+        $info['stable'] = '(none)';
+    }
+    $stable = (string)$info['stable'];
+    printf("%-20s %-10s %-15s %s\n", $info['name'],
+           $info['stable'], $info['lead'], $info['category']);
+}
+
+?>
\ No newline at end of file
diff --git a/pear/scripts/pearcmd-show-config.php b/pear/scripts/pearcmd-show-config.php
new file mode 100644 (file)
index 0000000..0ebc70a
--- /dev/null
@@ -0,0 +1,16 @@
+<?php
+
+$keys = $config->getKeys();
+foreach ($keys as $key) {
+    $value = $config->get($key);
+    $xi = "";
+    if ($config->isDefaulted($key)) {
+        $xi .= " (default)";
+    }
+    if (isset($fallback_done[$key])) {
+        $xi .= " (built-in)";
+    }
+    printf("%s = %s%s\n", $key, $value, $xi);
+}
+
+?>
\ No newline at end of file
diff --git a/pear/scripts/pearcmd-uninstall.php b/pear/scripts/pearcmd-uninstall.php
new file mode 100644 (file)
index 0000000..18f4e41
--- /dev/null
@@ -0,0 +1,26 @@
+<?php
+
+include_once 'PEAR/Installer.php';
+$pkgfile = $cmdargs[0];
+$installer =& new PEAR_Installer($script_dir, $ext_dir, $doc_dir);
+$installer->setErrorHandling(PEAR_ERROR_DIE,
+                             basename($pkgfile) . ": %s\n");
+$installer->debug = $verbose;
+$uninstall_options = array();
+foreach ($cmdopts as $opt) {
+    switch ($opt[0]) {
+        case 'r':
+            $uninstall_options['register_only'] = true;
+            break;
+        case 'f':
+            $uninstall_options['force'] = true;
+            break;
+    }
+}
+if ($installer->uninstall($pkgfile, $uninstall_options)) {
+    print "uninstall ok\n";
+} else {
+    print "uninstall failed\n";
+}
+
+?>