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