1 package CPAN::HandleConfig;
3 use vars qw(%can %keys $VERSION);
5 $VERSION = sprintf "%.6f", substr(q$Rev: 740 $,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...
21 "commandnumber_in_prompt",
24 "dontload_hash", # deprecated after 1.83_68 (rev. 581)
37 "inhibit_startup_message",
43 "make_install_make_command",
47 "mbuild_install_build_command",
55 "prerequisites_policy",
68 if ($^O eq "MSWin32") {
70 mbuild_install_build_command
71 make_install_make_command
74 if (exists $CPAN::Config->{$k}) {
75 for ("deleting previously set config variable '$k' => '$CPAN::Config->{$k}'") {
76 $CPAN::Frontend ? $CPAN::Frontend->mywarn($_) : warn $_;
78 delete $CPAN::Config->{$k};
83 # returns true on successful action
87 CPAN->debug("self[$self]args[".join(" | ",@args)."]");
88 my($o,$str,$func,$args,$key_exists);
92 $self->$o(args => \@args);
95 CPAN->debug("o[$o]") if $CPAN::DEBUG;
96 unless (exists $keys{$o}) {
97 $CPAN::Frontend->mywarn("Warning: unknown configuration variable '$o'\n");
102 CPAN->debug("func[$func]") if $CPAN::DEBUG;
104 # Let's avoid eval, it's easier to comprehend without.
105 if ($func eq "push") {
106 push @{$CPAN::Config->{$o}}, @args;
108 } elsif ($func eq "pop") {
109 pop @{$CPAN::Config->{$o}};
111 } elsif ($func eq "shift") {
112 shift @{$CPAN::Config->{$o}};
114 } elsif ($func eq "unshift") {
115 unshift @{$CPAN::Config->{$o}}, @args;
117 } elsif ($func eq "splice") {
118 splice @{$CPAN::Config->{$o}}, @args;
121 $CPAN::Config->{$o} = [@args];
124 $self->prettyprint($o);
127 if ($o eq "urllist") {
128 # reset the cached values
129 undef $CPAN::FTP::Thesite;
130 undef $CPAN::FTP::Themethod;
131 } elsif ($o eq "dontload_list") {
132 # empty it, it will be built up again
133 $CPAN::META->{dontload_hash} = {};
137 } elsif ($o =~ /_hash$/) {
138 @args = () if @args==1 && $args[0] eq "";
139 push @args, "" if @args % 2;
140 $CPAN::Config->{$o} = { @args };
142 $CPAN::Config->{$o} = $args[0] if defined $args[0];
143 $self->prettyprint($o);
150 my $v = $CPAN::Config->{$k};
153 if (ref $v eq "ARRAY") {
154 @report = map {"\t[$_]\n"} @$v;
156 @report = map { sprintf("\t%-18s => %s\n",
158 defined $v->{$_} ? $v->{$_} : "UNDEFINED"
161 $CPAN::Frontend->myprint(
171 } elsif (defined $v) {
172 $CPAN::Frontend->myprint(sprintf " %-18s [%s]\n", $k, $v);
174 $CPAN::Frontend->myprint(sprintf " %-18s [%s]\n", $k, "UNDEFINED");
179 my($self,@args) = @_;
182 if ($args[0] eq "args") {
183 # we have not signed that contract
185 $configpm = $args[0];
188 unless (defined $configpm){
189 $configpm ||= $INC{"CPAN/MyConfig.pm"};
190 $configpm ||= $INC{"CPAN/Config.pm"};
191 $configpm || Carp::confess(q{
192 CPAN::Config::commit called without an argument.
193 Please specify a filename where to save the configuration or try
194 "o conf init" to have an interactive course through configing.
199 $mode = (stat $configpm)[2];
200 if ($mode && ! -w _) {
201 Carp::confess("$configpm is not writable");
206 $msg = <<EOF unless $configpm =~ /MyConfig/;
208 # This is CPAN.pm's systemwide configuration file. This file provides
209 # defaults for users, and the values can be changed in a per-user
210 # configuration file. The user-config file is being looked for as
211 # ~/.cpan/CPAN/MyConfig.pm.
215 my($fh) = FileHandle->new;
216 rename $configpm, "$configpm~" if -f $configpm;
217 open $fh, ">$configpm" or
218 $CPAN::Frontend->mydie("Couldn't open >$configpm: $!");
219 $fh->print(qq[$msg\$CPAN::Config = \{\n]);
220 foreach (sort keys %$CPAN::Config) {
221 unless (exists $keys{$_}) {
222 $CPAN::Frontend->mywarn("Dropping unknown config variable '$_'\n");
223 delete $CPAN::Config->{$_};
228 $self->neatvalue($CPAN::Config->{$_}),
233 $fh->print("};\n1;\n__END__\n");
236 #$mode = 0444 | ( $mode & 0111 ? 0111 : 0 );
237 #chmod $mode, $configpm;
238 ###why was that so? $self->defaults;
239 $CPAN::Frontend->myprint("commit: wrote '$configpm'\n");
243 # stolen from MakeMaker; not taking the original because it is buggy;
244 # bugreport will have to say: keys of hashes remain unquoted and can
245 # produce syntax errors
248 return "undef" unless defined $v;
250 return "q[$v]" unless $t;
254 foreach my $elem (@$v) {
255 push @neat, "q[$elem]";
257 push @m, join ", ", @neat;
261 return "$v" unless $t eq 'HASH';
263 while (($key,$val) = each %$v){
264 last unless defined $key; # cautious programming in case (undef,undef) is true
265 push(@m,"q[$key]=>".$self->neatvalue($val)) ;
267 return "{ ".join(', ',@m)." }";
273 for my $config (qw(CPAN/MyConfig.pm CPAN/Config.pm)) {
274 CPAN::Shell->reload_this($config) and $done++;
280 =head2 C<< CLASS->safe_quote ITEM >>
282 Quotes an item to become safe against spaces
283 in shell interpolation. An item is enclosed
286 - the item contains spaces in the middle
287 - the item does not start with a quote
289 This happens to avoid shell interpolation
290 problems when whitespace is present in
293 This method uses C<commands_quote> to determine
294 the correct quote. If C<commands_quote> is
295 a space, no quoting will take place.
298 if it starts and ends with the same quote character: leave it as it is
300 if it contains no whitespace: leave it as it is
302 if it contains whitespace, then
304 if it contains quotes: better leave it as it is
306 else: quote it with the correct quote type for the box we're on
311 # Instead of patching the guess, set commands_quote
313 my ($quotes,$use_quote)
320 my ($self, $command) = @_;
321 # Set up quote/default quote
322 my $quote = $CPAN::Config->{commands_quote} || $quotes;
326 and $command !~ /[$quote]/) {
327 return qq<$use_quote$command$use_quote>
334 my($self,@args) = @_;
335 undef $CPAN::Config->{'inhibit_startup_message'}; # lazy trick to
344 # This is a piece of repeated code that is abstracted here for
345 # maintainability. RMB
348 my($configpmdir, $configpmtest) = @_;
349 if (-w $configpmtest) {
350 return $configpmtest;
351 } elsif (-w $configpmdir) {
352 #_#_# following code dumped core on me with 5.003_11, a.k.
353 my $configpm_bak = "$configpmtest.bak";
354 unlink $configpm_bak if -f $configpm_bak;
355 if( -f $configpmtest ) {
356 if( rename $configpmtest, $configpm_bak ) {
357 $CPAN::Frontend->mywarn(<<END);
358 Old configuration file $configpmtest
359 moved to $configpm_bak
363 my $fh = FileHandle->new;
364 if ($fh->open(">$configpmtest")) {
366 return $configpmtest;
368 # Should never happen
369 Carp::confess("Cannot open >$configpmtest");
374 sub require_myconfig_or_config () {
375 return if $INC{"CPAN/MyConfig.pm"};
378 unshift @INC, File::Spec->catdir($home,'.cpan');
379 eval { require CPAN::MyConfig };
380 my $err_myconfig = $@;
381 if ($err_myconfig and $err_myconfig !~ m#locate CPAN/MyConfig\.pm#) {
382 die "Error while requiring CPAN::MyConfig:\n$err_myconfig";
384 unless ($INC{"CPAN/MyConfig.pm"}) { # this guy has settled his needs already
385 eval {require CPAN::Config;}; # not everybody has one
387 if ($err_config and $err_config !~ m#locate CPAN/Config\.pm#) {
388 die "Error while requiring CPAN::Config:\n$err_config";
395 if ($CPAN::META->has_usable("File::HomeDir")) {
396 $home = File::HomeDir->my_data;
404 my($self, %args) = @_;
405 $CPAN::Be_Silent++ if $args{be_silent};
409 require_myconfig_or_config;
410 return unless @miss = $self->missing_config_data;
412 require CPAN::FirstTime;
413 my($configpm,$fh,$redo,$theycalled);
415 $theycalled++ if @miss==1 && $miss[0] eq 'inhibit_startup_message';
416 if (defined $INC{"CPAN/Config.pm"} && -w $INC{"CPAN/Config.pm"}) {
417 $configpm = $INC{"CPAN/Config.pm"};
419 } elsif (defined $INC{"CPAN/MyConfig.pm"} && -w $INC{"CPAN/MyConfig.pm"}) {
420 $configpm = $INC{"CPAN/MyConfig.pm"};
423 my($path_to_cpan) = File::Basename::dirname($INC{"CPAN.pm"});
424 my($configpmdir) = File::Spec->catdir($path_to_cpan,"CPAN");
425 my($configpmtest) = File::Spec->catfile($configpmdir,"Config.pm");
427 if (-d $configpmdir or File::Path::mkpath($configpmdir)) {
428 $configpm = _configpmtest($configpmdir,$configpmtest);
429 $inc_key = "CPAN/Config.pm";
432 $configpmdir = File::Spec->catdir(home,".cpan","CPAN");
433 File::Path::mkpath($configpmdir);
434 $configpmtest = File::Spec->catfile($configpmdir,"MyConfig.pm");
435 $configpm = _configpmtest($configpmdir,$configpmtest);
436 $inc_key = "CPAN/MyConfig.pm";
439 $INC{$inc_key} = $configpm;
441 my $text = qq{WARNING: CPAN.pm is unable to } .
442 qq{create a configuration file.};
443 output($text, 'confess');
448 $CPAN::Frontend->myprint(<<END) if $redo && ! $theycalled;
449 Sorry, we have to rerun the configuration dialog for CPAN.pm due to
450 the following indispensable but missing parameters:
454 $CPAN::Frontend->myprint(qq{
455 $configpm initialized.
459 CPAN::FirstTime::init($configpm, %args);
462 sub missing_config_data {
473 "inhibit_startup_message",
480 "mbuild_install_arg",
481 "mbuild_install_build_command",
485 "prerequisites_policy",
491 next unless exists $keys{$_};
492 push @miss, $_ unless defined $CPAN::Config->{$_};
498 $CPAN::Frontend->myprint(q[
500 commit commit session changes to disk
501 defaults reload default config values from disk
503 init go through a dialog to set all parameters
505 Edit key values as in the following (the "o" is a literal letter o):
506 o conf build_cache 15
507 o conf build_dir "/foo/bar"
509 o conf urllist unshift ftp://ftp.foo.bar/
510 o conf inhibit_startup_message 1
513 undef; #don't reprint CPAN::Config
517 my($word,$line,$pos) = @_;
519 CPAN->debug("word[$word] line[$line] pos[$pos]") if $CPAN::DEBUG;
520 my(@words) = split " ", substr($line,0,$pos+1);
525 $words[2] =~ /list$/ && @words == 3
527 $words[2] =~ /list$/ && @words == 4 && length($word)
530 return grep /^\Q$word\E/, qw(splice shift unshift pop push);
531 } elsif (@words >= 4) {
535 my(@o_conf) = sort grep { !$seen{$_}++ }
539 return grep /^\Q$word\E/, @o_conf;
544 CPAN::Config; ####::###### #hide from indexer
545 # note: J. Nick Koston wrote me that they are using
546 # CPAN::Config->commit although undocumented. I suggested
547 # CPAN::Shell->o("conf","commit") even when ugly it is at least
550 # that's why I added the CPAN::Config class with autoload and
554 use vars qw($AUTOLOAD $VERSION);
555 $VERSION = sprintf "%.2f", substr(q$Rev: 740 $,4)/100;
557 # formerly CPAN::HandleConfig was known as CPAN::Config
560 $CPAN::Frontend->mywarn("Dispatching deprecated method '$l' to CPAN::HandleConfig");
562 CPAN::HandleConfig->$l(@_);
570 # cperl-indent-level: 4