sl@0: # auto.tcl -- sl@0: # sl@0: # utility procs formerly in init.tcl dealing with auto execution sl@0: # of commands and can be auto loaded themselves. sl@0: # sl@0: # RCS: @(#) $Id: auto.tcl,v 1.12.2.10 2005/07/23 03:31:41 dgp Exp $ sl@0: # sl@0: # Copyright (c) 1991-1993 The Regents of the University of California. sl@0: # Copyright (c) 1994-1998 Sun Microsystems, Inc. sl@0: # sl@0: # See the file "license.terms" for information on usage and redistribution sl@0: # of this file, and for a DISCLAIMER OF ALL WARRANTIES. sl@0: # sl@0: sl@0: # auto_reset -- sl@0: # sl@0: # Destroy all cached information for auto-loading and auto-execution, sl@0: # so that the information gets recomputed the next time it's needed. sl@0: # Also delete any procedures that are listed in the auto-load index sl@0: # except those defined in this file. sl@0: # sl@0: # Arguments: sl@0: # None. sl@0: sl@0: proc auto_reset {} { sl@0: global auto_execs auto_index auto_oldpath sl@0: foreach p [info procs] { sl@0: if {[info exists auto_index($p)] && ![string match auto_* $p] sl@0: && ([lsearch -exact {unknown pkg_mkIndex tclPkgSetup sl@0: tcl_findLibrary pkg_compareExtension sl@0: tclPkgUnknown tcl::MacOSXPkgUnknown sl@0: tcl::MacPkgUnknown} $p] < 0)} { sl@0: rename $p {} sl@0: } sl@0: } sl@0: unset -nocomplain auto_execs auto_index auto_oldpath sl@0: } sl@0: sl@0: # tcl_findLibrary -- sl@0: # sl@0: # This is a utility for extensions that searches for a library directory sl@0: # using a canonical searching algorithm. A side effect is to source sl@0: # the initialization script and set a global library variable. sl@0: # sl@0: # Arguments: sl@0: # basename Prefix of the directory name, (e.g., "tk") sl@0: # version Version number of the package, (e.g., "8.0") sl@0: # patch Patchlevel of the package, (e.g., "8.0.3") sl@0: # initScript Initialization script to source (e.g., tk.tcl) sl@0: # enVarName environment variable to honor (e.g., TK_LIBRARY) sl@0: # varName Global variable to set when done (e.g., tk_library) sl@0: sl@0: proc tcl_findLibrary {basename version patch initScript enVarName varName} { sl@0: upvar #0 $varName the_library sl@0: global env errorInfo sl@0: sl@0: set dirs {} sl@0: set errors {} sl@0: sl@0: # The C application may have hardwired a path, which we honor sl@0: sl@0: if {[info exists the_library] && $the_library ne ""} { sl@0: lappend dirs $the_library sl@0: } else { sl@0: sl@0: # Do the canonical search sl@0: sl@0: # 1. From an environment variable, if it exists. sl@0: # Placing this first gives the end-user ultimate control sl@0: # to work-around any bugs, or to customize. sl@0: sl@0: if {[info exists env($enVarName)]} { sl@0: lappend dirs $env($enVarName) sl@0: } sl@0: sl@0: # 2. In the package script directory registered within sl@0: # the configuration of the package itself. sl@0: # sl@0: # Only do this for Tcl 8.5+, when Tcl_RegsiterConfig() is available. sl@0: #if {[catch { sl@0: # ::${basename}::pkgconfig get scriptdir,runtime sl@0: #} value] == 0} { sl@0: # lappend dirs $value sl@0: #} sl@0: sl@0: # 3. Relative to auto_path directories. This checks relative to the sl@0: # Tcl library as well as allowing loading of libraries added to the sl@0: # auto_path that is not relative to the core library or binary paths. sl@0: foreach d $::auto_path { sl@0: lappend dirs [file join $d $basename$version] sl@0: if {$::tcl_platform(platform) eq "unix" sl@0: && $::tcl_platform(os) eq "Darwin"} { sl@0: # 4. On MacOSX, check the Resources/Scripts subdir too sl@0: lappend dirs [file join $d $basename$version Resources Scripts] sl@0: } sl@0: } sl@0: sl@0: # 3. Various locations relative to the executable sl@0: # ../lib/foo1.0 (From bin directory in install hierarchy) sl@0: # ../../lib/foo1.0 (From bin/arch directory in install hierarchy) sl@0: # ../library (From unix directory in build hierarchy) sl@0: set parentDir [file dirname [file dirname [info nameofexecutable]]] sl@0: set grandParentDir [file dirname $parentDir] sl@0: lappend dirs [file join $parentDir lib $basename$version] sl@0: lappend dirs [file join $grandParentDir lib $basename$version] sl@0: lappend dirs [file join $parentDir library] sl@0: sl@0: # Remaining locations are out of date (when relevant, they ought sl@0: # to be covered by the $::auto_path seach above). sl@0: # sl@0: # ../../library (From unix/arch directory in build hierarchy) sl@0: # ../../foo1.0.1/library sl@0: # (From unix directory in parallel build hierarchy) sl@0: # ../../../foo1.0.1/library sl@0: # (From unix/arch directory in parallel build hierarchy) sl@0: # sl@0: # For the sake of extra compatibility safety, we keep adding these sl@0: # paths during the 8.4.* release series. sl@0: if {1} { sl@0: lappend dirs [file join $grandParentDir library] sl@0: lappend dirs [file join $grandParentDir $basename$patch library] sl@0: lappend dirs [file join [file dirname $grandParentDir] \ sl@0: $basename$patch library] sl@0: } sl@0: } sl@0: # uniquify $dirs in order sl@0: array set seen {} sl@0: foreach i $dirs { sl@0: # For Tcl 8.4.9, we've disabled the use of [file normalize] here. sl@0: # This means that two different path names that are the same path sl@0: # in normalized form, will both remain on the search path. There sl@0: # should be no harm in that, just a bit more file system access sl@0: # than is strictly necessary. sl@0: # sl@0: # [file normalize] has been disabled because of reports it has sl@0: # caused difficulties with the freewrap utility. To keep sl@0: # compatibility with freewrap's needs, we'll keep this disabled sl@0: # throughout the 8.4.x (x >= 9) releases. See Bug 1072136. sl@0: if {1 || [interp issafe]} { sl@0: set norm $i sl@0: } else { sl@0: set norm [file normalize $i] sl@0: } sl@0: if {[info exists seen($norm)]} { continue } sl@0: set seen($norm) "" sl@0: lappend uniqdirs $i sl@0: } sl@0: set dirs $uniqdirs sl@0: foreach i $dirs { sl@0: set the_library $i sl@0: set file [file join $i $initScript] sl@0: sl@0: # source everything when in a safe interpreter because sl@0: # we have a source command, but no file exists command sl@0: sl@0: if {[interp issafe] || [file exists $file]} { sl@0: if {![catch {uplevel #0 [list source $file]} msg]} { sl@0: return sl@0: } else { sl@0: append errors "$file: $msg\n$errorInfo\n" sl@0: } sl@0: } sl@0: } sl@0: unset -nocomplain the_library sl@0: set msg "Can't find a usable $initScript in the following directories: \n" sl@0: append msg " $dirs\n\n" sl@0: append msg "$errors\n\n" sl@0: append msg "This probably means that $basename wasn't installed properly.\n" sl@0: error $msg sl@0: } sl@0: sl@0: sl@0: # ---------------------------------------------------------------------- sl@0: # auto_mkindex sl@0: # ---------------------------------------------------------------------- sl@0: # The following procedures are used to generate the tclIndex file sl@0: # from Tcl source files. They use a special safe interpreter to sl@0: # parse Tcl source files, writing out index entries as "proc" sl@0: # commands are encountered. This implementation won't work in a sl@0: # safe interpreter, since a safe interpreter can't create the sl@0: # special parser and mess with its commands. sl@0: sl@0: if {[interp issafe]} { sl@0: return ;# Stop sourcing the file here sl@0: } sl@0: sl@0: # auto_mkindex -- sl@0: # Regenerate a tclIndex file from Tcl source files. Takes as argument sl@0: # the name of the directory in which the tclIndex file is to be placed, sl@0: # followed by any number of glob patterns to use in that directory to sl@0: # locate all of the relevant files. sl@0: # sl@0: # Arguments: sl@0: # dir - Name of the directory in which to create an index. sl@0: # args - Any number of additional arguments giving the sl@0: # names of files within dir. If no additional sl@0: # are given auto_mkindex will look for *.tcl. sl@0: sl@0: proc auto_mkindex {dir args} { sl@0: global errorCode errorInfo sl@0: sl@0: if {[interp issafe]} { sl@0: error "can't generate index within safe interpreter" sl@0: } sl@0: sl@0: set oldDir [pwd] sl@0: cd $dir sl@0: set dir [pwd] sl@0: sl@0: append index "# Tcl autoload index file, version 2.0\n" sl@0: append index "# This file is generated by the \"auto_mkindex\" command\n" sl@0: append index "# and sourced to set up indexing information for one or\n" sl@0: append index "# more commands. Typically each line is a command that\n" sl@0: append index "# sets an element in the auto_index array, where the\n" sl@0: append index "# element name is the name of a command and the value is\n" sl@0: append index "# a script that loads the command.\n\n" sl@0: if {[llength $args] == 0} { sl@0: set args *.tcl sl@0: } sl@0: sl@0: auto_mkindex_parser::init sl@0: foreach file [eval [linsert $args 0 glob --]] { sl@0: if {[catch {auto_mkindex_parser::mkindex $file} msg] == 0} { sl@0: append index $msg sl@0: } else { sl@0: set code $errorCode sl@0: set info $errorInfo sl@0: cd $oldDir sl@0: error $msg $info $code sl@0: } sl@0: } sl@0: auto_mkindex_parser::cleanup sl@0: sl@0: set fid [open "tclIndex" w] sl@0: puts -nonewline $fid $index sl@0: close $fid sl@0: cd $oldDir sl@0: } sl@0: sl@0: # Original version of auto_mkindex that just searches the source sl@0: # code for "proc" at the beginning of the line. sl@0: sl@0: proc auto_mkindex_old {dir args} { sl@0: global errorCode errorInfo sl@0: set oldDir [pwd] sl@0: cd $dir sl@0: set dir [pwd] sl@0: append index "# Tcl autoload index file, version 2.0\n" sl@0: append index "# This file is generated by the \"auto_mkindex\" command\n" sl@0: append index "# and sourced to set up indexing information for one or\n" sl@0: append index "# more commands. Typically each line is a command that\n" sl@0: append index "# sets an element in the auto_index array, where the\n" sl@0: append index "# element name is the name of a command and the value is\n" sl@0: append index "# a script that loads the command.\n\n" sl@0: if {[llength $args] == 0} { sl@0: set args *.tcl sl@0: } sl@0: foreach file [eval [linsert $args 0 glob --]] { sl@0: set f "" sl@0: set error [catch { sl@0: set f [open $file] sl@0: while {[gets $f line] >= 0} { sl@0: if {[regexp {^proc[ ]+([^ ]*)} $line match procName]} { sl@0: set procName [lindex [auto_qualify $procName "::"] 0] sl@0: append index "set [list auto_index($procName)]" sl@0: append index " \[list source \[file join \$dir [list $file]\]\]\n" sl@0: } sl@0: } sl@0: close $f sl@0: } msg] sl@0: if {$error} { sl@0: set code $errorCode sl@0: set info $errorInfo sl@0: catch {close $f} sl@0: cd $oldDir sl@0: error $msg $info $code sl@0: } sl@0: } sl@0: set f "" sl@0: set error [catch { sl@0: set f [open tclIndex w] sl@0: puts -nonewline $f $index sl@0: close $f sl@0: cd $oldDir sl@0: } msg] sl@0: if {$error} { sl@0: set code $errorCode sl@0: set info $errorInfo sl@0: catch {close $f} sl@0: cd $oldDir sl@0: error $msg $info $code sl@0: } sl@0: } sl@0: sl@0: # Create a safe interpreter that can be used to parse Tcl source files sl@0: # generate a tclIndex file for autoloading. This interp contains sl@0: # commands for things that need index entries. Each time a command sl@0: # is executed, it writes an entry out to the index file. sl@0: sl@0: namespace eval auto_mkindex_parser { sl@0: variable parser "" ;# parser used to build index sl@0: variable index "" ;# maintains index as it is built sl@0: variable scriptFile "" ;# name of file being processed sl@0: variable contextStack "" ;# stack of namespace scopes sl@0: variable imports "" ;# keeps track of all imported cmds sl@0: variable initCommands "" ;# list of commands that create aliases sl@0: sl@0: proc init {} { sl@0: variable parser sl@0: variable initCommands sl@0: sl@0: if {![interp issafe]} { sl@0: set parser [interp create -safe] sl@0: $parser hide info sl@0: $parser hide rename sl@0: $parser hide proc sl@0: $parser hide namespace sl@0: $parser hide eval sl@0: $parser hide puts sl@0: $parser invokehidden namespace delete :: sl@0: $parser invokehidden proc unknown {args} {} sl@0: sl@0: # We'll need access to the "namespace" command within the sl@0: # interp. Put it back, but move it out of the way. sl@0: sl@0: $parser expose namespace sl@0: $parser invokehidden rename namespace _%@namespace sl@0: $parser expose eval sl@0: $parser invokehidden rename eval _%@eval sl@0: sl@0: # Install all the registered psuedo-command implementations sl@0: sl@0: foreach cmd $initCommands { sl@0: eval $cmd sl@0: } sl@0: } sl@0: } sl@0: proc cleanup {} { sl@0: variable parser sl@0: interp delete $parser sl@0: unset parser sl@0: } sl@0: } sl@0: sl@0: # auto_mkindex_parser::mkindex -- sl@0: # sl@0: # Used by the "auto_mkindex" command to create a "tclIndex" file for sl@0: # the given Tcl source file. Executes the commands in the file, and sl@0: # handles things like the "proc" command by adding an entry for the sl@0: # index file. Returns a string that represents the index file. sl@0: # sl@0: # Arguments: sl@0: # file Name of Tcl source file to be indexed. sl@0: sl@0: proc auto_mkindex_parser::mkindex {file} { sl@0: variable parser sl@0: variable index sl@0: variable scriptFile sl@0: variable contextStack sl@0: variable imports sl@0: sl@0: set scriptFile $file sl@0: sl@0: set fid [open $file] sl@0: set contents [read $fid] sl@0: close $fid sl@0: sl@0: # There is one problem with sourcing files into the safe sl@0: # interpreter: references like "$x" will fail since code is not sl@0: # really being executed and variables do not really exist. sl@0: # To avoid this, we replace all $ with \0 (literally, the null char) sl@0: # later, when getting proc names we will have to reverse this replacement, sl@0: # in case there were any $ in the proc name. This will cause a problem sl@0: # if somebody actually tries to have a \0 in their proc name. Too bad sl@0: # for them. sl@0: set contents [string map "$ \u0000" $contents] sl@0: sl@0: set index "" sl@0: set contextStack "" sl@0: set imports "" sl@0: sl@0: $parser eval $contents sl@0: sl@0: foreach name $imports { sl@0: catch {$parser eval [list _%@namespace forget $name]} sl@0: } sl@0: return $index sl@0: } sl@0: sl@0: # auto_mkindex_parser::hook command sl@0: # sl@0: # Registers a Tcl command to evaluate when initializing the sl@0: # slave interpreter used by the mkindex parser. sl@0: # The command is evaluated in the master interpreter, and can sl@0: # use the variable auto_mkindex_parser::parser to get to the slave sl@0: sl@0: proc auto_mkindex_parser::hook {cmd} { sl@0: variable initCommands sl@0: sl@0: lappend initCommands $cmd sl@0: } sl@0: sl@0: # auto_mkindex_parser::slavehook command sl@0: # sl@0: # Registers a Tcl command to evaluate when initializing the sl@0: # slave interpreter used by the mkindex parser. sl@0: # The command is evaluated in the slave interpreter. sl@0: sl@0: proc auto_mkindex_parser::slavehook {cmd} { sl@0: variable initCommands sl@0: sl@0: # The $parser variable is defined to be the name of the sl@0: # slave interpreter when this command is used later. sl@0: sl@0: lappend initCommands "\$parser eval [list $cmd]" sl@0: } sl@0: sl@0: # auto_mkindex_parser::command -- sl@0: # sl@0: # Registers a new command with the "auto_mkindex_parser" interpreter sl@0: # that parses Tcl files. These commands are fake versions of things sl@0: # like the "proc" command. When you execute them, they simply write sl@0: # out an entry to a "tclIndex" file for auto-loading. sl@0: # sl@0: # This procedure allows extensions to register their own commands sl@0: # with the auto_mkindex facility. For example, a package like sl@0: # [incr Tcl] might register a "class" command so that class definitions sl@0: # could be added to a "tclIndex" file for auto-loading. sl@0: # sl@0: # Arguments: sl@0: # name Name of command recognized in Tcl files. sl@0: # arglist Argument list for command. sl@0: # body Implementation of command to handle indexing. sl@0: sl@0: proc auto_mkindex_parser::command {name arglist body} { sl@0: hook [list auto_mkindex_parser::commandInit $name $arglist $body] sl@0: } sl@0: sl@0: # auto_mkindex_parser::commandInit -- sl@0: # sl@0: # This does the actual work set up by auto_mkindex_parser::command sl@0: # This is called when the interpreter used by the parser is created. sl@0: # sl@0: # Arguments: sl@0: # name Name of command recognized in Tcl files. sl@0: # arglist Argument list for command. sl@0: # body Implementation of command to handle indexing. sl@0: sl@0: proc auto_mkindex_parser::commandInit {name arglist body} { sl@0: variable parser sl@0: sl@0: set ns [namespace qualifiers $name] sl@0: set tail [namespace tail $name] sl@0: if {$ns eq ""} { sl@0: set fakeName [namespace current]::_%@fake_$tail sl@0: } else { sl@0: set fakeName [namespace current]::[string map {:: _} _%@fake_$name] sl@0: } sl@0: proc $fakeName $arglist $body sl@0: sl@0: # YUK! Tcl won't let us alias fully qualified command names, sl@0: # so we can't handle names like "::itcl::class". Instead, sl@0: # we have to build procs with the fully qualified names, and sl@0: # have the procs point to the aliases. sl@0: sl@0: if {[string match *::* $name]} { sl@0: set exportCmd [list _%@namespace export [namespace tail $name]] sl@0: $parser eval [list _%@namespace eval $ns $exportCmd] sl@0: sl@0: # The following proc definition does not work if you sl@0: # want to tolerate space or something else diabolical sl@0: # in the procedure name, (i.e., space in $alias) sl@0: # The following does not work: sl@0: # "_%@eval {$alias} \$args" sl@0: # because $alias gets concat'ed to $args. sl@0: # The following does not work because $cmd is somehow undefined sl@0: # "set cmd {$alias} \; _%@eval {\$cmd} \$args" sl@0: # A gold star to someone that can make test sl@0: # autoMkindex-3.3 work properly sl@0: sl@0: set alias [namespace tail $fakeName] sl@0: $parser invokehidden proc $name {args} "_%@eval {$alias} \$args" sl@0: $parser alias $alias $fakeName sl@0: } else { sl@0: $parser alias $name $fakeName sl@0: } sl@0: return sl@0: } sl@0: sl@0: # auto_mkindex_parser::fullname -- sl@0: # Used by commands like "proc" within the auto_mkindex parser. sl@0: # Returns the qualified namespace name for the "name" argument. sl@0: # If the "name" does not start with "::", elements are added from sl@0: # the current namespace stack to produce a qualified name. Then, sl@0: # the name is examined to see whether or not it should really be sl@0: # qualified. If the name has more than the leading "::", it is sl@0: # returned as a fully qualified name. Otherwise, it is returned sl@0: # as a simple name. That way, the Tcl autoloader will recognize sl@0: # it properly. sl@0: # sl@0: # Arguments: sl@0: # name - Name that is being added to index. sl@0: sl@0: proc auto_mkindex_parser::fullname {name} { sl@0: variable contextStack sl@0: sl@0: if {![string match ::* $name]} { sl@0: foreach ns $contextStack { sl@0: set name "${ns}::$name" sl@0: if {[string match ::* $name]} { sl@0: break sl@0: } sl@0: } sl@0: } sl@0: sl@0: if {[namespace qualifiers $name] eq ""} { sl@0: set name [namespace tail $name] sl@0: } elseif {![string match ::* $name]} { sl@0: set name "::$name" sl@0: } sl@0: sl@0: # Earlier, mkindex replaced all $'s with \0. Now, we have to reverse sl@0: # that replacement. sl@0: return [string map "\u0000 $" $name] sl@0: } sl@0: sl@0: # Register all of the procedures for the auto_mkindex parser that sl@0: # will build the "tclIndex" file. sl@0: sl@0: # AUTO MKINDEX: proc name arglist body sl@0: # Adds an entry to the auto index list for the given procedure name. sl@0: sl@0: auto_mkindex_parser::command proc {name args} { sl@0: variable index sl@0: variable scriptFile sl@0: # Do some fancy reformatting on the "source" call to handle platform sl@0: # differences with respect to pathnames. Use format just so that the sl@0: # command is a little easier to read (otherwise it'd be full of sl@0: # backslashed dollar signs, etc. sl@0: append index [list set auto_index([fullname $name])] \ sl@0: [format { [list source [file join $dir %s]]} \ sl@0: [file split $scriptFile]] "\n" sl@0: } sl@0: sl@0: # Conditionally add support for Tcl byte code files. There are some sl@0: # tricky details here. First, we need to get the tbcload library sl@0: # initialized in the current interpreter. We cannot load tbcload into the sl@0: # slave until we have done so because it needs access to the tcl_patchLevel sl@0: # variable. Second, because the package index file may defer loading the sl@0: # library until we invoke a command, we need to explicitly invoke auto_load sl@0: # to force it to be loaded. This should be a noop if the package has sl@0: # already been loaded sl@0: sl@0: auto_mkindex_parser::hook { sl@0: if {![catch {package require tbcload}]} { sl@0: if {[namespace which -command tbcload::bcproc] eq ""} { sl@0: auto_load tbcload::bcproc sl@0: } sl@0: load {} tbcload $auto_mkindex_parser::parser sl@0: sl@0: # AUTO MKINDEX: tbcload::bcproc name arglist body sl@0: # Adds an entry to the auto index list for the given pre-compiled sl@0: # procedure name. sl@0: sl@0: auto_mkindex_parser::commandInit tbcload::bcproc {name args} { sl@0: variable index sl@0: variable scriptFile sl@0: # Do some nice reformatting of the "source" call, to get around sl@0: # path differences on different platforms. We use the format sl@0: # command just so that the code is a little easier to read. sl@0: append index [list set auto_index([fullname $name])] \ sl@0: [format { [list source [file join $dir %s]]} \ sl@0: [file split $scriptFile]] "\n" sl@0: } sl@0: } sl@0: } sl@0: sl@0: # AUTO MKINDEX: namespace eval name command ?arg arg...? sl@0: # Adds the namespace name onto the context stack and evaluates the sl@0: # associated body of commands. sl@0: # sl@0: # AUTO MKINDEX: namespace import ?-force? pattern ?pattern...? sl@0: # Performs the "import" action in the parser interpreter. This is sl@0: # important for any commands contained in a namespace that affect sl@0: # the index. For example, a script may say "itcl::class ...", sl@0: # or it may import "itcl::*" and then say "class ...". This sl@0: # procedure does the import operation, but keeps track of imported sl@0: # patterns so we can remove the imports later. sl@0: sl@0: auto_mkindex_parser::command namespace {op args} { sl@0: switch -- $op { sl@0: eval { sl@0: variable parser sl@0: variable contextStack sl@0: sl@0: set name [lindex $args 0] sl@0: set args [lrange $args 1 end] sl@0: sl@0: set contextStack [linsert $contextStack 0 $name] sl@0: $parser eval [list _%@namespace eval $name] $args sl@0: set contextStack [lrange $contextStack 1 end] sl@0: } sl@0: import { sl@0: variable parser sl@0: variable imports sl@0: foreach pattern $args { sl@0: if {$pattern ne "-force"} { sl@0: lappend imports $pattern sl@0: } sl@0: } sl@0: catch {$parser eval "_%@namespace import $args"} sl@0: } sl@0: } sl@0: } sl@0: sl@0: return