aboutsummaryrefslogtreecommitdiff
path: root/gdb/testsuite/lib/tuiterm.exp
blob: c58b7cfda215f8a0dde940e8f658ef41e5241557 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
# Copyright 2019 Free Software Foundation, Inc.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# An ANSI terminal emulator for expect.

# The expect "spawn" function puts the tty name into the spawn_out
# array; but dejagnu doesn't export this globally.  So, we have to
# wrap spawn with our own function, so that we can capture this value.
# The value is later used in calls to stty.
rename spawn builtin_spawn
proc spawn {args} {
    set result [uplevel builtin_spawn $args]
    global gdb_spawn_name
    upvar spawn_out spawn_out
    set gdb_spawn_name $spawn_out(slave,name)
    return $result
}

namespace eval Term {
    variable _rows
    variable _cols
    variable _chars

    variable _cur_x
    variable _cur_y

    variable _attrs

    variable _last_char

    # If ARG is empty, return DEF: otherwise ARG.  This is useful for
    # defaulting arguments in CSIs.
    proc _default {arg def} {
	if {$arg == ""} {
	    return $def
	}
	return $arg
    }

    # Erase in the line Y from SX to just before EX.
    proc _clear_in_line {sx ex y} {
	variable _attrs
	variable _chars
	set lattr [array get _attrs]
	while {$sx < $ex} {
	    set _chars($sx,$y) [list " " $lattr]
	    incr sx
	}
    }

    # Erase the lines from SY to just before EY.
    proc _clear_lines {sy ey} {
	variable _cols
	while {$sy < $ey} {
	    _clear_in_line 0 $_cols $sy
	    incr sy
	}
    }

    # Beep.
    proc _ctl_0x07 {} {
    }

    # Backspace.
    proc _ctl_0x08 {} {
	variable _cur_x
	incr _cur_x -1
	if {$_cur_x < 0} {
	    variable _cur_y
	    variable _cols
	    set _cur_x [expr {$_cols - 1}]
	    incr _cur_y -1
	    if {$_cur_y < 0} {
		set _cur_y 0
	    }
	}
    }

    # Linefeed.
    proc _ctl_0x0a {} {
	variable _cur_y
	variable _rows
	incr _cur_y 1
	if {$_cur_y >= $_rows} {
	    error "FIXME scroll"
	}
    }

    # Carriage return.
    proc _ctl_0x0d {} {
	variable _cur_x
	set _cur_x 0
    }

    # Cursor Up.
    proc _csi_A {args} {
	variable _cur_y
	set arg [_default [lindex $args 0] 1]
	set _cur_y [expr {max ($_cur_y - $arg, 0)}]
    }

    # Cursor Down.
    proc _csi_B {args} {
	variable _cur_y
	variable _rows
	set arg [_default [lindex $args 0] 1]
	set _cur_y [expr {min ($_cur_y + $arg, $_rows)}]
    }

    # Cursor Forward.
    proc _csi_C {args} {
	variable _cur_x
	variable _cols
	set arg [_default [lindex $args 0] 1]
	set _cur_x [expr {min ($_cur_x + $arg, $_cols)}]
    }

    # Cursor Back.
    proc _csi_D {args} {
	variable _cur_x
	set arg [_default [lindex $args 0] 1]
	set _cur_x [expr {max ($_cur_x - $arg, 0)}]
    }

    # Cursor Next Line.
    proc _csi_E {args} {
	variable _cur_x
	variable _cur_y
	variable _rows
	set arg [_default [lindex $args 0] 1]
	set _cur_x 0
	set _cur_y [expr {min ($_cur_y + $arg, $_rows)}]
    }

    # Cursor Previous Line.
    proc _csi_F {args} {
	variable _cur_x
	variable _cur_y
	variable _rows
	set arg [_default [lindex $args 0] 1]
	set _cur_x 0
	set _cur_y [expr {max ($_cur_y - $arg, 0)}]
    }

