| 1 |
# package.tcl -- |
| 2 |
# |
| 3 |
# utility procs formerly in init.tcl which can be loaded on demand |
| 4 |
# for package management. |
| 5 |
# |
| 6 |
# Copyright (c) 1991-1993 The Regents of the University of California. |
| 7 |
# Copyright (c) 1994-1998 Sun Microsystems, Inc. |
| 8 |
# |
| 9 |
# See the file "license.terms" for information on usage and redistribution |
| 10 |
# of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 11 |
# |
| 12 |
|
| 13 |
namespace eval tcl::Pkg {} |
| 14 |
|
| 15 |
# ::tcl::Pkg::CompareExtension -- |
| 16 |
# |
| 17 |
# Used internally by pkg_mkIndex to compare the extension of a file to a given |
| 18 |
# extension. On Windows, it uses a case-insensitive comparison because the |
| 19 |
# file system can be file insensitive. |
| 20 |
# |
| 21 |
# Arguments: |
| 22 |
# fileName name of a file whose extension is compared |
| 23 |
# ext (optional) The extension to compare against; you must |
| 24 |
# provide the starting dot. |
| 25 |
# Defaults to [info sharedlibextension] |
| 26 |
# |
| 27 |
# Results: |
| 28 |
# Returns 1 if the extension matches, 0 otherwise |
| 29 |
|
| 30 |
proc tcl::Pkg::CompareExtension {fileName {ext {}}} { |
| 31 |
global tcl_platform |
| 32 |
if {$ext eq ""} {set ext [info sharedlibextension]} |
| 33 |
if {$tcl_platform(platform) eq "windows"} { |
| 34 |
return [string equal -nocase [file extension $fileName] $ext] |
| 35 |
} else { |
| 36 |
# Some unices add trailing numbers after the .so, so |
| 37 |
# we could have something like '.so.1.2'. |
| 38 |
set root $fileName |
| 39 |
while {1} { |
| 40 |
set currExt [file extension $root] |
| 41 |
if {$currExt eq $ext} { |
| 42 |
return 1 |
| 43 |
} |
| 44 |
|
| 45 |
# The current extension does not match; if it is not a numeric |
| 46 |
# value, quit, as we are only looking to ignore version number |
| 47 |
# extensions. Otherwise we might return 1 in this case: |
| 48 |
# tcl::Pkg::CompareExtension foo.so.bar .so |
| 49 |
# which should not match. |
| 50 |
|
| 51 |
if {![string is integer -strict [string range $currExt 1 end]]} { |
| 52 |
return 0 |
| 53 |
} |
| 54 |
set root [file rootname $root] |
| 55 |
} |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
# pkg_mkIndex -- |
| 60 |
# This procedure creates a package index in a given directory. The package |
| 61 |
# index consists of a "pkgIndex.tcl" file whose contents are a Tcl script that |
| 62 |
# sets up package information with "package require" commands. The commands |
| 63 |
# describe all of the packages defined by the files given as arguments. |
| 64 |
# |
| 65 |
# Arguments: |
| 66 |
# -direct (optional) If this flag is present, the generated |
| 67 |
# code in pkgMkIndex.tcl will cause the package to be |
| 68 |
# loaded when "package require" is executed, rather |
| 69 |
# than lazily when the first reference to an exported |
| 70 |
# procedure in the package is made. |
| 71 |
# -verbose (optional) Verbose output; the name of each file that |
| 72 |
# was successfully processed is printed out. Additionally, |
| 73 |
# if processing of a file failed a message is printed. |
| 74 |
# -load pat (optional) Preload any packages whose names match |
| 75 |
# the pattern. Used to handle DLLs that depend on |
| 76 |
# other packages during their Init procedure. |
| 77 |
# dir - Name of the directory in which to create the index. |
| 78 |
# args - Any number of additional arguments, each giving |
| 79 |
# a glob pattern that matches the names of one or |
| 80 |
# more shared libraries or Tcl script files in |
| 81 |
# dir. |
| 82 |
|
| 83 |
proc pkg_mkIndex {args} { |
| 84 |
set usage {"pkg_mkIndex ?-direct? ?-lazy? ?-load pattern? ?-verbose? ?--? dir ?pattern ...?"} |
| 85 |
|
| 86 |
set argCount [llength $args] |
| 87 |
if {$argCount < 1} { |
| 88 |
return -code error "wrong # args: should be\n$usage" |
| 89 |
} |
| 90 |
|
| 91 |
set more "" |
| 92 |
set direct 1 |
| 93 |
set doVerbose 0 |
| 94 |
set loadPat "" |
| 95 |
for {set idx 0} {$idx < $argCount} {incr idx} { |
| 96 |
set flag [lindex $args $idx] |
| 97 |
switch -glob -- $flag { |
| 98 |
-- { |
| 99 |
# done with the flags |
| 100 |
incr idx |
| 101 |
break |
| 102 |
} |
| 103 |
-verbose { |
| 104 |
set doVerbose 1 |
| 105 |
} |
| 106 |
-lazy { |
| 107 |
set direct 0 |
| 108 |
append more " -lazy" |
| 109 |
} |
| 110 |
-direct { |
| 111 |
append more " -direct" |
| 112 |
} |
| 113 |
-load { |
| 114 |
incr idx |
| 115 |
set loadPat [lindex $args $idx] |
| 116 |
append more " -load $loadPat" |
| 117 |
} |
| 118 |
-* { |
| 119 |
return -code error "unknown flag $flag: should be\n$usage" |
| 120 |
} |
| 121 |
default { |
| 122 |
# done with the flags |
| 123 |
break |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
set dir [lindex $args $idx] |
| 129 |
set patternList [lrange $args [expr {$idx + 1}] end] |
| 130 |
if {![llength $patternList]} { |
| 131 |
set patternList [list "*.tcl" "*[info sharedlibextension]"] |
| 132 |
} |
| 133 |
|
| 134 |
try { |
| 135 |
set fileList [glob -directory $dir -tails -types {r f} -- \ |
| 136 |
{*}$patternList] |
| 137 |
} on error {msg opt} { |
| 138 |
return -options $opt $msg |
| 139 |
} |
| 140 |
foreach file $fileList { |
| 141 |
# For each file, figure out what commands and packages it provides. |
| 142 |
# To do this, create a child interpreter, load the file into the |
| 143 |
# interpreter, and get a list of the new commands and packages that |
| 144 |
# are defined. |
| 145 |
|
| 146 |
if {$file eq "pkgIndex.tcl"} { |
| 147 |
continue |
| 148 |
} |
| 149 |
|
| 150 |
set c [interp create] |
| 151 |
|
| 152 |
# Load into the child any packages currently loaded in the parent |
| 153 |
# interpreter that match the -load pattern. |
| 154 |
|
| 155 |
if {$loadPat ne ""} { |
| 156 |
if {$doVerbose} { |
| 157 |
tclLog "currently loaded packages: '[info loaded]'" |
| 158 |
tclLog "trying to load all packages matching $loadPat" |
| 159 |
} |
| 160 |
if {![llength [info loaded]]} { |
| 161 |
tclLog "warning: no packages are currently loaded, nothing" |
| 162 |
tclLog "can possibly match '$loadPat'" |
| 163 |
} |
| 164 |
} |
| 165 |
foreach pkg [info loaded] { |
| 166 |
if {![string match -nocase $loadPat [lindex $pkg 1]]} { |
| 167 |
continue |
| 168 |
} |
| 169 |
if {$doVerbose} { |
| 170 |
tclLog "package [lindex $pkg 1] matches '$loadPat'" |
| 171 |
} |
| 172 |
try { |
| 173 |
load [lindex $pkg 0] [lindex $pkg 1] $c |
| 174 |
} on error err { |
| 175 |
if {$doVerbose} { |
| 176 |
tclLog "warning: load [lindex $pkg 0]\ |
| 177 |
[lindex $pkg 1]\nfailed with: $err" |
| 178 |
} |
| 179 |
} on ok {} { |
| 180 |
if {$doVerbose} { |
| 181 |
tclLog "loaded [lindex $pkg 0] [lindex $pkg 1]" |
| 182 |
} |
| 183 |
} |
| 184 |
if {[lindex $pkg 1] eq "Tk"} { |
| 185 |
# Withdraw . if Tk was loaded, to avoid showing a window. |
| 186 |
$c eval [list wm withdraw .] |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
$c eval { |
| 191 |
# Stub out the package command so packages can require other |
| 192 |
# packages. |
| 193 |
|
| 194 |
rename package __package_orig |
| 195 |
proc package {what args} { |
| 196 |
switch -- $what { |
| 197 |
require { |
| 198 |
return; # Ignore transitive requires |
| 199 |
} |
| 200 |
default { |
| 201 |
__package_orig $what {*}$args |
| 202 |
} |
| 203 |
} |
| 204 |
} |
| 205 |
proc tclPkgUnknown args {} |
| 206 |
package unknown tclPkgUnknown |
| 207 |
|
| 208 |
# Stub out the unknown command so package can call into each other |
| 209 |
# during their initialization. |
| 210 |
|
| 211 |
proc unknown {args} {} |
| 212 |
|
| 213 |
# Stub out the auto_import mechanism |
| 214 |
|
| 215 |
proc auto_import {args} {} |
| 216 |
|
| 217 |
# reserve the ::tcl namespace for support procs and temporary |
| 218 |
# variables. This might make it awkward to generate a |
| 219 |
# pkgIndex.tcl file for the ::tcl namespace. |
| 220 |
|
| 221 |
namespace eval ::tcl { |
| 222 |
variable dir ;# Current directory being processed |
| 223 |
variable file ;# Current file being processed |
| 224 |
variable direct ;# -direct flag value |
| 225 |
variable x ;# Loop variable |
| 226 |
variable debug ;# For debugging |
| 227 |
variable type ;# "load" or "source", for -direct |
| 228 |
variable namespaces ;# Existing namespaces (e.g., ::tcl) |
| 229 |
variable packages ;# Existing packages (e.g., Tcl) |
| 230 |
variable origCmds ;# Existing commands |
| 231 |
variable newCmds ;# Newly created commands |
| 232 |
variable newPkgs {} ;# Newly created packages |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
$c eval [list set ::tcl::dir $dir] |
| 237 |
$c eval [list set ::tcl::file $file] |
| 238 |
$c eval [list set ::tcl::direct $direct] |
| 239 |
|
| 240 |
# Download needed procedures into the child because we've just deleted |
| 241 |
# the unknown procedure. This doesn't handle procedures with default |
| 242 |
# arguments. |
| 243 |
|
| 244 |
foreach p {::tcl::Pkg::CompareExtension} { |
| 245 |
$c eval [list namespace eval [namespace qualifiers $p] {}] |
| 246 |
$c eval [list proc $p [info args $p] [info body $p]] |
| 247 |
} |
| 248 |
|
| 249 |
try { |
| 250 |
$c eval { |
| 251 |
set ::tcl::debug "loading or sourcing" |
| 252 |
|
| 253 |
# we need to track command defined by each package even in the |
| 254 |
# -direct case, because they are needed internally by the |
| 255 |
# "partial pkgIndex.tcl" step above. |
| 256 |
|
| 257 |
proc ::tcl::GetAllNamespaces {{root ::}} { |
| 258 |
set list $root |
| 259 |
foreach ns [namespace children $root] { |
| 260 |
lappend list {*}[::tcl::GetAllNamespaces $ns] |
| 261 |
} |
| 262 |
return $list |
| 263 |
} |
| 264 |
|
| 265 |
# init the list of existing namespaces, packages, commands |
| 266 |
|
| 267 |
foreach ::tcl::x [::tcl::GetAllNamespaces] { |
| 268 |
set ::tcl::namespaces($::tcl::x) 1 |
| 269 |
} |
| 270 |
foreach ::tcl::x [package names] { |
| 271 |
if {[package provide $::tcl::x] ne ""} { |
| 272 |
set ::tcl::packages($::tcl::x) 1 |
| 273 |
} |
| 274 |
} |
| 275 |
set ::tcl::origCmds [info commands] |
| 276 |
|
| 277 |
# Try to load the file if it has the shared library extension, |
| 278 |
# otherwise source it. It's important not to try to load |
| 279 |
# files that aren't shared libraries, because on some systems |
| 280 |
# (like SunOS) the loader will abort the whole application |
| 281 |
# when it gets an error. |
| 282 |
|
| 283 |
if {[::tcl::Pkg::CompareExtension $::tcl::file [info sharedlibextension]]} { |
| 284 |
# The "file join ." command below is necessary. Without |
| 285 |
# it, if the file name has no \'s and we're on UNIX, the |
| 286 |
# load command will invoke the LD_LIBRARY_PATH search |
| 287 |
# mechanism, which could cause the wrong file to be used. |
| 288 |
|
| 289 |
set ::tcl::debug loading |
| 290 |
load [file join $::tcl::dir $::tcl::file] |
| 291 |
set ::tcl::type load |
| 292 |
} else { |
| 293 |
set ::tcl::debug sourcing |
| 294 |
source -encoding utf-8 [file join $::tcl::dir $::tcl::file] |
| 295 |
set ::tcl::type source |
| 296 |
} |
| 297 |
|
| 298 |
# As a performance optimization, if we are creating direct |
| 299 |
# load packages, don't bother figuring out the set of commands |
| 300 |
# created by the new packages. We only need that list for |
| 301 |
# setting up the autoloading used in the non-direct case. |
| 302 |
if {!$::tcl::direct} { |
| 303 |
# See what new namespaces appeared, and import commands |
| 304 |
# from them. Only exported commands go into the index. |
| 305 |
|
| 306 |
foreach ::tcl::x [::tcl::GetAllNamespaces] { |
| 307 |
if {![info exists ::tcl::namespaces($::tcl::x)]} { |
| 308 |
namespace import -force ${::tcl::x}::* |
| 309 |
} |
| 310 |
|
| 311 |
# Figure out what commands appeared |
| 312 |
|
| 313 |
foreach ::tcl::x [info commands] { |
| 314 |
set ::tcl::newCmds($::tcl::x) 1 |
| 315 |
} |
| 316 |
foreach ::tcl::x $::tcl::origCmds { |
| 317 |
unset -nocomplain ::tcl::newCmds($::tcl::x) |
| 318 |
} |
| 319 |
foreach ::tcl::x [array names ::tcl::newCmds] { |
| 320 |
# determine which namespace a command comes from |
| 321 |
|
| 322 |
set ::tcl::abs [namespace origin $::tcl::x] |
| 323 |
|
| 324 |
# special case so that global names have no |
| 325 |
# leading ::, this is required by the unknown |
| 326 |
# command |
| 327 |
|
| 328 |
set ::tcl::abs \ |
| 329 |
[lindex [auto_qualify $::tcl::abs ::] 0] |
| 330 |
|
| 331 |
if {$::tcl::x ne $::tcl::abs} { |
| 332 |
# Name changed during qualification |
| 333 |
|
| 334 |
set ::tcl::newCmds($::tcl::abs) 1 |
| 335 |
unset ::tcl::newCmds($::tcl::x) |
| 336 |
} |
| 337 |
} |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
# Look through the packages that appeared, and if there is a |
| 342 |
# version provided, then record it |
| 343 |
|
| 344 |
foreach ::tcl::x [package names] { |
| 345 |
if {[package provide $::tcl::x] ne "" |
| 346 |
&& ![info exists ::tcl::packages($::tcl::x)]} { |
| 347 |
lappend ::tcl::newPkgs \ |
| 348 |
[list $::tcl::x [package provide $::tcl::x]] |
| 349 |
} |
| 350 |
} |
| 351 |
} |
| 352 |
} on error msg { |
| 353 |
set what [$c eval set ::tcl::debug] |
| 354 |
if {$doVerbose} { |
| 355 |
tclLog "warning: error while $what $file: $msg" |
| 356 |
} |
| 357 |
} on ok {} { |
| 358 |
set what [$c eval set ::tcl::debug] |
| 359 |
if {$doVerbose} { |
| 360 |
tclLog "successful $what of $file" |
| 361 |
} |
| 362 |
set type [$c eval set ::tcl::type] |
| 363 |
set cmds [lsort [$c eval array names ::tcl::newCmds]] |
| 364 |
set pkgs [$c eval set ::tcl::newPkgs] |
| 365 |
if {$doVerbose} { |
| 366 |
if {!$direct} { |
| 367 |
tclLog "commands provided were $cmds" |
| 368 |
} |
| 369 |
tclLog "packages provided were $pkgs" |
| 370 |
} |
| 371 |
if {[llength $pkgs] > 1} { |
| 372 |
tclLog "warning: \"$file\" provides more than one package ($pkgs)" |
| 373 |
} |
| 374 |
foreach pkg $pkgs { |
| 375 |
# cmds is empty/not used in the direct case |
| 376 |
lappend files($pkg) [list $file $type $cmds] |
| 377 |
} |
| 378 |
|
| 379 |
if {$doVerbose} { |
| 380 |
tclLog "processed $file" |
| 381 |
} |
| 382 |
} |
| 383 |
interp delete $c |
| 384 |
} |
| 385 |
|
| 386 |
append index "# Tcl package index file, version 1.1\n" |
| 387 |
append index "# This file is generated by the \"pkg_mkIndex$more\" command\n" |
| 388 |
append index "# and sourced either when an application starts up or\n" |
| 389 |
append index "# by a \"package unknown\" script. It invokes the\n" |
| 390 |
append index "# \"package ifneeded\" command to set up package-related\n" |
| 391 |
append index "# information so that packages will be loaded automatically\n" |
| 392 |
append index "# in response to \"package require\" commands. When this\n" |
| 393 |
append index "# script is sourced, the variable \$dir must contain the\n" |
| 394 |
append index "# full path name of this file's directory.\n" |
| 395 |
|
| 396 |
foreach pkg [lsort [array names files]] { |
| 397 |
set cmd {} |
| 398 |
lassign $pkg name version |
| 399 |
lappend cmd ::tcl::Pkg::Create -name $name -version $version |
| 400 |
foreach spec [lsort -index 0 $files($pkg)] { |
| 401 |
foreach {file type procs} $spec { |
| 402 |
if {$direct} { |
| 403 |
set procs {} |
| 404 |
} |
| 405 |
lappend cmd "-$type" [list $file $procs] |
| 406 |
} |
| 407 |
} |
| 408 |
append index "\n[eval $cmd]" |
| 409 |
} |
| 410 |
|
| 411 |
set f [open [file join $dir pkgIndex.tcl] w] |
| 412 |
puts $f $index |
| 413 |
close $f |
| 414 |
} |
| 415 |
|
| 416 |
# tclPkgSetup -- |
| 417 |
# This is a utility procedure use by pkgIndex.tcl files. It is invoked as |
| 418 |
# part of a "package ifneeded" script. It calls "package provide" to indicate |
| 419 |
# that a package is available, then sets entries in the auto_index array so |
| 420 |
# that the package's files will be auto-loaded when the commands are used. |
| 421 |
# |
| 422 |
# Arguments: |
| 423 |
# dir - Directory containing all the files for this package. |
| 424 |
# pkg - Name of the package (no version number). |
| 425 |
# version - Version number for the package, such as 2.1.3. |
| 426 |
# files - List of files that constitute the package. Each |
| 427 |
# element is a sub-list with three elements. The first |
| 428 |
# is the name of a file relative to $dir, the second is |
| 429 |
# "load" or "source", indicating whether the file is a |
| 430 |
# loadable binary or a script to source, and the third |
| 431 |
# is a list of commands defined by this file. |
| 432 |
|
| 433 |
proc tclPkgSetup {dir pkg version files} { |
| 434 |
global auto_index |
| 435 |
|
| 436 |
package provide $pkg $version |
| 437 |
foreach fileInfo $files { |
| 438 |
set f [lindex $fileInfo 0] |
| 439 |
set type [lindex $fileInfo 1] |
| 440 |
foreach cmd [lindex $fileInfo 2] { |
| 441 |
if {$type eq "load"} { |
| 442 |
set auto_index($cmd) [list load [file join $dir $f] $pkg] |
| 443 |
} else { |
| 444 |
set auto_index($cmd) [list source -encoding utf-8 [file join $dir $f]] |
| 445 |
} |
| 446 |
} |
| 447 |
} |
| 448 |
} |
| 449 |
|
| 450 |
# tclPkgUnknown -- |
| 451 |
# This procedure provides the default for the "package unknown" function. It |
| 452 |
# is invoked when a package that's needed can't be found. It scans the |
| 453 |
# auto_path directories and their immediate children looking for pkgIndex.tcl |
| 454 |
# files and sources any such files that are found to setup the package |
| 455 |
# database. As it searches, it will recognize changes to the auto_path and |
| 456 |
# scan any new directories. |
| 457 |
# |
| 458 |
# Arguments: |
| 459 |
# name - Name of desired package. Not used. |
| 460 |
# version - Version of desired package. Not used. |
| 461 |
# exact - Either "-exact" or omitted. Not used. |
| 462 |
|
| 463 |
proc tclPkgUnknown {name args} { |
| 464 |
global auto_path env |
| 465 |
|
| 466 |
if {![info exists auto_path]} { |
| 467 |
return |
| 468 |
} |
| 469 |
# Cache the auto_path, because it may change while we run through the |
| 470 |
# first set of pkgIndex.tcl files |
| 471 |
set old_path [set use_path $auto_path] |
| 472 |
while {[llength $use_path]} { |
| 473 |
set dir [lindex $use_path end] |
| 474 |
|
| 475 |
# Make sure we only scan each directory one time. |
| 476 |
if {[info exists tclSeenPath($dir)]} { |
| 477 |
set use_path [lrange $use_path 0 end-1] |
| 478 |
continue |
| 479 |
} |
| 480 |
set tclSeenPath($dir) 1 |
| 481 |
|
| 482 |
# Get the pkgIndex.tcl files in subdirectories of auto_path directories. |
| 483 |
# - Safe Base interpreters have a restricted "glob" command that |
| 484 |
# works in this case. |
| 485 |
# - The "catch" was essential when there was no safe glob and every |
| 486 |
# call in a safe interp failed; it is retained only for corner |
| 487 |
# cases in which the eventual call to glob returns an error. |
| 488 |
catch { |
| 489 |
foreach file [glob -directory $dir -join -nocomplain \ |
| 490 |
* pkgIndex.tcl] { |
| 491 |
set dir [file dirname $file] |
| 492 |
if {![info exists procdDirs($dir)]} { |
| 493 |
try { |
| 494 |
source -encoding utf-8 $file |
| 495 |
} trap {POSIX EACCES} {} { |
| 496 |
# $file was not readable; silently ignore |
| 497 |
continue |
| 498 |
} trap {TCL PACKAGE VERSIONCONFLICT} {} { |
| 499 |
# In case of version conflict, silently ignore |
| 500 |
continue |
| 501 |
} on error msg { |
| 502 |
tclLog "error reading package index file $file: $msg" |
| 503 |
} on ok {} { |
| 504 |
set procdDirs($dir) 1 |
| 505 |
} |
| 506 |
} |
| 507 |
} |
| 508 |
} |
| 509 |
set dir [lindex $use_path end] |
| 510 |
if {![info exists procdDirs($dir)]} { |
| 511 |
set file [file join $dir pkgIndex.tcl] |
| 512 |
# safe interps usually don't have "file exists", |
| 513 |
if {[interp issafe] || [file exists $file]} { |
| 514 |
try { |
| 515 |
source -encoding utf-8 $file |
| 516 |
} trap {POSIX EACCES} {} { |
| 517 |
# $file was not readable; silently ignore |
| 518 |
continue |
| 519 |
} trap {TCL PACKAGE VERSIONCONFLICT} {} { |
| 520 |
# In case of version conflict, silently ignore |
| 521 |
continue |
| 522 |
} on error msg { |
| 523 |
tclLog "error reading package index file $file: $msg" |
| 524 |
} on ok {} { |
| 525 |
set procdDirs($dir) 1 |
| 526 |
} |
| 527 |
} |
| 528 |
} |
| 529 |
|
| 530 |
set use_path [lrange $use_path 0 end-1] |
| 531 |
|
| 532 |
# Check whether any of the index scripts we [source]d above set a new |
| 533 |
# value for $::auto_path. If so, then find any new directories on the |
| 534 |
# $::auto_path, and lappend them to the $use_path we are working from. |
| 535 |
# This gives index scripts the (arguably unwise) power to expand the |
| 536 |
# index script search path while the search is in progress. |
| 537 |
set index 0 |
| 538 |
if {[llength $old_path] == [llength $auto_path]} { |
| 539 |
foreach dir $auto_path old $old_path { |
| 540 |
if {$dir ne $old} { |
| 541 |
# This entry in $::auto_path has changed. |
| 542 |
break |
| 543 |
} |
| 544 |
incr index |
| 545 |
} |
| 546 |
} |
| 547 |
|
| 548 |
# $index now points to the first element of $auto_path that has |
| 549 |
# changed, or the beginning if $auto_path has changed length Scan the |
| 550 |
# new elements of $auto_path for directories to add to $use_path. |
| 551 |
# Don't add directories we've already seen, or ones already on the |
| 552 |
# $use_path. |
| 553 |
foreach dir [lrange $auto_path $index end] { |
| 554 |
if {![info exists tclSeenPath($dir)] && ($dir ni $use_path)} { |
| 555 |
lappend use_path $dir |
| 556 |
} |
| 557 |
} |
| 558 |
set old_path $auto_path |
| 559 |
} |
| 560 |
} |
| 561 |
|
| 562 |
# tcl::MacOSXPkgUnknown -- |
| 563 |
# This procedure extends the "package unknown" function for MacOSX. It scans |
| 564 |
# the Resources/Scripts directories of the immediate children of the auto_path |
| 565 |
# directories for pkgIndex files. |
| 566 |
# |
| 567 |
# Arguments: |
| 568 |
# original - original [package unknown] procedure |
| 569 |
# name - Name of desired package. Not used. |
| 570 |
# version - Version of desired package. Not used. |
| 571 |
# exact - Either "-exact" or omitted. Not used. |
| 572 |
|
| 573 |
proc tcl::MacOSXPkgUnknown {original name args} { |
| 574 |
# First do the cross-platform default search |
| 575 |
uplevel 1 $original [linsert $args 0 $name] |
| 576 |
|
| 577 |
# Now do MacOSX specific searching |
| 578 |
global auto_path |
| 579 |
|
| 580 |
if {![info exists auto_path]} { |
| 581 |
return |
| 582 |
} |
| 583 |
# Cache the auto_path, because it may change while we run through the |
| 584 |
# first set of pkgIndex.tcl files |
| 585 |
set old_path [set use_path $auto_path] |
| 586 |
while {[llength $use_path]} { |
| 587 |
set dir [lindex $use_path end] |
| 588 |
|
| 589 |
# Make sure we only scan each directory one time. |
| 590 |
if {[info exists tclSeenPath($dir)]} { |
| 591 |
set use_path [lrange $use_path 0 end-1] |
| 592 |
continue |
| 593 |
} |
| 594 |
set tclSeenPath($dir) 1 |
| 595 |
|
| 596 |
# get the pkgIndex files out of the subdirectories |
| 597 |
# Safe interpreters do not use tcl::MacOSXPkgUnknown - see init.tcl. |
| 598 |
foreach file [glob -directory $dir -join -nocomplain \ |
| 599 |
* Resources Scripts pkgIndex.tcl] { |
| 600 |
set dir [file dirname $file] |
| 601 |
if {![info exists procdDirs($dir)]} { |
| 602 |
try { |
| 603 |
source $file |
| 604 |
} trap {POSIX EACCES} {} { |
| 605 |
# $file was not readable; silently ignore |
| 606 |
continue |
| 607 |
} trap {TCL PACKAGE VERSIONCONFLICT} {} { |
| 608 |
# In case of version conflict, silently ignore |
| 609 |
continue |
| 610 |
} on error msg { |
| 611 |
tclLog "error reading package index file $file: $msg" |
| 612 |
} on ok {} { |
| 613 |
set procdDirs($dir) 1 |
| 614 |
} |
| 615 |
} |
| 616 |
} |
| 617 |
set use_path [lrange $use_path 0 end-1] |
| 618 |
|
| 619 |
# Check whether any of the index scripts we [source]d above set a new |
| 620 |
# value for $::auto_path. If so, then find any new directories on the |
| 621 |
# $::auto_path, and lappend them to the $use_path we are working from. |
| 622 |
# This gives index scripts the (arguably unwise) power to expand the |
| 623 |
# index script search path while the search is in progress. |
| 624 |
set index 0 |
| 625 |
if {[llength $old_path] == [llength $auto_path]} { |
| 626 |
foreach dir $auto_path old $old_path { |
| 627 |
if {$dir ne $old} { |
| 628 |
# This entry in $::auto_path has changed. |
| 629 |
break |
| 630 |
} |
| 631 |
incr index |
| 632 |
} |
| 633 |
} |
| 634 |
|
| 635 |
# $index now points to the first element of $auto_path that has |
| 636 |
# changed, or the beginning if $auto_path has changed length Scan the |
| 637 |
# new elements of $auto_path for directories to add to $use_path. |
| 638 |
# Don't add directories we've already seen, or ones already on the |
| 639 |
# $use_path. |
| 640 |
foreach dir [lrange $auto_path $index end] { |
| 641 |
if {![info exists tclSeenPath($dir)] && ($dir ni $use_path)} { |
| 642 |
lappend use_path $dir |
| 643 |
} |
| 644 |
} |
| 645 |
set old_path $auto_path |
| 646 |
} |
| 647 |
} |
| 648 |
|
| 649 |
# ::tcl::Pkg::Create -- |
| 650 |
# |
| 651 |
# Given a package specification generate a "package ifneeded" statement |
| 652 |
# for the package, suitable for inclusion in a pkgIndex.tcl file. |
| 653 |
# |
| 654 |
# Arguments: |
| 655 |
# args arguments used by the Create function: |
| 656 |
# -name packageName |
| 657 |
# -version packageVersion |
| 658 |
# -load {filename ?{procs}?} |
| 659 |
# ... |
| 660 |
# -source {filename ?{procs}?} |
| 661 |
# ... |
| 662 |
# |
| 663 |
# Any number of -load and -source parameters may be |
| 664 |
# specified, so long as there is at least one -load or |
| 665 |
# -source parameter. If the procs component of a module |
| 666 |
# specifier is left off, that module will be set up for |
| 667 |
# direct loading; otherwise, it will be set up for lazy |
| 668 |
# loading. If both -source and -load are specified, the |
| 669 |
# -load'ed files will be loaded first, followed by the |
| 670 |
# -source'd files. |
| 671 |
# |
| 672 |
# Results: |
| 673 |
# An appropriate "package ifneeded" statement for the package. |
| 674 |
|
| 675 |
proc ::tcl::Pkg::Create {args} { |
| 676 |
append err(usage) "[lindex [info level 0] 0] " |
| 677 |
append err(usage) "-name packageName -version packageVersion" |
| 678 |
append err(usage) "?-load {filename ?{procs}?}? ... " |
| 679 |
append err(usage) "?-source {filename ?{procs}?}? ..." |
| 680 |
|
| 681 |
set err(wrongNumArgs) "wrong # args: should be \"$err(usage)\"" |
| 682 |
set err(valueMissing) "value for \"%s\" missing: should be \"$err(usage)\"" |
| 683 |
set err(unknownOpt) "unknown option \"%s\": should be \"$err(usage)\"" |
| 684 |
set err(noLoadOrSource) "at least one of -load and -source must be given" |
| 685 |
|
| 686 |
# process arguments |
| 687 |
set len [llength $args] |
| 688 |
if {$len < 6} { |
| 689 |
error $err(wrongNumArgs) |
| 690 |
} |
| 691 |
|
| 692 |
# Initialize parameters |
| 693 |
array set opts {-name {} -version {} -source {} -load {}} |
| 694 |
|
| 695 |
# process parameters |
| 696 |
for {set i 0} {$i < $len} {incr i} { |
| 697 |
set flag [lindex $args $i] |
| 698 |
incr i |
| 699 |
switch -glob -- $flag { |
| 700 |
"-name" - |
| 701 |
"-version" { |
| 702 |
if {$i >= $len} { |
| 703 |
error [format $err(valueMissing) $flag] |
| 704 |
} |
| 705 |
set opts($flag) [lindex $args $i] |
| 706 |
} |
| 707 |
"-source" - |
| 708 |
"-load" { |
| 709 |
if {$i >= $len} { |
| 710 |
error [format $err(valueMissing) $flag] |
| 711 |
} |
| 712 |
lappend opts($flag) [lindex $args $i] |
| 713 |
} |
| 714 |
default { |
| 715 |
error [format $err(unknownOpt) [lindex $args $i]] |
| 716 |
} |
| 717 |
} |
| 718 |
} |
| 719 |
|
| 720 |
# Validate the parameters |
| 721 |
if {![llength $opts(-name)]} { |
| 722 |
error [format $err(valueMissing) "-name"] |
| 723 |
} |
| 724 |
if {![llength $opts(-version)]} { |
| 725 |
error [format $err(valueMissing) "-version"] |
| 726 |
} |
| 727 |
|
| 728 |
if {!([llength $opts(-source)] || [llength $opts(-load)])} { |
| 729 |
error $err(noLoadOrSource) |
| 730 |
} |
| 731 |
|
| 732 |
# OK, now everything is good. Generate the package ifneeded statement. |
| 733 |
set cmdline "package ifneeded $opts(-name) $opts(-version) " |
| 734 |
|
| 735 |
set cmdList {} |
| 736 |
set lazyFileList {} |
| 737 |
|
| 738 |
# Handle -load and -source specs |
| 739 |
foreach key {load source} { |
| 740 |
foreach filespec $opts(-$key) { |
| 741 |
lassign $filespec filename proclist |
| 742 |
|
| 743 |
if { [llength $proclist] == 0 } { |
| 744 |
set cmd "\[list $key \[file join \$dir [list $filename]\]\]" |
| 745 |
lappend cmdList $cmd |
| 746 |
} else { |
| 747 |
lappend lazyFileList [list $filename $key $proclist] |
| 748 |
} |
| 749 |
} |
| 750 |
} |
| 751 |
|
| 752 |
if {[llength $lazyFileList]} { |
| 753 |
lappend cmdList "\[list tclPkgSetup \$dir $opts(-name)\ |
| 754 |
$opts(-version) [list $lazyFileList]\]" |
| 755 |
} |
| 756 |
append cmdline [join $cmdList "\\n"] |
| 757 |
return $cmdline |
| 758 |
} |
| 759 |
|
| 760 |
interp alias {} ::pkg::create {} ::tcl::Pkg::Create |