Previous Table of Contents Next


arch.sh.fctn Function

The arch.sh.fctn script emulates SunOS 4.x system architecture.

#
#  %M%:    Version:  %I%    Date:     %G%
#

[ $_Arch_loaded ] || {


# Function:
#   @(#)Arch                     (Wayne.Thompson@Sun.COM)05/14/92
#
# Description:
#   This function emulates 4.x arch(1).
#
# Variables:
#
# Usage:
#   Arch
#   Arch -k
#   Arch archname
#
# Return:
#   Ø success
#   1 non-success
#
# Dependencies:
#   This function works under SunOs 3.x - 5.x.
#
# Bugs:
#

Arch () {
  case $1 in
    ")
       /bin/arch $* 2>&- || /bin/expr `/bin/uname -m`: '\
             (sun[Ø-9]*\)' ;;

        -k)
            /bin/arch $* 2>&- || /bin/uname -m
            ;;

        *)
            if [ -x /bin/arch ]
            then
                /bin/arch $*
            else
                [ $* = `/bin/expr \`/bin/uname -m\` :'\(sun[Ø-9]*\)'` ]
            fi
            ;;
    esac
}

_Arch_loaded=1
}

array.sh.fctn Function

The following function extends the functionality of the Bourneshell to include associative arrays:

## >> BEGIN package: arrays>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ##

# Package:
#     @(#)arrays,v1.Ø         (Wayne.Thompson@Sun.COM) 05/02/93
#
# Description:
#     This package contains functions that emulate
      associativearrays.
#     Keys are limited to letters, digits, underscores, hyphens
      and periods.
#
# Variables:
#     __array_size_<array>
#     __array_keys_<array>
#     __array__<array>_<key>
#     __array_filename
#     __array_name
#     __array_key
#     __array_keys
#     __array_cell
#
# Usage:
#     setarray array key [value ...]
#     getarray array key
#     unsetarray array [key ...]
#     keys array
#     sizearray array
#     defined array [key]
#     dumparray pathname [array ...]
#
# Dependencies:
#     Array names and keys are combined to form strings which are
#     evaluated as parameters.
#
# Bugs:
#
### >> BEGIN function: setarray>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ###

# Function:
#     @(#)setarray,v1.Ø           (Wayne.Thompson@Sun.COM) 05/16/93
#
# Description:
#     This function assigns values to array elements. If more thanone
#     value is provided and the key is an integer, values will be
#     assigned to successive elements beginning with the initialkey.
#
# Variables:
#     __array_size_<array>
#     __array_keys_<array>
#     __array__<array>_<key>
#     __array_name
#     __array_key
#     __array_cell
#
# Usage:
#     setarray array key [value ...]
#     setarray pathname
#
# Return:
#
# Dependencies:
#
# Bugs:
#

