Upgrade to CPAN 1.87_63
[p5sagit/p5-mst-13.2.git] / lib / CPAN / HandleConfig.pm
1 package CPAN::HandleConfig;
2 use strict;
3 use vars qw(%can %keys $VERSION);
4
5 $VERSION = sprintf "%.6f", substr(q$Rev: 826 $,4)/1000000 + 5.4;
6
7 %can = (
8         commit   => "Commit changes to disk",
9         defaults => "Reload defaults from disk",
10         help     => "Short help about 'o conf' usage",
11         init     => "Interactive setting of all options",
12 );
13
14 %keys = map { $_ => undef } (
15                              #  allow_unauthenticated ?? some day...
16                              "build_cache",
17                              "build_dir",
18                              "bzip2",
19                              "cache_metadata",
20                              "check_sigs",
21                              "colorize_output",
22                              "colorize_print",
23                              "colorize_warn",
24                              "commandnumber_in_prompt",
25                              "cpan_home",
26                              "curl",
27                              "dontload_hash", # deprecated after 1.83_68 (rev. 581)
28                              "dontload_list",
29                              "ftp",
30                              "ftp_passive",
31                              "ftp_proxy",
32                              "getcwd",
33                              "gpg",
34                              "gzip",
35                              "histfile",
36                              "histsize",
37                              "http_proxy",
38                              "inactivity_timeout",
39                              "index_expire",
40                              "inhibit_startup_message",
41                              "keep_source_where",
42                              "lynx",
43                              "make",
44                              "make_arg",
45                              "make_install_arg",
46                              "make_install_make_command",
47                              "makepl_arg",
48                              "mbuild_arg",
49                              "mbuild_install_arg",
50                              "mbuild_install_build_command",
51                              "mbuildpl_arg",
52                              "ncftp",
53                              "ncftpget",
54                              "no_proxy",
55                              "pager",
56                              "password",
57                              "prefer_installer",
58                              "prerequisites_policy",
59                              "proxy_pass",
60                              "proxy_user",
61                              "scan_cache",
62                              "shell",
63                              "show_upload_date",
64                              "tar",
65                              "term_is_latin",
66                              "term_ornaments",
67                              "test_report",
68                              "unzip",
69                              "urllist",
70                              "username",
71                              "wait_list",
72                              "wget",
73                             );
74 if ($^O eq "MSWin32") {
75     for my $k (qw(
76                   mbuild_install_build_command
77                   make_install_make_command
78                  )) {
79         delete $keys{$k};
80         if (exists $CPAN::Config->{$k}) {
81             for ("deleting previously set config variable '$k' => '$CPAN::Config->{$k}'") {
82                 $CPAN::Frontend ? $CPAN::Frontend->mywarn($_) : warn $_;
83             }
84             delete $CPAN::Config->{$k};
85         }
86     }
87 }
88
89 # returns true on successful action
90 sub edit {
91     my($self,@args) = @_;
92     return unless @args;
93     CPAN->debug("self[$self]args[".join(" | ",@args)."]");
94     my($o,$str,$func,$args,$key_exists);
95     $o = shift @args;
96     $DB::single = 1;
97     if($can{$o}) {
98         $self->$o(args => \@args); # o conf init => sub init => sub load
99         return 1;
100     } else {
101         CPAN->debug("o[$o]") if $CPAN::DEBUG;
102         unless (exists $keys{$o}) {
103             $CPAN::Frontend->mywarn("Warning: unknown configuration variable '$o'\n");
104         }
105         if ($o =~ /list$/) {
106             $func = shift @args;
107             $func ||= "";
108             CPAN->debug("func[$func]") if $CPAN::DEBUG;
109             my $changed;
110             # Let's avoid eval, it's easier to comprehend without.
111             if ($func eq "push") {
112                 push @{$CPAN::Config->{$o}}, @args;
113                 $changed = 1;
114             } elsif ($func eq "pop") {
115                 pop @{$CPAN::Config->{$o}};
116                 $changed = 1;
117             } elsif ($func eq "shift") {
118                 shift @{$CPAN::Config->{$o}};
119                 $changed = 1;
120             } elsif ($func eq "unshift") {
121                 unshift @{$CPAN::Config->{$o}}, @args;
122                 $changed = 1;
123             } elsif ($func eq "splice") {
124                 splice @{$CPAN::Config->{$o}}, @args;
125                 $changed = 1;
126             } elsif (@args) {
127                 $CPAN::Config->{$o} = [@args];
128                 $changed = 1;
129             } else {
130                 $self->prettyprint($o);
131             }
132             if ($changed) {
133                 if ($o eq "urllist") {
134                     # reset the cached values
135                     undef $CPAN::FTP::Thesite;
136                     undef $CPAN::FTP::Themethod;
137                 } elsif ($o eq "dontload_list") {
138                     # empty it, it will be built up again
139                     $CPAN::META->{dontload_hash} = {};
140                 }
141             }
142             return $changed;
143         } elsif ($o =~ /_hash$/) {
144             @args = () if @args==1 && $args[0] eq "";
145             push @args, "" if @args % 2;
146             $CPAN::Config->{$o} = { @args };
147         } else {
148             $CPAN::Config->{$o} = $args[0] if defined $args[0];
149             $self->prettyprint($o);
150             return 1;
151         }
152     }
153 }
154
155 sub prettyprint {
156   my($self,$k) = @_;
157   my $v = $CPAN::Config->{$k};
158   if (ref $v) {
159     my(@report);
160     if (ref $v eq "ARRAY") {
161       @report = map {"\t[$_]\n"} @$v;
162     } else {
163       @report = map { sprintf("\t%-18s => %s\n",
164                               map { "[$_]" } $_,
165                               defined $v->{$_} ? $v->{$_} : "UNDEFINED"
166                              )} keys %$v;
167     }
168     $CPAN::Frontend->myprint(
169                              join(
170                                   "",
171                                   sprintf(
172                                           "    %-18s\n",
173                                           $k
174                                          ),
175                                   @report
176                                  )
177                             );
178   } elsif (defined $v) {
179     $CPAN::Frontend->myprint(sprintf "    %-18s [%s]\n", $k, $v);
180   } else {
181     $CPAN::Frontend->myprint(sprintf "    %-18s [%s]\n", $k, "UNDEFINED");
182   }
183 }
184
185 sub commit {
186     my($self,@args) = @_;
187     my $configpm;
188     if (@args) {
189       if ($args[0] eq "args") {
190         # we have not signed that contract
191       } else {
192         $configpm = $args[0];
193       }
194     }
195     unless (defined $configpm){
196         $configpm ||= $INC{"CPAN/MyConfig.pm"};
197         $configpm ||= $INC{"CPAN/Config.pm"};
198         $configpm || Carp::confess(q{
199 CPAN::Config::commit called without an argument.
200 Please specify a filename where to save the configuration or try
201 "o conf init" to have an interactive course through configing.
202 });
203     }
204     my($mode);
205     if (-f $configpm) {
206         $mode = (stat $configpm)[2];
207         if ($mode && ! -w _) {
208             Carp::confess("$configpm is not writable");
209         }
210     }
211
212     my $msg;
213     $msg = <<EOF unless $configpm =~ /MyConfig/;
214
215 # This is CPAN.pm's systemwide configuration file. This file provides
216 # defaults for users, and the values can be changed in a per-user
217 # configuration file. The user-config file is being looked for as
218 # ~/.cpan/CPAN/MyConfig.pm.
219
220 EOF
221     $msg ||= "\n";
222     my($fh) = FileHandle->new;
223     rename $configpm, "$configpm~" if -f $configpm;
224     open $fh, ">$configpm" or
225         $CPAN::Frontend->mydie("Couldn't open >$configpm: $!");
226     $fh->print(qq[$msg\$CPAN::Config = \{\n]);
227     foreach (sort keys %$CPAN::Config) {
228         unless (exists $keys{$_}) {
229             $CPAN::Frontend->mywarn("Dropping unknown config variable '$_'\n");
230             delete $CPAN::Config->{$_};
231             next;
232         }
233         $fh->print(
234                    "  '$_' => ",
235                    $self->neatvalue($CPAN::Config->{$_}),
236                    ",\n"
237                   );
238     }
239
240     $fh->print("};\n1;\n__END__\n");
241     close $fh;
242
243     #$mode = 0444 | ( $mode & 0111 ? 0111 : 0 );
244     #chmod $mode, $configpm;
245 ###why was that so?    $self->defaults;
246     $CPAN::Frontend->myprint("commit: wrote '$configpm'\n");
247     1;
248 }
249
250 # stolen from MakeMaker; not taking the original because it is buggy;
251 # bugreport will have to say: keys of hashes remain unquoted and can
252 # produce syntax errors
253 sub neatvalue {
254     my($self, $v) = @_;
255     return "undef" unless defined $v;
256     my($t) = ref $v;
257     return "q[$v]" unless $t;
258     if ($t eq 'ARRAY') {
259         my(@m, @neat);
260         push @m, "[";
261         foreach my $elem (@$v) {
262             push @neat, "q[$elem]";
263         }
264         push @m, join ", ", @neat;
265         push @m, "]";
266         return join "", @m;
267     }
268     return "$v" unless $t eq 'HASH';
269     my(@m, $key, $val);
270     while (($key,$val) = each %$v){
271         last unless defined $key; # cautious programming in case (undef,undef) is true
272         push(@m,"q[$key]=>".$self->neatvalue($val)) ;
273     }
274     return "{ ".join(', ',@m)." }";
275 }
276
277 sub defaults {
278     my($self) = @_;
279     my $done;
280     for my $config (qw(CPAN/MyConfig.pm CPAN/Config.pm)) {
281       CPAN::Shell->reload_this($config) and $done++;
282       $CPAN::Frontend->myprint("'$config' reread\n");
283       last if $done;
284     }
285     1;
286 }
287
288 =head2 C<< CLASS->safe_quote ITEM >>
289
290 Quotes an item to become safe against spaces
291 in shell interpolation. An item is enclosed
292 in double quotes if:
293
294   - the item contains spaces in the middle
295   - the item does not start with a quote
296
297 This happens to avoid shell interpolation
298 problems when whitespace is present in
299 directory names.
300
301 This method uses C<commands_quote> to determine
302 the correct quote. If C<commands_quote> is
303 a space, no quoting will take place.
304
305
306 if it starts and ends with the same quote character: leave it as it is
307
308 if it contains no whitespace: leave it as it is
309
310 if it contains whitespace, then
311
312 if it contains quotes: better leave it as it is
313
314 else: quote it with the correct quote type for the box we're on
315
316 =cut
317
318 {
319     # Instead of patching the guess, set commands_quote
320     # to the right value
321     my ($quotes,$use_quote)
322         = $^O eq 'MSWin32'
323             ? ('"', '"')
324                 : (q<"'>, "'")
325                     ;
326
327     sub safe_quote {
328         my ($self, $command) = @_;
329         # Set up quote/default quote
330         my $quote = $CPAN::Config->{commands_quote} || $quotes;
331
332         if ($quote ne ' '
333             and $command =~ /\s/
334             and $command !~ /[$quote]/) {
335             return qq<$use_quote$command$use_quote>
336         }
337         return $command;
338     }
339 }
340
341 sub init {
342     my($self,@args) = @_;
343     undef $CPAN::Config->{'inhibit_startup_message'}; # lazy trick to
344                                                       # have the least
345                                                       # important
346                                                       # variable
347                                                       # undefined
348     $self->load(@args);
349     1;
350 }
351
352 # This is a piece of repeated code that is abstracted here for
353 # maintainability.  RMB
354 #
355 sub _configpmtest {
356     my($configpmdir, $configpmtest) = @_; 
357     if (-w $configpmtest) {
358         return $configpmtest;
359     } elsif (-w $configpmdir) {
360         #_#_# following code dumped core on me with 5.003_11, a.k.
361         my $configpm_bak = "$configpmtest.bak";
362         unlink $configpm_bak if -f $configpm_bak;
363         if( -f $configpmtest ) {
364             if( rename $configpmtest, $configpm_bak ) {
365                                 $CPAN::Frontend->mywarn(<<END);
366 Old configuration file $configpmtest
367     moved to $configpm_bak
368 END
369             }
370         }
371         my $fh = FileHandle->new;
372         if ($fh->open(">$configpmtest")) {
373             $fh->print("1;\n");
374             return $configpmtest;
375         } else {
376             # Should never happen
377             Carp::confess("Cannot open >$configpmtest");
378         }
379     } else { return }
380 }
381
382 sub require_myconfig_or_config () {
383     return if $INC{"CPAN/MyConfig.pm"};
384     local @INC = @INC;
385     my $home = home();
386     unshift @INC, File::Spec->catdir($home,'.cpan');
387     eval { require CPAN::MyConfig };
388     my $err_myconfig = $@;
389     if ($err_myconfig and $err_myconfig !~ m#locate CPAN/MyConfig\.pm#) {
390         die "Error while requiring CPAN::MyConfig:\n$err_myconfig";
391     }
392     unless ($INC{"CPAN/MyConfig.pm"}) { # this guy has settled his needs already
393       eval {require CPAN::Config;}; # not everybody has one
394       my $err_config = $@;
395       if ($err_config and $err_config !~ m#locate CPAN/Config\.pm#) {
396           die "Error while requiring CPAN::Config:\n$err_config";
397       }
398     }
399 }
400
401 sub home () {
402     my $home;
403     if ($CPAN::META->has_usable("File::HomeDir")) {
404         $home = File::HomeDir->my_data;
405     } else {
406         $home = $ENV{HOME};
407     }
408     $home;
409 }
410
411 sub load {
412     my($self, %args) = @_;
413         $CPAN::Be_Silent++ if $args{be_silent};
414
415     my(@miss);
416     use Carp;
417     require_myconfig_or_config;
418     return unless @miss = $self->missing_config_data;
419
420     require CPAN::FirstTime;
421     my($configpm,$fh,$redo,$theycalled);
422     $redo ||= "";
423     $theycalled++ if @miss==1 && $miss[0] eq 'inhibit_startup_message';
424     if (defined $INC{"CPAN/Config.pm"} && -w $INC{"CPAN/Config.pm"}) {
425         $configpm = $INC{"CPAN/Config.pm"};
426         $redo++;
427     } elsif (defined $INC{"CPAN/MyConfig.pm"} && -w $INC{"CPAN/MyConfig.pm"}) {
428         $configpm = $INC{"CPAN/MyConfig.pm"};
429         $redo++;
430     } else {
431         my($path_to_cpan) = File::Basename::dirname($INC{"CPAN.pm"});
432         my($configpmdir) = File::Spec->catdir($path_to_cpan,"CPAN");
433         my($configpmtest) = File::Spec->catfile($configpmdir,"Config.pm");
434         my $inc_key;
435         if (-d $configpmdir or File::Path::mkpath($configpmdir)) {
436             $configpm = _configpmtest($configpmdir,$configpmtest);
437             $inc_key = "CPAN/Config.pm";
438         }
439         unless ($configpm) {
440             $configpmdir = File::Spec->catdir(home,".cpan","CPAN");
441             File::Path::mkpath($configpmdir);
442             $configpmtest = File::Spec->catfile($configpmdir,"MyConfig.pm");
443             $configpm = _configpmtest($configpmdir,$configpmtest);
444             $inc_key = "CPAN/MyConfig.pm";
445         }
446         if ($configpm) {
447           $INC{$inc_key} = $configpm;
448         } else {
449           my $text = qq{WARNING: CPAN.pm is unable to } .
450               qq{create a configuration file.};
451           output($text, 'confess');
452         }
453
454     }
455     local($") = ", ";
456     if ($redo && ! $theycalled){
457         $CPAN::Frontend->myprint(<<END);
458 Sorry, we have to rerun the configuration dialog for CPAN.pm due to
459 the following indispensable but missing parameters:
460
461 @miss
462 END
463         $args{args} = ["\\b".join("|",@miss)."\\b"];
464     }
465     if (0) {
466         # where do we need this?
467         $CPAN::Frontend->myprint(qq{
468 $configpm initialized.
469 });
470     }
471     CPAN::FirstTime::init($configpm, %args);
472 }
473
474 sub missing_config_data {
475     my(@miss);
476     for (
477          "build_cache",
478          "build_dir",
479          "cache_metadata",
480          "cpan_home",
481          "ftp_proxy",
482          #"gzip",
483          "http_proxy",
484          "index_expire",
485          "inhibit_startup_message",
486          "keep_source_where",
487          #"make",
488          "make_arg",
489          "make_install_arg",
490          "makepl_arg",
491          "mbuild_arg",
492          "mbuild_install_arg",
493          "mbuild_install_build_command",
494          "mbuildpl_arg",
495          "no_proxy",
496          #"pager",
497          "prerequisites_policy",
498          "scan_cache",
499          #"tar",
500          #"unzip",
501          "urllist",
502         ) {
503         next unless exists $keys{$_};
504         push @miss, $_ unless defined $CPAN::Config->{$_};
505     }
506     return @miss;
507 }
508
509 sub help {
510     $CPAN::Frontend->myprint(q[
511 Known options:
512   commit    commit session changes to disk
513   defaults  reload default config values from disk
514   help      this help
515   init      go through a dialog to set all parameters
516
517 Edit key values as in the following (the "o" is a literal letter o):
518   o conf build_cache 15
519   o conf build_dir "/foo/bar"
520   o conf urllist shift
521   o conf urllist unshift ftp://ftp.foo.bar/
522   o conf inhibit_startup_message 1
523
524 ]);
525     undef; #don't reprint CPAN::Config
526 }
527
528 sub cpl {
529     my($word,$line,$pos) = @_;
530     $word ||= "";
531     CPAN->debug("word[$word] line[$line] pos[$pos]") if $CPAN::DEBUG;
532     my(@words) = split " ", substr($line,0,$pos+1);
533     if (
534         defined($words[2])
535         and
536         $words[2] =~ /list$/
537         and
538         (
539          @words == 3
540          ||
541          @words == 4 && length($word)
542         )
543        ) {
544         return grep /^\Q$word\E/, qw(splice shift unshift pop push);
545     } elsif (defined($words[2])
546              and
547              $words[2] eq "init"
548              and
549             (
550              @words == 3
551              ||
552              @words == 4 && length($word)
553             )) {
554         return sort grep /^\Q$word\E/, keys %keys;
555     } elsif (@words >= 4) {
556         return ();
557     }
558     my %seen;
559     my(@o_conf) =  sort grep { !$seen{$_}++ }
560         keys %can,
561             keys %$CPAN::Config,
562                 keys %keys;
563     return grep /^\Q$word\E/, @o_conf;
564 }
565
566
567 package
568     CPAN::Config; ####::###### #hide from indexer
569 # note: J. Nick Koston wrote me that they are using
570 # CPAN::Config->commit although undocumented. I suggested
571 # CPAN::Shell->o("conf","commit") even when ugly it is at least
572 # documented
573
574 # that's why I added the CPAN::Config class with autoload and
575 # deprecated warning
576
577 use strict;
578 use vars qw($AUTOLOAD $VERSION);
579 $VERSION = sprintf "%.2f", substr(q$Rev: 826 $,4)/100;
580
581 # formerly CPAN::HandleConfig was known as CPAN::Config
582 sub AUTOLOAD {
583   my($l) = $AUTOLOAD;
584   $CPAN::Frontend->mywarn("Dispatching deprecated method '$l' to CPAN::HandleConfig");
585   $l =~ s/.*:://;
586   CPAN::HandleConfig->$l(@_);
587 }
588
589 1;
590
591 __END__
592 # Local Variables:
593 # mode: cperl
594 # cperl-indent-level: 4
595 # End: