* configure.in: Revise sol-thread.o test.
[deliverable/binutils-gdb.git] / gdb / gdbtk.tcl
CommitLineData
4604b34c 1# GDB GUI setup for GDB, the GNU debugger.
1a57cd09 2# Copyright 1994, 1995, 1996
4604b34c
SG
3# Free Software Foundation, Inc.
4
5# Written by Stu Grossman <grossman@cygnus.com> of Cygnus Support.
6
7# This file is part of GDB.
8
9# This program is free software; you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation; either version 2 of the License, or
12# (at your option) any later version.
13
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18
19# You should have received a copy of the GNU General Public License
20# along with this program; if not, write to the Free Software
4e327047 21# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
754e5da2
SG
22
23set cfile Blank
006e71e9 24set wins($cfile) .src.text
754e5da2 25set current_label {}
8532893d 26set cfunc NIL
86db943c 27set line_numbers 1
546b8ca7 28set breakpoint_file(-1) {[garbage]}
280c564c 29set disassemble_with_source nosource
b66051ec 30set expr_update_list(0) 0
5bac2b50 31set gdb_prompt "(gdb) "
86db943c 32
9e9cf822
SS
33set debug_interface 0
34
006e71e9
SG
35#option add *Foreground Black
36#option add *Background White
37#option add *Font -*-*-medium-r-normal--18-*-*-*-m-*-*-1
754e5da2
SG
38
39proc echo string {puts stdout $string}
40
4e327047
TT
41# Assign elements from LIST to variables named in ARGS. FIXME replace
42# with TclX version someday.
43proc lassign {list args} {
44 set len [expr {[llength $args] - 1}]
45 while {$len >= 0} {
46 upvar [lindex $args $len] local
47 set local [lindex $list $len]
48 decr len
49 }
50}
51
52#
53# Local procedure:
54#
55# decr (var val) - compliment to incr
56#
57# Description:
58#
59#
60proc decr {var {val 1}} {
61 upvar $var num
62 set num [expr {$num - $val}]
63 return $num
64}
65
66#
67# Center a window on the screen.
68#
69proc center_window toplevel {
70 # Withdraw and update, to ensure geometry computations are finished.
71 wm withdraw $toplevel
72 update idletasks
73
74 set x [expr {[winfo screenwidth $toplevel] / 2
75 - [winfo reqwidth $toplevel] / 2
76 - [winfo vrootx $toplevel]}]
77 set y [expr {[winfo screenheight $toplevel] / 2
78 - [winfo reqheight $toplevel] / 2
79 - [winfo vrooty $toplevel]}]
80 wm geometry $toplevel +${x}+${y}
81 wm deiconify $toplevel
82}
83
84#
85# Rearrange the bindtags so the widget comes after the class. I was
86# always for Ousterhout putting the class bindings first, but no...
87#
88proc bind_widget_after_class {widget} {
89 set class [winfo class $widget]
90 set newList {}
91 foreach tag [bindtags $widget] {
92 if {$tag == $widget} {
93 # Nothing.
94 } {
95 lappend newList $tag
96 if {$tag == $class} {
97 lappend newList $widget
98 }
99 }
100 }
101 bindtags $widget $newList
102}
103
f0b0d915
TT
104#
105# Make sure line number $LINE is visible in the text widget. But be
106# more clever than the "see" command: if LINE is not currently
107# displayed, arrange for LINE to be centered. There are cases in
108# which this does not work, so as a last resort we revert to "see".
109#
110# This is inefficient, but probably not slow enough to actually
111# notice.
112#
113proc ensure_line_visible {text line} {
114 set pixHeight [winfo height $text]
115 # Compute height of widget in lines. This fails if a line is wider
116 # than the screen. FIXME.
117 set topLine [lindex [split [$text index @0,0] .] 0]
118 set botLine [lindex [split [$text index @0,${pixHeight}] .] 0]
119
120 if {$line > $topLine && $line < $botLine} then {
121 # Onscreen, and not on the very edge.
122 return
123 }
124
125 set newTop [expr {$line - ($botLine - $topLine)}]
126 if {$newTop < 0} then {
127 set newTop 0
128 }
129 $text yview moveto $newTop
130
131 # In case the above failed.
132 $text see ${line}.0
133}
134
4e327047
TT
135if {[info exists env(EDITOR)]} then {
136 set editor $env(EDITOR)
137} else {
138 set editor emacs
8532893d
SG
139}
140
141# GDB callbacks
142#
143# These functions are called by GDB (from C code) to do various things in
144# TK-land. All start with the prefix `gdbtk_tcl_' to make them easy to find.
145#
146
147#
148# GDB Callback:
149#
150# gdbtk_tcl_fputs (text) - Output text to the command window
151#
152# Description:
153#
154# GDB calls this to output TEXT to the GDB command window. The text is
155# placed at the end of the text widget. Note that output may not occur,
156# due to buffering. Use gdbtk_tcl_flush to cause an immediate update.
157#
158
754e5da2 159proc gdbtk_tcl_fputs {arg} {
4e327047
TT
160 .cmd.text insert end "$arg"
161 .cmd.text see end
8532893d
SG
162}
163
86db943c 164proc gdbtk_tcl_fputs_error {arg} {
4e327047
TT
165 .cmd.text insert end "$arg"
166 .cmd.text see end
86db943c
SG
167}
168
8532893d
SG
169#
170# GDB Callback:
171#
172# gdbtk_tcl_flush () - Flush output to the command window
173#
174# Description:
175#
176# GDB calls this to force all buffered text to the GDB command window.
177#
178
179proc gdbtk_tcl_flush {} {
4e327047
TT
180 .cmd.text see end
181 update idletasks
754e5da2
SG
182}
183
8532893d
SG
184#
185# GDB Callback:
186#
187# gdbtk_tcl_query (message) - Create a yes/no query dialog box
188#
189# Description:
190#
191# GDB calls this to create a yes/no dialog box containing MESSAGE. GDB
192# is hung while the dialog box is active (ie: no commands will work),
193# however windows can still be refreshed in case of damage or exposure.
194#
754e5da2
SG
195
196proc gdbtk_tcl_query {message} {
4e327047
TT
197 # FIXME We really want a Help button here. But Tk's brain-damaged
198 # modal dialogs won't really allow it. Should have async dialog
199 # here.
200 set result [tk_dialog .query "gdb : query" "$message" questhead 0 Yes No]
201 return [expr {!$result}]
202}
754e5da2 203
8532893d
SG
204#
205# GDB Callback:
206#
207# gdbtk_start_variable_annotation (args ...) -
208#
209# Description:
210#
211# Not yet implemented.
212#
754e5da2 213
4e327047
TT
214proc gdbtk_tcl_start_variable_annotation {valaddr ref_type stor_cl
215 cum_expr field type_cast} {
216 echo "gdbtk_tcl_start_variable_annotation $valaddr $ref_type $stor_cl $cum_expr $field $type_cast"
754e5da2
SG
217}
218
8532893d
SG
219#
220# GDB Callback:
221#
222# gdbtk_end_variable_annotation (args ...) -
223#
224# Description:
225#
226# Not yet implemented.
227#
228
754e5da2
SG
229proc gdbtk_tcl_end_variable_annotation {} {
230 echo gdbtk_tcl_end_variable_annotation
231}
232
8532893d
SG
233#
234# GDB Callback:
235#
236# gdbtk_tcl_breakpoint (action bpnum file line) - Notify the TK
237# interface of changes to breakpoints.
238#
239# Description:
240#
241# GDB calls this to notify TK of changes to breakpoints. ACTION is one
242# of:
243# create - Notify of breakpoint creation
244# delete - Notify of breakpoint deletion
6131622e 245# modify - Notify of breakpoint modification
8532893d
SG
246#
247
6131622e
SG
248# file line pc type enabled disposition silent ignore_count commands cond_string thread hit_count
249
250proc gdbtk_tcl_breakpoint {action bpnum} {
251 set bpinfo [gdb_get_breakpoint_info $bpnum]
252 set file [lindex $bpinfo 0]
253 set line [lindex $bpinfo 1]
254 set pc [lindex $bpinfo 2]
255 set enable [lindex $bpinfo 4]
256
257 if {$action == "modify"} {
f61f41d9 258 if {$enable == "1"} {
6131622e
SG
259 set action enable
260 } else {
261 set action disable
262 }
263 }
264
8532893d 265 ${action}_breakpoint $bpnum $file $line $pc
754e5da2
SG
266}
267
41756e56
FF
268#
269# GDB Callback:
270#
271# gdbtk_tcl_readline_begin (message) - Notify Tk to open an interaction
272# window and start gathering user input
273#
274# Description:
275#
276# GDB calls this to notify TK that it needs to open an interaction
277# window, displaying the given message, and be prepared to accept
278# calls to gdbtk_tcl_readline to gather user input.
279
280proc gdbtk_tcl_readline_begin {message} {
281 global readline_text
282
283 # If another readline window already exists, just bring it to the front.
284 if {[winfo exists .rl]} {raise .rl ; return}
285
286 # Create top level frame with scrollbar and text widget.
287 toplevel .rl
288 wm title .rl "Interaction Window"
289 wm iconname .rl "Input"
290 message .rl.msg -text $message -aspect 7500 -justify left
291 text .rl.text -width 80 -height 20 -setgrid true -cursor hand2 \
292 -yscrollcommand {.rl.scroll set}
293 scrollbar .rl.scroll -command {.rl.text yview}
294 pack .rl.msg -side top -fill x
295 pack .rl.scroll -side right -fill y
296 pack .rl.text -side left -fill both -expand true
297
298 # When the user presses return, get the text from the command start mark to the
299 # current insert point, stash it in the readline text variable, and update the
300 # command start mark to the current insert point
301 bind .rl.text <Return> {
302 set readline_text [.rl.text get cmdstart {end - 1 char}]
303 .rl.text mark set cmdstart insert
304 }
3f8eefba
FF
305 bind .rl.text <BackSpace> {
306 if [%W compare insert > cmdstart] {
307 %W delete {insert - 1 char} insert
308 } else {
309 bell
310 }
311 break
312 }
313 bind .rl.text <Any-Key> {
314 if [%W compare insert < cmdstart] {
315 %W mark set insert end
316 }
317 }
318 bind .rl.text <Control-u> {
319 %W delete cmdstart "insert lineend"
320 %W see insert
321 }
41756e56
FF
322 bindtags .rl.text {.rl.text Text all}
323}
324
325#
326# GDB Callback:
327#
328# gdbtk_tcl_readline (prompt) - Get one user input line
329#
330# Description:
331#
332# GDB calls this to get one line of input from the user interaction
333# window, using "prompt" as the command line prompt.
334
335proc gdbtk_tcl_readline {prompt} {
336 global readline_text
337
338 .rl.text insert end $prompt
339 .rl.text mark set cmdstart insert
340 .rl.text mark gravity cmdstart left
341 .rl.text see insert
342
343 # Make this window the current one for input.
344 focus .rl.text
345 grab .rl
346 tkwait variable readline_text
347 grab release .rl
348 return $readline_text
349}
350
351#
352# GDB Callback:
353#
354# gdbtk_tcl_readline_end - Terminate a user interaction
355#
356# Description:
357#
358# GDB calls this when it is done getting interactive user input.
359# Destroy the interaction window.
360
361proc gdbtk_tcl_readline_end {} {
362 if {[winfo exists .rl]} { destroy .rl }
363}
364
6131622e
SG
365proc create_breakpoints_window {} {
366 global bpframe_lasty
367
4e327047 368 if {[winfo exists .breakpoints]} {raise .breakpoints ; return}
6131622e
SG
369
370 build_framework .breakpoints "Breakpoints" ""
371
372# First, delete all the old view menu entries
373
374 .breakpoints.menubar.view.menu delete 0 last
375
376# Get rid of label
377
378 destroy .breakpoints.label
379
380# Replace text with a canvas and fix the scrollbars
381
382 destroy .breakpoints.text
6131622e
SG
383 scrollbar .breakpoints.scrollx -orient horizontal \
384 -command {.breakpoints.c xview} -relief sunken
4e327047
TT
385 canvas .breakpoints.c -relief sunken -bd 2 \
386 -cursor hand2 \
387 -yscrollcommand {.breakpoints.scroll set} \
388 -xscrollcommand {.breakpoints.scrollx set}
389 .breakpoints.scroll configure -command {.breakpoints.c yview}
6131622e
SG
390
391 pack .breakpoints.scrollx -side bottom -fill x -in .breakpoints.info
392 pack .breakpoints.c -side left -expand yes -fill both \
393 -in .breakpoints.info
394
395 set bpframe_lasty 0
396
397# Create a frame for each breakpoint
398
399 foreach bpnum [gdb_get_breakpoint_list] {
400 add_breakpoint_frame $bpnum
401 }
402}
403
404# Create a frame for bpnum in the .breakpoints canvas
405
406proc add_breakpoint_frame bpnum {
4e327047
TT
407 global bpframe_lasty
408 global enabled
409 global disposition
410
411 if {![winfo exists .breakpoints]} return
412
413 set bpinfo [gdb_get_breakpoint_info $bpnum]
414
415 lassign $bpinfo file line pc type enabled($bpnum) disposition($bpnum) \
416 silent ignore_count commands cond thread hit_count
417
418 set f .breakpoints.c.$bpnum
419
420 if {![winfo exists $f]} {
421 frame $f -relief sunken -bd 2
422
423 label $f.id -text "#$bpnum $file:$line ($pc)" \
424 -relief flat -bd 2 -anchor w
425 frame $f.hit_count
426 label $f.hit_count.label -text "Hit count:" -relief flat \
427 -bd 2 -anchor w -width 11
428 label $f.hit_count.val -text $hit_count -relief flat \
429 -bd 2 -anchor w
430 checkbutton $f.hit_count.enabled -text Enabled \
431 -variable enabled($bpnum) -anchor w -relief flat
432
433 pack $f.hit_count.label $f.hit_count.val -side left
434 pack $f.hit_count.enabled -side right
435
436 frame $f.thread
437 label $f.thread.label -text "Thread: " -relief flat -bd 2 \
438 -width 11 -anchor w
439 entry $f.thread.entry -bd 2 -relief sunken -width 10
440 $f.thread.entry insert end $thread
441 pack $f.thread.label -side left
442 pack $f.thread.entry -side left -fill x
443
444 frame $f.cond
445 label $f.cond.label -text "Condition: " -relief flat -bd 2 \
446 -width 11 -anchor w
447 entry $f.cond.entry -bd 2 -relief sunken
448 $f.cond.entry insert end $cond
449 pack $f.cond.label -side left
450 pack $f.cond.entry -side left -fill x -expand yes
451
452 frame $f.ignore_count
453 label $f.ignore_count.label -text "Ignore count: " \
454 -relief flat -bd 2 -width 11 -anchor w
455 entry $f.ignore_count.entry -bd 2 -relief sunken -width 10
456 $f.ignore_count.entry insert end $ignore_count
457 pack $f.ignore_count.label -side left
458 pack $f.ignore_count.entry -side left -fill x
459
460 frame $f.disps
461
462 label $f.disps.label -text "Disposition: " -relief flat -bd 2 \
463 -anchor w -width 11
464
465 radiobutton $f.disps.delete -text Delete \
466 -variable disposition($bpnum) -anchor w -relief flat \
467 -command "gdb_cmd \"delete break $bpnum\"" \
468 -value delete
469
470 radiobutton $f.disps.disable -text Disable \
471 -variable disposition($bpnum) -anchor w -relief flat \
472 -command "gdb_cmd \"disable break $bpnum\"" \
473 -value disable
474
475 radiobutton $f.disps.donttouch -text "Leave alone" \
476 -variable disposition($bpnum) -anchor w -relief flat \
477 -command "gdb_cmd \"enable break $bpnum\"" \
478 -value donttouch
479
480 pack $f.disps.label $f.disps.delete $f.disps.disable \
481 $f.disps.donttouch -side left -anchor w
482 text $f.commands -relief sunken -bd 2 -setgrid true \
483 -cursor hand2 -height 3 -width 30
484
485 foreach line $commands {
486 $f.commands insert end "${line}\n"
487 }
6131622e 488
4e327047
TT
489 pack $f.id -side top -anchor nw -fill x
490 pack $f.hit_count $f.cond $f.thread $f.ignore_count $f.disps \
491 $f.commands -side top -fill x -anchor nw
492 }
6131622e 493
4e327047
TT
494 set tag [.breakpoints.c create window 0 $bpframe_lasty -window $f -anchor nw]
495 update
496 set bbox [.breakpoints.c bbox $tag]
6131622e 497
4e327047 498 set bpframe_lasty [lindex $bbox 3]
f1b64caa 499
4e327047 500 .breakpoints.c configure -width [lindex $bbox 2]
6131622e
SG
501}
502
503# Delete a breakpoint frame
504
505proc delete_breakpoint_frame bpnum {
506 global bpframe_lasty
507
4e327047 508 if {![winfo exists .breakpoints]} return
6131622e
SG
509
510# First, clear the canvas
511
512 .breakpoints.c delete all
513
514# Now, repopulate it with all but the doomed breakpoint
515
516 set bpframe_lasty 0
517 foreach bp [gdb_get_breakpoint_list] {
518 if {$bp != $bpnum} {
519 add_breakpoint_frame $bp
520 }
521 }
522}
523
335129a9 524proc asm_win_name {funcname} {
546b8ca7
SG
525 if {$funcname == "*None*"} {return .asm.text}
526
335129a9
SG
527 regsub -all {\.} $funcname _ temp
528
529 return .asm.func_${temp}
530}
531
8532893d
SG
532#
533# Local procedure:
534#
535# create_breakpoint (bpnum file line pc) - Record breakpoint info in TK land
536#
537# Description:
538#
539# GDB calls this indirectly (through gdbtk_tcl_breakpoint) to notify TK
540# land of breakpoint creation. This consists of recording the file and
541# line number in the breakpoint_file and breakpoint_line arrays. Also,
542# if there is already a window associated with FILE, it is updated with
543# a breakpoint tag.
544#
545
546proc create_breakpoint {bpnum file line pc} {
754e5da2
SG
547 global wins
548 global breakpoint_file
549 global breakpoint_line
8532893d 550 global pos_to_breakpoint
335129a9 551 global pos_to_bpcount
8532893d
SG
552 global cfunc
553 global pclist
754e5da2
SG
554
555# Record breakpoint locations
556
557 set breakpoint_file($bpnum) $file
558 set breakpoint_line($bpnum) $line
8532893d 559 set pos_to_breakpoint($file:$line) $bpnum
4e327047 560 if {![info exists pos_to_bpcount($file:$line)]} {
335129a9
SG
561 set pos_to_bpcount($file:$line) 0
562 }
563 incr pos_to_bpcount($file:$line)
564 set pos_to_breakpoint($pc) $bpnum
4e327047 565 if {![info exists pos_to_bpcount($pc)]} {
335129a9
SG
566 set pos_to_bpcount($pc) 0
567 }
568 incr pos_to_bpcount($pc)
754e5da2 569
8532893d 570# If there's a window for this file, update it
754e5da2 571
4e327047 572 if {[info exists wins($file)]} {
754e5da2
SG
573 insert_breakpoint_tag $wins($file) $line
574 }
8532893d
SG
575
576# If there's an assembly window, update that too
577
335129a9 578 set win [asm_win_name $cfunc]
4e327047 579 if {[winfo exists $win]} {
637b1661 580 insert_breakpoint_tag $win [pc_to_line $pclist($cfunc) $pc]
8532893d 581 }
6131622e
SG
582
583# Update the breakpoints window
584
585 add_breakpoint_frame $bpnum
754e5da2
SG
586}
587
8532893d
SG
588#
589# Local procedure:
590#
591# delete_breakpoint (bpnum file line pc) - Delete breakpoint info from TK land
592#
593# Description:
594#
595# GDB calls this indirectly (through gdbtk_tcl_breakpoint) to notify TK
596# land of breakpoint destruction. This consists of removing the file and
597# line number from the breakpoint_file and breakpoint_line arrays. Also,
598# if there is already a window associated with FILE, the tags are removed
599# from it.
600#
601
602proc delete_breakpoint {bpnum file line pc} {
754e5da2
SG
603 global wins
604 global breakpoint_file
605 global breakpoint_line
8532893d 606 global pos_to_breakpoint
335129a9
SG
607 global pos_to_bpcount
608 global cfunc pclist
754e5da2 609
8532893d 610# Save line number and file for later
754e5da2
SG
611
612 set line $breakpoint_line($bpnum)
613
8532893d
SG
614 set file $breakpoint_file($bpnum)
615
754e5da2
SG
616# Reset breakpoint annotation info
617
335129a9 618 if {$pos_to_bpcount($file:$line) > 0} {
637b1661 619 decr pos_to_bpcount($file:$line)
335129a9
SG
620
621 if {$pos_to_bpcount($file:$line) == 0} {
637b1661
SG
622 catch "unset pos_to_breakpoint($file:$line)"
623
335129a9
SG
624 unset breakpoint_file($bpnum)
625 unset breakpoint_line($bpnum)
754e5da2 626
8532893d 627# If there's a window for this file, update it
754e5da2 628
4e327047 629 if {[info exists wins($file)]} {
335129a9
SG
630 delete_breakpoint_tag $wins($file) $line
631 }
632 }
633 }
634
635# If there's an assembly window, update that too
636
637 if {$pos_to_bpcount($pc) > 0} {
637b1661 638 decr pos_to_bpcount($pc)
335129a9
SG
639
640 if {$pos_to_bpcount($pc) == 0} {
637b1661
SG
641 catch "unset pos_to_breakpoint($pc)"
642
335129a9 643 set win [asm_win_name $cfunc]
4e327047 644 if {[winfo exists $win]} {
637b1661 645 delete_breakpoint_tag $win [pc_to_line $pclist($cfunc) $pc]
335129a9
SG
646 }
647 }
754e5da2 648 }
6131622e
SG
649
650 delete_breakpoint_frame $bpnum
754e5da2
SG
651}
652
8532893d
SG
653#
654# Local procedure:
655#
656# enable_breakpoint (bpnum file line pc) - Record breakpoint info in TK land
657#
658# Description:
659#
660# GDB calls this indirectly (through gdbtk_tcl_breakpoint) to notify TK
661# land of a breakpoint being enabled. This consists of unstippling the
662# specified breakpoint indicator.
663#
664
665proc enable_breakpoint {bpnum file line pc} {
666 global wins
335129a9 667 global cfunc pclist
f61f41d9 668 global enabled
335129a9 669
4e327047 670 if {[info exists wins($file)]} {
335129a9
SG
671 $wins($file) tag configure $line -fgstipple {}
672 }
754e5da2 673
335129a9
SG
674# If there's an assembly window, update that too
675
676 set win [asm_win_name $cfunc]
4e327047 677 if {[winfo exists $win]} {
637b1661 678 $win tag configure [pc_to_line $pclist($cfunc) $pc] -fgstipple {}
335129a9 679 }
f61f41d9
MT
680
681# If there's a breakpoint window, update that too
682
4e327047 683 if {[winfo exists .breakpoints]} {
f61f41d9
MT
684 set enabled($bpnum) 1
685 }
754e5da2
SG
686}
687
8532893d
SG
688#
689# Local procedure:
690#
691# disable_breakpoint (bpnum file line pc) - Record breakpoint info in TK land
692#
693# Description:
694#
695# GDB calls this indirectly (through gdbtk_tcl_breakpoint) to notify TK
696# land of a breakpoint being disabled. This consists of stippling the
697# specified breakpoint indicator.
698#
699
700proc disable_breakpoint {bpnum file line pc} {
701 global wins
335129a9 702 global cfunc pclist
f61f41d9 703 global enabled
335129a9 704
4e327047 705 if {[info exists wins($file)]} {
335129a9
SG
706 $wins($file) tag configure $line -fgstipple gray50
707 }
754e5da2 708
335129a9
SG
709# If there's an assembly window, update that too
710
711 set win [asm_win_name $cfunc]
4e327047 712 if {[winfo exists $win]} {
637b1661 713 $win tag configure [pc_to_line $pclist($cfunc) $pc] -fgstipple gray50
335129a9 714 }
f61f41d9
MT
715
716# If there's a breakpoint window, update that too
717
4e327047 718 if {[winfo exists .breakpoints]} {
f61f41d9
MT
719 set enabled($bpnum) 0
720 }
8532893d
SG
721}
722
723#
724# Local procedure:
725#
726# insert_breakpoint_tag (win line) - Insert a breakpoint tag in WIN.
727#
728# Description:
729#
730# GDB calls this indirectly (through gdbtk_tcl_breakpoint) to insert a
731# breakpoint tag into window WIN at line LINE.
732#
754e5da2 733
8532893d
SG
734proc insert_breakpoint_tag {win line} {
735 $win configure -state normal
736 $win delete $line.0
737 $win insert $line.0 "B"
9e9cf822 738 $win tag add margin $line.0 $line.8
8532893d
SG
739
740 $win configure -state disabled
741}
742
743#
744# Local procedure:
745#
746# delete_breakpoint_tag (win line) - Remove a breakpoint tag from WIN.
747#
748# Description:
749#
750# GDB calls this indirectly (through gdbtk_tcl_breakpoint) to remove a
751# breakpoint tag from window WIN at line LINE.
752#
753
754proc delete_breakpoint_tag {win line} {
755 $win configure -state normal
756 $win delete $line.0
746d1df4
SG
757 if {[string range $win 0 3] == ".src"} then {
758 $win insert $line.0 "\xa4"
759 } else {
760 $win insert $line.0 " "
761 }
9e9cf822 762 $win tag add margin $line.0 $line.8
8532893d
SG
763 $win configure -state disabled
764}
754e5da2 765
479f0f18 766proc gdbtk_tcl_busy {} {
fda6fadc
SS
767 if {[winfo exists .cmd]} {
768 .cmd.text configure -state disabled
769 }
4e327047 770 if {[winfo exists .src]} {
6131622e
SG
771 .src.start configure -state disabled
772 .src.stop configure -state normal
773 .src.step configure -state disabled
774 .src.next configure -state disabled
775 .src.continue configure -state disabled
776 .src.finish configure -state disabled
777 .src.up configure -state disabled
778 .src.down configure -state disabled
779 .src.bottom configure -state disabled
86db943c 780 }
4e327047 781 if {[winfo exists .asm]} {
6131622e
SG
782 .asm.stepi configure -state disabled
783 .asm.nexti configure -state disabled
784 .asm.continue configure -state disabled
785 .asm.finish configure -state disabled
786 .asm.up configure -state disabled
787 .asm.down configure -state disabled
788 .asm.bottom configure -state disabled
86db943c 789 }
6131622e 790 return
479f0f18
SG
791}
792
793proc gdbtk_tcl_idle {} {
fda6fadc
SS
794 if {[winfo exists .cmd]} {
795 .cmd.text configure -state normal
796 }
4e327047 797 if {[winfo exists .src]} {
6131622e
SG
798 .src.start configure -state normal
799 .src.stop configure -state disabled
800 .src.step configure -state normal
801 .src.next configure -state normal
802 .src.continue configure -state normal
803 .src.finish configure -state normal
804 .src.up configure -state normal
805 .src.down configure -state normal
806 .src.bottom configure -state normal
86db943c 807 }
4e327047 808 if {[winfo exists .asm]} {
6131622e
SG
809 .asm.stepi configure -state normal
810 .asm.nexti configure -state normal
811 .asm.continue configure -state normal
812 .asm.finish configure -state normal
813 .asm.up configure -state normal
814 .asm.down configure -state normal
815 .asm.bottom configure -state normal
86db943c 816 }
6131622e 817 return
479f0f18
SG
818}
819
637b1661
SG
820#
821# Local procedure:
822#
823# pc_to_line (pclist pc) - convert PC to a line number.
824#
825# Description:
826#
827# Convert PC to a line number from PCLIST. If exact line isn't found,
828# we return the first line that starts before PC.
829#
830proc pc_to_line {pclist pc} {
831 set line [lsearch -exact $pclist $pc]
832
833 if {$line >= 1} { return $line }
834
835 set line 1
836 foreach linepc [lrange $pclist 1 end] {
837 if {$pc < $linepc} { decr line ; return $line }
838 incr line
839 }
4e327047 840 return [expr {$line - 1}]
637b1661
SG
841}
842
8532893d
SG
843#
844# Menu:
845#
846# file popup menu - Define the file popup menu.
847#
848# Description:
849#
850# This menu just contains a bunch of buttons that do various things to
851# the line under the cursor.
852#
853# Items:
854#
855# Edit - Run the editor (specified by the environment variable EDITOR) on
856# this file, at the current line.
857# Breakpoint - Set a breakpoint at the current line. This just shoves
858# a `break' command at GDB with the appropriate file and line
859# number. Eventually, GDB calls us back (at gdbtk_tcl_breakpoint)
860# to notify us of where the breakpoint needs to show up.
861#
862
4e327047 863menu .file_popup -cursor hand2 -tearoff 0
8532893d
SG
864.file_popup add command -label "Not yet set" -state disabled
865.file_popup add separator
4e327047
TT
866.file_popup add command -label "Edit" \
867 -command {exec $editor +$selected_line $selected_file &}
868.file_popup add command -label "Set breakpoint" \
869 -command {gdb_cmd "break $selected_file:$selected_line"}
8532893d 870
6131622e
SG
871# Use this procedure to get the GDB core to execute the string `cmd'. This is
872# a wrapper around gdb_cmd, which will catch errors, and send output to the
873# command window. It will also cause all of the other windows to be updated.
874
875proc interactive_cmd {cmd} {
876 catch {gdb_cmd "$cmd"} result
877 .cmd.text insert end $result
4e327047 878 .cmd.text see end
6131622e
SG
879 update_ptr
880}
881
8532893d
SG
882#
883# Bindings:
884#
885# file popup menu - Define the file popup menu bindings.
886#
887# Description:
888#
4e327047
TT
889# This defines the binding for the file popup menu. It simply
890# unhighlights the line under the cursor.
8532893d
SG
891#
892
893bind .file_popup <Any-ButtonRelease-1> {
4e327047
TT
894 global selected_win
895 # Unhighlight the selected line
896 $selected_win tag delete breaktag
754e5da2
SG
897}
898
8532893d
SG
899#
900# Local procedure:
901#
9e9cf822 902# listing_window_popup (win x y xrel yrel) - Handle popups for listing window
8532893d
SG
903#
904# Description:
905#
9e9cf822
SS
906# This procedure is invoked by holding down button 2 (usually) in the
907# listing window. The action taken depends upon where the button was
908# pressed. If it was in the left margin (the breakpoint column), it
909# sets or clears a breakpoint. In the main text area, it will pop up a
910# menu.
8532893d
SG
911#
912
9e9cf822 913proc listing_window_popup {win x y xrel yrel} {
754e5da2
SG
914 global wins
915 global win_to_file
916 global file_to_debug_file
917 global highlight
918 global selected_line
919 global selected_file
920 global selected_win
9e9cf822 921 global pos_to_breakpoint
754e5da2 922
754e5da2
SG
923# Map TK window name back to file name.
924
925 set file $win_to_file($win)
926
9e9cf822 927 set pos [split [$win index @$xrel,$yrel] .]
754e5da2
SG
928
929# Record selected file and line for menu button actions
930
931 set selected_file $file_to_debug_file($file)
9e9cf822
SS
932 set selected_line [lindex $pos 0]
933 set selected_col [lindex $pos 1]
754e5da2
SG
934 set selected_win $win
935
754e5da2
SG
936# Post the menu near the pointer, (and grab it)
937
8532893d 938 .file_popup entryconfigure 0 -label "$selected_file:$selected_line"
9e9cf822
SS
939
940 tk_popup .file_popup $x $y
754e5da2
SG
941}
942
8532893d
SG
943#
944# Local procedure:
945#
9e9cf822 946# toggle_breakpoint (win x y xrel yrel) - Handle clicks on breakdots
8532893d
SG
947#
948# Description:
949#
9e9cf822 950# This procedure sets or clears breakpoints where the button clicked.
8532893d
SG
951#
952
9e9cf822 953proc toggle_breakpoint {win x y xrel yrel} {
8532893d
SG
954 global wins
955 global win_to_file
956 global file_to_debug_file
957 global highlight
958 global selected_line
959 global selected_file
960 global selected_win
961 global pos_to_breakpoint
962
963# Map TK window name back to file name.
964
965 set file $win_to_file($win)
966
967 set pos [split [$win index @$xrel,$yrel] .]
968
9e9cf822 969# Record selected file and line
8532893d
SG
970
971 set selected_file $file_to_debug_file($file)
972 set selected_line [lindex $pos 0]
973 set selected_col [lindex $pos 1]
974 set selected_win $win
975
976# If we're in the margin, then toggle the breakpoint
977
9e9cf822
SS
978 if {$selected_col < 8} { # this is alway true actually
979 set pos_break $selected_file:$selected_line
980 set pos $file:$selected_line
981 set tmp pos_to_breakpoint($pos)
982 if {[info exists $tmp]} {
983 set bpnum [set $tmp]
984 gdb_cmd "delete $bpnum"
985 } else {
986 gdb_cmd "break $pos_break"
987 }
988 return
8532893d 989 }
8532893d
SG
990}
991
992#
993# Local procedure:
994#
995# asm_window_button_1 (win x y xrel yrel) - Handle button 1 in asm window
996#
997# Description:
998#
999# This procedure is invoked as a result of holding down button 1 in the
1000# assembly window. The action taken depends upon where the button was
1001# pressed. If it was in the left margin (the breakpoint column), it
1002# sets or clears a breakpoint. In the main text area, it will pop up a
1003# menu.
1004#
1005
1006proc asm_window_button_1 {win x y xrel yrel} {
1007 global wins
1008 global win_to_file
1009 global file_to_debug_file
1010 global highlight
1011 global selected_line
1012 global selected_file
1013 global selected_win
1014 global pos_to_breakpoint
1015 global pclist
1016 global cfunc
1017
1018 set pos [split [$win index @$xrel,$yrel] .]
1019
1020# Record selected file and line for menu button actions
1021
1022 set selected_line [lindex $pos 0]
1023 set selected_col [lindex $pos 1]
1024 set selected_win $win
1025
1026# Figure out the PC
1027
1028 set pc [lindex $pclist($cfunc) $selected_line]
1029
1030# If we're in the margin, then toggle the breakpoint
1031
746d1df4 1032 if {$selected_col < 11} {
8532893d 1033 set tmp pos_to_breakpoint($pc)
4e327047 1034 if {[info exists $tmp]} {
8532893d
SG
1035 set bpnum [set $tmp]
1036 gdb_cmd "delete $bpnum"
1037 } else {
1038 gdb_cmd "break *$pc"
1039 }
1040 return
1041 }
1042
1043# Post the menu near the pointer, (and grab it)
1044
1045# .file_popup entryconfigure 0 -label "$selected_file:$selected_line"
1046# .file_popup post [expr $x-[winfo width .file_popup]/2] [expr $y-10]
1047# grab .file_popup
1048}
1049
1050#
1051# Local procedure:
1052#
e12533e3 1053# do_nothing - Does absolutely nothing.
8532893d
SG
1054#
1055# Description:
1056#
1057# This procedure does nothing. It is used as a placeholder to allow
1058# the disabling of bindings that would normally be inherited from the
1059# parent widget. I can't think of any other way to do this.
1060#
1061
754e5da2
SG
1062proc do_nothing {} {}
1063
479f0f18
SG
1064#
1065# Local procedure:
1066#
e12533e3
SS
1067# not_implemented_yet - warn that a feature is unavailable
1068#
1069# Description:
1070#
1071# This procedure warns that something doesn't actually work yet.
1072#
1073
1074proc not_implemented_yet {message} {
c4a5c37c
SS
1075 tk_dialog .unimpl "gdb : unimpl" \
1076 "$message: not implemented in the interface yet" \
4e327047 1077 warning 0 "OK"
e12533e3
SS
1078}
1079
1080##
1081# Local procedure:
1082#
6131622e 1083# create_expr_window - Create expression display window
479f0f18
SG
1084#
1085# Description:
1086#
1087# Create the expression display window.
1088#
1089
09722039 1090set expr_num 0
4e327047 1091set delete_expr_num 0
09722039 1092
4e327047
TT
1093# Set delete_expr_num, and set -state of Delete button.
1094proc expr_update_button {num} {
1095 global delete_expr_num
1096 set delete_expr_num $num
1097 if {$num > 0} then {
1098 set state normal
1099 } else {
1100 set state disabled
1101 }
1102 .expr.buts.delete configure -state $state
1103}
09722039 1104
4e327047
TT
1105proc add_expr {expr} {
1106 global expr_update_list
1107 global expr_num
09722039 1108
4e327047 1109 incr expr_num
09722039 1110
4e327047
TT
1111 set e .expr.exprs
1112 set f e$expr_num
09722039 1113
4e327047
TT
1114 checkbutton $e.updates.$f -text "" -relief flat \
1115 -variable expr_update_list($expr_num)
1116 text $e.expressions.$f -width 20 -height 1
1117 $e.expressions.$f insert 0.0 $expr
1118 bind $e.expressions.$f <1> "update_expr $expr_num"
1119 text $e.values.$f -width 20 -height 1
09722039 1120
4e327047
TT
1121 # Set up some bindings.
1122 foreach frame {updates expressions values} {
1123 bind $e.$frame.$f <FocusIn> "expr_update_button $expr_num"
1124 bind $e.$frame.$f <FocusOut> "expr_update_button 0"
1125 }
09722039 1126
4e327047 1127 update_expr $expr_num
09722039 1128
4e327047
TT
1129 pack $e.updates.$f -side top
1130 pack $e.expressions.$f -side top -expand yes -fill x
1131 pack $e.values.$f -side top -expand yes -fill x
09722039
SG
1132}
1133
09722039 1134proc delete_expr {} {
4e327047
TT
1135 global delete_expr_num
1136 if {$delete_expr_num > 0} then {
1137 set e .expr.exprs
1138 set f e${delete_expr_num}
09722039 1139
4e327047
TT
1140 destroy $e.updates.$f $e.expressions.$f $e.values.$f
1141
1142 # FIXME should we unset an element of expr_update_list here?
1143 }
09722039
SG
1144}
1145
1146proc update_expr {expr_num} {
4e327047 1147 global expr_update_list
09722039 1148
4e327047
TT
1149 set e .expr.exprs
1150 set f e${expr_num}
09722039 1151
4e327047
TT
1152 set expr [$e.expressions.$f get 0.0 end]
1153 $e.values.$f delete 0.0 end
1154 if {! [catch {gdb_eval $expr} val]} {
1155 $e.values.$f insert 0.0 $val
1156 } {
1157 # FIXME consider flashing widget here.
1158 }
280c564c
SG
1159}
1160
1161proc update_exprs {} {
1162 global expr_update_list
09722039 1163
280c564c 1164 foreach expr_num [array names expr_update_list] {
4e327047 1165 if {$expr_update_list($expr_num)} {
280c564c
SG
1166 update_expr $expr_num
1167 }
1168 }
09722039
SG
1169}
1170
6131622e 1171proc create_expr_window {} {
280c564c 1172
4e327047 1173 if {[winfo exists .expr]} {raise .expr ; return}
280c564c 1174
479f0f18 1175 toplevel .expr
4e327047
TT
1176 wm title .expr "GDB Expressions"
1177 wm iconname .expr "Expressions"
09722039 1178
4e327047
TT
1179 frame .expr.entryframe -borderwidth 2 -relief raised
1180 label .expr.entryframe.entrylab -text "Expression: "
1181 entry .expr.entryframe.entry -borderwidth 2 -relief sunken
1182 bind .expr.entryframe.entry <Return> {
1183 add_expr [.expr.entryframe.entry get]
1184 .expr.entryframe.entry delete 0 end
1185 }
09722039 1186
4e327047
TT
1187 pack .expr.entryframe.entrylab -side left
1188 pack .expr.entryframe.entry -side left -fill x -expand yes
09722039 1189
4e327047 1190 frame .expr.buts -borderwidth 2 -relief raised
09722039 1191
4e327047
TT
1192 button .expr.buts.delete -text Delete -command delete_expr \
1193 -state disabled
09722039 1194
4e327047
TT
1195 button .expr.buts.close -text Close -command {destroy .expr}
1196 button .expr.buts.help -text Help -state disabled
09722039 1197
4e327047
TT
1198 pack .expr.buts.delete -side left
1199 pack .expr.buts.help .expr.buts.close -side right
09722039
SG
1200
1201 pack .expr.buts -side bottom -fill x
1202 pack .expr.entryframe -side bottom -fill x
1203
4e327047
TT
1204 frame .expr.exprs -borderwidth 2 -relief raised
1205
1206 # Use three subframes so columns will line up. Easier than
1207 # dealing with BLT for a table geometry manager. Someday Tk
1208 # will have one, use it then. FIXME this messes up keyboard
1209 # traversal.
1210 frame .expr.exprs.updates -borderwidth 0 -relief flat
1211 frame .expr.exprs.expressions -borderwidth 0 -relief flat
1212 frame .expr.exprs.values -borderwidth 0 -relief flat
09722039 1213
4e327047
TT
1214 label .expr.exprs.updates.label -text Update
1215 pack .expr.exprs.updates.label -side top -anchor w
1216 label .expr.exprs.expressions.label -text Expression
1217 pack .expr.exprs.expressions.label -side top -anchor w
1218 label .expr.exprs.values.label -text Value
1219 pack .expr.exprs.values.label -side top -anchor w
09722039 1220
4e327047
TT
1221 pack .expr.exprs.updates -side left
1222 pack .expr.exprs.values .expr.exprs.expressions \
1223 -side right -expand 1 -fill x
09722039 1224
4e327047 1225 pack .expr.exprs -side top -fill both -expand 1 -anchor w
479f0f18
SG
1226}
1227
1228#
1229# Local procedure:
1230#
1231# display_expression (expression) - Display EXPRESSION in display window
1232#
1233# Description:
1234#
e12533e3 1235# Display EXPRESSION and its value in the expression display window.
479f0f18
SG
1236#
1237
1238proc display_expression {expression} {
6131622e 1239 create_expr_window
479f0f18 1240
09722039 1241 add_expr $expression
479f0f18
SG
1242}
1243
8532893d
SG
1244#
1245# Local procedure:
1246#
1247# create_file_win (filename) - Create a win for FILENAME.
1248#
1249# Return value:
1250#
1251# The new text widget.
1252#
1253# Description:
1254#
1255# This procedure creates a text widget for FILENAME. It returns the
1256# newly created widget. First, a text widget is created, and given basic
1257# configuration info. Second, all the bindings are setup. Third, the
1258# file FILENAME is read into the text widget. Fourth, margins and line
1259# numbers are added.
1260#
1261
746d1df4 1262proc create_file_win {filename debug_file} {
754e5da2
SG
1263 global breakpoint_file
1264 global breakpoint_line
86db943c 1265 global line_numbers
9e9cf822 1266 global debug_interface
754e5da2 1267
8532893d
SG
1268# Replace all the dirty characters in $filename with clean ones, and generate
1269# a unique name for the text widget.
1270
746d1df4 1271 regsub -all {\.} $filename {} temp
006e71e9 1272 set win .src.text$temp
8532893d 1273
637b1661
SG
1274# Open the file, and read it into the text widget
1275
4e327047 1276 if {[catch "open $filename" fh]} {
746d1df4
SG
1277# File can't be read. Put error message into .src.nofile window and return.
1278
1279 catch {destroy .src.nofile}
6131622e 1280 text .src.nofile -height 25 -width 88 -relief sunken \
4e327047 1281 -borderwidth 2 -yscrollcommand ".src.scroll set" \
746d1df4
SG
1282 -setgrid true -cursor hand2
1283 .src.nofile insert 0.0 $fh
1284 .src.nofile configure -state disabled
1285 bind .src.nofile <1> do_nothing
1286 bind .src.nofile <B1-Motion> do_nothing
1287 return .src.nofile
637b1661
SG
1288 }
1289
8532893d
SG
1290# Actually create and do basic configuration on the text widget.
1291
6131622e 1292 text $win -height 25 -width 88 -relief sunken -borderwidth 2 \
4e327047 1293 -yscrollcommand ".src.scroll set" -setgrid true -cursor hand2
8532893d
SG
1294
1295# Setup all the bindings
1296
754e5da2 1297 bind $win <Enter> {focus %W}
479f0f18 1298 bind $win <1> do_nothing
754e5da2 1299 bind $win <B1-Motion> do_nothing
479f0f18 1300
f1b64caa
SG
1301 bind $win <Key-Alt_R> do_nothing
1302 bind $win <Key-Alt_L> do_nothing
1303 bind $win <Key-Prior> "$win yview {@0,0 - 10 lines}"
1304 bind $win <Key-Next> "$win yview {@0,0 + 10 lines}"
1305 bind $win <Key-Up> "$win yview {@0,0 - 1 lines}"
1306 bind $win <Key-Down> "$win yview {@0,0 + 1 lines}"
1307 bind $win <Key-Home> {update_listing [gdb_loc]}
4e327047 1308 bind $win <Key-End> "$win see end"
f1b64caa 1309
6131622e
SG
1310 bind $win n {interactive_cmd next}
1311 bind $win s {interactive_cmd step}
1312 bind $win c {interactive_cmd continue}
1313 bind $win f {interactive_cmd finish}
1314 bind $win u {interactive_cmd up}
1315 bind $win d {interactive_cmd down}
8532893d 1316
9e9cf822
SS
1317 if $debug_interface {
1318 bind $win <Control-C> {
1319 puts stdout burp
1320 }
1321 }
1322
754e5da2
SG
1323 $win delete 0.0 end
1324 $win insert 0.0 [read $fh]
1325 close $fh
8532893d 1326
86db943c 1327# Add margins (for annotations) and a line number to each line (if requested)
8532893d 1328
754e5da2
SG
1329 set numlines [$win index end]
1330 set numlines [lindex [split $numlines .] 0]
4e327047 1331 if {$line_numbers} {
86db943c
SG
1332 for {set i 1} {$i <= $numlines} {incr i} {
1333 $win insert $i.0 [format " %4d " $i]
1334 $win tag add source $i.8 "$i.0 lineend"
1335 }
1336 } else {
1337 for {set i 1} {$i <= $numlines} {incr i} {
1338 $win insert $i.0 " "
1339 $win tag add source $i.8 "$i.0 lineend"
1340 }
1341 }
479f0f18 1342
746d1df4
SG
1343# Add the breakdots
1344
1345 foreach i [gdb_sourcelines $debug_file] {
1346 $win delete $i.0
1347 $win insert $i.0 "\xa4"
1348 $win tag add margin $i.0 $i.8
1349 }
1350
9e9cf822
SS
1351 # A debugging trick to highlight sensitive regions.
1352 if $debug_interface {
1353 $win tag bind source <Enter> {
1354 %W tag configure source -background yellow
1355 }
1356 $win tag bind source <Leave> {
1357 %W tag configure source -background green
1358 }
1359 $win tag bind margin <Enter> {
1360 %W tag configure margin -background red
1361 }
1362 $win tag bind margin <Leave> {
1363 %W tag configure margin -background skyblue
1364 }
1365 }
1366
1367 $win tag bind margin <1> {
1368 toggle_breakpoint %W %X %Y %x %y
1369 }
1370
479f0f18
SG
1371 $win tag bind source <1> {
1372 %W mark set anchor "@%x,%y wordstart"
1373 set last [%W index "@%x,%y wordend"]
1374 %W tag remove sel 0.0 anchor
1375 %W tag remove sel $last end
1376 %W tag add sel anchor $last
1377 }
1378# $win tag bind source <Double-Button-1> {
1379# %W mark set anchor "@%x,%y wordstart"
1380# set last [%W index "@%x,%y wordend"]
1381# %W tag remove sel 0.0 anchor
1382# %W tag remove sel $last end
1383# %W tag add sel anchor $last
1384# echo "Selected [selection get]"
1385# }
1386 $win tag bind source <B1-Motion> {
1387 %W tag remove sel 0.0 anchor
1388 %W tag remove sel $last end
1389 %W tag add sel anchor @%x,%y
754e5da2 1390 }
f0b0d915
TT
1391 $win tag bind sel <1> break
1392 $win tag bind sel <Double-Button-1> {
9e9cf822
SS
1393 display_expression [selection get]
1394 break
f0b0d915
TT
1395 }
1396 $win tag bind sel <B1-Motion> break
1397 $win tag lower sel
479f0f18 1398
9e9cf822
SS
1399 $win tag bind source <2> {
1400 listing_window_popup %W %X %Y %x %y
1401 }
1402
f0b0d915
TT
1403 # Make these bindings do nothing on the text window -- they
1404 # are completely handled by the tag bindings above.
1405 bind $win <1> break
1406 bind $win <B1-Motion> break
1407 bind $win <Double-Button-1> break
754e5da2 1408
8532893d
SG
1409# Scan though the breakpoint data base and install any destined for this file
1410
754e5da2
SG
1411 foreach bpnum [array names breakpoint_file] {
1412 if {$breakpoint_file($bpnum) == $filename} {
1413 insert_breakpoint_tag $win $breakpoint_line($bpnum)
1414 }
1415 }
1416
8532893d
SG
1417# Disable the text widget to prevent user modifications
1418
754e5da2
SG
1419 $win configure -state disabled
1420 return $win
1421}
1422
8532893d
SG
1423#
1424# Local procedure:
1425#
637b1661 1426# create_asm_win (funcname pc) - Create an assembly win for FUNCNAME.
8532893d
SG
1427#
1428# Return value:
1429#
1430# The new text widget.
1431#
1432# Description:
1433#
1434# This procedure creates a text widget for FUNCNAME. It returns the
1435# newly created widget. First, a text widget is created, and given basic
1436# configuration info. Second, all the bindings are setup. Third, the
1437# function FUNCNAME is read into the text widget.
1438#
1439
637b1661 1440proc create_asm_win {funcname pc} {
8532893d
SG
1441 global breakpoint_file
1442 global breakpoint_line
8532893d 1443 global pclist
280c564c 1444 global disassemble_with_source
8532893d
SG
1445
1446# Replace all the dirty characters in $filename with clean ones, and generate
1447# a unique name for the text widget.
1448
335129a9 1449 set win [asm_win_name $funcname]
8532893d
SG
1450
1451# Actually create and do basic configuration on the text widget.
1452
6131622e 1453 text $win -height 25 -width 80 -relief sunken -borderwidth 2 \
4e327047 1454 -setgrid true -cursor hand2 -yscrollcommand ".asm.scroll set"
8532893d
SG
1455
1456# Setup all the bindings
1457
1458 bind $win <Enter> {focus %W}
f0b0d915
TT
1459 bind $win <1> {asm_window_button_1 %W %X %Y %x %y; break}
1460 bind $win <B1-Motion> break
1461 bind $win <Double-Button-1> break
f1b64caa
SG
1462
1463 bind $win <Key-Alt_R> do_nothing
1464 bind $win <Key-Alt_L> do_nothing
f1b64caa 1465
6131622e
SG
1466 bind $win n {interactive_cmd nexti}
1467 bind $win s {interactive_cmd stepi}
1468 bind $win c {interactive_cmd continue}
1469 bind $win f {interactive_cmd finish}
1470 bind $win u {interactive_cmd up}
1471 bind $win d {interactive_cmd down}
8532893d
SG
1472
1473# Disassemble the code, and read it into the new text widget
1474
6131622e 1475 $win insert end [gdb_disassemble $disassemble_with_source $pc]
8532893d
SG
1476
1477 set numlines [$win index end]
1478 set numlines [lindex [split $numlines .] 0]
637b1661 1479 decr numlines
8532893d
SG
1480
1481# Delete the first and last lines, cuz these contain useless info
1482
09722039
SG
1483# $win delete 1.0 2.0
1484# $win delete {end - 1 lines} end
1485# decr numlines 2
8532893d
SG
1486
1487# Add margins (for annotations) and note the PC for each line
1488
637b1661 1489 catch "unset pclist($funcname)"
335129a9 1490 lappend pclist($funcname) Unused
8532893d
SG
1491 for {set i 1} {$i <= $numlines} {incr i} {
1492 scan [$win get $i.0 "$i.0 lineend"] "%s " pc
1493 lappend pclist($funcname) $pc
1494 $win insert $i.0 " "
1495 }
1496
8532893d
SG
1497# Scan though the breakpoint data base and install any destined for this file
1498
1499# foreach bpnum [array names breakpoint_file] {
1500# if {$breakpoint_file($bpnum) == $filename} {
1501# insert_breakpoint_tag $win $breakpoint_line($bpnum)
1502# }
1503# }
1504
1505# Disable the text widget to prevent user modifications
1506
1507 $win configure -state disabled
1508 return $win
1509}
1510
8532893d
SG
1511#
1512# Local procedure:
1513#
1514# update_listing (linespec) - Update the listing window according to
1515# LINESPEC.
1516#
1517# Description:
1518#
1519# This procedure is called from various places to update the listing
1520# window based on LINESPEC. It is usually invoked with the result of
1521# gdb_loc.
1522#
1523# It will move the cursor, and scroll the text widget if necessary.
1524# Also, it will switch to another text widget if necessary, and update
1525# the label widget too.
1526#
1527# LINESPEC is a list of the form:
1528#
1529# { DEBUG_FILE FUNCNAME FILENAME LINE }, where:
1530#
1531# DEBUG_FILE - is the abbreviated form of the file name. This is usually
1532# the file name string given to the cc command. This is
1533# primarily needed for breakpoint commands, and when an
1534# abbreviated for of the filename is desired.
1535# FUNCNAME - is the name of the function.
1536# FILENAME - is the fully qualified (absolute) file name. It is usually
1537# the same as $PWD/$DEBUG_FILE, where PWD is the working dir
1538# at the time the cc command was given. This is used to
1539# actually locate the file to be displayed.
1540# LINE - The line number to be displayed.
1541#
1542# Usually, this procedure will just move the cursor one line down to the
1543# next line to be executed. However, if the cursor moves out of range
1544# or into another file, it will scroll the text widget so that the line
1545# of interest is in the middle of the viewable portion of the widget.
1546#
1547
754e5da2
SG
1548proc update_listing {linespec} {
1549 global pointers
754e5da2
SG
1550 global wins cfile
1551 global current_label
1552 global win_to_file
1553 global file_to_debug_file
746d1df4 1554 global .src.label
754e5da2 1555
8532893d
SG
1556# Rip the linespec apart
1557
4e327047 1558 lassign $linespec debug_file funcname filename line
754e5da2 1559
8532893d
SG
1560# Sometimes there's no source file for this location
1561
754e5da2
SG
1562 if {$filename == ""} {set filename Blank}
1563
8532893d
SG
1564# If we want to switch files, we need to unpack the current text widget, and
1565# stick in the new one.
1566
754e5da2
SG
1567 if {$filename != $cfile} then {
1568 pack forget $wins($cfile)
1569 set cfile $filename
8532893d
SG
1570
1571# Create a text widget for this file if necessary
1572
4e327047 1573 if {![info exists wins($cfile)]} then {
746d1df4
SG
1574 set wins($cfile) [create_file_win $cfile $debug_file]
1575 if {$wins($cfile) != ".src.nofile"} {
637b1661
SG
1576 set win_to_file($wins($cfile)) $cfile
1577 set file_to_debug_file($cfile) $debug_file
1578 set pointers($cfile) 1.1
1579 }
754e5da2
SG
1580 }
1581
8532893d
SG
1582# Pack the text widget into the listing widget, and scroll to the right place
1583
746d1df4
SG
1584 pack $wins($cfile) -side left -expand yes -in .src.info \
1585 -fill both -after .src.scroll
1586
1587# Make the scrollbar point at the new text widget
1588
1589 .src.scroll configure -command "$wins($cfile) yview"
1590
f0b0d915
TT
1591 # $wins($cfile) see "${line}.0 linestart"
1592 ensure_line_visible $wins($cfile) $line
754e5da2
SG
1593 }
1594
8532893d
SG
1595# Update the label widget in case the filename or function name has changed
1596
754e5da2
SG
1597 if {$current_label != "$filename.$funcname"} then {
1598 set tail [expr [string last / $filename] + 1]
746d1df4
SG
1599 set .src.label "[string range $filename $tail end] : ${funcname}()"
1600# .src.label configure -text "[string range $filename $tail end] : ${funcname}()"
754e5da2
SG
1601 set current_label $filename.$funcname
1602 }
1603
8532893d
SG
1604# Update the pointer, scrolling the text widget if necessary to keep the
1605# pointer in an acceptable part of the screen.
1606
4e327047 1607 if {[info exists pointers($cfile)]} then {
754e5da2
SG
1608 $wins($cfile) configure -state normal
1609 set pointer_pos $pointers($cfile)
1610 $wins($cfile) configure -state normal
746d1df4
SG
1611 $wins($cfile) delete $pointer_pos "$pointer_pos + 2 char"
1612 $wins($cfile) insert $pointer_pos " "
754e5da2
SG
1613
1614 set pointer_pos [$wins($cfile) index $line.1]
1615 set pointers($cfile) $pointer_pos
1616
746d1df4
SG
1617 $wins($cfile) delete $pointer_pos "$pointer_pos + 2 char"
1618 $wins($cfile) insert $pointer_pos "->"
f0b0d915 1619 ensure_line_visible $wins($cfile) $line
754e5da2
SG
1620 $wins($cfile) configure -state disabled
1621 }
1622}
1623
8532893d
SG
1624#
1625# Local procedure:
1626#
746d1df4 1627# create_asm_window - Open up the assembly window.
8532893d
SG
1628#
1629# Description:
1630#
1631# Create an assembly window if it doesn't exist.
1632#
1633
746d1df4 1634proc create_asm_window {} {
8532893d
SG
1635 global cfunc
1636
4e327047 1637 if {[winfo exists .asm]} {raise .asm ; return}
280c564c
SG
1638
1639 set cfunc *None*
1640 set win [asm_win_name $cfunc]
335129a9 1641
280c564c 1642 build_framework .asm Assembly "*NIL*"
006e71e9 1643
09722039
SG
1644# First, delete all the old menu entries
1645
280c564c 1646 .asm.menubar.view.menu delete 0 last
09722039 1647
4e327047 1648 .asm.text configure -yscrollcommand ".asm.scroll set"
8532893d 1649
280c564c
SG
1650 frame .asm.row1
1651 frame .asm.row2
8532893d 1652
280c564c 1653 button .asm.stepi -width 6 -text Stepi \
6131622e 1654 -command {interactive_cmd stepi}
280c564c 1655 button .asm.nexti -width 6 -text Nexti \
6131622e 1656 -command {interactive_cmd nexti}
280c564c 1657 button .asm.continue -width 6 -text Cont \
6131622e 1658 -command {interactive_cmd continue}
280c564c 1659 button .asm.finish -width 6 -text Finish \
6131622e
SG
1660 -command {interactive_cmd finish}
1661 button .asm.up -width 6 -text Up -command {interactive_cmd up}
280c564c 1662 button .asm.down -width 6 -text Down \
6131622e 1663 -command {interactive_cmd down}
280c564c 1664 button .asm.bottom -width 6 -text Bottom \
6131622e 1665 -command {interactive_cmd {frame 0}}
8532893d 1666
280c564c
SG
1667 pack .asm.stepi .asm.continue .asm.up .asm.bottom -side left -padx 3 -pady 5 -in .asm.row1
1668 pack .asm.nexti .asm.finish .asm.down -side left -padx 3 -pady 5 -in .asm.row2
006e71e9 1669
280c564c 1670 pack .asm.row2 .asm.row1 -side bottom -anchor w -before .asm.info
8532893d 1671
280c564c 1672 update
006e71e9 1673
280c564c 1674 update_assembly [gdb_loc]
09722039
SG
1675
1676# We do this update_assembly to get the proper value of disassemble-from-exec.
1677
1678# exec file menu item
280c564c
SG
1679 .asm.menubar.view.menu add radiobutton -label "Exec file" \
1680 -variable disassemble-from-exec -value 1
09722039 1681# target memory menu item
280c564c
SG
1682 .asm.menubar.view.menu add radiobutton -label "Target memory" \
1683 -variable disassemble-from-exec -value 0
1684
1685# Disassemble with source
1686 .asm.menubar.view.menu add checkbutton -label "Source" \
1687 -variable disassemble_with_source -onvalue source \
1688 -offvalue nosource -command {
1689 foreach asm [info command .asm.func_*] {
1690 destroy $asm
1691 }
1692 set cfunc NIL
1693 update_assembly [gdb_loc]
1694 }
8532893d
SG
1695}
1696
746d1df4 1697proc reg_config_menu {} {
746d1df4
SG
1698 catch {destroy .reg.config}
1699 toplevel .reg.config
1700 wm geometry .reg.config +300+300
1701 wm title .reg.config "Register configuration"
1702 wm iconname .reg.config "Reg config"
1703 set regnames [gdb_regnames]
1704 set num_regs [llength $regnames]
1705
86db943c
SG
1706 frame .reg.config.buts
1707
1708 button .reg.config.done -text " Done " -command "
1709 recompute_reg_display_list $num_regs
1710 populate_reg_window
1711 update_registers all
1712 destroy .reg.config "
1713
1714 button .reg.config.update -text Update -command "
1715 recompute_reg_display_list $num_regs
1716 populate_reg_window
1717 update_registers all "
1718
1719 pack .reg.config.buts -side bottom -fill x
746d1df4 1720
86db943c
SG
1721 pack .reg.config.done -side left -fill x -expand yes -in .reg.config.buts
1722 pack .reg.config.update -side right -fill x -expand yes -in .reg.config.buts
746d1df4
SG
1723
1724# Since there can be lots of registers, we build the window with no more than
1725# 32 rows, and as many columns as needed.
1726
1727# First, figure out how many columns we need and create that many column frame
1728# widgets
1729
1730 set ncols [expr ($num_regs + 31) / 32]
1731
1732 for {set col 0} {$col < $ncols} {incr col} {
1733 frame .reg.config.col$col
1734 pack .reg.config.col$col -side left -anchor n
1735 }
1736
1737# Now, create the checkbutton widgets and pack them in the appropriate columns
1738
1739 set col 0
1740 set row 0
1741 for {set regnum 0} {$regnum < $num_regs} {incr regnum} {
1742 set regname [lindex $regnames $regnum]
1743 checkbutton .reg.config.col$col.$row -text $regname -pady 0 \
86db943c 1744 -variable regena($regnum) -relief flat -anchor w -bd 1
746d1df4
SG
1745
1746 pack .reg.config.col$col.$row -side top -fill both
1747
1748 incr row
1749 if {$row >= 32} {
1750 incr col
1751 set row 0
1752 }
1753 }
1754}
1755
335129a9
SG
1756#
1757# Local procedure:
1758#
746d1df4 1759# create_registers_window - Open up the register display window.
335129a9
SG
1760#
1761# Description:
1762#
1763# Create the register display window, with automatic updates.
1764#
1765
746d1df4
SG
1766proc create_registers_window {} {
1767 global reg_format
1768
4e327047 1769 if {[winfo exists .reg]} {raise .reg ; return}
746d1df4
SG
1770
1771# Create an initial register display list consisting of all registers
1772
4e327047 1773 if {![info exists reg_format]} {
746d1df4
SG
1774 global reg_display_list
1775 global changed_reg_list
cb3313c1 1776 global regena
746d1df4
SG
1777
1778 set reg_format {}
1779 set num_regs [llength [gdb_regnames]]
1780 for {set regnum 0} {$regnum < $num_regs} {incr regnum} {
cb3313c1 1781 set regena($regnum) 1
746d1df4
SG
1782 }
1783 recompute_reg_display_list $num_regs
1784 set changed_reg_list $reg_display_list
1785 }
1786
1787 build_framework .reg Registers
1788
86db943c
SG
1789# First, delete all the old menu entries
1790
1791 .reg.menubar.view.menu delete 0 last
746d1df4
SG
1792
1793# Hex menu item
8e5bc49f
SG
1794 .reg.menubar.view.menu add radiobutton -label Hex \
1795 -command {set reg_format x ; update_registers all}
746d1df4 1796
746d1df4 1797# Decimal menu item
8e5bc49f
SG
1798 .reg.menubar.view.menu add radiobutton -label Decimal \
1799 -command {set reg_format d ; update_registers all}
746d1df4 1800
746d1df4 1801# Octal menu item
8e5bc49f
SG
1802 .reg.menubar.view.menu add radiobutton -label Octal \
1803 -command {set reg_format o ; update_registers all}
746d1df4 1804
746d1df4 1805# Natural menu item
8e5bc49f
SG
1806 .reg.menubar.view.menu add radiobutton -label Natural \
1807 -command {set reg_format {} ; update_registers all}
746d1df4 1808
86db943c
SG
1809# Config menu item
1810 .reg.menubar.view.menu add separator
1811
1812 .reg.menubar.view.menu add command -label Config -command {
1813 reg_config_menu }
746d1df4
SG
1814
1815 destroy .reg.label
1816
1817# Install the reg names
1818
1819 populate_reg_window
86db943c 1820 update_registers all
746d1df4
SG
1821}
1822
cb3313c1 1823# Convert regena into a list of the enabled $regnums
746d1df4
SG
1824
1825proc recompute_reg_display_list {num_regs} {
1826 global reg_display_list
cb3313c1
SG
1827 global regmap
1828 global regena
746d1df4
SG
1829
1830 catch {unset reg_display_list}
cb3313c1
SG
1831
1832 set line 1
746d1df4 1833 for {set regnum 0} {$regnum < $num_regs} {incr regnum} {
746d1df4 1834
cb3313c1 1835 if {[set regena($regnum)] != 0} {
746d1df4 1836 lappend reg_display_list $regnum
cb3313c1
SG
1837 set regmap($regnum) $line
1838 incr line
746d1df4
SG
1839 }
1840 }
1841}
1842
1843# Fill out the register window with the names of the regs specified in
1844# reg_display_list.
1845
1846proc populate_reg_window {} {
1847 global max_regname_width
1848 global reg_display_list
1849
1850 .reg.text configure -state normal
1851
1852 .reg.text delete 0.0 end
1853
1854 set regnames [eval gdb_regnames $reg_display_list]
1855
1856# Figure out the longest register name
335129a9 1857
746d1df4 1858 set max_regname_width 0
335129a9 1859
746d1df4
SG
1860 foreach reg $regnames {
1861 set len [string length $reg]
1862 if {$len > $max_regname_width} {set max_regname_width $len}
1863 }
1864
1865 set width [expr $max_regname_width + 15]
1866
1867 set height [llength $regnames]
1868
1869 if {$height > 60} {set height 60}
335129a9 1870
746d1df4
SG
1871 .reg.text configure -height $height -width $width
1872
1873 foreach reg $regnames {
1874 .reg.text insert end [format "%-*s \n" $max_regname_width ${reg}]
335129a9 1875 }
746d1df4
SG
1876
1877 .reg.text yview 0
1878 .reg.text configure -state disabled
335129a9
SG
1879}
1880
1881#
1882# Local procedure:
1883#
1884# update_registers - Update the registers window.
1885#
1886# Description:
1887#
1888# This procedure updates the registers window.
1889#
1890
746d1df4
SG
1891proc update_registers {which} {
1892 global max_regname_width
1893 global reg_format
1894 global reg_display_list
1895 global changed_reg_list
1896 global highlight
cb3313c1 1897 global regmap
335129a9 1898
746d1df4 1899 set margin [expr $max_regname_width + 1]
006e71e9 1900 set win .reg.text
746d1df4
SG
1901 set winwidth [lindex [$win configure -width] 4]
1902 set valwidth [expr $winwidth - $margin]
335129a9
SG
1903
1904 $win configure -state normal
1905
746d1df4 1906 if {$which == "all"} {
cb3313c1 1907 set lineindex 1
746d1df4
SG
1908 foreach regnum $reg_display_list {
1909 set regval [gdb_fetch_registers $reg_format $regnum]
1910 set regval [format "%-*s" $valwidth $regval]
cb3313c1
SG
1911 $win delete $lineindex.$margin "$lineindex.0 lineend"
1912 $win insert $lineindex.$margin $regval
1913 incr lineindex
746d1df4
SG
1914 }
1915 $win configure -state disabled
1916 return
1917 }
335129a9 1918
746d1df4
SG
1919# Unhighlight the old values
1920
1921 foreach regnum $changed_reg_list {
1922 $win tag delete $win.$regnum
1923 }
1924
1925# Now, highlight the changed values of the interesting registers
1926
1927 set changed_reg_list [eval gdb_changed_register_list $reg_display_list]
1928
cb3313c1 1929 set lineindex 1
746d1df4
SG
1930 foreach regnum $changed_reg_list {
1931 set regval [gdb_fetch_registers $reg_format $regnum]
1932 set regval [format "%-*s" $valwidth $regval]
cb3313c1
SG
1933
1934 set lineindex $regmap($regnum)
746d1df4
SG
1935 $win delete $lineindex.$margin "$lineindex.0 lineend"
1936 $win insert $lineindex.$margin $regval
1937 $win tag add $win.$regnum $lineindex.0 "$lineindex.0 lineend"
1938 eval $win tag configure $win.$regnum $highlight
1939 }
335129a9 1940
335129a9
SG
1941 $win configure -state disabled
1942}
1943
8532893d
SG
1944#
1945# Local procedure:
1946#
1947# update_assembly - Update the assembly window.
1948#
1949# Description:
1950#
1951# This procedure updates the assembly window.
1952#
1953
1954proc update_assembly {linespec} {
1955 global asm_pointers
8532893d
SG
1956 global wins cfunc
1957 global current_label
1958 global win_to_file
1959 global file_to_debug_file
1960 global current_asm_label
1961 global pclist
746d1df4 1962 global .asm.label
8532893d
SG
1963
1964# Rip the linespec apart
1965
4e327047 1966 lassign $linespec debug_file funcname filename line pc
8532893d 1967
335129a9 1968 set win [asm_win_name $cfunc]
8532893d
SG
1969
1970# Sometimes there's no source file for this location
1971
1972 if {$filename == ""} {set filename Blank}
1973
1974# If we want to switch funcs, we need to unpack the current text widget, and
1975# stick in the new one.
1976
637b1661 1977 if {$funcname != $cfunc } {
546b8ca7 1978 set oldwin $win
8532893d
SG
1979 set cfunc $funcname
1980
335129a9 1981 set win [asm_win_name $cfunc]
8532893d
SG
1982
1983# Create a text widget for this func if necessary
1984
637b1661
SG
1985 if {![winfo exists $win]} {
1986 create_asm_win $cfunc $pc
8532893d
SG
1987 set asm_pointers($cfunc) 1.1
1988 set current_asm_label NIL
1989 }
1990
1991# Pack the text widget, and scroll to the right place
1992
546b8ca7 1993 pack forget $oldwin
8532893d 1994 pack $win -side left -expand yes -fill both \
006e71e9 1995 -after .asm.scroll
746d1df4 1996 .asm.scroll configure -command "$win yview"
637b1661 1997 set line [pc_to_line $pclist($cfunc) $pc]
f0b0d915 1998 ensure_line_visible $win $line
0af608b8 1999 update
8532893d
SG
2000 }
2001
2002# Update the label widget in case the filename or function name has changed
2003
335129a9 2004 if {$current_asm_label != "$pc $funcname"} then {
746d1df4 2005 set .asm.label "$pc $funcname"
335129a9 2006 set current_asm_label "$pc $funcname"
8532893d
SG
2007 }
2008
2009# Update the pointer, scrolling the text widget if necessary to keep the
2010# pointer in an acceptable part of the screen.
2011
4e327047 2012 if {[info exists asm_pointers($cfunc)]} then {
8532893d
SG
2013 $win configure -state normal
2014 set pointer_pos $asm_pointers($cfunc)
2015 $win configure -state normal
746d1df4
SG
2016 $win delete $pointer_pos "$pointer_pos + 2 char"
2017 $win insert $pointer_pos " "
8532893d
SG
2018
2019# Map the PC back to a line in the window
2020
637b1661 2021 set line [pc_to_line $pclist($cfunc) $pc]
8532893d
SG
2022
2023 if {$line == -1} {
2024 echo "Can't find PC $pc"
2025 return
2026 }
2027
8532893d
SG
2028 set pointer_pos [$win index $line.1]
2029 set asm_pointers($cfunc) $pointer_pos
2030
746d1df4
SG
2031 $win delete $pointer_pos "$pointer_pos + 2 char"
2032 $win insert $pointer_pos "->"
f0b0d915 2033 ensure_line_visible $win $line
8532893d
SG
2034 $win configure -state disabled
2035 }
2036}
2037
006e71e9
SG
2038#
2039# Local procedure:
2040#
2041# update_ptr - Update the listing window.
2042#
2043# Description:
2044#
2045# This routine will update the listing window using the result of
2046# gdb_loc.
2047#
2048
8532893d
SG
2049proc update_ptr {} {
2050 update_listing [gdb_loc]
4e327047 2051 if {[winfo exists .asm]} {
8532893d
SG
2052 update_assembly [gdb_loc]
2053 }
4e327047 2054 if {[winfo exists .reg]} {
746d1df4 2055 update_registers changed
335129a9 2056 }
4e327047 2057 if {[winfo exists .expr]} {
280c564c
SG
2058 update_exprs
2059 }
4e327047 2060 if {[winfo exists .autocmd]} {
6131622e
SG
2061 update_autocmd
2062 }
8532893d
SG
2063}
2064
006e71e9 2065# Make toplevel window disappear
754e5da2 2066
006e71e9 2067wm withdraw .
754e5da2 2068
754e5da2 2069proc files_command {} {
4e327047
TT
2070 toplevel .files_window
2071
2072 wm minsize .files_window 1 1
2073 # wm overrideredirect .files_window true
76e641bd 2074 listbox .files_window.list -width 30 -height 20 -setgrid true \
4e327047
TT
2075 -yscrollcommand {.files_window.scroll set} -relief sunken \
2076 -borderwidth 2
2077 scrollbar .files_window.scroll -orient vertical \
2078 -command {.files_window.list yview} -relief sunken
2079 button .files_window.close -text Close -command {destroy .files_window}
2080 .files_window.list configure -selectmode single
2081
f4e769dc
TT
2082 # Get the file list from GDB, sort it, and insert into the widget.
2083 eval .files_window.list insert 0 [lsort [gdb_listfiles]]
c81a3fa9 2084
4e327047
TT
2085 pack .files_window.close -side bottom -fill x -expand no -anchor s
2086 pack .files_window.scroll -side right -fill both
2087 pack .files_window.list -side left -fill both -expand yes
2088 bind .files_window.list <Any-ButtonRelease-1> {
2089 set file [%W get [%W curselection]]
2090 gdb_cmd "list $file:1,0"
2091 update_listing [gdb_loc $file:1]
2092 destroy .files_window
2093 }
754e5da2
SG
2094}
2095
2096button .files -text Files -command files_command
2097
4604b34c
SG
2098proc apply_filespec {label default command} {
2099 set filename [FSBox $label $default]
2100 if {$filename != ""} {
4e327047 2101 if {[catch {gdb_cmd "$command $filename"} retval]} {
4604b34c 2102 tk_dialog .filespec_error "gdb : $label error" \
4e327047
TT
2103 "Error in command \"$command $filename\"" error \
2104 0 Dismiss
4604b34c
SG
2105 return
2106 }
2107 update_ptr
2108 }
2109}
2110
4e327047
TT
2111# Run editor.
2112proc run_editor {editor file} {
2113 # FIXME should use index of line in middle of window, not line at
2114 # top.
2115 global wins
2116 set lineNo [lindex [split [$wins($file) index @0,0] .] 0]
2117 exec $editor +$lineNo $file
2118}
754e5da2 2119
4e327047 2120# Setup command window
006e71e9 2121proc build_framework {win {title GDBtk} {label {}}} {
746d1df4 2122 global ${win}.label
006e71e9
SG
2123
2124 toplevel ${win}
04576ab6 2125 wm title ${win} $title
006e71e9
SG
2126 wm minsize ${win} 1 1
2127
2128 frame ${win}.menubar
2129
2130 menubutton ${win}.menubar.file -padx 12 -text File \
2131 -menu ${win}.menubar.file.menu -underline 0
2132
2133 menu ${win}.menubar.file.menu
e12533e3 2134 ${win}.menubar.file.menu add command -label File... \
4604b34c 2135 -command {apply_filespec File a.out file}
e12533e3 2136 ${win}.menubar.file.menu add command -label Target... \
c4a5c37c 2137 -command { not_implemented_yet "target" }
006e71e9 2138 ${win}.menubar.file.menu add command -label Edit \
4e327047 2139 -command {run_editor $editor $cfile}
e12533e3
SS
2140 ${win}.menubar.file.menu add separator
2141 ${win}.menubar.file.menu add command -label "Exec File..." \
4604b34c 2142 -command {apply_filespec {Exec File} a.out exec-file}
e12533e3 2143 ${win}.menubar.file.menu add command -label "Symbol File..." \
4604b34c 2144 -command {apply_filespec {Symbol File} a.out symbol-file}
e12533e3
SS
2145 ${win}.menubar.file.menu add command -label "Add Symbol File..." \
2146 -command { not_implemented_yet "menu item, add symbol file" }
2147 ${win}.menubar.file.menu add command -label "Core File..." \
4604b34c
SG
2148 -command {apply_filespec {Core File} core core-file}
2149
e12533e3 2150 ${win}.menubar.file.menu add separator
006e71e9
SG
2151 ${win}.menubar.file.menu add command -label Close \
2152 -command "destroy ${win}"
e12533e3 2153 ${win}.menubar.file.menu add separator
006e71e9 2154 ${win}.menubar.file.menu add command -label Quit \
6131622e 2155 -command {interactive_cmd quit}
006e71e9 2156
c4a5c37c
SS
2157 menubutton ${win}.menubar.commands -padx 12 -text Commands \
2158 -menu ${win}.menubar.commands.menu -underline 0
2159
2160 menu ${win}.menubar.commands.menu
2161 ${win}.menubar.commands.menu add command -label Run \
6131622e 2162 -command {interactive_cmd run}
c4a5c37c 2163 ${win}.menubar.commands.menu add command -label Step \
6131622e 2164 -command {interactive_cmd step}
c4a5c37c 2165 ${win}.menubar.commands.menu add command -label Next \
6131622e 2166 -command {interactive_cmd next}
c4a5c37c 2167 ${win}.menubar.commands.menu add command -label Continue \
6131622e 2168 -command {interactive_cmd continue}
c4a5c37c
SS
2169 ${win}.menubar.commands.menu add separator
2170 ${win}.menubar.commands.menu add command -label Stepi \
6131622e 2171 -command {interactive_cmd stepi}
c4a5c37c 2172 ${win}.menubar.commands.menu add command -label Nexti \
6131622e 2173 -command {interactive_cmd nexti}
c4a5c37c 2174
09722039 2175 menubutton ${win}.menubar.view -padx 12 -text Options \
006e71e9
SG
2176 -menu ${win}.menubar.view.menu -underline 0
2177
2178 menu ${win}.menubar.view.menu
c4a5c37c
SS
2179 ${win}.menubar.view.menu add command -label Hex \
2180 -command {echo Hex}
006e71e9
SG
2181 ${win}.menubar.view.menu add command -label Decimal \
2182 -command {echo Decimal}
c4a5c37c
SS
2183 ${win}.menubar.view.menu add command -label Octal \
2184 -command {echo Octal}
006e71e9
SG
2185
2186 menubutton ${win}.menubar.window -padx 12 -text Window \
2187 -menu ${win}.menubar.window.menu -underline 0
2188
2189 menu ${win}.menubar.window.menu
006e71e9 2190 ${win}.menubar.window.menu add command -label Command \
280c564c 2191 -command create_command_window
c4a5c37c
SS
2192 ${win}.menubar.window.menu add separator
2193 ${win}.menubar.window.menu add command -label Source \
6131622e 2194 -command create_source_window
006e71e9 2195 ${win}.menubar.window.menu add command -label Assembly \
6131622e 2196 -command create_asm_window
c4a5c37c
SS
2197 ${win}.menubar.window.menu add separator
2198 ${win}.menubar.window.menu add command -label Registers \
6131622e 2199 -command create_registers_window
09722039 2200 ${win}.menubar.window.menu add command -label Expressions \
6131622e
SG
2201 -command create_expr_window
2202 ${win}.menubar.window.menu add command -label "Auto Command" \
2203 -command create_autocmd_window
f1b64caa
SG
2204 ${win}.menubar.window.menu add command -label Breakpoints \
2205 -command create_breakpoints_window
09722039 2206
280c564c
SG
2207# ${win}.menubar.window.menu add separator
2208# ${win}.menubar.window.menu add command -label Files \
2209# -command { not_implemented_yet "files window" }
006e71e9
SG
2210
2211 menubutton ${win}.menubar.help -padx 12 -text Help \
2212 -menu ${win}.menubar.help.menu -underline 0
2213
2214 menu ${win}.menubar.help.menu
2215 ${win}.menubar.help.menu add command -label "with GDBtk" \
2216 -command {echo "with GDBtk"}
2217 ${win}.menubar.help.menu add command -label "with this window" \
2218 -command {echo "with this window"}
c981300c
SG
2219 ${win}.menubar.help.menu add command -label "Report bug" \
2220 -command {exec send-pr}
006e71e9 2221
c4a5c37c 2222 pack ${win}.menubar.file \
c4a5c37c
SS
2223 ${win}.menubar.view \
2224 ${win}.menubar.window -side left
2225 pack ${win}.menubar.help -side right
006e71e9
SG
2226
2227 frame ${win}.info
6131622e 2228 text ${win}.text -height 25 -width 80 -relief sunken -borderwidth 2 \
006e71e9
SG
2229 -setgrid true -cursor hand2 -yscrollcommand "${win}.scroll set"
2230
746d1df4 2231 set ${win}.label $label
6131622e 2232 label ${win}.label -textvariable ${win}.label -borderwidth 2 -relief sunken
754e5da2 2233
6131622e
SG
2234 scrollbar ${win}.scroll -orient vertical -command "${win}.text yview" \
2235 -relief sunken
006e71e9 2236
f1b64caa
SG
2237 bind $win <Key-Alt_R> do_nothing
2238 bind $win <Key-Alt_L> do_nothing
f1b64caa 2239
006e71e9
SG
2240 pack ${win}.label -side bottom -fill x -in ${win}.info
2241 pack ${win}.scroll -side right -fill y -in ${win}.info
2242 pack ${win}.text -side left -expand yes -fill both -in ${win}.info
2243
2244 pack ${win}.menubar -side top -fill x
2245 pack ${win}.info -side top -fill both -expand yes
2246}
2247
746d1df4
SG
2248proc create_source_window {} {
2249 global wins
2250 global cfile
2251
4e327047 2252 if {[winfo exists .src]} {raise .src ; return}
280c564c 2253
746d1df4
SG
2254 build_framework .src Source "*No file*"
2255
86db943c
SG
2256# First, delete all the old view menu entries
2257
2258 .src.menubar.view.menu delete 0 last
2259
546b8ca7
SG
2260# Source file selection
2261 .src.menubar.view.menu add command -label "Select source file" \
2262 -command files_command
2263
86db943c
SG
2264# Line numbers enable/disable menu item
2265 .src.menubar.view.menu add checkbutton -variable line_numbers \
2266 -label "Line numbers" -onvalue 1 -offvalue 0 -command {
2267 foreach source [array names wins] {
2268 if {$source == "Blank"} continue
2269 destroy $wins($source)
2270 unset wins($source)
2271 }
2272 set cfile Blank
2273 update_listing [gdb_loc]
2274 }
2275
746d1df4
SG
2276 frame .src.row1
2277 frame .src.row2
2278
2279 button .src.start -width 6 -text Start -command \
6131622e
SG
2280 {interactive_cmd {break main}
2281 interactive_cmd {enable delete $bpnum}
2282 interactive_cmd run }
746d1df4
SG
2283 button .src.stop -width 6 -text Stop -fg red -activeforeground red \
2284 -state disabled -command gdb_stop
2285 button .src.step -width 6 -text Step \
6131622e 2286 -command {interactive_cmd step}
746d1df4 2287 button .src.next -width 6 -text Next \
6131622e 2288 -command {interactive_cmd next}
746d1df4 2289 button .src.continue -width 6 -text Cont \
6131622e 2290 -command {interactive_cmd continue}
746d1df4 2291 button .src.finish -width 6 -text Finish \
6131622e 2292 -command {interactive_cmd finish}
86db943c 2293 button .src.up -width 6 -text Up \
6131622e 2294 -command {interactive_cmd up}
746d1df4 2295 button .src.down -width 6 -text Down \
6131622e 2296 -command {interactive_cmd down}
746d1df4 2297 button .src.bottom -width 6 -text Bottom \
6131622e 2298 -command {interactive_cmd {frame 0}}
746d1df4
SG
2299
2300 pack .src.start .src.step .src.continue .src.up .src.bottom \
2301 -side left -padx 3 -pady 5 -in .src.row1
2302 pack .src.stop .src.next .src.finish .src.down -side left -padx 3 \
2303 -pady 5 -in .src.row2
2304
86db943c 2305 pack .src.row2 .src.row1 -side bottom -anchor w -before .src.info
746d1df4
SG
2306
2307 $wins($cfile) insert 0.0 " This page intentionally left blank."
2308 $wins($cfile) configure -width 88 -state disabled \
4e327047 2309 -yscrollcommand ".src.scroll set"
746d1df4 2310}
754e5da2 2311
6131622e
SG
2312proc update_autocmd {} {
2313 global .autocmd.label
2314 global accumulate_output
2315
2316 catch {gdb_cmd "${.autocmd.label}"} result
4e327047 2317 if {!$accumulate_output} { .autocmd.text delete 0.0 end }
6131622e 2318 .autocmd.text insert end $result
4e327047 2319 .autocmd.text see end
6131622e
SG
2320}
2321
2322proc create_autocmd_window {} {
4e327047 2323 global .autocmd.label
6131622e 2324
4e327047 2325 if {[winfo exists .autocmd]} {raise .autocmd ; return}
6131622e 2326
4e327047 2327 build_framework .autocmd "Auto Command" ""
6131622e 2328
4e327047 2329 # First, delete all the old view menu entries
6131622e 2330
4e327047 2331 .autocmd.menubar.view.menu delete 0 last
6131622e 2332
4e327047 2333 # Accumulate output option
6131622e 2334
4e327047
TT
2335 .autocmd.menubar.view.menu add checkbutton \
2336 -variable accumulate_output \
2337 -label "Accumulate output" -onvalue 1 -offvalue 0
6131622e 2338
4e327047 2339 # Now, create entry widget with label
6131622e 2340
4e327047 2341 frame .autocmd.entryframe
6131622e 2342
4e327047
TT
2343 entry .autocmd.entry -borderwidth 2 -relief sunken
2344 bind .autocmd.entry <Key-Return> {
2345 set .autocmd.label [.autocmd.entry get]
2346 .autocmd.entry delete 0 end
2347 }
6131622e 2348
4e327047 2349 label .autocmd.entrylab -text "Command: "
6131622e 2350
4e327047
TT
2351 pack .autocmd.entrylab -in .autocmd.entryframe -side left
2352 pack .autocmd.entry -in .autocmd.entryframe -side left -fill x -expand yes
6131622e 2353
4e327047 2354 pack .autocmd.entryframe -side bottom -fill x -before .autocmd.info
6131622e
SG
2355}
2356
f1b64caa
SG
2357# Return the longest common prefix in SLIST. Can be empty string.
2358
2359proc find_lcp slist {
2360# Handle trivial cases where list is empty or length 1
2361 if {[llength $slist] <= 1} {return [lindex $slist 0]}
2362
2363 set prefix [lindex $slist 0]
2364 set prefixlast [expr [string length $prefix] - 1]
2365
2366 foreach str [lrange $slist 1 end] {
2367 set test_str [string range $str 0 $prefixlast]
2368 while {[string compare $test_str $prefix] != 0} {
2369 decr prefixlast
2370 set prefix [string range $prefix 0 $prefixlast]
2371 set test_str [string range $str 0 $prefixlast]
2372 }
2373 if {$prefixlast < 0} break
2374 }
2375 return $prefix
2376}
2377
2378# Look through COMPLETIONS to generate the suffix needed to do command
2379# completion on CMD.
2380
2381proc find_completion {cmd completions} {
2382# Get longest common prefix
2383 set lcp [find_lcp $completions]
2384 set cmd_len [string length $cmd]
2385# Return suffix beyond end of cmd
2386 return [string range $lcp $cmd_len end]
2387}
2388
746d1df4 2389proc create_command_window {} {
754e5da2 2390 global command_line
f1b64caa 2391 global saw_tab
5bac2b50 2392 global gdb_prompt
754e5da2 2393
f1b64caa 2394 set saw_tab 0
4e327047 2395 if {[winfo exists .cmd]} {raise .cmd ; return}
280c564c 2396
746d1df4
SG
2397 build_framework .cmd Command "* Command Buffer *"
2398
4e327047
TT
2399 # Put focus on command area.
2400 focus .cmd.text
2401
754e5da2 2402 set command_line {}
746d1df4
SG
2403
2404 gdb_cmd {set language c}
2405 gdb_cmd {set height 0}
2406 gdb_cmd {set width 0}
2407
b8f3d4c6
SS
2408 bind .cmd.text <Control-c> gdb_stop
2409
4e327047
TT
2410 # Tk uses the Motifism that Delete means delete forward. I
2411 # hate this, and I'm not gonna take it any more.
2412 set bsBinding [bind Text <BackSpace>]
2413 bind .cmd.text <Delete> "delete_char %W ; $bsBinding; break"
b8f3d4c6 2414 bind .cmd.text <BackSpace> {
81ae689a 2415 if {([%W cget -state] == "disabled")} { break }
b8f3d4c6
SS
2416 delete_char %W
2417 }
2418 bind .cmd.text <Control-u> {
2419 if {([%W cget -state] == "disabled")} { break }
2420 delete_line %W
2421 break
2422 }
746d1df4 2423 bind .cmd.text <Any-Key> {
b8f3d4c6 2424 if {([%W cget -state] == "disabled")} { break }
4e327047
TT
2425 set saw_tab 0
2426 %W insert end %A
2427 %W see end
2428 append command_line %A
2429 break
2430 }
746d1df4 2431 bind .cmd.text <Key-Return> {
b8f3d4c6 2432 if {([%W cget -state] == "disabled")} { break }
4e327047
TT
2433 set saw_tab 0
2434 %W insert end \n
2435 interactive_cmd $command_line
2436
2437 # %W see end
2438 # catch "gdb_cmd [list $command_line]" result
2439 # %W insert end $result
2440 set command_line {}
2441 # update_ptr
5bac2b50 2442 %W insert end "$gdb_prompt"
4e327047
TT
2443 %W see end
2444 break
2445 }
4604b34c 2446 bind .cmd.text <Button-2> {
4e327047
TT
2447 %W insert end [selection get]
2448 %W see end
2449 append command_line [selection get]
2450 break
4604b34c 2451 }
f0b0d915
TT
2452 bind .cmd.text <B2-Motion> break
2453 bind .cmd.text <ButtonRelease-2> break
f1b64caa 2454 bind .cmd.text <Key-Tab> {
b8f3d4c6 2455 if {([%W cget -state] == "disabled")} { break }
4e327047
TT
2456 set choices [gdb_cmd "complete $command_line"]
2457 set choices [string trimright $choices \n]
2458 set choices [split $choices \n]
2459
2460 # Just do completion if this is the first tab
2461 if {!$saw_tab} {
2462 set saw_tab 1
2463 set completion [find_completion $command_line $choices]
2464 append command_line $completion
2465 # Here is where the completion is actually done. If there
2466 # is one match, complete the command and print a space.
2467 # If two or more matches, complete the command and beep.
2468 # If no match, just beep.
2469 switch [llength $choices] {
2470 0 {}
2471 1 {
2472 %W insert end "$completion "
2473 append command_line " "
2474 return
2475 }
746d1df4 2476
4e327047
TT
2477 default {
2478 %W insert end $completion
2479 }
2480 }
2481 bell
2482 %W see end
2483 } else {
2484 # User hit another consecutive tab. List the choices.
2485 # Note that at this point, choices may contain commands
2486 # with spaces. We have to lop off everything before (and
2487 # including) the last space so that the completion list
2488 # only shows the possibilities for the last token.
2489 set choices [lsort $choices]
2490 if {[regexp ".* " $command_line prefix]} {
2491 regsub -all $prefix $choices {} choices
2492 }
5bac2b50 2493 %W insert end "\n[join $choices { }]\n$gdb_prompt$command_line"
4e327047
TT
2494 %W see end
2495 }
2496 break
754e5da2 2497 }
4e327047 2498}
754e5da2 2499
81ae689a
FF
2500# Trim one character off the command line. The argument is ignored.
2501
4e327047
TT
2502proc delete_char {win} {
2503 global command_line
2504 set tmp [expr [string length $command_line] - 2]
2505 set command_line [string range $command_line 0 $tmp]
2506}
2507
81ae689a
FF
2508# FIXME: This should actually check that the first characters of the current
2509# line match the gdb prompt, since the user can move the insertion point
2510# anywhere. It should also check that the insertion point is in the last
2511# line of the text widget.
2512
4e327047 2513proc delete_line {win} {
81ae689a
FF
2514 global command_line
2515 global gdb_prompt
4e327047 2516
81ae689a
FF
2517 set tmp [string length $gdb_prompt]
2518 $win delete "insert linestart + $tmp chars" "insert lineend"
2519 $win see insert
2520 set command_line {}
754e5da2
SG
2521}
2522
e12533e3
SS
2523#
2524# fileselect.tcl --
2525# simple file selector.
2526#
2527# Mario Jorge Silva msilva@cs.Berkeley.EDU
2528# University of California Berkeley Ph: +1(510)642-8248
2529# Computer Science Division, 571 Evans Hall Fax: +1(510)642-5775
2530# Berkeley CA 94720
2531#
2532#
2533# Copyright 1993 Regents of the University of California
2534# Permission to use, copy, modify, and distribute this
2535# software and its documentation for any purpose and without
2536# fee is hereby granted, provided that this copyright
2537# notice appears in all copies. The University of California
2538# makes no representations about the suitability of this
2539# software for any purpose. It is provided "as is" without
2540# express or implied warranty.
2541#
2542
2543
2544# names starting with "fileselect" are reserved by this module
2545# no other names used.
2546# Hack - FSBox is defined instead of fileselect for backwards compatibility
2547
2548
2549# this is the proc that creates the file selector box
2550# purpose - comment string
2551# defaultName - initial value for name
2552# cmd - command to eval upon OK
2553# errorHandler - command to eval upon Cancel
2554# If neither cmd or errorHandler are specified, the return value
2555# of the FSBox procedure is the selected file name.
2556
2557proc FSBox {{purpose "Select file:"} {defaultName ""} {cmd ""} {errorHandler
2558""}} {
2559 global fileselect
2560 set w .fileSelect
4e327047 2561 if {[Exwin_Toplevel $w "Select File" FileSelect]} {
e12533e3
SS
2562 # path independent names for the widgets
2563
2564 set fileselect(list) $w.file.sframe.list
2565 set fileselect(scroll) $w.file.sframe.scroll
2566 set fileselect(direntry) $w.file.f1.direntry
2567 set fileselect(entry) $w.file.f2.entry
2568 set fileselect(ok) $w.but.ok
2569 set fileselect(cancel) $w.but.cancel
2570 set fileselect(msg) $w.label
2571
2572 set fileselect(result) "" ;# value to return if no callback procedures
2573
2574 # widgets
2575 Widget_Label $w label {top fillx pady 10 padx 20} -anchor w -width 24
2576 Widget_Frame $w file Dialog {left expand fill} -bd 10
2577
2578 Widget_Frame $w.file f1 Exmh {top fillx}
2579 Widget_Label $w.file.f1 label {left} -text "Dir"
2580 Widget_Entry $w.file.f1 direntry {right fillx expand} -width 30
2581
2582 Widget_Frame $w.file sframe
2583
2584 scrollbar $w.file.sframe.yscroll -relief sunken \
2585 -command [list $w.file.sframe.list yview]
2586 listbox $w.file.sframe.list -relief sunken \
2587 -yscroll [list $w.file.sframe.yscroll set] -setgrid 1
2588 pack append $w.file.sframe \
2589 $w.file.sframe.yscroll {right filly} \
2590 $w.file.sframe.list {left expand fill}
2591
2592 Widget_Frame $w.file f2 Exmh {top fillx}
2593 Widget_Label $w.file.f2 label {left} -text Name
2594 Widget_Entry $w.file.f2 entry {right fillx expand}
2595
2596 # buttons
2597 $w.but.quit configure -text Cancel \
2598 -command [list fileselect.cancel.cmd $w]
2599
2600 Widget_AddBut $w.but ok OK \
2601 [list fileselect.ok.cmd $w $cmd $errorHandler] {left padx 1}
2602
2603 Widget_AddBut $w.but list List \
2604 [list fileselect.list.cmd $w] {left padx 1}
2605 Widget_CheckBut $w.but listall "List all" fileselect(pattern)
2606 $w.but.listall configure -onvalue "{*,.*}" -offvalue "*" \
2607 -command {fileselect.list.cmd $fileselect(direntry)}
2608 $w.but.listall deselect
2609
2610 # Set up bindings for the browser.
2611 foreach ww [list $w $fileselect(entry)] {
2612 bind $ww <Return> [list $fileselect(ok) invoke]
2613 bind $ww <Control-c> [list $fileselect(cancel) invoke]
2614 }
2615 bind $fileselect(direntry) <Return> [list fileselect.list.cmd %W]
2616 bind $fileselect(direntry) <Tab> [list fileselect.tab.dircmd]
2617 bind $fileselect(entry) <Tab> [list fileselect.tab.filecmd]
4e327047
TT
2618
2619 $fileselect(list) configure -selectmode single
2620
e12533e3
SS
2621 bind $fileselect(list) <Button-1> {
2622 # puts stderr "button 1 release"
e12533e3
SS
2623 $fileselect(entry) delete 0 end
2624 $fileselect(entry) insert 0 [%W get [%W nearest %y]]
2625 }
2626
2627 bind $fileselect(list) <Key> {
e12533e3
SS
2628 $fileselect(entry) delete 0 end
2629 $fileselect(entry) insert 0 [%W get [%W nearest %y]]
2630 }
2631
2632 bind $fileselect(list) <Double-ButtonPress-1> {
2633 # puts stderr "double button 1"
e12533e3
SS
2634 $fileselect(entry) delete 0 end
2635 $fileselect(entry) insert 0 [%W get [%W nearest %y]]
2636 $fileselect(ok) invoke
2637 }
2638
2639 bind $fileselect(list) <Return> {
e12533e3
SS
2640 $fileselect(entry) delete 0 end
2641 $fileselect(entry) insert 0 [%W get [%W nearest %y]]
2642 $fileselect(ok) invoke
2643 }
2644 }
2645 set fileselect(text) $purpose
2646 $fileselect(msg) configure -text $purpose
2647 $fileselect(entry) delete 0 end
2648 $fileselect(entry) insert 0 [file tail $defaultName]
2649
2650 if {[info exists fileselect(lastDir)] && ![string length $defaultName]} {
2651 set dir $fileselect(lastDir)
2652 } else {
2653 set dir [file dirname $defaultName]
2654 }
2655 set fileselect(pwd) [pwd]
2656 fileselect.cd $dir
2657 $fileselect(direntry) delete 0 end
2658 $fileselect(direntry) insert 0 [pwd]/
2659
2660 $fileselect(list) delete 0 end
2661 $fileselect(list) insert 0 "Big directory:"
2662 $fileselect(list) insert 1 $dir
2663 $fileselect(list) insert 2 "Press Return for Listing"
2664
2665 fileselect.list.cmd $fileselect(direntry) startup
2666
2667 # set kbd focus to entry widget
2668
2669# Exwin_ToplevelFocus $w $fileselect(entry)
2670
2671 # Wait for button hits if no callbacks are defined
2672
2673 if {"$cmd" == "" && "$errorHandler" == ""} {
2674 # wait for the box to be destroyed
2675 update idletask
2676 grab $w
2677 tkwait variable fileselect(result)
2678 grab release $w
2679
2680 set path $fileselect(result)
2681 set fileselect(lastDir) [pwd]
2682 fileselect.cd $fileselect(pwd)
2683 return [string trimright [string trim $path] /]
2684 }
2685 fileselect.cd $fileselect(pwd)
2686 return ""
2687}
2688
2689proc fileselect.cd { dir } {
2690 global fileselect
4e327047 2691 if {[catch {cd $dir} err]} {
e12533e3
SS
2692 fileselect.yck $dir
2693 cd
2694 }
2695}
2696# auxiliary button procedures
2697
2698proc fileselect.yck { {tag {}} } {
2699 global fileselect
2700 $fileselect(msg) configure -text "Yck! $tag"
2701}
4e327047 2702
e12533e3
SS
2703proc fileselect.ok {} {
2704 global fileselect
2705 $fileselect(msg) configure -text $fileselect(text)
2706}
2707
2708proc fileselect.cancel.cmd {w} {
2709 global fileselect
2710 set fileselect(result) {}
4604b34c 2711 destroy $w
e12533e3
SS
2712}
2713
2714proc fileselect.list.cmd {w {state normal}} {
2715 global fileselect
2716 set seldir [$fileselect(direntry) get]
2717 if {[catch {glob $seldir} dir]} {
2718 fileselect.yck "glob failed"
2719 return
2720 }
2721 if {[llength $dir] > 1} {
2722 set dir [file dirname $seldir]
2723 set pat [file tail $seldir]
2724 } else {
2725 set pat $fileselect(pattern)
2726 }
2727 fileselect.ok
2728 update idletasks
4e327047 2729 if {[file isdirectory $dir]} {
e12533e3
SS
2730 fileselect.getfiles $dir $pat $state
2731 focus $fileselect(entry)
2732 } else {
2733 fileselect.yck "not a dir"
2734 }
2735}
2736
2737proc fileselect.ok.cmd {w cmd errorHandler} {
2738 global fileselect
2739 set selname [$fileselect(entry) get]
2740 set seldir [$fileselect(direntry) get]
2741
4e327047 2742 if {[string match /* $selname]} {
e12533e3
SS
2743 set selected $selname
2744 } else {
4e327047 2745 if {[string match ~* $selname]} {
e12533e3
SS
2746 set selected $selname
2747 } else {
2748 set selected $seldir/$selname
2749 }
2750 }
2751
2752 # some nasty file names may cause "file isdirectory" to return an error
4e327047 2753 if {[catch {file isdirectory $selected} isdir]} {
e12533e3
SS
2754 fileselect.yck "isdirectory failed"
2755 return
2756 }
4e327047
TT
2757 if {[catch {glob $selected} globlist]} {
2758 if {![file isdirectory [file dirname $selected]]} {
e12533e3
SS
2759 fileselect.yck "bad pathname"
2760 return
2761 }
2762 set globlist $selected
2763 }
2764 fileselect.ok
2765 update idletasks
2766
2767 if {[llength $globlist] > 1} {
2768 set dir [file dirname $selected]
2769 set pat [file tail $selected]
2770 fileselect.getfiles $dir $pat
2771 return
2772 } else {
2773 set selected $globlist
2774 }
4e327047 2775 if {[file isdirectory $selected]} {
e12533e3
SS
2776 fileselect.getfiles $selected $fileselect(pattern)
2777 $fileselect(entry) delete 0 end
2778 return
2779 }
2780
2781 if {$cmd != {}} {
2782 $cmd $selected
2783 } else {
2784 set fileselect(result) $selected
2785 }
4604b34c 2786 destroy $w
e12533e3
SS
2787}
2788
2789proc fileselect.getfiles { dir {pat *} {state normal} } {
2790 global fileselect
2791 $fileselect(msg) configure -text Listing...
2792 update idletasks
2793
2794 set currentDir [pwd]
2795 fileselect.cd $dir
4e327047 2796 if {[catch {set files [lsort [glob -nocomplain $pat]]} err]} {
e12533e3
SS
2797 $fileselect(msg) configure -text $err
2798 $fileselect(list) delete 0 end
2799 update idletasks
2800 return
2801 }
2802 switch -- $state {
2803 normal {
2804 # Normal case - show current directory
2805 $fileselect(direntry) delete 0 end
2806 $fileselect(direntry) insert 0 [pwd]/
2807 }
2808 opt {
2809 # Directory already OK (tab related)
2810 }
2811 newdir {
2812 # Changing directory (tab related)
2813 fileselect.cd $currentDir
2814 }
2815 startup {
2816 # Avoid listing huge directories upon startup.
2817 $fileselect(direntry) delete 0 end
2818 $fileselect(direntry) insert 0 [pwd]/
2819 if {[llength $files] > 32} {
2820 fileselect.ok
2821 return
2822 }
2823 }
2824 }
2825
2826 # build a reordered list of the files: directories are displayed first
2827 # and marked with a trailing "/"
4e327047 2828 if {[string compare $dir /]} {
e12533e3
SS
2829 fileselect.putfiles $files [expr {($pat == "*") ? 1 : 0}]
2830 } else {
2831 fileselect.putfiles $files
2832 }
2833 fileselect.ok
2834}
2835
2836proc fileselect.putfiles {files {dotdot 0} } {
2837 global fileselect
2838
2839 $fileselect(list) delete 0 end
2840 if {$dotdot} {
2841 $fileselect(list) insert end "../"
2842 }
2843 foreach i $files {
2844 if {[file isdirectory $i]} {
2845 $fileselect(list) insert end $i/
2846 } else {
2847 $fileselect(list) insert end $i
2848 }
2849 }
2850}
2851
2852proc FileExistsDialog { name } {
2853 set w .fileExists
2854 global fileExists
2855 set fileExists(ok) 0
2856 {
2857 message $w.msg -aspect 1000
2858 pack $w.msg -side top -fill both -padx 20 -pady 20
2859 $w.but.quit config -text Cancel -command {FileExistsCancel}
2860 button $w.but.ok -text OK -command {FileExistsOK}
2861 pack $w.but.ok -side left
2862 bind $w.msg <Return> {FileExistsOK}
2863 }
2864 $w.msg config -text "Warning: file exists
2865$name
2866OK to overwrite it?"
2867
2868 set fileExists(focus) [focus]
2869 focus $w.msg
2870 grab $w
2871 tkwait variable fileExists(ok)
2872 grab release $w
4604b34c 2873 destroy $w
e12533e3
SS
2874 return $fileExists(ok)
2875}
4e327047 2876
e12533e3
SS
2877proc FileExistsCancel {} {
2878 global fileExists
2879 set fileExists(ok) 0
2880}
4e327047 2881
e12533e3
SS
2882proc FileExistsOK {} {
2883 global fileExists
2884 set fileExists(ok) 1
2885}
2886
2887proc fileselect.getfiledir { dir {basedir [pwd]} } {
2888 global fileselect
2889
2890 set path [$fileselect(direntry) get]
2891 set returnList {}
2892
2893 if {$dir != 0} {
2894 if {[string index $path 0] == "~"} {
2895 set path $path/
2896 }
2897 } else {
2898 set path [$fileselect(entry) get]
2899 }
4e327047 2900 if {[catch {set listFile [glob -nocomplain $path*]}]} {
e12533e3
SS
2901 return $returnList
2902 }
2903 foreach el $listFile {
2904 if {$dir != 0} {
4e327047 2905 if {[file isdirectory $el]} {
e12533e3
SS
2906 lappend returnList [file tail $el]
2907 }
4e327047 2908 } elseif {![file isdirectory $el]} {
e12533e3
SS
2909 lappend returnList [file tail $el]
2910 }
2911 }
2912
2913 return $returnList
2914}
2915
2916proc fileselect.gethead { list } {
2917 set returnHead ""
2918
2919 for {set i 0} {[string length [lindex $list 0]] > $i}\
2920 {incr i; set returnHead $returnHead$thisChar} {
2921 set thisChar [string index [lindex $list 0] $i]
2922 foreach el $list {
2923 if {[string length $el] < $i} {
2924 return $returnHead
2925 }
2926 if {$thisChar != [string index $el $i]} {
2927 return $returnHead
2928 }
2929 }
2930 }
2931 return $returnHead
2932}
4e327047
TT
2933
2934# FIXME this function is a crock. Can write tilde expanding function
2935# in terms of glob and quote_glob; do so.
e12533e3
SS
2936proc fileselect.expand.tilde { } {
2937 global fileselect
2938
2939 set entry [$fileselect(direntry) get]
2940 set dir [string range $entry 1 [string length $entry]]
2941
2942 if {$dir == ""} {
2943 return
2944 }
2945
2946 set listmatch {}
2947
2948 ## look in /etc/passwd
4e327047
TT
2949 if {[file exists /etc/passwd]} {
2950 if {[catch {set users [exec cat /etc/passwd | sed s/:.*//]} err]} {
e12533e3
SS
2951 puts "Error\#1 $err"
2952 return
2953 }
2954 set list [split $users "\n"]
2955 }
2956 if {[lsearch -exact $list "+"] != -1} {
4e327047 2957 if {[catch {set users [exec ypcat passwd | sed s/:.*//]} err]} {
e12533e3
SS
2958 puts "Error\#2 $err"
2959 return
2960 }
2961 set list [concat $list [split $users "\n"]]
2962 }
2963 $fileselect(list) delete 0 end
2964 foreach el $list {
4e327047 2965 if {[string match $dir* $el]} {
e12533e3
SS
2966 lappend listmatch $el
2967 $fileselect(list) insert end $el
2968 }
2969 }
2970 set addings [fileselect.gethead $listmatch]
2971 if {$addings == ""} {
2972 return
2973 }
2974 $fileselect(direntry) delete 0 end
2975 if {[llength $listmatch] == 1} {
2976 $fileselect(direntry) insert 0 [file dirname ~$addings/]
2977 fileselect.getfiles [$fileselect(direntry) get]
2978 } else {
2979 $fileselect(direntry) insert 0 ~$addings
2980 }
2981}
2982
2983proc fileselect.tab.dircmd { } {
2984 global fileselect
2985
2986 set dir [$fileselect(direntry) get]
2987 if {$dir == ""} {
2988 $fileselect(direntry) delete 0 end
2989 $fileselect(direntry) insert 0 [pwd]
4e327047 2990 if {[string compare [pwd] "/"]} {
e12533e3
SS
2991 $fileselect(direntry) insert end /
2992 }
2993 return
2994 }
4e327047 2995 if {[catch {set tmp [file isdirectory [file dirname $dir]]}]} {
e12533e3
SS
2996 if {[string index $dir 0] == "~"} {
2997 fileselect.expand.tilde
2998 }
2999 return
3000 }
3001 if {!$tmp} {
3002 return
3003 }
3004 set dirFile [fileselect.getfiledir 1 $dir]
4e327047 3005 if {![llength $dirFile]} {
e12533e3
SS
3006 return
3007 }
3008 if {[llength $dirFile] == 1} {
3009 $fileselect(direntry) delete 0 end
3010 $fileselect(direntry) insert 0 [file dirname $dir]
4e327047 3011 if {[string compare [file dirname $dir] /]} {
e12533e3
SS
3012 $fileselect(direntry) insert end /[lindex $dirFile 0]/
3013 } else {
3014 $fileselect(direntry) insert end [lindex $dirFile 0]/
3015 }
3016 fileselect.getfiles [$fileselect(direntry) get] \
3017 "[file tail [$fileselect(direntry) get]]$fileselect(pattern)" opt
3018 return
3019 }
3020 set headFile [fileselect.gethead $dirFile]
3021 $fileselect(direntry) delete 0 end
3022 $fileselect(direntry) insert 0 [file dirname $dir]
4e327047 3023 if {[string compare [file dirname $dir] /]} {
e12533e3
SS
3024 $fileselect(direntry) insert end /$headFile
3025 } else {
3026 $fileselect(direntry) insert end $headFile
3027 }
3028 if {$headFile == "" && [file isdirectory $dir]} {
3029 fileselect.getfiles $dir\
3030 "[file tail [$fileselect(direntry) get]]$fileselect(pattern)" opt
3031 } else {
3032 fileselect.getfiles [file dirname $dir]\
3033 "[file tail [$fileselect(direntry) get]]*" newdir
3034 }
3035}
3036
3037proc fileselect.tab.filecmd { } {
3038 global fileselect
3039
3040 set dir [$fileselect(direntry) get]
3041 if {$dir == ""} {
3042 set dir [pwd]
3043 }
3044 if {![file isdirectory $dir]} {
3045 error "dir $dir doesn't exist"
3046 }
3047 set listFile [fileselect.getfiledir 0 $dir]
3048 puts $listFile
4e327047 3049 if {![llength $listFile]} {
e12533e3
SS
3050 return
3051 }
3052 if {[llength $listFile] == 1} {
3053 $fileselect(entry) delete 0 end
3054 $fileselect(entry) insert 0 [lindex $listFile 0]
3055 return
3056 }
3057 set headFile [fileselect.gethead $listFile]
3058 $fileselect(entry) delete 0 end
3059 $fileselect(entry) insert 0 $headFile
3060 fileselect.getfiles $dir "[$fileselect(entry) get]$fileselect(pattern)" opt
3061}
3062
3063proc Exwin_Toplevel { path name {class Dialog} {dismiss yes}} {
3064 global exwin
4e327047 3065 if {[catch {wm state $path} state]} {
e12533e3 3066 set t [Widget_Toplevel $path $name $class]
4e327047 3067 if {![info exists exwin(toplevels)]} {
e12533e3
SS
3068 set exwin(toplevels) [option get . exwinPaths {}]
3069 }
3070 set ix [lsearch $exwin(toplevels) $t]
3071 if {$ix < 0} {
3072 lappend exwin(toplevels) $t
3073 }
3074 if {$dismiss == "yes"} {
3075 set f [Widget_Frame $t but Menubar {top fill}]
3076 Widget_AddBut $f quit "Dismiss" [list Exwin_Dismiss $path]
3077 }
3078 return 1
3079 } else {
3080 if {$state != "normal"} {
3081 catch {
3082 wm geometry $path $exwin(geometry,$path)
3083# Exmh_Debug Exwin_Toplevel $path $exwin(geometry,$path)
3084 }
3085 wm deiconify $path
3086 } else {
3087 catch {raise $path}
3088 }
3089 return 0
3090 }
3091}
3092
3093proc Exwin_Dismiss { path {geo ok} } {
3094 global exwin
3095 case $geo {
3096 "ok" {
3097 set exwin(geometry,$path) [wm geometry $path]
3098 }
3099 "nosize" {
3100 set exwin(geometry,$path) [string trimleft [wm geometry $path] 0123456789x]
3101 }
3102 default {
3103 catch {unset exwin(geometry,$path)}
3104 }
3105 }
3106 wm withdraw $path
3107}
3108
3109proc Widget_Toplevel { path name {class Dialog} {x {}} {y {}} } {
3110 set self [toplevel $path -class $class]
3111 set usergeo [option get $path position Position]
3112 if {$usergeo != {}} {
4e327047 3113 if {[catch {wm geometry $self $usergeo} err]} {
e12533e3
SS
3114# Exmh_Debug Widget_Toplevel $self $usergeo => $err
3115 }
3116 } else {
3117 if {($x != {}) && ($y != {})} {
3118# Exmh_Debug Event position $self +$x+$y
3119 wm geometry $self +$x+$y
3120 }
3121 }
3122 wm title $self $name
3123 wm group $self .
3124 return $self
3125}
3126
3127proc Widget_Frame {par child {class GDB} {where {top expand fill}} args } {
3128 if {$par == "."} {
3129 set self .$child
3130 } else {
3131 set self $par.$child
3132 }
3133 eval {frame $self -class $class} $args
3134 pack append $par $self $where
3135 return $self
3136}
3137
3138proc Widget_AddBut {par but txt cmd {where {right padx 1}} } {
3139 # Create a Packed button. Return the button pathname
3140 set cmd2 [list button $par.$but -text $txt -command $cmd]
4e327047 3141 if {[catch $cmd2 t]} {
e12533e3
SS
3142 puts stderr "Widget_AddBut (warning) $t"
3143 eval $cmd2 {-font fixed}
3144 }
3145 pack append $par $par.$but $where
3146 return $par.$but
3147}
4e327047 3148
e12533e3
SS
3149proc Widget_CheckBut {par but txt var {where {right padx 1}} } {
3150 # Create a check button. Return the button pathname
3151 set cmd [list checkbutton $par.$but -text $txt -variable $var]
4e327047 3152 if {[catch $cmd t]} {
e12533e3
SS
3153 puts stderr "Widget_CheckBut (warning) $t"
3154 eval $cmd {-font fixed}
3155 }
3156 pack append $par $par.$but $where
3157 return $par.$but
3158}
3159
3160proc Widget_Label { frame {name label} {where {left fill}} args} {
3161 set cmd [list label $frame.$name ]
4e327047 3162 if {[catch [concat $cmd $args] t]} {
e12533e3
SS
3163 puts stderr "Widget_Label (warning) $t"
3164 eval $cmd $args {-font fixed}
3165 }
3166 pack append $frame $frame.$name $where
3167 return $frame.$name
3168}
4e327047 3169
e12533e3
SS
3170proc Widget_Entry { frame {name entry} {where {left fill}} args} {
3171 set cmd [list entry $frame.$name ]
4e327047 3172 if {[catch [concat $cmd $args] t]} {
e12533e3
SS
3173 puts stderr "Widget_Entry (warning) $t"
3174 eval $cmd $args {-font fixed}
3175 }
3176 pack append $frame $frame.$name $where
3177 return $frame.$name
3178}
3179
3180# End of fileselect.tcl.
3181
4e327047
TT
3182#
3183# Create a copyright window and center it on the screen. Arrange for
3184# it to disappear when the user clicks it, or after a suitable period
3185# of time.
3186#
3187proc create_copyright_window {} {
3188 toplevel .c
3189 message .c.m -text [gdb_cmd {show version}] -aspect 500 -relief raised
3190 pack .c.m
a5cffdc4 3191
4e327047 3192 bind .c.m <1> {destroy .c}
f0b0d915 3193 bind .c <Leave> {destroy .c}
4e327047
TT
3194 # "suitable period" currently means "15 seconds".
3195 after 15000 {
3196 if {[winfo exists .c]} then {
3197 destroy .c
3198 }
3199 }
746d1df4 3200
4e327047
TT
3201 wm transient .c .
3202 center_window .c
a5cffdc4 3203}
746d1df4 3204
954a4a2a
FF
3205# Begin support primarily for debugging the tcl/tk portion of gdbtk. You can
3206# start gdbtk, and then issue the command "tk tclsh" and a window will pop up
3207# giving you direct access to the tcl interpreter. With this, it is very easy
3208# to examine the values of global variables, directly invoke routines that are
3209# part of the gdbtk interface, replace existing proc's with new ones, etc.
3210# This code was inspired from example 11-3 in Brent Welch's "Practical
3211# Programming in Tcl and Tk"
3212
3213set tcl_prompt "tcl> "
3214
3215# Get the current command that user has typed, from cmdstart to end of text
3216# widget. Evaluate it, insert result back into text widget, issue a new
3217# prompt, update text widget and update command start mark.
3218
3219proc evaluate_tcl_command { twidget } {
3220 global tcl_prompt
3221
3222 set command [$twidget get cmdstart end]
3223 if [info complete $command] {
3224 set err [catch {uplevel #0 $command} result]
3225 $twidget insert insert \n$result\n
3226 $twidget insert insert $tcl_prompt
3227 $twidget see insert
3228 $twidget mark set cmdstart insert
3229 return
3230 }
3231}
3232
3233# Create the evaluation window and set up the keybindings to evaluate the
3234# last single line entered by the user. FIXME: allow multiple lines?
3235
3236proc tclsh {} {
3237 global tcl_prompt
3238
5bac2b50
FF
3239 # If another evaluation window already exists, just bring it to the front.
3240 if {[winfo exists .eval]} {raise .eval ; return}
3241
954a4a2a
FF
3242 # Create top level frame with scrollbar and text widget.
3243 toplevel .eval
3244 wm title .eval "Tcl Evaluation"
3245 wm iconname .eval "Tcl"
3246 text .eval.text -width 80 -height 20 -setgrid true -cursor hand2 \
3247 -yscrollcommand {.eval.scroll set}
3248 scrollbar .eval.scroll -command {.eval.text yview}
41756e56
FF
3249 pack .eval.scroll -side right -fill y
3250 pack .eval.text -side left -fill both -expand true
954a4a2a
FF
3251
3252 # Insert the tcl_prompt and initialize the cmdstart mark
3253 .eval.text insert insert $tcl_prompt
3254 .eval.text mark set cmdstart insert
3255 .eval.text mark gravity cmdstart left
3256
3257 # Make this window the current one for input.
3258 focus .eval.text
3259
3260 # Keybindings that limit input and evaluate things
3261 bind .eval.text <Return> { evaluate_tcl_command .eval.text ; break }
3f8eefba
FF
3262 bind .eval.text <BackSpace> {
3263 if [%W compare insert > cmdstart] {
3264 %W delete {insert - 1 char} insert
3265 } else {
3266 bell
3267 }
3268 break
3269 }
954a4a2a
FF
3270 bind .eval.text <Any-Key> {
3271 if [%W compare insert < cmdstart] {
3272 %W mark set insert end
3273 }
3274 }
3f8eefba
FF
3275 bind .eval.text <Control-u> {
3276 %W delete cmdstart "insert lineend"
3277 %W see insert
3278 }
954a4a2a
FF
3279 bindtags .eval.text {.eval.text Text all}
3280}
3281
5bac2b50
FF
3282# This proc is executed just prior to falling into the Tk main event loop.
3283proc gdbtk_tcl_preloop {} {
3284 global gdb_prompt
3285 .cmd.text insert end "$gdb_prompt"
3286 .cmd.text see end
3287 update
3288}
3289
4e327047
TT
3290# FIXME need to handle mono here. In Tk4 that is more complicated.
3291set highlight "-background red2 -borderwidth 2 -relief sunken"
09722039 3292
4e327047
TT
3293# Setup the initial windows
3294create_source_window
3295create_command_window
09722039 3296
4e327047
TT
3297# Make this last so user actually sees it.
3298create_copyright_window
3299# Refresh.
6131622e 3300update
09722039 3301
4e327047
TT
3302if {[file exists ~/.gdbtkinit]} {
3303 source ~/.gdbtkinit
6bd7d9fa 3304}
This page took 0.250047 seconds and 4 git commands to generate.