xref: /titanic_51/usr/src/lib/libshell/common/scripts/cpvprint.sh (revision 3e14f97f673e8a630f076077de35afdd43dc1587)
134f9b3eeSRoland Mainz#!/usr/bin/ksh93
234f9b3eeSRoland Mainz
334f9b3eeSRoland Mainz#
434f9b3eeSRoland Mainz# CDDL HEADER START
534f9b3eeSRoland Mainz#
634f9b3eeSRoland Mainz# The contents of this file are subject to the terms of the
734f9b3eeSRoland Mainz# Common Development and Distribution License (the "License").
834f9b3eeSRoland Mainz# You may not use this file except in compliance with the License.
934f9b3eeSRoland Mainz#
1034f9b3eeSRoland Mainz# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
1134f9b3eeSRoland Mainz# or http://www.opensolaris.org/os/licensing.
1234f9b3eeSRoland Mainz# See the License for the specific language governing permissions
1334f9b3eeSRoland Mainz# and limitations under the License.
1434f9b3eeSRoland Mainz#
1534f9b3eeSRoland Mainz# When distributing Covered Code, include this CDDL HEADER in each
1634f9b3eeSRoland Mainz# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1734f9b3eeSRoland Mainz# If applicable, add the following below this CDDL HEADER, with the
1834f9b3eeSRoland Mainz# fields enclosed by brackets "[]" replaced with your own identifying
1934f9b3eeSRoland Mainz# information: Portions Copyright [yyyy] [name of copyright owner]
2034f9b3eeSRoland Mainz#
2134f9b3eeSRoland Mainz# CDDL HEADER END
2234f9b3eeSRoland Mainz#
2334f9b3eeSRoland Mainz
2434f9b3eeSRoland Mainz#
25*3e14f97fSRoger A. Faulkner# Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
2634f9b3eeSRoland Mainz#
2734f9b3eeSRoland Mainz
2834f9b3eeSRoland Mainz#
2934f9b3eeSRoland Mainz# cpvprint - compound variable pretty printer
3034f9b3eeSRoland Mainz#
3134f9b3eeSRoland Mainz
3234f9b3eeSRoland Mainz# Solaris needs /usr/xpg6/bin:/usr/xpg4/bin because the tools in /usr/bin are not POSIX-conformant
3334f9b3eeSRoland Mainzexport PATH=/usr/xpg6/bin:/usr/xpg4/bin:/bin:/usr/bin
3434f9b3eeSRoland Mainz
3534f9b3eeSRoland Mainz# Make sure all math stuff runs in the "C" locale to avoid problems
3634f9b3eeSRoland Mainz# with alternative # radix point representations (e.g. ',' instead of
3734f9b3eeSRoland Mainz# '.' in de_DE.*-locales). This needs to be set _before_ any
3834f9b3eeSRoland Mainz# floating-point constants are defined in this script).
3934f9b3eeSRoland Mainzif [[ "${LC_ALL}" != "" ]] ; then
4034f9b3eeSRoland Mainz    export \
4134f9b3eeSRoland Mainz        LC_MONETARY="${LC_ALL}" \
4234f9b3eeSRoland Mainz        LC_MESSAGES="${LC_ALL}" \
4334f9b3eeSRoland Mainz        LC_COLLATE="${LC_ALL}" \
4434f9b3eeSRoland Mainz        LC_CTYPE="${LC_ALL}"
4534f9b3eeSRoland Mainz        unset LC_ALL
4634f9b3eeSRoland Mainzfi
4734f9b3eeSRoland Mainzexport LC_NUMERIC=C
4834f9b3eeSRoland Mainz
4934f9b3eeSRoland Mainzfunction fatal_error
5034f9b3eeSRoland Mainz{
5134f9b3eeSRoland Mainz	print -u2 "${progname}: $*"
5234f9b3eeSRoland Mainz	exit 1
5334f9b3eeSRoland Mainz}
5434f9b3eeSRoland Mainz
5534f9b3eeSRoland Mainzfunction prettyprint_compoundvar
5634f9b3eeSRoland Mainz{
5734f9b3eeSRoland Mainz	nameref var=$1
5834f9b3eeSRoland Mainz
5934f9b3eeSRoland Mainz	# print tree
6034f9b3eeSRoland Mainz	str="${ print -v var ; }"
6134f9b3eeSRoland Mainz	# do some "pretty-printing" for human users (the output is still a
6234f9b3eeSRoland Mainz	# valid compound variable value)
6334f9b3eeSRoland Mainz	# (note: This does not scale well with large files)
6434f9b3eeSRoland Mainz	str="${str//$'\t'typeset -l -E /$'\t'float }"
6534f9b3eeSRoland Mainz	str="${str//$'\t'typeset -l -i /$'\t'integer }"
6634f9b3eeSRoland Mainz	str="${str//$'\t'typeset -C /$'\t'compound }"
6734f9b3eeSRoland Mainz	print -r -- "${str}"
6834f9b3eeSRoland Mainz
6934f9b3eeSRoland Mainz	return 0
7034f9b3eeSRoland Mainz}
7134f9b3eeSRoland Mainz
7234f9b3eeSRoland Mainzfunction usage
7334f9b3eeSRoland Mainz{
7434f9b3eeSRoland Mainz	OPTIND=0
7534f9b3eeSRoland Mainz	getopts -a "${progname}" "${cpvprint_usage}" OPT '-?'
7634f9b3eeSRoland Mainz	exit 2
7734f9b3eeSRoland Mainz}
7834f9b3eeSRoland Mainz
7934f9b3eeSRoland Mainz# HTML constants
8034f9b3eeSRoland Mainzcompound -r hc=(
8134f9b3eeSRoland Mainz	compound -r doctype=(
8234f9b3eeSRoland Mainz		compound -r xhtml=(
8334f9b3eeSRoland Mainz			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'
8434f9b3eeSRoland Mainz		)
8534f9b3eeSRoland Mainz	)
8634f9b3eeSRoland Mainz	compound -r namespace=(
8734f9b3eeSRoland Mainz		typeset -r xhtml=$'http://www.w3.org/1999/xhtml'
8834f9b3eeSRoland Mainz	)
8934f9b3eeSRoland Mainz	typeset -r xml_head=$'<?xml version="1.0" encoding="UTF-8"?>\n'
9034f9b3eeSRoland Mainz)
9134f9b3eeSRoland Mainz
9234f9b3eeSRoland Mainz# main
9334f9b3eeSRoland Mainzbuiltin basename
9434f9b3eeSRoland Mainz
9534f9b3eeSRoland Mainzset -o noglob
9634f9b3eeSRoland Mainzset -o errexit
9734f9b3eeSRoland Mainzset -o nounset
9834f9b3eeSRoland Mainz
9934f9b3eeSRoland Mainz# tree variable
10034f9b3eeSRoland Mainzcompound tree
10134f9b3eeSRoland Mainz
10234f9b3eeSRoland Mainztypeset progname="${ basename "${0}" ; }"
10334f9b3eeSRoland Mainz
10434f9b3eeSRoland Mainztypeset -r cpvprint_usage=$'+
10534f9b3eeSRoland Mainz[-?\n@(#)\$Id: cpvprint (Roland Mainz) 2009-06-15 \$\n]
10634f9b3eeSRoland Mainz[-author?Roland Mainz <roland.mainz@nrubsig.org>]
10734f9b3eeSRoland Mainz[+NAME?cpvprint - render compound variable trees in various formats]
10834f9b3eeSRoland Mainz[+DESCRIPTION?\bcpvprint\b is converter which reads a ksh compound
10934f9b3eeSRoland Mainz	variable and prints it on a different format. Supported
11034f9b3eeSRoland Mainz	formats are \'default\', \'altdefault\',
11134f9b3eeSRoland Mainz	\'tree\', \'alttree\',
11234f9b3eeSRoland Mainz	\'pretty\', \'pretty.html\', \'list\' and \'fulllist\']
11334f9b3eeSRoland Mainz
11434f9b3eeSRoland Mainzformat [ arguments ]
11534f9b3eeSRoland Mainz
11634f9b3eeSRoland Mainz[+SEE ALSO?\bksh93\b(1), \bcpvlint\b(1)]
11734f9b3eeSRoland Mainz'
11834f9b3eeSRoland Mainz
11934f9b3eeSRoland Mainzwhile getopts -a "${progname}" "${cpvprint_usage}" OPT ; do
12034f9b3eeSRoland Mainz#	printmsg "## OPT=|${OPT}|, OPTARG=|${OPTARG}|"
12134f9b3eeSRoland Mainz	case ${OPT} in
12234f9b3eeSRoland Mainz		*) usage ;;
12334f9b3eeSRoland Mainz	esac
12434f9b3eeSRoland Mainzdone
12534f9b3eeSRoland Mainzshift $((OPTIND-1))
12634f9b3eeSRoland Mainz
12734f9b3eeSRoland Mainz# prechecks
12834f9b3eeSRoland Mainz(( $# > 0 )) || usage
12934f9b3eeSRoland Mainz
13034f9b3eeSRoland Mainzprintformat="$1"
13134f9b3eeSRoland Mainzshift
13234f9b3eeSRoland Mainz
13334f9b3eeSRoland Mainz# read variable
13434f9b3eeSRoland Mainzcase $# in
13534f9b3eeSRoland Mainz	0)
13634f9b3eeSRoland Mainz		read -C tree || fatal_error $"Read error."
13734f9b3eeSRoland Mainz		;;
13834f9b3eeSRoland Mainz	1)
13934f9b3eeSRoland Mainz		integer fd
14034f9b3eeSRoland Mainz
14134f9b3eeSRoland Mainz		redirect {fd}<> "$1" || fatal_error $"Cannot open file."
14234f9b3eeSRoland Mainz		read -u${fd} -C tree || fatal_error $"Read error."
14334f9b3eeSRoland Mainz		redirect {fd}<&- || fatal_error $"Close error."
14434f9b3eeSRoland Mainz		;;
14534f9b3eeSRoland Mainz	2)
14634f9b3eeSRoland Mainz		print -u2 -f $"%s: Unsupported number of arguments.\n" "$0"
14734f9b3eeSRoland Mainz		exit 1
14834f9b3eeSRoland Mainz		;;
14934f9b3eeSRoland Mainzesac
15034f9b3eeSRoland Mainz
15134f9b3eeSRoland Mainz# print variable
15234f9b3eeSRoland Mainzcase ${printformat} in
15334f9b3eeSRoland Mainz	'default' | 'tree')
15434f9b3eeSRoland Mainz		print -v tree
15534f9b3eeSRoland Mainz		;;
15634f9b3eeSRoland Mainz	'altdefault' | 'alttree')
15734f9b3eeSRoland Mainz		print -C tree
15834f9b3eeSRoland Mainz		;;
15934f9b3eeSRoland Mainz	'pretty')
16034f9b3eeSRoland Mainz		# print variable tree (same as $ print -v filetree # except that it "looks better")
16134f9b3eeSRoland Mainz		prettyprint_compoundvar tree
16234f9b3eeSRoland Mainz		;;
16334f9b3eeSRoland Mainz	'pretty.html')
16434f9b3eeSRoland Mainz		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' \
16534f9b3eeSRoland Mainz			"${hc.xml_head}" \
16634f9b3eeSRoland Mainz			"${hc.doctype.xhtml.transitional}" \
16734f9b3eeSRoland Mainz			"${hc.namespace.xhtml}" \
16834f9b3eeSRoland Mainz			"ksh Compound Variable Pretty Printer (cpvprint)" \
16934f9b3eeSRoland Mainz			"" \
17034f9b3eeSRoland Mainz			"$(prettyprint_compoundvar tree)" | iconv -f "UTF-8" - -
17134f9b3eeSRoland Mainz		;;
17234f9b3eeSRoland Mainz	'list')
17334f9b3eeSRoland Mainz		set | egrep '^tree.' | sed 's/^tree\.//' | egrep -v '^[[:alnum:]]+(\.([[:alnum:]\.]+)(\[.*\])*)*=\('
17434f9b3eeSRoland Mainz		;;
17534f9b3eeSRoland Mainz	'fulllist')
17634f9b3eeSRoland Mainz		set | egrep "^tree."
17734f9b3eeSRoland Mainz		;;
17834f9b3eeSRoland Mainz	*)
17934f9b3eeSRoland Mainz		fatal_error $"Unsupported format."
18034f9b3eeSRoland Mainz		;;
18134f9b3eeSRoland Mainzesac
18234f9b3eeSRoland Mainz
18334f9b3eeSRoland Mainzexit 0
18434f9b3eeSRoland Mainz# EOF.
185