1 package CPAN::HandleConfig;
3 use vars qw(%can %keys $VERSION);
5 $VERSION = sprintf "%.6f", substr(q$Rev: 958 $,4)/1000000 + 5.4;
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",
14 %keys = map { $_ => undef } (
15 # allow_unauthenticated ?? some day...
18 "build_requires_install_policy",
25 "commandnumber_in_prompt",
29 "dontload_hash", # deprecated after 1.83_68 (rev. 581)
42 "inhibit_startup_message",
48 "make_install_make_command",
52 "mbuild_install_build_command",
60 "prerequisites_policy",
76 if ($^O eq "MSWin32") {
78 mbuild_install_build_command
79 make_install_make_command
82 if (exists $CPAN::Config->{$k}) {
83 for ("deleting previously set config variable '$k' => '$CPAN::Config->{$k}'") {
84 $CPAN::Frontend ? $CPAN::Frontend->mywarn($_) : warn $_;
86 delete $CPAN::Config->{$k};
91 # returns true on successful action
95 CPAN->debug("self[$self]args[".join(" | ",@args)."]");
96 my($o,$str,$func,$args,$key_exists);
100 $self->$o(args => \@args); # o conf init => sub init => sub load
103 CPAN->debug("o[$o]") if $CPAN::DEBUG;
104 unless (exists $keys{$o}) {
105 $CPAN::Frontend->mywarn("Warning: unknown configuration variable '$o'\n");
110 CPAN->debug("func[$func]") if $CPAN::DEBUG;
112 # Let's avoid eval, it's easier to comprehend without.
113 if ($func eq "push") {
114 push @{$CPAN::Config->{$o}}, @args;
116 } elsif ($func eq "pop") {
117 pop @{$CPAN::Config->{$o}};
119 } elsif ($func eq "shift") {
120 shift @{$CPAN::Config->{$o}};
122 } elsif ($func eq "unshift") {
123 unshift @{$CPAN::Config->{$o}}, @args;
125 } elsif ($func eq "splice") {
126 splice @{$CPAN::Config->{$o}}, @args;
129 $CPAN::Config->{$o} = [@args];
132 $self->prettyprint($o);
135 if ($o eq "urllist") {
136 # reset the cached values
137 undef $CPAN::FTP::Thesite;
138 undef $CPAN::FTP::Themethod;
139 } elsif ($o eq "dontload_list") {
140 # empty it, it will be built up again
141 $CPAN::META->{dontload_hash} = {};
145 } elsif ($o =~ /_hash$/) {
146 @args = () if @args==1 && $args[0] eq "";
147 push @args, "" if @args % 2;
148 $CPAN::Config->{$o} = { @args };
150 $CPAN::Config->{$o} = $args[0] if defined $args[0];
151 $self->prettyprint($o)
152 if exists $keys{$o} or defined $CPAN::Config->{$o};
160 my $v = $CPAN::Config->{$k};
163 if (ref $v eq "ARRAY") {
164 @report = map {"\t[$_]\n"} @$v;
166 @report = map { sprintf("\t%-18s => %s\n",
168 defined $v->{$_} ? $v->{$_} : "UNDEFINED"
171 $CPAN::Frontend->myprint(
181 } elsif (defined $v) {
182 $CPAN::Frontend->myprint(sprintf " %-18s [%s]\n", $k, $v);
184 $CPAN::Frontend->myprint(sprintf " %-18s [%s]\n", $k, "UNDEFINED");
189 my($self,@args) = @_;
192 if ($args[0] eq "args") {
193 # we have not signed that contract
195 $configpm = $args[0];
198 unless (defined $configpm){
199 $configpm ||= $INC{"CPAN/MyConfig.pm"};
200 $configpm ||= $INC{"CPAN/Config.pm"};
201 $configpm || Carp::confess(q{
202 CPAN::Config::commit called without an argument.
203 Please specify a filename where to save the configuration or try
204 "o conf init" to have an interactive course through configing.
209 $mode = (stat $configpm)[2];
210 if ($mode && ! -w _) {
211 Carp::confess("$configpm is not writable");
216 $msg = <<EOF unless $configpm =~ /MyConfig/;
218 # This is CPAN.pm's systemwide configuration file. This file provides
219 # defaults for users, and the values can be changed in a per-user
220 # configuration file. The user-config file is being looked for as
221 # ~/.cpan/CPAN/MyConfig.pm.
225 my($fh) = FileHandle->new;
226 rename $configpm, "$configpm~" if -f $configpm;
227 open $fh, ">$configpm" or
228 $CPAN::Frontend->mydie("Couldn't open >$configpm: $!");
229 $fh->print(qq[$msg\$CPAN::Config = \{\n]);
230 foreach (sort keys %$CPAN::Config) {
231 unless (exists $keys{$_}) {
232 $CPAN::Frontend->mywarn("Dropping unknown config variable '$_'\n");
233 delete $CPAN::Config->{$_};
238 $self->neatvalue($CPAN::Config->{$_}),
243 $fh->print("};\n1;\n__END__\n");
246 #$mode = 0444 | ( $mode & 0111 ? 0111 : 0 );
247 #chmod $mode, $configpm;
248 ###why was that so? $self->defaults;
249 $CPAN::Frontend->myprint("commit: wrote '$configpm'\n");
253 # stolen from MakeMaker; not taking the original because it is buggy;
254 # bugreport will have to say: keys of hashes remain unquoted and can
255 # produce syntax errors
258 return "undef" unless defined $v;
260 return "q[$v]" unless $t;
264 foreach my $elem (@$v) {
265 push @neat, "q[$elem]";
267 push @m, join ", ", @neat;
271 return "$v" unless $t eq 'HASH';
273 while (($key,$val) = each %$v){
274 last unless defined $key; # cautious programming in case (undef,undef) is true
275 push(@m,"q[$key]=>".$self->neatvalue($val)) ;
277 return "{ ".join(', ',@m)." }";
283 for my $config (qw(CPAN/MyConfig.pm CPAN/Config.pm)) {
285 CPAN::Shell->reload_this($config);
286 $CPAN::Frontend->myprint("'$INC{$config}' reread\n");
293 =head2 C<< CLASS->safe_quote ITEM >>
295 Quotes an item to become safe against spaces
296 in shell interpolation. An item is enclosed
299 - the item contains spaces in the middle
300 - the item does not start with a quote
302 This happens to avoid shell interpolation
303 problems when whitespace is present in
306 This method uses C<commands_quote> to determine
307 the correct quote. If C<commands_quote> is
308 a space, no quoting will take place.
311 if it starts and ends with the same quote character: leave it as it is
313 if it contains no whitespace: leave it as it is
315 if it contains whitespace, then
317 if it contains quotes: better leave it as it is
319 else: quote it with the correct quote type for the box we're on
324 # Instead of patching the guess, set commands_quote
326 my ($quotes,$use_quote)
333 my ($self, $command) = @_;
334 # Set up quote/default quote
335 my $quote = $CPAN::Config->{commands_quote} || $quotes;
339 and $command !~ /[$quote]/) {
340 return qq<$use_quote$command$use_quote>
347 my($self,@args) = @_;
348 undef $CPAN::Config->{'inhibit_startup_message'}; # lazy trick to
357 # This is a piece of repeated code that is abstracted here for
358 # maintainability. RMB
361 my($configpmdir, $configpmtest) = @_;
362 if (-w $configpmtest) {
363 return $configpmtest;
364 } elsif (-w $configpmdir) {
365 #_#_# following code dumped core on me with 5.003_11, a.k.
366 my $configpm_bak = "$configpmtest.bak";
367 unlink $configpm_bak if -f $configpm_bak;
368 if( -f $configpmtest ) {
369 if( rename $configpmtest, $configpm_bak ) {
370 $CPAN::Frontend->mywarn(<<END);
371 Old configuration file $configpmtest
372 moved to $configpm_bak
376 my $fh = FileHandle->new;
377 if ($fh->open(">$configpmtest")) {
379 return $configpmtest;
381 # Should never happen
382 Carp::confess("Cannot open >$configpmtest");
387 sub require_myconfig_or_config () {
388 return if $INC{"CPAN/MyConfig.pm"};
391 unshift @INC, File::Spec->catdir($home,'.cpan');
392 eval { require CPAN::MyConfig };
393 my $err_myconfig = $@;
394 if ($err_myconfig and $err_myconfig !~ m#locate CPAN/MyConfig\.pm#) {
395 die "Error while requiring CPAN::MyConfig:\n$err_myconfig";
397 unless ($INC{"CPAN/MyConfig.pm"}) { # this guy has settled his needs already
398 eval {require CPAN::Config;}; # not everybody has one
400 if ($err_config and $err_config !~ m#locate CPAN/Config\.pm#) {
401 die "Error while requiring CPAN::Config:\n$err_config";
408 if ($CPAN::META->has_usable("File::HomeDir")) {
409 $home = File::HomeDir->my_data;
417 my($self, %args) = @_;
418 $CPAN::Be_Silent++ if $args{be_silent};
422 require_myconfig_or_config;
423 return unless @miss = $self->missing_config_data;
425 require CPAN::FirstTime;
426 my($configpm,$fh,$redo,$theycalled);
428 $theycalled++ if @miss==1 && $miss[0] eq 'inhibit_startup_message';
429 if (defined $INC{"CPAN/Config.pm"} && -w $INC{"CPAN/Config.pm"}) {
430 $configpm = $INC{"CPAN/Config.pm"};
432 } elsif (defined $INC{"CPAN/MyConfig.pm"} && -w $INC{"CPAN/MyConfig.pm"}) {
433 $configpm = $INC{"CPAN/MyConfig.pm"};
436 my($path_to_cpan) = File::Basename::dirname($INC{"CPAN.pm"});
437 my($configpmdir) = File::Spec->catdir($path_to_cpan,"CPAN");
438 my($configpmtest) = File::Spec->catfile($configpmdir,"Config.pm");
440 if (-d $configpmdir or File::Path::mkpath($configpmdir)) {
441 $configpm = _configpmtest($configpmdir,$configpmtest);
442 $inc_key = "CPAN/Config.pm";
445 $configpmdir = File::Spec->catdir(home,".cpan","CPAN");
446 File::Path::mkpath($configpmdir);
447 $configpmtest = File::Spec->catfile($configpmdir,"MyConfig.pm");
448 $configpm = _configpmtest($configpmdir,$configpmtest);
449 $inc_key = "CPAN/MyConfig.pm";
452 $INC{$inc_key} = $configpm;
454 my $text = qq{WARNING: CPAN.pm is unable to } .
455 qq{create a configuration file.};
456 output($text, 'confess');
461 if ($redo && ! $theycalled){
462 $CPAN::Frontend->myprint(<<END);
463 Sorry, we have to rerun the configuration dialog for CPAN.pm due to
464 the following indispensable but missing parameters:
468 $args{args} = \@miss;
471 # where do we need this?
472 $CPAN::Frontend->myprint(qq{
473 $configpm initialized.
476 CPAN::FirstTime::init($configpm, %args);
479 sub missing_config_data {
490 "inhibit_startup_message",
497 "mbuild_install_arg",
498 "mbuild_install_build_command",
502 "prerequisites_policy",
508 next unless exists $keys{$_};
509 push @miss, $_ unless defined $CPAN::Config->{$_};
515 $CPAN::Frontend->myprint(q[
517 commit commit session changes to disk
518 defaults reload default config values from disk
520 init enter a dialog to set all or a set of parameters
522 Edit key values as in the following (the "o" is a literal letter o):
523 o conf build_cache 15
524 o conf build_dir "/foo/bar"
526 o conf urllist unshift ftp://ftp.foo.bar/
527 o conf inhibit_startup_message 1
530 undef; #don't reprint CPAN::Config
534 my($word,$line,$pos) = @_;
536 CPAN->debug("word[$word] line[$line] pos[$pos]") if $CPAN::DEBUG;
537 my(@words) = split " ", substr($line,0,$pos+1);
546 @words == 4 && length($word)
549 return grep /^\Q$word\E/, qw(splice shift unshift pop push);
550 } elsif (defined($words[2])
557 @words >= 4 && length($word)
559 return sort grep /^\Q$word\E/, keys %keys;
560 } elsif (@words >= 4) {
564 my(@o_conf) = sort grep { !$seen{$_}++ }
568 return grep /^\Q$word\E/, @o_conf;
573 CPAN::Config; ####::###### #hide from indexer
574 # note: J. Nick Koston wrote me that they are using
575 # CPAN::Config->commit although undocumented. I suggested
576 # CPAN::Shell->o("conf","commit") even when ugly it is at least
579 # that's why I added the CPAN::Config class with autoload and
583 use vars qw($AUTOLOAD $VERSION);
584 $VERSION = sprintf "%.2f", substr(q$Rev: 958 $,4)/100;
586 # formerly CPAN::HandleConfig was known as CPAN::Config
589 $CPAN::Frontend->mywarn("Dispatching deprecated method '$l' to CPAN::HandleConfig");
591 CPAN::HandleConfig->$l(@_);
600 This program is free software; you can redistribute it and/or
601 modify it under the same terms as Perl itself.
607 # cperl-indent-level: 4