    # Cursor Horizontal Absolute.
    proc _csi_G {args} {
	variable _cur_x
	variable _cols
	set arg [_default [lindex $args 0] 1]
	set _cur_x [expr {min ($arg - 1, $_cols)}]
    }

    # Move cursor (don't know the official name of this one).
    proc _csi_H {args} {
	variable _cur_x
	variable _cur_y
	set _cur_y [expr {[_default [lindex $args 0] 1] - 1}]
	set _cur_x [expr {[_default [lindex $args 1] 1] - 1}]
    }

    # Cursor Forward Tabulation.
    proc _csi_I {args} {
	set n [_default [lindex $args 0] 1]
	variable _cur_x
	variable _cols
	incr _cur_x [expr {$n * 8 - $_cur_x % 8}]
	if {$_cur_x >= $_cols} {
	    set _cur_x [expr {$_cols - 1}]
	}
    }

    # Erase.
    proc _csi_J {args} {
	variable _cur_x
	variable _cur_y
	variable _rows
	variable _cols
	set arg [_default [lindex $args 0] 0]
	if {$arg == 0} {
	    _clear_in_line $_cur_x $_cols $_cur_y
	    _clear_lines [expr {$_cur_y + 1}] $_rows
	} elseif {$arg == 1} {
	    _clear_lines 0 [expr {$_cur_y - 1}]
	    _clear_in_line 0 $_cur_x $_cur_y
	} elseif {$arg == 2} {
	    _clear_lines 0 $_rows
	}
    }

    # Erase Line.
    proc _csi_K {args} {
	variable _cur_x
	variable _cur_y
	variable _cols
	set arg [_default [lindex $args 0] 0]
	if {$arg == 0} {
	    # From cursor to end.
	    _clear_in_line $_cur_x $_cols $_cur_y
	} elseif {$arg == 1} {
	    _clear_in_line 0 $_cur_x $_cur_y
	} elseif {$arg == 2} {
	    _clear_in_line 0 $_cols $_cur_y
	}
    }

    # Delete lines.
    proc _csi_M {args} {
	variable _cur_y
	variable _rows
	variable _cols
	variable _chars
	set count [_default [lindex $args 0] 1]
	set y $_cur_y
	set next_y [expr {$y + 1}]
	while {$count > 0 && $next_y < $_rows} {
	    for {set x 0} {$x < $_cols} {incr x} {
		set _chars($x,$y) $_chars($x,$next_y)
	    }
	    incr y
	    incr next_y
	    incr count -1
	}
	_clear_lines $next_y $_rows
    }

    # Erase chars.
    proc _csi_X {args} {
	set n [_default [lindex $args 0] 1]
	_insert [string repeat " " $n]
    }

    # Repeat.
    proc _csi_b {args} {
	variable _last_char
	set n [_default [lindex $args 0] 1]
	_insert [string repeat $_last_char $n]
    }

    # Line Position Absolute.
    proc _csi_d {args} {
	variable _cur_y
	set _cur_y [expr {[_default [lindex $args 0] 1] - 1}]
    }

    # Select Graphic Rendition.
    proc _csi_m {args} {
	variable _attrs
	foreach item $args {
	    switch -exact -- $item {
		"" - 0 {
		    set _attrs(intensity) normal
		    set _attrs(fg) default
		    set _attrs(bg) default
		    set _attrs(underline) 0
		    set _attrs(reverse) 0
		}
		1 {
		    set _attrs(intensity) bold
		}
		2 {
		    set _attrs(intensity) dim
		}
		4 {
		    set _attrs(underline) 1
		}
		7 {
		    set _attrs(reverse) 1
		}
		22 {
		    set _attrs(intensity) normal
		}
		24 {
		    set _attrs(underline) 0
		}
		27 {
		    set _attrs(reverse) 1
		}
		30 - 31 - 32 - 33 - 34 - 35 - 36 - 37 {
		    set _attrs(fg) $item
		}
		39 {
		    set _attrs(fg) default
		}
		40 - 41 - 42 - 43 - 44 - 45 - 46 - 47 {
		    set _attrs(bg) $item
		}
		49 {
		    set _attrs(bg) default
		}
	    }
	}
    }

