xref: /titanic_50/usr/src/lib/libshell/common/scripts/svcproptree1.sh (revision 9d25110c26dac1ee74833ce1708685385a732e52)
1#!/usr/bin/ksh93
2
3#
4# CDDL HEADER START
5#
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.
9#
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.
14#
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]
20#
21# CDDL HEADER END
22#
23
24#
25# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
26# Use is subject to license terms.
27#
28
29# Solaris needs /usr/xpg6/bin:/usr/xpg4/bin because the tools in /usr/bin are not POSIX-conformant
30export PATH=/usr/xpg6/bin:/usr/xpg4/bin:/bin:/usr/bin
31
32# Make sure all math stuff runs in the "C" locale to avoid problems
33# with alternative # radix point representations (e.g. ',' instead of
34# '.' in de_DE.*-locales). This needs to be set _before_ any
35# floating-point constants are defined in this script).
36if [[ "${LC_ALL}" != "" ]] ; then
37    export \
38        LC_MONETARY="${LC_ALL}" \
39        LC_MESSAGES="${LC_ALL}" \
40        LC_COLLATE="${LC_ALL}" \
41        LC_CTYPE="${LC_ALL}"
42        unset LC_ALL
43fi
44export LC_NUMERIC=C
45
46function fatal_error
47{
48	print -u2 "${progname}: $*"
49	exit 1
50}
51
52
53function svcproptovartree
54{
55	nameref tree=$1
56
57	typeset name
58	typeset servicename
59	typeset propname
60
61	typeset datatype
62
63	typeset -a fields
64	integer num_fields
65	integer i
66
67	while IFS=' ' read -A fields ; do
68		num_fields=${#fields[*]}
69
70		name="${fields[0]}"
71		datatype="${fields[1]}"
72		# parse service/property name
73		servicename="${name%~(Er):properties/.*}"
74		servicename="${servicename/~(El)svc:\//}" # strip "svc:/"
75		propname="${name#~(El).*:properties/}"
76
77		if [[ "${tree["${servicename}"].properties[*]}" == "" ]] ; then
78			typeset -A tree["${servicename}"].properties=( )
79		fi
80
81		nameref node=tree["${servicename}"].properties["${propname}"]
82
83		node=(
84			typeset datatype="${datatype}"
85			typeset valuelist="true"
86			typeset -a values
87		)
88
89		for (( i=2 ; i < num_fields ; i++ )) ; do
90			node.values+=( "${fields[i]}" )
91		done
92	done
93
94	return 0
95}
96
97function usage
98{
99	OPTIND=0
100	getopts -a "${progname}" "${svcproptree1_usage}" OPT '-?'
101	exit 2
102}
103
104# program start
105builtin basename
106builtin cat
107builtin date
108builtin uname
109
110typeset progname="${ basename "${0}" ; }"
111
112typeset -r svcproptree1_usage=$'+
113[-?\n@(#)\$Id: svcproptree1 (Roland Mainz) 2008-10-14 \$\n]
114[-author?Roland Mainz <roland.mainz@nrubsig.org>]
115[+NAME?svcproptree1 - SMF tree demo]
116[+DESCRIPTION?\bsvcproptree1\b is a small ksh93 compound variable demo
117	which reads accepts a SMF service pattern name input file,
118	reads the matching service properties and converts them into an internal
119	variable tree representation and outputs it in the format
120	specified by viewmode (either "list", "namelist" or "tree")..]
121
122pattern viewmode
123
124[+SEE ALSO?\bksh93\b(1), \bsvcprop\b(1)]
125'
126
127while getopts -a "${progname}" "${svcproptree1_usage}" OPT ; do
128#	printmsg "## OPT=|${OPT}|, OPTARG=|${OPTARG}|"
129	case ${OPT} in
130		*)	usage ;;
131	esac
132done
133shift $((OPTIND-1))
134
135typeset svcpattern="$1"
136typeset viewmode="$2"
137
138if [[ "${viewmode}" != ~(Elr)(list|namelist|tree) ]] ; then
139	fatal_error $"Invalid view mode \"${viewmode}\"."
140fi
141
142typeset svc=(
143	typeset -A proptree
144)
145
146typeset s
147
148s="$(/usr/bin/svcprop -f "${svcpattern}")" || fatal_error $"svcprop failed with exit code $?."
149print -u2 $"#loading completed."
150
151print -r -- "$s" | svcproptovartree svc.proptree
152print -u2 $"#parsing completed."
153
154case "${viewmode}" in
155	list)
156		set | egrep "^svc.proptree\[" | fgrep -v ']=$'
157		;;
158	namelist)
159		typeset + | egrep "^svc.proptree\["
160		;;
161	tree)
162		printf "%B\n" svc
163		;;
164	*)
165		fatal_error $"Invalid view mode \"${viewmode}\"."
166		;;
167esac
168
169print -u2 $"#done."
170
171exit 0
172# EOF.
173