Annotation of pkgsrc/mk/scripts/shell-lib, revision 1.1
1.1 ! jlam 1: # $NetBSD$
! 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.
! 36:
! 37: ######################################################################
! 38: # msg_log logfile msg
! 39: # Output msg to logfile. If logfile is "stdout" or "stderr"
! 40: # then output to there instead.
! 41: ######################################################################
! 42: msg_log()
! 43: {
! 44: : ${echo=echo}
! 45:
! 46: _msg_log="$1"; shift
! 47: case $_msg_log in
! 48: stdout) $echo "$@" ;;
! 49: stderr) $echo "$@" 1>&2 ;;
! 50: *) $echo "$@" >> $_msg_log ;;
! 51: esac
! 52: }
! 53:
! 54: ######################################################################
! 55: # die msg
! 56: # Output $msg to stderr, and exit with a positive error code.
! 57: ######################################################################
! 58: die()
! 59: {
! 60: msg_log stderr "$@"
! 61: exit 1
! 62: }
! 63:
! 64: ######################################################################
! 65: # check_prog var prog ...
! 66: # If $var is empty or unset, then set it to the path of one of
! 67: # the program names in the list.
! 68: ######################################################################
! 69: check_prog()
! 70: {
! 71: : ${test=test}
! 72:
! 73: _ckp_var="$1"; shift
! 74:
! 75: eval _ckp_tmp=\"\$$_ckp_var\"
! 76: if $test "x$_ckp_tmp" != "x"; then
! 77: return 0
! 78: fi
! 79:
! 80: for _ckp_prog do
! 81: _ckp_IFS="${IFS}"; IFS=":"
! 82: for _ckp_dir in ${PATH}; do
! 83: if $test -x "$_ckp_dir/$_ckp_prog"; then
! 84: eval $_ckp_var=\""$_ckp_dir/$_ckp_prog"\"
! 85: return 1
! 86: fi
! 87: done
! 88: IFS="${_ckp_IFS}"
! 89: done
! 90:
! 91: die "$_ckp_var could not be set."
! 92: }
! 93:
! 94: ######################################################################
! 95: # shquote arg
! 96: # Returns a backslashed and quoted version of arg in $shquoted.
! 97: ######################################################################
! 98: shquote()
! 99: {
! 100: : ${echo=echo}
! 101: : ${sed=sed}
! 102:
! 103: _shq_arg=$1
! 104: _shq_sed="$sed -e 1s/^X//"
! 105: _shq_sed_quote_subst='s/\([\`\"$\\]\)/\\\1/g'
! 106: case $_shq_arg in
! 107: *[\`\"\$\\]*)
! 108: shquoted=`$echo "X$_shq_arg" | $_shq_sed -e "$_shq_sed_quote_subst"`
! 109: ;;
! 110: *)
! 111: shquoted="$_shq_arg"
! 112: ;;
! 113: esac
! 114: case $shquoted in
! 115: *[\[\~\#\^\&\*\(\)\{\}\|\;\<\>\?\'\ \ ]*|*]*|"")
! 116: shquoted="\"$shquoted\""
! 117: ;;
! 118: esac
! 119: }
! 120:
! 121: ######################################################################
! 122: ######################################################################
! 123: ###
! 124: ### Queue routines. The queue is implemented as a set of variables
! 125: ### that is unique to each queue name, thus the use of multiple queues
! 126: ### is allowed.
! 127: ###
! 128: ######################################################################
! 129: ######################################################################
! 130:
! 131: ######################################################################
! 132: # init_queue name
! 133: # Initialize the named queue.
! 134: ######################################################################
! 135: init_queue()
! 136: {
! 137: _qname="$1"
! 138: eval "_q${_qname}head=1111111111"
! 139: eval "_q${_qname}tail=1111111111"
! 140: }
! 141:
! 142: ######################################################################
! 143: # append_queue name item ...
! 144: # Append items onto the end of the named queue in FIFO order.
! 145: ######################################################################
! 146: append_queue()
! 147: {
! 148: : ${test=test}
! 149:
! 150: _qname="$1"; shift
! 151: while $test $# -gt 0; do
! 152: eval "_qtail=\"\$_q${_qname}tail\""
! 153: eval "_q${_qname}${_qtail}=\"\${1}\""
! 154: case $_qtail in
! 155: *000000000) _qtail=${_qtail%000000000}1 ;;
! 156: *) _qtail=${_qtail}0 ;;
! 157: esac
! 158: eval "_q${_qname}tail=\"\${_qtail}\""
! 159: shift
! 160: done
! 161: }
! 162:
! 163: ######################################################################
! 164: # prepend_queue name item ...
! 165: # Prepend items to the head of the named queue in LIFO order.
! 166: ######################################################################
! 167: prepend_queue()
! 168: {
! 169: : ${test=test}
! 170:
! 171: _qname="$1"; shift
! 172: while $test $# -gt 0; do
! 173: eval "_qhead=\"\$_q${_qname}head\""
! 174: case $_qhead in
! 175: *1) _qhead=${_qhead%1}000000000 ;;
! 176: *) _qhead=${_qhead%0} ;;
! 177: esac
! 178: eval "_q${_qname}${_qhead}=\"\${1}\""
! 179: eval "_q${_qname}head=\"\${_qhead}\""
! 180: shift
! 181: done
! 182: }
! 183:
! 184: ######################################################################
! 185: # head_queue name var
! 186: # Return the head of the named queue in $var.
! 187: ######################################################################
! 188: head_queue()
! 189: {
! 190: _qname="$1"; shift
! 191: eval "_qhead=\"\$_q${_qname}head\""
! 192: eval "${1}=\"\$_q${_qname}${_qhead}\""
! 193: }
! 194:
! 195: ######################################################################
! 196: # pop_queue name var
! 197: # Pop off the head of the named queue and return it in $var.
! 198: ######################################################################
! 199: pop_queue()
! 200: {
! 201: _qname="$1"; shift
! 202: head_queue $_qname $1
! 203: case $_qhead in
! 204: *000000000) _qhead=${_qhead%000000000}1 ;;
! 205: *) _qhead=${_qhead}0 ;;
! 206: esac
! 207: eval "_q${_qname}head=\"\${_qhead}\""
! 208: }
! 209:
! 210: ######################################################################
! 211: # queue_is_empty name
! 212: # Return 0 if the named queue is empty and 1 otherwise.
! 213: ######################################################################
! 214: queue_is_empty()
! 215: {
! 216: : ${test=test}
! 217:
! 218: _qname="$1"
! 219: eval "_qhead=\"\$_q${_qname}head\""
! 220: eval "_qtail=\"\$_q${_qname}tail\""
! 221: $test "$_qhead" = "$_qtail"
! 222: return $?
! 223: }
! 224:
! 225: ######################################################################
! 226: ######################################################################
! 227: ###
! 228: ### File queue routines. The file queue is implemented as a file
! 229: ### whose lines represent the queue elements. The file queue name
! 230: ### is simply the file used for the queue, thus the use of multiple
! 231: ### queues is allowed.
! 232: ###
! 233: ######################################################################
! 234: ######################################################################
! 235:
! 236: ######################################################################
! 237: # init_fqueue name
! 238: # Initialize the named file queue.
! 239: ######################################################################
! 240: init_fqueue()
! 241: {
! 242: _fqname="$1"
! 243: : > "$_fqname"
! 244: }
! 245:
! 246: ######################################################################
! 247: # append_fqueue name item ...
! 248: # Append items onto the end of the named file queue in FIFO order.
! 249: ######################################################################
! 250: append_fqueue()
! 251: {
! 252: : ${echo=echo}
! 253: : ${test=test}
! 254:
! 255: _fqname="$1"; shift
! 256: while $test $# -gt 0; do
! 257: $echo "$1" >> "$_fqname"
! 258: shift
! 259: done
! 260: }
! 261:
! 262: ######################################################################
! 263: # prepend_fqueue name item ...
! 264: # Prepend items to the head of the named file queue in LIFO order.
! 265: ######################################################################
! 266: prepend_fqueue()
! 267: {
! 268: : ${cat=cat}
! 269: : ${echo=echo}
! 270: : ${mv=mv}
! 271:
! 272: _fqname="$1"; shift
! 273: _fqtmpfile="$_fqname.$$"
! 274: init_queue _fqtmpqueue
! 275: prepend_queue _fqtmpqueue "$@"
! 276: while ! queue_is_empty _fqtmpqueue; do
! 277: pop_queue _fqtmpqueue _fqelt
! 278: $echo "$_fqelt" >> "$_fqtmpfile"
! 279: done
! 280: $cat "$_fqname" >> "$_fqtmpfile"
! 281: $mv -f "$_fqtmpfile" "$_fqname"
! 282: }
! 283:
! 284: ######################################################################
! 285: # head_fqueue name var
! 286: # Return the head of the named file queue in $var.
! 287: ######################################################################
! 288: head_fqueue()
! 289: {
! 290: : ${head=head}
! 291:
! 292: _fqname="$1"; shift
! 293: _tmp=`$head -n 1 "$_fqname"`
! 294: eval "${1}=\"\$_tmp\""
! 295: }
! 296:
! 297: ######################################################################
! 298: # pop_fqueue name var
! 299: # Pop off the head of the named file queue and return it in $var.
! 300: ######################################################################
! 301: pop_fqueue()
! 302: {
! 303: : ${mv=mv}
! 304: : ${sed=sed}
! 305:
! 306: _fqname="$1"; shift
! 307: _fqtmpfile="$_fqname.$$"
! 308: head_fqueue "$_fqname" $1
! 309: $sed "1,1d" "$_fqname" >> "$_fqtmpfile"
! 310: $mv -f "$_fqtmpfile" "$_fqname"
! 311: }
! 312:
! 313: ######################################################################
! 314: # fqueue_is_empty name
! 315: # Return 0 if the named file queue is empty and >0 otherwise.
! 316: ######################################################################
! 317: fqueue_is_empty()
! 318: {
! 319: : ${test=test}
! 320: : ${wc=wc}
! 321:
! 322: _fqname="$1"
! 323: if $test -f "$_fqname"; then
! 324: _fqlines=`$wc -l < "$_fqname"`
! 325: return $_fqlines
! 326: else
! 327: return 1
! 328: fi
! 329: }
CVSweb <webmaster@jp.NetBSD.org>