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