2 # An almost ksh-compatible `autoload'. A function declared as `autoload' will
3 # be read in from a file the same name as the function found by searching the
4 # $FPATH (which works the same as $PATH), then that definition will be run.
6 # To do this without source support, we define a dummy function that, when
7 # executed, will load the file (thereby re-defining the function), then
8 # execute that newly-redefined function with the original arguments.
10 # It's not identical to ksh because ksh apparently does lazy evaluation
11 # and looks for the file to load from only when the function is referenced.
12 # This one requires that the file exist when the function is declared as
15 # usage: autoload func [func...]
17 # The first cut of this was by Bill Trost, trost@reed.bitnet
23 # Declare a function ($1) to be autoloaded from a file ($2) when it is first
24 # called. This defines a `temporary' function that will `.' the file
25 # containg the real function definition, then execute that new definition with
26 # the arguments given to this `fake' function. The autoload function defined
27 # by the file and the file itself *must* be named identically.
32 eval $1 '() { . '$2' ; '$1' "$@" ; return $? ; }'
36 # Search $FPATH for a file the same name as the function given as $1, and
37 # autoload the function from that file. There is no default $FPATH.
43 # Save the list of functions; we're going to blow away the arguments
44 # in a second. If any of the names contain white space, TFB.
50 # This should, I think, list the functions marked as autoload and not
51 # yet defined, but we don't have enough information to do that here.
53 if [ $# -eq 0 ] ; then
54 echo "usage: autoload function [function...]" >&2
59 # If there is no $FPATH, there is no work to be done
62 if [ -z "$FPATH" ] ; then
63 echo autoload: FPATH not set or null >&2
68 # This treats FPATH exactly like PATH: a null field anywhere in the
69 # FPATH is treated the same as the current directory.
71 # The path splitting command is taken from Kernighan and Pike
74 # fp=$(echo $FPATH | sed 's/^:/.:/
79 # replaced with builtin mechanisms 2001 Oct 10
86 for FUNC in $args ; do
88 # We're blowing away the arguments to autoload here...
89 # We have to; there are no arrays (well, there are, but
90 # this doesn't use them yet).
94 while [ $# -ne 0 ] ; do
95 if [ -f $1/$FUNC ] ; then
101 if [ $# -eq 0 ] ; then
102 echo "$FUNC: autoload function not found" >&2
106 # echo auto-loading $FUNC from $1/$FUNC