do not print to STDOUT if we're in background, as requested by Theo
authorespie <espie@openbsd.org>
Sat, 30 Jan 2021 11:19:01 +0000 (11:19 +0000)
committerespie <espie@openbsd.org>
Sat, 30 Jan 2021 11:19:01 +0000 (11:19 +0000)
who pointed at ssh code for process group handling. Thanks

usr.sbin/pkg_add/OpenBSD/ProgressMeter.pm
usr.sbin/pkg_add/OpenBSD/ProgressMeter/Term.pm
usr.sbin/pkg_add/OpenBSD/State.pm

index 01b755f..7c09d19 100644 (file)
@@ -1,5 +1,5 @@
 # ex:ts=8 sw=4:
-# $OpenBSD: ProgressMeter.pm,v 1.50 2021/01/30 10:43:43 espie Exp $
+# $OpenBSD: ProgressMeter.pm,v 1.51 2021/01/30 11:19:01 espie Exp $
 #
 # Copyright (c) 2010 Marc Espie <espie@openbsd.org>
 #
@@ -90,6 +90,12 @@ sub handle_continue
        $self->{continued} = 1;
 }
 
+sub can_output
+{
+       my $self = shift;
+       return $self->{state}->can_output;
+}
+
 # stub class when no actual progressmeter that still prints out.
 package OpenBSD::ProgressMeter::Stub;
 our @ISA = qw(OpenBSD::ProgressMeter);
index eb5150f..e3ec480 100644 (file)
@@ -1,5 +1,5 @@
 # ex:ts=8 sw=4:
-# $OpenBSD: Term.pm,v 1.41 2021/01/30 10:43:43 espie Exp $
+# $OpenBSD: Term.pm,v 1.42 2021/01/30 11:19:01 espie Exp $
 #
 # Copyright (c) 2004-2007 Marc Espie <espie@openbsd.org>
 #
@@ -202,6 +202,7 @@ sub _show
 sub message
 {
        my ($self, $message) = @_;
+       return unless $self->can_output;
        if ($self->{cleareol}) {
                $message .= $self->{cleareol};
        } elsif ($self->{playfield} > length($message)) {
@@ -218,6 +219,8 @@ sub show
 {
        my ($self, $current, $total) = @_;
 
+       return unless $self->can_output;
+
        if ($self->{playfield}) {
                my $stars = int (($current * $self->{playfield}) / $total + 0.5);
                my $percent = int (($current * 100)/$total + 0.5);
@@ -244,10 +247,12 @@ sub clear
 {
        my $self = shift;
        return unless length($self->{lastdisplay}) > 0;
-       if ($self->{cleareol}) {
-               print "\r", $self->{cleareol};
-       } else {
-               print "\r", ' 'x length($self->{lastdisplay}), "\r";
+       if ($self->can_output) {
+               if ($self->{cleareol}) {
+                       print "\r", $self->{cleareol};
+               } else {
+                       print "\r", ' 'x length($self->{lastdisplay}), "\r";
+               }
        }
        $self->{lastdisplay} = '';
        delete $self->{stars};
@@ -256,7 +261,7 @@ sub clear
 sub disable
 {
        my $self = shift;
-       print "\n" if length($self->{lastdisplay}) > 0;
+       print "\n" if length($self->{lastdisplay}) > 0 and $self->can_output;
 
        bless $self, "OpenBSD::ProgressMeter::Stub";
 }
@@ -267,7 +272,7 @@ sub next
        $self->clear;
 
        $todo //= 'ok';
-       print "\r$self->{header}: $todo\n";
+       print "\r$self->{header}: $todo\n" if $self->can_output;
 }
 
 package ProgressSizer;
index 38d3c6f..8750341 100644 (file)
@@ -1,5 +1,5 @@
 # ex:ts=8 sw=4:
-# $OpenBSD: State.pm,v 1.66 2021/01/30 10:37:22 espie Exp $
+# $OpenBSD: State.pm,v 1.67 2021/01/30 11:19:01 espie Exp $
 #
 # Copyright (c) 2007-2014 Marc Espie <espie@openbsd.org>
 #
@@ -120,6 +120,9 @@ sub init
        $self->{subst} = OpenBSD::Subst->new;
        $self->{repo} = OpenBSD::PackageRepositoryFactory->new($self);
        $self->{export_level} = 1;
+       $SIG{'CONT'} = sub {
+               $self->handle_continue;
+       }
 }
 
 sub repo
@@ -131,9 +134,18 @@ sub repo
 sub handle_continue
 {
        my $self = shift;
-       $self->find_window_size(1);
+       $self->find_window_size(1);     # 1 is legacy interface for dpb
+       # invalidate cache so this runs again after continue
+       delete $self->{can_output};
 }
 
+OpenBSD::Auto::cache(can_output,
+       sub {
+               require POSIX;
+               # XXX uses POSIX semantics so fd, we can hardcode stdout ;)
+               return getpgrp() == POSIX::tcgetpgrp(1); 
+       });
+
 sub sync_display
 {
 }
@@ -241,7 +253,7 @@ sub _fhprint
 sub _print
 {
        my $self = shift;
-       $self->_fhprint(\*STDOUT, @_);
+       $self->_fhprint(\*STDOUT, @_) if $self->can_output;
 }
 
 sub _errprint
@@ -271,13 +283,13 @@ sub fhsay
 sub print
 {
        my $self = shift;
-       $self->fhprint(\*STDOUT, @_);
+       $self->fhprint(\*STDOUT, @_) if $self->can_output;
 }
 
 sub say
 {
        my $self = shift;
-       $self->fhsay(\*STDOUT, @_);
+       $self->fhsay(\*STDOUT, @_) if $self->can_output;
 }
 
 sub errprint
@@ -380,9 +392,6 @@ sub find_window_size
                        $self->find_window_size;
                };
        }
-       $SIG{'CONT'} = sub {
-               $self->handle_continue;
-       }
 }
 
 OpenBSD::Auto::cache(signer_list,