Hi!

----

I've attached a small toy/demo script called "svcproptree1.ksh.txt"
which loads the SMF properly tree into a shell compound variable (a
compound variable is a single variable which contains several other
variables (the closest equivalent may be |struct| in C/C++, however
compound variables go a bit further than that, e.g. some people have
interpreted the compound variables as "tree variable")), e.g. $ ksh93
svcproptree1.ksh '*' tree # will result in the tree shown in
http://opensolaris.pastebin.ca/raw/705213

Would such a thing somehow usefull for the SMF project, for example
build a loadable shell function which loads the properties for a
specific service as part of the method scripts instead of letting all
scripts do "manual" parsing of the SMF properties (e.g. $ ksh93
svcproptree1.ksh 'finger' list # loads all properties for the "finger"
servce into a "tree" variable and the script could then access the
properly "tm_man_in_fingerd/manpath" via
manpath="${svc.proptree[network/finger:default/].properties[tm_man_in_fingerd/manpath].value}"
(the actual variable path could be shortend via "nameref" usage)) ?

----

Bye,
Roland

-- 
  __ .  . __
 (o.\ \/ /.o) roland.mainz at nrubsig.org
  \__\/\/__/  MPEG specialist, C&&JAVA&&Sun&&Unix programmer
  /O /==\ O\  TEL +49 641 7950090
 (;O/ \/ \O;)
-------------- next part --------------
#!/bin/ksh93

#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#

#
# Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# ident "%Z%%M% %I%     %E% SMI"
#

# Solaris needs /usr/xpg4/bin/ because the tools in /usr/bin are not 
POSIX-conformant
export PATH=/usr/xpg4/bin:/bin:/usr/bin

function fatal_error
{
    print -u 2 "${progname}: $@"
    exit 1
}


function svcproptovartree
{
#set -o xtrace
    nameref tree=$1

    typeset name
    typeset servicename
    typeset propname

    typeset datatype
    typeset value
       
    while read name datatype value ; do
        # parse service/property name
        servicename="${name%~(Er):properties/.*}"
        servicename="${servicename/~(El)svc:\//}" # strip "svc:/"
        propname="${name#~(El).*:properties/}"

        if [[ "${tree["${servicename}"].properties}" = "" ]] ; then
            typeset -A tree["${servicename}"].properties=( )
        fi

        tree["${servicename}"].properties["${propname}"]=(
            typeset datatype="${datatype}"
            typeset value="${value}"
        )
    done
   
    return 0
}

function usage
{
    OPTIND=0
    getopts -a "${progname}" "${USAGE}" OPT '-?'
    exit 2
}

# program start
builtin basename
builtin cat
builtin date
builtin uname

typeset progname="$(basename "${0}")"

USAGE=$'
[-?\n@(#)\$Id: svcproptree1 (Roland Mainz) 2007-09-15 \$\n]
[-author?Roland Mainz <roland.mainz at nrubsig.org]
[+NAME?svcproptree1 - XML tree demo]
[+DESCRIPTION?\bxmldocumenttree\b is a small ksh93 compound variable demo
        which reads a XML input file, converts it into an internal
        variable tree representation and outputs it in the format
        specified by viewmode (either "list", "namelist" or "tree")..]

pattern viewmode

[+SEE ALSO?\bksh93\b(1), svcprop\b(1)\b]
'

while getopts -a "${progname}" "${USAGE}" OPT ; do 
#    printmsg "## OPT=|${OPT}|, OPTARG=|${OPTARG}|"
    case ${OPT} in
        *)    usage ;;
    esac
done
shift $((OPTIND-1))

typeset svcpattern="$1"
typeset viewmode="$2"

if [[ "${viewmode}" != ~(Elr)(list|namelist|tree) ]] ; then
    fatal_error $"Invalid view mode \"${viewmode}\"."
fi

typeset svc=(
    typeset -A proptree
)

typeset s

s="$(/usr/bin/svcprop -f "${svcpattern}")" || fatal_error "svcprop failed with 
exit code $?."
print -u2 "#loading completed."

print -- "$s" | svcproptovartree svc.proptree
print -u2 "#parsing completed."

case "${viewmode}" in
    list)
        set | egrep "^svc.proptree\[" | fgrep -v ']=$'
        ;;
    namelist)
        typeset + | egrep "^svc.proptree\["
        ;;
    tree)
        print -- "${svc}"
        ;;
       *)
        fatal_error $"Invalid view mode \"${viewmode}\"."
        ;;
esac

print -u2 "#done."

exit 0
# EOF.

Reply via email to