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