init.tcl 24 KB

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