comparison CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/tcl8.6/auto.tcl @ 68:5028fdace37b

planemo upload commit 2e9511a184a1ca667c7be0c6321a36dc4e3d116d
author jpayne
date Tue, 18 Mar 2025 16:23:26 -0400
parents
children
comparison
equal deleted inserted replaced
67:0e9998148a16 68:5028fdace37b
1 # auto.tcl --
2 #
3 # utility procs formerly in init.tcl dealing with auto execution of commands
4 # and can be auto loaded themselves.
5 #
6 # Copyright (c) 1991-1993 The Regents of the University of California.
7 # Copyright (c) 1994-1998 Sun Microsystems, Inc.
8 #
9 # See the file "license.terms" for information on usage and redistribution of
10 # this file, and for a DISCLAIMER OF ALL WARRANTIES.
11 #
12
13 # auto_reset --
14 #
15 # Destroy all cached information for auto-loading and auto-execution, so that
16 # the information gets recomputed the next time it's needed. Also delete any
17 # commands that are listed in the auto-load index.
18 #
19 # Arguments:
20 # None.
21
22 proc auto_reset {} {
23 global auto_execs auto_index auto_path
24 if {[array exists auto_index]} {
25 foreach cmdName [array names auto_index] {
26 set fqcn [namespace which $cmdName]
27 if {$fqcn eq ""} {
28 continue
29 }
30 rename $fqcn {}
31 }
32 }
33 unset -nocomplain auto_execs auto_index ::tcl::auto_oldpath
34 if {[catch {llength $auto_path}]} {
35 set auto_path [list [info library]]
36 } elseif {[info library] ni $auto_path} {
37 lappend auto_path [info library]
38 }
39 }
40
41 # tcl_findLibrary --
42 #
43 # This is a utility for extensions that searches for a library directory
44 # using a canonical searching algorithm. A side effect is to source the
45 # initialization script and set a global library variable.
46 #
47 # Arguments:
48 # basename Prefix of the directory name, (e.g., "tk")
49 # version Version number of the package, (e.g., "8.0")
50 # patch Patchlevel of the package, (e.g., "8.0.3")
51 # initScript Initialization script to source (e.g., tk.tcl)
52 # enVarName environment variable to honor (e.g., TK_LIBRARY)
53 # varName Global variable to set when done (e.g., tk_library)
54
55 proc tcl_findLibrary {basename version patch initScript enVarName varName} {
56 upvar #0 $varName the_library
57 global auto_path env tcl_platform
58
59 set dirs {}
60 set errors {}
61
62 # The C application may have hardwired a path, which we honor
63
64 if {[info exists the_library] && $the_library ne ""} {
65 lappend dirs $the_library
66 } else {
67 # Do the canonical search
68
69 # 1. From an environment variable, if it exists. Placing this first
70 # gives the end-user ultimate control to work-around any bugs, or
71 # to customize.
72
73 if {[info exists env($enVarName)]} {
74 lappend dirs $env($enVarName)
75 }
76
77 # 2. In the package script directory registered within the
78 # configuration of the package itself.
79
80 catch {
81 lappend dirs [::${basename}::pkgconfig get scriptdir,runtime]
82 }
83
84 # 3. Relative to auto_path directories. This checks relative to the
85 # Tcl library as well as allowing loading of libraries added to the
86 # auto_path that is not relative to the core library or binary paths.
87 foreach d $auto_path {
88 lappend dirs [file join $d $basename$version]
89 if {$tcl_platform(platform) eq "unix"
90 && $tcl_platform(os) eq "Darwin"} {
91 # 4. On MacOSX, check the Resources/Scripts subdir too
92 lappend dirs [file join $d $basename$version Resources Scripts]
93 }
94 }
95
96 # 3. Various locations relative to the executable
97 # ../lib/foo1.0 (From bin directory in install hierarchy)
98 # ../../lib/foo1.0 (From bin/arch directory in install hierarchy)
99 # ../library (From unix directory in build hierarchy)
100 #
101 # Remaining locations are out of date (when relevant, they ought to be
102 # covered by the $::auto_path seach above) and disabled.
103 #
104 # ../../library (From unix/arch directory in build hierarchy)
105 # ../../foo1.0.1/library
106 # (From unix directory in parallel build hierarchy)
107 # ../../../foo1.0.1/library
108 # (From unix/arch directory in parallel build hierarchy)
109
110 set parentDir [file dirname [file dirname [info nameofexecutable]]]
111 set grandParentDir [file dirname $parentDir]
112 lappend dirs [file join $parentDir lib $basename$version]
113 lappend dirs [file join $grandParentDir lib $basename$version]
114 lappend dirs [file join $parentDir library]
115 if {0} {
116 lappend dirs [file join $grandParentDir library]
117 lappend dirs [file join $grandParentDir $basename$patch library]
118 lappend dirs [file join [file dirname $grandParentDir] \
119 $basename$patch library]
120 }
121 }
122 # uniquify $dirs in order
123 array set seen {}
124 foreach i $dirs {
125 # Make sure $i is unique under normalization. Avoid repeated [source].
126 if {[interp issafe]} {
127 # Safe interps have no [file normalize].
128 set norm $i
129 } else {
130 set norm [file normalize $i]
131 }
132 if {[info exists seen($norm)]} {
133 continue
134 }
135 set seen($norm) {}
136
137 set the_library $i
138 set file [file join $i $initScript]
139
140 # source everything when in a safe interpreter because we have a
141 # source command, but no file exists command
142
143 if {[interp issafe] || [file exists $file]} {
144 if {![catch {uplevel #0 [list source $file]} msg opts]} {
145 return
146 }
147 append errors "$file: $msg\n"
148 append errors [dict get $opts -errorinfo]\n
149 }
150 }
151 unset -nocomplain the_library
152 set msg "Can't find a usable $initScript in the following directories: \n"
153 append msg " $dirs\n\n"
154 append msg "$errors\n\n"
155 append msg "This probably means that $basename wasn't installed properly.\n"
156 error $msg
157 }
158
159
160 # ----------------------------------------------------------------------
161 # auto_mkindex
162 # ----------------------------------------------------------------------
163 # The following procedures are used to generate the tclIndex file from Tcl
164 # source files. They use a special safe interpreter to parse Tcl source
165 # files, writing out index entries as "proc" commands are encountered. This
166 # implementation won't work in a safe interpreter, since a safe interpreter
167 # can't create the special parser and mess with its commands.
168
169 if {[interp issafe]} {
170 return ;# Stop sourcing the file here
171 }
172
173 # auto_mkindex --
174 # Regenerate a tclIndex file from Tcl source files. Takes as argument the
175 # name of the directory in which the tclIndex file is to be placed, followed
176 # by any number of glob patterns to use in that directory to locate all of the
177 # relevant files.
178 #
179 # Arguments:
180 # dir - Name of the directory in which to create an index.
181
182 # args - Any number of additional arguments giving the names of files
183 # within dir. If no additional are given auto_mkindex will look
184 # for *.tcl.
185
186 proc auto_mkindex {dir args} {
187 if {[interp issafe]} {
188 error "can't generate index within safe interpreter"
189 }
190
191 set oldDir [pwd]
192 cd $dir
193
194 append index "# Tcl autoload index file, version 2.0\n"
195 append index "# This file is generated by the \"auto_mkindex\" command\n"
196 append index "# and sourced to set up indexing information for one or\n"
197 append index "# more commands. Typically each line is a command that\n"
198 append index "# sets an element in the auto_index array, where the\n"
199 append index "# element name is the name of a command and the value is\n"
200 append index "# a script that loads the command.\n\n"
201 if {![llength $args]} {
202 set args *.tcl
203 }
204
205 auto_mkindex_parser::init
206 foreach file [lsort [glob -- {*}$args]] {
207 try {
208 append index [auto_mkindex_parser::mkindex $file]
209 } on error {msg opts} {
210 cd $oldDir
211 return -options $opts $msg
212 }
213 }
214 auto_mkindex_parser::cleanup
215
216 set fid [open "tclIndex" w]
217 puts -nonewline $fid $index
218 close $fid
219 cd $oldDir
220 }
221
222 # Original version of auto_mkindex that just searches the source code for
223 # "proc" at the beginning of the line.
224
225 proc auto_mkindex_old {dir args} {
226 set oldDir [pwd]
227 cd $dir
228 set dir [pwd]
229 append index "# Tcl autoload index file, version 2.0\n"
230 append index "# This file is generated by the \"auto_mkindex\" command\n"
231 append index "# and sourced to set up indexing information for one or\n"
232 append index "# more commands. Typically each line is a command that\n"
233 append index "# sets an element in the auto_index array, where the\n"
234 append index "# element name is the name of a command and the value is\n"
235 append index "# a script that loads the command.\n\n"
236 if {![llength $args]} {
237 set args *.tcl
238 }
239 foreach file [lsort [glob -- {*}$args]] {
240 set f ""
241 set error [catch {
242 set f [open $file]
243 fconfigure $f -eofchar "\032 {}"
244 while {[gets $f line] >= 0} {
245 if {[regexp {^proc[ ]+([^ ]*)} $line match procName]} {
246 set procName [lindex [auto_qualify $procName "::"] 0]
247 append index "set [list auto_index($procName)]"
248 append index " \[list source \[file join \$dir [list $file]\]\]\n"
249 }
250 }
251 close $f
252 } msg opts]
253 if {$error} {
254 catch {close $f}
255 cd $oldDir
256 return -options $opts $msg
257 }
258 }
259 set f ""
260 set error [catch {
261 set f [open tclIndex w]
262 puts -nonewline $f $index
263 close $f
264 cd $oldDir
265 } msg opts]
266 if {$error} {
267 catch {close $f}
268 cd $oldDir
269 error $msg $info $code
270 return -options $opts $msg
271 }
272 }
273
274 # Create a safe interpreter that can be used to parse Tcl source files
275 # generate a tclIndex file for autoloading. This interp contains commands for
276 # things that need index entries. Each time a command is executed, it writes
277 # an entry out to the index file.
278
279 namespace eval auto_mkindex_parser {
280 variable parser "" ;# parser used to build index
281 variable index "" ;# maintains index as it is built
282 variable scriptFile "" ;# name of file being processed
283 variable contextStack "" ;# stack of namespace scopes
284 variable imports "" ;# keeps track of all imported cmds
285 variable initCommands ;# list of commands that create aliases
286 if {![info exists initCommands]} {
287 set initCommands [list]
288 }
289
290 proc init {} {
291 variable parser
292 variable initCommands
293
294 if {![interp issafe]} {
295 set parser [interp create -safe]
296 $parser hide info
297 $parser hide rename
298 $parser hide proc
299 $parser hide namespace
300 $parser hide eval
301 $parser hide puts
302 foreach ns [$parser invokehidden namespace children ::] {
303 # MUST NOT DELETE "::tcl" OR BAD THINGS HAPPEN!
304 if {$ns eq "::tcl"} continue
305 $parser invokehidden namespace delete $ns
306 }
307 foreach cmd [$parser invokehidden info commands ::*] {
308 $parser invokehidden rename $cmd {}
309 }
310 $parser invokehidden proc unknown {args} {}
311
312 # We'll need access to the "namespace" command within the
313 # interp. Put it back, but move it out of the way.
314
315 $parser expose namespace
316 $parser invokehidden rename namespace _%@namespace
317 $parser expose eval
318 $parser invokehidden rename eval _%@eval
319
320 # Install all the registered psuedo-command implementations
321
322 foreach cmd $initCommands {
323 eval $cmd
324 }
325 }
326 }
327 proc cleanup {} {
328 variable parser
329 interp delete $parser
330 unset parser
331 }
332 }
333
334 # auto_mkindex_parser::mkindex --
335 #
336 # Used by the "auto_mkindex" command to create a "tclIndex" file for the given
337 # Tcl source file. Executes the commands in the file, and handles things like
338 # the "proc" command by adding an entry for the index file. Returns a string
339 # that represents the index file.
340 #
341 # Arguments:
342 # file Name of Tcl source file to be indexed.
343
344 proc auto_mkindex_parser::mkindex {file} {
345 variable parser
346 variable index
347 variable scriptFile
348 variable contextStack
349 variable imports
350
351 set scriptFile $file
352
353 set fid [open $file]
354 fconfigure $fid -eofchar "\032 {}"
355 set contents [read $fid]
356 close $fid
357
358 # There is one problem with sourcing files into the safe interpreter:
359 # references like "$x" will fail since code is not really being executed
360 # and variables do not really exist. To avoid this, we replace all $ with
361 # \0 (literally, the null char) later, when getting proc names we will
362 # have to reverse this replacement, in case there were any $ in the proc
363 # name. This will cause a problem if somebody actually tries to have a \0
364 # in their proc name. Too bad for them.
365 set contents [string map [list \$ \0] $contents]
366
367 set index ""
368 set contextStack ""
369 set imports ""
370
371 $parser eval $contents
372
373 foreach name $imports {
374 catch {$parser eval [list _%@namespace forget $name]}
375 }
376 return $index
377 }
378
379 # auto_mkindex_parser::hook command
380 #
381 # Registers a Tcl command to evaluate when initializing the child interpreter
382 # used by the mkindex parser. The command is evaluated in the parent
383 # interpreter, and can use the variable auto_mkindex_parser::parser to get to
384 # the child
385
386 proc auto_mkindex_parser::hook {cmd} {
387 variable initCommands
388
389 lappend initCommands $cmd
390 }
391
392 # auto_mkindex_parser::slavehook command
393 #
394 # Registers a Tcl command to evaluate when initializing the child interpreter
395 # used by the mkindex parser. The command is evaluated in the child
396 # interpreter.
397
398 proc auto_mkindex_parser::slavehook {cmd} {
399 variable initCommands
400
401 # The $parser variable is defined to be the name of the child interpreter
402 # when this command is used later.
403
404 lappend initCommands "\$parser eval [list $cmd]"
405 }
406
407 # auto_mkindex_parser::command --
408 #
409 # Registers a new command with the "auto_mkindex_parser" interpreter that
410 # parses Tcl files. These commands are fake versions of things like the
411 # "proc" command. When you execute them, they simply write out an entry to a
412 # "tclIndex" file for auto-loading.
413 #
414 # This procedure allows extensions to register their own commands with the
415 # auto_mkindex facility. For example, a package like [incr Tcl] might
416 # register a "class" command so that class definitions could be added to a
417 # "tclIndex" file for auto-loading.
418 #
419 # Arguments:
420 # name Name of command recognized in Tcl files.
421 # arglist Argument list for command.
422 # body Implementation of command to handle indexing.
423
424 proc auto_mkindex_parser::command {name arglist body} {
425 hook [list auto_mkindex_parser::commandInit $name $arglist $body]
426 }
427
428 # auto_mkindex_parser::commandInit --
429 #
430 # This does the actual work set up by auto_mkindex_parser::command. This is
431 # called when the interpreter used by the parser is created.
432 #
433 # Arguments:
434 # name Name of command recognized in Tcl files.
435 # arglist Argument list for command.
436 # body Implementation of command to handle indexing.
437
438 proc auto_mkindex_parser::commandInit {name arglist body} {
439 variable parser
440
441 set ns [namespace qualifiers $name]
442 set tail [namespace tail $name]
443 if {$ns eq ""} {
444 set fakeName [namespace current]::_%@fake_$tail
445 } else {
446 set fakeName [namespace current]::[string map {:: _} _%@fake_$name]
447 }
448 proc $fakeName $arglist $body
449
450 # YUK! Tcl won't let us alias fully qualified command names, so we can't
451 # handle names like "::itcl::class". Instead, we have to build procs with
452 # the fully qualified names, and have the procs point to the aliases.
453
454 if {[string match *::* $name]} {
455 set exportCmd [list _%@namespace export [namespace tail $name]]
456 $parser eval [list _%@namespace eval $ns $exportCmd]
457
458 # The following proc definition does not work if you want to tolerate
459 # space or something else diabolical in the procedure name, (i.e.,
460 # space in $alias). The following does not work:
461 # "_%@eval {$alias} \$args"
462 # because $alias gets concat'ed to $args. The following does not work
463 # because $cmd is somehow undefined
464 # "set cmd {$alias} \; _%@eval {\$cmd} \$args"
465 # A gold star to someone that can make test autoMkindex-3.3 work
466 # properly
467
468 set alias [namespace tail $fakeName]
469 $parser invokehidden proc $name {args} "_%@eval {$alias} \$args"
470 $parser alias $alias $fakeName
471 } else {
472 $parser alias $name $fakeName
473 }
474 return
475 }
476
477 # auto_mkindex_parser::fullname --
478 #
479 # Used by commands like "proc" within the auto_mkindex parser. Returns the
480 # qualified namespace name for the "name" argument. If the "name" does not
481 # start with "::", elements are added from the current namespace stack to
482 # produce a qualified name. Then, the name is examined to see whether or not
483 # it should really be qualified. If the name has more than the leading "::",
484 # it is returned as a fully qualified name. Otherwise, it is returned as a
485 # simple name. That way, the Tcl autoloader will recognize it properly.
486 #
487 # Arguments:
488 # name - Name that is being added to index.
489
490 proc auto_mkindex_parser::fullname {name} {
491 variable contextStack
492
493 if {![string match ::* $name]} {
494 foreach ns $contextStack {
495 set name "${ns}::$name"
496 if {[string match ::* $name]} {
497 break
498 }
499 }
500 }
501
502 if {[namespace qualifiers $name] eq ""} {
503 set name [namespace tail $name]
504 } elseif {![string match ::* $name]} {
505 set name "::$name"
506 }
507
508 # Earlier, mkindex replaced all $'s with \0. Now, we have to reverse that
509 # replacement.
510 return [string map [list \0 \$] $name]
511 }
512
513 # auto_mkindex_parser::indexEntry --
514 #
515 # Used by commands like "proc" within the auto_mkindex parser to add a
516 # correctly-quoted entry to the index. This is shared code so it is done
517 # *right*, in one place.
518 #
519 # Arguments:
520 # name - Name that is being added to index.
521
522 proc auto_mkindex_parser::indexEntry {name} {
523 variable index
524 variable scriptFile
525
526 # We convert all metacharacters to their backslashed form, and pre-split
527 # the file name that we know about (which will be a proper list, and so
528 # correctly quoted).
529
530 set name [string range [list \}[fullname $name]] 2 end]
531 set filenameParts [file split $scriptFile]
532
533 append index [format \
534 {set auto_index(%s) [list source [file join $dir %s]]%s} \
535 $name $filenameParts \n]
536 return
537 }
538
539 if {[llength $::auto_mkindex_parser::initCommands]} {
540 return
541 }
542
543 # Register all of the procedures for the auto_mkindex parser that will build
544 # the "tclIndex" file.
545
546 # AUTO MKINDEX: proc name arglist body
547 # Adds an entry to the auto index list for the given procedure name.
548
549 auto_mkindex_parser::command proc {name args} {
550 indexEntry $name
551 }
552
553 # Conditionally add support for Tcl byte code files. There are some tricky
554 # details here. First, we need to get the tbcload library initialized in the
555 # current interpreter. We cannot load tbcload into the child until we have
556 # done so because it needs access to the tcl_patchLevel variable. Second,
557 # because the package index file may defer loading the library until we invoke
558 # a command, we need to explicitly invoke auto_load to force it to be loaded.
559 # This should be a noop if the package has already been loaded
560
561 auto_mkindex_parser::hook {
562 try {
563 package require tbcload
564 } on error {} {
565 # OK, don't have it so do nothing
566 } on ok {} {
567 if {[namespace which -command tbcload::bcproc] eq ""} {
568 auto_load tbcload::bcproc
569 }
570 load {} tbcload $auto_mkindex_parser::parser
571
572 # AUTO MKINDEX: tbcload::bcproc name arglist body
573 # Adds an entry to the auto index list for the given pre-compiled
574 # procedure name.
575
576 auto_mkindex_parser::commandInit tbcload::bcproc {name args} {
577 indexEntry $name
578 }
579 }
580 }
581
582 # AUTO MKINDEX: namespace eval name command ?arg arg...?
583 # Adds the namespace name onto the context stack and evaluates the associated
584 # body of commands.
585 #
586 # AUTO MKINDEX: namespace import ?-force? pattern ?pattern...?
587 # Performs the "import" action in the parser interpreter. This is important
588 # for any commands contained in a namespace that affect the index. For
589 # example, a script may say "itcl::class ...", or it may import "itcl::*" and
590 # then say "class ...". This procedure does the import operation, but keeps
591 # track of imported patterns so we can remove the imports later.
592
593 auto_mkindex_parser::command namespace {op args} {
594 switch -- $op {
595 eval {
596 variable parser
597 variable contextStack
598
599 set name [lindex $args 0]
600 set args [lrange $args 1 end]
601
602 set contextStack [linsert $contextStack 0 $name]
603 $parser eval [list _%@namespace eval $name] $args
604 set contextStack [lrange $contextStack 1 end]
605 }
606 import {
607 variable parser
608 variable imports
609 foreach pattern $args {
610 if {$pattern ne "-force"} {
611 lappend imports $pattern
612 }
613 }
614 catch {$parser eval "_%@namespace import $args"}
615 }
616 ensemble {
617 variable parser
618 variable contextStack
619 if {[lindex $args 0] eq "create"} {
620 set name ::[join [lreverse $contextStack] ::]
621 catch {
622 set name [dict get [lrange $args 1 end] -command]
623 if {![string match ::* $name]} {
624 set name ::[join [lreverse $contextStack] ::]$name
625 }
626 regsub -all ::+ $name :: name
627 }
628 # create artifical proc to force an entry in the tclIndex
629 $parser eval [list ::proc $name {} {}]
630 }
631 }
632 }
633 }
634
635 # AUTO MKINDEX: oo::class create name ?definition?
636 # Adds an entry to the auto index list for the given class name.
637 auto_mkindex_parser::command oo::class {op name {body ""}} {
638 if {$op eq "create"} {
639 indexEntry $name
640 }
641 }
642 auto_mkindex_parser::command class {op name {body ""}} {
643 if {$op eq "create"} {
644 indexEntry $name
645 }
646 }
647
648 return