summaryrefslogtreecommitdiff
path: root/usr/src/cmd/mdb/tools/scripts/tigen.sh
blob: d74a55486b9c7142be899a1bf509e8a71e1e9076 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/bin/ksh
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License, Version 1.0 only
# (the "License").  You may not use this file except in compliance
# with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#
# Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
#

#
# Terminal Info Generator
#
# This script generates a static terminfo database for use by mdb.  For each
# of the terminal properties used by mdb_termio.c, this script uses tput(1)
# to determine the value of the given attribute for each specified terminal
# type.  The script produces an ANSI-C source file which contains a static
# array for each terminal type storing the properties.  An additional array
# is then declared containing a list of the terminal types and pointers to
# the previous arrays.  Finally, source code for several terminfo routines
# are included that simply access the arrays and return the saved properties.
#

PATH=/usr/bin; export PATH

PROGNAME=$(basename "$0")

usage()
{
	echo "Usage: $PROGNAME -s skel -t termio [-v] term ..." >&2
	exit 2
}

extract_section()
{
	typeset skel="$1"
	typeset secname="$2"

	nawk <$skel -v name=$secname -v skel=$skel '
	    /\/\* [^ ]* [^ ]* \*\// && $3 == name {
		if ($2 == "BEGIN") {
			printing = 1;
			printf("# %d \"%s\"\n", NR + 1, skel);
		} else {
			printing = 0;
		}
		next;
	    }

	    printing != 0 { print; }
	'
}

verbose=false
termio_c=
terminfo_skel=

while getopts s:t:v name ; do
	case $name in
	    v)
		verbose=true
		;;
	    s)
		terminfo_skel=$OPTARG
		;;
	    t)
		termio_c=$OPTARG
		;;
	    ?)
		usage
		;;
	esac
done
shift $(($OPTIND - 1))

[[ -z "$terminfo_skel" || -z "$termio_c" || $# -eq 0 ]] && usage

termlist=$*
for term in $termlist; do
	tput -T $term init >/dev/null 2>&1
	if [ $? -ne 0 ]; then
		echo "`basename $0`: invalid terminal -- $term" >& 2
		exit 1
	fi
done

# Extract the prologue from the skeleton
extract_section $terminfo_skel PROLOGUE

#
# For each terminal in the terminal list, produce a property definition array
# listing each property we need in mdb_termio.c and its current value.
#
for term in $termlist; do
	#
	# We don't want the compiler to blame the skeleton if it doesn't like
	# the array we generate here, so point the finger elsewhere
	#
	echo "# 1 \"dynamic $term data from tigen\""

	cterm=$(echo "$term" |tr '-' '_')

	$verbose && echo "loading terminfo for $term ... \c" >& 2
	echo "static const termio_attr_t ${cterm}_attrs[] = {"

	sed -n '/termio_attrs\[\] = /,/^}/p' $termio_c | \
	    sed -n \ 's/{ "\([a-z0-9]*\)", \([A-Z_]*\),.*/\1 \2/p' | \
	    while read attr type; do

		case "$type" in
		TIO_ATTR_REQSTR|TIO_ATTR_STR)
			data="\"`tput -T $term $attr | od -bv |
			    sed 's/^[0-9]*//;s/ /\\\\\\\\/g;/^\$/d'`\""
			[ "$data" = '""' ] && data=NULL
			;;
		TIO_ATTR_BOOL)
			tput -T $term $attr
			data=`expr 1 - $?`
			;;
		TIO_ATTR_INT)
			data=`tput -T $term $attr`
			;;
		*)
			echo "`basename $0`: unknown type for $attr: $type" >& 2
			exit 1
		esac
		echo "\t{ \"$attr\", $type, (void *)$data },"
	done

	echo "\t{ NULL, 0, NULL }"
	echo "};\n"

	$verbose && echo "done" >& 2
done

#
# For each terminal in the terminal list, produce an entry in the terminal
# database array linking this terminal to its terminfo property array.
#
echo "# 1 \"dynamic array from tigen\""
echo "static const termio_desc_t termio_db[] = {"
for term in $termlist; do
	cterm=$(echo "$term" |tr '-' '_')
	echo "\t{ \"$term\", ${cterm}_attrs },"
done
echo "\t{ NULL, NULL }\n};"

extract_section $terminfo_skel EPILOGUE

exit 0