1 | # init.tcl -- |
---|
2 | # |
---|
3 | # Default system startup file for Tcl-based applications. Defines |
---|
4 | # "unknown" procedure and auto-load facilities. |
---|
5 | # |
---|
6 | # RCS: @(#) $Id: init.tcl,v 1.104 2008/03/28 17:31:44 dgp Exp $ |
---|
7 | # |
---|
8 | # Copyright (c) 1991-1993 The Regents of the University of California. |
---|
9 | # Copyright (c) 1994-1996 Sun Microsystems, Inc. |
---|
10 | # Copyright (c) 1998-1999 Scriptics Corporation. |
---|
11 | # Copyright (c) 2004 by Kevin B. Kenny. All rights reserved. |
---|
12 | # |
---|
13 | # See the file "license.terms" for information on usage and redistribution |
---|
14 | # of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
15 | # |
---|
16 | |
---|
17 | if {[info commands package] == ""} { |
---|
18 | error "version mismatch: library\nscripts expect Tcl version 7.5b1 or later but the loaded version is\nonly [info patchlevel]" |
---|
19 | } |
---|
20 | package require -exact Tcl 8.5.2 |
---|
21 | |
---|
22 | # Compute the auto path to use in this interpreter. |
---|
23 | # The values on the path come from several locations: |
---|
24 | # |
---|
25 | # The environment variable TCLLIBPATH |
---|
26 | # |
---|
27 | # tcl_library, which is the directory containing this init.tcl script. |
---|
28 | # [tclInit] (Tcl_Init()) searches around for the directory containing this |
---|
29 | # init.tcl and defines tcl_library to that location before sourcing it. |
---|
30 | # |
---|
31 | # The parent directory of tcl_library. Adding the parent |
---|
32 | # means that packages in peer directories will be found automatically. |
---|
33 | # |
---|
34 | # Also add the directory ../lib relative to the directory where the |
---|
35 | # executable is located. This is meant to find binary packages for the |
---|
36 | # same architecture as the current executable. |
---|
37 | # |
---|
38 | # tcl_pkgPath, which is set by the platform-specific initialization routines |
---|
39 | # On UNIX it is compiled in |
---|
40 | # On Windows, it is not used |
---|
41 | |
---|
42 | if {![info exists auto_path]} { |
---|
43 | if {[info exists env(TCLLIBPATH)]} { |
---|
44 | set auto_path $env(TCLLIBPATH) |
---|
45 | } else { |
---|
46 | set auto_path "" |
---|
47 | } |
---|
48 | } |
---|
49 | namespace eval tcl { |
---|
50 | variable Dir |
---|
51 | foreach Dir [list $::tcl_library [file dirname $::tcl_library]] { |
---|
52 | if {$Dir ni $::auto_path} { |
---|
53 | lappend ::auto_path $Dir |
---|
54 | } |
---|
55 | } |
---|
56 | set Dir [file join [file dirname [file dirname \ |
---|
57 | [info nameofexecutable]]] lib] |
---|
58 | if {$Dir ni $::auto_path} { |
---|
59 | lappend ::auto_path $Dir |
---|
60 | } |
---|
61 | catch { |
---|
62 | foreach Dir $::tcl_pkgPath { |
---|
63 | if {$Dir ni $::auto_path} { |
---|
64 | lappend ::auto_path $Dir |
---|
65 | } |
---|
66 | } |
---|
67 | } |
---|
68 | |
---|
69 | if {![interp issafe]} { |
---|
70 | variable Path [encoding dirs] |
---|
71 | set Dir [file join $::tcl_library encoding] |
---|
72 | if {$Dir ni $Path} { |
---|
73 | lappend Path $Dir |
---|
74 | encoding dirs $Path |
---|
75 | } |
---|
76 | } |
---|
77 | |
---|
78 | # TIP #255 min and max functions |
---|
79 | namespace eval mathfunc { |
---|
80 | proc min {args} { |
---|
81 | if {[llength $args] == 0} { |
---|
82 | return -code error \ |
---|
83 | "too few arguments to math function \"min\"" |
---|
84 | } |
---|
85 | set val Inf |
---|
86 | foreach arg $args { |
---|
87 | # This will handle forcing the numeric value without |
---|
88 | # ruining the internal type of a numeric object |
---|
89 | if {[catch {expr {double($arg)}} err]} { |
---|
90 | return -code error $err |
---|
91 | } |
---|
92 | if {$arg < $val} { set val $arg } |
---|
93 | } |
---|
94 | return $val |
---|
95 | } |
---|
96 | proc max {args} { |
---|
97 | if {[llength $args] == 0} { |
---|
98 | return -code error \ |
---|
99 | "too few arguments to math function \"max\"" |
---|
100 | } |
---|
101 | set val -Inf |
---|
102 | foreach arg $args { |
---|
103 | # This will handle forcing the numeric value without |
---|
104 | # ruining the internal type of a numeric object |
---|
105 | if {[catch {expr {double($arg)}} err]} { |
---|
106 | return -code error $err |
---|
107 | } |
---|
108 | if {$arg > $val} { set val $arg } |
---|
109 | } |
---|
110 | return $val |
---|
111 | } |
---|
112 | } |
---|
113 | } |
---|
114 | |
---|
115 | # Windows specific end of initialization |
---|
116 | |
---|
117 | if {(![interp issafe]) && ($tcl_platform(platform) eq "windows")} { |
---|
118 | namespace eval tcl { |
---|
119 | proc EnvTraceProc {lo n1 n2 op} { |
---|
120 | set x $::env($n2) |
---|
121 | set ::env($lo) $x |
---|
122 | set ::env([string toupper $lo]) $x |
---|
123 | } |
---|
124 | proc InitWinEnv {} { |
---|
125 | global env tcl_platform |
---|
126 | foreach p [array names env] { |
---|
127 | set u [string toupper $p] |
---|
128 | if {$u ne $p} { |
---|
129 | switch -- $u { |
---|
130 | COMSPEC - |
---|
131 | PATH { |
---|
132 | if {![info exists env($u)]} { |
---|
133 | set env($u) $env($p) |
---|
134 | } |
---|
135 | trace add variable env($p) write \ |
---|
136 | [namespace code [list EnvTraceProc $p]] |
---|
137 | trace add variable env($u) write \ |
---|
138 | [namespace code [list EnvTraceProc $p]] |
---|
139 | } |
---|
140 | } |
---|
141 | } |
---|
142 | } |
---|
143 | if {![info exists env(COMSPEC)]} { |
---|
144 | if {$tcl_platform(os) eq "Windows NT"} { |
---|
145 | set env(COMSPEC) cmd.exe |
---|
146 | } else { |
---|
147 | set env(COMSPEC) command.com |
---|
148 | } |
---|
149 | } |
---|
150 | } |
---|
151 | InitWinEnv |
---|
152 | } |
---|
153 | } |
---|
154 | |
---|
155 | # Setup the unknown package handler |
---|
156 | |
---|
157 | |
---|
158 | if {[interp issafe]} { |
---|
159 | package unknown ::tclPkgUnknown |
---|
160 | } else { |
---|
161 | # Set up search for Tcl Modules (TIP #189). |
---|
162 | # and setup platform specific unknown package handlers |
---|
163 | if {$::tcl_platform(os) eq "Darwin" |
---|
164 | && $::tcl_platform(platform) eq "unix"} { |
---|
165 | package unknown {::tcl::tm::UnknownHandler \ |
---|
166 | {::tcl::MacOSXPkgUnknown ::tclPkgUnknown}} |
---|
167 | } else { |
---|
168 | package unknown {::tcl::tm::UnknownHandler ::tclPkgUnknown} |
---|
169 | } |
---|
170 | |
---|
171 | # Set up the 'clock' ensemble |
---|
172 | |
---|
173 | namespace eval ::tcl::clock [list variable TclLibDir $::tcl_library] |
---|
174 | |
---|
175 | proc clock args { |
---|
176 | namespace eval ::tcl::clock [list namespace ensemble create -command \ |
---|
177 | [uplevel 1 [list namespace origin [lindex [info level 0] 0]]] \ |
---|
178 | -subcommands { |
---|
179 | add clicks format microseconds milliseconds scan seconds |
---|
180 | }] |
---|
181 | |
---|
182 | # Auto-loading stubs for 'clock.tcl' |
---|
183 | |
---|
184 | foreach cmd {add format scan} { |
---|
185 | proc ::tcl::clock::$cmd args { |
---|
186 | variable TclLibDir |
---|
187 | source -encoding utf-8 [file join $TclLibDir clock.tcl] |
---|
188 | return [uplevel 1 [info level 0]] |
---|
189 | } |
---|
190 | } |
---|
191 | |
---|
192 | return [uplevel 1 [info level 0]] |
---|
193 | } |
---|
194 | } |
---|
195 | |
---|
196 | # Conditionalize for presence of exec. |
---|
197 | |
---|
198 | if {[namespace which -command exec] eq ""} { |
---|
199 | |
---|
200 | # Some machines do not have exec. Also, on all |
---|
201 | # platforms, safe interpreters do not have exec. |
---|
202 | |
---|
203 | set auto_noexec 1 |
---|
204 | } |
---|
205 | |
---|
206 | # Define a log command (which can be overwitten to log errors |
---|
207 | # differently, specially when stderr is not available) |
---|
208 | |
---|
209 | if {[namespace which -command tclLog] eq ""} { |
---|
210 | proc tclLog {string} { |
---|
211 | catch {puts stderr $string} |
---|
212 | } |
---|
213 | } |
---|
214 | |
---|
215 | # query -- |
---|
216 | # Sends a query to the CommandExecutor of Orxonox and |
---|
217 | # waits for the response. |
---|
218 | # This procedure will be changed to it's real function |
---|
219 | # by Orxonox itself. |
---|
220 | # |
---|
221 | # Arguments: |
---|
222 | # args - The command to send to Orxonox |
---|
223 | |
---|
224 | proc query args { |
---|
225 | return -code error "Can't query Orxonox now" |
---|
226 | } |
---|
227 | |
---|
228 | # crossquery -- |
---|
229 | # Sends a query to another Tcl-interpreter in Orxonox and |
---|
230 | # waits for the response. |
---|
231 | # This procedure will be changed to it's real function |
---|
232 | # by Orxonox itself. |
---|
233 | # |
---|
234 | # Arguments: |
---|
235 | # id - The ID of the other interpreter |
---|
236 | # args - The command to send to Orxonox |
---|
237 | |
---|
238 | proc query {id args} { |
---|
239 | return -code error "Can't query interpreter with ID $id now" |
---|
240 | } |
---|
241 | |
---|
242 | # execute -- |
---|
243 | # This procedure will be changed by Orxonox itself. |
---|
244 | # execute calls a command in Orxonox. |
---|
245 | # |
---|
246 | # Arguments: |
---|
247 | # args - The command |
---|
248 | |
---|
249 | proc execute args { |
---|
250 | } |
---|
251 | |
---|
252 | # redef_puts -- |
---|
253 | # Redefines puts to write directly into the Orxonox console |
---|
254 | # if the channel is stdout or stderr. |
---|
255 | |
---|
256 | proc redef_puts {} { |
---|
257 | if ![llength [info command ::tcl::puts]] { |
---|
258 | rename puts ::tcl::puts |
---|
259 | proc puts args { |
---|
260 | set la [llength $args] |
---|
261 | if {$la<1 || $la>3} { |
---|
262 | error "usage: puts ?-nonewline? ?channel? string" |
---|
263 | } |
---|
264 | set nl \n |
---|
265 | if {[lindex $args 0]=="-nonewline"} { |
---|
266 | set nl "" |
---|
267 | set args [lrange $args 1 end] |
---|
268 | } |
---|
269 | if {[llength $args]==1} { |
---|
270 | set args [list stdout [join $args]] ; |
---|
271 | } |
---|
272 | foreach {channel s} $args break |
---|
273 | if {$channel=="stdout" || $channel=="stderr"} { |
---|
274 | set cmd "execute puts" |
---|
275 | if {$nl==""} {lappend cmd 0} else {lappend cmd 1} |
---|
276 | lappend cmd $s |
---|
277 | eval $cmd |
---|
278 | } else { |
---|
279 | set cmd ::tcl::puts |
---|
280 | if {$nl==""} {lappend cmd -nonewline} |
---|
281 | lappend cmd $channel $s |
---|
282 | eval $cmd |
---|
283 | } |
---|
284 | } |
---|
285 | } |
---|
286 | } |
---|
287 | |
---|
288 | # unknown -- |
---|
289 | # This procedure is called when a Tcl command is invoked that doesn't |
---|
290 | # exist in the interpreter. It takes the following steps to make the |
---|
291 | # command available: |
---|
292 | # |
---|
293 | # 1. See if the command has the form "namespace inscope ns cmd" and |
---|
294 | # if so, concatenate its arguments onto the end and evaluate it. |
---|
295 | # 2. See if the autoload facility can locate the command in a |
---|
296 | # Tcl script file. If so, load it and execute it. |
---|
297 | # 3. If the command was invoked interactively at top-level: |
---|
298 | # (a) see if the command exists as an executable UNIX program. |
---|
299 | # If so, "exec" the command. |
---|
300 | # (b) see if the command requests csh-like history substitution |
---|
301 | # in one of the common forms !!, !<number>, or ^old^new. If |
---|
302 | # so, emulate csh's history substitution. |
---|
303 | # (c) see if the command is a unique abbreviation for another |
---|
304 | # command. If so, invoke the command. |
---|
305 | # |
---|
306 | # Arguments: |
---|
307 | # args - A list whose elements are the words of the original |
---|
308 | # command, including the command name. |
---|
309 | |
---|
310 | proc unknown args { |
---|
311 | variable ::tcl::UnknownPending |
---|
312 | global auto_noexec auto_noload env tcl_interactive |
---|
313 | |
---|
314 | # If the command word has the form "namespace inscope ns cmd" |
---|
315 | # then concatenate its arguments onto the end and evaluate it. |
---|
316 | |
---|
317 | set cmd [lindex $args 0] |
---|
318 | if {[regexp "^:*namespace\[ \t\n\]+inscope" $cmd] && [llength $cmd] == 4} { |
---|
319 | #return -code error "You need an {*}" |
---|
320 | set arglist [lrange $args 1 end] |
---|
321 | set ret [catch {uplevel 1 ::$cmd $arglist} result opts] |
---|
322 | dict unset opts -errorinfo |
---|
323 | dict incr opts -level |
---|
324 | return -options $opts $result |
---|
325 | } |
---|
326 | |
---|
327 | catch {set savedErrorInfo $::errorInfo} |
---|
328 | catch {set savedErrorCode $::errorCode} |
---|
329 | set name $cmd |
---|
330 | if {![info exists auto_noload]} { |
---|
331 | # |
---|
332 | # Make sure we're not trying to load the same proc twice. |
---|
333 | # |
---|
334 | if {[info exists UnknownPending($name)]} { |
---|
335 | return -code error "self-referential recursion\ |
---|
336 | in \"unknown\" for command \"$name\""; |
---|
337 | } |
---|
338 | set UnknownPending($name) pending; |
---|
339 | set ret [catch { |
---|
340 | auto_load $name [uplevel 1 {::namespace current}] |
---|
341 | } msg opts] |
---|
342 | unset UnknownPending($name); |
---|
343 | if {$ret != 0} { |
---|
344 | dict append opts -errorinfo "\n (autoloading \"$name\")" |
---|
345 | return -options $opts $msg |
---|
346 | } |
---|
347 | if {![array size UnknownPending]} { |
---|
348 | unset UnknownPending |
---|
349 | } |
---|
350 | if {$msg} { |
---|
351 | catch {set ::errorCode $savedErrorCode} |
---|
352 | catch {set ::errorInfo $savedErrorInfo} |
---|
353 | set code [catch {uplevel 1 $args} msg opts] |
---|
354 | if {$code == 1} { |
---|
355 | # |
---|
356 | # Compute stack trace contribution from the [uplevel]. |
---|
357 | # Note the dependence on how Tcl_AddErrorInfo, etc. |
---|
358 | # construct the stack trace. |
---|
359 | # |
---|
360 | set errorInfo [dict get $opts -errorinfo] |
---|
361 | set errorCode [dict get $opts -errorcode] |
---|
362 | set cinfo $args |
---|
363 | if {[string bytelength $cinfo] > 150} { |
---|
364 | set cinfo [string range $cinfo 0 150] |
---|
365 | while {[string bytelength $cinfo] > 150} { |
---|
366 | set cinfo [string range $cinfo 0 end-1] |
---|
367 | } |
---|
368 | append cinfo ... |
---|
369 | } |
---|
370 | append cinfo "\"\n (\"uplevel\" body line 1)" |
---|
371 | append cinfo "\n invoked from within" |
---|
372 | append cinfo "\n\"uplevel 1 \$args\"" |
---|
373 | # |
---|
374 | # Try each possible form of the stack trace |
---|
375 | # and trim the extra contribution from the matching case |
---|
376 | # |
---|
377 | set expect "$msg\n while executing\n\"$cinfo" |
---|
378 | if {$errorInfo eq $expect} { |
---|
379 | # |
---|
380 | # The stack has only the eval from the expanded command |
---|
381 | # Do not generate any stack trace here. |
---|
382 | # |
---|
383 | dict unset opts -errorinfo |
---|
384 | dict incr opts -level |
---|
385 | return -options $opts $msg |
---|
386 | } |
---|
387 | # |
---|
388 | # Stack trace is nested, trim off just the contribution |
---|
389 | # from the extra "eval" of $args due to the "catch" above. |
---|
390 | # |
---|
391 | set expect "\n invoked from within\n\"$cinfo" |
---|
392 | set exlen [string length $expect] |
---|
393 | set eilen [string length $errorInfo] |
---|
394 | set i [expr {$eilen - $exlen - 1}] |
---|
395 | set einfo [string range $errorInfo 0 $i] |
---|
396 | # |
---|
397 | # For now verify that $errorInfo consists of what we are about |
---|
398 | # to return plus what we expected to trim off. |
---|
399 | # |
---|
400 | if {$errorInfo ne "$einfo$expect"} { |
---|
401 | error "Tcl bug: unexpected stack trace in \"unknown\"" {} \ |
---|
402 | [list CORE UNKNOWN BADTRACE $einfo $expect $errorInfo] |
---|
403 | } |
---|
404 | return -code error -errorcode $errorCode \ |
---|
405 | -errorinfo $einfo $msg |
---|
406 | } else { |
---|
407 | dict incr opts -level |
---|
408 | return -options $opts $msg |
---|
409 | } |
---|
410 | } |
---|
411 | } |
---|
412 | |
---|
413 | if {([info level] == 1) && ([info script] eq "") \ |
---|
414 | && [info exists tcl_interactive] && $tcl_interactive} { |
---|
415 | if {![info exists auto_noexec]} { |
---|
416 | set new [auto_execok $name] |
---|
417 | if {$new ne ""} { |
---|
418 | set redir "" |
---|
419 | if {[namespace which -command console] eq ""} { |
---|
420 | set redir ">&@stdout <@stdin" |
---|
421 | } |
---|
422 | uplevel 1 [list ::catch \ |
---|
423 | [concat exec $redir $new [lrange $args 1 end]] \ |
---|
424 | ::tcl::UnknownResult ::tcl::UnknownOptions] |
---|
425 | dict incr ::tcl::UnknownOptions -level |
---|
426 | return -options $::tcl::UnknownOptions $::tcl::UnknownResult |
---|
427 | } |
---|
428 | } |
---|
429 | if {$name eq "!!"} { |
---|
430 | set newcmd [history event] |
---|
431 | } elseif {[regexp {^!(.+)$} $name -> event]} { |
---|
432 | set newcmd [history event $event] |
---|
433 | } elseif {[regexp {^\^([^^]*)\^([^^]*)\^?$} $name -> old new]} { |
---|
434 | set newcmd [history event -1] |
---|
435 | catch {regsub -all -- $old $newcmd $new newcmd} |
---|
436 | } |
---|
437 | if {[info exists newcmd]} { |
---|
438 | tclLog $newcmd |
---|
439 | history change $newcmd 0 |
---|
440 | uplevel 1 [list ::catch $newcmd \ |
---|
441 | ::tcl::UnknownResult ::tcl::UnknownOptions] |
---|
442 | dict incr ::tcl::UnknownOptions -level |
---|
443 | return -options $::tcl::UnknownOptions $::tcl::UnknownResult |
---|
444 | } |
---|
445 | |
---|
446 | set ret [catch {set candidates [info commands $name*]} msg] |
---|
447 | if {$name eq "::"} { |
---|
448 | set name "" |
---|
449 | } |
---|
450 | if {$ret != 0} { |
---|
451 | dict append opts -errorinfo \ |
---|
452 | "\n (expanding command prefix \"$name\" in unknown)" |
---|
453 | return -options $opts $msg |
---|
454 | } |
---|
455 | # Filter out bogus matches when $name contained |
---|
456 | # a glob-special char [Bug 946952] |
---|
457 | if {$name eq ""} { |
---|
458 | # Handle empty $name separately due to strangeness |
---|
459 | # in [string first] (See RFE 1243354) |
---|
460 | set cmds $candidates |
---|
461 | } else { |
---|
462 | set cmds [list] |
---|
463 | foreach x $candidates { |
---|
464 | if {[string first $name $x] == 0} { |
---|
465 | lappend cmds $x |
---|
466 | } |
---|
467 | } |
---|
468 | } |
---|
469 | if {[llength $cmds] == 1} { |
---|
470 | uplevel 1 [list ::catch [lreplace $args 0 0 [lindex $cmds 0]] \ |
---|
471 | ::tcl::UnknownResult ::tcl::UnknownOptions] |
---|
472 | dict incr ::tcl::UnknownOptions -level |
---|
473 | return -options $::tcl::UnknownOptions $::tcl::UnknownResult |
---|
474 | } |
---|
475 | if {[llength $cmds]} { |
---|
476 | return -code error "ambiguous command name \"$name\": [lsort $cmds]" |
---|
477 | } |
---|
478 | } |
---|
479 | # return -code error "invalid command name \"$name\"" |
---|
480 | return [query $args] |
---|
481 | } |
---|
482 | |
---|
483 | # auto_load -- |
---|
484 | # Checks a collection of library directories to see if a procedure |
---|
485 | # is defined in one of them. If so, it sources the appropriate |
---|
486 | # library file to create the procedure. Returns 1 if it successfully |
---|
487 | # loaded the procedure, 0 otherwise. |
---|
488 | # |
---|
489 | # Arguments: |
---|
490 | # cmd - Name of the command to find and load. |
---|
491 | # namespace (optional) The namespace where the command is being used - must be |
---|
492 | # a canonical namespace as returned [namespace current] |
---|
493 | # for instance. If not given, namespace current is used. |
---|
494 | |
---|
495 | proc auto_load {cmd {namespace {}}} { |
---|
496 | global auto_index auto_path |
---|
497 | |
---|
498 | if {$namespace eq ""} { |
---|
499 | set namespace [uplevel 1 [list ::namespace current]] |
---|
500 | } |
---|
501 | set nameList [auto_qualify $cmd $namespace] |
---|
502 | # workaround non canonical auto_index entries that might be around |
---|
503 | # from older auto_mkindex versions |
---|
504 | lappend nameList $cmd |
---|
505 | foreach name $nameList { |
---|
506 | if {[info exists auto_index($name)]} { |
---|
507 | namespace eval :: $auto_index($name) |
---|
508 | # There's a couple of ways to look for a command of a given |
---|
509 | # name. One is to use |
---|
510 | # info commands $name |
---|
511 | # Unfortunately, if the name has glob-magic chars in it like * |
---|
512 | # or [], it may not match. For our purposes here, a better |
---|
513 | # route is to use |
---|
514 | # namespace which -command $name |
---|
515 | if {[namespace which -command $name] ne ""} { |
---|
516 | return 1 |
---|
517 | } |
---|
518 | } |
---|
519 | } |
---|
520 | if {![info exists auto_path]} { |
---|
521 | return 0 |
---|
522 | } |
---|
523 | |
---|
524 | if {![auto_load_index]} { |
---|
525 | return 0 |
---|
526 | } |
---|
527 | foreach name $nameList { |
---|
528 | if {[info exists auto_index($name)]} { |
---|
529 | namespace eval :: $auto_index($name) |
---|
530 | if {[namespace which -command $name] ne ""} { |
---|
531 | return 1 |
---|
532 | } |
---|
533 | } |
---|
534 | } |
---|
535 | return 0 |
---|
536 | } |
---|
537 | |
---|
538 | # auto_load_index -- |
---|
539 | # Loads the contents of tclIndex files on the auto_path directory |
---|
540 | # list. This is usually invoked within auto_load to load the index |
---|
541 | # of available commands. Returns 1 if the index is loaded, and 0 if |
---|
542 | # the index is already loaded and up to date. |
---|
543 | # |
---|
544 | # Arguments: |
---|
545 | # None. |
---|
546 | |
---|
547 | proc auto_load_index {} { |
---|
548 | variable ::tcl::auto_oldpath |
---|
549 | global auto_index auto_path |
---|
550 | |
---|
551 | if {[info exists auto_oldpath] && ($auto_oldpath eq $auto_path)} { |
---|
552 | return 0 |
---|
553 | } |
---|
554 | set auto_oldpath $auto_path |
---|
555 | |
---|
556 | # Check if we are a safe interpreter. In that case, we support only |
---|
557 | # newer format tclIndex files. |
---|
558 | |
---|
559 | set issafe [interp issafe] |
---|
560 | for {set i [expr {[llength $auto_path] - 1}]} {$i >= 0} {incr i -1} { |
---|
561 | set dir [lindex $auto_path $i] |
---|
562 | set f "" |
---|
563 | if {$issafe} { |
---|
564 | catch {source [file join $dir tclIndex]} |
---|
565 | } elseif {[catch {set f [open [file join $dir tclIndex]]}]} { |
---|
566 | continue |
---|
567 | } else { |
---|
568 | set error [catch { |
---|
569 | set id [gets $f] |
---|
570 | if {$id eq "# Tcl autoload index file, version 2.0"} { |
---|
571 | eval [read $f] |
---|
572 | } elseif {$id eq "# Tcl autoload index file: each line identifies a Tcl"} { |
---|
573 | while {[gets $f line] >= 0} { |
---|
574 | if {([string index $line 0] eq "#") \ |
---|
575 | || ([llength $line] != 2)} { |
---|
576 | continue |
---|
577 | } |
---|
578 | set name [lindex $line 0] |
---|
579 | set auto_index($name) \ |
---|
580 | "source [file join $dir [lindex $line 1]]" |
---|
581 | } |
---|
582 | } else { |
---|
583 | error "[file join $dir tclIndex] isn't a proper Tcl index file" |
---|
584 | } |
---|
585 | } msg opts] |
---|
586 | if {$f ne ""} { |
---|
587 | close $f |
---|
588 | } |
---|
589 | if {$error} { |
---|
590 | return -options $opts $msg |
---|
591 | } |
---|
592 | } |
---|
593 | } |
---|
594 | return 1 |
---|
595 | } |
---|
596 | |
---|
597 | # auto_qualify -- |
---|
598 | # |
---|
599 | # Compute a fully qualified names list for use in the auto_index array. |
---|
600 | # For historical reasons, commands in the global namespace do not have leading |
---|
601 | # :: in the index key. The list has two elements when the command name is |
---|
602 | # relative (no leading ::) and the namespace is not the global one. Otherwise |
---|
603 | # only one name is returned (and searched in the auto_index). |
---|
604 | # |
---|
605 | # Arguments - |
---|
606 | # cmd The command name. Can be any name accepted for command |
---|
607 | # invocations (Like "foo::::bar"). |
---|
608 | # namespace The namespace where the command is being used - must be |
---|
609 | # a canonical namespace as returned by [namespace current] |
---|
610 | # for instance. |
---|
611 | |
---|
612 | proc auto_qualify {cmd namespace} { |
---|
613 | |
---|
614 | # count separators and clean them up |
---|
615 | # (making sure that foo:::::bar will be treated as foo::bar) |
---|
616 | set n [regsub -all {::+} $cmd :: cmd] |
---|
617 | |
---|
618 | # Ignore namespace if the name starts with :: |
---|
619 | # Handle special case of only leading :: |
---|
620 | |
---|
621 | # Before each return case we give an example of which category it is |
---|
622 | # with the following form : |
---|
623 | # ( inputCmd, inputNameSpace) -> output |
---|
624 | |
---|
625 | if {[string match ::* $cmd]} { |
---|
626 | if {$n > 1} { |
---|
627 | # ( ::foo::bar , * ) -> ::foo::bar |
---|
628 | return [list $cmd] |
---|
629 | } else { |
---|
630 | # ( ::global , * ) -> global |
---|
631 | return [list [string range $cmd 2 end]] |
---|
632 | } |
---|
633 | } |
---|
634 | |
---|
635 | # Potentially returning 2 elements to try : |
---|
636 | # (if the current namespace is not the global one) |
---|
637 | |
---|
638 | if {$n == 0} { |
---|
639 | if {$namespace eq "::"} { |
---|
640 | # ( nocolons , :: ) -> nocolons |
---|
641 | return [list $cmd] |
---|
642 | } else { |
---|
643 | # ( nocolons , ::sub ) -> ::sub::nocolons nocolons |
---|
644 | return [list ${namespace}::$cmd $cmd] |
---|
645 | } |
---|
646 | } elseif {$namespace eq "::"} { |
---|
647 | # ( foo::bar , :: ) -> ::foo::bar |
---|
648 | return [list ::$cmd] |
---|
649 | } else { |
---|
650 | # ( foo::bar , ::sub ) -> ::sub::foo::bar ::foo::bar |
---|
651 | return [list ${namespace}::$cmd ::$cmd] |
---|
652 | } |
---|
653 | } |
---|
654 | |
---|
655 | # auto_import -- |
---|
656 | # |
---|
657 | # Invoked during "namespace import" to make see if the imported commands |
---|
658 | # reside in an autoloaded library. If so, the commands are loaded so |
---|
659 | # that they will be available for the import links. If not, then this |
---|
660 | # procedure does nothing. |
---|
661 | # |
---|
662 | # Arguments - |
---|
663 | # pattern The pattern of commands being imported (like "foo::*") |
---|
664 | # a canonical namespace as returned by [namespace current] |
---|
665 | |
---|
666 | proc auto_import {pattern} { |
---|
667 | global auto_index |
---|
668 | |
---|
669 | # If no namespace is specified, this will be an error case |
---|
670 | |
---|
671 | if {![string match *::* $pattern]} { |
---|
672 | return |
---|
673 | } |
---|
674 | |
---|
675 | set ns [uplevel 1 [list ::namespace current]] |
---|
676 | set patternList [auto_qualify $pattern $ns] |
---|
677 | |
---|
678 | auto_load_index |
---|
679 | |
---|
680 | foreach pattern $patternList { |
---|
681 | foreach name [array names auto_index $pattern] { |
---|
682 | if {([namespace which -command $name] eq "") |
---|
683 | && ([namespace qualifiers $pattern] eq [namespace qualifiers $name])} { |
---|
684 | namespace eval :: $auto_index($name) |
---|
685 | } |
---|
686 | } |
---|
687 | } |
---|
688 | } |
---|
689 | |
---|
690 | # auto_execok -- |
---|
691 | # |
---|
692 | # Returns string that indicates name of program to execute if |
---|
693 | # name corresponds to a shell builtin or an executable in the |
---|
694 | # Windows search path, or "" otherwise. Builds an associative |
---|
695 | # array auto_execs that caches information about previous checks, |
---|
696 | # for speed. |
---|
697 | # |
---|
698 | # Arguments: |
---|
699 | # name - Name of a command. |
---|
700 | |
---|
701 | if {$tcl_platform(platform) eq "windows"} { |
---|
702 | # Windows version. |
---|
703 | # |
---|
704 | # Note that info executable doesn't work under Windows, so we have to |
---|
705 | # look for files with .exe, .com, or .bat extensions. Also, the path |
---|
706 | # may be in the Path or PATH environment variables, and path |
---|
707 | # components are separated with semicolons, not colons as under Unix. |
---|
708 | # |
---|
709 | proc auto_execok name { |
---|
710 | global auto_execs env tcl_platform |
---|
711 | |
---|
712 | if {[info exists auto_execs($name)]} { |
---|
713 | return $auto_execs($name) |
---|
714 | } |
---|
715 | set auto_execs($name) "" |
---|
716 | |
---|
717 | set shellBuiltins [list cls copy date del erase dir echo mkdir \ |
---|
718 | md rename ren rmdir rd time type ver vol] |
---|
719 | if {$tcl_platform(os) eq "Windows NT"} { |
---|
720 | # NT includes the 'start' built-in |
---|
721 | lappend shellBuiltins "start" |
---|
722 | } |
---|
723 | if {[info exists env(PATHEXT)]} { |
---|
724 | # Add an initial ; to have the {} extension check first. |
---|
725 | set execExtensions [split ";$env(PATHEXT)" ";"] |
---|
726 | } else { |
---|
727 | set execExtensions [list {} .com .exe .bat] |
---|
728 | } |
---|
729 | |
---|
730 | if {$name in $shellBuiltins} { |
---|
731 | # When this is command.com for some reason on Win2K, Tcl won't |
---|
732 | # exec it unless the case is right, which this corrects. COMSPEC |
---|
733 | # may not point to a real file, so do the check. |
---|
734 | set cmd $env(COMSPEC) |
---|
735 | if {[file exists $cmd]} { |
---|
736 | set cmd [file attributes $cmd -shortname] |
---|
737 | } |
---|
738 | return [set auto_execs($name) [list $cmd /c $name]] |
---|
739 | } |
---|
740 | |
---|
741 | if {[llength [file split $name]] != 1} { |
---|
742 | foreach ext $execExtensions { |
---|
743 | set file ${name}${ext} |
---|
744 | if {[file exists $file] && ![file isdirectory $file]} { |
---|
745 | return [set auto_execs($name) [list $file]] |
---|
746 | } |
---|
747 | } |
---|
748 | return "" |
---|
749 | } |
---|
750 | |
---|
751 | set path "[file dirname [info nameof]];.;" |
---|
752 | if {[info exists env(WINDIR)]} { |
---|
753 | set windir $env(WINDIR) |
---|
754 | } |
---|
755 | if {[info exists windir]} { |
---|
756 | if {$tcl_platform(os) eq "Windows NT"} { |
---|
757 | append path "$windir/system32;" |
---|
758 | } |
---|
759 | append path "$windir/system;$windir;" |
---|
760 | } |
---|
761 | |
---|
762 | foreach var {PATH Path path} { |
---|
763 | if {[info exists env($var)]} { |
---|
764 | append path ";$env($var)" |
---|
765 | } |
---|
766 | } |
---|
767 | |
---|
768 | foreach dir [split $path {;}] { |
---|
769 | # Skip already checked directories |
---|
770 | if {[info exists checked($dir)] || ($dir eq {})} { continue } |
---|
771 | set checked($dir) {} |
---|
772 | foreach ext $execExtensions { |
---|
773 | set file [file join $dir ${name}${ext}] |
---|
774 | if {[file exists $file] && ![file isdirectory $file]} { |
---|
775 | return [set auto_execs($name) [list $file]] |
---|
776 | } |
---|
777 | } |
---|
778 | } |
---|
779 | return "" |
---|
780 | } |
---|
781 | |
---|
782 | } else { |
---|
783 | # Unix version. |
---|
784 | # |
---|
785 | proc auto_execok name { |
---|
786 | global auto_execs env |
---|
787 | |
---|
788 | if {[info exists auto_execs($name)]} { |
---|
789 | return $auto_execs($name) |
---|
790 | } |
---|
791 | set auto_execs($name) "" |
---|
792 | if {[llength [file split $name]] != 1} { |
---|
793 | if {[file executable $name] && ![file isdirectory $name]} { |
---|
794 | set auto_execs($name) [list $name] |
---|
795 | } |
---|
796 | return $auto_execs($name) |
---|
797 | } |
---|
798 | foreach dir [split $env(PATH) :] { |
---|
799 | if {$dir eq ""} { |
---|
800 | set dir . |
---|
801 | } |
---|
802 | set file [file join $dir $name] |
---|
803 | if {[file executable $file] && ![file isdirectory $file]} { |
---|
804 | set auto_execs($name) [list $file] |
---|
805 | return $auto_execs($name) |
---|
806 | } |
---|
807 | } |
---|
808 | return "" |
---|
809 | } |
---|
810 | |
---|
811 | } |
---|
812 | |
---|
813 | # ::tcl::CopyDirectory -- |
---|
814 | # |
---|
815 | # This procedure is called by Tcl's core when attempts to call the |
---|
816 | # filesystem's copydirectory function fail. The semantics of the call |
---|
817 | # are that 'dest' does not yet exist, i.e. dest should become the exact |
---|
818 | # image of src. If dest does exist, we throw an error. |
---|
819 | # |
---|
820 | # Note that making changes to this procedure can change the results |
---|
821 | # of running Tcl's tests. |
---|
822 | # |
---|
823 | # Arguments: |
---|
824 | # action - "renaming" or "copying" |
---|
825 | # src - source directory |
---|
826 | # dest - destination directory |
---|
827 | proc tcl::CopyDirectory {action src dest} { |
---|
828 | set nsrc [file normalize $src] |
---|
829 | set ndest [file normalize $dest] |
---|
830 | |
---|
831 | if {$action eq "renaming"} { |
---|
832 | # Can't rename volumes. We could give a more precise |
---|
833 | # error message here, but that would break the test suite. |
---|
834 | if {$nsrc in [file volumes]} { |
---|
835 | return -code error "error $action \"$src\" to\ |
---|
836 | \"$dest\": trying to rename a volume or move a directory\ |
---|
837 | into itself" |
---|
838 | } |
---|
839 | } |
---|
840 | if {[file exists $dest]} { |
---|
841 | if {$nsrc eq $ndest} { |
---|
842 | return -code error "error $action \"$src\" to\ |
---|
843 | \"$dest\": trying to rename a volume or move a directory\ |
---|
844 | into itself" |
---|
845 | } |
---|
846 | if {$action eq "copying"} { |
---|
847 | # We used to throw an error here, but, looking more closely |
---|
848 | # at the core copy code in tclFCmd.c, if the destination |
---|
849 | # exists, then we should only call this function if -force |
---|
850 | # is true, which means we just want to over-write. So, |
---|
851 | # the following code is now commented out. |
---|
852 | # |
---|
853 | # return -code error "error $action \"$src\" to\ |
---|
854 | # \"$dest\": file already exists" |
---|
855 | } else { |
---|
856 | # Depending on the platform, and on the current |
---|
857 | # working directory, the directories '.', '..' |
---|
858 | # can be returned in various combinations. Anyway, |
---|
859 | # if any other file is returned, we must signal an error. |
---|
860 | set existing [glob -nocomplain -directory $dest * .*] |
---|
861 | lappend existing {*}[glob -nocomplain -directory $dest \ |
---|
862 | -type hidden * .*] |
---|
863 | foreach s $existing { |
---|
864 | if {([file tail $s] ne ".") && ([file tail $s] ne "..")} { |
---|
865 | return -code error "error $action \"$src\" to\ |
---|
866 | \"$dest\": file already exists" |
---|
867 | } |
---|
868 | } |
---|
869 | } |
---|
870 | } else { |
---|
871 | if {[string first $nsrc $ndest] != -1} { |
---|
872 | set srclen [expr {[llength [file split $nsrc]] -1}] |
---|
873 | set ndest [lindex [file split $ndest] $srclen] |
---|
874 | if {$ndest eq [file tail $nsrc]} { |
---|
875 | return -code error "error $action \"$src\" to\ |
---|
876 | \"$dest\": trying to rename a volume or move a directory\ |
---|
877 | into itself" |
---|
878 | } |
---|
879 | } |
---|
880 | file mkdir $dest |
---|
881 | } |
---|
882 | # Have to be careful to capture both visible and hidden files. |
---|
883 | # We will also be more generous to the file system and not |
---|
884 | # assume the hidden and non-hidden lists are non-overlapping. |
---|
885 | # |
---|
886 | # On Unix 'hidden' files begin with '.'. On other platforms |
---|
887 | # or filesystems hidden files may have other interpretations. |
---|
888 | set filelist [concat [glob -nocomplain -directory $src *] \ |
---|
889 | [glob -nocomplain -directory $src -types hidden *]] |
---|
890 | |
---|
891 | foreach s [lsort -unique $filelist] { |
---|
892 | if {([file tail $s] ne ".") && ([file tail $s] ne "..")} { |
---|
893 | file copy -force $s [file join $dest [file tail $s]] |
---|
894 | } |
---|
895 | } |
---|
896 | return |
---|
897 | } |
---|