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