xref: /titanic_50/usr/src/lib/libshell/common/scripts/svcproptree1.sh (revision 84ba300aaa958c8e8427c2ec66a932d86bee71c4)
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 (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
26#
27
28# Solaris needs /usr/xpg6/bin:/usr/xpg4/bin because the tools in /usr/bin are not POSIX-conformant
29export PATH=/usr/xpg6/bin:/usr/xpg4/bin:/bin:/usr/bin
30
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).
35if [[ "${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
42fi
43export LC_NUMERIC=C
44
45function fatal_error
46{
47	print -u2 "${progname}: $*"
48	exit 1
49}
50
51
52function svcproptovartree
53{
54	nameref tree=$1
55
56	typeset name
57	typeset servicename
58	typeset propname
59
60	typeset datatype
61
62	typeset -a fields
63	integer num_fields
64	integer i
65
66	while IFS=' ' read -A fields ; do
67		num_fields=${#fields[*]}
68
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/}"
75
76		[[ "${ typeset +p "tree[${servicename}].properties" ; }" == "" ]] && compound -A tree[${servicename}].properties
77
78		nameref node=tree[${servicename}].properties[${propname}]
79
80		node=(
81			typeset datatype="${datatype}"
82			typeset valuelist="true"
83			typeset -a values
84		)
85
86		for (( i=2 ; i < num_fields ; i++ )) ; do
87			node.values+=( "${fields[i]}" )
88		done
89	done
90
91	return 0
92}
93
94function usage
95{
96	OPTIND=0
97	getopts -a "${progname}" "${svcproptree1_usage}" OPT '-?'
98	exit 2
99}
100
101# program start
102builtin basename
103builtin cat
104builtin date
105builtin uname
106
107typeset progname="${ basename "${0}" ; }"
108
109typeset -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")..]
118
119pattern viewmode
120
121[+SEE ALSO?\bksh93\b(1), \bsvcprop\b(1)]
122'
123
124while getopts -a "${progname}" "${svcproptree1_usage}" OPT ; do
125#	printmsg "## OPT=|${OPT}|, OPTARG=|${OPTARG}|"
126	case ${OPT} in
127		*)	usage ;;
128	esac
129done
130shift $((OPTIND-1))
131
132typeset svcpattern="$1"
133typeset viewmode="$2"
134
135if [[ "${viewmode}" != ~(Elr)(list|namelist|tree|compacttree) ]] ; then
136	fatal_error $"Invalid view mode \"${viewmode}\"."
137fi
138
139compound svc=(
140	typeset -A proptree
141)
142
143typeset s
144
145s="$(/usr/bin/svcprop -f "${svcpattern}")" || fatal_error $"svcprop failed with exit code $?."
146print -u2 $"#loading completed."
147
148print -r -- "$s" | svcproptovartree svc.proptree
149print -u2 $"#parsing completed."
150
151case "${viewmode}" in
152	list)
153		set | egrep "^svc.proptree\[" | fgrep -v ']=$'
154		;;
155	namelist)
156		typeset + | egrep "^svc.proptree\["
157		;;
158	tree)
159		print -v svc
160		;;
161	compacttree)
162		print -C svc
163		;;
164	*)
165		fatal_error $"Invalid view mode \"${viewmode}\"."
166		;;
167esac
168
169print -u2 $"#done."
170
171exit 0
172# EOF.
173