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
78 if [info exists auto_index
($cmd)] {
79 uplevel #0 source $auto_index($cmd)
82 if [catch {set path
$auto_path}] {
83 if [catch {set path
$env(TCLLIBPATH
)}] {
84 if [catch {set path
[info library
]}] {
89 if [info exists auto_oldpath
] {
90 if {$auto_oldpath == $path} {
94 set auto_oldpath
$path
95 catch {unset auto_index
}
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"
104 while {[gets $f line
] >= 0} {
105 if {([string index
$line 0] == "#") ||
([llength $line] != 2)} {
108 set name
[lindex $line 0]
109 if {![info exists auto_index
($name)]} {
110 set auto_index
($name) $dir/[lindex $line 1]
118 if [info exists auto_index
($cmd)] {
119 uplevel #0 source $auto_index($cmd)
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.
130 proc auto_execok name
{
131 global auto_execs env
133 if [info exists auto_execs
($name)] {
134 return $auto_execs($name)
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
147 # Destroy all cached information for auto-loading and auto-execution,
148 # so that the information gets recomputed the next time it's needed.
151 global auto_execs auto_index
152 unset auto_execs auto_index