1 # Copyright 2019-2021 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 proc tuiterm_spawn { args } {
23 set result [uplevel builtin_spawn $args]
25 upvar spawn_out spawn_out
26 if { [info exists spawn_out] } {
27 set gdb_spawn_name $spawn_out(slave,name)
34 # Initialize tuiterm.exp environment.
35 proc tuiterm_env_init { } {
36 # Override spawn with tui_spawn.
37 rename spawn builtin_spawn
38 rename tuiterm_spawn spawn
41 # Finalize tuiterm.exp environment.
42 proc tuiterm_env_finish { } {
44 rename spawn tuiterm_spawn
45 rename builtin_spawn spawn
60 variable _resize_count
62 # If ARG is empty, return DEF: otherwise ARG. This is useful for
63 # defaulting arguments in CSIs.
64 proc _default {arg def} {
71 # Erase in the line Y from SX to just before EX.
72 proc _clear_in_line {sx ex y} {
75 set lattr [array get _attrs]
77 set _chars($sx,$y) [list " " $lattr]
82 # Erase the lines from SY to just before EY.
83 proc _clear_lines {sy ey} {
86 _clear_in_line 0 $_cols $sy
102 set _cur_x [expr {$_cols - 1}]
115 if {$_cur_y >= $_rows} {
128 # https://vt100.net/docs/vt510-rm/ICH.html
130 set n [_default [lindex $args 0] 1]
135 set out_x [expr {$_cur_x + $n}]
136 for {set i 0} {$i < $n} {incr i} {
137 set _chars($out_x,$_cur_y) $_chars($in_x,$_cur_y)
145 # https://vt100.net/docs/vt510-rm/CUU.html
148 set arg [_default [lindex $args 0] 1]
149 set _cur_y [expr {max ($_cur_y - $arg, 0)}]
154 # https://vt100.net/docs/vt510-rm/CUD.html
158 set arg [_default [lindex $args 0] 1]
159 set _cur_y [expr {min ($_cur_y + $arg, $_rows)}]
164 # https://vt100.net/docs/vt510-rm/CUF.html
168 set arg [_default [lindex $args 0] 1]
169 set _cur_x [expr {min ($_cur_x + $arg, $_cols)}]
174 # https://vt100.net/docs/vt510-rm/CUB.html
177 set arg [_default [lindex $args 0] 1]
178 set _cur_x [expr {max ($_cur_x - $arg, 0)}]
183 # https://vt100.net/docs/vt510-rm/CNL.html
188 set arg [_default [lindex $args 0] 1]
190 set _cur_y [expr {min ($_cur_y + $arg, $_rows)}]
193 # Cursor Previous Line.
195 # https://vt100.net/docs/vt510-rm/CPL.html
200 set arg [_default [lindex $args 0] 1]
202 set _cur_y [expr {max ($_cur_y - $arg, 0)}]
205 # Cursor Horizontal Absolute.
207 # https://vt100.net/docs/vt510-rm/CHA.html
211 set arg [_default [lindex $args 0] 1]
212 set _cur_x [expr {min ($arg - 1, $_cols)}]
217 # https://vt100.net/docs/vt510-rm/CUP.html
221 set _cur_y [expr {[_default [lindex $args 0] 1] - 1}]
222 set _cur_x [expr {[_default [lindex $args 1] 1] - 1}]
225 # Cursor Horizontal Forward Tabulation.
227 # https://vt100.net/docs/vt510-rm/CHT.html
229 set n [_default [lindex $args 0] 1]
232 incr _cur_x [expr {$n * 8 - $_cur_x % 8}]
233 if {$_cur_x >= $_cols} {
234 set _cur_x [expr {$_cols - 1}]
240 # https://vt100.net/docs/vt510-rm/ED.html
246 set arg [_default [lindex $args 0] 0]
248 _clear_in_line $_cur_x $_cols $_cur_y
249 _clear_lines [expr {$_cur_y + 1}] $_rows
250 } elseif {$arg == 1} {
251 _clear_lines 0 [expr {$_cur_y - 1}]
252 _clear_in_line 0 $_cur_x $_cur_y
253 } elseif {$arg == 2} {
254 _clear_lines 0 $_rows
260 # https://vt100.net/docs/vt510-rm/EL.html
265 set arg [_default [lindex $args 0] 0]
267 # From cursor to end.
268 _clear_in_line $_cur_x $_cols $_cur_y
269 } elseif {$arg == 1} {
270 _clear_in_line 0 $_cur_x $_cur_y
271 } elseif {$arg == 2} {
272 _clear_in_line 0 $_cols $_cur_y
278 # https://vt100.net/docs/vt510-rm/DL.html
284 set count [_default [lindex $args 0] 1]
286 set next_y [expr {$y + 1}]
287 while {$count > 0 && $next_y < $_rows} {
288 for {set x 0} {$x < $_cols} {incr x} {
289 set _chars($x,$y) $_chars($x,$next_y)
295 _clear_lines $next_y $_rows
300 # https://vt100.net/docs/vt510-rm/ECH.html
302 set n [_default [lindex $args 0] 1]
303 # Erase characters but don't move cursor.
308 set lattr [array get _attrs]
310 for {set i 0} {$i < $n} {incr i} {
311 set _chars($x,$_cur_y) [list " " $lattr]
316 # Cursor Backward Tabulation.
318 # https://vt100.net/docs/vt510-rm/CBT.html
320 set n [_default [lindex $args 0] 1]
322 set _cur_x [expr {max (int (($_cur_x - 1) / 8) * 8 - ($n - 1) * 8, 0)}]
327 # https://www.xfree86.org/current/ctlseqs.html (See `(REP)`)
330 set n [_default [lindex $args 0] 1]
331 _insert [string repeat $_last_char $n]
334 # Vertical Line Position Absolute.
336 # https://vt100.net/docs/vt510-rm/VPA.html
339 set _cur_y [expr {[_default [lindex $args 0] 1] - 1}]
342 # Select Graphic Rendition.
344 # https://vt100.net/docs/vt510-rm/SGR.html
348 switch -exact -- $item {
350 set _attrs(intensity) normal
351 set _attrs(fg) default
352 set _attrs(bg) default
353 set _attrs(underline) 0
354 set _attrs(reverse) 0
357 set _attrs(intensity) bold
360 set _attrs(intensity) dim
363 set _attrs(underline) 1
366 set _attrs(reverse) 1
369 set _attrs(intensity) normal
372 set _attrs(underline) 0
375 set _attrs(reverse) 1
377 30 - 31 - 32 - 33 - 34 - 35 - 36 - 37 {
381 set _attrs(fg) default
383 40 - 41 - 42 - 43 - 44 - 45 - 46 - 47 {
387 set _attrs(bg) default
393 # Insert string at the cursor location.
395 verbose "INSERT <<$str>>"
402 set lattr [array get _attrs]
403 foreach char [split $str {}] {
404 set _chars($_cur_x,$_cur_y) [list $char $lattr]
406 if {$_cur_x >= $_cols} {
409 if {$_cur_y >= $_rows} {
417 proc _setup {rows cols} {
419 set stty_init "rows $rows columns $cols"
426 variable _resize_count
441 _clear_lines 0 $_rows
444 # Accept some output from gdb and update the screen. WAIT_FOR is
445 # a regexp matching the line to wait for. Return 0 on timeout, 1
447 proc wait_for {wait_for} {
453 set prompt_wait_for "$gdb_prompt \$"
457 -re "^\[\x07\x08\x0a\x0d\]" {
458 scan $expect_out(0,string) %c val
459 set hexval [format "%02x" $val]
460 verbose "+++ _ctl_0x${hexval}"
463 -re "^\x1b(\[0-9a-zA-Z\])" {
464 verbose "+++ unsupported escape"
465 error "unsupported escape"
467 -re "^\x1b\\\[(\[0-9;\]*)(\[a-zA-Z@\])" {
468 set cmd $expect_out(2,string)
469 set params [split $expect_out(1,string) ";"]
470 verbose "+++ _csi_$cmd <<<$expect_out(1,string)>>>"
471 eval _csi_$cmd $params
473 -re "^\[^\x07\x08\x0a\x0d\x1b\]+" {
474 _insert $expect_out(0,string)
476 set _last_char [string index $expect_out(0,string) end]
480 # Assume a timeout means we somehow missed the
481 # expected result, and carry on.
486 # If the cursor appears just after the prompt, return. It
487 # isn't reliable to check this only after an insertion,
488 # because curses may make "unusual" redrawing decisions.
489 if {$wait_for == "$prompt_wait_for"} {
490 set prev [get_line $_cur_y $_cur_x]
492 set prev [get_line $_cur_y]
494 if {[regexp -- $wait_for $prev]} {
495 if {$wait_for == "$prompt_wait_for"} {
498 set wait_for $prompt_wait_for
505 # Like ::clean_restart, but ensures that gdb starts in an
506 # environment where the TUI can work. ROWS and COLS are the size
507 # of the terminal. EXECUTABLE, if given, is passed to
509 proc clean_restart {rows cols {executable {}}} {
511 save_vars {env(TERM) stty_init} {
514 if {$executable == ""} {
517 ::clean_restart $executable
522 # Setup ready for starting the tui, but don't actually start it.
523 # Returns 1 on success, 0 if TUI tests should be skipped.
524 proc prepare_for_tui {} {
525 if {[skip_tui_tests]} {
529 gdb_test_no_output "set tui border-kind ascii"
530 gdb_test_no_output "maint set tui-resize-message on"
534 # Start the TUI. Returns 1 on success, 0 if TUI tests should be
537 if {![prepare_for_tui]} {
541 command_no_prompt_prefix "tui enable"
545 # Send the command CMD to gdb, then wait for a gdb prompt to be
546 # seen in the TUI. CMD should not end with a newline -- that will
547 # be supplied by this function.
551 set str [string_to_regexp $cmd]
552 set str "^$gdb_prompt $str"
556 # As proc command, but don't wait for a initial prompt. This is used for
557 # inital terminal commands, where there's no prompt yet.
558 proc command_no_prompt_prefix {cmd} {
560 set str [string_to_regexp $cmd]
564 # Return the text of screen line N, without attributes. Lines are
565 # 0-based. If C is given, stop before column C. Columns are also
567 proc get_line {n {c ""}} {
569 # This can happen during resizing, if the cursor seems to
570 # temporarily be off-screen.
578 set c [_default $c $_cols]
581 append result [lindex $_chars($x,$n) 0]
587 # Get just the character at (X, Y).
588 proc get_char {x y} {
590 return [lindex $_chars($x,$y) 0]
593 # Get the entire screen as a string.
594 proc get_all_lines {} {
600 for {set y 0} {$y < $_rows} {incr y} {
601 for {set x 0} {$x < $_cols} {incr x} {
602 append result [lindex $_chars($x,$y) 0]
610 # Get the text just before the cursor.
611 proc get_current_line {} {
614 return [get_line $_cur_y $_cur_x]
617 # Helper function for check_box. Returns empty string if the box
618 # is found, description of why not otherwise.
619 proc _check_box {x y width height} {
620 set x2 [expr {$x + $width - 1}]
621 set y2 [expr {$y + $height - 1}]
623 if {[get_char $x $y] != "+"} {
626 if {[get_char $x $y2] != "+"} {
629 if {[get_char $x2 $y] != "+"} {
632 if {[get_char $x2 $y2] != "+"} {
636 # Note we do not check the full horizonal borders of the box.
637 # The top will contain a title, and the bottom may as well, if
638 # it is overlapped by some other border. However, at most a
639 # title should appear as '+-VERY LONG TITLE-+', so we can
640 # check for the '+-' on the left, and '-+' on the right.
641 if {[get_char [expr {$x + 1}] $y] != "-"} {
642 return "ul title padding"
645 if {[get_char [expr {$x2 - 1}] $y] != "-"} {
646 return "ul title padding"
649 # Now check the vertical borders.
650 for {set i [expr {$y + 1}]} {$i < $y2 - 1} {incr i} {
651 if {[get_char $x $i] != "|"} {
652 return "left side $i"
654 if {[get_char $x2 $i] != "|"} {
655 return "right side $i"
662 # Check for a box at the given coordinates.
663 proc check_box {test_name x y width height} {
664 set why [_check_box $x $y $width $height]
669 fail "$test_name ($why)"
673 # Check whether the text contents of the terminal match the
674 # regular expression. Note that text styling is not considered.
675 proc check_contents {test_name regexp} {
676 set contents [get_all_lines]
677 if {![gdb_assert {[regexp -- $regexp $contents]} $test_name]} {
682 # Check the contents of a box on the screen. This is a little
683 # like check_contents, but doens't check the whole screen
684 # contents, only the contents of a single box. This procedure
685 # includes (effectively) a call to check_box to ensure there is a
686 # box where expected, if there is then the contents of the box are
687 # matched against REGEXP.
688 proc check_box_contents {test_name x y width height regexp} {
691 set why [_check_box $x $y $width $height]
694 fail "$test_name (box check: $why)"
698 # Now grab the contents of the box, join each line together
699 # with a newline character and match against REGEXP.
701 for {set yy [expr {$y + 1}]} {$yy < [expr {$y + $height - 1}]} {incr yy} {
702 for {set xx [expr {$x + 1}]} {$xx < [expr {$x + $width - 1}]} {incr xx} {
703 append result [lindex $_chars($xx,$yy) 0]
708 if {![gdb_assert {[regexp -- $regexp $result]} $test_name]} {
713 # A debugging function to dump the current screen, with line
715 proc dump_screen {} {
718 verbose -log "Screen Dump ($_cols x $_rows):"
719 for {set y 0} {$y < $_rows} {incr y} {
720 set fmt [format %5d $y]
721 verbose -log "$fmt [get_line $y]"
725 # Resize the terminal.
726 proc _do_resize {rows cols} {
731 set old_rows [expr {min ($_rows, $rows)}]
732 set old_cols [expr {min ($_cols, $cols)}]
735 array set local_chars [array get _chars]
740 _clear_lines 0 $_rows
742 for {set x 0} {$x < $old_cols} {incr x} {
743 for {set y 0} {$y < $old_rows} {incr y} {
744 set _chars($x,$y) $local_chars($x,$y)
749 proc resize {rows cols} {
752 variable _resize_count
754 global gdb_spawn_name
755 # expect handles each argument to stty separately. This means
756 # that gdb will see SIGWINCH twice. Rather than rely on this
757 # behavior (which, after all, could be changed), we make it
758 # explicit here. This also simplifies waiting for the redraw.
759 _do_resize $rows $_cols
760 stty rows $_rows < $gdb_spawn_name
761 # Due to the strange column resizing behavior, and because we
762 # don't care about this intermediate resize, we don't check
764 wait_for "@@ resize done $_resize_count"
766 # Somehow the number of columns transmitted to gdb is one less
767 # than what we request from expect. We hide this weird
768 # details from the caller.
769 _do_resize $_rows $cols
770 stty columns [expr {$_cols + 1}] < $gdb_spawn_name
771 wait_for "@@ resize done $_resize_count, size = ${_cols}x${rows}"