<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/env perl
# $XTermId: tcapquery.pl,v 1.29 2019/04/29 23:27:57 tom Exp $
# -----------------------------------------------------------------------------
# this file is part of xterm
#
# Copyright 2004-2018,2019 by Thomas E. Dickey
#
#                         All Rights Reserved
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
# Except as contained in this notice, the name(s) of the above copyright
# holders shall not be used in advertising or otherwise to promote the
# sale, use or other dealings in this Software without prior written
# authorization.
# -----------------------------------------------------------------------------
# Test the tcap-query option of xterm.

use strict;
use warnings;

use Getopt::Std;
use IO::Handle;

our (
    $opt_a, $opt_b, $opt_c, $opt_e, $opt_f, $opt_i,
    $opt_k, $opt_m, $opt_q, $opt_t, $opt_x, $opt_X
);

our @query_params;
our @query_result;

$Getopt::Std::STANDARD_HELP_VERSION = 1;
&amp;getopts('abcefikmqt:x:X') || die(
    "Usage: $0 [options]\n
Options:\n
  -a      (same as -c -e -f -k -m)
  -b      use both terminfo and termcap (default is termcap)
  -c      cursor-keys
  -e      editing keypad-keys
  -f      function-keys
  -i      use terminfo rather than termcap names
  -k      numeric keypad-keys
  -m      miscellaneous (none of -c, -e, -f, -k)
  -q      quicker results by merging queries
  -t NAME use given NAME for \$TERM, set that in xterm's tcap keyboard
  -x KEY  extended cursor/editing key (terminfo only)
  -X      test all extended cursor- and/or editing-keys (terminfo)
"
);

if (
    not(   defined($opt_c)
        or defined($opt_e)
        or defined($opt_f)
        or defined($opt_k)
        or defined($opt_m)
        or defined($opt_x) )
  )
{
    $opt_a = 1;
}

sub no_reply($) {
    open TTY, "+&lt;/dev/tty" or die("Cannot open /dev/tty\n");
    autoflush TTY 1;
    my $old = `stty -g`;
    system "stty raw -echo min 0 time 5";

    print TTY @_;
    close TTY;
    system "stty $old";
}

sub get_reply($) {
    open TTY, "+&lt;/dev/tty" or die("Cannot open /dev/tty\n");
    autoflush TTY 1;
    my $old = `stty -g`;
    system "stty raw -echo min 0 time 5";

    print TTY @_;
    my $reply = &lt;TTY&gt;;
    close TTY;
    system "stty $old";
    if ( defined $reply ) {
        die("^C received\n") if ( "$reply" eq "\003" );
    }
    return $reply;
}

sub hexified($) {
    my $value  = $_[0];
    my $result = "";
    my $n;

    for ( $n = 0 ; $n &lt; length($value) ; ++$n ) {
        $result .= sprintf( "%02X", ord substr( $value, $n, 1 ) );
    }
    return $result;
}

sub modify_tcap($) {
    my $name  = $_[0];
    my $param = &amp;hexified($name);
    &amp;no_reply( "\x1bP+p" . $param . "\x1b\\" );
}

sub begin_query() {
    @query_params = ();
}

