3 # Test whether programs exit upon a single EOF from a tty.
5 # Copyright (C) 2003, 2006, 2007 Free Software Foundation, Inc.
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 $PERL -e 1 > /dev/null 2>&1 || {
25 echo 1>&2 "$0: configure didn't find a usable version of Perl," \
26 "so can't run this test"
33 exec $PERL -w -- - <<\EOF
35 # Ensure that cat exits upon a single EOF (^D) from a tty.
36 # Do the same for all programs that can read stdin,
37 # require no arguments and that write to standard output.
40 (my $ME = $ENV{ARGV_0}) =~ s|.*/||;
42 # Some older versions of Expect.pm (e.g. 1.07) lack the log_user method,
43 # so check for that, too.
44 eval { require Expect; Expect->require_version('1.11') };
45 $@ and (warn "$ME: this script requires Perl's Expect package >=1.11\n"),
50 my @stdin_reading_commands = qw(
81 my $stderr = 'tty-eof.err';
82 foreach my $cmd ((@stdin_reading_commands), 'cut -f2')
86 $exp->spawn("$cmd 2> $stderr")
87 or (warn "$ME: cannot run `$cmd': $!\n"), $fail=1, next;
88 # No input for cut -f2.
90 or $exp->send("a b\n");
91 $exp->send("\cD"); # This is Control-D. FIXME: what if that's not EOF?
92 $exp->expect (0, '-re', "^a b\\r?\$");
93 my $found = $exp->expect (1, '-re', "^.+\$");
94 # $found and warn "F: $found: " . $exp->exp_match () . "\n";
95 $exp->expect(1, 'eof');
96 # Expect no output from cut, since we gave it no input.
97 defined $found || $cmd =~ /^cut/
98 or (warn "$ME: $cmd didn't produce expected output\n"),
100 defined $exp->exitstatus
101 or (warn "$ME: $cmd didn't exit after ^D from standard input\n"),
103 my $s = $exp->exitstatus;
105 or (warn "$ME: $cmd exited with status $s (expected 0)\n"),
109 # dd normally writes to stderr. If it exits successfully, we're done.
110 $cmd eq 'dd' && $s == 0
115 warn "$ME: $cmd wrote to stderr:\n";
116 system "cat $stderr";
123 or warn "$ME: failed to remove stderr file from $cmd, $stderr: $!\n";