annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/tcl8.6/init.tcl @ 69:33d812a61356

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