diff options
author | Marc Espie <espie@cvs.openbsd.org> | 2014-11-29 10:42:52 +0000 |
---|---|---|
committer | Marc Espie <espie@cvs.openbsd.org> | 2014-11-29 10:42:52 +0000 |
commit | f5cd659fc3f97d3be7b79e8b4860ce2601af631f (patch) | |
tree | 39f1cda33e59ed4449fd0874d491bf02545ebca5 /usr.sbin | |
parent | 5e656b282188a47d0da4c9aa49dcbfc95c38f93a (diff) |
simplify the interactive code into its own little object with simpler
interface, so that most is it interactive tests vanish from the main
program.
Diffstat (limited to 'usr.sbin')
-rw-r--r-- | usr.sbin/pkg_add/OpenBSD/AddCreateDelete.pm | 62 | ||||
-rw-r--r-- | usr.sbin/pkg_add/OpenBSD/AddDelete.pm | 23 | ||||
-rw-r--r-- | usr.sbin/pkg_add/OpenBSD/CollisionReport.pm | 11 | ||||
-rw-r--r-- | usr.sbin/pkg_add/OpenBSD/Interactive.pm | 56 | ||||
-rw-r--r-- | usr.sbin/pkg_add/OpenBSD/PkgAdd.pm | 16 | ||||
-rw-r--r-- | usr.sbin/pkg_add/OpenBSD/PkgCheck.pm | 35 | ||||
-rw-r--r-- | usr.sbin/pkg_add/OpenBSD/PkgDelete.pm | 12 | ||||
-rw-r--r-- | usr.sbin/pkg_add/OpenBSD/Replace.pm | 5 |
8 files changed, 115 insertions, 105 deletions
diff --git a/usr.sbin/pkg_add/OpenBSD/AddCreateDelete.pm b/usr.sbin/pkg_add/OpenBSD/AddCreateDelete.pm index 554dfe0380f..76122626c68 100644 --- a/usr.sbin/pkg_add/OpenBSD/AddCreateDelete.pm +++ b/usr.sbin/pkg_add/OpenBSD/AddCreateDelete.pm @@ -1,5 +1,5 @@ # ex:ts=8 sw=4: -# $OpenBSD: AddCreateDelete.pm,v 1.25 2014/06/03 13:13:53 espie Exp $ +# $OpenBSD: AddCreateDelete.pm,v 1.26 2014/11/29 10:42:51 espie Exp $ # # Copyright (c) 2007-2014 Marc Espie <espie@openbsd.org> # @@ -65,10 +65,45 @@ sub handle_options { my ($state, $opt_string, @usage) = @_; - $state->SUPER::handle_options($opt_string.'L:mnx', @usage); + $state->SUPER::handle_options($opt_string.'IiL:mnx', @usage); $state->progress->setup($state->opt('x'), $state->opt('m'), $state); $state->{not} = $state->opt('n'); + if ($state->opt('i') && $state->opt('I')) { + $state->usage("-i and -I are reverse options, make up your mind"); + } + my $i; + if ($state->opt('i')) { + $i = 1; + } elsif ($state->opt('I')) { + $i = 0; + } else { + $i = -t STDIN; + } + if ($i) { + require OpenBSD::Interactive; + $state->{interactive} = OpenBSD::Interactive->new($state); + } else { + $state->{interactive} = OpenBSD::InteractiveStub->new($state); + } +} + + +sub is_interactive +{ + return shift->{interactive}->is_interactive; +} + +sub confirm +{ + my $self = shift; + return $self->{interactive}->confirm(@_); +} + +sub ask_list +{ + my $self = shift; + return $self->{interactive}->ask_list(@_); } sub vsystem @@ -140,4 +175,27 @@ sub handle_options $state->handle_options($opt_string, $self, @usage); } +package OpenBSD::InteractiveStub; +sub new +{ + my $class = shift; + bless {}, $class; +} + +sub ask_list +{ + my ($self, $prompt, @values) = @_; + return $values[0]; +} + +sub confirm +{ + my ($self, $prompt, $yesno) = @_; + return $yesno; +} + +sub is_interactive +{ + return 0; +} 1; diff --git a/usr.sbin/pkg_add/OpenBSD/AddDelete.pm b/usr.sbin/pkg_add/OpenBSD/AddDelete.pm index 1a1a19da9af..98fa71d1513 100644 --- a/usr.sbin/pkg_add/OpenBSD/AddDelete.pm +++ b/usr.sbin/pkg_add/OpenBSD/AddDelete.pm @@ -1,5 +1,5 @@ # ex:ts=8 sw=4: -# $OpenBSD: AddDelete.pm,v 1.66 2014/07/11 12:50:15 espie Exp $ +# $OpenBSD: AddDelete.pm,v 1.67 2014/11/29 10:42:51 espie Exp $ # # Copyright (c) 2007-2010 Marc Espie <espie@openbsd.org> # @@ -343,9 +343,7 @@ sub choose_location } my %h = map {($_->name, $_)} @$list; - if ($state->{interactive}) { - require OpenBSD::Interactive; - + if ($state->is_interactive) { $h{'<None>'} = undef; $state->progress->clear; my $result = $state->ask_list("Ambiguous: choose package for $name", 1, sort keys %h); @@ -357,23 +355,6 @@ sub choose_location } } -sub confirm -{ - my ($state, $prompt, $default) = @_; - - return 0 if !$state->{interactive}; - require OpenBSD::Interactive; - return OpenBSD::Interactive::confirm($prompt, $default); -} - -sub ask_list -{ - my ($state, $prompt, $interactive, @values) = @_; - - require OpenBSD::Interactive; - return OpenBSD::Interactive::ask_list($prompt, $interactive, @values); -} - sub status { my $self = shift; diff --git a/usr.sbin/pkg_add/OpenBSD/CollisionReport.pm b/usr.sbin/pkg_add/OpenBSD/CollisionReport.pm index d82647aed91..f7cec0615cb 100644 --- a/usr.sbin/pkg_add/OpenBSD/CollisionReport.pm +++ b/usr.sbin/pkg_add/OpenBSD/CollisionReport.pm @@ -1,5 +1,5 @@ # ex:ts=8 sw=4: -# $OpenBSD: CollisionReport.pm,v 1.44 2012/04/28 12:00:10 espie Exp $ +# $OpenBSD: CollisionReport.pm,v 1.45 2014/11/29 10:42:51 espie Exp $ # # Copyright (c) 2003-2006 Marc Espie <espie@openbsd.org> # @@ -134,13 +134,8 @@ sub collision_report } my $dorepair = 0; if ($found == 0) { - if ($state->defines('repair')) { - $dorepair = 1; - } elsif ($state->{interactive}) { - if ($state->confirm("It seems to be a missing package registration\nRepair", 0)) { - $dorepair = 1; - } - } + $dorepair = $state->defines('repair') || + $state->confirm("It seems to be a missing package registration\nRepair", 0)); } if ($dorepair == 1) { for my $f (@$list) { diff --git a/usr.sbin/pkg_add/OpenBSD/Interactive.pm b/usr.sbin/pkg_add/OpenBSD/Interactive.pm index 99a3cd49285..7c72a8689f3 100644 --- a/usr.sbin/pkg_add/OpenBSD/Interactive.pm +++ b/usr.sbin/pkg_add/OpenBSD/Interactive.pm @@ -1,5 +1,5 @@ # ex:ts=8 sw=4: -# $OpenBSD: Interactive.pm,v 1.17 2010/12/24 09:04:14 espie Exp $ +# $OpenBSD: Interactive.pm,v 1.18 2014/11/29 10:42:51 espie Exp $ # # Copyright (c) 2005-2007 Marc Espie <espie@openbsd.org> # @@ -19,25 +19,34 @@ use warnings; package OpenBSD::Interactive; -my $always = 0; +sub new +{ + my ($class, $state) = @_; + bless { + state => $state, + always => 0, + }, $class; +} sub ask_list { - my ($prompt, $interactive, @values) = @_; - if (!$interactive || !-t STDIN || $always) { + my ($self, $prompt, @values) = @_; + if ($self->{always}) { return $values[0]; } - print STDERR $prompt, "\n"; + + $self->{state}->errsay('#1', $prompt); my $i = 0; for my $v (@values) { - printf STDERR "%s\t%2d: %s\n", $i == 0 ? " a" : "" , $i, $v; + $self->{state}->errsay("#1\t#2: #3", + $i == 0 ? "a" : "", $i, $v); $i++; } LOOP: - print STDERR "Your choice: "; + $self->{state}->errprint("Your choice: "); my $result = <STDIN>; unless (defined $result) { - print STDERR "\n"; + $self->{state}->errsay(""); return $values[0]; } chomp $result; @@ -45,40 +54,38 @@ LOOP: return $values[0]; } if ($result eq 'a') { - $always = 1; + $self->{always} = 1; return $values[0]; } if ($result =~ m/^\d+$/o) { if ($result >= 0 && $result < @values) { return $values[$result]; } - print STDERR "invalid numeric value !\n"; + $self->{state}->errsay("invalid numeric value !"); goto LOOP; } if (grep { $result eq $_ } @values) { return $result; } else { - print STDERR "Ambiguous value !\n"; + $self->{state}->errsay("Ambiguous value !"); goto LOOP; } } sub confirm { - my ($prompt, $default) = @_; - if (!-t STDIN) { - return 0; - } - if ($always) { + my ($self, $prompt, $yesno) = @_; + if ($self->{always}) { return 1; } LOOP2: - print STDERR $prompt, $default ? "? [Y/n/a] " : "? [y/N/a] "; + $self->{state}->errprint("#1 ?[#2/a] ", + $prompt, $yesno ? "Y/n" : "y/N"); my $result = <STDIN>; unless(defined $result) { - print STDERR "\n"; - return $default; + $self->{state}->errsay(""); + return $yesno; } chomp $result; $result =~ s/\s+//go; @@ -90,14 +97,19 @@ LOOP2: return 0; } if ($result eq 'a') { - $always = 1; + $self->{always} = 1; return 1; } if ($result eq '') { - return $default; + return $yesno; } - print STDERR "Ambiguous answer\n"; + $self->{state}->errsay("Ambiguous answer"); goto LOOP2; } +sub is_interactive +{ + return 1; +} + 1; diff --git a/usr.sbin/pkg_add/OpenBSD/PkgAdd.pm b/usr.sbin/pkg_add/OpenBSD/PkgAdd.pm index 60e0c7bdf1e..2cd3114d41a 100644 --- a/usr.sbin/pkg_add/OpenBSD/PkgAdd.pm +++ b/usr.sbin/pkg_add/OpenBSD/PkgAdd.pm @@ -1,7 +1,7 @@ #! /usr/bin/perl # ex:ts=8 sw=4: -# $OpenBSD: PkgAdd.pm,v 1.77 2014/11/25 14:16:15 espie Exp $ +# $OpenBSD: PkgAdd.pm,v 1.78 2014/11/29 10:42:51 espie Exp $ # # Copyright (c) 2003-2014 Marc Espie <espie@openbsd.org> # @@ -558,14 +558,10 @@ sub check_forward_dependencies if ($state->defines('updatedepends')) { $state->errsay("Forcing update"); return $no_merge; - } elsif ($state->{interactive}) { - if ($state->confirm("Proceed with update anyway", 0)) { + } elsif ($state->confirm("Proceed with update anyway", 0)) { return $no_merge; - } else { - return undef; - } } else { - return undef; + return undef; } } return 1; @@ -710,11 +706,7 @@ sub check_digital_signature } else { $url = $pkgname; } - if ($state->{interactive}) { - $state->errprint('UNSIGNED PACKAGE #1: ', - $url); - $okay = $state->confirm("install anyway", 0); - } + $okay = $state->confirm("UNSIGNED PACKAGE $url: install anyway", 0); if (!$okay) { $state->fatal("Unsigned package #1", $url); } diff --git a/usr.sbin/pkg_add/OpenBSD/PkgCheck.pm b/usr.sbin/pkg_add/OpenBSD/PkgCheck.pm index 9b5ab64ffd5..0612f189ee7 100644 --- a/usr.sbin/pkg_add/OpenBSD/PkgCheck.pm +++ b/usr.sbin/pkg_add/OpenBSD/PkgCheck.pm @@ -1,7 +1,7 @@ #! /usr/bin/perl # ex:ts=8 sw=4: -# $OpenBSD: PkgCheck.pm,v 1.54 2014/07/27 22:18:36 espie Exp $ +# $OpenBSD: PkgCheck.pm,v 1.55 2014/11/29 10:42:51 espie Exp $ # # Copyright (c) 2003-2014 Marc Espie <espie@openbsd.org> # @@ -306,15 +306,8 @@ sub handle_options my $self = shift; $self->{no_exports} = 1; - $self->SUPER::handle_options('fB:Iiq', + $self->SUPER::handle_options('fB:q', '[-fIimnqvx] [-B pkg-destdir] [-D value]'); - if ($self->opt('i')) { - $self->{interactive} = 1; - } elsif ($self->opt('I')) { - $self->{interactive} = 0; - } else { - $self->{interactive} = -t STDIN; - } $self->{force} = $self->opt('f'); $self->{quick} = $self->opt('q'); if (defined $self->opt('B')) { @@ -390,12 +383,9 @@ sub ask_delete_deps my ($self, $state, $l) = @_; if ($state->{force}) { $self->{req}->delete(@$l); - } elsif ($state->{interactive}) { - require OpenBSD::Interactive; - if (OpenBSD::Interactive::confirm("Remove missing ". + } elsif ($state->confirm("Remove missing ". $state->safe($self->string(@$l)))) { $self->{req}->delete(@$l); - } } } @@ -404,12 +394,9 @@ sub ask_add_deps my ($self, $state, $l) = @_; if ($state->{force}) { $self->{req}->add(@$l); - } elsif ($state->{interactive}) { - require OpenBSD::Interactive; - if (OpenBSD::Interactive::confirm("Add missing ". + } elsif ($state->confirm("Add missing ". $self->string(@$l))) { $self->{req}->add(@$l); - } } } @@ -605,11 +592,8 @@ sub may_remove my ($self, $state, $name) = @_; if ($state->{force}) { $self->remove($state, $name); - } elsif ($state->{interactive}) { - require OpenBSD::Interactive; - if (OpenBSD::Interactive::confirm("Remove wrong package $name")) { + } elsif ($state->confirm("Remove wrong package $name")) { $self->remove($state, $name); - } } $state->{bogus}{$name} = 1; } @@ -740,9 +724,7 @@ sub install_pkglocate if (OpenBSD::PkgSpec->new($spec)->match_ref(\@l)) { return 1; } - require OpenBSD::Interactive; - unless (OpenBSD::Interactive::confirm( - "Unknown file system entries.\n". + unless ($state->confirm("Unknown file system entries.\n". "Do you want to install $spec to look them up")) { return 0; } @@ -785,11 +767,8 @@ sub display_tmps } if ($state->{force}) { unlink(@{$state->{tmps}}); - } elsif ($state->{interactive}) { - require OpenBSD::Interactive; - if (OpenBSD::Interactive::confirm("Remove")) { + } elsif ($state->confirm("Remove")) { unlink(@{$state->{tmps}}); - } } } diff --git a/usr.sbin/pkg_add/OpenBSD/PkgDelete.pm b/usr.sbin/pkg_add/OpenBSD/PkgDelete.pm index 582444d0ffc..96b778ea9bd 100644 --- a/usr.sbin/pkg_add/OpenBSD/PkgDelete.pm +++ b/usr.sbin/pkg_add/OpenBSD/PkgDelete.pm @@ -1,6 +1,6 @@ #!/usr/bin/perl # ex:ts=8 sw=4: -# $OpenBSD: PkgDelete.pm,v 1.33 2014/07/12 19:50:43 espie Exp $ +# $OpenBSD: PkgDelete.pm,v 1.34 2014/11/29 10:42:51 espie Exp $ # # Copyright (c) 2003-2010 Marc Espie <espie@openbsd.org> # @@ -265,10 +265,7 @@ sub delete_dependencies if ($state->defines("dependencies")) { return 1; } - if ($state->{interactive}) { - return $state->confirm("Delete them as well", 0); - } - return 0; + return $state->confirm("Delete them as well", 0); } sub fix_bad_dependencies @@ -278,10 +275,7 @@ sub fix_bad_dependencies if ($state->defines("baddepend")) { return 1; } - if ($state->{interactive}) { - return $state->confirm("Delete anyway", 0); - } - return 0; + return $state->confirm("Delete anyway", 0); } sub process_set diff --git a/usr.sbin/pkg_add/OpenBSD/Replace.pm b/usr.sbin/pkg_add/OpenBSD/Replace.pm index 5fdaeb14401..f76807d70b1 100644 --- a/usr.sbin/pkg_add/OpenBSD/Replace.pm +++ b/usr.sbin/pkg_add/OpenBSD/Replace.pm @@ -1,5 +1,5 @@ # ex:ts=8 sw=4: -# $OpenBSD: Replace.pm,v 1.88 2014/02/11 08:58:34 sthen Exp $ +# $OpenBSD: Replace.pm,v 1.89 2014/11/29 10:42:51 espie Exp $ # # Copyright (c) 2004-2014 Marc Espie <espie@openbsd.org> # @@ -112,8 +112,7 @@ sub is_set_safe if (!$state->defines('paranoid')) { $state->errsay("Running update"); return 1; - } elsif ($state->{interactive}) { - + } elsif ($state->is_interactive) { if ($state->confirm("proceed with update anyway", 0)) { return 1; } else { |