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