-
Notifications
You must be signed in to change notification settings - Fork 17
/
tkcon.tcl
executable file
·5325 lines (5032 loc) · 159 KB
/
tkcon.tcl
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
#!/bin/sh
# \
exec ${QROUTER_WISH:=wish} "$0" ${1+"$@"}
#
## tkcon.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 <[email protected]>, Jan Nijtmans <[email protected]>
## Crimmins <[email protected]>, Wart <[email protected]>
##
## Copyright 1995-2001 Jeffrey Hobbs
## Initiated: Thu Aug 17 15:36:47 PDT 1995
##
##
## source standard_disclaimer.tcl
## source bourbon_ware.tcl
##
# Proxy support for retrieving the current version of Tkcon.
#
# Mon Jun 25 12:19:56 2001 - Pat Thoyts <[email protected]>
#
# In your tkcon.cfg or .tkconrc file put your proxy details into the
# `proxy' member of the `PRIV' array. e.g.:
#
# set ::tkcon::PRIV(proxy) wwwproxy:8080
#
# If you want to be prompted for proxy authentication details (eg for
# an NT proxy server) make the second element of this variable non-nil - eg:
#
# set ::tkcon::PRIV(proxy) {wwwproxy:8080 1}
#
# Or you can set the above variable from within tkcon by calling
#
# tkcon master set ::tkcon:PRIV(proxy) wwwproxy:8080
#
if {$tcl_version < 8.0} {
return -code error "tkcon requires at least Tcl/Tk8"
} else {
package require Tk $tcl_version
}
catch {package require bogus-package-name}
foreach pkg [info loaded {}] {
set file [lindex $pkg 0]
set name [lindex $pkg 1]
if {![catch {set version [package require $name]}]} {
if {[string match {} [package ifneeded $name $version]]} {
package ifneeded $name $version [list load $file $name]
}
}
}
catch {unset pkg file name version}
# Tk 8.4 makes previously exposed stuff private.
# FIX: Update tkcon to not rely on the private Tk code.
#
if {![llength [info globals tkPriv]]} {
::tk::unsupported::ExposePrivateVariable tkPriv
}
foreach cmd {SetCursor UpDownLine Transpose ScrollPages} {
if {![llength [info commands tkText$cmd]]} {
::tk::unsupported::ExposePrivateCommand tkText$cmd
}
}
# Initialize the ::tkcon namespace
#
namespace eval ::tkcon {
# The OPT variable is an array containing most of the optional
# info to configure. COLOR has the color data.
variable OPT
variable COLOR
# PRIV is used for internal data that only tkcon should fiddle with.
variable PRIV
set PRIV(WWW) [info exists embed_args]
}
## ::tkcon::Init - inits tkcon
#
# Calls: ::tkcon::InitUI
# Outputs: errors found in tkcon's resource file
##
proc ::tkcon::Init {} {
variable OPT
variable COLOR
variable PRIV
global tcl_platform env argc argv tcl_interactive errorInfo
if {![info exists argv]} {
set argv {}
set argc 0
}
set tcl_interactive 1
if {[info exists PRIV(name)]} {
set title $PRIV(name)
} else {
MainInit
# some main initialization occurs later in this proc,
# to go after the UI init
set MainInit 1
set title Main
}
##
## When setting up all the default values, we always check for
## prior existence. This allows users who embed tkcon to modify
## the initial state before tkcon initializes itself.
##
# bg == {} will get bg color from the main toplevel (in InitUI)
foreach {key default} {
bg {}
blink \#FFFF00
cursor \#000000
disabled \#4D4D4D
proc \#008800
var \#FFC0D0
prompt \#8F4433
stdin \#000000
stdout \#0000FF
stderr \#FF0000
} {
if {![info exists COLOR($key)]} { set COLOR($key) $default }
}
foreach {key default} {
autoload {}
blinktime 500
blinkrange 1
buffer 512
calcmode 0
cols 80
debugPrompt {(level \#$level) debug [history nextid] > }
dead {}
expandorder {Pathname Variable Procname}
font {}
history 48
hoterrors 1
library {}
lightbrace 1
lightcmd 1
maineval {}
maxmenu 15
nontcl 0
prompt1 {ignore this, it's set below}
rows 20
scrollypos right
showmenu 1
showmultiple 1
showstatusbar 0
slaveeval {}
slaveexit close
subhistory 1
gc-delay 60000
gets {congets}
usehistory 1
exec slave
} {
if {![info exists OPT($key)]} { set OPT($key) $default }
}
foreach {key default} {
app {}
appname {}
apptype slave
namesp ::
cmd {}
cmdbuf {}
cmdsave {}
event 1
deadapp 0
deadsock 0
debugging 0
displayWin .
histid 0
find {}
find,case 0
find,reg 0
errorInfo {}
showOnStartup 1
slavealias { edit more less tkcon }
slaveprocs {
alias clear dir dump echo idebug lremove
tkcon_puts tkcon_gets observe observe_var unalias which what
}
version 2.3
RCS {RCS: @(#) $Id: tkcon.tcl,v 1.1.1.1 2011/04/10 21:15:05 tim Exp $}
HEADURL {http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/tkcon/tkcon/tkcon.tcl?rev=HEAD}
docs "http://tkcon.sourceforge.net/"
email {[email protected]}
root .
} {
if {![info exists PRIV($key)]} { set PRIV($key) $default }
}
## NOTES FOR STAYING IN PRIMARY INTERPRETER:
##
## If you set ::tkcon::OPT(exec) to {}, then instead of a multiple
## interp model, you get tkcon operating in the main interp by default.
## This can be useful when attaching to programs that like to operate
## in the main interpter (for example, based on special wish'es).
## You can set this from the command line with -exec ""
## A side effect is that all tkcon command line args will be used
## by the first console only.
#set OPT(exec) {}
if {$PRIV(WWW)} {
lappend PRIV(slavealias) history
set OPT(prompt1) {[history nextid] % }
} else {
lappend PRIV(slaveprocs) tcl_unknown unknown
set OPT(prompt1) {([file tail [pwd]]) [history nextid] % }
}
## If we are using the default '.' toplevel, and there appear to be
## children of '.', then make sure we use a disassociated toplevel.
if {$PRIV(root) == "." && [llength [winfo children .]]} {
set PRIV(root) .tkcon
}
## Do platform specific configuration here, other than defaults
### Use tkcon.cfg filename for resource filename on non-unix systems
### Determine what directory the resource file should be in
switch $tcl_platform(platform) {
macintosh {
if {![interp issafe]} {cd [file dirname [info script]]}
set envHome PREF_FOLDER
set rcfile tkcon.cfg
set histfile qrouter_tkcon.hst
catch {console hide}
}
windows {
set envHome HOME
set rcfile tkcon.cfg
set histfile qrouter_tkcon.hst
}
unix {
set envHome HOME
set rcfile .tkconrc
set histfile .qrouter_tkcon_hst
}
}
if {[info exists env($envHome)]} {
if {![info exists PRIV(rcfile)]} {
set PRIV(rcfile) [file join $env($envHome) $rcfile]
}
if {![info exists PRIV(histfile)]} {
set PRIV(histfile) [file join $env($envHome) $histfile]
}
}
## Handle command line arguments before sourcing resource file to
## find if resource file is being specified (let other args pass).
if {[set i [lsearch -exact $argv -rcfile]] != -1} {
set PRIV(rcfile) [lindex $argv [incr i]]
}
if {!$PRIV(WWW) && [file exists $PRIV(rcfile)]} {
set code [catch {uplevel \#0 [list source $PRIV(rcfile)]} err]
}
if {[info exists env(TK_CON_LIBRARY)]} {
lappend ::auto_path $env(TK_CON_LIBRARY)
} else {
lappend ::auto_path $OPT(library)
}
if {![info exists ::tcl_pkgPath]} {
set dir [file join [file dirname [info nameofexec]] lib]
if {[llength [info commands @scope]]} {
set dir [file join $dir itcl]
}
catch {source [file join $dir pkgIndex.tcl]}
}
catch {tclPkgUnknown dummy-name dummy-version}
## Handle rest of command line arguments after sourcing resource file
## and slave is created, but before initializing UI or setting packages.
set slaveargs {}
set slavefiles {}
set truth {^(1|yes|true|on)$}
for {set i 0} {$i < $argc} {incr i} {
set arg [lindex $argv $i]
if {[string match {-*} $arg]} {
set val [lindex $argv [incr i]]
## Handle arg based options
switch -glob -- $arg {
-- - -argv {
set argv [concat -- [lrange $argv $i end]]
set argc [llength $argv]
break
}
-color-* { set COLOR([string range $arg 7 end]) $val }
-exec { set OPT(exec) $val }
-main - -e - -eval { append OPT(maineval) \n$val\n }
-package - -load { lappend OPT(autoload) $val }
-slave { append OPT(slaveeval) \n$val\n }
-nontcl { set OPT(nontcl) [regexp -nocase $truth $val]}
-root { set PRIV(root) $val }
-font { set OPT(font) $val }
-rcfile {}
default { lappend slaveargs $arg; incr i -1 }
}
} elseif {[file isfile $arg]} {
lappend slavefiles $arg
} else {
lappend slaveargs $arg
}
}
## Create slave executable
if {[string compare {} $OPT(exec)]} {
uplevel \#0 ::tkcon::InitSlave $OPT(exec) $slaveargs
} else {
set argc [llength $slaveargs]
set argv $slaveargs
uplevel \#0 $slaveargs
}
## Attach to the slave, EvalAttached will then be effective
Attach $PRIV(appname) $PRIV(apptype)
InitUI $title
## swap puts and gets with the tkcon versions to make sure all
## input and output is handled by tkcon
if {![catch {rename ::puts ::tkcon_tcl_puts}]} {
interp alias {} ::puts {} ::tkcon_puts
}
if {($OPT(gets) != "") && ![catch {rename ::gets ::tkcon_tcl_gets}]} {
interp alias {} ::gets {} ::tkcon_gets
}
EvalSlave history keep $OPT(history)
if {[info exists MainInit]} {
# Source history file only for the main console, as all slave
# consoles will adopt from the main's history, but still
# keep separate histories
if {!$PRIV(WWW) && $OPT(usehistory) && [file exists $PRIV(histfile)]} {
puts -nonewline "loading history file ... "
# The history file is built to be loaded in and
# understood by tkcon
if {[catch {uplevel \#0 [list source $PRIV(histfile)]} herr]} {
puts stderr "error:\n$herr"
append PRIV(errorInfo) $errorInfo\n
}
set PRIV(event) [EvalSlave history nextid]
puts "[expr {$PRIV(event)-1}] events added"
}
}
## Autoload specified packages in slave
set pkgs [EvalSlave package names]
foreach pkg $OPT(autoload) {
puts -nonewline "autoloading package \"$pkg\" ... "
if {[lsearch -exact $pkgs $pkg]>-1} {
if {[catch {EvalSlave package require [list $pkg]} pkgerr]} {
puts stderr "error:\n$pkgerr"
append PRIV(errorInfo) $errorInfo\n
} else { puts "OK" }
} else {
puts stderr "error: package does not exist"
}
}
## Evaluate maineval in slave
if {[string compare {} $OPT(maineval)] && \
[catch {uplevel \#0 $OPT(maineval)} merr]} {
puts stderr "error in eval:\n$merr"
append PRIV(errorInfo) $errorInfo\n
}
## Source extra command line argument files into slave executable
foreach fn $slavefiles {
puts -nonewline "slave sourcing \"$fn\" ... "
if {[catch {EvalSlave source [list $fn]} fnerr]} {
puts stderr "error:\n$fnerr"
append PRIV(errorInfo) $errorInfo\n
} else { puts "OK" }
}
## Evaluate slaveeval in slave
if {[string compare {} $OPT(slaveeval)] && \
[catch {interp eval $OPT(exec) $OPT(slaveeval)} serr]} {
puts stderr "error in slave eval:\n$serr"
append PRIV(errorInfo) $errorInfo\n
}
## Output any error/output that may have been returned from rcfile
if {[info exists code] && $code && [string compare {} $err]} {
puts stderr "error in $PRIV(rcfile):\n$err"
append PRIV(errorInfo) $errorInfo
}
if {[string compare {} $OPT(exec)]} {
StateCheckpoint [concat $PRIV(name) $OPT(exec)] slave
}
StateCheckpoint $PRIV(name) slave
Prompt "$title console display active (Tcl$::tcl_patchLevel / Tk$::tk_patchLevel)\n"
}
## ::tkcon::InitSlave - inits the slave by placing key procs and aliases in it
## It's arg[cv] are based on passed in options, while argv0 is the same as
## the master. tcl_interactive is the same as the master as well.
# ARGS: slave - name of slave to init. If it does not exist, it is created.
# args - args to pass to a slave as argv/argc
##
proc ::tkcon::InitSlave {slave args} {
variable OPT
variable COLOR
variable PRIV
global argv0 tcl_interactive tcl_library env auto_path
if {[string match {} $slave]} {
return -code error "Don't init the master interpreter, goofball"
}
if {![interp exists $slave]} { interp create $slave }
if {[interp eval $slave info command source] == ""} {
$slave alias source SafeSource $slave
$slave alias load SafeLoad $slave
$slave alias open SafeOpen $slave
$slave alias file file
interp eval $slave [dump var -nocomplain tcl_library auto_path env]
interp eval $slave { catch {source [file join $tcl_library init.tcl]} }
interp eval $slave { catch unknown }
}
$slave alias exit exit
interp eval $slave {
# Do package require before changing around puts/gets
catch {package require bogus-package-name}
catch {rename ::puts ::tkcon_tcl_puts}
}
foreach cmd $PRIV(slaveprocs) { $slave eval [dump proc $cmd] }
foreach cmd $PRIV(slavealias) { $slave alias $cmd $cmd }
interp alias $slave ::ls $slave ::dir -full
interp alias $slave ::puts $slave ::tkcon_puts
if {$OPT(gets) != ""} {
interp eval $slave { catch {rename ::gets ::tkcon_tcl_gets} }
interp alias $slave ::gets $slave ::tkcon_gets
}
if {[info exists argv0]} {interp eval $slave [list set argv0 $argv0]}
interp eval $slave set tcl_interactive $tcl_interactive \; \
set auto_path [list $auto_path] \; \
set argc [llength $args] \; \
set argv [list $args] \; {
if {![llength [info command bgerror]]} {
proc bgerror err {
global errorInfo
set body [info body bgerror]
rename ::bgerror {}
if {[auto_load bgerror]} { return [bgerror $err] }
proc bgerror err $body
tkcon bgerror $err $errorInfo
}
}
}
foreach pkg [lremove [package names] Tcl] {
foreach v [package versions $pkg] {
interp eval $slave [list package ifneeded $pkg $v \
[package ifneeded $pkg $v]]
}
}
}
## ::tkcon::InitInterp - inits an interpreter by placing key
## procs and aliases in it.
# ARGS: name - interp name
# type - interp type (slave|interp)
##
proc ::tkcon::InitInterp {name type} {
variable OPT
variable PRIV
## Don't allow messing up a local master interpreter
if {[string match namespace $type] || ([string match slave $type] && \
[regexp {^([Mm]ain|Slave[0-9]+)$} $name])} return
set old [Attach]
set oldname $PRIV(namesp)
catch {
Attach $name $type
EvalAttached { catch {rename ::puts ::tkcon_tcl_puts} }
foreach cmd $PRIV(slaveprocs) { EvalAttached [dump proc $cmd] }
switch -exact $type {
slave {
foreach cmd $PRIV(slavealias) {
Main interp alias $name ::$cmd $PRIV(name) ::$cmd
}
}
interp {
set thistkcon [tk appname]
foreach cmd $PRIV(slavealias) {
EvalAttached "proc $cmd args { send [list $thistkcon] $cmd \$args }"
}
}
}
## Catch in case it's a 7.4 (no 'interp alias') interp
EvalAttached {
catch {interp alias {} ::ls {} ::dir -full}
if {[catch {interp alias {} ::puts {} ::tkcon_puts}]} {
catch {rename ::tkcon_puts ::puts}
}
}
if {$OPT(gets) != ""} {
EvalAttached {
catch {rename ::gets ::tkcon_tcl_gets}
if {[catch {interp alias {} ::gets {} ::tkcon_gets}]} {
catch {rename ::tkcon_gets ::gets}
}
}
}
return
} {err}
eval Attach $old
AttachNamespace $oldname
if {[string compare {} $err]} { return -code error $err }
}
## ::tkcon::InitUI - inits UI portion (console) of tkcon
## Creates all elements of the console window and sets up the text tags
# ARGS: root - widget pathname of the tkcon console root
# title - title for the console root and main (.) windows
# Calls: ::tkcon::InitMenus, ::tkcon::Prompt
##
proc ::tkcon::InitUI {title} {
variable OPT
variable PRIV
variable COLOR
set root $PRIV(root)
if {[string match . $root]} { set w {} } else { set w [toplevel $root] }
if {!$PRIV(WWW)} {
wm withdraw $root
wm protocol $root WM_DELETE_WINDOW exit
}
set PRIV(base) $w
## Text Console
set PRIV(console) [set con $w.text]
text $con -wrap char -yscrollcommand [list $w.sy set] \
-foreground $COLOR(stdin) \
-insertbackground $COLOR(cursor)
$con mark set output 1.0
$con mark set limit 1.0
if {[string compare {} $COLOR(bg)]} {
$con configure -background $COLOR(bg)
}
set COLOR(bg) [$con cget -background]
if {[string compare {} $OPT(font)]} {
## Set user-requested font, if any
$con configure -font $OPT(font)
} else {
## otherwise make sure the font is monospace
set font [$con cget -font]
if {![font metrics $font -fixed]} {
font create tkconfixed -family Courier -size 12
$con configure -font tkconfixed
}
}
set OPT(font) [$con cget -font]
if {!$PRIV(WWW)} {
$con configure -setgrid 1 -width $OPT(cols) -height $OPT(rows)
}
bindtags $con [list $con TkConsole TkConsolePost $root all]
## Menus
## catch against use in plugin
if {[catch {menu $w.mbar} PRIV(menubar)]} {
set PRIV(menubar) [frame $w.mbar -relief raised -bd 1]
}
## Scrollbar
set PRIV(scrolly) [scrollbar $w.sy -takefocus 0 -bd 1 \
-command [list $con yview]]
InitMenus $PRIV(menubar) $title
Bindings
if {$OPT(showmenu)} {
$root configure -menu $PRIV(menubar)
}
pack $w.sy -side $OPT(scrollypos) -fill y
pack $con -fill both -expand 1
set PRIV(statusbar) [set sbar [frame $w.sbar]]
label $sbar.attach -relief sunken -bd 1 -anchor w \
-textvariable ::tkcon::PRIV(StatusAttach)
label $sbar.mode -relief sunken -bd 1 -anchor w \
-textvariable ::tkcon::PRIV(StatusMode)
label $sbar.cursor -relief sunken -bd 1 -anchor w -width 6 \
-textvariable ::tkcon::PRIV(StatusCursor)
grid $sbar.attach $sbar.mode $sbar.cursor -sticky news -padx 1
grid columnconfigure $sbar 0 -weight 1
grid columnconfigure $sbar 1 -weight 1
grid columnconfigure $sbar 2 -weight 0
if {$OPT(showstatusbar)} {
pack $sbar -side bottom -fill x -before $::tkcon::PRIV(scrolly)
}
foreach col {prompt stdout stderr stdin proc} {
$con tag configure $col -foreground $COLOR($col)
}
$con tag configure var -background $COLOR(var)
$con tag raise sel
$con tag configure blink -background $COLOR(blink)
$con tag configure find -background $COLOR(blink)
if {!$PRIV(WWW)} {
wm title $root "tkcon $PRIV(version) $title"
bind $con <Configure> {
scan [wm geometry [winfo toplevel %W]] "%%dx%%d" \
::tkcon::OPT(cols) ::tkcon::OPT(rows)
}
if {$PRIV(showOnStartup)} { wm deiconify $root }
}
if {$PRIV(showOnStartup)} { focus -force $PRIV(console) }
if {$OPT(gc-delay)} {
after $OPT(gc-delay) ::tkcon::GarbageCollect
}
}
## ::tkcon::GarbageCollect - do various cleanup ops periodically to our setup
##
proc ::tkcon::GarbageCollect {} {
variable OPT
variable PRIV
set w $PRIV(console)
## Remove error tags that no longer span anything
## Make sure the tag pattern matches the unique tag prefix
foreach tag [$w tag names] {
if {[string match _tag* $tag] && ![llength [$w tag ranges $tag]]} {
$w tag delete $tag
}
}
if {$OPT(gc-delay)} {
after $OPT(gc-delay) ::tkcon::GarbageCollect
}
}
## ::tkcon::Eval - evaluates commands input into console window
## This is the first stage of the evaluating commands in the console.
## They need to be broken up into consituent commands (by ::tkcon::CmdSep) in
## case a multiple commands were pasted in, then each is eval'ed (by
## ::tkcon::EvalCmd) in turn. Any uncompleted command will not be eval'ed.
# ARGS: w - console text widget
# Calls: ::tkcon::CmdGet, ::tkcon::CmdSep, ::tkcon::EvalCmd
##
proc ::tkcon::Eval {w} {
set incomplete [CmdSep [CmdGet $w] cmds last]
$w mark set insert end-1c
$w insert end \n
if {[llength $cmds]} {
foreach c $cmds {EvalCmd $w $c}
$w insert insert $last {}
} elseif {!$incomplete} {
EvalCmd $w $last
}
$w see insert
}
## ::tkcon::EvalCmd - evaluates a single command, adding it to history
# ARGS: w - console text widget
# cmd - the command to evaluate
# Calls: ::tkcon::Prompt
# Outputs: result of command to stdout (or stderr if error occured)
# Returns: next event number
##
proc ::tkcon::EvalCmd {w cmd} {
variable OPT
variable PRIV
$w mark set output end
if {[string compare {} $cmd]} {
set code 0
if {$OPT(subhistory)} {
set ev [EvalSlave history nextid]
incr ev -1
if {[string match !! $cmd]} {
set code [catch {EvalSlave history event $ev} cmd]
if {!$code} {$w insert output $cmd\n stdin}
} elseif {[regexp {^!(.+)$} $cmd dummy event]} {
## Check last event because history event is broken
set code [catch {EvalSlave history event $ev} cmd]
if {!$code && ![string match ${event}* $cmd]} {
set code [catch {EvalSlave history event $event} cmd]
}
if {!$code} {$w insert output $cmd\n stdin}
} elseif {[regexp {^\^([^^]*)\^([^^]*)\^?$} $cmd dummy old new]} {
set code [catch {EvalSlave history event $ev} cmd]
if {!$code} {
regsub -all -- $old $cmd $new cmd
$w insert output $cmd\n stdin
}
} elseif {$OPT(calcmode) && ![catch {expr $cmd} err]} {
EvalSlave history add $cmd
set cmd $err
set code -1
}
}
if {$code} {
$w insert output $cmd\n stderr
} else {
## We are about to evaluate the command, so move the limit
## mark to ensure that further <Return>s don't cause double
## evaluation of this command - for cases like the command
## has a vwait or something in it
$w mark set limit end
if {$OPT(nontcl) && [string match interp $PRIV(apptype)]} {
set code [catch {EvalSend $cmd} res]
if {$code == 1} {
set PRIV(errorInfo) "Non-Tcl errorInfo not available"
}
} elseif {[string match socket $PRIV(apptype)]} {
set code [catch {EvalSocket $cmd} res]
if {$code == 1} {
set PRIV(errorInfo) "Socket-based errorInfo not available"
}
} else {
set code [catch {EvalAttached $cmd} res]
if {$code == 1} {
if {[catch {EvalAttached [list set errorInfo]} err]} {
set PRIV(errorInfo) "Error getting errorInfo:\n$err"
} else {
set PRIV(errorInfo) $err
}
}
}
EvalSlave history add $cmd
if {$code} {
if {$OPT(hoterrors)} {
set tag [UniqueTag $w]
$w insert output $res [list stderr $tag] \n stderr
$w tag bind $tag <Enter> \
[list $w tag configure $tag -underline 1]
$w tag bind $tag <Leave> \
[list $w tag configure $tag -underline 0]
$w tag bind $tag <ButtonRelease-1> \
"if {!\[info exists tkPriv(mouseMoved)\] || !\$tkPriv(mouseMoved)} \
{[list edit -attach [Attach] -type error -- $PRIV(errorInfo)]}"
} else {
$w insert output $res\n stderr
}
} elseif {[string compare {} $res]} {
$w insert output $res\n stdout
}
}
}
Prompt
set PRIV(event) [EvalSlave history nextid]
}
## ::tkcon::EvalSlave - evaluates the args in the associated slave
## args should be passed to this procedure like they would be at
## the command line (not like to 'eval').
# ARGS: args - the command and args to evaluate
##
proc ::tkcon::EvalSlave args {
interp eval $::tkcon::OPT(exec) $args
}
## ::tkcon::EvalOther - evaluate a command in a foreign interp or slave
## without attaching to it. No check for existence is made.
# ARGS: app - interp/slave name
# type - (slave|interp)
##
proc ::tkcon::EvalOther { app type args } {
if {[string compare slave $type]==0} {
return [Slave $app $args]
} else {
return [uplevel 1 send [list $app] $args]
}
}
## ::tkcon::EvalSend - sends the args to the attached interpreter
## Varies from 'send' by determining whether attachment is dead
## when an error is received
# ARGS: cmd - the command string to send across
# Returns: the result of the command
##
proc ::tkcon::EvalSend cmd {
variable OPT
variable PRIV
if {$PRIV(deadapp)} {
if {[lsearch -exact [winfo interps] $PRIV(app)]<0} {
return
} else {
set PRIV(appname) [string range $PRIV(appname) 5 end]
set PRIV(deadapp) 0
Prompt "\n\"$PRIV(app)\" alive\n" [CmdGet $PRIV(console)]
}
}
set code [catch {send -displayof $PRIV(displayWin) $PRIV(app) $cmd} result]
if {$code && [lsearch -exact [winfo interps] $PRIV(app)]<0} {
## Interpreter disappeared
if {[string compare leave $OPT(dead)] && \
([string match ignore $OPT(dead)] || \
[tk_dialog $PRIV(base).dead "Dead Attachment" \
"\"$PRIV(app)\" appears to have died.\
\nReturn to primary slave interpreter?" questhead 0 OK No])} {
set PRIV(appname) "DEAD:$PRIV(appname)"
set PRIV(deadapp) 1
} else {
set err "Attached Tk interpreter \"$PRIV(app)\" died."
Attach {}
set PRIV(deadapp) 0
EvalSlave set errorInfo $err
}
Prompt \n [CmdGet $PRIV(console)]
}
return -code $code $result
}
## ::tkcon::EvalSocket - sends the string to an interpreter attached via
## a tcp/ip socket
##
## In the EvalSocket case, ::tkcon::PRIV(app) is the socket id
##
## Must determine whether socket is dead when an error is received
# ARGS: cmd - the data string to send across
# Returns: the result of the command
##
proc ::tkcon::EvalSocket cmd {
variable OPT
variable PRIV
global tcl_version
if {$PRIV(deadapp)} {
if {![info exists PRIV(app)] || \
[catch {eof $PRIV(app)} eof] || $eof} {
return
} else {
set PRIV(appname) [string range $PRIV(appname) 5 end]
set PRIV(deadapp) 0
Prompt "\n\"$PRIV(app)\" alive\n" [CmdGet $PRIV(console)]
}
}
# Sockets get \'s interpreted, so that users can
# send things like \n\r or explicit hex values
set cmd [subst -novariables -nocommands $cmd]
#puts [list $PRIV(app) $cmd]
set code [catch {puts $PRIV(app) $cmd ; flush $PRIV(app)} result]
if {$code && [eof $PRIV(app)]} {
## Interpreter died or disappeared
puts "$code eof [eof $PRIV(app)]"
EvalSocketClosed
}
return -code $code $result
}
## ::tkcon::EvalSocketEvent - fileevent command for an interpreter attached
## via a tcp/ip socket
## Must determine whether socket is dead when an error is received
# ARGS: args - the args to send across
# Returns: the result of the command
##
proc ::tkcon::EvalSocketEvent {} {
variable PRIV
if {[gets $PRIV(app) line] == -1} {
if {[eof $PRIV(app)]} {
EvalSocketClosed
}
return
}
puts $line
}
## ::tkcon::EvalSocketClosed - takes care of handling a closed eval socket
##
# ARGS: args - the args to send across
# Returns: the result of the command
##
proc ::tkcon::EvalSocketClosed {} {
variable OPT
variable PRIV
catch {close $PRIV(app)}
if {[string compare leave $OPT(dead)] && \
([string match ignore $OPT(dead)] || \
[tk_dialog $PRIV(base).dead "Dead Attachment" \
"\"$PRIV(app)\" appears to have died.\
\nReturn to primary slave interpreter?" questhead 0 OK No])} {
set PRIV(appname) "DEAD:$PRIV(appname)"
set PRIV(deadapp) 1
} else {
set err "Attached Tk interpreter \"$PRIV(app)\" died."
Attach {}
set PRIV(deadapp) 0
EvalSlave set errorInfo $err
}
Prompt \n [CmdGet $PRIV(console)]
}
## ::tkcon::EvalNamespace - evaluates the args in a particular namespace
## This is an override for ::tkcon::EvalAttached for when the user wants
## to attach to a particular namespace of the attached interp
# ARGS: attached
# namespace the namespace to evaluate in
# args the args to evaluate
# RETURNS: the result of the command
##
proc ::tkcon::EvalNamespace { attached namespace args } {
if {[llength $args]} {
uplevel \#0 $attached \
[list [concat [list namespace eval $namespace] $args]]
}
}
## ::tkcon::Namespaces - return all the namespaces descendent from $ns
##
#
##
proc ::tkcon::Namespaces {{ns ::} {l {}}} {
if {[string compare {} $ns]} { lappend l $ns }
foreach i [EvalAttached [list namespace children $ns]] {
set l [Namespaces $i $l]
}
return $l
}
## ::tkcon::CmdGet - gets the current command from the console widget
# ARGS: w - console text widget
# Returns: text which compromises current command line
##
proc ::tkcon::CmdGet w {
if {![llength [$w tag nextrange prompt limit end]]} {
$w tag add stdin limit end-1c
return [$w get limit end-1c]
}
}
## ::tkcon::CmdSep - separates multiple commands into a list and remainder
# ARGS: cmd - (possible) multiple command to separate
# list - varname for the list of commands that were separated.
# last - varname of any remainder (like an incomplete final command).
# If there is only one command, it's placed in this var.
# Returns: constituent command info in varnames specified by list & rmd.
##
proc ::tkcon::CmdSep {cmd list last} {
upvar 1 $list cmds $last inc
set inc {}
set cmds {}
foreach c [split [string trimleft $cmd] \n] {
if {[string compare $inc {}]} {
append inc \n$c
} else {
append inc [string trimleft $c]
}
if {[info complete $inc] && ![regexp {[^\\]\\$} $inc]} {
if {[regexp "^\[^#\]" $inc]} {lappend cmds $inc}
set inc {}
}
}
set i [string compare $inc {}]
if {!$i && [string compare $cmds {}] && ![string match *\n $cmd]} {
set inc [lindex $cmds end]
set cmds [lreplace $cmds end end]
}
return $i
}
## ::tkcon::CmdSplit - splits multiple commands into a list
# ARGS: cmd - (possible) multiple command to separate
# Returns: constituent commands in a list
##
proc ::tkcon::CmdSplit {cmd} {
set inc {}
set cmds {}
foreach cmd [split [string trimleft $cmd] \n] {
if {[string compare {} $inc]} {
append inc \n$cmd
} else {
append inc [string trimleft $cmd]
}
if {[info complete $inc] && ![regexp {[^\\]\\$} $inc]} {
#set inc [string trimright $inc]
if {[regexp "^\[^#\]" $inc]} {lappend cmds $inc}
set inc {}
}
}
if {[regexp "^\[^#\]" $inc]} {lappend cmds $inc}
return $cmds
}
## ::tkcon::UniqueTag - creates a uniquely named tag, reusing names
## Called by ::tkcon::EvalCmd
# ARGS: w - text widget
# Outputs: tag name guaranteed unique in the widget