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