xref: /onnv-gate/usr/src/cmd/mdb/tools/scripts/tigen.sh (revision 0:68f95e015346)
1*0Sstevel@tonic-gate#!/bin/ksh
2*0Sstevel@tonic-gate#
3*0Sstevel@tonic-gate# CDDL HEADER START
4*0Sstevel@tonic-gate#
5*0Sstevel@tonic-gate# The contents of this file are subject to the terms of the
6*0Sstevel@tonic-gate# Common Development and Distribution License, Version 1.0 only
7*0Sstevel@tonic-gate# (the "License").  You may not use this file except in compliance
8*0Sstevel@tonic-gate# with the License.
9*0Sstevel@tonic-gate#
10*0Sstevel@tonic-gate# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
11*0Sstevel@tonic-gate# or http://www.opensolaris.org/os/licensing.
12*0Sstevel@tonic-gate# See the License for the specific language governing permissions
13*0Sstevel@tonic-gate# and limitations under the License.
14*0Sstevel@tonic-gate#
15*0Sstevel@tonic-gate# When distributing Covered Code, include this CDDL HEADER in each
16*0Sstevel@tonic-gate# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
17*0Sstevel@tonic-gate# If applicable, add the following below this CDDL HEADER, with the
18*0Sstevel@tonic-gate# fields enclosed by brackets "[]" replaced with your own identifying
19*0Sstevel@tonic-gate# information: Portions Copyright [yyyy] [name of copyright owner]
20*0Sstevel@tonic-gate#
21*0Sstevel@tonic-gate# CDDL HEADER END
22*0Sstevel@tonic-gate#
23*0Sstevel@tonic-gate#
24*0Sstevel@tonic-gate# Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
25*0Sstevel@tonic-gate# Use is subject to license terms.
26*0Sstevel@tonic-gate#
27*0Sstevel@tonic-gate#ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate#
29*0Sstevel@tonic-gate
30*0Sstevel@tonic-gate#
31*0Sstevel@tonic-gate# Terminal Info Generator
32*0Sstevel@tonic-gate#
33*0Sstevel@tonic-gate# This script generates a static terminfo database for use by mdb.  For each
34*0Sstevel@tonic-gate# of the terminal properties used by mdb_termio.c, this script uses tput(1)
35*0Sstevel@tonic-gate# to determine the value of the given attribute for each specified terminal
36*0Sstevel@tonic-gate# type.  The script produces an ANSI-C source file which contains a static
37*0Sstevel@tonic-gate# array for each terminal type storing the properties.  An additional array
38*0Sstevel@tonic-gate# is then declared containing a list of the terminal types and pointers to
39*0Sstevel@tonic-gate# the previous arrays.  Finally, source code for several terminfo routines
40*0Sstevel@tonic-gate# are included that simply access the arrays and return the saved properties.
41*0Sstevel@tonic-gate#
42*0Sstevel@tonic-gate
43*0Sstevel@tonic-gatePATH=/usr/bin; export PATH
44*0Sstevel@tonic-gate
45*0Sstevel@tonic-gatePROGNAME=$(basename "$0")
46*0Sstevel@tonic-gate
47*0Sstevel@tonic-gateusage()
48*0Sstevel@tonic-gate{
49*0Sstevel@tonic-gate	echo "Usage: $PROGNAME -s skel -t termio [-v] term ..." >&2
50*0Sstevel@tonic-gate	exit 2
51*0Sstevel@tonic-gate}
52*0Sstevel@tonic-gate
53*0Sstevel@tonic-gateextract_section()
54*0Sstevel@tonic-gate{
55*0Sstevel@tonic-gate	typeset skel="$1"
56*0Sstevel@tonic-gate	typeset secname="$2"
57*0Sstevel@tonic-gate
58*0Sstevel@tonic-gate	nawk <$skel -v name=$secname -v skel=$skel '
59*0Sstevel@tonic-gate	    /\/\* [^ ]* [^ ]* \*\// && $3 == name {
60*0Sstevel@tonic-gate		if ($2 == "BEGIN") {
61*0Sstevel@tonic-gate			printing = 1;
62*0Sstevel@tonic-gate			printf("# %d \"%s\"\n", NR + 1, skel);
63*0Sstevel@tonic-gate		} else {
64*0Sstevel@tonic-gate			printing = 0;
65*0Sstevel@tonic-gate		}
66*0Sstevel@tonic-gate		next;
67*0Sstevel@tonic-gate	    }
68*0Sstevel@tonic-gate
69*0Sstevel@tonic-gate	    printing != 0 { print; }
70*0Sstevel@tonic-gate	'
71*0Sstevel@tonic-gate}
72*0Sstevel@tonic-gate
73*0Sstevel@tonic-gateverbose=false
74*0Sstevel@tonic-gatetermio_c=
75*0Sstevel@tonic-gateterminfo_skel=
76*0Sstevel@tonic-gate
77*0Sstevel@tonic-gatewhile getopts s:t:v name ; do
78*0Sstevel@tonic-gate	case $name in
79*0Sstevel@tonic-gate	    v)
80*0Sstevel@tonic-gate		verbose=true
81*0Sstevel@tonic-gate		;;
82*0Sstevel@tonic-gate	    s)
83*0Sstevel@tonic-gate		terminfo_skel=$OPTARG
84*0Sstevel@tonic-gate		;;
85*0Sstevel@tonic-gate	    t)
86*0Sstevel@tonic-gate		termio_c=$OPTARG
87*0Sstevel@tonic-gate		;;
88*0Sstevel@tonic-gate	    ?)
89*0Sstevel@tonic-gate		usage
90*0Sstevel@tonic-gate		;;
91*0Sstevel@tonic-gate	esac
92*0Sstevel@tonic-gatedone
93*0Sstevel@tonic-gateshift $(($OPTIND - 1))
94*0Sstevel@tonic-gate
95*0Sstevel@tonic-gate[[ -z "$terminfo_skel" || -z "$termio_c" || $# -eq 0 ]] && usage
96*0Sstevel@tonic-gate
97*0Sstevel@tonic-gatetermlist=$*
98*0Sstevel@tonic-gatefor term in $termlist; do
99*0Sstevel@tonic-gate	tput -T $term init >/dev/null 2>&1
100*0Sstevel@tonic-gate	if [ $? -ne 0 ]; then
101*0Sstevel@tonic-gate		echo "`basename $0`: invalid terminal -- $term" >& 2
102*0Sstevel@tonic-gate		exit 1
103*0Sstevel@tonic-gate	fi
104*0Sstevel@tonic-gatedone
105*0Sstevel@tonic-gate
106*0Sstevel@tonic-gate# Extract the prologue from the skeleton
107*0Sstevel@tonic-gateextract_section $terminfo_skel PROLOGUE
108*0Sstevel@tonic-gate
109*0Sstevel@tonic-gate#
110*0Sstevel@tonic-gate# For each terminal in the terminal list, produce a property definition array
111*0Sstevel@tonic-gate# listing each property we need in mdb_termio.c and its current value.
112*0Sstevel@tonic-gate#
113*0Sstevel@tonic-gatefor term in $termlist; do
114*0Sstevel@tonic-gate	#
115*0Sstevel@tonic-gate	# We don't want the compiler to blame the skeleton if it doesn't like
116*0Sstevel@tonic-gate	# the array we generate here, so point the finger elsewhere
117*0Sstevel@tonic-gate	#
118*0Sstevel@tonic-gate	echo "# 1 \"dynamic $term data from tigen\""
119*0Sstevel@tonic-gate
120*0Sstevel@tonic-gate	cterm=$(echo "$term" |tr '-' '_')
121*0Sstevel@tonic-gate
122*0Sstevel@tonic-gate	$verbose && echo "loading terminfo for $term ... \c" >& 2
123*0Sstevel@tonic-gate	echo "static const termio_attr_t ${cterm}_attrs[] = {"
124*0Sstevel@tonic-gate
125*0Sstevel@tonic-gate	sed -n '/termio_attrs\[\] = /,/^\}/p' $termio_c | \
126*0Sstevel@tonic-gate	    sed -n \ 's/{ "\([a-z0-9]*\)", \([A-Z_]*\),.*/\1 \2/p' | \
127*0Sstevel@tonic-gate	    while read attr type; do
128*0Sstevel@tonic-gate
129*0Sstevel@tonic-gate		case "$type" in
130*0Sstevel@tonic-gate		TIO_ATTR_REQSTR|TIO_ATTR_STR)
131*0Sstevel@tonic-gate			data="\"`tput -T $term $attr | od -bv |
132*0Sstevel@tonic-gate			    sed 's/^[0-9]*//;s/ /\\\\\\\\/g;/^\$/d'`\""
133*0Sstevel@tonic-gate			[ "$data" = '""' ] && data=NULL
134*0Sstevel@tonic-gate			;;
135*0Sstevel@tonic-gate		TIO_ATTR_BOOL)
136*0Sstevel@tonic-gate			tput -T $term $attr
137*0Sstevel@tonic-gate			data=`expr 1 - $?`
138*0Sstevel@tonic-gate			;;
139*0Sstevel@tonic-gate		TIO_ATTR_INT)
140*0Sstevel@tonic-gate			data=`tput -T $term $attr`
141*0Sstevel@tonic-gate			;;
142*0Sstevel@tonic-gate		*)
143*0Sstevel@tonic-gate			echo "`basename $0`: unknown type for $attr: $type" >& 2
144*0Sstevel@tonic-gate			exit 1
145*0Sstevel@tonic-gate		esac
146*0Sstevel@tonic-gate		echo "\t{ \"$attr\", $type, (void *)$data },"
147*0Sstevel@tonic-gate	done
148*0Sstevel@tonic-gate
149*0Sstevel@tonic-gate	echo "\t{ NULL, NULL, NULL }"
150*0Sstevel@tonic-gate	echo "};\n"
151*0Sstevel@tonic-gate
152*0Sstevel@tonic-gate	$verbose && echo "done" >& 2
153*0Sstevel@tonic-gatedone
154*0Sstevel@tonic-gate
155*0Sstevel@tonic-gate#
156*0Sstevel@tonic-gate# For each terminal in the terminal list, produce an entry in the terminal
157*0Sstevel@tonic-gate# database array linking this terminal to its terminfo property array.
158*0Sstevel@tonic-gate#
159*0Sstevel@tonic-gateecho "# 1 \"dynamic array from tigen\""
160*0Sstevel@tonic-gateecho "static const termio_desc_t termio_db[] = {"
161*0Sstevel@tonic-gatefor term in $termlist; do
162*0Sstevel@tonic-gate	cterm=$(echo "$term" |tr '-' '_')
163*0Sstevel@tonic-gate	echo "\t{ \"$term\", ${cterm}_attrs },"
164*0Sstevel@tonic-gatedone
165*0Sstevel@tonic-gateecho "\t{ NULL, NULL }\n};"
166*0Sstevel@tonic-gate
167*0Sstevel@tonic-gateextract_section $terminfo_skel EPILOGUE
168*0Sstevel@tonic-gate
169*0Sstevel@tonic-gateexit 0
170