tcapquery.pl revision 20d2c4d2
1#!/usr/bin/perl -w
2# $XTermId: tcapquery.pl,v 1.18 2010/01/04 09:43:46 tom Exp $
3# -----------------------------------------------------------------------------
4# this file is part of xterm
5#
6# Copyright 2004-2008,2010 by Thomas E. Dickey
7#
8#                         All Rights Reserved
9#
10# Permission is hereby granted, free of charge, to any person obtaining a
11# copy of this software and associated documentation files (the
12# "Software"), to deal in the Software without restriction, including
13# without limitation the rights to use, copy, modify, merge, publish,
14# distribute, sublicense, and/or sell copies of the Software, and to
15# permit persons to whom the Software is furnished to do so, subject to
16# the following conditions:
17#
18# The above copyright notice and this permission notice shall be included
19# in all copies or substantial portions of the Software.
20#
21# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24# IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
25# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
26# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
27# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28#
29# Except as contained in this notice, the name(s) of the above copyright
30# holders shall not be used in advertising or otherwise to promote the
31# sale, use or other dealings in this Software without prior written
32# authorization.
33# -----------------------------------------------------------------------------
34# Test the tcap-query option of xterm.
35
36use strict;
37
38use Getopt::Std;
39use IO::Handle;
40
41our ($opt_a, $opt_b, $opt_c, $opt_e, $opt_f, $opt_i, $opt_k, $opt_m, $opt_t, $opt_x, $opt_X);
42&getopts('abcefikmt:x:X') || die("Usage: $0 [options]\n
43Options:\n
44  -a      (same as -c -e -f -k -m)
45  -b      use both terminfo and termcap (default is termcap)
46  -c      cursor-keys
47  -e      editing keypad-keys
48  -f      function-keys
49  -i      use terminfo rather than termcap names
50  -k      numeric keypad-keys
51  -m      miscellaneous (none of -c, -e, -f, -k)
52  -t NAME use given NAME for \$TERM, set that in xterm's tcap keyboard
53  -x KEY  extended cursor/editing key (terminfo only)
54  -X      test all extended cursor- and/or editing-keys (terminfo)
55");
56
57if ( not ( defined($opt_c)
58	or defined($opt_e)
59	or defined($opt_f)
60	or defined($opt_k)
61	or defined($opt_m)
62	or defined($opt_x) ) ) {
63	$opt_a=1;
64}
65
66sub no_reply($) {
67	open TTY, "+</dev/tty" or die("Cannot open /dev/tty\n");
68	autoflush TTY 1;
69	my $old=`stty -g`;
70	system "stty raw -echo min 0 time 5";
71
72	print TTY @_;
73	close TTY;
74	system "stty $old";
75}
76
77sub get_reply($) {
78	open TTY, "+</dev/tty" or die("Cannot open /dev/tty\n");
79	autoflush TTY 1;
80	my $old=`stty -g`;
81	system "stty raw -echo min 0 time 5";
82
83	print TTY @_;
84	my $reply=<TTY>;
85	close TTY;
86	system "stty $old";
87	if ( defined $reply ) {
88		die("^C received\n") if ( "$reply" eq "\003" );
89	}
90	return $reply;
91}
92
93sub hexified($) {
94	my $value = $_[0];
95	my $result = "";
96	my $n;
97
98	for ( $n = 0; $n < length($value); ++$n) {
99		$result .= sprintf("%02X", ord substr($value,$n,1));
100	}
101	return $result;
102}
103
104sub modify_tcap($) {
105	my $name = $_[0];
106	my $param = hexified($name);
107	no_reply("\x1bP+p" . $param . "\x1b\\");
108}
109
110sub query_tcap($$) {
111	my $tcap = $_[0];
112	my $tinfo = $_[1];
113	my $param1 = hexified($tcap);
114	my $param2 = hexified($tinfo);
115	my $reply;
116
117	# uncomment one of the following lines
118	if ( defined($opt_b) ) {
119		$reply=get_reply("\x1bP+q" . $param1 . ";" . $param2 . "\x1b\\");
120	} elsif ( defined($opt_i) ) {
121		$reply=get_reply("\x1bP+q" . $param2 . "\x1b\\");
122	} else {
123		$reply=get_reply("\x1bP+q" . $param1 . "\x1b\\");
124	}
125
126	return unless defined $reply;
127	if ( $reply =~ /\x1bP1\+r[[:xdigit:]]+=[[:xdigit:]]*.*/ ) {
128		my $value = $reply;
129		my $n;
130
131		$value =~ s/^\x1bP1\+r//;
132		$value =~ s/\x1b\\//;
133
134		my $result = "";
135		for ( $n = 0; $n < length($value); ) {
136			my $c = substr($value,$n,1);
137			# handle semicolon and equals
138			if ( $c =~ /[[:punct:]]/ ) {
139				$n += 1;
140				$result .= $c;
141			} else {
142				# handle hex-data
143				my $k = hex substr($value,$n,2);
144				if ( $k == 0x1b ) {
145					$result .= "\\E";
146				} elsif ( $k == 0x7f ) {
147					$result .= "^?";
148				} elsif ( $k == 32 ) {
149					$result .= "\\s";
150				} elsif ( $k < 32 ) {
151					$result .= sprintf("^%c", $k + 64);
152				} elsif ( $k > 128 ) {
153					$result .= sprintf("\\%03o", $k);
154				} else {
155					$result .= chr($k);
156				}
157				$n += 2;
158			}
159		}
160
161		printf "%s\n", $result;
162	}
163}
164
165# extended-keys are a feature of ncurses 5.0 and later
166sub query_extended($) {
167	my $name = $_[0];
168	my $n;
169
170	$name = "k" . $name if ( $name !~ /^k/ );
171
172	for ( $n = 2; $n <= 7; ++$n) {
173		my $test = $name;
174		$test = $test . $n if ( $n > 2 );
175		query_tcap( $name, $test );
176	}
177}
178
179query_tcap(	"TN",	"name");
180if ( defined($opt_t) ) {
181	printf "Setting TERM=%s\n", $opt_t;
182	modify_tcap($opt_t);
183}
184
185# See xtermcapKeycode()
186if ( defined($opt_a) || defined($opt_c) ) {
187query_tcap(	"ku",	"kcuu1");
188query_tcap(	"kd",	"kcud1");
189query_tcap(	"kr",	"kcuf1");
190query_tcap(	"kl",	"kcub1");
191
192query_tcap(	"kF",	"kind");
193query_tcap(	"kR",	"kri");
194query_tcap(	"%i",	"kRIT");
195query_tcap(	"#4",	"kLFT");
196}
197
198if ( defined($opt_a) || defined($opt_e) ) {
199query_tcap(	"kD",	"kdch1");
200query_tcap(	"kI",	"kich1");
201
202query_tcap(	"kh",	"khome");
203query_tcap(	"\@7",	"kend");
204query_tcap(	"#2",	"kHOM");
205query_tcap(	"*7",	"kEND");
206
207query_tcap(	"*6",	"kslt");
208query_tcap(	"#6",	"kSLT");
209query_tcap(	"\@0",	"kfnd");
210query_tcap(	"*0",	"kFND");
211
212query_tcap(	"kN",	"knp");
213query_tcap(	"kP",	"kpp");
214
215query_tcap(	"%c",	"kNXT");
216query_tcap(	"%e",	"kPRV");
217}
218
219if ( defined($opt_a) || defined($opt_f) ) {
220query_tcap(	"k1",	"kf1");
221query_tcap(	"k2",	"kf2");
222query_tcap(	"k3",	"kf3");
223query_tcap(	"k4",	"kf4");
224query_tcap(	"k5",	"kf5");
225query_tcap(	"k6",	"kf6");
226query_tcap(	"k7",	"kf7");
227query_tcap(	"k8",	"kf8");
228query_tcap(	"k9",	"kf9");
229query_tcap(	"k;",	"kf10");
230query_tcap(	"F1",	"kf11");
231query_tcap(	"F2",	"kf12");
232query_tcap(	"F3",	"kf13");
233query_tcap(	"F4",	"kf14");
234query_tcap(	"F5",	"kf15");
235query_tcap(	"F6",	"kf16");
236query_tcap(	"F7",	"kf17");
237query_tcap(	"F8",	"kf18");
238query_tcap(	"F9",	"kf19");
239query_tcap(	"FA",	"kf20");
240query_tcap(	"FB",	"kf21");
241query_tcap(	"FC",	"kf22");
242query_tcap(	"FD",	"kf23");
243query_tcap(	"FE",	"kf24");
244query_tcap(	"FF",	"kf25");
245query_tcap(	"FG",	"kf26");
246query_tcap(	"FH",	"kf27");
247query_tcap(	"FI",	"kf28");
248query_tcap(	"FJ",	"kf29");
249query_tcap(	"FK",	"kf30");
250query_tcap(	"FL",	"kf31");
251query_tcap(	"FM",	"kf32");
252query_tcap(	"FN",	"kf33");
253query_tcap(	"FO",	"kf34");
254query_tcap(	"FP",	"kf35");
255query_tcap(	"FQ",	"kf36");
256query_tcap(	"FR",	"kf37");
257query_tcap(	"FS",	"kf38");
258query_tcap(	"FT",	"kf39");
259query_tcap(	"FU",	"kf40");
260query_tcap(	"FV",	"kf41");
261query_tcap(	"FW",	"kf42");
262query_tcap(	"FX",	"kf43");
263query_tcap(	"FY",	"kf44");
264query_tcap(	"FZ",	"kf45");
265query_tcap(	"Fa",	"kf46");
266query_tcap(	"Fb",	"kf47");
267query_tcap(	"Fc",	"kf48");
268query_tcap(	"Fd",	"kf49");
269query_tcap(	"Fe",	"kf50");
270query_tcap(	"Ff",	"kf51");
271query_tcap(	"Fg",	"kf52");
272query_tcap(	"Fh",	"kf53");
273query_tcap(	"Fi",	"kf54");
274query_tcap(	"Fj",	"kf55");
275query_tcap(	"Fk",	"kf56");
276query_tcap(	"Fl",	"kf57");
277query_tcap(	"Fm",	"kf58");
278query_tcap(	"Fn",	"kf59");
279query_tcap(	"Fo",	"kf60");
280query_tcap(	"Fp",	"kf61");
281query_tcap(	"Fq",	"kf62");
282query_tcap(	"Fr",	"kf63");
283}
284
285if ( defined($opt_a) || defined($opt_k) ) {
286query_tcap(	"K1",	"ka1");
287query_tcap(	"K3",	"ka3");
288query_tcap(	"K4",	"kc1");
289query_tcap(	"K5",	"kc3");
290}
291
292if ( defined($opt_a) || defined($opt_m) ) {
293query_tcap(	"kB",	"kcbt");
294query_tcap(	"kC",	"kclr");
295query_tcap(	"&8",	"kund");
296
297query_tcap(	"kb",	"kbs");
298
299query_tcap(	"%1",	"khlp");
300query_tcap(	"#1",	"kHLP");
301
302query_tcap(	"Co",	"colors");
303}
304
305if ( defined ($opt_x) ) {
306	query_extended($opt_x);
307}
308
309if ( defined ($opt_X) ) {
310	if ( defined($opt_c) ) {
311		query_extended("DN");
312		query_extended("UP");
313		query_extended("LFT");
314		query_extended("RIT");
315	}
316	if ( defined($opt_e) ) {
317		query_extended("DC");
318		query_extended("END");
319		query_extended("HOM");
320		query_extended("IC");
321		query_extended("NXT");
322		query_extended("PRV");
323	}
324}
325