home *** CD-ROM | disk | FTP | other *** search
/ PC World 2003 March / PCWorld_2003-03_cd.bin / Software / Topware / activeperl / ActivePerl / Perl / bin / ppm3-bin.bat < prev    next >
Encoding:
DOS Batch File  |  2002-12-01  |  141.9 KB  |  4,844 lines

  1. @rem = '--*-Perl-*--
  2. @echo off
  3. if "%OS%" == "Windows_NT" goto WinNT
  4. perl -x -S "%0" %1 %2 %3 %4 %5 %6 %7 %8 %9
  5. goto endofperl
  6. :WinNT
  7. perl -x -S %0 %*
  8. if NOT "%COMSPEC%" == "%SystemRoot%\system32\cmd.exe" goto endofperl
  9. if %errorlevel% == 9009 echo You do not have Perl in your PATH.
  10. if errorlevel 1 goto script_failed_so_exit_with_non_zero_val 2>nul
  11. goto endofperl
  12. @rem ';
  13. #!perl 
  14. #line 15
  15.  
  16. require 5.006;    # require 5.6.0
  17. use strict;
  18.  
  19. # A command-line shell implementation. The code which invokes it is at the
  20. # bottom of this file.
  21. package PPMShell;
  22. use base qw(PPM::Term::Shell);
  23.  
  24. use Data::Dumper;
  25. use Text::Autoformat qw(autoformat form);
  26. use Getopt::Long;
  27.  
  28. # These must come _after_ the options parsing.
  29. require PPM::UI;
  30. require PPM::Trace;
  31. PPM::Trace->import(qw(trace));
  32.  
  33. my $NAME    = q{PPM - Programmer's Package Manager};
  34. my $SHORT_NAME    = q{PPM};
  35. my $VERSION    = '3.0.1';
  36.  
  37. sub dictsort(@);
  38.  
  39. #=============================================================================
  40. # Output Methods
  41. #
  42. # PPM behaves differently under different calling circumstances. Here are the
  43. # various classes of messages it prints out:
  44. # 1. error/warning    - an error or "bad thing" has occurred
  45. # 2. informational    - required information like search results
  46. # 3. verbose        - verbose that's only needed in interactive mode
  47. #
  48. # Here are the cases:
  49. # 1. PPM is in interactive mode: everything gets printed.
  50. # 2. PPM is in batch mode: everything minus 'verbose' gets printed.
  51. #=============================================================================
  52. sub error {
  53.     my $o = shift;
  54.     return 1 unless $o->{SHELL}{output}{error};
  55.     CORE::print STDERR @_;
  56. }
  57. sub errorf {
  58.     my $o = shift;
  59.     return 1 unless $o->{SHELL}{output}{error};
  60.     CORE::printf STDERR @_;
  61. }
  62. sub warn { goto &error }
  63. sub warnf { goto &errorf }
  64. sub inform {
  65.     my $o = shift;
  66.     return 1 unless $o->{SHELL}{output}{inform};
  67.     CORE::print @_;
  68. }
  69. sub informf {
  70.     my $o = shift;
  71.     return 1 unless $o->{SHELL}{output}{inform};
  72.     CORE::printf @_;
  73. }
  74. sub verbose {
  75.     my $o = shift;
  76.     return 1 unless $o->{SHELL}{output}{verbose};
  77.     CORE::print @_;
  78. }
  79. sub verbosef {
  80.     my $o = shift;
  81.     return 1 unless $o->{SHELL}{output}{verbose};
  82.     CORE::printf @_;
  83. }
  84. sub assertw {
  85.     my $o = shift;
  86.     my $cond = shift;
  87.     my $msg = shift;
  88.     $o->warn("Warning: $msg\n") unless $cond;
  89.     return $cond;
  90. }
  91. sub assert {
  92.     my $o = shift;
  93.     my $cond = shift;
  94.     my $msg = shift;
  95.     $o->error("Error: $msg\n") unless $cond;
  96.     return $cond;
  97. }
  98.  
  99. sub mode {
  100.     my $o = shift;
  101.     $o->{SHELL}{mode};
  102. }
  103. sub setmode {
  104.     my $o = shift;
  105.     my $newmode = shift || '';
  106.     my $oldmode = $o->{SHELL}{mode};
  107.     if ($newmode eq 'SHELL') {
  108.     $o->{SHELL}{output}{error}   = 1;
  109.     $o->{SHELL}{output}{inform}  = 1;
  110.     $o->{SHELL}{output}{verbose} = 1;
  111.     }
  112.     elsif ($newmode eq 'BATCH') {
  113.     $o->{SHELL}{output}{error}   = 1;
  114.     $o->{SHELL}{output}{inform}  = 1;
  115.     $o->{SHELL}{output}{verbose} = 0;
  116.     }
  117.     elsif ($newmode eq 'SCRIPT') {
  118.     $o->{SHELL}{output}{error}   = 1;
  119.     $o->{SHELL}{output}{inform}  = 1;
  120.     $o->{SHELL}{output}{verbose} = 0;
  121.     }
  122.     elsif ($newmode eq 'SILENT') {
  123.     $o->{SHELL}{output}{error}   = 1;
  124.     $o->{SHELL}{output}{inform}  = 0;
  125.     $o->{SHELL}{output}{verbose} = 0;
  126.     }
  127.     $o->{SHELL}{mode} = $newmode;
  128.     return $oldmode;
  129. }
  130.  
  131. # Older versions of PPM3 had one "Active" repository. This code reads
  132. # $o->conf('repository') if it exists, and moves it into
  133. # $o->conf('active_reps'), which is a list. The old one is deleted -- old PPMs
  134. # will reset it if needed, but it will be ignored if 'active_reps' exists.
  135. sub init_active_reps {
  136.     my $o = shift;
  137.  
  138.     if ($o->conf('repository') and not $o->conf('active_reps')) {
  139.     my @active = $o->conf('repository');
  140.     delete $o->{SHELL}{conf}{DATA}{repository};
  141.     $o->conf('active_reps', \@active);
  142.     }
  143.     elsif (not defined $o->conf('active_reps')) {
  144.     my @active = $o->reps_all; # enable all repositories
  145.     $o->conf('active_reps', \@active);
  146.     }
  147. }
  148.  
  149. sub init {
  150.     my $o = shift;
  151.     $o->cache_clear('query');
  152.     $o->cache_clear('search');
  153.     $o->{API}{case_ignore} = 1;
  154.  
  155.     # Load the configuration;
  156.     $o->{SHELL}{conf} = PPM::Config::load_config_file('cmdline');
  157.     $o->init_active_reps;
  158.  
  159.     # check whether there's a target in the parent's perl that hasn't been
  160.     # installed in the "targets" file:
  161.     my $ppmsitelib = $ENV{PPM3_PERL_SITELIB};
  162.     if ($ppmsitelib and opendir(PPMDIR, "$ppmsitelib/ppm-conf")) {
  163.         my @files = map  { "$ppmsitelib/ppm-conf/$_" }
  164.                 grep { /^ppminst/i && !/(~|\.bak)\z/ } readdir PPMDIR;
  165.     closedir PPMDIR;
  166.     my $found = 0;
  167.     if (@files == 1) {
  168.         my @targets = PPM::UI::target_list()->result_l;
  169.         for my $target (@targets) {
  170.         my $info = PPM::UI::target_raw_info($target);
  171.         next unless $info and $info->is_success;
  172.         ++$found and last
  173.             if path_under($info->result->{path}, $files[0]);
  174.         }
  175.         unless ($found) {
  176.         # We're going to add a new target:
  177.         # 1. if we can find ppm3-bin.cfg, use that
  178.         # 2. if not, guess lots of stuff
  179.         my $ppm3_bin_cfg = "$ENV{PPM3_PERL_PREFIX}/bin/ppm3-bin.cfg";
  180.         my $r = PPM::UI::target_add(undef, From => $ppm3_bin_cfg)
  181.             if -f $ppm3_bin_cfg;
  182.         unless ($r and $r->is_success) {
  183.             PPM::UI::target_add(
  184.             'TEMP',
  185.             type => 'Local',
  186.             path => $files[0],
  187.             );
  188.         }
  189.         }
  190.     }
  191.     }
  192.  
  193.     # set the initial target:
  194.     if (defined $o->{API}{args}{target}) {
  195.     my $t = $o->{API}{args}{target};
  196.     my $prefix = $ENV{PPM3_PERL_PREFIX};
  197.     if ($t ne 'auto') {
  198.         # A full name or number given:
  199.         $o->run('target', 'select', $o->{API}{args}{target});
  200.     }
  201.     elsif ($prefix) {
  202.         # Auto-select target, based on where we came from:
  203.         my @l = $o->conf('target');
  204.         push @l, PPM::UI::target_list()->result_l;
  205.         for my $target (@l) {
  206.         next unless $target;
  207.         my $info = PPM::UI::target_raw_info($target);
  208.         next unless $info and $info->is_success;
  209.         next unless path_under($info->result->{path}, "$prefix/");
  210.         my $mode = $o->setmode('SILENT');
  211.         $o->run('target', 'select', $target);
  212.         $o->setmode($mode);
  213.         last;
  214.         }
  215.     }
  216.     }
  217. }
  218.  
  219. sub preloop {
  220.     my $o = shift;
  221.  
  222.     if ($o->conf('verbose-startup') and $o->mode eq 'SHELL') {
  223.     my $profile_track = $o->conf('profile-track');
  224.     chomp (my $startup = <<END);
  225. $NAME version $VERSION.
  226. Copyright (c) 2001 ActiveState SRL. All Rights Reserved.
  227.  
  228. Entering interactive shell.
  229. END
  230.     my $profile_tracking_warning = $profile_track ? '' : <<'END';
  231.  
  232. Profile tracking is not enabled. If you save and restore profiles manually,
  233. your profile may be out of sync with your computer. See 'help profile' for
  234. more information.
  235. END
  236.     $o->inform($startup);
  237.     $o->inform(<<END);
  238.  Using $o->{API}{readline} as readline library.
  239. $profile_tracking_warning
  240. Type 'help' to get started.
  241.  
  242. END
  243.     }
  244.     else {
  245.     $o->inform("$NAME ($VERSION). Type 'help' to get started.\n");
  246.     }
  247.  
  248.     $o->term->SetHistory(@{$o->conf('history') || []})
  249.     if $o->term->Features->{setHistory};
  250. }
  251.  
  252. sub postloop {
  253.     my $o = shift;
  254.     trace(1, "PPM: exiting...\n");
  255.     if ($o->mode eq 'SHELL' and $o->term->Features->{getHistory}) {
  256.     my @h = $o->term->GetHistory;
  257.     my $max_history = $o->conf('max_history') || 100;
  258.     splice @h, 0, (@h - $max_history)
  259.         if @h > $max_history;
  260.     my $old = $o->setmode('SILENT');
  261.     $o->conf('history', \@h);
  262.     $o->setmode($old);
  263.     }
  264. }
  265.  
  266. #============================================================================
  267. # Cache of search and query results
  268. #============================================================================
  269. sub cache_set_current {
  270.     my $o = shift;
  271.     my $type = shift;
  272.     my $set = shift;
  273.     $set = $o->{SHELL}{CACHE}{$type}{current} unless defined $set;
  274.     $o->{SHELL}{CACHE}{$type}{current} = $set;
  275.     return $o->{SHELL}{CACHE}{$type}{current};
  276. }
  277.  
  278. sub cache_set_index {
  279.     my $o = shift;
  280.     my $type = shift;
  281.     my $index = shift;
  282.     $index = $o->{SHELL}{CACHE}{$type}{index} unless defined $index;
  283.     $o->{SHELL}{CACHE}{$type}{index} = $index;
  284.     return $o->{SHELL}{CACHE}{$type}{index};
  285. }
  286.  
  287. sub cache_set_add {
  288.     my $o = shift;
  289.     my $type = shift;
  290.     my $query = shift;
  291.     my $entries = shift;
  292.     my $sort_field = $o->conf('sort-field');
  293.     my @sorted = $o->sort_pkgs($sort_field, @$entries);
  294.     my $set = {
  295.           query => $query,
  296.           raw => $entries,
  297.           $sort_field => \@sorted,
  298.         };
  299.     push @{$o->{SHELL}{CACHE}{$type}{sets}}, $set;
  300. }
  301.  
  302. sub cache_entry {
  303.     my $o = shift;
  304.     my $type = shift;        # 'query' or 'cache';
  305.     my $index = shift;        # defaults to currently selected index
  306.     my $set = shift;        # defaults to currently selected set
  307.  
  308.     $index = $o->{SHELL}{CACHE}{$type}{index} unless defined $index;
  309.  
  310.     my $src = $o->cache_set($type, $set);
  311.     return undef unless $src and bounded(0, $index, $#$src);
  312.  
  313.     # Make sure we display only valid entries:
  314.     my $tar = $o->conf('target');
  315.     $src->[$index]->make_complete($tar);
  316.     return $src->[$index];
  317. }
  318.  
  319. sub cache_set {
  320.     my $o = shift;
  321.     my $type = shift;        # 'query' or 'cache'
  322.     my $set = shift;        # defaults to currently selected set
  323.     my $entry = shift;        # defaults to 'results';
  324.  
  325.     $entry = $o->conf('sort-field') unless defined $entry;
  326.     return undef unless grep { lc($entry) eq $_ } (sort_fields(), 'query');
  327.  
  328.     $set = $o->{SHELL}{CACHE}{$type}{current} unless defined $set;
  329.     my $src = $o->{SHELL}{CACHE}{$type}{sets};
  330.  
  331.     return undef unless defined $set;
  332.     return undef unless bounded(0, $set, $#$src);
  333.  
  334.     # We've changed sort-field at some point -- make sure the sorted data
  335.     # exists, or else build it:
  336.     unless (defined $src->[$set]{$entry}) {
  337.     my $raw = $src->[$set]{raw};
  338.     my @sorted = $o->sort_pkgs($entry, @$raw);
  339.     $src->[$set]{$entry} = \@sorted;
  340.     }
  341.     
  342.     return wantarray ? @{$src->[$set]{$entry}} : $src->[$set]{$entry};
  343. }
  344.  
  345. sub cache_clear {
  346.     my $o = shift;
  347.     my $type = shift;        # 'query' or 'cache'
  348.     $o->{SHELL}{CACHE}{$type}{sets} = [];
  349.     $o->{SHELL}{CACHE}{$type}{current} = -1;
  350.     $o->{SHELL}{CACHE}{$type}{index} = -1;
  351. }
  352.  
  353. sub cache_sets {
  354.     my $o = shift;
  355.     my $type = shift;
  356.     @{$o->{SHELL}{CACHE}{$type}{sets}};
  357. }
  358.  
  359. # This sub searches for an entry in the cache whose name matches that thing
  360. # passed in. It searches in the current cache first. If the name isn't found,
  361. # it searches in all caches. If the name still isn't found, it returns undef.
  362. sub cache_find {
  363.     my $o = shift;
  364.     my $type = shift;
  365.     my $name = shift;
  366.  
  367.     my $ncaches = $o->cache_sets($type);
  368.     my $current = $o->cache_set_current($type);
  369.  
  370.     # First, search the current set:
  371.     my @pkgs = map { $_ ? $_->name : '' } $o->cache_set($type);
  372.     my $ind  = find_index($name, 0, @pkgs);
  373.     return ($current, $ind) if $ind >= 0;
  374.  
  375.     # Now try to find in all the sets:
  376.     for my $s (0 .. $ncaches - 1) {
  377.     next if $s == $current;
  378.     @pkgs = map { $_ ? $_->name : '' } $o->cache_set($type, $s);
  379.     $ind  = find_index($name, 0, @pkgs);
  380.     return ($s, $ind) if $ind >= 0;
  381.     }
  382.     return (-1, -1);
  383. }
  384.  
  385. # A pretty separator to print between logically separate items:
  386. my $SEP;
  387. BEGIN {
  388.     $SEP = '=' x 20;
  389. }
  390.  
  391. # Useful functions:
  392. sub max (&@) {
  393.     my $code = shift;
  394.     my $max;
  395.     local $_;
  396.     for (@_) {
  397.     my $res = $code->($_);
  398.     $max = $res if not defined $max or $max < $res;
  399.     }
  400.     $max || 0;
  401. }
  402.  
  403. sub min (&@) {
  404.     my $code = shift;
  405.     my $min;
  406.     local $_;
  407.     for (@_) {
  408.     my $res = $code->($_);
  409.     $min = $res if not defined $min or $min > $res;
  410.     }
  411.     $min || 0;
  412. }
  413.  
  414. sub sum (&@) {
  415.     my $code = shift;
  416.     my $sum = 0;
  417.     local $_;
  418.     for (@_) {
  419.     my $res = $code->($_);
  420.     $sum += $res if defined $res;
  421.     }
  422.     $sum || 0;
  423. }
  424.  
  425. #============================================================================
  426. # Repository:
  427. # rep            # displays repositories
  428. # rep add http://...    # adds a new repository
  429. # rep del <\d+>        # deletes the specified repository
  430. # rep [set] 1        # sets the specified repository active
  431. #============================================================================
  432. sub smry_repository { "adds, removes, or sets repositories" }
  433. sub help_repository { <<'END' }
  434. repository -- Repository Control
  435.   Synopsis
  436.      rep                        Displays all repositories
  437.      rep add [name] <location>  Adds a new repository; makes it active
  438.      rep delete <name or num>   Deletes specified repository
  439.      rep describe <name or num> Displays information about the specified
  440.                                 repository
  441.      rep rename <name or num> <name>
  442.                                 Renames the specified repository to
  443.                                 the given name
  444.      rep on <name>              Activates the specified repository
  445.      rep off <name or num>      Removes the repository from the active list
  446.      rep up <name or num>       Moves the specified repository up one
  447.      rep down <name or num>     Moves the specified repository down one
  448.  
  449.   Description
  450.     The *repository* (or *rep*) command controls two lists or repositories:
  451.  
  452.     1   The list of "active" repositories. This is the list of repositories
  453.         used by *search*, *install*, *upgrade*, and *verify*.
  454.  
  455.     2   The list of all known repositories. You can designate a repository
  456.         "inactive", which means PPM will not use it in any commands.
  457.  
  458.     If no arguments are given, the rep command will list the active
  459.     repositories defined in the PPM settings. The order is significant: when
  460.     installing a package, PPM will try the first repository, then the
  461.     second, and so on, until it find the package you asked for. When
  462.     searching, PPM merges the results of all the repositories together, so
  463.     the order is less important (see the *search* command).
  464.  
  465.     For example, when you enter:
  466.  
  467.         rep
  468.  
  469.     PPM3 will return something resembling this:
  470.  
  471.         Repositories:
  472.         [1] ActiveCD
  473.         [2] ActiveState Package Repository
  474.         [ ] An inactive repository
  475.  
  476.     In the example above, entering 'rep off 2' will disable the second
  477.     repository (the ActiveStat Package Repository). To add another
  478.     repository:
  479.  
  480.         rep add [options] <NAME> <LOCATION>
  481.  
  482.     The following options are available for the 'add' command:
  483.  
  484.     *   -username
  485.  
  486.     *   -password
  487.  
  488.     These options allow you to specify a username and password to be used
  489.     when logging into a repository. Currently, these are only used for FTP
  490.     and WWW repositories.
  491.  
  492.     For example:
  493.  
  494.         rep add "EZE" http://ppm.ActiveState.com/PPMPackages/5.8plus
  495.  
  496.     with "EZE" being the name of the repository (for easy reference) and the
  497.     location noted by the http location. If you were to enter the rep
  498.     command again, you would see:
  499.  
  500.         ppm> rep
  501.         Repositories:
  502.         [1] ActiveCD
  503.         [2] ActiveState Package Repository
  504.         [3] EZE
  505.         [ ] An inactive repository
  506.  
  507.     To move the new repository to the top of the Active list, you would
  508.     type:
  509.  
  510.         ppm> rep up EZE
  511.         Repositories:
  512.         [1] ActiveCD
  513.         [2] EZE
  514.         [3] ActiveState Package Repository
  515.         [ ] An inactive repository
  516.         ppm> rep up EZE
  517.         Repositories:
  518.         [1] EZE
  519.         [2] ActiveCD
  520.         [3] ActiveState Package Repository
  521.         [ ] An inactive repository
  522.  
  523.     To disable the ActiveCD repository temporarily, enter the following:
  524.  
  525.         ppm> rep off ActiveCD
  526.         Repositories:
  527.         [1] EZE
  528.         [2] ActiveState Package Repository
  529.         [ ] ActiveCD
  530.         [ ] An inactive repository
  531.  
  532.     To describe a repository, refer to it either by name, or by the number
  533.     displayed next to the repository in the Active Repositories list. You
  534.     must refer to inactive repositories by their full name.
  535.  
  536.         ppm> rep describe 2
  537.         Describing Active Repository 2:
  538.             Name: ActiveState Package Repository
  539.         Location: http://ppm.ActiveState.com/cgibin/PPM/...
  540.             Type: PPMServer 2.00
  541.         ppm> rep describe ActiveCD
  542.         Describing Inactive Repository:
  543.             Name: ActiveCD
  544.         Location: F:\PPMPackages\5.8plus
  545.             Type: Local Directory
  546.  
  547.     To re-activate the ActiveCD repository, use the *rep on* command. You
  548.     must refer to inactive repositories by name, not number.
  549.  
  550.         ppm> rep on ActiveCD
  551.         Active Repositories:
  552.         [1] EZE
  553.         [2] ActiveState Package Repository
  554.         [3] ActiveCD
  555.         [ ] An inactive repository
  556.  
  557.   Repository Types
  558.     PPM3 supports several types of package repositories:
  559.  
  560.     1.  PPM Server 3.0
  561.  
  562.         ActiveState's SOAP-driven package server. Because all searches are
  563.         done server-side, the server can deliver much richer information
  564.         about packages than other repositories.
  565.  
  566.     2.  PPM Server 2.0
  567.  
  568.         The SOAP server designed for PPM version 2. PPM3 ships with the PPM2
  569.         repository as well as the PPM3 repository, so you can use either.
  570.         Simple searches are performed server-side. If your search is too
  571.         complicated for the server, PPM3 will download the package summary
  572.         and search by itself.
  573.  
  574.     3.  Web Repositories
  575.  
  576.         Older versions of PPM used non-SOAP repositories (directories full
  577.         of PPD files accessible using a web browser). Over the history of
  578.         PPM, there have been several different ways of organising the files
  579.         so that PPM can search for packages properly. PPM3 tries to download
  580.         a summary file first -- if that fails, it gets the directory index.
  581.         It parses the summary or the index, and caches it. Searches are done
  582.         from the cache.
  583.  
  584.     4.  FTP Repositories
  585.  
  586.         FTP is another way of exposing a directory full of PPD files. PPM3
  587.         consideres FTP repositories a subset of Web repositories. Treat them
  588.         as identical: PPM3 downloads the summary or the "index" (file
  589.         listing in this case), parses it, and then searches from it.
  590.  
  591.     5.  Local Repositories
  592.  
  593.         To support installing packages from the ActiveCD, a local directory
  594.         can be a repository. PPM3 searches the files in the directory. All
  595.         valid path formats are supported, including UNC paths.
  596. END
  597. sub comp_repository {
  598.     my $o = shift;
  599.     my ($word, $line, $start) = @_;
  600.     my @words = $o->line_parsed($line);
  601.     my $words = scalar @words;
  602.     my @reps = PPM::UI::repository_list()->result_l;
  603.     my $reps = @reps;
  604.     my @compls = qw(add delete describe rename set select);
  605.     push @compls, ($reps ? (1 .. $reps) : ()); 
  606.  
  607.     if ($words == 1 or $words == 2 and $start != length($line)) {
  608.     return $o->completions($word, \@compls);
  609.     }
  610.     if ($words == 2 or $words == 3 and $start != length($line)) {
  611.     return (readline::rl_filename_list($word))
  612.       if $words[1] eq 'add';
  613.     return $o->completions($word, [1 .. $reps])
  614.       if $o->completions($words[1], [qw(delete describe rename set select)]) == 1;
  615.     }
  616.     ();
  617. }
  618. sub reps_all {
  619.     my $o = shift;
  620.     my $l = PPM::UI::repository_list();
  621.     unless ($l->is_success) {
  622.     $o->warn($l->msg);
  623.     return () unless $l->ok;
  624.     }
  625.     $l->result_l;
  626. }
  627. sub reps_on {
  628.     my $o = shift;
  629.     return @{$o->conf('active_reps')};
  630. }
  631. sub reps_off {
  632.     my $o = shift;
  633.     my @reps = $o->reps_all;
  634.     my @reps_on = $o->reps_on;
  635.     my @off;
  636.     for my $r (@reps) {
  637.     push @off, $r unless grep { $_ eq $r } @reps_on;
  638.     }
  639.     @off;
  640. }
  641. sub rep_on {
  642.     my $o = shift;
  643.     my $rep = shift;
  644.     my @reps = ($o->reps_on, $rep);
  645.     my $m = $o->setmode('SILENT');
  646.     $o->conf('active_reps', \@reps);
  647.     $o->setmode($m);
  648. }
  649. sub rep_off {
  650.     my $o = shift;
  651.     my $rep = shift;
  652.     my @reps = grep { $_ ne $rep } $o->reps_on;
  653.     my $m = $o->setmode('SILENT');
  654.     $o->conf('active_reps', \@reps);
  655.     $o->setmode($m);
  656. }
  657. sub rep_ison {
  658.     my $o = shift;
  659.     my $rep = shift;
  660.     scalar grep { $_ eq $rep } $o->reps_on;
  661. }
  662. sub rep_isoff {
  663.     my $o = shift;
  664.     my $rep = shift;
  665.     scalar grep { $_ eq $rep } $o->reps_off;
  666. }
  667. sub rep_exists {
  668.     my $o = shift;
  669.     my $rep = shift;
  670.     scalar grep { $_ eq $rep } $o->reps_all;
  671. }
  672. sub rep_uniq {
  673.     my $o = shift;
  674.     my $rep = shift;
  675.     unless ($o->rep_exists($rep) or $rep =~ /^\d+$/) {
  676.     /\Q$rep\E/i and return $_ for $o->reps_all;
  677.     }
  678.     $rep;
  679. }
  680. sub rep_up {
  681.     my $o = shift;
  682.     my $rep = shift;
  683.     my @reps = $o->reps_on;
  684.     my $ind = find_index($rep, 0, @reps);
  685.     if (bounded(1, $ind, $#reps)) {
  686.     @reps = (
  687.         @reps[0 .. $ind - 2],
  688.         $rep,
  689.         $reps[$ind - 1],
  690.         @reps[$ind + 1 .. $#reps]
  691.     );
  692.     }
  693.     my $m = $o->setmode('SILENT');
  694.     $o->conf('active_reps', \@reps);
  695.     $o->setmode($m);
  696. }
  697. sub rep_down {
  698.     my $o = shift;
  699.     my $rep = shift;
  700.     my @reps = $o->reps_on;
  701.     my $ind = find_index($rep, 0, @reps);
  702.     if (bounded(0, $ind, $#reps - 1)) {
  703.     @reps = (
  704.         @reps[0 .. $ind - 1],
  705.         $reps[$ind + 1],
  706.         $rep,
  707.         @reps[$ind + 2 .. $#reps]
  708.     );
  709.     }
  710.     my $m = $o->setmode('SILENT');
  711.     $o->conf('active_reps', \@reps);
  712.     $o->setmode($m);
  713. }
  714. sub run_repository {
  715.     my $o = shift;
  716.     my @args = @_;
  717.     my (@reps, @reps_off, @reps_on);
  718.     my $refresh = sub {
  719.     @reps = $o->reps_all;
  720.     @reps_off = $o->reps_off;
  721.     @reps_on = $o->reps_on;
  722.     };
  723.     &$refresh;
  724.     trace(1, "PPM: repository @args\n");
  725.  
  726.     if (@args) {
  727.     my ($cmd, @args) = @args;
  728.     #=====================================================================
  729.     # add, delete, describe, rename commands:
  730.     #=====================================================================
  731.     if (matches($cmd, "add")) {
  732.         # Support for usernames and passwords.
  733.         my ($user, $pass);
  734.         {
  735.         local *ARGV;
  736.         @ARGV = @args;
  737.         GetOptions(
  738.             "username=s"    => \$user,
  739.             "password=s"    => \$pass,
  740.         );
  741.         @args = @ARGV;
  742.         }
  743.         $o->warn(<<END) and return unless (@args == 1 or @args == 2);
  744. repository: invalid 'add' command arguments. See 'help repository'.
  745. END
  746.         my $name = $args[0];
  747.         my $url  = $args[1];
  748.         if (@args == 1) {    # rep add http://...
  749.         $url = $name;
  750.         $name = 'Autonamed';
  751.         for (my $i=1; $i<=@reps; $i++) {
  752.             my $tmp = "$name $i";
  753.             $name = $tmp and last
  754.               unless (grep { $tmp eq $_ } @reps);
  755.         }
  756.         }
  757.         my $ok = PPM::UI::repository_add($name, $url, $user, $pass);
  758.         unless ($ok->is_success) {
  759.         $o->warn($ok->msg);
  760.         return unless $ok->ok;
  761.         }
  762.         $o->rep_on($name);
  763.     }
  764.     elsif (matches($cmd, "del|ete")) {
  765.         my $arg = $args[0];
  766.         my $gonner = $arg;
  767.         if ($arg =~ /^\d+$/) {
  768.         return unless $o->assert(
  769.             bounded(1, $arg, scalar @reps_on),
  770.             "no such active repository $arg"
  771.         );
  772.         $gonner = $reps_on[$arg - 1];
  773.         }
  774.         else {
  775.         $gonner = $o->rep_uniq($gonner);
  776.         return unless $o->assert(
  777.             $o->rep_exists($gonner),
  778.             "no such repository '$gonner'"
  779.         );
  780.         }
  781.         my $ok = PPM::UI::repository_del($gonner);
  782.         unless ($ok->is_success) {
  783.         $o->warn($ok->msg);
  784.         return unless $ok->ok;
  785.         }
  786.         $o->rep_off($gonner);
  787.     }
  788.     elsif (matches($cmd, "des|cribe")) {
  789.         my $arg = $args[0] || '1';
  790.         my $rep = $arg;
  791.         if ($arg =~ /^\d+$/) {
  792.         return unless $o->assert(
  793.             bounded(1, $arg, scalar @reps_on),
  794.             "no such active repository $arg"
  795.         );
  796.         $rep = $reps_on[$arg - 1];
  797.         }
  798.         else {
  799.         $rep = $o->rep_uniq($rep);
  800.         return unless $o->assert(
  801.             $o->rep_exists($rep),
  802.             "no such repository '$rep'"
  803.         );
  804.         }
  805.         my $info = PPM::UI::repository_info($rep);
  806.         unless ($info->is_success) {
  807.         $o->warn($info->msg);
  808.         return unless $info->ok;
  809.         }
  810.         my $type = $o->rep_ison($rep) ? "Active" : "Inactive";
  811.         my $num  = (
  812.         $o->rep_ison($rep)
  813.         ? " " . find_index($rep, 1, @reps_on)
  814.         : ""
  815.         );
  816.         my @info = $info->result_l;
  817.         my @keys = qw(Name Location Type);
  818.         push @keys, qw(Username) if @info >= 4;
  819.         push @keys, qw(Password) if @info >= 5;
  820.         $o->inform("Describing $type Repository$num:\n");
  821.         $o->print_pairs(\@keys, \@info);
  822.         return 1;
  823.     }
  824.     elsif (matches($cmd, 'r|ename')) {
  825.         my $arg = $args[0];
  826.         my $name = $args[1];
  827.         my $rep = $arg;
  828.         if ($arg =~ /^\d+$/) {
  829.         return unless $o->assert(
  830.             bounded(1, $arg, scalar @reps_on),
  831.             "no such active repository $arg"
  832.         );
  833.         $rep = $reps_on[$arg - 1];
  834.         }
  835.         else {
  836.         $rep = $o->rep_uniq($rep);
  837.         return unless $o->assert(
  838.             $o->rep_exists($rep),
  839.             "no such repository '$rep'"
  840.         );
  841.         }
  842.         my $ok = PPM::UI::repository_rename($rep, $name);
  843.         unless ($ok->is_success) {
  844.         $o->warn($ok->msg);
  845.         return unless $ok->ok;
  846.         }
  847.         $o->rep_on($name) if $o->rep_ison($rep);
  848.         $o->rep_off($rep);
  849.     }
  850.  
  851.     #=====================================================================
  852.     # On, off, up, and down commands:
  853.     #=====================================================================
  854.     elsif (matches($cmd, 'on')) {
  855.         my $rep = $o->rep_uniq($args[0]);
  856.         return unless $o->assert(
  857.         $o->rep_isoff($rep),
  858.         "no such inactive repository '$rep'"
  859.         );
  860.         $o->rep_on($rep);
  861.         $o->cache_clear('search');
  862.     }
  863.     elsif (matches($cmd, 'of|f')) {
  864.         my $arg = $args[0];
  865.         my $rep = $arg;
  866.         if ($arg =~ /^\d+$/) {
  867.         return unless $o->assert(
  868.             bounded(1, $arg, scalar @reps_on),
  869.             "no such active repository $arg"
  870.         );
  871.         $rep = $reps_on[$arg - 1];
  872.         }
  873.         else {
  874.         $rep = $o->rep_uniq($rep);
  875.         return unless $o->assert(
  876.             $o->rep_exists($rep),
  877.             "no such repository '$rep'"
  878.         );
  879.         }
  880.         $o->rep_off($rep);
  881.         $o->cache_clear('search');
  882.     }
  883.     elsif (matches($cmd, 'up')) {
  884.         my $arg = $args[0];
  885.         my $rep = $arg;
  886.         if ($arg =~ /^\d+$/) {
  887.         return unless $o->assert(
  888.             bounded(1, $arg, scalar @reps_on),
  889.             "no such active repository $arg"
  890.         );
  891.         $rep = $reps_on[$arg - 1];
  892.         }
  893.         else {
  894.         return unless $o->assert(
  895.             $o->rep_exists($rep),
  896.             "no such repository '$rep'"
  897.         );
  898.         }
  899.         $o->rep_up($rep);
  900.     }
  901.     elsif (matches($cmd, 'do|wn')) {
  902.         my $arg = $args[0];
  903.         my $rep = $arg;
  904.         if ($arg =~ /^\d+$/) {
  905.         return unless $o->assert(
  906.             bounded(1, $arg, scalar @reps_on),
  907.             "no such active repository $arg"
  908.         );
  909.         $rep = $reps_on[$arg - 1];
  910.         }
  911.         else {
  912.         return unless $o->assert(
  913.             $o->rep_exists($rep),
  914.             "no such repository '$rep'"
  915.         );
  916.         }
  917.         $o->rep_down($rep);
  918.     }
  919.  
  920.     else {
  921.         $o->warn(<<END) and return;
  922. No such repository command '$cmd'; see 'help repository'.
  923. END
  924.     }
  925.     }
  926.     &$refresh;
  927.     unless(@reps) {
  928.     $o->warn("No repositories. Use 'rep add' to add a repository.\n");
  929.     }
  930.     else {
  931.     my $i = 0;
  932.     my $count = @reps_on;
  933.     my $l = length($count);
  934.     $o->inform("Repositories:\n");
  935.     for my $r (@reps_on) {
  936.         my $n = sprintf("%${l}d", $i + 1);
  937.         $o->inform("[$n] $r\n");
  938.         $i++;
  939.     }
  940.     for my $r ($o->dictsort(@reps_off)) {
  941.         my $s = ' ' x $l;
  942.         $o->inform("[$s] $r\n");
  943.     }
  944.     }
  945.     1;
  946. }
  947.  
  948. #============================================================================
  949. # Search:
  950. # search        # displays previous searches
  951. # search <\d+>        # displays results of previous search
  952. # search <terms>    # executes a new search on the current repository
  953. #============================================================================
  954. sub smry_search { "searches for packages in a repository" }
  955. sub help_search { <<'END' }
  956. search -- Search for Packages
  957.   Synopsis
  958.      search                Displays list of previous searches
  959.      search <number>       Displays results of search <number>
  960.      search <glob pattern> Performs a new search
  961.      search <field>=<glob> Searches for all packages matching the field.
  962.      search *              Displays all packages in the current repository
  963.  
  964.     The available fields are 'ABSTRACT', 'NAME', 'TITLE', 'AUTHOR', and
  965.     'VERSION'. 'NAME' is used when you do not specify a field.
  966.  
  967.   Description
  968.     Use the search command to look through the repository for packages. PPM
  969.     version 3.0 provides powerful search functionality. For example:
  970.  
  971.     1.  Search for 'CGI' anywhere in the name:
  972.  
  973.           search CGI
  974.  
  975.         Example results:
  976.  
  977.           Apache-CGI
  978.           CGI-Application
  979.           CGI-ArgChecker
  980.  
  981.     2.  Search for 'CGI' at the beginning of the name:
  982.  
  983.           search CGI*
  984.  
  985.         Example results:
  986.  
  987.           CGI-ArgChecker
  988.           CGI-Application
  989.  
  990.     3.  Search for all modules authored by someone with 'smith' in their
  991.         name or email:
  992.  
  993.           search AUTHOR=smith 
  994.  
  995.         Example results:
  996.  
  997.           Apache-ProxyPass
  998.           Business-ISBN
  999.  
  1000.     4.  Search for 'compress' anywhere in the abstract:
  1001.  
  1002.           search ABSTRACT=compress
  1003.  
  1004.         Example results:
  1005.  
  1006.           Apache-GzipChain
  1007.           IO-Zlib
  1008.  
  1009.     5.  Search for 'CGI' in the name, or 'web' in the abstract:
  1010.  
  1011.           search CGI or ABSTRACT=web
  1012.  
  1013.         Example results:
  1014.  
  1015.           CGI-XMLForm
  1016.           HTML-Clean
  1017.  
  1018.     6.  Search for 'XML' in the name and either 'parser' in the name or
  1019.         'pars' in the abstract, but not with 'XPath' in the name:
  1020.  
  1021.           search XML and (parser or ABSTRACT=pars) and not XPath
  1022.  
  1023.         Example results:
  1024.  
  1025.           XML-Node
  1026.           XML-Parser-EasyTree
  1027.  
  1028.     7.  PPM Server 3.0 repositories only: search by module name, even if
  1029.         unrelated to the containing package:
  1030.  
  1031.           search Data::Grove
  1032.                                 
  1033.         Example results:
  1034.  
  1035.           libxml-perl
  1036.  
  1037.     8.  Browse all packages in the repository:
  1038.  
  1039.           search *
  1040.  
  1041.         Example results:
  1042.  
  1043.           Affix-Infix2Postfix
  1044.           AI-Fuzzy
  1045.           [many more...]
  1046.  
  1047.     Recall previous searches using the 'search <number>' command. PPM3
  1048.     stores searches for each session until you exit PPM.
  1049.  
  1050.     Some package names or versions are too long to be displayed in the
  1051.     search results. If a name is too long, you will see a '~' (tilde) as the
  1052.     last visible character in the column. You can use *describe* to view
  1053.     detailed information about such packages.
  1054.  
  1055.   Search Results
  1056.     When you type a command like "search XML", PPM searches in each of the
  1057.     Active Repositories (see the *repository* command) for your package. The
  1058.     results are merged into one list, and duplicates (packages found in more
  1059.     than one repository) are hidden.
  1060.  
  1061.     You can control what fields PPM shows for each package. The fields each
  1062.     have a built-in weight, which is used to calculate how wide to make each
  1063.     field based on the width of your screen. Information that doesn't fit
  1064.     into a field is truncated, and a tilde ("~") character is displayed in
  1065.     the last column of the field.
  1066.  
  1067.     Let's get down to an example:
  1068.  
  1069.         ppm> search XML
  1070.         Searching in Active Repositories
  1071.             1. CGI-XMLForm           [0.10] Extension to CGI.pm which
  1072.             2. Data-DumpXML          [1.01] Dump arbitrary data structures
  1073.             3. DBIx-XML_RDB          [0.05] Perl extension for creating XML
  1074.             4. DBIx-XMLMessage       [0.03] XML Message exchange between DBI
  1075.             5. GoXML-XQI            [1.1.4] Perl extension for the XML Query
  1076.             6. Language-DATR-DATR2~ [0.901] manipulate DATR .dtr, XML, HTML,
  1077.             7. libxml-perl           [0.07] support for deeply nested
  1078.             8. Mail-FilterXML         [0.1] Undetermined
  1079.             9. Mail-XML              [0.03] Adds a toXML() method to
  1080.            10. Pod-XML               [0.93] Module to convert POD to XML
  1081.  
  1082.     As you can see, the three fields being displayed are:
  1083.  
  1084.     1   NAME
  1085.  
  1086.         The package name
  1087.  
  1088.     2   VERSION
  1089.  
  1090.         The package version
  1091.  
  1092.     3   ABSTRACT
  1093.  
  1094.         The package abstract
  1095.  
  1096.     You can customize the view somewhat. If you want to view the authors,
  1097.     but not the abstract, you can run the same *search* command after using
  1098.     *set* to change the fields:
  1099.  
  1100.         ppm> set fields="NAME VERSION AUTHOR"
  1101.         Setting 'fields' set to 'name version author'.
  1102.         ppm> search XML
  1103.         Using cached search result set 1.
  1104.             1. CGI-XMLForm         [0.10] Matt Sergeant (matt@sergeant.org)
  1105.             2. Data-DumpXML        [1.01] Gisle Aas (gisle@aas.no)
  1106.             3. DBIx-XML_RDB        [0.05] Matt Sergeant (matt@sergeant.org)
  1107.             4. DBIx-XMLMessage     [0.03] Andrei Nossov (andrein@andrein.com)
  1108.             5. GoXML-XQI          [1.1.4] Matthew MacKenzie (matt@goxml.com)
  1109.             6. Language-DATR-DAT~ [0.901] Lee Goddard (lgoddard@cpan.org)
  1110.             7. libxml-perl         [0.07] Ken MacLeod (ken@bitsko.slc.ut.us)
  1111.             8. Mail-FilterXML       [0.1] Matthew MacKenzie (matt@goxml.com)
  1112.             9. Mail-XML            [0.03] Matthew MacKenzie (matt@goxml.com)
  1113.            10. Pod-XML             [0.93] Matt Sergeant (matt@sergeant.org)
  1114.  
  1115.     You can change the order in which the results are sorted, and what
  1116.     columns are displayed. The settings *fields* and *sort-field* changes
  1117.     this. You can sort by any valid field name (even fields which are not
  1118.     displayed). See the *settings* command for the valid field names.
  1119.  
  1120.     PPM always hides "duplicate" results. It decides whether a result is
  1121.     duplicated based on the fields being displayed. If the same package is
  1122.     found in more than one repository, but you don't have the REPOSITORY
  1123.     field showing, PPM will only list the package once.
  1124. END
  1125. sub comp_search {()}
  1126. sub run_search {
  1127.     my $o = shift;
  1128.     my @args = @_;
  1129.     my $query = $o->raw_args || join ' ', @args;
  1130.     trace(1, "PPM: search @args\n\tquery='$query'\n");
  1131.     return unless $o->assert(
  1132.     scalar $o->reps_on,
  1133.     "you must activate a repository before searching."
  1134.     );
  1135.  
  1136.     # No args: show cached result sets
  1137.     unless (@args) {
  1138.     my @search_results = $o->cache_sets('search');
  1139.     my $search_result_current = $o->cache_set_current('search');
  1140.     if (@search_results) {
  1141.         $o->inform("Search Result Sets:\n");
  1142.         my $i = 0;
  1143.         for (@search_results) {
  1144.         $o->informf("%s%2d",
  1145.                $search_result_current == $i ? "*" : " ",
  1146.                $i + 1);
  1147.         $o->inform(". $_->{query}\n");
  1148.         $i++;
  1149.         }
  1150.     }
  1151.     else {
  1152.         $o->warn("No search result sets -- provide a search term.\n");
  1153.         return;
  1154.     }
  1155.     }
  1156.  
  1157.     # Args:
  1158.     else {
  1159.     # Show specified result set
  1160.     if ($query =~ /^\d+/) {
  1161.         my $set = int($query);
  1162.         my $s = $o->cache_set('search', $set - 1);
  1163.         unless ($set > 0 and defined $s) {
  1164.         $o->warn("No such search result set '$set'.\n");
  1165.         return;
  1166.         }
  1167.  
  1168.         $query = $o->cache_set('search', $set-1, 'query');
  1169.         $o->inform("Search Results Set $set ($query):\n");
  1170.         $o->print_formatted($s, $o->cache_set_index('search'));
  1171.         $o->cache_set_current('search', $set-1);
  1172.         $o->cache_set_index('search', -1);
  1173.     }
  1174.        
  1175.     # Query is the same as a previous query on the same repository: 
  1176.     # Use cached results and set them as default
  1177.     elsif(grep { $_->{query} eq $query } $o->cache_sets('search')) {
  1178.         my @entries = $o->cache_sets('search');
  1179.         for (my $i=0; $i<@entries; $i++) {
  1180.         if ($o->cache_set('search', $i, 'query') eq $query) {
  1181.             $o->inform("Using cached search result set ", $i+1, ".\n");
  1182.             $o->cache_set_current('search', $i);
  1183.             my $set = $o->cache_set('search');
  1184.             $o->print_formatted($set);
  1185.         }
  1186.         }
  1187.     }
  1188.  
  1189.     # Perform a new search
  1190.     else {
  1191.         my @rlist = $o->reps_on;
  1192.         my $targ = $o->conf('target');
  1193.         my $case = not $o->conf('case-sensitivity');
  1194.  
  1195.         $o->inform("Searching in Active Repositories\n");
  1196.         my $ok = PPM::UI::search(\@rlist, $targ, $query, $case);
  1197.         unless ($ok->is_success) {
  1198.         $o->warn($ok->msg);
  1199.         return unless $ok->ok;
  1200.         }
  1201.         my @matches = $ok->result_l;
  1202.         unless (@matches) {
  1203.         $o->warn("No matches for '$query'; see 'help search'.\n");
  1204.         return 1;
  1205.         }
  1206.         $o->cache_set_index('search', -1);
  1207.         $o->cache_set_add('search', $query, \@matches);
  1208.         $o->cache_set_current('search', scalar($o->cache_sets('search')) - 1);
  1209.         my @set = $o->cache_set('search');
  1210.         $o->print_formatted(\@set);
  1211.     }
  1212.     }
  1213.     1;
  1214. }
  1215. sub alias_search { qw(s) }
  1216.  
  1217. #============================================================================
  1218. # tree
  1219. # tree        # shows the dependency tree for the default/current pkg
  1220. # tree <\d+>    # shows dep tree for numbered pkg in current search set
  1221. # tree <pkg>    # shows dep tree for given package
  1222. # tree <url>    # shows dep tree for package located at <url>
  1223. # tree <glob>    # searches for matches
  1224. #============================================================================
  1225. sub smry_tree { "shows package dependency tree" }
  1226. sub help_tree { <<'END' }
  1227. tree -- Show Dependency Tree for Packages
  1228.   Synopsis
  1229.      tree                Displays the dependency-tree of the current
  1230.                          or default package
  1231.      tree <number>       Displays the dependency-tree of the given <number>
  1232.      tree <range>        Displays a <range> of dependency-trees
  1233.      tree <package name> Displays the dependency-tree of the named package
  1234.      tree <url>          Displays the dependency-tree for the
  1235.                          package at <url>
  1236.      tree <glob pattern> Performs a new search using <glob pattern>
  1237.  
  1238.   Description
  1239.     The tree command is used to show the "dependency tree" of a given
  1240.     package (additional packages that are required by the current package).
  1241.     For example:
  1242.  
  1243.         tree SOAP-lite
  1244.  
  1245.     returns:
  1246.  
  1247.         ====================
  1248.         SOAP-Lite 0.51
  1249.         |__MIME-tools 5.316
  1250.         |   |__MailTools 1.15
  1251.         |   \__IO-stringy 1.216
  1252.         |
  1253.         \__MIME-Lite 2.105
  1254.         ====================
  1255.  
  1256.     SOAP-Lite requires four other packages.
  1257.  
  1258.     When tree is called without a <name> or <number> switch, the command
  1259.     will return the dependency tree of the first package in the default
  1260.     search result. If there is no default search, you will be requested to
  1261.     use search to find a package.
  1262. END
  1263. sub comp_tree { goto &comp_describe }
  1264. sub run_tree {
  1265.     my $o = shift;
  1266.     my @args = @_;
  1267.     trace(1, "PPM: tree @args\n");
  1268.  
  1269.     # Check for anything that looks like a query. If it does, just
  1270.     # send it to search() instead.
  1271.     my $query = $o->raw_args || join ' ', @args;
  1272.     $query ||= '';
  1273.     if ($query and not PPM::UI::is_pkg($args[0]) and not parse_range($query)) {
  1274.     $o->inform("Wildcards detected; using 'search' instead...\n");
  1275.     return $o->run('search', @_);
  1276.     }
  1277.  
  1278.     # No Args: describes current index of current result set, or 1.
  1279.     unless (@args) {
  1280.     my @search_results = $o->cache_sets('search');
  1281.     my $search_result_current = $o->cache_set_current('search');
  1282.     unless (@search_results and
  1283.         bounded(0, $search_result_current, $#search_results)) {
  1284.         $o->warn("No search results to show dependency tree for -- " . 
  1285.           "use 'search' to find a package.\n");
  1286.         return;
  1287.     }
  1288.     else {
  1289.         my @res = $o->cache_set('search');
  1290.         my $npkgs = @res;
  1291.         $o->inform("$SEP\n");
  1292.         if ($o->cache_entry('search')) {
  1293.         my $n = $o->cache_set_index('search') + 1;
  1294.         $o->inform("Package $n:\n");
  1295.         $o->tree_pkg($o->cache_entry('search'));
  1296.         }
  1297.         elsif (defined $o->cache_entry('search', 0)) {
  1298.         $o->inform("Package 1:\n");
  1299.         $o->tree_pkg($o->cache_entry('search', 0));
  1300.         $o->cache_set_index('search', 0);
  1301.         }
  1302.         else {
  1303.         $o->inform("Search Results are empty -- use 'search' again.\n");
  1304.         }
  1305.         $o->inform("$SEP\n");
  1306.     }
  1307.     }
  1308.  
  1309.     # Args provided
  1310.     else {
  1311.  
  1312.     # Describe a particular number:
  1313.     if (my @r = parse_range(@args)) {
  1314.         my @search_results = $o->cache_sets('search');
  1315.         my $search_result_current = $o->cache_set_current('search');
  1316.         unless (bounded(0, $search_result_current, $#search_results)) {
  1317.         $o->inform("No search results to show dependency tree for -- " . 
  1318.           "use 'search' to find a package.\n");
  1319.         return;
  1320.         }
  1321.         else {
  1322.         for my $n (@r) {
  1323.             my $sr = $o->cache_set('search');
  1324.             $o->inform("$SEP\n");
  1325.             if (bounded(1, $n, scalar @$sr)) {
  1326.             $o->inform("Package $n:\n");
  1327.             $o->tree_pkg($o->cache_entry('search', $n-1));
  1328.             }
  1329.             else {
  1330.             $o->inform("No such package $n in result set.\n");
  1331.             }
  1332.             $o->cache_set_index('search', $n - 1);
  1333.         }
  1334.         $o->inform("$SEP\n");
  1335.         }
  1336.     }
  1337.  
  1338.     # Describe a particular package
  1339.     else {
  1340.         return unless $o->assert(
  1341.         scalar $o->reps_on,
  1342.         "No repositories -- use 'rep add' to add a repository.\n"
  1343.         );
  1344.         my $pkg =
  1345.           PPM::UI::describe([$o->reps_on], $o->conf('target'), $args[0]);
  1346.         unless ($pkg->is_success) {
  1347.         $o->warn($pkg->msg);
  1348.         return unless $pkg->ok;
  1349.         }
  1350.         if ($pkg->ok) {
  1351.         $o->inform("$SEP\n");
  1352.         $o->tree_pkg($pkg->result);
  1353.         $o->inform("$SEP\n");
  1354.         }
  1355.     }
  1356.     }
  1357.     1;
  1358. }
  1359.  
  1360. #============================================================================
  1361. # Describe:
  1362. # des        # describes default or current package
  1363. # des <\d+>    # describes numbered package in the current search set
  1364. # des <pkg>    # describes the named package (bypasses cached results)
  1365. # des <url>    # describes the package located at <url>
  1366. #============================================================================
  1367. sub smry_describe { "describes packages in detail" }
  1368. sub help_describe { <<'END' }
  1369. describe -- Describe Packages
  1370.   Synopsis
  1371.      des                Describes default/current package
  1372.      des <number>       Describes package <number> in the
  1373.                         current search set
  1374.      des <range>        Describes packages in the given 
  1375.                         <range> from the current search
  1376.      des <package name> Describes named package
  1377.      des <url>          Describes package located at <url>
  1378.      des <glob pattern> Performes a new search using <glob pattern>
  1379.  
  1380.   Description
  1381.     The describe command returns information about a package, including the
  1382.     name of the package, the author's name and a brief description (called
  1383.     an "Abstract") about the package. For example:
  1384.  
  1385.         describe libnet
  1386.  
  1387.     returns:
  1388.  
  1389.         ===============================
  1390.         Package 1
  1391.         Name: libnet
  1392.         Version: 1.07.03
  1393.         Author: Graham Barr
  1394.         Abstract: Collection of Network protocol modules
  1395.         Implementations:
  1396.                 1.sun4-solaris-thread-multi
  1397.                 2.i686-linux-thread-multi
  1398.                 3.MSWIn32-x86-multi-thread
  1399.         ===============================
  1400.  
  1401.     There are two modifiers to the describe command:
  1402.  
  1403.     -ppd
  1404.         Displays the raw PPD of the package.
  1405.  
  1406.     -dump
  1407.         The same as -ppd.
  1408.  
  1409.     When the describe command is called without arguments, it returns
  1410.     information about the first package in the current search. If there is
  1411.     no default search set, you will be prompted to use the search command to
  1412.     find a package.
  1413.  
  1414.     If describe is called with a numeric argument, that number is set as the
  1415.     default package and the information about that package is returned. If
  1416.     the number given doesn't exist, you will be prompted to use search to
  1417.     find a package. Also, you can use describe to get descriptions of
  1418.     several packages. For example:
  1419.  
  1420.         describe 4-7
  1421.  
  1422.     will return descriptions of packages 4 through 7 in the current search
  1423.     request. You can also enter:
  1424.  
  1425.         describe 3-4,10
  1426.  
  1427.     to get information on packages 3, 4 and 10.
  1428.  
  1429.     If you specify a URL as the argument to describe, PPM will describe the
  1430.     package located at the URL. The URL must point to a PPD file. The URL
  1431.     can also point to a PPD file on your computer.
  1432.  
  1433.     When the describe command is given a name with a wildcard (such as "*"
  1434.     or "?") it executes the search command with the given argument. For
  1435.     example, describe Tk* will return the name(s) of any packages that match
  1436.     the search parameters.
  1437.  
  1438.   See Also
  1439.     properties
  1440. END
  1441. sub comp_describe {
  1442.     my $o = shift;
  1443.     my ($word, $line, $start) = @_;
  1444.  
  1445.     # If no search results
  1446.     my $n_results = $o->cache_sets('search');
  1447.     my $n_current = $o->cache_set_current('search');
  1448.     return ()
  1449.       unless ($n_results and bounded(0, $n_current, $n_results - 1));
  1450.     my @words = $o->line_parsed($line);
  1451.  
  1452.     # If the previous word isn't a number or the command, stop.
  1453.     return ()
  1454.       if ($#words > 0 and
  1455.       $words[$#words] !~ /^\d+/ and
  1456.       $start == length($line) or 
  1457.       $#words > 1);
  1458.  
  1459.     # This is the most optimistic list:
  1460.     my @results = $o->cache_set('search');
  1461.     my $npkgs = @results;
  1462.     my @compls = (1 .. $npkgs);
  1463.  
  1464.     # If the previous word is a number, return only other numbers:
  1465.     return $o->completions($word, \@compls)
  1466.       if $words[$#words] =~ /^\d+/;
  1467.  
  1468.     # Either a number or the names of the packages
  1469.     push @compls, map { $_->name } @results;
  1470.     return $o->completions($word, \@compls);
  1471. }
  1472. sub run_describe {
  1473.     my $o = shift;
  1474.     my @args = @_;
  1475.     
  1476.     # Check for options:
  1477.     my $ppd;
  1478.     {
  1479.     local @ARGV = @args;
  1480.     GetOptions(ppd => \$ppd, dump => \$ppd);
  1481.     @args = @ARGV;
  1482.     }
  1483.  
  1484.     trace(1, "PPM: describe @args\n");
  1485.  
  1486.     # Check for anything that looks like a query. If it does, just
  1487.     # send it to search() instead.
  1488.     my $query = $o->raw_args || join ' ', @args;
  1489.     if ($query and not PPM::UI::is_pkg($args[0]) and not parse_range($query)) {
  1490.     $o->inform("Wildcards detected; using 'search' instead...\n");
  1491.     return $o->run('search', @_);
  1492.     }
  1493.  
  1494.     my $dumper = sub {
  1495.     my $o = shift;
  1496.     my $pkg_obj = shift;
  1497.     my $ppd = $pkg_obj->getppd($o->conf('target'))->result;
  1498.     $o->page($ppd);
  1499.     };
  1500.     my $displayer = $ppd ? $dumper : \&describe_pkg;
  1501.  
  1502.     # No Args: describes current index of current result set, or 1.
  1503.     unless (@args) {
  1504.     my @search_results = $o->cache_sets('search');
  1505.     my $search_result_current = $o->cache_set_current('search');
  1506.     unless (@search_results and
  1507.         bounded(0, $search_result_current, $#search_results)) {
  1508.         $o->warn("No search results to describe -- " . 
  1509.           "use 'search' to find a package.\n");
  1510.         return;
  1511.     }
  1512.     else {
  1513.         my @res = $o->cache_set('search');
  1514.         my $npkgs = @res;
  1515.         $o->inform("$SEP\n");
  1516.         if ($o->cache_entry('search')) {
  1517.         my $n = $o->cache_set_index('search') + 1;
  1518.         $o->inform("Package $n:\n");
  1519.         $o->$displayer($o->cache_entry('search'));
  1520.         }
  1521.         elsif (defined $o->cache_entry('search', 0)) {
  1522.         $o->inform("Package 1:\n");
  1523.         $o->$displayer($o->cache_entry('search', 0));
  1524.         $o->cache_set_index('search', 0);
  1525.         }
  1526.         else {
  1527.         $o->warn("Search Results are empty -- use 'search' again.\n");
  1528.         }
  1529.         $o->inform("$SEP\n");
  1530.     }
  1531.     }
  1532.  
  1533.     # Args provided
  1534.     else {
  1535.  
  1536.     # Describe a particular number:
  1537.     if (my @r = parse_range(@args)) {
  1538.         my @search_results = $o->cache_sets('search');
  1539.         my $search_result_current = $o->cache_set_current('search');
  1540.         unless (bounded(0, $search_result_current, $#search_results)) {
  1541.         $o->warn("No search results to describe -- " . 
  1542.           "use 'search' to find a package.\n");
  1543.         return;
  1544.         }
  1545.         else {
  1546.         for my $n (@r) {
  1547.             my $sr = $o->cache_set('search');
  1548.             $o->inform("$SEP\n");
  1549.             if (bounded(1, $n, scalar @$sr)) {
  1550.             $o->inform("Package $n:\n");
  1551.             $o->$displayer($o->cache_entry('search', $n-1));
  1552.             }
  1553.             else {
  1554.             $o->inform("No such package $n in result set.\n");
  1555.             }
  1556.             $o->cache_set_index('search', $n - 1);
  1557.         }
  1558.         $o->inform("$SEP\n");
  1559.         }
  1560.     }
  1561.  
  1562.     # Describe a particular package
  1563.     else {
  1564.         return unless $o->assert(
  1565.         scalar $o->reps_on,
  1566.         "No repositories -- use 'rep add' to add a repository.\n"
  1567.         );
  1568.         my ($set, $index) = $o->cache_find('search', $args[0]);
  1569.         my ($ok, $pkg);
  1570.         if ($index >= 0) {
  1571.         $o->cache_set_current('search', $set);
  1572.         $o->cache_set_index('search', $index);
  1573.         $pkg = $o->cache_entry('search');
  1574.         }
  1575.         else {
  1576.         $ok = PPM::UI::describe([$o->reps_on],
  1577.                     $o->conf('target'), $args[0]);
  1578.         unless ($ok->is_success) {
  1579.             $o->inform($ok->msg);
  1580.             return unless $ok->ok;
  1581.         }
  1582.         $pkg = $ok->result;
  1583.         $o->cache_set_add('search', $args[0], [$pkg]);
  1584.         my $last = $o->cache_sets('search') - 1;
  1585.         $o->cache_set_current('search', $last);
  1586.         $o->cache_set_index('search', 0);
  1587.         }
  1588.         $o->inform("$SEP\n");
  1589.         $o->$displayer($pkg);
  1590.         $o->inform("$SEP\n");
  1591.     }
  1592.     }
  1593.     1;
  1594. }
  1595.  
  1596. #============================================================================
  1597. # Install:
  1598. # i        # installs default or current package
  1599. # i <\d+>    # installs numbered package in current search set
  1600. # i <pkg>    # installs named package
  1601. # i <url>    # installs the package at <url>
  1602. #============================================================================
  1603. sub smry_install { "installs packages" }
  1604. sub help_install { <<'END' }
  1605. install -- Install Packages
  1606.   Synopsis
  1607.      install           Installs default package
  1608.      install <number>  Installs packages by a specific <number>
  1609.      install <range>   Installs packages in the given numeric <range>
  1610.      install <name>    Installs named package
  1611.      install <url>     Installs the package located at <url>
  1612.  
  1613.   Description
  1614.     The install command is used to install packages from the repository.
  1615.     Install packages by name or number (the number is given by the
  1616.     repository or search request), or set a default package using the
  1617.     describe command. You can specify a full URL to a PPD file; the URL may
  1618.     point to a PPD file on your computer.
  1619.  
  1620.     If you have profile tracking enabled, (see 'help profile') the current
  1621.     profile will be updated to include the newly installed package(s).
  1622.  
  1623.     The following modifiers can be used with the install command:
  1624.  
  1625.     *   -force
  1626.  
  1627.     *   -noforce
  1628.  
  1629.     *   -follow
  1630.  
  1631.     *   -nofollow
  1632.  
  1633.     The force and follow switches determine how packages are installed:
  1634.  
  1635.      FORCE       FOLLOW          RESULT
  1636.      false       false           Checks to see if the package is installed and
  1637.                                  if it is, installation stops. If there are any
  1638.                                  missing prerequisites, the installation will
  1639.                                  fail.
  1640.  
  1641.      false       true            Checks to see if the package is installed and
  1642.                                  if it is, installation stops. If there are any
  1643.                                  missing prerequisites, they are automatically
  1644.                                  installed. NOTE: this is the default setting
  1645.                                  when PPM is first installed.
  1646.  
  1647.      true        false           If the package is installed, PPM will
  1648.                                  reinstall the package. If there are any
  1649.                                  missing prerequisites, the installation will
  1650.                                  fail.
  1651.  
  1652.      true        true            If the package is installed, PPM will
  1653.                                  reinstall the package. All prerequisites are
  1654.                                  installed, missing or not.
  1655.     
  1656.     If you do not specify any options, install uses the default settings.
  1657.     Set or view the current defaults using the 'settings' command.
  1658.  
  1659.     For example:
  1660.  
  1661.         install foo
  1662.  
  1663.     will install the package named "foo", using the default settings.
  1664.     Over-ride the defaults using the install modifiers described above.
  1665.  
  1666.     For example:
  1667.  
  1668.         install foo -force
  1669.  
  1670.     will install the "foo" package, even if it has already been installed.
  1671.     If both -force and -follow are set to "true", all the prerequisites for
  1672.     any package you install will also be installed. For example, the
  1673.     installation of a tk-related package, like "tk-ach" which is 8.4 kB will
  1674.     be preceded by the installation of Tk, which is 1.7 MB.
  1675.  
  1676.     You can also install by package number. Package numbers are based on the
  1677.     current repository or current search request. For example:
  1678.  
  1679.         install 6
  1680.  
  1681.     installs package number 6. You can install more than one package at one
  1682.     time:
  1683.  
  1684.         install 3-5
  1685.  
  1686.     installs packages 3, 4 and 5. You can also type install 3-6,8 to install
  1687.     packages 3,4,5,6 and 8.
  1688.  
  1689.   See Also
  1690.     profile
  1691. END
  1692. sub comp_install { goto &comp_describe }
  1693. sub run_install {
  1694.     my $o = shift;
  1695.     my @args = @_;
  1696.     trace(1, "PPM: install @args\n");
  1697.  
  1698.     # Get the install options
  1699.     my %opts = (
  1700.     force  => $o->conf('force-install'),
  1701.     follow => $o->conf('follow-install'),
  1702.     dryrun => 0,
  1703.     );
  1704.     {
  1705.     local @ARGV = @args;
  1706.     GetOptions('force!'  => \$opts{force},
  1707.            'follow!' => \$opts{follow},
  1708.            'dryrun'  => \$opts{dryrun},
  1709.           );
  1710.     @args = @ARGV;
  1711.     }
  1712.  
  1713.     # No Args -- installs default package
  1714.     unless (@args) {
  1715.     my @search_results = $o->cache_sets('search');
  1716.     my $search_result_current = $o->cache_set_current('search');
  1717.     unless (@search_results and
  1718.         bounded(0, $search_result_current, $#search_results)) {
  1719.         $o->warn("No search results to install -- " . 
  1720.           "use 'search' to find a package.\n");
  1721.         return;
  1722.     }
  1723.     else {
  1724.         my @results = $o->cache_set('search');
  1725.         my $npkgs = @results;
  1726.         my $pkg;
  1727.         if ($o->cache_entry('search')) {
  1728.         my $n = $o->cache_set_index('search') + 1;
  1729.         $o->inform("Package $n:\n");
  1730.         $pkg = $o->cache_entry('search');
  1731.         }
  1732.         else {
  1733.         $o->inform("Package 1:\n");
  1734.         $pkg = $o->cache_entry('search', 0);
  1735.         }
  1736.         return $o->install_pkg($pkg, \%opts);
  1737.     }
  1738.     }
  1739.  
  1740.     # Args provided
  1741.     else {
  1742.  
  1743.     # Install a particular number:
  1744.     if (my @r = parse_range(@args)) {
  1745.         my @search_results = $o->cache_sets('search');
  1746.         my $search_result_current = $o->cache_set_current('search');
  1747.         unless (@search_results and
  1748.             bounded(0, $search_result_current, $#search_results)) {
  1749.         $o->warn("No search results to install -- " . 
  1750.           "use 'search' to find a package.\n");
  1751.         return;
  1752.         }
  1753.         else {
  1754.         my $ok = 0;
  1755.         for my $n (@r) {
  1756.             my $sr = $o->cache_set('search');
  1757.             if (bounded(1, $n, scalar @$sr)) {
  1758.             $o->inform("Package $n:\n");
  1759.             my $pkg = $sr->[$n-1];
  1760.             $ok++ if $o->install_pkg($pkg, \%opts);
  1761.             }
  1762.             else {
  1763.             $o->inform("No such package $n in result set.\n");
  1764.             }
  1765.         }
  1766.         return unless $ok;
  1767.         }
  1768.     }
  1769.  
  1770.     # Install a particular package
  1771.     else {
  1772.         if ($o->reps_on) {
  1773.         return $o->install_pkg($args[0], \%opts);
  1774.         }
  1775.         elsif ($o->reps_all) {
  1776.         $o->warn("Can't install: no repositories are active.\n");
  1777.         }
  1778.         else {
  1779.         $o->warn("Can't install: no repositories defined.\n");
  1780.         }
  1781.         return;
  1782.     }
  1783.     }
  1784.     1;
  1785. }
  1786.  
  1787. #============================================================================
  1788. # Target:
  1789. # t        # displays a list of backend targets
  1790. # t [set] <\d+>    # sets numbered target as default backend target
  1791. # t des [<\d+>]    # describes the given (or default) target
  1792. #============================================================================
  1793. sub smry_targets { "views or sets target installer backends" }
  1794. sub help_targets { <<'END' }
  1795. targets -- View Target Installer Backends
  1796.   Synopsis
  1797.      target                      Displays a list of backend targets
  1798.      target <number>             Sets <number> as default backend target
  1799.      target [select] <name or num>
  1800.                                  Sets <name or num> as default backend target
  1801.      target describe [name or num]
  1802.                                  Describes the given (or default) target
  1803.      target set <key> <val>      Sets the target's <key> to <val> 
  1804.      target rename <name or num> <name>
  1805.                                  Renames the given target to <name>
  1806.  
  1807.   Description
  1808.     The target is the destination location of the install routine, such as
  1809.     the directory where the packages are installed when they're downloaded
  1810.     from the repository. For example:
  1811.  
  1812.         target
  1813.  
  1814.     returns:
  1815.  
  1816.         Targets:
  1817.           1. ActivePerl 618
  1818.         * 2. ActivePerl 629
  1819.  
  1820.     This shows that there are two available targets, and that the second
  1821.     target (ActivePerl 629) is currently the default (as shown by the
  1822.     asterisk). Using multiple targets, you can manage multiple installations
  1823.     of Perl from a single command-line.
  1824. END
  1825. sub comp_targets {
  1826.     my $o = shift;
  1827.     my ($word, $line, $start) = @_;
  1828.     my @words = $o->line_parsed($line);
  1829.     my $words = scalar @words;
  1830.     my @compls;
  1831.     my @targs = PPM::UI::target_list()->result_l;
  1832.  
  1833.     # only return 'set' and 'describe' when we're completing the second word
  1834.     if ($words == 1 or $words == 2 and $start != length($line)) {
  1835.     @compls = ('set', 'select', 'describe', 'rename', 1 .. scalar @targs);
  1836.     return $o->completions($word, \@compls);
  1837.     }
  1838.  
  1839.     if ($words == 2 or $words == 3 and $start != length($line)) {
  1840.     # complete 'set'
  1841.     if (matches($words[1], 's|et')) {
  1842.         my $targ = $o->conf('target');
  1843.         @compls = map { $_->[0] }
  1844.               grep { $_->[1] }
  1845.               PPM::UI::target_config_keys($targ)->result_l;
  1846.         return $o->completions($word, \@compls);
  1847.     }
  1848.     # complete 'describe' and 'rename'
  1849.     elsif (matches($words[1], 'd|escribe')
  1850.         or matches($words[1], 'r|ename')
  1851.         or matches($words[1], 's|elect')) {
  1852.         return $o->completions($word, [1 .. scalar @targs]);
  1853.     }
  1854.     }
  1855.     ();
  1856. }
  1857. sub run_targets {
  1858.     my $o = shift;
  1859.     my @args = @_;
  1860.     trace(1, "PPM: target @args\n");
  1861.  
  1862.     my @targets = PPM::UI::target_list()->result_l;
  1863.     my $targets = @targets;
  1864.  
  1865.     # No arguments: print targets
  1866.     if (@args) {
  1867.     my ($cmd, @rest) = @args;
  1868.     if ($cmd =~ /^\d+$/
  1869.         or matches($cmd, 'se|lect')) {
  1870.         my $num =     $cmd =~ /^\d+$/        ? $cmd        :
  1871.             $rest[0] =~ /^\d+$/    ? $rest[0]    :
  1872.             do {
  1873.                 my $n = find_index($rest[0], 1, @targets);
  1874.                 if ($n < 1) {
  1875.                 $o->warn("No such target '$rest[0]'.\n");
  1876.                 return;
  1877.                 }
  1878.                 $n;
  1879.             };
  1880.  
  1881.         # QA the number: is it too high/low?
  1882.         unless(bounded(1, $num, $targets)) {
  1883.         $o->warn("No such target number '$num'.\n");
  1884.         return;
  1885.         }
  1886.         else {
  1887.         $o->conf('target', $targets[$num-1]);
  1888.         $o->cache_clear('query');
  1889.         }
  1890.     }
  1891.     elsif (matches($cmd, 'r|ename')) {
  1892.         my ($oldnum, $newname) = @rest;
  1893.         $oldnum =    $oldnum =~ /^\d+$/ ? $oldnum :
  1894.             do {
  1895.                 my $n = find_index($oldnum, 1, @targets);
  1896.                 if ($n < 1) {
  1897.                 $o->warn("No such target '$oldnum'.\n");
  1898.                 return;
  1899.                 };
  1900.                 $n;
  1901.             };
  1902.         unless (defined $oldnum && $oldnum =~ /^\d+$/) {
  1903.         $o->warn(<<END);
  1904. target: '$cmd' requires a numeric argument. See 'help $cmd'.
  1905. END
  1906.         return;
  1907.         }
  1908.         unless (bounded(1, $oldnum, $targets)) {
  1909.         $o->warn("No such target number '$oldnum'.\n");
  1910.         return;
  1911.         }
  1912.         unless (defined $newname and $newname) {
  1913.         $newname = '' unless defined $newname;
  1914.         $o->warn(<<END);
  1915. Target names must be non-empty: '$newname' is not a valid name.
  1916. END
  1917.         return;
  1918.         }
  1919.         
  1920.         my $oldname = $targets[$oldnum - 1];
  1921.         my $ret = PPM::UI::target_rename($oldname, $newname);
  1922.         $o->warn($ret->msg) unless $ret->ok;
  1923.         $o->conf('target', $newname)
  1924.           if $o->conf('target') eq $oldname;
  1925.         @targets = PPM::UI::target_list()->result_l;
  1926.         $targets = scalar @targets;
  1927.     }
  1928.     elsif (matches($cmd, "s|et")) {
  1929.         my ($key, $value) = @rest;
  1930.         if (defined $key and $key =~ /=/ and not defined $value) {
  1931.         ($key, $value) = split /=/, $key;
  1932.         }
  1933.         unless(defined($key) && $key) {
  1934.         $o->warn(<<END);
  1935. You must specify what option to set. See 'help target'.
  1936. END
  1937.         return;
  1938.         }
  1939.         unless(defined($value)) {
  1940.         $o->warn(<<END);
  1941. You must provide a value for the option. See 'help target'.
  1942. END
  1943.         return;
  1944.         }
  1945.         my $targ = $o->conf('target');
  1946.         my %keys = map { @$_ }
  1947.                PPM::UI::target_config_keys($targ)->result_l;
  1948.         unless ($keys{$key}) {
  1949.         $o->warn("Invalid set key '$key'; these are the settable values:\n");
  1950.         $o->warn("    $_\n") for (grep { $keys{$_} } keys %keys);
  1951.         return;
  1952.         }
  1953.         my $ok = PPM::UI::target_config_set($targ, $key, $value);
  1954.         unless ($ok->is_success) {
  1955.         $o->warn($ok->msg);
  1956.         return unless $ok->ok;
  1957.         }
  1958.         $o->inform("Target attribute '$key' set to '$value'\n");
  1959.         return 1;
  1960.     }
  1961.     elsif (matches($cmd, "d|escribe")) {
  1962.         my %opts = (exec => 1);
  1963.         my $sel;
  1964.         if (@rest) {
  1965.         local @ARGV = @rest;
  1966.         GetOptions(\%opts, 'exec!');
  1967.         @rest = @ARGV;
  1968.         }
  1969.         if (@rest) {
  1970.         $sel =    $rest[0] =~ /^\d+$/ ? $rest[0] :
  1971.                 do {
  1972.                 my $n = find_index($rest[0], 1, @targets);
  1973.                 if ($n < 1) {
  1974.                     $o->warn("No such target '$rest[0]'.\n");
  1975.                     return;
  1976.                 };
  1977.                 $n;
  1978.                 };
  1979.         unless(bounded(1, $sel, $targets)) {
  1980.             $o->warn("No such target number '$sel'.\n");
  1981.         }
  1982.         }
  1983.         else {
  1984.         $sel = find_index($o->conf('target'), 1, @targets);
  1985.         }
  1986.         my $targ = $targets[$sel-1];
  1987.         my (@keys, @vals);
  1988.         my $res = $opts{exec}
  1989.         ? PPM::UI::target_info($targ)
  1990.         : PPM::UI::target_raw_info($targ);
  1991.         unless ($res->is_success) {
  1992.         $o->warn($res->msg);
  1993.         return unless $res->ok;
  1994.         }
  1995.         my %h = $res->result_h;
  1996.         my @h = sort keys %h;
  1997.         push @keys, @h;
  1998.         push @vals, $h{$_} for @h;
  1999.         if ($opts{exec}) {
  2000.         for (PPM::UI::target_config_info($targ)->result_l) {
  2001.             push @keys, $_->[0];
  2002.             push @vals, $_->[1];
  2003.         }
  2004.         }
  2005.         $_ = ucfirst $_ for @keys;
  2006.         $o->inform("Describing target $sel ($targ):\n");
  2007.         $o->print_pairs(\@keys, \@vals);
  2008.         return 1;
  2009.     }
  2010.     }
  2011.     unless($targets) {
  2012.     $o->warn("No targets. Install a PPM target.\n");
  2013.     return;
  2014.     }
  2015.     else {
  2016.     $o->conf('target', $targets[0])
  2017.         unless $o->conf('target');
  2018.     my $i = 0;
  2019.     $o->inform("Targets:\n");
  2020.     for (@targets) {
  2021.         $o->informf(
  2022.         "%s%2d",
  2023.         $o->conf('target') eq $targets[$i] ? "*" : " ",
  2024.         $i + 1
  2025.         );
  2026.         $o->inform(". $_\n");
  2027.         $i++;
  2028.     }
  2029.     }
  2030.     1;
  2031. }
  2032.  
  2033. #============================================================================
  2034. # Query:
  2035. # query        # displays list of previous queries
  2036. # query <\d+>    # displays results of previous query
  2037. # query <terms>    # performs a new query and displays results
  2038. #============================================================================
  2039. sub smry_query { "queries installed packages" }
  2040. sub help_query { <<'END' }
  2041. query -- Query Installed Packages
  2042.   Synopsis
  2043.      query                   Displays list of previous queries
  2044.      query <number>          Displays results of previous query
  2045.      query <glob pattern>    Performs a new query using <glob pattern>
  2046.      query *                 Displays a list of all installed packages
  2047.  
  2048.   Description
  2049.     The query command displays a list of all installed packages, or a list
  2050.     based on the <glob pattern> switch. You can also check the list of past
  2051.     queries, or the results of a past query.
  2052.  
  2053.     With PPM 3.0, you can now perform much more powerful queries. The syntax
  2054.     is identical to the 'search' command, and almost all the search switches
  2055.     are also available for querying installed packages.
  2056.  
  2057.     Recall previous queries with the 'query <number>' command. PPM3 stores
  2058.     all queries from the current PPM session.
  2059.  
  2060.     Note: Depending on the value of the "case-sensitivity" setting, the
  2061.     query may or may not be case-sensitive. See "help settings" for
  2062.     instructions on setting the default case sensitivity.
  2063.  
  2064.   See Also
  2065.     search, settings
  2066. END
  2067. sub comp_query {()}
  2068. sub run_query {
  2069.     my $o = shift;
  2070.     my $query = $o->raw_args || join ' ', @_;
  2071.     trace(1, "PPM: query @_\n\tquery='$query'\n");
  2072.     my @targets = PPM::UI::target_list()->result_l;
  2073.     my $target = $o->conf('target');
  2074.     my $case = not $o->conf('case-sensitivity');
  2075.     $o->warn("You must install an installation target before using PPM.\n")
  2076.       and return unless @targets;
  2077.  
  2078.     # No args: show cached query sets
  2079.     unless ($query =~ /\S/) {
  2080.     my @query_results = $o->cache_sets('query');
  2081.     my $query_result_current = $o->cache_set_current('query');
  2082.     if (@query_results) {
  2083.         $o->inform("Query Result Sets:\n");
  2084.         my $i = 0;
  2085.         for (@query_results) {
  2086.         $o->informf("%s%2d",
  2087.                $query_result_current == $i ? "*" : " ",
  2088.                $i + 1);
  2089.         $o->inform(". $_->{query}\n");
  2090.         $i++;
  2091.         }
  2092.     }
  2093.     else {
  2094.         $o->warn("No query result sets -- provide a query term.\n");
  2095.         return;
  2096.     }
  2097.     }
  2098.  
  2099.     # Args:
  2100.     else {
  2101.     # Show specified result set 
  2102.     if ($query =~ /^\d+/) {
  2103.         my $set = int($query);
  2104.         unless (defined $o->cache_set('query', $set-1)) {
  2105.         $o->warn("No such query result set '$set'.\n");
  2106.         return;
  2107.         }
  2108.  
  2109.         $query = $o->cache_set('query', $set-1, 'query');
  2110.         $o->inform("Query Results Set $set ($query):\n");
  2111.         $o->print_formatted([$o->cache_set('query', $set-1)],
  2112.                 $o->cache_set_index('query'));
  2113.                 
  2114.         $o->cache_set_current('query', $set-1);
  2115.         $o->cache_set_index('query', -1);
  2116.     }
  2117.  
  2118.     # Query is the same a a previous query on the same target:
  2119.     # Use cached results and set them as default
  2120.     elsif (grep { $_->{query} eq $query } $o->cache_sets('query')) {
  2121.         for (my $i=0; $i<$o->cache_sets('query'); $i++) {
  2122.         if ($o->cache_set('query', $i, 'query') eq $query) {
  2123.             $o->inform("Using cached query result set ", $i+1, ".\n");
  2124.             $o->cache_set_current('query', $i);
  2125.             my $set = $o->cache_set('query');
  2126.             $o->print_formatted($set);
  2127.         }
  2128.         }
  2129.     }
  2130.  
  2131.     # Perform a new query.
  2132.     else {
  2133.         my $num = find_index($target, 1, @targets);
  2134.         $o->inform("Querying target $num (");
  2135.         if (length($target) > 30) {
  2136.         $o->inform(substr($target, 0, 30), "...");
  2137.         }
  2138.         else {
  2139.         $o->inform($target);
  2140.         }
  2141.         $o->inform(")\n");
  2142.  
  2143.         my $res = PPM::UI::query($target, $query, $case);
  2144.         unless ($res->ok) {
  2145.         $o->inform($res->msg);
  2146.         return;
  2147.         }
  2148.         my @matches = $res->result_l;
  2149.         if (@matches) {
  2150.         $o->cache_set_add('query', $query, \@matches);
  2151.         $o->cache_set_current('query', scalar($o->cache_sets('query')) - 1);
  2152.         my @set = $o->cache_set('query');
  2153.         $o->print_formatted(\@set);
  2154.         }
  2155.         else {
  2156.         $o->warn("No matches for '$query'; see 'help query'.\n");
  2157.         }
  2158.     }
  2159.     }
  2160.     1;
  2161. }
  2162.  
  2163. #============================================================================
  2164. # Properties:
  2165. # prop        # describes default installed package
  2166. # prop <\d+>    # describes numbered installed package
  2167. # prop <pkg>    # describes named installed package
  2168. # prop <url>    # describes installed package at location <url>
  2169. #============================================================================
  2170. sub smry_properties { "describes installed packages in detail" }
  2171. sub help_properties { <<'END' }
  2172. properties -- Describe Installed Packages
  2173.   Synopsis
  2174.      prop                    Describes default installed package
  2175.      prop <number>           Describes installed package <number>
  2176.      prop <range>            Describes a <range> of installed packages
  2177.      prop <package name>     Describes named installed package
  2178.      prop <url>              Describes installed package located at <url>
  2179.      prop <glob pattern>     Performs a new query using <glob pattern>
  2180.  
  2181.   Description
  2182.     The properties command is an verbose form of the describe command. In
  2183.     addition to summary information, properties will display the
  2184.     installation date and a URL showing the location of the package within
  2185.     the repository.
  2186.  
  2187.     If you specify the package as a URL, PPM determines the package name
  2188.     from the URL and searches for that.
  2189.  
  2190.     When the properties command is used with wildcard arguments, the text
  2191.     entered at the PPM prompt is passed to the query command.
  2192.  
  2193.     For example, typing 'properties libnet' will give you:
  2194.  
  2195.         ====================
  2196.             Name: libnet
  2197.          Version: 1.07.03
  2198.           Author: Graham Barr
  2199.            Title: libnet
  2200.         Abstract: Collection of Network protocol modules
  2201.         InstDate: Fri Oct  2 16:15:15 1998
  2202.         Location: http://ppm-ia.ActiveState.com/PPM/...
  2203.         ====================
  2204.  
  2205.   See Also
  2206.     describe
  2207. END
  2208. sub comp_properties {
  2209.     my $o = shift;
  2210.     my ($word, $line, $start) = @_;
  2211.  
  2212.     # If no query results
  2213.     my $n_results = scalar $o->cache_sets('query');
  2214.     my $n_current = $o->cache_set_current('query');
  2215.     unless ($n_results and bounded(0, $n_current, $n_results - 1)) {
  2216.     my $targ = $o->conf('target') or return ();
  2217.     my $r = PPM::UI::query($targ, '*');
  2218.     return () unless $r->ok;
  2219.     $o->cache_set_add('query', '*', $r->result);
  2220.     $o->cache_set_current('query', scalar($o->cache_sets('query')) - 1);
  2221.     }
  2222.     my @words = $o->line_parsed($line);
  2223.  
  2224.     # If the previous word isn't a number or the command, stop.
  2225.     return ()
  2226.       if ($#words > 0 and
  2227.       $words[$#words] !~ /^\d+/ and
  2228.       $start == length($line) or 
  2229.       $#words > 1);
  2230.  
  2231.     # This is the most optimistic list:
  2232.     my @results = $o->cache_set('query');
  2233.     my $npkgs = @results;
  2234.     my @compls = (1 .. $npkgs);
  2235.  
  2236.     # If the previous word is a number, return only other numbers:
  2237.     return $o->completions($word, \@compls)
  2238.       if ($words[$#words] =~ /^\d+/);
  2239.  
  2240.     # Either a number or the names of the packages
  2241.     push @compls, map { $_->name } @results;
  2242.     return $o->completions($word, \@compls);
  2243. }
  2244. sub run_properties {
  2245.     my $o = shift;
  2246.     my @args = @_;
  2247.     my $args = $args[0];
  2248.     trace(1, "PPM: properties @args\n");
  2249.  
  2250.     # Check for anything that looks like a query. If it does, send it
  2251.     # to query instead.
  2252.     my $query = $o->raw_args || join ' ', @args;
  2253.     $query ||= '';
  2254.     if ($query and not PPM::UI::is_pkg($args[0]) and not parse_range($query)) {
  2255.     $o->inform("Wildcards detected; using 'query' instead.\n");
  2256.     return $o->run('query', @_);
  2257.     }
  2258.     
  2259.     # No Args: describes current index of current result set, or 1.
  2260.     my $n_results = $o->cache_sets('query');
  2261.     my $n_current = $o->cache_set_current('query');
  2262.     my $ind = $o->cache_set_index('query');
  2263.     unless (@args) {
  2264.     unless ($n_results and bounded(0, $n_current, $n_results - 1)) {
  2265.         $o->inform("No query results to describe -- " . 
  2266.           "use 'query' to find a package.\n");
  2267.         return;
  2268.     }
  2269.     else {
  2270.         my @results = $o->cache_set('query');
  2271.         my $npkgs = @results;
  2272.         $o->inform("$SEP\n");
  2273.         if (bounded(0, $ind, $npkgs-1)) {
  2274.         my $n = $ind + 1;
  2275.         $o->inform("Package $n:\n");
  2276.         $o->describe_pkg($o->cache_entry('query', $ind));
  2277.         }
  2278.         else {
  2279.         $o->inform("Package 1:\n");
  2280.         $o->describe_pkg($results[0]);
  2281.         $o->cache_set_index('query', 0);
  2282.         }
  2283.         $o->inform("$SEP\n");
  2284.     }
  2285.     }
  2286.  
  2287.     # Args provided
  2288.     else {
  2289.  
  2290.     # Describe a particular number:
  2291.     if (my @r = parse_range(@args)) {
  2292.         unless ($n_results and bounded(0, $n_current, $n_results - 1)) {
  2293.         $o->inform("No query results to describe -- " . 
  2294.           "use 'query' to find a package.\n");
  2295.         return;
  2296.         }
  2297.         else {
  2298.         for my $n (@r) {
  2299.             my @results = $o->cache_set('query');
  2300.             my $npkgs = @results;
  2301.             $o->inform("$SEP\n");
  2302.             if (bounded(1, $n, $npkgs)) {
  2303.             $o->inform("Package $n:\n");
  2304.             $o->cache_set_index('query', $n-1);
  2305.             my $old = $o->cache_entry('query');
  2306.             my $prop =
  2307.               PPM::UI::properties($o->conf('target'), $old->name);
  2308.             unless ($prop->is_success) {
  2309.                 $o->warn($prop->msg);
  2310.                 next unless $prop->ok;
  2311.             }
  2312.             my ($pkg, $idate, $loc) = $prop->result_l;
  2313.             $o->describe_pkg($pkg,
  2314.                      [qw(InstDate Location)],
  2315.                      [$idate, $loc],
  2316.                     );
  2317.             }
  2318.             else {
  2319.             $o->inform("No such package $n in result set.\n");
  2320.             }
  2321.         }
  2322.         $o->inform("$SEP\n");
  2323.         }
  2324.     }
  2325.  
  2326.     # Query a particular package
  2327.     else {
  2328.         if ($o->conf('target')) {
  2329.         my $prop =
  2330.           PPM::UI::properties($o->conf('target'), $args);
  2331.         unless ($prop->is_success) {
  2332.             $o->warn($prop->msg);
  2333.             return unless $prop->ok;
  2334.         }
  2335.         my ($pkg, $idate, $loc) = $prop->result_l;
  2336.         my ($s, $index) = $o->cache_find('query', $args);
  2337.         $o->inform("$SEP\n") if $pkg;
  2338.         $o->describe_pkg($pkg,
  2339.                  [qw(InstDate Location)],
  2340.                  [$idate, $loc],
  2341.                 )
  2342.           if $pkg;
  2343.         $o->inform("$SEP\n") if $pkg;
  2344.         if ($index >= 0) {
  2345.             $o->cache_set_current('query', $s);
  2346.             $o->cache_set_index('query', $index);
  2347.         }
  2348.         elsif ($pkg) {
  2349.             $o->cache_set_add('query', $args[0], [$pkg]);
  2350.             my $last = $o->cache_sets('query') - 1;
  2351.             $o->cache_set_current('query', $last);
  2352.             $o->cache_set_index('query', 0);
  2353.         }
  2354.         $o->warn("Package '$args' not found; 'query' for it first.\n")
  2355.           and return unless $pkg;
  2356.         }
  2357.         else {
  2358.         # XXX: Change this output.
  2359.         $o->warn(
  2360.             "There are no targets installed.\n"
  2361.         );
  2362.         return;
  2363.         }
  2364.     }
  2365.     }
  2366.     1;
  2367. }
  2368.  
  2369. #============================================================================
  2370. # Uninstall:
  2371. # uninst    # removes default installed package
  2372. # uninst <\d+>    # removes specified package
  2373. # uninst <pkg>    # removes specified package
  2374. # uninst <url>    # removes the package located at <url>
  2375. #============================================================================
  2376. sub smry_uninstall { "uninstalls packages" }
  2377. sub help_uninstall { <<'END' }
  2378. remove, uninstall -- Uninstalls Installed Packages
  2379.   Synopsis
  2380.      remove              Deletes default installed package
  2381.      remove <number>     Deletes installed package <number>
  2382.      remove <range>      Deletes a <range> of installed packages
  2383.      remove <name>       Deletes a packages by a specific name
  2384.      remove <url>        Deletes the package located at <url>
  2385.  
  2386.   Description
  2387.     The remove and uninstall commands function identically. They are used to
  2388.     delete packages from the current target (specified using the target
  2389.     command). If profile tracking is enabled, (see 'help profile') the
  2390.     current PPM profile on ASPN will be updated.
  2391.  
  2392.     Packages can be removed by package name, by their numerical listing, or
  2393.     by specifying a URL to a PPD file. For example:
  2394.  
  2395.         remove XML-DOM
  2396.  
  2397.     will delete the XML-DOM package from the target.
  2398.  
  2399.     To remove package by number:
  2400.  
  2401.         remove 6
  2402.  
  2403.     and the sixth package in your current query will be removed. If no
  2404.     queries have been run in the current PPM session, you will be prompted
  2405.     to use a query to find a package before deleting it. Remember that
  2406.     removing packages clears all previous query requests, since the
  2407.     numerical sequence stored in any query will no longer be true once
  2408.     package(s) have been removed.
  2409.  
  2410.     Packages can also be removed in groups. For example:
  2411.  
  2412.         remove 4-7
  2413.  
  2414.     will delete packages 4, 5, 6, and 7 from your target. You can also skip
  2415.     packages:
  2416.  
  2417.         remove 3-5, 7
  2418.  
  2419.     this will delete packages 3, 4, 5 and 7, but will leave 6 intact.
  2420.     Remember to run a new query whenever you remove a package from your
  2421.     target.
  2422.  
  2423.     If you specify the package as a URL, PPM determines the package name
  2424.     from the URL and removes that.
  2425.  
  2426.     Please note that wildcards like "*" or "?" cannot be used with the
  2427.     remove command.
  2428.  
  2429.   See Also
  2430.     profile
  2431. END
  2432. sub comp_uninstall { goto &comp_properties; }
  2433. sub run_uninstall {
  2434.     my $o = shift;
  2435.     my @args = @_;
  2436.     trace(1, "PPM: uninstall @args\n");
  2437.  
  2438.     # Get the force option:
  2439.     my ($force);
  2440.     {
  2441.     local @ARGV = @args;
  2442.     GetOptions(
  2443.         'force!' => \$force,
  2444.     );
  2445.     @args = @ARGV;
  2446.     }
  2447.     
  2448.     my $args = $args[0];
  2449.  
  2450.     # No Args -- removes default package
  2451.     my $n_results = $o->cache_sets('query');
  2452.     my $n_current = $o->cache_set_current('query');
  2453.     my $ind = $o->cache_set_index('query');
  2454.     unless (@args) {
  2455.     unless ($n_results and bounded(0, $n_current, $n_results - 1)) {
  2456.         $o->warn("No query results to uninstall -- " . 
  2457.           "use 'query' to find a package.\n");
  2458.         return;
  2459.     }
  2460.     else {
  2461.         my @results = $o->cache_set('query');
  2462.         if (bounded(0, $ind, $#results)) {
  2463.         my $n = $ind + 1;
  2464.         $o->inform("Package $n:\n");
  2465.         $o->remove_pkg($o->cache_entry('query', $ind)->name, $force);
  2466.         }
  2467.         else {
  2468.         $o->inform("Package 1:\n");
  2469.         $o->remove_pkg($o->cache_entry('query', 0)->name, $force);
  2470.         }
  2471.     }
  2472.     }
  2473.  
  2474.     # Args provided
  2475.     else {
  2476.     # Uninstall a particular number:
  2477.     if (my @r = parse_range(@args)) {
  2478.         unless ($n_results and bounded(0, $n_current, $n_results - 1)) {
  2479.         $o->warn("No query results to uninstall -- " . 
  2480.           "use 'query' to find a package.\n");
  2481.         return;
  2482.         }
  2483.         else {
  2484.         my @results = $o->cache_set('query');
  2485.         my $npkgs = @results;
  2486.         my $ok = 0;
  2487.         for my $n (@r) {
  2488.             if (bounded(1, $n, $npkgs)) {
  2489.             $o->inform("Package $n:\n");
  2490.             $ok |=
  2491.               $o->remove_pkg($o->cache_entry('query', $n-1)->name,
  2492.                      $force, 1);
  2493.             }
  2494.             else {
  2495.             $o->warn("No such package $n in result set.\n");
  2496.             }
  2497.         }
  2498.         $o->cache_clear('query') if $ok;
  2499.         }
  2500.     }
  2501.  
  2502.     # Uninstall a particular package
  2503.     else {
  2504.         if ($o->conf('target')) {
  2505.         $o->remove_pkg($_, $force) for @args;
  2506.         }
  2507.         else {
  2508.         print
  2509.           "No targets -- use 'rep add' to add a target.\n";
  2510.         return;
  2511.         }
  2512.     }
  2513.     }
  2514.     1;
  2515. }
  2516. sub alias_uninstall { qw(remove) }
  2517.  
  2518. #============================================================================
  2519. # Settings:
  2520. #============================================================================
  2521. my (%lib_keys, @ui_keys);
  2522. my (@path_keys, @boolean_keys, @integer_keys);
  2523. my (%cache_clear_keys);
  2524. BEGIN {
  2525.     %lib_keys = ('download-chunksize' => 'downloadbytes',
  2526.         'tempdir' => 'tempdir',
  2527.         'trace-file' => 'tracefile',
  2528.         'trace-level' => 'tracelvl',
  2529.         'profile-track' => 'profile_enable',
  2530.         );
  2531.     @ui_keys = qw(
  2532.     case-sensitivity
  2533.     pager
  2534.     fields
  2535.     follow-install
  2536.     force-install
  2537.     prompt-context
  2538.     prompt-slotsize
  2539.     prompt-verbose
  2540.     sort-field
  2541.     verbose-startup
  2542.  
  2543.     install-verbose
  2544.     upgrade-verbose
  2545.     remove-verbose
  2546.     );
  2547.     @boolean_keys = qw(case-sensitivity force-install follow-install
  2548.                prompt-context prompt-verbose profile-track
  2549.                verbose-startup install-verbose upgrade-verbose
  2550.                remove-verbose
  2551.               );
  2552.     @integer_keys = qw(download-chunksize prompt-slotsize trace-level);
  2553.     @path_keys = qw(tempdir pager trace-file);
  2554.     @cache_clear_keys{qw/
  2555.     case-sensitivity
  2556.     /} = ();
  2557. }
  2558. sub settings_getkeys {
  2559.     my $o = shift;
  2560.     my @keys = @ui_keys;
  2561.     push @keys, keys %lib_keys;
  2562.     @keys;
  2563. }
  2564. sub settings_getvals {
  2565.     my $o = shift;
  2566.     my @vals;
  2567.     push @vals, $o->settings_getkey($_) for $o->settings_getkeys;
  2568.     @vals;
  2569. }
  2570.  
  2571. sub conf {
  2572.     my $o   = shift;
  2573.     my $key = shift;
  2574.     my $val = shift;
  2575.     my $un  = shift;
  2576.     return $o->settings_setkey($key, $val, $un) if defined $val;
  2577.     return $o->settings_getkey($key);
  2578. }
  2579.  
  2580. sub settings_getkey {
  2581.     my $o = shift;
  2582.     my $key = shift;
  2583.     return PPM::UI::config_get($lib_keys{$key})->result if $lib_keys{$key};
  2584.     return $o->{SHELL}{conf}{DATA}{$key};
  2585. }
  2586. sub settings_setkey {
  2587.     my $o = shift;
  2588.     my ($key, $val, $un) = @_;
  2589.     if (grep { $key eq $_ } @boolean_keys) {
  2590.     $val = 0 if $un;
  2591.     unless ($val =~ /^\d+$/ && ($val == 0 || $val == 1)) {
  2592.         $o->warn(<<END);
  2593. Setting '$key' must be boolean: '0' or '1'. See 'help settings'.
  2594. END
  2595.         return;
  2596.     }
  2597.     }
  2598.     elsif (grep { $key eq $_ } @integer_keys) {
  2599.     $val = 0 if $un;
  2600.     unless ($val =~ /^\d+$/) {
  2601.         $o->warn(<<END);
  2602. Setting '$key' must be numeric. See 'help settings'.
  2603. END
  2604.         return;
  2605.     }
  2606.     }
  2607.     elsif ($key eq 'sort-field') {
  2608.     $val = 'name' if $un;
  2609.     my @fields = sort_fields();
  2610.     unless (grep { lc($val) eq $_ } @fields) {
  2611.         $o->warn(<<END);
  2612. Error setting '$key' to '$val': should be one of:
  2613. @fields.
  2614. END
  2615.         return;
  2616.     }
  2617.     else {
  2618.         $val = lc($val);
  2619.         $o->cache_set_index('search', -1); # invalidates current indices.
  2620.         $o->cache_set_index('query', -1);
  2621.     }
  2622.     }
  2623.     elsif ($key eq 'fields') {
  2624.     $val = 'name version abstract' if $un;
  2625.     my @fields = sort_fields();
  2626.     my @vals = split ' ', $val;
  2627.     for my $v (@vals) {
  2628.         unless (grep { lc $v eq lc $_ } @fields) {
  2629.         $o->warn(<<END);
  2630. Error adding field '$v': should be one of:
  2631. @fields.
  2632. END
  2633.         return;
  2634.         }
  2635.     }
  2636.     $val = lc $val;
  2637.     }
  2638.  
  2639.     if ($un and $key eq 'tempdir') {
  2640.     $o->warn("Can't unset 'tempdir': use 'set' instead.\n");
  2641.     return;
  2642.     }
  2643.  
  2644.     # Check for any cache-clearing that needs to happen:
  2645.     if (exists $cache_clear_keys{$key}) {
  2646.     $o->cache_clear('search');
  2647.     $o->cache_clear('query');
  2648.     }
  2649.  
  2650.     if ($lib_keys{$key}) { PPM::UI::config_set($lib_keys{$key}, $val) }
  2651.     else {
  2652.     $o->{SHELL}{conf}{DATA}{$key} = $val;
  2653.     $o->{SHELL}{conf}->save;
  2654.     }
  2655.     $o->inform(<<END);
  2656. Setting '$key' set to '$val'.
  2657. END
  2658. }
  2659.  
  2660. sub smry_settings { "view or set PPM options" }
  2661. sub help_settings { <<'END' }
  2662. settings -- View or Set PPM Settings
  2663.   Synopsis
  2664.      set                 Displays current settings
  2665.      set <name>          Displays the current setting of the given <name>
  2666.      set <name> <value>  Sets <name> to <value>
  2667.      unset <name>        Sets <name> to a "false" value: '0' for boolean
  2668.                          Settings, '' for others.
  2669.  
  2670.   Description
  2671.     The settings command is used to configure the default PPM environment.
  2672.     Settings such as the number of lines displayed per page,
  2673.     case-sensitivity, and the log file are configured using the settings
  2674.     command.
  2675.  
  2676.     Setting names may be abbreviated to uniqueness. For example, instead of
  2677.     typing 'case-sensitivity', you may type 'case'.
  2678.  
  2679.     Available settings:
  2680.  
  2681.      NAME                VALUE           DESCRIPTION
  2682.      case-sensitivity    1 or 0      If 1, searches and queries are
  2683.                                      case-sensitive.
  2684.  
  2685.      download-chunksize  integer     If this is set to a positive,
  2686.                                      non-zero integer, PPM updates the
  2687.                                      status after "integer" of bytes
  2688.                                      transferred during an install or
  2689.                                      upgrade.
  2690.  
  2691.      fields              fields      A space-separated list of fields to 
  2692.                                      display in the search results. Valid
  2693.                                      fields are:
  2694.  
  2695.                                        ABSTRACT
  2696.                                        AUTHOR
  2697.                                        NAME
  2698.                                        REPOSITORY
  2699.                                        TITLE
  2700.                                        VERSION
  2701.  
  2702.                                      Usually, NAME and TITLE have the same
  2703.                                      content.
  2704.  
  2705.      follow-install      1 or 0      See 'help install' for details.
  2706.  
  2707.      force-install       1 or 0      See 'help install' for details.
  2708.  
  2709.      install-verbose     1 or 0      If 0, suppresses most output when
  2710.                                      installing packages. If 1, PPM prints
  2711.                                      each file as it is installed.
  2712.  
  2713.      pager               path        The path to an external pager program
  2714.                                      used to page long displays. If blank,
  2715.                                      or set to 'internal', the internal
  2716.                                      pager is used. If 'none', paging
  2717.                                      is disabled.
  2718.  
  2719.      profile-track       1 or 0      If 1, PPM arranges to have the 
  2720.                                      ASPN server track your PPM profile. 
  2721.                                      This means that every time your install
  2722.                                      or remove a package, your profile is
  2723.                                      updated on the server. If 0, you must
  2724.                                      manually save your profile using
  2725.                                      'profile save'.
  2726.  
  2727.      prompt-context      1 or 0      If 1, enables the prompt to change
  2728.                                      based on the current state of PPM, i.e
  2729.                                      showing current target, query, etc.
  2730.  
  2731.      prompt-slotsize     integer     If prompt-verbose is 1, this defines
  2732.                                      the width of each slot in the prompt.
  2733.                                      For instance, 4 means to use 4 
  2734.                                      character-wide slots.
  2735.  
  2736.      prompt-verbose      1 or 0      If 0, uses numbers to represent the
  2737.                                      context in the prompt; much shorter.
  2738.                                      If prompt-context is set to 0, there
  2739.                                      will be no visible difference in the
  2740.                                      'prompt-verbose' settings.
  2741.  
  2742.      remove-verbose      1 or 0      If 0, suppresses most output when
  2743.                                      removing packages. If 1, prints the
  2744.                                      name of each file as it is removed.
  2745.  
  2746.      sort-field          field       The field by which to sort search and
  2747.                                      query results. Valid fields are
  2748.                                      ABSTRACT, AUTHOR, NAME, TITLE
  2749.                                      and VERSION.
  2750.  
  2751.      tempdir             path        A temporary directory into which
  2752.                                      packages are downloaded and expanded
  2753.                                      during 'install' and 'upgrade'.
  2754.  
  2755.      trace-file          path        A file to which PPM will write tracing
  2756.                                      information.
  2757.  
  2758.      trace-level         integer     If 0 or negative, tracing is disabled.
  2759.                                      Positive, non-zero integers result in
  2760.                                      tracing information being written to
  2761.                                      'trace-file'. Higher settings of
  2762.                                      'trace-level' result in more trace
  2763.                                      information.
  2764.  
  2765.      upgrade-verbose     1 or 0      If 0, suppresses most output when
  2766.                                      upgrading packages. If 1, prints the
  2767.                                      name of each file as it is upgraded.
  2768.  
  2769.     For information about migrating options used by previous versions of
  2770.     PPM, see 'help ppm_migration'.
  2771.  
  2772.     When you assign a value to a setting, PPM saves the configuration.
  2773.     Therefore, setting values persist across sessions.
  2774. END
  2775. sub comp_settings {
  2776.     my $o = shift;
  2777.     my ($word, $line, $start) = @_;
  2778.     my @words = $o->line_parsed($line);
  2779.  
  2780.     # To please the users of Bash, we'll allow 'set foo=bar' to work as well,
  2781.     # since it's really easy to do:
  2782.     if (defined $words[1] and $words[1] =~ /=/ and not defined $words[2]) {
  2783.     my @kv = split '=', $words[1];
  2784.     splice(@words, 1, 1, @kv);
  2785.     }
  2786.     my $words = @words;
  2787.     my @compls;
  2788.  
  2789.     # return the keys when we're completing the second word
  2790.     if ($words == 1 or $words == 2 and $start != length($line)) {
  2791.     @compls = $o->settings_getkeys();
  2792.     return $o->completions($word, \@compls);
  2793.     }
  2794.  
  2795.     # Return no completions for 'unset'.
  2796.     return () if matches($o->{API}{cmd}{run}{name}, 'u|nset');
  2797.  
  2798.     # provide intelligent completion for arguments:
  2799.     if ($words ==2 or $words == 3 and $start != length($line)) {
  2800.     # Completion for boolean values:
  2801.     my @bool = $o->completions($words[1], \@boolean_keys);
  2802.     my @path = $o->completions($words[1], \@path_keys);
  2803.     if (@bool == 1) {
  2804.         return $o->completions($word, [0, 1]);
  2805.     }
  2806.     elsif (@path == 1) {
  2807.         @compls = readline::rl_filename_list($word);
  2808.         return $o->completions($word, \@compls);
  2809.     }
  2810.     elsif (matches($words[1], 's|ort-field')) {
  2811.         @compls = sort_fields();
  2812.         return $o->completions(lc($word), \@compls);
  2813.     }
  2814.     }
  2815.  
  2816.     # Don't complete for anything else.
  2817.     ()
  2818. }
  2819. sub run_settings {
  2820.     my $o = shift;
  2821.     my @args = @_;
  2822.     my $key = $args[0];
  2823.     my $val = $args[1];
  2824.  
  2825.     # To please the users of Bash, we'll allow 'set foo=bar' to work as well,
  2826.     # since it's really easy to do:
  2827.     if (defined $key and $key =~ /=/ and not defined $val) {
  2828.     ($key, $val) = split '=', $key;
  2829.     }
  2830.  
  2831.     trace(1, "PPM: settings @args\n");
  2832.     my $unset = matches($o->{API}{cmd}{run}{name}, 'u|nset');
  2833.     my @stuff = $o->completions($key, [$o->settings_getkeys()])
  2834.       if $key;
  2835.     my $fullkey = $stuff[0] if @stuff == 1;
  2836.     if (defined $key and defined $val) {
  2837.     # validate the key:
  2838.     unless ($fullkey) {
  2839.         $key = '' unless defined $key;
  2840.         $o->warn("Unknown or ambiguous setting '$key'. See 'help settings'.\n");
  2841.         return;
  2842.     }
  2843.     $o->conf($fullkey, $val, $unset);
  2844.     }
  2845.     elsif (defined $key) {
  2846.     unless ($fullkey) {
  2847.         $key = '' unless defined $key;
  2848.         $o->warn("Unknown or ambiguous setting '$key'. See 'help settings'.\n");
  2849.         return;
  2850.     }
  2851.     if ($unset) {
  2852.         $o->conf($fullkey, '', $unset);
  2853.     }
  2854.     else {
  2855.         my $val = $o->conf($fullkey);
  2856.         $o->print_pairs([$fullkey], [$val]);
  2857.     }
  2858.     }
  2859.     else {
  2860.     my (@keys, @vals);
  2861.     @keys = $o->settings_getkeys();
  2862.     @vals = $o->settings_getvals();
  2863.     my %k;
  2864.     @k{@keys} = @vals;
  2865.     @keys = sort keys %k;
  2866.     @vals = map { $k{$_} } @keys;
  2867.     $o->print_pairs(\@keys, \@vals);
  2868.     }
  2869. }
  2870. sub alias_settings { qw(unset) }
  2871.  
  2872. sub help_help { <<'END' }
  2873. help -- General help, or help on specific commands.
  2874.   Synopsis
  2875.      help                Lists available commands and help topics
  2876.      help 'command'      Lists detailed help about a specific command
  2877.  
  2878.   Description
  2879.     The help command provides a brief description of the commands available
  2880.     within PPM. For help on a specific command, enter help followed by the
  2881.     command name. For example, enter help settings or help set for a
  2882.     detailed description of the settings command.
  2883.  
  2884.     There are some extra help topics built into PPM. They can be accessed
  2885.     within the PPM environment as follows:
  2886.  
  2887.       help ppm_migration
  2888.  
  2889.     shows more details about the changes from previous versions of PPM
  2890.  
  2891.       help quickstart
  2892.  
  2893.     an easy-to-follow guide to getting started with PPM
  2894.  
  2895.       help prompt
  2896.  
  2897.     provides a detailed explanation about the PPM prompt
  2898. END
  2899.  
  2900. #============================================================================
  2901. # Version:
  2902. #============================================================================
  2903. sub smry_version { "displays the PPM version ($VERSION)" }
  2904. sub help_version { <<'END' }
  2905. version -- print the name and version of PPM.
  2906.     Prints the name and version of PPM3.
  2907. END
  2908. sub comp_version {()}
  2909. sub run_version {
  2910.     my $o = shift;
  2911.     if ($o->mode eq 'SHELL') {
  2912.     $o->inform("$NAME version $VERSION\n");
  2913.     }
  2914.     else {
  2915.     $o->inform("$SHORT_NAME $VERSION\n");
  2916.     }
  2917.     1;
  2918. }
  2919.  
  2920. #============================================================================
  2921. # Exit:
  2922. #============================================================================
  2923. sub help_exit { <<'END' }
  2924. exit, q, quit -- Exit the program
  2925.   Synopsis
  2926.      exit                Exit
  2927.      quit                Exit
  2928.      q                   Exit
  2929.      q <query>           Perform a new query (shortcut for query)
  2930.  
  2931.   Description
  2932.     When you leave the PPM environment, the current settings are saved.
  2933. END
  2934. sub comp_exit {
  2935.     my $o = shift;
  2936.     return &comp_query
  2937.     if $o->{API}{cmd}{run}{name} eq 'q' and @_;
  2938.     ();
  2939. }
  2940. sub run_exit {
  2941.     my $o = shift;
  2942.     # Special case: 'q' with no arguments should mean 'quit', but 'q' with
  2943.     # arguments should mean 'query'.
  2944.     if ($o->{API}{cmd}{run}{name} eq 'q' and @_) {
  2945.     return $o->run('query', @_);
  2946.     }
  2947.     $o->stoploop;
  2948. }
  2949. sub alias_exit { qw(quit q) }
  2950.  
  2951. #============================================================================
  2952. # Upgrade
  2953. # upgrade    # lists upgrades available
  2954. # upgrade <\d+> # upgrades specified package
  2955. # upgrade<pkg>    # upgrades named package
  2956. #============================================================================
  2957. sub smry_upgrade { "shows availables upgrades for installed packages" }
  2958. sub help_upgrade { <<'END' }
  2959. upgrade -- List or install available upgrades
  2960.   Synopsis
  2961.      upgrade [*]         Lists upgrades available for all installed packages
  2962.      upgrade <number>    Upgrades installed package <number>
  2963.      upgrade <range>     Upgrades a <range> of installed packages
  2964.      upgrade <package>   Upgrades the named <package>
  2965.  
  2966.   Description
  2967.     The upgrade command lists package upgrades that are available on the
  2968.     current repository for packages installed on your system. To install
  2969.     available upgrades, use the '--install' option.
  2970.  
  2971.     If profile tracking is enabled, (see 'help profile'), your profile will
  2972.     be updated to reflect changes to any packages which are upgraded.
  2973.  
  2974.     There are several modifiers to the upgrade command:
  2975.  
  2976.     -install
  2977.         Installs, rather than lists, available upgrades
  2978.  
  2979.     -precious
  2980.         Allows upgrading of "precious" packages
  2981.  
  2982.     -force
  2983.         See 'help install'
  2984.  
  2985.     -follow
  2986.         See 'help install'
  2987.  
  2988.     By default, 'upgrade' typed by itself only lists the available upgrades.
  2989.     To actually install all available upgrades, enter
  2990.  
  2991.         upgrade -install
  2992.  
  2993.     To enable upgrading "precious" packages, enter
  2994.  
  2995.         upgrade -install -precious
  2996.  
  2997.   See Also
  2998.     profile
  2999. END
  3000. sub comp_upgrade { goto &comp_properties; }
  3001. sub run_upgrade {
  3002.     my $o = shift;
  3003.     my @args = @_;
  3004.     trace(1, "PPM: upgrade @args\n");
  3005.  
  3006.     # Get options:
  3007.     my %opts = (
  3008.     install => 0,
  3009.     doprecious => 0,
  3010.     dryrun => 0,
  3011.     force => $o->conf('force-install'),
  3012.     follow => $o->conf('follow-install'),
  3013.     );
  3014.     {
  3015.     local @ARGV = @args;
  3016.     GetOptions(install => \$opts{install},
  3017.            precious => \$opts{doprecious},
  3018.            'force!' => \$opts{force},
  3019.            'follow!' => \$opts{follow},
  3020.            dryrun => \$opts{dryrun},
  3021.           );
  3022.     @args = @ARGV;
  3023.     }
  3024.  
  3025.     my $rlist = [$o->reps_on];
  3026.     my $targ  = $o->conf('target');
  3027.     my @pkgs;
  3028.  
  3029.     # Allow 'upgrade *';
  3030.     @args = grep { $_ ne '*' } @args;
  3031.  
  3032.     # List upgrades for a particular package
  3033.     if (@args) {
  3034.     my $pkg = $args[0];
  3035.     my @n = parse_range($o->raw_args);
  3036.     for my $n (@n) {
  3037.         my $ppd = $o->cache_entry('query', $n-1);
  3038.         unless($ppd) {
  3039.         $o->warn("No such query result '$pkg' in result set.\n");
  3040.         return;
  3041.         }
  3042.         else {
  3043.         push @pkgs, $ppd;
  3044.         }
  3045.     }
  3046.  
  3047.     # The name of the package:
  3048.     unless (@n) {
  3049.         my $ppd = PPM::UI::properties($o->conf('target'), $pkg);
  3050.         unless ($ppd->is_success) {
  3051.         $o->warn($ppd->msg);
  3052.         return unless $ppd->ok;
  3053.         }
  3054.         my $real_ppd = ($ppd->result_l)[0];
  3055.         push @pkgs, $real_ppd;
  3056.     }
  3057.     }
  3058.     # List upgrades for all packages
  3059.     else {
  3060.     @pkgs = PPM::UI::query($targ, '*', 0)->result_l;
  3061.     @pkgs = $o->sort_pkgs($o->conf('sort-field'), @pkgs);
  3062.     }
  3063.  
  3064.     my $verify = PPM::UI::verify_pkgs($rlist, $targ, @pkgs);
  3065.     unless ($verify->is_success) {
  3066.     $o->error("Error verifying packages: ", $verify->msg_raw, "\n");
  3067.     return;
  3068.     }
  3069.     my %bypackage;
  3070.     for my $result ($verify->result_l) {
  3071.     next unless $result->is_success; # ignore unfound packages
  3072.     my ($uptodate, $server_pkg, $inst_pkg, $b, $p) = $result->result_l;
  3073.     my $name = $server_pkg->name;
  3074.     my $nver = $server_pkg->version;
  3075.     my $over = $inst_pkg->version;
  3076.     my $repo = $server_pkg->repository->name;
  3077.     $bypackage{$name}{$repo} = {
  3078.         uptodate => $uptodate,
  3079.         oldver => $over,
  3080.         newver => $nver,
  3081.         repo => $repo,
  3082.         bundled => $b,
  3083.         precious => $p,
  3084.         pkg => $server_pkg,
  3085.     };
  3086.     }
  3087.     for my $pkg (sort keys %bypackage) {
  3088.     my $default;
  3089.     my @updates;
  3090.     my $p = $bypackage{$pkg};
  3091.     for my $rep (sort { $p->{$b}{newver} cmp $p->{$a}{newver} } keys %$p) {
  3092.         my $tmp = $default = $p->{$rep};
  3093.         push @updates, [@$tmp{qw(oldver newver repo)}] unless $tmp->{uptodate};
  3094.     }
  3095.     my $upgrade = $opts{install} ? 1 : 0;
  3096.         for (@updates) {
  3097.         $o->inform("$pkg $_->[0]: new version $_->[1] available in $_->[2]\n");
  3098.     }
  3099.     unless (@updates) {
  3100.         $o->inform("$pkg $default->{oldver}: up to date.\n");
  3101.         $upgrade &= $opts{force};
  3102.     }
  3103.     if ($upgrade) {
  3104.         my @k = keys %$p;
  3105.         my $ask = (@updates > 1 or @k > 1 and !@updates);
  3106.         if ($ask) {
  3107.         # Which one do they want to install?
  3108.         $o->inform(<<MANY);
  3109.  
  3110.    Note: $pkg version $default->{oldver} is available from more than one place.
  3111.    Which repository would you like to upgrade from?
  3112.  
  3113. MANY
  3114.         my @repos = map { $_->[2] } @updates;
  3115.         $o->print_pairs([ 1 .. @repos ], \@repos, '. ');
  3116.         $o->inform("\n");
  3117.         my $rep = $o->prompt(
  3118.             "Repository? [$default->{repo}] ",
  3119.             $default->{repo},
  3120.             [ 1 .. @repos, @repos ],
  3121.         );
  3122.         $rep = $repos[$rep - 1] if $rep =~ /^\d+$/;
  3123.         $default = $p->{$rep};
  3124.         }
  3125.         elsif (!@updates) {
  3126.         ($default) = values %$p;
  3127.         }
  3128.         if (not $default->{precious} or $default->{precious} && $opts{doprecious}) {
  3129.         $o->upgrade_pkg($default->{pkg}, \%opts);
  3130.         }
  3131.         else {
  3132.         $o->warn(<<END);
  3133. Use '-precious' to force precious packages to be upgraded.
  3134. END
  3135.         }
  3136.     }
  3137.     }
  3138.     1;
  3139. }
  3140.  
  3141. #============================================================================
  3142. # Profile:
  3143. # profile        # lists the profiles available on the repository
  3144. # profile N        # switches profiles
  3145. # profile add "name"    # adds a new profile
  3146. # profile delete N    # deletes the given profile
  3147. # profile describe N    # describes the given profile
  3148. # profile save        # saves the current state to the current profile
  3149. # profile restore    # restores the current profile
  3150. # profile rename    # renames the given profile
  3151. #============================================================================
  3152. sub smry_profiles { "manage PPM profiles" }
  3153. sub help_profiles { <<'END' }
  3154. profile -- Manage PPM Profiles
  3155.   Synopsis
  3156.      profile                     Lists profiles available on the repository
  3157.      profile <num>               Switches to the given profile
  3158.      profile add <name>          Creates a new profile on the repository
  3159.      profile delete <name or num>
  3160.                                  Deletes the given profile
  3161.      profile describe [name or num]
  3162.                                  Describes the current or given profile
  3163.      profile save                Saves the client state to the current profile
  3164.      profile restore             Restores the current profile
  3165.      profile rename <name or num> <name>
  3166.                                  Renames the given profile to <name>
  3167.  
  3168.   Description
  3169.     Profiles store information about packages that are installed on your
  3170.     system. If the 'profile-track' setting is enabled, your ASPN Profile
  3171.     will be updated with information about installed packages. Profiles
  3172.     allow you to easily migrate, reinstall, upgrade or restore PPM packages
  3173.     in one or more locations.
  3174.  
  3175.     To use profiles, you must have a license for ASPN. For license
  3176.     information, see http://www.ActiveState.com/ASPN/About. Disable profile
  3177.     tracking by setting 'profile-track=0'.
  3178. END
  3179. sub comp_profiles {
  3180.     my $o = shift;
  3181.     my ($word, $line, $start) = @_;
  3182.     my @words = $o->line_parsed($line);
  3183.     my $words = scalar @words;
  3184.     my @profs = PPM::UI::profile_list();
  3185.     my @cmds = ('add', 'delete', 'describe', 'save', 'restore', 'rename');
  3186.  
  3187.     if ($words == 1 or $words == 2 and $start != length($line)) {
  3188.     my @compls = (@cmds, 1 .. scalar @profs);
  3189.     return $o->completions($word, \@compls);
  3190.     }
  3191.     if ($words == 2 or $words == 3 and $start != length($line)) {
  3192.     return ()
  3193.       if ($o->completions($words[1], [qw(add save restore)])==1);
  3194.     return $o->completions($word, [1 .. scalar @profs])
  3195.       if ($o->completions($words[1], [qw(delete describe rename)])==1);
  3196.     }
  3197.     ();
  3198. }
  3199. sub run_profiles {
  3200.     my $o = shift;
  3201.     my @args = @_;
  3202.     trace(1, "PPM: profile @args\n");
  3203.  
  3204.     my $ok = PPM::UI::profile_list();
  3205.     unless ($ok->is_success) {
  3206.     $o->warn($ok->msg);
  3207.     return unless $ok->ok;
  3208.     }
  3209.     my @profiles = dictsort $ok->result_l;
  3210.     $ok = PPM::UI::profile_get();
  3211.     unless ($ok->is_success) {
  3212.     $o->warn($ok->msg);
  3213.     return unless $ok->ok;
  3214.     }
  3215.     my $profile = $ok->result;
  3216.     my $which = find_index($profile, 0, @profiles);
  3217.     if ($which < 0 and @profiles) {
  3218.     $profile = $profiles[0];
  3219.     PPM::UI::profile_set($profile);
  3220.     }
  3221.  
  3222.     if (@args) {
  3223.     # Switch to profile N:
  3224.     if ($args[0] =~ /^\d+$/) {
  3225.         my $num = $args[0];
  3226.         if (bounded(1, $num, scalar @profiles)) {
  3227.         my $profile = $profiles[$num-1];
  3228.         PPM::UI::profile_set($profile);
  3229.         }
  3230.         else {
  3231.         $o->warn("No such profile number '$num'.\n");
  3232.         return;
  3233.         }
  3234.     }
  3235.  
  3236.     # Describe profile N:
  3237.     elsif (matches($args[0], "des|cribe")) {
  3238.         my $num =     $args[1] =~ /^\d+$/ ? $args[1] :
  3239.             do {
  3240.                 my $n = find_index($args[1], 1, @profiles);
  3241.                 if ($n < 1) {
  3242.                 $o->warn("No such profile '$args[1]'.\n");
  3243.                 return;
  3244.                 }
  3245.                 $n;
  3246.             } if defined $args[1];
  3247.         my $prof;
  3248.         if (defined $num and $num =~ /^\d+$/) {
  3249.         if (bounded(1, $num, scalar @profiles)) {
  3250.             $prof = $profiles[$num - 1];
  3251.         }
  3252.         else {
  3253.             $o->warn("No such profile number '$num'.\n");
  3254.             return;
  3255.         }
  3256.         }
  3257.         elsif (defined $num) {
  3258.         $o->warn("Argument to '$args[0]' must be numeric; see 'help profile'.\n");
  3259.         return;
  3260.         }
  3261.         else {
  3262.         $prof = $profile;
  3263.         }
  3264.  
  3265.         my $res = PPM::UI::profile_info($prof);
  3266.         $o->warn($res->msg) and return unless $res->ok;
  3267.         my @res = $res->result_l;
  3268.         {
  3269.         my ($pkg, $version, $target);
  3270.         my $picture = <<'END';
  3271. [[[[[[[[[[[[[[[[[[[    [[[[[[[[[[[    [[[[[[[[[[[[[[[[[[[[[[
  3272. END
  3273.         ($pkg, $version, $target) = qw(PACKAGE VERSION TARGET);
  3274.         my $text = '';
  3275.         $text .= form($picture, $pkg, $version, $target)
  3276.           if @res;
  3277.         for my $entity (@res) {
  3278.             ($pkg, $version, $target) = @$entity;
  3279.             $version = "[$version]";
  3280.             $text .= form($picture, $pkg, $version, $target);
  3281.         }
  3282.         if (@res) {
  3283.             $o->inform("Describing Profile '$prof':\n");
  3284.         }
  3285.         else {
  3286.             $o->inform("Profile '$prof' is empty.\n");
  3287.         }
  3288.         $o->page($text);
  3289.         }
  3290.         return 1;
  3291.     }
  3292.  
  3293.     # Add a profile "name":
  3294.     elsif (matches($args[0], "a|dd")) {
  3295.         my $name = $args[1];
  3296.         if ($name) {
  3297.         # Note: do some heavy-duty error-checking; XXX
  3298.         PPM::UI::profile_add($name);
  3299.         PPM::UI::profile_save($name)
  3300.           if $o->conf('profile-track');
  3301.         PPM::UI::profile_set($name)
  3302.           unless $which >= 0;
  3303.         @profiles = PPM::UI::profile_list()->result_l;
  3304.         }
  3305.         else {
  3306.         $o->warn("Invalid use of 'add' command; see 'help profile'.\n");
  3307.         return;
  3308.         }
  3309.     }
  3310.  
  3311.     # Remove profile N:
  3312.     elsif (matches($args[0], "del|ete")) {
  3313.         my $num =    $args[1] =~ /^\d+$/ ? $args[1] :
  3314.             do {
  3315.                 my $n = find_index($args[1], 1, @profiles);
  3316.                 if ($n < 1) {
  3317.                 $o->inform("No such profile '$args[1]'.\n");
  3318.                 return;
  3319.                 }
  3320.                 $n;
  3321.             } if defined $args[1];
  3322.         if (defined $num and $num =~ /^\d+$/) {
  3323.         my $dead_profile = $profiles[$num-1];
  3324.         if (bounded(1, $num, scalar @profiles)) {
  3325.             PPM::UI::profile_del($dead_profile);
  3326.             @profiles = dictsort PPM::UI::profile_list()->result_l;
  3327.             if (@profiles and $dead_profile eq $profile) {
  3328.             $profile = $profiles[0];
  3329.             PPM::UI::profile_set($profile);
  3330.             }
  3331.             elsif (not @profiles) {
  3332.             $o->conf('profile-track', 0);
  3333.             PPM::UI::profile_set('');
  3334.             }
  3335.         }
  3336.         else {
  3337.             $o->warn("No such profile '$num'.\n");
  3338.             return;
  3339.         }
  3340.         }
  3341.         elsif (defined $num) {
  3342.         $o->warn(<<END);
  3343. Argument to '$args[0]' must be numeric; see 'help profile'.
  3344. END
  3345.         return;
  3346.         }
  3347.         else {
  3348.         $o->warn(<<END);
  3349. Invalid use of '$args[0]' command; see 'help profile'.
  3350. END
  3351.         return;
  3352. }
  3353.     }
  3354.  
  3355.     # Save current profile:
  3356.     elsif (matches($args[0], "s|ave")) {
  3357.         unless (@profiles) {
  3358.         $o->warn(<<END);
  3359. No profiles on the server. Use 'profile add' to add a profile.
  3360. END
  3361.         return;
  3362.         }
  3363.         unless ($which >= 0) {
  3364.         $o->warn(<<END);
  3365. No profile selected. Use 'profile <number>' to select a profile.
  3366. END
  3367.         return;
  3368.         }
  3369.         my $ok = PPM::UI::profile_save($profile);
  3370.         if ($ok->ok) {
  3371.         $o->inform("Profile '$profile' saved.\n");
  3372.         }
  3373.         else {
  3374.         $o->warn($ok->msg);
  3375.         return;
  3376.         }
  3377.         return 1;
  3378.     }
  3379.  
  3380.     # Rename profile:
  3381.     elsif (matches($args[0], "ren|ame")) {
  3382.         unless (@profiles) {
  3383.         $o->warn(<<END);
  3384. No profiles on the server. Use 'profile add' to add a profile.
  3385. END
  3386.         return;
  3387.         }
  3388.  
  3389.         # Determine the old name:
  3390.         my $num =    $args[1] =~ /^\d+$/ ? $args[1] :
  3391.             do {
  3392.                 my $n = find_index($args[1], 1, @profiles);
  3393.                 if ($n < 1) {
  3394.                 $o->warn("No such profile '$args[1]'.\n");
  3395.                 return;
  3396.                 };
  3397.                 $n;
  3398.             } if defined $args[1];
  3399.         my $oldprof;
  3400.         if (defined $num and $num =~ /^\d+$/) {
  3401.         if (bounded(1, $num, scalar @profiles)) {
  3402.             $oldprof = $profiles[$num - 1];
  3403.         }
  3404.         else {
  3405.             $o->warn("No such profile number '$num'.\n");
  3406.             return;
  3407.         }
  3408.         }
  3409.         elsif (defined $num) {
  3410.         $o->warn("Argument to '$args[0]' must be numeric; see 'help profile'.\n");
  3411.         return;
  3412.         }
  3413.         else {
  3414.         $o->warn("profile: invalid use of '$args[0]' command: see 'help profile'.\n");
  3415.         return;
  3416.         }
  3417.  
  3418.         # Validate the new name:
  3419.         my $newprof = $args[2];
  3420.         unless (defined $newprof and length($newprof)) {
  3421.         $newprof = '' unless defined $newprof;
  3422.         $o->warn(<<END);
  3423. Profile names must be non-empty: '$newprof' is not a valid name.
  3424. END
  3425.         return;
  3426.         }
  3427.  
  3428.         # Actually do it:
  3429.         my $ok = PPM::UI::profile_rename($oldprof, $newprof);
  3430.         unless ($ok->is_success) {
  3431.         $o->warn($ok->msg);
  3432.         return unless $ok->ok;
  3433.         }
  3434.         if ($profile eq $oldprof) {
  3435.         $profile = $newprof;
  3436.         PPM::UI::profile_set($profile);
  3437.         }
  3438.         @profiles = dictsort PPM::UI::profile_list()->result_l;
  3439.     }
  3440.  
  3441.     # Restore current profile:
  3442.     elsif (matches($args[0], "res|tore")) {
  3443.         unless (@profiles) {
  3444.         $o->warn(<<END);
  3445. No profiles on this server. Use 'profile add' to add a profile.
  3446. END
  3447.         return;
  3448.         }
  3449.         unless ($which >= 0) {
  3450.         $o->warn(<<END);
  3451. No profile selected. Use 'profile <number>' to select a profile.
  3452. END
  3453.         return;
  3454.         }
  3455.         my ($clean_packages, $dry) = (0, 0);
  3456.         my ($force, $follow) = (1, 0);
  3457.         {
  3458.         local @ARGV = @args;
  3459.         GetOptions('clean!' => \$clean_packages,
  3460.                'force!' => \$force,
  3461.                'follow!' => \$follow,
  3462.                'dryrun' => \$dry,
  3463.               );
  3464.         @args = @ARGV;
  3465.         }
  3466.         my $cb_inst = $dry ? \&dr_install : \&cb_install;
  3467.         my $cb_rm   = $dry ? \&dr_remove  : \&cb_remove ;
  3468.         my $ok = PPM::UI::profile_restore($profile, $cb_inst,
  3469.                           $cb_rm, $force, $follow,
  3470.                           $dry, $clean_packages);
  3471.         if ($ok->ok) {
  3472.         $o->cache_clear('query');
  3473.         $o->inform("Profile '$profile' restored.\n");
  3474.         }
  3475.         else {
  3476.         $o->warn($ok->msg);
  3477.         return;
  3478.         }
  3479.         return 1;
  3480.     }
  3481.  
  3482.     # Unrecognized subcommand:
  3483.     else {
  3484.         $o->warn("No such profile command '$args[0]'; see 'help profile'.\n");
  3485.         return;
  3486.     }
  3487.     }
  3488.     if (@profiles) {
  3489.     @profiles = dictsort @profiles;
  3490.     my $i = 0;
  3491.     $o->inform("Profiles:\n");
  3492.     my $profile = PPM::UI::profile_get()->result;
  3493.     for (@profiles) {
  3494.         $o->informf("%s%2d", $profile eq $profiles[$i] ? "*" : " ", $i + 1);
  3495.         $o->inform(". $_\n");
  3496.         $i++;
  3497.     }
  3498.     }
  3499.     elsif (defined $args[0] and matches($args[0], "del|ete")) {
  3500.     # assume that we just deleted the last profile
  3501.     $o->warn(<<END);
  3502. Profile deleted; no remaining profiles on the server.
  3503. END
  3504.     }
  3505.     else {
  3506.     $o->warn(<<END);
  3507. No profiles. Use 'profile add' to add a profile.
  3508. END
  3509.     }
  3510.     1;
  3511. }
  3512.  
  3513. #============================================================================
  3514. # Help-only topics:
  3515. #============================================================================
  3516. sub smry_prompt { "how to interpret the PPM prompt" }
  3517. sub help_prompt { <<'END' }
  3518. prompt -- information about the PPM3 prompt
  3519.   Description
  3520.     The PPM prompt can tell you six things:
  3521.  
  3522.     1)  The current repository;
  3523.  
  3524.     2)  The current target;
  3525.  
  3526.     3)  The last search you made on the current repository;
  3527.  
  3528.     4)  The last query you made on the current target;
  3529.  
  3530.     5)  The last package you described from this repository; and,
  3531.  
  3532.     6)  The last package you described from this target.
  3533.  
  3534.     To enable the prompt to tell you this information, you must set
  3535.     'prompt-context' to '1'. The following examples all assume this setting.
  3536.  
  3537.   Examples
  3538.     1   Repository and Target:
  3539.  
  3540.         Set 'prompt-context' The prompt will resemble:
  3541.  
  3542.             ppm:1:1> 
  3543.  
  3544.         In this case, the first '1' means that the first repository is
  3545.         selected. The second '1' means the first target is selected. You can
  3546.         prove this by adding another repository and switching to it:
  3547.  
  3548.             ppm:1:1> rep add TEMP http://my/repository
  3549.             Repositories:
  3550.               1. ActiveState Package Repository
  3551.             * 2. TEMP
  3552.             ppm:1:1> rep 2
  3553.             Repositories:
  3554.               1. ActiveState Package Repository
  3555.             * 2. TEMP
  3556.             ppm:2:1> 
  3557.  
  3558.         The same is true for targets. If you have multiple versions of Perl
  3559.         installed, when you swtich to a different target the second number
  3560.         reflects the change.
  3561.  
  3562.         If you delete all the repositories, the repository number changes to
  3563.         '?'. The same goes for targets. If either item is indicated by a
  3564.         question mark, you must configure a repository or target before
  3565.         proceeding.
  3566.  
  3567.     2   Search and Query:
  3568.  
  3569.         PPM stores searches and search results from in the current session.
  3570.         The prompt displays the search number:
  3571.  
  3572.             ppm:1:1> search Text
  3573.             [results displayed here]
  3574.             ppm:1:1:s1> 
  3575.  
  3576.         The 's1' indicates that the last search you performed can be viewed
  3577.         again by entering 'search 1'. Type 'search' with no arguments to
  3578.         view the list of cached searches:
  3579.  
  3580.             ppm:1:1:s1> search
  3581.             Search Result Sets:
  3582.             * 1. Text
  3583.  
  3584.         If you then enter 'search 1', you will see the same results as when
  3585.         you typed 'search Text' earlier. If you search for something else
  3586.         ('search Parse') then the number will change to 's2':
  3587.  
  3588.             ppm:1:1:s1> search Parse
  3589.             [results displayed here]
  3590.             ppm:1:1:s2>
  3591.  
  3592.         The same indicators apply to the query command. When you run a
  3593.         query, a numerical indicator displays the current query:
  3594.  
  3595.             ppm:1:1:s1> query PPM
  3596.             [results displayed here]
  3597.             ppm:1:1:s1:q1> 
  3598.  
  3599.         You can view the past queries with 'query', and view results by
  3600.         querying a particular number.
  3601.  
  3602.     3   Describe and Properties:
  3603.  
  3604.         When you use the describe command with the numerical switch (to view
  3605.         package information based on the package number in the last search
  3606.         or query), PPM sets that index to the current index. If you use the
  3607.         desribe command with the name switch, and the name is found within
  3608.         the current result, the index is set to the current one. If no
  3609.         package is found, PPM creates a new search or query on-the-fly, and
  3610.         sets it as the current search or query.
  3611.  
  3612.         For example:
  3613.  
  3614.             ppm:1:1> search Text
  3615.             1. Convert-Context  [0.501]     an Attributed Text data type
  3616.             2. gettext          [1.01]      message handling functions
  3617.             3. HTML-FromText    [1.005]     mark up text as HTML
  3618.             4. HTML-Subtext     [1.03]      Perform text substitutions on an HTML
  3619.                                             template
  3620.             5. Locale-Maketext  [0.18]      framework for software localization
  3621.             ppm:1:1:s1>
  3622.  
  3623.             ppm:1:1:s1> describe 1
  3624.             ====================
  3625.             Package 1:
  3626.                 Name: Convert-Context
  3627.              Version: 0.501
  3628.               Author: Martin Schwartz (martin@nacho.de)
  3629.             Abstract: an Attributed Text data type
  3630.             Implementations:
  3631.                    1. i686-linux-thread-multi
  3632.                    2. MSWin32-x86-multi-thread
  3633.                    3. sun4-solaris-thread-multi
  3634.             ====================
  3635.             ppm:1:1:s1:sp1> 
  3636.  
  3637.         The last prompt has an extra 'sp1'. That stands for 'search package
  3638.         1', and it means that PPM considers 'Convert-Context' to be the
  3639.         default package. If you now type 'describe' or 'install' with no
  3640.         arguments, PPM will apply your command to this package.
  3641.  
  3642.         If you go back to where you had no default package selected:
  3643.  
  3644.             ppm:1:1> search Text
  3645.             1. Convert-Context  [0.501]     an Attributed Text data type
  3646.             2. gettext          [1.01]      message handling functions
  3647.             3. HTML-FromText    [1.005]     mark up text as HTML
  3648.             4. HTML-Subtext     [1.03]      Perform text substitutions on an HTML
  3649.                                             template
  3650.             5. Locale-Maketext  [0.18]      framework for software localization
  3651.             ppm:1:1:s1>
  3652.  
  3653.         ...and you describe 'Locale-Maketext', you will see this:
  3654.  
  3655.             ppm:1:1:s1> describe Locale-Maketext
  3656.             ====================
  3657.                 Name: Locale-Maketext
  3658.              Version: 0.18
  3659.               Author: Sean M. Burke (sburke@cpan.org)
  3660.             Abstract: framework for software localization
  3661.             Prerequisites:
  3662.                    1. I18N-LangTags 0.13
  3663.             Implementations:
  3664.                    1. i686-linux-thread-multi
  3665.                    2. MSWin32-x86-multi-thread
  3666.                    3. sun4-solaris-thread-multi
  3667.             ====================
  3668.             ppm:1:1:s1:sp5>
  3669.  
  3670.         Notice that the correct package got selected, even though you
  3671.         specified it by name.
  3672.  
  3673.     This behaviour also applies to the query and properties commands.
  3674.  
  3675.   See Also
  3676.     describe, properties, query, search
  3677. END
  3678.  
  3679. sub run_quickstart  { $_[0]->help('quickstart') }
  3680. sub smry_quickstart { "a crash course in using PPM" }
  3681. sub help_quickstart { <<'END' }
  3682. quickstart -- a beginners' guide to PPM3
  3683.   Description
  3684.     PPM (Programmer's Package Manager) is a utility for managing software
  3685.     "packages". A package is a modular extension for a language or a
  3686.     software program. Packages reside in repositories. PPM can use three
  3687.     types of repositories:
  3688.  
  3689.      1) A directory on a CD-ROM or hard drive in your computer
  3690.      2) A website
  3691.      3) A remote Repository Server (such as ASPN)
  3692.  
  3693.     Common Commands:
  3694.  
  3695.     To view PPM help:
  3696.  
  3697.       help
  3698.       help <command>
  3699.  
  3700.     To view the name of the current repository:
  3701.  
  3702.       repository
  3703.  
  3704.     To search the current repository:
  3705.  
  3706.       search <keywords>
  3707.  
  3708.     To install a package:
  3709.  
  3710.       install <package_name>
  3711.  
  3712.     Most commands can be truncated; as long as the command is unambiguous,
  3713.     PPM will recognize it. For example, 'repository add foo" can be entered
  3714.     as 'rep add foo'.
  3715.  
  3716.     PPM features user profiles, which store information about installed
  3717.     packages. Profiles are stored as part of your ASPN account; thus, you
  3718.     can easily maintain package profiles for different languages, or
  3719.     configure one machine with your favorite packages, and then copy that
  3720.     installation to another machine by accessing your ASPN profile.
  3721.  
  3722.     For more information, type 'help profile' at the PPM prompt.
  3723. END
  3724.  
  3725. sub smry_ppm_migration { "guide for those familiar with PPM" }
  3726. sub help_ppm_migration { <<'END' }
  3727. ppm migration -- PPM Migration Guide
  3728.   Description
  3729.     Those familiar with PPM version 2 should appreciate the extended
  3730.     functionality of PPM version 3, including the command-line history,
  3731.     autocomplete and profiles. Some PPM version 2 commands are different in
  3732.     PPM version 3. Examples of command changes include:
  3733.  
  3734.     1   Adding a repository
  3735.  
  3736.         PPM2:
  3737.  
  3738.           set repository my_repository http://my/repository
  3739.  
  3740.         PPM3:
  3741.  
  3742.           repository add my_repository http://my/repository
  3743.  
  3744.     2   Removing a repository
  3745.  
  3746.         PPM2:
  3747.  
  3748.           set repository --remove my_repository
  3749.  
  3750.         PPM3:
  3751.  
  3752.           repository del my_repository
  3753.  
  3754.     3   Setting the temporary directory
  3755.  
  3756.         PPM2:
  3757.  
  3758.           set build DIRECTORY
  3759.  
  3760.         PPM3
  3761.  
  3762.           set tempdir DIRECTORY
  3763.  
  3764.     4   Setting frequency of download updates
  3765.  
  3766.         PPM2:
  3767.  
  3768.           set downloadstatus NUMBER
  3769.  
  3770.         PPM3:
  3771.  
  3772.           set download-chunksize NUMBER
  3773.  
  3774.     5   Changing the installation root directory:
  3775.  
  3776.         PPM2:
  3777.  
  3778.           set root DIRECTORY
  3779.  
  3780.         PPM3:
  3781.  
  3782.           target set root DIRECTORY
  3783.  
  3784.     6   Listing all installed packages:
  3785.  
  3786.         PPM2:
  3787.  
  3788.           query
  3789.  
  3790.         PPM3:
  3791.  
  3792.           query *
  3793.  
  3794.     7   Listing all packages on server:
  3795.  
  3796.         PPM2:
  3797.  
  3798.           search
  3799.  
  3800.         PPM3:
  3801.  
  3802.           search *
  3803. END
  3804.  
  3805. sub smry_unicode { "notes about unicode author names" }
  3806. sub help_unicode { <<'END' }
  3807. unicode -- Notes About Unicode Author Names
  3808.   Description
  3809.     CPAN author names are defined to be in Unicode. Unicode is an
  3810.     international standard ISO 10646, defining the *Universal Character Set
  3811.     (UCS)*. UCS contains all characters of all other character set
  3812.     standards. For more information about Unicode, see
  3813.     http://www.unicode.org/.
  3814.  
  3815.     The CPAN authors website is located at your local CPAN mirror under
  3816.     /authors/00whois.html. For example, you can view it at
  3817.     http://www.cpan.org/authors/00whois.html. This page can be rendered by
  3818.     Mozilla 0.9.8 and Internet Explorer 5.0, but you may have to install
  3819.     extra language packs to view all the author names.
  3820.  
  3821.     By default, PPM3 renders all characters as Latin1 when it prints them to
  3822.     your console. Characters outside the Latin1 range (0-255) are not
  3823.     printed at all.
  3824.  
  3825.     If your console can render UTF-8 characters, you can tell PPM3 not to
  3826.     recode characters by using one of the following environment variables:
  3827.  
  3828.     *   LC_ALL
  3829.  
  3830.     *   LC_CTYPE
  3831.  
  3832.     *   LANG
  3833.  
  3834.     *   PPM_LANG
  3835.  
  3836.     PPM3 requires one of these environment variables to contain the string
  3837.     'UTF-8'. For example, the following setting make PPM3 print
  3838.     beautifully-formatted authors in RedHat Linux 7.2 (assumes you're using
  3839.     a Bourne shell):
  3840.  
  3841.       $ PPM_LANG='en_US.UTF-8' xterm -u8 -e ppm3
  3842.  
  3843.     Linux and Solaris users should refer to xterm for more information about
  3844.     setting up xterm to display UTF-8 characters.
  3845. END
  3846.  
  3847. #============================================================================
  3848. # Utility Functions
  3849. #============================================================================
  3850. sub sort_fields { qw(name title author abstract version repository) }
  3851. sub sort_pkgs {
  3852.     my $o = shift;
  3853.     my $field = lc shift;
  3854.     my @pkgs = @_;
  3855.     my $targ = $o->conf('target');
  3856.     my $filt = sub { $_[0]->getppd_obj($targ)->result->$field };
  3857.     if ($field eq 'name') {
  3858.     return dictsort $filt, @pkgs;
  3859.     }
  3860.     if ($field eq 'title') {
  3861.     return dictsort $filt, @pkgs;
  3862.     }
  3863.     if ($field eq 'author') {
  3864.     return dictsort $filt, @pkgs;
  3865.     }
  3866.     if ($field eq 'abstract') {
  3867.     return dictsort $filt, @pkgs;
  3868.     }
  3869.     if ($field eq 'repository') {
  3870.     return dictsort sub { $_[0]->repository->name }, @pkgs;
  3871.     }
  3872.     if ($field eq 'version') {
  3873.     return sort {
  3874.         my $pa = $a->getppd_obj($targ)->result;
  3875.         my $pb = $b->getppd_obj($targ)->result;
  3876.         $pb->uptodate($pa->version_osd) <=> $pa->uptodate($pb->version_osd)
  3877.     } @pkgs;
  3878.     }
  3879.     @pkgs;
  3880. }
  3881.  
  3882. sub find_index {
  3883.     my $entry = shift || '';
  3884.     my $index = shift;
  3885.     $index = 0 unless defined $index;
  3886.     for (my $i=0; $i<@_; $i++) {
  3887.     return $index + $i if $entry eq $_[$i];
  3888.     }
  3889.     return $index - 1;
  3890. }
  3891.  
  3892. sub bounded {
  3893.     my $lb = shift;
  3894.     my $d = shift;
  3895.     my $ub = shift;
  3896.     return ($d >= $lb and $d <= $ub);
  3897. }
  3898.  
  3899. sub dictsort(@) {
  3900.     my $o = shift if eval { $_[0]->isa("PPMShell") };
  3901.     my $filt = ref($_[0]) eq 'CODE' ? shift @_ : undef;
  3902.     return map { $_->[0] }
  3903.        sort { lc $a->[1] cmp lc $b->[1] }
  3904.        map { [ $_, $filt ? $filt->($_) : $_ ] } @_;
  3905. }
  3906.  
  3907. sub path_under {
  3908.     my $path = shift;
  3909.     my $cmp  = shift;
  3910.     if ($^O eq 'MSWin32') {
  3911.     $path =~ s#\\#/#g;
  3912.     $cmp  =~ s#\\#/#g;
  3913.     return $path =~ /^\Q$cmp\E/i;
  3914.     }
  3915.     else {
  3916.     return $path =~ /^\Q$cmp\E/;
  3917.     }
  3918. }
  3919.  
  3920. sub prompt_str {
  3921.     my $o = shift;
  3922.  
  3923.     # Hack: set the pager here, instead of in settings_setkey()
  3924.     $o->{API}{pager} = $o->conf('pager');
  3925.  
  3926.     my @search_results = $o->cache_sets('search');
  3927.     my $search_result_current = $o->cache_set_current('search');
  3928.     my $search_result_index = $o->cache_set_index('search');
  3929.     my @query_results = $o->cache_sets('query');
  3930.     my $query_result_current = $o->cache_set_current('query');
  3931.     my $query_result_index = $o->cache_set_index('query');
  3932.  
  3933.     # Make sure a profile is selected if they turned tracking on.
  3934.     my $profile_track = $o->conf('profile-track');
  3935.     my $profile       = PPM::UI::profile_get()->result;
  3936.     $o->setup_profile()
  3937.     if $profile_track and not $profile and $o->mode eq 'SHELL';
  3938.  
  3939.     my @targs = PPM::UI::target_list()->result_l;
  3940.     if (@targs and not find_index($o->conf('target'), 1, @targs)) {
  3941.     $o->conf('target', $targs[0]);
  3942.     }
  3943.  
  3944.     if ($o->conf('prompt-context')) {
  3945.     my ($targ, $rep, $s, $sp, $q, $qp);
  3946.  
  3947.     if ($o->conf('prompt-verbose')) {
  3948.         my $sz = $o->conf('prompt-slotsize');
  3949.         $targ = substr($o->conf('target'), 0, $sz);
  3950.         $rep  = substr($o->conf('repository'), 0, $sz);
  3951.  
  3952.         my $sq_tmp = $o->cache_set('search', undef, 'query');
  3953.         my $ss_tmp = $o->cache_set('search');
  3954.         my $sp_tmp = $o->cache_entry('search');
  3955.         $s = (defined $sq_tmp)
  3956.           ? ":" . substr($sq_tmp, 0, $sz)
  3957.           : "";
  3958.         $sp = ($s and defined $sp_tmp and
  3959.            bounded(0, $search_result_index, $#$ss_tmp))
  3960.           ? ":" . substr($sp_tmp->name, 0, $sz)
  3961.           : "";
  3962.  
  3963.         my $qq_tmp = $o->cache_set('query', undef, 'query');
  3964.         my $qs_tmp = $o->cache_set('query');
  3965.         my $qp_tmp = $o->cache_entry('query');
  3966.         $q = (defined $qq_tmp)
  3967.           ? ":" . substr($qq_tmp, 0, $sz)
  3968.           : "";
  3969.         $qp = ($q and defined $qp_tmp and
  3970.            bounded(0, $query_result_index, $#$qs_tmp))
  3971.           ? ":" . substr($qp_tmp->name, 0, $sz)
  3972.           : "";
  3973.     }
  3974.     else {
  3975.         # Target and Repository:
  3976.         $targ = find_index($o->conf('target'), 1, @targs);
  3977.         $targ = '?' if $targ == 0;
  3978.     
  3979.         # Search number & package:
  3980.         $s = @search_results ? ":s".($search_result_current + 1) : "";
  3981.         my $sp_tmp = $o->cache_set('search');
  3982.         $sp = ($s and defined $sp_tmp and 
  3983.            bounded(0, $search_result_index, $#$sp_tmp))
  3984.           ? ":sp".($search_result_index + 1)
  3985.           : "";
  3986.     
  3987.         # Query number & package:
  3988.         $q = @query_results ? ":q".($query_result_current + 1) : "";
  3989.         my $qp_tmp = $o->cache_set('query');
  3990.         $qp = ($q and defined $qp_tmp and
  3991.            bounded(0, $query_result_index, $#$qp_tmp))
  3992.           ? ":qp".($query_result_index + 1)
  3993.           : "";
  3994.     }
  3995.     return "ppm:$targ$s$sp$q$qp> ";
  3996.     }
  3997.     else {
  3998.     return "ppm> ";
  3999.     }
  4000. }
  4001.  
  4002. {
  4003.     # Weights for particular fields: these are stored in percentage of the
  4004.     # screen width, based on the number of columns they use on an 80 column
  4005.     # terminal. They also have a minimum and maximum.
  4006.     use constant MIN    => 0;
  4007.     use constant MAX    => 1;
  4008.     my %weight = (
  4009.     name     => [12, 20],
  4010.     title    => [12, 20],
  4011.     abstract => [12, 20],
  4012.     author   => [12, 20],
  4013.     repository => [12, 20],
  4014.     version  => [ 4,  9],
  4015.     );
  4016.     my %meth = (
  4017.     name     => 'name',
  4018.     title    => 'title',
  4019.     version  => 'version',
  4020.     abstract => 'abstract',
  4021.     author   => 'author',
  4022.     repository => sub {
  4023.         my $o = shift;
  4024.         my $rep = $o->repository or return "Installed";
  4025.         my $name = $rep->name;
  4026.         my $id   = $o->id || $name;
  4027.         my $loc  = $rep->location;
  4028.         "$name [$loc]"
  4029.     },
  4030.     );
  4031.     # These are Text::Autoformat justification marks. They're actually used to
  4032.     # build a printf() format string, since it's so much more efficient for a
  4033.     # non-line-wrapping case.
  4034.     my %just = (
  4035.     name     => '<',
  4036.     title    => '<',
  4037.     abstract => '<',
  4038.     author   => '<',
  4039.     repository => '<',
  4040.     version  => '>',
  4041.     );
  4042.     my %plus = (
  4043.     name     => '0',
  4044.     title    => '0',
  4045.     abstract => '0',
  4046.     author   => '0',
  4047.     repository => '0',
  4048.     version  => '2',
  4049.     );
  4050.     my %filt = (
  4051.     version => q{"[$_]"},
  4052.     );
  4053.     sub picture_optimized {
  4054.     my $o = shift;
  4055.     my @items = @{shift(@_)};
  4056.     unless ($o->conf('fields')) {
  4057.         my $m = $o->setmode('SILENT');
  4058.         $o->conf('fields', '', 1);
  4059.         $o->setmode($m);
  4060.     }
  4061.     my @fields = split ' ', $o->conf('fields');
  4062.     $_ = lc $_ for @fields;
  4063.     my (%max_width, %width);
  4064.     my $cols = $o->termsize->{cols};
  4065.     for my $f (@fields) {
  4066.         my $meth = $meth{$f};
  4067.         $max_width{$f} = max { length($_->$meth) } @items;
  4068.         $max_width{$f} += $plus{$f};
  4069.         $width{$f} = $max_width{$f} / 80 * $cols;
  4070.         my $max_f  = $weight{$f}[MAX] / 80 * $cols;
  4071.         my $min_f  = $weight{$f}[MIN];
  4072.         my $gw     = $width{$f};
  4073.         $width{$f} = (
  4074.         $width{$f} > $max_width{$f} ? $max_width{$f} :
  4075.         $width{$f} > $max_f         ? $max_f         :
  4076.         $width{$f} < $min_f         ? $min_f         : $width{$f}
  4077.         );
  4078.     }
  4079.     my $right = $fields[-1];
  4080.     my $index_sz = length( scalar(@items) ) + 3; # index spaces
  4081.     my $space_sz = @fields + 1; # separator spaces
  4082.     my $room = $cols - $index_sz - $space_sz;
  4083.     $width{$right} = $room - sum { $width{$_} } @fields[0 .. $#fields-1];
  4084.     while ($width{$right} > $max_width{$right}) {
  4085.         my $smallest;
  4086.         my $n;
  4087.         for my $k (@fields[0 .. $#fields-1]) {
  4088.         my $max = $max_width{$k};
  4089.         my $sz  = $width{$k};
  4090.         $smallest = $k, $n = $max - $sz if $max - $sz > $n;
  4091.         }
  4092.         $width{$right}--;
  4093.         $width{$smallest}++;
  4094.     }
  4095.     while ($width{$right} < $weight{$right}[MIN]) {
  4096.         my $biggest;
  4097.         my $n;
  4098.         for my $k (@fields[0 .. $#fields-1]) {
  4099.         my $max = $max_width{$k};
  4100.         my $sz  = $width{$k};
  4101.         $biggest = $k, $n = $max - $sz if $max - $sz < $n;
  4102.         }
  4103.         $width{$right}++;
  4104.         $width{$biggest}--;
  4105.     }
  4106.     my $picture;
  4107.     $picture = "\%${index_sz}s "; # printf picture
  4108.     $picture .= join ' ', map {
  4109.         my $w = $width{$_};
  4110.         my $c = $just{$_};
  4111.         my $pad = $c eq '>' ? '' : '-';
  4112.         "\%${pad}${w}s" # printf picture
  4113.     } @fields;
  4114.     ($picture, \@fields, [@width{@fields}]);
  4115.     }
  4116.  
  4117.     sub print_formatted {
  4118.     my $o = shift;
  4119.     my $targ = $o->conf('target');
  4120.     my @items = map { $_->getppd_obj($targ)->result } @{shift(@_)};
  4121.     my $selected = shift;
  4122.     my $format;
  4123.  
  4124.     # Generate a picture and a list of fields for Text::Autoformat:
  4125.     my (@fields, %width);
  4126.     my ($picture, $f, $w) = $o->picture_optimized(\@items);
  4127.     $picture .= "\n";
  4128.     @fields = @$f;
  4129.     @width{@fields} = @$w;
  4130.  
  4131.     # The line-breaking sub: use '~' as hyphenation signal
  4132.     my $wrap = sub {
  4133.         my ($str, $maxlen, $width) = @_;
  4134.         my $field = substr($str, 0, $maxlen - 1) . '~';
  4135.         my $left  = substr($str, $maxlen - 1);
  4136.         ($field, $left);
  4137.     };
  4138.  
  4139.     my $lines = 0;
  4140.     my $i = 1;
  4141.     my @text;
  4142.     my %seen;
  4143.     for my $pkg (@items) {
  4144.         my $star = (defined $selected and $selected == $i - 1) ? "*" : " ";
  4145.         my $num  = "$star $i.";
  4146.         my @vals = (
  4147.         map {
  4148.             my $field  = $_;
  4149.             my $method = $meth{$field};
  4150.             local $_   = $pkg->$method;
  4151.             my $val = defined $filt{$field} ? eval $filt{$field} : $_;
  4152.             ($val) = $wrap->($val, $width{$field})
  4153.                 if length $val > $width{$field};
  4154.             $val;
  4155.         }
  4156.         @fields
  4157.         );
  4158. #        my $key = join '', @vals;
  4159. #        if (exists $seen{$key}) {
  4160. #        my $index = $seen{$key};
  4161. #        substr($text[$index], 0, 1) = '+';
  4162. #        next;
  4163. #        }
  4164. #        $seen{$key} = $i - 1;
  4165.         (my $inc = sprintf $picture, $num, @vals) =~ s/[ ]+$//;
  4166.         push @text, $inc;
  4167.         $i++;
  4168.     }
  4169.  
  4170.     # And, page it.
  4171.     $o->page(join '', @text);
  4172.     }
  4173. }
  4174.  
  4175. sub tree_pkg {
  4176.     my $o = shift;
  4177.     my @rlist = $o->reps_on;
  4178.     my $tar = $o->conf('target');
  4179.     my $pkg = shift;
  4180.     my $ppd;
  4181.     if (eval { $pkg->isa('PPM::Package') }) {
  4182.     $ppd = $pkg->getppd_obj($tar)->result;
  4183.     }
  4184.     else {
  4185.     my ($s, $i) = $o->cache_find('search', $pkg);
  4186.     if ($i >= 0) {
  4187.         $ppd = $o->cache_entry('search', $i, $s);
  4188.     } 
  4189.     else {
  4190.         my $ok = PPM::UI::describe(\@rlist, $tar, $pkg);
  4191.         unless ($ok->is_success) {
  4192.         $o->warn($ok->msg);
  4193.         return unless $ok->ok;
  4194.         }
  4195.         $ppd = $ok->result->getppd_obj($tar)->result;
  4196.     }
  4197.     }
  4198.  
  4199.     my $pad = "\n";
  4200.     $o->inform($ppd->name, " ", $ppd->version);
  4201.     $o->Tree(\@rlist, $tar, $ppd->name, $pad, {});
  4202.     $o->inform($pad);
  4203. }
  4204.  
  4205. my ($VER, $HOR, $COR, $TEE, $SIZ) = ('|', '_', '\\', '|', ' ');
  4206.  
  4207. sub Tree {
  4208.     my $o = shift;
  4209.     my $reps = shift;
  4210.     my $tar = shift;
  4211.     my $pkg = shift;
  4212.     my $ind = shift;
  4213.     my $seen = shift;
  4214.     my $pad = $ind . "  " . $VER;
  4215.  
  4216.     my $ppd;
  4217.     if (exists $seen->{$pkg}) {
  4218.     $ppd = $seen->{$pkg};
  4219.     }
  4220.     else {
  4221.     my ($s, $i) = $o->cache_find('search', $pkg);
  4222.     if ($i >= 0) {
  4223.         $ppd = $o->cache_entry('search', $i, $s);
  4224.     }
  4225.     else {
  4226.         my $ok = PPM::UI::describe($reps, $tar, $pkg);
  4227.         unless ($ok->is_success) {
  4228.         $o->inform(" -- package not found; skipping tree");
  4229.         return 0 unless $ok->ok;
  4230.         }
  4231.         $ppd = $ok->result;
  4232.     }
  4233.     $ppd->make_complete($tar);
  4234.     $ppd = $ppd->getppd_obj($tar)->result;
  4235.     $seen->{$pkg} = $ppd;
  4236.     }
  4237.  
  4238.     my @impls   = $ppd->implementations;
  4239.     return 0 unless @impls;
  4240.     my @prereqs = $impls[0]->prereqs;
  4241.     return 0 unless @prereqs;
  4242.     my $nums = scalar @prereqs;
  4243.  
  4244.     for (1..$nums) {
  4245.     my $doneblank = 0;
  4246.     my $pre = $prereqs[$_-1];
  4247.     my $txt = $pre->name . " " . $pre->version;
  4248.     if ($_ == $nums) {
  4249.         substr($pad, -1) = $COR;
  4250.         $o->inform($pad, "$HOR$HOR", $txt);
  4251.         substr($pad, -1) = ' ';
  4252.     }
  4253.     else {
  4254.         substr($pad, -1) = $TEE;
  4255.         $o->inform($pad, "$HOR$HOR", $txt);
  4256.         substr($pad, -1) = $VER;
  4257.     }
  4258.     if ($o->Tree($reps, $tar, $pre->name, $pad, $seen) != 0 and
  4259.         $doneblank == 0) {
  4260.         $o->inform($pad); ++$doneblank;
  4261.     }
  4262.     }
  4263.     return $nums;
  4264. }
  4265.  
  4266. sub describe_pkg {
  4267.     my $o = shift;
  4268.     my $pkg = shift;
  4269.     my ($extra_keys, $extra_vals) = (shift || [], shift || []);
  4270.     my $n; 
  4271.  
  4272.     # Get the PPM::PPD object out of the PPM::Package object.
  4273.     my $pkg_des = $pkg->describe($o->conf('target'))->result;
  4274.  
  4275.     # Basic information:
  4276.     $n = $o->print_pairs(
  4277.     [qw(Name Version Author Title Abstract), @$extra_keys],
  4278.     [(map { $pkg_des->$_ } qw(name version author title abstract)),
  4279.      @$extra_vals],
  4280.     undef,    # separator
  4281.     undef,    # left
  4282.     undef,    # indent
  4283.     undef,    # length
  4284.     1,    # wrap (yes, please wrap)
  4285.     );
  4286.  
  4287.     # The repository:
  4288.     if (my $rep = $pkg_des->repository) {
  4289.     $o->print_pairs(
  4290.         ["Location"],
  4291.         [$rep->name],
  4292.         undef,    # separator
  4293.         undef,    # left
  4294.         undef,    # indent
  4295.         $n,        # length
  4296.         1,        # wrap
  4297.     );
  4298.     }
  4299.     
  4300.     # Prerequisites:
  4301.     my @impls = grep { $_->architecture } $pkg_des->implementations;
  4302.     my @prereqs = @impls ? $impls[0]->prereqs : ();
  4303.     $o->inform("Prerequisites:\n") if @prereqs;
  4304.     $o->print_pairs(
  4305.     [ 1 .. @prereqs ],
  4306.     [ map { $_->name . ' ' . $_->version} @prereqs ],
  4307.     '. ',    # separator
  4308.     undef,    # left
  4309.     undef,    # indent
  4310.     $n,    # length
  4311.     0,    # wrap (no, please don't wrap)
  4312.     );
  4313.     
  4314.     # Implementations:
  4315.     $o->inform("Available Platforms:\n") if @impls;
  4316.     my @impl_strings;
  4317.     for (@impls) {
  4318.     my $arch  = $_->architecture;
  4319.     my $os    = $_->os;
  4320.     my $osver = $_->osversion;
  4321.     my $str   = $arch;
  4322.     $osver    =~ s/\Q(any version)\E//g;
  4323.     if ($os and $osver) {
  4324.         $str .= ", $os $osver";
  4325.     }
  4326.     push @impl_strings, $str;
  4327.     }
  4328.     @impl_strings = dictsort @impl_strings;
  4329.     $o->print_pairs(
  4330.     [ 1 .. @impls ],
  4331.     [ @impl_strings ],
  4332.     '. ', undef, undef, $n
  4333.     );
  4334. }
  4335.  
  4336. sub remove_pkg {
  4337.     my $o = shift;
  4338.     my $package = shift;
  4339.     my $target = $o->conf('target');
  4340.     my $force = shift;
  4341.     my $quell_clear = shift;
  4342.     my $verbose = $o->conf('remove-verbose');
  4343.     my $ok = PPM::UI::remove($target, $package, $force, sub { $o->cb_remove(@_) }, $verbose);
  4344.     unless ($ok->is_success) {
  4345.     $o->warn($ok->msg);
  4346.     return 0 unless $ok->ok;
  4347.     }
  4348.     else {
  4349.     $o->warn_profile_change($ok);
  4350.     }
  4351.     $o->cache_clear('query') if ($ok->ok and not $quell_clear);
  4352.     1;
  4353. }
  4354.  
  4355. sub upgrade_pkg {
  4356.     push @_, 'upgrade';
  4357.     goto &install_pkg;
  4358. }
  4359. sub install_pkg {
  4360.     my $o = shift;
  4361.     my $pkg = shift;
  4362.     my $opts = shift;
  4363.     my $action = shift;
  4364.     my $quell_clear = shift;
  4365.     $action = 'install' unless defined $action;
  4366.  
  4367.     # Find the package:
  4368.     while (1) {
  4369.     # 1. Return if they specified a full filename or URL:
  4370.     last if PPM::UI::is_pkg($pkg);
  4371.  
  4372.     # 2. Check if whatever they specified returns 1 search result:
  4373.     my $search =
  4374.       PPM::UI::search([$o->reps_on], $o->conf('target'), $pkg, 
  4375.               $o->conf('case-sensitivity'));
  4376.     unless ($search->is_success) {
  4377.         $o->warn($search->msg);
  4378.         return unless $search->ok;
  4379.     }
  4380.     my @ret = $search->result_l;
  4381.     if (@ret > 1) {
  4382.         $o->warn(<<END);
  4383. Searching for '$pkg' returned multiple results. Using 'search' instead...
  4384. END
  4385.         $o->run_search($pkg);
  4386.         return;
  4387.     }
  4388.     elsif (not @ret) {
  4389.         $o->warn(<<END);
  4390. Searching for '$pkg' returned no results. Try a broader search first.
  4391. END
  4392.         return;
  4393.     }
  4394.     $pkg = $ret[0]->name;
  4395.     last;
  4396.     }
  4397.  
  4398.     my $cb = (
  4399.     $opts->{dryrun}
  4400.     ? $action eq 'install' ? \&dr_install : \&dr_upgrade
  4401.     : $action eq 'install' ? \&cb_install : \&cb_upgrade
  4402.     );
  4403.  
  4404.     # Now, do the install
  4405.     my $ok;
  4406.     my @rlist = $o->reps_on;
  4407.     my $targ = $o->conf('target');
  4408.  
  4409.     if ($action eq 'install') {
  4410.     $opts->{verbose} = $o->conf('install-verbose');
  4411.     my $prop = PPM::UI::properties($targ, $pkg);
  4412.     my $pkgname = ref $pkg ? eval { $pkg->name } || $pkg : $pkg;
  4413.     $o->inform("Note: Package '$pkgname' is already installed.\n")
  4414.         if $prop->ok;
  4415.     $ok = PPM::UI::install(\@rlist, $targ, $pkg, $opts, sub {$o->$cb(@_)});
  4416.     }
  4417.     else {
  4418.     $opts->{verbose} = $o->conf('upgrade-verbose');
  4419.     $ok = PPM::UI::upgrade(\@rlist, $targ, $pkg, $opts, sub {$o->$cb(@_)});
  4420.     }
  4421.  
  4422.     unless ($ok->is_success) {
  4423.     $o->warn($ok->msg);
  4424.     return unless $ok->ok;
  4425.     }
  4426.     else {
  4427.     $o->warn_profile_change($ok);
  4428.     $o->cache_clear('query') unless $quell_clear;
  4429.     }
  4430.     1;
  4431. }
  4432.  
  4433. # The dry run callback; just prints out package name and version:
  4434. sub dr_install {
  4435.     my $o = shift;
  4436.     my $pkg = shift;
  4437.     my $version = shift;
  4438.     my $target_name = shift;
  4439.     $o->inform(<<END);
  4440. Dry run install '$pkg' version $version in $target_name.
  4441. END
  4442. }
  4443.  
  4444. sub dr_upgrade {
  4445.     my $o = shift;
  4446.     my $pkg = shift;
  4447.     my $version = shift;
  4448.     my $target_name = shift;
  4449.     $o->inform(<<END);
  4450. Dry run upgrade '$pkg' version $version in $target_name.
  4451. END
  4452. }
  4453.  
  4454. sub dr_remove {
  4455.     my $o = shift;
  4456.     my $pkg = shift;
  4457.     my $version = shift;
  4458.     my $target_name = shift;
  4459.     $o->inform(<<END);
  4460. Dry run remove '$pkg' version $version from $target_name.
  4461. END
  4462. }
  4463.  
  4464. sub cb_remove {
  4465.     my $o = shift;
  4466.     my $pkg = shift;
  4467.     my $version = shift;
  4468.     my $target_name = shift;
  4469.     my $status = shift;
  4470.     if ($status eq 'COMPLETE') {
  4471.     $o->inform(
  4472.         "Successfully removed $pkg version $version from $target_name.\n"
  4473.     )
  4474.     }
  4475.     else {
  4476.     $o->inform(<<END);
  4477. $SEP
  4478. Remove '$pkg' version $version from $target_name.
  4479. $SEP
  4480. END
  4481.     }
  4482. }
  4483.  
  4484. sub cb_install {
  4485.     my $o = shift;
  4486.     unshift @_, $o, 'install';
  4487.     &cb_status;
  4488. }
  4489.  
  4490. sub cb_upgrade {
  4491.     my $o = shift;
  4492.     unshift @_, $o, 'upgrade';
  4493.     &cb_status;
  4494. }
  4495.  
  4496. sub cb_status {
  4497.     my $o = shift;
  4498.     my $ACTION = shift;
  4499.     my $pkg = shift;
  4500.     my $version = shift;
  4501.     my $target_name = shift;
  4502.     my $status = shift;
  4503.     my $bytes = shift;
  4504.     my $total = shift;
  4505.     my $secs = shift;
  4506.  
  4507.     my $cols = $ENV{COLUMNS} || 78;
  4508.  
  4509.     $o->inform(<<END) and return if ($status eq 'PRE-INSTALL');
  4510. $SEP
  4511. \u$ACTION '$pkg' version $version in $target_name.
  4512. $SEP
  4513. END
  4514.  
  4515.     # Print the output on one line, repeatedly:
  4516.     my ($line, $pad, $eol);
  4517.     if ($status eq 'DOWNLOAD') {
  4518.     if ($bytes < $total) {
  4519.         $line = "Transferring data: $bytes/$total bytes.";
  4520.         $eol = "\r";
  4521.     }
  4522.     else {
  4523.         $line = "Downloaded $bytes bytes.";
  4524.         $eol = "\n";
  4525.     }
  4526.     }
  4527.     elsif ($status eq 'PRE-EXPAND') {
  4528.     $line = ""; #"Extracting package. This may take a few seconds.";
  4529.     $eol = "\r";  #"\n";
  4530.     }
  4531.     elsif ($status eq 'EXPAND') {
  4532.     $line = "Extracting $bytes/$total: $secs";
  4533.     $eol = $bytes < $total ? "\r" : "\n";
  4534.     }
  4535.     elsif ($status eq 'COMPLETE') {
  4536.     my $verb = $ACTION eq 'install' ? 'installed' : 'upgraded';
  4537.     $o->inform(
  4538.         "Successfully $verb $pkg version $version in $target_name.\n"
  4539.     );
  4540.     return;
  4541.     }
  4542.     $pad = ' ' x ($cols - length($line));
  4543.     $o->verbose($line, $pad, $eol);
  4544. }
  4545.  
  4546. sub warn_profile_change {
  4547.     my $o = shift;
  4548.     my $ok = shift;
  4549.  
  4550.     my $profile_track = $o->conf('profile-track');
  4551.     my $profile = PPM::UI::profile_get()->result;
  4552.  
  4553.     if ($profile_track) {
  4554.     $o->verbose(<<END);
  4555. Tracking changes to profile '$profile'.
  4556. END
  4557.     }
  4558. }
  4559.  
  4560. sub parse_range {
  4561.     my @numbers;
  4562.     my $arg;
  4563.     while ($arg = shift) {
  4564.       while ($arg) {
  4565.     if ($arg =~ s/^\s*,?\s*(\d+)\s*-\s*(\d+)//) {
  4566.         push @numbers, ($1 .. $2);
  4567.     }
  4568.     elsif ($arg =~ s/^\s*,?\s*(\d+)//) {
  4569.         push @numbers, $1;
  4570.     }
  4571.     else {
  4572.         last;
  4573.     }
  4574.       }
  4575.     }
  4576.     @numbers;
  4577. }
  4578.  
  4579. sub raw_args {
  4580.     my $o = shift;
  4581.     strip($o->line_args);
  4582. }
  4583.  
  4584. sub strip {
  4585.     my $f = shift;
  4586.     $f =~ s/^\s*//;
  4587.     $f =~ s/\s*$//;
  4588.     $f;
  4589. }
  4590.  
  4591. # matches("neil", "ne|il") => 1
  4592. # matches("ne", "ne|il") => 1
  4593. # matches("n", "ne|il") => 0
  4594. sub matches {
  4595.     my $cmd = shift;
  4596.     my $pat = shift || "";
  4597.  
  4598.     my ($required, $extra) = split '\|', $pat;
  4599.     $extra ||= "";
  4600.     my $regex = "$required(?:";
  4601.     for (my $i=1; $i<=length($extra); $i++) {
  4602.     $regex .= '|' . substr($extra, 0, $i);
  4603.     }
  4604.     $regex .= ")";
  4605.     return $cmd =~ /^$regex$/i;
  4606. }
  4607.  
  4608. sub pause_exit {
  4609.     my $o = shift;
  4610.     my $exit_code = shift || 0;
  4611.     my $pause = shift || 0;
  4612.     if ($pause) {
  4613.     if ($o->have_readkey) {
  4614.         $o->inform("Hit any key to exit...");
  4615.     }
  4616.     else {
  4617.         $o->inform("Hit <ENTER> to exit...");
  4618.     }
  4619.     $o->readkey;
  4620.     }
  4621.     exit $exit_code;
  4622. }
  4623.  
  4624. #============================================================================
  4625. # Check if this is the first time we've ever used profiles. This can be
  4626. # guessed: if the 'profile' entry is not set, but the 'profile-track' flag
  4627. # is, then it's the first time profile-track has been set to '1'.
  4628. #============================================================================
  4629. sub setup_profile {
  4630.     my $o = shift;
  4631.     $o->inform(<<END);
  4632. $SEP
  4633. You have profile tracking turned on: now it's time to choose a profile name.
  4634. ActiveState's PPM 3.0 Server will track which packages you have installed on
  4635. your machine. This information is stored in a "profile", located on the
  4636. server.
  4637.  
  4638. Here are some features of profiles:
  4639.  o You can have as many profiles as you want;
  4640.  o Each profile can track an unlimited number of packages;
  4641.  o PPM defaults to "tracking" your profile (it updates your profile every time
  4642.    you add or remove a package;
  4643.  o You can disable profile tracking by modifying the 'profile-track' option;
  4644.  o You can manually select, save, and restore profiles;
  4645.  o You can view your profile from ASPN as well as inside PPM 3.
  4646. $SEP
  4647.  
  4648. END
  4649.  
  4650.     my $response = PPM::UI::profile_list();
  4651.     my @l;
  4652.     unless ($response->ok) {
  4653.     $o->warn($response->msg);
  4654.     $o->warn(<<END);
  4655.  
  4656. You can still use PPM3, but profiles are not enabled. To try setting up
  4657. profiles again, enter 'set profile-track=1'. Or, you can set up profiles
  4658. by hand, using the 'profile add' command.
  4659.  
  4660. END
  4661.     $o->run('unset', 'profile-track');
  4662.     return;
  4663.     }
  4664.     else {
  4665.     @l = sort $response->result_l;
  4666.     $o->inform("It looks like you have profiles on the server already.\n")
  4667.       if @l;
  4668.     $o->print_pairs([1 .. @l], \@l, '. ', 1, ' ');
  4669.     $o->inform("\n") if @l;
  4670.     }
  4671.  
  4672.     require PPM::Sysinfo;
  4673.     (my $suggest = PPM::Sysinfo::hostname()) =~ s/\..*$//;
  4674.     $suggest ||= "Default Profile";
  4675.     my $profile_name = $o->prompt(
  4676.     "What profile name would you like? [$suggest] ", $suggest, @l
  4677.     );
  4678.  
  4679.     my $select_existing = grep { $profile_name eq $_ } $response->result_l
  4680.       if $response->ok;
  4681.     if ($select_existing) {
  4682.     $o->inform("Selecting profile '$profile_name'...\n");
  4683.     PPM::UI::profile_set($profile_name);
  4684.     $o->inform(<<END);
  4685. You should probably run either 'profile save' or 'profile restore' to bring
  4686. the profile in sync with your computer.
  4687. END
  4688.     }
  4689.     elsif ($response->ok) {
  4690.     $o->inform("Creating profile '$profile_name'...\n");
  4691.     $o->run('profile', 'add', $profile_name);
  4692.     $o->inform("Saving profile '$profile_name'...\n");
  4693.     $o->run('profile', 'save');
  4694.     $o->inform(<<END);
  4695. Congratulations! PPM is now set up to track your profile.
  4696. END
  4697.     }
  4698.     else {
  4699.     $o->warn($response->msg);
  4700.     $o->warn(<<END);
  4701.  
  4702. You can still use PPM3, but profiles will not be enabled. To try setting up
  4703. profiles again, enter 'set profile-track=1'. Or, you can set up profiles
  4704. yourself using the 'profile add' command.
  4705.  
  4706. END
  4707.     $o->run('unset', 'profile-track');
  4708.     }
  4709. }
  4710.  
  4711. package main;
  4712. use Getopt::Long;
  4713. use Data::Dumper;
  4714.  
  4715. $ENV{PERL_READLINE_NOWARN} = "1";
  4716. $ENV{PERL_RL} = $^O eq 'MSWin32' ? "0" : "Perl";
  4717.  
  4718. my ($pause, $input_file, $target);
  4719.  
  4720. BEGIN {
  4721.     my ($shared_config_files, @fixpath, $gen_inst_key);
  4722.  
  4723.     Getopt::Long::Configure('pass_through');
  4724.     $target = 'auto';
  4725.     GetOptions(
  4726.     'file=s' => \$input_file,
  4727.     'shared' => \$shared_config_files,
  4728.     'target:s' => \$target,
  4729.     'fixpath=s' => \@fixpath,
  4730.     'generate-inst-key' => \$gen_inst_key,
  4731.     pause => \$pause,
  4732.     );
  4733.     Getopt::Long::Configure('no_pass_through');
  4734.  
  4735.     if ($shared_config_files) {
  4736.     $ENV{PPM3_shared_config} = 1;
  4737.     }
  4738.  
  4739.     if (@fixpath) {
  4740.     PPM::UI::target_fix_paths(@fixpath);
  4741.     exit;
  4742.     }
  4743.     if ($gen_inst_key) {
  4744.     require PPM::Config;
  4745.     PPM::Config::load_config_file('instkey');
  4746.     exit;
  4747.     }
  4748. }
  4749.  
  4750. # If we're being run from a file, tell Term::Shell about it:
  4751. if ($input_file) {
  4752.     my $line = 0;
  4753.     open SCRIPT, $input_file or die "$0: can't open $input_file: $!";
  4754.     my $shell = PPMShell->new(
  4755.     term => ['PPM3', \*SCRIPT, \*STDOUT],
  4756.     target => $target,
  4757.     pager => 'none',
  4758.     );
  4759.     $shell->setmode('SCRIPT');
  4760.     while (<SCRIPT>) {
  4761.     $line++;
  4762.     next if /^\s*#/ or /^\s*$/;
  4763.     my ($cmd, @args) = $shell->line_parsed($_);
  4764.     my $ret = $shell->run($cmd, @args);
  4765.     my $warn = <<END;
  4766. $0: $input_file:$line: fatal error: unknown or ambiguous command '$cmd'. 
  4767. END
  4768.     $shell->warn($warn) and $shell->pause_exit(2, $pause)
  4769.         unless $shell->{API}{cmd}{run}{found};
  4770.     $shell->pause_exit(1, $pause) unless $ret;
  4771.     }
  4772.     close SCRIPT;
  4773.     $shell->pause_exit(0, $pause);
  4774. }
  4775.  
  4776. # If we've been told what to do from the command-line, do it right away:
  4777. elsif (@ARGV) {
  4778.     my $shell = PPMShell->new(target => $target, pager => 'none');
  4779.     $shell->setmode('BATCH');
  4780.     my $ret = $shell->run($ARGV[0], @ARGV[1..$#ARGV]);
  4781.     my $warn = <<END;
  4782. Unknown or ambiguous command '$ARGV[0]'; type 'help' for commands.
  4783. END
  4784.     $shell->warn($warn) and $shell->pause_exit(2, $pause)
  4785.     unless $shell->{API}{cmd}{run}{found};
  4786.     $shell->pause_exit(0, $pause) if $ret;
  4787.     $shell->pause_exit(1, $pause);
  4788. }
  4789.  
  4790. # Just run the command loop
  4791. if (-t STDIN and -t STDOUT) {
  4792.     my $shell = PPMShell->new(target => $target);
  4793.     $shell->setmode('SHELL');
  4794.     $shell->cmdloop;
  4795. }
  4796. else {
  4797.     die <<END;
  4798.  
  4799. Error:
  4800.     PPM3 cannot be run in interactive shell mode unless both STDIN and
  4801.     STDOUT are connected to a terminal or console. If you want to
  4802.     capture the output of a command, use PPM3 in batch mode like this:
  4803.  
  4804.        ppm3 search IO-stringy > results.txt
  4805.  
  4806.     Type 'perldoc ppm3' for more information.
  4807.  
  4808. END
  4809. }
  4810.  
  4811.  
  4812. =head1 NAME
  4813.  
  4814. ppm3-bin - ppm3 executable
  4815.  
  4816. =head1 SYNOPSIS
  4817.  
  4818. Do not run I<ppm3-bin> manually. It is meant to be called by the wrapper
  4819. program I<ppm3>. See L<ppm3>.
  4820.  
  4821. =head1 DESCRIPTION
  4822.  
  4823. I<ppm3> runs I<ppm3-bin> after setting up a few environment variables. You
  4824. should run I<ppm3> instead.
  4825.  
  4826. For information about I<ppm3> commands, see L<ppm3>.
  4827.  
  4828. =head1 SEE ALSO
  4829.  
  4830. See L<ppm3>.
  4831.  
  4832. =head1 AUTHOR
  4833.  
  4834. ActiveState Corporation (support@ActiveState.com)
  4835.  
  4836. =head1 COPYRIGHT
  4837.  
  4838. Copyright (C) 2001, 2002, ActiveState Corporation. All Rights Reserved.
  4839.  
  4840. =cut
  4841.  
  4842. __END__
  4843. :endofperl
  4844.