]>
Commit | Line | Data |
---|---|---|
1 | # init.tcl -- | |
2 | # | |
3 | # Default system startup file for Tcl-based applications. Defines | |
4 | # "unknown" procedure and auto-load facilities. | |
5 | # | |
6 | # $Header: /user6/ouster/tcl/scripts/RCS/init.tcl,v 1.7 92/07/25 16:29:36 ouster Exp $ SPRITE (Berkeley) | |
7 | # | |
8 | # Copyright 1991-1992 Regents of the University of California | |
9 | # Permission to use, copy, modify, and distribute this | |
10 | # software and its documentation for any purpose and without | |
11 | # fee is hereby granted, provided that this copyright | |
12 | # notice appears in all copies. The University of California | |
13 | # makes no representations about the suitability of this | |
14 | # software for any purpose. It is provided "as is" without | |
15 | # express or implied warranty. | |
16 | # | |
17 | ||
18 | # unknown: | |
19 | # Invoked when a Tcl command is invoked that doesn't exist in the | |
20 | # interpreter: | |
21 | # | |
22 | # 1. See if the autoload facility can locate the command in a | |
23 | # Tcl script file. If so, load it and execute it. | |
24 | # 2. See if the command exists as an executable UNIX program. | |
25 | # If so, "exec" the command. | |
26 | # 3. See if the command is a valid abbreviation for another command. | |
27 | # if so, invoke the command. However, only permit abbreviations | |
28 | # at top-level. | |
29 | ||
30 | proc unknown args { | |
31 | global auto_noexec auto_noload env unknown_active | |
32 | ||
33 | if [info exists unknown_active] { | |
34 | unset unknown_active | |
35 | error "unexpected recursion in \"unknown\" command" | |
36 | } | |
37 | set unknown_active 1 | |
38 | set name [lindex $args 0] | |
39 | if ![info exists auto_noload] { | |
40 | if [auto_load $name] { | |
41 | unset unknown_active | |
42 | return [uplevel $args] | |
43 | } | |
44 | } | |
45 | if ![info exists auto_noexec] { | |
46 | if [auto_execok $name] { | |
47 | unset unknown_active | |
48 | return [uplevel exec $args] | |
49 | } | |
50 | } | |
51 | if {([info level] == 1) && ([info script] == "")} { | |
52 | set cmds [info commands $name*] | |
53 | if {[llength $cmds] == 1} { | |
54 | unset unknown_active | |
55 | return [uplevel [lreplace $args 0 0 $cmds]] | |
56 | } | |
57 | if {[llength $cmds] != 0} { | |
58 | unset unknown_active | |
59 | if {$name == ""} { | |
60 | error "empty command name \"\"" | |
61 | } else { | |
62 | error "ambiguous command name \"$name\": [lsort $cmds]" | |
63 | } | |
64 | } | |
65 | } | |
66 | unset unknown_active | |
67 | error "invalid command name \"$name\"" | |
68 | } | |
69 | ||
70 | # auto_load: | |
71 | # Checks a collection of library directories to see if a procedure | |
72 | # is defined in one of them. If so, it sources the appropriate | |
73 | # library file to create the procedure. Returns 1 if it successfully | |
74 | # loaded the procedure, 0 otherwise. | |
75 | ||
76 | proc auto_load cmd { | |
77 | global auto_index auto_oldpath auto_path env | |
78 | if [info exists auto_index($cmd)] { | |
79 | uplevel #0 source $auto_index($cmd) | |
80 | return 1 | |
81 | } | |
82 | if [catch {set path $auto_path}] { | |
83 | if [catch {set path $env(TCLLIBPATH)}] { | |
84 | if [catch {set path [info library]}] { | |
85 | return 0 | |
86 | } | |
87 | } | |
88 | } | |
89 | if [info exists auto_oldpath] { | |
90 | if {$auto_oldpath == $path} { | |
91 | return 0 | |
92 | } | |
93 | } | |
94 | set auto_oldpath $path | |
95 | catch {unset auto_index} | |
96 | foreach dir $path { | |
97 | set f "" | |
98 | catch { | |
99 | set f [open $dir/tclindex] | |
100 | if {[gets $f] != "# Tcl autoload index file: each line identifies a Tcl"} { | |
101 | puts stdout "Bad id line in file $dir/tclindex" | |
102 | error done | |
103 | } | |
104 | while {[gets $f line] >= 0} { | |
105 | if {([string index $line 0] == "#") || ([llength $line] != 2)} { | |
106 | continue | |
107 | } | |
108 | set name [lindex $line 0] | |
109 | if {![info exists auto_index($name)]} { | |
110 | set auto_index($name) $dir/[lindex $line 1] | |
111 | } | |
112 | } | |
113 | } | |
114 | if {$f != ""} { | |
115 | close $f | |
116 | } | |
117 | } | |
118 | if [info exists auto_index($cmd)] { | |
119 | uplevel #0 source $auto_index($cmd) | |
120 | return 1 | |
121 | } | |
122 | return 0 | |
123 | } | |
124 | ||
125 | # auto_execok: | |
126 | # Returns 1 if there's an executable in the current path for the | |
127 | # given name, 0 otherwise. Builds an associative array auto_execs | |
128 | # that caches information about previous checks, for speed. | |
129 | ||
130 | proc auto_execok name { | |
131 | global auto_execs env | |
132 | ||
133 | if [info exists auto_execs($name)] { | |
134 | return $auto_execs($name) | |
135 | } | |
136 | set auto_execs($name) 0 | |
137 | foreach dir [split $env(PATH) :] { | |
138 | if {[file executable $dir/$name] && ![file isdirectory $dir/$name]} { | |
139 | set auto_execs($name) 1 | |
140 | return 1 | |
141 | } | |
142 | } | |
143 | return 0 | |
144 | } | |
145 | ||
146 | # auto_reset: | |
147 | # Destroy all cached information for auto-loading and auto-execution, | |
148 | # so that the information gets recomputed the next time it's needed. | |
149 | ||
150 | proc auto_reset {} { | |
151 | global auto_execs auto_index | |
152 | unset auto_execs auto_index | |
153 | } |