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 / _d8152a6e1cf7443dafa1015eea31183f < prev    next >
Encoding:
Text File  |  2004-04-13  |  142.5 KB  |  4,897 lines

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