    # Insert string at the cursor location.
    proc _insert {str} {
	verbose "INSERT <<$str>>"
	variable _cur_x
	variable _cur_y
	variable _rows
	variable _cols
	variable _attrs
	variable _chars
	set lattr [array get _attrs]
	foreach char [split $str {}] {
	    set _chars($_cur_x,$_cur_y) [list $char $lattr]
	    incr _cur_x
	    if {$_cur_x >= $_cols} {
		set _cur_x 0
		incr _cur_y
		if {$_cur_y >= $_rows} {
		    error "FIXME scroll"
		}
	    }
	}
    }

    # Initialize.
    proc _setup {rows cols} {
	global stty_init
	set stty_init "rows $rows columns $cols"

	variable _rows
	variable _cols
	variable _cur_x
	variable _cur_y
	variable _attrs

	set _rows $rows
	set _cols $cols
	set _cur_x 0
	set _cur_y 0
	array set _attrs {
	    intensity normal
	    fg default
	    bg default
	    underline 0
	    reverse 0
	}

	_clear_lines 0 $_rows
    }

    # Accept some output from gdb and update the screen.
    proc _accept {} {
	global expect_out
	gdb_expect {
	    -re "^\[\x07\x08\x0a\x0d\]" {
		scan $expect_out(0,string) %c val
		set hexval [format "%02x" $val]
		verbose "+++ _ctl_0x${hexval}"
		_ctl_0x${hexval}
		exp_continue
	    }
	    -re "^\x1b(\[0-9a-zA-Z\])" {
		verbose "+++ unsupported escape"
		error "unsupported escape"
	    }
	    -re "^\x1b\\\[(\[0-9;\]*)(\[0-9a-zA-Z@\])" {
		set cmd $expect_out(2,string)
		set params [split $expect_out(1,string) ";"]
		verbose "+++ _csi_$cmd <<<$expect_out(1,string)>>>"
		eval _csi_$cmd $params
		exp_continue
	    }
	    -re "^\[^\x07\x08\x0a\x0d\x1b\]+" {
		_insert $expect_out(0,string)
		variable _last_char
		set _last_char [string index $expect_out(0,string) end]
		# If the prompt was just inserted, return.
		variable _cur_x
		variable _cur_y
		global gdb_prompt
		set prev [get_line $_cur_y $_cur_x]
		if {![regexp -- "$gdb_prompt \$" $prev]} {
		    exp_continue
		}
	    }
	}
    }

    # Like ::clean_restart, but ensures that gdb starts in an
    # environment where the TUI can work.  ROWS and COLS are the size
    # of the terminal.  EXECUTABLE, if given, is passed to
    # clean_restart.
    proc clean_restart {rows cols {executable {}}} {
	global env stty_init
	save_vars {env(TERM) stty_init} {
	    setenv TERM ansi
	    _setup $rows $cols
	    if {$executable == ""} {
		::clean_restart
	    } else {
		::clean_restart $executable
	    }
	}
    }

    # Start the TUI.  Returns 1 on success, 0 if TUI tests should be
    # skipped.
    proc enter_tui {} {
	if {[skip_tui_tests]} {
	    return 0
	}

	gdb_test_no_output "set tui border-kind ascii"
	command "tui enable"
	return 1
    }

    # Send the command CMD to gdb, then wait for a gdb prompt to be
    # seen in the TUI.  CMD should not end with a newline -- that will
    # be supplied by this function.
    proc command {cmd} {
	send_gdb "$cmd\n"
	_accept
    }

