init.tcl 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  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.8
  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 ::tcl::initClock {} {
  160. # Auto-loading stubs for 'clock.tcl'
  161. foreach cmd {add format scan} {
  162. proc ::tcl::clock::$cmd args {
  163. variable TclLibDir
  164. source -encoding utf-8 [file join $TclLibDir clock.tcl]
  165. return [uplevel 1 [info level 0]]
  166. }
  167. }
  168. rename ::tcl::initClock {}
  169. }
  170. ::tcl::initClock
  171. }
  172. # Conditionalize for presence of exec.
  173. if {[namespace which -command exec] eq ""} {
  174. # Some machines do not have exec. Also, on all
  175. # platforms, safe interpreters do not have exec.
  176. set auto_noexec 1
  177. }
  178. # Define a log command (which can be overwitten to log errors
  179. # differently, specially when stderr is not available)
  180. if {[namespace which -command tclLog] eq ""} {
  181. proc tclLog {string} {
  182. catch {puts stderr $string}
  183. }
  184. }
  185. # unknown --
  186. # This procedure is called when a Tcl command is invoked that doesn't
  187. # exist in the interpreter. It takes the following steps to make the
  188. # command available:
  189. #
  190. # 1. See if the autoload facility can locate the command in a
  191. # Tcl script file. If so, load it and execute it.
  192. # 2. If the command was invoked interactively at top-level:
  193. # (a) see if the command exists as an executable UNIX program.
  194. # If so, "exec" the command.
  195. # (b) see if the command requests csh-like history substitution
  196. # in one of the common forms !!, !<number>, or ^old^new. If
  197. # so, emulate csh's history substitution.
  198. # (c) see if the command is a unique abbreviation for another
  199. # command. If so, invoke the command.
  200. #
  201. # Arguments:
  202. # args - A list whose elements are the words of the original
  203. # command, including the command name.
  204. proc unknown args {
  205. variable ::tcl::UnknownPending
  206. global auto_noexec auto_noload env tcl_interactive errorInfo errorCode
  207. if {[info exists errorInfo]} {
  208. set savedErrorInfo $errorInfo
  209. }
  210. if {[info exists errorCode]} {
  211. set savedErrorCode $errorCode
  212. }
  213. set name [lindex $args 0]
  214. if {![info exists auto_noload]} {
  215. #
  216. # Make sure we're not trying to load the same proc twice.
  217. #
  218. if {[info exists UnknownPending($name)]} {
  219. return -code error "self-referential recursion\
  220. in \"unknown\" for command \"$name\""
  221. }
  222. set UnknownPending($name) pending
  223. set ret [catch {
  224. auto_load $name [uplevel 1 {::namespace current}]
  225. } msg opts]
  226. unset UnknownPending($name)
  227. if {$ret != 0} {
  228. dict append opts -errorinfo "\n (autoloading \"$name\")"
  229. return -options $opts $msg
  230. }
  231. if {![array size UnknownPending]} {
  232. unset UnknownPending
  233. }
  234. if {$msg} {
  235. if {[info exists savedErrorCode]} {
  236. set ::errorCode $savedErrorCode
  237. } else {
  238. unset -nocomplain ::errorCode
  239. }
  240. if {[info exists savedErrorInfo]} {
  241. set errorInfo $savedErrorInfo
  242. } else {
  243. unset -nocomplain errorInfo
  244. }
  245. set code [catch {uplevel 1 $args} msg opts]
  246. if {$code == 1} {
  247. #
  248. # Compute stack trace contribution from the [uplevel].
  249. # Note the dependence on how Tcl_AddErrorInfo, etc.
  250. # construct the stack trace.
  251. #
  252. set errInfo [dict get $opts -errorinfo]
  253. set errCode [dict get $opts -errorcode]
  254. set cinfo $args
  255. if {[string bytelength $cinfo] > 150} {
  256. set cinfo [string range $cinfo 0 150]
  257. while {[string bytelength $cinfo] > 150} {
  258. set cinfo [string range $cinfo 0 end-1]
  259. }
  260. append cinfo ...
  261. }
  262. set tail "\n (\"uplevel\" body line 1)\n invoked\
  263. from within\n\"uplevel 1 \$args\""
  264. set expect "$msg\n while executing\n\"$cinfo\"$tail"
  265. if {$errInfo eq $expect} {
  266. #
  267. # The stack has only the eval from the expanded command
  268. # Do not generate any stack trace here.
  269. #
  270. dict unset opts -errorinfo
  271. dict incr opts -level
  272. return -options $opts $msg
  273. }
  274. #
  275. # Stack trace is nested, trim off just the contribution
  276. # from the extra "eval" of $args due to the "catch" above.
  277. #
  278. set last [string last $tail $errInfo]
  279. if {$last + [string length $tail] != [string length $errInfo]} {
  280. # Very likely cannot happen
  281. return -options $opts $msg
  282. }
  283. set errInfo [string range $errInfo 0 $last-1]
  284. set tail "\"$cinfo\""
  285. set last [string last $tail $errInfo]
  286. if {$last + [string length $tail] != [string length $errInfo]} {
  287. return -code error -errorcode $errCode \
  288. -errorinfo $errInfo $msg
  289. }
  290. set errInfo [string range $errInfo 0 $last-1]
  291. set tail "\n invoked from within\n"
  292. set last [string last $tail $errInfo]
  293. if {$last + [string length $tail] == [string length $errInfo]} {
  294. return -code error -errorcode $errCode \
  295. -errorinfo [string range $errInfo 0 $last-1] $msg
  296. }
  297. set tail "\n while executing\n"
  298. set last [string last $tail $errInfo]
  299. if {$last + [string length $tail] == [string length $errInfo]} {
  300. return -code error -errorcode $errCode \
  301. -errorinfo [string range $errInfo 0 $last-1] $msg
  302. }
  303. return -options $opts $msg
  304. } else {
  305. dict incr opts -level
  306. return -options $opts $msg
  307. }
  308. }
  309. }
  310. if {([info level] == 1) && ([info script] eq "")
  311. && [info exists tcl_interactive] && $tcl_interactive} {
  312. if {![info exists auto_noexec]} {
  313. set new [auto_execok $name]
  314. if {$new ne ""} {
  315. set redir ""
  316. if {[namespace which -command console] eq ""} {
  317. set redir ">&@stdout <@stdin"
  318. }
  319. uplevel 1 [list ::catch \
  320. [concat exec $redir $new [lrange $args 1 end]] \
  321. ::tcl::UnknownResult ::tcl::UnknownOptions]
  322. dict incr ::tcl::UnknownOptions -level
  323. return -options $::tcl::UnknownOptions $::tcl::UnknownResult
  324. }
  325. }
  326. if {$name eq "!!"} {
  327. set newcmd [history event]
  328. } elseif {[regexp {^!(.+)$} $name -> event]} {
  329. set newcmd [history event $event]
  330. } elseif {[regexp {^\^([^^]*)\^([^^]*)\^?$} $name -> old new]} {
  331. set newcmd [history event -1]
  332. catch {regsub -all -- $old $newcmd $new newcmd}
  333. }
  334. if {[info exists newcmd]} {
  335. tclLog $newcmd
  336. history change $newcmd 0
  337. uplevel 1 [list ::catch $newcmd \
  338. ::tcl::UnknownResult ::tcl::UnknownOptions]
  339. dict incr ::tcl::UnknownOptions -level
  340. return -options $::tcl::UnknownOptions $::tcl::UnknownResult
  341. }
  342. set ret [catch {set candidates [info commands $name*]} msg]
  343. if {$name eq "::"} {
  344. set name ""
  345. }
  346. if {$ret != 0} {
  347. dict append opts -errorinfo \
  348. "\n (expanding command prefix \"$name\" in unknown)"
  349. return -options $opts $msg
  350. }
  351. # Filter out bogus matches when $name contained
  352. # a glob-special char [Bug 946952]
  353. if {$name eq ""} {
  354. # Handle empty $name separately due to strangeness
  355. # in [string first] (See RFE 1243354)
  356. set cmds $candidates
  357. } else {
  358. set cmds [list]
  359. foreach x $candidates {
  360. if {[string first $name $x] == 0} {
  361. lappend cmds $x
  362. }
  363. }
  364. }
  365. if {[llength $cmds] == 1} {
  366. uplevel 1 [list ::catch [lreplace $args 0 0 [lindex $cmds 0]] \
  367. ::tcl::UnknownResult ::tcl::UnknownOptions]
  368. dict incr ::tcl::UnknownOptions -level
  369. return -options $::tcl::UnknownOptions $::tcl::UnknownResult
  370. }
  371. if {[llength $cmds]} {
  372. return -code error "ambiguous command name \"$name\": [lsort $cmds]"
  373. }
  374. }
  375. return -code error -errorcode [list TCL LOOKUP COMMAND $name] \
  376. "invalid command name \"$name\""
  377. }
  378. # auto_load --
  379. # Checks a collection of library directories to see if a procedure
  380. # is defined in one of them. If so, it sources the appropriate
  381. # library file to create the procedure. Returns 1 if it successfully
  382. # loaded the procedure, 0 otherwise.
  383. #
  384. # Arguments:
  385. # cmd - Name of the command to find and load.
  386. # namespace (optional) The namespace where the command is being used - must be
  387. # a canonical namespace as returned [namespace current]
  388. # for instance. If not given, namespace current is used.
  389. proc auto_load {cmd {namespace {}}} {
  390. global auto_index auto_path
  391. if {$namespace eq ""} {
  392. set namespace [uplevel 1 [list ::namespace current]]
  393. }
  394. set nameList [auto_qualify $cmd $namespace]
  395. # workaround non canonical auto_index entries that might be around
  396. # from older auto_mkindex versions
  397. lappend nameList $cmd
  398. foreach name $nameList {
  399. if {[info exists auto_index($name)]} {
  400. namespace eval :: $auto_index($name)
  401. # There's a couple of ways to look for a command of a given
  402. # name. One is to use
  403. # info commands $name
  404. # Unfortunately, if the name has glob-magic chars in it like *
  405. # or [], it may not match. For our purposes here, a better
  406. # route is to use
  407. # namespace which -command $name
  408. if {[namespace which -command $name] ne ""} {
  409. return 1
  410. }
  411. }
  412. }
  413. if {![info exists auto_path]} {
  414. return 0
  415. }
  416. if {![auto_load_index]} {
  417. return 0
  418. }
  419. foreach name $nameList {
  420. if {[info exists auto_index($name)]} {
  421. namespace eval :: $auto_index($name)
  422. if {[namespace which -command $name] ne ""} {
  423. return 1
  424. }
  425. }
  426. }
  427. return 0
  428. }
  429. # auto_load_index --
  430. # Loads the contents of tclIndex files on the auto_path directory
  431. # list. This is usually invoked within auto_load to load the index
  432. # of available commands. Returns 1 if the index is loaded, and 0 if
  433. # the index is already loaded and up to date.
  434. #
  435. # Arguments:
  436. # None.
  437. proc auto_load_index {} {
  438. variable ::tcl::auto_oldpath
  439. global auto_index auto_path
  440. if {[info exists auto_oldpath] && ($auto_oldpath eq $auto_path)} {
  441. return 0
  442. }
  443. set auto_oldpath $auto_path
  444. # Check if we are a safe interpreter. In that case, we support only
  445. # newer format tclIndex files.
  446. set issafe [interp issafe]
  447. for {set i [expr {[llength $auto_path] - 1}]} {$i >= 0} {incr i -1} {
  448. set dir [lindex $auto_path $i]
  449. set f ""
  450. if {$issafe} {
  451. catch {source [file join $dir tclIndex]}
  452. } elseif {[catch {set f [open [file join $dir tclIndex]]}]} {
  453. continue
  454. } else {
  455. set error [catch {
  456. set id [gets $f]
  457. if {$id eq "# Tcl autoload index file, version 2.0"} {
  458. eval [read $f]
  459. } elseif {$id eq "# Tcl autoload index file: each line identifies a Tcl"} {
  460. while {[gets $f line] >= 0} {
  461. if {([string index $line 0] eq "#") \
  462. || ([llength $line] != 2)} {
  463. continue
  464. }
  465. set name [lindex $line 0]
  466. set auto_index($name) \
  467. "source [file join $dir [lindex $line 1]]"
  468. }
  469. } else {
  470. error "[file join $dir tclIndex] isn't a proper Tcl index file"
  471. }
  472. } msg opts]
  473. if {$f ne ""} {
  474. close $f
  475. }
  476. if {$error} {
  477. return -options $opts $msg
  478. }
  479. }
  480. }
  481. return 1
  482. }
  483. # auto_qualify --
  484. #
  485. # Compute a fully qualified names list for use in the auto_index array.
  486. # For historical reasons, commands in the global namespace do not have leading
  487. # :: in the index key. The list has two elements when the command name is
  488. # relative (no leading ::) and the namespace is not the global one. Otherwise
  489. # only one name is returned (and searched in the auto_index).
  490. #
  491. # Arguments -
  492. # cmd The command name. Can be any name accepted for command
  493. # invocations (Like "foo::::bar").
  494. # namespace The namespace where the command is being used - must be
  495. # a canonical namespace as returned by [namespace current]
  496. # for instance.
  497. proc auto_qualify {cmd namespace} {
  498. # count separators and clean them up
  499. # (making sure that foo:::::bar will be treated as foo::bar)
  500. set n [regsub -all {::+} $cmd :: cmd]
  501. # Ignore namespace if the name starts with ::
  502. # Handle special case of only leading ::
  503. # Before each return case we give an example of which category it is
  504. # with the following form :
  505. # (inputCmd, inputNameSpace) -> output
  506. if {[string match ::* $cmd]} {
  507. if {$n > 1} {
  508. # (::foo::bar , *) -> ::foo::bar
  509. return [list $cmd]
  510. } else {
  511. # (::global , *) -> global
  512. return [list [string range $cmd 2 end]]
  513. }
  514. }
  515. # Potentially returning 2 elements to try :
  516. # (if the current namespace is not the global one)
  517. if {$n == 0} {
  518. if {$namespace eq "::"} {
  519. # (nocolons , ::) -> nocolons
  520. return [list $cmd]
  521. } else {
  522. # (nocolons , ::sub) -> ::sub::nocolons nocolons
  523. return [list ${namespace}::$cmd $cmd]
  524. }
  525. } elseif {$namespace eq "::"} {
  526. # (foo::bar , ::) -> ::foo::bar
  527. return [list ::$cmd]
  528. } else {
  529. # (foo::bar , ::sub) -> ::sub::foo::bar ::foo::bar
  530. return [list ${namespace}::$cmd ::$cmd]
  531. }
  532. }
  533. # auto_import --
  534. #
  535. # Invoked during "namespace import" to make see if the imported commands
  536. # reside in an autoloaded library. If so, the commands are loaded so
  537. # that they will be available for the import links. If not, then this
  538. # procedure does nothing.
  539. #
  540. # Arguments -
  541. # pattern The pattern of commands being imported (like "foo::*")
  542. # a canonical namespace as returned by [namespace current]
  543. proc auto_import {pattern} {
  544. global auto_index
  545. # If no namespace is specified, this will be an error case
  546. if {![string match *::* $pattern]} {
  547. return
  548. }
  549. set ns [uplevel 1 [list ::namespace current]]
  550. set patternList [auto_qualify $pattern $ns]
  551. auto_load_index
  552. foreach pattern $patternList {
  553. foreach name [array names auto_index $pattern] {
  554. if {([namespace which -command $name] eq "")
  555. && ([namespace qualifiers $pattern] eq [namespace qualifiers $name])} {
  556. namespace eval :: $auto_index($name)
  557. }
  558. }
  559. }
  560. }
  561. # auto_execok --
  562. #
  563. # Returns string that indicates name of program to execute if
  564. # name corresponds to a shell builtin or an executable in the
  565. # Windows search path, or "" otherwise. Builds an associative
  566. # array auto_execs that caches information about previous checks,
  567. # for speed.
  568. #
  569. # Arguments:
  570. # name - Name of a command.
  571. if {$tcl_platform(platform) eq "windows"} {
  572. # Windows version.
  573. #
  574. # Note that file executable doesn't work under Windows, so we have to
  575. # look for files with .exe, .com, or .bat extensions. Also, the path
  576. # may be in the Path or PATH environment variables, and path
  577. # components are separated with semicolons, not colons as under Unix.
  578. #
  579. proc auto_execok name {
  580. global auto_execs env tcl_platform
  581. if {[info exists auto_execs($name)]} {
  582. return $auto_execs($name)
  583. }
  584. set auto_execs($name) ""
  585. set shellBuiltins [list assoc cls copy date del dir echo erase ftype \
  586. md mkdir mklink move rd ren rename rmdir start time type ver vol]
  587. if {[info exists env(PATHEXT)]} {
  588. # Add an initial ; to have the {} extension check first.
  589. set execExtensions [split ";$env(PATHEXT)" ";"]
  590. } else {
  591. set execExtensions [list {} .com .exe .bat .cmd]
  592. }
  593. if {[string tolower $name] in $shellBuiltins} {
  594. # When this is command.com for some reason on Win2K, Tcl won't
  595. # exec it unless the case is right, which this corrects. COMSPEC
  596. # may not point to a real file, so do the check.
  597. set cmd $env(COMSPEC)
  598. if {[file exists $cmd]} {
  599. set cmd [file attributes $cmd -shortname]
  600. }
  601. return [set auto_execs($name) [list $cmd /c $name]]
  602. }
  603. if {[llength [file split $name]] != 1} {
  604. foreach ext $execExtensions {
  605. set file ${name}${ext}
  606. if {[file exists $file] && ![file isdirectory $file]} {
  607. return [set auto_execs($name) [list $file]]
  608. }
  609. }
  610. return ""
  611. }
  612. set path "[file dirname [info nameof]];.;"
  613. if {[info exists env(WINDIR)]} {
  614. set windir $env(WINDIR)
  615. }
  616. if {[info exists windir]} {
  617. if {$tcl_platform(os) eq "Windows NT"} {
  618. append path "$windir/system32;"
  619. }
  620. append path "$windir/system;$windir;"
  621. }
  622. foreach var {PATH Path path} {
  623. if {[info exists env($var)]} {
  624. append path ";$env($var)"
  625. }
  626. }
  627. foreach ext $execExtensions {
  628. unset -nocomplain checked
  629. foreach dir [split $path {;}] {
  630. # Skip already checked directories
  631. if {[info exists checked($dir)] || ($dir eq "")} {
  632. continue
  633. }
  634. set checked($dir) {}
  635. set file [file join $dir ${name}${ext}]
  636. if {[file exists $file] && ![file isdirectory $file]} {
  637. return [set auto_execs($name) [list $file]]
  638. }
  639. }
  640. }
  641. return ""
  642. }
  643. } else {
  644. # Unix version.
  645. #
  646. proc auto_execok name {
  647. global auto_execs env
  648. if {[info exists auto_execs($name)]} {
  649. return $auto_execs($name)
  650. }
  651. set auto_execs($name) ""
  652. if {[llength [file split $name]] != 1} {
  653. if {[file executable $name] && ![file isdirectory $name]} {
  654. set auto_execs($name) [list $name]
  655. }
  656. return $auto_execs($name)
  657. }
  658. foreach dir [split $env(PATH) :] {
  659. if {$dir eq ""} {
  660. set dir .
  661. }
  662. set file [file join $dir $name]
  663. if {[file executable $file] && ![file isdirectory $file]} {
  664. set auto_execs($name) [list $file]
  665. return $auto_execs($name)
  666. }
  667. }
  668. return ""
  669. }
  670. }
  671. # ::tcl::CopyDirectory --
  672. #
  673. # This procedure is called by Tcl's core when attempts to call the
  674. # filesystem's copydirectory function fail. The semantics of the call
  675. # are that 'dest' does not yet exist, i.e. dest should become the exact
  676. # image of src. If dest does exist, we throw an error.
  677. #
  678. # Note that making changes to this procedure can change the results
  679. # of running Tcl's tests.
  680. #
  681. # Arguments:
  682. # action - "renaming" or "copying"
  683. # src - source directory
  684. # dest - destination directory
  685. proc tcl::CopyDirectory {action src dest} {
  686. set nsrc [file normalize $src]
  687. set ndest [file normalize $dest]
  688. if {$action eq "renaming"} {
  689. # Can't rename volumes. We could give a more precise
  690. # error message here, but that would break the test suite.
  691. if {$nsrc in [file volumes]} {
  692. return -code error "error $action \"$src\" to\
  693. \"$dest\": trying to rename a volume or move a directory\
  694. into itself"
  695. }
  696. }
  697. if {[file exists $dest]} {
  698. if {$nsrc eq $ndest} {
  699. return -code error "error $action \"$src\" to\
  700. \"$dest\": trying to rename a volume or move a directory\
  701. into itself"
  702. }
  703. if {$action eq "copying"} {
  704. # We used to throw an error here, but, looking more closely
  705. # at the core copy code in tclFCmd.c, if the destination
  706. # exists, then we should only call this function if -force
  707. # is true, which means we just want to over-write. So,
  708. # the following code is now commented out.
  709. #
  710. # return -code error "error $action \"$src\" to\
  711. # \"$dest\": file already exists"
  712. } else {
  713. # Depending on the platform, and on the current
  714. # working directory, the directories '.', '..'
  715. # can be returned in various combinations. Anyway,
  716. # if any other file is returned, we must signal an error.
  717. set existing [glob -nocomplain -directory $dest * .*]
  718. lappend existing {*}[glob -nocomplain -directory $dest \
  719. -type hidden * .*]
  720. foreach s $existing {
  721. if {[file tail $s] ni {. ..}} {
  722. return -code error "error $action \"$src\" to\
  723. \"$dest\": file already exists"
  724. }
  725. }
  726. }
  727. } else {
  728. if {[string first $nsrc $ndest] != -1} {
  729. set srclen [expr {[llength [file split $nsrc]] - 1}]
  730. set ndest [lindex [file split $ndest] $srclen]
  731. if {$ndest eq [file tail $nsrc]} {
  732. return -code error "error $action \"$src\" to\
  733. \"$dest\": trying to rename a volume or move a directory\
  734. into itself"
  735. }
  736. }
  737. file mkdir $dest
  738. }
  739. # Have to be careful to capture both visible and hidden files.
  740. # We will also be more generous to the file system and not
  741. # assume the hidden and non-hidden lists are non-overlapping.
  742. #
  743. # On Unix 'hidden' files begin with '.'. On other platforms
  744. # or filesystems hidden files may have other interpretations.
  745. set filelist [concat [glob -nocomplain -directory $src *] \
  746. [glob -nocomplain -directory $src -types hidden *]]
  747. foreach s [lsort -unique $filelist] {
  748. if {[file tail $s] ni {. ..}} {
  749. file copy -force -- $s [file join $dest [file tail $s]]
  750. }
  751. }
  752. return
  753. }