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