3 # Default system startup file for Tcl-based applications. Defines
4 # "unknown" procedure and auto-load facilities.
6 # $Header: /user6/ouster/tcl/scripts/RCS/init.tcl,v 1.7 92/07/25 16:29:36 ouster Exp $ SPRITE (Berkeley)
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.
19 # Invoked when a Tcl command is invoked that doesn't exist in the
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
31 global auto_noexec auto_noload env unknown_active
33 if [info exists unknown_active
] {
35 error "unexpected recursion in \"unknown\" command"
38 set name
[lindex $args 0]
39 if ![info exists auto_noload
] {
40 if [auto_load $name] {
42 return [uplevel $args]
45 if ![info exists auto_noexec
] {
46 if [auto_execok $name] {
48 return [uplevel exec $args]
51 if {([info level
] == 1) && ([info script
] == "")} {
52 set cmds
[info commands
$name*]
53 if {[llength $cmds] == 1} {
55 return [uplevel [lreplace $args 0 0 $cmds]]
57 if {[llength $cmds] != 0} {
60 error "empty command name \"\""
62 error "ambiguous command name \"$name\": [lsort $cmds]"
67 error "invalid command name \"$name\""
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.
77 global auto_index auto_oldpath auto_path env
79 if [info exists auto_index
($cmd)] {
80 uplevel #0 source $auto_index($cmd)
83 if [catch {set path
$auto_path}] {
84 if [catch {set path
$env(TCLLIBPATH
)}] {
85 if [catch {set path
[info library
]}] {
90 if [info exists auto_oldpath
] {
91 if {$auto_oldpath == $path} {
95 set auto_oldpath
$path
96 catch {unset auto_index
}
100 set f
[open $dir/tclindex
]
101 if {[gets $f] != "# Tcl autoload index file: each line identifies a Tcl"} {
102 puts stdout
"Bad id line in file $dir/tclindex"
105 while {[gets $f line
] >= 0} {
106 if {([string index
$line 0] == "#") ||
([llength $line] != 2)} {
109 set name
[lindex $line 0]
110 if {![info exists auto_index
($name)]} {
111 set auto_index
($name) $dir/[lindex $line 1]
119 if [info exists auto_index
($cmd)] {
120 uplevel #0 source $auto_index($cmd)
127 # Returns 1 if there's an executable in the current path for the
128 # given name, 0 otherwise. Builds an associative array auto_execs
129 # that caches information about previous checks, for speed.
131 proc auto_execok name
{
132 global auto_execs env
134 if [info exists auto_execs
($name)] {
135 return $auto_execs($name)
137 set auto_execs
($name) 0
138 foreach dir
[split $env(PATH
) :] {
139 if {[file executable
$dir/$name] && ![file isdirectory
$dir/$name]} {
140 set auto_execs
($name) 1
148 # Destroy all cached information for auto-loading and auto-execution,
149 # so that the information gets recomputed the next time it's needed.
152 global auto_execs auto_index
153 unset auto_execs auto_index