xref: /onnv-gate/usr/src/lib/libshell/common/scripts/cpvprint.sh (revision 12068:08a39a083754)
110898Sroland.mainz@nrubsig.org#!/usr/bin/ksh93
210898Sroland.mainz@nrubsig.org
310898Sroland.mainz@nrubsig.org#
410898Sroland.mainz@nrubsig.org# CDDL HEADER START
510898Sroland.mainz@nrubsig.org#
610898Sroland.mainz@nrubsig.org# The contents of this file are subject to the terms of the
710898Sroland.mainz@nrubsig.org# Common Development and Distribution License (the "License").
810898Sroland.mainz@nrubsig.org# You may not use this file except in compliance with the License.
910898Sroland.mainz@nrubsig.org#
1010898Sroland.mainz@nrubsig.org# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
1110898Sroland.mainz@nrubsig.org# or http://www.opensolaris.org/os/licensing.
1210898Sroland.mainz@nrubsig.org# See the License for the specific language governing permissions
1310898Sroland.mainz@nrubsig.org# and limitations under the License.
1410898Sroland.mainz@nrubsig.org#
1510898Sroland.mainz@nrubsig.org# When distributing Covered Code, include this CDDL HEADER in each
1610898Sroland.mainz@nrubsig.org# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1710898Sroland.mainz@nrubsig.org# If applicable, add the following below this CDDL HEADER, with the
1810898Sroland.mainz@nrubsig.org# fields enclosed by brackets "[]" replaced with your own identifying
1910898Sroland.mainz@nrubsig.org# information: Portions Copyright [yyyy] [name of copyright owner]
2010898Sroland.mainz@nrubsig.org#
2110898Sroland.mainz@nrubsig.org# CDDL HEADER END
2210898Sroland.mainz@nrubsig.org#
2310898Sroland.mainz@nrubsig.org
2410898Sroland.mainz@nrubsig.org#
25*12068SRoger.Faulkner@Oracle.COM# Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
2610898Sroland.mainz@nrubsig.org#
2710898Sroland.mainz@nrubsig.org
2810898Sroland.mainz@nrubsig.org#
2910898Sroland.mainz@nrubsig.org# cpvprint - compound variable pretty printer
3010898Sroland.mainz@nrubsig.org#
3110898Sroland.mainz@nrubsig.org
3210898Sroland.mainz@nrubsig.org# Solaris needs /usr/xpg6/bin:/usr/xpg4/bin because the tools in /usr/bin are not POSIX-conformant
3310898Sroland.mainz@nrubsig.orgexport PATH=/usr/xpg6/bin:/usr/xpg4/bin:/bin:/usr/bin
3410898Sroland.mainz@nrubsig.org
3510898Sroland.mainz@nrubsig.org# Make sure all math stuff runs in the "C" locale to avoid problems
3610898Sroland.mainz@nrubsig.org# with alternative # radix point representations (e.g. ',' instead of
3710898Sroland.mainz@nrubsig.org# '.' in de_DE.*-locales). This needs to be set _before_ any
3810898Sroland.mainz@nrubsig.org# floating-point constants are defined in this script).
3910898Sroland.mainz@nrubsig.orgif [[ "${LC_ALL}" != "" ]] ; then
4010898Sroland.mainz@nrubsig.org    export \
4110898Sroland.mainz@nrubsig.org        LC_MONETARY="${LC_ALL}" \
4210898Sroland.mainz@nrubsig.org        LC_MESSAGES="${LC_ALL}" \
4310898Sroland.mainz@nrubsig.org        LC_COLLATE="${LC_ALL}" \
4410898Sroland.mainz@nrubsig.org        LC_CTYPE="${LC_ALL}"
4510898Sroland.mainz@nrubsig.org        unset LC_ALL
4610898Sroland.mainz@nrubsig.orgfi
4710898Sroland.mainz@nrubsig.orgexport LC_NUMERIC=C
4810898Sroland.mainz@nrubsig.org
4910898Sroland.mainz@nrubsig.orgfunction fatal_error
5010898Sroland.mainz@nrubsig.org{
5110898Sroland.mainz@nrubsig.org	print -u2 "${progname}: $*"
5210898Sroland.mainz@nrubsig.org	exit 1
5310898Sroland.mainz@nrubsig.org}
5410898Sroland.mainz@nrubsig.org
5510898Sroland.mainz@nrubsig.orgfunction prettyprint_compoundvar
5610898Sroland.mainz@nrubsig.org{
5710898Sroland.mainz@nrubsig.org	nameref var=$1
5810898Sroland.mainz@nrubsig.org
5910898Sroland.mainz@nrubsig.org	# print tree
6010898Sroland.mainz@nrubsig.org	str="${ print -v var ; }"
6110898Sroland.mainz@nrubsig.org	# do some "pretty-printing" for human users (the output is still a
6210898Sroland.mainz@nrubsig.org	# valid compound variable value)
6310898Sroland.mainz@nrubsig.org	# (note: This does not scale well with large files)
6410898Sroland.mainz@nrubsig.org	str="${str//$'\t'typeset -l -E /$'\t'float }"
6510898Sroland.mainz@nrubsig.org	str="${str//$'\t'typeset -l -i /$'\t'integer }"
6610898Sroland.mainz@nrubsig.org	str="${str//$'\t'typeset -C /$'\t'compound }"
6710898Sroland.mainz@nrubsig.org	print -r -- "${str}"
6810898Sroland.mainz@nrubsig.org
6910898Sroland.mainz@nrubsig.org	return 0
7010898Sroland.mainz@nrubsig.org}
7110898Sroland.mainz@nrubsig.org
7210898Sroland.mainz@nrubsig.orgfunction usage
7310898Sroland.mainz@nrubsig.org{
7410898Sroland.mainz@nrubsig.org	OPTIND=0
7510898Sroland.mainz@nrubsig.org	getopts -a "${progname}" "${cpvprint_usage}" OPT '-?'
7610898Sroland.mainz@nrubsig.org	exit 2
7710898Sroland.mainz@nrubsig.org}
7810898Sroland.mainz@nrubsig.org
7910898Sroland.mainz@nrubsig.org# HTML constants
8010898Sroland.mainz@nrubsig.orgcompound -r hc=(
8110898Sroland.mainz@nrubsig.org	compound -r doctype=(
8210898Sroland.mainz@nrubsig.org		compound -r xhtml=(
8310898Sroland.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'
8410898Sroland.mainz@nrubsig.org		)
8510898Sroland.mainz@nrubsig.org	)
8610898Sroland.mainz@nrubsig.org	compound -r namespace=(
8710898Sroland.mainz@nrubsig.org		typeset -r xhtml=$'http://www.w3.org/1999/xhtml'
8810898Sroland.mainz@nrubsig.org	)
8910898Sroland.mainz@nrubsig.org	typeset -r xml_head=$'<?xml version="1.0" encoding="UTF-8"?>\n'
9010898Sroland.mainz@nrubsig.org)
9110898Sroland.mainz@nrubsig.org
9210898Sroland.mainz@nrubsig.org# main
9310898Sroland.mainz@nrubsig.orgbuiltin basename
9410898Sroland.mainz@nrubsig.org
9510898Sroland.mainz@nrubsig.orgset -o noglob
9610898Sroland.mainz@nrubsig.orgset -o errexit
9710898Sroland.mainz@nrubsig.orgset -o nounset
9810898Sroland.mainz@nrubsig.org
9910898Sroland.mainz@nrubsig.org# tree variable
10010898Sroland.mainz@nrubsig.orgcompound tree
10110898Sroland.mainz@nrubsig.org
10210898Sroland.mainz@nrubsig.orgtypeset progname="${ basename "${0}" ; }"
10310898Sroland.mainz@nrubsig.org
10410898Sroland.mainz@nrubsig.orgtypeset -r cpvprint_usage=$'+
10510898Sroland.mainz@nrubsig.org[-?\n@(#)\$Id: cpvprint (Roland Mainz) 2009-06-15 \$\n]
10610898Sroland.mainz@nrubsig.org[-author?Roland Mainz <roland.mainz@nrubsig.org>]
10710898Sroland.mainz@nrubsig.org[+NAME?cpvprint - render compound variable trees in various formats]
10810898Sroland.mainz@nrubsig.org[+DESCRIPTION?\bcpvprint\b is converter which reads a ksh compound
10910898Sroland.mainz@nrubsig.org	variable and prints it on a different format. Supported
11010898Sroland.mainz@nrubsig.org	formats are \'default\', \'altdefault\',
11110898Sroland.mainz@nrubsig.org	\'tree\', \'alttree\',
11210898Sroland.mainz@nrubsig.org	\'pretty\', \'pretty.html\', \'list\' and \'fulllist\']
11310898Sroland.mainz@nrubsig.org
11410898Sroland.mainz@nrubsig.orgformat [ arguments ]
11510898Sroland.mainz@nrubsig.org
11610898Sroland.mainz@nrubsig.org[+SEE ALSO?\bksh93\b(1), \bcpvlint\b(1)]
11710898Sroland.mainz@nrubsig.org'
11810898Sroland.mainz@nrubsig.org
11910898Sroland.mainz@nrubsig.orgwhile getopts -a "${progname}" "${cpvprint_usage}" OPT ; do
12010898Sroland.mainz@nrubsig.org#	printmsg "## OPT=|${OPT}|, OPTARG=|${OPTARG}|"
12110898Sroland.mainz@nrubsig.org	case ${OPT} in
12210898Sroland.mainz@nrubsig.org		*) usage ;;
12310898Sroland.mainz@nrubsig.org	esac
12410898Sroland.mainz@nrubsig.orgdone
12510898Sroland.mainz@nrubsig.orgshift $((OPTIND-1))
12610898Sroland.mainz@nrubsig.org
12710898Sroland.mainz@nrubsig.org# prechecks
12810898Sroland.mainz@nrubsig.org(( $# > 0 )) || usage
12910898Sroland.mainz@nrubsig.org
13010898Sroland.mainz@nrubsig.orgprintformat="$1"
13110898Sroland.mainz@nrubsig.orgshift
13210898Sroland.mainz@nrubsig.org
13310898Sroland.mainz@nrubsig.org# read variable
13410898Sroland.mainz@nrubsig.orgcase $# in
13510898Sroland.mainz@nrubsig.org	0)
13610898Sroland.mainz@nrubsig.org		read -C tree || fatal_error $"Read error."
13710898Sroland.mainz@nrubsig.org		;;
13810898Sroland.mainz@nrubsig.org	1)
13910898Sroland.mainz@nrubsig.org		integer fd
14010898Sroland.mainz@nrubsig.org
14110898Sroland.mainz@nrubsig.org		redirect {fd}<> "$1" || fatal_error $"Cannot open file."
14210898Sroland.mainz@nrubsig.org		read -u${fd} -C tree || fatal_error $"Read error."
14310898Sroland.mainz@nrubsig.org		redirect {fd}<&- || fatal_error $"Close error."
14410898Sroland.mainz@nrubsig.org		;;
14510898Sroland.mainz@nrubsig.org	2)
14610898Sroland.mainz@nrubsig.org		print -u2 -f $"%s: Unsupported number of arguments.\n" "$0"
14710898Sroland.mainz@nrubsig.org		exit 1
14810898Sroland.mainz@nrubsig.org		;;
14910898Sroland.mainz@nrubsig.orgesac
15010898Sroland.mainz@nrubsig.org
15110898Sroland.mainz@nrubsig.org# print variable
15210898Sroland.mainz@nrubsig.orgcase ${printformat} in
15310898Sroland.mainz@nrubsig.org	'default' | 'tree')
15410898Sroland.mainz@nrubsig.org		print -v tree
15510898Sroland.mainz@nrubsig.org		;;
15610898Sroland.mainz@nrubsig.org	'altdefault' | 'alttree')
15710898Sroland.mainz@nrubsig.org		print -C tree
15810898Sroland.mainz@nrubsig.org		;;
15910898Sroland.mainz@nrubsig.org	'pretty')
16010898Sroland.mainz@nrubsig.org		# print variable tree (same as $ print -v filetree # except that it "looks better")
16110898Sroland.mainz@nrubsig.org		prettyprint_compoundvar tree
16210898Sroland.mainz@nrubsig.org		;;
16310898Sroland.mainz@nrubsig.org	'pretty.html')
16410898Sroland.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' \
16510898Sroland.mainz@nrubsig.org			"${hc.xml_head}" \
16610898Sroland.mainz@nrubsig.org			"${hc.doctype.xhtml.transitional}" \
16710898Sroland.mainz@nrubsig.org			"${hc.namespace.xhtml}" \
16810898Sroland.mainz@nrubsig.org			"ksh Compound Variable Pretty Printer (cpvprint)" \
16910898Sroland.mainz@nrubsig.org			"" \
17010898Sroland.mainz@nrubsig.org			"$(prettyprint_compoundvar tree)" | iconv -f "UTF-8" - -
17110898Sroland.mainz@nrubsig.org		;;
17210898Sroland.mainz@nrubsig.org	'list')
17310898Sroland.mainz@nrubsig.org		set | egrep '^tree.' | sed 's/^tree\.//' | egrep -v '^[[:alnum:]]+(\.([[:alnum:]\.]+)(\[.*\])*)*=\('
17410898Sroland.mainz@nrubsig.org		;;
17510898Sroland.mainz@nrubsig.org	'fulllist')
17610898Sroland.mainz@nrubsig.org		set | egrep "^tree."
17710898Sroland.mainz@nrubsig.org		;;
17810898Sroland.mainz@nrubsig.org	*)
17910898Sroland.mainz@nrubsig.org		fatal_error $"Unsupported format."
18010898Sroland.mainz@nrubsig.org		;;
18110898Sroland.mainz@nrubsig.orgesac
18210898Sroland.mainz@nrubsig.org
18310898Sroland.mainz@nrubsig.orgexit 0
18410898Sroland.mainz@nrubsig.org# EOF.
185