t / t7006 / test-terminal.perlon commit Merge branch 'jc/maint-no-openssl-build-fix' into maint (40d675f)
   1#!/usr/bin/perl
   2use 5.008;
   3use strict;
   4use warnings;
   5use IO::Pty;
   6use File::Copy;
   7
   8# Run @$argv in the background with stdout redirected to $out.
   9sub start_child {
  10        my ($argv, $out) = @_;
  11        my $pid = fork;
  12        if (not defined $pid) {
  13                die "fork failed: $!"
  14        } elsif ($pid == 0) {
  15                open STDOUT, ">&", $out;
  16                close $out;
  17                exec(@$argv) or die "cannot exec '$argv->[0]': $!"
  18        }
  19        return $pid;
  20}
  21
  22# Wait for $pid to finish.
  23sub finish_child {
  24        # Simplified from wait_or_whine() in run-command.c.
  25        my ($pid) = @_;
  26
  27        my $waiting = waitpid($pid, 0);
  28        if ($waiting < 0) {
  29                die "waitpid failed: $!";
  30        } elsif ($? & 127) {
  31                my $code = $? & 127;
  32                warn "died of signal $code";
  33                return $code - 128;
  34        } else {
  35                return $? >> 8;
  36        }
  37}
  38
  39sub xsendfile {
  40        my ($out, $in) = @_;
  41
  42        # Note: the real sendfile() cannot read from a terminal.
  43
  44        # It is unspecified by POSIX whether reads
  45        # from a disconnected terminal will return
  46        # EIO (as in AIX 4.x, IRIX, and Linux) or
  47        # end-of-file.  Either is fine.
  48        copy($in, $out, 4096) or $!{EIO} or die "cannot copy from child: $!";
  49}
  50
  51if ($#ARGV < 1) {
  52        die "usage: test-terminal program args";
  53}
  54my $master = new IO::Pty;
  55my $slave = $master->slave;
  56my $pid = start_child(\@ARGV, $slave);
  57close $slave;
  58xsendfile(\*STDOUT, $master);
  59exit(finish_child($pid));