1*10898Sroland.mainz@nrubsig.org#!/usr/bin/ksh93
2*10898Sroland.mainz@nrubsig.org
3*10898Sroland.mainz@nrubsig.org#
4*10898Sroland.mainz@nrubsig.org# CDDL HEADER START
5*10898Sroland.mainz@nrubsig.org#
6*10898Sroland.mainz@nrubsig.org# The contents of this file are subject to the terms of the
7*10898Sroland.mainz@nrubsig.org# Common Development and Distribution License (the "License").
8*10898Sroland.mainz@nrubsig.org# You may not use this file except in compliance with the License.
9*10898Sroland.mainz@nrubsig.org#
10*10898Sroland.mainz@nrubsig.org# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
11*10898Sroland.mainz@nrubsig.org# or http://www.opensolaris.org/os/licensing.
12*10898Sroland.mainz@nrubsig.org# See the License for the specific language governing permissions
13*10898Sroland.mainz@nrubsig.org# and limitations under the License.
14*10898Sroland.mainz@nrubsig.org#
15*10898Sroland.mainz@nrubsig.org# When distributing Covered Code, include this CDDL HEADER in each
16*10898Sroland.mainz@nrubsig.org# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
17*10898Sroland.mainz@nrubsig.org# If applicable, add the following below this CDDL HEADER, with the
18*10898Sroland.mainz@nrubsig.org# fields enclosed by brackets "[]" replaced with your own identifying
19*10898Sroland.mainz@nrubsig.org# information: Portions Copyright [yyyy] [name of copyright owner]
20*10898Sroland.mainz@nrubsig.org#
21*10898Sroland.mainz@nrubsig.org# CDDL HEADER END
22*10898Sroland.mainz@nrubsig.org#
23*10898Sroland.mainz@nrubsig.org
24*10898Sroland.mainz@nrubsig.org#
25*10898Sroland.mainz@nrubsig.org# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
26*10898Sroland.mainz@nrubsig.org# Use is subject to license terms.
27*10898Sroland.mainz@nrubsig.org#
28*10898Sroland.mainz@nrubsig.org
29*10898Sroland.mainz@nrubsig.org#
30*10898Sroland.mainz@nrubsig.org# cpvprint - compound variable pretty printer
31*10898Sroland.mainz@nrubsig.org#
32*10898Sroland.mainz@nrubsig.org
33*10898Sroland.mainz@nrubsig.org# Solaris needs /usr/xpg6/bin:/usr/xpg4/bin because the tools in /usr/bin are not POSIX-conformant
34*10898Sroland.mainz@nrubsig.orgexport PATH=/usr/xpg6/bin:/usr/xpg4/bin:/bin:/usr/bin
35*10898Sroland.mainz@nrubsig.org
36*10898Sroland.mainz@nrubsig.org# Make sure all math stuff runs in the "C" locale to avoid problems
37*10898Sroland.mainz@nrubsig.org# with alternative # radix point representations (e.g. ',' instead of
38*10898Sroland.mainz@nrubsig.org# '.' in de_DE.*-locales). This needs to be set _before_ any
39*10898Sroland.mainz@nrubsig.org# floating-point constants are defined in this script).
40*10898Sroland.mainz@nrubsig.orgif [[ "${LC_ALL}" != "" ]] ; then
41*10898Sroland.mainz@nrubsig.org    export \
42*10898Sroland.mainz@nrubsig.org        LC_MONETARY="${LC_ALL}" \
43*10898Sroland.mainz@nrubsig.org        LC_MESSAGES="${LC_ALL}" \
44*10898Sroland.mainz@nrubsig.org        LC_COLLATE="${LC_ALL}" \
45*10898Sroland.mainz@nrubsig.org        LC_CTYPE="${LC_ALL}"
46*10898Sroland.mainz@nrubsig.org        unset LC_ALL
47*10898Sroland.mainz@nrubsig.orgfi
48*10898Sroland.mainz@nrubsig.orgexport LC_NUMERIC=C
49*10898Sroland.mainz@nrubsig.org
50*10898Sroland.mainz@nrubsig.orgfunction fatal_error
51*10898Sroland.mainz@nrubsig.org{
52*10898Sroland.mainz@nrubsig.org	print -u2 "${progname}: $*"
53*10898Sroland.mainz@nrubsig.org	exit 1
54*10898Sroland.mainz@nrubsig.org}
55*10898Sroland.mainz@nrubsig.org
56*10898Sroland.mainz@nrubsig.orgfunction prettyprint_compoundvar
57*10898Sroland.mainz@nrubsig.org{
58*10898Sroland.mainz@nrubsig.org	nameref var=$1
59*10898Sroland.mainz@nrubsig.org
60*10898Sroland.mainz@nrubsig.org	# print tree
61*10898Sroland.mainz@nrubsig.org	str="${ print -v var ; }"
62*10898Sroland.mainz@nrubsig.org	# do some "pretty-printing" for human users (the output is still a
63*10898Sroland.mainz@nrubsig.org	# valid compound variable value)
64*10898Sroland.mainz@nrubsig.org	# (note: This does not scale well with large files)
65*10898Sroland.mainz@nrubsig.org	str="${str//$'\t'typeset -l -E /$'\t'float }"
66*10898Sroland.mainz@nrubsig.org	str="${str//$'\t'typeset -l -i /$'\t'integer }"
67*10898Sroland.mainz@nrubsig.org	str="${str//$'\t'typeset -C /$'\t'compound }"
68*10898Sroland.mainz@nrubsig.org	print -r -- "${str}"
69*10898Sroland.mainz@nrubsig.org
70*10898Sroland.mainz@nrubsig.org	return 0
71*10898Sroland.mainz@nrubsig.org}
72*10898Sroland.mainz@nrubsig.org
73*10898Sroland.mainz@nrubsig.orgfunction usage
74*10898Sroland.mainz@nrubsig.org{
75*10898Sroland.mainz@nrubsig.org	OPTIND=0
76*10898Sroland.mainz@nrubsig.org	getopts -a "${progname}" "${cpvprint_usage}" OPT '-?'
77*10898Sroland.mainz@nrubsig.org	exit 2
78*10898Sroland.mainz@nrubsig.org}
79*10898Sroland.mainz@nrubsig.org
80*10898Sroland.mainz@nrubsig.org# HTML constants
81*10898Sroland.mainz@nrubsig.orgcompound -r hc=(
82*10898Sroland.mainz@nrubsig.org	compound -r doctype=(
83*10898Sroland.mainz@nrubsig.org		compound -r xhtml=(
84*10898Sroland.mainz@nrubsig.org			typeset -r transitional=$'<!DOCTYPE html\n\tPUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"\n\t"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n'
85*10898Sroland.mainz@nrubsig.org		)
86*10898Sroland.mainz@nrubsig.org	)
87*10898Sroland.mainz@nrubsig.org	compound -r namespace=(
88*10898Sroland.mainz@nrubsig.org		typeset -r xhtml=$'http://www.w3.org/1999/xhtml'
89*10898Sroland.mainz@nrubsig.org	)
90*10898Sroland.mainz@nrubsig.org	typeset -r xml_head=$'<?xml version="1.0" encoding="UTF-8"?>\n'
91*10898Sroland.mainz@nrubsig.org)
92*10898Sroland.mainz@nrubsig.org
93*10898Sroland.mainz@nrubsig.org# main
94*10898Sroland.mainz@nrubsig.orgbuiltin basename
95*10898Sroland.mainz@nrubsig.org
96*10898Sroland.mainz@nrubsig.orgset -o noglob
97*10898Sroland.mainz@nrubsig.orgset -o errexit
98*10898Sroland.mainz@nrubsig.orgset -o nounset
99*10898Sroland.mainz@nrubsig.org
100*10898Sroland.mainz@nrubsig.org# tree variable
101*10898Sroland.mainz@nrubsig.orgcompound tree
102*10898Sroland.mainz@nrubsig.org
103*10898Sroland.mainz@nrubsig.orgtypeset progname="${ basename "${0}" ; }"
104*10898Sroland.mainz@nrubsig.org
105*10898Sroland.mainz@nrubsig.orgtypeset -r cpvprint_usage=$'+
106*10898Sroland.mainz@nrubsig.org[-?\n@(#)\$Id: cpvprint (Roland Mainz) 2009-06-15 \$\n]
107*10898Sroland.mainz@nrubsig.org[-author?Roland Mainz <roland.mainz@nrubsig.org>]
108*10898Sroland.mainz@nrubsig.org[+NAME?cpvprint - render compound variable trees in various formats]
109*10898Sroland.mainz@nrubsig.org[+DESCRIPTION?\bcpvprint\b is converter which reads a ksh compound
110*10898Sroland.mainz@nrubsig.org	variable and prints it on a different format. Supported
111*10898Sroland.mainz@nrubsig.org	formats are \'default\', \'altdefault\',
112*10898Sroland.mainz@nrubsig.org	\'tree\', \'alttree\',
113*10898Sroland.mainz@nrubsig.org	\'pretty\', \'pretty.html\', \'list\' and \'fulllist\']
114*10898Sroland.mainz@nrubsig.org
115*10898Sroland.mainz@nrubsig.orgformat [ arguments ]
116*10898Sroland.mainz@nrubsig.org
117*10898Sroland.mainz@nrubsig.org[+SEE ALSO?\bksh93\b(1), \bcpvlint\b(1)]
118*10898Sroland.mainz@nrubsig.org'
119*10898Sroland.mainz@nrubsig.org
120*10898Sroland.mainz@nrubsig.orgwhile getopts -a "${progname}" "${cpvprint_usage}" OPT ; do
121*10898Sroland.mainz@nrubsig.org#	printmsg "## OPT=|${OPT}|, OPTARG=|${OPTARG}|"
122*10898Sroland.mainz@nrubsig.org	case ${OPT} in
123*10898Sroland.mainz@nrubsig.org		*) usage ;;
124*10898Sroland.mainz@nrubsig.org	esac
125*10898Sroland.mainz@nrubsig.orgdone
126*10898Sroland.mainz@nrubsig.orgshift $((OPTIND-1))
127*10898Sroland.mainz@nrubsig.org
128*10898Sroland.mainz@nrubsig.org# prechecks
129*10898Sroland.mainz@nrubsig.org(( $# > 0 )) || usage
130*10898Sroland.mainz@nrubsig.org
131*10898Sroland.mainz@nrubsig.orgprintformat="$1"
132*10898Sroland.mainz@nrubsig.orgshift
133*10898Sroland.mainz@nrubsig.org
134*10898Sroland.mainz@nrubsig.org# read variable
135*10898Sroland.mainz@nrubsig.orgcase $# in
136*10898Sroland.mainz@nrubsig.org	0)
137*10898Sroland.mainz@nrubsig.org		read -C tree || fatal_error $"Read error."
138*10898Sroland.mainz@nrubsig.org		;;
139*10898Sroland.mainz@nrubsig.org	1)
140*10898Sroland.mainz@nrubsig.org		integer fd
141*10898Sroland.mainz@nrubsig.org
142*10898Sroland.mainz@nrubsig.org		redirect {fd}<> "$1" || fatal_error $"Cannot open file."
143*10898Sroland.mainz@nrubsig.org		read -u${fd} -C tree || fatal_error $"Read error."
144*10898Sroland.mainz@nrubsig.org		redirect {fd}<&- || fatal_error $"Close error."
145*10898Sroland.mainz@nrubsig.org		;;
146*10898Sroland.mainz@nrubsig.org	2)
147*10898Sroland.mainz@nrubsig.org		print -u2 -f $"%s: Unsupported number of arguments.\n" "$0"
148*10898Sroland.mainz@nrubsig.org		exit 1
149*10898Sroland.mainz@nrubsig.org		;;
150*10898Sroland.mainz@nrubsig.orgesac
151*10898Sroland.mainz@nrubsig.org
152*10898Sroland.mainz@nrubsig.org# print variable
153*10898Sroland.mainz@nrubsig.orgcase ${printformat} in
154*10898Sroland.mainz@nrubsig.org	'default' | 'tree')
155*10898Sroland.mainz@nrubsig.org		print -v tree
156*10898Sroland.mainz@nrubsig.org		;;
157*10898Sroland.mainz@nrubsig.org	'altdefault' | 'alttree')
158*10898Sroland.mainz@nrubsig.org		print -C tree
159*10898Sroland.mainz@nrubsig.org		;;
160*10898Sroland.mainz@nrubsig.org	'pretty')
161*10898Sroland.mainz@nrubsig.org		# print variable tree (same as $ print -v filetree # except that it "looks better")
162*10898Sroland.mainz@nrubsig.org		prettyprint_compoundvar tree
163*10898Sroland.mainz@nrubsig.org		;;
164*10898Sroland.mainz@nrubsig.org	'pretty.html')
165*10898Sroland.mainz@nrubsig.org		printf '%s%s<html xmlns="%s" xml:lang="en" lang="en">\n<head><meta name="generator" content="%H" /><title>%H</title></head>\n<body><pre>%H\n</pre></body></html>\n' \
166*10898Sroland.mainz@nrubsig.org			"${hc.xml_head}" \
167*10898Sroland.mainz@nrubsig.org			"${hc.doctype.xhtml.transitional}" \
168*10898Sroland.mainz@nrubsig.org			"${hc.namespace.xhtml}" \
169*10898Sroland.mainz@nrubsig.org			"ksh Compound Variable Pretty Printer (cpvprint)" \
170*10898Sroland.mainz@nrubsig.org			"" \
171*10898Sroland.mainz@nrubsig.org			"$(prettyprint_compoundvar tree)" | iconv -f "UTF-8" - -
172*10898Sroland.mainz@nrubsig.org		;;
173*10898Sroland.mainz@nrubsig.org	'list')
174*10898Sroland.mainz@nrubsig.org		set | egrep '^tree.' | sed 's/^tree\.//' | egrep -v '^[[:alnum:]]+(\.([[:alnum:]\.]+)(\[.*\])*)*=\('
175*10898Sroland.mainz@nrubsig.org		;;
176*10898Sroland.mainz@nrubsig.org	'fulllist')
177*10898Sroland.mainz@nrubsig.org		set | egrep "^tree."
178*10898Sroland.mainz@nrubsig.org		;;
179*10898Sroland.mainz@nrubsig.org	*)
180*10898Sroland.mainz@nrubsig.org		fatal_error $"Unsupported format."
181*10898Sroland.mainz@nrubsig.org		;;
182*10898Sroland.mainz@nrubsig.orgesac
183*10898Sroland.mainz@nrubsig.org
184*10898Sroland.mainz@nrubsig.orgexit 0
185*10898Sroland.mainz@nrubsig.org# EOF.
186