Annotation of pkgsrc/mk/scripts/shell-lib, revision 1.5

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

CVSweb <webmaster@jp.NetBSD.org>