init.tcl 24 KB

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