8354 sync regcomp(3C) with upstream (fix make catalog)
[unleashed/tickless.git] / usr / src / lib / libshell / common / scripts / svcproptree1.sh
blob60974a361fc142e4aef5f8926ec526cacdc89768
1 #!/usr/bin/ksh93
4 # CDDL HEADER START
6 # The contents of this file are subject to the terms of the
7 # Common Development and Distribution License (the "License").
8 # You may not use this file except in compliance with the License.
10 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
11 # or http://www.opensolaris.org/os/licensing.
12 # See the License for the specific language governing permissions
13 # and limitations under the License.
15 # When distributing Covered Code, include this CDDL HEADER in each
16 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
17 # If applicable, add the following below this CDDL HEADER, with the
18 # fields enclosed by brackets "[]" replaced with your own identifying
19 # information: Portions Copyright [yyyy] [name of copyright owner]
21 # CDDL HEADER END
25 # Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28 # Solaris needs /usr/xpg6/bin:/usr/xpg4/bin because the tools in /usr/bin are not POSIX-conformant
29 export PATH=/usr/xpg6/bin:/usr/xpg4/bin:/bin:/usr/bin
31 # Make sure all math stuff runs in the "C" locale to avoid problems
32 # with alternative # radix point representations (e.g. ',' instead of
33 # '.' in de_DE.*-locales). This needs to be set _before_ any
34 # floating-point constants are defined in this script).
35 if [[ "${LC_ALL}" != "" ]] ; then
36 export \
37 LC_MONETARY="${LC_ALL}" \
38 LC_MESSAGES="${LC_ALL}" \
39 LC_COLLATE="${LC_ALL}" \
40 LC_CTYPE="${LC_ALL}"
41 unset LC_ALL
43 export LC_NUMERIC=C
45 function fatal_error
47 print -u2 "${progname}: $*"
48 exit 1
52 function svcproptovartree
54 nameref tree=$1
56 typeset name
57 typeset servicename
58 typeset propname
60 typeset datatype
62 typeset -a fields
63 integer num_fields
64 integer i
66 while IFS=' ' read -A fields ; do
67 num_fields=${#fields[*]}
69 name="${fields[0]}"
70 datatype="${fields[1]}"
71 # parse service/property name
72 servicename="${name%~(Er):properties/.*}"
73 servicename="${servicename/~(El)svc:\//}" # strip "svc:/"
74 propname="${name#~(El).*:properties/}"
76 [[ "${ typeset +p "tree[${servicename}].properties" ; }" == "" ]] && compound -A tree[${servicename}].properties
78 nameref node=tree[${servicename}].properties[${propname}]
80 node=(
81 typeset datatype="${datatype}"
82 typeset valuelist="true"
83 typeset -a values
86 for (( i=2 ; i < num_fields ; i++ )) ; do
87 node.values+=( "${fields[i]}" )
88 done
89 done
91 return 0
94 function usage
96 OPTIND=0
97 getopts -a "${progname}" "${svcproptree1_usage}" OPT '-?'
98 exit 2
101 # program start
102 builtin basename
103 builtin cat
104 builtin date
105 builtin uname
107 typeset progname="${ basename "${0}" ; }"
109 typeset -r svcproptree1_usage=$'+
110 [-?\n@(#)\$Id: svcproptree1 (Roland Mainz) 2010-04-02 \$\n]
111 [-author?Roland Mainz <roland.mainz@nrubsig.org>]
112 [+NAME?svcproptree1 - SMF tree demo]
113 [+DESCRIPTION?\bsvcproptree1\b is a small ksh93 compound variable demo
114 which reads accepts a SMF service pattern name input file,
115 reads the matching service properties and converts them into an internal
116 variable tree representation and outputs it in the format
117 specified by viewmode (either "list", "namelist", "tree" or "compacttree")..]
119 pattern viewmode
121 [+SEE ALSO?\bksh93\b(1), \bsvcprop\b(1)]
124 while getopts -a "${progname}" "${svcproptree1_usage}" OPT ; do
125 # printmsg "## OPT=|${OPT}|, OPTARG=|${OPTARG}|"
126 case ${OPT} in
127 *) usage ;;
128 esac
129 done
130 shift $((OPTIND-1))
132 typeset svcpattern="$1"
133 typeset viewmode="$2"
135 if [[ "${viewmode}" != ~(Elr)(list|namelist|tree|compacttree) ]] ; then
136 fatal_error $"Invalid view mode \"${viewmode}\"."
139 compound svc=(
140 typeset -A proptree
143 typeset s
145 s="$(/usr/bin/svcprop -f "${svcpattern}")" || fatal_error $"svcprop failed with exit code $?."
146 print -u2 $"#loading completed."
148 print -r -- "$s" | svcproptovartree svc.proptree
149 print -u2 $"#parsing completed."
151 case "${viewmode}" in
152 list)
153 set | egrep "^svc.proptree\[" | fgrep -v ']=$'
155 namelist)
156 typeset + | egrep "^svc.proptree\["
158 tree)
159 print -v svc
161 compacttree)
162 print -C svc
165 fatal_error $"Invalid view mode \"${viewmode}\"."
167 esac
169 print -u2 $"#done."
171 exit 0
172 # EOF.