sub add_param($) {
    $query_params[ $#query_params + 1 ] = &amp;hexified( $_[0] );
}

sub finish_query() {
    my $reply = &amp;get_reply( "\x1bP+q" . join( ';', @query_params ) . "\x1b\\" );

    return unless defined $reply;
    if ( $reply =~ /\x1bP1\+r[[:xdigit:]]+=[[:xdigit:]]*.*/ ) {
        my $n;

        $reply =~ s/^\x1bP1\+r//;
        $reply =~ s/\x1b\\//;

        my $result = "";
        my $count  = 0;
        my $state  = 0;
        my $error  = "?";
        for ( $n = 0 ; $n &lt; length($reply) ; ) {
            my $c = substr( $reply, $n, 1 );

            if ( $c eq ';' ) {
                $n += 1;
                printf "%d%s\t%s\n", $count, $error, $result
                  if ( $result ne "" );
                $result = "";
                $state  = 0;
                $error  = "?";
                $count++;
            }
            elsif ( $c eq '=' ) {
                $error = ""
                  if (  $count &lt;= $#query_params
                    and &amp;hexified($result) eq $query_params[$count] );
                $n += 1;
                $result .= $c;
                $state = 1;
            }
            elsif ( $c =~ /[[:punct:]]/ ) {
                $n += 1;
                $result .= $c;
            }
            else {
                my $k = hex substr( $reply, $n, 2 );
                if ( $k == 0x1b ) {
                    $result .= "\\E";
                }
                elsif ( $k == 0x7f ) {
                    $result .= "^?";
                }
                elsif ( $k == 32 ) {
                    $result .= "\\s";
                }
                elsif ( $k &lt; 32 ) {
                    $result .= sprintf( "^%c", $k + 64 );
                }
                elsif ( $k &gt; 128 ) {
                    $result .= sprintf( "\\%03o", $k );
                }
                else {
                    $result .= chr($k);
                }
                $n += 2;
            }
        }
        printf "%d%s\t%s\n", $count, $error, $result if ( $result ne "" );
    }
}

sub query_tcap($$) {
    my $tcap  = shift;
    my $tinfo = shift;

    &amp;begin_query unless ($opt_q);
    &amp;add_param($tcap)  if ( $opt_b or not $opt_i );
    &amp;add_param($tinfo) if ( $opt_b or $opt_i );
    &amp;finish_query unless ($opt_q);
}

# extended-keys are a feature of ncurses 5.0 and later
sub query_extended($) {
    my $name = $_[0];
    my $n;

    $name = "k" . $name if ( $name !~ /^k/ );

    for ( $n = 2 ; $n &lt;= 7 ; ++$n ) {
        my $test = $name;
        $test = $test . $n if ( $n &gt; 2 );
        &amp;query_tcap( $name, $test );
    }
}

&amp;begin_query if ($opt_q);

&amp;query_tcap( "TN", "name" );
if ( defined($opt_t) ) {
    printf "Setting TERM=%s\n", $opt_t;
    &amp;modify_tcap($opt_t);
}

# See xtermcapKeycode()
if ( defined($opt_a) || defined($opt_c) ) {
    &amp;query_tcap( "ku", "kcuu1" );
    &amp;query_tcap( "kd", "kcud1" );
    &amp;query_tcap( "kr", "kcuf1" );
    &amp;query_tcap( "kl", "kcub1" );

    &amp;query_tcap( "kF", "kind" );
    &amp;query_tcap( "kR", "kri" );
    &amp;query_tcap( "%i", "kRIT" );
    &amp;query_tcap( "#4", "kLFT" );
}

if ( defined($opt_a) || defined($opt_e) ) {
    &amp;query_tcap( "kD", "kdch1" );
    &amp;query_tcap( "kI", "kich1" );

    &amp;query_tcap( "kh",  "khome" );
    &amp;query_tcap( "\@7", "kend" );
    &amp;query_tcap( "#2",  "kHOM" );
    &amp;query_tcap( "*7",  "kEND" );

    &amp;query_tcap( "*6",  "kslt" );
    &amp;query_tcap( "#6",  "kSLT" );
    &amp;query_tcap( "\@0", "kfnd" );
    &amp;query_tcap( "*0",  "kFND" );

    &amp;query_tcap( "kN", "knp" );
    &amp;query_tcap( "kP", "kpp" );

    &amp;query_tcap( "%c", "kNXT" );
    &amp;query_tcap( "%e", "kPRV" );
}

if ( defined($opt_a) || defined($opt_f) ) {
    &amp;query_tcap( "k1", "kf1" );
    &amp;query_tcap( "k2", "kf2" );
    &amp;query_tcap( "k3", "kf3" );
    &amp;query_tcap( "k4", "kf4" );
    &amp;query_tcap( "k5", "kf5" );
    &amp;query_tcap( "k6", "kf6" );
    &amp;query_tcap( "k7", "kf7" );
    &amp;query_tcap( "k8", "kf8" );
    &amp;query_tcap( "k9", "kf9" );
    &amp;query_tcap( "k;", "kf10" );
    &amp;query_tcap( "F1", "kf11" );
    &amp;query_tcap( "F2", "kf12" );
    &amp;query_tcap( "F3", "kf13" );
    &amp;query_tcap( "F4", "kf14" );
    &amp;query_tcap( "F5", "kf15" );
    &amp;query_tcap( "F6", "kf16" );
    &amp;query_tcap( "F7", "kf17" );
    &amp;query_tcap( "F8", "kf18" );
    &amp;query_tcap( "F9", "kf19" );
    &amp;query_tcap( "FA", "kf20" );
    &amp;query_tcap( "FB", "kf21" );
    &amp;query_tcap( "FC", "kf22" );
    &amp;query_tcap( "FD", "kf23" );
    &amp;query_tcap( "FE", "kf24" );
    &amp;query_tcap( "FF", "kf25" );
    &amp;query_tcap( "FG", "kf26" );
    &amp;query_tcap( "FH", "kf27" );
    &amp;query_tcap( "FI", "kf28" );
    &amp;query_tcap( "FJ", "kf29" );
    &amp;query_tcap( "FK", "kf30" );
    &amp;query_tcap( "FL", "kf31" );
    &amp;query_tcap( "FM", "kf32" );
    &amp;query_tcap( "FN", "kf33" );
    &amp;query_tcap( "FO", "kf34" );
    &amp;query_tcap( "FP", "kf35" );
    &amp;query_tcap( "FQ", "kf36" );
    &amp;query_tcap( "FR", "kf37" );
    &amp;query_tcap( "FS", "kf38" );
    &amp;query_tcap( "FT", "kf39" );
    &amp;query_tcap( "FU", "kf40" );
    &amp;query_tcap( "FV", "kf41" );
    &amp;query_tcap( "FW", "kf42" );
    &amp;query_tcap( "FX", "kf43" );
    &amp;query_tcap( "FY", "kf44" );
    &amp;query_tcap( "FZ", "kf45" );
    &amp;query_tcap( "Fa", "kf46" );
    &amp;query_tcap( "Fb", "kf47" );
    &amp;query_tcap( "Fc", "kf48" );
    &amp;query_tcap( "Fd", "kf49" );
    &amp;query_tcap( "Fe", "kf50" );
    &amp;query_tcap( "Ff", "kf51" );
    &amp;query_tcap( "Fg", "kf52" );
    &amp;query_tcap( "Fh", "kf53" );
    &amp;query_tcap( "Fi", "kf54" );
    &amp;query_tcap( "Fj", "kf55" );
    &amp;query_tcap( "Fk", "kf56" );
    &amp;query_tcap( "Fl", "kf57" );
    &amp;query_tcap( "Fm", "kf58" );
    &amp;query_tcap( "Fn", "kf59" );
    &amp;query_tcap( "Fo", "kf60" );
    &amp;query_tcap( "Fp", "kf61" );
    &amp;query_tcap( "Fq", "kf62" );
    &amp;query_tcap( "Fr", "kf63" );
}

if ( defined($opt_a) || defined($opt_k) ) {
    &amp;query_tcap( "K1", "ka1" );
    &amp;query_tcap( "K3", "ka3" );
    &amp;query_tcap( "K4", "kc1" );
    &amp;query_tcap( "K5", "kc3" );
}

if ( defined($opt_a) || defined($opt_m) ) {
    &amp;query_tcap( "kB", "kcbt" );
    &amp;query_tcap( "kC", "kclr" );
    &amp;query_tcap( "&amp;8", "kund" );

    &amp;query_tcap( "kb", "kbs" );

    &amp;query_tcap( "%1", "khlp" );
    &amp;query_tcap( "#1", "kHLP" );

    &amp;query_tcap( "Co", "colors" );
    &amp;query_tcap( "Co", "RGB" ) if ($opt_i);
}

if ( defined($opt_x) ) {
    &amp;query_extended($opt_x);
}

if ( defined($opt_X) ) {
    if ( defined($opt_c) ) {
        &amp;query_extended("DN");
        &amp;query_extended("UP");
        &amp;query_extended("LFT");
        &amp;query_extended("RIT");
    }
    if ( defined($opt_e) ) {
        &amp;query_extended("DC");
        &amp;query_extended("END");
        &amp;query_extended("HOM");
        &amp;query_extended("IC");
        &amp;query_extended("NXT");
        &amp;query_extended("PRV");
    }
}

&amp;finish_query if ($opt_q);

1;
</pre></body></html>