-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathembedded-tclobj.cc
1247 lines (1247 loc) · 27.1 KB
/
embedded-tclobj.cc
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
static char code[] = "\n\
Class InitObject\n\
\n\
Object instproc init-vars {args} {\n\
$self init-default-vars [$self info class]\n\
\n\
set shadow_args \"\"\n\
for {} {$args != \"\"} {set args [lrange $args 2 end]} {\n\
set key [lindex $args 0]\n\
set val [lindex $args 1]\n\
if {$val != \"\" && [string match {-[A-z]*} $key]} {\n\
set cmd [string range $key 1 end]\n\
if ![catch \"$self $cmd $val\"] {\n\
continue\n\
}\n\
}\n\
lappend shadow_args $key $val\n\
}\n\
return $shadow_args\n\
}\n\
\n\
Object instproc init-default-vars {classes} {\n\
foreach cl $classes {\n\
if {$cl == \"Object\"} continue\n\
$self init-default-vars \"[$cl info superclass]\"\n\
foreach var [$cl info vars] {\n\
if [catch \"$self set $var\"] {\n\
$self set $var [$cl set $var]\n\
}\n\
}\n\
}\n\
}\n\
\n\
\n\
\n\
Class SplitObject\n\
SplitObject set id 0\n\
\n\
SplitObject instproc init args {\n\
$self next\n\
if [catch \"$self create-shadow $args\"] {\n\
error \"__FAILED_SHADOW_OBJECT_\" \"\"\n\
}\n\
}\n\
\n\
SplitObject instproc set args {\n\
set var [lindex $args 0]\n\
$self instvar -parse-part1 $var\n\
if {[llength $args] == 1} {\n\
return [subst $[subst $var]]\n\
} else {\n\
return [set $var [lindex $args 1]]\n\
}\n\
}\n\
\n\
SplitObject instproc destroy {} {\n\
$self delete-shadow\n\
$self next\n\
}\n\
\n\
SplitObject proc getid {} {\n\
$self instvar id\n\
incr id\n\
return _o$id\n\
}\n\
\n\
SplitObject proc is-class cl {\n\
if [catch \"SplitObject info subclass $cl\" v] {\n\
return 0\n\
}\n\
return $v\n\
}\n\
\n\
SplitObject instproc unknown args {\n\
if [catch \"$self cmd $args\" ret] {\n\
set cls [$self info class]\n\
global errorInfo\n\
set savedInfo $errorInfo\n\
error \"error when calling class $cls: $args\" $savedInfo\n\
}\n\
return $ret\n\
}\n\
\n\
proc new { className args } {\n\
set o [SplitObject getid]\n\
if [catch \"$className create $o $args\" msg] {\n\
if [string match \"__FAILED_SHADOW_OBJECT_\" $msg] {\n\
delete $o\n\
return \"\"\n\
}\n\
global errorInfo\n\
error \"class $className: constructor failed: $msg\" $errorInfo\n\
}\n\
return $o\n\
}\n\
\n\
proc delete o {\n\
$o delete_tkvar\n\
$o destroy\n\
}\n\
\n\
SplitObject proc register className {\n\
set classes [split $className /]\n\
set parent SplitObject\n\
set path \"\"\n\
set sep \"\"\n\
foreach cl $classes {\n\
set path $path$sep$cl\n\
if ![$self is-class $path] {\n\
Class $path -superclass $parent\n\
}\n\
set sep /\n\
set parent $path\n\
}\n\
}\n\
\n\
SplitObject instproc warn-instvar item {\n\
$self instvar issuedWarning\n\
if ![info exists issuedWarning($item)] {\n\
set issuedWarning($item) 1\n\
puts stderr \"warning: no class variable $item\\n\"\n\
$self instvar SplitObject_issued_undeclared_warning\n\
if ![info exists SplitObject_issued_undeclared_warning] {\n\
puts stderr \"\\tsee tcl-object.tcl in tclcl for info about this warning.\\n\"\n\
set SplitObject_issued_undeclared_warning 1\n\
}\n\
}\n\
}\n\
\n\
SplitObject instproc init-instvar var {\n\
set cl [$self info class]\n\
while { \"$cl\" != \"\" && \"$cl\" != \"SplitObject\" } {\n\
foreach c $cl {\n\
if ![catch \"$c set $var\" val] {\n\
$self set $var $val\n\
return\n\
}\n\
}\n\
set parents \"\"\n\
foreach c $cl {\n\
if { $cl != \"SplitObject\" && $cl != \"Object\" } {\n\
set parents \"$parents [$c info superclass]\"\n\
}\n\
}\n\
set cl $parents\n\
}\n\
$self warn-instvar [$self info class]::$var\n\
}\n\
\n\
proc tkerror msg {\n\
global errorInfo\n\
puts -nonewline \"$msg: \"\n\
puts $errorInfo\n\
exit 1\n\
}\n\
\n\
proc bgerror msg {\n\
global errorInfo\n\
puts -nonewline \"$msg: \"\n\
puts $errorInfo\n\
exit 1\n\
}\n\
\n\
Object instproc public args {\n\
eval $self instproc $args\n\
}\n\
\n\
Object instproc private args {\n\
eval $self instproc $args\n\
}\n\
\n\
Object instproc proc.public args {\n\
eval $self proc $args\n\
}\n\
\n\
Object instproc proc.private args {\n\
eval $self proc $args\n\
}\n\
\n\
Object instproc proc.invoke { arglist body args } {\n\
$self proc invokeproc_ $arglist $body\n\
eval [list $self] invokeproc_ $args\n\
}\n\
\n\
\n\
\n\
Object instproc tkvar args {\n\
foreach var $args {\n\
if { [llength $var] > 1 } {\n\
set varname [lindex $var 1]\n\
set var [lindex $var 0]\n\
} else {\n\
set varname $var\n\
}\n\
uplevel upvar #0 $self/$var $varname\n\
}\n\
}\n\
\n\
Object instproc tkvarname var {\n\
return $self/$var\n\
}\n\
\n\
Object instproc delete_tkvar { } {\n\
set fullname [$self tkvarname foo]\n\
regexp \"(.*)foo$\" $fullname dummy prefix\n\
foreach global [info globals \"$prefix*\"] {\n\
global $global\n\
unset $global\n\
}\n\
}\n\
\n\
Object instproc info_tkvar { pattern } {\n\
set pattern [$self tkvarname $pattern]\n\
return [info globals $pattern]\n\
}\n\
\n\
proc TclObject args {\n\
return [eval SplitObject $args]\n\
}\n\
\n\
proc SplitObjectCompare {a b} {\n\
set o1 [string range $a 2 end]\n\
set o2 [string range $b 2 end]\n\
if {$o1 < $o2} {\n\
return -1\n\
} elseif {$o1 == $o2} {\n\
return 0\n\
} else {\n\
return 1\n\
}\n\
}\n\
\n\
Object instproc extract-var varname {\n\
set aidx [string first \"(\" $varname]\n\
if { $aidx >= 0 } {\n\
string range $varname 0 [incr aidx -1]\n\
} else {\n\
set varname\n\
}\n\
}\n\
\n\
Object instproc add-to-list {list elem} {\n\
$self instvar [$self extract-var $list]\n\
set ret 0\n\
if ![info exists $list] {\n\
set $list $elem\n\
set ret 1\n\
} elseif { [lsearch [set $list] $elem] < 0 } {\n\
lappend $list $elem\n\
set ret 1\n\
}\n\
set ret\n\
}\n\
\n\
Object instproc remove-from-list {list elem} {\n\
$self instvar [$self extract-var $list]\n\
set wtag \"$self: remove $elem from $list failed\"\n\
set ret 0\n\
if ![info exists $list] {\n\
warn \"$wtag: list does not exist\"\n\
} else {\n\
set k [lsearch [set $list] $elem]\n\
if { $k < 0 } {\n\
warn \"$wtag: element does not exist\"\n\
} else {\n\
set $list [lreplace [set $list] $k $k]\n\
set ret 1\n\
}\n\
}\n\
set ret\n\
}\n\
\n\
\n\
\n\
Class Import\n\
\n\
\n\
Import public init { } {\n\
$self next\n\
$self set use_http_cache_ 1\n\
}\n\
\n\
\n\
Import public import { args } {\n\
$self instvar import_dirs_ table_\n\
\n\
if { ![info exists import_dirs_] } {\n\
$self init_table\n\
}\n\
\n\
$self consistency_check\n\
\n\
\n\
foreach item $args {\n\
if [info exists table_($item)] {\n\
set file_list $table_($item)\n\
foreach file $table_($item) {\n\
if { [set msg [$self source_file $file]]!=\"\"} {\n\
error \"could not source $file for\\\n\
$item:\\n$msg\"\n\
}\n\
}\n\
} else {\n\
\n\
set list {}\n\
foreach dir $import_dirs_ {\n\
lappend list [$self file join $dir \\\n\
[$self class_to_file \\\n\
$item].mash]\n\
}\n\
\n\
set imported 0\n\
foreach filename $list {\n\
if { [$self source_file $filename] == \"\" } {\n\
set imported 1\n\
break\n\
}\n\
}\n\
\n\
if { ! $imported } {\n\
error \"don't know how to import $item\\n not\\\n\
mapped in: $import_dirs_\\\n\
\\n and not found in default\\\n\
locations: $list\"\n\
}\n\
}\n\
}\n\
}\n\
\n\
\n\
\n\
Import public override_importTable_mapping { object file_list } {\n\
$self instvar overrideTable_ import_dirs_\n\
\n\
if { [info exists import_dirs_] } {\n\
puts stderr \"warning: ignoring \\\"override_importTable_mapping\\\n\
$object $file_list\\\" \\n\\\n\
It is illegal to modify the internal table \\\n\
after the first call to import.\"\n\
return\n\
}\n\
\n\
if { [info exists overrideTable_($object)] } {\n\
unset overrideTable_($object)\n\
}\n\
\n\
foreach file $file_list {\n\
set fname [$self condense_into_absolute_filename \\\n\
[$self file join [pwd] $file]]\n\
lappend overrideTable_($object) $fname\n\
}\n\
}\n\
\n\
\n\
Import proc.private redefine_unknown {} {\n\
\n\
rename unknown unknown.orig\n\
proc unknown { args } {\n\
if { ![catch \"eval {unknown.orig} $args\" m] } { \n\
return\n\
}\n\
$self instvar autoimport_\n\
if { [info exists autoimport_] && $autoimport_ } {\n\
really_import [lindex $args 0]\n\
} else {\n\
error \"$m\" \n\
}\n\
}\n\
\n\
Import proc.private redefine_unknown {} {}\n\
}\n\
\n\
Import proc.public enable_autoimport {} {\n\
import Class Object mashutils\n\
\n\
Import set autoimport_ 1\n\
$self redefine_unknown\n\
return\n\
}\n\
\n\
\n\
Import proc.public disable_autoimport {} {\n\
Import set autoimport_ 0\n\
return\n\
}\n\
\n\
Import disable_autoimport \n\
\n\
\n\
\n\
Import private init_table { } {\n\
$self instvar import_dirs_\n\
global env\n\
\n\
if { ![info exists env(TCLCL_IMPORT_DIRS)] } {\n\
set env(TCLCL_IMPORT_DIRS) .\n\
}\n\
\n\
set import_dirs_ \"\"\n\
foreach dir [$self smart_parse_env_var $env(TCLCL_IMPORT_DIRS)] {\n\
lappend import_dirs_ [$self condense_to_absolute_filename $dir]\n\
}\n\
\n\
set dirs [$self find_import_dirs $import_dirs_]\n\
\n\
$self make_table $dirs\n\
}\n\
\n\
\n\
Import private make_table { dirs } {\n\
foreach d $dirs {\n\
$self read_dir $d\n\
}\n\
\n\
$self incorporate_table_overrides\n\
}\n\
\n\
\n\
Import private incorporate_table_overrides {} {\n\
$self instvar overrideTable_ table_\n\
\n\
foreach object [array names overrideTable_] {\n\
set table_($object) $overrideTable_($object)\n\
}\n\
}\n\
\n\
\n\
Import private find_import_dirs { dirs } {\n\
set list {}\n\
foreach dir $dirs {\n\
set importLocation [$self file join $dir importLocation]\n\
set r [$self file readable $importLocation]\n\
if [lindex $r 0] {\n\
set lines [$self read_file_into_list $importLocation]\n\
foreach line $lines {\n\
lappend list [$self \\\n\
condense_to_absolute_filename \\\n\
[$self file join $dir $line]]\n\
}\n\
if { [lindex $r 1] != {} } {\n\
unset [lindex $r 1]\n\
}\n\
} else {\n\
lappend list $dir\n\
}\n\
}\n\
\n\
$self instvar last_modified_\n\
set dirs \"\"\n\
foreach d $list {\n\
set import_table [$self file join $d importTable]\n\
set last_modified_($import_table) -1\n\
set r [$self file readable $import_table]\n\
if [lindex $r 0] {\n\
lappend dirs $d\n\
if { [lindex $r 1] != {} } {\n\
unset [lindex $r 1]\n\
}\n\
}\n\
}\n\
\n\
\n\
return $dirs\n\
}\n\
\n\
\n\
Import private read_dir { dir } {\n\
$self instvar table_ classes_mapped_ last_modified_\n\
\n\
set importTableFile [$self condense_to_absolute_filename \\\n\
[$self file join $dir importTable]]\n\
set last_modified_($importTableFile) -1\n\
\n\
set lines [$self read_file_into_list $importTableFile]\n\
\n\
foreach line $lines {\n\
set index [lindex $line 0]\n\
set fname [$self condense_to_absolute_filename \\\n\
[$self file join $dir [lindex $line 1]]]\n\
set last_modified [string trim [lindex $line 2]]\n\
\n\
if [info exists classes_mapped_($index)] {\n\
continue\n\
}\n\
\n\
if {[info exists table_($index)]} {\n\
if {-1!=[lsearch -exact $table_($index) $fname]} {\n\
continue\n\
}\n\
}\n\
\n\
lappend table_($index) $fname\n\
if { $last_modified!={} } {\n\
set last_modified_($fname) $last_modified\n\
}\n\
\n\
set this_mappings($index) 1\n\
}\n\
\n\
foreach index [array names this_mappings] {\n\
set classes_mapped_($index) 1\n\
}\n\
}\n\
\n\
\n\
Import private smart_parse_env_var { env_value } {\n\
set env $env_value\n\
while {[string length [set env [string trim $env \":\"]]] != 0 } {\n\
if [regexp {^([^:]+)://([^:/]+)(:([0-9]+))?(/[^:]*)} \\\n\
$env url protocol server x port trailingpath] {\n\
lappend list $url\n\
regsub {([^:]+)://([^:/]+)(:([0-9]+))?(/[^:]*)} \\\n\
$env {} env\n\
} else {\n\
regexp {^[^:]+} $env dir\n\
lappend list $dir\n\
regsub {^[^:]+} $env {} env\n\
}\n\
}\n\
\n\
return $list\n\
}\n\
\n\
\n\
Import private consistency_check { } {\n\
global env\n\
$self instvar orig_val_\n\
\n\
if { ![info exists orig_val_] } {\n\
set orig_val_ $env(TCLCL_IMPORT_DIRS)\n\
}\n\
\n\
if { $env(TCLCL_IMPORT_DIRS) != $orig_val_ } {\n\
puts stderr \"warning: ignoring modification to\\\n\
env(TCLCL_IMPORT_DIRS)\\nit is illegal to\\\n\
modify this after the first call to the\\\n\
import procedure.\"\n\
}\n\
}\n\
\n\
\n\
Import private source_file { file } {\n\
set file_readable_result [$self file readable $file]\n\
set file_readable [lindex $file_readable_result 0]\n\
\n\
if { $file_readable } {\n\
set read_token [lindex $file_readable_result 1] \n\
$self source $file $read_token\n\
if { $read_token!={} } { unset $read_token }\n\
return \"\"\n\
} else {\n\
return [lindex $file_readable_result 1]\n\
}\n\
}\n\
\n\
\n\
Import private source { file { read_token {} } } {\n\
$self instvar loaded_ uniq_num_\n\
\n\
if { ![info exists uniq_num_] } {\n\
set uniq_num_ 0\n\
}\n\
\n\
set file [$self condense_to_absolute_filename $file]\n\
if [info exists loaded_($file)] {\n\
return \n\
}\n\
set loaded_($file) 1\n\
\n\
incr uniq_num_ \n\
uplevel \\#0 \"rename source source.$uniq_num_\"\n\
uplevel \\#0 \"proc source {args} \\{ $self source \\$args \\}\"\n\
\n\
if [$self is_http_url $file] {\n\
set buffer [$self read_url $file $read_token]\n\
\n\
if [catch \"uplevel \\#0 {$buffer}\" errormsg] {\n\
global errorInfo\n\
error \"error in $file: $errormsg\\n$errorInfo\\n\\n\"\n\
}\n\
} else {\n\
\n\
if [catch \"uplevel \\#0 source.orig $file\" errormsg] {\n\
global errorInfo\n\
error \"error in $file: $errormsg\\n$errorInfo\\n\\n\"\n\
}\n\
}\n\
\n\
uplevel \\#0 {rename source {}}\n\
uplevel \\#0 \"rename source.$uniq_num_ source\"\n\
incr uniq_num_ -1\n\
}\n\
\n\
\n\
Import private enable { args } {\n\
}\n\
\n\
\n\
Import private class_to_file c {\n\
regsub -all -- \"/\" $c \"-\" filename\n\
return $filename\n\
}\n\
\n\
\n\
\n\
Import private is_http_url { name } {\n\
if [regexp {([^:]+)://([^:/]+)(:([0-9]+))?(/.*)} $name url protocol \\\n\
server x port trailingpath] {\n\
if { ![info exists protocol] } {\n\
return 0\n\
} else {\n\
return [regexp -nocase {http} $protocol]\n\
}\n\
} else {\n\
return 0\n\
}\n\
}\n\
\n\
\n\
Import private read_url { url {token {}} } {\n\
$self instvar use_http_cache_ cache_ last_modified_\n\
if { $token == {} } {\n\
\n\
if $use_http_cache_ {\n\
if { ![info exists cache_] } {\n\
set cache_ [new HTTPCache]\n\
}\n\
\n\
if [info exists last_modified_($url)] {\n\
set buffer [$cache_ get $url \\\n\
$last_modified_($url)]\n\
} else {\n\
set buffer [$cache_ get $url]\n\
}\n\
if { $buffer==\"\" } { unset buffer }\n\
}\n\
\n\
if { ![info exists buffer] } {\n\
set token [Http geturl $url]\n\
if { [lindex [set code [::http::code $token]] 1] \\\n\
!= 200 } {\n\
error \"couldn't read \\\"$url\\\": no such file \\\n\
or directory (HTTP code $code)\"\n\
}\n\
set buffer [::http::data $token]\n\
unset $token\n\
\n\
if $use_http_cache_ {\n\
if { ![info exists cache_] } {\n\
set cache_ [new HTTPCache]\n\
}\n\
\n\
$cache_ put $url $buffer\n\
}\n\
}\n\
} else {\n\
set buffer [::http::data $token]\n\
}\n\
return $buffer\n\
}\n\
\n\
\n\
Import private condense_to_absolute_filename { name } {\n\
\n\
return $name\n\
\n\
set before_cd [pwd]\n\
while { ![catch \"file readlink $filename\"] } {\n\
set filename [file readlink $filename]\n\
}\n\
set dirname [$self file dirname $filename]\n\
set tailname [file tail $filename]\n\
set condensed_name $filename\n\
if { ![catch \"cd $dirname\"] } {\n\
set condensed_name [ufile join [pwd] $tailname]\n\
}\n\
cd $before_cd\n\
return $condensed_name\n\
}\n\
\n\
\n\
Import private read_file_into_list { filename } {\n\
if [$self is_http_url $filename] {\n\
set buffer [$self read_url $filename]\n\
set lines [split $buffer \"\\n\"]\n\
} else {\n\
set f [open $filename \"r\"]\n\
set lines {}\n\
while 1 {\n\
set line [gets $f]\n\
if [eof $f] {\n\
close $f\n\
break\n\
}\n\
lappend lines \"$line\"\n\
}\n\
}\n\
\n\
return $lines\n\
}\n\
\n\
\n\
Import private file_readable { args } {\n\
if { [llength $args] == 0 } {\n\
error \"wrong # args: should be \\\"$self file\\\n\
readable name ?arg ...?\\\"\"\n\
}\n\
\n\
set name [lindex $args 0]\n\
if [$self is_http_url $name] {\n\
$self instvar use_http_cache_ cache_ last_modified_\n\
if $use_http_cache_ {\n\
if { ![info exists cache_] } {\n\
set cache_ [new HTTPCache]\n\
}\n\
\n\
if [info exists last_modified_($name)] {\n\
set buffer [$cache_ get $name \\\n\
$last_modified_($name)]\n\
} else {\n\
set buffer [$cache_ get $name]\n\
}\n\
if { $buffer!={} } {\n\
$self instvar buf_cnt_\n\
if ![info exists buf_cnt_] {\n\
set buf_cnt_ 0\n\
}\n\
set token ::http::readable_$buf_cnt_\n\
upvar #0 $token state\n\
set state(body) $buffer\n\
incr buf_cnt_\n\
return [list 1 $token]\n\
}\n\
}\n\
if [catch {set token [Http geturl $name]} m] {\n\
return [list 0 \"error executing \\\"::http::geturl\\\n\
$name\\\": $m\"]\n\
} else {\n\
set code [::http::code $token]\n\
if {[lindex $code 1] != 200} {\n\
return [list 0 $code]\n\
} else {\n\
if $use_http_cache_ {\n\
if { ![info exists cache_] } {\n\
set cache_ [new HTTPCache]\n\
}\n\
$cache_ put $name [::http::data $token]\n\
}\n\
\n\
return [list 1 $token]\n\
}\n\
}\n\
} else {\n\
eval file readable $args\n\
} \n\
}\n\
\n\
\n\
Import public file { option args } {\n\
if { $option == \"readable\" } {\n\
eval [list $self] file_readable $args\n\
} elseif { $option == \"dirname\" } {\n\
if { [llength $args] == 0 } {\n\
error \"wrong # args: should be \\\"$self file\\\n\
dirname name ?arg ...?\\\"\"\n\
} else {\n\
set name [lindex $args 0]\n\
if [$self is_http_url $name] {\n\
set url $name\n\
regexp {([^:]+)://([^:/]+)(:([0-9]+))?(/.*)} \\\n\
$name url protocol server x \\\n\
port trailingpath\n\
if {[string length $trailingpath] == 0} {\n\
set trailingpath /\n\
}\n\
set trailingpath [file dirname \"$trailingpath\"]\n\
return \"$protocol://$server$x$trailingpath\"\n\
} else {\n\
eval {file $option} $args\n\
}\n\
}\n\
} elseif { $option == \"join\" } {\n\
if { [llength $args] == 0 } {\n\
error \"wrong # args: should be \\\"$self file\\\n\
join name ?arg ...?\\\"\"\n\
} else {\n\
set base_url \"[string trimright [lindex $args 0] /]/\"\n\
set file_name [lindex $args 1]\n\
if [$self is_http_url $file_name] {\n\
return $file_name\n\
}\n\
if { [$self is_http_url $base_url] && \\\n\
[llength $args] ==2 } {\n\
regexp {([^:]+)://([^:/]+)(:([0-9]+))?(/.*)} \\\n\
$base_url url protocol server \\\n\
x port trailingpath\n\
regsub -all {^\\./} $file_name {} file_name\n\
regsub -all {/\\./} $file_name {/} file_name\n\
set counter 0\n\
while [regsub {^\\.\\./} $file_name {} \\\n\
file_name] {\n\
incr counter\n\
}\n\
while { $counter > 0 } {\n\
set trailingpath [$self \\\n\
format_as_dir_string \\\n\
[$self file dirname \\\n\
$trailingpath]]\n\
incr counter -1\n\
}\n\
set trailingpath \"[$self format_as_dir_string \\\n\
$trailingpath]$file_name\"\n\
return \"$protocol://$server$x$trailingpath\"\n\
} else {\n\
eval {file $option} $args\n\
}\n\
}\n\
} else {\n\
eval {file $option} $args\n\
}\n\
}\n\
\n\
\n\
Import private format_as_dir_string { dir_string } {\n\
return \"[string trimright [$self file join $dir_string .] .]\"\n\
}\n\
\n\
\n\
rename source source.orig\n\
proc source {fileName} {\n\
Import instvar instance_\n\
\n\
if ![info exists instance_] {\n\
set instance_ [new Import]\n\
}\n\
\n\
if [$instance_ is_http_url $fileName] {\n\
set buffer [$instance_ read_url $fileName]\n\
uplevel eval $buffer\n\
} else {\n\
uplevel source.orig [list $fileName]\n\
}\n\
}\n\
\n\
\n\
\n\
\n\
proc import args {\n\
if { ![catch \"Import set autoimport_\"] && ![Import set autoimport_] } {\n\
if [catch \"really_import $args\" error_msg] {\n\
error $error_msg\n\
}\n\
}\n\
}\n\
\n\
\n\
proc import args {\n\
Import instvar instance_\n\
\n\
if ![info exists instance_] {\n\
set instance_ [new Import]\n\
}\n\
\n\
if [catch \"eval $instance_ import $args\" errormsg] {\n\
error $errormsg\n\
}\n\
}\n\
\n\
\n\
\n\
proc override_importTable_mapping { object file_list } {\n\
Import instvar instance_\n\
\n\
if ![info exists instance_] {\n\
set instance_ [new Import]\n\
}\n\
\n\
$instance_ override_importTable_mapping $object $file_list\n\
}\n\
\n\
\n\
proc import_use_http_cache { {yes 1} } {\n\
Import instvar instance_\n\
\n\
if ![info exists instance_] {\n\
set instance_ [new Import]\n\
}\n\
\n\
$instance_ set use_http_cache_ 1\n\
}\n\
\n\
\n\
\n\
\n\
Class HTTP\n\
\n\
HTTP public init { } {\n\
$self next\n\
\n\
if { [lsearch -exact [package names] Tk] != -1 } {\n\
$self set enable_output_ 1\n\
} else {\n\
$self set enable_output_ 0\n\
}\n\
$self set token_count_ 0\n\
}\n\
\n\
\n\
HTTP public geturl { url } {\n\
set token [$self start_fetch $url]\n\
$self wait\n\
return $token\n\
}\n\
\n\
HTTP public geturls { args } {\n\
set tokens [eval \"$self start_fetch $args\"] \n\
$self wait\n\
return $tokens\n\
}\n\
\n\
HTTP public start_fetch { args } {\n\
$self instvar token_count_ urls_\n\
\n\
set urls_ $args\n\
foreach url $args {\n\
lappend tokens [::http::geturl $url \\\n\
-progress \"$self progress_callback\" \\\n\
-command \"$self fetch_done\"]\n\
incr token_count_\n\
}\n\
\n\
if { [llength $tokens] == 1 } {\n\
return [lindex $tokens 0]\n\
} else {\n\
return $tokens\n\
}\n\
}\n\
\n\
\n\
HTTP public wait { } {\n\
$self tkvar vwait_\n\
if { ![info exists vwait_] } {\n\
$self start_ping_pong\n\
if { ![info exists vwait_] } { vwait [$self tkvarname vwait_] }\n\
$self stop_ping_pong\n\
}\n\
unset vwait_\n\
}\n\
\n\
\n\
HTTP private fetch_done { token } {\n\
$self instvar token_count_\n\
$self tkvar vwait_\n\
incr token_count_ -1\n\
if { $token_count_ <= 0 } {\n\
set vwait_ 1\n\
set token_count_ 0\n\
$self instvar total_bytes_ current_bytes_ per_token_\n\
unset total_bytes_ current_bytes_ per_token_\n\
}\n\
}\n\
\n\
\n\
HTTP private progress_callback { token total_bytes current_bytes } {\n\
$self instvar total_bytes_ current_bytes_ per_token_\n\
if { ![info exists total_bytes_] } {\n\
set total_bytes_ 0\n\
set current_bytes_ 0\n\
}\n\
\n\
if { ![info exists per_token_($token)] } {\n\
set per_token_($token) $current_bytes\n\
incr total_bytes_ $total_bytes\n\
incr current_bytes_ $current_bytes\n\
} else {\n\
set current_bytes_ [expr $current_bytes_ - \\\n\
$per_token_($token) + $current_bytes]\n\
set per_token_($token) $current_bytes\n\
}\n\
\n\
$self instvar urls_\n\
$self print_status \"Fetching $urls_ ... (rcvd $current_bytes_ bytes)\"\n\
}\n\
\n\
\n\
HTTP private build_widget { } {\n\
$self instvar frame_ rect_\n\
if { ![info exists frame_] } {\n\
set cnt 0\n\
while [winfo exists .http_$cnt] { incr $cnt }\n\
set frame_ .http_$cnt\n\
toplevel $frame_\n\
wm withdraw $frame_\n\
wm transient $frame_ .\n\
wm title $frame_ \"HTTP Status\"\n\
set new_toplevel 1\n\
}\n\
\n\
set textheight [font metric http_font -linespace]\n\
label $frame_.label -font http_font -width 100 \\\n\