1 # Copyright 2019-2022 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.
19 # Size of the terminal.
23 # Buffer / contents of the terminal.
26 # Position of the cursor.
34 variable _resize_count
37 verbose -log "+++ $what"
40 # Call BODY, then log WHAT along with the original and new cursor position.
41 proc _log_cur { what body } {
45 set orig_cur_row $_cur_row
46 set orig_cur_col $_cur_col
50 _log "$what, cursor: ($orig_cur_row, $orig_cur_col) -> ($_cur_row, $_cur_col)"
53 # If ARG is empty, return DEF: otherwise ARG. This is useful for
54 # defaulting arguments in CSIs.
55 proc _default {arg def} {
62 # Erase in the line Y from SX to just before EX.
63 proc _clear_in_line {sx ex y} {
66 set lattr [array get _attrs]
68 set _chars($sx,$y) [list " " $lattr]
73 # Erase the lines from SY to just before EY.
74 proc _clear_lines {sy ey} {
77 _clear_in_line 0 $_cols $sy
88 _log_cur "Backspace" {
96 set _cur_col [expr {$_cols - 1}]
107 _log_cur "Line feed" {
112 if {$_cur_row >= $_rows} {
120 _log_cur "Carriage return" {
129 # https://vt100.net/docs/vt510-rm/ICH.html
131 set n [_default [lindex $args 0] 1]
133 _log_cur "Insert Character ($n)" {
139 set out_x [expr {$_cur_col + $n}]
140 for {set i 0} {$i < $n} {incr i} {
141 set _chars($out_x,$_cur_row) $_chars($in_x,$_cur_row)
150 # https://vt100.net/docs/vt510-rm/CUU.html
152 set arg [_default [lindex $args 0] 1]
154 _log_cur "Cursor Up ($arg)" {
157 set _cur_row [expr {max ($_cur_row - $arg, 0)}]
163 # https://vt100.net/docs/vt510-rm/CUD.html
165 set arg [_default [lindex $args 0] 1]
167 _log_cur "Cursor Down ($arg)" {
171 set _cur_row [expr {min ($_cur_row + $arg, $_rows)}]
177 # https://vt100.net/docs/vt510-rm/CUF.html
179 set arg [_default [lindex $args 0] 1]
181 _log_cur "Cursor Forward ($arg)" {
185 set _cur_col [expr {min ($_cur_col + $arg, $_cols)}]
191 # https://vt100.net/docs/vt510-rm/CUB.html
193 set arg [_default [lindex $args 0] 1]
195 _log_cur "Cursor Backward ($arg)" {
198 set _cur_col [expr {max ($_cur_col - $arg, 0)}]
204 # https://vt100.net/docs/vt510-rm/CNL.html
206 set arg [_default [lindex $args 0] 1]
208 _log_cur "Cursor Next Line ($arg)" {
214 set _cur_row [expr {min ($_cur_row + $arg, $_rows)}]
218 # Cursor Previous Line.
220 # https://vt100.net/docs/vt510-rm/CPL.html
222 set arg [_default [lindex $args 0] 1]
224 _log_cur "Cursor Previous Line ($arg)" {
230 set _cur_row [expr {max ($_cur_row - $arg, 0)}]
234 # Cursor Horizontal Absolute.
236 # https://vt100.net/docs/vt510-rm/CHA.html
238 set arg [_default [lindex $args 0] 1]
240 _log_cur "Cursor Horizontal Absolute ($arg)" {
244 set _cur_col [expr {min ($arg - 1, $_cols)}]
250 # https://vt100.net/docs/vt510-rm/CUP.html
252 set row [_default [lindex $args 0] 1]
253 set col [_default [lindex $args 1] 1]
255 _log_cur "Cursor Position ($row, $col)" {
259 set _cur_row [expr {$row - 1}]
260 set _cur_col [expr {$col - 1}]
264 # Cursor Horizontal Forward Tabulation.
266 # https://vt100.net/docs/vt510-rm/CHT.html
268 set n [_default [lindex $args 0] 1]
270 _log_cur "Cursor Horizontal Forward Tabulation ($n)" {
274 incr _cur_col [expr {$n * 8 - $_cur_col % 8}]
275 if {$_cur_col >= $_cols} {
276 set _cur_col [expr {$_cols - 1}]
283 # https://vt100.net/docs/vt510-rm/ED.html
285 set arg [_default [lindex $args 0] 0]
287 _log_cur "Erase in Display ($arg)" {
294 _clear_in_line $_cur_col $_cols $_cur_row
295 _clear_lines [expr {$_cur_row + 1}] $_rows
296 } elseif {$arg == 1} {
297 _clear_lines 0 [expr {$_cur_row - 1}]
298 _clear_in_line 0 $_cur_col $_cur_row
299 } elseif {$arg == 2} {
300 _clear_lines 0 $_rows
307 # https://vt100.net/docs/vt510-rm/EL.html
309 set arg [_default [lindex $args 0] 0]
311 _log_cur "Erase in Line ($arg)" {
317 # From cursor to end.
318 _clear_in_line $_cur_col $_cols $_cur_row
319 } elseif {$arg == 1} {
320 _clear_in_line 0 $_cur_col $_cur_row
321 } elseif {$arg == 2} {
322 _clear_in_line 0 $_cols $_cur_row
329 # https://vt100.net/docs/vt510-rm/DL.html
331 set count [_default [lindex $args 0] 1]
333 _log_cur "Delete line ($count)" {
340 set next_y [expr {$y + $count}]
341 while {$next_y < $_rows} {
342 for {set x 0} {$x < $_cols} {incr x} {
343 set _chars($x,$y) $_chars($x,$next_y)
348 _clear_lines $y $_rows
354 # https://vt100.net/docs/vt510-rm/ECH.html
356 set n [_default [lindex $args 0] 1]
358 _log_cur "Erase chars ($n)" {
359 # Erase characters but don't move cursor.
365 set lattr [array get _attrs]
367 for {set i 0} {$i < $n} {incr i} {
368 set _chars($x,$_cur_row) [list " " $lattr]
374 # Cursor Backward Tabulation.
376 # https://vt100.net/docs/vt510-rm/CBT.html
378 set n [_default [lindex $args 0] 1]
380 _log_cur "Cursor Backward Tabulation ($n)" {
383 set _cur_col [expr {max (int (($_cur_col - 1) / 8) * 8 - ($n - 1) * 8, 0)}]
389 # https://www.xfree86.org/current/ctlseqs.html (See `(REP)`)
391 set n [_default [lindex $args 0] 1]
393 _log_cur "Repeat ($n)" {
396 _insert [string repeat $_last_char $n]
400 # Vertical Line Position Absolute.
402 # https://vt100.net/docs/vt510-rm/VPA.html
404 set row [_default [lindex $args 0] 1]
406 _log_cur "Vertical Line Position Absolute ($row)" {
409 set _cur_row [expr {$row - 1}]
413 # Select Graphic Rendition.
415 # https://vt100.net/docs/vt510-rm/SGR.html
417 _log_cur "Select Graphic Rendition ([join $args {, }])" {
421 switch -exact -- $item {
423 set _attrs(intensity) normal
424 set _attrs(fg) default
425 set _attrs(bg) default
426 set _attrs(underline) 0
427 set _attrs(reverse) 0
430 set _attrs(intensity) bold
433 set _attrs(intensity) dim
436 set _attrs(underline) 1
439 set _attrs(reverse) 1
442 set _attrs(intensity) normal
445 set _attrs(underline) 0
448 set _attrs(reverse) 1
450 30 - 31 - 32 - 33 - 34 - 35 - 36 - 37 {
454 set _attrs(fg) default
456 40 - 41 - 42 - 43 - 44 - 45 - 46 - 47 {
460 set _attrs(bg) default
467 # Insert string at the cursor location.
469 _log_cur "Inserted string '$str'" {
470 _log "Inserting string '$str'"
478 set lattr [array get _attrs]
479 foreach char [split $str {}] {
480 _log_cur " Inserted char '$char'" {
481 set _chars($_cur_col,$_cur_row) [list $char $lattr]
483 if {$_cur_col >= $_cols} {
486 if {$_cur_row >= $_rows} {
496 proc _setup {rows cols} {
498 set stty_init "rows $rows columns $cols"
505 variable _resize_count
520 _clear_lines 0 $_rows
523 # Accept some output from gdb and update the screen. WAIT_FOR is
524 # a regexp matching the line to wait for. Return 0 on timeout, 1
526 proc wait_for {wait_for} {
532 set prompt_wait_for "$gdb_prompt \$"
536 -re "^\[\x07\x08\x0a\x0d\]" {
537 scan $expect_out(0,string) %c val
538 set hexval [format "%02x" $val]
539 _log "wait_for: _ctl_0x${hexval}"
542 -re "^\x1b(\[0-9a-zA-Z\])" {
543 _log "wait_for: unsupported escape"
544 error "unsupported escape"
546 -re "^\x1b\\\[(\[0-9;\]*)(\[a-zA-Z@\])" {
547 set cmd $expect_out(2,string)
548 set params [split $expect_out(1,string) ";"]
549 _log "wait_for: _csi_$cmd <<<$expect_out(1,string)>>>"
550 eval _csi_$cmd $params
552 -re "^\[^\x07\x08\x0a\x0d\x1b\]+" {
553 _insert $expect_out(0,string)
555 set _last_char [string index $expect_out(0,string) end]
559 # Assume a timeout means we somehow missed the
560 # expected result, and carry on.
565 # If the cursor appears just after the prompt, return. It
566 # isn't reliable to check this only after an insertion,
567 # because curses may make "unusual" redrawing decisions.
568 if {$wait_for == "$prompt_wait_for"} {
569 set prev [get_line $_cur_row $_cur_col]
571 set prev [get_line $_cur_row]
573 if {[regexp -- $wait_for $prev]} {
574 if {$wait_for == "$prompt_wait_for"} {
577 set wait_for $prompt_wait_for
584 # Like ::clean_restart, but ensures that gdb starts in an
585 # environment where the TUI can work. ROWS and COLS are the size
586 # of the terminal. EXECUTABLE, if given, is passed to
588 proc clean_restart {rows cols {executable {}}} {
590 save_vars {env(TERM) stty_init} {
593 if {$executable == ""} {
596 ::clean_restart $executable
601 # Setup ready for starting the tui, but don't actually start it.
602 # Returns 1 on success, 0 if TUI tests should be skipped.
603 proc prepare_for_tui {} {
604 if {[skip_tui_tests]} {
608 gdb_test_no_output "set tui border-kind ascii"
609 gdb_test_no_output "maint set tui-resize-message on"
613 # Start the TUI. Returns 1 on success, 0 if TUI tests should be
616 if {![prepare_for_tui]} {
620 command_no_prompt_prefix "tui enable"
624 # Send the command CMD to gdb, then wait for a gdb prompt to be
625 # seen in the TUI. CMD should not end with a newline -- that will
626 # be supplied by this function.
630 set str [string_to_regexp $cmd]
631 set str "^$gdb_prompt $str"
635 # As proc command, but don't wait for a initial prompt. This is used for
636 # inital terminal commands, where there's no prompt yet.
637 proc command_no_prompt_prefix {cmd} {
639 set str [string_to_regexp $cmd]
643 # Return the text of screen line N, without attributes. Lines are
644 # 0-based. If C is given, stop before column C. Columns are also
646 proc get_line {n {c ""}} {
648 # This can happen during resizing, if the cursor seems to
649 # temporarily be off-screen.
657 set c [_default $c $_cols]
660 append result [lindex $_chars($x,$n) 0]
666 # Get just the character at (X, Y).
667 proc get_char {x y} {
669 return [lindex $_chars($x,$y) 0]
672 # Get the entire screen as a string.
673 proc get_all_lines {} {
679 for {set y 0} {$y < $_rows} {incr y} {
680 for {set x 0} {$x < $_cols} {incr x} {
681 append result [lindex $_chars($x,$y) 0]
689 # Get the text just before the cursor.
690 proc get_current_line {} {
693 return [get_line $_cur_row $_cur_col]
696 # Helper function for check_box. Returns empty string if the box
697 # is found, description of why not otherwise.
698 proc _check_box {x y width height} {
699 set x2 [expr {$x + $width - 1}]
700 set y2 [expr {$y + $height - 1}]
702 verbose -log "_check_box x=$x, y=$y, x2=$x2, y2=$y2, width=$width, height=$height"
704 set c [get_char $x $y]
706 return "ul corner is $c, not +"
709 set c [get_char $x $y2]
711 return "ll corner is $c, not +"
714 set c [get_char $x2 $y]
716 return "ur corner is $c, not +"
719 set c [get_char $x2 $y2]
721 return "lr corner is $c, not +"
724 # Note we do not check the full horizonal borders of the box.
725 # The top will contain a title, and the bottom may as well, if
726 # it is overlapped by some other border. However, at most a
727 # title should appear as '+-VERY LONG TITLE-+', so we can
728 # check for the '+-' on the left, and '-+' on the right.
729 set c [get_char [expr {$x + 1}] $y]
731 return "ul title padding is $c, not -"
734 set c [get_char [expr {$x2 - 1}] $y]
736 return "ul title padding is $c, not -"
739 # Now check the vertical borders.
740 for {set i [expr {$y + 1}]} {$i < $y2 - 1} {incr i} {
741 set c [get_char $x $i]
743 return "left side $i is $c, not |"
746 set c [get_char $x2 $i]
748 return "right side $i is $c, not |"
755 # Check for a box at the given coordinates.
756 proc check_box {test_name x y width height} {
757 set why [_check_box $x $y $width $height]
762 fail "$test_name ($why)"
766 # Check whether the text contents of the terminal match the
767 # regular expression. Note that text styling is not considered.
768 proc check_contents {test_name regexp} {
769 set contents [get_all_lines]
770 if {![gdb_assert {[regexp -- $regexp $contents]} $test_name]} {
775 # Check that the region of the screen described by X, Y, WIDTH,
776 # and HEIGHT match REGEXP. This is like check_contents except
777 # only part of the screen is checked. This can be used to check
778 # the contents within a box (though check_box_contents is a better
779 # choice for boxes with a border).
780 proc check_region_contents { test_name x y width height regexp } {
783 # Now grab the contents of the box, join each line together
784 # with a '\r\n' sequence and match against REGEXP.
786 for {set yy $y} {$yy < [expr {$y + $height}]} {incr yy} {
788 # Add the end of line sequence only if this isn't the
792 for {set xx $x} {$xx < [expr {$x + $width}]} {incr xx} {
793 append result [lindex $_chars($xx,$yy) 0]
797 if {![gdb_assert {[regexp -- $regexp $result]} $test_name]} {
802 # Check the contents of a box on the screen. This is a little
803 # like check_contents, but doens't check the whole screen
804 # contents, only the contents of a single box. This procedure
805 # includes (effectively) a call to check_box to ensure there is a
806 # box where expected, if there is then the contents of the box are
807 # matched against REGEXP.
808 proc check_box_contents {test_name x y width height regexp} {
811 set why [_check_box $x $y $width $height]
814 fail "$test_name (box check: $why)"
818 check_region_contents $test_name [expr {$x + 1}] [expr {$y + 1}] \
819 [expr {$width - 2}] [expr {$height - 2}] $regexp
822 # A debugging function to dump the current screen, with line
824 proc dump_screen {} {
827 verbose -log "Screen Dump ($_cols x $_rows):"
828 for {set y 0} {$y < $_rows} {incr y} {
829 set fmt [format %5d $y]
830 verbose -log "$fmt [get_line $y]"
834 # Resize the terminal.
835 proc _do_resize {rows cols} {
840 set old_rows [expr {min ($_rows, $rows)}]
841 set old_cols [expr {min ($_cols, $cols)}]
844 array set local_chars [array get _chars]
849 _clear_lines 0 $_rows
851 for {set x 0} {$x < $old_cols} {incr x} {
852 for {set y 0} {$y < $old_rows} {incr y} {
853 set _chars($x,$y) $local_chars($x,$y)
858 proc resize {rows cols} {
861 variable _resize_count
863 # expect handles each argument to stty separately. This means
864 # that gdb will see SIGWINCH twice. Rather than rely on this
865 # behavior (which, after all, could be changed), we make it
866 # explicit here. This also simplifies waiting for the redraw.
867 _do_resize $rows $_cols
868 stty rows $_rows < $::gdb_tty_name
869 # Due to the strange column resizing behavior, and because we
870 # don't care about this intermediate resize, we don't check
872 wait_for "@@ resize done $_resize_count"
874 # Somehow the number of columns transmitted to gdb is one less
875 # than what we request from expect. We hide this weird
876 # details from the caller.
877 _do_resize $_rows $cols
878 stty columns [expr {$_cols + 1}] < $::gdb_tty_name
879 wait_for "@@ resize done $_resize_count, size = ${_cols}x${rows}"