Menu

[r3]: / cmp / TkCon / tkcon_proc.tcl  Maximize  Restore  History

Download this file

1245 lines (1175 with data), 36.5 kB

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
##
## tkcon_proc.tcl
## Enhanced Tk Console, part of the VerTcl system
##
## Originally based off Brent Welch's Tcl Shell Widget
## (from "Practical Programming in Tcl and Tk")
##
## Thanks to the following (among many) for early bug reports & code ideas:
## Steven Wahl <steven@indra.com>, Jan Nijtmans <nijtmans@nici.kun.nl>
## Crimmins <markcrim@umich.edu>, Wart <wart@ugcs.caltech.edu>
##
## Copyright 1995-1999 Jeffrey Hobbs
## Initiated: Thu Aug 17 15:36:47 PDT 1995
##
## jeff.hobbs@acm.org
##
## source standard_disclaimer.tcl
## source bourbon_ware.tcl
##
package require StdLib
package require Interface
PkgProvide TkConProc
# Initialize the ::tkcon namespace
#
namespace eval ::tkcon {
set PRIV(WWW) [info exists embed_args]
}
##
## Some procedures to make up for lack of built-in shell commands
##
proc help {} {
puts "
\[alias\]
akin to the csh alias command
If called with no args, then it dumps out all current aliases
If called with one arg, returns the alias of that arg (or {} if none)
ARGS: newcmd - (optional) command to bind alias to
args - command and args being aliased
\[cct\]
compiler interface, type: \"cct -h\" for additional help
\[clear\]
clear - clears the buffer of the console (not the history though)
\[dir\]
directory list
ARGS: args - names/glob patterns of directories to list
OPTS: -all - list hidden files as well (Unix dot files)
-long - list in full format \"permissions size date filename\"
-full - displays / after directories and link paths for links
RET: a directory listing
\[dump\]
outputs variables/procedure/widget info in source'able form.
Accepts glob style pattern matching for the names
ARGS: type - type of thing to dump: must be variable, procedure, widget
OPTS: -nocomplain
don't complain if no vars match something
-filter pattern
specifies a glob filter pattern to be used by the variable
method as an array filter pattern (it filters down for
nested elements) and in the widget method as a config
option filter pattern
--
forcibly ends options recognition
RET: the values of the requested items in a 'source'able form
\[echo\]
relaxes the one string restriction of 'puts'
ARGS: any number of strings to output to stdout
\[help\]
print this message
\[idebug\]
interactive debugger
ARGS: opt
args - additional arguments
\[lremove\]
remove items from a list
OPTS: -all remove all instances of each item
-glob remove all instances matching glob pattern
-regexp remove all instances matching regexp pattern
ARGS: l a list to remove items from
args items to remove (these are 'join'ed together)
\[observe\]
like trace, but not
ARGS: opt - option
name - name of variable or command
args - additional arguments
\[observe_var\]
auxilary function for observing vars, called by trace
via observe
ARGS: name - variable name
el - array element name, if any
op - operation type (rwu)
\[unalias\]
unaliases an alias'ed command
ARGS: cmd - command to unbind as an alias
\[what\]
tells you what a string is recognized as
ARGS: str - string to id
RET: id types of command as list
\[which\]
tells you where a command is found
ARGS: cmd - command name
RET: where command is found (internal / external / unknown)
"
puts "\n \[predefined aliases\]\n"
set List {}
set Max 0
foreach {A P T} [ alias ] {
lappend List [ list $A $T ]
set L [ string length $A ]
if { $L > $Max } { set Max $L }
}
foreach {V} [ lsort -index 0 $List ] {
foreach {A T} $V break
puts [ format { %*s -> %s} $Max $A $T ]
}
puts "\n \[environment\]\n"
set List {}
set Max 0
foreach {A T} [ array get ::env TK_CON* ] {
lappend List [ list $A $T ]
set L [ string length $A ]
if { $L > $Max } { set Max $L }
}
foreach {V} [ lsort -index 0 $List ] {
foreach {A T} $V break
puts [ format { %-*s -> %s} $Max $A $T ]
}
}
proc cctputs { FH } {
if {[ eof $FH ]} {
puts "finish background process"
catch { close $FH }
return
}
puts [gets $FH]
}
proc cct {args} {
::StdLib::Push ERROR PUTS
set FH [ ::ITclSh::Open [ concat package Cct $args ] ]
::StdLib::Pop ERROR
fileevent $FH readable [ list cctputs $FH ]
}
## tkcon_puts -
## This allows me to capture all stdout/stderr to the console window
## This will be renamed to 'puts' at the appropriate time during init
##
# ARGS: same as usual
# Outputs: the string with a color-coded text tag
##
proc tkcon_puts args {
set len [llength $args]
foreach {arg1 arg2 arg3} $args { break }
if {$len == 1} {
tkcon console insert output "$arg1\n" stdout
} elseif {$len == 2} {
if {![string compare $arg1 -nonewline]} {
tkcon console insert output $arg2 stdout
} elseif {![string compare $arg1 stdout] \
|| ![string compare $arg1 stderr]} {
tkcon console insert output "$arg2\n" $arg1
} else {
set len 0
}
} elseif {$len == 3} {
if {![string compare $arg1 -nonewline] \
&& (![string compare $arg2 stdout] \
|| ![string compare $arg2 stderr])} {
tkcon console insert output $arg3 $arg2
} elseif {(![string compare $arg1 stdout] \
|| ![string compare $arg1 stderr]) \
&& ![string compare $arg3 nonewline]} {
tkcon console insert output $arg2 $arg1
} else {
set len 0
}
} else {
set len 0
}
## $len == 0 means it wasn't handled by tkcon above.
##
if {$len == 0} {
global errorCode errorInfo
if {[catch "tkcon_tcl_puts $args" msg]} {
regsub tkcon_tcl_puts $msg puts msg
regsub -all tkcon_tcl_puts $errorInfo puts errorInfo
return -code error $msg
}
return $msg
}
## WARNING: This update should behave well because it uses idletasks,
## however, if there are weird looping problems with events, or
## hanging in waits, try commenting this out.
if {$len} {
tkcon console see output
update idletasks
}
}
## tkcon_gets -
## This allows me to capture all stdin input without needing to stdin
## This will be renamed to 'gets' at the appropriate time during init
##
# ARGS: same as gets
# Outputs: same as gets
##
proc tkcon_gets args {
set len [llength $args]
if {$len != 1 && $len != 2} {
return -code error \
"wrong # args: should be \"gets channelId ?varName?\""
}
if {[string compare stdin [lindex $args 0]]} {
return [uplevel 1 tkcon_tcl_gets $args]
}
set gtype [tkcon set ::tkcon::OPT(gets)]
if {$gtype == ""} { set gtype congets }
set data [tkcon $gtype]
if {$len == 2} {
upvar 1 [lindex $args 1] var
set var $data
return [string length $data]
}
return $data
}
## echo
## Relaxes the one string restriction of 'puts'
# ARGS: any number of strings to output to stdout
##
proc echo args { puts [concat $args] }
## clear - clears the buffer of the console (not the history though)
## This is executed in the parent interpreter
##
proc clear {{pcnt 100}} {
if {![regexp {^[0-9]*$} $pcnt] || $pcnt < 1 || $pcnt > 100} {
return -code error \
"invalid percentage to clear: must be 1-100 (100 default)"
} elseif {$pcnt == 100} {
tkcon console delete 1.0 end
} else {
set tmp [expr {$pcnt/100.0*[tkcon console index end]}]
tkcon console delete 1.0 "$tmp linestart"
}
}
## alias - akin to the csh alias command
## If called with no args, then it dumps out all current aliases
## If called with one arg, returns the alias of that arg (or {} if none)
# ARGS: newcmd - (optional) command to bind alias to
# args - command and args being aliased
##
proc alias {{newcmd {}} args} {
if {[string match {} $newcmd]} {
set res {}
foreach a [interp aliases] {
lappend res [list $a -> [interp alias {} $a]]
}
return [join $res \n]
} elseif {![llength $args]} {
interp alias {} $newcmd
} else {
eval interp alias [list {} $newcmd {}] $args
}
}
## unalias - unaliases an alias'ed command
# ARGS: cmd - command to unbind as an alias
##
proc unalias {cmd} {
interp alias {} $cmd {}
}
## dump - outputs variables/procedure/widget info in source'able form.
## Accepts glob style pattern matching for the names
# ARGS: type - type of thing to dump: must be variable, procedure, widget
# OPTS: -nocomplain
# don't complain if no vars match something
# -filter pattern
# specifies a glob filter pattern to be used by the variable
# method as an array filter pattern (it filters down for
# nested elements) and in the widget method as a config
# option filter pattern
# -- forcibly ends options recognition
# Returns: the values of the requested items in a 'source'able form
##
proc dump {type args} {
set whine 1
set code ok
if {![llength $args]} {
## If no args, assume they gave us something to dump and
## we'll try anything
set args $type
set type any
}
while {[string match -* [lindex $args 0]]} {
switch -glob -- [lindex $args 0] {
-n* { set whine 0; set args [lreplace $args 0 0] }
-f* { set fltr [lindex $args 1]; set args [lreplace $args 0 1] }
-- { set args [lreplace $args 0 0]; break }
default {return -code error "unknown option \"[lindex $args 0]\""}
}
}
if {$whine && ![llength $args]} {
return -code error "wrong \# args: [lindex [info level 0] 0] type\
?-nocomplain? ?-filter pattern? ?--? pattern ?pattern ...?"
}
set res {}
switch -glob -- $type {
c* {
# command
# outputs commands by figuring out, as well as possible, what it is
# this does not attempt to auto-load anything
foreach arg $args {
if {[llength [set cmds [info commands $arg]]]} {
foreach cmd [lsort $cmds] {
if {[lsearch -exact [interp aliases] $cmd] > -1} {
append res "\#\# ALIAS: $cmd =>\
[interp alias {} $cmd]\n"
} elseif {
[llength [info procs $cmd]] ||
([string match *::* $cmd] &&
[llength [namespace eval [namespace qual $cmd] \
info procs [namespace tail $cmd]]])
} {
if {[catch {dump p -- $cmd} msg] && $whine} {
set code error
}
append res $msg\n
} else {
append res "\#\# COMMAND: $cmd\n"
}
}
} elseif {$whine} {
append res "\#\# No known command $arg\n"
set code error
}
}
}
v* {
# variable
# outputs variables value(s), whether array or simple.
if {![info exists fltr]} { set fltr * }
foreach arg $args {
if {![llength [set vars [uplevel 1 info vars [list $arg]]]]} {
if {[uplevel 1 info exists $arg]} {
set vars $arg
} elseif {$whine} {
append res "\#\# No known variable $arg\n"
set code error
continue
} else { continue }
}
foreach var [lsort $vars] {
if {[uplevel 1 [list info locals $var]] == ""} {
# use the proper scope of the var, but
# namespace which won't id locals correctly
set var [uplevel 1 \
[list namespace which -variable $var]]
}
upvar 1 $var v
if {[array exists v] || [catch {string length $v}]} {
set nst {}
append res "array set [list $var] \{\n"
if {[array size v]} {
foreach i [lsort [array names v $fltr]] {
upvar 0 v\($i\) __a
if {[array exists __a]} {
append nst "\#\# NESTED ARRAY ELEM: $i\n"
append nst "upvar 0 [list $var\($i\)] __a;\
[dump v -filter $fltr __a]\n"
} else {
append res " [list $i]\t[list $v($i)]\n"
}
}
} else {
## empty array
append res " empty array\n"
append nst "unset [list $var](empty)\n"
}
append res "\}\n$nst"
} else {
append res [list set $var $v]\n
}
}
}
}
p* {
# procedure
foreach arg $args {
if {
![llength [set procs [info proc $arg]]] &&
([string match *::* $arg] &&
[llength [set ps [namespace eval \
[namespace qualifier $arg] \
info procs [namespace tail $arg]]]])
} {
set procs {}
set namesp [namespace qualifier $arg]
foreach p $ps {
lappend procs ${namesp}::$p
}
}
if {[llength $procs]} {
foreach p [lsort $procs] {
set as {}
foreach a [info args $p] {
if {[info default $p $a tmp]} {
lappend as [list $a $tmp]
} else {
lappend as $a
}
}
append res [list proc $p $as [info body $p]]\n
}
} elseif {$whine} {
append res "\#\# No known proc $arg\n"
set code error
}
}
}
w* {
# widget
## The user should have Tk loaded
if {![llength [info command winfo]]} {
return -code error "winfo not present, cannot dump widgets"
}
if {![info exists fltr]} { set fltr .* }
foreach arg $args {
if {[llength [set ws [info command $arg]]]} {
foreach w [lsort $ws] {
if {[winfo exists $w]} {
if {[catch {$w configure} cfg]} {
append res "\#\# Widget $w\
does not support configure method"
set code error
} else {
append res "\#\# [winfo class $w]\
$w\n$w configure"
foreach c $cfg {
if {[llength $c] != 5} continue
## Check to see that the option does
## not match the default, then check
## the item against the user filter
if {[string compare [lindex $c 3] \
[lindex $c 4]] && \
[regexp -nocase -- $fltr $c]} {
append res " \\\n\t[list [lindex $c 0]\
[lindex $c 4]]"
}
}
append res \n
}
}
}
} elseif {$whine} {
append res "\#\# No known widget $arg\n"
set code error
}
}
}
a* {
## see if we recognize it, other complain
if {[regexp {(var|com|proc|widget)} \
[set types [uplevel 1 what $args]]]} {
foreach type $types {
if {[regexp {(var|com|proc|widget)} $type]} {
append res "[uplevel 1 dump $type $args]\n"
}
}
} else {
set res "dump was unable to resolve type for \"$args\""
set code error
}
}
default {
return -code error "bad [lindex [info level 0] 0] option\
\"$type\": must be variable, command, procedure,\
or widget"
}
}
return -code $code [string trimright $res \n]
}
## idebug - interactive debugger
# ARGS: opt
#
##
proc idebug {opt args} {
global IDEBUG
if {![info exists IDEBUG(on)]} {
array set IDEBUG { on 0 id * debugging 0 }
}
set level [expr {[info level]-1}]
switch -glob -- $opt {
on {
if {[llength $args]} { set IDEBUG(id) $args }
return [set IDEBUG(on) 1]
}
off { return [set IDEBUG(on) 0] }
id {
if {![llength $args]} {
return $IDEBUG(id)
} else { return [set IDEBUG(id) $args] }
}
break {
if {!$IDEBUG(on) || $IDEBUG(debugging) || \
([llength $args] && \
![string match $IDEBUG(id) $args]) || [info level]<1} {
return
}
set IDEBUG(debugging) 1
puts stderr "idebug at level \#$level: [lindex [info level -1] 0]"
set tkcon [llength [info command tkcon]]
if {$tkcon} {
tkcon show
tkcon master eval set ::tkcon::OPT(prompt2) \$::tkcon::OPT(prompt1)
tkcon master eval set ::tkcon::OPT(prompt1) \$::tkcon::OPT(debugPrompt)
set slave [tkcon set ::tkcon::OPT(exec)]
set event [tkcon set ::tkcon::PRIV(event)]
tkcon set ::tkcon::OPT(exec) [tkcon master interp create debugger]
tkcon set ::tkcon::PRIV(event) 1
}
set max $level
while 1 {
set err {}
if {$tkcon} {
# tkcon's overload of gets is advanced enough to not need
# this, but we get a little better control this way.
tkcon evalSlave set level $level
tkcon prompt
set line [tkcon getcommand]
tkcon console mark set output end
} else {
puts -nonewline stderr "(level \#$level) debug > "
gets stdin line
while {![info complete $line]} {
puts -nonewline "> "
append line "\n[gets stdin]"
}
}
if {[string match {} $line]} continue
set key [lindex $line 0]
if {![regexp {^([#-]?[0-9]+)} [lreplace $line 0 0] lvl]} {
set lvl \#$level
}
set res {}; set c 0
switch -- $key {
+ {
## Allow for jumping multiple levels
if {$level < $max} {
idebug trace [incr level] $level 0 VERBOSE
}
}
- {
## Allow for jumping multiple levels
if {$level > 1} {
idebug trace [incr level -1] $level 0 VERBOSE
}
}
. { set c [catch {idebug trace $level $level 0 VERBOSE} res] }
v { set c [catch {idebug show vars $lvl } res] }
V { set c [catch {idebug show vars $lvl VERBOSE} res] }
l { set c [catch {idebug show locals $lvl } res] }
L { set c [catch {idebug show locals $lvl VERBOSE} res] }
g { set c [catch {idebug show globals $lvl } res] }
G { set c [catch {idebug show globals $lvl VERBOSE} res] }
t { set c [catch {idebug trace 1 $max $level } res] }
T { set c [catch {idebug trace 1 $max $level VERBOSE} res]}
b { set c [catch {idebug body $lvl} res] }
o { set res [set IDEBUG(on) [expr {!$IDEBUG(on)}]] }
h - ? {
puts stderr " + Move down in call stack
- Move up in call stack
. Show current proc name and params
v Show names of variables currently in scope
V Show names of variables currently in scope with values
l Show names of local (transient) variables
L Show names of local (transient) variables with values
g Show names of declared global variables
G Show names of declared global variables with values
t Show a stack trace
T Show a verbose stack trace
b Show body of current proc
o Toggle on/off any further debugging
c,q Continue regular execution (Quit debugger)
h,? Print this help
default Evaluate line at current level (\#$level)"
}
c - q break
default { set c [catch {uplevel \#$level $line} res] }
}
if {$tkcon} {
tkcon set ::tkcon::PRIV(event) \
[tkcon evalSlave eval history add [list $line]\
\; history nextid]
}
if {$c} {
puts stderr $res
} elseif {[string compare {} $res]} {
puts $res
}
}
set IDEBUG(debugging) 0
if {$tkcon} {
tkcon master interp delete debugger
tkcon master eval set ::tkcon::OPT(prompt1) \$::tkcon::OPT(prompt2)
tkcon set ::tkcon::OPT(exec) $slave
tkcon set ::tkcon::PRIV(event) $event
tkcon prompt
}
}
bo* {
if {[regexp {^([#-]?[0-9]+)} $args level]} {
return [uplevel $level {dump c -no [lindex [info level 0] 0]}]
}
}
t* {
if {[llength $args]<2} return
set min [set max [set lvl $level]]
set exp {^#?([0-9]+)? ?#?([0-9]+) ?#?([0-9]+)? ?(VERBOSE)?}
if {![regexp $exp $args junk min max lvl verbose]} return
for {set i $max} {
$i>=$min && ![catch {uplevel \#$i info level 0} info]
} {incr i -1} {
if {$i==$lvl} {
puts -nonewline stderr "* \#$i:\t"
} else {
puts -nonewline stderr " \#$i:\t"
}
set name [lindex $info 0]
if {[string compare VERBOSE $verbose] || \
![llength [info procs $name]]} {
puts $info
} else {
puts "proc $name {[info args $name]} { ... }"
set idx 0
foreach arg [info args $name] {
if {[string match args $arg]} {
puts "\t$arg = [lrange $info [incr idx] end]"
break
} else {
puts "\t$arg = [lindex $info [incr idx]]"
}
}
}
}
}
s* {
#var, local, global
set level \#$level
if {![regexp {^([vgl][^ ]*) ?([#-]?[0-9]+)? ?(VERBOSE)?} \
$args junk type level verbose]} return
switch -glob -- $type {
v* { set vars [uplevel $level {lsort [info vars]}] }
l* { set vars [uplevel $level {lsort [info locals]}] }
g* { set vars [lremove [uplevel $level {info vars}] \
[uplevel $level {info locals}]] }
}
if {[string match VERBOSE $verbose]} {
return [uplevel $level dump var -nocomplain $vars]
} else {
return $vars
}
}
e* - pu* {
if {[llength $opt]==1 && [catch {lindex [info level -1] 0} id]} {
set id [lindex [info level 0] 0]
} else {
set id [lindex $opt 1]
}
if {$IDEBUG(on) && [string match $IDEBUG(id) $id]} {
if {[string match e* $opt]} {
puts [concat $args]
} else { eval puts $args }
}
}
default {
return -code error "bad [lindex [info level 0] 0] option \"$opt\",\
must be: [join [lsort [list on off id break print body\
trace show puts echo]] {, }]"
}
}
}
## observe - like trace, but not
# ARGS: opt - option
# name - name of variable or command
##
proc observe {opt name args} {
global tcl_observe
switch -glob -- $opt {
co* {
if {[regexp {^(catch|lreplace|set|puts|for|incr|info|uplevel)$} \
$name]} {
return -code error "cannot observe \"$name\":\
infinite eval loop will occur"
}
set old ${name}@
while {[llength [info command $old]]} { append old @ }
rename $name $old
set max 4
regexp {^[0-9]+} $args max
## idebug trace could be used here
proc $name args "
for {set i \[info level\]; set max \[expr \[info level\]-$max\]} {
\$i>=\$max && !\[catch {uplevel \#\$i info level 0} info\]
} {incr i -1} {
puts -nonewline stderr \" \#\$i:\t\"
puts \$info
}
uplevel \[lreplace \[info level 0\] 0 0 $old\]
"
set tcl_observe($name) $old
}
cd* {
if {[info exists tcl_observe($name)] && [catch {
rename $name {}
rename $tcl_observe($name) $name
unset tcl_observe($name)
} err]} { return -code error $err }
}
ci* {
## What a useless method...
if {[info exists tcl_observe($name)]} {
set i $tcl_observe($name)
set res "\"$name\" observes true command \"$i\""
while {[info exists tcl_observe($i)]} {
append res "\n\"$name\" observes true command \"$i\""
set i $tcl_observe($name)
}
return $res
}
}
va* - vd* {
set type [lindex $args 0]
set args [lrange $args 1 end]
if {![regexp {^[rwu]} $type type]} {
return -code error "bad [lindex [info level 0] 0] $opt type\
\"$type\", must be: read, write or unset"
}
if {![llength $args]} { set args observe_var }
uplevel 1 [list trace $opt $name $type $args]
}
vi* {
uplevel 1 [list trace vinfo $name]
}
default {
return -code error "bad [lindex [info level 0] 0] option\
\"[lindex $args 0]\", must be: [join [lsort \
[list command cdelete cinfo variable vdelete vinfo]] {, }]"
}
}
}
## observe_var - auxilary function for observing vars, called by trace
## via observe
# ARGS: name - variable name
# el - array element name, if any
# op - operation type (rwu)
##
proc observe_var {name el op} {
if {[string match u $op]} {
if {[string compare {} $el]} {
puts "unset \"${name}($el)\""
} else {
puts "unset \"$name\""
}
} else {
upvar 1 $name $name
if {[info exists ${name}($el)]} {
puts [dump v ${name}($el)]
} else {
puts [dump v $name]
}
}
}
## which - tells you where a command is found
# ARGS: cmd - command name
# Returns: where command is found (internal / external / unknown)
##
proc which cmd {
## This tries to auto-load a command if not recognized
set types [what $cmd 1]
if {[llength $types]} {
set out {}
foreach type $types {
switch -- $type {
alias { set res "$cmd: aliased to [alias $cmd]" }
procedure { set res "$cmd: procedure" }
command { set res "$cmd: internal command" }
executable { lappend out [auto_execok $cmd] }
variable { lappend out "$cmd: variable" }
}
if {[info exists res]} {
global auto_index
if {[info exists auto_index($cmd)]} {
## This tells you where the command MIGHT have come from -
## not true if the command was redefined interactively or
## existed before it had to be auto_loaded. This is just
## provided as a hint at where it MAY have come from
append res " ($auto_index($cmd))"
}
lappend out $res
unset res
}
}
return [join $out \n]
} else {
return -code error "$cmd: command not found"
}
}
## what - tells you what a string is recognized as
# ARGS: str - string to id
# Returns: id types of command as list
##
proc what {str {autoload 0}} {
set types {}
if {[llength [info commands $str]] || ($autoload && \
[auto_load $str] && [llength [info commands $str]])} {
if {[lsearch -exact [interp aliases] $str] > -1} {
lappend types "alias"
} elseif {
[llength [info procs $str]] ||
([string match *::* $str] &&
[llength [namespace eval [namespace qualifier $str] \
info procs [namespace tail $str]]])
} {
lappend types "procedure"
} else {
lappend types "command"
}
}
if {[llength [uplevel 1 info vars $str]]} {
lappend types "variable"
}
if {[file isdirectory $str]} {
lappend types "directory"
}
if {[file isfile $str]} {
lappend types "file"
}
if {[llength [info commands winfo]] && [winfo exists $str]} {
lappend types "widget"
}
if {[string compare {} [auto_execok $str]]} {
lappend types "executable"
}
return $types
}
## dir - directory list
# ARGS: args - names/glob patterns of directories to list
# OPTS: -all - list hidden files as well (Unix dot files)
# -long - list in full format "permissions size date filename"
# -full - displays / after directories and link paths for links
# Returns: a directory listing
##
proc dir {args} {
array set s {
all 0 full 0 long 0
0 --- 1 --x 2 -w- 3 -wx 4 r-- 5 r-x 6 rw- 7 rwx
}
while {[string match \-* [lindex $args 0]]} {
set str [lindex $args 0]
set args [lreplace $args 0 0]
switch -glob -- $str {
-a* {set s(all) 1} -f* {set s(full) 1}
-l* {set s(long) 1} -- break
default {
return -code error "unknown option \"$str\",\
should be one of: -all, -full, -long"
}
}
}
set sep [string trim [file join . .] .]
if {![llength $args]} { set args . }
foreach arg $args {
if {[file isdir $arg]} {
set arg [string trimright $arg $sep]$sep
if {$s(all)} {
lappend out [list $arg [lsort [glob -nocomplain -- $arg.* $arg*]]]
} else {
lappend out [list $arg [lsort [glob -nocomplain -- $arg*]]]
}
} else {
lappend out [list [file dirname $arg]$sep \
[lsort [glob -nocomplain -- $arg]]]
}
}
if {$s(long)} {
set old [clock scan {1 year ago}]
set fmt "%s%9d %s %s\n"
foreach o $out {
set d [lindex $o 0]
append res $d:\n
foreach f [lindex $o 1] {
file lstat $f st
set f [file tail $f]
if {$s(full)} {
switch -glob $st(type) {
d* { append f $sep }
l* { append f "@ -> [file readlink $d$sep$f]" }
default { if {[file exec $d$sep$f]} { append f * } }
}
}
if {[string match file $st(type)]} {
set mode -
} else {
set mode [string index $st(type) 0]
}
foreach j [split [format %03o [expr {$st(mode)&0777}]] {}] {
append mode $s($j)
}
if {$st(mtime)>$old} {
set cfmt {%b %d %H:%M}
} else {
set cfmt {%b %d %Y}
}
append res [format $fmt $mode $st(size) \
[clock format $st(mtime) -format $cfmt] $f]
}
append res \n
}
} else {
foreach o $out {
set d [lindex $o 0]
append res "$d:\n"
set i 0
foreach f [lindex $o 1] {
if {[string len [file tail $f]] > $i} {
set i [string len [file tail $f]]
}
}
set i [expr {$i+2+$s(full)}]
set j 80
## This gets the number of cols in the tkcon console widget
if {[llength [info commands tkcon]]} {
set j [expr {[tkcon master set ::tkcon::OPT(cols)]/$i}]
}
set k 0
foreach f [lindex $o 1] {
set f [file tail $f]
if {$s(full)} {
switch -glob [file type $d$sep$f] {
d* { append f $sep }
l* { append f @ }
default { if {[file exec $d$sep$f]} { append f * } }
}
}
append res [format "%-${i}s" $f]
if {[incr k]%$j == 0} {set res [string trimright $res]\n}
}
append res \n\n
}
}
return [string trimright $res]
}
interp alias {} ::ls {} ::dir -full
## lremove - remove items from a list
# OPTS:
# -all remove all instances of each item
# -glob remove all instances matching glob pattern
# -regexp remove all instances matching regexp pattern
# ARGS: l a list to remove items from
# args items to remove (these are 'join'ed together)
##
proc lremove {args} {
array set opts {-all 0 pattern -exact}
while {[string match -* [lindex $args 0]]} {
switch -glob -- [lindex $args 0] {
-a* { set opts(-all) 1 }
-g* { set opts(pattern) -glob }
-r* { set opts(pattern) -regexp }
-- { set args [lreplace $args 0 0]; break }
default {return -code error "unknown option \"[lindex $args 0]\""}
}
set args [lreplace $args 0 0]
}
set l [lindex $args 0]
foreach i [join [lreplace $args 0 0]] {
if {[set ix [lsearch $opts(pattern) $l $i]] == -1} continue
set l [lreplace $l $ix $ix]
if {$opts(-all)} {
while {[set ix [lsearch $opts(pattern) $l $i]] != -1} {
set l [lreplace $l $ix $ix]
}
}
}
return $l
}
if {!$::tkcon::PRIV(WWW)} {;
## Unknown changed to get output into tkcon window
# unknown:
# Invoked automatically whenever an unknown command is encountered.
# Works through a list of "unknown handlers" that have been registered
# to deal with unknown commands. Extensions can integrate their own
# handlers into the 'unknown' facility via 'unknown_handler'.
#
# If a handler exists that recognizes the command, then it will
# take care of the command action and return a valid result or a
# Tcl error. Otherwise, it should return "-code continue" (=2)
# and responsibility for the command is passed to the next handler.
#
# Arguments:
# args - A list whose elements are the words of the original
# command, including the command name.
proc unknown args {
global unknown_handler_order unknown_handlers errorInfo errorCode
#
# Be careful to save error info now, and restore it later
# for each handler. Some handlers generate their own errors
# and disrupt handling.
#
set savedErrorCode $errorCode
set savedErrorInfo $errorInfo
if {![info exists unknown_handler_order] || \
![info exists unknown_handlers]} {
set unknown_handlers(tcl) tcl_unknown
set unknown_handler_order tcl
}
foreach handler $unknown_handler_order {
set status [catch {uplevel 1 $unknown_handlers($handler) $args} result]
if {$status == 1} {
#
# Strip the last five lines off the error stack (they're
# from the "uplevel" command).
#
set new [split $errorInfo \n]
set new [join [lrange $new 0 [expr {[llength $new]-6}]] \n]
return -code $status -errorcode $errorCode \
-errorinfo $new $result
} elseif {$status != 4} {
return -code $status $result
}
set errorCode $savedErrorCode
set errorInfo $savedErrorInfo
}
set name [lindex $args 0]
return -code error "invalid command name \"$name\""
}
# tcl_unknown:
# Invoked when a Tcl command is invoked that doesn't exist in the
# interpreter:
#
# 1. See if the autoload facility can locate the command in a
# Tcl script file. If so, load it and execute it.
# 2. If the command was invoked interactively at top-level:
# (a) see if the command exists as an executable UNIX program.
# If so, "exec" the command.
# (b) see if the command requests csh-like history substitution
# in one of the common forms !!, !<number>, or ^old^new. If
# so, emulate csh's history substitution.
# (c) see if the command is a unique abbreviation for another
# command. If so, invoke the command.
#
# Arguments:
# args - A list whose elements are the words of the original
# command, including the command name.
proc tcl_unknown args {
global auto_noexec auto_noload env unknown_pending tcl_interactive
global errorCode errorInfo
# If the command word has the form "namespace inscope ns cmd"
# then concatenate its arguments onto the end and evaluate it.
set cmd [lindex $args 0]
if {[regexp "^namespace\[ \t\n\]+inscope" $cmd] && [llength $cmd] == 4} {
set arglist [lrange $args 1 end]
set ret [catch {uplevel 1 $cmd $arglist} result]
if {$ret == 0} {
return $result
} else {
return -code $ret -errorcode $errorCode $result
}
}
# Save the values of errorCode and errorInfo variables, since they
# may get modified if caught errors occur below. The variables will
# be restored just before re-executing the missing command.
set savedErrorCode $errorCode
set savedErrorInfo $errorInfo
set name [lindex $args 0]
if {![info exists auto_noload]} {
#
# Make sure we're not trying to load the same proc twice.
#
if {[info exists unknown_pending($name)]} {
return -code error "self-referential recursion in \"unknown\" for command \"$name\""
}
set unknown_pending($name) pending
if {[llength [info args auto_load]]==1} {
set ret [catch {auto_load $name} msg]
} else {
set ret [catch {auto_load $name [uplevel 1 {namespace current}]} msg]
}
unset unknown_pending($name)
if {$ret} {
return -code $ret -errorcode $errorCode \
"error while autoloading \"$name\": $msg"
}
if {![array size unknown_pending]} { unset unknown_pending }
if {$msg} {
set errorCode $savedErrorCode
set errorInfo $savedErrorInfo
set code [catch {uplevel 1 $args} msg]
if {$code == 1} {
#
# Strip the last five lines off the error stack (they're
# from the "uplevel" command).
#
set new [split $errorInfo \n]
set new [join [lrange $new 0 [expr {[llength $new]-6}]] \n]
return -code error -errorcode $errorCode \
-errorinfo $new $msg
} else {
return -code $code $msg
}
}
}
if {[info level] == 1 && [string match {} [info script]] \
&& [info exists tcl_interactive] && $tcl_interactive} {
if {![info exists auto_noexec]} {
set new [auto_execok $name]
if {[string compare {} $new]} {
set errorCode $savedErrorCode
set errorInfo $savedErrorInfo
# set FH [uplevel [list open [concat |$new [lrange $args 1 end] 2>@stdout ] r]]
# while {![eof $FH]} { puts [gets $FH] }
# catch {close $FH} ERR
# return
return [uplevel 1 exec $new [lrange $args 1 end]]
#return [uplevel exec >&@stdout <@stdin $new [lrange $args 1 end]]
}
}
set errorCode $savedErrorCode
set errorInfo $savedErrorInfo
##
## History substitution moved into ::tkcon::EvalCmd
##
if {[string compare $name "::"] == 0} {
set name ""
}
if {$ret != 0} {
return -code $ret -errorcode $errorCode \
"error in unknown while checking if \"$name\" is a unique command abbreviation: $msg"
}
set cmds [info commands $name*]
if {[llength $cmds] == 1} {
return [uplevel 1 [lreplace $args 0 0 $cmds]]
}
if {[llength $cmds]} {
if {$name == ""} {
return -code error "empty command name \"\""
} else {
return -code error \
"ambiguous command name \"$name\": [lsort $cmds]"
}
}
## We've got nothing so far
## Check and see if Tk wasn't loaded, but it appears to be a Tk cmd
if {![uplevel \#0 info exists tk_version]} {
lappend tkcmds bell bind bindtags button \
canvas checkbutton clipboard destroy \
entry event focus font frame grab grid image \
label listbox lower menu menubutton message \
option pack place radiobutton raise \
scale scrollbar selection send spinbox \
text tk tkwait toplevel winfo wm
if {[lsearch -exact $tkcmds $name] >= 0 && \
[tkcon master tk_messageBox -icon question -parent . \
-title "Load Tk?" -type retrycancel -default retry \
-message "This appears to be a Tk command, but Tk\
has not yet been loaded. Shall I retry the command\
with loading Tk first?"] == "retry"} {
return [uplevel 1 "load {} Tk; $args"]
}
}
}
return -code continue
}
} ; # end exclusionary code for WWW
## read the user based init-files
if {[info exists env(TK_CON_COMMAND)] && [ file exists $env(TK_CON_COMMAND) ]} {
uplevel #0 source $env(TK_CON_COMMAND)
}
if {[info exists env(TK_CON_LOCAL)] && [ file exists $env(TK_CON_LOCAL) ]} {
uplevel #0 source $env(TK_CON_LOCAL)
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.