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