Annotation of pkgsrc/mk/scripts/shell-lib, revision 1.4
1.4 ! rillig 1: # $NetBSD: shell-lib,v 1.3 2005/08/24 21:51:10 rillig Exp $
1.1 jlam 2: #
3: # Copyright (c) 2004 The NetBSD Foundation, Inc.
4: # All rights reserved.
5: #
6: # This code is derived from software contributed to The NetBSD Foundation
7: # by Johnny C. Lam.
8: #
9: # Redistribution and use in source and binary forms, with or without
10: # modification, are permitted provided that the following conditions
11: # are met:
12: # 1. Redistributions of source code must retain the above copyright
13: # notice, this list of conditions and the following disclaimer.
14: # 2. Redistributions in binary form must reproduce the above copyright
15: # notice, this list of conditions and the following disclaimer in the
16: # documentation and/or other materials provided with the distribution.
17: # 3. All advertising materials mentioning features or use of this software
18: # must display the following acknowledgement:
19: # This product includes software developed by the NetBSD
20: # Foundation, Inc. and its contributors.
21: # 4. Neither the name of The NetBSD Foundation nor the names of its
22: # contributors may be used to endorse or promote products derived
23: # from this software without specific prior written permission.
24: #
25: # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26: # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27: # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28: # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29: # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30: # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31: # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32: # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33: # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34: # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35: # POSSIBILITY OF SUCH DAMAGE.
1.2 jlam 36: #
1.1 jlam 37:
38: ######################################################################
39: # msg_log logfile msg
40: # Output msg to logfile. If logfile is "stdout" or "stderr"
41: # then output to there instead.
42: ######################################################################
43: msg_log()
44: {
45: : ${echo=echo}
46:
47: _msg_log="$1"; shift
48: case $_msg_log in
49: stdout) $echo "$@" ;;
50: stderr) $echo "$@" 1>&2 ;;
51: *) $echo "$@" >> $_msg_log ;;
52: esac
53: }
54:
55: ######################################################################
56: # die msg
57: # Output $msg to stderr, and exit with a positive error code.
58: ######################################################################
59: die()
60: {
61: msg_log stderr "$@"
62: exit 1
63: }
64:
65: ######################################################################
66: # check_prog var prog ...
67: # If $var is empty or unset, then set it to the path of one of
68: # the program names in the list.
69: ######################################################################
70: check_prog()
71: {
72: : ${test=test}
73:
74: _ckp_var="$1"; shift
75:
76: eval _ckp_tmp=\"\$$_ckp_var\"
77: if $test "x$_ckp_tmp" != "x"; then
78: return 0
79: fi
80:
81: for _ckp_prog do
82: _ckp_IFS="${IFS}"; IFS=":"
83: for _ckp_dir in ${PATH}; do
84: if $test -x "$_ckp_dir/$_ckp_prog"; then
85: eval $_ckp_var=\""$_ckp_dir/$_ckp_prog"\"
86: return 1
87: fi
88: done
89: IFS="${_ckp_IFS}"
90: done
91:
92: die "$_ckp_var could not be set."
93: }
94:
95: ######################################################################
96: # shquote arg
97: # Returns a backslashed and quoted version of arg in $shquoted.
98: ######################################################################
99: shquote()
100: {
101: : ${echo=echo}
102: : ${sed=sed}
103:
104: _shq_arg=$1
105: _shq_sed="$sed -e 1s/^X//"
106: _shq_sed_quote_subst='s/\([\`\"$\\]\)/\\\1/g'
107: case $_shq_arg in
108: *[\`\"\$\\]*)
109: shquoted=`$echo "X$_shq_arg" | $_shq_sed -e "$_shq_sed_quote_subst"`
110: ;;
111: *)
112: shquoted="$_shq_arg"
113: ;;
114: esac
115: case $shquoted in
116: *[\[\~\#\^\&\*\(\)\{\}\|\;\<\>\?\'\ \ ]*|*]*|"")
117: shquoted="\"$shquoted\""
118: ;;
119: esac
120: }
121:
122: ######################################################################
1.2 jlam 123: # lock_file -f path [-n token]
124: # Attempt to create a lockfile at $path. Any directories in the
125: # path should already exist. If $token is specified, then assume
126: # that it is unique between machines sharing an NFS mount.
127: #
128: # (1) Create globally-unique filename in the same filesystem as the
129: # lockfile.
130: # (2) Try to create a hard-link from this file to the lockfile, but
131: # ignoring any errors.
132: # (3) If the two files are the same file, then the lock was successfully
133: # obtained; otherwise, the lock attempt wasn't successful.
134: ######################################################################
135: lock_file()
136: {
137: : ${dirname=dirname}
138: : ${echo=echo}
139: : ${link=link}
140: : ${mkdir=mkdir}
141: : ${mktemp=mktemp}
142: : ${rm=rm}
143: : ${test=test}
144: : ${touch=touch}
145:
146: _lf_lockfile=
147: _lf_nfs=
148: while $test $# -gt 0; do
149: case $1 in
150: -f) _lf_lockfile="$2"; shift ;;
151: -n) _lf_nfs="$2"; shift ;;
152: esac
153: shift
154: done
155: if $test -z "$_lf_lockfile"; then
156: $echo 1>&2 "$0: no lock file specified."
157: exit
158: fi
159: _lf_pid=$$
160: _lf_lockdir=`$dirname $_lf_lockfile`
161: _lf_uniqfile=`$mktemp "$_lf_lockdir/.lock.$_lf_nfs.$_lf_pid.XXXXXX" 2>/dev/null` || return 1
162: if $test -n "$_lf_nfs"; then
163: { $echo $_lf_pid; $echo $_lf_nfs; } > $_lf_uniqfile
164: else
165: $echo $_lf_pid > $_lf_uniqfile
166: fi
167: $link $_lf_uniqfile $_lf_lockfile 2>/dev/null
168: if $test $_lf_uniqfile -ef $_lf_lockfile; then
169: _lf_result=0
170: else
171: _lf_result=1
172: fi
173: $rm -f $_lf_uniqfile
174: return $_lf_result
175: }
176:
177: ######################################################################
1.1 jlam 178: ######################################################################
179: ###
180: ### Queue routines. The queue is implemented as a set of variables
181: ### that is unique to each queue name, thus the use of multiple queues
182: ### is allowed.
183: ###
184: ######################################################################
185: ######################################################################
186:
187: ######################################################################
188: # init_queue name
189: # Initialize the named queue.
190: ######################################################################
191: init_queue()
192: {
193: _qname="$1"
194: eval "_q${_qname}head=1111111111"
195: eval "_q${_qname}tail=1111111111"
196: }
197:
198: ######################################################################
199: # append_queue name item ...
200: # Append items onto the end of the named queue in FIFO order.
201: ######################################################################
202: append_queue()
203: {
204: : ${test=test}
205:
206: _qname="$1"; shift
207: while $test $# -gt 0; do
208: eval "_qtail=\"\$_q${_qname}tail\""
209: eval "_q${_qname}${_qtail}=\"\${1}\""
210: case $_qtail in
211: *000000000) _qtail=${_qtail%000000000}1 ;;
212: *) _qtail=${_qtail}0 ;;
213: esac
214: eval "_q${_qname}tail=\"\${_qtail}\""
215: shift
216: done
217: }
218:
219: ######################################################################
220: # prepend_queue name item ...
221: # Prepend items to the head of the named queue in LIFO order.
222: ######################################################################
223: prepend_queue()
224: {
225: : ${test=test}
226:
227: _qname="$1"; shift
228: while $test $# -gt 0; do
229: eval "_qhead=\"\$_q${_qname}head\""
230: case $_qhead in
231: *1) _qhead=${_qhead%1}000000000 ;;
232: *) _qhead=${_qhead%0} ;;
233: esac
234: eval "_q${_qname}${_qhead}=\"\${1}\""
235: eval "_q${_qname}head=\"\${_qhead}\""
236: shift
237: done
238: }
239:
240: ######################################################################
241: # head_queue name var
242: # Return the head of the named queue in $var.
243: ######################################################################
244: head_queue()
245: {
246: _qname="$1"; shift
247: eval "_qhead=\"\$_q${_qname}head\""
248: eval "${1}=\"\$_q${_qname}${_qhead}\""
249: }
250:
251: ######################################################################
252: # pop_queue name var
253: # Pop off the head of the named queue and return it in $var.
254: ######################################################################
255: pop_queue()
256: {
257: _qname="$1"; shift
258: head_queue $_qname $1
259: case $_qhead in
260: *000000000) _qhead=${_qhead%000000000}1 ;;
261: *) _qhead=${_qhead}0 ;;
262: esac
263: eval "_q${_qname}head=\"\${_qhead}\""
264: }
265:
266: ######################################################################
267: # queue_is_empty name
268: # Return 0 if the named queue is empty and 1 otherwise.
269: ######################################################################
270: queue_is_empty()
271: {
272: : ${test=test}
273:
274: _qname="$1"
275: eval "_qhead=\"\$_q${_qname}head\""
276: eval "_qtail=\"\$_q${_qname}tail\""
277: $test "$_qhead" = "$_qtail"
278: return $?
279: }
280:
281: ######################################################################
282: ######################################################################
283: ###
284: ### File queue routines. The file queue is implemented as a file
285: ### whose lines represent the queue elements. The file queue name
286: ### is simply the file used for the queue, thus the use of multiple
287: ### queues is allowed.
288: ###
289: ######################################################################
290: ######################################################################
291:
292: ######################################################################
293: # init_fqueue name
294: # Initialize the named file queue.
295: ######################################################################
296: init_fqueue()
297: {
298: _fqname="$1"
299: : > "$_fqname"
300: }
301:
302: ######################################################################
303: # append_fqueue name item ...
304: # Append items onto the end of the named file queue in FIFO order.
305: ######################################################################
306: append_fqueue()
307: {
308: : ${echo=echo}
309: : ${test=test}
310:
311: _fqname="$1"; shift
312: while $test $# -gt 0; do
313: $echo "$1" >> "$_fqname"
314: shift
315: done
316: }
317:
318: ######################################################################
319: # prepend_fqueue name item ...
320: # Prepend items to the head of the named file queue in LIFO order.
321: ######################################################################
322: prepend_fqueue()
323: {
324: : ${cat=cat}
325: : ${echo=echo}
326: : ${mv=mv}
327:
328: _fqname="$1"; shift
329: _fqtmpfile="$_fqname.$$"
330: init_queue _fqtmpqueue
331: prepend_queue _fqtmpqueue "$@"
332: while ! queue_is_empty _fqtmpqueue; do
333: pop_queue _fqtmpqueue _fqelt
334: $echo "$_fqelt" >> "$_fqtmpfile"
335: done
336: $cat "$_fqname" >> "$_fqtmpfile"
337: $mv -f "$_fqtmpfile" "$_fqname"
338: }
339:
340: ######################################################################
341: # head_fqueue name var
342: # Return the head of the named file queue in $var.
343: ######################################################################
344: head_fqueue()
345: {
346: : ${head=head}
347:
348: _fqname="$1"; shift
349: _tmp=`$head -n 1 "$_fqname"`
350: eval "${1}=\"\$_tmp\""
351: }
352:
353: ######################################################################
354: # pop_fqueue name var
355: # Pop off the head of the named file queue and return it in $var.
356: ######################################################################
357: pop_fqueue()
358: {
359: : ${mv=mv}
360: : ${sed=sed}
361:
362: _fqname="$1"; shift
363: _fqtmpfile="$_fqname.$$"
364: head_fqueue "$_fqname" $1
365: $sed "1,1d" "$_fqname" >> "$_fqtmpfile"
366: $mv -f "$_fqtmpfile" "$_fqname"
367: }
368:
369: ######################################################################
370: # fqueue_is_empty name
371: # Return 0 if the named file queue is empty and >0 otherwise.
372: ######################################################################
373: fqueue_is_empty()
374: {
375: : ${test=test}
376: : ${wc=wc}
377:
378: _fqname="$1"
379: if $test -f "$_fqname"; then
380: _fqlines=`$wc -l < "$_fqname"`
381: return $_fqlines
382: else
383: return 1
384: fi
385: }
CVSweb <webmaster@jp.NetBSD.org>