aboutsummaryrefslogtreecommitdiffstats
path: root/examples/functions/autoload
blob: a563a77910e17b787ef1e1abe4e45bf95709074c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#
# An almost ksh-compatible `autoload'.  A function declared as `autoload' will
# be read in from a file the same name as the function found by searching the
# $FPATH (which works the same as $PATH), then that definition will be run.
#
# To do this without source support, we define a dummy function that, when
# executed, will load the file (thereby re-defining the function), then 
# execute that newly-redefined function with the original arguments.
#
# It's not identical to ksh because ksh apparently does lazy evaluation
# and looks for the file to load from only when the function is referenced.
# This one requires that the file exist when the function is declared as
# `autoload'.
#
# usage: autoload func [func...]
#
# The first cut of this was by Bill Trost, trost@reed.bitnet
#
# Chet Ramey
# chet@ins.CWRU.Edu

#
# Declare a function ($1) to be autoloaded from a file ($2) when it is first
# called.  This defines a `temporary' function that will `.' the file 
# containg the real function definition, then execute that new definition with
# the arguments given to this `fake' function.  The autoload function defined
# by the file and the file itself *must* be named identically.
#

aload()
{
	eval $1 '() {  . '$2' ; '$1' "$@" ; return $? ; }'
}

#
# Search $FPATH for a file the same name as the function given as $1, and
# autoload the function from that file.  There is no default $FPATH.
#

autoload()
{
	#
	# Save the list of functions; we're going to blow away the arguments
	# in a second.  If any of the names contain white space, TFB.
	#

	local args="$*"

	#
	# This should, I think, list the functions marked as autoload and not
	# yet defined, but we don't have enough information to do that here.
	#
	if [ $# -eq 0 ] ; then
		echo "usage: autoload function [function...]" >&2
		return 1
	fi

	#
	# If there is no $FPATH, there is no work to be done
	#

	if [ -z "$FPATH" ] ; then
		echo autoload: FPATH not set or null >&2
		return 1
	fi

	#
	# This treats FPATH exactly like PATH: a null field anywhere in the
	# FPATH is treated the same as the current directory.
	#
	# The path splitting command is taken from Kernighan and Pike
	#

#	fp=$(echo $FPATH | sed 's/^:/.:/
#				s/::/:.:/g
#				s/:$/:./
#				s/:/ /g')

	# replaced with builtin mechanisms 2001 Oct 10

	fp=${FPATH/#:/.:}
	fp=${fp//::/:.:}
	fp=${fp/%:/:.}
	fp=${fp//:/ }

	for FUNC in $args ; do
		#
		# We're blowing away the arguments to autoload here...
		# We have to; there are no arrays (well, there are, but
		# this doesn't use them yet).
		#
		set -- $fp

		while [ $# -ne 0 ] ; do
			if [ -f $1/$FUNC ] ; then
				break			# found it! 
			fi
			shift
		done

		if [ $# -eq 0 ] ; then
			echo "$FUNC: autoload function not found" >&2
			continue
		fi

#		echo auto-loading $FUNC from $1/$FUNC
		aload $FUNC $1/$FUNC
	done

	return 0
}