Imported Upstream version 7.9
[platform/upstream/gdb.git] / gdb / testsuite / gdb.base / readline.exp
1 # Copyright 2002-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 # This file was written by Tom Tromey <tromey@redhat.com>
17
18 # This file is part of the gdb testsuite.
19
20 #
21 # Tests for readline operations.
22 #
23
24 # This function is used to test operate-and-get-next.
25 # NAME is the name of the test.
26 # ARGS is a list of alternating commands and expected results.
27 proc operate_and_get_next {name args} {
28   global gdb_prompt
29
30   set my_gdb_prompt "($gdb_prompt| >)"
31
32   set reverse {}
33   foreach {item result} $args {
34     verbose "sending $item"
35     sleep 1
36
37     # We can't use gdb_test here because we might see a " >" prompt.
38     set status 0
39     send_gdb "$item\n"
40     gdb_expect {
41       -re "$item" {
42         # Ok
43       }
44       timeout {
45         set status 1
46       }
47     }
48
49     if {! $status} {
50       gdb_expect {
51         -re "$result" {
52           # Ok.
53         }
54         timeout {
55           set status 1
56         }
57       }
58     }
59
60     if {$status} {
61       fail "$name - send $item"
62       return 0
63     }
64     pass "$name - send $item"
65
66     set reverse [linsert $reverse 0 $item $result]
67   }
68
69   # Now use C-p to go back to the start.
70   foreach {item result} $reverse {
71     # Actually send C-p followed by C-l.  This lets us recognize the
72     # command when gdb prints it again.
73     send_gdb "\x10\x0c"
74     set status 0
75     gdb_expect {
76       -re "$item" {
77         # Ok
78       }
79       timeout {
80         set status 1
81       }
82     }
83     if {$status} {
84       fail "$name - C-p to $item"
85       return 0
86     }
87     pass "$name - C-p to $item"
88   }
89
90   # Now C-o through the list.  Don't send the command, since it is
91   # already there.  Strip off the first command from the list so we
92   # can see the next command inside the loop.
93   set count 0
94   foreach {item result} $args {
95     set status 0
96
97     # If this isn't the first item, make sure we see the command at
98     # the prompt.
99     if {$count > 0} {
100       gdb_expect {
101         -re ".*$item" {
102           # Ok
103         }
104         timeout {
105           set status 1
106         }
107       }
108     }
109
110     if {! $status} {
111       # For the last item, send a simple \n instead of C-o.
112       if {$count == [llength $args] - 2} {
113         send_gdb "\n"
114       } else {
115         # 15 is C-o.
116         send_gdb [format %c 15]
117       }
118       set status 0
119       gdb_expect {
120         -re "$result" {
121           # Ok
122         }
123         timeout {
124           set status 1
125         }
126       }
127     }
128
129     if {$status} {
130       fail "$name - C-o for $item"
131       return 0
132     }
133     pass "$name - C-o for $item"
134
135     set count [expr {$count + 2}]
136   }
137
138   # Match the prompt so the next test starts at the right place.
139   gdb_test "" ".*" "$name - final prompt"
140
141   return 1
142 }
143
144
145 gdb_start
146 gdb_reinitialize_dir $srcdir/$subdir
147
148 if { ![readline_is_used] } {
149     unsupported "readline isn't used."
150     return -1
151 }
152
153 set oldtimeout1 $timeout
154 set timeout 30
155
156
157 # A simple test of operate-and-get-next.
158 operate_and_get_next "Simple operate-and-get-next" \
159   "p 1" ".* = 1" \
160   "p 2" ".* = 2" \
161   "p 3" ".* = 3"
162
163 # Test operate-and-get-next with a secondary prompt.
164 operate_and_get_next "operate-and-get-next with secondary prompt" \
165   "if 1 > 0" "" \
166   "p 5" "" \
167   "end" ".* = 5"
168
169 # Verify that arrow keys work in secondary prompts.  The control
170 # sequence is a hard-coded VT100 up arrow.
171 gdb_test "print 42" "\\\$\[0-9\]* = 42"
172 set msg "arrow keys with secondary prompt"
173 gdb_test_multiple "if 1 > 0\n\033\[A\033\[A\nend" $msg {
174     -re ".*\\\$\[0-9\]* = 42\r\n$gdb_prompt $" {
175         pass $msg
176     }
177     -re ".*Undefined command:.*$gdb_prompt $" {
178         fail $msg
179     }
180 }
181
182 # Now repeat the first test with a history file that fills the entire
183 # history list.
184
185 if [info exists env(GDBHISTFILE)] {
186     set old_gdbhistfile $env(GDBHISTFILE)
187 }
188 if [info exists env(HISTSIZE)] {
189     set old_histsize $env(HISTSIZE)
190 }
191 set env(GDBHISTFILE) "${srcdir}/${subdir}/gdb_history"
192 set env(HISTSIZE) "10"
193
194 gdb_exit
195 gdb_start
196 gdb_reinitialize_dir $srcdir/$subdir
197
198 operate_and_get_next "Simple operate-and-get-next" \
199   "p 7" ".* = 7" \
200   "p 8" ".* = 8" \
201   "p 9" ".* = 9"
202
203
204 # Restore globals modified in this test...
205 if [info exists old_gdbhistfile] {
206     set env(GDBHISTFILE) $old_gdbhistfile
207 } else {
208     unset env(GDBHISTFILE)
209 }
210 if [info exists old_histsize] {
211     set env(HISTSIZE) $old_histsize
212 } else {
213     unset env(HISTSIZE)
214 }
215 set timeout $oldtimeout1
216
217 return 0