setarray () {
    case $# in
        Ø)
            echo >&2 setarray: error: "$@"
            echo >&2 usage: setarray array key '[value ...]'
            exit 1
            ;;

        1)
            if [ -f $1 ]
            then
                while read __array_name __array_key __array_value
                do
                    setarray $__array_name $__array_key$__array_value
                done < $1
                return
            else
                echo >&2 setarray: error: $1: no such file
                exit 1
            fi
            ;;

        2|3)
            ;;

        *)
            case $2 in
                [Ø-9]*)
                    ;;

                *)
                    echo >&2 setarray: error: "$@"
                    echo >&2 setarray: error: 2nd argumentmust be an
                    integer
                    exit 1
                    ;;
            esac
            ;;
    esac
    __array_name=$1
    __array_key=$2
    case $2 in
        *[-.]*)
            __array_cell=`echo $2 | /bin/tr '[-.]' __`
            shift 2
            set $__array_name $__array_cell "$@"
            ;;
    esac
    eval "
        if [ \${__array_size_$1+1} ]
        then
            [ \${__array__$1_$2+1} ] || {
                __array_size_$1=\`/bin/expr\$__array_size_$1 + 1\`
                __array_keys_$1=\"\$__array_keys_$1\$__array_key \"
            }
        else
            __array_size_$1=1
            __array_keys_$1=\$__array_key
        fi
        __array__$1_$2=\$3
    "
    case $# in
        2)
            shift 2
            ;;
        *)
            shift 3
            ;;
    esac
    while [ $# -gt Ø ]
    do
        __array_key=`/bin/expr $__array_key + 1`
        eval "
            [ \${__array__${__array_name}_$__array_key+1}] || {
             __array_size_$__array_name=\`/bin/expr\
              $__array_size_$__array_name + 1\`
          __array_keys_$__array_name=\"\$__array_keys_$__array_name
           \$__array_key\"
            }
            __array__${__array_name}_$__array_key=\$1
        "
        shift
    done
}

### << END function: setarray <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ###

### >> BEGIN function: getarray>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ###

# Function:
#     @(#)getarray,v1.Ø           (Wayne.Thompson@Sun.COM) 05/02/93
#
# Description:
#     This function prints array values.
#
# Variables:
#     __array__<array>_<key>
#     __array_name
#
# Usage:
#     getarray array [key ...]
#
# Return:
#
# Dependencies:
#
# Bugs:
#

getarray () {
    case $# in
        Ø)
            echo >&2 getarray: error: "$@"
            echo >&2 usage: getarray array '[key ...]'
            exit 1
            ;;

        1)  # entire array
            set $1 `keys $1`
            ;;

    esac
    __array_name=$1
    shift
    while [ $# -gt Ø ]
    do
        case $1 in
            *[-.]*)
                eval echo \$__array__${__array_name}_`echo $1|
                  /bin/tr '[-.]' __`
                ;;

            *)
                eval echo \$__array__${__array_name}_$1
                ;;
        esac
        shift
    done
}
### << END function: getarray<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ###

### >> BEGIN function: unsetarray >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ###

# Function:
#     @(#)unsetarray,v1.Ø           Wayne.Thompson@Sun.COM) 05/02/93
#
# Description:
#     This function unsets (undefines) one or more array elements
#     or an entire array.
#
# Variables:
#     __array_size_<array>
#     __array_keys_<array>
#     __array__<array>_<key>
#     __array_name
#     __array_key
#     __array_keys
#
# Usage:
#     unsetarray array [key ...]
#
# Return:
#
# Dependencies:
#
# Bugs:
#
unsetarray () {
    case $# in
        Ø)
            echo >&2 unsetarray: error: "$@"
            echo >&2 usage: unsetarray array '[key ...]'
            exit 1
            ;;

        1)  # entire array
            set $1 `keys $1`
            ;;
    esac
    __array_name=$1
    shift
    while [ $# -gt Ø ]
    do
        eval "
            __array_keys=
            [ \${__array__${__array_name}_$1+1} ]&&
            for __array_key in \$__array_keys_$__array_name
            do
                case \$__array_key in
                    \$1)
                        case \$1 in
                            *[-.]*)
                               unset__array__${__array_name}_\`echo
      \$1 | /bin/tr '[-.]' __\`
                                ;;
                            *)
                                unset __array__${__array_name}_$1
                                ;;
                        esac
                        __array_size_$__array_name=\`/bin/expr
                        \$__array_size_$__array_name - 1\`
                        ;;
                            *)
                        case \$__array_keys in
                            '')
                                __array_keys=\$__array_key
                                ;;
                            *)
                               __array_keys=\"\$__array_keys\$__array_key
                                \" ;;
                        esac
                        ;;
                esac
            done
            __array_keys_$__array_name=\$__array_keys
        "
        shift
    done
    eval "
        case \$__array_size_$__array_name in
            Ø)
                unset __array_size_$__array_name__array_keys_$__
                 array_name ;;
        esac
    "
}
### << END function: unsetarray<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ###

### >> BEGIN function: keys>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ###
# Function:
#     @(#)keys,v1.Ø            (Wayne.Thompson@Sun.COM) 05/02/93
#
# Description:
#     This function prints the keys of an array.
#
# Variables:
#     __array_keys_<array>
#
# Usage:
#     keys array
#
# Return:
#
# Dependencies:
#
# Bugs:
#

keys () {
    case $# in
        1)
            eval echo \$__array_keys_$1
            ;;

        *)
            echo >&2 keys: error: "$@"
            echo >&2 usage: keys array
            exit 1
            ;;
    esac
}

### << END function: keys<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ###

### >> BEGIN function: sizearray>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>###

# Function:
#     @(#)sizearray,v1.Ø               (Wayne.Thompson@Sun.COM) 05/02/93
#
# Description:
#     This function prints the number of defined array elements.
#
# Variables:
#     __array_size_<array>
#
# Usage:
#     sizearray array
#
# Return:
#
# Dependencies:
#
# Bugs:
#

sizearray () {
    case $# in
        1)
            eval echo \${__array_size_$1:-Ø}
            ;;

        *)
            echo >&2 sizearray: error: "$@"
            echo >&2 usage: sizearray array
            exit 1
            ;;

    esac
}

### << END function: sizearray<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ###

### >> BEGIN function: defined>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ###

# Function:
#     @(#)defined,v1.Ø             (Wayne.Thompson@Sun.COM) 05/02/93
#
# Description:
#     This function returns whether an array element or array is
      defined.
#
# Variables:
#     __array_size_<array>
#     __array__<array>_<key>
#
# Usage:
#     defined array [key]
#
# Return:
#     Ø         defined
#     1         undefined
#
# Dependencies:
#
# Bugs:
#

defined () {
    case $# in
        1)
            eval set \${__array_size_$1+Ø} 1
            ;;

        2)
            case $2 in
                *[-.]*) set $1 `echo $2 | /bin/tr '[-.]' __`;;
            esac
            eval set \${__array__$1_$2+Ø} 1
            ;;

        *)
            echo >&2 defined: error: "$@"
            echo >&2 usage: defined array '[key]'
            exit 1
            ;;

    esac
    return $1
}
### << END function: defined<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ###

### >> BEGIN function: dumparray>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ###
# Function:
#     @(#)dumparray,v1.Ø              (Wayne.Thompson@Sun.COM) 05/02/93
#
# Description:
#   This function dumps arrays in file in a format readable by
#   shell dot (.) command.
#
# Variables:
#   __array_filename
#
# Usage:
#   dumparray pathname [array ...]
#
# Return:
#   Ø         success
#   1         failure
#
# Dependencies:
#
# Bugs:
#     Since this depends on the output of set, and set does not
      preserve
#     quoting, the resultant file may be unusable. Use of
#     storearray/setarray is safer, albeit slower.
#

dumparray () {
    case $# in
        Ø)
            echo >&2 dumparray: error: "$@"
            echo >&2 usage: dumparray pathname '[array ...]'
            exit 1
            ;;
    esac
    __array_filename=$1
    shift
    set |
    /bin/awk '
        /\(\){$/ { exit }            # functions follow parameters
        /^__array_(filename|name|key|keys|cell)=/ { next }
              #temp storage
        /^__array_(size|keys)*_('`echo ${*:-.\*} | /bin/tr"
             " \|`')(_|=)/ {
            if (NF > 1) {
                n = index($Ø, "=");
                print substr($Ø , 1,n)"'\''"substr($Ø,n+1)"'\''";
            }
            else {
                print;
            }
        }
    ' > $__array_filename
}
### << END function: dumparray<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ###

## << END package: arrays<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ##


Previous Table of Contents Next