    # Return the text of screen line N, without attributes.  Lines are
    # 0-based.  If C is given, stop before column C.  Columns are also
    # zero-based.
    proc get_line {n {c ""}} {
	set result ""
	variable _cols
	variable _chars
	set c [_default $c $_cols]
	set x 0
	while {$x < $c} {
	    append result [lindex $_chars($x,$n) 0]
	    incr x
	}
	return $result
    }

    # Get just the character at (X, Y).
    proc get_char {x y} {
	variable _chars
	return [lindex $_chars($x,$y) 0]
    }

    # Get the entire screen as a string.
    proc get_all_lines {} {
	variable _rows
	variable _cols
	variable _chars

	set result ""
	for {set y 0} {$y < $_rows} {incr y} {
	    for {set x 0} {$x < $_cols} {incr x} {
		append result [lindex $_chars($x,$y) 0]
	    }
	    append result "\n"
	}

	return $result
    }

    # Get the text just before the cursor.
    proc get_current_line {} {
	variable _cur_x
	variable _cur_y
	return [get_line $_cur_y $_cur_x]
    }

    # Helper function for check_box.  Returns empty string if the box
    # is found, description of why not otherwise.
    proc _check_box {x y width height} {
	set x2 [expr {$x + $width - 1}]
	set y2 [expr {$y + $height - 1}]

	if {[get_char $x $y] != "+"} {
	    return "ul corner"
	}
	if {[get_char $x $y2] != "+"} {
	    return "ll corner"
	}
	if {[get_char $x2 $y] != "+"} {
	    return "ur corner"
	}
	if {[get_char $x2 $y2] != "+"} {
	    return "lr corner"
	}

	for {set i [expr {$x + 1}]} {$i < $x2 - 1} {incr i} {
	    # Note we do not check the top border of the box, because
	    # it will contain a title.
	    if {[get_char $i $y2] != "-"} {
		return "bottom border $i"
	    }
	}
	for {set i [expr {$y + 1}]} {$i < $y2 - 1} {incr i} {
	    if {[get_char $x $i] != "|"} {
		return "left side $i"
	    }
	    if {[get_char $x2 $i] != "|"} {
		return "right side $i"
	    }
	}

	return ""
    }

    # Check for a box at the given coordinates.
    proc check_box {test_name x y width height} {
	set why [_check_box $x $y $width $height]
	if {$why == ""} {
	    pass $test_name
	} else {
	    dump_screen
	    fail "$test_name ($why)"
	}
    }

    # Check whether the text contents of the terminal match the
    # regular expression.  Note that text styling is not considered.
    proc check_contents {test_name regexp} {
	set contents [get_all_lines]
	if {![gdb_assert {[regexp -- $regexp $contents]} $test_name]} {
	    dump_screen
	}
    }

    # A debugging function to dump the current screen, with line
    # numbers.
    proc dump_screen {} {
	variable _rows
	verbose "Screen Dump:"
	for {set y 0} {$y < $_rows} {incr y} {
	    set fmt [format %5d $y]
	    verbose "$fmt [get_line $y]"
	}
    }

    # Resize the terminal.
    proc resize {rows cols} {
	variable _chars
	variable _rows
	variable _cols

	set old_rows [expr {min ($_rows, $rows)}]
	set old_cols [expr {min ($_cols, $cols)}]

	# Copy locally.
	array set local_chars [array get _chars]
	unset _chars

	set _rows $rows
	set _cols $cols
	_clear_lines 0 $_rows

	for {set x 0} {$x < $old_cols} {incr x} {
	    for {set y 0} {$y < $old_rows} {incr y} {
		set _chars($x,$y) $local_chars($x,$y)
	    }
	}

	global gdb_spawn_name
	# Somehow the number of columns transmitted to gdb is one less
	# than what we request from expect.  We hide this weird
	# details from the caller.
	stty rows $_rows columns [expr {$_cols + 1}] \
	    < $gdb_spawn_name
	_accept
    }
}