init.tcl 24 KB

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