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