source: vendor/bash/3.1-p17/examples/scripts.noah/require.bash

Last change on this file was 3228, checked in by bird, 18 years ago

bash 3.1

File size: 5.2 KB
Line 
1# require.bash
2# Author: Noah Friedman <friedman@prep.ai.mit.edu>
3# Created: 1992-07-08
4# Last modified: 1993-09-29
5# Public domain
6
7# Commentary:
8
9# These functions provide an interface based on the lisp implementation for
10# loading libraries when they are needed and eliminating redundant loading.
11# The basic idea is that each "package" (or set of routines, even if it is
12# only one function) registers itself with a symbol that marks a "feature"
13# as being "provided". If later you "require" a given feature, you save
14# yourself the trouble of explicitly loading it again.
15#
16# At the bottom of each package, put a "provide foobar", so when another
17# package has a "require foobar", it gets loaded and registered as a
18# "feature" that won't need to get loaded again. (See warning below for
19# reasons why provide should be put at the end.)
20#
21# The list of provided features are kept in the `FEATURES' variable, which
22# is not exported. Care should be taken not to munge this in the shell.
23# The search path comes from a colon-separated `FPATH' variable. It has no
24# default value and must be set by the user.
25#
26# Require uses `fpath_search', which works by scanning all of FPATH for a
27# file named the same as the required symbol but with a `.bash' appended to
28# the name. If that is found, it is loaded. If it is not, FPATH is
29# searched again for a file name the same as the feature (i.e. without any
30# extension). Fpath_search may be useful for doing library filename
31# lookups in other functions (such as a `load' or `autoload' function).
32#
33# Warning: Because require ultimately uses the builtin `source' command to
34# read in files, it has no way of undoing the commands contained in the
35# file if there is an error or if no provide statement appeared (this
36# differs from the lisp implementation of require, which normally undoes
37# most of the forms that were loaded if the require fails). Therefore, to
38# minize the number of problems caused by requiring a faulty package (such
39# as syntax errors in the source file) it is better to put the provide at
40# the end of the file, rather than at the beginning.
41
42# Code:
43
44# Exporting this variable would cause considerable lossage, since none of
45# the functions are exported (or at least, they're not guaranteed to be)
46export -n FEATURES
47
48#:docstring
49:
50# Null function. Provided only so that one can put page breaks in source
51# files without any ill effects.
52#:end docstring:
53#
54# (\\014 == C-l)
55eval "function $(echo -e \\014) () { : }"
56
57
58#:docstring featurep:
59# Usage: featurep argument
60#
61# Returns 0 (true) if argument is a provided feature. Returns 1 (false)
62# otherwise.
63#:end docstring:
64
65###;;;autoload
66function featurep ()
67{
68 local feature="$1"
69
70 case " ${FEATURES} " in
71 *" ${feature} "* ) return 0 ;;
72 esac
73
74 return 1
75}
76
77
78#:docstring provide:
79# Usage: provide symbol ...
80#
81# Register a list of symbols as provided features
82#:end docstring:
83
84###;;;autoload
85function provide ()
86{
87 local feature
88
89 for feature in "$@" ; do
90 if ! featurep "${feature}" ; then
91 FEATURES="${FEATURES} ${feature}"
92 fi
93 done
94
95 return 0
96}
97
98
99#:docstring require:
100# Usage: require feature {file}
101#
102# Load FEATURE if it is not already provided. Note that require does not
103# call `provide' to register features. The loaded file must do that
104# itself. If the package does not explicitly do a `provide' after being
105# loaded, require will complain about the feature not being provided on
106# stderr.
107#
108# Optional argument FILE means to try to load FEATURE from FILE. If no
109# file argument is given, require searches through FPATH (see fpath_search)
110# for the appropriate file.
111#
112# If the variable REQUIRE_FAILURE_FATAL is set, require will cause the
113# current shell invocation to exit, rather than merely return. This may be
114# useful for a shell script that vitally depends on a package.
115#
116#:end docstring:
117
118###;;;autoload
119function require ()
120{
121 local feature="$1"
122 local path="$2"
123 local file
124
125 if ! featurep "${feature}" ; then
126 file=$(fpath_search "${feature}" "${path}") && source "${file}"
127
128 if ! featurep "${feature}" ; then
129 echo "require: ${feature}: feature was not provided." 1>&2
130 if [ "${REQUIRE_FAILURE_FATAL+set}" = "set" ]; then
131 exit 1
132 fi
133 return 1
134 fi
135 fi
136
137 return 0
138}
139
140#:docstring fpath_search:
141# Usage: fpath_search filename {path ...}
142#
143# Search $FPATH for `filename' or, if `path' (a list) is specified, search
144# those directories instead of $FPATH. First the path is searched for an
145# occurrence of `filename.bash, then a second search is made for just
146# `filename'.
147#:end docstring:
148
149###;;;autoload
150function fpath_search ()
151{
152 local name="$1"
153 local path="$2"
154 local suffix=".bash"
155 local file
156
157 if [ -z "${path}" ]; then path="${FPATH}"; fi
158
159 for file in "${name}${suffix}" "${name}" ; do
160 set -- $(IFS=':'
161 set -- ${path}
162 for p in "$@" ; do
163 echo -n "${p:-.} "
164 done)
165
166 while [ $# -ne 0 ]; do
167 test -f "${1}/${file}" && { file="${1}/${file}"; break 2 }
168 shift
169 done
170 done
171
172 if [ $# -eq 0 ]; then
173 echo "fpath_search: ${name}: file not found in fpath" 1>&2
174 return 1
175 fi
176
177 echo "${file}"
178 return 0
179}
180
181provide require
182
183# require.bash ends here
Note: See TracBrowser for help on using the repository browser.