Tizen 2.0 Release
[external/tizen-coreutils.git] / tests / misc / tty-eof
1 #!/bin/sh
2 # -*- perl -*-
3 # Test whether programs exit upon a single EOF from a tty.
4
5 # Copyright (C) 2003, 2006, 2007 Free Software Foundation, Inc.
6
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.
11
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.
16
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
20 # 02110-1301, USA.
21
22 : ${PERL=perl}
23
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"
27   exit 77
28 }
29
30 ARGV_0=$0
31 export ARGV_0
32
33 exec $PERL -w -- - <<\EOF
34
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.
38 use strict;
39
40 (my $ME = $ENV{ARGV_0}) =~ s|.*/||;
41
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"),
46   exit 77;
47
48 {
49   my $fail = 0;
50   my @stdin_reading_commands = qw(
51     base64
52     cat
53     cksum
54     dd
55     expand
56     fmt
57     fold
58     head
59     md5sum
60     nl
61     od
62     paste
63     pr
64     ptx
65     sha1sum
66     sha224sum
67     sha256sum
68     sha384sum
69     sha512sum
70     shuf
71     sort
72     sum
73     tac
74     tail
75     tee
76     tsort
77     unexpand
78     uniq
79     wc
80   );
81   my $stderr = 'tty-eof.err';
82   foreach my $cmd ((@stdin_reading_commands), 'cut -f2')
83     {
84       my $exp = new Expect;
85       $exp->log_user(0);
86       $exp->spawn("$cmd 2> $stderr")
87         or (warn "$ME: cannot run `$cmd': $!\n"), $fail=1, next;
88       # No input for cut -f2.
89       $cmd =~ /^cut/
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"),
99           $fail=1, next;
100       defined $exp->exitstatus
101         or (warn "$ME: $cmd didn't exit after ^D from standard input\n"),
102           $fail=1, next;
103       my $s = $exp->exitstatus;
104       $s == 0
105         or (warn "$ME: $cmd exited with status $s (expected 0)\n"),
106           $fail=1;
107       $exp->hard_close();
108
109       # dd normally writes to stderr.  If it exits successfully, we're done.
110       $cmd eq 'dd' && $s == 0
111         and next;
112
113       if (-s $stderr)
114         {
115           warn "$ME: $cmd wrote to stderr:\n";
116           system "cat $stderr";
117           $fail = 1;
118         }
119     }
120   continue
121     {
122       unlink $stderr
123         or warn "$ME: failed to remove stderr file from $cmd, $stderr: $!\n";
124     }
125
126   exit $fail
127 }
128 EOF