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

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