Clean up batch-preserve-term-settings.exp
[binutils-gdb.git] / gdb / testsuite / gdb.base / batch-preserve-term-settings.exp
1 # Copyright (C) 2015 Free Software Foundation, Inc.
2
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 3 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16 # Check that "gdb -batch -ex run" does not leave the terminal in the
17 # wrong state.
18
19 standard_testfile
20
21 if {[build_executable "failed to prepare" $testfile $srcfile debug] == -1} {
22 return -1
23 }
24
25 set file_arg $binfile
26 if [is_remote host] {
27 set file_arg [remote_download host $file_arg]
28 }
29
30 # The shell's prompt.
31 set shell_prompt "$ "
32 set shell_prompt_re [string_to_regexp $shell_prompt]
33
34 # Spawn shell. Returns true on success, false otherwise.
35
36 proc spawn_shell {} {
37 global shell_prompt_re
38
39 set res [remote_spawn host "/bin/sh"]
40 if { $res < 0 || $res == "" } {
41 unsupported "Spawning shell failed."
42 return 0
43 }
44
45 set gotit 0
46 set test "spawn shell"
47 gdb_expect {
48 -re "$shell_prompt_re$" {
49 pass $test
50 set gotit 1
51 }
52 timeout {
53 fail "$test (timeout)"
54 }
55 eof {
56 fail "$test (eof)"
57 }
58 }
59
60 return $gotit
61 }
62
63 # Exit the shell.
64
65 proc exit_shell {} {
66 global shell_prompt_re
67
68 set test "exit shell"
69 send_gdb "exit\n"
70 gdb_expect {
71 timeout {
72 fail "$test (timeout)"
73 return 0
74 }
75 eof {
76 pass "$test"
77 }
78 }
79 if ![is_remote host] {
80 remote_close host
81 }
82 }
83
84 # Run "stty" and store the output in $result. Returns true on
85 # success, false otherwise.
86
87 proc run_stty {message result} {
88 global shell_prompt_re
89
90 upvar $result output
91
92 send_gdb "stty || echo \"not found\"\n"
93 set gotit 0
94 gdb_expect {
95 -re "not found.*not found.*$shell_prompt_re$" {
96 pass "$message (not found)"
97 }
98 -re "(.*)$shell_prompt_re$" {
99 set output $expect_out(1,string)
100 set gotit 1
101 pass $message
102 }
103 timeout {
104 fail "$message (timeout)"
105 }
106 eof {
107 fail "$message (eof)"
108 }
109 }
110 return $gotit
111 }
112
113 # Check that "gdb -batch -ex run" does not leave the terminal in the
114 # wrong state.
115
116 proc test_terminal_settings_preserved {} {
117 global file_arg
118 global GDB INTERNAL_GDBFLAGS GDBFLAGS
119 global gdb_prompt
120 global shell_prompt_re
121
122 if ![spawn_shell] {
123 return
124 }
125
126 set stty_supported [run_stty "stty before" stty_before]
127
128 set test "gdb -batch -ex run"
129 append EXTRA_GDBFLAGS "-batch"
130 append EXTRA_GDBFLAGS " -ex \"set height unlimited\""
131 append EXTRA_GDBFLAGS " -ex \"start\""
132 append EXTRA_GDBFLAGS " --args \"$file_arg\""
133 send_gdb "$GDB $INTERNAL_GDBFLAGS $GDBFLAGS $EXTRA_GDBFLAGS [host_info gdb_opts]\n"
134 gdb_expect {
135 -re "Don't know how to run.*$shell_prompt_re$" {
136 unsupported $test
137 }
138 -re "$gdb_prompt $" {
139 # -batch implies no GDB prompt.
140 fail $test
141 }
142 -re "Temporary breakpoint .*$shell_prompt_re$" {
143 pass $test
144 }
145 timeout {
146 fail "$test (timeout)"
147 }
148 eof {
149 fail "$test (eof)"
150 }
151 }
152
153 set test "echo test_echo"
154 send_gdb "echo test_echo\n"
155 gdb_expect {
156 -re "^echo test_echo\r\ntest_echo\r\n.*$shell_prompt_re$" {
157 pass $test
158 }
159 timeout {
160 fail "$test (timeout)"
161 }
162 eof {
163 fail "$test (eof)"
164 }
165 }
166
167 set test "terminal settings preserved"
168 if $stty_supported {
169 run_stty "stty after" stty_after
170
171 gdb_assert [string equal $stty_before $stty_after] $test
172 } else {
173 unsupported "$test (no stty)"
174 }
175
176 exit_shell
177 }
178
179 test_terminal_settings_preserved