1 # Copyright 2019-2020 Free Software Foundation, Inc.
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.
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.
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/>.
16 # An ANSI terminal emulator for expect.
18 # The expect "spawn" function puts the tty name into the spawn_out
19 # array; but dejagnu doesn't export this globally. So, we have to
20 # wrap spawn with our own function, so that we can capture this value.
21 # The value is later used in calls to stty.
22 rename spawn builtin_spawn
24 set result [uplevel builtin_spawn $args]
26 upvar spawn_out spawn_out
27 set gdb_spawn_name $spawn_out(slave,name)
43 variable _resize_count
45 # If ARG is empty, return DEF: otherwise ARG. This is useful for
46 # defaulting arguments in CSIs.
47 proc _default {arg def} {
54 # Erase in the line Y from SX to just before EX.
55 proc _clear_in_line {sx ex y} {
58 set lattr [array get _attrs]
60 set _chars($sx,$y) [list " " $lattr]
65 # Erase the lines from SY to just before EY.
66 proc _clear_lines {sy ey} {
69 _clear_in_line 0 $_cols $sy
85 set _cur_x [expr {$_cols - 1}]
98 if {$_cur_y >= $_rows} {
109 # Make room for characters.
111 set n [_default [lindex $args 0] 1]
116 set out_x [expr {$_cur_x + $n}]
117 for {set i 0} {$i < $n} {incr i} {
118 set _chars($out_x,$_cur_y) $_chars($in_x,$_cur_y)
127 set arg [_default [lindex $args 0] 1]
128 set _cur_y [expr {max ($_cur_y - $arg, 0)}]
135 set arg [_default [lindex $args 0] 1]
136 set _cur_y [expr {min ($_cur_y + $arg, $_rows)}]
143 set arg [_default [lindex $args 0] 1]
144 set _cur_x [expr {min ($_cur_x + $arg, $_cols)}]
150 set arg [_default [lindex $args 0] 1]
151 set _cur_x [expr {max ($_cur_x - $arg, 0)}]
159 set arg [_default [lindex $args 0] 1]
161 set _cur_y [expr {min ($_cur_y + $arg, $_rows)}]
164 # Cursor Previous Line.
169 set arg [_default [lindex $args 0] 1]
171 set _cur_y [expr {max ($_cur_y - $arg, 0)}]
174 # Cursor Horizontal Absolute.
178 set arg [_default [lindex $args 0] 1]
179 set _cur_x [expr {min ($arg - 1, $_cols)}]
182 # Move cursor (don't know the official name of this one).
186 set _cur_y [expr {[_default [lindex $args 0] 1] - 1}]
187 set _cur_x [expr {[_default [lindex $args 1] 1] - 1}]
190 # Cursor Forward Tabulation.
192 set n [_default [lindex $args 0] 1]
195 incr _cur_x [expr {$n * 8 - $_cur_x % 8}]
196 if {$_cur_x >= $_cols} {
197 set _cur_x [expr {$_cols - 1}]
207 set arg [_default [lindex $args 0] 0]
209 _clear_in_line $_cur_x $_cols $_cur_y
210 _clear_lines [expr {$_cur_y + 1}] $_rows
211 } elseif {$arg == 1} {
212 _clear_lines 0 [expr {$_cur_y - 1}]
213 _clear_in_line 0 $_cur_x $_cur_y
214 } elseif {$arg == 2} {
215 _clear_lines 0 $_rows
224 set arg [_default [lindex $args 0] 0]
226 # From cursor to end.
227 _clear_in_line $_cur_x $_cols $_cur_y
228 } elseif {$arg == 1} {
229 _clear_in_line 0 $_cur_x $_cur_y
230 } elseif {$arg == 2} {
231 _clear_in_line 0 $_cols $_cur_y
241 set count [_default [lindex $args 0] 1]
243 set next_y [expr {$y + 1}]
244 while {$count > 0 && $next_y < $_rows} {
245 for {set x 0} {$x < $_cols} {incr x} {
246 set _chars($x,$y) $_chars($x,$next_y)
252 _clear_lines $next_y $_rows
257 set n [_default [lindex $args 0] 1]
258 # Erase characters but don't move cursor.
263 set lattr [array get _attrs]
265 for {set i 0} {$i < $n} {incr i} {
266 set _chars($x,$_cur_y) [list " " $lattr]
271 # Backward tab stops.
273 set n [_default [lindex $args 0] 1]
275 set _cur_x [expr {max (int (($_cur_x - 1) / 8) * 8 - ($n - 1) * 8, 0)}]
281 set n [_default [lindex $args 0] 1]
282 _insert [string repeat $_last_char $n]
285 # Line Position Absolute.
288 set _cur_y [expr {[_default [lindex $args 0] 1] - 1}]
291 # Select Graphic Rendition.
295 switch -exact -- $item {
297 set _attrs(intensity) normal
298 set _attrs(fg) default
299 set _attrs(bg) default
300 set _attrs(underline) 0
301 set _attrs(reverse) 0
304 set _attrs(intensity) bold
307 set _attrs(intensity) dim
310 set _attrs(underline) 1
313 set _attrs(reverse) 1
316 set _attrs(intensity) normal
319 set _attrs(underline) 0
322 set _attrs(reverse) 1
324 30 - 31 - 32 - 33 - 34 - 35 - 36 - 37 {
328 set _attrs(fg) default
330 40 - 41 - 42 - 43 - 44 - 45 - 46 - 47 {
334 set _attrs(bg) default
340 # Insert string at the cursor location.
342 verbose "INSERT <<$str>>"
349 set lattr [array get _attrs]
350 foreach char [split $str {}] {
351 set _chars($_cur_x,$_cur_y) [list $char $lattr]
353 if {$_cur_x >= $_cols} {
356 if {$_cur_y >= $_rows} {
364 proc _setup {rows cols} {
366 set stty_init "rows $rows columns $cols"
373 variable _resize_count
388 _clear_lines 0 $_rows
391 # Accept some output from gdb and update the screen.
392 proc _accept {wait_for} {
398 set prompt_wait_for "$gdb_prompt \$"
402 -re "^\[\x07\x08\x0a\x0d\]" {
403 scan $expect_out(0,string) %c val
404 set hexval [format "%02x" $val]
405 verbose "+++ _ctl_0x${hexval}"
408 -re "^\x1b(\[0-9a-zA-Z\])" {
409 verbose "+++ unsupported escape"
410 error "unsupported escape"
412 -re "^\x1b\\\[(\[0-9;\]*)(\[a-zA-Z@\])" {
413 set cmd $expect_out(2,string)
414 set params [split $expect_out(1,string) ";"]
415 verbose "+++ _csi_$cmd <<<$expect_out(1,string)>>>"
416 eval _csi_$cmd $params
418 -re "^\[^\x07\x08\x0a\x0d\x1b\]+" {
419 _insert $expect_out(0,string)
421 set _last_char [string index $expect_out(0,string) end]
425 # Assume a timeout means we somehow missed the
426 # expected result, and carry on.
431 # If the cursor appears just after the prompt, return. It
432 # isn't reliable to check this only after an insertion,
433 # because curses may make "unusual" redrawing decisions.
434 if {$wait_for == "$prompt_wait_for"} {
435 set prev [get_line $_cur_y $_cur_x]
437 set prev [get_line $_cur_y]
439 if {[regexp -- $wait_for $prev]} {
440 if {$wait_for == "$prompt_wait_for"} {
443 set wait_for $prompt_wait_for
448 # Like ::clean_restart, but ensures that gdb starts in an
449 # environment where the TUI can work. ROWS and COLS are the size
450 # of the terminal. EXECUTABLE, if given, is passed to
452 proc clean_restart {rows cols {executable {}}} {
454 save_vars {env(TERM) stty_init} {
457 if {$executable == ""} {
460 ::clean_restart $executable
465 # Setup ready for starting the tui, but don't actually start it.
466 # Returns 1 on success, 0 if TUI tests should be skipped.
467 proc prepare_for_tui {} {
468 if {[skip_tui_tests]} {
472 gdb_test_no_output "set tui border-kind ascii"
473 gdb_test_no_output "maint set tui-resize-message on"
477 # Start the TUI. Returns 1 on success, 0 if TUI tests should be
480 if {![prepare_for_tui]} {
488 # Send the command CMD to gdb, then wait for a gdb prompt to be
489 # seen in the TUI. CMD should not end with a newline -- that will
490 # be supplied by this function.
493 _accept [string_to_regexp $cmd]
496 # Return the text of screen line N, without attributes. Lines are
497 # 0-based. If C is given, stop before column C. Columns are also
499 proc get_line {n {c ""}} {
501 # This can happen during resizing, if the cursor seems to
502 # temporarily be off-screen.
510 set c [_default $c $_cols]
513 append result [lindex $_chars($x,$n) 0]
519 # Get just the character at (X, Y).
520 proc get_char {x y} {
522 return [lindex $_chars($x,$y) 0]
525 # Get the entire screen as a string.
526 proc get_all_lines {} {
532 for {set y 0} {$y < $_rows} {incr y} {
533 for {set x 0} {$x < $_cols} {incr x} {
534 append result [lindex $_chars($x,$y) 0]
542 # Get the text just before the cursor.
543 proc get_current_line {} {
546 return [get_line $_cur_y $_cur_x]
549 # Helper function for check_box. Returns empty string if the box
550 # is found, description of why not otherwise.
551 proc _check_box {x y width height} {
552 set x2 [expr {$x + $width - 1}]
553 set y2 [expr {$y + $height - 1}]
555 if {[get_char $x $y] != "+"} {
558 if {[get_char $x $y2] != "+"} {
561 if {[get_char $x2 $y] != "+"} {
564 if {[get_char $x2 $y2] != "+"} {
568 # Note we do not check the horizonal borders of the box. The
569 # top will contain a title, and the bottom may as well, if it
570 # is overlapped by some other border.
571 for {set i [expr {$y + 1}]} {$i < $y2 - 1} {incr i} {
572 if {[get_char $x $i] != "|"} {
573 return "left side $i"
575 if {[get_char $x2 $i] != "|"} {
576 return "right side $i"
583 # Check for a box at the given coordinates.
584 proc check_box {test_name x y width height} {
585 set why [_check_box $x $y $width $height]
590 fail "$test_name ($why)"
594 # Check whether the text contents of the terminal match the
595 # regular expression. Note that text styling is not considered.
596 proc check_contents {test_name regexp} {
597 set contents [get_all_lines]
598 if {![gdb_assert {[regexp -- $regexp $contents]} $test_name]} {
603 # Check the contents of a box on the screen. This is a little
604 # like check_contents, but doens't check the whole screen
605 # contents, only the contents of a single box. This procedure
606 # includes (effectively) a call to check_box to ensure there is a
607 # box where expected, if there is then the contents of the box are
608 # matched against REGEXP.
609 proc check_box_contents {test_name x y width height regexp} {
612 set why [_check_box $x $y $width $height]
615 fail "$test_name (box check: $why)"
619 # Now grab the contents of the box, join each line together
620 # with a newline character and match against REGEXP.
622 for {set yy [expr {$y + 1}]} {$yy < [expr {$y + $height - 1}]} {incr yy} {
623 for {set xx [expr {$x + 1}]} {$xx < [expr {$x + $width - 1}]} {incr xx} {
624 append result [lindex $_chars($xx,$yy) 0]
629 if {![gdb_assert {[regexp -- $regexp $result]} $test_name]} {
634 # A debugging function to dump the current screen, with line
636 proc dump_screen {} {
639 verbose -log "Screen Dump ($_cols x $_rows):"
640 for {set y 0} {$y < $_rows} {incr y} {
641 set fmt [format %5d $y]
642 verbose -log "$fmt [get_line $y]"
646 # Resize the terminal.
647 proc _do_resize {rows cols} {
652 set old_rows [expr {min ($_rows, $rows)}]
653 set old_cols [expr {min ($_cols, $cols)}]
656 array set local_chars [array get _chars]
661 _clear_lines 0 $_rows
663 for {set x 0} {$x < $old_cols} {incr x} {
664 for {set y 0} {$y < $old_rows} {incr y} {
665 set _chars($x,$y) $local_chars($x,$y)
670 proc resize {rows cols} {
673 variable _resize_count
675 global gdb_spawn_name
676 # expect handles each argument to stty separately. This means
677 # that gdb will see SIGWINCH twice. Rather than rely on this
678 # behavior (which, after all, could be changed), we make it
679 # explicit here. This also simplifies waiting for the redraw.
680 _do_resize $rows $_cols
681 stty rows $_rows < $gdb_spawn_name
682 # Due to the strange column resizing behavior, and because we
683 # don't care about this intermediate resize, we don't check
685 _accept "@@ resize done $_resize_count"
687 # Somehow the number of columns transmitted to gdb is one less
688 # than what we request from expect. We hide this weird
689 # details from the caller.
690 _do_resize $_rows $cols
691 stty columns [expr {$_cols + 1}] < $gdb_spawn_name
692 _accept "@@ resize done $_resize_count, size = ${_cols}x${rows}"