tests: move input-tty into a test-lib.sh function
[platform/upstream/coreutils.git] / tests / misc / stty
1 #! /bin/sh
2 # Make sure stty can parse most of its options.
3
4 # Copyright (C) 1998-2004, 2006-2008 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 3 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, see <http://www.gnu.org/licenses/>.
18
19 if test "$VERBOSE" = yes; then
20   set -x
21   stty --version
22 fi
23
24 # Make sure there's a tty on stdin.
25 . $srcdir/test-lib.sh
26 require_controlling_input_terminal_
27
28 # The following list of reversible options was generated with
29 # grep -w REV stty.c|sed -n '/^  {"/{s//REV_/;s/".*/=1/;p;}'|fmt
30 REV_parenb=1 REV_parodd=1 REV_hupcl=1 REV_hup=1 REV_cstopb=1 REV_cread=1
31 REV_clocal=1 REV_crtscts=1 REV_ignbrk=1 REV_brkint=1 REV_ignpar=1
32 REV_parmrk=1 REV_inpck=1 REV_istrip=1 REV_inlcr=1 REV_igncr=1 REV_icrnl=1
33 REV_ixon=1 REV_ixoff=1 REV_tandem=1 REV_iuclc=1 REV_ixany=1 REV_imaxbel=1
34 REV_opost=1 REV_olcuc=1 REV_ocrnl=1 REV_onlcr=1 REV_onocr=1 REV_onlret=1
35 REV_ofill=1 REV_ofdel=1 REV_isig=1 REV_icanon=1 REV_iexten=1 REV_echo=1
36 REV_echoe=1 REV_crterase=1 REV_echok=1 REV_echonl=1 REV_noflsh=1
37 REV_xcase=1 REV_tostop=1 REV_echoprt=1 REV_prterase=1 REV_echoctl=1
38 REV_ctlecho=1 REV_echoke=1 REV_crtkill=1 REV_evenp=1 REV_parity=1
39 REV_oddp=1 REV_nl=1 REV_cooked=1 REV_raw=1 REV_pass8=1 REV_litout=1
40 REV_cbreak=1 REV_decctlq=1 REV_tabs=1 REV_lcase=1 REV_LCASE=1
41
42 fail=0
43
44 saved_state=.saved-state
45 stty --save > $saved_state || fail=1
46 stty `cat $saved_state` || fail=1
47
48 # This would segfault prior to sh-utils-2.0j.
49 stty erase - || fail=1
50
51 # These would improperly ignore invalid options through coreutils 5.2.1.
52 stty -F 2>/dev/null && fail=1
53 stty -raw -F no/such/file 2>/dev/null && fail=1
54 stty -raw -a 2>/dev/null && fail=1
55
56 # Build a list of all boolean options stty accepts on this system.
57 # Don't depend on terminal width.  Put each option on its own line,
58 # remove all non-boolean ones, then remove any leading hyphens.
59 sed_del='/^speed/d;/^rows/d;/^columns/d;/ = /d'
60 options=`stty -a | tr -s ';' '\n' | sed "s/^ //;$sed_del;s/-//g"`
61
62 # Take them one at a time, with and without the leading `-'.
63 for opt in $options; do
64   # `stty parenb' and `stty -parenb' fail with this message
65   # stty: standard input: unable to perform all requested operations
66   # on Linux 2.2.0-pre4 kernels, so skip those tests.
67   test $opt = parenb && continue
68   stty $opt || fail=1
69
70   # Likewise, `stty -cread' would fail, so skip that, too.
71   test $opt = cread && continue
72   rev=`eval echo "\\\$REV_$opt"`
73   if test -n "$rev"; then
74     stty -$opt || { fail=1; echo -$opt; }
75   fi
76 done
77
78 if test -n "$RUN_LONG_TESTS"; then
79   # Take them in pairs.
80   for opt1 in $options; do
81     echo .|tr -d '\n'
82     for opt2 in $options; do
83
84       stty $opt1 $opt2 || fail=1
85
86       rev1=`eval echo "\\\$REV_$opt1"`
87       rev2=`eval echo "\\\$REV_$opt2"`
88       if test -n "$rev1"; then
89         stty -$opt1 $opt2 || fail=1
90       fi
91       if test -n "$rev2"; then
92         stty $opt1 -$opt2 || fail=1
93       fi
94       if test "$rev1$rev2" = 11; then
95         stty -$opt1 -$opt2 || fail=1
96       fi
97     done
98   done
99 fi
100
101 stty `cat $saved_state`
102
103 (exit $fail); exit $fail