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