Add/fix copyright notices and adjust to latest GNU FDL.
[platform/upstream/coreutils.git] / tests / misc / tty-eof
1 #!/bin/sh
2 # Test whether programs exit upon a single EOF from a tty.
3
4 # Copyright (C) 2003, 2006 Free Software Foundation, Inc.
5
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 # 02110-1301, USA.
20
21 : ${PERL=perl}
22
23 $PERL -e 1 > /dev/null 2>&1 || {
24   echo 1>&2 "$0: configure didn't find a usable version of Perl," \
25     "so can't run this test"
26   exit 77
27 }
28
29 ARGV_0=$0
30 export ARGV_0
31
32 exec $PERL -w -- - <<\EOF
33
34 # Ensure that cat exits upon a single EOF (^D) from a tty.
35 # Do the same for all programs that can read stdin,
36 # require no arguments and that write to standard output.
37 use strict;
38
39 (my $ME = $ENV{ARGV_0}) =~ s|.*/||;
40
41 # Some older versions of Expect.pm (e.g. 1.07) lack the log_user method,
42 # so check for that, too.
43 eval { require Expect; Expect->require_version('1.11') };
44 $@ and (warn "$ME: this script requires Perl's Expect package >=1.11\n"),
45   exit 77;
46
47 {
48   my $fail = 0;
49   my @stdin_reading_commands = qw(
50     base64
51     cat
52     cksum
53     dd
54     expand
55     fmt
56     fold
57     head
58     md5sum
59     nl
60     od
61     paste
62     pr
63     ptx
64     sha1sum
65     sha224sum
66     sha256sum
67     sha384sum
68     sha512sum
69     sort
70     sum
71     tac
72     tail
73     tee
74     tsort
75     unexpand
76     uniq
77     wc
78   );
79   foreach my $cmd ((@stdin_reading_commands), 'cut -f2')
80     {
81       my $exp = new Expect;
82       $exp->log_user(0);
83       $exp->spawn($cmd)
84         or (warn "$ME: cannot run `$cmd': $!\n"), $fail=1, next;
85       # No input for cut -f2.
86       $cmd =~ /^cut/
87         or $exp->send("a b\n");
88       $exp->send("\cD");  # This is Control-D.  FIXME: what if that's not EOF?
89       $exp->expect (0, '-re', "^a b\\r?\$");
90       my $found = $exp->expect (1, '-re', "^.+\$");
91       # $found and warn "F: $found: " . $exp->exp_match () . "\n";
92       $exp->expect(1, 'eof');
93       # Expect no output from cut, since we gave it no input.
94       defined $found || $cmd =~ /^cut/
95         or (warn "$ME: $cmd didn't produce expected output\n"),
96           $fail=1, next;
97       defined defined $exp->exitstatus
98         or (warn "$ME: $cmd didn't exit after ^D from standard input\n"),
99           $fail=1, next;
100       my $s = $exp->exitstatus;
101       $s == 0
102         or (warn "$ME: $cmd exited with status $s (expected 0)\n"),
103           $fail=1;
104       $exp->hard_close();
105     }
106
107   exit $fail
108 }
109 EOF