package.tcl 22 KB

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