Imported from ../bash-1.14.7.tar.gz.
[platform/upstream/bash.git] / examples / functions / autoload
1 #
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.
5 #
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.
9 #
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
13 # `autoload'.
14 #
15 # usage: autoload func [func...]
16 #
17 # The first cut of this was by Bill Trost, trost@reed.bitnet
18 #
19 # Chet Ramey
20 # chet@ins.CWRU.Edu
21
22 #
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.
28 #
29
30 aload()
31 {
32         eval $1 '() {  . '$2' ; '$1' "$@" ; return $?; }'
33 }
34
35 #
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.
38 #
39
40 autoload()
41 {
42         #
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.
45         #
46
47         local args="$*"
48
49         #
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.
52         #
53         if [ $# -eq 0 ] ; then
54                 echo "usage: autoload function [function...]"
55                 return 1
56         fi
57
58         #
59         # If there is no $FPATH, there is no work to be done
60         #
61
62         if [ -z "$FPATH" ] ; then
63                 echo autoload: FPATH not set
64                 return 1
65         fi
66
67         #
68         # This treats FPATH exactly like PATH: a null field anywhere in the
69         # FPATH is treated the same as the current directory.
70         #
71         # The path splitting command is taken from Kernighan and Pike
72         #
73
74         fp=$(echo $FPATH | sed 's/^:/.:/
75                                 s/::/:.:/g
76                                 s/:$/:./
77                                 s/:/ /g')
78
79         for FUNC in $args ; do
80                 #
81                 # We're blowing away the arguments to autoload here...
82                 # We have to; there are no arrays.
83                 #
84                 set $fp
85
86                 while [ $# -ne 0 ] ; do
87                         if [ -f $1/$FUNC ] ; then
88                                 break                   # found it! 
89                         fi
90                         shift
91                 done
92
93                 if [ $# -eq 0 ] ; then
94                         echo "$FUNC: autoload function not found"
95                         continue
96                 fi
97
98 #               echo auto-loading $FUNC from $1/$FUNC
99                 aload $FUNC $1/$FUNC
100         done
101
102         return 0
103 }