home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 November / CPNL0711.ISO / beeld / teken / scribus-1.3.3.9-win32-install.exe / tcl / tcl8.4 / auto.tcl next >
Text File  |  2002-10-28  |  20KB  |  584 lines

  1. # auto.tcl --
  2. #
  3. # utility procs formerly in init.tcl dealing with auto execution
  4. # of commands and can be auto loaded themselves.
  5. #
  6. # RCS: @(#) $Id: auto.tcl,v 1.12 2002/10/28 16:34:25 dgp Exp $
  7. #
  8. # Copyright (c) 1991-1993 The Regents of the University of California.
  9. # Copyright (c) 1994-1998 Sun Microsystems, Inc.
  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.  
  15. # auto_reset --
  16. #
  17. # Destroy all cached information for auto-loading and auto-execution,
  18. # so that the information gets recomputed the next time it's needed.
  19. # Also delete any procedures that are listed in the auto-load index
  20. # except those defined in this file.
  21. #
  22. # Arguments: 
  23. # None.
  24.  
  25. proc auto_reset {} {
  26.     global auto_execs auto_index auto_oldpath
  27.     foreach p [info procs] {
  28.     if {[info exists auto_index($p)] && ![string match auto_* $p]
  29.         && ([lsearch -exact {unknown pkg_mkIndex tclPkgSetup
  30.             tcl_findLibrary pkg_compareExtension
  31.             tclPkgUnknown tcl::MacOSXPkgUnknown
  32.             tcl::MacPkgUnknown} $p] < 0)} {
  33.         rename $p {}
  34.     }
  35.     }
  36.     catch {unset auto_execs}
  37.     catch {unset auto_index}
  38.     catch {unset auto_oldpath}
  39. }
  40.  
  41. # tcl_findLibrary --
  42. #
  43. #    This is a utility for extensions that searches for a library directory
  44. #    using a canonical searching algorithm. A side effect is to source
  45. #    the initialization script and set a global library variable.
  46. #
  47. # Arguments:
  48. #     basename    Prefix of the directory name, (e.g., "tk")
  49. #    version        Version number of the package, (e.g., "8.0")
  50. #    patch        Patchlevel of the package, (e.g., "8.0.3")
  51. #    initScript    Initialization script to source (e.g., tk.tcl)
  52. #    enVarName    environment variable to honor (e.g., TK_LIBRARY)
  53. #    varName        Global variable to set when done (e.g., tk_library)
  54.  
  55. proc tcl_findLibrary {basename version patch initScript enVarName varName} {
  56.     upvar #0 $varName the_library
  57.     global env errorInfo
  58.  
  59.     set dirs {}
  60.     set errors {}
  61.  
  62.     # The C application may have hardwired a path, which we honor
  63.     
  64.     set variableSet [info exists the_library]
  65.     if {$variableSet && [string compare $the_library {}]} {
  66.     lappend dirs $the_library
  67.     } else {
  68.  
  69.     # Do the canonical search
  70.  
  71.     # 1. From an environment variable, if it exists
  72.  
  73.         if {[info exists env($enVarName)]} {
  74.             lappend dirs $env($enVarName)
  75.         }
  76.  
  77.     # 2. Relative to the Tcl library
  78.  
  79.         lappend dirs [file join [file dirname [info library]] \
  80.         $basename$version]
  81.  
  82.     # 3. Various locations relative to the executable
  83.     # ../lib/foo1.0        (From bin directory in install hierarchy)
  84.     # ../../lib/foo1.0    (From bin/arch directory in install hierarchy)
  85.     # ../library        (From unix directory in build hierarchy)
  86.     # ../../library        (From unix/arch directory in build hierarchy)
  87.     # ../../foo1.0.1/library
  88.     #        (From unix directory in parallel build hierarchy)
  89.     # ../../../foo1.0.1/library
  90.     #        (From unix/arch directory in parallel build hierarchy)
  91.  
  92.         set parentDir [file dirname [file dirname [info nameofexecutable]]]
  93.         set grandParentDir [file dirname $parentDir]
  94.         lappend dirs [file join $parentDir lib $basename$version]
  95.         lappend dirs [file join $grandParentDir lib $basename$version]
  96.         lappend dirs [file join $parentDir library]
  97.         lappend dirs [file join $grandParentDir library]
  98.         lappend dirs [file join $grandParentDir $basename$patch library]
  99.         lappend dirs [file join [file dirname $grandParentDir] \
  100.         $basename$patch library]
  101.  
  102.     # 4. On MacOSX, check the directories in the tcl_pkgPath
  103.     if {[string equal $::tcl_platform(platform) "unix"] && \
  104.         [string equal $::tcl_platform(os) "Darwin"]} {
  105.         foreach d $::tcl_pkgPath {
  106.         lappend dirs [file join $d $basename$version]
  107.         lappend dirs [file join $d $basename$version Resources Scripts]
  108.         }
  109.     }
  110.     }
  111.     foreach i $dirs {
  112.         set the_library $i
  113.         set file [file join $i $initScript]
  114.  
  115.     # source everything when in a safe interpreter because
  116.     # we have a source command, but no file exists command
  117.  
  118.         if {[interp issafe] || [file exists $file]} {
  119.             if {![catch {uplevel #0 [list source $file]} msg]} {
  120.                 return
  121.             } else {
  122.                 append errors "$file: $msg\n$errorInfo\n"
  123.             }
  124.         }
  125.     }
  126.     if {!$variableSet} {
  127.     unset the_library
  128.     }
  129.     set msg "Can't find a usable $initScript in the following directories: \n"
  130.     append msg "    $dirs\n\n"
  131.     append msg "$errors\n\n"
  132.     append msg "This probably means that $basename wasn't installed properly.\n"
  133.     error $msg
  134. }
  135.  
  136.  
  137. # ----------------------------------------------------------------------
  138. # auto_mkindex
  139. # ----------------------------------------------------------------------
  140. # The following procedures are used to generate the tclIndex file
  141. # from Tcl source files.  They use a special safe interpreter to
  142. # parse Tcl source files, writing out index entries as "proc"
  143. # commands are encountered.  This implementation won't work in a
  144. # safe interpreter, since a safe interpreter can't create the
  145. # special parser and mess with its commands.  
  146.  
  147. if {[interp issafe]} {
  148.     return    ;# Stop sourcing the file here
  149. }
  150.  
  151. # auto_mkindex --
  152. # Regenerate a tclIndex file from Tcl source files.  Takes as argument
  153. # the name of the directory in which the tclIndex file is to be placed,
  154. # followed by any number of glob patterns to use in that directory to
  155. # locate all of the relevant files.
  156. #
  157. # Arguments: 
  158. # dir -        Name of the directory in which to create an index.
  159. # args -    Any number of additional arguments giving the
  160. #        names of files within dir.  If no additional
  161. #        are given auto_mkindex will look for *.tcl.
  162.  
  163. proc auto_mkindex {dir args} {
  164.     global errorCode errorInfo
  165.  
  166.     if {[interp issafe]} {
  167.         error "can't generate index within safe interpreter"
  168.     }
  169.  
  170.     set oldDir [pwd]
  171.     cd $dir
  172.     set dir [pwd]
  173.  
  174.     append index "# Tcl autoload index file, version 2.0\n"
  175.     append index "# This file is generated by the \"auto_mkindex\" command\n"
  176.     append index "# and sourced to set up indexing information for one or\n"
  177.     append index "# more commands.  Typically each line is a command that\n"
  178.     append index "# sets an element in the auto_index array, where the\n"
  179.     append index "# element name is the name of a command and the value is\n"
  180.     append index "# a script that loads the command.\n\n"
  181.     if {$args == ""} {
  182.     set args *.tcl
  183.     }
  184.  
  185.     auto_mkindex_parser::init
  186.     foreach file [eval glob $args] {
  187.         if {[catch {auto_mkindex_parser::mkindex $file} msg] == 0} {
  188.             append index $msg
  189.         } else {
  190.             set code $errorCode
  191.             set info $errorInfo
  192.             cd $oldDir
  193.             error $msg $info $code
  194.         }
  195.     }
  196.     auto_mkindex_parser::cleanup
  197.  
  198.     set fid [open "tclIndex" w]
  199.     puts -nonewline $fid $index
  200.     close $fid
  201.     cd $oldDir
  202. }
  203.  
  204. # Original version of auto_mkindex that just searches the source
  205. # code for "proc" at the beginning of the line.
  206.  
  207. proc auto_mkindex_old {dir args} {
  208.     global errorCode errorInfo
  209.     set oldDir [pwd]
  210.     cd $dir
  211.     set dir [pwd]
  212.     append index "# Tcl autoload index file, version 2.0\n"
  213.     append index "# This file is generated by the \"auto_mkindex\" command\n"
  214.     append index "# and sourced to set up indexing information for one or\n"
  215.     append index "# more commands.  Typically each line is a command that\n"
  216.     append index "# sets an element in the auto_index array, where the\n"
  217.     append index "# element name is the name of a command and the value is\n"
  218.     append index "# a script that loads the command.\n\n"
  219.     if {[string equal $args ""]} {
  220.     set args *.tcl
  221.     }
  222.     foreach file [eval glob $args] {
  223.     set f ""
  224.     set error [catch {
  225.         set f [open $file]
  226.         while {[gets $f line] >= 0} {
  227.         if {[regexp {^proc[     ]+([^     ]*)} $line match procName]} {
  228.             set procName [lindex [auto_qualify $procName "::"] 0]
  229.             append index "set [list auto_index($procName)]"
  230.             append index " \[list source \[file join \$dir [list $file]\]\]\n"
  231.         }
  232.         }
  233.         close $f
  234.     } msg]
  235.     if {$error} {
  236.         set code $errorCode
  237.         set info $errorInfo
  238.         catch {close $f}
  239.         cd $oldDir
  240.         error $msg $info $code
  241.     }
  242.     }
  243.     set f ""
  244.     set error [catch {
  245.     set f [open tclIndex w]
  246.     puts -nonewline $f $index
  247.     close $f
  248.     cd $oldDir
  249.     } msg]
  250.     if {$error} {
  251.     set code $errorCode
  252.     set info $errorInfo
  253.     catch {close $f}
  254.     cd $oldDir
  255.     error $msg $info $code
  256.     }
  257. }
  258.  
  259. # Create a safe interpreter that can be used to parse Tcl source files
  260. # generate a tclIndex file for autoloading.  This interp contains
  261. # commands for things that need index entries.  Each time a command
  262. # is executed, it writes an entry out to the index file.
  263.  
  264. namespace eval auto_mkindex_parser {
  265.     variable parser ""          ;# parser used to build index
  266.     variable index ""           ;# maintains index as it is built
  267.     variable scriptFile ""      ;# name of file being processed
  268.     variable contextStack ""    ;# stack of namespace scopes
  269.     variable imports ""         ;# keeps track of all imported cmds
  270.     variable initCommands ""    ;# list of commands that create aliases
  271.  
  272.     proc init {} {
  273.     variable parser
  274.     variable initCommands
  275.  
  276.     if {![interp issafe]} {
  277.         set parser [interp create -safe]
  278.         $parser hide info
  279.         $parser hide rename
  280.         $parser hide proc
  281.         $parser hide namespace
  282.         $parser hide eval
  283.         $parser hide puts
  284.         $parser invokehidden namespace delete ::
  285.         $parser invokehidden proc unknown {args} {}
  286.  
  287.         # We'll need access to the "namespace" command within the
  288.         # interp.  Put it back, but move it out of the way.
  289.  
  290.         $parser expose namespace
  291.         $parser invokehidden rename namespace _%@namespace
  292.         $parser expose eval
  293.         $parser invokehidden rename eval _%@eval
  294.  
  295.         # Install all the registered psuedo-command implementations
  296.  
  297.         foreach cmd $initCommands {
  298.         eval $cmd
  299.         }
  300.     }
  301.     }
  302.     proc cleanup {} {
  303.     variable parser
  304.     interp delete $parser
  305.     unset parser
  306.     }
  307. }
  308.  
  309. # auto_mkindex_parser::mkindex --
  310. #
  311. # Used by the "auto_mkindex" command to create a "tclIndex" file for
  312. # the given Tcl source file.  Executes the commands in the file, and
  313. # handles things like the "proc" command by adding an entry for the
  314. # index file.  Returns a string that represents the index file.
  315. #
  316. # Arguments: 
  317. #    file    Name of Tcl source file to be indexed.
  318.  
  319. proc auto_mkindex_parser::mkindex {file} {
  320.     variable parser
  321.     variable index
  322.     variable scriptFile
  323.     variable contextStack
  324.     variable imports
  325.  
  326.     set scriptFile $file
  327.  
  328.     set fid [open $file]
  329.     set contents [read $fid]
  330.     close $fid
  331.  
  332.     # There is one problem with sourcing files into the safe
  333.     # interpreter:  references like "$x" will fail since code is not
  334.     # really being executed and variables do not really exist.
  335.     # To avoid this, we replace all $ with \0 (literally, the null char)
  336.     # later, when getting proc names we will have to reverse this replacement,
  337.     # in case there were any $ in the proc name.  This will cause a problem
  338.     # if somebody actually tries to have a \0 in their proc name.  Too bad
  339.     # for them.
  340.     regsub -all {\$} $contents "\0" contents
  341.     
  342.     set index ""
  343.     set contextStack ""
  344.     set imports ""
  345.  
  346.     $parser eval $contents
  347.  
  348.     foreach name $imports {
  349.         catch {$parser eval [list _%@namespace forget $name]}
  350.     }
  351.     return $index
  352. }
  353.  
  354. # auto_mkindex_parser::hook command
  355. #
  356. # Registers a Tcl command to evaluate when initializing the
  357. # slave interpreter used by the mkindex parser.
  358. # The command is evaluated in the master interpreter, and can
  359. # use the variable auto_mkindex_parser::parser to get to the slave
  360.  
  361. proc auto_mkindex_parser::hook {cmd} {
  362.     variable initCommands
  363.  
  364.     lappend initCommands $cmd
  365. }
  366.  
  367. # auto_mkindex_parser::slavehook command
  368. #
  369. # Registers a Tcl command to evaluate when initializing the
  370. # slave interpreter used by the mkindex parser.
  371. # The command is evaluated in the slave interpreter.
  372.  
  373. proc auto_mkindex_parser::slavehook {cmd} {
  374.     variable initCommands
  375.  
  376.     # The $parser variable is defined to be the name of the
  377.     # slave interpreter when this command is used later.
  378.  
  379.     lappend initCommands "\$parser eval [list $cmd]"
  380. }
  381.  
  382. # auto_mkindex_parser::command --
  383. #
  384. # Registers a new command with the "auto_mkindex_parser" interpreter
  385. # that parses Tcl files.  These commands are fake versions of things
  386. # like the "proc" command.  When you execute them, they simply write
  387. # out an entry to a "tclIndex" file for auto-loading.
  388. #
  389. # This procedure allows extensions to register their own commands
  390. # with the auto_mkindex facility.  For example, a package like
  391. # [incr Tcl] might register a "class" command so that class definitions
  392. # could be added to a "tclIndex" file for auto-loading.
  393. #
  394. # Arguments:
  395. #    name     Name of command recognized in Tcl files.
  396. #    arglist    Argument list for command.
  397. #    body     Implementation of command to handle indexing.
  398.  
  399. proc auto_mkindex_parser::command {name arglist body} {
  400.     hook [list auto_mkindex_parser::commandInit $name $arglist $body]
  401. }
  402.  
  403. # auto_mkindex_parser::commandInit --
  404. #
  405. # This does the actual work set up by auto_mkindex_parser::command
  406. # This is called when the interpreter used by the parser is created.
  407. #
  408. # Arguments:
  409. #    name     Name of command recognized in Tcl files.
  410. #    arglist    Argument list for command.
  411. #    body     Implementation of command to handle indexing.
  412.  
  413. proc auto_mkindex_parser::commandInit {name arglist body} {
  414.     variable parser
  415.  
  416.     set ns [namespace qualifiers $name]
  417.     set tail [namespace tail $name]
  418.     if {[string equal $ns ""]} {
  419.         set fakeName "[namespace current]::_%@fake_$tail"
  420.     } else {
  421.         set fakeName "_%@fake_$name"
  422.         regsub -all {::} $fakeName "_" fakeName
  423.         set fakeName "[namespace current]::$fakeName"
  424.     }
  425.     proc $fakeName $arglist $body
  426.  
  427.     # YUK!  Tcl won't let us alias fully qualified command names,
  428.     # so we can't handle names like "::itcl::class".  Instead,
  429.     # we have to build procs with the fully qualified names, and
  430.     # have the procs point to the aliases.
  431.  
  432.     if {[regexp {::} $name]} {
  433.         set exportCmd [list _%@namespace export [namespace tail $name]]
  434.         $parser eval [list _%@namespace eval $ns $exportCmd]
  435.  
  436.     # The following proc definition does not work if you
  437.     # want to tolerate space or something else diabolical
  438.     # in the procedure name, (i.e., space in $alias)
  439.     # The following does not work:
  440.     #   "_%@eval {$alias} \$args"
  441.     # because $alias gets concat'ed to $args.
  442.     # The following does not work because $cmd is somehow undefined
  443.     #   "set cmd {$alias} \; _%@eval {\$cmd} \$args"
  444.     # A gold star to someone that can make test
  445.     # autoMkindex-3.3 work properly
  446.  
  447.         set alias [namespace tail $fakeName]
  448.         $parser invokehidden proc $name {args} "_%@eval {$alias} \$args"
  449.         $parser alias $alias $fakeName
  450.     } else {
  451.         $parser alias $name $fakeName
  452.     }
  453.     return
  454. }
  455.  
  456. # auto_mkindex_parser::fullname --
  457. # Used by commands like "proc" within the auto_mkindex parser.
  458. # Returns the qualified namespace name for the "name" argument.
  459. # If the "name" does not start with "::", elements are added from
  460. # the current namespace stack to produce a qualified name.  Then,
  461. # the name is examined to see whether or not it should really be
  462. # qualified.  If the name has more than the leading "::", it is
  463. # returned as a fully qualified name.  Otherwise, it is returned
  464. # as a simple name.  That way, the Tcl autoloader will recognize
  465. # it properly.
  466. #
  467. # Arguments:
  468. # name -        Name that is being added to index.
  469.  
  470. proc auto_mkindex_parser::fullname {name} {
  471.     variable contextStack
  472.  
  473.     if {![string match ::* $name]} {
  474.         foreach ns $contextStack {
  475.             set name "${ns}::$name"
  476.             if {[string match ::* $name]} {
  477.                 break
  478.             }
  479.         }
  480.     }
  481.  
  482.     if {[string equal [namespace qualifiers $name] ""]} {
  483.         set name [namespace tail $name]
  484.     } elseif {![string match ::* $name]} {
  485.         set name "::$name"
  486.     }
  487.     
  488.     # Earlier, mkindex replaced all $'s with \0.  Now, we have to reverse
  489.     # that replacement.
  490.     regsub -all "\0" $name "\$" name
  491.     return $name
  492. }
  493.  
  494. # Register all of the procedures for the auto_mkindex parser that
  495. # will build the "tclIndex" file.
  496.  
  497. # AUTO MKINDEX:  proc name arglist body
  498. # Adds an entry to the auto index list for the given procedure name.
  499.  
  500. auto_mkindex_parser::command proc {name args} {
  501.     variable index
  502.     variable scriptFile
  503.     # Do some fancy reformatting on the "source" call to handle platform
  504.     # differences with respect to pathnames.  Use format just so that the
  505.     # command is a little easier to read (otherwise it'd be full of 
  506.     # backslashed dollar signs, etc.
  507.     append index [list set auto_index([fullname $name])] \
  508.         [format { [list source [file join $dir %s]]} \
  509.         [file split $scriptFile]] "\n"
  510. }
  511.  
  512. # Conditionally add support for Tcl byte code files.  There are some
  513. # tricky details here.  First, we need to get the tbcload library
  514. # initialized in the current interpreter.  We cannot load tbcload into the
  515. # slave until we have done so because it needs access to the tcl_patchLevel
  516. # variable.  Second, because the package index file may defer loading the
  517. # library until we invoke a command, we need to explicitly invoke auto_load
  518. # to force it to be loaded.  This should be a noop if the package has
  519. # already been loaded
  520.  
  521. auto_mkindex_parser::hook {
  522.     if {![catch {package require tbcload}]} {
  523.     if {[llength [info commands tbcload::bcproc]] == 0} {
  524.         auto_load tbcload::bcproc
  525.     }
  526.     load {} tbcload $auto_mkindex_parser::parser
  527.  
  528.     # AUTO MKINDEX:  tbcload::bcproc name arglist body
  529.     # Adds an entry to the auto index list for the given pre-compiled
  530.     # procedure name.  
  531.  
  532.     auto_mkindex_parser::commandInit tbcload::bcproc {name args} {
  533.         variable index
  534.         variable scriptFile
  535.         # Do some nice reformatting of the "source" call, to get around
  536.         # path differences on different platforms.  We use the format
  537.         # command just so that the code is a little easier to read.
  538.         append index [list set auto_index([fullname $name])] \
  539.             [format { [list source [file join $dir %s]]} \
  540.             [file split $scriptFile]] "\n"
  541.     }
  542.     }
  543. }
  544.  
  545. # AUTO MKINDEX:  namespace eval name command ?arg arg...?
  546. # Adds the namespace name onto the context stack and evaluates the
  547. # associated body of commands.
  548. #
  549. # AUTO MKINDEX:  namespace import ?-force? pattern ?pattern...?
  550. # Performs the "import" action in the parser interpreter.  This is
  551. # important for any commands contained in a namespace that affect
  552. # the index.  For example, a script may say "itcl::class ...",
  553. # or it may import "itcl::*" and then say "class ...".  This
  554. # procedure does the import operation, but keeps track of imported
  555. # patterns so we can remove the imports later.
  556.  
  557. auto_mkindex_parser::command namespace {op args} {
  558.     switch -- $op {
  559.         eval {
  560.             variable parser
  561.             variable contextStack
  562.  
  563.             set name [lindex $args 0]
  564.             set args [lrange $args 1 end]
  565.  
  566.             set contextStack [linsert $contextStack 0 $name]
  567.         $parser eval [list _%@namespace eval $name] $args
  568.             set contextStack [lrange $contextStack 1 end]
  569.         }
  570.         import {
  571.             variable parser
  572.             variable imports
  573.             foreach pattern $args {
  574.                 if {[string compare $pattern "-force"]} {
  575.                     lappend imports $pattern
  576.                 }
  577.             }
  578.             catch {$parser eval "_%@namespace import $args"}
  579.         }
  580.     }
  581. }
  582.  
  583. return
  584.