xref: /titanic_52/usr/src/cmd/ast/libshell/common/scripts/cpvprint.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) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
26*906afcb8SAndy Fiddaman#
27*906afcb8SAndy Fiddaman
28*906afcb8SAndy Fiddaman#
29*906afcb8SAndy Fiddaman# cpvprint - compound variable pretty printer
30*906afcb8SAndy Fiddaman#
31*906afcb8SAndy Fiddaman
32*906afcb8SAndy Fiddaman# Solaris needs /usr/xpg6/bin:/usr/xpg4/bin because the tools in /usr/bin are not POSIX-conformant
33*906afcb8SAndy Fiddamanexport PATH=/usr/xpg6/bin:/usr/xpg4/bin:/bin:/usr/bin
34*906afcb8SAndy Fiddaman
35*906afcb8SAndy Fiddaman# Make sure all math stuff runs in the "C" locale to avoid problems
36*906afcb8SAndy Fiddaman# with alternative # radix point representations (e.g. ',' instead of
37*906afcb8SAndy Fiddaman# '.' in de_DE.*-locales). This needs to be set _before_ any
38*906afcb8SAndy Fiddaman# floating-point constants are defined in this script).
39*906afcb8SAndy Fiddamanif [[ "${LC_ALL}" != "" ]] ; then
40*906afcb8SAndy Fiddaman    export \
41*906afcb8SAndy Fiddaman        LC_MONETARY="${LC_ALL}" \
42*906afcb8SAndy Fiddaman        LC_MESSAGES="${LC_ALL}" \
43*906afcb8SAndy Fiddaman        LC_COLLATE="${LC_ALL}" \
44*906afcb8SAndy Fiddaman        LC_CTYPE="${LC_ALL}"
45*906afcb8SAndy Fiddaman        unset LC_ALL
46*906afcb8SAndy Fiddamanfi
47*906afcb8SAndy Fiddamanexport LC_NUMERIC=C
48*906afcb8SAndy Fiddaman
49*906afcb8SAndy Fiddamanfunction fatal_error
50*906afcb8SAndy Fiddaman{
51*906afcb8SAndy Fiddaman	print -u2 "${progname}: $*"
52*906afcb8SAndy Fiddaman	exit 1
53*906afcb8SAndy Fiddaman}
54*906afcb8SAndy Fiddaman
55*906afcb8SAndy Fiddamanfunction prettyprint_compoundvar
56*906afcb8SAndy Fiddaman{
57*906afcb8SAndy Fiddaman	nameref var=$1
58*906afcb8SAndy Fiddaman
59*906afcb8SAndy Fiddaman	# print tree
60*906afcb8SAndy Fiddaman	str="${ print -v var ; }"
61*906afcb8SAndy Fiddaman	# do some "pretty-printing" for human users (the output is still a
62*906afcb8SAndy Fiddaman	# valid compound variable value)
63*906afcb8SAndy Fiddaman	# (note: This does not scale well with large files)
64*906afcb8SAndy Fiddaman	str="${str//$'\t'typeset -l -E /$'\t'float }"
65*906afcb8SAndy Fiddaman	str="${str//$'\t'typeset -l -i /$'\t'integer }"
66*906afcb8SAndy Fiddaman	str="${str//$'\t'typeset -C /$'\t'compound }"
67*906afcb8SAndy Fiddaman	print -r -- "${str}"
68*906afcb8SAndy Fiddaman
69*906afcb8SAndy Fiddaman	return 0
70*906afcb8SAndy Fiddaman}
71*906afcb8SAndy Fiddaman
72*906afcb8SAndy Fiddamanfunction usage
73*906afcb8SAndy Fiddaman{
74*906afcb8SAndy Fiddaman	OPTIND=0
75*906afcb8SAndy Fiddaman	getopts -a "${progname}" "${cpvprint_usage}" OPT '-?'
76*906afcb8SAndy Fiddaman	exit 2
77*906afcb8SAndy Fiddaman}
78*906afcb8SAndy Fiddaman
79*906afcb8SAndy Fiddaman# HTML constants
80*906afcb8SAndy Fiddamancompound -r hc=(
81*906afcb8SAndy Fiddaman	compound -r doctype=(
82*906afcb8SAndy Fiddaman		compound -r xhtml=(
83*906afcb8SAndy Fiddaman			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'
84*906afcb8SAndy Fiddaman		)
85*906afcb8SAndy Fiddaman	)
86*906afcb8SAndy Fiddaman	compound -r namespace=(
87*906afcb8SAndy Fiddaman		typeset -r xhtml=$'http://www.w3.org/1999/xhtml'
88*906afcb8SAndy Fiddaman	)
89*906afcb8SAndy Fiddaman	typeset -r xml_head=$'<?xml version="1.0" encoding="UTF-8"?>\n'
90*906afcb8SAndy Fiddaman)
91*906afcb8SAndy Fiddaman
92*906afcb8SAndy Fiddaman# main
93*906afcb8SAndy Fiddamanbuiltin basename
94*906afcb8SAndy Fiddaman
95*906afcb8SAndy Fiddamanset -o noglob
96*906afcb8SAndy Fiddamanset -o errexit
97*906afcb8SAndy Fiddamanset -o nounset
98*906afcb8SAndy Fiddaman
99*906afcb8SAndy Fiddaman# tree variable
100*906afcb8SAndy Fiddamancompound tree
101*906afcb8SAndy Fiddaman
102*906afcb8SAndy Fiddamantypeset progname="${ basename "${0}" ; }"
103*906afcb8SAndy Fiddaman
104*906afcb8SAndy Fiddamantypeset -r cpvprint_usage=$'+
105*906afcb8SAndy Fiddaman[-?\n@(#)\$Id: cpvprint (Roland Mainz) 2009-06-15 \$\n]
106*906afcb8SAndy Fiddaman[-author?Roland Mainz <roland.mainz@nrubsig.org>]
107*906afcb8SAndy Fiddaman[+NAME?cpvprint - render compound variable trees in various formats]
108*906afcb8SAndy Fiddaman[+DESCRIPTION?\bcpvprint\b is converter which reads a ksh compound
109*906afcb8SAndy Fiddaman	variable and prints it on a different format. Supported
110*906afcb8SAndy Fiddaman	formats are \'default\', \'altdefault\',
111*906afcb8SAndy Fiddaman	\'tree\', \'alttree\',
112*906afcb8SAndy Fiddaman	\'pretty\', \'pretty.html\', \'list\' and \'fulllist\']
113*906afcb8SAndy Fiddaman
114*906afcb8SAndy Fiddamanformat [ arguments ]
115*906afcb8SAndy Fiddaman
116*906afcb8SAndy Fiddaman[+SEE ALSO?\bksh93\b(1), \bcpvlint\b(1)]
117*906afcb8SAndy Fiddaman'
118*906afcb8SAndy Fiddaman
119*906afcb8SAndy Fiddamanwhile getopts -a "${progname}" "${cpvprint_usage}" OPT ; do
120*906afcb8SAndy Fiddaman#	printmsg "## OPT=|${OPT}|, OPTARG=|${OPTARG}|"
121*906afcb8SAndy Fiddaman	case ${OPT} in
122*906afcb8SAndy Fiddaman		*) usage ;;
123*906afcb8SAndy Fiddaman	esac
124*906afcb8SAndy Fiddamandone
125*906afcb8SAndy Fiddamanshift $((OPTIND-1))
126*906afcb8SAndy Fiddaman
127*906afcb8SAndy Fiddaman# prechecks
128*906afcb8SAndy Fiddaman(( $# > 0 )) || usage
129*906afcb8SAndy Fiddaman
130*906afcb8SAndy Fiddamanprintformat="$1"
131*906afcb8SAndy Fiddamanshift
132*906afcb8SAndy Fiddaman
133*906afcb8SAndy Fiddaman# read variable
134*906afcb8SAndy Fiddamancase $# in
135*906afcb8SAndy Fiddaman	0)
136*906afcb8SAndy Fiddaman		read -C tree || fatal_error $"Read error."
137*906afcb8SAndy Fiddaman		;;
138*906afcb8SAndy Fiddaman	1)
139*906afcb8SAndy Fiddaman		integer fd
140*906afcb8SAndy Fiddaman
141*906afcb8SAndy Fiddaman		redirect {fd}<> "$1" || fatal_error $"Cannot open file."
142*906afcb8SAndy Fiddaman		read -u${fd} -C tree || fatal_error $"Read error."
143*906afcb8SAndy Fiddaman		redirect {fd}<&- || fatal_error $"Close error."
144*906afcb8SAndy Fiddaman		;;
145*906afcb8SAndy Fiddaman	2)
146*906afcb8SAndy Fiddaman		print -u2 -f $"%s: Unsupported number of arguments.\n" "$0"
147*906afcb8SAndy Fiddaman		exit 1
148*906afcb8SAndy Fiddaman		;;
149*906afcb8SAndy Fiddamanesac
150*906afcb8SAndy Fiddaman
151*906afcb8SAndy Fiddaman# print variable
152*906afcb8SAndy Fiddamancase ${printformat} in
153*906afcb8SAndy Fiddaman	'default' | 'tree')
154*906afcb8SAndy Fiddaman		print -v tree
155*906afcb8SAndy Fiddaman		;;
156*906afcb8SAndy Fiddaman	'altdefault' | 'alttree')
157*906afcb8SAndy Fiddaman		print -C tree
158*906afcb8SAndy Fiddaman		;;
159*906afcb8SAndy Fiddaman	'pretty')
160*906afcb8SAndy Fiddaman		# print variable tree (same as $ print -v filetree # except that it "looks better")
161*906afcb8SAndy Fiddaman		prettyprint_compoundvar tree
162*906afcb8SAndy Fiddaman		;;
163*906afcb8SAndy Fiddaman	'pretty.html')
164*906afcb8SAndy Fiddaman		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' \
165*906afcb8SAndy Fiddaman			"${hc.xml_head}" \
166*906afcb8SAndy Fiddaman			"${hc.doctype.xhtml.transitional}" \
167*906afcb8SAndy Fiddaman			"${hc.namespace.xhtml}" \
168*906afcb8SAndy Fiddaman			"ksh Compound Variable Pretty Printer (cpvprint)" \
169*906afcb8SAndy Fiddaman			"" \
170*906afcb8SAndy Fiddaman			"$(prettyprint_compoundvar tree)" | iconv -f "UTF-8" - -
171*906afcb8SAndy Fiddaman		;;
172*906afcb8SAndy Fiddaman	'list')
173*906afcb8SAndy Fiddaman		set | egrep '^tree.' | sed 's/^tree\.//' | egrep -v '^[[:alnum:]]+(\.([[:alnum:]\.]+)(\[.*\])*)*=\('
174*906afcb8SAndy Fiddaman		;;
175*906afcb8SAndy Fiddaman	'fulllist')
176*906afcb8SAndy Fiddaman		set | egrep "^tree."
177*906afcb8SAndy Fiddaman		;;
178*906afcb8SAndy Fiddaman	*)
179*906afcb8SAndy Fiddaman		fatal_error $"Unsupported format."
180*906afcb8SAndy Fiddaman		;;
181*906afcb8SAndy Fiddamanesac
182*906afcb8SAndy Fiddaman
183*906afcb8SAndy Fiddamanexit 0
184*906afcb8SAndy Fiddaman# EOF.
185